Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * CBFS Image Manipulation |
| 3 | * |
| 4 | * Copyright (C) 2013 The Chromium OS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <libgen.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "common.h" |
| 27 | #include "cbfs_image.h" |
| 28 | |
| 29 | /* The file name align is not defined in CBFS spec -- only a preference by |
| 30 | * (old) cbfstool. */ |
| 31 | #define CBFS_FILENAME_ALIGN (16) |
| 32 | |
| 33 | /* To make CBFS more friendly to ROM, fill -1 (0xFF) instead of zero. */ |
| 34 | #define CBFS_CONTENT_DEFAULT_VALUE (-1) |
| 35 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 36 | /* Type and format */ |
| 37 | |
| 38 | struct typedesc_t { |
| 39 | uint32_t type; |
| 40 | const char *name; |
| 41 | }; |
| 42 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 43 | static const struct typedesc_t types_cbfs_entry[] = { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 44 | {CBFS_COMPONENT_STAGE, "stage"}, |
| 45 | {CBFS_COMPONENT_PAYLOAD, "payload"}, |
| 46 | {CBFS_COMPONENT_OPTIONROM, "optionrom"}, |
| 47 | {CBFS_COMPONENT_BOOTSPLASH, "bootsplash"}, |
| 48 | {CBFS_COMPONENT_RAW, "raw"}, |
| 49 | {CBFS_COMPONENT_VSA, "vsa"}, |
| 50 | {CBFS_COMPONENT_MBI, "mbi"}, |
| 51 | {CBFS_COMPONENT_MICROCODE, "microcode"}, |
Martin Roth | dde307c | 2015-03-24 15:54:20 -0600 | [diff] [blame] | 52 | {CBFS_COMPONENT_FSP, "fsp"}, |
| 53 | {CBFS_COMPONENT_MRC, "mrc"}, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 54 | {CBFS_COMPONENT_CMOS_DEFAULT, "cmos_default"}, |
| 55 | {CBFS_COMPONENT_CMOS_LAYOUT, "cmos_layout"}, |
Martin Roth | dde307c | 2015-03-24 15:54:20 -0600 | [diff] [blame] | 56 | {CBFS_COMPONENT_SPD, "spd"}, |
| 57 | {CBFS_COMPONENT_MRC_CACHE, "mrc_cache"}, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 58 | {CBFS_COMPONENT_DELETED, "deleted"}, |
| 59 | {CBFS_COMPONENT_NULL, "null"}, |
| 60 | {0, NULL}, |
| 61 | }; |
| 62 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 63 | static const struct typedesc_t types_cbfs_compression[] = { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 64 | {CBFS_COMPRESS_NONE, "none"}, |
| 65 | {CBFS_COMPRESS_LZMA, "LZMA"}, |
| 66 | {0, NULL}, |
| 67 | }; |
| 68 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 69 | static const char *lookup_name_by_type(const struct typedesc_t *desc, uint32_t type, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 70 | const char *default_value) |
| 71 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 72 | int i; |
| 73 | for (i = 0; desc[i].name; i++) |
| 74 | if (desc[i].type == type) |
| 75 | return desc[i].name; |
| 76 | return default_value; |
| 77 | } |
| 78 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 79 | static const char *get_cbfs_entry_type_name(uint32_t type) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 80 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 81 | return lookup_name_by_type(types_cbfs_entry, type, "(unknown)"); |
| 82 | } |
| 83 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 84 | /* CBFS image */ |
| 85 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 86 | static int cbfs_calculate_file_header_size(const char *name) |
| 87 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 88 | return (sizeof(struct cbfs_file) + |
| 89 | align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN)); |
| 90 | } |
| 91 | |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 92 | static int cbfs_fix_legacy_size(struct cbfs_image *image, char *hdr_loc) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 93 | { |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 94 | // A bug in old cbfstool may produce extra few bytes (by alignment) and |
| 95 | // cause cbfstool to overwrite things after free space -- which is |
| 96 | // usually CBFS header on x86. We need to workaround that. |
| 97 | |
| 98 | struct cbfs_file *entry, *first = NULL, *last = NULL; |
| 99 | for (first = entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 100 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 101 | entry = cbfs_find_next_entry(image, entry)) { |
| 102 | last = entry; |
| 103 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 104 | if ((char *)first < (char *)hdr_loc && |
| 105 | (char *)entry > (char *)hdr_loc) { |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 106 | WARN("CBFS image was created with old cbfstool with size bug. " |
| 107 | "Fixing size in last entry...\n"); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 108 | last->len = htonl(ntohl(last->len) - image->header->align); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 109 | DEBUG("Last entry has been changed from 0x%x to 0x%x.\n", |
| 110 | cbfs_get_entry_addr(image, entry), |
| 111 | cbfs_get_entry_addr(image, |
| 112 | cbfs_find_next_entry(image, last))); |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 117 | void cbfs_put_header(void *dest, const struct cbfs_header *header) |
| 118 | { |
| 119 | struct buffer outheader; |
| 120 | |
| 121 | outheader.data = dest; |
| 122 | outheader.size = 0; |
| 123 | |
| 124 | xdr_be.put32(&outheader, header->magic); |
| 125 | xdr_be.put32(&outheader, header->version); |
| 126 | xdr_be.put32(&outheader, header->romsize); |
| 127 | xdr_be.put32(&outheader, header->bootblocksize); |
| 128 | xdr_be.put32(&outheader, header->align); |
| 129 | xdr_be.put32(&outheader, header->offset); |
| 130 | xdr_be.put32(&outheader, header->architecture); |
| 131 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 132 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 133 | static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output, |
| 134 | struct cbfs_payload_segment *input) |
| 135 | { |
| 136 | struct buffer seg = { |
| 137 | .data = (void *)input, |
| 138 | .size = sizeof(*input), |
| 139 | }; |
| 140 | output->type = xdr_be.get32(&seg); |
| 141 | output->compression = xdr_be.get32(&seg); |
| 142 | output->offset = xdr_be.get32(&seg); |
| 143 | output->load_addr = xdr_be.get64(&seg); |
| 144 | output->len = xdr_be.get32(&seg); |
| 145 | output->mem_len = xdr_be.get32(&seg); |
| 146 | assert(seg.size == 0); |
| 147 | } |
| 148 | |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 149 | void cbfs_get_header(struct cbfs_header *header, const void *src) |
| 150 | { |
| 151 | struct buffer outheader; |
| 152 | |
| 153 | outheader.data = (void *)src; /* We're not modifying the data */ |
| 154 | outheader.size = 0; |
| 155 | |
| 156 | header->magic = xdr_be.get32(&outheader); |
| 157 | header->version = xdr_be.get32(&outheader); |
| 158 | header->romsize = xdr_be.get32(&outheader); |
| 159 | header->bootblocksize = xdr_be.get32(&outheader); |
| 160 | header->align = xdr_be.get32(&outheader); |
| 161 | header->offset = xdr_be.get32(&outheader); |
| 162 | header->architecture = xdr_be.get32(&outheader); |
| 163 | } |
| 164 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 165 | int cbfs_image_create(struct cbfs_image *image, |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 166 | uint32_t architecture, |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 167 | size_t size, |
| 168 | uint32_t align, |
| 169 | struct buffer *bootblock, |
| 170 | int32_t bootblock_offset, |
| 171 | int32_t header_offset, |
| 172 | int32_t entries_offset) |
| 173 | { |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 174 | struct cbfs_header header; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 175 | struct cbfs_file *entry; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 176 | int32_t *rel_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 177 | uint32_t cbfs_len; |
Hung-Te Lin | b02c873 | 2013-03-15 17:40:08 +0800 | [diff] [blame] | 178 | size_t entry_header_len; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 179 | void *header_loc; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 180 | |
| 181 | DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, " |
| 182 | "header=0x%x+0x%zx, entries_offset=0x%x\n", |
| 183 | bootblock_offset, bootblock->size, |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 184 | header_offset, sizeof(header), entries_offset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 185 | |
| 186 | if (buffer_create(&image->buffer, size, "(new)") != 0) |
| 187 | return -1; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 188 | if ((image->header = malloc(sizeof(*image->header))) == NULL) |
| 189 | return -1; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 190 | memset(image->buffer.data, CBFS_CONTENT_DEFAULT_VALUE, size); |
| 191 | |
| 192 | // Adjust legcay top-aligned address to ROM offset. |
| 193 | if (IS_TOP_ALIGNED_ADDRESS(entries_offset)) |
| 194 | entries_offset += (int32_t)size; |
| 195 | if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset)) |
| 196 | bootblock_offset += (int32_t)size; |
| 197 | if (IS_TOP_ALIGNED_ADDRESS(header_offset)) |
| 198 | header_offset += (int32_t) size; |
| 199 | |
| 200 | DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, " |
| 201 | "header=0x%x, entries_offset=0x%x\n", |
| 202 | bootblock_offset, header_offset, entries_offset); |
| 203 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 204 | // Prepare bootblock |
| 205 | if (bootblock_offset + bootblock->size > size) { |
| 206 | ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
| 207 | bootblock_offset, bootblock->size, size); |
| 208 | return -1; |
| 209 | } |
Hung-Te Lin | c5ff648 | 2013-02-06 12:41:49 +0800 | [diff] [blame] | 210 | if (entries_offset > bootblock_offset && |
| 211 | entries_offset < bootblock->size) { |
| 212 | ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n", |
| 213 | bootblock_offset, bootblock->size, entries_offset); |
| 214 | return -1; |
| 215 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 216 | memcpy(image->buffer.data + bootblock_offset, bootblock->data, |
| 217 | bootblock->size); |
| 218 | |
| 219 | // Prepare header |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 220 | if (header_offset + sizeof(header) > size - sizeof(int32_t)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 221 | ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 222 | header_offset, sizeof(header), size); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 223 | return -1; |
| 224 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 225 | image->header->magic = CBFS_HEADER_MAGIC; |
| 226 | image->header->version = CBFS_HEADER_VERSION; |
| 227 | image->header->romsize = size; |
| 228 | image->header->bootblocksize = bootblock->size; |
| 229 | image->header->align = align; |
| 230 | image->header->offset = entries_offset; |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 231 | image->header->architecture = architecture; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 232 | |
| 233 | header_loc = (image->buffer.data + header_offset); |
| 234 | cbfs_put_header(header_loc, image->header); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 235 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 236 | // The last 4 byte of the image contain the relative offset from the end |
| 237 | // of the image to the master header as a 32-bit signed integer. x86 |
| 238 | // relies on this also being its (memory-mapped, top-aligned) absolute |
| 239 | // 32-bit address by virtue of how two's complement numbers work. |
| 240 | assert(size % sizeof(int32_t) == 0); |
| 241 | rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t)); |
| 242 | *rel_offset = header_offset - size; |
| 243 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 244 | // Prepare entries |
| 245 | if (align_up(entries_offset, align) != entries_offset) { |
| 246 | ERROR("Offset (0x%x) must be aligned to 0x%x.\n", |
| 247 | entries_offset, align); |
| 248 | return -1; |
| 249 | } |
Hung-Te Lin | b02c873 | 2013-03-15 17:40:08 +0800 | [diff] [blame] | 250 | entry_header_len = cbfs_calculate_file_header_size(""); |
| 251 | if (entries_offset + entry_header_len > size) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 252 | ERROR("Offset (0x%x+0x%zx) exceed ROM size(0x%zx)\n", |
Hung-Te Lin | b02c873 | 2013-03-15 17:40:08 +0800 | [diff] [blame] | 253 | entries_offset, entry_header_len, size); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 254 | return -1; |
| 255 | } |
| 256 | entry = (struct cbfs_file *)(image->buffer.data + entries_offset); |
| 257 | // To calculate available length, find |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 258 | // e = min(bootblock, header, rel_offset) where e > entries_offset. |
| 259 | cbfs_len = size - sizeof(int32_t); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 260 | if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) |
| 261 | cbfs_len = bootblock_offset; |
| 262 | if (header_offset > entries_offset && header_offset < cbfs_len) |
| 263 | cbfs_len = header_offset; |
Hung-Te Lin | b02c873 | 2013-03-15 17:40:08 +0800 | [diff] [blame] | 264 | cbfs_len -= entries_offset + align + entry_header_len; |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 265 | cbfs_create_empty_entry(entry, cbfs_len, ""); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 266 | LOG("Created CBFS image (capacity = %d bytes)\n", cbfs_len); |
| 267 | return 0; |
| 268 | } |
| 269 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 270 | int cbfs_image_from_file(struct cbfs_image *image, |
| 271 | const char *filename, uint32_t offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 272 | { |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 273 | void *header_loc; |
| 274 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 275 | if (buffer_from_file(&image->buffer, filename) != 0) |
| 276 | return -1; |
| 277 | DEBUG("read_cbfs_image: %s (%zd bytes)\n", image->buffer.name, |
| 278 | image->buffer.size); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 279 | header_loc = cbfs_find_header(image->buffer.data, |
| 280 | image->buffer.size, |
| 281 | offset); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 282 | if (!header_loc) { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 283 | ERROR("%s does not have CBFS master header.\n", filename); |
| 284 | cbfs_image_delete(image); |
| 285 | return -1; |
| 286 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 287 | |
| 288 | if ((image->header = malloc(sizeof(*image->header))) == NULL) |
| 289 | return -1; |
| 290 | |
| 291 | cbfs_get_header(image->header, header_loc); |
| 292 | cbfs_fix_legacy_size(image, header_loc); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 297 | int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset, |
| 298 | size_t copy_size) |
| 299 | { |
| 300 | struct cbfs_file *src_entry, *dst_entry; |
| 301 | struct cbfs_header *copy_header; |
| 302 | size_t align, entry_offset; |
| 303 | ssize_t last_entry_size; |
| 304 | |
| 305 | size_t header_offset, header_end; |
| 306 | size_t cbfs_offset, cbfs_end; |
| 307 | size_t copy_end = copy_offset + copy_size; |
| 308 | |
| 309 | align = htonl(image->header->align); |
| 310 | |
| 311 | header_offset = (char *)image->header - image->buffer.data; |
| 312 | header_end = header_offset + sizeof(image->header); |
| 313 | |
| 314 | cbfs_offset = htonl(image->header->offset); |
| 315 | cbfs_end = htonl(image->header->romsize); |
| 316 | |
| 317 | if (copy_end > image->buffer.size) { |
| 318 | ERROR("Copy offset out of range: [%zx:%zx)\n", |
| 319 | copy_offset, copy_end); |
| 320 | return 1; |
| 321 | } |
| 322 | |
| 323 | /* Range check requested copy region with header and source cbfs. */ |
| 324 | if ((copy_offset >= header_offset && copy_offset < header_end) || |
| 325 | (copy_end >= header_offset && copy_end <= header_end)) { |
| 326 | ERROR("New image would overlap old header.\n"); |
| 327 | } |
| 328 | |
| 329 | if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) || |
| 330 | (copy_end >= cbfs_offset && copy_end <= cbfs_end)) { |
| 331 | ERROR("New image would overlap old one.\n"); |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | /* This will work, let's create a copy. */ |
| 336 | copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset); |
| 337 | *copy_header = *image->header; |
| 338 | |
| 339 | copy_header->bootblocksize = 0; |
| 340 | /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/ |
| 341 | copy_header->romsize = htonl(copy_end); |
| 342 | entry_offset = align_up(copy_offset + sizeof(*copy_header), align); |
| 343 | copy_header->offset = htonl(entry_offset); |
| 344 | dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset); |
| 345 | |
| 346 | /* Copy non-empty files */ |
| 347 | for (src_entry = cbfs_find_first_entry(image); |
| 348 | src_entry && cbfs_is_valid_entry(image, src_entry); |
| 349 | src_entry = cbfs_find_next_entry(image, src_entry)) { |
| 350 | size_t entry_size; |
| 351 | |
| 352 | if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) || |
| 353 | (src_entry->type == htonl(CBFS_COMPONENT_DELETED))) |
| 354 | continue; |
| 355 | |
| 356 | entry_size = htonl(src_entry->len) + htonl(src_entry->offset); |
| 357 | memcpy(dst_entry, src_entry, entry_size); |
| 358 | dst_entry = (struct cbfs_file *)( |
| 359 | (uintptr_t)dst_entry + align_up(entry_size, align)); |
| 360 | |
| 361 | if (((char *)dst_entry - image->buffer.data) >= copy_end) { |
| 362 | ERROR("Ran out of room in copy region.\n"); |
| 363 | return 1; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /* Last entry size is all the room above it. */ |
| 368 | last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data) |
| 369 | - cbfs_calculate_file_header_size(""); |
| 370 | |
| 371 | if (last_entry_size < 0) |
| 372 | WARN("No room to create the last entry!\n") |
| 373 | else |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 374 | cbfs_create_empty_entry(dst_entry, last_entry_size, ""); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 379 | int cbfs_image_write_file(struct cbfs_image *image, const char *filename) |
| 380 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 381 | assert(image && image->buffer.data); |
| 382 | return buffer_write_file(&image->buffer, filename); |
| 383 | } |
| 384 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 385 | int cbfs_image_delete(struct cbfs_image *image) |
| 386 | { |
Edward O'Callaghan | a0f9ece | 2014-03-09 00:05:18 +1100 | [diff] [blame] | 387 | if (image == NULL) |
| 388 | return 0; |
| 389 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 390 | buffer_delete(&image->buffer); |
| 391 | image->header = NULL; |
| 392 | return 0; |
| 393 | } |
| 394 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 395 | /* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */ |
| 396 | static int cbfs_add_entry_at(struct cbfs_image *image, |
| 397 | struct cbfs_file *entry, |
| 398 | uint32_t size, |
| 399 | const char *name, |
| 400 | uint32_t type, |
| 401 | const void *data, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 402 | uint32_t content_offset) |
| 403 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 404 | struct cbfs_file *next = cbfs_find_next_entry(image, entry); |
| 405 | uint32_t addr = cbfs_get_entry_addr(image, entry), |
| 406 | addr_next = cbfs_get_entry_addr(image, next); |
| 407 | uint32_t header_size = cbfs_calculate_file_header_size(name), |
| 408 | min_entry_size = cbfs_calculate_file_header_size(""); |
| 409 | uint32_t len, target; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 410 | uint32_t align = image->header->align; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 411 | |
| 412 | target = content_offset - header_size; |
| 413 | if (target % align) |
| 414 | target -= target % align; |
| 415 | if (target < addr) { |
| 416 | ERROR("No space to hold cbfs_file header."); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | // Process buffer BEFORE content_offset. |
| 421 | if (target - addr > min_entry_size) { |
| 422 | DEBUG("|min|...|header|content|... <create new entry>\n"); |
| 423 | len = target - addr - min_entry_size; |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 424 | cbfs_create_empty_entry(entry, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 425 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 426 | entry = cbfs_find_next_entry(image, entry); |
| 427 | addr = cbfs_get_entry_addr(image, entry); |
| 428 | } |
| 429 | |
| 430 | len = size + (content_offset - addr - header_size); |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 431 | cbfs_create_empty_entry(entry, len, name); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 432 | if (len != size) { |
| 433 | DEBUG("|..|header|content|... <use offset to create entry>\n"); |
| 434 | DEBUG("before: offset=0x%x, len=0x%x\n", |
| 435 | ntohl(entry->offset), ntohl(entry->len)); |
| 436 | // TODO reset expanded name buffer to 0xFF. |
| 437 | entry->offset = htonl(ntohl(entry->offset) + (len - size)); |
| 438 | entry->len = htonl(size); |
| 439 | DEBUG("after: offset=0x%x, len=0x%x\n", |
| 440 | ntohl(entry->offset), ntohl(entry->len)); |
| 441 | } |
| 442 | |
| 443 | // Ready to fill data into entry. |
| 444 | assert(ntohl(entry->len) == size); |
| 445 | entry->type = htonl(type); |
| 446 | DEBUG("content_offset: 0x%x, entry location: %x\n", |
| 447 | content_offset, (int)((char*)CBFS_SUBHEADER(entry) - |
| 448 | image->buffer.data)); |
| 449 | assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == |
| 450 | content_offset); |
| 451 | memcpy(CBFS_SUBHEADER(entry), data, size); |
| 452 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 453 | |
| 454 | // Process buffer AFTER entry. |
| 455 | entry = cbfs_find_next_entry(image, entry); |
| 456 | addr = cbfs_get_entry_addr(image, entry); |
| 457 | assert(addr < addr_next); |
| 458 | |
| 459 | if (addr_next - addr < min_entry_size) { |
| 460 | DEBUG("No space after content to keep CBFS structure.\n"); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | len = addr_next - addr - min_entry_size; |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 465 | cbfs_create_empty_entry(entry, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 466 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 471 | const char *name, uint32_t type, uint32_t content_offset) |
| 472 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 473 | uint32_t entry_type; |
| 474 | uint32_t addr, addr_next; |
| 475 | struct cbfs_file *entry, *next; |
| 476 | uint32_t header_size, need_size, new_size; |
| 477 | |
| 478 | header_size = cbfs_calculate_file_header_size(name); |
| 479 | |
| 480 | need_size = header_size + buffer->size; |
| 481 | DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", |
| 482 | name, content_offset, header_size, buffer->size, need_size); |
| 483 | |
| 484 | if (IS_TOP_ALIGNED_ADDRESS(content_offset)) { |
| 485 | // legacy cbfstool takes top-aligned address. |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 486 | uint32_t theromsize = image->header->romsize; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 487 | INFO("Converting top-aligned address 0x%x to offset: 0x%x\n", |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 488 | content_offset, content_offset + theromsize); |
| 489 | content_offset += theromsize; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // Merge empty entries. |
| 493 | DEBUG("(trying to merge empty entries...)\n"); |
| 494 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 495 | |
| 496 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 497 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 498 | entry = cbfs_find_next_entry(image, entry)) { |
| 499 | |
| 500 | entry_type = ntohl(entry->type); |
| 501 | if (entry_type != CBFS_COMPONENT_NULL) |
| 502 | continue; |
| 503 | |
| 504 | addr = cbfs_get_entry_addr(image, entry); |
| 505 | next = cbfs_find_next_entry(image, entry); |
| 506 | addr_next = cbfs_get_entry_addr(image, next); |
| 507 | |
| 508 | DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n", |
| 509 | addr, addr_next - addr, addr_next - addr); |
Aaron Durbin | 1ebc7e9 | 2014-01-21 15:28:38 -0600 | [diff] [blame] | 510 | |
| 511 | /* Will the file fit? Don't yet worry if we have space for a new |
| 512 | * "empty" entry. We take care of that later. |
| 513 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 514 | if (addr + need_size > addr_next) |
| 515 | continue; |
| 516 | |
| 517 | // Can we simply put object here? |
| 518 | if (!content_offset || content_offset == addr + header_size) { |
| 519 | DEBUG("Filling new entry data (%zd bytes).\n", |
| 520 | buffer->size); |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 521 | cbfs_create_empty_entry(entry, buffer->size, name); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 522 | entry->type = htonl(type); |
| 523 | memcpy(CBFS_SUBHEADER(entry), buffer->data, buffer->size); |
| 524 | if (verbose) |
| 525 | cbfs_print_entry_info(image, entry, stderr); |
| 526 | |
| 527 | // setup new entry |
Paul Menzel | 4159a80 | 2013-07-14 00:24:31 +0200 | [diff] [blame] | 528 | DEBUG("Setting new empty entry.\n"); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 529 | entry = cbfs_find_next_entry(image, entry); |
| 530 | new_size = (cbfs_get_entry_addr(image, next) - |
| 531 | cbfs_get_entry_addr(image, entry)); |
Aaron Durbin | 1ebc7e9 | 2014-01-21 15:28:38 -0600 | [diff] [blame] | 532 | |
| 533 | /* Entry was added and no space for new "empty" entry */ |
| 534 | if (new_size < cbfs_calculate_file_header_size("")) { |
| 535 | DEBUG("No need for new \"empty\" entry\n"); |
| 536 | /* No need to increase the size of the just |
| 537 | * stored file to extend to next file. Alignment |
| 538 | * of next file takes care of this. |
| 539 | */ |
| 540 | return 0; |
| 541 | } |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 542 | new_size -= cbfs_calculate_file_header_size(""); |
| 543 | DEBUG("new size: %d\n", new_size); |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 544 | cbfs_create_empty_entry(entry, new_size, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 545 | if (verbose) |
| 546 | cbfs_print_entry_info(image, entry, stderr); |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | // We need to put content here, and the case is really |
| 551 | // complicated... |
| 552 | assert(content_offset); |
| 553 | if (addr_next < content_offset) { |
| 554 | DEBUG("Not for specified offset yet"); |
| 555 | continue; |
| 556 | } else if (addr > content_offset) { |
| 557 | DEBUG("Exceed specified content_offset."); |
| 558 | break; |
| 559 | } else if (addr + header_size > content_offset) { |
| 560 | ERROR("Not enough space for header.\n"); |
| 561 | break; |
| 562 | } else if (content_offset + buffer->size > addr_next) { |
| 563 | ERROR("Not enough space for content.\n"); |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | // TODO there are more few tricky cases that we may |
| 568 | // want to fit by altering offset. |
| 569 | DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n", |
| 570 | addr, addr_next - addr, content_offset); |
| 571 | |
| 572 | if (cbfs_add_entry_at(image, entry, buffer->size, name, type, |
| 573 | buffer->data, content_offset) == 0) { |
| 574 | return 0; |
| 575 | } |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n", |
| 580 | buffer->name, buffer->size, buffer->size / 1024, content_offset); |
| 581 | return -1; |
| 582 | } |
| 583 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 584 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) |
| 585 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 586 | struct cbfs_file *entry; |
| 587 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 588 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 589 | entry = cbfs_find_next_entry(image, entry)) { |
| 590 | if (strcasecmp(CBFS_NAME(entry), name) == 0) { |
| 591 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 592 | return entry; |
| 593 | } |
| 594 | } |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 599 | const char *filename) |
| 600 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 601 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 602 | struct buffer buffer; |
| 603 | if (!entry) { |
| 604 | ERROR("File not found: %s\n", entry_name); |
| 605 | return -1; |
| 606 | } |
| 607 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", |
| 608 | entry_name, cbfs_get_entry_addr(image, entry), |
| 609 | get_cbfs_entry_type_name(ntohl(entry->type)), ntohl(entry->len)); |
| 610 | |
| 611 | if (ntohl(entry->type) != CBFS_COMPONENT_RAW) { |
| 612 | WARN("Only 'raw' files are safe to extract.\n"); |
| 613 | } |
| 614 | |
| 615 | buffer.data = CBFS_SUBHEADER(entry); |
| 616 | buffer.size = ntohl(entry->len); |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 617 | buffer.name = (char *)"(cbfs_export_entry)"; |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 618 | if (buffer_write_file(&buffer, filename) != 0) { |
| 619 | ERROR("Failed to write %s into %s.\n", |
| 620 | entry_name, filename); |
| 621 | return -1; |
| 622 | } |
| 623 | INFO("Successfully dumped the file to: %s\n", filename); |
| 624 | return 0; |
| 625 | } |
| 626 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 627 | int cbfs_remove_entry(struct cbfs_image *image, const char *name) |
| 628 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 629 | struct cbfs_file *entry, *next; |
| 630 | size_t len; |
| 631 | entry = cbfs_get_entry(image, name); |
| 632 | if (!entry) { |
| 633 | ERROR("CBFS file %s not found.\n", name); |
| 634 | return -1; |
| 635 | } |
| 636 | next = cbfs_find_next_entry(image, entry); |
| 637 | assert(next); |
| 638 | DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", |
| 639 | CBFS_NAME(entry), cbfs_get_entry_addr(image, entry)); |
| 640 | entry->type = htonl(CBFS_COMPONENT_DELETED); |
| 641 | len = (cbfs_get_entry_addr(image, next) - |
| 642 | cbfs_get_entry_addr(image, entry)); |
| 643 | entry->offset = htonl(cbfs_calculate_file_header_size("")); |
| 644 | entry->len = htonl(len - ntohl(entry->offset)); |
| 645 | memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry)); |
| 646 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, |
| 647 | ntohl(entry->len)); |
| 648 | return 0; |
| 649 | } |
| 650 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 651 | int cbfs_print_header_info(struct cbfs_image *image) |
| 652 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 653 | char *name = strdup(image->buffer.name); |
| 654 | assert(image && image->header); |
| 655 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 656 | "alignment: %d bytes, architecture: %s\n\n", |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 657 | basename(name), |
| 658 | image->buffer.size / 1024, |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 659 | image->header->bootblocksize, |
| 660 | image->header->romsize, |
| 661 | image->header->offset, |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 662 | image->header->align, |
| 663 | arch_to_string(image->header->architecture)); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 664 | free(name); |
| 665 | return 0; |
| 666 | } |
| 667 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 668 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) |
| 669 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 670 | fprintf(fp, |
| 671 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 672 | "length: %d/%d\n", |
| 673 | lookup_name_by_type(types_cbfs_compression, |
| 674 | stage->compression, "(unknown)"), |
| 675 | stage->entry, |
| 676 | stage->load, |
| 677 | stage->len, |
| 678 | stage->memlen); |
| 679 | return 0; |
| 680 | } |
| 681 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 682 | static int cbfs_print_decoded_payload_segment_info( |
| 683 | struct cbfs_payload_segment *seg, FILE *fp) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 684 | { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 685 | /* The input (seg) must be already decoded by |
| 686 | * cbfs_decode_payload_segment. |
| 687 | */ |
| 688 | switch (seg->type) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 689 | case PAYLOAD_SEGMENT_CODE: |
| 690 | case PAYLOAD_SEGMENT_DATA: |
| 691 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 692 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 693 | (seg->type == PAYLOAD_SEGMENT_CODE ? |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 694 | "code " : "data"), |
| 695 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 696 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 697 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 698 | seg->offset, seg->load_addr, seg->len, |
| 699 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 700 | break; |
| 701 | |
| 702 | case PAYLOAD_SEGMENT_ENTRY: |
| 703 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 704 | seg->load_addr); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 705 | break; |
| 706 | |
| 707 | case PAYLOAD_SEGMENT_BSS: |
| 708 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 709 | "length 0x%x)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 710 | seg->load_addr, seg->len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 711 | break; |
| 712 | |
| 713 | case PAYLOAD_SEGMENT_PARAMS: |
| 714 | fprintf(fp, " parameters\n"); |
| 715 | break; |
| 716 | |
| 717 | default: |
| 718 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 719 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 720 | seg->type, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 721 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 722 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 723 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 724 | seg->offset, seg->load_addr, seg->len, |
| 725 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | int cbfs_print_entry_info(struct cbfs_image *image, struct cbfs_file *entry, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 732 | void *arg) |
| 733 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 734 | const char *name = CBFS_NAME(entry); |
| 735 | struct cbfs_payload_segment *payload; |
| 736 | FILE *fp = (FILE *)arg; |
| 737 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 738 | if (!cbfs_is_valid_entry(image, entry)) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 739 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 740 | cbfs_get_entry_addr(image, entry)); |
| 741 | return -1; |
| 742 | } |
| 743 | if (!fp) |
| 744 | fp = stdout; |
| 745 | |
| 746 | fprintf(fp, "%-30s 0x%-8x %-12s %d\n", |
| 747 | *name ? name : "(empty)", |
| 748 | cbfs_get_entry_addr(image, entry), |
| 749 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 750 | ntohl(entry->len)); |
| 751 | |
| 752 | if (!verbose) |
| 753 | return 0; |
| 754 | |
| 755 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 756 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 757 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 758 | ntohl(entry->len)); |
| 759 | |
| 760 | /* note the components of the subheader may be in host order ... */ |
| 761 | switch (ntohl(entry->type)) { |
| 762 | case CBFS_COMPONENT_STAGE: |
| 763 | cbfs_print_stage_info((struct cbfs_stage *) |
| 764 | CBFS_SUBHEADER(entry), fp); |
| 765 | break; |
| 766 | |
| 767 | case CBFS_COMPONENT_PAYLOAD: |
| 768 | payload = (struct cbfs_payload_segment *) |
| 769 | CBFS_SUBHEADER(entry); |
| 770 | while (payload) { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 771 | struct cbfs_payload_segment seg; |
| 772 | cbfs_decode_payload_segment(&seg, payload); |
| 773 | cbfs_print_decoded_payload_segment_info( |
| 774 | &seg, fp); |
| 775 | if (seg.type == PAYLOAD_SEGMENT_ENTRY) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 776 | break; |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 777 | else |
Aaron Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 778 | payload ++; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 779 | } |
| 780 | break; |
| 781 | default: |
| 782 | break; |
| 783 | } |
| 784 | return 0; |
| 785 | } |
| 786 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 787 | int cbfs_print_directory(struct cbfs_image *image) |
| 788 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 789 | cbfs_print_header_info(image); |
| 790 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 791 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 792 | return 0; |
| 793 | } |
| 794 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 795 | int cbfs_merge_empty_entry(struct cbfs_image *image, struct cbfs_file *entry, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 796 | void *arg) |
| 797 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 798 | struct cbfs_file *next; |
| 799 | uint32_t type, addr, last_addr; |
| 800 | |
| 801 | type = ntohl(entry->type); |
| 802 | if (type == CBFS_COMPONENT_DELETED) { |
| 803 | // Ready to be recycled. |
| 804 | type = CBFS_COMPONENT_NULL; |
| 805 | entry->type = htonl(type); |
| 806 | } |
| 807 | if (type != CBFS_COMPONENT_NULL) |
| 808 | return 0; |
| 809 | |
| 810 | next = cbfs_find_next_entry(image, entry); |
| 811 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 812 | while (next && cbfs_is_valid_entry(image, next)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 813 | type = ntohl(next->type); |
| 814 | if (type == CBFS_COMPONENT_DELETED) { |
| 815 | type = CBFS_COMPONENT_NULL; |
| 816 | next->type = htonl(type); |
| 817 | } |
| 818 | if (type != CBFS_COMPONENT_NULL) |
| 819 | return 0; |
| 820 | |
| 821 | addr = cbfs_get_entry_addr(image, entry); |
| 822 | last_addr = cbfs_get_entry_addr( |
| 823 | image, cbfs_find_next_entry(image, next)); |
| 824 | |
| 825 | // Now, we find two deleted/empty entries; try to merge now. |
| 826 | DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n", |
| 827 | cbfs_get_entry_addr(image, entry), ntohl(entry->len), |
| 828 | cbfs_get_entry_addr(image, next), ntohl(next->len)); |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 829 | cbfs_create_empty_entry(entry, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 830 | (last_addr - addr - |
| 831 | cbfs_calculate_file_header_size("")), |
| 832 | ""); |
| 833 | DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len)); |
| 834 | next = cbfs_find_next_entry(image, entry); |
| 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 839 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 840 | void *arg) |
| 841 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 842 | int count = 0; |
| 843 | struct cbfs_file *entry; |
| 844 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 845 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 846 | entry = cbfs_find_next_entry(image, entry)) { |
| 847 | count ++; |
| 848 | if (callback(image, entry, arg) != 0) |
| 849 | break; |
| 850 | } |
| 851 | return count; |
| 852 | } |
| 853 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 854 | static int cbfs_header_valid(struct cbfs_header *header, size_t size) |
| 855 | { |
| 856 | if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && |
| 857 | ((ntohl(header->version) == CBFS_HEADER_VERSION1) || |
| 858 | (ntohl(header->version) == CBFS_HEADER_VERSION2)) && |
| 859 | (ntohl(header->romsize) <= size) && |
| 860 | (ntohl(header->offset) < ntohl(header->romsize))) |
| 861 | return 1; |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 866 | uint32_t forced_offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 867 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 868 | size_t offset; |
| 869 | int found = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 870 | int32_t rel_offset; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 871 | struct cbfs_header *header, *result = NULL; |
| 872 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 873 | if (forced_offset < (size - sizeof(struct cbfs_header))) { |
| 874 | /* Check if the forced header is valid. */ |
| 875 | header = (struct cbfs_header *)(data + forced_offset); |
| 876 | if (cbfs_header_valid(header, size)) |
| 877 | return header; |
| 878 | return NULL; |
| 879 | } |
| 880 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 881 | // Try finding relative offset of master header at end of file first. |
| 882 | rel_offset = *(int32_t *)(data + size - sizeof(int32_t)); |
| 883 | offset = size + rel_offset; |
| 884 | DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n", |
| 885 | (size_t)rel_offset, (size_t)-rel_offset, offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 886 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 887 | if (offset >= size - sizeof(*header) || |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 888 | !cbfs_header_valid((struct cbfs_header *)(data + offset), size)) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 889 | // Some use cases append non-CBFS data to the end of the ROM. |
| 890 | DEBUG("relative offset seems wrong, scanning whole image...\n"); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 891 | offset = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 892 | } |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 893 | |
| 894 | for (; offset + sizeof(*header) < size; offset++) { |
| 895 | header = (struct cbfs_header *)(data + offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 896 | if (!cbfs_header_valid(header, size)) |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 897 | continue; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 898 | if (!found++) |
| 899 | result = header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 900 | } |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 901 | if (found > 1) |
| 902 | // Top-aligned images usually have a working relative offset |
| 903 | // field, so this is more likely to happen on bottom-aligned |
| 904 | // ones (where the first header is the "outermost" one) |
| 905 | WARN("Multiple (%d) CBFS headers found, using the first one.\n", |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 906 | found); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 907 | return result; |
| 908 | } |
| 909 | |
| 910 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 911 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) |
| 912 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 913 | assert(image && image->header); |
| 914 | return (struct cbfs_file *)(image->buffer.data + |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 915 | image->header->offset); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 919 | struct cbfs_file *entry) |
| 920 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 921 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 922 | int align = image->header->align; |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 923 | assert(entry && cbfs_is_valid_entry(image, entry)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 924 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 925 | addr = align_up(addr, align); |
| 926 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 927 | } |
| 928 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 929 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) |
| 930 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 931 | assert(image && image->buffer.data && entry); |
| 932 | return (int32_t)((char *)entry - image->buffer.data); |
| 933 | } |
| 934 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 935 | int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry) |
| 936 | { |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 937 | return (entry && |
| 938 | (char *)entry >= image->buffer.data && |
| 939 | (char *)entry + sizeof(entry->magic) < |
| 940 | image->buffer.data + image->buffer.size && |
| 941 | memcmp(entry->magic, CBFS_FILE_MAGIC, |
| 942 | sizeof(entry->magic)) == 0); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 943 | } |
| 944 | |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 945 | int cbfs_create_empty_entry(struct cbfs_file *entry, |
| 946 | size_t len, const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 947 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 948 | memset(entry, CBFS_CONTENT_DEFAULT_VALUE, sizeof(*entry)); |
| 949 | memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); |
| 950 | entry->type = htonl(CBFS_COMPONENT_NULL); |
| 951 | entry->len = htonl(len); |
| 952 | entry->checksum = 0; // TODO Build a checksum algorithm. |
| 953 | entry->offset = htonl(cbfs_calculate_file_header_size(name)); |
| 954 | memset(CBFS_NAME(entry), 0, ntohl(entry->offset) - sizeof(*entry)); |
| 955 | strcpy(CBFS_NAME(entry), name); |
| 956 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); |
| 957 | return 0; |
| 958 | } |
| 959 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 960 | /* Finds a place to hold whole data in same memory page. */ |
| 961 | static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) |
| 962 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 963 | if (!page) |
| 964 | return 1; |
| 965 | return (start / page) == (start + size - 1) / page; |
| 966 | } |
| 967 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 968 | /* Tests if data can fit in a range by given offset: |
| 969 | * start ->| header_len | offset (+ size) |<- end |
| 970 | */ |
| 971 | static int is_in_range(uint32_t start, uint32_t end, uint32_t header_len, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 972 | uint32_t offset, uint32_t size) |
| 973 | { |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 974 | return (offset >= start + header_len && offset + size <= end); |
| 975 | } |
| 976 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 977 | int32_t cbfs_locate_entry(struct cbfs_image *image, const char *name, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 978 | uint32_t size, uint32_t page_size, uint32_t align) |
| 979 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 980 | struct cbfs_file *entry; |
| 981 | size_t need_len; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 982 | uint32_t addr, addr_next, addr2, addr3, offset, header_len; |
| 983 | |
| 984 | /* Default values: allow fitting anywhere in ROM. */ |
| 985 | if (!page_size) |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 986 | page_size = image->header->romsize; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 987 | if (!align) |
| 988 | align = 1; |
| 989 | |
| 990 | if (size > page_size) |
| 991 | ERROR("Input file size (%d) greater than page size (%d).\n", |
| 992 | size, page_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 993 | |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 994 | if (page_size % image->header->align) |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 995 | WARN("%s: Page size (%#x) not aligned with CBFS image (%#x).\n", |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 996 | __func__, page_size, image->header->align); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 997 | |
| 998 | /* TODO Old cbfstool always assume input is a stage file (and adding |
| 999 | * sizeof(cbfs_stage) for header. We should fix that by adding "-t" |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1000 | * (type) param in future. For right now, we assume cbfs_stage is the |
| 1001 | * largest structure and add it into header size. */ |
| 1002 | assert(sizeof(struct cbfs_stage) >= sizeof(struct cbfs_payload)); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1003 | header_len = (cbfs_calculate_file_header_size(name) + |
| 1004 | sizeof(struct cbfs_stage)); |
| 1005 | need_len = header_len + size; |
| 1006 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1007 | // Merge empty entries to build get max available space. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1008 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 1009 | |
| 1010 | /* Three cases of content location on memory page: |
| 1011 | * case 1. |
| 1012 | * | PAGE 1 | PAGE 2 | |
| 1013 | * | <header><content>| Fit. Return start of content. |
| 1014 | * |
| 1015 | * case 2. |
| 1016 | * | PAGE 1 | PAGE 2 | |
| 1017 | * | <header><content> | Fits when we shift content to align |
| 1018 | * shift-> | <header>|<content> | at starting of PAGE 2. |
| 1019 | * |
| 1020 | * case 3. (large content filling whole page) |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1021 | * | PAGE 1 | PAGE 2 | PAGE 3 | |
| 1022 | * | <header>< content > | Can't fit. If we shift content to |
| 1023 | * |trial-> <header>< content > | PAGE 2, header can't fit in free |
| 1024 | * | shift-> <header><content> space, so we must use PAGE 3. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1025 | * |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1026 | * The returned address can be then used as "base-address" (-b) in add-* |
| 1027 | * commands (will be re-calculated and positioned by cbfs_add_entry_at). |
| 1028 | * For stage targets, the address is also used to re-link stage before |
| 1029 | * being added into CBFS. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1030 | */ |
| 1031 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1032 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1033 | entry = cbfs_find_next_entry(image, entry)) { |
| 1034 | |
| 1035 | uint32_t type = ntohl(entry->type); |
| 1036 | if (type != CBFS_COMPONENT_NULL) |
| 1037 | continue; |
| 1038 | |
| 1039 | addr = cbfs_get_entry_addr(image, entry); |
| 1040 | addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry( |
| 1041 | image, entry)); |
| 1042 | if (addr_next - addr < need_len) |
| 1043 | continue; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1044 | |
| 1045 | offset = align_up(addr + header_len, align); |
| 1046 | if (is_in_same_page(offset, size, page_size) && |
| 1047 | is_in_range(addr, addr_next, header_len, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1048 | DEBUG("cbfs_locate_entry: FIT (PAGE1)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1049 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | addr2 = align_up(addr, page_size); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1053 | offset = align_up(addr2, align); |
| 1054 | if (is_in_range(addr, addr_next, header_len, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1055 | DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1056 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1059 | /* Assume page_size >= header_len so adding one page will |
| 1060 | * definitely provide the space for header. */ |
| 1061 | assert(page_size >= header_len); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1062 | addr3 = addr2 + page_size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1063 | offset = align_up(addr3, align); |
| 1064 | if (is_in_range(addr, addr_next, header_len, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1065 | DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1066 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | return -1; |
| 1070 | } |