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