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