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> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 6 | * Copyright (C) 2012 Google, Inc. |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Uwe Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 25 | #include <libgen.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 26 | #include "common.h" |
| 27 | #include "cbfs.h" |
| 28 | #include "elf.h" |
| 29 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 30 | #define dprintf(x...) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 31 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 32 | size_t getfilesize(const char *filename) |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 33 | { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 34 | size_t size; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 35 | FILE *file = fopen(filename, "rb"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 36 | if (file == NULL) |
| 37 | return -1; |
| 38 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 39 | fseek(file, 0, SEEK_END); |
| 40 | size = ftell(file); |
| 41 | fclose(file); |
| 42 | return size; |
| 43 | } |
| 44 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 45 | void *loadfile(const char *filename, uint32_t * romsize_p, void *content, |
| 46 | int place) |
| 47 | { |
| 48 | FILE *file = fopen(filename, "rb"); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 49 | if (file == NULL) |
| 50 | return NULL; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 51 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 52 | fseek(file, 0, SEEK_END); |
| 53 | *romsize_p = ftell(file); |
| 54 | fseek(file, 0, SEEK_SET); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 55 | if (!content) { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 56 | content = malloc(*romsize_p); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 57 | if (!content) { |
| 58 | printf("Could not get %d bytes for file %s\n", |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 59 | *romsize_p, filename); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 60 | exit(1); |
| 61 | } |
| 62 | } else if (place == SEEK_END) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 63 | content -= *romsize_p; |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 64 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 65 | if (!fread(content, *romsize_p, 1, file)) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 66 | printf("Failed to read %s\n", filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | fclose(file); |
| 70 | return content; |
| 71 | } |
| 72 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 73 | static struct cbfs_header *master_header; |
| 74 | static uint32_t phys_start, phys_end, align; |
| 75 | uint32_t romsize; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 76 | void *offset; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 77 | uint32_t arch = CBFS_ARCHITECTURE_UNKNOWN; |
| 78 | |
| 79 | static struct { |
| 80 | uint32_t arch; |
| 81 | const char *name; |
| 82 | } arch_names[] = { |
| 83 | { CBFS_ARCHITECTURE_ARMV7, "armv7" }, |
| 84 | { CBFS_ARCHITECTURE_X86, "x86" }, |
| 85 | { CBFS_ARCHITECTURE_UNKNOWN, "unknown" } |
| 86 | }; |
| 87 | |
| 88 | uint32_t string_to_arch(const char *arch_string) |
| 89 | { |
| 90 | int i; |
| 91 | uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN; |
| 92 | |
| 93 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 94 | if (!strcasecmp(arch_string, arch_names[i].name)) { |
| 95 | ret = arch_names[i].arch; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | const char *arch_to_string(uint32_t a) |
| 104 | { |
| 105 | int i; |
| 106 | const char *ret = NULL; |
| 107 | |
| 108 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 109 | if (a == arch_names[i].arch) { |
| 110 | ret = arch_names[i].name; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return ret; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | int find_master_header(void *romarea, size_t size) |
| 120 | { |
| 121 | size_t offset; |
| 122 | |
| 123 | if (master_header) |
| 124 | return 0; |
| 125 | |
| 126 | for (offset = 0; offset < size - sizeof(struct cbfs_header); offset++) { |
| 127 | struct cbfs_header *tmp = romarea + offset; |
| 128 | |
| 129 | if (tmp->magic == ntohl(CBFS_HEADER_MAGIC)) { |
| 130 | master_header = tmp; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return master_header ? 0 : 1; |
| 136 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 137 | |
| 138 | void recalculate_rom_geometry(void *romarea) |
| 139 | { |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 140 | if (find_master_header(romarea, romsize)) { |
| 141 | fprintf(stderr, "E: Cannot find master header\n"); |
| 142 | exit(1); |
| 143 | } |
| 144 | |
| 145 | /* Update old headers */ |
| 146 | if (master_header->version == VERSION1 && |
| 147 | ntohl(master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) { |
| 148 | printf("Updating CBFS master header to version 2\n"); |
| 149 | master_header->architecture = htonl(CBFS_ARCHITECTURE_X86); |
| 150 | } |
| 151 | |
| 152 | arch = ntohl(master_header->architecture); |
| 153 | |
| 154 | switch (arch) { |
| 155 | case CBFS_ARCHITECTURE_ARMV7: |
| 156 | offset = romarea; |
| 157 | phys_start = (0 + ntohl(master_header->offset)) & 0xffffffff; |
| 158 | phys_end = romsize & 0xffffffff; |
| 159 | break; |
| 160 | case CBFS_ARCHITECTURE_X86: |
| 161 | offset = romarea + romsize - 0x100000000ULL; |
| 162 | phys_start = (0 - romsize + ntohl(master_header->offset)) & |
| 163 | 0xffffffff; |
| 164 | phys_end = (0 - ntohl(master_header->bootblocksize) - |
| 165 | sizeof(struct cbfs_header)) & 0xffffffff; |
| 166 | break; |
| 167 | default: |
| 168 | fprintf(stderr, "E: Unknown architecture\n"); |
| 169 | exit(1); |
| 170 | } |
| 171 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 172 | align = ntohl(master_header->align); |
| 173 | } |
| 174 | |
| 175 | void *loadrom(const char *filename) |
| 176 | { |
| 177 | void *romarea = loadfile(filename, &romsize, 0, SEEK_SET); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 178 | if (romarea == NULL) |
| 179 | return NULL; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 180 | recalculate_rom_geometry(romarea); |
| 181 | return romarea; |
| 182 | } |
| 183 | |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 184 | int writerom(const char *filename, void *start, uint32_t size) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 185 | { |
| 186 | FILE *file = fopen(filename, "wb"); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 187 | if (!file) { |
| 188 | fprintf(stderr, "Could not open '%s' for writing: ", filename); |
| 189 | perror(""); |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | if (fwrite(start, size, 1, file) != 1) { |
| 194 | fprintf(stderr, "Could not write to '%s': ", filename); |
| 195 | perror(""); |
| 196 | return 1; |
| 197 | } |
| 198 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 199 | fclose(file); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 200 | return 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 201 | } |
| 202 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 203 | int cbfs_file_header(unsigned long physaddr) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 204 | { |
| 205 | /* maybe improve this test */ |
| 206 | return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0); |
| 207 | } |
| 208 | |
| 209 | struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size) |
| 210 | { |
| 211 | struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 212 | strncpy((char *)(nextfile->magic), "LARCHIVE", 8); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 213 | nextfile->len = htonl(size); |
| 214 | nextfile->type = htonl(0xffffffff); |
| 215 | nextfile->checksum = 0; // FIXME? |
| 216 | nextfile->offset = htonl(sizeof(struct cbfs_file) + 16); |
| 217 | memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16); |
| 218 | return nextfile; |
| 219 | } |
| 220 | |
| 221 | int iself(unsigned char *input) |
| 222 | { |
| 223 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; |
| 224 | return !memcmp(ehdr->e_ident, ELFMAG, 4); |
| 225 | } |
| 226 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 227 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 228 | uint32_t type; |
| 229 | const char *name; |
| 230 | } filetypes[] = { |
| 231 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 232 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 233 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 234 | {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"}, |
| 235 | {CBFS_COMPONENT_RAW, "raw"}, |
| 236 | {CBFS_COMPONENT_VSA, "vsa"}, |
| 237 | {CBFS_COMPONENT_MBI, "mbi"}, |
| 238 | {CBFS_COMPONENT_MICROCODE, "microcode"}, |
Patrick Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 239 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 240 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 241 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 242 | {CBFS_COMPONENT_NULL, "null"} |
| 243 | }; |
| 244 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 245 | void print_supported_filetypes(void) |
| 246 | { |
| 247 | int i, number = ARRAY_SIZE(filetypes); |
| 248 | |
| 249 | for (i=0; i<number; i++) { |
| 250 | printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
| 251 | if ((i%8) == 7) |
| 252 | printf("\n"); |
| 253 | } |
| 254 | } |
| 255 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 256 | const char *strfiletype(uint32_t number) |
| 257 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 258 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 259 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 260 | if (filetypes[i].type == number) |
| 261 | return filetypes[i].name; |
| 262 | return "unknown"; |
| 263 | } |
| 264 | |
| 265 | uint64_t intfiletype(const char *name) |
| 266 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 267 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 268 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 269 | if (strcmp(filetypes[i].name, name) == 0) |
| 270 | return filetypes[i].type; |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | void print_cbfs_directory(const char *filename) |
| 275 | { |
| 276 | printf |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 277 | ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
| 278 | "Alignment: %d bytes, architecture: %s\n\n", |
Uwe Hermann | 942a40d | 2010-02-10 19:52:35 +0000 | [diff] [blame] | 279 | basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize), |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 280 | romsize, ntohl(master_header->offset), align, arch_to_string(arch)); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 281 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 282 | uint32_t current = phys_start; |
| 283 | while (current < phys_end) { |
| 284 | if (!cbfs_file_header(current)) { |
| 285 | current += align; |
| 286 | continue; |
| 287 | } |
| 288 | struct cbfs_file *thisfile = |
| 289 | (struct cbfs_file *)phys_to_virt(current); |
| 290 | uint32_t length = ntohl(thisfile->len); |
Maciej Pijanka | f44eb78 | 2010-01-07 21:37:18 +0000 | [diff] [blame] | 291 | char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 292 | if (strlen(fname) == 0) |
Maciej Pijanka | f44eb78 | 2010-01-07 21:37:18 +0000 | [diff] [blame] | 293 | fname = "(empty)"; |
| 294 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 295 | printf("%-30s 0x%-8x %-12s %d\n", fname, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 296 | current - phys_start + ntohl(master_header->offset), |
| 297 | strfiletype(ntohl(thisfile->type)), length); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 298 | current = |
| 299 | ALIGN(current + ntohl(thisfile->len) + |
| 300 | ntohl(thisfile->offset), align); |
| 301 | } |
| 302 | } |
| 303 | |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 304 | int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath) |
| 305 | { |
| 306 | // Identify the coreboot image. |
| 307 | printf( |
| 308 | "%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\nAlignment: %d bytes\n\n", |
| 309 | basename((char *)filename), romsize / 1024, ntohl(master_header->bootblocksize), |
| 310 | romsize, ntohl(master_header->offset), align); |
| 311 | |
| 312 | FILE *outfile = NULL; |
| 313 | uint32_t current = phys_start; |
| 314 | while (current < phys_end) { |
| 315 | if (!cbfs_file_header(current)) { |
| 316 | current += align; |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | // Locate the file start struct |
| 321 | struct cbfs_file *thisfile = |
| 322 | (struct cbfs_file *)phys_to_virt(current); |
| 323 | // And its length |
| 324 | uint32_t length = ntohl(thisfile->len); |
| 325 | // Locate the file name |
| 326 | char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 327 | |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 328 | // It's not the file we are looking for.. |
| 329 | if (strcmp(fname, payloadname) != 0) |
| 330 | { |
| 331 | current = |
| 332 | ALIGN(current + ntohl(thisfile->len) + |
| 333 | ntohl(thisfile->offset), align); |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | // Else, it's our file. |
Peter Stuge | 441426b | 2011-01-17 05:08:32 +0000 | [diff] [blame] | 338 | 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] | 339 | current - phys_start, strfiletype(ntohl(thisfile->type)), |
| 340 | length); |
| 341 | |
| 342 | // If we are not dumping to stdout, open the out file. |
| 343 | outfile = fopen(outpath, "wb"); |
| 344 | if (!outfile) |
| 345 | { |
| 346 | printf("Could not open the file %s for writing. Aborting.\n", outpath); |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW) |
| 351 | { |
| 352 | printf("Warning: only 'raw' files are safe to extract.\n"); |
| 353 | } |
| 354 | |
| 355 | fwrite(((char *)thisfile) |
| 356 | + ntohl(thisfile->offset), length, 1, outfile); |
| 357 | |
| 358 | fclose(outfile); |
Peter Stuge | 441426b | 2011-01-17 05:08:32 +0000 | [diff] [blame] | 359 | printf("Successfully dumped the file.\n"); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 360 | |
| 361 | // We'll only dump one file. |
| 362 | return 0; |
| 363 | } |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 364 | printf("File %s not found.\n", payloadname); |
| 365 | return 1; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 369 | int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location) |
| 370 | { |
| 371 | uint32_t current = phys_start; |
| 372 | while (current < phys_end) { |
| 373 | if (!cbfs_file_header(current)) { |
| 374 | current += align; |
| 375 | continue; |
| 376 | } |
| 377 | struct cbfs_file *thisfile = |
| 378 | (struct cbfs_file *)phys_to_virt(current); |
| 379 | uint32_t length = ntohl(thisfile->len); |
| 380 | |
| 381 | dprintf("at %x, %x bytes\n", current, length); |
| 382 | /* Is this a free chunk? */ |
| 383 | if ((thisfile->type == CBFS_COMPONENT_DELETED) |
| 384 | || (thisfile->type == CBFS_COMPONENT_NULL)) { |
| 385 | dprintf("null||deleted at %x, %x bytes\n", current, |
| 386 | length); |
| 387 | /* if this is the right size, and if specified, the right location, use it */ |
| 388 | if ((contentsize <= length) |
| 389 | && ((location == 0) || (current == location))) { |
| 390 | if (contentsize < length) { |
| 391 | dprintf |
| 392 | ("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n", |
| 393 | length, contentsize, |
| 394 | ALIGN(current + contentsize, |
| 395 | align), |
| 396 | length - contentsize); |
| 397 | uint32_t start = |
| 398 | ALIGN(current + contentsize, align); |
| 399 | uint32_t size = |
| 400 | current + ntohl(thisfile->offset) |
| 401 | + length - start - 16 - |
| 402 | sizeof(struct cbfs_file); |
| 403 | cbfs_create_empty_file(start, size); |
| 404 | } |
| 405 | dprintf("copying data\n"); |
| 406 | memcpy(phys_to_virt(current), content, |
| 407 | contentsize); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 408 | return 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 409 | } |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 410 | if (location != 0) { |
| 411 | /* CBFS has the constraint that the chain always moves up in memory. so once |
| 412 | we're past the place we seek, we don't need to look any further */ |
| 413 | if (current > location) { |
| 414 | printf |
| 415 | ("the requested space is not available\n"); |
| 416 | return 1; |
| 417 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 418 | |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 419 | /* Is the requested location inside the current chunk? */ |
| 420 | if ((current < location) |
| 421 | && ((location + contentsize) <= |
| 422 | (current + length))) { |
| 423 | /* Split it up. In the next iteration the code will be at the right place. */ |
| 424 | dprintf("split up. new length: %x\n", |
| 425 | location - current - |
| 426 | ntohl(thisfile->offset)); |
| 427 | thisfile->len = |
| 428 | htonl(location - current - |
| 429 | ntohl(thisfile->offset)); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 430 | cbfs_create_empty_file(location, |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 431 | length - |
| 432 | (location - |
| 433 | current)); |
| 434 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | current = |
| 438 | ALIGN(current + ntohl(thisfile->len) + |
| 439 | ntohl(thisfile->offset), align); |
| 440 | } |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 441 | 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] | 442 | printf("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 443 | return 1; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 446 | |
| 447 | static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first, |
| 448 | struct cbfs_file *second) |
| 449 | { |
| 450 | uint32_t new_length = |
| 451 | ntohl(first->len) + ntohl(second->len) + ntohl(second->offset); |
| 452 | first->len = htonl(new_length); |
| 453 | first->checksum = 0; // FIXME? |
| 454 | return first; |
| 455 | } |
| 456 | |
| 457 | static struct cbfs_file *next_file(struct cbfs_file *prev) |
| 458 | { |
| 459 | uint32_t pos = (prev == NULL) ? phys_start : |
| 460 | ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset), |
| 461 | align); |
| 462 | |
| 463 | for (; pos < phys_end; pos += align) { |
| 464 | if (cbfs_file_header(pos)) |
| 465 | return (struct cbfs_file *)phys_to_virt(pos); |
| 466 | } |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | int remove_file_from_cbfs(const char *filename) |
| 472 | { |
| 473 | struct cbfs_file *prev = NULL; |
| 474 | struct cbfs_file *cur = next_file(prev); |
| 475 | struct cbfs_file *next = next_file(cur); |
| 476 | for (; cur; prev = cur, cur = next, next = next_file(next)) { |
| 477 | |
| 478 | /* Check if this is the file to remove. */ |
| 479 | char *name = (char *)cur + sizeof(*cur); |
| 480 | if (strcmp(name, filename)) |
| 481 | continue; |
| 482 | |
| 483 | /* Mark the file as free space and erase its name. */ |
| 484 | cur->type = CBFS_COMPONENT_NULL; |
| 485 | name[0] = '\0'; |
| 486 | |
| 487 | /* Merge it with the previous file if possible. */ |
| 488 | if (prev && prev->type == CBFS_COMPONENT_NULL) |
| 489 | cur = merge_adjacent_files(prev, cur); |
| 490 | |
| 491 | /* Merge it with the next file if possible. */ |
| 492 | if (next && next->type == CBFS_COMPONENT_NULL) |
| 493 | merge_adjacent_files(cur, next); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | printf("CBFS file %s not found.\n", filename); |
| 498 | return 1; |
| 499 | } |
| 500 | |
| 501 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 502 | /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns |
| 503 | the new location that points to the cbfs_file header */ |
| 504 | void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, |
| 505 | uint32_t type, uint32_t * location) |
| 506 | { |
| 507 | uint32_t filename_len = ALIGN(strlen(filename) + 1, 16); |
| 508 | uint32_t headersize = sizeof(struct cbfs_file) + filename_len; |
| 509 | if ((location != 0) && (*location != 0)) { |
| 510 | uint32_t offset = *location % align; |
| 511 | /* If offset >= (headersize % align), we can stuff the header into the offset. |
| 512 | Otherwise the header has to be aligned itself, and put before the offset data */ |
| 513 | if (offset >= (headersize % align)) { |
| 514 | offset -= (headersize % align); |
| 515 | } else { |
| 516 | offset += align - (headersize % align); |
| 517 | } |
| 518 | headersize += offset; |
| 519 | *location -= headersize; |
| 520 | } |
| 521 | void *newdata = malloc(*datasize + headersize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 522 | if (!newdata) { |
| 523 | printf("Could not get %d bytes for CBFS file.\n", *datasize + |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 524 | headersize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 525 | exit(1); |
| 526 | } |
Peter Stuge | f4aca1d | 2009-12-06 12:14:39 +0000 | [diff] [blame] | 527 | memset(newdata, 0xff, *datasize + headersize); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 528 | struct cbfs_file *nextfile = (struct cbfs_file *)newdata; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 529 | strncpy((char *)(nextfile->magic), "LARCHIVE", 8); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 530 | nextfile->len = htonl(*datasize); |
| 531 | nextfile->type = htonl(type); |
| 532 | nextfile->checksum = 0; // FIXME? |
| 533 | nextfile->offset = htonl(headersize); |
| 534 | strcpy(newdata + sizeof(struct cbfs_file), filename); |
| 535 | memcpy(newdata + headersize, data, *datasize); |
| 536 | *datasize += headersize; |
| 537 | return newdata; |
| 538 | } |
| 539 | |
| 540 | int create_cbfs_image(const char *romfile, uint32_t _romsize, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 541 | const char *bootblock, uint32_t align, uint32_t offs) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 542 | { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 543 | uint32_t bootblocksize = 0; |
| 544 | struct cbfs_header *master_header; |
| 545 | unsigned char *romarea, *bootblk; |
| 546 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 547 | romsize = _romsize; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 548 | romarea = malloc(romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 549 | if (!romarea) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 550 | fprintf(stderr, "E: Could not get %d bytes of memory" |
| 551 | " for CBFS image.\n", romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 552 | exit(1); |
| 553 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 554 | memset(romarea, 0xff, romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 555 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 556 | if (align == 0) |
| 557 | align = 64; |
| 558 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 559 | bootblk = loadfile(bootblock, &bootblocksize, |
| 560 | romarea + romsize, SEEK_END); |
| 561 | if (!bootblk) { |
| 562 | fprintf(stderr, "E: Could not load bootblock %s.\n", |
| 563 | bootblock); |
| 564 | free(romarea); |
| 565 | return 1; |
| 566 | } |
| 567 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 568 | // TODO(hungte) Replace magic numbers by named constants. |
| 569 | switch (arch) { |
| 570 | case CBFS_ARCHITECTURE_ARMV7: |
| 571 | /* Set up physical/virtual mapping */ |
| 572 | offset = romarea; |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 573 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 574 | // should be aligned to align but then we need to dynamically |
| 575 | // create the jump to the bootblock |
| 576 | loadfile(bootblock, &bootblocksize, romarea + 0x20 + |
| 577 | sizeof(struct cbfs_header), SEEK_SET); |
| 578 | master_header = (struct cbfs_header *)(romarea + 0x20); |
| 579 | uint32_t *arm_vec = (uint32_t *)romarea; |
| 580 | /* |
| 581 | * Encoding for this branch instruction is: |
| 582 | * 31:28 - condition (0xe for always/unconditional) |
| 583 | * 27:24 - 0xa |
| 584 | * 23: 0 - sign-extended offset (in multiples of 4) |
| 585 | * |
| 586 | * When executing the branch, the PC will read as the address |
| 587 | * of current instruction + 8. |
| 588 | */ |
| 589 | arm_vec[0] = htonl(0x0e0000ea); // branch to . + 64 bytes |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 590 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame^] | 591 | master_header->magic = ntohl(CBFS_HEADER_MAGIC); |
| 592 | master_header->version = ntohl(VERSION); |
| 593 | master_header->romsize = htonl(romsize); |
| 594 | master_header->bootblocksize = htonl(bootblocksize); |
| 595 | master_header->align = htonl(align); |
| 596 | master_header->offset = htonl( |
| 597 | ALIGN((0x40 + bootblocksize), align)); |
| 598 | master_header->architecture = htonl(CBFS_ARCHITECTURE_ARMV7); |
| 599 | |
| 600 | ((uint32_t *) phys_to_virt(0x4))[0] = |
| 601 | virt_to_phys(master_header); |
| 602 | |
| 603 | recalculate_rom_geometry(romarea); |
| 604 | |
| 605 | cbfs_create_empty_file( |
| 606 | ALIGN((0x40 + bootblocksize), align), |
| 607 | romsize - ALIGN((bootblocksize + 0x40), align) |
| 608 | //- sizeof(struct cbfs_header) |
| 609 | - sizeof(struct cbfs_file) ); |
| 610 | break; |
| 611 | |
| 612 | case CBFS_ARCHITECTURE_X86: |
| 613 | // Set up physical/virtual mapping |
| 614 | offset = romarea + romsize - 0x100000000ULL; |
| 615 | |
| 616 | loadfile(bootblock, &bootblocksize, romarea + romsize, |
| 617 | SEEK_END); |
| 618 | master_header = (struct cbfs_header *)(romarea + romsize - |
| 619 | bootblocksize - sizeof(struct cbfs_header)); |
| 620 | |
| 621 | master_header->magic = ntohl(CBFS_HEADER_MAGIC); |
| 622 | master_header->version = ntohl(VERSION); |
| 623 | master_header->romsize = htonl(romsize); |
| 624 | master_header->bootblocksize = htonl(bootblocksize); |
| 625 | master_header->align = htonl(align); |
| 626 | master_header->offset = htonl(offs); |
| 627 | master_header->architecture = htonl(CBFS_ARCHITECTURE_X86); |
| 628 | |
| 629 | ((uint32_t *) phys_to_virt(CBFS_HEADPTR_ADDR_X86))[0] = |
| 630 | virt_to_phys(master_header); |
| 631 | |
| 632 | recalculate_rom_geometry(romarea); |
| 633 | |
| 634 | cbfs_create_empty_file((0 - romsize + offs) & 0xffffffff, |
| 635 | romsize - offs - bootblocksize - |
| 636 | sizeof(struct cbfs_header) - |
| 637 | sizeof(struct cbfs_file) - 16); |
| 638 | break; |
| 639 | |
| 640 | default: |
| 641 | // Should not happen. |
| 642 | fprintf(stderr, "E: You found a bug in cbfstool.\n"); |
| 643 | exit(1); |
| 644 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 645 | |
| 646 | writerom(romfile, romarea, romsize); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 647 | free(romarea); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 648 | return 0; |
| 649 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 650 | |
| 651 | static int in_segment(int addr, int size, int gran) |
| 652 | { |
| 653 | return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1))); |
| 654 | } |
| 655 | |
| 656 | uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, |
| 657 | const char *filename, uint32_t alignment) |
| 658 | { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 659 | void *rom; |
| 660 | size_t filename_size, headersize, totalsize; |
| 661 | int ret = 0; |
| 662 | uint32_t current; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 663 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 664 | rom = loadrom(romfile); |
| 665 | if (rom == NULL) { |
| 666 | fprintf(stderr, "E: Could not load ROM image '%s'.\n", |
| 667 | romfile); |
| 668 | return 0; |
| 669 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 670 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 671 | filename_size = strlen(filename); |
| 672 | headersize = sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16) + |
| 673 | sizeof(struct cbfs_stage); |
| 674 | totalsize = headersize + filesize; |
| 675 | |
| 676 | current = phys_start; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 677 | while (current < phys_end) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 678 | uint32_t top; |
| 679 | struct cbfs_file *thisfile; |
| 680 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 681 | if (!cbfs_file_header(current)) { |
| 682 | current += align; |
| 683 | continue; |
| 684 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 685 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 686 | thisfile = (struct cbfs_file *)phys_to_virt(current); |
| 687 | |
| 688 | top = current + ntohl(thisfile->len) + ntohl(thisfile->offset); |
| 689 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 690 | if (((ntohl(thisfile->type) == 0x0) |
| 691 | || (ntohl(thisfile->type) == 0xffffffff)) |
| 692 | && (ntohl(thisfile->len) + ntohl(thisfile->offset) >= |
| 693 | totalsize)) { |
| 694 | if (in_segment |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 695 | (current + headersize, filesize, alignment)) { |
| 696 | ret = current + headersize; |
| 697 | break; |
| 698 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 699 | if ((ALIGN(current, alignment) + filesize < top) |
| 700 | && (ALIGN(current, alignment) - headersize > |
| 701 | current) |
| 702 | && in_segment(ALIGN(current, alignment), filesize, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 703 | alignment)) { |
| 704 | ret = ALIGN(current, alignment); |
| 705 | break; |
| 706 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 707 | if ((ALIGN(current, alignment) + alignment + filesize < |
| 708 | top) |
| 709 | && (ALIGN(current, alignment) + alignment - |
| 710 | headersize > current) |
| 711 | && in_segment(ALIGN(current, alignment) + alignment, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 712 | filesize, alignment)) { |
| 713 | ret = ALIGN(current, alignment) + alignment; |
| 714 | break; |
| 715 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 716 | } |
| 717 | current = |
| 718 | ALIGN(current + ntohl(thisfile->len) + |
| 719 | ntohl(thisfile->offset), align); |
| 720 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 721 | |
| 722 | free(rom); |
| 723 | return ret; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 724 | } |