4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
28 int get_size(const char *size)
31 int val = strtoul(size, &next, 0);
33 /* Support modifiers for the size kK and mM for kbytes and
34 mbytes respectfully */
37 if (*next == 'k' || *next == 'K')
39 else if (*next == 'm' || *next == 'M')
46 int copy_from_fd(int fd, void *ptr, int size)
48 unsigned char *p = ptr;
51 int ret = read(fd, p, size);
54 ERROR("Error while reading: %m\n");
59 ERROR("Unexpected end of file\n");
70 int size_and_open(const char *filename, unsigned int *size)
74 int fd = open(filename, O_RDONLY);
77 ERROR("Unable to open %s: %m\n", filename);
82 ERROR("Unable to stat %s: %m\n", filename);
91 int map_rom(struct rom *rom, int size)
93 rom->ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
96 if (rom->ptr == MAP_FAILED) {
97 ERROR("Could not map the rom: %m\n");
105 int open_rom(struct rom *rom, const char *filename)
108 unsigned long offset;
110 if (stat(filename, &s)) {
111 ERROR("Could not stat %s: %m\n", filename);
115 rom->fd = open(filename, O_RDWR);
118 ERROR("Could not open %s: %m\n", filename);
123 if (map_rom(rom, s.st_size))
126 /* Find the master header */
128 offset = ROM_READL(rom, s.st_size - 4);
130 rom->header = (struct romfs_header *)
131 ROM_PTR(rom, s.st_size - (0xFFFFFFFF - offset) - 1);
133 if (ntohl(rom->header->magic) != HEADER_MAGIC) {
134 ERROR("This does not appear to be a valid ROM\n");
138 /* Check that the alignment is correct */
139 if (ntohl(rom->header->align) == 0) {
140 ERROR("The alignment in the ROM is 0 - probably malformed\n");
144 /* Sanity check that the size matches the file size */
146 if (ntohl(rom->header->romsize) != s.st_size) {
147 ERROR("The ROM size in the header does not match the file\n");
148 ERROR("ROM size is %d bytes, file size is %d bytes\n",
149 ntohl(rom->header->romsize), (unsigned int)s.st_size);
153 rom->size = ntohl(rom->header->romsize);
154 rom->fssize = rom->size - ntohl(rom->header->bootblocksize);
159 if (rom->ptr != NULL)
160 munmap(rom->ptr, s.st_size);
170 int create_rom(struct rom *rom, const unsigned char *filename,
171 int romsize, int bootblocksize, int align)
173 unsigned char null = '\0';
176 ERROR("%s already exists - cannot create\n", filename);
180 /* Remember the size of the entire ROM */
183 /* The size of the archive section is everything but the bootblock */
184 rom->fssize = romsize - bootblocksize;
186 /* Open the file descriptor */
188 rom->fd = open((char *)filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
191 ERROR("Could not create %s: %m\n", filename);
195 /* Size the new file appropriately */
196 lseek(rom->fd, romsize - 1, SEEK_SET);
197 write(rom->fd, &null, 1);
199 if (map_rom(rom, romsize)) {
204 /* Clear the reset vector */
205 memset(rom->ptr + rom->size - 16, 0, 16);
207 ROM_WRITEL(rom, rom->size - 4,
208 0xFFFFFFF0 - sizeof(struct romfs_header));
210 /* This is a pointer to the header for easy access */
211 rom->header = (struct romfs_header *)
212 ROM_PTR(rom, rom->size - 16 - sizeof(struct romfs_header));
214 rom->header->magic = htonl(HEADER_MAGIC);
215 rom->header->romsize = htonl(romsize);
216 rom->header->bootblocksize = htonl(bootblocksize);
217 rom->header->align = htonl(align);
218 rom->header->offset = htonl(0);
223 int add_bootblock(struct rom *rom, const char *filename)
226 //unsigned int offset;
227 int fd = size_and_open(filename, &size);
229 struct romfs_header tmp;
234 if (size > ntohl(rom->header->bootblocksize)) {
235 ERROR("The bootblock size is not correct (%d vs %d)\n",
236 size, ntohl(rom->header->bootblocksize));
240 /* Copy the current header into a temporary buffer */
241 memcpy(&tmp, rom->header, sizeof(struct romfs_header));
243 /* Copy the bootblock into place at the end of the file */
245 ret = copy_from_fd(fd, ROM_PTR(rom, rom->size - ntohl(rom->header->bootblocksize)), size);
250 ERROR("Unable to add %s to the bootblock\n", filename);
254 /* FIXME: This should point to a location defined by coreboot */
256 ROM_WRITEL(rom, rom->size - 4,
257 0xFFFFFFF0 - sizeof(struct romfs_header));
259 /* This is a pointer to the header for easy access */
260 rom->header = (struct romfs_header *)
261 ROM_PTR(rom, rom->size - 16 - sizeof(struct romfs_header));
264 /* Figure out the new location for the header */
265 offset = ROM_READL(rom, rom->size - 4);
267 rom->header = (struct romfs_header *)
268 ROM_PTR(rom, offset - (0xFFFFFFFF - rom->size));
271 /* Replace the LAR header */
272 memcpy(rom->header, &tmp, sizeof(struct romfs_header));
277 int rom_exists(struct rom *rom)