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 | |
Hung-Te Lin | 332795c | 2013-01-28 15:53:34 +0800 | [diff] [blame] | 30 | /* Utilities */ |
| 31 | |
| 32 | /* Small, OS/libc independent runtime check for endianess */ |
| 33 | int is_big_endian(void) |
| 34 | { |
| 35 | static const uint32_t inttest = 0x12345678; |
| 36 | uint8_t inttest_lsb = *(uint8_t *)&inttest; |
| 37 | if (inttest_lsb == 0x12) { |
| 38 | return 1; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
Hung-Te Lin | 3cfacbf | 2013-01-30 00:43:46 +0800 | [diff] [blame^] | 43 | /* Buffer and file I/O */ |
| 44 | |
| 45 | int buffer_create(struct buffer *buffer, size_t size, const char *name) { |
| 46 | buffer->name = strdup(name); |
| 47 | buffer->size = size; |
| 48 | buffer->data = (char *)malloc(buffer->size); |
| 49 | if (!buffer->data) { |
| 50 | fprintf(stderr, "buffer_create: Insufficient memory (0x%zx).\n", |
| 51 | size); |
| 52 | } |
| 53 | return (buffer->data == NULL); |
| 54 | } |
| 55 | |
| 56 | int buffer_from_file(struct buffer *buffer, const char *filename) { |
| 57 | FILE *fp = fopen(filename, "rb"); |
| 58 | if (!fp) { |
| 59 | perror(filename); |
| 60 | return -1; |
| 61 | } |
| 62 | fseek(fp, 0, SEEK_END); |
| 63 | buffer->size = ftell(fp); |
| 64 | buffer->name = strdup(filename); |
| 65 | rewind(fp); |
| 66 | buffer->data = (char *)malloc(buffer->size); |
| 67 | assert(buffer->data); |
| 68 | if (fread(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 69 | fprintf(stderr, "incomplete read: %s\n", filename); |
| 70 | fclose(fp); |
| 71 | return -1; |
| 72 | } |
| 73 | fclose(fp); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int buffer_write_file(struct buffer *buffer, const char *filename) { |
| 78 | FILE *fp = fopen(filename, "wb"); |
| 79 | if (!fp) { |
| 80 | perror(filename); |
| 81 | return -1; |
| 82 | } |
| 83 | assert(buffer && buffer->data); |
| 84 | if (fwrite(buffer->data, 1, buffer->size, fp) != buffer->size) { |
| 85 | fprintf(stderr, "incomplete write: %s\n", filename); |
| 86 | fclose(fp); |
| 87 | return -1; |
| 88 | } |
| 89 | fclose(fp); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | void buffer_delete(struct buffer *buffer) { |
| 94 | assert(buffer); |
| 95 | if (buffer->name) { |
| 96 | free(buffer->name); |
| 97 | buffer->name = NULL; |
| 98 | } |
| 99 | if (buffer->data) { |
| 100 | free(buffer->data); |
| 101 | buffer->data = NULL; |
| 102 | } |
| 103 | buffer->size = 0; |
| 104 | } |
| 105 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 106 | size_t getfilesize(const char *filename) |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 107 | { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 108 | size_t size; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 109 | FILE *file = fopen(filename, "rb"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 110 | if (file == NULL) |
| 111 | return -1; |
| 112 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 113 | fseek(file, 0, SEEK_END); |
| 114 | size = ftell(file); |
| 115 | fclose(file); |
| 116 | return size; |
| 117 | } |
| 118 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 119 | void *loadfile(const char *filename, uint32_t * romsize_p, void *content, |
| 120 | int place) |
| 121 | { |
| 122 | FILE *file = fopen(filename, "rb"); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 123 | if (file == NULL) |
| 124 | return NULL; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 125 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 126 | fseek(file, 0, SEEK_END); |
| 127 | *romsize_p = ftell(file); |
| 128 | fseek(file, 0, SEEK_SET); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 129 | if (!content) { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 130 | content = malloc(*romsize_p); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 131 | if (!content) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 132 | ERROR("Could not get %d bytes for file %s\n", |
| 133 | *romsize_p, filename); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 134 | exit(1); |
| 135 | } |
| 136 | } else if (place == SEEK_END) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 137 | content -= *romsize_p; |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 138 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 139 | if (!fread(content, *romsize_p, 1, file)) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 140 | ERROR("Failed to read %s\n", filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | fclose(file); |
| 144 | return content; |
| 145 | } |
| 146 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 147 | static struct cbfs_header *master_header; |
| 148 | static uint32_t phys_start, phys_end, align; |
| 149 | uint32_t romsize; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 150 | void *offset; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 151 | uint32_t arch = CBFS_ARCHITECTURE_UNKNOWN; |
| 152 | |
| 153 | static struct { |
| 154 | uint32_t arch; |
| 155 | const char *name; |
| 156 | } arch_names[] = { |
| 157 | { CBFS_ARCHITECTURE_ARMV7, "armv7" }, |
| 158 | { CBFS_ARCHITECTURE_X86, "x86" }, |
| 159 | { CBFS_ARCHITECTURE_UNKNOWN, "unknown" } |
| 160 | }; |
| 161 | |
| 162 | uint32_t string_to_arch(const char *arch_string) |
| 163 | { |
| 164 | int i; |
| 165 | uint32_t ret = CBFS_ARCHITECTURE_UNKNOWN; |
| 166 | |
| 167 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 168 | if (!strcasecmp(arch_string, arch_names[i].name)) { |
| 169 | ret = arch_names[i].arch; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | const char *arch_to_string(uint32_t a) |
| 178 | { |
| 179 | int i; |
| 180 | const char *ret = NULL; |
| 181 | |
| 182 | for (i = 0; i < ARRAY_SIZE(arch_names); i++) { |
| 183 | if (a == arch_names[i].arch) { |
| 184 | ret = arch_names[i].name; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return ret; |
| 190 | |
| 191 | } |
| 192 | |
| 193 | int find_master_header(void *romarea, size_t size) |
| 194 | { |
| 195 | size_t offset; |
| 196 | |
| 197 | if (master_header) |
| 198 | return 0; |
| 199 | |
| 200 | for (offset = 0; offset < size - sizeof(struct cbfs_header); offset++) { |
| 201 | struct cbfs_header *tmp = romarea + offset; |
| 202 | |
| 203 | if (tmp->magic == ntohl(CBFS_HEADER_MAGIC)) { |
| 204 | master_header = tmp; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return master_header ? 0 : 1; |
| 210 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 211 | |
| 212 | void recalculate_rom_geometry(void *romarea) |
| 213 | { |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 214 | if (find_master_header(romarea, romsize)) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 215 | ERROR("Cannot find master header\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 216 | exit(1); |
| 217 | } |
| 218 | |
| 219 | /* Update old headers */ |
Hung-Te Lin | 086842a | 2013-01-04 12:33:03 +0800 | [diff] [blame] | 220 | if (master_header->version == CBFS_HEADER_VERSION1 && |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 221 | ntohl(master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 222 | DEBUG("Updating CBFS master header to version 2\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 223 | master_header->architecture = htonl(CBFS_ARCHITECTURE_X86); |
| 224 | } |
| 225 | |
| 226 | arch = ntohl(master_header->architecture); |
| 227 | |
| 228 | switch (arch) { |
| 229 | case CBFS_ARCHITECTURE_ARMV7: |
| 230 | offset = romarea; |
| 231 | phys_start = (0 + ntohl(master_header->offset)) & 0xffffffff; |
| 232 | phys_end = romsize & 0xffffffff; |
| 233 | break; |
| 234 | case CBFS_ARCHITECTURE_X86: |
| 235 | offset = romarea + romsize - 0x100000000ULL; |
| 236 | phys_start = (0 - romsize + ntohl(master_header->offset)) & |
| 237 | 0xffffffff; |
| 238 | phys_end = (0 - ntohl(master_header->bootblocksize) - |
| 239 | sizeof(struct cbfs_header)) & 0xffffffff; |
| 240 | break; |
| 241 | default: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 242 | ERROR("Unknown architecture\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 243 | exit(1); |
| 244 | } |
| 245 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 246 | align = ntohl(master_header->align); |
| 247 | } |
| 248 | |
| 249 | void *loadrom(const char *filename) |
| 250 | { |
| 251 | void *romarea = loadfile(filename, &romsize, 0, SEEK_SET); |
Patrick Georgi | 45d8a83 | 2009-09-15 08:21:46 +0000 | [diff] [blame] | 252 | if (romarea == NULL) |
| 253 | return NULL; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 254 | recalculate_rom_geometry(romarea); |
| 255 | return romarea; |
| 256 | } |
| 257 | |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 258 | int writerom(const char *filename, void *start, uint32_t size) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 259 | { |
| 260 | FILE *file = fopen(filename, "wb"); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 261 | if (!file) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 262 | ERROR("Could not open '%s' for writing: ", filename); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 263 | perror(""); |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | if (fwrite(start, size, 1, file) != 1) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 268 | ERROR("Could not write to '%s': ", filename); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 269 | perror(""); |
| 270 | return 1; |
| 271 | } |
| 272 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 273 | fclose(file); |
Stefan Reinauer | 9bb04385 | 2010-06-24 13:37:59 +0000 | [diff] [blame] | 274 | return 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 275 | } |
| 276 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 277 | int cbfs_file_header(unsigned long physaddr) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 278 | { |
| 279 | /* maybe improve this test */ |
| 280 | return (strncmp(phys_to_virt(physaddr), "LARCHIVE", 8) == 0); |
| 281 | } |
| 282 | |
| 283 | struct cbfs_file *cbfs_create_empty_file(uint32_t physaddr, uint32_t size) |
| 284 | { |
| 285 | struct cbfs_file *nextfile = (struct cbfs_file *)phys_to_virt(physaddr); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 286 | strncpy((char *)(nextfile->magic), "LARCHIVE", 8); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 287 | nextfile->len = htonl(size); |
| 288 | nextfile->type = htonl(0xffffffff); |
| 289 | nextfile->checksum = 0; // FIXME? |
| 290 | nextfile->offset = htonl(sizeof(struct cbfs_file) + 16); |
| 291 | memset(((void *)nextfile) + sizeof(struct cbfs_file), 0, 16); |
| 292 | return nextfile; |
| 293 | } |
| 294 | |
| 295 | int iself(unsigned char *input) |
| 296 | { |
| 297 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *) input; |
| 298 | return !memcmp(ehdr->e_ident, ELFMAG, 4); |
| 299 | } |
| 300 | |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 301 | static struct filetypes_t { |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 302 | uint32_t type; |
| 303 | const char *name; |
| 304 | } filetypes[] = { |
| 305 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 306 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 307 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 308 | {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"}, |
| 309 | {CBFS_COMPONENT_RAW, "raw"}, |
| 310 | {CBFS_COMPONENT_VSA, "vsa"}, |
| 311 | {CBFS_COMPONENT_MBI, "mbi"}, |
| 312 | {CBFS_COMPONENT_MICROCODE, "microcode"}, |
Patrick Georgi | a865b17 | 2011-01-14 07:40:24 +0000 | [diff] [blame] | 313 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos default"}, |
Mathias Krause | 941158f | 2012-04-12 21:36:23 +0200 | [diff] [blame] | 314 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos layout"}, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 315 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 316 | {CBFS_COMPONENT_NULL, "null"} |
| 317 | }; |
| 318 | |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 319 | void print_supported_filetypes(void) |
| 320 | { |
| 321 | int i, number = ARRAY_SIZE(filetypes); |
| 322 | |
| 323 | for (i=0; i<number; i++) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 324 | LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':','); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 325 | if ((i%8) == 7) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 326 | LOG("\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 330 | const char *strfiletype(uint32_t number) |
| 331 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 332 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 333 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 334 | if (filetypes[i].type == number) |
| 335 | return filetypes[i].name; |
| 336 | return "unknown"; |
| 337 | } |
| 338 | |
| 339 | uint64_t intfiletype(const char *name) |
| 340 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 341 | size_t i; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 342 | for (i = 0; i < (sizeof(filetypes) / sizeof(struct filetypes_t)); i++) |
| 343 | if (strcmp(filetypes[i].name, name) == 0) |
| 344 | return filetypes[i].type; |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | void print_cbfs_directory(const char *filename) |
| 349 | { |
Hung-Te Lin | 5a9f45c | 2013-01-28 23:42:25 +0800 | [diff] [blame] | 350 | char *name = strdup(filename); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 351 | printf |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 352 | ("%s: %d kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
| 353 | "alignment: %d bytes, architecture: %s\n\n", |
Hung-Te Lin | 5a9f45c | 2013-01-28 23:42:25 +0800 | [diff] [blame] | 354 | basename(name), romsize / 1024, ntohl(master_header->bootblocksize), |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 355 | romsize, ntohl(master_header->offset), align, arch_to_string(arch)); |
Hung-Te Lin | 5a9f45c | 2013-01-28 23:42:25 +0800 | [diff] [blame] | 356 | free(name); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 357 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 358 | uint32_t current = phys_start; |
| 359 | while (current < phys_end) { |
| 360 | if (!cbfs_file_header(current)) { |
| 361 | current += align; |
| 362 | continue; |
| 363 | } |
| 364 | struct cbfs_file *thisfile = |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 365 | (struct cbfs_file *)phys_to_virt(current); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 366 | uint32_t length = ntohl(thisfile->len); |
Maciej Pijanka | f44eb78 | 2010-01-07 21:37:18 +0000 | [diff] [blame] | 367 | char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 368 | if (strlen(fname) == 0) |
Maciej Pijanka | f44eb78 | 2010-01-07 21:37:18 +0000 | [diff] [blame] | 369 | fname = "(empty)"; |
| 370 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 371 | printf("%-30s 0x%-8x %-12s %d\n", fname, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 372 | current - phys_start + ntohl(master_header->offset), |
| 373 | strfiletype(ntohl(thisfile->type)), length); |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 374 | |
| 375 | /* note the components of the subheader are in host order ... */ |
| 376 | switch (ntohl(thisfile->type)) { |
| 377 | case CBFS_COMPONENT_STAGE: |
| 378 | { |
| 379 | struct cbfs_stage *stage = CBFS_SUBHEADER(thisfile); |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 380 | INFO(" %s compression, entry: 0x%llx, load: 0x%llx, length: %d/%d\n", |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 381 | stage->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", |
| 382 | (unsigned long long)stage->entry, |
| 383 | (unsigned long long)stage->load, |
| 384 | stage->len, |
| 385 | stage->memlen); |
| 386 | break; |
| 387 | } |
| 388 | case CBFS_COMPONENT_PAYLOAD: |
| 389 | { |
| 390 | struct cbfs_payload_segment *payload = CBFS_SUBHEADER(thisfile); |
| 391 | while(payload) { |
| 392 | switch(payload->type) { |
| 393 | case PAYLOAD_SEGMENT_CODE: |
| 394 | case PAYLOAD_SEGMENT_DATA: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 395 | INFO(" %s (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d)\n", |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 396 | payload->type == PAYLOAD_SEGMENT_CODE ? "code " : "data" , |
| 397 | payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", |
| 398 | ntohl(payload->offset), |
| 399 | (unsigned long long)ntohll(payload->load_addr), |
| 400 | ntohl(payload->len), ntohl(payload->mem_len)); |
| 401 | break; |
| 402 | case PAYLOAD_SEGMENT_ENTRY: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 403 | INFO(" entry (0x%llx)\n", (unsigned long long)ntohll(payload->load_addr)); |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 404 | break; |
| 405 | case PAYLOAD_SEGMENT_BSS: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 406 | INFO(" BSS (address 0x%016llx, length 0x%x)\n", (unsigned long long)ntohll(payload->load_addr), ntohl(payload->len)); |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 407 | break; |
| 408 | case PAYLOAD_SEGMENT_PARAMS: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 409 | INFO(" parameters\n"); |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 410 | break; |
| 411 | default: |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 412 | INFO(" %x (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d\n", |
Stefan Reinauer | db5b893 | 2013-01-18 15:53:22 -0800 | [diff] [blame] | 413 | payload->type, |
| 414 | payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no", |
| 415 | ntohl(payload->offset), |
| 416 | (unsigned long long)ntohll(payload->load_addr), |
| 417 | ntohl(payload->len), |
| 418 | ntohl(payload->mem_len)); |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if(payload->type == PAYLOAD_SEGMENT_ENTRY) |
| 423 | payload=NULL; |
| 424 | else |
| 425 | payload++; |
| 426 | } |
| 427 | break; |
| 428 | } |
| 429 | default: |
| 430 | break; |
| 431 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 432 | current = |
| 433 | ALIGN(current + ntohl(thisfile->len) + |
| 434 | ntohl(thisfile->offset), align); |
| 435 | } |
| 436 | } |
| 437 | |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 438 | int extract_file_from_cbfs(const char *filename, const char *payloadname, const char *outpath) |
| 439 | { |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 440 | FILE *outfile = NULL; |
| 441 | uint32_t current = phys_start; |
| 442 | while (current < phys_end) { |
| 443 | if (!cbfs_file_header(current)) { |
| 444 | current += align; |
| 445 | continue; |
| 446 | } |
| 447 | |
| 448 | // Locate the file start struct |
| 449 | struct cbfs_file *thisfile = |
| 450 | (struct cbfs_file *)phys_to_virt(current); |
| 451 | // And its length |
| 452 | uint32_t length = ntohl(thisfile->len); |
| 453 | // Locate the file name |
| 454 | char *fname = (char *)(phys_to_virt(current) + sizeof(struct cbfs_file)); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 455 | |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 456 | // It's not the file we are looking for.. |
| 457 | if (strcmp(fname, payloadname) != 0) |
| 458 | { |
| 459 | current = |
| 460 | ALIGN(current + ntohl(thisfile->len) + |
| 461 | ntohl(thisfile->offset), align); |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | // Else, it's our file. |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 466 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", fname, |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 467 | current - phys_start, strfiletype(ntohl(thisfile->type)), |
| 468 | length); |
| 469 | |
| 470 | // If we are not dumping to stdout, open the out file. |
| 471 | outfile = fopen(outpath, "wb"); |
| 472 | if (!outfile) |
| 473 | { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 474 | ERROR("Could not open the file %s for writing.\n", outpath); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 475 | return 1; |
| 476 | } |
| 477 | |
| 478 | if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW) |
| 479 | { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 480 | WARN("Only 'raw' files are safe to extract.\n"); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | fwrite(((char *)thisfile) |
| 484 | + ntohl(thisfile->offset), length, 1, outfile); |
| 485 | |
| 486 | fclose(outfile); |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 487 | LOG("Successfully dumped the file.\n"); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 488 | |
| 489 | // We'll only dump one file. |
| 490 | return 0; |
| 491 | } |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 492 | ERROR("File %s not found.\n", payloadname); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 493 | return 1; |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 497 | int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location) |
| 498 | { |
| 499 | uint32_t current = phys_start; |
| 500 | while (current < phys_end) { |
| 501 | if (!cbfs_file_header(current)) { |
| 502 | current += align; |
| 503 | continue; |
| 504 | } |
| 505 | struct cbfs_file *thisfile = |
| 506 | (struct cbfs_file *)phys_to_virt(current); |
| 507 | uint32_t length = ntohl(thisfile->len); |
| 508 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 509 | DEBUG("at %x, %x bytes\n", current, length); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 510 | /* Is this a free chunk? */ |
| 511 | if ((thisfile->type == CBFS_COMPONENT_DELETED) |
| 512 | || (thisfile->type == CBFS_COMPONENT_NULL)) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 513 | DEBUG("null||deleted at %x, %x bytes\n", current, |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 514 | length); |
| 515 | /* if this is the right size, and if specified, the right location, use it */ |
| 516 | if ((contentsize <= length) |
| 517 | && ((location == 0) || (current == location))) { |
| 518 | if (contentsize < length) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 519 | DEBUG("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n", |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 520 | length, contentsize, |
| 521 | ALIGN(current + contentsize, |
| 522 | align), |
| 523 | length - contentsize); |
| 524 | uint32_t start = |
| 525 | ALIGN(current + contentsize, align); |
| 526 | uint32_t size = |
| 527 | current + ntohl(thisfile->offset) |
| 528 | + length - start - 16 - |
| 529 | sizeof(struct cbfs_file); |
| 530 | cbfs_create_empty_file(start, size); |
| 531 | } |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 532 | DEBUG("copying data\n"); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 533 | memcpy(phys_to_virt(current), content, |
| 534 | contentsize); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 535 | return 0; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 536 | } |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 537 | if (location != 0) { |
| 538 | /* CBFS has the constraint that the chain always moves up in memory. so once |
| 539 | we're past the place we seek, we don't need to look any further */ |
| 540 | if (current > location) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 541 | ERROR("The requested space is not available\n"); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 542 | return 1; |
| 543 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 544 | |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 545 | /* Is the requested location inside the current chunk? */ |
| 546 | if ((current < location) |
| 547 | && ((location + contentsize) <= |
| 548 | (current + length))) { |
| 549 | /* Split it up. In the next iteration the code will be at the right place. */ |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 550 | DEBUG("split up. new length: %x\n", |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 551 | location - current - |
| 552 | ntohl(thisfile->offset)); |
| 553 | thisfile->len = |
| 554 | htonl(location - current - |
| 555 | ntohl(thisfile->offset)); |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 556 | cbfs_create_empty_file(location, |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 557 | length - |
| 558 | (location - |
| 559 | current)); |
| 560 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | current = |
| 564 | ALIGN(current + ntohl(thisfile->len) + |
| 565 | ntohl(thisfile->offset), align); |
| 566 | } |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 567 | ERROR("Could not add the file to CBFS, it's probably too big.\n"); |
| 568 | ERROR("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 569 | return 1; |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 572 | |
| 573 | static struct cbfs_file *merge_adjacent_files(struct cbfs_file *first, |
| 574 | struct cbfs_file *second) |
| 575 | { |
| 576 | uint32_t new_length = |
| 577 | ntohl(first->len) + ntohl(second->len) + ntohl(second->offset); |
| 578 | first->len = htonl(new_length); |
| 579 | first->checksum = 0; // FIXME? |
| 580 | return first; |
| 581 | } |
| 582 | |
| 583 | static struct cbfs_file *next_file(struct cbfs_file *prev) |
| 584 | { |
| 585 | uint32_t pos = (prev == NULL) ? phys_start : |
| 586 | ALIGN(virt_to_phys(prev) + ntohl(prev->len) + ntohl(prev->offset), |
| 587 | align); |
| 588 | |
| 589 | for (; pos < phys_end; pos += align) { |
| 590 | if (cbfs_file_header(pos)) |
| 591 | return (struct cbfs_file *)phys_to_virt(pos); |
| 592 | } |
| 593 | return NULL; |
| 594 | } |
| 595 | |
| 596 | |
| 597 | int remove_file_from_cbfs(const char *filename) |
| 598 | { |
| 599 | struct cbfs_file *prev = NULL; |
| 600 | struct cbfs_file *cur = next_file(prev); |
| 601 | struct cbfs_file *next = next_file(cur); |
| 602 | for (; cur; prev = cur, cur = next, next = next_file(next)) { |
| 603 | |
| 604 | /* Check if this is the file to remove. */ |
| 605 | char *name = (char *)cur + sizeof(*cur); |
| 606 | if (strcmp(name, filename)) |
| 607 | continue; |
| 608 | |
| 609 | /* Mark the file as free space and erase its name. */ |
| 610 | cur->type = CBFS_COMPONENT_NULL; |
| 611 | name[0] = '\0'; |
| 612 | |
| 613 | /* Merge it with the previous file if possible. */ |
| 614 | if (prev && prev->type == CBFS_COMPONENT_NULL) |
| 615 | cur = merge_adjacent_files(prev, cur); |
| 616 | |
| 617 | /* Merge it with the next file if possible. */ |
| 618 | if (next && next->type == CBFS_COMPONENT_NULL) |
| 619 | merge_adjacent_files(cur, next); |
| 620 | |
| 621 | return 0; |
| 622 | } |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 623 | ERROR("CBFS file %s not found.\n", filename); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 628 | /* returns new data block with cbfs_file header, suitable to dump into the ROM. location returns |
| 629 | the new location that points to the cbfs_file header */ |
| 630 | void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, |
| 631 | uint32_t type, uint32_t * location) |
| 632 | { |
| 633 | uint32_t filename_len = ALIGN(strlen(filename) + 1, 16); |
| 634 | uint32_t headersize = sizeof(struct cbfs_file) + filename_len; |
| 635 | if ((location != 0) && (*location != 0)) { |
| 636 | uint32_t offset = *location % align; |
| 637 | /* If offset >= (headersize % align), we can stuff the header into the offset. |
| 638 | Otherwise the header has to be aligned itself, and put before the offset data */ |
| 639 | if (offset >= (headersize % align)) { |
| 640 | offset -= (headersize % align); |
| 641 | } else { |
| 642 | offset += align - (headersize % align); |
| 643 | } |
| 644 | headersize += offset; |
| 645 | *location -= headersize; |
| 646 | } |
| 647 | void *newdata = malloc(*datasize + headersize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 648 | if (!newdata) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 649 | ERROR("Could not get %d bytes for CBFS file.\n", *datasize + |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 650 | headersize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 651 | exit(1); |
| 652 | } |
Peter Stuge | f4aca1d | 2009-12-06 12:14:39 +0000 | [diff] [blame] | 653 | memset(newdata, 0xff, *datasize + headersize); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 654 | struct cbfs_file *nextfile = (struct cbfs_file *)newdata; |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 655 | strncpy((char *)(nextfile->magic), "LARCHIVE", 8); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 656 | nextfile->len = htonl(*datasize); |
| 657 | nextfile->type = htonl(type); |
| 658 | nextfile->checksum = 0; // FIXME? |
| 659 | nextfile->offset = htonl(headersize); |
| 660 | strcpy(newdata + sizeof(struct cbfs_file), filename); |
| 661 | memcpy(newdata + headersize, data, *datasize); |
| 662 | *datasize += headersize; |
| 663 | return newdata; |
| 664 | } |
| 665 | |
| 666 | int create_cbfs_image(const char *romfile, uint32_t _romsize, |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 667 | const char *bootblock, uint32_t align, uint32_t offs) |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 668 | { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 669 | uint32_t bootblocksize = 0; |
| 670 | struct cbfs_header *master_header; |
| 671 | unsigned char *romarea, *bootblk; |
| 672 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 673 | romsize = _romsize; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 674 | romarea = malloc(romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 675 | if (!romarea) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 676 | ERROR("Could not get %d bytes of memory" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 677 | " for CBFS image.\n", romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 678 | exit(1); |
| 679 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 680 | memset(romarea, 0xff, romsize); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 681 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 682 | if (align == 0) |
| 683 | align = 64; |
| 684 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 685 | bootblk = loadfile(bootblock, &bootblocksize, |
| 686 | romarea + romsize, SEEK_END); |
| 687 | if (!bootblk) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 688 | ERROR("Could not load bootblock %s.\n", |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 689 | bootblock); |
| 690 | free(romarea); |
| 691 | return 1; |
| 692 | } |
| 693 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 694 | // TODO(hungte) Replace magic numbers by named constants. |
| 695 | switch (arch) { |
| 696 | case CBFS_ARCHITECTURE_ARMV7: |
| 697 | /* Set up physical/virtual mapping */ |
| 698 | offset = romarea; |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 699 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 700 | /* |
David Hendricks | 0b23d47 | 2013-01-14 20:58:50 -0800 | [diff] [blame] | 701 | * The initial jump instruction and bootblock will be placed |
| 702 | * before and after the master header, respectively. The |
| 703 | * bootblock image must contain a blank, aligned region large |
| 704 | * enough for the master header to fit. |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 705 | * |
David Hendricks | 0b23d47 | 2013-01-14 20:58:50 -0800 | [diff] [blame] | 706 | * An anchor string must be left such that when cbfstool is run |
| 707 | * we can find it and insert the master header at the next |
| 708 | * aligned boundary. |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 709 | */ |
David Hendricks | 0b23d47 | 2013-01-14 20:58:50 -0800 | [diff] [blame] | 710 | loadfile(bootblock, &bootblocksize, romarea + offs, SEEK_SET); |
| 711 | |
| 712 | unsigned char *p = romarea + offs; |
| 713 | while (1) { |
| 714 | /* FIXME: assumes little endian... */ |
| 715 | if (*(uint32_t *)p == 0xdeadbeef) |
| 716 | break; |
| 717 | if (p >= (romarea + _romsize)) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 718 | ERROR("Could not determine CBFS " |
| 719 | "header location.\n"); |
David Hendricks | 0b23d47 | 2013-01-14 20:58:50 -0800 | [diff] [blame] | 720 | return 1; |
| 721 | } |
| 722 | p += (sizeof(unsigned int)); |
| 723 | } |
| 724 | unsigned int u = ALIGN((unsigned int)(p - romarea), align); |
| 725 | master_header = (struct cbfs_header *)(romarea + u); |
Stefan Reinauer | 853270a | 2009-09-22 15:55:01 +0000 | [diff] [blame] | 726 | |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 727 | master_header->magic = ntohl(CBFS_HEADER_MAGIC); |
Hung-Te Lin | 086842a | 2013-01-04 12:33:03 +0800 | [diff] [blame] | 728 | master_header->version = ntohl(CBFS_HEADER_VERSION); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 729 | master_header->romsize = htonl(romsize); |
| 730 | master_header->bootblocksize = htonl(bootblocksize); |
| 731 | master_header->align = htonl(align); |
| 732 | master_header->offset = htonl( |
| 733 | ALIGN((0x40 + bootblocksize), align)); |
| 734 | master_header->architecture = htonl(CBFS_ARCHITECTURE_ARMV7); |
| 735 | |
David Hendricks | 454856b | 2013-01-02 17:29:00 -0800 | [diff] [blame] | 736 | ((uint32_t *) phys_to_virt(0x4 + offs))[0] = |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 737 | virt_to_phys(master_header); |
| 738 | |
| 739 | recalculate_rom_geometry(romarea); |
| 740 | |
| 741 | cbfs_create_empty_file( |
David Hendricks | 454856b | 2013-01-02 17:29:00 -0800 | [diff] [blame] | 742 | offs + ALIGN((0x40 + bootblocksize), align), |
| 743 | romsize - offs - sizeof(struct cbfs_file) - |
| 744 | ALIGN((bootblocksize + 0x40), align)); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 745 | break; |
| 746 | |
| 747 | case CBFS_ARCHITECTURE_X86: |
| 748 | // Set up physical/virtual mapping |
| 749 | offset = romarea + romsize - 0x100000000ULL; |
| 750 | |
| 751 | loadfile(bootblock, &bootblocksize, romarea + romsize, |
| 752 | SEEK_END); |
| 753 | master_header = (struct cbfs_header *)(romarea + romsize - |
| 754 | bootblocksize - sizeof(struct cbfs_header)); |
| 755 | |
| 756 | master_header->magic = ntohl(CBFS_HEADER_MAGIC); |
Hung-Te Lin | 086842a | 2013-01-04 12:33:03 +0800 | [diff] [blame] | 757 | master_header->version = ntohl(CBFS_HEADER_VERSION); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 758 | master_header->romsize = htonl(romsize); |
| 759 | master_header->bootblocksize = htonl(bootblocksize); |
| 760 | master_header->align = htonl(align); |
| 761 | master_header->offset = htonl(offs); |
| 762 | master_header->architecture = htonl(CBFS_ARCHITECTURE_X86); |
| 763 | |
| 764 | ((uint32_t *) phys_to_virt(CBFS_HEADPTR_ADDR_X86))[0] = |
| 765 | virt_to_phys(master_header); |
| 766 | |
| 767 | recalculate_rom_geometry(romarea); |
| 768 | |
| 769 | cbfs_create_empty_file((0 - romsize + offs) & 0xffffffff, |
| 770 | romsize - offs - bootblocksize - |
| 771 | sizeof(struct cbfs_header) - |
| 772 | sizeof(struct cbfs_file) - 16); |
| 773 | break; |
| 774 | |
| 775 | default: |
| 776 | // Should not happen. |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 777 | ERROR("You found a bug in cbfstool.\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 778 | exit(1); |
| 779 | } |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 780 | |
| 781 | writerom(romfile, romarea, romsize); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 782 | free(romarea); |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 783 | return 0; |
| 784 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 785 | |
| 786 | static int in_segment(int addr, int size, int gran) |
| 787 | { |
| 788 | return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1))); |
| 789 | } |
| 790 | |
| 791 | uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, |
| 792 | const char *filename, uint32_t alignment) |
| 793 | { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 794 | void *rom; |
| 795 | size_t filename_size, headersize, totalsize; |
| 796 | int ret = 0; |
| 797 | uint32_t current; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 798 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 799 | rom = loadrom(romfile); |
| 800 | if (rom == NULL) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 801 | ERROR("Could not load ROM image '%s'.\n", romfile); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 804 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 805 | filename_size = strlen(filename); |
| 806 | headersize = sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16) + |
| 807 | sizeof(struct cbfs_stage); |
| 808 | totalsize = headersize + filesize; |
| 809 | |
| 810 | current = phys_start; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 811 | while (current < phys_end) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 812 | uint32_t top; |
| 813 | struct cbfs_file *thisfile; |
| 814 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 815 | if (!cbfs_file_header(current)) { |
| 816 | current += align; |
| 817 | continue; |
| 818 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 819 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 820 | thisfile = (struct cbfs_file *)phys_to_virt(current); |
| 821 | |
| 822 | top = current + ntohl(thisfile->len) + ntohl(thisfile->offset); |
| 823 | |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 824 | if (((ntohl(thisfile->type) == 0x0) |
| 825 | || (ntohl(thisfile->type) == 0xffffffff)) |
| 826 | && (ntohl(thisfile->len) + ntohl(thisfile->offset) >= |
| 827 | totalsize)) { |
| 828 | if (in_segment |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 829 | (current + headersize, filesize, alignment)) { |
| 830 | ret = current + headersize; |
| 831 | break; |
| 832 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 833 | if ((ALIGN(current, alignment) + filesize < top) |
| 834 | && (ALIGN(current, alignment) - headersize > |
| 835 | current) |
| 836 | && in_segment(ALIGN(current, alignment), filesize, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 837 | alignment)) { |
| 838 | ret = ALIGN(current, alignment); |
| 839 | break; |
| 840 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 841 | if ((ALIGN(current, alignment) + alignment + filesize < |
| 842 | top) |
| 843 | && (ALIGN(current, alignment) + alignment - |
| 844 | headersize > current) |
| 845 | && in_segment(ALIGN(current, alignment) + alignment, |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 846 | filesize, alignment)) { |
| 847 | ret = ALIGN(current, alignment) + alignment; |
| 848 | break; |
| 849 | } |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 850 | } |
| 851 | current = |
| 852 | ALIGN(current + ntohl(thisfile->len) + |
| 853 | ntohl(thisfile->offset), align); |
| 854 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 855 | |
| 856 | free(rom); |
| 857 | return ret; |
Patrick Georgi | 0da38dd | 2009-11-09 17:18:02 +0000 | [diff] [blame] | 858 | } |