2 * common utility functions for cbfstool
4 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
31 uint32_t getfilesize(const char *filename)
34 FILE *file = fopen(filename, "rb");
35 fseek(file, 0, SEEK_END);
41 void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
44 FILE *file = fopen(filename, "rb");
47 fseek(file, 0, SEEK_END);
48 *romsize_p = ftell(file);
49 fseek(file, 0, SEEK_SET);
51 content = malloc(*romsize_p);
53 printf("Could not get %d bytes for file %s\n",
54 *romsize_p, filename);
57 } else if (place == SEEK_END)
58 content -= *romsize_p;
60 if (!fread(content, *romsize_p, 1, file)) {
61 printf("failed to read %s\n", filename);
68 struct cbfs_header *master_header;
69 uint32_t phys_start, phys_end, align, romsize;
72 void recalculate_rom_geometry(void *romarea)
74 offset = romarea + romsize - 0x100000000ULL;
75 master_header = (struct cbfs_header *)
76 phys_to_virt(*((uint32_t *) phys_to_virt(0xfffffffc)));
77 phys_start = (0 - romsize + ntohl(master_header->offset)) & 0xffffffff;
79 (0 - ntohl(master_header->bootblocksize) -
80 sizeof(struct cbfs_header)) & 0xffffffff;
81 align = ntohl(master_header->align);
84 void *loadrom(const char *filename)
86 void *romarea = loadfile(filename, &romsize, 0, SEEK_SET);
89 recalculate_rom_geometry(romarea);
93 int writerom(const char *filename, void *start, uint32_t size)
95 FILE *file = fopen(filename, "wb");
97 fprintf(stderr, "Could not open '%s' for writing: ", filename);
102 if (fwrite(start, size, 1, file) != 1) {
103 fprintf(stderr, "Could not write to '%s': ", filename);
112 int cbfs_file_header(uint32_t physaddr)
114 /* maybe improve this test */
115 return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0);
118 struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size)
120 struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr);
121 strncpy(nextfile->magic, "LARCHIVE", 8);
122 nextfile->len = htonl(size);
123 nextfile->type = htonl(0xffffffff);
124 nextfile->checksum = 0; // FIXME?
125 nextfile->offset = htonl(sizeof(struct cbfs_file) + 16);
126 memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16);
130 int iself(unsigned char *input)
132 Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input;
133 return !memcmp(ehdr->e_ident, ELFMAG, 4);
140 {CBFS_COMPONENT_STAGE, "stage"},
141 {CBFS_COMPONENT_PAYLOAD, "payload"},
142 {CBFS_COMPONENT_OPTIONROM, "optionrom"},
143 {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"},
144 {CBFS_COMPONENT_RAW, "raw"},
145 {CBFS_COMPONENT_VSA, "vsa"},
146 {CBFS_COMPONENT_MBI, "mbi"},
147 {CBFS_COMPONENT_MICROCODE, "microcode"},
148 {CBFS_COMPONENT_DELETED, "deleted"},
149 {CBFS_COMPONENT_NULL, "null"}
152 void print_supported_filetypes(void)
154 int i, number = ARRAY_SIZE(filetypes);
156 for (i=0; i<number; i++) {
157 printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
163 const char *strfiletype(uint32_t number)
166 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
167 if (filetypes[i].type == number)
168 return filetypes[i].name;
172 uint64_t intfiletype(const char *name)
175 for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++)
176 if (strcmp(filetypes[i].name, name) == 0)
177 return filetypes[i].type;
181 void print_cbfs_directory(const char *filename)
184 ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
185 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
186 romsize, ntohl(master_header->offset), align);
187 printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type");
188 uint32_t current = phys_start;
189 while (current < phys_end) {
190 if (!cbfs_file_header(current)) {
194 struct cbfs_file *thisfile =
195 (struct cbfs_file *)phys_to_virt(current);
196 uint32_t length = ntohl(thisfile->len);
197 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
198 if (strlen(fname) == 0)
201 printf("%-30s 0x%-8x %-12s %d\n", fname,
202 current - phys_start, strfiletype(ntohl(thisfile->type)),
205 ALIGN(current + ntohl(thisfile->len) +
206 ntohl(thisfile->offset), align);
210 int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath)
212 // Identify the coreboot image.
214 "%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n",
215 basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize),
216 romsize, ntohl(master_header->offset), align);
218 FILE *outfile = NULL;
219 uint32_t current = phys_start;
220 while (current < phys_end) {
221 if (!cbfs_file_header(current)) {
226 // Locate the file start struct
227 struct cbfs_file *thisfile =
228 (struct cbfs_file *)phys_to_virt(current);
230 uint32_t length = ntohl(thisfile->len);
231 // Locate the file name
232 char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file));
233 // It's not the file we are looking for..
234 if (strcmp(fname, payloadname) != 0)
237 ALIGN(current + ntohl(thisfile->len) +
238 ntohl(thisfile->offset), align);
242 // Else, it's our file.
243 printf("Found %.30s payload at 0x%x, type %.12s, size %d\n", fname,
244 current - phys_start, strfiletype(ntohl(thisfile->type)),
247 // If we are not dumping to stdout, open the out file.
248 outfile = fopen(outpath, "wb");
251 printf("Could not open the file %s for writing. Aborting.\n", outpath);
255 if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW)
257 printf("Warning: only 'raw' files are safe to extract.\n");
260 fwrite(((char *)thisfile)
261 + ntohl(thisfile->offset), length, 1, outfile);
264 printf("Successfully dumped the payload.\n");
266 // We'll only dump one file.
273 int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
275 uint32_t current = phys_start;
276 while (current < phys_end) {
277 if (!cbfs_file_header(current)) {
281 struct cbfs_file *thisfile =
282 (struct cbfs_file *)phys_to_virt(current);
283 uint32_t length = ntohl(thisfile->len);
285 dprintf("at %x, %x bytes\n", current, length);
286 /* Is this a free chunk? */
287 if ((thisfile->type == CBFS_COMPONENT_DELETED)
288 || (thisfile->type == CBFS_COMPONENT_NULL)) {
289 dprintf("null||deleted at %x, %x bytes\n", current,
291 /* if this is the right size, and if specified, the right location, use it */
292 if ((contentsize <= length)
293 && ((location == 0) || (current == location))) {
294 if (contentsize < length) {
296 ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
298 ALIGN(current + contentsize,
300 length - contentsize);
302 ALIGN(current + contentsize, align);
304 current + ntohl(thisfile->offset)
305 + length - start - 16 -
306 sizeof(struct cbfs_file);
307 cbfs_create_empty_file(start, size);
309 dprintf("copying data\n");
310 memcpy(phys_to_virt(current), content,
315 /* CBFS has the constraint that the chain always moves up in memory. so once
316 we're past the place we seek, we don't need to look any further */
317 if (current > location) {
319 ("the requested space is not available\n");
323 /* Is the requested location inside the current chunk? */
324 if ((current < location)
325 && ((location + contentsize) <=
326 (current + length))) {
327 /* Split it up. In the next iteration the code will be at the right place. */
328 dprintf("split up. new length: %x\n",
330 ntohl(thisfile->offset));
332 htonl(location - current -
333 ntohl(thisfile->offset));
334 struct cbfs_file *nextfile =
335 cbfs_create_empty_file(location,
343 ALIGN(current + ntohl(thisfile->len) +
344 ntohl(thisfile->offset), align);
346 printf("Could not add the file to CBFS, it's probably too big.\n");
347 printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
351 /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns
352 the new location that points to the cbfs_file header */
353 void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
354 uint32_t type, uint32_t * location)
356 uint32_t filename_len = ALIGN(strlen(filename) + 1, 16);
357 uint32_t headersize = sizeof(struct cbfs_file) + filename_len;
358 if ((location != 0) && (*location != 0)) {
359 uint32_t offset = *location % align;
360 /* If offset >= (headersize % align), we can stuff the header into the offset.
361 Otherwise the header has to be aligned itself, and put before the offset data */
362 if (offset >= (headersize % align)) {
363 offset -= (headersize % align);
365 offset += align - (headersize % align);
367 headersize += offset;
368 *location -= headersize;
370 void *newdata = malloc(*datasize + headersize);
372 printf("Could not get %d bytes for CBFS file.\n", *datasize +
376 memset(newdata, 0xff, *datasize + headersize);
377 struct cbfs_file *nextfile = (struct cbfs_file *)newdata;
378 strncpy(nextfile->magic, "LARCHIVE", 8);
379 nextfile->len = htonl(*datasize);
380 nextfile->type = htonl(type);
381 nextfile->checksum = 0; // FIXME?
382 nextfile->offset = htonl(headersize);
383 strcpy(newdata + sizeof(struct cbfs_file), filename);
384 memcpy(newdata + headersize, data, *datasize);
385 *datasize += headersize;
389 int create_cbfs_image(const char *romfile, uint32_t _romsize,
390 const char *bootblock, uint32_t align)
393 unsigned char *romarea = malloc(romsize);
395 printf("Could not get %d bytes of memory for CBFS image.\n",
399 memset(romarea, 0xff, romsize);
401 // Set up physical/virtual mapping
402 offset = romarea + romsize - 0x100000000ULL;
407 uint32_t bootblocksize = 0;
408 loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END);
409 struct cbfs_header *master_header =
410 (struct cbfs_header *)(romarea + romsize - bootblocksize -
411 sizeof(struct cbfs_header));
412 master_header->magic = ntohl(0x4f524243);
413 master_header->version = ntohl(0x31313131);
414 master_header->romsize = htonl(romsize);
415 master_header->bootblocksize = htonl(bootblocksize);
416 master_header->align = htonl(align);
417 master_header->offset = htonl(0);
418 ((uint32_t *) phys_to_virt(0xfffffffc))[0] =
419 virt_to_phys(master_header);
421 recalculate_rom_geometry(romarea);
423 struct cbfs_file *one_empty_file =
424 cbfs_create_empty_file((0 - romsize) & 0xffffffff,
425 romsize - bootblocksize -
426 sizeof(struct cbfs_header) -
427 sizeof(struct cbfs_file) - 16);
429 writerom(romfile, romarea, romsize);
433 static int in_segment(int addr, int size, int gran)
435 return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1)));
438 uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
439 const char *filename, uint32_t alignment)
441 void *rom = loadrom(romfile);
442 int filename_size = strlen(filename);
445 sizeof(struct cbfs_file) + ALIGN(filename_size + 1,
446 16) + sizeof(struct cbfs_stage);
447 int totalsize = headersize + filesize;
449 uint32_t current = phys_start;
450 while (current < phys_end) {
451 if (!cbfs_file_header(current)) {
455 struct cbfs_file *thisfile =
456 (struct cbfs_file *)phys_to_virt(current);
459 current + ntohl(thisfile->len) + ntohl(thisfile->offset);
460 if (((ntohl(thisfile->type) == 0x0)
461 || (ntohl(thisfile->type) == 0xffffffff))
462 && (ntohl(thisfile->len) + ntohl(thisfile->offset) >=
465 (current + headersize, filesize, alignment))
466 return current + headersize;
467 if ((ALIGN(current, alignment) + filesize < top)
468 && (ALIGN(current, alignment) - headersize >
470 && in_segment(ALIGN(current, alignment), filesize,
472 return ALIGN(current, alignment);
473 if ((ALIGN(current, alignment) + alignment + filesize <
475 && (ALIGN(current, alignment) + alignment -
476 headersize > current)
477 && in_segment(ALIGN(current, alignment) + alignment,
478 filesize, alignment))
479 return ALIGN(current, alignment) + alignment;
482 ALIGN(current + ntohl(thisfile->len) +
483 ntohl(thisfile->offset), align);