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