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. |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 16 | #include <inttypes.h> |
| 17 | #include <libgen.h> |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 18 | #include <stddef.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 22 | #include <strings.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 23 | |
| 24 | #include "common.h" |
| 25 | #include "cbfs_image.h" |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 26 | #include "elfparsing.h" |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 27 | #include "rmodule.h" |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 28 | |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 29 | /* Even though the file-adding functions---cbfs_add_entry() and |
| 30 | * cbfs_add_entry_at()---perform their sizing checks against the beginning of |
| 31 | * the subsequent section rather than a stable recorded value such as an empty |
| 32 | * file header's len field, it's possible to prove two interesting properties |
| 33 | * about their behavior: |
| 34 | * - Placing a new file within an empty entry located below an existing file |
| 35 | * entry will never leave an aligned flash address containing neither the |
| 36 | * beginning of a file header nor part of a file. |
| 37 | * - Placing a new file in an empty entry at the very end of the image such |
| 38 | * that it fits, but leaves no room for a final header, is guaranteed not to |
| 39 | * change the total amount of space for entries, even if that new file is |
| 40 | * later removed from the CBFS. |
| 41 | * These properties are somewhat nonobvious from the implementation, so the |
| 42 | * reader is encouraged to blame this comment and examine the full proofs |
| 43 | * in the commit message before making significant changes that would risk |
| 44 | * removing said guarantees. |
| 45 | */ |
| 46 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 47 | /* The file name align is not defined in CBFS spec -- only a preference by |
| 48 | * (old) cbfstool. */ |
| 49 | #define CBFS_FILENAME_ALIGN (16) |
| 50 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 51 | /* Type and format */ |
| 52 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 53 | static const struct typedesc_t types_cbfs_compression[] = { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 54 | {CBFS_COMPRESS_NONE, "none"}, |
| 55 | {CBFS_COMPRESS_LZMA, "LZMA"}, |
Sol Boucher | 5bb90e6 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 56 | {0, NULL} |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 57 | }; |
| 58 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 59 | 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] | 60 | const char *default_value) |
| 61 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 62 | int i; |
| 63 | for (i = 0; desc[i].name; i++) |
| 64 | if (desc[i].type == type) |
| 65 | return desc[i].name; |
| 66 | return default_value; |
| 67 | } |
| 68 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 69 | static int lookup_type_by_name(const struct typedesc_t *desc, const char *name) |
| 70 | { |
| 71 | int i; |
| 72 | for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i); |
| 73 | return desc[i].name ? (int)desc[i].type : -1; |
| 74 | } |
| 75 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 76 | static const char *get_cbfs_entry_type_name(uint32_t type) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 77 | { |
Patrick Georgi | dc37dab | 2015-09-09 16:46:00 +0200 | [diff] [blame] | 78 | return lookup_name_by_type(filetypes, type, "(unknown)"); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 81 | int cbfs_parse_comp_algo(const char *name) |
| 82 | { |
| 83 | return lookup_type_by_name(types_cbfs_compression, name); |
| 84 | } |
| 85 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 86 | static const char *get_hash_attr_name(uint16_t hash_type) |
| 87 | { |
| 88 | return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)"); |
| 89 | } |
| 90 | |
| 91 | int cbfs_parse_hash_algo(const char *name) |
| 92 | { |
| 93 | return lookup_type_by_name(types_cbfs_hash, name); |
| 94 | } |
| 95 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 96 | /* CBFS image */ |
| 97 | |
Patrick Georgi | 11ee08f | 2015-08-11 15:10:02 +0200 | [diff] [blame] | 98 | size_t cbfs_calculate_file_header_size(const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 99 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 100 | return (sizeof(struct cbfs_file) + |
| 101 | align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN)); |
| 102 | } |
| 103 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 104 | /* Only call on legacy CBFSes possessing a master header. */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 105 | 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] | 106 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 107 | assert(image); |
| 108 | assert(cbfs_is_legacy_cbfs(image)); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 109 | // A bug in old cbfstool may produce extra few bytes (by alignment) and |
| 110 | // cause cbfstool to overwrite things after free space -- which is |
| 111 | // usually CBFS header on x86. We need to workaround that. |
| 112 | |
| 113 | struct cbfs_file *entry, *first = NULL, *last = NULL; |
| 114 | for (first = entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 115 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 116 | entry = cbfs_find_next_entry(image, entry)) { |
| 117 | last = entry; |
| 118 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 119 | if ((char *)first < (char *)hdr_loc && |
| 120 | (char *)entry > (char *)hdr_loc) { |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 121 | WARN("CBFS image was created with old cbfstool with size bug. " |
| 122 | "Fixing size in last entry...\n"); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 123 | last->len = htonl(ntohl(last->len) - image->header.align); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 124 | DEBUG("Last entry has been changed from 0x%x to 0x%x.\n", |
| 125 | cbfs_get_entry_addr(image, entry), |
| 126 | cbfs_get_entry_addr(image, |
| 127 | cbfs_find_next_entry(image, last))); |
| 128 | } |
| 129 | return 0; |
| 130 | } |
| 131 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 132 | void cbfs_put_header(void *dest, const struct cbfs_header *header) |
| 133 | { |
| 134 | struct buffer outheader; |
| 135 | |
| 136 | outheader.data = dest; |
| 137 | outheader.size = 0; |
| 138 | |
| 139 | xdr_be.put32(&outheader, header->magic); |
| 140 | xdr_be.put32(&outheader, header->version); |
| 141 | xdr_be.put32(&outheader, header->romsize); |
| 142 | xdr_be.put32(&outheader, header->bootblocksize); |
| 143 | xdr_be.put32(&outheader, header->align); |
| 144 | xdr_be.put32(&outheader, header->offset); |
| 145 | xdr_be.put32(&outheader, header->architecture); |
| 146 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 147 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 148 | static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output, |
| 149 | struct cbfs_payload_segment *input) |
| 150 | { |
| 151 | struct buffer seg = { |
| 152 | .data = (void *)input, |
| 153 | .size = sizeof(*input), |
| 154 | }; |
| 155 | output->type = xdr_be.get32(&seg); |
| 156 | output->compression = xdr_be.get32(&seg); |
| 157 | output->offset = xdr_be.get32(&seg); |
| 158 | output->load_addr = xdr_be.get64(&seg); |
| 159 | output->len = xdr_be.get32(&seg); |
| 160 | output->mem_len = xdr_be.get32(&seg); |
| 161 | assert(seg.size == 0); |
| 162 | } |
| 163 | |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 164 | static int cbfs_file_get_compression_info(struct cbfs_file *entry, |
| 165 | uint32_t *decompressed_size) |
| 166 | { |
| 167 | unsigned int compression = CBFS_COMPRESS_NONE; |
| 168 | *decompressed_size = ntohl(entry->len); |
| 169 | for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); |
| 170 | attr != NULL; |
| 171 | attr = cbfs_file_next_attr(entry, attr)) { |
| 172 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) { |
| 173 | struct cbfs_file_attr_compression *ac = |
| 174 | (struct cbfs_file_attr_compression *)attr; |
| 175 | compression = ntohl(ac->compression); |
| 176 | if (decompressed_size) |
| 177 | *decompressed_size = |
| 178 | ntohl(ac->decompressed_size); |
| 179 | } |
| 180 | } |
| 181 | return compression; |
| 182 | } |
| 183 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 184 | static struct cbfs_file_attr_hash *cbfs_file_get_next_hash( |
| 185 | struct cbfs_file *entry, struct cbfs_file_attr_hash *cur) |
| 186 | { |
| 187 | struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur; |
| 188 | if (attr == NULL) { |
| 189 | attr = cbfs_file_first_attr(entry); |
| 190 | if (attr == NULL) |
| 191 | return NULL; |
| 192 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 193 | return (struct cbfs_file_attr_hash *)attr; |
| 194 | } |
| 195 | while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) { |
| 196 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 197 | return (struct cbfs_file_attr_hash *)attr; |
| 198 | }; |
| 199 | return NULL; |
| 200 | } |
| 201 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 202 | void cbfs_get_header(struct cbfs_header *header, void *src) |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 203 | { |
| 204 | struct buffer outheader; |
| 205 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 206 | outheader.data = src; /* We're not modifying the data */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 207 | outheader.size = 0; |
| 208 | |
| 209 | header->magic = xdr_be.get32(&outheader); |
| 210 | header->version = xdr_be.get32(&outheader); |
| 211 | header->romsize = xdr_be.get32(&outheader); |
| 212 | header->bootblocksize = xdr_be.get32(&outheader); |
| 213 | header->align = xdr_be.get32(&outheader); |
| 214 | header->offset = xdr_be.get32(&outheader); |
| 215 | header->architecture = xdr_be.get32(&outheader); |
| 216 | } |
| 217 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 218 | int cbfs_image_create(struct cbfs_image *image, size_t entries_size) |
| 219 | { |
| 220 | assert(image); |
| 221 | assert(image->buffer.data); |
| 222 | |
| 223 | size_t empty_header_len = cbfs_calculate_file_header_size(""); |
| 224 | uint32_t entries_offset = 0; |
| 225 | uint32_t align = CBFS_ENTRY_ALIGNMENT; |
| 226 | if (image->has_header) { |
| 227 | entries_offset = image->header.offset; |
| 228 | |
| 229 | if (entries_offset > image->buffer.size) { |
| 230 | ERROR("CBFS file entries are located outside CBFS itself\n"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | align = image->header.align; |
| 235 | } |
| 236 | |
| 237 | // This attribute must be given in order to prove that this module |
| 238 | // correctly preserves certain CBFS properties. See the block comment |
| 239 | // near the top of this file (and the associated commit message). |
| 240 | if (align < empty_header_len) { |
| 241 | ERROR("CBFS must be aligned to at least %zu bytes\n", |
| 242 | empty_header_len); |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (entries_size > image->buffer.size - entries_offset) { |
| 247 | ERROR("CBFS doesn't have enough space to fit its file entries\n"); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | if (empty_header_len > entries_size) { |
| 252 | ERROR("CBFS is too small to fit any header\n"); |
| 253 | return -1; |
| 254 | } |
| 255 | struct cbfs_file *entry_header = |
| 256 | (struct cbfs_file *)(image->buffer.data + entries_offset); |
| 257 | // This alignment is necessary in order to prove that this module |
| 258 | // correctly preserves certain CBFS properties. See the block comment |
| 259 | // near the top of this file (and the associated commit message). |
| 260 | entries_size -= entries_size % align; |
| 261 | |
| 262 | size_t capacity = entries_size - empty_header_len; |
| 263 | LOG("Created CBFS (capacity = %zu bytes)\n", capacity); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 264 | return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL, |
| 265 | capacity, ""); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int cbfs_legacy_image_create(struct cbfs_image *image, |
| 269 | uint32_t architecture, |
| 270 | uint32_t align, |
| 271 | struct buffer *bootblock, |
| 272 | uint32_t bootblock_offset, |
| 273 | uint32_t header_offset, |
| 274 | uint32_t entries_offset) |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 275 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 276 | assert(image); |
| 277 | assert(image->buffer.data); |
| 278 | assert(bootblock); |
| 279 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 280 | int32_t *rel_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 281 | uint32_t cbfs_len; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 282 | void *header_loc; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 283 | size_t size = image->buffer.size; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 284 | |
| 285 | DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, " |
| 286 | "header=0x%x+0x%zx, entries_offset=0x%x\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 287 | bootblock_offset, bootblock->size, header_offset, |
| 288 | sizeof(image->header), entries_offset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 289 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 290 | // Adjust legacy top-aligned address to ROM offset. |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 291 | if (IS_TOP_ALIGNED_ADDRESS(entries_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 292 | entries_offset = size + (int32_t)entries_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 293 | if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 294 | bootblock_offset = size + (int32_t)bootblock_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 295 | if (IS_TOP_ALIGNED_ADDRESS(header_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 296 | header_offset = size + (int32_t)header_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 297 | |
| 298 | DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, " |
| 299 | "header=0x%x, entries_offset=0x%x\n", |
| 300 | bootblock_offset, header_offset, entries_offset); |
| 301 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 302 | // Prepare bootblock |
| 303 | if (bootblock_offset + bootblock->size > size) { |
| 304 | ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
| 305 | bootblock_offset, bootblock->size, size); |
| 306 | return -1; |
| 307 | } |
Hung-Te Lin | c5ff648 | 2013-02-06 12:41:49 +0800 | [diff] [blame] | 308 | if (entries_offset > bootblock_offset && |
| 309 | entries_offset < bootblock->size) { |
| 310 | ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n", |
| 311 | bootblock_offset, bootblock->size, entries_offset); |
| 312 | return -1; |
| 313 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 314 | memcpy(image->buffer.data + bootblock_offset, bootblock->data, |
| 315 | bootblock->size); |
| 316 | |
| 317 | // Prepare header |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 318 | if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 319 | ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 320 | header_offset, sizeof(image->header), size); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 321 | return -1; |
| 322 | } |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 323 | image->header.magic = CBFS_HEADER_MAGIC; |
| 324 | image->header.version = CBFS_HEADER_VERSION; |
| 325 | image->header.romsize = size; |
| 326 | image->header.bootblocksize = bootblock->size; |
| 327 | image->header.align = align; |
| 328 | image->header.offset = entries_offset; |
| 329 | image->header.architecture = architecture; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 330 | |
| 331 | header_loc = (image->buffer.data + header_offset); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 332 | cbfs_put_header(header_loc, &image->header); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 333 | image->has_header = true; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 334 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 335 | // The last 4 byte of the image contain the relative offset from the end |
| 336 | // of the image to the master header as a 32-bit signed integer. x86 |
| 337 | // relies on this also being its (memory-mapped, top-aligned) absolute |
| 338 | // 32-bit address by virtue of how two's complement numbers work. |
| 339 | assert(size % sizeof(int32_t) == 0); |
| 340 | rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t)); |
| 341 | *rel_offset = header_offset - size; |
| 342 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 343 | // Prepare entries |
| 344 | if (align_up(entries_offset, align) != entries_offset) { |
| 345 | ERROR("Offset (0x%x) must be aligned to 0x%x.\n", |
| 346 | entries_offset, align); |
| 347 | return -1; |
| 348 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 349 | // To calculate available length, find |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 350 | // e = min(bootblock, header, rel_offset) where e > entries_offset. |
| 351 | cbfs_len = size - sizeof(int32_t); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 352 | if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) |
| 353 | cbfs_len = bootblock_offset; |
| 354 | if (header_offset > entries_offset && header_offset < cbfs_len) |
| 355 | cbfs_len = header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 356 | |
| 357 | if (cbfs_image_create(image, cbfs_len - entries_offset)) |
| 358 | return -1; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 362 | int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in, |
| 363 | uint32_t offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 364 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 365 | assert(out); |
| 366 | assert(in); |
| 367 | assert(in->data); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 368 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 369 | buffer_clone(&out->buffer, in); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 370 | out->has_header = false; |
| 371 | |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 372 | if (cbfs_is_valid_cbfs(out)) { |
| 373 | return 0; |
| 374 | } |
| 375 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 376 | void *header_loc = cbfs_find_header(in->data, in->size, offset); |
| 377 | if (header_loc) { |
| 378 | cbfs_get_header(&out->header, header_loc); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 379 | out->has_header = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 380 | cbfs_fix_legacy_size(out, header_loc); |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 381 | return 0; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 382 | } else if (offset != ~0u) { |
| 383 | ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n"); |
| 384 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 385 | } |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 386 | ERROR("Selected image region is not a valid CBFS.\n"); |
| 387 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 388 | } |
| 389 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 390 | int cbfs_copy_instance(struct cbfs_image *image, size_t copy_offset, |
| 391 | size_t copy_size) |
| 392 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 393 | assert(image); |
| 394 | if (!cbfs_is_legacy_cbfs(image)) |
| 395 | return -1; |
| 396 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 397 | struct cbfs_file *src_entry, *dst_entry; |
| 398 | struct cbfs_header *copy_header; |
| 399 | size_t align, entry_offset; |
| 400 | ssize_t last_entry_size; |
| 401 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 402 | size_t cbfs_offset, cbfs_end; |
| 403 | size_t copy_end = copy_offset + copy_size; |
| 404 | |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 405 | align = image->header.align; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 406 | |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 407 | cbfs_offset = image->header.offset; |
| 408 | cbfs_end = image->header.romsize; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 409 | |
| 410 | if (copy_end > image->buffer.size) { |
| 411 | ERROR("Copy offset out of range: [%zx:%zx)\n", |
| 412 | copy_offset, copy_end); |
| 413 | return 1; |
| 414 | } |
| 415 | |
Sol Boucher | 297c88c | 2015-05-05 15:35:18 -0700 | [diff] [blame] | 416 | /* Range check requested copy region with source cbfs. */ |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 417 | if ((copy_offset >= cbfs_offset && copy_offset < cbfs_end) || |
| 418 | (copy_end >= cbfs_offset && copy_end <= cbfs_end)) { |
| 419 | ERROR("New image would overlap old one.\n"); |
| 420 | return 1; |
| 421 | } |
| 422 | |
| 423 | /* This will work, let's create a copy. */ |
| 424 | copy_header = (struct cbfs_header *)(image->buffer.data + copy_offset); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 425 | cbfs_put_header(copy_header, &image->header); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 426 | |
| 427 | copy_header->bootblocksize = 0; |
| 428 | /* Romsize is a misnomer. It's the absolute limit of cbfs content.*/ |
| 429 | copy_header->romsize = htonl(copy_end); |
| 430 | entry_offset = align_up(copy_offset + sizeof(*copy_header), align); |
| 431 | copy_header->offset = htonl(entry_offset); |
| 432 | dst_entry = (struct cbfs_file *)(image->buffer.data + entry_offset); |
| 433 | |
| 434 | /* Copy non-empty files */ |
| 435 | for (src_entry = cbfs_find_first_entry(image); |
| 436 | src_entry && cbfs_is_valid_entry(image, src_entry); |
| 437 | src_entry = cbfs_find_next_entry(image, src_entry)) { |
| 438 | size_t entry_size; |
| 439 | |
| 440 | if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) || |
| 441 | (src_entry->type == htonl(CBFS_COMPONENT_DELETED))) |
| 442 | continue; |
| 443 | |
| 444 | entry_size = htonl(src_entry->len) + htonl(src_entry->offset); |
| 445 | memcpy(dst_entry, src_entry, entry_size); |
| 446 | dst_entry = (struct cbfs_file *)( |
| 447 | (uintptr_t)dst_entry + align_up(entry_size, align)); |
| 448 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 449 | if ((size_t)((char *)dst_entry - image->buffer.data) >= |
| 450 | copy_end) { |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 451 | ERROR("Ran out of room in copy region.\n"); |
| 452 | return 1; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /* Last entry size is all the room above it. */ |
| 457 | last_entry_size = copy_end - ((char *)dst_entry - image->buffer.data) |
| 458 | - cbfs_calculate_file_header_size(""); |
| 459 | |
| 460 | if (last_entry_size < 0) |
| 461 | WARN("No room to create the last entry!\n") |
| 462 | else |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 463 | cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL, |
| 464 | last_entry_size, ""); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 469 | int cbfs_image_delete(struct cbfs_image *image) |
| 470 | { |
Edward O'Callaghan | a0f9ece | 2014-03-09 00:05:18 +1100 | [diff] [blame] | 471 | if (image == NULL) |
| 472 | return 0; |
| 473 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 474 | buffer_delete(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 478 | /* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */ |
| 479 | static int cbfs_add_entry_at(struct cbfs_image *image, |
| 480 | struct cbfs_file *entry, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 481 | const void *data, |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 482 | uint32_t content_offset, |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 483 | const struct cbfs_file *header) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 484 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 485 | struct cbfs_file *next = cbfs_find_next_entry(image, entry); |
| 486 | uint32_t addr = cbfs_get_entry_addr(image, entry), |
| 487 | addr_next = cbfs_get_entry_addr(image, next); |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 488 | uint32_t min_entry_size = cbfs_calculate_file_header_size(""); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 489 | uint32_t len, header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 490 | uint32_t align = image->has_header ? image->header.align : |
| 491 | CBFS_ENTRY_ALIGNMENT; |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 492 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 493 | |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 494 | header_offset = content_offset - header_size; |
| 495 | if (header_offset % align) |
| 496 | header_offset -= header_offset % align; |
| 497 | if (header_offset < addr) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 498 | ERROR("No space to hold cbfs_file header."); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | // Process buffer BEFORE content_offset. |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 503 | if (header_offset - addr > min_entry_size) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 504 | DEBUG("|min|...|header|content|... <create new entry>\n"); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 505 | len = header_offset - addr - min_entry_size; |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 506 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 507 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 508 | entry = cbfs_find_next_entry(image, entry); |
| 509 | addr = cbfs_get_entry_addr(image, entry); |
| 510 | } |
| 511 | |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 512 | len = content_offset - addr - header_size; |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 513 | memcpy(entry, header, header_size); |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 514 | if (len != 0) { |
| 515 | /* the header moved backwards a bit to accomodate cbfs_file |
| 516 | * alignment requirements, so patch up ->offset to still point |
| 517 | * to file data. |
| 518 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 519 | DEBUG("|..|header|content|... <use offset to create entry>\n"); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 520 | DEBUG("before: offset=0x%x\n", ntohl(entry->offset)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 521 | // TODO reset expanded name buffer to 0xFF. |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 522 | entry->offset = htonl(ntohl(entry->offset) + len); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 523 | DEBUG("after: offset=0x%x\n", ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Ready to fill data into entry. |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 527 | DEBUG("content_offset: 0x%x, entry location: %x\n", |
| 528 | content_offset, (int)((char*)CBFS_SUBHEADER(entry) - |
| 529 | image->buffer.data)); |
| 530 | assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 531 | (ptrdiff_t)content_offset); |
Patrick Georgi | 19c80b2 | 2015-08-25 13:16:04 +0200 | [diff] [blame] | 532 | memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 533 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 534 | |
| 535 | // Process buffer AFTER entry. |
| 536 | entry = cbfs_find_next_entry(image, entry); |
| 537 | addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 538 | if (addr == addr_next) |
| 539 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 540 | |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 541 | assert(addr < addr_next); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 542 | if (addr_next - addr < min_entry_size) { |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 543 | DEBUG("No need for new \"empty\" entry\n"); |
| 544 | /* No need to increase the size of the just |
| 545 | * stored file to extend to next file. Alignment |
| 546 | * of next file takes care of this. |
| 547 | */ |
| 548 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | len = addr_next - addr - min_entry_size; |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 552 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 553 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 558 | uint32_t content_offset, |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 559 | struct cbfs_file *header) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 560 | { |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 561 | assert(image); |
| 562 | assert(buffer); |
| 563 | assert(buffer->data); |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 564 | assert(!IS_TOP_ALIGNED_ADDRESS(content_offset)); |
| 565 | |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 566 | const char *name = header->filename; |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 567 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 568 | uint32_t entry_type; |
| 569 | uint32_t addr, addr_next; |
| 570 | struct cbfs_file *entry, *next; |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 571 | uint32_t need_size; |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 572 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 573 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 574 | need_size = header_size + buffer->size; |
| 575 | DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", |
| 576 | name, content_offset, header_size, buffer->size, need_size); |
| 577 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 578 | // Merge empty entries. |
| 579 | DEBUG("(trying to merge empty entries...)\n"); |
| 580 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 581 | |
| 582 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 583 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 584 | entry = cbfs_find_next_entry(image, entry)) { |
| 585 | |
| 586 | entry_type = ntohl(entry->type); |
| 587 | if (entry_type != CBFS_COMPONENT_NULL) |
| 588 | continue; |
| 589 | |
| 590 | addr = cbfs_get_entry_addr(image, entry); |
| 591 | next = cbfs_find_next_entry(image, entry); |
| 592 | addr_next = cbfs_get_entry_addr(image, next); |
| 593 | |
| 594 | DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n", |
| 595 | addr, addr_next - addr, addr_next - addr); |
Aaron Durbin | 1ebc7e9 | 2014-01-21 15:28:38 -0600 | [diff] [blame] | 596 | |
| 597 | /* Will the file fit? Don't yet worry if we have space for a new |
| 598 | * "empty" entry. We take care of that later. |
| 599 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 600 | if (addr + need_size > addr_next) |
| 601 | continue; |
| 602 | |
Patrick Georgi | aa44dbd | 2015-08-12 12:05:21 +0200 | [diff] [blame] | 603 | // Test for complicated cases |
| 604 | if (content_offset > 0) { |
| 605 | if (addr_next < content_offset) { |
| 606 | DEBUG("Not for specified offset yet"); |
| 607 | continue; |
| 608 | } else if (addr > content_offset) { |
| 609 | DEBUG("Exceed specified content_offset."); |
| 610 | break; |
| 611 | } else if (addr + header_size > content_offset) { |
| 612 | ERROR("Not enough space for header.\n"); |
| 613 | break; |
| 614 | } else if (content_offset + buffer->size > addr_next) { |
| 615 | ERROR("Not enough space for content.\n"); |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | // TODO there are more few tricky cases that we may |
| 621 | // want to fit by altering offset. |
| 622 | |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 623 | if (content_offset == 0) { |
| 624 | // we tested every condition earlier under which |
| 625 | // placing the file there might fail |
| 626 | content_offset = addr + header_size; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 627 | } |
| 628 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 629 | DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n", |
| 630 | addr, addr_next - addr, content_offset); |
| 631 | |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 632 | if (cbfs_add_entry_at(image, entry, buffer->data, |
| 633 | content_offset, header) == 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n", |
| 640 | buffer->name, buffer->size, buffer->size / 1024, content_offset); |
| 641 | return -1; |
| 642 | } |
| 643 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 644 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) |
| 645 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 646 | struct cbfs_file *entry; |
| 647 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 648 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 649 | entry = cbfs_find_next_entry(image, entry)) { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 650 | if (strcasecmp(entry->filename, name) == 0) { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 651 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 652 | return entry; |
| 653 | } |
| 654 | } |
| 655 | return NULL; |
| 656 | } |
| 657 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 658 | static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff) |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 659 | { |
| 660 | struct buffer reader; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 661 | char *orig_buffer; |
| 662 | char *new_buffer; |
| 663 | size_t new_buff_sz; |
| 664 | decomp_func_ptr decompress; |
| 665 | |
| 666 | buffer_clone(&reader, buff); |
| 667 | |
| 668 | /* The stage metadata is in little endian. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 669 | stage->compression = xdr_le.get32(&reader); |
| 670 | stage->entry = xdr_le.get64(&reader); |
| 671 | stage->load = xdr_le.get64(&reader); |
| 672 | stage->len = xdr_le.get32(&reader); |
| 673 | stage->memlen = xdr_le.get32(&reader); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 674 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 675 | /* Create a buffer just with the uncompressed program now that the |
| 676 | * struct cbfs_stage has been peeled off. */ |
| 677 | if (stage->compression == CBFS_COMPRESS_NONE) { |
| 678 | new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage); |
| 679 | |
| 680 | orig_buffer = buffer_get(buff); |
| 681 | new_buffer = calloc(1, new_buff_sz); |
| 682 | memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage), |
| 683 | new_buff_sz); |
| 684 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
| 685 | free(orig_buffer); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 686 | return 0; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 687 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 688 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 689 | decompress = decompression_function(stage->compression); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 690 | if (decompress == NULL) |
| 691 | return -1; |
| 692 | |
| 693 | orig_buffer = buffer_get(buff); |
| 694 | |
| 695 | /* This can be too big of a buffer needed, but there's no current |
| 696 | * field indicating decompressed size of data. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 697 | new_buff_sz = stage->memlen; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 698 | new_buffer = calloc(1, new_buff_sz); |
| 699 | |
| 700 | if (decompress(orig_buffer + sizeof(struct cbfs_stage), |
| 701 | (int)(buffer_size(buff) - sizeof(struct cbfs_stage)), |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 702 | new_buffer, (int)new_buff_sz, &new_buff_sz)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 703 | ERROR("Couldn't decompress stage.\n"); |
| 704 | free(new_buffer); |
| 705 | return -1; |
| 706 | } |
| 707 | |
| 708 | /* Include correct size for full stage info. */ |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 709 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 710 | |
| 711 | /* True decompressed size is just the data size -- no metadata. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 712 | stage->len = new_buff_sz; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 713 | /* Stage is not compressed. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 714 | stage->compression = CBFS_COMPRESS_NONE; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 715 | |
| 716 | free(orig_buffer); |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 721 | static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch) |
| 722 | { |
| 723 | int endian; |
| 724 | int nbits; |
| 725 | int machine; |
| 726 | |
| 727 | switch (cbfs_arch) { |
| 728 | case CBFS_ARCHITECTURE_X86: |
| 729 | endian = ELFDATA2LSB; |
| 730 | nbits = ELFCLASS32; |
| 731 | machine = EM_386; |
| 732 | break; |
| 733 | case CBFS_ARCHITECTURE_ARM: |
| 734 | endian = ELFDATA2LSB; |
| 735 | nbits = ELFCLASS32; |
| 736 | machine = EM_ARM; |
| 737 | break; |
| 738 | case CBFS_ARCHITECTURE_AARCH64: |
| 739 | endian = ELFDATA2LSB; |
| 740 | nbits = ELFCLASS64; |
| 741 | machine = EM_AARCH64; |
| 742 | break; |
| 743 | case CBFS_ARCHITECTURE_MIPS: |
| 744 | endian = ELFDATA2LSB; |
| 745 | nbits = ELFCLASS32; |
| 746 | machine = EM_MIPS; |
| 747 | break; |
| 748 | case CBFS_ARCHITECTURE_RISCV: |
| 749 | endian = ELFDATA2LSB; |
| 750 | nbits = ELFCLASS32; |
| 751 | machine = EM_RISCV; |
| 752 | break; |
| 753 | default: |
| 754 | ERROR("Unsupported arch: %x\n", cbfs_arch); |
| 755 | return -1; |
| 756 | } |
| 757 | |
| 758 | elf_init_eheader(ehdr, machine, nbits, endian); |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch) |
| 763 | { |
| 764 | Elf64_Ehdr ehdr; |
| 765 | Elf64_Shdr shdr; |
| 766 | struct cbfs_stage stage; |
| 767 | struct elf_writer *ew; |
| 768 | struct buffer elf_out; |
| 769 | size_t empty_sz; |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 770 | int rmod_ret; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 771 | |
| 772 | if (cbfs_stage_decompress(&stage, buff)) { |
| 773 | ERROR("Failed to decompress stage.\n"); |
| 774 | return -1; |
| 775 | } |
| 776 | |
| 777 | if (init_elf_from_arch(&ehdr, arch)) |
| 778 | return -1; |
| 779 | |
| 780 | ehdr.e_entry = stage.entry; |
| 781 | |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 782 | /* Attempt rmodule translation first. */ |
| 783 | rmod_ret = rmodule_stage_to_elf(&ehdr, buff); |
| 784 | |
| 785 | if (rmod_ret < 0) { |
| 786 | ERROR("rmodule parsing failed\n"); |
| 787 | return -1; |
| 788 | } else if (rmod_ret == 0) |
| 789 | return 0; |
| 790 | |
| 791 | /* Rmodule couldn't do anything with the data. Continue on with SELF. */ |
| 792 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 793 | ew = elf_writer_init(&ehdr); |
| 794 | if (ew == NULL) { |
| 795 | ERROR("Unable to init ELF writer.\n"); |
| 796 | return -1; |
| 797 | } |
| 798 | |
| 799 | memset(&shdr, 0, sizeof(shdr)); |
| 800 | shdr.sh_type = SHT_PROGBITS; |
| 801 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; |
| 802 | shdr.sh_addr = stage.load; |
| 803 | shdr.sh_size = stage.len; |
| 804 | empty_sz = stage.memlen - stage.len; |
| 805 | |
| 806 | if (elf_writer_add_section(ew, &shdr, buff, ".program")) { |
| 807 | ERROR("Unable to add ELF section: .program\n"); |
| 808 | elf_writer_destroy(ew); |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | if (empty_sz != 0) { |
| 813 | struct buffer b; |
| 814 | |
| 815 | buffer_init(&b, NULL, NULL, 0); |
| 816 | memset(&shdr, 0, sizeof(shdr)); |
| 817 | shdr.sh_type = SHT_NOBITS; |
| 818 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 819 | shdr.sh_addr = stage.load + stage.len; |
| 820 | shdr.sh_size = empty_sz; |
| 821 | if (elf_writer_add_section(ew, &shdr, &b, ".empty")) { |
| 822 | ERROR("Unable to add ELF section: .empty\n"); |
| 823 | elf_writer_destroy(ew); |
| 824 | return -1; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (elf_writer_serialize(ew, &elf_out)) { |
| 829 | ERROR("Unable to create ELF file from stage.\n"); |
| 830 | elf_writer_destroy(ew); |
| 831 | return -1; |
| 832 | } |
| 833 | |
| 834 | /* Flip buffer with the created ELF one. */ |
| 835 | buffer_delete(buff); |
| 836 | *buff = elf_out; |
| 837 | |
| 838 | elf_writer_destroy(ew); |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 843 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 844 | const char *filename, uint32_t arch) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 845 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 846 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 847 | struct buffer buffer; |
| 848 | if (!entry) { |
| 849 | ERROR("File not found: %s\n", entry_name); |
| 850 | return -1; |
| 851 | } |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 852 | |
| 853 | unsigned int decompressed_size = 0; |
| 854 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 855 | &decompressed_size); |
| 856 | |
| 857 | decomp_func_ptr decompress = decompression_function(compression); |
| 858 | if (!decompress) { |
| 859 | ERROR("looking up decompression routine failed\n"); |
| 860 | return -1; |
| 861 | } |
| 862 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 863 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", |
| 864 | entry_name, cbfs_get_entry_addr(image, entry), |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 865 | get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 866 | |
Patrick Georgi | 011b0b3 | 2015-08-26 12:16:54 +0200 | [diff] [blame] | 867 | if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) { |
| 868 | WARN("Payloads are extracted in SELF format.\n"); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 869 | } |
| 870 | |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 871 | buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0); |
| 872 | |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 873 | buffer.data = malloc(decompressed_size); |
| 874 | buffer.size = decompressed_size; |
| 875 | if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len), |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 876 | buffer.data, buffer.size, NULL)) { |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 877 | ERROR("decompression failed for %s\n", entry_name); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 878 | buffer_delete(&buffer); |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 879 | return -1; |
| 880 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * The stage metadata is never compressed proper for cbfs_stage |
| 884 | * files. The contents of the stage data can be though. Therefore |
| 885 | * one has to do a second pass for stages to potentially decompress |
| 886 | * the stage data to make it more meaningful. |
| 887 | */ |
| 888 | if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) { |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 889 | if (cbfs_stage_make_elf(&buffer, arch)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 890 | buffer_delete(&buffer); |
| 891 | return -1; |
| 892 | } |
| 893 | } |
| 894 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 895 | if (buffer_write_file(&buffer, filename) != 0) { |
| 896 | ERROR("Failed to write %s into %s.\n", |
| 897 | entry_name, filename); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 898 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 899 | return -1; |
| 900 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 901 | |
| 902 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 903 | INFO("Successfully dumped the file to: %s\n", filename); |
| 904 | return 0; |
| 905 | } |
| 906 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 907 | int cbfs_remove_entry(struct cbfs_image *image, const char *name) |
| 908 | { |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 909 | struct cbfs_file *entry; |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 910 | entry = cbfs_get_entry(image, name); |
| 911 | if (!entry) { |
| 912 | ERROR("CBFS file %s not found.\n", name); |
| 913 | return -1; |
| 914 | } |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 915 | DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 916 | entry->filename, cbfs_get_entry_addr(image, entry)); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 917 | entry->type = htonl(CBFS_COMPONENT_DELETED); |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 918 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 919 | return 0; |
| 920 | } |
| 921 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 922 | int cbfs_print_header_info(struct cbfs_image *image) |
| 923 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 924 | char *name = strdup(image->buffer.name); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 925 | assert(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 926 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 927 | "alignment: %d bytes, architecture: %s\n\n", |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 928 | basename(name), |
| 929 | image->buffer.size / 1024, |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 930 | image->header.bootblocksize, |
| 931 | image->header.romsize, |
| 932 | image->header.offset, |
| 933 | image->header.align, |
| 934 | arch_to_string(image->header.architecture)); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 935 | free(name); |
| 936 | return 0; |
| 937 | } |
| 938 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 939 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) |
| 940 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 941 | fprintf(fp, |
| 942 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 943 | "length: %d/%d\n", |
| 944 | lookup_name_by_type(types_cbfs_compression, |
| 945 | stage->compression, "(unknown)"), |
| 946 | stage->entry, |
| 947 | stage->load, |
| 948 | stage->len, |
| 949 | stage->memlen); |
| 950 | return 0; |
| 951 | } |
| 952 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 953 | static int cbfs_print_decoded_payload_segment_info( |
| 954 | struct cbfs_payload_segment *seg, FILE *fp) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 955 | { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 956 | /* The input (seg) must be already decoded by |
| 957 | * cbfs_decode_payload_segment. |
| 958 | */ |
| 959 | switch (seg->type) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 960 | case PAYLOAD_SEGMENT_CODE: |
| 961 | case PAYLOAD_SEGMENT_DATA: |
| 962 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 963 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 964 | (seg->type == PAYLOAD_SEGMENT_CODE ? |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 965 | "code " : "data"), |
| 966 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 967 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 968 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 969 | seg->offset, seg->load_addr, seg->len, |
| 970 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 971 | break; |
| 972 | |
| 973 | case PAYLOAD_SEGMENT_ENTRY: |
| 974 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 975 | seg->load_addr); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 976 | break; |
| 977 | |
| 978 | case PAYLOAD_SEGMENT_BSS: |
| 979 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 980 | "length 0x%x)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 981 | seg->load_addr, seg->len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 982 | break; |
| 983 | |
| 984 | case PAYLOAD_SEGMENT_PARAMS: |
| 985 | fprintf(fp, " parameters\n"); |
| 986 | break; |
| 987 | |
| 988 | default: |
| 989 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 990 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 991 | seg->type, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 992 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 993 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 994 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 995 | seg->offset, seg->load_addr, seg->len, |
| 996 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 997 | break; |
| 998 | } |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | 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] | 1003 | void *arg) |
| 1004 | { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1005 | const char *name = entry->filename; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1006 | struct cbfs_payload_segment *payload; |
| 1007 | FILE *fp = (FILE *)arg; |
| 1008 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1009 | if (!cbfs_is_valid_entry(image, entry)) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1010 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 1011 | cbfs_get_entry_addr(image, entry)); |
| 1012 | return -1; |
| 1013 | } |
| 1014 | if (!fp) |
| 1015 | fp = stdout; |
| 1016 | |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1017 | unsigned int decompressed_size = 0; |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 1018 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 1019 | &decompressed_size); |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1020 | |
| 1021 | if (compression == CBFS_COMPRESS_NONE) { |
| 1022 | fprintf(fp, "%-30s 0x%-8x %-12s %d\n", |
| 1023 | *name ? name : "(empty)", |
| 1024 | cbfs_get_entry_addr(image, entry), |
| 1025 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1026 | ntohl(entry->len)); |
| 1027 | } else { |
| 1028 | fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n", |
| 1029 | *name ? name : "(empty)", |
| 1030 | cbfs_get_entry_addr(image, entry), |
| 1031 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1032 | ntohl(entry->len), |
| 1033 | decompressed_size, |
| 1034 | lookup_name_by_type(types_cbfs_compression, |
| 1035 | compression, "(unknown)") |
| 1036 | ); |
| 1037 | } |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1038 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1039 | struct cbfs_file_attr_hash *hash = NULL; |
| 1040 | while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) { |
| 1041 | unsigned int hash_type = ntohl(hash->hash_type); |
| 1042 | if (hash_type > CBFS_NUM_SUPPORTED_HASHES) { |
| 1043 | fprintf(fp, "invalid hash type %d\n", hash_type); |
| 1044 | break; |
| 1045 | } |
| 1046 | size_t hash_len = widths_cbfs_hash[hash_type]; |
| 1047 | char *hash_str = bintohex(hash->hash_data, hash_len); |
| 1048 | uint8_t local_hash[hash_len]; |
| 1049 | if (vb2_digest_buffer(CBFS_SUBHEADER(entry), |
| 1050 | ntohl(entry->len), hash_type, local_hash, |
| 1051 | hash_len) != VB2_SUCCESS) { |
| 1052 | fprintf(fp, "failed to hash '%s'\n", name); |
| 1053 | break; |
| 1054 | } |
| 1055 | int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0; |
| 1056 | const char *valid_str = valid ? "valid" : "invalid"; |
| 1057 | |
| 1058 | fprintf(fp, " hash %s:%s %s\n", |
| 1059 | get_hash_attr_name(hash_type), |
| 1060 | hash_str, valid_str); |
| 1061 | free(hash_str); |
| 1062 | } |
| 1063 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1064 | if (!verbose) |
| 1065 | return 0; |
| 1066 | |
| 1067 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 1068 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 1069 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 1070 | ntohl(entry->len)); |
| 1071 | |
| 1072 | /* note the components of the subheader may be in host order ... */ |
| 1073 | switch (ntohl(entry->type)) { |
| 1074 | case CBFS_COMPONENT_STAGE: |
| 1075 | cbfs_print_stage_info((struct cbfs_stage *) |
| 1076 | CBFS_SUBHEADER(entry), fp); |
| 1077 | break; |
| 1078 | |
| 1079 | case CBFS_COMPONENT_PAYLOAD: |
Paul Menzel | 831bbe8 | 2015-08-08 20:20:57 +0200 | [diff] [blame] | 1080 | payload = (struct cbfs_payload_segment *) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1081 | CBFS_SUBHEADER(entry); |
| 1082 | while (payload) { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1083 | struct cbfs_payload_segment seg; |
| 1084 | cbfs_decode_payload_segment(&seg, payload); |
| 1085 | cbfs_print_decoded_payload_segment_info( |
| 1086 | &seg, fp); |
| 1087 | if (seg.type == PAYLOAD_SEGMENT_ENTRY) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1088 | break; |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1089 | else |
Aaron Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 1090 | payload ++; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1091 | } |
| 1092 | break; |
| 1093 | default: |
| 1094 | break; |
| 1095 | } |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1099 | int cbfs_print_directory(struct cbfs_image *image) |
| 1100 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1101 | if (cbfs_is_legacy_cbfs(image)) |
| 1102 | cbfs_print_header_info(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1103 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 1104 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1108 | 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] | 1109 | unused void *arg) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1110 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1111 | struct cbfs_file *next; |
Aaron Durbin | 285111f | 2015-08-08 20:25:17 +0200 | [diff] [blame] | 1112 | uint8_t *name; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1113 | uint32_t type, addr, last_addr; |
| 1114 | |
| 1115 | type = ntohl(entry->type); |
| 1116 | if (type == CBFS_COMPONENT_DELETED) { |
| 1117 | // Ready to be recycled. |
| 1118 | type = CBFS_COMPONENT_NULL; |
| 1119 | entry->type = htonl(type); |
Aaron Durbin | 285111f | 2015-08-08 20:25:17 +0200 | [diff] [blame] | 1120 | // Place NUL byte as first byte of name to be viewed as "empty". |
| 1121 | name = (void *)&entry[1]; |
| 1122 | *name = '\0'; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1123 | } |
| 1124 | if (type != CBFS_COMPONENT_NULL) |
| 1125 | return 0; |
| 1126 | |
| 1127 | next = cbfs_find_next_entry(image, entry); |
| 1128 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1129 | while (next && cbfs_is_valid_entry(image, next)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1130 | type = ntohl(next->type); |
| 1131 | if (type == CBFS_COMPONENT_DELETED) { |
| 1132 | type = CBFS_COMPONENT_NULL; |
| 1133 | next->type = htonl(type); |
| 1134 | } |
| 1135 | if (type != CBFS_COMPONENT_NULL) |
| 1136 | return 0; |
| 1137 | |
| 1138 | addr = cbfs_get_entry_addr(image, entry); |
| 1139 | last_addr = cbfs_get_entry_addr( |
| 1140 | image, cbfs_find_next_entry(image, next)); |
| 1141 | |
| 1142 | // Now, we find two deleted/empty entries; try to merge now. |
| 1143 | DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n", |
| 1144 | cbfs_get_entry_addr(image, entry), ntohl(entry->len), |
| 1145 | cbfs_get_entry_addr(image, next), ntohl(next->len)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1146 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1147 | (last_addr - addr - |
| 1148 | cbfs_calculate_file_header_size("")), |
| 1149 | ""); |
| 1150 | DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len)); |
| 1151 | next = cbfs_find_next_entry(image, entry); |
| 1152 | } |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1156 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1157 | void *arg) |
| 1158 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1159 | int count = 0; |
| 1160 | struct cbfs_file *entry; |
| 1161 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1162 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1163 | entry = cbfs_find_next_entry(image, entry)) { |
| 1164 | count ++; |
| 1165 | if (callback(image, entry, arg) != 0) |
| 1166 | break; |
| 1167 | } |
| 1168 | return count; |
| 1169 | } |
| 1170 | |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1171 | static int cbfs_header_valid(struct cbfs_header *header) |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1172 | { |
| 1173 | if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && |
| 1174 | ((ntohl(header->version) == CBFS_HEADER_VERSION1) || |
| 1175 | (ntohl(header->version) == CBFS_HEADER_VERSION2)) && |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1176 | (ntohl(header->offset) < ntohl(header->romsize))) |
| 1177 | return 1; |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 1182 | uint32_t forced_offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1183 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1184 | size_t offset; |
| 1185 | int found = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1186 | int32_t rel_offset; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1187 | struct cbfs_header *header, *result = NULL; |
| 1188 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1189 | if (forced_offset < (size - sizeof(struct cbfs_header))) { |
| 1190 | /* Check if the forced header is valid. */ |
| 1191 | header = (struct cbfs_header *)(data + forced_offset); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1192 | if (cbfs_header_valid(header)) |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1193 | return header; |
| 1194 | return NULL; |
| 1195 | } |
| 1196 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1197 | // Try finding relative offset of master header at end of file first. |
| 1198 | rel_offset = *(int32_t *)(data + size - sizeof(int32_t)); |
| 1199 | offset = size + rel_offset; |
| 1200 | DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n", |
| 1201 | (size_t)rel_offset, (size_t)-rel_offset, offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1202 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1203 | if (offset >= size - sizeof(*header) || |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1204 | !cbfs_header_valid((struct cbfs_header *)(data + offset))) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1205 | // Some use cases append non-CBFS data to the end of the ROM. |
| 1206 | DEBUG("relative offset seems wrong, scanning whole image...\n"); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1207 | offset = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1208 | } |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1209 | |
| 1210 | for (; offset + sizeof(*header) < size; offset++) { |
| 1211 | header = (struct cbfs_header *)(data + offset); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1212 | if (!cbfs_header_valid(header)) |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1213 | continue; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1214 | if (!found++) |
| 1215 | result = header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1216 | } |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1217 | if (found > 1) |
| 1218 | // Top-aligned images usually have a working relative offset |
| 1219 | // field, so this is more likely to happen on bottom-aligned |
| 1220 | // ones (where the first header is the "outermost" one) |
| 1221 | WARN("Multiple (%d) CBFS headers found, using the first one.\n", |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1222 | found); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1223 | return result; |
| 1224 | } |
| 1225 | |
| 1226 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1227 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) |
| 1228 | { |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1229 | assert(image); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1230 | if (image->has_header) |
| 1231 | /* header.offset is relative to start of flash, not |
| 1232 | * start of region, so use it with the full image. |
| 1233 | */ |
| 1234 | return (struct cbfs_file *) |
| 1235 | (buffer_get_original_backing(&image->buffer) + |
| 1236 | image->header.offset); |
| 1237 | else |
| 1238 | return (struct cbfs_file *)buffer_get(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1242 | struct cbfs_file *entry) |
| 1243 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1244 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1245 | int align = image->has_header ? image->header.align : |
| 1246 | CBFS_ENTRY_ALIGNMENT; |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1247 | assert(entry && cbfs_is_valid_entry(image, entry)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1248 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 1249 | addr = align_up(addr, align); |
| 1250 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 1251 | } |
| 1252 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1253 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) |
| 1254 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1255 | assert(image && image->buffer.data && entry); |
| 1256 | return (int32_t)((char *)entry - image->buffer.data); |
| 1257 | } |
| 1258 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1259 | int cbfs_is_valid_cbfs(struct cbfs_image *image) |
| 1260 | { |
| 1261 | return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC, |
| 1262 | strlen(CBFS_FILE_MAGIC)); |
| 1263 | } |
| 1264 | |
| 1265 | int cbfs_is_legacy_cbfs(struct cbfs_image *image) |
| 1266 | { |
| 1267 | return image->has_header; |
| 1268 | } |
| 1269 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1270 | int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry) |
| 1271 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1272 | uint32_t offset = cbfs_get_entry_addr(image, entry); |
| 1273 | |
| 1274 | if (offset >= image->buffer.size) |
| 1275 | return 0; |
| 1276 | |
| 1277 | struct buffer entry_data; |
| 1278 | buffer_clone(&entry_data, &image->buffer); |
| 1279 | buffer_seek(&entry_data, offset); |
| 1280 | return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1281 | strlen(CBFS_FILE_MAGIC)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1284 | struct cbfs_file *cbfs_create_file_header(int type, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1285 | size_t len, const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1286 | { |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1287 | struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER); |
| 1288 | memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1289 | memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1290 | entry->type = htonl(type); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1291 | entry->len = htonl(len); |
Patrick Georgi | 0d618af | 2015-07-15 18:28:23 +0200 | [diff] [blame] | 1292 | entry->attributes_offset = 0; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1293 | entry->offset = htonl(cbfs_calculate_file_header_size(name)); |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1294 | memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry)); |
| 1295 | strcpy(entry->filename, name); |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1296 | return entry; |
| 1297 | } |
| 1298 | |
| 1299 | int cbfs_create_empty_entry(struct cbfs_file *entry, int type, |
| 1300 | size_t len, const char *name) |
| 1301 | { |
| 1302 | struct cbfs_file *tmp = cbfs_create_file_header(type, len, name); |
| 1303 | memcpy(entry, tmp, ntohl(tmp->offset)); |
| 1304 | free(tmp); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1305 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1309 | struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file) |
| 1310 | { |
| 1311 | /* attributes_offset should be 0 when there is no attribute, but all |
| 1312 | * values that point into the cbfs_file header are invalid, too. */ |
| 1313 | if (ntohl(file->attributes_offset) <= sizeof(*file)) |
| 1314 | return NULL; |
| 1315 | |
| 1316 | /* There needs to be enough space for the file header and one |
| 1317 | * attribute header for this to make sense. */ |
| 1318 | if (ntohl(file->offset) <= |
| 1319 | sizeof(*file) + sizeof(struct cbfs_file_attribute)) |
| 1320 | return NULL; |
| 1321 | |
| 1322 | return (struct cbfs_file_attribute *) |
| 1323 | (((uint8_t *)file) + ntohl(file->attributes_offset)); |
| 1324 | } |
| 1325 | |
| 1326 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 1327 | struct cbfs_file_attribute *attr) |
| 1328 | { |
| 1329 | /* ex falso sequitur quodlibet */ |
| 1330 | if (attr == NULL) |
| 1331 | return NULL; |
| 1332 | |
| 1333 | /* Is there enough space for another attribute? */ |
| 1334 | if ((uint8_t *)attr + ntohl(attr->len) + |
| 1335 | sizeof(struct cbfs_file_attribute) >= |
| 1336 | (uint8_t *)file + ntohl(file->offset)) |
| 1337 | return NULL; |
| 1338 | |
| 1339 | struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) |
| 1340 | (((uint8_t *)attr) + ntohl(attr->len)); |
| 1341 | /* If any, "unused" attributes must come last. */ |
| 1342 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) |
| 1343 | return NULL; |
| 1344 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) |
| 1345 | return NULL; |
| 1346 | |
| 1347 | return next; |
| 1348 | } |
| 1349 | |
| 1350 | struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, |
| 1351 | uint32_t tag, |
| 1352 | uint32_t size) |
| 1353 | { |
| 1354 | struct cbfs_file_attribute *attr, *next; |
| 1355 | next = cbfs_file_first_attr(header); |
| 1356 | do { |
| 1357 | attr = next; |
| 1358 | next = cbfs_file_next_attr(header, attr); |
| 1359 | } while (next != NULL); |
| 1360 | uint32_t header_size = ntohl(header->offset) + size; |
| 1361 | if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) { |
| 1362 | DEBUG("exceeding allocated space for cbfs_file headers"); |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | /* attr points to the last valid attribute now. |
| 1366 | * If NULL, we have to create the first one. */ |
| 1367 | if (attr == NULL) { |
| 1368 | /* New attributes start where the header ends. |
| 1369 | * header->offset is later set to accomodate the |
| 1370 | * additional structure. |
| 1371 | * No endianess translation necessary here, because both |
| 1372 | * fields are encoded the same way. */ |
| 1373 | header->attributes_offset = header->offset; |
| 1374 | attr = (struct cbfs_file_attribute *) |
| 1375 | (((uint8_t *)header) + |
| 1376 | ntohl(header->attributes_offset)); |
| 1377 | } else { |
| 1378 | attr = (struct cbfs_file_attribute *) |
| 1379 | (((uint8_t *)attr) + |
| 1380 | ntohl(attr->len)); |
| 1381 | } |
| 1382 | header->offset = htonl(header_size); |
| 1383 | memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size); |
| 1384 | attr->tag = htonl(tag); |
| 1385 | attr->len = htonl(size); |
| 1386 | return attr; |
| 1387 | } |
| 1388 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1389 | int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer, |
| 1390 | enum vb2_hash_algorithm hash_type) |
| 1391 | { |
zbao | 37450ff | 2015-11-05 14:35:57 +0800 | [diff] [blame^] | 1392 | uint32_t hash_index = hash_type; |
| 1393 | |
| 1394 | if (hash_index >= CBFS_NUM_SUPPORTED_HASHES) |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1395 | return -1; |
| 1396 | |
| 1397 | unsigned hash_size = widths_cbfs_hash[hash_type]; |
| 1398 | if (hash_size == 0) |
| 1399 | return -1; |
| 1400 | |
| 1401 | struct cbfs_file_attr_hash *attrs = |
| 1402 | (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header, |
| 1403 | CBFS_FILE_ATTR_TAG_HASH, |
| 1404 | sizeof(struct cbfs_file_attr_hash) + hash_size); |
| 1405 | |
| 1406 | if (attrs == NULL) |
| 1407 | return -1; |
| 1408 | |
| 1409 | attrs->hash_type = htonl(hash_type); |
| 1410 | if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer), |
| 1411 | hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS) |
| 1412 | return -1; |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1417 | /* Finds a place to hold whole data in same memory page. */ |
| 1418 | static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) |
| 1419 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1420 | if (!page) |
| 1421 | return 1; |
| 1422 | return (start / page) == (start + size - 1) / page; |
| 1423 | } |
| 1424 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1425 | /* Tests if data can fit in a range by given offset: |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1426 | * start ->| metadata_size | offset (+ size) |<- end |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1427 | */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1428 | static int is_in_range(size_t start, size_t end, size_t metadata_size, |
| 1429 | size_t offset, size_t size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1430 | { |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1431 | return (offset >= start + metadata_size && offset + size <= end); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1434 | int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size, |
| 1435 | size_t page_size, size_t align, size_t metadata_size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1436 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1437 | struct cbfs_file *entry; |
| 1438 | size_t need_len; |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1439 | size_t addr, addr_next, addr2, addr3, offset; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1440 | |
| 1441 | /* Default values: allow fitting anywhere in ROM. */ |
| 1442 | if (!page_size) |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1443 | page_size = image->has_header ? image->header.romsize : |
| 1444 | image->buffer.size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1445 | if (!align) |
| 1446 | align = 1; |
| 1447 | |
| 1448 | if (size > page_size) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1449 | ERROR("Input file size (%zd) greater than page size (%zd).\n", |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1450 | size, page_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1451 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1452 | size_t image_align = image->has_header ? image->header.align : |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1453 | CBFS_ENTRY_ALIGNMENT; |
| 1454 | if (page_size % image_align) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1455 | WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1456 | __func__, page_size, image_align); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1457 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1458 | need_len = metadata_size + size; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1459 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1460 | // Merge empty entries to build get max available space. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1461 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 1462 | |
| 1463 | /* Three cases of content location on memory page: |
| 1464 | * case 1. |
| 1465 | * | PAGE 1 | PAGE 2 | |
| 1466 | * | <header><content>| Fit. Return start of content. |
| 1467 | * |
| 1468 | * case 2. |
| 1469 | * | PAGE 1 | PAGE 2 | |
| 1470 | * | <header><content> | Fits when we shift content to align |
| 1471 | * shift-> | <header>|<content> | at starting of PAGE 2. |
| 1472 | * |
| 1473 | * case 3. (large content filling whole page) |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1474 | * | PAGE 1 | PAGE 2 | PAGE 3 | |
| 1475 | * | <header>< content > | Can't fit. If we shift content to |
| 1476 | * |trial-> <header>< content > | PAGE 2, header can't fit in free |
| 1477 | * | shift-> <header><content> space, so we must use PAGE 3. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1478 | * |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1479 | * The returned address can be then used as "base-address" (-b) in add-* |
| 1480 | * commands (will be re-calculated and positioned by cbfs_add_entry_at). |
| 1481 | * For stage targets, the address is also used to re-link stage before |
| 1482 | * being added into CBFS. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1483 | */ |
| 1484 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1485 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1486 | entry = cbfs_find_next_entry(image, entry)) { |
| 1487 | |
| 1488 | uint32_t type = ntohl(entry->type); |
| 1489 | if (type != CBFS_COMPONENT_NULL) |
| 1490 | continue; |
| 1491 | |
| 1492 | addr = cbfs_get_entry_addr(image, entry); |
| 1493 | addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry( |
| 1494 | image, entry)); |
| 1495 | if (addr_next - addr < need_len) |
| 1496 | continue; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1497 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1498 | offset = align_up(addr + metadata_size, align); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1499 | if (is_in_same_page(offset, size, page_size) && |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1500 | is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1501 | DEBUG("cbfs_locate_entry: FIT (PAGE1)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1502 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | addr2 = align_up(addr, page_size); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1506 | offset = align_up(addr2, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1507 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1508 | DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1509 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1512 | /* Assume page_size >= metadata_size so adding one page will |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1513 | * definitely provide the space for header. */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1514 | assert(page_size >= metadata_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1515 | addr3 = addr2 + page_size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1516 | offset = align_up(addr3, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1517 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1518 | DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1519 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1520 | } |
| 1521 | } |
| 1522 | return -1; |
| 1523 | } |