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