Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* cbfstool, CLI utility for CBFS file manipulation */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 3 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 4 | #include <stdio.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 5 | #include <stdlib.h> |
Stefan Reinauer | 336daa7 | 2009-12-21 15:09:01 +0000 | [diff] [blame] | 6 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 7 | #include <strings.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 8 | #include <ctype.h> |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 9 | #include <unistd.h> |
| 10 | #include <getopt.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 11 | #include "common.h" |
| 12 | #include "cbfs.h" |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 13 | #include "cbfs_image.h" |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 14 | #include "cbfs_sections.h" |
Aaron Durbin | facf149 | 2017-12-18 14:50:22 -0700 | [diff] [blame] | 15 | #include "elfparsing.h" |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 16 | #include "partitioned_file.h" |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 17 | #include "lz4/lib/xxhash.h" |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 18 | #include <commonlib/bsd/cbfs_private.h> |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 19 | #include <commonlib/bsd/compression.h> |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 20 | #include <commonlib/bsd/metadata_hash.h> |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 21 | #include <commonlib/fsp.h> |
Nico Huber | 607796a | 2017-01-17 13:01:25 +0100 | [diff] [blame] | 22 | #include <commonlib/endian.h> |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 23 | #include <commonlib/helpers.h> |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 24 | #include <commonlib/region.h> |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 25 | #include <vboot_host.h> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 26 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 27 | struct command { |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 28 | const char *name; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 29 | const char *optstring; |
| 30 | int (*function) (void); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 31 | // Whether to populate param.image_region before invoking function |
| 32 | bool accesses_region; |
Vadim Bendebury | 7559946 | 2015-12-09 09:39:31 -0800 | [diff] [blame] | 33 | // This set to true means two things: |
| 34 | // - in case of a command operating on a region, the region's contents |
| 35 | // will be written back to image_file at the end |
| 36 | // - write access to the file is required |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 37 | bool modifies_region; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 40 | static struct param { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 41 | partitioned_file_t *image_file; |
| 42 | struct buffer *image_region; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 43 | const char *name; |
| 44 | const char *filename; |
| 45 | const char *fmap; |
| 46 | const char *region_name; |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 47 | const char *source_region; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 48 | const char *bootblock; |
| 49 | const char *ignore_section; |
Rizwan Qureshi | c1072f2 | 2018-06-04 23:02:46 +0530 | [diff] [blame] | 50 | const char *ucode_region; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 51 | uint64_t u64val; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 52 | uint32_t type; |
| 53 | uint32_t baseaddress; |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 54 | /* |
| 55 | * Input can be negative. It will be transformed to offset from start of region (if |
| 56 | * negative) and stored in baseaddress. |
| 57 | */ |
| 58 | long long int baseaddress_input; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 59 | uint32_t baseaddress_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 60 | uint32_t loadaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 61 | uint32_t headeroffset; |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 62 | /* |
| 63 | * Input can be negative. It will be transformed to offset from start of region (if |
| 64 | * negative) and stored in baseaddress. |
| 65 | */ |
| 66 | long long int headeroffset_input; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 67 | uint32_t headeroffset_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 68 | uint32_t entrypoint; |
| 69 | uint32_t size; |
| 70 | uint32_t alignment; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 71 | uint32_t pagesize; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 72 | uint32_t cbfsoffset; |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 73 | /* |
| 74 | * Input can be negative. It will be transformed to corresponding region offset (if |
| 75 | * negative) and stored in baseaddress. |
| 76 | */ |
| 77 | long long int cbfsoffset_input; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 78 | uint32_t cbfsoffset_assigned; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 79 | uint32_t arch; |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 80 | uint32_t padding; |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 81 | uint32_t topswap_size; |
Patrick Georgi | 16b3e4b | 2016-12-12 18:38:01 +0100 | [diff] [blame] | 82 | bool u64val_assigned; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 83 | bool fill_partial_upward; |
| 84 | bool fill_partial_downward; |
| 85 | bool show_immutable; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 86 | bool stage_xip; |
Kyösti Mälkki | c36469e | 2020-05-28 23:12:02 +0300 | [diff] [blame] | 87 | bool force_pow2_pagesize; |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 88 | bool autogen_attr; |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 89 | bool machine_parseable; |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 90 | bool unprocessed; |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 91 | bool ibb; |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 92 | enum cbfs_compression compression; |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 93 | int precompression; |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 94 | enum vb2_hash_algorithm hash; |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 95 | /* For linux payloads */ |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 96 | char *initrd; |
| 97 | char *cmdline; |
Patrick Georgi | 01fbc3a | 2016-10-12 16:46:13 +0200 | [diff] [blame] | 98 | int force; |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 99 | /* |
| 100 | * Base and size of extended window for decoding SPI flash greater than 16MiB in host |
| 101 | * address space on x86 platforms. The assumptions here are: |
| 102 | * 1. Top 16MiB is still decoded in the fixed decode window just below 4G boundary. |
| 103 | * 2. Rest of the SPI flash below the top 16MiB is mapped at the top of extended |
| 104 | * window. Even though the platform might support a larger extended window, the SPI |
| 105 | * flash part used by the mainboard might not be large enough to be mapped in the entire |
| 106 | * window. In such cases, the mapping is assumed to be in the top part of the extended |
| 107 | * window with the bottom part remaining unused. |
| 108 | */ |
| 109 | uint32_t ext_win_base; |
| 110 | uint32_t ext_win_size; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 111 | } param = { |
| 112 | /* All variables not listed are initialized as zero. */ |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 113 | .arch = CBFS_ARCHITECTURE_UNKNOWN, |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 114 | .compression = CBFS_COMPRESS_NONE, |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 115 | .hash = VB2_HASH_INVALID, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 116 | .headeroffset = ~0, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 117 | .region_name = SECTION_NAME_PRIMARY_CBFS, |
Patrick Georgi | d9edb18 | 2016-12-06 18:55:26 +0100 | [diff] [blame] | 118 | .u64val = -1, |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 119 | }; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 120 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 121 | /* |
| 122 | * This "metadata_hash cache" caches the value and location of the CBFS metadata |
| 123 | * hash embedded in the bootblock when CBFS verification is enabled. The first |
| 124 | * call to get_mh_cache() searches for the cache by scanning the whole bootblock |
| 125 | * for its 8-byte signature, later calls will just return the previously found |
| 126 | * information again. If the cbfs_hash.algo member in the result is |
| 127 | * VB2_HASH_INVALID, that means no metadata hash was found and this image does |
| 128 | * not use CBFS verification. |
| 129 | */ |
| 130 | struct mh_cache { |
| 131 | const char *region; |
| 132 | size_t offset; |
| 133 | struct vb2_hash cbfs_hash; |
Julius Werner | 76dab5f | 2020-03-19 21:09:35 -0700 | [diff] [blame] | 134 | platform_fixup_func fixup; |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 135 | bool initialized; |
| 136 | }; |
| 137 | |
| 138 | static struct mh_cache *get_mh_cache(void) |
| 139 | { |
| 140 | static struct mh_cache mhc; |
| 141 | |
| 142 | if (mhc.initialized) |
| 143 | return &mhc; |
| 144 | |
| 145 | mhc.initialized = true; |
| 146 | |
| 147 | const struct fmap *fmap = partitioned_file_get_fmap(param.image_file); |
| 148 | if (!fmap) |
| 149 | goto no_metadata_hash; |
| 150 | |
| 151 | /* Find the bootblock. If there is a "BOOTBLOCK" FMAP section, it's |
| 152 | there. If not, it's a normal file in the primary CBFS section. */ |
| 153 | size_t offset, size; |
| 154 | struct buffer buffer; |
| 155 | if (fmap_find_area(fmap, SECTION_NAME_BOOTBLOCK)) { |
| 156 | if (!partitioned_file_read_region(&buffer, param.image_file, |
| 157 | SECTION_NAME_BOOTBLOCK)) |
| 158 | goto no_metadata_hash; |
| 159 | mhc.region = SECTION_NAME_BOOTBLOCK; |
| 160 | offset = 0; |
| 161 | size = buffer.size; |
| 162 | } else { |
| 163 | struct cbfs_image cbfs; |
| 164 | struct cbfs_file *bootblock; |
| 165 | if (!partitioned_file_read_region(&buffer, param.image_file, |
| 166 | SECTION_NAME_PRIMARY_CBFS)) |
| 167 | goto no_metadata_hash; |
| 168 | mhc.region = SECTION_NAME_PRIMARY_CBFS; |
| 169 | if (cbfs_image_from_buffer(&cbfs, &buffer, param.headeroffset)) |
| 170 | goto no_metadata_hash; |
| 171 | bootblock = cbfs_get_entry(&cbfs, "bootblock"); |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 172 | if (!bootblock || be32toh(bootblock->type) != CBFS_TYPE_BOOTBLOCK) |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 173 | goto no_metadata_hash; |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 174 | offset = (void *)bootblock + be32toh(bootblock->offset) - |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 175 | buffer_get(&cbfs.buffer); |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 176 | size = be32toh(bootblock->len); |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Find and validate the metadata hash anchor inside the bootblock and |
| 180 | record its exact byte offset from the start of the FMAP region. */ |
| 181 | struct metadata_hash_anchor *anchor = memmem(buffer_get(&buffer) + offset, |
| 182 | size, METADATA_HASH_ANCHOR_MAGIC, sizeof(anchor->magic)); |
| 183 | if (anchor) { |
| 184 | if (!vb2_digest_size(anchor->cbfs_hash.algo)) { |
| 185 | ERROR("Unknown CBFS metadata hash type: %d\n", |
| 186 | anchor->cbfs_hash.algo); |
| 187 | goto no_metadata_hash; |
| 188 | } |
| 189 | mhc.cbfs_hash = anchor->cbfs_hash; |
| 190 | mhc.offset = (void *)anchor - buffer_get(&buffer); |
Julius Werner | 76dab5f | 2020-03-19 21:09:35 -0700 | [diff] [blame] | 191 | mhc.fixup = platform_fixups_probe(&buffer, mhc.offset, |
| 192 | mhc.region); |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 193 | return &mhc; |
| 194 | } |
| 195 | |
| 196 | no_metadata_hash: |
| 197 | mhc.cbfs_hash.algo = VB2_HASH_INVALID; |
| 198 | return &mhc; |
| 199 | } |
| 200 | |
| 201 | static void update_and_info(const char *name, void *dst, void *src, size_t size) |
| 202 | { |
| 203 | if (!memcmp(dst, src, size)) |
| 204 | return; |
| 205 | char *src_str = bintohex(src, size); |
| 206 | char *dst_str = bintohex(dst, size); |
| 207 | INFO("Updating %s from %s to %s\n", name, dst_str, src_str); |
| 208 | memcpy(dst, src, size); |
| 209 | free(src_str); |
| 210 | free(dst_str); |
| 211 | } |
| 212 | |
| 213 | static int update_anchor(struct mh_cache *mhc, uint8_t *fmap_hash) |
| 214 | { |
| 215 | struct buffer buffer; |
| 216 | if (!partitioned_file_read_region(&buffer, param.image_file, |
| 217 | mhc->region)) |
| 218 | return -1; |
| 219 | struct metadata_hash_anchor *anchor = buffer_get(&buffer) + mhc->offset; |
| 220 | /* The metadata hash anchor should always still be where we left it. */ |
| 221 | assert(!memcmp(anchor->magic, METADATA_HASH_ANCHOR_MAGIC, |
| 222 | sizeof(anchor->magic)) && |
| 223 | anchor->cbfs_hash.algo == mhc->cbfs_hash.algo); |
| 224 | update_and_info("CBFS metadata hash", anchor->cbfs_hash.raw, |
| 225 | mhc->cbfs_hash.raw, vb2_digest_size(anchor->cbfs_hash.algo)); |
| 226 | if (fmap_hash) { |
| 227 | update_and_info("FMAP hash", |
| 228 | metadata_hash_anchor_fmap_hash(anchor), fmap_hash, |
| 229 | vb2_digest_size(anchor->cbfs_hash.algo)); |
| 230 | } |
Julius Werner | 76dab5f | 2020-03-19 21:09:35 -0700 | [diff] [blame] | 231 | if (mhc->fixup && mhc->fixup(&buffer, mhc->offset) != 0) |
| 232 | return -1; |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 233 | if (!partitioned_file_write_region(param.image_file, &buffer)) |
| 234 | return -1; |
| 235 | return 0; |
| 236 | |
| 237 | } |
| 238 | |
| 239 | /* This should be called after every time CBFS metadata might have changed. It |
| 240 | will recalculate and update the metadata hash in the bootblock if needed. */ |
| 241 | static int maybe_update_metadata_hash(struct cbfs_image *cbfs) |
| 242 | { |
| 243 | if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)) |
| 244 | return 0; /* Metadata hash only embedded in primary CBFS. */ |
| 245 | |
| 246 | struct mh_cache *mhc = get_mh_cache(); |
| 247 | if (mhc->cbfs_hash.algo == VB2_HASH_INVALID) |
| 248 | return 0; |
| 249 | |
| 250 | cb_err_t err = cbfs_walk(cbfs, NULL, NULL, &mhc->cbfs_hash, |
| 251 | CBFS_WALK_WRITEBACK_HASH); |
| 252 | if (err != CB_CBFS_NOT_FOUND) { |
| 253 | ERROR("Unexpected cbfs_walk() error %d\n", err); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | return update_anchor(mhc, NULL); |
| 258 | } |
| 259 | |
| 260 | /* This should be called after every time the FMAP or the bootblock itself might |
| 261 | have changed, and will write the new FMAP hash into the metadata hash anchor |
| 262 | in the bootblock if required (usually when the bootblock is first added). */ |
| 263 | static int maybe_update_fmap_hash(void) |
| 264 | { |
| 265 | if (strcmp(param.region_name, SECTION_NAME_BOOTBLOCK) && |
| 266 | strcmp(param.region_name, SECTION_NAME_FMAP) && |
| 267 | param.type != CBFS_TYPE_BOOTBLOCK) |
| 268 | return 0; /* FMAP and bootblock didn't change. */ |
| 269 | |
| 270 | struct mh_cache *mhc = get_mh_cache(); |
| 271 | if (mhc->cbfs_hash.algo == VB2_HASH_INVALID) |
| 272 | return 0; |
| 273 | |
| 274 | uint8_t fmap_hash[VB2_MAX_DIGEST_SIZE]; |
| 275 | const struct fmap *fmap = partitioned_file_get_fmap(param.image_file); |
| 276 | if (!fmap || vb2_digest_buffer((const void *)fmap, fmap_size(fmap), |
| 277 | mhc->cbfs_hash.algo, fmap_hash, sizeof(fmap_hash))) |
| 278 | return -1; |
| 279 | return update_anchor(mhc, fmap_hash); |
| 280 | } |
| 281 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 282 | static bool region_is_flashmap(const char *region) |
| 283 | { |
| 284 | return partitioned_file_region_check_magic(param.image_file, region, |
| 285 | FMAP_SIGNATURE, strlen(FMAP_SIGNATURE)); |
| 286 | } |
| 287 | |
| 288 | /* @return Same as cbfs_is_valid_cbfs(), but for a named region. */ |
| 289 | static bool region_is_modern_cbfs(const char *region) |
| 290 | { |
| 291 | return partitioned_file_region_check_magic(param.image_file, region, |
| 292 | CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC)); |
| 293 | } |
| 294 | |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 295 | /* This describes a window from the SPI flash address space into the host address space. */ |
| 296 | struct mmap_window { |
| 297 | struct region flash_space; |
| 298 | struct region host_space; |
| 299 | }; |
| 300 | |
| 301 | enum mmap_window_type { |
| 302 | X86_DEFAULT_DECODE_WINDOW, /* Decode window just below 4G boundary */ |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 303 | X86_EXTENDED_DECODE_WINDOW, /* Extended decode window for mapping greater than 16MiB |
| 304 | flash */ |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 305 | MMAP_MAX_WINDOWS, |
| 306 | }; |
| 307 | |
| 308 | /* Table of all the decode windows supported by the platform. */ |
| 309 | static struct mmap_window mmap_window_table[MMAP_MAX_WINDOWS]; |
| 310 | |
| 311 | static void add_mmap_window(enum mmap_window_type idx, size_t flash_offset, size_t host_offset, |
| 312 | size_t window_size) |
| 313 | { |
| 314 | if (idx >= MMAP_MAX_WINDOWS) { |
| 315 | ERROR("Incorrect mmap window index(%d)\n", idx); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | mmap_window_table[idx].flash_space.offset = flash_offset; |
| 320 | mmap_window_table[idx].host_space.offset = host_offset; |
| 321 | mmap_window_table[idx].flash_space.size = window_size; |
| 322 | mmap_window_table[idx].host_space.size = window_size; |
| 323 | } |
| 324 | |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 325 | #define DEFAULT_DECODE_WINDOW_TOP (4ULL * GiB) |
| 326 | #define DEFAULT_DECODE_WINDOW_MAX_SIZE (16 * MiB) |
| 327 | |
| 328 | static bool create_mmap_windows(void) |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 329 | { |
| 330 | static bool done; |
| 331 | |
| 332 | if (done) |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 333 | return done; |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 334 | |
| 335 | const size_t image_size = partitioned_file_total_size(param.image_file); |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 336 | const size_t std_window_size = MIN(DEFAULT_DECODE_WINDOW_MAX_SIZE, image_size); |
| 337 | const size_t std_window_flash_offset = image_size - std_window_size; |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Default decode window lives just below 4G boundary in host space and maps up to a |
| 341 | * maximum of 16MiB. If the window is smaller than 16MiB, the SPI flash window is mapped |
| 342 | * at the top of the host window just below 4G. |
| 343 | */ |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 344 | add_mmap_window(X86_DEFAULT_DECODE_WINDOW, std_window_flash_offset, |
| 345 | DEFAULT_DECODE_WINDOW_TOP - std_window_size, std_window_size); |
| 346 | |
| 347 | if (param.ext_win_size && (image_size > DEFAULT_DECODE_WINDOW_MAX_SIZE)) { |
| 348 | /* |
| 349 | * If the platform supports extended window and the SPI flash size is greater |
| 350 | * than 16MiB, then create a mapping for the extended window as well. |
| 351 | * The assumptions here are: |
| 352 | * 1. Top 16MiB is still decoded in the fixed decode window just below 4G |
| 353 | * boundary. |
| 354 | * 2. Rest of the SPI flash below the top 16MiB is mapped at the top of extended |
| 355 | * window. Even though the platform might support a larger extended window, the |
| 356 | * SPI flash part used by the mainboard might not be large enough to be mapped |
| 357 | * in the entire window. In such cases, the mapping is assumed to be in the top |
| 358 | * part of the extended window with the bottom part remaining unused. |
| 359 | * |
| 360 | * Example: |
| 361 | * ext_win_base = 0xF8000000 |
| 362 | * ext_win_size = 32 * MiB |
| 363 | * ext_win_limit = ext_win_base + ext_win_size - 1 = 0xF9FFFFFF |
| 364 | * |
| 365 | * If SPI flash is 32MiB, then top 16MiB is mapped from 0xFF000000 - 0xFFFFFFFF |
| 366 | * whereas the bottom 16MiB is mapped from 0xF9000000 - 0xF9FFFFFF. The extended |
| 367 | * window 0xF8000000 - 0xF8FFFFFF remains unused. |
| 368 | */ |
| 369 | const size_t ext_window_mapped_size = MIN(param.ext_win_size, |
| 370 | image_size - std_window_size); |
| 371 | const size_t ext_window_top = param.ext_win_base + param.ext_win_size; |
| 372 | add_mmap_window(X86_EXTENDED_DECODE_WINDOW, |
| 373 | std_window_flash_offset - ext_window_mapped_size, |
| 374 | ext_window_top - ext_window_mapped_size, |
| 375 | ext_window_mapped_size); |
| 376 | |
| 377 | if (region_overlap(&mmap_window_table[X86_EXTENDED_DECODE_WINDOW].host_space, |
| 378 | &mmap_window_table[X86_DEFAULT_DECODE_WINDOW].host_space)) { |
| 379 | const struct region *ext_region; |
| 380 | |
| 381 | ext_region = &mmap_window_table[X86_EXTENDED_DECODE_WINDOW].host_space; |
| 382 | ERROR("Extended window(base=0x%zx, limit=0x%zx) overlaps with default window!\n", |
| 383 | region_offset(ext_region), region_end(ext_region)); |
| 384 | |
| 385 | return false; |
| 386 | } |
| 387 | } |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 388 | |
| 389 | done = true; |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 390 | return done; |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static unsigned int convert_address(const struct region *to, const struct region *from, |
| 394 | unsigned int addr) |
| 395 | { |
| 396 | /* |
| 397 | * Calculate the offset in the "from" region and use that offset to calculate |
| 398 | * corresponding address in the "to" region. |
| 399 | */ |
| 400 | size_t offset = addr - region_offset(from); |
| 401 | return region_offset(to) + offset; |
| 402 | } |
| 403 | |
| 404 | enum mmap_addr_type { |
| 405 | HOST_SPACE_ADDR, |
| 406 | FLASH_SPACE_ADDR, |
| 407 | }; |
| 408 | |
| 409 | static int find_mmap_window(enum mmap_addr_type addr_type, unsigned int addr) |
| 410 | { |
| 411 | size_t i; |
| 412 | |
| 413 | for (i = 0; i < ARRAY_SIZE(mmap_window_table); i++) { |
| 414 | const struct region *reg; |
| 415 | |
| 416 | if (addr_type == HOST_SPACE_ADDR) |
| 417 | reg = &mmap_window_table[i].host_space; |
| 418 | else |
| 419 | reg = &mmap_window_table[i].flash_space; |
| 420 | |
Kyösti Mälkki | b57373b | 2021-02-13 01:57:14 +0200 | [diff] [blame] | 421 | if (region_offset(reg) <= addr && |
| 422 | ((uint64_t)region_offset(reg) + (uint64_t)region_sz(reg) - 1) >= addr) |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 423 | return i; |
| 424 | } |
| 425 | |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | static unsigned int convert_host_to_flash(const struct buffer *region, unsigned int addr) |
| 430 | { |
| 431 | int idx; |
| 432 | const struct region *to, *from; |
| 433 | |
| 434 | idx = find_mmap_window(HOST_SPACE_ADDR, addr); |
| 435 | if (idx == -1) { |
| 436 | ERROR("Host address(%x) not in any mmap window!\n", addr); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | to = &mmap_window_table[idx].flash_space; |
| 441 | from = &mmap_window_table[idx].host_space; |
| 442 | |
| 443 | /* region->offset is subtracted because caller expects offset in the given region. */ |
| 444 | return convert_address(to, from, addr) - region->offset; |
| 445 | } |
| 446 | |
| 447 | static unsigned int convert_flash_to_host(const struct buffer *region, unsigned int addr) |
| 448 | { |
| 449 | int idx; |
| 450 | const struct region *to, *from; |
| 451 | |
| 452 | /* |
| 453 | * region->offset is added because caller provides offset in the given region. This is |
| 454 | * converted to an absolute address in the SPI flash space. This is done before the |
| 455 | * conversion as opposed to after in convert_host_to_flash() above because the address |
| 456 | * is actually an offset within the region. So, it needs to be converted into an |
| 457 | * absolute address in the SPI flash space before converting into an address in host |
| 458 | * space. |
| 459 | */ |
| 460 | addr += region->offset; |
| 461 | idx = find_mmap_window(FLASH_SPACE_ADDR, addr); |
| 462 | |
| 463 | if (idx == -1) { |
| 464 | ERROR("SPI flash address(%x) not in any mmap window!\n", addr); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | to = &mmap_window_table[idx].host_space; |
| 469 | from = &mmap_window_table[idx].flash_space; |
| 470 | |
| 471 | return convert_address(to, from, addr); |
| 472 | } |
| 473 | |
| 474 | static unsigned int convert_addr_space(const struct buffer *region, unsigned int addr) |
Aaron Durbin | ab00d77 | 2016-05-04 16:07:15 -0500 | [diff] [blame] | 475 | { |
| 476 | assert(region); |
| 477 | |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 478 | assert(create_mmap_windows()); |
Aaron Durbin | ab00d77 | 2016-05-04 16:07:15 -0500 | [diff] [blame] | 479 | |
Furquan Shaikh | 0dcc066 | 2020-11-20 22:51:17 -0800 | [diff] [blame] | 480 | if (IS_HOST_SPACE_ADDRESS(addr)) |
| 481 | return convert_host_to_flash(region, addr); |
| 482 | else |
| 483 | return convert_flash_to_host(region, addr); |
Aaron Durbin | ab00d77 | 2016-05-04 16:07:15 -0500 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 487 | * This function takes offset value which represents the offset from one end of the region and |
| 488 | * converts it to offset from the other end of the region. offset is expected to be positive. |
| 489 | */ |
| 490 | static int convert_region_offset(unsigned int offset, uint32_t *region_offset) |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 491 | { |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 492 | size_t size; |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 493 | |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 494 | if (param.size) { |
| 495 | size = param.size; |
| 496 | } else { |
| 497 | assert(param.image_region); |
| 498 | size = param.image_region->size; |
Patrick Georgi | 9731119 | 2015-12-07 23:14:07 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 501 | if (size < offset) { |
| 502 | ERROR("Cannot convert region offset (size=0x%zx, offset=0x%x)\n", size, offset); |
| 503 | return 1; |
| 504 | } |
| 505 | |
| 506 | *region_offset = size - offset; |
| 507 | return 0; |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 510 | static int do_cbfs_locate(uint32_t *cbfs_addr, size_t data_size) |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 511 | { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 512 | uint32_t metadata_size = 0; |
| 513 | |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 514 | if (!param.name) { |
| 515 | ERROR("You need to specify -n/--name.\n"); |
| 516 | return 1; |
| 517 | } |
| 518 | |
| 519 | struct cbfs_image image; |
| 520 | if (cbfs_image_from_buffer(&image, param.image_region, |
| 521 | param.headeroffset)) |
| 522 | return 1; |
| 523 | |
| 524 | if (cbfs_get_entry(&image, param.name)) |
| 525 | WARN("'%s' already in CBFS.\n", param.name); |
| 526 | |
Aaron Durbin | facf149 | 2017-12-18 14:50:22 -0700 | [diff] [blame] | 527 | if (!data_size) { |
Julius Werner | 772714d | 2021-12-13 10:16:44 -0800 | [diff] [blame] | 528 | ERROR("File '%s' is empty?\n", param.name); |
| 529 | return 1; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 530 | } |
| 531 | |
Arthur Heymans | c78521b3 | 2021-06-16 17:16:05 +0200 | [diff] [blame] | 532 | /* Compute required page size */ |
Kyösti Mälkki | c36469e | 2020-05-28 23:12:02 +0300 | [diff] [blame] | 533 | if (param.force_pow2_pagesize) { |
Arthur Heymans | c78521b3 | 2021-06-16 17:16:05 +0200 | [diff] [blame] | 534 | param.pagesize = 1; |
| 535 | while (param.pagesize < data_size) |
| 536 | param.pagesize <<= 1; |
Kyösti Mälkki | c36469e | 2020-05-28 23:12:02 +0300 | [diff] [blame] | 537 | DEBUG("Page size is %d (0x%x)\n", param.pagesize, param.pagesize); |
| 538 | } |
| 539 | |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 540 | /* Include cbfs_file size along with space for with name. */ |
| 541 | metadata_size += cbfs_calculate_file_header_size(param.name); |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 542 | /* Adjust metadata_size if additional attributes were added */ |
| 543 | if (param.autogen_attr) { |
| 544 | if (param.alignment) |
| 545 | metadata_size += sizeof(struct cbfs_file_attr_align); |
| 546 | if (param.baseaddress_assigned || param.stage_xip) |
| 547 | metadata_size += sizeof(struct cbfs_file_attr_position); |
| 548 | } |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 549 | if (param.precompression || param.compression != CBFS_COMPRESS_NONE) |
| 550 | metadata_size += sizeof(struct cbfs_file_attr_compression); |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 551 | if (param.type == CBFS_TYPE_STAGE) |
| 552 | metadata_size += sizeof(struct cbfs_file_attr_stageheader); |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 553 | |
Werner Zeh | c7b2b7c | 2016-01-22 19:43:01 +0100 | [diff] [blame] | 554 | /* Take care of the hash attribute if it is used */ |
| 555 | if (param.hash != VB2_HASH_INVALID) |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 556 | metadata_size += cbfs_file_attr_hash_size(param.hash); |
Werner Zeh | c7b2b7c | 2016-01-22 19:43:01 +0100 | [diff] [blame] | 557 | |
Aaron Durbin | facf149 | 2017-12-18 14:50:22 -0700 | [diff] [blame] | 558 | int32_t address = cbfs_locate_entry(&image, data_size, param.pagesize, |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 559 | param.alignment, metadata_size); |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 560 | |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 561 | if (address < 0) { |
Julius Werner | 772714d | 2021-12-13 10:16:44 -0800 | [diff] [blame] | 562 | ERROR("'%s'(%u + %zu) can't fit in CBFS for page-size %#x, align %#x.\n", |
| 563 | param.name, metadata_size, data_size, param.pagesize, param.alignment); |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 564 | return 1; |
| 565 | } |
| 566 | |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 567 | *cbfs_addr = address; |
| 568 | return 0; |
| 569 | } |
| 570 | |
Patrick Georgi | f820497 | 2015-08-11 15:16:24 +0200 | [diff] [blame] | 571 | typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 572 | struct cbfs_file *header); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 573 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 574 | static int cbfs_add_integer_component(const char *name, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 575 | uint64_t u64val, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 576 | uint32_t offset, |
| 577 | uint32_t headeroffset) { |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 578 | struct cbfs_image image; |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 579 | struct cbfs_file *header = NULL; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 580 | struct buffer buffer; |
| 581 | int i, ret = 1; |
| 582 | |
| 583 | if (!name) { |
| 584 | ERROR("You need to specify -n/--name.\n"); |
| 585 | return 1; |
| 586 | } |
| 587 | |
| 588 | if (buffer_create(&buffer, 8, name) != 0) |
| 589 | return 1; |
| 590 | |
| 591 | for (i = 0; i < 8; i++) |
| 592 | buffer.data[i] = (u64val >> i*8) & 0xff; |
| 593 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 594 | if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) { |
| 595 | ERROR("Selected image region is not a CBFS.\n"); |
| 596 | goto done; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | if (cbfs_get_entry(&image, name)) { |
| 600 | ERROR("'%s' already in ROM image.\n", name); |
| 601 | goto done; |
| 602 | } |
| 603 | |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 604 | header = cbfs_create_file_header(CBFS_TYPE_RAW, |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 605 | buffer.size, name); |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 606 | |
| 607 | enum vb2_hash_algorithm algo = get_mh_cache()->cbfs_hash.algo; |
| 608 | if (algo != VB2_HASH_INVALID) |
| 609 | if (cbfs_add_file_hash(header, &buffer, algo)) { |
| 610 | ERROR("couldn't add hash for '%s'\n", name); |
| 611 | goto done; |
| 612 | } |
| 613 | |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 614 | if (cbfs_add_entry(&image, &buffer, offset, header, 0) != 0) { |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 615 | ERROR("Failed to add %llu into ROM image as '%s'.\n", |
| 616 | (long long unsigned)u64val, name); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 617 | goto done; |
| 618 | } |
| 619 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 620 | ret = maybe_update_metadata_hash(&image); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 621 | |
| 622 | done: |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 623 | free(header); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 624 | buffer_delete(&buffer); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 625 | return ret; |
| 626 | } |
| 627 | |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 628 | static int is_valid_topswap(void) |
| 629 | { |
| 630 | switch (param.topswap_size) { |
| 631 | case (64 * KiB): |
| 632 | case (128 * KiB): |
| 633 | case (256 * KiB): |
| 634 | case (512 * KiB): |
| 635 | case (1 * MiB): |
| 636 | break; |
| 637 | default: |
| 638 | ERROR("Invalid topswap_size %d, topswap can be 64K|128K|256K|512K|1M\n", |
| 639 | param.topswap_size); |
| 640 | return 0; |
| 641 | } |
| 642 | return 1; |
| 643 | } |
| 644 | |
| 645 | static void fill_header_offset(void *location, uint32_t offset) |
| 646 | { |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 647 | // TODO: When we have a BE target, we'll need to store this as BE |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 648 | write_le32(location, offset); |
| 649 | } |
| 650 | |
| 651 | static int update_master_header_loc_topswap(struct cbfs_image *image, |
| 652 | void *h_loc, uint32_t header_offset) |
| 653 | { |
| 654 | struct cbfs_file *entry; |
| 655 | void *ts_h_loc = h_loc; |
| 656 | |
| 657 | entry = cbfs_get_entry(image, "bootblock"); |
| 658 | if (entry == NULL) { |
| 659 | ERROR("Bootblock not in ROM image?!?\n"); |
| 660 | return 1; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * Check if the existing topswap boundary matches with |
| 665 | * the one provided. |
| 666 | */ |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 667 | if (param.topswap_size != be32toh(entry->len)/2) { |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 668 | ERROR("Top swap boundary does not match\n"); |
| 669 | return 1; |
| 670 | } |
| 671 | |
| 672 | ts_h_loc -= param.topswap_size; |
| 673 | fill_header_offset(ts_h_loc, header_offset); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 678 | static int cbfs_add_master_header(void) |
| 679 | { |
| 680 | const char * const name = "cbfs master header"; |
| 681 | struct cbfs_image image; |
| 682 | struct cbfs_file *header = NULL; |
| 683 | struct buffer buffer; |
| 684 | int ret = 1; |
Aaron Durbin | bb826ef | 2016-01-04 13:56:00 -0600 | [diff] [blame] | 685 | size_t offset; |
| 686 | size_t size; |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 687 | void *h_loc; |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 688 | |
| 689 | if (cbfs_image_from_buffer(&image, param.image_region, |
| 690 | param.headeroffset)) { |
| 691 | ERROR("Selected image region is not a CBFS.\n"); |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | if (cbfs_get_entry(&image, name)) { |
| 696 | ERROR("'%s' already in ROM image.\n", name); |
| 697 | return 1; |
| 698 | } |
| 699 | |
| 700 | if (buffer_create(&buffer, sizeof(struct cbfs_header), name) != 0) |
| 701 | return 1; |
| 702 | |
| 703 | struct cbfs_header *h = (struct cbfs_header *)buffer.data; |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 704 | h->magic = htobe32(CBFS_HEADER_MAGIC); |
| 705 | h->version = htobe32(CBFS_HEADER_VERSION); |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 706 | /* The 4 bytes are left out for two reasons: |
| 707 | * 1. the cbfs master header pointer resides there |
| 708 | * 2. some cbfs implementations assume that an image that resides |
| 709 | * below 4GB has a bootblock and get confused when the end of the |
| 710 | * image is at 4GB == 0. |
| 711 | */ |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 712 | h->bootblocksize = htobe32(4); |
| 713 | h->align = htobe32(CBFS_ALIGNMENT); |
Aaron Durbin | bb826ef | 2016-01-04 13:56:00 -0600 | [diff] [blame] | 714 | /* The offset and romsize fields within the master header are absolute |
| 715 | * values within the boot media. As such, romsize needs to relfect |
| 716 | * the end 'offset' for a CBFS. To achieve that the current buffer |
| 717 | * representing the CBFS region's size is added to the offset of |
| 718 | * the region within a larger image. |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 719 | */ |
Aaron Durbin | bb826ef | 2016-01-04 13:56:00 -0600 | [diff] [blame] | 720 | offset = buffer_get(param.image_region) - |
| 721 | buffer_get_original_backing(param.image_region); |
| 722 | size = buffer_size(param.image_region); |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 723 | h->romsize = htobe32(size + offset); |
| 724 | h->offset = htobe32(offset); |
| 725 | h->architecture = htobe32(CBFS_ARCHITECTURE_UNKNOWN); |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 726 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 727 | /* Never add a hash attribute to the master header. */ |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 728 | header = cbfs_create_file_header(CBFS_TYPE_CBFSHEADER, |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 729 | buffer_size(&buffer), name); |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 730 | if (cbfs_add_entry(&image, &buffer, 0, header, 0) != 0) { |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 731 | ERROR("Failed to add cbfs master header into ROM image.\n"); |
| 732 | goto done; |
| 733 | } |
| 734 | |
| 735 | struct cbfs_file *entry; |
| 736 | if ((entry = cbfs_get_entry(&image, name)) == NULL) { |
| 737 | ERROR("'%s' not in ROM image?!?\n", name); |
| 738 | goto done; |
| 739 | } |
| 740 | |
| 741 | uint32_t header_offset = CBFS_SUBHEADER(entry) - |
| 742 | buffer_get(&image.buffer); |
| 743 | header_offset = -(buffer_size(&image.buffer) - header_offset); |
| 744 | |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 745 | h_loc = (void *)(buffer_get(&image.buffer) + |
| 746 | buffer_size(&image.buffer) - 4); |
| 747 | fill_header_offset(h_loc, header_offset); |
| 748 | /* |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 749 | * If top swap present, update the header |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 750 | * location in secondary bootblock |
| 751 | */ |
| 752 | if (param.topswap_size) { |
| 753 | if (update_master_header_loc_topswap(&image, h_loc, |
| 754 | header_offset)) |
| 755 | return 1; |
| 756 | } |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 757 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 758 | ret = maybe_update_metadata_hash(&image); |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 759 | |
| 760 | done: |
| 761 | free(header); |
| 762 | buffer_delete(&buffer); |
| 763 | return ret; |
| 764 | } |
| 765 | |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 766 | static int add_topswap_bootblock(struct buffer *buffer, uint32_t *offset) |
| 767 | { |
| 768 | size_t bb_buf_size = buffer_size(buffer); |
| 769 | |
| 770 | if (bb_buf_size > param.topswap_size) { |
| 771 | ERROR("Bootblock bigger than the topswap boundary\n"); |
| 772 | ERROR("size = %zd, ts = %d\n", bb_buf_size, |
| 773 | param.topswap_size); |
| 774 | return 1; |
| 775 | } |
| 776 | |
| 777 | /* |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 778 | * Allocate topswap_size*2 bytes for bootblock to |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 779 | * accommodate the second bootblock. |
| 780 | */ |
| 781 | struct buffer new_bootblock, bb1, bb2; |
| 782 | if (buffer_create(&new_bootblock, 2 * param.topswap_size, |
| 783 | buffer->name)) |
| 784 | return 1; |
| 785 | |
| 786 | buffer_splice(&bb1, &new_bootblock, param.topswap_size - bb_buf_size, |
| 787 | bb_buf_size); |
| 788 | buffer_splice(&bb2, &new_bootblock, |
| 789 | buffer_size(&new_bootblock) - bb_buf_size, |
| 790 | bb_buf_size); |
| 791 | |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 792 | /* Copy to first bootblock */ |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 793 | memcpy(buffer_get(&bb1), buffer_get(buffer), bb_buf_size); |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 794 | /* Copy to second bootblock */ |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 795 | memcpy(buffer_get(&bb2), buffer_get(buffer), bb_buf_size); |
| 796 | |
| 797 | buffer_delete(buffer); |
| 798 | buffer_clone(buffer, &new_bootblock); |
| 799 | |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 800 | /* Update the location (offset) of bootblock in the region */ |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 801 | return convert_region_offset(buffer_size(buffer), offset); |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 802 | } |
| 803 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 804 | static int cbfs_add_component(const char *filename, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 805 | const char *name, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 806 | uint32_t headeroffset, |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 807 | convert_buffer_t convert) |
| 808 | { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 809 | /* |
| 810 | * The steps used to determine the final placement offset in CBFS, in order: |
| 811 | * |
Julius Werner | 20ad365 | 2021-12-09 13:20:59 -0800 | [diff] [blame] | 812 | * 1. If --base-address was passed, that value is used. If it was passed in the host |
| 813 | * address space, convert it to flash address space. (After that, |*offset| is always |
| 814 | * in the flash address space.) |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 815 | * |
| 816 | * 2. The convert() function may write a location back to |offset|, usually by calling |
| 817 | * do_cbfs_locate(). In this case, it needs to ensure that the location found can fit |
| 818 | * the CBFS file in its final form (after any compression and conversion). |
| 819 | * |
| 820 | * 3. If --align was passed and the offset is still undecided at this point, |
| 821 | * do_cbfs_locate() is called to find an appropriately aligned location. |
| 822 | * |
| 823 | * 4. If |offset| is still 0 at the end, cbfs_add_entry() will find the first available |
| 824 | * location that fits. |
| 825 | */ |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 826 | uint32_t offset = param.baseaddress_assigned ? param.baseaddress : 0; |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 827 | size_t len_align = 0; |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 828 | |
| 829 | if (param.alignment && param.baseaddress_assigned) { |
| 830 | ERROR("Cannot specify both alignment and base address\n"); |
| 831 | return 1; |
| 832 | } |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 833 | |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 834 | if (param.stage_xip && param.compression != CBFS_COMPRESS_NONE) { |
| 835 | ERROR("Cannot specify compression for XIP.\n"); |
| 836 | return 1; |
| 837 | } |
| 838 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 839 | if (!filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 840 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 841 | return 1; |
| 842 | } |
| 843 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 844 | if (!name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 845 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 846 | return 1; |
| 847 | } |
| 848 | |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 849 | if (param.type == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 850 | ERROR("You need to specify a valid -t/--type.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 851 | return 1; |
| 852 | } |
| 853 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 854 | struct cbfs_image image; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 855 | if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 856 | return 1; |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 857 | |
Patrick Georgi | 4e54bf9 | 2015-08-11 14:35:39 +0200 | [diff] [blame] | 858 | if (cbfs_get_entry(&image, name)) { |
| 859 | ERROR("'%s' already in ROM image.\n", name); |
| 860 | return 1; |
| 861 | } |
| 862 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 863 | struct buffer buffer; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 864 | if (buffer_from_file(&buffer, filename) != 0) { |
| 865 | ERROR("Could not load file '%s'.\n", filename); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 866 | return 1; |
| 867 | } |
| 868 | |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 869 | struct cbfs_file *header = |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 870 | cbfs_create_file_header(param.type, buffer.size, name); |
Patrick Georgi | 4110abc | 2015-08-11 15:10:33 +0200 | [diff] [blame] | 871 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 872 | /* Bootblock and CBFS header should never have file hashes. When adding |
| 873 | the bootblock it is important that we *don't* look up the metadata |
| 874 | hash yet (before it is added) or we'll cache an outdated result. */ |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 875 | if (param.type != CBFS_TYPE_BOOTBLOCK && param.type != CBFS_TYPE_CBFSHEADER) { |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 876 | enum vb2_hash_algorithm mh_algo = get_mh_cache()->cbfs_hash.algo; |
| 877 | if (mh_algo != VB2_HASH_INVALID && param.hash != mh_algo) { |
| 878 | if (param.hash == VB2_HASH_INVALID) { |
| 879 | param.hash = mh_algo; |
| 880 | } else { |
| 881 | ERROR("Cannot specify hash %s that's different from metadata hash algorithm %s\n", |
| 882 | vb2_get_hash_algorithm_name(param.hash), |
| 883 | vb2_get_hash_algorithm_name(mh_algo)); |
| 884 | goto error; |
| 885 | } |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 886 | } |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 887 | } |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 888 | |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 889 | /* |
| 890 | * Check if Intel CPU topswap is specified this will require a |
| 891 | * second bootblock to be added. |
| 892 | */ |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 893 | if (param.type == CBFS_TYPE_BOOTBLOCK && param.topswap_size) |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 894 | if (add_topswap_bootblock(&buffer, &offset)) |
| 895 | goto error; |
| 896 | |
Julius Werner | 20ad365 | 2021-12-09 13:20:59 -0800 | [diff] [blame] | 897 | /* With --base-address we allow host space addresses -- if so, convert it here. */ |
| 898 | if (IS_HOST_SPACE_ADDRESS(offset)) |
| 899 | offset = convert_addr_space(param.image_region, offset); |
| 900 | |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 901 | if (convert && convert(&buffer, &offset, header) != 0) { |
| 902 | ERROR("Failed to parse file '%s'.\n", filename); |
| 903 | goto error; |
| 904 | } |
| 905 | |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 906 | /* This needs to run after convert() to take compression into account. */ |
| 907 | if (!offset && param.alignment) |
Julius Werner | 772714d | 2021-12-13 10:16:44 -0800 | [diff] [blame] | 908 | if (do_cbfs_locate(&offset, buffer_size(&buffer))) |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 909 | goto error; |
| 910 | |
| 911 | /* This needs to run after convert() to hash the actual final file data. */ |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 912 | if (param.hash != VB2_HASH_INVALID && |
| 913 | cbfs_add_file_hash(header, &buffer, param.hash) == -1) { |
| 914 | ERROR("couldn't add hash for '%s'\n", name); |
| 915 | goto error; |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 918 | if (param.autogen_attr) { |
| 919 | /* Add position attribute if assigned */ |
| 920 | if (param.baseaddress_assigned || param.stage_xip) { |
| 921 | struct cbfs_file_attr_position *attrs = |
| 922 | (struct cbfs_file_attr_position *) |
| 923 | cbfs_add_file_attr(header, |
| 924 | CBFS_FILE_ATTR_TAG_POSITION, |
| 925 | sizeof(struct cbfs_file_attr_position)); |
| 926 | if (attrs == NULL) |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 927 | goto error; |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 928 | attrs->position = htobe32(offset); |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 929 | } |
| 930 | /* Add alignment attribute if used */ |
| 931 | if (param.alignment) { |
| 932 | struct cbfs_file_attr_align *attrs = |
| 933 | (struct cbfs_file_attr_align *) |
| 934 | cbfs_add_file_attr(header, |
| 935 | CBFS_FILE_ATTR_TAG_ALIGNMENT, |
| 936 | sizeof(struct cbfs_file_attr_align)); |
| 937 | if (attrs == NULL) |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 938 | goto error; |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 939 | attrs->alignment = htobe32(param.alignment); |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 943 | if (param.ibb) { |
| 944 | /* Mark as Initial Boot Block */ |
| 945 | struct cbfs_file_attribute *attrs = cbfs_add_file_attr(header, |
| 946 | CBFS_FILE_ATTR_TAG_IBB, |
| 947 | sizeof(struct cbfs_file_attribute)); |
| 948 | if (attrs == NULL) |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 949 | goto error; |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 950 | /* For Intel TXT minimum align is 16 */ |
| 951 | len_align = 16; |
| 952 | } |
| 953 | |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 954 | if (param.padding) { |
| 955 | const uint32_t hs = sizeof(struct cbfs_file_attribute); |
Julius Werner | 5779ca7 | 2020-11-20 16:12:40 -0800 | [diff] [blame] | 956 | uint32_t size = ALIGN_UP(MAX(hs, param.padding), |
| 957 | CBFS_ATTRIBUTE_ALIGN); |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 958 | INFO("Padding %d bytes\n", size); |
| 959 | struct cbfs_file_attribute *attr = |
| 960 | (struct cbfs_file_attribute *)cbfs_add_file_attr( |
| 961 | header, CBFS_FILE_ATTR_TAG_PADDING, |
| 962 | size); |
| 963 | if (attr == NULL) |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 964 | goto error; |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 967 | if (cbfs_add_entry(&image, &buffer, offset, header, len_align) != 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 968 | ERROR("Failed to add '%s' into ROM image.\n", filename); |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 969 | goto error; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 970 | } |
| 971 | |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 972 | free(header); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 973 | buffer_delete(&buffer); |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 974 | |
| 975 | return maybe_update_metadata_hash(&image) || maybe_update_fmap_hash(); |
| 976 | |
| 977 | error: |
| 978 | free(header); |
| 979 | buffer_delete(&buffer); |
| 980 | return 1; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 983 | static int cbfstool_convert_raw(struct buffer *buffer, |
| 984 | unused uint32_t *offset, struct cbfs_file *header) |
| 985 | { |
| 986 | char *compressed; |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 987 | int decompressed_size, compressed_size; |
| 988 | comp_func_ptr compress; |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 989 | |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 990 | decompressed_size = buffer->size; |
| 991 | if (param.precompression) { |
Nico Huber | 607796a | 2017-01-17 13:01:25 +0100 | [diff] [blame] | 992 | param.compression = read_le32(buffer->data); |
| 993 | decompressed_size = read_le32(buffer->data + sizeof(uint32_t)); |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 994 | compressed_size = buffer->size - 8; |
| 995 | compressed = malloc(compressed_size); |
| 996 | if (!compressed) |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 997 | return -1; |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 998 | memcpy(compressed, buffer->data + 8, compressed_size); |
| 999 | } else { |
Julius Werner | 105cdf5 | 2020-10-23 19:19:32 -0700 | [diff] [blame] | 1000 | if (param.compression == CBFS_COMPRESS_NONE) |
| 1001 | goto out; |
| 1002 | |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 1003 | compress = compression_function(param.compression); |
| 1004 | if (!compress) |
| 1005 | return -1; |
| 1006 | compressed = calloc(buffer->size, 1); |
| 1007 | if (!compressed) |
| 1008 | return -1; |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 1009 | |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 1010 | if (compress(buffer->data, buffer->size, |
| 1011 | compressed, &compressed_size)) { |
| 1012 | WARN("Compression failed - disabled\n"); |
| 1013 | free(compressed); |
Julius Werner | 105cdf5 | 2020-10-23 19:19:32 -0700 | [diff] [blame] | 1014 | goto out; |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 1015 | } |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 1016 | } |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 1017 | |
| 1018 | struct cbfs_file_attr_compression *attrs = |
| 1019 | (struct cbfs_file_attr_compression *) |
| 1020 | cbfs_add_file_attr(header, |
| 1021 | CBFS_FILE_ATTR_TAG_COMPRESSION, |
| 1022 | sizeof(struct cbfs_file_attr_compression)); |
| 1023 | if (attrs == NULL) { |
| 1024 | free(compressed); |
| 1025 | return -1; |
| 1026 | } |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 1027 | attrs->compression = htobe32(param.compression); |
| 1028 | attrs->decompressed_size = htobe32(decompressed_size); |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 1029 | |
| 1030 | free(buffer->data); |
| 1031 | buffer->data = compressed; |
| 1032 | buffer->size = compressed_size; |
| 1033 | |
Julius Werner | 105cdf5 | 2020-10-23 19:19:32 -0700 | [diff] [blame] | 1034 | out: |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 1035 | header->len = htobe32(buffer->size); |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1039 | static int cbfstool_convert_fsp(struct buffer *buffer, |
| 1040 | uint32_t *offset, struct cbfs_file *header) |
| 1041 | { |
| 1042 | uint32_t address; |
| 1043 | struct buffer fsp; |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1044 | |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1045 | /* |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1046 | * There are 4 different cases here: |
| 1047 | * |
| 1048 | * 1. --xip and --base-address: we need to place the binary at the given base address |
Julius Werner | 20ad365 | 2021-12-09 13:20:59 -0800 | [diff] [blame] | 1049 | * in the CBFS image and relocate it to that address. *offset was already filled in, |
| 1050 | * but we need to convert it to the host address space for relocation. |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1051 | * |
| 1052 | * 2. --xip but no --base-address: we implicitly force a 4K minimum alignment so that |
| 1053 | * relocation can occur. Call do_cbfs_locate() here to find an appropriate *offset. |
Julius Werner | 20ad365 | 2021-12-09 13:20:59 -0800 | [diff] [blame] | 1054 | * This also needs to be converted to the host address space for relocation. |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1055 | * |
| 1056 | * 3. No --xip but a --base-address: special case where --base-address does not have its |
| 1057 | * normal meaning, instead we use it as the relocation target address. We explicitly |
| 1058 | * reset *offset to 0 so that the file will be placed wherever it fits in CBFS. |
| 1059 | * |
| 1060 | * 4. No --xip and no --base-address: this means that the FSP was pre-linked and should |
| 1061 | * not be relocated. Just chain directly to convert_raw() for compression. |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1062 | */ |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1063 | |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1064 | if (param.stage_xip) { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1065 | if (!param.baseaddress_assigned) { |
| 1066 | param.alignment = 4*1024; |
Julius Werner | 772714d | 2021-12-13 10:16:44 -0800 | [diff] [blame] | 1067 | if (do_cbfs_locate(offset, buffer_size(buffer))) |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1068 | return -1; |
| 1069 | } |
Julius Werner | 20ad365 | 2021-12-09 13:20:59 -0800 | [diff] [blame] | 1070 | assert(!IS_HOST_SPACE_ADDRESS(*offset)); |
| 1071 | address = convert_addr_space(param.image_region, *offset); |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1072 | } else { |
Aaron Durbin | 493ec92 | 2016-05-16 15:18:45 -0500 | [diff] [blame] | 1073 | if (param.baseaddress_assigned == 0) { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1074 | INFO("Honoring pre-linked FSP module, no relocation.\n"); |
| 1075 | return cbfstool_convert_raw(buffer, offset, header); |
Aaron Durbin | 493ec92 | 2016-05-16 15:18:45 -0500 | [diff] [blame] | 1076 | } else { |
| 1077 | address = param.baseaddress; |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1078 | *offset = 0; |
Furquan Shaikh | c3bb692 | 2020-06-09 21:20:27 -0700 | [diff] [blame] | 1079 | } |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1080 | } |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1081 | |
| 1082 | /* Create a copy of the buffer to attempt relocation. */ |
| 1083 | if (buffer_create(&fsp, buffer_size(buffer), "fsp")) |
| 1084 | return -1; |
| 1085 | |
| 1086 | memcpy(buffer_get(&fsp), buffer_get(buffer), buffer_size(buffer)); |
| 1087 | |
| 1088 | /* Replace the buffer contents w/ the relocated ones on success. */ |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 1089 | if (fsp_component_relocate(address, buffer_get(&fsp), buffer_size(&fsp)) |
| 1090 | > 0) { |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1091 | buffer_delete(buffer); |
| 1092 | buffer_clone(buffer, &fsp); |
| 1093 | } else { |
| 1094 | buffer_delete(&fsp); |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame] | 1095 | WARN("Invalid FSP variant.\n"); |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | /* Let the raw path handle all the cbfs metadata logic. */ |
| 1099 | return cbfstool_convert_raw(buffer, offset, header); |
| 1100 | } |
| 1101 | |
Patrick Georgi | f820497 | 2015-08-11 15:16:24 +0200 | [diff] [blame] | 1102 | static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 1103 | struct cbfs_file *header) |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 1104 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1105 | struct buffer output; |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 1106 | size_t data_size; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 1107 | int ret; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 1108 | |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 1109 | if (elf_program_file_size(buffer, &data_size) < 0) { |
| 1110 | ERROR("Could not obtain ELF size\n"); |
| 1111 | return 1; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1115 | * We need a final location for XIP parsing, so we need to call do_cbfs_locate() early |
| 1116 | * here. That is okay because XIP stages may not be compressed, so their size cannot |
| 1117 | * change anymore at a later point. |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 1118 | */ |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1119 | if (param.stage_xip && |
| 1120 | do_cbfs_locate(offset, data_size)) { |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 1121 | ERROR("Could not find location for stage.\n"); |
| 1122 | return 1; |
| 1123 | } |
| 1124 | |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 1125 | struct cbfs_file_attr_stageheader *stageheader = (void *) |
| 1126 | cbfs_add_file_attr(header, CBFS_FILE_ATTR_TAG_STAGEHEADER, |
| 1127 | sizeof(struct cbfs_file_attr_stageheader)); |
| 1128 | if (!stageheader) |
| 1129 | return -1; |
| 1130 | |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 1131 | if (param.stage_xip) { |
Julius Werner | 20ad365 | 2021-12-09 13:20:59 -0800 | [diff] [blame] | 1132 | uint32_t host_space_address = convert_addr_space(param.image_region, *offset); |
| 1133 | assert(IS_HOST_SPACE_ADDRESS(host_space_address)); |
| 1134 | ret = parse_elf_to_xip_stage(buffer, &output, host_space_address, |
| 1135 | param.ignore_section, stageheader); |
Julius Werner | ff61a39 | 2021-01-12 15:21:03 -0800 | [diff] [blame] | 1136 | } else { |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 1137 | ret = parse_elf_to_stage(buffer, &output, param.ignore_section, |
| 1138 | stageheader); |
Julius Werner | ff61a39 | 2021-01-12 15:21:03 -0800 | [diff] [blame] | 1139 | } |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 1140 | if (ret != 0) |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1141 | return -1; |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 1142 | |
| 1143 | /* Store a hash of original uncompressed stage to compare later. */ |
| 1144 | size_t decmp_size = buffer_size(&output); |
| 1145 | uint32_t decmp_hash = XXH32(buffer_get(&output), decmp_size, 0); |
| 1146 | |
| 1147 | /* Chain to base conversion routine to handle compression. */ |
| 1148 | ret = cbfstool_convert_raw(&output, offset, header); |
| 1149 | if (ret != 0) |
| 1150 | goto fail; |
| 1151 | |
| 1152 | /* Special care must be taken for LZ4-compressed stages that the BSS is |
| 1153 | large enough to provide scratch space for in-place decompression. */ |
| 1154 | if (!param.precompression && param.compression == CBFS_COMPRESS_LZ4) { |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 1155 | size_t memlen = be32toh(stageheader->memlen); |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 1156 | size_t compressed_size = buffer_size(&output); |
| 1157 | uint8_t *compare_buffer = malloc(memlen); |
| 1158 | uint8_t *start = compare_buffer + memlen - compressed_size; |
| 1159 | if (!compare_buffer) { |
| 1160 | ERROR("Out of memory\n"); |
| 1161 | goto fail; |
| 1162 | } |
| 1163 | memcpy(start, buffer_get(&output), compressed_size); |
| 1164 | ret = ulz4fn(start, compressed_size, compare_buffer, memlen); |
| 1165 | if (ret == 0) { |
| 1166 | ERROR("Not enough scratch space to decompress LZ4 in-place -- increase BSS size or disable compression!\n"); |
| 1167 | free(compare_buffer); |
| 1168 | goto fail; |
| 1169 | } else if (ret != (int)decmp_size || |
| 1170 | decmp_hash != XXH32(compare_buffer, decmp_size, 0)) { |
| 1171 | ERROR("LZ4 compression BUG! Report to mailing list.\n"); |
| 1172 | free(compare_buffer); |
| 1173 | goto fail; |
| 1174 | } |
| 1175 | free(compare_buffer); |
| 1176 | } |
| 1177 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1178 | buffer_delete(buffer); |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 1179 | buffer_clone(buffer, &output); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1180 | return 0; |
Julius Werner | 81dc20e | 2020-10-15 17:37:57 -0700 | [diff] [blame] | 1181 | |
| 1182 | fail: |
| 1183 | buffer_delete(&output); |
| 1184 | return -1; |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1185 | } |
| 1186 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 1187 | static int cbfstool_convert_mkpayload(struct buffer *buffer, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 1188 | unused uint32_t *offset, struct cbfs_file *header) |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 1189 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1190 | struct buffer output; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 1191 | int ret; |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 1192 | /* Per default, try and see if payload is an ELF binary */ |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1193 | ret = parse_elf_to_payload(buffer, &output, param.compression); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 1194 | |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 1195 | /* If it's not an ELF, see if it's a FIT */ |
| 1196 | if (ret != 0) { |
| 1197 | ret = parse_fit_to_payload(buffer, &output, param.compression); |
| 1198 | if (ret == 0) |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 1199 | header->type = htobe32(CBFS_TYPE_FIT); |
Patrick Rudolph | 7ee05ed | 2018-04-26 09:35:13 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /* If it's not an FIT, see if it's a UEFI FV */ |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 1203 | if (ret != 0) |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1204 | ret = parse_fv_to_payload(buffer, &output, param.compression); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 1205 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 1206 | /* If it's neither ELF nor UEFI Fv, try bzImage */ |
| 1207 | if (ret != 0) |
| 1208 | ret = parse_bzImage_to_payload(buffer, &output, |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1209 | param.initrd, param.cmdline, param.compression); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 1210 | |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 1211 | /* Not a supported payload type */ |
| 1212 | if (ret != 0) { |
| 1213 | ERROR("Not a supported payload type (ELF / FV).\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1214 | buffer_delete(buffer); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1215 | return -1; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1218 | buffer_delete(buffer); |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 1219 | // Direct assign, no dupe. |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1220 | memcpy(buffer, &output, sizeof(*buffer)); |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 1221 | header->len = htobe32(output.size); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | static int cbfstool_convert_mkflatpayload(struct buffer *buffer, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 1226 | unused uint32_t *offset, struct cbfs_file *header) |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 1227 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1228 | struct buffer output; |
| 1229 | if (parse_flat_binary_to_payload(buffer, &output, |
| 1230 | param.loadaddress, |
| 1231 | param.entrypoint, |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1232 | param.compression) != 0) { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1233 | return -1; |
| 1234 | } |
| 1235 | buffer_delete(buffer); |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 1236 | // Direct assign, no dupe. |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1237 | memcpy(buffer, &output, sizeof(*buffer)); |
Alex James | 02001a38 | 2021-12-19 16:41:59 -0600 | [diff] [blame] | 1238 | header->len = htobe32(output.size); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 1242 | static int cbfs_add(void) |
| 1243 | { |
Julius Werner | c24db00 | 2020-11-18 18:00:31 -0800 | [diff] [blame] | 1244 | convert_buffer_t convert = cbfstool_convert_raw; |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1245 | |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 1246 | if (param.type == CBFS_TYPE_FSP) { |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1247 | convert = cbfstool_convert_fsp; |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1248 | } else if (param.type == CBFS_TYPE_STAGE) { |
| 1249 | ERROR("stages can only be added with cbfstool add-stage\n"); |
| 1250 | return 1; |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1251 | } else if (param.stage_xip) { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1252 | ERROR("cbfstool add supports xip only for FSP component type\n"); |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1253 | return 1; |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1254 | } |
| 1255 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1256 | return cbfs_add_component(param.filename, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 1257 | param.name, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1258 | param.headeroffset, |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 1259 | convert); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1262 | static int cbfs_add_stage(void) |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 1263 | { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1264 | if (param.stage_xip && param.baseaddress_assigned) { |
| 1265 | ERROR("Cannot specify base address for XIP.\n"); |
| 1266 | return 1; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 1267 | } |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1268 | param.type = CBFS_TYPE_STAGE; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 1269 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1270 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1271 | param.name, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1272 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1273 | cbfstool_convert_mkstage); |
| 1274 | } |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 1275 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1276 | static int cbfs_add_payload(void) |
| 1277 | { |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1278 | param.type = CBFS_TYPE_SELF; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1279 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1280 | param.name, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1281 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1282 | cbfstool_convert_mkpayload); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static int cbfs_add_flat_binary(void) |
| 1286 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1287 | if (param.loadaddress == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1288 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1289 | "-l/--load-address.\n"); |
| 1290 | return 1; |
| 1291 | } |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1292 | if (param.entrypoint == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1293 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1294 | "-e/--entry-point.\n"); |
| 1295 | return 1; |
| 1296 | } |
Julius Werner | 6ddacd6 | 2021-12-03 14:46:53 -0800 | [diff] [blame] | 1297 | param.type = CBFS_TYPE_SELF; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1298 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1299 | param.name, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1300 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 1301 | cbfstool_convert_mkflatpayload); |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1304 | static int cbfs_add_integer(void) |
| 1305 | { |
Patrick Georgi | 16b3e4b | 2016-12-12 18:38:01 +0100 | [diff] [blame] | 1306 | if (!param.u64val_assigned) { |
| 1307 | ERROR("You need to specify a value to write.\n"); |
| 1308 | return 1; |
| 1309 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1310 | return cbfs_add_integer_component(param.name, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1311 | param.u64val, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 1312 | param.baseaddress, |
| 1313 | param.headeroffset); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1314 | } |
| 1315 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1316 | static int cbfs_remove(void) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 1317 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1318 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1319 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1320 | return 1; |
| 1321 | } |
| 1322 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1323 | struct cbfs_image image; |
| 1324 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1325 | param.headeroffset)) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 1326 | return 1; |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 1327 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1328 | if (cbfs_remove_entry(&image, param.name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1329 | ERROR("Removing file '%s' failed.\n", |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 1330 | param.name); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 1331 | return 1; |
| 1332 | } |
| 1333 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 1334 | return maybe_update_metadata_hash(&image); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 1335 | } |
| 1336 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1337 | static int cbfs_create(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1338 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1339 | struct cbfs_image image; |
| 1340 | memset(&image, 0, sizeof(image)); |
| 1341 | buffer_clone(&image.buffer, param.image_region); |
| 1342 | |
| 1343 | if (param.fmap) { |
| 1344 | if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size || |
| 1345 | param.baseaddress_assigned || |
| 1346 | param.headeroffset_assigned || |
| 1347 | param.cbfsoffset_assigned || |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1348 | param.bootblock) { |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 1349 | ERROR("Since -M was provided, -m, -s, -b, -o, -H, and -B should be omitted\n"); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1350 | return 1; |
| 1351 | } |
| 1352 | |
| 1353 | return cbfs_image_create(&image, image.buffer.size); |
| 1354 | } |
| 1355 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 1356 | if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) { |
Stefan Reinauer | 60a4a73 | 2013-03-28 16:46:07 -0700 | [diff] [blame] | 1357 | ERROR("You need to specify -m/--machine arch.\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 1358 | return 1; |
| 1359 | } |
| 1360 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1361 | struct buffer bootblock; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1362 | if (!param.bootblock) { |
| 1363 | DEBUG("-B not given, creating image without bootblock.\n"); |
Patrick Georgi | 6b2d2db | 2016-12-14 16:11:58 +0100 | [diff] [blame] | 1364 | if (buffer_create(&bootblock, 0, "(dummy)") != 0) |
| 1365 | return 1; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1366 | } else if (buffer_from_file(&bootblock, param.bootblock)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1367 | return 1; |
| 1368 | } |
| 1369 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1370 | if (!param.alignment) |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 1371 | param.alignment = CBFS_ALIGNMENT; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1372 | |
| 1373 | // Set default offsets. x86, as usual, needs to be a special snowflake. |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1374 | if (!param.baseaddress_assigned) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1375 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 1376 | // Make sure there's at least enough room for rel_offset |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1377 | param.baseaddress = param.size - |
| 1378 | MAX(bootblock.size, sizeof(int32_t)); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1379 | DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n", |
| 1380 | param.baseaddress); |
| 1381 | } else { |
| 1382 | param.baseaddress = 0; |
| 1383 | DEBUG("bootblock starts at address 0x0.\n"); |
| 1384 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1385 | } |
| 1386 | if (!param.headeroffset_assigned) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1387 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 1388 | param.headeroffset = param.baseaddress - |
| 1389 | sizeof(struct cbfs_header); |
| 1390 | DEBUG("x86 -> CBFS header before bootblock (%#x).\n", |
| 1391 | param.headeroffset); |
| 1392 | } else { |
| 1393 | param.headeroffset = align_up(param.baseaddress + |
| 1394 | bootblock.size, sizeof(uint32_t)); |
| 1395 | DEBUG("CBFS header placed behind bootblock (%#x).\n", |
| 1396 | param.headeroffset); |
| 1397 | } |
| 1398 | } |
| 1399 | if (!param.cbfsoffset_assigned) { |
| 1400 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 1401 | param.cbfsoffset = 0; |
| 1402 | DEBUG("x86 -> CBFS entries start at address 0x0.\n"); |
| 1403 | } else { |
| 1404 | param.cbfsoffset = align_up(param.headeroffset + |
| 1405 | sizeof(struct cbfs_header), |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 1406 | CBFS_ALIGNMENT); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1407 | DEBUG("CBFS entries start beind master header (%#x).\n", |
| 1408 | param.cbfsoffset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1409 | } |
| 1410 | } |
| 1411 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1412 | int ret = cbfs_legacy_image_create(&image, |
| 1413 | param.arch, |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 1414 | CBFS_ALIGNMENT, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1415 | &bootblock, |
| 1416 | param.baseaddress, |
| 1417 | param.headeroffset, |
| 1418 | param.cbfsoffset); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1419 | buffer_delete(&bootblock); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1420 | return ret; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1423 | static int cbfs_layout(void) |
| 1424 | { |
| 1425 | const struct fmap *fmap = partitioned_file_get_fmap(param.image_file); |
| 1426 | if (!fmap) { |
| 1427 | LOG("This is a legacy image composed entirely of a single CBFS.\n"); |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
| 1431 | printf("This image contains the following sections that can be %s with this tool:\n", |
| 1432 | param.show_immutable ? "accessed" : "manipulated"); |
| 1433 | puts(""); |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 1434 | for (unsigned i = 0; i < fmap->nareas; ++i) { |
| 1435 | const struct fmap_area *current = fmap->areas + i; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1436 | |
| 1437 | bool readonly = partitioned_file_fmap_count(param.image_file, |
| 1438 | partitioned_file_fmap_select_children_of, current) || |
| 1439 | region_is_flashmap((const char *)current->name); |
| 1440 | if (!param.show_immutable && readonly) |
| 1441 | continue; |
| 1442 | |
| 1443 | printf("'%s'", current->name); |
| 1444 | |
| 1445 | // Detect consecutive sections that describe the same region and |
| 1446 | // show them as aliases. This cannot find equivalent entries |
| 1447 | // that aren't adjacent; however, fmaptool doesn't generate |
| 1448 | // FMAPs with such sections, so this convenience feature works |
| 1449 | // for all but the strangest manually created FMAP binaries. |
| 1450 | // TODO: This could be done by parsing the FMAP into some kind |
| 1451 | // of tree that had duplicate lists in addition to child lists, |
| 1452 | // which would allow covering that weird, unlikely case as well. |
| 1453 | unsigned lookahead; |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 1454 | for (lookahead = 1; i + lookahead < fmap->nareas; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1455 | ++lookahead) { |
| 1456 | const struct fmap_area *consecutive = |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 1457 | fmap->areas + i + lookahead; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1458 | if (consecutive->offset != current->offset || |
| 1459 | consecutive->size != current->size) |
| 1460 | break; |
| 1461 | printf(", '%s'", consecutive->name); |
| 1462 | } |
| 1463 | if (lookahead > 1) |
| 1464 | fputs(" are aliases for the same region", stdout); |
| 1465 | |
| 1466 | const char *qualifier = ""; |
| 1467 | if (readonly) |
| 1468 | qualifier = "read-only, "; |
| 1469 | else if (region_is_modern_cbfs((const char *)current->name)) |
| 1470 | qualifier = "CBFS, "; |
Patrick Georgi | 848e30d | 2019-06-18 22:02:13 +0200 | [diff] [blame] | 1471 | else if (current->flags & FMAP_AREA_PRESERVE) |
| 1472 | qualifier = "preserve, "; |
Werner Zeh | 7850b58 | 2018-04-26 09:26:38 +0200 | [diff] [blame] | 1473 | printf(" (%ssize %u, offset %u)\n", qualifier, current->size, |
| 1474 | current->offset); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1475 | |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 1476 | i += lookahead - 1; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1477 | } |
| 1478 | puts(""); |
| 1479 | |
| 1480 | if (param.show_immutable) { |
| 1481 | puts("It is at least possible to perform the read action on every section listed above."); |
| 1482 | } else { |
| 1483 | puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above."); |
| 1484 | puts("To see the image's read-only sections as well, rerun with the -w option."); |
| 1485 | } |
| 1486 | |
| 1487 | return 0; |
| 1488 | } |
| 1489 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1490 | static int cbfs_print(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1491 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 1492 | struct cbfs_image image; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1493 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1494 | param.headeroffset)) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1495 | return 1; |
Julius Werner | c4ee28c | 2020-04-27 19:31:03 -0700 | [diff] [blame] | 1496 | if (param.machine_parseable) { |
| 1497 | if (verbose) |
| 1498 | printf("[FMAP REGION]\t%s\n", param.region_name); |
| 1499 | cbfs_print_parseable_directory(&image); |
| 1500 | } else { |
Patrick Rudolph | fb87e41 | 2018-06-05 15:11:01 +0200 | [diff] [blame] | 1501 | printf("FMAP REGION: %s\n", param.region_name); |
Julius Werner | c4ee28c | 2020-04-27 19:31:03 -0700 | [diff] [blame] | 1502 | cbfs_print_directory(&image); |
Patrick Rudolph | fb87e41 | 2018-06-05 15:11:01 +0200 | [diff] [blame] | 1503 | } |
Julius Werner | c4ee28c | 2020-04-27 19:31:03 -0700 | [diff] [blame] | 1504 | |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 1505 | if (verbose) { |
| 1506 | struct mh_cache *mhc = get_mh_cache(); |
| 1507 | if (mhc->cbfs_hash.algo == VB2_HASH_INVALID) |
| 1508 | return 0; |
| 1509 | |
| 1510 | struct vb2_hash real_hash = { .algo = mhc->cbfs_hash.algo }; |
| 1511 | cb_err_t err = cbfs_walk(&image, NULL, NULL, &real_hash, |
| 1512 | CBFS_WALK_WRITEBACK_HASH); |
| 1513 | if (err != CB_CBFS_NOT_FOUND) { |
| 1514 | ERROR("Unexpected cbfs_walk() error %d\n", err); |
| 1515 | return 1; |
| 1516 | } |
| 1517 | char *hash_str = bintohex(real_hash.raw, |
| 1518 | vb2_digest_size(real_hash.algo)); |
| 1519 | printf("[METADATA HASH]\t%s:%s", |
| 1520 | vb2_get_hash_algorithm_name(real_hash.algo), hash_str); |
| 1521 | if (!strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)) { |
| 1522 | if (!memcmp(mhc->cbfs_hash.raw, real_hash.raw, |
| 1523 | vb2_digest_size(real_hash.algo))) |
| 1524 | printf(":valid"); |
| 1525 | else |
| 1526 | printf(":invalid"); |
| 1527 | } |
| 1528 | printf("\n"); |
| 1529 | free(hash_str); |
| 1530 | } |
| 1531 | |
Julius Werner | c4ee28c | 2020-04-27 19:31:03 -0700 | [diff] [blame] | 1532 | return 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1535 | static int cbfs_extract(void) |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 1536 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1537 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1538 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1539 | return 1; |
| 1540 | } |
| 1541 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1542 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1543 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1544 | return 1; |
| 1545 | } |
| 1546 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1547 | struct cbfs_image image; |
| 1548 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1549 | param.headeroffset)) |
| 1550 | return 1; |
| 1551 | |
Aaron Durbin | 1762502 | 2015-10-27 13:17:52 -0500 | [diff] [blame] | 1552 | return cbfs_export_entry(&image, param.name, param.filename, |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1553 | param.arch, !param.unprocessed); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | static int cbfs_write(void) |
| 1557 | { |
| 1558 | if (!param.filename) { |
| 1559 | ERROR("You need to specify a valid input -f/--file.\n"); |
| 1560 | return 1; |
| 1561 | } |
| 1562 | if (!partitioned_file_is_partitioned(param.image_file)) { |
| 1563 | ERROR("This operation isn't valid on legacy images having CBFS master headers\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1564 | return 1; |
| 1565 | } |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 1566 | |
Patrick Georgi | 01fbc3a | 2016-10-12 16:46:13 +0200 | [diff] [blame] | 1567 | if (!param.force && region_is_modern_cbfs(param.region_name)) { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1568 | ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n", |
| 1569 | param.region_name); |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
| 1573 | struct buffer new_content; |
| 1574 | if (buffer_from_file(&new_content, param.filename)) |
| 1575 | return 1; |
| 1576 | |
| 1577 | if (buffer_check_magic(&new_content, FMAP_SIGNATURE, |
| 1578 | strlen(FMAP_SIGNATURE))) { |
| 1579 | ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n", |
| 1580 | param.filename); |
| 1581 | buffer_delete(&new_content); |
| 1582 | return 1; |
| 1583 | } |
Patrick Georgi | 01fbc3a | 2016-10-12 16:46:13 +0200 | [diff] [blame] | 1584 | if (!param.force && buffer_check_magic(&new_content, CBFS_FILE_MAGIC, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1585 | strlen(CBFS_FILE_MAGIC))) { |
| 1586 | ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n", |
| 1587 | param.filename); |
| 1588 | buffer_delete(&new_content); |
| 1589 | return 1; |
| 1590 | } |
| 1591 | |
| 1592 | unsigned offset = 0; |
| 1593 | if (param.fill_partial_upward && param.fill_partial_downward) { |
| 1594 | ERROR("You may only specify one of -u and -d.\n"); |
| 1595 | buffer_delete(&new_content); |
| 1596 | return 1; |
| 1597 | } else if (!param.fill_partial_upward && !param.fill_partial_downward) { |
| 1598 | if (new_content.size != param.image_region->size) { |
| 1599 | ERROR("File to add is %zu bytes and would not fill %zu-byte target region (did you mean to pass either -u or -d?)\n", |
| 1600 | new_content.size, param.image_region->size); |
| 1601 | buffer_delete(&new_content); |
| 1602 | return 1; |
| 1603 | } |
| 1604 | } else { |
| 1605 | if (new_content.size > param.image_region->size) { |
| 1606 | ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n", |
| 1607 | new_content.size, param.image_region->size); |
| 1608 | buffer_delete(&new_content); |
| 1609 | return 1; |
| 1610 | } |
Patrick Georgi | d9edb18 | 2016-12-06 18:55:26 +0100 | [diff] [blame] | 1611 | if (param.u64val == (uint64_t)-1) { |
| 1612 | WARN("Written area will abut %s of target region: any unused space will keep its current contents\n", |
| 1613 | param.fill_partial_upward ? "bottom" : "top"); |
| 1614 | } else if (param.u64val > 0xff) { |
| 1615 | ERROR("given fill value (%x) is larger than a byte\n", (unsigned)(param.u64val & 0xff)); |
| 1616 | buffer_delete(&new_content); |
| 1617 | return 1; |
| 1618 | } else { |
| 1619 | memset(buffer_get(param.image_region), |
| 1620 | param.u64val & 0xff, |
| 1621 | buffer_size(param.image_region)); |
| 1622 | } |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1623 | if (param.fill_partial_downward) |
| 1624 | offset = param.image_region->size - new_content.size; |
| 1625 | } |
| 1626 | |
| 1627 | memcpy(param.image_region->data + offset, new_content.data, |
| 1628 | new_content.size); |
| 1629 | buffer_delete(&new_content); |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 1630 | |
| 1631 | return maybe_update_fmap_hash(); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | static int cbfs_read(void) |
| 1635 | { |
| 1636 | if (!param.filename) { |
| 1637 | ERROR("You need to specify a valid output -f/--file.\n"); |
| 1638 | return 1; |
| 1639 | } |
| 1640 | if (!partitioned_file_is_partitioned(param.image_file)) { |
| 1641 | ERROR("This operation isn't valid on legacy images having CBFS master headers\n"); |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
| 1645 | return buffer_write_file(param.image_region, param.filename); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1648 | static int cbfs_copy(void) |
| 1649 | { |
Patrick Georgi | 214e4af | 2015-11-20 19:22:50 +0100 | [diff] [blame] | 1650 | struct cbfs_image src_image; |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 1651 | struct buffer src_buf; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1652 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 1653 | if (!param.source_region) { |
| 1654 | ERROR("You need to specify -R/--source-region.\n"); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1655 | return 1; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1656 | } |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1657 | |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 1658 | /* Obtain the source region and convert it to a cbfs_image. */ |
| 1659 | if (!partitioned_file_read_region(&src_buf, param.image_file, |
| 1660 | param.source_region)) { |
| 1661 | ERROR("Region not found in image: %s\n", param.source_region); |
| 1662 | return 1; |
| 1663 | } |
| 1664 | |
| 1665 | if (cbfs_image_from_buffer(&src_image, &src_buf, param.headeroffset)) |
| 1666 | return 1; |
| 1667 | |
| 1668 | return cbfs_copy_instance(&src_image, param.image_region); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1669 | } |
| 1670 | |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 1671 | static int cbfs_compact(void) |
| 1672 | { |
| 1673 | struct cbfs_image image; |
| 1674 | if (cbfs_image_from_buffer(&image, param.image_region, |
| 1675 | param.headeroffset)) |
| 1676 | return 1; |
| 1677 | WARN("Compacting a CBFS doesn't honor alignment or fixed addresses!\n"); |
| 1678 | return cbfs_compact_instance(&image); |
| 1679 | } |
| 1680 | |
Patrick Georgi | 5d982d7 | 2017-09-19 14:39:58 +0200 | [diff] [blame] | 1681 | static int cbfs_expand(void) |
| 1682 | { |
| 1683 | struct buffer src_buf; |
| 1684 | |
| 1685 | /* Obtain the source region. */ |
| 1686 | if (!partitioned_file_read_region(&src_buf, param.image_file, |
| 1687 | param.region_name)) { |
| 1688 | ERROR("Region not found in image: %s\n", param.source_region); |
| 1689 | return 1; |
| 1690 | } |
| 1691 | |
| 1692 | return cbfs_expand_to_region(param.image_region); |
| 1693 | } |
| 1694 | |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 1695 | static int cbfs_truncate(void) |
| 1696 | { |
| 1697 | struct buffer src_buf; |
| 1698 | |
| 1699 | /* Obtain the source region. */ |
| 1700 | if (!partitioned_file_read_region(&src_buf, param.image_file, |
| 1701 | param.region_name)) { |
| 1702 | ERROR("Region not found in image: %s\n", param.source_region); |
| 1703 | return 1; |
| 1704 | } |
| 1705 | |
| 1706 | uint32_t size; |
| 1707 | int result = cbfs_truncate_space(param.image_region, &size); |
| 1708 | printf("0x%x\n", size); |
| 1709 | return result; |
| 1710 | } |
| 1711 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 1712 | static const struct command commands[] = { |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 1713 | {"add", "H:r:f:n:t:c:b:a:p:yvA:j:gh?", cbfs_add, true, true}, |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 1714 | {"add-flat-binary", "H:r:f:n:l:e:c:b:p:vA:gh?", cbfs_add_flat_binary, |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 1715 | true, true}, |
Raul E Rangel | db28040 | 2021-06-29 13:26:41 -0600 | [diff] [blame] | 1716 | {"add-payload", "H:r:f:n:c:b:a:C:I:p:vA:gh?", cbfs_add_payload, |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 1717 | true, true}, |
Kyösti Mälkki | c36469e | 2020-05-28 23:12:02 +0300 | [diff] [blame] | 1718 | {"add-stage", "a:H:r:f:n:t:c:b:P:QS:p:yvA:gh?", cbfs_add_stage, |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 1719 | true, true}, |
| 1720 | {"add-int", "H:r:i:n:b:vgh?", cbfs_add_integer, true, true}, |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 1721 | {"add-master-header", "H:r:vh?j:", cbfs_add_master_header, true, true}, |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 1722 | {"compact", "r:h?", cbfs_compact, true, true}, |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 1723 | {"copy", "r:R:h?", cbfs_copy, true, true}, |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 1724 | {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true}, |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1725 | {"extract", "H:r:m:n:f:Uvh?", cbfs_extract, true, false}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1726 | {"layout", "wvh?", cbfs_layout, false, false}, |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1727 | {"print", "H:r:vkh?", cbfs_print, true, false}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1728 | {"read", "r:f:vh?", cbfs_read, true, false}, |
| 1729 | {"remove", "H:r:n:vh?", cbfs_remove, true, true}, |
Patrick Georgi | d9edb18 | 2016-12-06 18:55:26 +0100 | [diff] [blame] | 1730 | {"write", "r:f:i:Fudvh?", cbfs_write, true, true}, |
Patrick Georgi | 5d982d7 | 2017-09-19 14:39:58 +0200 | [diff] [blame] | 1731 | {"expand", "r:h?", cbfs_expand, true, true}, |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 1732 | {"truncate", "r:h?", cbfs_truncate, true, true}, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1733 | }; |
| 1734 | |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 1735 | enum { |
| 1736 | /* begin after ASCII characters */ |
Furquan Shaikh | c42cf91 | 2020-04-14 00:14:44 -0700 | [diff] [blame] | 1737 | LONGOPT_START = 256, |
| 1738 | LONGOPT_IBB = LONGOPT_START, |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 1739 | LONGOPT_EXT_WIN_BASE, |
| 1740 | LONGOPT_EXT_WIN_SIZE, |
Furquan Shaikh | c42cf91 | 2020-04-14 00:14:44 -0700 | [diff] [blame] | 1741 | LONGOPT_END, |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 1742 | }; |
| 1743 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1744 | static struct option long_options[] = { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1745 | {"alignment", required_argument, 0, 'a' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1746 | {"base-address", required_argument, 0, 'b' }, |
| 1747 | {"bootblock", required_argument, 0, 'B' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1748 | {"cmdline", required_argument, 0, 'C' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1749 | {"compression", required_argument, 0, 'c' }, |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 1750 | {"topswap-size", required_argument, 0, 'j' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1751 | {"empty-fits", required_argument, 0, 'x' }, |
| 1752 | {"entry-point", required_argument, 0, 'e' }, |
| 1753 | {"file", required_argument, 0, 'f' }, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1754 | {"fill-downward", no_argument, 0, 'd' }, |
| 1755 | {"fill-upward", no_argument, 0, 'u' }, |
| 1756 | {"flashmap", required_argument, 0, 'M' }, |
| 1757 | {"fmap-regions", required_argument, 0, 'r' }, |
Patrick Georgi | 01fbc3a | 2016-10-12 16:46:13 +0200 | [diff] [blame] | 1758 | {"force", no_argument, 0, 'F' }, |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 1759 | {"source-region", required_argument, 0, 'R' }, |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1760 | {"hash-algorithm",required_argument, 0, 'A' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1761 | {"header-offset", required_argument, 0, 'H' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1762 | {"help", no_argument, 0, 'h' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1763 | {"ignore-sec", required_argument, 0, 'S' }, |
| 1764 | {"initrd", required_argument, 0, 'I' }, |
| 1765 | {"int", required_argument, 0, 'i' }, |
| 1766 | {"load-address", required_argument, 0, 'l' }, |
| 1767 | {"machine", required_argument, 0, 'm' }, |
| 1768 | {"name", required_argument, 0, 'n' }, |
| 1769 | {"offset", required_argument, 0, 'o' }, |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 1770 | {"padding", required_argument, 0, 'p' }, |
Kyösti Mälkki | c36469e | 2020-05-28 23:12:02 +0300 | [diff] [blame] | 1771 | {"pow2page", no_argument, 0, 'Q' }, |
Rizwan Qureshi | c1072f2 | 2018-06-04 23:02:46 +0530 | [diff] [blame] | 1772 | {"ucode-region", required_argument, 0, 'q' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1773 | {"size", required_argument, 0, 's' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 1774 | {"type", required_argument, 0, 't' }, |
| 1775 | {"verbose", no_argument, 0, 'v' }, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1776 | {"with-readonly", no_argument, 0, 'w' }, |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 1777 | {"xip", no_argument, 0, 'y' }, |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 1778 | {"gen-attribute", no_argument, 0, 'g' }, |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 1779 | {"mach-parseable",no_argument, 0, 'k' }, |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1780 | {"unprocessed", no_argument, 0, 'U' }, |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 1781 | {"ibb", no_argument, 0, LONGOPT_IBB }, |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 1782 | {"ext-win-base", required_argument, 0, LONGOPT_EXT_WIN_BASE }, |
| 1783 | {"ext-win-size", required_argument, 0, LONGOPT_EXT_WIN_SIZE }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1784 | {NULL, 0, 0, 0 } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1785 | }; |
| 1786 | |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 1787 | static int get_region_offset(long long int offset, uint32_t *region_offset) |
| 1788 | { |
| 1789 | /* If offset is not negative, no transformation required. */ |
| 1790 | if (offset >= 0) { |
| 1791 | *region_offset = offset; |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | /* Calculate offset from start of region. */ |
| 1796 | return convert_region_offset(-offset, region_offset); |
| 1797 | } |
| 1798 | |
| 1799 | static int calculate_region_offsets(void) |
| 1800 | { |
| 1801 | int ret = 0; |
| 1802 | |
| 1803 | if (param.baseaddress_assigned) |
| 1804 | ret |= get_region_offset(param.baseaddress_input, ¶m.baseaddress); |
| 1805 | if (param.headeroffset_assigned) |
| 1806 | ret |= get_region_offset(param.headeroffset_input, ¶m.headeroffset); |
| 1807 | if (param.cbfsoffset_assigned) |
| 1808 | ret |= get_region_offset(param.cbfsoffset_input, ¶m.cbfsoffset); |
| 1809 | |
| 1810 | return ret; |
| 1811 | } |
| 1812 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1813 | static int dispatch_command(struct command command) |
| 1814 | { |
| 1815 | if (command.accesses_region) { |
| 1816 | assert(param.image_file); |
| 1817 | |
| 1818 | if (partitioned_file_is_partitioned(param.image_file)) { |
Patrick Georgi | 2ed7295 | 2016-12-16 14:51:53 +0100 | [diff] [blame] | 1819 | INFO("Performing operation on '%s' region...\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1820 | param.region_name); |
| 1821 | } |
| 1822 | if (!partitioned_file_read_region(param.image_region, |
| 1823 | param.image_file, param.region_name)) { |
| 1824 | ERROR("The image will be left unmodified.\n"); |
| 1825 | return 1; |
| 1826 | } |
| 1827 | |
| 1828 | if (command.modifies_region) { |
| 1829 | // We (intentionally) don't support overwriting the FMAP |
| 1830 | // section. If you find yourself wanting to do this, |
| 1831 | // consider creating a new image rather than performing |
| 1832 | // whatever hacky transformation you were planning. |
| 1833 | if (region_is_flashmap(param.region_name)) { |
| 1834 | ERROR("Image region '%s' is read-only because it contains the FMAP.\n", |
| 1835 | param.region_name); |
| 1836 | ERROR("The image will be left unmodified.\n"); |
| 1837 | return 1; |
| 1838 | } |
| 1839 | // We don't allow writing raw data to regions that |
| 1840 | // contain nested regions, since doing so would |
| 1841 | // overwrite all such subregions. |
| 1842 | if (partitioned_file_region_contains_nested( |
| 1843 | param.image_file, param.region_name)) { |
| 1844 | ERROR("Image region '%s' is read-only because it contains nested regions.\n", |
| 1845 | param.region_name); |
| 1846 | ERROR("The image will be left unmodified.\n"); |
| 1847 | return 1; |
| 1848 | } |
| 1849 | } |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 1850 | |
| 1851 | /* |
| 1852 | * Once image region is read, input offsets can be adjusted accordingly if the |
| 1853 | * inputs are provided as negative integers i.e. offsets from end of region. |
| 1854 | */ |
| 1855 | if (calculate_region_offsets()) |
| 1856 | return 1; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | if (command.function()) { |
| 1860 | if (partitioned_file_is_partitioned(param.image_file)) { |
| 1861 | ERROR("Failed while operating on '%s' region!\n", |
| 1862 | param.region_name); |
| 1863 | ERROR("The image will be left unmodified.\n"); |
| 1864 | } |
| 1865 | return 1; |
| 1866 | } |
| 1867 | |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1871 | static void usage(char *name) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1872 | { |
| 1873 | printf |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 1874 | ("cbfstool: Management utility for CBFS formatted ROM images\n\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1875 | "USAGE:\n" " %s [-h]\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1876 | " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1877 | " -H header_offset Do not search for header; use this offset*\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 1878 | " -T Output top-aligned memory address\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1879 | " -u Accept short data; fill upward/from bottom\n" |
| 1880 | " -d Accept short data; fill downward/from top\n" |
Patrick Georgi | 01fbc3a | 2016-10-12 16:46:13 +0200 | [diff] [blame] | 1881 | " -F Force action\n" |
Patrick Georgi | 5b6bdcc | 2016-10-13 18:24:10 +0200 | [diff] [blame] | 1882 | " -g Generate position and alignment arguments\n" |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1883 | " -U Unprocessed; don't decompress or make ELF\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1884 | " -v Provide verbose output\n" |
| 1885 | " -h Display this help message\n\n" |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 1886 | " --ext-win-base Base of extended decode window in host address\n" |
| 1887 | " space(x86 only)\n" |
| 1888 | " --ext-win-size Size of extended decode window in host address\n" |
| 1889 | " space(x86 only)\n" |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1890 | "COMMANDs:\n" |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1891 | " add [-r image,regions] -f FILE -n NAME -t TYPE [-A hash] \\\n" |
Furquan Shaikh | 61c1a05 | 2016-05-09 11:53:34 -0700 | [diff] [blame] | 1892 | " [-c compression] [-b base-address | -a alignment] \\\n" |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 1893 | " [-p padding size] [-y|--xip if TYPE is FSP] \\\n" |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 1894 | " [-j topswap-size] (Intel CPUs only) [--ibb] \\\n" |
| 1895 | " [--ext-win-base win-base --ext-win-size win-size] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1896 | "Add a component\n" |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 1897 | " " |
| 1898 | " -j valid size: 0x10000 0x20000 0x40000 0x80000 0x100000 \n" |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1899 | " add-payload [-r image,regions] -f FILE -n NAME [-A hash] \\\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1900 | " [-c compression] [-b base-address] \\\n" |
| 1901 | " (linux specific: [-C cmdline] [-I initrd]) " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1902 | "Add a payload to the ROM\n" |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1903 | " add-stage [-r image,regions] -f FILE -n NAME [-A hash] \\\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1904 | " [-c compression] [-b base] [-S section-to-ignore] \\\n" |
Arthur Heymans | c78521b3 | 2021-06-16 17:16:05 +0200 | [diff] [blame] | 1905 | " [-a alignment] [-Q|--pow2page] \\\n" |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 1906 | " [-y|--xip] [--ibb] \\\n" |
| 1907 | " [--ext-win-base win-base --ext-win-size win-size] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1908 | "Add a stage to the ROM\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1909 | " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n" |
| 1910 | " [-A hash] -l load-address -e entry-point \\\n" |
| 1911 | " [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1912 | "Add a 32bit flat mode binary\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1913 | " add-int [-r image,regions] -i INTEGER -n NAME [-b base] " |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1914 | "Add a raw 64-bit integer value\n" |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 1915 | " add-master-header [-r image,regions] \\ \n" |
| 1916 | " [-j topswap-size] (Intel CPUs only) " |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 1917 | "Add a legacy CBFS master header\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1918 | " remove [-r image,regions] -n NAME " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1919 | "Remove a component\n" |
Aaron Durbin | 71c60ca | 2016-01-26 17:08:56 -0600 | [diff] [blame] | 1920 | " compact -r image,regions " |
| 1921 | "Defragment CBFS image.\n" |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 1922 | " copy -r image,regions -R source-region " |
| 1923 | "Create a copy (duplicate) cbfs instance in fmap\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1924 | " create -m ARCH -s size [-b bootblock offset] \\\n" |
Patrick Georgi | 83c2d12 | 2015-08-26 10:40:00 +0200 | [diff] [blame] | 1925 | " [-o CBFS offset] [-H header offset] [-B bootblock] " |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1926 | "Create a legacy ROM file with CBFS master header*\n" |
| 1927 | " create -M flashmap [-r list,of,regions,containing,cbfses] " |
| 1928 | "Create a new-style partitioned firmware image\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1929 | " layout [-w] " |
| 1930 | "List mutable (or, with -w, readable) image regions\n" |
Julius Werner | 4bfbabd | 2020-05-06 17:27:02 -0700 | [diff] [blame] | 1931 | " print [-r image,regions] [-k] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1932 | "Show the contents of the ROM\n" |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 1933 | " extract [-r image,regions] [-m ARCH] -n NAME -f FILE [-U] " |
| 1934 | "Extracts a file from ROM\n" |
Patrick Georgi | d9edb18 | 2016-12-06 18:55:26 +0100 | [diff] [blame] | 1935 | " write [-F] -r image,regions -f file [-u | -d] [-i int] " |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1936 | "Write file into same-size [or larger] raw region\n" |
| 1937 | " read [-r fmap-region] -f file " |
| 1938 | "Extract raw region contents into binary file\n" |
Patrick Georgi | 12631a4 | 2017-09-20 11:59:18 +0200 | [diff] [blame] | 1939 | " truncate [-r fmap-region] " |
| 1940 | "Truncate CBFS and print new size on stdout\n" |
Patrick Georgi | 5d982d7 | 2017-09-19 14:39:58 +0200 | [diff] [blame] | 1941 | " expand [-r fmap-region] " |
| 1942 | "Expand CBFS to span entire region\n" |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 1943 | "OFFSETs:\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 1944 | " Numbers accompanying -b, -H, and -o switches* may be provided\n" |
| 1945 | " in two possible formats: if their value is greater than\n" |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 1946 | " 0x80000000, they are interpreted as a top-aligned x86 memory\n" |
| 1947 | " address; otherwise, they are treated as an offset into flash.\n" |
Jonathan Neuschäfer | fbc66b9 | 2018-04-08 15:05:09 +0200 | [diff] [blame] | 1948 | "ARCHes:\n", name, name |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1949 | ); |
Jonathan Neuschäfer | fbc66b9 | 2018-04-08 15:05:09 +0200 | [diff] [blame] | 1950 | print_supported_architectures(); |
| 1951 | |
| 1952 | printf("TYPEs:\n"); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 1953 | print_supported_filetypes(); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1954 | printf( |
| 1955 | "\n* Note that these actions and switches are only valid when\n" |
| 1956 | " working with legacy images whose structure is described\n" |
| 1957 | " primarily by a CBFS master header. New-style images, in\n" |
| 1958 | " contrast, exclusively make use of an FMAP to describe their\n" |
| 1959 | " layout: this must minimally contain an '%s' section\n" |
| 1960 | " specifying the location of this FMAP itself and a '%s'\n" |
| 1961 | " section describing the primary CBFS. It should also be noted\n" |
| 1962 | " that, when working with such images, the -F and -r switches\n" |
Julius Werner | 0fd072d | 2021-12-13 10:28:29 -0800 | [diff] [blame] | 1963 | " default to '%s' for convenience, and the -b switch becomes\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 1964 | " relative to the selected CBFS region's lowest address.\n" |
| 1965 | " The one exception to this rule is the top-aligned address,\n" |
| 1966 | " which is always relative to the end of the entire image\n" |
| 1967 | " rather than relative to the local region; this is true for\n" |
| 1968 | " for both input (sufficiently large) and output (-T) data.\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1969 | SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS, |
| 1970 | SECTION_NAME_PRIMARY_CBFS |
| 1971 | ); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Furquan Shaikh | c42cf91 | 2020-04-14 00:14:44 -0700 | [diff] [blame] | 1974 | static bool valid_opt(size_t i, int c) |
| 1975 | { |
| 1976 | /* Check if it is one of the optstrings supported by the command. */ |
| 1977 | if (strchr(commands[i].optstring, c)) |
| 1978 | return true; |
| 1979 | |
| 1980 | /* |
| 1981 | * Check if it is one of the non-ASCII characters. Currently, the |
| 1982 | * non-ASCII characters are only checked against the valid list |
| 1983 | * irrespective of the command. |
| 1984 | */ |
| 1985 | if (c >= LONGOPT_START && c < LONGOPT_END) |
| 1986 | return true; |
| 1987 | |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1991 | int main(int argc, char **argv) |
| 1992 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 1993 | size_t i; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1994 | int c; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1995 | |
| 1996 | if (argc < 3) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1997 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1998 | return 1; |
| 1999 | } |
| 2000 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2001 | char *image_name = argv[1]; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 2002 | char *cmd = argv[2]; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2003 | optind += 2; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 2004 | |
| 2005 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 2006 | if (strcmp(cmd, commands[i].name) != 0) |
| 2007 | continue; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2008 | |
| 2009 | while (1) { |
| 2010 | char *suffix = NULL; |
| 2011 | int option_index = 0; |
| 2012 | |
| 2013 | c = getopt_long(argc, argv, commands[i].optstring, |
| 2014 | long_options, &option_index); |
Nico Huber | 5407310 | 2016-08-01 23:18:29 +0200 | [diff] [blame] | 2015 | if (c == -1) { |
| 2016 | if (optind < argc) { |
| 2017 | ERROR("%s: excessive argument -- '%s'" |
| 2018 | "\n", argv[0], argv[optind]); |
| 2019 | return 1; |
| 2020 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2021 | break; |
Nico Huber | 5407310 | 2016-08-01 23:18:29 +0200 | [diff] [blame] | 2022 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2023 | |
Joel Kitching | 672d5ad | 2018-10-11 17:01:30 +0800 | [diff] [blame] | 2024 | /* Filter out illegal long options */ |
Furquan Shaikh | c42cf91 | 2020-04-14 00:14:44 -0700 | [diff] [blame] | 2025 | if (!valid_opt(i, c)) { |
| 2026 | ERROR("%s: invalid option -- '%d'\n", |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 2027 | argv[0], c); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2028 | c = '?'; |
| 2029 | } |
| 2030 | |
| 2031 | switch(c) { |
| 2032 | case 'n': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2033 | param.name = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2034 | break; |
| 2035 | case 't': |
| 2036 | if (intfiletype(optarg) != ((uint64_t) - 1)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2037 | param.type = intfiletype(optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2038 | else |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2039 | param.type = strtoul(optarg, NULL, 0); |
| 2040 | if (param.type == 0) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 2041 | WARN("Unknown type '%s' ignored\n", |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2042 | optarg); |
| 2043 | break; |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 2044 | case 'c': { |
Patrick Georgi | ecaa570 | 2017-01-11 18:38:11 +0100 | [diff] [blame] | 2045 | if (strcmp(optarg, "precompression") == 0) { |
| 2046 | param.precompression = 1; |
| 2047 | break; |
| 2048 | } |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 2049 | int algo = cbfs_parse_comp_algo(optarg); |
| 2050 | if (algo >= 0) |
| 2051 | param.compression = algo; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2052 | else |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 2053 | WARN("Unknown compression '%s' ignored.\n", |
| 2054 | optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2055 | break; |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 2056 | } |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 2057 | case 'A': { |
Julius Werner | d477565 | 2020-03-13 16:43:34 -0700 | [diff] [blame] | 2058 | if (!vb2_lookup_hash_alg(optarg, ¶m.hash)) { |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 2059 | ERROR("Unknown hash algorithm '%s'.\n", |
| 2060 | optarg); |
| 2061 | return 1; |
| 2062 | } |
| 2063 | break; |
| 2064 | } |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2065 | case 'M': |
| 2066 | param.fmap = optarg; |
| 2067 | break; |
| 2068 | case 'r': |
| 2069 | param.region_name = optarg; |
| 2070 | break; |
Patrick Georgi | bd0bb23 | 2015-11-20 21:48:18 +0100 | [diff] [blame] | 2071 | case 'R': |
| 2072 | param.source_region = optarg; |
| 2073 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2074 | case 'b': |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 2075 | param.baseaddress_input = strtoll(optarg, &suffix, 0); |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2076 | if (!*optarg || (suffix && *suffix)) { |
| 2077 | ERROR("Invalid base address '%s'.\n", |
| 2078 | optarg); |
| 2079 | return 1; |
| 2080 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 2081 | // baseaddress may be zero on non-x86, so we |
| 2082 | // need an explicit "baseaddress_assigned". |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 2083 | param.baseaddress_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2084 | break; |
| 2085 | case 'l': |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2086 | param.loadaddress = strtoul(optarg, &suffix, 0); |
| 2087 | if (!*optarg || (suffix && *suffix)) { |
| 2088 | ERROR("Invalid load address '%s'.\n", |
| 2089 | optarg); |
| 2090 | return 1; |
| 2091 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2092 | break; |
| 2093 | case 'e': |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2094 | param.entrypoint = strtoul(optarg, &suffix, 0); |
| 2095 | if (!*optarg || (suffix && *suffix)) { |
| 2096 | ERROR("Invalid entry point '%s'.\n", |
| 2097 | optarg); |
| 2098 | return 1; |
| 2099 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2100 | break; |
| 2101 | case 's': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2102 | param.size = strtoul(optarg, &suffix, 0); |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2103 | if (!*optarg) { |
| 2104 | ERROR("Empty size specified.\n"); |
| 2105 | return 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2106 | } |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2107 | switch (tolower((int)suffix[0])) { |
| 2108 | case 'k': |
| 2109 | param.size *= 1024; |
| 2110 | break; |
| 2111 | case 'm': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2112 | param.size *= 1024 * 1024; |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2113 | break; |
| 2114 | case '\0': |
| 2115 | break; |
| 2116 | default: |
| 2117 | ERROR("Invalid suffix for size '%s'.\n", |
| 2118 | optarg); |
| 2119 | return 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2120 | } |
Patrick Georgi | e887ca5 | 2014-08-09 17:44:39 +0200 | [diff] [blame] | 2121 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2122 | case 'B': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2123 | param.bootblock = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2124 | break; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 2125 | case 'H': |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 2126 | param.headeroffset_input = strtoll(optarg, &suffix, 0); |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2127 | if (!*optarg || (suffix && *suffix)) { |
| 2128 | ERROR("Invalid header offset '%s'.\n", |
| 2129 | optarg); |
| 2130 | return 1; |
| 2131 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 2132 | param.headeroffset_assigned = 1; |
| 2133 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2134 | case 'a': |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2135 | param.alignment = strtoul(optarg, &suffix, 0); |
| 2136 | if (!*optarg || (suffix && *suffix)) { |
| 2137 | ERROR("Invalid alignment '%s'.\n", |
| 2138 | optarg); |
| 2139 | return 1; |
| 2140 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2141 | break; |
Daisuke Nojiri | ff906fb | 2017-10-30 17:38:04 -0700 | [diff] [blame] | 2142 | case 'p': |
| 2143 | param.padding = strtoul(optarg, &suffix, 0); |
| 2144 | if (!*optarg || (suffix && *suffix)) { |
| 2145 | ERROR("Invalid pad size '%s'.\n", |
| 2146 | optarg); |
| 2147 | return 1; |
| 2148 | } |
| 2149 | break; |
Kyösti Mälkki | c36469e | 2020-05-28 23:12:02 +0300 | [diff] [blame] | 2150 | case 'Q': |
| 2151 | param.force_pow2_pagesize = 1; |
| 2152 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2153 | case 'o': |
Furquan Shaikh | 6b6e9b5 | 2020-11-20 16:52:03 -0800 | [diff] [blame] | 2154 | param.cbfsoffset_input = strtoll(optarg, &suffix, 0); |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2155 | if (!*optarg || (suffix && *suffix)) { |
| 2156 | ERROR("Invalid cbfs offset '%s'.\n", |
| 2157 | optarg); |
| 2158 | return 1; |
| 2159 | } |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 2160 | param.cbfsoffset_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2161 | break; |
| 2162 | case 'f': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 2163 | param.filename = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2164 | break; |
Patrick Georgi | 01fbc3a | 2016-10-12 16:46:13 +0200 | [diff] [blame] | 2165 | case 'F': |
| 2166 | param.force = 1; |
| 2167 | break; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 2168 | case 'i': |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2169 | param.u64val = strtoull(optarg, &suffix, 0); |
Patrick Georgi | 16b3e4b | 2016-12-12 18:38:01 +0100 | [diff] [blame] | 2170 | param.u64val_assigned = 1; |
Nico Huber | 9ade717 | 2016-08-01 21:37:42 +0200 | [diff] [blame] | 2171 | if (!*optarg || (suffix && *suffix)) { |
| 2172 | ERROR("Invalid int parameter '%s'.\n", |
| 2173 | optarg); |
| 2174 | return 1; |
| 2175 | } |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 2176 | break; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2177 | case 'u': |
| 2178 | param.fill_partial_upward = true; |
| 2179 | break; |
| 2180 | case 'd': |
| 2181 | param.fill_partial_downward = true; |
| 2182 | break; |
| 2183 | case 'w': |
| 2184 | param.show_immutable = true; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 2185 | break; |
Rizwan Qureshi | 1dc188f | 2018-05-29 14:48:14 +0530 | [diff] [blame] | 2186 | case 'j': |
| 2187 | param.topswap_size = strtol(optarg, NULL, 0); |
| 2188 | if (!is_valid_topswap()) |
| 2189 | return 1; |
| 2190 | break; |
Rizwan Qureshi | c1072f2 | 2018-06-04 23:02:46 +0530 | [diff] [blame] | 2191 | case 'q': |
| 2192 | param.ucode_region = optarg; |
| 2193 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2194 | case 'v': |
| 2195 | verbose++; |
| 2196 | break; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 2197 | case 'm': |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 2198 | param.arch = string_to_arch(optarg); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 2199 | break; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 2200 | case 'I': |
| 2201 | param.initrd = optarg; |
| 2202 | break; |
| 2203 | case 'C': |
| 2204 | param.cmdline = optarg; |
| 2205 | break; |
Furquan Shaikh | 405304a | 2014-10-30 11:44:20 -0700 | [diff] [blame] | 2206 | case 'S': |
| 2207 | param.ignore_section = optarg; |
| 2208 | break; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 2209 | case 'y': |
| 2210 | param.stage_xip = true; |
| 2211 | break; |
Werner Zeh | e9995f1 | 2016-01-14 13:22:37 +0100 | [diff] [blame] | 2212 | case 'g': |
| 2213 | param.autogen_attr = true; |
| 2214 | break; |
Aaron Durbin | 5dc628a | 2016-01-26 15:35:34 -0600 | [diff] [blame] | 2215 | case 'k': |
| 2216 | param.machine_parseable = true; |
| 2217 | break; |
Joel Kitching | 21fdd89 | 2018-08-09 17:49:52 +0800 | [diff] [blame] | 2218 | case 'U': |
| 2219 | param.unprocessed = true; |
| 2220 | break; |
Philipp Deppenwiese | 7ba5871 | 2018-11-20 13:54:49 +0100 | [diff] [blame] | 2221 | case LONGOPT_IBB: |
| 2222 | param.ibb = true; |
| 2223 | break; |
Furquan Shaikh | 0ae389c | 2020-11-21 10:35:10 -0800 | [diff] [blame] | 2224 | case LONGOPT_EXT_WIN_BASE: |
| 2225 | param.ext_win_base = strtoul(optarg, &suffix, 0); |
| 2226 | if (!*optarg || (suffix && *suffix)) { |
| 2227 | ERROR("Invalid ext window base '%s'.\n", optarg); |
| 2228 | return 1; |
| 2229 | } |
| 2230 | break; |
| 2231 | case LONGOPT_EXT_WIN_SIZE: |
| 2232 | param.ext_win_size = strtoul(optarg, &suffix, 0); |
| 2233 | if (!*optarg || (suffix && *suffix)) { |
| 2234 | ERROR("Invalid ext window size '%s'.\n", optarg); |
| 2235 | return 1; |
| 2236 | } |
| 2237 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2238 | case 'h': |
| 2239 | case '?': |
| 2240 | usage(argv[0]); |
| 2241 | return 1; |
| 2242 | default: |
| 2243 | break; |
| 2244 | } |
| 2245 | } |
| 2246 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2247 | if (commands[i].function == cbfs_create) { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2248 | if (param.fmap) { |
| 2249 | struct buffer flashmap; |
| 2250 | if (buffer_from_file(&flashmap, param.fmap)) |
| 2251 | return 1; |
| 2252 | param.image_file = partitioned_file_create( |
| 2253 | image_name, &flashmap); |
| 2254 | buffer_delete(&flashmap); |
| 2255 | } else if (param.size) { |
| 2256 | param.image_file = partitioned_file_create_flat( |
| 2257 | image_name, param.size); |
| 2258 | } else { |
| 2259 | ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2260 | return 1; |
| 2261 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2262 | } else { |
Vadim Bendebury | 7559946 | 2015-12-09 09:39:31 -0800 | [diff] [blame] | 2263 | bool write_access = commands[i].modifies_region; |
| 2264 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2265 | param.image_file = |
Vadim Bendebury | 7559946 | 2015-12-09 09:39:31 -0800 | [diff] [blame] | 2266 | partitioned_file_reopen(image_name, |
| 2267 | write_access); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2268 | } |
| 2269 | if (!param.image_file) |
| 2270 | return 1; |
| 2271 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2272 | unsigned num_regions = 1; |
| 2273 | for (const char *list = strchr(param.region_name, ','); list; |
| 2274 | list = strchr(list + 1, ',')) |
| 2275 | ++num_regions; |
| 2276 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2277 | // If the action needs to read an image region, as indicated by |
| 2278 | // having accesses_region set in its command struct, that |
| 2279 | // region's buffer struct will be stored here and the client |
| 2280 | // will receive a pointer to it via param.image_region. It |
| 2281 | // need not write the buffer back to the image file itself, |
| 2282 | // since this behavior can be requested via its modifies_region |
| 2283 | // field. Additionally, it should never free the region buffer, |
| 2284 | // as that is performed automatically once it completes. |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2285 | struct buffer image_regions[num_regions]; |
| 2286 | memset(image_regions, 0, sizeof(image_regions)); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2287 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2288 | bool seen_primary_cbfs = false; |
| 2289 | char region_name_scratch[strlen(param.region_name) + 1]; |
| 2290 | strcpy(region_name_scratch, param.region_name); |
| 2291 | param.region_name = strtok(region_name_scratch, ","); |
| 2292 | for (unsigned region = 0; region < num_regions; ++region) { |
| 2293 | if (!param.region_name) { |
| 2294 | ERROR("Encountered illegal degenerate region name in -r list\n"); |
| 2295 | ERROR("The image will be left unmodified.\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2296 | partitioned_file_close(param.image_file); |
| 2297 | return 1; |
| 2298 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2299 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2300 | if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS) |
| 2301 | == 0) |
| 2302 | seen_primary_cbfs = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2303 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2304 | param.image_region = image_regions + region; |
| 2305 | if (dispatch_command(commands[i])) { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2306 | partitioned_file_close(param.image_file); |
| 2307 | return 1; |
| 2308 | } |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2309 | |
| 2310 | param.region_name = strtok(NULL, ","); |
| 2311 | } |
| 2312 | |
| 2313 | if (commands[i].function == cbfs_create && !seen_primary_cbfs) { |
| 2314 | ERROR("The creation -r list must include the mandatory '%s' section.\n", |
| 2315 | SECTION_NAME_PRIMARY_CBFS); |
| 2316 | ERROR("The image will be left unmodified.\n"); |
| 2317 | partitioned_file_close(param.image_file); |
| 2318 | return 1; |
| 2319 | } |
| 2320 | |
| 2321 | if (commands[i].modifies_region) { |
| 2322 | assert(param.image_file); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2323 | for (unsigned region = 0; region < num_regions; |
| 2324 | ++region) { |
| 2325 | |
| 2326 | if (!partitioned_file_write_region( |
| 2327 | param.image_file, |
| 2328 | image_regions + region)) { |
| 2329 | partitioned_file_close( |
| 2330 | param.image_file); |
| 2331 | return 1; |
| 2332 | } |
| 2333 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | partitioned_file_close(param.image_file); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 2337 | return 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 2340 | ERROR("Unknown command '%s'.\n", cmd); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 2341 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 2342 | return 1; |
| 2343 | } |