Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * common utility functions for cbfstool |
| 3 | * |
| 4 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
| 6 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Uwe Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 24 | #include <libgen.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 25 | #include "common.h" |
| 26 | #include "cbfs.h" |
| 27 | #include "elf.h" |
| 28 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 29 | #define dprintf(x...) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 30 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 31 | uint32_t getfilesize(const char *filename) |
| 32 | { |
| 33 | uint32_t size; |
| 34 | FILE *file = fopen(filename, "rb"); |
| 35 | fseek(file, 0, SEEK_END); |
| 36 | size = ftell(file); |
| 37 | fclose(file); |
| 38 | return size; |
| 39 | } |
| 40 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 41 | void *loadfile(const char *filename, uint32_t * romsize_p, void *content, |
| 42 | int place) |
| 43 | { |
| 44 | FILE *file = fopen(filename, "rb"); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 45 | if (file == NULL) |
| 46 | return NULL; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 47 | fseek(file, 0, SEEK_END); |
| 48 | *romsize_p = ftell(file); |
| 49 | fseek(file, 0, SEEK_SET); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 50 | if (!content) { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 51 | content = malloc(*romsize_p); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 52 | if (!content) { |
| 53 | printf("Could not get %d bytes for file %s\n", |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 54 | *romsize_p, filename); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 55 | exit(1); |
| 56 | } |
| 57 | } else if (place == SEEK_END) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 58 | content -= *romsize_p; |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 59 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 60 | if (!fread(content, *romsize_p, 1, file)) { |
| 61 | printf("failed to read %s\n", filename); |
| 62 | return NULL; |
| 63 | } |
| 64 | fclose(file); |
| 65 | return content; |
| 66 | } |
| 67 | |
| 68 | struct cbfs_header *master_header; |
| 69 | uint32_t phys_start, phys_end, align, romsize; |
| 70 | void *offset; |
| 71 | |
| 72 | void recalculate_rom_geometry(void *romarea) |
| 73 | { |
| 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; |
| 78 | phys_end = |
| 79 | (0 - ntohl(master_header->bootblocksize) - |
| 80 | sizeof(struct cbfs_header)) & 0xffffffff; |
| 81 | align = ntohl(master_header->align); |
| 82 | } |
| 83 | |
| 84 | void *loadrom(const char *filename) |
| 85 | { |
| 86 | void *romarea = loadfile(filename, &romsize, 0, SEEK_SET); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 87 | if (romarea == NULL) |
| 88 | return NULL; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 89 | recalculate_rom_geometry(romarea); |
| 90 | return romarea; |
| 91 | } |
| 92 | |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 93 | int writerom(const char *filename, void *start, uint32_t size) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 94 | { |
| 95 | FILE *file = fopen(filename, "wb"); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 96 | if (!file) { |
| 97 | fprintf(stderr, "Could not open '%s' for writing: ", filename); |
| 98 | perror(""); |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | if (fwrite(start, size, 1, file) != 1) { |
| 103 | fprintf(stderr, "Could not write to '%s': ", filename); |
| 104 | perror(""); |
| 105 | return 1; |
| 106 | } |
| 107 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 108 | fclose(file); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 109 | return 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int cbfs_file_header(uint32_t physaddr) |
| 113 | { |
| 114 | /* maybe improve this test */ |
| 115 | return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0); |
| 116 | } |
| 117 | |
| 118 | struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size) |
| 119 | { |
| 120 | struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 121 | strncpy((char *)(nextfile->magic), "LARCHIVE", 8); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 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); |
| 127 | return nextfile; |
| 128 | } |
| 129 | |
| 130 | int iself(unsigned char *input) |
| 131 | { |
| 132 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; |
| 133 | return !memcmp(ehdr->e_ident, ELFMAG, 4); |
| 134 | } |
| 135 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 136 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 137 | uint32_t type; |
| 138 | const char *name; |
| 139 | } filetypes[] = { |
| 140 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 141 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 142 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 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"}, |
Patrick Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 148 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 149 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 150 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 151 | {CBFS_COMPONENT_NULL, "null"} |
| 152 | }; |
| 153 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 154 | void print_supported_filetypes(void) |
| 155 | { |
| 156 | int i, number = ARRAY_SIZE(filetypes); |
| 157 | |
| 158 | for (i=0; i<number; i++) { |
| 159 | printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
| 160 | if ((i%8) == 7) |
| 161 | printf("\n"); |
| 162 | } |
| 163 | } |
| 164 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 165 | const char *strfiletype(uint32_t number) |
| 166 | { |
| 167 | int i; |
| 168 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 169 | if (filetypes[i].type == number) |
| 170 | return filetypes[i].name; |
| 171 | return "unknown"; |
| 172 | } |
| 173 | |
| 174 | uint64_t intfiletype(const char *name) |
| 175 | { |
| 176 | int i; |
| 177 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 178 | if (strcmp(filetypes[i].name, name) == 0) |
| 179 | return filetypes[i].type; |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | void print_cbfs_directory(const char *filename) |
| 184 | { |
| 185 | printf |
| 186 | ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n", |
Uwe Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 187 | basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize), |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 188 | romsize, ntohl(master_header->offset), align); |
| 189 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 190 | uint32_t current = phys_start; |
| 191 | while (current < phys_end) { |
| 192 | if (!cbfs_file_header(current)) { |
| 193 | current += align; |
| 194 | continue; |
| 195 | } |
| 196 | struct cbfs_file *thisfile = |
| 197 | (struct cbfs_file *)phys_to_virt(current); |
| 198 | uint32_t length = ntohl(thisfile->len); |
Maciej Pijanka | f44eb78 | 2010-01-07 21:37:18 +0000 | [diff] [blame] | 199 | char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 200 | if (strlen(fname) == 0) |
Maciej Pijanka | f44eb78 | 2010-01-07 21:37:18 +0000 | [diff] [blame] | 201 | fname = "(empty)"; |
| 202 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 203 | printf("%-30s 0x%-8x %-12s %d\n", fname, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 204 | current - phys_start, strfiletype(ntohl(thisfile->type)), |
| 205 | length); |
| 206 | current = |
| 207 | ALIGN(current + ntohl(thisfile->len) + |
| 208 | ntohl(thisfile->offset), align); |
| 209 | } |
| 210 | } |
| 211 | |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 212 | int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath) |
| 213 | { |
| 214 | // Identify the coreboot image. |
| 215 | printf( |
| 216 | "%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n", |
| 217 | basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize), |
| 218 | romsize, ntohl(master_header->offset), align); |
| 219 | |
| 220 | FILE *outfile = NULL; |
| 221 | uint32_t current = phys_start; |
| 222 | while (current < phys_end) { |
| 223 | if (!cbfs_file_header(current)) { |
| 224 | current += align; |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | // Locate the file start struct |
| 229 | struct cbfs_file *thisfile = |
| 230 | (struct cbfs_file *)phys_to_virt(current); |
| 231 | // And its length |
| 232 | uint32_t length = ntohl(thisfile->len); |
| 233 | // Locate the file name |
| 234 | char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 235 | |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 236 | // It's not the file we are looking for.. |
| 237 | if (strcmp(fname, payloadname) != 0) |
| 238 | { |
| 239 | current = |
| 240 | ALIGN(current + ntohl(thisfile->len) + |
| 241 | ntohl(thisfile->offset), align); |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | // Else, it's our file. |
Peter Stuge | 441426b | 2011-01-17 05:08:32 +0000 | [diff] [blame] | 246 | printf("Found file %.30s at 0x%x, type %.12s, size %d\n", fname, |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 247 | current - phys_start, strfiletype(ntohl(thisfile->type)), |
| 248 | length); |
| 249 | |
| 250 | // If we are not dumping to stdout, open the out file. |
| 251 | outfile = fopen(outpath, "wb"); |
| 252 | if (!outfile) |
| 253 | { |
| 254 | printf("Could not open the file %s for writing. Aborting.\n", outpath); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW) |
| 259 | { |
| 260 | printf("Warning: only 'raw' files are safe to extract.\n"); |
| 261 | } |
| 262 | |
| 263 | fwrite(((char *)thisfile) |
| 264 | + ntohl(thisfile->offset), length, 1, outfile); |
| 265 | |
| 266 | fclose(outfile); |
Peter Stuge | 441426b | 2011-01-17 05:08:32 +0000 | [diff] [blame] | 267 | printf("Successfully dumped the file.\n"); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 268 | |
| 269 | // We'll only dump one file. |
| 270 | return 0; |
| 271 | } |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 272 | printf("File %s not found.\n", payloadname); |
| 273 | return 1; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 277 | int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location) |
| 278 | { |
| 279 | uint32_t current = phys_start; |
| 280 | while (current < phys_end) { |
| 281 | if (!cbfs_file_header(current)) { |
| 282 | current += align; |
| 283 | continue; |
| 284 | } |
| 285 | struct cbfs_file *thisfile = |
| 286 | (struct cbfs_file *)phys_to_virt(current); |
| 287 | uint32_t length = ntohl(thisfile->len); |
| 288 | |
| 289 | dprintf("at %x, %x bytes\n", current, length); |
| 290 | /* Is this a free chunk? */ |
| 291 | if ((thisfile->type == CBFS_COMPONENT_DELETED) |
| 292 | || (thisfile->type == CBFS_COMPONENT_NULL)) { |
| 293 | dprintf("null||deleted at %x, %x bytes\n", current, |
| 294 | length); |
| 295 | /* if this is the right size, and if specified, the right location, use it */ |
| 296 | if ((contentsize <= length) |
| 297 | && ((location == 0) || (current == location))) { |
| 298 | if (contentsize < length) { |
| 299 | dprintf |
| 300 | ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n", |
| 301 | length, contentsize, |
| 302 | ALIGN(current + contentsize, |
| 303 | align), |
| 304 | length - contentsize); |
| 305 | uint32_t start = |
| 306 | ALIGN(current + contentsize, align); |
| 307 | uint32_t size = |
| 308 | current + ntohl(thisfile->offset) |
| 309 | + length - start - 16 - |
| 310 | sizeof(struct cbfs_file); |
| 311 | cbfs_create_empty_file(start, size); |
| 312 | } |
| 313 | dprintf("copying data\n"); |
| 314 | memcpy(phys_to_virt(current), content, |
| 315 | contentsize); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 316 | return 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 317 | } |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 318 | if (location != 0) { |
| 319 | /* CBFS has the constraint that the chain always moves up in memory. so once |
| 320 | we're past the place we seek, we don't need to look any further */ |
| 321 | if (current > location) { |
| 322 | printf |
| 323 | ("the requested space is not available\n"); |
| 324 | return 1; |
| 325 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 326 | |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 327 | /* Is the requested location inside the current chunk? */ |
| 328 | if ((current < location) |
| 329 | && ((location + contentsize) <= |
| 330 | (current + length))) { |
| 331 | /* Split it up. In the next iteration the code will be at the right place. */ |
| 332 | dprintf("split up. new length: %x\n", |
| 333 | location - current - |
| 334 | ntohl(thisfile->offset)); |
| 335 | thisfile->len = |
| 336 | htonl(location - current - |
| 337 | ntohl(thisfile->offset)); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 338 | cbfs_create_empty_file(location, |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 339 | length - |
| 340 | (location - |
| 341 | current)); |
| 342 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | current = |
| 346 | ALIGN(current + ntohl(thisfile->len) + |
| 347 | ntohl(thisfile->offset), align); |
| 348 | } |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 349 | printf("Could not add the file to CBFS, it's probably too big.\n"); |
Uwe Hermann | 6a3ca4e | 2009-11-12 20:06:32 +0000 | [diff] [blame] | 350 | printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 351 | return 1; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 354 | |
| 355 | static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first, |
| 356 | struct cbfs_file *second) |
| 357 | { |
| 358 | uint32_t new_length = |
| 359 | ntohl(first->len) + ntohl(second->len) + ntohl(second->offset); |
| 360 | first->len = htonl(new_length); |
| 361 | first->checksum = 0; // FIXME? |
| 362 | return first; |
| 363 | } |
| 364 | |
| 365 | static struct cbfs_file *next_file(struct cbfs_file *prev) |
| 366 | { |
| 367 | uint32_t pos = (prev == NULL) ? phys_start : |
| 368 | ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset), |
| 369 | align); |
| 370 | |
| 371 | for (; pos < phys_end; pos += align) { |
| 372 | if (cbfs_file_header(pos)) |
| 373 | return (struct cbfs_file *)phys_to_virt(pos); |
| 374 | } |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | int remove_file_from_cbfs(const char *filename) |
| 380 | { |
| 381 | struct cbfs_file *prev = NULL; |
| 382 | struct cbfs_file *cur = next_file(prev); |
| 383 | struct cbfs_file *next = next_file(cur); |
| 384 | for (; cur; prev = cur, cur = next, next = next_file(next)) { |
| 385 | |
| 386 | /* Check if this is the file to remove. */ |
| 387 | char *name = (char *)cur + sizeof(*cur); |
| 388 | if (strcmp(name, filename)) |
| 389 | continue; |
| 390 | |
| 391 | /* Mark the file as free space and erase its name. */ |
| 392 | cur->type = CBFS_COMPONENT_NULL; |
| 393 | name[0] = '\0'; |
| 394 | |
| 395 | /* Merge it with the previous file if possible. */ |
| 396 | if (prev && prev->type == CBFS_COMPONENT_NULL) |
| 397 | cur = merge_adjacent_files(prev, cur); |
| 398 | |
| 399 | /* Merge it with the next file if possible. */ |
| 400 | if (next && next->type == CBFS_COMPONENT_NULL) |
| 401 | merge_adjacent_files(cur, next); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | printf("CBFS file %s not found.\n", filename); |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 410 | /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns |
| 411 | the new location that points to the cbfs_file header */ |
| 412 | void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, |
| 413 | uint32_t type, uint32_t * location) |
| 414 | { |
| 415 | uint32_t filename_len = ALIGN(strlen(filename) + 1, 16); |
| 416 | uint32_t headersize = sizeof(struct cbfs_file) + filename_len; |
| 417 | if ((location != 0) && (*location != 0)) { |
| 418 | uint32_t offset = *location % align; |
| 419 | /* If offset >= (headersize % align), we can stuff the header into the offset. |
| 420 | Otherwise the header has to be aligned itself, and put before the offset data */ |
| 421 | if (offset >= (headersize % align)) { |
| 422 | offset -= (headersize % align); |
| 423 | } else { |
| 424 | offset += align - (headersize % align); |
| 425 | } |
| 426 | headersize += offset; |
| 427 | *location -= headersize; |
| 428 | } |
| 429 | void *newdata = malloc(*datasize + headersize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 430 | if (!newdata) { |
| 431 | printf("Could not get %d bytes for CBFS file.\n", *datasize + |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 432 | headersize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 433 | exit(1); |
| 434 | } |
Peter Stuge | f4aca1d | 2009-12-06 12:14:39 +0000 | [diff] [blame] | 435 | memset(newdata, 0xff, *datasize + headersize); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 436 | struct cbfs_file *nextfile = (struct cbfs_file *)newdata; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 437 | strncpy((char *)(nextfile->magic), "LARCHIVE", 8); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 438 | nextfile->len = htonl(*datasize); |
| 439 | nextfile->type = htonl(type); |
| 440 | nextfile->checksum = 0; // FIXME? |
| 441 | nextfile->offset = htonl(headersize); |
| 442 | strcpy(newdata + sizeof(struct cbfs_file), filename); |
| 443 | memcpy(newdata + headersize, data, *datasize); |
| 444 | *datasize += headersize; |
| 445 | return newdata; |
| 446 | } |
| 447 | |
| 448 | int create_cbfs_image(const char *romfile, uint32_t _romsize, |
| 449 | const char *bootblock, uint32_t align) |
| 450 | { |
| 451 | romsize = _romsize; |
| 452 | unsigned char *romarea = malloc(romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 453 | if (!romarea) { |
| 454 | printf("Could not get %d bytes of memory for CBFS image.\n", |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 455 | romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 456 | exit(1); |
| 457 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 458 | memset(romarea, 0xff, romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 459 | |
| 460 | // Set up physical/virtual mapping |
| 461 | offset = romarea + romsize - 0x100000000ULL; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 462 | |
| 463 | if (align == 0) |
| 464 | align = 64; |
| 465 | |
| 466 | uint32_t bootblocksize = 0; |
| 467 | loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END); |
| 468 | struct cbfs_header *master_header = |
| 469 | (struct cbfs_header *)(romarea + romsize - bootblocksize - |
| 470 | sizeof(struct cbfs_header)); |
| 471 | master_header->magic = ntohl(0x4f524243); |
| 472 | master_header->version = ntohl(0x31313131); |
| 473 | master_header->romsize = htonl(romsize); |
| 474 | master_header->bootblocksize = htonl(bootblocksize); |
| 475 | master_header->align = htonl(align); |
| 476 | master_header->offset = htonl(0); |
| 477 | ((uint32_t *) phys_to_virt(0xfffffffc))[0] = |
| 478 | virt_to_phys(master_header); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 479 | |
| 480 | recalculate_rom_geometry(romarea); |
| 481 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 482 | cbfs_create_empty_file((0 - romsize) & 0xffffffff, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 483 | romsize - bootblocksize - |
| 484 | sizeof(struct cbfs_header) - |
| 485 | sizeof(struct cbfs_file) - 16); |
| 486 | |
| 487 | writerom(romfile, romarea, romsize); |
| 488 | return 0; |
| 489 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 490 | |
| 491 | static int in_segment(int addr, int size, int gran) |
| 492 | { |
| 493 | return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1))); |
| 494 | } |
| 495 | |
| 496 | uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, |
| 497 | const char *filename, uint32_t alignment) |
| 498 | { |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 499 | loadrom(romfile); |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 500 | int filename_size = strlen(filename); |
| 501 | |
| 502 | int headersize = |
| 503 | sizeof(struct cbfs_file) + ALIGN(filename_size + 1, |
| 504 | 16) + sizeof(struct cbfs_stage); |
| 505 | int totalsize = headersize + filesize; |
| 506 | |
| 507 | uint32_t current = phys_start; |
| 508 | while (current < phys_end) { |
| 509 | if (!cbfs_file_header(current)) { |
| 510 | current += align; |
| 511 | continue; |
| 512 | } |
| 513 | struct cbfs_file *thisfile = |
| 514 | (struct cbfs_file *)phys_to_virt(current); |
| 515 | |
| 516 | uint32_t top = |
| 517 | current + ntohl(thisfile->len) + ntohl(thisfile->offset); |
| 518 | if (((ntohl(thisfile->type) == 0x0) |
| 519 | || (ntohl(thisfile->type) == 0xffffffff)) |
| 520 | && (ntohl(thisfile->len) + ntohl(thisfile->offset) >= |
| 521 | totalsize)) { |
| 522 | if (in_segment |
| 523 | (current + headersize, filesize, alignment)) |
| 524 | return current + headersize; |
| 525 | if ((ALIGN(current, alignment) + filesize < top) |
| 526 | && (ALIGN(current, alignment) - headersize > |
| 527 | current) |
| 528 | && in_segment(ALIGN(current, alignment), filesize, |
| 529 | alignment)) |
| 530 | return ALIGN(current, alignment); |
| 531 | if ((ALIGN(current, alignment) + alignment + filesize < |
| 532 | top) |
| 533 | && (ALIGN(current, alignment) + alignment - |
| 534 | headersize > current) |
| 535 | && in_segment(ALIGN(current, alignment) + alignment, |
| 536 | filesize, alignment)) |
| 537 | return ALIGN(current, alignment) + alignment; |
| 538 | } |
| 539 | current = |
| 540 | ALIGN(current + ntohl(thisfile->len) + |
| 541 | ntohl(thisfile->offset), align); |
| 542 | } |
| 543 | return 0; |
| 544 | } |