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. |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 5 | * Copyright (C) 2016 Siemens AG. All rights reserved. |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 17 | #include <inttypes.h> |
| 18 | #include <libgen.h> |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 19 | #include <stddef.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 23 | #include <strings.h> |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 24 | #include <commonlib/endian.h> |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 25 | |
| 26 | #include "common.h" |
| 27 | #include "cbfs_image.h" |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 28 | #include "elfparsing.h" |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 29 | #include "rmodule.h" |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 30 | |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 31 | /* Even though the file-adding functions---cbfs_add_entry() and |
| 32 | * cbfs_add_entry_at()---perform their sizing checks against the beginning of |
| 33 | * the subsequent section rather than a stable recorded value such as an empty |
| 34 | * file header's len field, it's possible to prove two interesting properties |
| 35 | * about their behavior: |
| 36 | * - Placing a new file within an empty entry located below an existing file |
| 37 | * entry will never leave an aligned flash address containing neither the |
| 38 | * beginning of a file header nor part of a file. |
| 39 | * - Placing a new file in an empty entry at the very end of the image such |
| 40 | * that it fits, but leaves no room for a final header, is guaranteed not to |
| 41 | * change the total amount of space for entries, even if that new file is |
| 42 | * later removed from the CBFS. |
| 43 | * These properties are somewhat nonobvious from the implementation, so the |
| 44 | * reader is encouraged to blame this comment and examine the full proofs |
| 45 | * in the commit message before making significant changes that would risk |
| 46 | * removing said guarantees. |
| 47 | */ |
| 48 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 49 | /* The file name align is not defined in CBFS spec -- only a preference by |
| 50 | * (old) cbfstool. */ |
| 51 | #define CBFS_FILENAME_ALIGN (16) |
| 52 | |
Ronald G. Minnich | aa2f739 | 2013-12-03 11:13:35 -0800 | [diff] [blame] | 53 | 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] | 54 | const char *default_value) |
| 55 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 56 | int i; |
| 57 | for (i = 0; desc[i].name; i++) |
| 58 | if (desc[i].type == type) |
| 59 | return desc[i].name; |
| 60 | return default_value; |
| 61 | } |
| 62 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 63 | static int lookup_type_by_name(const struct typedesc_t *desc, const char *name) |
| 64 | { |
| 65 | int i; |
| 66 | for (i = 0; desc[i].name && strcasecmp(name, desc[i].name); ++i); |
| 67 | return desc[i].name ? (int)desc[i].type : -1; |
| 68 | } |
| 69 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 70 | static const char *get_cbfs_entry_type_name(uint32_t type) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 71 | { |
Patrick Georgi | dc37dab | 2015-09-09 16:46:00 +0200 | [diff] [blame] | 72 | return lookup_name_by_type(filetypes, type, "(unknown)"); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 73 | } |
| 74 | |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 75 | int cbfs_parse_comp_algo(const char *name) |
| 76 | { |
| 77 | return lookup_type_by_name(types_cbfs_compression, name); |
| 78 | } |
| 79 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 80 | static const char *get_hash_attr_name(uint16_t hash_type) |
| 81 | { |
| 82 | return lookup_name_by_type(types_cbfs_hash, hash_type, "(invalid)"); |
| 83 | } |
| 84 | |
| 85 | int cbfs_parse_hash_algo(const char *name) |
| 86 | { |
| 87 | return lookup_type_by_name(types_cbfs_hash, name); |
| 88 | } |
| 89 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 90 | /* CBFS image */ |
| 91 | |
Patrick Georgi | 11ee08f | 2015-08-11 15:10:02 +0200 | [diff] [blame] | 92 | size_t cbfs_calculate_file_header_size(const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 93 | { |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 94 | return (sizeof(struct cbfs_file) + |
| 95 | align_up(strlen(name) + 1, CBFS_FILENAME_ALIGN)); |
| 96 | } |
| 97 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 98 | /* Only call on legacy CBFSes possessing a master header. */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 99 | 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] | 100 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 101 | assert(image); |
| 102 | assert(cbfs_is_legacy_cbfs(image)); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 103 | // A bug in old cbfstool may produce extra few bytes (by alignment) and |
| 104 | // cause cbfstool to overwrite things after free space -- which is |
| 105 | // usually CBFS header on x86. We need to workaround that. |
Patrick Georgi | 343ea08 | 2016-02-10 18:07:52 +0100 | [diff] [blame] | 106 | // Except when we run across a file that contains the actual header, |
| 107 | // in which case this image is a safe, new-style |
| 108 | // `cbfstool add-master-header` based image. |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 109 | |
| 110 | struct cbfs_file *entry, *first = NULL, *last = NULL; |
| 111 | for (first = entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 112 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 113 | entry = cbfs_find_next_entry(image, entry)) { |
Patrick Georgi | 343ea08 | 2016-02-10 18:07:52 +0100 | [diff] [blame] | 114 | /* Is the header guarded by a CBFS file entry? Then exit */ |
| 115 | if (((char *)entry) + ntohl(entry->offset) == hdr_loc) { |
| 116 | return 0; |
| 117 | } |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 118 | last = entry; |
| 119 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 120 | if ((char *)first < (char *)hdr_loc && |
| 121 | (char *)entry > (char *)hdr_loc) { |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 122 | WARN("CBFS image was created with old cbfstool with size bug. " |
| 123 | "Fixing size in last entry...\n"); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 124 | last->len = htonl(ntohl(last->len) - image->header.align); |
Hung-Te Lin | 49fcd75 | 2013-01-29 03:16:20 +0800 | [diff] [blame] | 125 | DEBUG("Last entry has been changed from 0x%x to 0x%x.\n", |
| 126 | cbfs_get_entry_addr(image, entry), |
| 127 | cbfs_get_entry_addr(image, |
| 128 | cbfs_find_next_entry(image, last))); |
| 129 | } |
| 130 | return 0; |
| 131 | } |
| 132 | |
Ronald G. Minnich | b5adeee | 2014-01-06 08:38:15 -0800 | [diff] [blame] | 133 | void cbfs_put_header(void *dest, const struct cbfs_header *header) |
| 134 | { |
| 135 | struct buffer outheader; |
| 136 | |
| 137 | outheader.data = dest; |
| 138 | outheader.size = 0; |
| 139 | |
| 140 | xdr_be.put32(&outheader, header->magic); |
| 141 | xdr_be.put32(&outheader, header->version); |
| 142 | xdr_be.put32(&outheader, header->romsize); |
| 143 | xdr_be.put32(&outheader, header->bootblocksize); |
| 144 | xdr_be.put32(&outheader, header->align); |
| 145 | xdr_be.put32(&outheader, header->offset); |
| 146 | xdr_be.put32(&outheader, header->architecture); |
| 147 | } |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 148 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 149 | static void cbfs_decode_payload_segment(struct cbfs_payload_segment *output, |
| 150 | struct cbfs_payload_segment *input) |
| 151 | { |
| 152 | struct buffer seg = { |
| 153 | .data = (void *)input, |
| 154 | .size = sizeof(*input), |
| 155 | }; |
| 156 | output->type = xdr_be.get32(&seg); |
| 157 | output->compression = xdr_be.get32(&seg); |
| 158 | output->offset = xdr_be.get32(&seg); |
| 159 | output->load_addr = xdr_be.get64(&seg); |
| 160 | output->len = xdr_be.get32(&seg); |
| 161 | output->mem_len = xdr_be.get32(&seg); |
| 162 | assert(seg.size == 0); |
| 163 | } |
| 164 | |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 165 | static int cbfs_file_get_compression_info(struct cbfs_file *entry, |
| 166 | uint32_t *decompressed_size) |
| 167 | { |
| 168 | unsigned int compression = CBFS_COMPRESS_NONE; |
Patrick Georgi | a2ce710 | 2016-12-14 16:08:52 +0100 | [diff] [blame] | 169 | if (decompressed_size) |
| 170 | *decompressed_size = ntohl(entry->len); |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 171 | for (struct cbfs_file_attribute *attr = cbfs_file_first_attr(entry); |
| 172 | attr != NULL; |
| 173 | attr = cbfs_file_next_attr(entry, attr)) { |
| 174 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_COMPRESSION) { |
| 175 | struct cbfs_file_attr_compression *ac = |
| 176 | (struct cbfs_file_attr_compression *)attr; |
| 177 | compression = ntohl(ac->compression); |
| 178 | if (decompressed_size) |
| 179 | *decompressed_size = |
| 180 | ntohl(ac->decompressed_size); |
| 181 | } |
| 182 | } |
| 183 | return compression; |
| 184 | } |
| 185 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 186 | static struct cbfs_file_attr_hash *cbfs_file_get_next_hash( |
| 187 | struct cbfs_file *entry, struct cbfs_file_attr_hash *cur) |
| 188 | { |
| 189 | struct cbfs_file_attribute *attr = (struct cbfs_file_attribute *)cur; |
| 190 | if (attr == NULL) { |
| 191 | attr = cbfs_file_first_attr(entry); |
| 192 | if (attr == NULL) |
| 193 | return NULL; |
| 194 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 195 | return (struct cbfs_file_attr_hash *)attr; |
| 196 | } |
| 197 | while ((attr = cbfs_file_next_attr(entry, attr)) != NULL) { |
| 198 | if (ntohl(attr->tag) == CBFS_FILE_ATTR_TAG_HASH) |
| 199 | return (struct cbfs_file_attr_hash *)attr; |
| 200 | }; |
| 201 | return NULL; |
| 202 | } |
| 203 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 204 | void cbfs_get_header(struct cbfs_header *header, void *src) |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 205 | { |
| 206 | struct buffer outheader; |
| 207 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 208 | outheader.data = src; /* We're not modifying the data */ |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 209 | outheader.size = 0; |
| 210 | |
| 211 | header->magic = xdr_be.get32(&outheader); |
| 212 | header->version = xdr_be.get32(&outheader); |
| 213 | header->romsize = xdr_be.get32(&outheader); |
| 214 | header->bootblocksize = xdr_be.get32(&outheader); |
| 215 | header->align = xdr_be.get32(&outheader); |
| 216 | header->offset = xdr_be.get32(&outheader); |
| 217 | header->architecture = xdr_be.get32(&outheader); |
| 218 | } |
| 219 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 220 | int cbfs_image_create(struct cbfs_image *image, size_t entries_size) |
| 221 | { |
| 222 | assert(image); |
| 223 | assert(image->buffer.data); |
| 224 | |
| 225 | size_t empty_header_len = cbfs_calculate_file_header_size(""); |
| 226 | uint32_t entries_offset = 0; |
| 227 | uint32_t align = CBFS_ENTRY_ALIGNMENT; |
| 228 | if (image->has_header) { |
| 229 | entries_offset = image->header.offset; |
| 230 | |
| 231 | if (entries_offset > image->buffer.size) { |
| 232 | ERROR("CBFS file entries are located outside CBFS itself\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | align = image->header.align; |
| 237 | } |
| 238 | |
| 239 | // This attribute must be given in order to prove that this module |
| 240 | // correctly preserves certain CBFS properties. See the block comment |
| 241 | // near the top of this file (and the associated commit message). |
| 242 | if (align < empty_header_len) { |
| 243 | ERROR("CBFS must be aligned to at least %zu bytes\n", |
| 244 | empty_header_len); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | if (entries_size > image->buffer.size - entries_offset) { |
| 249 | ERROR("CBFS doesn't have enough space to fit its file entries\n"); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | if (empty_header_len > entries_size) { |
| 254 | ERROR("CBFS is too small to fit any header\n"); |
| 255 | return -1; |
| 256 | } |
| 257 | struct cbfs_file *entry_header = |
| 258 | (struct cbfs_file *)(image->buffer.data + entries_offset); |
| 259 | // This alignment is necessary in order to prove that this module |
| 260 | // correctly preserves certain CBFS properties. See the block comment |
| 261 | // near the top of this file (and the associated commit message). |
| 262 | entries_size -= entries_size % align; |
| 263 | |
| 264 | size_t capacity = entries_size - empty_header_len; |
| 265 | LOG("Created CBFS (capacity = %zu bytes)\n", capacity); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 266 | return cbfs_create_empty_entry(entry_header, CBFS_COMPONENT_NULL, |
| 267 | capacity, ""); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | int cbfs_legacy_image_create(struct cbfs_image *image, |
| 271 | uint32_t architecture, |
| 272 | uint32_t align, |
| 273 | struct buffer *bootblock, |
| 274 | uint32_t bootblock_offset, |
| 275 | uint32_t header_offset, |
| 276 | uint32_t entries_offset) |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 277 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 278 | assert(image); |
| 279 | assert(image->buffer.data); |
| 280 | assert(bootblock); |
| 281 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 282 | int32_t *rel_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 283 | uint32_t cbfs_len; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 284 | void *header_loc; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 285 | size_t size = image->buffer.size; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 286 | |
| 287 | DEBUG("cbfs_image_create: bootblock=0x%x+0x%zx, " |
| 288 | "header=0x%x+0x%zx, entries_offset=0x%x\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 289 | bootblock_offset, bootblock->size, header_offset, |
| 290 | sizeof(image->header), entries_offset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 291 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 292 | // Adjust legacy top-aligned address to ROM offset. |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 293 | if (IS_TOP_ALIGNED_ADDRESS(entries_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 294 | entries_offset = size + (int32_t)entries_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 295 | if (IS_TOP_ALIGNED_ADDRESS(bootblock_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 296 | bootblock_offset = size + (int32_t)bootblock_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 297 | if (IS_TOP_ALIGNED_ADDRESS(header_offset)) |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 298 | header_offset = size + (int32_t)header_offset; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 299 | |
| 300 | DEBUG("cbfs_create_image: (real offset) bootblock=0x%x, " |
| 301 | "header=0x%x, entries_offset=0x%x\n", |
| 302 | bootblock_offset, header_offset, entries_offset); |
| 303 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 304 | // Prepare bootblock |
| 305 | if (bootblock_offset + bootblock->size > size) { |
| 306 | ERROR("Bootblock (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
| 307 | bootblock_offset, bootblock->size, size); |
| 308 | return -1; |
| 309 | } |
Hung-Te Lin | c5ff648 | 2013-02-06 12:41:49 +0800 | [diff] [blame] | 310 | if (entries_offset > bootblock_offset && |
| 311 | entries_offset < bootblock->size) { |
| 312 | ERROR("Bootblock (0x%x+0x%zx) overlap CBFS data (0x%x)\n", |
| 313 | bootblock_offset, bootblock->size, entries_offset); |
| 314 | return -1; |
| 315 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 316 | memcpy(image->buffer.data + bootblock_offset, bootblock->data, |
| 317 | bootblock->size); |
| 318 | |
| 319 | // Prepare header |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 320 | if (header_offset + sizeof(image->header) > size - sizeof(int32_t)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 321 | ERROR("Header (0x%x+0x%zx) exceed ROM size (0x%zx)\n", |
Sol Boucher | 5bad395 | 2015-05-05 20:35:26 -0700 | [diff] [blame] | 322 | header_offset, sizeof(image->header), size); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 323 | return -1; |
| 324 | } |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 325 | image->header.magic = CBFS_HEADER_MAGIC; |
| 326 | image->header.version = CBFS_HEADER_VERSION; |
| 327 | image->header.romsize = size; |
| 328 | image->header.bootblocksize = bootblock->size; |
| 329 | image->header.align = align; |
| 330 | image->header.offset = entries_offset; |
| 331 | image->header.architecture = architecture; |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 332 | |
| 333 | header_loc = (image->buffer.data + header_offset); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 334 | cbfs_put_header(header_loc, &image->header); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 335 | image->has_header = true; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 336 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 337 | // The last 4 byte of the image contain the relative offset from the end |
| 338 | // of the image to the master header as a 32-bit signed integer. x86 |
| 339 | // relies on this also being its (memory-mapped, top-aligned) absolute |
| 340 | // 32-bit address by virtue of how two's complement numbers work. |
| 341 | assert(size % sizeof(int32_t) == 0); |
| 342 | rel_offset = (int32_t *)(image->buffer.data + size - sizeof(int32_t)); |
| 343 | *rel_offset = header_offset - size; |
| 344 | |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 345 | // Prepare entries |
| 346 | if (align_up(entries_offset, align) != entries_offset) { |
| 347 | ERROR("Offset (0x%x) must be aligned to 0x%x.\n", |
| 348 | entries_offset, align); |
| 349 | return -1; |
| 350 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 351 | // To calculate available length, find |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 352 | // e = min(bootblock, header, rel_offset) where e > entries_offset. |
| 353 | cbfs_len = size - sizeof(int32_t); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 354 | if (bootblock_offset > entries_offset && bootblock_offset < cbfs_len) |
| 355 | cbfs_len = bootblock_offset; |
| 356 | if (header_offset > entries_offset && header_offset < cbfs_len) |
| 357 | cbfs_len = header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 358 | |
| 359 | if (cbfs_image_create(image, cbfs_len - entries_offset)) |
| 360 | return -1; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 361 | return 0; |
| 362 | } |
| 363 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 364 | int cbfs_image_from_buffer(struct cbfs_image *out, struct buffer *in, |
| 365 | uint32_t offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 366 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 367 | assert(out); |
| 368 | assert(in); |
| 369 | assert(in->data); |
Alexandru Gagniuc | c1d1fd8 | 2014-02-05 01:10:08 -0600 | [diff] [blame] | 370 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 371 | buffer_clone(&out->buffer, in); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 372 | out->has_header = false; |
| 373 | |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 374 | if (cbfs_is_valid_cbfs(out)) { |
| 375 | return 0; |
| 376 | } |
| 377 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 378 | void *header_loc = cbfs_find_header(in->data, in->size, offset); |
| 379 | if (header_loc) { |
| 380 | cbfs_get_header(&out->header, header_loc); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 381 | out->has_header = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 382 | cbfs_fix_legacy_size(out, header_loc); |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 383 | return 0; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 384 | } else if (offset != ~0u) { |
| 385 | ERROR("The -H switch is only valid on legacy images having CBFS master headers.\n"); |
| 386 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 387 | } |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 388 | ERROR("Selected image region is not a valid CBFS.\n"); |
| 389 | return 1; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 390 | } |
| 391 | |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 392 | int cbfs_copy_instance(struct cbfs_image *image, struct buffer *dst) |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 393 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 394 | assert(image); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 395 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 396 | struct cbfs_file *src_entry, *dst_entry; |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 397 | size_t align; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 398 | ssize_t last_entry_size; |
| 399 | |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 400 | size_t copy_end = buffer_size(dst); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 401 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 402 | align = CBFS_ENTRY_ALIGNMENT; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 403 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 404 | dst_entry = (struct cbfs_file *)buffer_get(dst); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 405 | |
| 406 | /* Copy non-empty files */ |
| 407 | for (src_entry = cbfs_find_first_entry(image); |
| 408 | src_entry && cbfs_is_valid_entry(image, src_entry); |
| 409 | src_entry = cbfs_find_next_entry(image, src_entry)) { |
| 410 | size_t entry_size; |
| 411 | |
| 412 | if ((src_entry->type == htonl(CBFS_COMPONENT_NULL)) || |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 413 | (src_entry->type == htonl(CBFS_COMPONENT_CBFSHEADER)) || |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 414 | (src_entry->type == htonl(CBFS_COMPONENT_DELETED))) |
| 415 | continue; |
| 416 | |
| 417 | entry_size = htonl(src_entry->len) + htonl(src_entry->offset); |
| 418 | memcpy(dst_entry, src_entry, entry_size); |
| 419 | dst_entry = (struct cbfs_file *)( |
| 420 | (uintptr_t)dst_entry + align_up(entry_size, align)); |
| 421 | |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 422 | if ((size_t)((void *)dst_entry - buffer_get(dst)) >= |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 423 | copy_end) { |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 424 | ERROR("Ran out of room in copy region.\n"); |
| 425 | return 1; |
| 426 | } |
| 427 | } |
| 428 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 429 | /* Last entry size is all the room above it, except for top 4 bytes |
| 430 | * which may be used by the master header pointer. This messes with |
| 431 | * the ability to stash something "top-aligned" into the region, but |
| 432 | * keeps things simpler. */ |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 433 | last_entry_size = copy_end - ((void *)dst_entry - buffer_get(dst)) |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 434 | - cbfs_calculate_file_header_size("") - sizeof(int32_t); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 435 | |
| 436 | if (last_entry_size < 0) |
| 437 | WARN("No room to create the last entry!\n") |
| 438 | else |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 439 | cbfs_create_empty_entry(dst_entry, CBFS_COMPONENT_NULL, |
| 440 | last_entry_size, ""); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 445 | static size_t cbfs_file_entry_metadata_size(const struct cbfs_file *f) |
| 446 | { |
| 447 | return ntohl(f->offset); |
| 448 | } |
| 449 | |
| 450 | static size_t cbfs_file_entry_data_size(const struct cbfs_file *f) |
| 451 | { |
| 452 | return ntohl(f->len); |
| 453 | } |
| 454 | |
| 455 | static size_t cbfs_file_entry_size(const struct cbfs_file *f) |
| 456 | { |
| 457 | return cbfs_file_entry_metadata_size(f) + cbfs_file_entry_data_size(f); |
| 458 | } |
| 459 | |
| 460 | int cbfs_compact_instance(struct cbfs_image *image) |
| 461 | { |
| 462 | assert(image); |
| 463 | |
| 464 | struct cbfs_file *prev; |
| 465 | struct cbfs_file *cur; |
| 466 | |
| 467 | /* The prev entry will always be an empty entry. */ |
| 468 | prev = NULL; |
| 469 | |
| 470 | /* |
| 471 | * Note: this function does not honor alignment or fixed location files. |
| 472 | * It's behavior is akin to cbfs_copy_instance() in that it expects |
| 473 | * the caller to understand the ramifications of compacting a |
| 474 | * fragmented CBFS image. |
| 475 | */ |
| 476 | |
| 477 | for (cur = cbfs_find_first_entry(image); |
| 478 | cur && cbfs_is_valid_entry(image, cur); |
| 479 | cur = cbfs_find_next_entry(image, cur)) { |
| 480 | size_t prev_size; |
| 481 | size_t cur_size; |
| 482 | size_t empty_metadata_size; |
| 483 | size_t spill_size; |
| 484 | uint32_t type = htonl(cur->type); |
| 485 | |
| 486 | /* Current entry is empty. Kepp track of it. */ |
| 487 | if ((type == htonl(CBFS_COMPONENT_NULL)) || |
| 488 | (type == htonl(CBFS_COMPONENT_DELETED))) { |
| 489 | prev = cur; |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | /* Need to ensure the previous entry is an empty one. */ |
| 494 | if (prev == NULL) |
| 495 | continue; |
| 496 | |
| 497 | /* At this point prev is an empty entry. Put the non-empty |
| 498 | * file in prev's location. Then add a new emptry entry. This |
| 499 | * essentialy bubbles empty entries towards the end. */ |
| 500 | |
| 501 | prev_size = cbfs_file_entry_size(prev); |
| 502 | cur_size = cbfs_file_entry_size(cur); |
| 503 | |
| 504 | /* |
| 505 | * Adjust the empty file size by the actual space occupied |
| 506 | * bewtween the beginning of the empty file and the non-empty |
| 507 | * file. |
| 508 | */ |
| 509 | prev_size += (cbfs_get_entry_addr(image, cur) - |
| 510 | cbfs_get_entry_addr(image, prev)) - prev_size; |
| 511 | |
| 512 | /* Move the non-empty file over the empty file. */ |
| 513 | memmove(prev, cur, cur_size); |
| 514 | |
| 515 | /* |
| 516 | * Get location of the empty file. Note that since prev was |
| 517 | * overwritten with the non-empty file the previously moved |
| 518 | * file needs to be used to calculate the empty file's location. |
| 519 | */ |
| 520 | cur = cbfs_find_next_entry(image, prev); |
| 521 | |
| 522 | /* |
| 523 | * The total space to work with for swapping the 2 entries |
| 524 | * consists of the 2 files' sizes combined. However, the |
| 525 | * cbfs_file entries start on CBFS_ALIGNMENT boundaries. |
| 526 | * Because of this the empty file size may end up smaller |
| 527 | * because of the non-empty file's metadata and data length. |
| 528 | * |
| 529 | * Calculate the spill size which is the amount of data lost |
| 530 | * due to the alignment constraints after moving the non-empty |
| 531 | * file. |
| 532 | */ |
| 533 | spill_size = (cbfs_get_entry_addr(image, cur) - |
| 534 | cbfs_get_entry_addr(image, prev)) - cur_size; |
| 535 | |
| 536 | empty_metadata_size = cbfs_calculate_file_header_size(""); |
| 537 | |
| 538 | /* Check if new empty size can contain the metadata. */ |
| 539 | if (empty_metadata_size + spill_size > prev_size) { |
| 540 | ERROR("Unable to swap '%s' with prev empty entry.\n", |
| 541 | prev->filename); |
| 542 | return 1; |
| 543 | } |
| 544 | |
| 545 | /* Update the empty file's size. */ |
| 546 | prev_size -= spill_size + empty_metadata_size; |
| 547 | |
| 548 | /* Create new empty file. */ |
| 549 | cbfs_create_empty_entry(cur, CBFS_COMPONENT_NULL, |
| 550 | prev_size, ""); |
| 551 | |
| 552 | /* Merge any potential empty entries together. */ |
| 553 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 554 | |
| 555 | /* |
| 556 | * Since current switched to an empty file keep track of it. |
| 557 | * Even if any empty files were merged the empty entry still |
| 558 | * starts at previously calculated location. |
| 559 | */ |
| 560 | prev = cur; |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 566 | int cbfs_image_delete(struct cbfs_image *image) |
| 567 | { |
Edward O'Callaghan | a0f9ece | 2014-03-09 00:05:18 +1100 | [diff] [blame] | 568 | if (image == NULL) |
| 569 | return 0; |
| 570 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 571 | buffer_delete(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 575 | /* Tries to add an entry with its data (CBFS_SUBHEADER) at given offset. */ |
| 576 | static int cbfs_add_entry_at(struct cbfs_image *image, |
| 577 | struct cbfs_file *entry, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 578 | const void *data, |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 579 | uint32_t content_offset, |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 580 | const struct cbfs_file *header) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 581 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 582 | struct cbfs_file *next = cbfs_find_next_entry(image, entry); |
| 583 | uint32_t addr = cbfs_get_entry_addr(image, entry), |
| 584 | addr_next = cbfs_get_entry_addr(image, next); |
Patrick Georgi | 7fd1418 | 2015-08-11 15:55:16 +0200 | [diff] [blame] | 585 | uint32_t min_entry_size = cbfs_calculate_file_header_size(""); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 586 | uint32_t len, header_offset; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 587 | uint32_t align = image->has_header ? image->header.align : |
| 588 | CBFS_ENTRY_ALIGNMENT; |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 589 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 590 | |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 591 | header_offset = content_offset - header_size; |
| 592 | if (header_offset % align) |
| 593 | header_offset -= header_offset % align; |
| 594 | if (header_offset < addr) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 595 | ERROR("No space to hold cbfs_file header."); |
| 596 | return -1; |
| 597 | } |
| 598 | |
| 599 | // Process buffer BEFORE content_offset. |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 600 | if (header_offset - addr > min_entry_size) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 601 | DEBUG("|min|...|header|content|... <create new entry>\n"); |
Patrick Georgi | 4eb8abe | 2015-08-25 12:24:49 +0200 | [diff] [blame] | 602 | len = header_offset - addr - min_entry_size; |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 603 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 604 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 605 | entry = cbfs_find_next_entry(image, entry); |
| 606 | addr = cbfs_get_entry_addr(image, entry); |
| 607 | } |
| 608 | |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 609 | len = content_offset - addr - header_size; |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 610 | memcpy(entry, header, header_size); |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 611 | if (len != 0) { |
| 612 | /* the header moved backwards a bit to accomodate cbfs_file |
| 613 | * alignment requirements, so patch up ->offset to still point |
| 614 | * to file data. |
| 615 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 616 | DEBUG("|..|header|content|... <use offset to create entry>\n"); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 617 | DEBUG("before: offset=0x%x\n", ntohl(entry->offset)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 618 | // TODO reset expanded name buffer to 0xFF. |
Patrick Georgi | 7a33b53 | 2015-08-25 13:00:04 +0200 | [diff] [blame] | 619 | entry->offset = htonl(ntohl(entry->offset) + len); |
Patrick Georgi | ae7efb9 | 2015-08-25 13:11:28 +0200 | [diff] [blame] | 620 | DEBUG("after: offset=0x%x\n", ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Ready to fill data into entry. |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 624 | DEBUG("content_offset: 0x%x, entry location: %x\n", |
| 625 | content_offset, (int)((char*)CBFS_SUBHEADER(entry) - |
| 626 | image->buffer.data)); |
| 627 | assert((char*)CBFS_SUBHEADER(entry) - image->buffer.data == |
Patrick Georgi | cccc9d4 | 2015-04-28 13:09:36 +0200 | [diff] [blame] | 628 | (ptrdiff_t)content_offset); |
Patrick Georgi | 19c80b2 | 2015-08-25 13:16:04 +0200 | [diff] [blame] | 629 | memcpy(CBFS_SUBHEADER(entry), data, ntohl(entry->len)); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 630 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 631 | |
| 632 | // Process buffer AFTER entry. |
| 633 | entry = cbfs_find_next_entry(image, entry); |
| 634 | addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 635 | if (addr == addr_next) |
| 636 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 637 | |
Sol Boucher | 0572565 | 2015-04-02 20:58:26 -0700 | [diff] [blame] | 638 | assert(addr < addr_next); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 639 | if (addr_next - addr < min_entry_size) { |
Sol Boucher | 636cc85 | 2015-04-03 09:13:04 -0700 | [diff] [blame] | 640 | DEBUG("No need for new \"empty\" entry\n"); |
| 641 | /* No need to increase the size of the just |
| 642 | * stored file to extend to next file. Alignment |
| 643 | * of next file takes care of this. |
| 644 | */ |
| 645 | return 0; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | len = addr_next - addr - min_entry_size; |
Patrick Georgi | 29a04d9 | 2015-11-20 23:23:44 +0100 | [diff] [blame] | 649 | /* keep space for master header pointer */ |
| 650 | if ((void *)entry + min_entry_size + len > buffer_get(&image->buffer) + |
| 651 | buffer_size(&image->buffer) - sizeof(int32_t)) { |
| 652 | len -= sizeof(int32_t); |
| 653 | } |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 654 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, len, ""); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 655 | if (verbose > 1) cbfs_print_entry_info(image, entry, stderr); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | int cbfs_add_entry(struct cbfs_image *image, struct buffer *buffer, |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 660 | uint32_t content_offset, |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 661 | struct cbfs_file *header) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 662 | { |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 663 | assert(image); |
| 664 | assert(buffer); |
| 665 | assert(buffer->data); |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 666 | assert(!IS_TOP_ALIGNED_ADDRESS(content_offset)); |
| 667 | |
Patrick Georgi | a60e7b6 | 2015-08-25 22:26:02 +0200 | [diff] [blame] | 668 | const char *name = header->filename; |
Patrick Georgi | e590358 | 2015-08-25 13:53:42 +0200 | [diff] [blame] | 669 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 670 | uint32_t entry_type; |
| 671 | uint32_t addr, addr_next; |
| 672 | struct cbfs_file *entry, *next; |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 673 | uint32_t need_size; |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 674 | uint32_t header_size = ntohl(header->offset); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 675 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 676 | need_size = header_size + buffer->size; |
| 677 | DEBUG("cbfs_add_entry('%s'@0x%x) => need_size = %u+%zu=%u\n", |
| 678 | name, content_offset, header_size, buffer->size, need_size); |
| 679 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 680 | // Merge empty entries. |
| 681 | DEBUG("(trying to merge empty entries...)\n"); |
| 682 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 683 | |
| 684 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 685 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 686 | entry = cbfs_find_next_entry(image, entry)) { |
| 687 | |
| 688 | entry_type = ntohl(entry->type); |
| 689 | if (entry_type != CBFS_COMPONENT_NULL) |
| 690 | continue; |
| 691 | |
| 692 | addr = cbfs_get_entry_addr(image, entry); |
| 693 | next = cbfs_find_next_entry(image, entry); |
| 694 | addr_next = cbfs_get_entry_addr(image, next); |
| 695 | |
| 696 | DEBUG("cbfs_add_entry: space at 0x%x+0x%x(%d) bytes\n", |
| 697 | addr, addr_next - addr, addr_next - addr); |
Aaron Durbin | 1ebc7e9 | 2014-01-21 15:28:38 -0600 | [diff] [blame] | 698 | |
| 699 | /* Will the file fit? Don't yet worry if we have space for a new |
| 700 | * "empty" entry. We take care of that later. |
| 701 | */ |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 702 | if (addr + need_size > addr_next) |
| 703 | continue; |
| 704 | |
Patrick Georgi | aa44dbd | 2015-08-12 12:05:21 +0200 | [diff] [blame] | 705 | // Test for complicated cases |
| 706 | if (content_offset > 0) { |
| 707 | if (addr_next < content_offset) { |
| 708 | DEBUG("Not for specified offset yet"); |
| 709 | continue; |
| 710 | } else if (addr > content_offset) { |
| 711 | DEBUG("Exceed specified content_offset."); |
| 712 | break; |
| 713 | } else if (addr + header_size > content_offset) { |
| 714 | ERROR("Not enough space for header.\n"); |
| 715 | break; |
| 716 | } else if (content_offset + buffer->size > addr_next) { |
| 717 | ERROR("Not enough space for content.\n"); |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | // TODO there are more few tricky cases that we may |
| 723 | // want to fit by altering offset. |
| 724 | |
Patrick Georgi | dd2d3f9 | 2015-08-12 12:29:20 +0200 | [diff] [blame] | 725 | if (content_offset == 0) { |
| 726 | // we tested every condition earlier under which |
| 727 | // placing the file there might fail |
| 728 | content_offset = addr + header_size; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 729 | } |
| 730 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 731 | DEBUG("section 0x%x+0x%x for content_offset 0x%x.\n", |
| 732 | addr, addr_next - addr, content_offset); |
| 733 | |
Patrick Georgi | d5a4afa | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 734 | if (cbfs_add_entry_at(image, entry, buffer->data, |
| 735 | content_offset, header) == 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 736 | return 0; |
| 737 | } |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | ERROR("Could not add [%s, %zd bytes (%zd KB)@0x%x]; too big?\n", |
| 742 | buffer->name, buffer->size, buffer->size / 1024, content_offset); |
| 743 | return -1; |
| 744 | } |
| 745 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 746 | struct cbfs_file *cbfs_get_entry(struct cbfs_image *image, const char *name) |
| 747 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 748 | struct cbfs_file *entry; |
| 749 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 750 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 751 | entry = cbfs_find_next_entry(image, entry)) { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 752 | if (strcasecmp(entry->filename, name) == 0) { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 753 | DEBUG("cbfs_get_entry: found %s\n", name); |
| 754 | return entry; |
| 755 | } |
| 756 | } |
| 757 | return NULL; |
| 758 | } |
| 759 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 760 | static int cbfs_stage_decompress(struct cbfs_stage *stage, struct buffer *buff) |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 761 | { |
| 762 | struct buffer reader; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 763 | char *orig_buffer; |
| 764 | char *new_buffer; |
| 765 | size_t new_buff_sz; |
| 766 | decomp_func_ptr decompress; |
| 767 | |
| 768 | buffer_clone(&reader, buff); |
| 769 | |
| 770 | /* The stage metadata is in little endian. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 771 | stage->compression = xdr_le.get32(&reader); |
| 772 | stage->entry = xdr_le.get64(&reader); |
| 773 | stage->load = xdr_le.get64(&reader); |
| 774 | stage->len = xdr_le.get32(&reader); |
| 775 | stage->memlen = xdr_le.get32(&reader); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 776 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 777 | /* Create a buffer just with the uncompressed program now that the |
| 778 | * struct cbfs_stage has been peeled off. */ |
| 779 | if (stage->compression == CBFS_COMPRESS_NONE) { |
| 780 | new_buff_sz = buffer_size(buff) - sizeof(struct cbfs_stage); |
| 781 | |
| 782 | orig_buffer = buffer_get(buff); |
| 783 | new_buffer = calloc(1, new_buff_sz); |
| 784 | memcpy(new_buffer, orig_buffer + sizeof(struct cbfs_stage), |
| 785 | new_buff_sz); |
| 786 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
| 787 | free(orig_buffer); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 788 | return 0; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 789 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 790 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 791 | decompress = decompression_function(stage->compression); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 792 | if (decompress == NULL) |
| 793 | return -1; |
| 794 | |
| 795 | orig_buffer = buffer_get(buff); |
| 796 | |
| 797 | /* This can be too big of a buffer needed, but there's no current |
| 798 | * field indicating decompressed size of data. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 799 | new_buff_sz = stage->memlen; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 800 | new_buffer = calloc(1, new_buff_sz); |
| 801 | |
| 802 | if (decompress(orig_buffer + sizeof(struct cbfs_stage), |
| 803 | (int)(buffer_size(buff) - sizeof(struct cbfs_stage)), |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 804 | new_buffer, (int)new_buff_sz, &new_buff_sz)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 805 | ERROR("Couldn't decompress stage.\n"); |
| 806 | free(new_buffer); |
| 807 | return -1; |
| 808 | } |
| 809 | |
| 810 | /* Include correct size for full stage info. */ |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 811 | buffer_init(buff, buff->name, new_buffer, new_buff_sz); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 812 | |
| 813 | /* True decompressed size is just the data size -- no metadata. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 814 | stage->len = new_buff_sz; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 815 | /* Stage is not compressed. */ |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 816 | stage->compression = CBFS_COMPRESS_NONE; |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 817 | |
| 818 | free(orig_buffer); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 823 | static int cbfs_payload_decompress(struct cbfs_payload_segment *segments, |
| 824 | struct buffer *buff, int num_seg) |
| 825 | { |
| 826 | struct buffer new_buffer; |
| 827 | struct buffer seg_buffer; |
| 828 | size_t new_buff_sz; |
| 829 | char *in_ptr; |
| 830 | char *out_ptr; |
| 831 | size_t new_offset; |
| 832 | decomp_func_ptr decompress; |
| 833 | |
| 834 | new_offset = num_seg * sizeof(*segments); |
| 835 | new_buff_sz = num_seg * sizeof(*segments); |
| 836 | |
| 837 | /* Find out and allocate the amount of memory occupied |
| 838 | * by the binary data */ |
| 839 | for (int i = 0; i < num_seg; i++) |
| 840 | new_buff_sz += segments[i].mem_len; |
| 841 | |
Furquan Shaikh | 58644a0 | 2016-08-05 08:27:18 -0700 | [diff] [blame] | 842 | if (buffer_create(&new_buffer, new_buff_sz, "decompressed_buff")) |
| 843 | return -1; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 844 | |
| 845 | in_ptr = buffer_get(buff) + new_offset; |
| 846 | out_ptr = buffer_get(&new_buffer) + new_offset; |
| 847 | |
| 848 | for (int i = 0; i < num_seg; i++) { |
| 849 | struct buffer tbuff; |
| 850 | size_t decomp_size; |
| 851 | |
| 852 | /* The payload uses an unknown compression algorithm. */ |
| 853 | decompress = decompression_function(segments[i].compression); |
| 854 | if (decompress == NULL) { |
| 855 | ERROR("Unknown decompression algorithm: %u", |
| 856 | segments[i].compression); |
| 857 | return -1; |
| 858 | } |
| 859 | |
| 860 | /* Segments BSS and ENTRY do not have binary data. */ |
| 861 | if (segments[i].type == PAYLOAD_SEGMENT_BSS || |
| 862 | segments[i].type == PAYLOAD_SEGMENT_ENTRY) { |
| 863 | continue; |
| 864 | } else if (segments[i].type == PAYLOAD_SEGMENT_PARAMS) { |
| 865 | memcpy(out_ptr, in_ptr, segments[i].len); |
| 866 | segments[i].offset = new_offset; |
| 867 | new_offset += segments[i].len; |
| 868 | in_ptr += segments[i].len; |
| 869 | out_ptr += segments[i].len; |
| 870 | segments[i].compression = CBFS_COMPRESS_NONE; |
| 871 | continue; |
| 872 | } |
| 873 | |
Furquan Shaikh | 58644a0 | 2016-08-05 08:27:18 -0700 | [diff] [blame] | 874 | if (buffer_create(&tbuff, segments[i].mem_len, "segment")) { |
| 875 | buffer_delete(&new_buffer); |
| 876 | return -1; |
| 877 | } |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 878 | |
| 879 | if (decompress(in_ptr, segments[i].len, buffer_get(&tbuff), |
| 880 | (int) buffer_size(&tbuff), |
| 881 | &decomp_size)) { |
| 882 | ERROR("Couldn't decompress payload segment %u\n", i); |
| 883 | buffer_delete(&new_buffer); |
Furquan Shaikh | 9844d56 | 2016-08-05 08:32:23 -0700 | [diff] [blame] | 884 | buffer_delete(&tbuff); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 885 | return -1; |
| 886 | } |
| 887 | |
| 888 | memcpy(out_ptr, buffer_get(&tbuff), decomp_size); |
| 889 | |
| 890 | in_ptr += segments[i].len; |
| 891 | |
| 892 | /* Update the offset of the segment. */ |
| 893 | segments[i].offset = new_offset; |
| 894 | /* True decompressed size is just the data size. No metadata */ |
| 895 | segments[i].len = decomp_size; |
| 896 | /* Segment is not compressed. */ |
| 897 | segments[i].compression = CBFS_COMPRESS_NONE; |
| 898 | |
| 899 | /* Update the offset and output buffer pointer. */ |
| 900 | new_offset += decomp_size; |
| 901 | out_ptr += decomp_size; |
| 902 | |
| 903 | buffer_delete(&tbuff); |
| 904 | } |
| 905 | |
| 906 | buffer_splice(&seg_buffer, &new_buffer, 0, 0); |
| 907 | xdr_segs(&seg_buffer, segments, num_seg); |
| 908 | |
| 909 | buffer_delete(buff); |
| 910 | *buff = new_buffer; |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 915 | static int init_elf_from_arch(Elf64_Ehdr *ehdr, uint32_t cbfs_arch) |
| 916 | { |
| 917 | int endian; |
| 918 | int nbits; |
| 919 | int machine; |
| 920 | |
| 921 | switch (cbfs_arch) { |
| 922 | case CBFS_ARCHITECTURE_X86: |
| 923 | endian = ELFDATA2LSB; |
| 924 | nbits = ELFCLASS32; |
| 925 | machine = EM_386; |
| 926 | break; |
| 927 | case CBFS_ARCHITECTURE_ARM: |
| 928 | endian = ELFDATA2LSB; |
| 929 | nbits = ELFCLASS32; |
| 930 | machine = EM_ARM; |
| 931 | break; |
| 932 | case CBFS_ARCHITECTURE_AARCH64: |
| 933 | endian = ELFDATA2LSB; |
| 934 | nbits = ELFCLASS64; |
| 935 | machine = EM_AARCH64; |
| 936 | break; |
| 937 | case CBFS_ARCHITECTURE_MIPS: |
| 938 | endian = ELFDATA2LSB; |
| 939 | nbits = ELFCLASS32; |
| 940 | machine = EM_MIPS; |
| 941 | break; |
| 942 | case CBFS_ARCHITECTURE_RISCV: |
| 943 | endian = ELFDATA2LSB; |
| 944 | nbits = ELFCLASS32; |
| 945 | machine = EM_RISCV; |
| 946 | break; |
| 947 | default: |
| 948 | ERROR("Unsupported arch: %x\n", cbfs_arch); |
| 949 | return -1; |
| 950 | } |
| 951 | |
| 952 | elf_init_eheader(ehdr, machine, nbits, endian); |
| 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static int cbfs_stage_make_elf(struct buffer *buff, uint32_t arch) |
| 957 | { |
| 958 | Elf64_Ehdr ehdr; |
| 959 | Elf64_Shdr shdr; |
| 960 | struct cbfs_stage stage; |
| 961 | struct elf_writer *ew; |
| 962 | struct buffer elf_out; |
| 963 | size_t empty_sz; |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 964 | int rmod_ret; |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 965 | |
Antonello Dettori | 0b80628 | 2016-06-26 00:24:25 +0200 | [diff] [blame] | 966 | if (arch == CBFS_ARCHITECTURE_UNKNOWN) { |
| 967 | ERROR("You need to specify -m ARCH.\n"); |
| 968 | return -1; |
| 969 | } |
| 970 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 971 | if (cbfs_stage_decompress(&stage, buff)) { |
| 972 | ERROR("Failed to decompress stage.\n"); |
| 973 | return -1; |
| 974 | } |
| 975 | |
| 976 | if (init_elf_from_arch(&ehdr, arch)) |
| 977 | return -1; |
| 978 | |
| 979 | ehdr.e_entry = stage.entry; |
| 980 | |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 981 | /* Attempt rmodule translation first. */ |
| 982 | rmod_ret = rmodule_stage_to_elf(&ehdr, buff); |
| 983 | |
| 984 | if (rmod_ret < 0) { |
| 985 | ERROR("rmodule parsing failed\n"); |
| 986 | return -1; |
| 987 | } else if (rmod_ret == 0) |
| 988 | return 0; |
| 989 | |
| 990 | /* Rmodule couldn't do anything with the data. Continue on with SELF. */ |
| 991 | |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 992 | ew = elf_writer_init(&ehdr); |
| 993 | if (ew == NULL) { |
| 994 | ERROR("Unable to init ELF writer.\n"); |
| 995 | return -1; |
| 996 | } |
| 997 | |
| 998 | memset(&shdr, 0, sizeof(shdr)); |
| 999 | shdr.sh_type = SHT_PROGBITS; |
| 1000 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; |
| 1001 | shdr.sh_addr = stage.load; |
| 1002 | shdr.sh_size = stage.len; |
| 1003 | empty_sz = stage.memlen - stage.len; |
| 1004 | |
| 1005 | if (elf_writer_add_section(ew, &shdr, buff, ".program")) { |
| 1006 | ERROR("Unable to add ELF section: .program\n"); |
| 1007 | elf_writer_destroy(ew); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | if (empty_sz != 0) { |
| 1012 | struct buffer b; |
| 1013 | |
| 1014 | buffer_init(&b, NULL, NULL, 0); |
| 1015 | memset(&shdr, 0, sizeof(shdr)); |
| 1016 | shdr.sh_type = SHT_NOBITS; |
| 1017 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 1018 | shdr.sh_addr = stage.load + stage.len; |
| 1019 | shdr.sh_size = empty_sz; |
| 1020 | if (elf_writer_add_section(ew, &shdr, &b, ".empty")) { |
| 1021 | ERROR("Unable to add ELF section: .empty\n"); |
| 1022 | elf_writer_destroy(ew); |
| 1023 | return -1; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | if (elf_writer_serialize(ew, &elf_out)) { |
| 1028 | ERROR("Unable to create ELF file from stage.\n"); |
| 1029 | elf_writer_destroy(ew); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | |
| 1033 | /* Flip buffer with the created ELF one. */ |
| 1034 | buffer_delete(buff); |
| 1035 | *buff = elf_out; |
| 1036 | |
| 1037 | elf_writer_destroy(ew); |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1042 | static int cbfs_payload_make_elf(struct buffer *buff, uint32_t arch) |
| 1043 | { |
| 1044 | Elf64_Ehdr ehdr; |
| 1045 | Elf64_Shdr shdr; |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1046 | struct cbfs_payload_segment *segs = NULL; |
Jonathan Neuschäfer | 5de5458 | 2016-08-09 15:01:58 +0200 | [diff] [blame] | 1047 | struct elf_writer *ew = NULL; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1048 | struct buffer elf_out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1049 | int segments = 0; |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1050 | int retval = -1; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1051 | |
Antonello Dettori | 0b80628 | 2016-06-26 00:24:25 +0200 | [diff] [blame] | 1052 | if (arch == CBFS_ARCHITECTURE_UNKNOWN) { |
| 1053 | ERROR("You need to specify -m ARCH.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1054 | goto out; |
Antonello Dettori | 0b80628 | 2016-06-26 00:24:25 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1057 | /* Count the number of segments inside buffer */ |
| 1058 | while (true) { |
| 1059 | uint32_t payload_type = 0; |
| 1060 | |
| 1061 | struct cbfs_payload_segment *seg; |
| 1062 | |
| 1063 | seg = buffer_get(buff); |
| 1064 | payload_type = read_be32(&seg[segments].type); |
| 1065 | |
| 1066 | if (payload_type == PAYLOAD_SEGMENT_CODE) { |
| 1067 | segments++; |
| 1068 | } else if (payload_type == PAYLOAD_SEGMENT_DATA) { |
| 1069 | segments++; |
| 1070 | } else if (payload_type == PAYLOAD_SEGMENT_BSS) { |
| 1071 | segments++; |
| 1072 | } else if (payload_type == PAYLOAD_SEGMENT_PARAMS) { |
| 1073 | segments++; |
| 1074 | } else if (payload_type == PAYLOAD_SEGMENT_ENTRY) { |
| 1075 | /* The last segment in a payload is always ENTRY as |
| 1076 | * specified by the parse_elf_to_payload() function. |
| 1077 | * Therefore there is no need to continue looking for |
| 1078 | * segments.*/ |
| 1079 | segments++; |
| 1080 | break; |
| 1081 | } else { |
| 1082 | ERROR("Unknown payload segment type: %x\n", |
| 1083 | payload_type); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1084 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | segs = malloc(segments * sizeof(*segs)); |
| 1089 | |
| 1090 | /* Decode xdr segments */ |
| 1091 | for (int i = 0; i < segments; i++) { |
| 1092 | struct cbfs_payload_segment *serialized_seg = buffer_get(buff); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1093 | xdr_get_seg(&segs[i], &serialized_seg[i]); |
| 1094 | } |
| 1095 | |
| 1096 | if (cbfs_payload_decompress(segs, buff, segments)) { |
| 1097 | ERROR("Failed to decompress payload.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1098 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | if (init_elf_from_arch(&ehdr, arch)) |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1102 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1103 | |
| 1104 | ehdr.e_entry = segs[segments-1].load_addr; |
| 1105 | |
| 1106 | ew = elf_writer_init(&ehdr); |
| 1107 | if (ew == NULL) { |
| 1108 | ERROR("Unable to init ELF writer.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1109 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | for (int i = 0; i < segments; i++) { |
| 1113 | struct buffer tbuff; |
Furquan Shaikh | f3bba44 | 2016-08-05 08:12:31 -0700 | [diff] [blame] | 1114 | size_t empty_sz = 0; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1115 | |
| 1116 | memset(&shdr, 0, sizeof(shdr)); |
| 1117 | char *name = NULL; |
| 1118 | |
| 1119 | if (segs[i].type == PAYLOAD_SEGMENT_CODE) { |
| 1120 | shdr.sh_type = SHT_PROGBITS; |
| 1121 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR; |
| 1122 | shdr.sh_addr = segs[i].load_addr; |
| 1123 | shdr.sh_size = segs[i].len; |
| 1124 | empty_sz = segs[i].mem_len - segs[i].len; |
| 1125 | name = strdup(".text"); |
| 1126 | buffer_splice(&tbuff, buff, segs[i].offset, |
| 1127 | segs[i].len); |
| 1128 | } else if (segs[i].type == PAYLOAD_SEGMENT_DATA) { |
| 1129 | shdr.sh_type = SHT_PROGBITS; |
| 1130 | shdr.sh_flags = SHF_ALLOC | SHF_WRITE; |
| 1131 | shdr.sh_addr = segs[i].load_addr; |
| 1132 | shdr.sh_size = segs[i].len; |
| 1133 | empty_sz = segs[i].mem_len - segs[i].len; |
| 1134 | name = strdup(".data"); |
| 1135 | buffer_splice(&tbuff, buff, segs[i].offset, |
| 1136 | segs[i].len); |
| 1137 | } else if (segs[i].type == PAYLOAD_SEGMENT_BSS) { |
| 1138 | shdr.sh_type = SHT_NOBITS; |
| 1139 | shdr.sh_flags = SHF_ALLOC | SHF_WRITE; |
| 1140 | shdr.sh_addr = segs[i].load_addr; |
| 1141 | shdr.sh_size = segs[i].len; |
| 1142 | name = strdup(".bss"); |
| 1143 | buffer_splice(&tbuff, buff, 0, 0); |
| 1144 | } else if (segs[i].type == PAYLOAD_SEGMENT_PARAMS) { |
| 1145 | shdr.sh_type = SHT_NOTE; |
| 1146 | shdr.sh_flags = 0; |
| 1147 | shdr.sh_size = segs[i].len; |
| 1148 | name = strdup(".note.pinfo"); |
| 1149 | buffer_splice(&tbuff, buff, segs[i].offset, |
| 1150 | segs[i].len); |
| 1151 | } else if (segs[i].type == PAYLOAD_SEGMENT_ENTRY) { |
| 1152 | break; |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | if (elf_writer_add_section(ew, &shdr, &tbuff, name)) { |
| 1157 | ERROR("Unable to add ELF section: %s\n", name); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1158 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | if (empty_sz != 0) { |
| 1162 | struct buffer b; |
| 1163 | |
| 1164 | buffer_init(&b, NULL, NULL, 0); |
| 1165 | memset(&shdr, 0, sizeof(shdr)); |
| 1166 | shdr.sh_type = SHT_NOBITS; |
| 1167 | shdr.sh_flags = SHF_WRITE | SHF_ALLOC; |
| 1168 | shdr.sh_addr = segs[i].load_addr + segs[i].len; |
| 1169 | shdr.sh_size = empty_sz; |
| 1170 | name = strdup(".empty"); |
| 1171 | if (elf_writer_add_section(ew, &shdr, &b, name)) { |
| 1172 | ERROR("Unable to add ELF section: %s\n", name); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1173 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1174 | } |
| 1175 | } |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | if (elf_writer_serialize(ew, &elf_out)) { |
| 1179 | ERROR("Unable to create ELF file from stage.\n"); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1180 | goto out; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | /* Flip buffer with the created ELF one. */ |
| 1184 | buffer_delete(buff); |
| 1185 | *buff = elf_out; |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1186 | retval = 0; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1187 | |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1188 | out: |
| 1189 | free(segs); |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1190 | elf_writer_destroy(ew); |
Furquan Shaikh | 7b40517 | 2016-08-05 08:20:37 -0700 | [diff] [blame] | 1191 | return retval; |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1194 | int cbfs_export_entry(struct cbfs_image *image, const char *entry_name, |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 1195 | const char *filename, uint32_t arch) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1196 | { |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1197 | struct cbfs_file *entry = cbfs_get_entry(image, entry_name); |
| 1198 | struct buffer buffer; |
| 1199 | if (!entry) { |
| 1200 | ERROR("File not found: %s\n", entry_name); |
| 1201 | return -1; |
| 1202 | } |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1203 | |
| 1204 | unsigned int decompressed_size = 0; |
| 1205 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 1206 | &decompressed_size); |
| 1207 | |
| 1208 | decomp_func_ptr decompress = decompression_function(compression); |
| 1209 | if (!decompress) { |
| 1210 | ERROR("looking up decompression routine failed\n"); |
| 1211 | return -1; |
| 1212 | } |
| 1213 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1214 | LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", |
| 1215 | entry_name, cbfs_get_entry_addr(image, entry), |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1216 | get_cbfs_entry_type_name(ntohl(entry->type)), decompressed_size); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1217 | |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1218 | buffer_init(&buffer, strdup("(cbfs_export_entry)"), NULL, 0); |
| 1219 | |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1220 | buffer.data = malloc(decompressed_size); |
| 1221 | buffer.size = decompressed_size; |
| 1222 | if (decompress(CBFS_SUBHEADER(entry), ntohl(entry->len), |
Aaron Durbin | 5213c53 | 2015-10-23 17:38:40 -0500 | [diff] [blame] | 1223 | buffer.data, buffer.size, NULL)) { |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1224 | ERROR("decompression failed for %s\n", entry_name); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1225 | buffer_delete(&buffer); |
Patrick Georgi | 23aeaff | 2015-08-26 13:01:10 +0200 | [diff] [blame] | 1226 | return -1; |
| 1227 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1228 | |
| 1229 | /* |
| 1230 | * The stage metadata is never compressed proper for cbfs_stage |
| 1231 | * files. The contents of the stage data can be though. Therefore |
| 1232 | * one has to do a second pass for stages to potentially decompress |
| 1233 | * the stage data to make it more meaningful. |
| 1234 | */ |
| 1235 | if (ntohl(entry->type) == CBFS_COMPONENT_STAGE) { |
Aaron Durbin | 5a1e85c | 2015-10-27 21:02:30 -0500 | [diff] [blame] | 1236 | if (cbfs_stage_make_elf(&buffer, arch)) { |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1237 | buffer_delete(&buffer); |
| 1238 | return -1; |
| 1239 | } |
Antonello Dettori | fda691e | 2016-06-09 12:35:36 +0200 | [diff] [blame] | 1240 | } else if (ntohl(entry->type) == CBFS_COMPONENT_PAYLOAD) { |
| 1241 | if (cbfs_payload_make_elf(&buffer, arch)) { |
| 1242 | buffer_delete(&buffer); |
| 1243 | return -1; |
| 1244 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1247 | if (buffer_write_file(&buffer, filename) != 0) { |
| 1248 | ERROR("Failed to write %s into %s.\n", |
| 1249 | entry_name, filename); |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1250 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1251 | return -1; |
| 1252 | } |
Aaron Durbin | 539aed0 | 2015-10-23 17:42:32 -0500 | [diff] [blame] | 1253 | |
| 1254 | buffer_delete(&buffer); |
Hung-Te Lin | 0f8af71 | 2013-01-29 02:29:49 +0800 | [diff] [blame] | 1255 | INFO("Successfully dumped the file to: %s\n", filename); |
| 1256 | return 0; |
| 1257 | } |
| 1258 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1259 | int cbfs_remove_entry(struct cbfs_image *image, const char *name) |
| 1260 | { |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 1261 | struct cbfs_file *entry; |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1262 | entry = cbfs_get_entry(image, name); |
| 1263 | if (!entry) { |
| 1264 | ERROR("CBFS file %s not found.\n", name); |
| 1265 | return -1; |
| 1266 | } |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1267 | DEBUG("cbfs_remove_entry: Removed %s @ 0x%x\n", |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1268 | entry->filename, cbfs_get_entry_addr(image, entry)); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1269 | entry->type = htonl(CBFS_COMPONENT_DELETED); |
Patrick Georgi | 4d1c5aa | 2015-07-17 22:07:26 +0200 | [diff] [blame] | 1270 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1271 | return 0; |
| 1272 | } |
| 1273 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1274 | int cbfs_print_header_info(struct cbfs_image *image) |
| 1275 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1276 | char *name = strdup(image->buffer.name); |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1277 | assert(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1278 | printf("%s: %zd kB, bootblocksize %d, romsize %d, offset 0x%x\n" |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 1279 | "alignment: %d bytes, architecture: %s\n\n", |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1280 | basename(name), |
| 1281 | image->buffer.size / 1024, |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1282 | image->header.bootblocksize, |
| 1283 | image->header.romsize, |
| 1284 | image->header.offset, |
| 1285 | image->header.align, |
| 1286 | arch_to_string(image->header.architecture)); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1287 | free(name); |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1291 | static int cbfs_print_stage_info(struct cbfs_stage *stage, FILE* fp) |
| 1292 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1293 | fprintf(fp, |
| 1294 | " %s compression, entry: 0x%" PRIx64 ", load: 0x%" PRIx64 ", " |
| 1295 | "length: %d/%d\n", |
| 1296 | lookup_name_by_type(types_cbfs_compression, |
| 1297 | stage->compression, "(unknown)"), |
| 1298 | stage->entry, |
| 1299 | stage->load, |
| 1300 | stage->len, |
| 1301 | stage->memlen); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1305 | static int cbfs_print_decoded_payload_segment_info( |
| 1306 | struct cbfs_payload_segment *seg, FILE *fp) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1307 | { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1308 | /* The input (seg) must be already decoded by |
| 1309 | * cbfs_decode_payload_segment. |
| 1310 | */ |
| 1311 | switch (seg->type) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1312 | case PAYLOAD_SEGMENT_CODE: |
| 1313 | case PAYLOAD_SEGMENT_DATA: |
| 1314 | fprintf(fp, " %s (%s compression, offset: 0x%x, " |
| 1315 | "load: 0x%" PRIx64 ", length: %d/%d)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1316 | (seg->type == PAYLOAD_SEGMENT_CODE ? |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1317 | "code " : "data"), |
| 1318 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1319 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1320 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1321 | seg->offset, seg->load_addr, seg->len, |
| 1322 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1323 | break; |
| 1324 | |
| 1325 | case PAYLOAD_SEGMENT_ENTRY: |
| 1326 | fprintf(fp, " entry (0x%" PRIx64 ")\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1327 | seg->load_addr); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1328 | break; |
| 1329 | |
| 1330 | case PAYLOAD_SEGMENT_BSS: |
| 1331 | fprintf(fp, " BSS (address 0x%016" PRIx64 ", " |
| 1332 | "length 0x%x)\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1333 | seg->load_addr, seg->len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1334 | break; |
| 1335 | |
| 1336 | case PAYLOAD_SEGMENT_PARAMS: |
| 1337 | fprintf(fp, " parameters\n"); |
| 1338 | break; |
| 1339 | |
| 1340 | default: |
| 1341 | fprintf(fp, " 0x%x (%s compression, offset: 0x%x, " |
| 1342 | "load: 0x%" PRIx64 ", length: %d/%d\n", |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1343 | seg->type, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1344 | lookup_name_by_type(types_cbfs_compression, |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1345 | seg->compression, |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1346 | "(unknown)"), |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1347 | seg->offset, seg->load_addr, seg->len, |
| 1348 | seg->mem_len); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1349 | break; |
| 1350 | } |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | 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] | 1355 | void *arg) |
| 1356 | { |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1357 | const char *name = entry->filename; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1358 | struct cbfs_payload_segment *payload; |
| 1359 | FILE *fp = (FILE *)arg; |
| 1360 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1361 | if (!cbfs_is_valid_entry(image, entry)) { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1362 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 1363 | cbfs_get_entry_addr(image, entry)); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | if (!fp) |
| 1367 | fp = stdout; |
| 1368 | |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1369 | unsigned int decompressed_size = 0; |
Patrick Georgi | a71c83f | 2015-08-26 12:23:26 +0200 | [diff] [blame] | 1370 | unsigned int compression = cbfs_file_get_compression_info(entry, |
| 1371 | &decompressed_size); |
Patrick Georgi | c82725c | 2015-08-26 12:13:03 +0200 | [diff] [blame] | 1372 | |
| 1373 | if (compression == CBFS_COMPRESS_NONE) { |
| 1374 | fprintf(fp, "%-30s 0x%-8x %-12s %d\n", |
| 1375 | *name ? name : "(empty)", |
| 1376 | cbfs_get_entry_addr(image, entry), |
| 1377 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1378 | ntohl(entry->len)); |
| 1379 | } else { |
| 1380 | fprintf(fp, "%-30s 0x%-8x %-12s %d (%d after %s decompression)\n", |
| 1381 | *name ? name : "(empty)", |
| 1382 | cbfs_get_entry_addr(image, entry), |
| 1383 | get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1384 | ntohl(entry->len), |
| 1385 | decompressed_size, |
| 1386 | lookup_name_by_type(types_cbfs_compression, |
| 1387 | compression, "(unknown)") |
| 1388 | ); |
| 1389 | } |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1390 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1391 | struct cbfs_file_attr_hash *hash = NULL; |
| 1392 | while ((hash = cbfs_file_get_next_hash(entry, hash)) != NULL) { |
| 1393 | unsigned int hash_type = ntohl(hash->hash_type); |
Furquan Shaikh | 1d56eef | 2016-12-02 09:24:50 -0800 | [diff] [blame] | 1394 | if (hash_type >= CBFS_NUM_SUPPORTED_HASHES) { |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1395 | fprintf(fp, "invalid hash type %d\n", hash_type); |
| 1396 | break; |
| 1397 | } |
| 1398 | size_t hash_len = widths_cbfs_hash[hash_type]; |
| 1399 | char *hash_str = bintohex(hash->hash_data, hash_len); |
| 1400 | uint8_t local_hash[hash_len]; |
| 1401 | if (vb2_digest_buffer(CBFS_SUBHEADER(entry), |
| 1402 | ntohl(entry->len), hash_type, local_hash, |
| 1403 | hash_len) != VB2_SUCCESS) { |
| 1404 | fprintf(fp, "failed to hash '%s'\n", name); |
Patrick Georgi | 862df92 | 2016-12-14 16:10:00 +0100 | [diff] [blame] | 1405 | free(hash_str); |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | int valid = memcmp(local_hash, hash->hash_data, hash_len) == 0; |
| 1409 | const char *valid_str = valid ? "valid" : "invalid"; |
| 1410 | |
| 1411 | fprintf(fp, " hash %s:%s %s\n", |
| 1412 | get_hash_attr_name(hash_type), |
| 1413 | hash_str, valid_str); |
| 1414 | free(hash_str); |
| 1415 | } |
| 1416 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1417 | if (!verbose) |
| 1418 | return 0; |
| 1419 | |
| 1420 | DEBUG(" cbfs_file=0x%x, offset=0x%x, content_address=0x%x+0x%x\n", |
| 1421 | cbfs_get_entry_addr(image, entry), ntohl(entry->offset), |
| 1422 | cbfs_get_entry_addr(image, entry) + ntohl(entry->offset), |
| 1423 | ntohl(entry->len)); |
| 1424 | |
| 1425 | /* note the components of the subheader may be in host order ... */ |
| 1426 | switch (ntohl(entry->type)) { |
| 1427 | case CBFS_COMPONENT_STAGE: |
| 1428 | cbfs_print_stage_info((struct cbfs_stage *) |
| 1429 | CBFS_SUBHEADER(entry), fp); |
| 1430 | break; |
| 1431 | |
| 1432 | case CBFS_COMPONENT_PAYLOAD: |
Paul Menzel | 831bbe8 | 2015-08-08 20:20:57 +0200 | [diff] [blame] | 1433 | payload = (struct cbfs_payload_segment *) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1434 | CBFS_SUBHEADER(entry); |
| 1435 | while (payload) { |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1436 | struct cbfs_payload_segment seg; |
| 1437 | cbfs_decode_payload_segment(&seg, payload); |
| 1438 | cbfs_print_decoded_payload_segment_info( |
| 1439 | &seg, fp); |
| 1440 | if (seg.type == PAYLOAD_SEGMENT_ENTRY) |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1441 | break; |
Hung-Te Lin | 0780d67 | 2014-05-16 10:14:05 +0800 | [diff] [blame] | 1442 | else |
Aaron Durbin | ca63027 | 2014-08-05 10:48:20 -0500 | [diff] [blame] | 1443 | payload ++; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1444 | } |
| 1445 | break; |
| 1446 | default: |
| 1447 | break; |
| 1448 | } |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1452 | static int cbfs_print_parseable_entry_info(struct cbfs_image *image, |
| 1453 | struct cbfs_file *entry, void *arg) |
| 1454 | { |
| 1455 | FILE *fp = (FILE *)arg; |
| 1456 | const char *name; |
| 1457 | const char *type; |
| 1458 | size_t offset; |
| 1459 | size_t metadata_size; |
| 1460 | size_t data_size; |
| 1461 | const char *sep = "\t"; |
| 1462 | |
| 1463 | if (!cbfs_is_valid_entry(image, entry)) { |
| 1464 | ERROR("cbfs_print_entry_info: Invalid entry at 0x%x\n", |
| 1465 | cbfs_get_entry_addr(image, entry)); |
| 1466 | return -1; |
| 1467 | } |
| 1468 | |
| 1469 | name = entry->filename; |
| 1470 | if (*name == '\0') |
| 1471 | name = "(empty)"; |
| 1472 | type = get_cbfs_entry_type_name(ntohl(entry->type)), |
| 1473 | metadata_size = ntohl(entry->offset); |
| 1474 | data_size = ntohl(entry->len); |
| 1475 | offset = cbfs_get_entry_addr(image, entry); |
| 1476 | |
| 1477 | fprintf(fp, "%s%s", name, sep); |
| 1478 | fprintf(fp, "0x%zx%s", offset, sep); |
| 1479 | fprintf(fp, "%s%s", type, sep); |
| 1480 | fprintf(fp, "0x%zx%s", metadata_size, sep); |
| 1481 | fprintf(fp, "0x%zx%s", data_size, sep); |
| 1482 | fprintf(fp, "0x%zx\n", metadata_size + data_size); |
| 1483 | |
| 1484 | return 0; |
| 1485 | } |
| 1486 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1487 | int cbfs_print_directory(struct cbfs_image *image) |
| 1488 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1489 | if (cbfs_is_legacy_cbfs(image)) |
| 1490 | cbfs_print_header_info(image); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1491 | printf("%-30s %-10s %-12s Size\n", "Name", "Offset", "Type"); |
| 1492 | cbfs_walk(image, cbfs_print_entry_info, NULL); |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1496 | int cbfs_print_parseable_directory(struct cbfs_image *image) |
| 1497 | { |
Furquan Shaikh | 161d233 | 2016-05-26 14:41:02 -0700 | [diff] [blame] | 1498 | size_t i; |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1499 | const char *header[] = { |
| 1500 | "Name", |
| 1501 | "Offset", |
| 1502 | "Type", |
| 1503 | "Metadata Size", |
| 1504 | "Data Size", |
| 1505 | "Total Size", |
| 1506 | }; |
| 1507 | const char *sep = "\t"; |
| 1508 | |
| 1509 | for (i = 0; i < ARRAY_SIZE(header) - 1; i++) |
| 1510 | fprintf(stdout, "%s%s", header[i], sep); |
| 1511 | fprintf(stdout, "%s\n", header[i]); |
| 1512 | cbfs_walk(image, cbfs_print_parseable_entry_info, stdout); |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1516 | 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] | 1517 | unused void *arg) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1518 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1519 | struct cbfs_file *next; |
Aaron Durbin | 285111f | 2015-08-08 20:25:17 +0200 | [diff] [blame] | 1520 | uint8_t *name; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1521 | uint32_t type, addr, last_addr; |
| 1522 | |
| 1523 | type = ntohl(entry->type); |
| 1524 | if (type == CBFS_COMPONENT_DELETED) { |
| 1525 | // Ready to be recycled. |
| 1526 | type = CBFS_COMPONENT_NULL; |
| 1527 | entry->type = htonl(type); |
Aaron Durbin | 285111f | 2015-08-08 20:25:17 +0200 | [diff] [blame] | 1528 | // Place NUL byte as first byte of name to be viewed as "empty". |
| 1529 | name = (void *)&entry[1]; |
| 1530 | *name = '\0'; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1531 | } |
| 1532 | if (type != CBFS_COMPONENT_NULL) |
| 1533 | return 0; |
| 1534 | |
| 1535 | next = cbfs_find_next_entry(image, entry); |
| 1536 | |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1537 | while (next && cbfs_is_valid_entry(image, next)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1538 | type = ntohl(next->type); |
| 1539 | if (type == CBFS_COMPONENT_DELETED) { |
| 1540 | type = CBFS_COMPONENT_NULL; |
| 1541 | next->type = htonl(type); |
| 1542 | } |
| 1543 | if (type != CBFS_COMPONENT_NULL) |
| 1544 | return 0; |
| 1545 | |
| 1546 | addr = cbfs_get_entry_addr(image, entry); |
| 1547 | last_addr = cbfs_get_entry_addr( |
| 1548 | image, cbfs_find_next_entry(image, next)); |
| 1549 | |
| 1550 | // Now, we find two deleted/empty entries; try to merge now. |
| 1551 | DEBUG("join_empty_entry: combine 0x%x+0x%x and 0x%x+0x%x.\n", |
| 1552 | cbfs_get_entry_addr(image, entry), ntohl(entry->len), |
| 1553 | cbfs_get_entry_addr(image, next), ntohl(next->len)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1554 | cbfs_create_empty_entry(entry, CBFS_COMPONENT_NULL, |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1555 | (last_addr - addr - |
| 1556 | cbfs_calculate_file_header_size("")), |
| 1557 | ""); |
| 1558 | DEBUG("new empty entry: length=0x%x\n", ntohl(entry->len)); |
| 1559 | next = cbfs_find_next_entry(image, entry); |
| 1560 | } |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1564 | int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1565 | void *arg) |
| 1566 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1567 | int count = 0; |
| 1568 | struct cbfs_file *entry; |
| 1569 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1570 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1571 | entry = cbfs_find_next_entry(image, entry)) { |
| 1572 | count ++; |
| 1573 | if (callback(image, entry, arg) != 0) |
| 1574 | break; |
| 1575 | } |
| 1576 | return count; |
| 1577 | } |
| 1578 | |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1579 | static int cbfs_header_valid(struct cbfs_header *header) |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1580 | { |
| 1581 | if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && |
| 1582 | ((ntohl(header->version) == CBFS_HEADER_VERSION1) || |
| 1583 | (ntohl(header->version) == CBFS_HEADER_VERSION2)) && |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1584 | (ntohl(header->offset) < ntohl(header->romsize))) |
| 1585 | return 1; |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
| 1589 | struct cbfs_header *cbfs_find_header(char *data, size_t size, |
| 1590 | uint32_t forced_offset) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1591 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1592 | size_t offset; |
| 1593 | int found = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1594 | int32_t rel_offset; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1595 | struct cbfs_header *header, *result = NULL; |
| 1596 | |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1597 | if (forced_offset < (size - sizeof(struct cbfs_header))) { |
| 1598 | /* Check if the forced header is valid. */ |
| 1599 | header = (struct cbfs_header *)(data + forced_offset); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1600 | if (cbfs_header_valid(header)) |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1601 | return header; |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1605 | // Try finding relative offset of master header at end of file first. |
| 1606 | rel_offset = *(int32_t *)(data + size - sizeof(int32_t)); |
| 1607 | offset = size + rel_offset; |
| 1608 | DEBUG("relative offset: %#zx(-%#zx), offset: %#zx\n", |
| 1609 | (size_t)rel_offset, (size_t)-rel_offset, offset); |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1610 | |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1611 | if (offset >= size - sizeof(*header) || |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1612 | !cbfs_header_valid((struct cbfs_header *)(data + offset))) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1613 | // Some use cases append non-CBFS data to the end of the ROM. |
| 1614 | DEBUG("relative offset seems wrong, scanning whole image...\n"); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1615 | offset = 0; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1616 | } |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1617 | |
| 1618 | for (; offset + sizeof(*header) < size; offset++) { |
| 1619 | header = (struct cbfs_header *)(data + offset); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1620 | if (!cbfs_header_valid(header)) |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1621 | continue; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1622 | if (!found++) |
| 1623 | result = header; |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1624 | } |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1625 | if (found > 1) |
| 1626 | // Top-aligned images usually have a working relative offset |
| 1627 | // field, so this is more likely to happen on bottom-aligned |
| 1628 | // ones (where the first header is the "outermost" one) |
| 1629 | WARN("Multiple (%d) CBFS headers found, using the first one.\n", |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1630 | found); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1631 | return result; |
| 1632 | } |
| 1633 | |
| 1634 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1635 | struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) |
| 1636 | { |
Sol Boucher | 3e060ed | 2015-05-05 15:40:15 -0700 | [diff] [blame] | 1637 | assert(image); |
Patrick Georgi | 7db2b6c | 2015-11-11 15:35:24 +0100 | [diff] [blame] | 1638 | if (image->has_header) |
| 1639 | /* header.offset is relative to start of flash, not |
| 1640 | * start of region, so use it with the full image. |
| 1641 | */ |
| 1642 | return (struct cbfs_file *) |
| 1643 | (buffer_get_original_backing(&image->buffer) + |
| 1644 | image->header.offset); |
| 1645 | else |
| 1646 | return (struct cbfs_file *)buffer_get(&image->buffer); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1650 | struct cbfs_file *entry) |
| 1651 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1652 | uint32_t addr = cbfs_get_entry_addr(image, entry); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1653 | int align = image->has_header ? image->header.align : |
| 1654 | CBFS_ENTRY_ALIGNMENT; |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1655 | assert(entry && cbfs_is_valid_entry(image, entry)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1656 | addr += ntohl(entry->offset) + ntohl(entry->len); |
| 1657 | addr = align_up(addr, align); |
| 1658 | return (struct cbfs_file *)(image->buffer.data + addr); |
| 1659 | } |
| 1660 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1661 | uint32_t cbfs_get_entry_addr(struct cbfs_image *image, struct cbfs_file *entry) |
| 1662 | { |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1663 | assert(image && image->buffer.data && entry); |
| 1664 | return (int32_t)((char *)entry - image->buffer.data); |
| 1665 | } |
| 1666 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1667 | int cbfs_is_valid_cbfs(struct cbfs_image *image) |
| 1668 | { |
| 1669 | return buffer_check_magic(&image->buffer, CBFS_FILE_MAGIC, |
| 1670 | strlen(CBFS_FILE_MAGIC)); |
| 1671 | } |
| 1672 | |
| 1673 | int cbfs_is_legacy_cbfs(struct cbfs_image *image) |
| 1674 | { |
| 1675 | return image->has_header; |
| 1676 | } |
| 1677 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1678 | int cbfs_is_valid_entry(struct cbfs_image *image, struct cbfs_file *entry) |
| 1679 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1680 | uint32_t offset = cbfs_get_entry_addr(image, entry); |
| 1681 | |
| 1682 | if (offset >= image->buffer.size) |
| 1683 | return 0; |
| 1684 | |
| 1685 | struct buffer entry_data; |
| 1686 | buffer_clone(&entry_data, &image->buffer); |
| 1687 | buffer_seek(&entry_data, offset); |
| 1688 | return buffer_check_magic(&entry_data, CBFS_FILE_MAGIC, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1689 | strlen(CBFS_FILE_MAGIC)); |
Hung-Te Lin | eab2c81 | 2013-01-29 01:56:17 +0800 | [diff] [blame] | 1690 | } |
| 1691 | |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1692 | struct cbfs_file *cbfs_create_file_header(int type, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1693 | size_t len, const char *name) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1694 | { |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1695 | struct cbfs_file *entry = malloc(MAX_CBFS_FILE_HEADER_BUFFER); |
| 1696 | memset(entry, CBFS_CONTENT_DEFAULT_VALUE, MAX_CBFS_FILE_HEADER_BUFFER); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1697 | memcpy(entry->magic, CBFS_FILE_MAGIC, sizeof(entry->magic)); |
Patrick Georgi | edf25d9 | 2015-08-12 09:12:06 +0200 | [diff] [blame] | 1698 | entry->type = htonl(type); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1699 | entry->len = htonl(len); |
Patrick Georgi | 0d618af | 2015-07-15 18:28:23 +0200 | [diff] [blame] | 1700 | entry->attributes_offset = 0; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1701 | entry->offset = htonl(cbfs_calculate_file_header_size(name)); |
Patrick Georgi | c569b8b | 2015-07-15 16:42:38 +0200 | [diff] [blame] | 1702 | memset(entry->filename, 0, ntohl(entry->offset) - sizeof(*entry)); |
| 1703 | strcpy(entry->filename, name); |
Patrick Georgi | 57edf16 | 2015-08-12 09:20:11 +0200 | [diff] [blame] | 1704 | return entry; |
| 1705 | } |
| 1706 | |
| 1707 | int cbfs_create_empty_entry(struct cbfs_file *entry, int type, |
| 1708 | size_t len, const char *name) |
| 1709 | { |
| 1710 | struct cbfs_file *tmp = cbfs_create_file_header(type, len, name); |
| 1711 | memcpy(entry, tmp, ntohl(tmp->offset)); |
| 1712 | free(tmp); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1713 | memset(CBFS_SUBHEADER(entry), CBFS_CONTENT_DEFAULT_VALUE, len); |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Patrick Georgi | 2c61506 | 2015-07-15 20:49:00 +0200 | [diff] [blame] | 1717 | struct cbfs_file_attribute *cbfs_file_first_attr(struct cbfs_file *file) |
| 1718 | { |
| 1719 | /* attributes_offset should be 0 when there is no attribute, but all |
| 1720 | * values that point into the cbfs_file header are invalid, too. */ |
| 1721 | if (ntohl(file->attributes_offset) <= sizeof(*file)) |
| 1722 | return NULL; |
| 1723 | |
| 1724 | /* There needs to be enough space for the file header and one |
| 1725 | * attribute header for this to make sense. */ |
| 1726 | if (ntohl(file->offset) <= |
| 1727 | sizeof(*file) + sizeof(struct cbfs_file_attribute)) |
| 1728 | return NULL; |
| 1729 | |
| 1730 | return (struct cbfs_file_attribute *) |
| 1731 | (((uint8_t *)file) + ntohl(file->attributes_offset)); |
| 1732 | } |
| 1733 | |
| 1734 | struct cbfs_file_attribute *cbfs_file_next_attr(struct cbfs_file *file, |
| 1735 | struct cbfs_file_attribute *attr) |
| 1736 | { |
| 1737 | /* ex falso sequitur quodlibet */ |
| 1738 | if (attr == NULL) |
| 1739 | return NULL; |
| 1740 | |
| 1741 | /* Is there enough space for another attribute? */ |
| 1742 | if ((uint8_t *)attr + ntohl(attr->len) + |
| 1743 | sizeof(struct cbfs_file_attribute) >= |
| 1744 | (uint8_t *)file + ntohl(file->offset)) |
| 1745 | return NULL; |
| 1746 | |
| 1747 | struct cbfs_file_attribute *next = (struct cbfs_file_attribute *) |
| 1748 | (((uint8_t *)attr) + ntohl(attr->len)); |
| 1749 | /* If any, "unused" attributes must come last. */ |
| 1750 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED) |
| 1751 | return NULL; |
| 1752 | if (ntohl(next->tag) == CBFS_FILE_ATTR_TAG_UNUSED2) |
| 1753 | return NULL; |
| 1754 | |
| 1755 | return next; |
| 1756 | } |
| 1757 | |
| 1758 | struct cbfs_file_attribute *cbfs_add_file_attr(struct cbfs_file *header, |
| 1759 | uint32_t tag, |
| 1760 | uint32_t size) |
| 1761 | { |
| 1762 | struct cbfs_file_attribute *attr, *next; |
| 1763 | next = cbfs_file_first_attr(header); |
| 1764 | do { |
| 1765 | attr = next; |
| 1766 | next = cbfs_file_next_attr(header, attr); |
| 1767 | } while (next != NULL); |
| 1768 | uint32_t header_size = ntohl(header->offset) + size; |
| 1769 | if (header_size > MAX_CBFS_FILE_HEADER_BUFFER) { |
| 1770 | DEBUG("exceeding allocated space for cbfs_file headers"); |
| 1771 | return NULL; |
| 1772 | } |
| 1773 | /* attr points to the last valid attribute now. |
| 1774 | * If NULL, we have to create the first one. */ |
| 1775 | if (attr == NULL) { |
| 1776 | /* New attributes start where the header ends. |
| 1777 | * header->offset is later set to accomodate the |
| 1778 | * additional structure. |
| 1779 | * No endianess translation necessary here, because both |
| 1780 | * fields are encoded the same way. */ |
| 1781 | header->attributes_offset = header->offset; |
| 1782 | attr = (struct cbfs_file_attribute *) |
| 1783 | (((uint8_t *)header) + |
| 1784 | ntohl(header->attributes_offset)); |
| 1785 | } else { |
| 1786 | attr = (struct cbfs_file_attribute *) |
| 1787 | (((uint8_t *)attr) + |
| 1788 | ntohl(attr->len)); |
| 1789 | } |
| 1790 | header->offset = htonl(header_size); |
| 1791 | memset(attr, CBFS_CONTENT_DEFAULT_VALUE, size); |
| 1792 | attr->tag = htonl(tag); |
| 1793 | attr->len = htonl(size); |
| 1794 | return attr; |
| 1795 | } |
| 1796 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1797 | int cbfs_add_file_hash(struct cbfs_file *header, struct buffer *buffer, |
| 1798 | enum vb2_hash_algorithm hash_type) |
| 1799 | { |
zbao | 37450ff | 2015-11-05 14:35:57 +0800 | [diff] [blame] | 1800 | uint32_t hash_index = hash_type; |
| 1801 | |
| 1802 | if (hash_index >= CBFS_NUM_SUPPORTED_HASHES) |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1803 | return -1; |
| 1804 | |
| 1805 | unsigned hash_size = widths_cbfs_hash[hash_type]; |
| 1806 | if (hash_size == 0) |
| 1807 | return -1; |
| 1808 | |
| 1809 | struct cbfs_file_attr_hash *attrs = |
| 1810 | (struct cbfs_file_attr_hash *)cbfs_add_file_attr(header, |
| 1811 | CBFS_FILE_ATTR_TAG_HASH, |
| 1812 | sizeof(struct cbfs_file_attr_hash) + hash_size); |
| 1813 | |
| 1814 | if (attrs == NULL) |
| 1815 | return -1; |
| 1816 | |
| 1817 | attrs->hash_type = htonl(hash_type); |
| 1818 | if (vb2_digest_buffer(buffer_get(buffer), buffer_size(buffer), |
| 1819 | hash_type, attrs->hash_data, hash_size) != VB2_SUCCESS) |
| 1820 | return -1; |
| 1821 | |
| 1822 | return 0; |
| 1823 | } |
| 1824 | |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1825 | /* Finds a place to hold whole data in same memory page. */ |
| 1826 | static int is_in_same_page(uint32_t start, uint32_t size, uint32_t page) |
| 1827 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1828 | if (!page) |
| 1829 | return 1; |
| 1830 | return (start / page) == (start + size - 1) / page; |
| 1831 | } |
| 1832 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1833 | /* Tests if data can fit in a range by given offset: |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1834 | * start ->| metadata_size | offset (+ size) |<- end |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1835 | */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1836 | static int is_in_range(size_t start, size_t end, size_t metadata_size, |
| 1837 | size_t offset, size_t size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1838 | { |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1839 | return (offset >= start + metadata_size && offset + size <= end); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1840 | } |
| 1841 | |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 1842 | static size_t absolute_align(const struct cbfs_image *image, size_t val, |
| 1843 | size_t align) |
| 1844 | { |
| 1845 | const size_t region_offset = buffer_offset(&image->buffer); |
| 1846 | /* To perform alignment on absolute address, take the region offset */ |
| 1847 | /* of the image into account. */ |
| 1848 | return align_up(val + region_offset, align) - region_offset; |
| 1849 | |
| 1850 | } |
| 1851 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1852 | int32_t cbfs_locate_entry(struct cbfs_image *image, size_t size, |
| 1853 | size_t page_size, size_t align, size_t metadata_size) |
Stefan Reinauer | dc7bc8e | 2013-03-26 12:51:36 -0700 | [diff] [blame] | 1854 | { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1855 | struct cbfs_file *entry; |
| 1856 | size_t need_len; |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1857 | size_t addr, addr_next, addr2, addr3, offset; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1858 | |
| 1859 | /* Default values: allow fitting anywhere in ROM. */ |
| 1860 | if (!page_size) |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1861 | page_size = image->has_header ? image->header.romsize : |
| 1862 | image->buffer.size; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1863 | if (!align) |
| 1864 | align = 1; |
| 1865 | |
| 1866 | if (size > page_size) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1867 | ERROR("Input file size (%zd) greater than page size (%zd).\n", |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1868 | size, page_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1869 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1870 | size_t image_align = image->has_header ? image->header.align : |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1871 | CBFS_ENTRY_ALIGNMENT; |
| 1872 | if (page_size % image_align) |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1873 | WARN("%s: Page size (%#zx) not aligned with CBFS image (%#zx).\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1874 | __func__, page_size, image_align); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1875 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1876 | need_len = metadata_size + size; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1877 | |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1878 | // Merge empty entries to build get max available space. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1879 | cbfs_walk(image, cbfs_merge_empty_entry, NULL); |
| 1880 | |
| 1881 | /* Three cases of content location on memory page: |
| 1882 | * case 1. |
| 1883 | * | PAGE 1 | PAGE 2 | |
| 1884 | * | <header><content>| Fit. Return start of content. |
| 1885 | * |
| 1886 | * case 2. |
| 1887 | * | PAGE 1 | PAGE 2 | |
| 1888 | * | <header><content> | Fits when we shift content to align |
| 1889 | * shift-> | <header>|<content> | at starting of PAGE 2. |
| 1890 | * |
| 1891 | * case 3. (large content filling whole page) |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1892 | * | PAGE 1 | PAGE 2 | PAGE 3 | |
| 1893 | * | <header>< content > | Can't fit. If we shift content to |
| 1894 | * |trial-> <header>< content > | PAGE 2, header can't fit in free |
| 1895 | * | shift-> <header><content> space, so we must use PAGE 3. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1896 | * |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1897 | * The returned address can be then used as "base-address" (-b) in add-* |
| 1898 | * commands (will be re-calculated and positioned by cbfs_add_entry_at). |
| 1899 | * For stage targets, the address is also used to re-link stage before |
| 1900 | * being added into CBFS. |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1901 | */ |
| 1902 | for (entry = cbfs_find_first_entry(image); |
Hung-Te Lin | 408aefd | 2013-02-09 10:38:55 +0800 | [diff] [blame] | 1903 | entry && cbfs_is_valid_entry(image, entry); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1904 | entry = cbfs_find_next_entry(image, entry)) { |
| 1905 | |
| 1906 | uint32_t type = ntohl(entry->type); |
| 1907 | if (type != CBFS_COMPONENT_NULL) |
| 1908 | continue; |
| 1909 | |
| 1910 | addr = cbfs_get_entry_addr(image, entry); |
| 1911 | addr_next = cbfs_get_entry_addr(image, cbfs_find_next_entry( |
| 1912 | image, entry)); |
| 1913 | if (addr_next - addr < need_len) |
| 1914 | continue; |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1915 | |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 1916 | offset = absolute_align(image, addr + metadata_size, align); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1917 | if (is_in_same_page(offset, size, page_size) && |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1918 | is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1919 | DEBUG("cbfs_locate_entry: FIT (PAGE1)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1920 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | addr2 = align_up(addr, page_size); |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 1924 | offset = absolute_align(image, addr2, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1925 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1926 | DEBUG("cbfs_locate_entry: OVERLAP (PAGE2)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1927 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1928 | } |
| 1929 | |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1930 | /* Assume page_size >= metadata_size so adding one page will |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1931 | * definitely provide the space for header. */ |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1932 | assert(page_size >= metadata_size); |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1933 | addr3 = addr2 + page_size; |
Werner Zeh | 95bfcae | 2016-01-25 12:47:20 +0100 | [diff] [blame] | 1934 | offset = absolute_align(image, addr3, align); |
Aaron Durbin | d733941 | 2015-09-15 12:50:14 -0500 | [diff] [blame] | 1935 | if (is_in_range(addr, addr_next, metadata_size, offset, size)) { |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1936 | DEBUG("cbfs_locate_entry: OVERLAP+ (PAGE3)."); |
Hung-Te Lin | e4ea2ca | 2013-03-19 12:24:43 +0800 | [diff] [blame] | 1937 | return offset; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | return -1; |
| 1941 | } |