Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 1 | /* |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 2 | * cbfstool, CLI utility for CBFS file manipulation |
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 | * Copyright (C) 2009 coresystems GmbH |
| 5 | * written by Patrick Georgi <patrick.georgi@coresystems.de> |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 6 | * Copyright (C) 2012 Google, Inc. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
Stefan Reinauer | 336daa7 | 2009-12-21 15:09:01 +0000 | [diff] [blame] | 20 | #include <string.h> |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 21 | #include <strings.h> |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 22 | #include <ctype.h> |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | #include <getopt.h> |
Patrick Georgi | b7b56dd8 | 2009-09-14 13:29:27 +0000 | [diff] [blame] | 25 | #include "common.h" |
| 26 | #include "cbfs.h" |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 27 | #include "cbfs_image.h" |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 28 | #include "cbfs_sections.h" |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 29 | #include "fit.h" |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 30 | #include "partitioned_file.h" |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 31 | #include <commonlib/fsp1_1.h> |
Ronald G. Minnich | 5d01ec0 | 2009-03-31 11:57:36 +0000 | [diff] [blame] | 32 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 33 | #define SECTION_WITH_FIT_TABLE "BOOTBLOCK" |
| 34 | |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 35 | struct command { |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 36 | const char *name; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 37 | const char *optstring; |
| 38 | int (*function) (void); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 39 | // Whether to populate param.image_region before invoking function |
| 40 | bool accesses_region; |
| 41 | // Whether to write that region's contents back to image_file at the end |
| 42 | bool modifies_region; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 45 | static struct param { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 46 | partitioned_file_t *image_file; |
| 47 | struct buffer *image_region; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 48 | const char *name; |
| 49 | const char *filename; |
| 50 | const char *fmap; |
| 51 | const char *region_name; |
| 52 | const char *bootblock; |
| 53 | const char *ignore_section; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 54 | uint64_t u64val; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 55 | uint32_t type; |
| 56 | uint32_t baseaddress; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 57 | uint32_t baseaddress_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 58 | uint32_t loadaddress; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 59 | uint32_t copyoffset; |
| 60 | uint32_t copyoffset_assigned; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 61 | uint32_t headeroffset; |
| 62 | uint32_t headeroffset_assigned; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 63 | uint32_t entrypoint; |
| 64 | uint32_t size; |
| 65 | uint32_t alignment; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 66 | uint32_t pagesize; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 67 | uint32_t cbfsoffset; |
| 68 | uint32_t cbfsoffset_assigned; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 69 | uint32_t arch; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 70 | bool fill_partial_upward; |
| 71 | bool fill_partial_downward; |
| 72 | bool show_immutable; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 73 | bool stage_xip; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 74 | int fit_empty_entries; |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 75 | enum comp_algo compression; |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 76 | enum vb2_hash_algorithm hash; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 77 | /* for linux payloads */ |
| 78 | char *initrd; |
| 79 | char *cmdline; |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 80 | } param = { |
| 81 | /* All variables not listed are initialized as zero. */ |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 82 | .arch = CBFS_ARCHITECTURE_UNKNOWN, |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 83 | .compression = CBFS_COMPRESS_NONE, |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 84 | .hash = VB2_HASH_INVALID, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 85 | .headeroffset = ~0, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 86 | .region_name = SECTION_NAME_PRIMARY_CBFS, |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 87 | }; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 88 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 89 | static bool region_is_flashmap(const char *region) |
| 90 | { |
| 91 | return partitioned_file_region_check_magic(param.image_file, region, |
| 92 | FMAP_SIGNATURE, strlen(FMAP_SIGNATURE)); |
| 93 | } |
| 94 | |
| 95 | /* @return Same as cbfs_is_valid_cbfs(), but for a named region. */ |
| 96 | static bool region_is_modern_cbfs(const char *region) |
| 97 | { |
| 98 | return partitioned_file_region_check_magic(param.image_file, region, |
| 99 | CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC)); |
| 100 | } |
| 101 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 102 | /* |
| 103 | * Converts between offsets from the start of the specified image region and |
Patrick Georgi | 9731119 | 2015-12-07 23:14:07 +0100 | [diff] [blame] | 104 | * "top-aligned" offsets from the top of the image region. Works in either |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 105 | * direction: pass in one type of offset and receive the other type. |
| 106 | * N.B. A top-aligned offset is always a positive number, and should not be |
| 107 | * confused with a top-aliged *address*, which is its arithmetic inverse. */ |
| 108 | static unsigned convert_to_from_top_aligned(const struct buffer *region, |
| 109 | unsigned offset) |
| 110 | { |
| 111 | assert(region); |
| 112 | |
Patrick Georgi | 9731119 | 2015-12-07 23:14:07 +0100 | [diff] [blame] | 113 | /* cover the situation where a negative base address is given by the |
| 114 | * user. Callers of this function negate it, so it'll be a positive |
| 115 | * number smaller than the region. |
| 116 | */ |
| 117 | if ((offset > 0) && (offset < region->size)) { |
| 118 | return region->size - offset; |
| 119 | } |
| 120 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 121 | size_t image_size = partitioned_file_total_size(param.image_file); |
| 122 | return image_size - region->offset - offset; |
| 123 | } |
| 124 | |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 125 | static int do_cbfs_locate(int32_t *cbfs_addr, size_t metadata_size) |
| 126 | { |
| 127 | if (!param.filename) { |
| 128 | ERROR("You need to specify -f/--filename.\n"); |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | if (!param.name) { |
| 133 | ERROR("You need to specify -n/--name.\n"); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | struct cbfs_image image; |
| 138 | if (cbfs_image_from_buffer(&image, param.image_region, |
| 139 | param.headeroffset)) |
| 140 | return 1; |
| 141 | |
| 142 | if (cbfs_get_entry(&image, param.name)) |
| 143 | WARN("'%s' already in CBFS.\n", param.name); |
| 144 | |
| 145 | struct buffer buffer; |
| 146 | if (buffer_from_file(&buffer, param.filename) != 0) { |
| 147 | ERROR("Cannot load %s.\n", param.filename); |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | /* Include cbfs_file size along with space for with name. */ |
| 152 | metadata_size += cbfs_calculate_file_header_size(param.name); |
| 153 | |
| 154 | int32_t address = cbfs_locate_entry(&image, buffer.size, param.pagesize, |
| 155 | param.alignment, metadata_size); |
| 156 | buffer_delete(&buffer); |
| 157 | |
| 158 | if (address == -1) { |
| 159 | ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n", |
| 160 | param.name, param.pagesize, param.alignment); |
| 161 | return 1; |
| 162 | } |
| 163 | |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 164 | *cbfs_addr = address; |
| 165 | return 0; |
| 166 | } |
| 167 | |
Patrick Georgi | f820497 | 2015-08-11 15:16:24 +0200 | [diff] [blame] | 168 | typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 169 | struct cbfs_file *header); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 170 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 171 | static int cbfs_add_integer_component(const char *name, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 172 | uint64_t u64val, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 173 | uint32_t offset, |
| 174 | uint32_t headeroffset) { |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 175 | struct cbfs_image image; |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 176 | struct cbfs_file *header = NULL; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 177 | struct buffer buffer; |
| 178 | int i, ret = 1; |
| 179 | |
| 180 | if (!name) { |
| 181 | ERROR("You need to specify -n/--name.\n"); |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | if (buffer_create(&buffer, 8, name) != 0) |
| 186 | return 1; |
| 187 | |
| 188 | for (i = 0; i < 8; i++) |
| 189 | buffer.data[i] = (u64val >> i*8) & 0xff; |
| 190 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 191 | if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) { |
| 192 | ERROR("Selected image region is not a CBFS.\n"); |
| 193 | goto done; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if (cbfs_get_entry(&image, name)) { |
| 197 | ERROR("'%s' already in ROM image.\n", name); |
| 198 | goto done; |
| 199 | } |
| 200 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 201 | if (IS_TOP_ALIGNED_ADDRESS(offset)) |
| 202 | offset = convert_to_from_top_aligned(param.image_region, |
| 203 | -offset); |
| 204 | |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 205 | header = cbfs_create_file_header(CBFS_COMPONENT_RAW, |
| 206 | buffer.size, name); |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 207 | if (cbfs_add_entry(&image, &buffer, offset, header) != 0) { |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 208 | ERROR("Failed to add %llu into ROM image as '%s'.\n", |
| 209 | (long long unsigned)u64val, name); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 210 | goto done; |
| 211 | } |
| 212 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 213 | ret = 0; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 214 | |
| 215 | done: |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 216 | free(header); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 217 | buffer_delete(&buffer); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 218 | return ret; |
| 219 | } |
| 220 | |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 221 | static int cbfs_add_master_header(void) |
| 222 | { |
| 223 | const char * const name = "cbfs master header"; |
| 224 | struct cbfs_image image; |
| 225 | struct cbfs_file *header = NULL; |
| 226 | struct buffer buffer; |
| 227 | int ret = 1; |
| 228 | |
| 229 | if (cbfs_image_from_buffer(&image, param.image_region, |
| 230 | param.headeroffset)) { |
| 231 | ERROR("Selected image region is not a CBFS.\n"); |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | if (cbfs_get_entry(&image, name)) { |
| 236 | ERROR("'%s' already in ROM image.\n", name); |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | if (buffer_create(&buffer, sizeof(struct cbfs_header), name) != 0) |
| 241 | return 1; |
| 242 | |
| 243 | struct cbfs_header *h = (struct cbfs_header *)buffer.data; |
| 244 | h->magic = htonl(CBFS_HEADER_MAGIC); |
| 245 | h->version = htonl(CBFS_HEADER_VERSION); |
| 246 | h->romsize = htonl(param.image_region->size); |
| 247 | /* The 4 bytes are left out for two reasons: |
| 248 | * 1. the cbfs master header pointer resides there |
| 249 | * 2. some cbfs implementations assume that an image that resides |
| 250 | * below 4GB has a bootblock and get confused when the end of the |
| 251 | * image is at 4GB == 0. |
| 252 | */ |
| 253 | h->bootblocksize = htonl(4); |
| 254 | h->align = htonl(CBFS_ENTRY_ALIGNMENT); |
| 255 | /* offset relative to romsize above, which covers precisely the CBFS |
| 256 | * region. |
| 257 | */ |
| 258 | h->offset = htonl(0); |
| 259 | h->architecture = htonl(CBFS_ARCHITECTURE_UNKNOWN); |
| 260 | |
| 261 | header = cbfs_create_file_header(CBFS_COMPONENT_CBFSHEADER, |
| 262 | buffer_size(&buffer), name); |
| 263 | if (cbfs_add_entry(&image, &buffer, 0, header) != 0) { |
| 264 | ERROR("Failed to add cbfs master header into ROM image.\n"); |
| 265 | goto done; |
| 266 | } |
| 267 | |
| 268 | struct cbfs_file *entry; |
| 269 | if ((entry = cbfs_get_entry(&image, name)) == NULL) { |
| 270 | ERROR("'%s' not in ROM image?!?\n", name); |
| 271 | goto done; |
| 272 | } |
| 273 | |
| 274 | uint32_t header_offset = CBFS_SUBHEADER(entry) - |
| 275 | buffer_get(&image.buffer); |
| 276 | header_offset = -(buffer_size(&image.buffer) - header_offset); |
| 277 | |
| 278 | // TODO: when we have a BE target, we'll need to store this as BE |
| 279 | *(uint32_t *)(buffer_get(&image.buffer) + |
| 280 | buffer_size(&image.buffer) - 4) = |
Aaron Durbin | 4b93a4f | 2015-09-21 13:10:13 -0500 | [diff] [blame] | 281 | swab32(htonl(header_offset)); |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 282 | |
| 283 | ret = 0; |
| 284 | |
| 285 | done: |
| 286 | free(header); |
| 287 | buffer_delete(&buffer); |
| 288 | return ret; |
| 289 | } |
| 290 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 291 | static int cbfs_add_component(const char *filename, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 292 | const char *name, |
| 293 | uint32_t type, |
| 294 | uint32_t offset, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 295 | uint32_t headeroffset, |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 296 | convert_buffer_t convert) |
| 297 | { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 298 | if (!filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 299 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 303 | if (!name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 304 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 305 | return 1; |
| 306 | } |
| 307 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 308 | if (type == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 309 | ERROR("You need to specify a valid -t/--type.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 313 | struct cbfs_image image; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 314 | if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 315 | return 1; |
Stefan Reinauer | 8f50e53 | 2013-11-13 14:34:57 -0800 | [diff] [blame] | 316 | |
Patrick Georgi | 4e54bf9 | 2015-08-11 14:35:39 +0200 | [diff] [blame] | 317 | if (cbfs_get_entry(&image, name)) { |
| 318 | ERROR("'%s' already in ROM image.\n", name); |
| 319 | return 1; |
| 320 | } |
| 321 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 322 | struct buffer buffer; |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 323 | if (buffer_from_file(&buffer, filename) != 0) { |
| 324 | ERROR("Could not load file '%s'.\n", filename); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 325 | return 1; |
| 326 | } |
| 327 | |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 328 | struct cbfs_file *header = |
| 329 | cbfs_create_file_header(type, buffer.size, name); |
Patrick Georgi | 4110abc | 2015-08-11 15:10:33 +0200 | [diff] [blame] | 330 | |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 331 | if (convert && convert(&buffer, &offset, header) != 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 332 | ERROR("Failed to parse file '%s'.\n", filename); |
| 333 | buffer_delete(&buffer); |
Patrick Georgi | 56f5fb7 | 2009-09-30 11:21:18 +0000 | [diff] [blame] | 334 | return 1; |
Stefan Reinauer | fbadc49 | 2011-10-14 12:44:14 -0700 | [diff] [blame] | 335 | } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 336 | |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 337 | if (param.hash != VB2_HASH_INVALID) |
| 338 | if (cbfs_add_file_hash(header, &buffer, param.hash) == -1) { |
| 339 | ERROR("couldn't add hash for '%s'\n", name); |
| 340 | free(header); |
| 341 | buffer_delete(&buffer); |
| 342 | return 1; |
| 343 | } |
| 344 | |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 345 | if (IS_TOP_ALIGNED_ADDRESS(offset)) |
| 346 | offset = convert_to_from_top_aligned(param.image_region, |
| 347 | -offset); |
| 348 | |
Patrick Georgi | f5252f3 | 2015-08-25 22:27:57 +0200 | [diff] [blame] | 349 | if (cbfs_add_entry(&image, &buffer, offset, header) != 0) { |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 350 | ERROR("Failed to add '%s' into ROM image.\n", filename); |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 351 | free(header); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 352 | buffer_delete(&buffer); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 353 | return 1; |
| 354 | } |
| 355 | |
Patrick Georgi | 3ba501b | 2015-08-25 13:48:10 +0200 | [diff] [blame] | 356 | free(header); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 357 | buffer_delete(&buffer); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
Daisuke Nojiri | 8984a63 | 2015-07-09 15:07:45 -0700 | [diff] [blame] | 361 | static int cbfstool_convert_raw(struct buffer *buffer, |
| 362 | unused uint32_t *offset, struct cbfs_file *header) |
| 363 | { |
| 364 | char *compressed; |
| 365 | int compressed_size; |
| 366 | |
| 367 | comp_func_ptr compress = compression_function(param.compression); |
| 368 | if (!compress) |
| 369 | return -1; |
| 370 | compressed = calloc(buffer->size, 1); |
| 371 | |
| 372 | if (compress(buffer->data, buffer->size, |
| 373 | compressed, &compressed_size)) { |
| 374 | WARN("Compression failed - disabled\n"); |
| 375 | } else { |
| 376 | struct cbfs_file_attr_compression *attrs = |
| 377 | (struct cbfs_file_attr_compression *) |
| 378 | cbfs_add_file_attr(header, |
| 379 | CBFS_FILE_ATTR_TAG_COMPRESSION, |
| 380 | sizeof(struct cbfs_file_attr_compression)); |
| 381 | if (attrs == NULL) |
| 382 | return -1; |
| 383 | attrs->compression = htonl(param.compression); |
| 384 | attrs->decompressed_size = htonl(buffer->size); |
| 385 | |
| 386 | free(buffer->data); |
| 387 | buffer->data = compressed; |
| 388 | buffer->size = compressed_size; |
| 389 | |
| 390 | header->len = htonl(buffer->size); |
| 391 | } |
| 392 | return 0; |
| 393 | } |
| 394 | |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 395 | static int cbfstool_convert_fsp(struct buffer *buffer, |
| 396 | uint32_t *offset, struct cbfs_file *header) |
| 397 | { |
| 398 | uint32_t address; |
| 399 | struct buffer fsp; |
| 400 | |
| 401 | address = *offset; |
| 402 | |
| 403 | /* Ensure the address is a memory mapped one. */ |
| 404 | if (!IS_TOP_ALIGNED_ADDRESS(address)) |
| 405 | address = -convert_to_from_top_aligned(param.image_region, |
| 406 | address); |
| 407 | |
| 408 | /* Create a copy of the buffer to attempt relocation. */ |
| 409 | if (buffer_create(&fsp, buffer_size(buffer), "fsp")) |
| 410 | return -1; |
| 411 | |
| 412 | memcpy(buffer_get(&fsp), buffer_get(buffer), buffer_size(buffer)); |
| 413 | |
| 414 | /* Replace the buffer contents w/ the relocated ones on success. */ |
| 415 | if (fsp1_1_relocate(address, buffer_get(&fsp), buffer_size(&fsp)) > 0) { |
| 416 | buffer_delete(buffer); |
| 417 | buffer_clone(buffer, &fsp); |
| 418 | } else { |
| 419 | buffer_delete(&fsp); |
| 420 | WARN("FSP was not a 1.1 variant.\n"); |
| 421 | } |
| 422 | |
| 423 | /* Let the raw path handle all the cbfs metadata logic. */ |
| 424 | return cbfstool_convert_raw(buffer, offset, header); |
| 425 | } |
| 426 | |
Patrick Georgi | f820497 | 2015-08-11 15:16:24 +0200 | [diff] [blame] | 427 | static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 428 | struct cbfs_file *header) |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 429 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 430 | struct buffer output; |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 431 | int ret; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 432 | |
| 433 | if (param.stage_xip) { |
| 434 | int32_t address; |
| 435 | |
| 436 | if (do_cbfs_locate(&address, sizeof(struct cbfs_stage))) { |
| 437 | ERROR("Could not find location for XIP stage.\n"); |
| 438 | return 1; |
| 439 | } |
| 440 | |
| 441 | /* Pass in a top aligned address. */ |
| 442 | address = -convert_to_from_top_aligned(param.image_region, |
| 443 | address); |
| 444 | *offset = address; |
| 445 | |
| 446 | ret = parse_elf_to_xip_stage(buffer, &output, offset, |
| 447 | param.ignore_section); |
| 448 | } else |
| 449 | ret = parse_elf_to_stage(buffer, &output, param.compression, |
| 450 | offset, param.ignore_section); |
| 451 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 452 | if (ret != 0) |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 453 | return -1; |
| 454 | buffer_delete(buffer); |
| 455 | // direct assign, no dupe. |
| 456 | memcpy(buffer, &output, sizeof(*buffer)); |
Patrick Georgi | dc9dbc0 | 2015-08-25 20:17:14 +0200 | [diff] [blame] | 457 | header->len = htonl(output.size); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 461 | static int cbfstool_convert_mkpayload(struct buffer *buffer, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 462 | unused uint32_t *offset, struct cbfs_file *header) |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 463 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 464 | struct buffer output; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 465 | int ret; |
| 466 | /* per default, try and see if payload is an ELF binary */ |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 467 | ret = parse_elf_to_payload(buffer, &output, param.compression); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 468 | |
| 469 | /* If it's not an ELF, see if it's a UEFI FV */ |
| 470 | if (ret != 0) |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 471 | ret = parse_fv_to_payload(buffer, &output, param.compression); |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 472 | |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 473 | /* If it's neither ELF nor UEFI Fv, try bzImage */ |
| 474 | if (ret != 0) |
| 475 | ret = parse_bzImage_to_payload(buffer, &output, |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 476 | param.initrd, param.cmdline, param.compression); |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 477 | |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 478 | /* Not a supported payload type */ |
| 479 | if (ret != 0) { |
| 480 | ERROR("Not a supported payload type (ELF / FV).\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 481 | buffer_delete(buffer); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 482 | return -1; |
Stefan Reinauer | 543a682 | 2013-02-04 15:39:13 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 485 | buffer_delete(buffer); |
| 486 | // direct assign, no dupe. |
| 487 | memcpy(buffer, &output, sizeof(*buffer)); |
Patrick Georgi | dc9dbc0 | 2015-08-25 20:17:14 +0200 | [diff] [blame] | 488 | header->len = htonl(output.size); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int cbfstool_convert_mkflatpayload(struct buffer *buffer, |
Patrick Georgi | 056f6a1 | 2015-08-25 15:53:52 +0200 | [diff] [blame] | 493 | unused uint32_t *offset, struct cbfs_file *header) |
Stefan Reinauer | 2dd161f | 2015-03-04 00:55:03 +0100 | [diff] [blame] | 494 | { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 495 | struct buffer output; |
| 496 | if (parse_flat_binary_to_payload(buffer, &output, |
| 497 | param.loadaddress, |
| 498 | param.entrypoint, |
Sol Boucher | 6533671 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 499 | param.compression) != 0) { |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 500 | return -1; |
| 501 | } |
| 502 | buffer_delete(buffer); |
| 503 | // direct assign, no dupe. |
| 504 | memcpy(buffer, &output, sizeof(*buffer)); |
Patrick Georgi | dc9dbc0 | 2015-08-25 20:17:14 +0200 | [diff] [blame] | 505 | header->len = htonl(output.size); |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 506 | return 0; |
| 507 | } |
| 508 | |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 509 | static int cbfs_add(void) |
| 510 | { |
Alexandru Gagniuc | 38bc916 | 2015-09-07 00:05:44 -0700 | [diff] [blame] | 511 | int32_t address; |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 512 | convert_buffer_t convert; |
Alexandru Gagniuc | 38bc916 | 2015-09-07 00:05:44 -0700 | [diff] [blame] | 513 | |
| 514 | if (param.alignment && param.baseaddress) { |
| 515 | ERROR("Cannot specify both alignment and base address\n"); |
| 516 | return 1; |
| 517 | } |
| 518 | |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 519 | convert = cbfstool_convert_raw; |
| 520 | |
| 521 | /* Set the alignment to 4KiB minimum for FSP blobs when no base address |
| 522 | * is provided so that relocation can occur. */ |
| 523 | if (param.type == CBFS_COMPONENT_FSP) { |
| 524 | if (!param.baseaddress_assigned) |
| 525 | param.alignment = 4*1024; |
| 526 | convert = cbfstool_convert_fsp; |
| 527 | } |
| 528 | |
Alexandru Gagniuc | 38bc916 | 2015-09-07 00:05:44 -0700 | [diff] [blame] | 529 | if (param.alignment) { |
Aaron Durbin | 9b9d4b3 | 2015-09-15 21:30:58 -0500 | [diff] [blame] | 530 | /* CBFS compression file attribute is unconditionally added. */ |
| 531 | size_t metadata_sz = sizeof(struct cbfs_file_attr_compression); |
| 532 | if (do_cbfs_locate(&address, metadata_sz)) |
Alexandru Gagniuc | 38bc916 | 2015-09-07 00:05:44 -0700 | [diff] [blame] | 533 | return 1; |
| 534 | param.baseaddress = address; |
| 535 | } |
| 536 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 537 | return cbfs_add_component(param.filename, |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 538 | param.name, |
| 539 | param.type, |
| 540 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 541 | param.headeroffset, |
Aaron Durbin | 8a414a0 | 2015-10-01 17:02:45 -0500 | [diff] [blame] | 542 | convert); |
Hung-Te Lin | 5f3eb26 | 2013-01-29 10:24:00 +0800 | [diff] [blame] | 543 | } |
| 544 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 545 | static int cbfs_add_stage(void) |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 546 | { |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 547 | if (param.stage_xip) { |
| 548 | if (param.baseaddress_assigned) { |
| 549 | ERROR("Cannot specify base address for XIP.\n"); |
| 550 | return 1; |
| 551 | } |
| 552 | |
| 553 | if (param.compression != CBFS_COMPRESS_NONE) { |
| 554 | ERROR("Cannot specify compression for XIP.\n"); |
| 555 | return 1; |
| 556 | } |
| 557 | } |
| 558 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 559 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 560 | param.name, |
| 561 | CBFS_COMPONENT_STAGE, |
| 562 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 563 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 564 | cbfstool_convert_mkstage); |
| 565 | } |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 566 | |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 567 | static int cbfs_add_payload(void) |
| 568 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 569 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 570 | param.name, |
| 571 | CBFS_COMPONENT_PAYLOAD, |
| 572 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 573 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 574 | cbfstool_convert_mkpayload); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | static int cbfs_add_flat_binary(void) |
| 578 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 579 | if (param.loadaddress == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 580 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 581 | "-l/--load-address.\n"); |
| 582 | return 1; |
| 583 | } |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 584 | if (param.entrypoint == 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 585 | ERROR("You need to specify a valid " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 586 | "-e/--entry-point.\n"); |
| 587 | return 1; |
| 588 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 589 | return cbfs_add_component(param.filename, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 590 | param.name, |
| 591 | CBFS_COMPONENT_PAYLOAD, |
| 592 | param.baseaddress, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 593 | param.headeroffset, |
Hung-Te Lin | c13e4bf | 2013-01-29 15:22:11 +0800 | [diff] [blame] | 594 | cbfstool_convert_mkflatpayload); |
Stefan Reinauer | 20848ee | 2012-10-22 16:04:13 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 597 | static int cbfs_add_integer(void) |
| 598 | { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 599 | return cbfs_add_integer_component(param.name, |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 600 | param.u64val, |
Vadim Bendebury | 458a12e | 2014-12-23 15:10:12 -0800 | [diff] [blame] | 601 | param.baseaddress, |
| 602 | param.headeroffset); |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 605 | static int cbfs_remove(void) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 606 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 607 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 608 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 609 | return 1; |
| 610 | } |
| 611 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 612 | struct cbfs_image image; |
| 613 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 614 | param.headeroffset)) |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 615 | return 1; |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 616 | |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 617 | if (cbfs_remove_entry(&image, param.name) != 0) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 618 | ERROR("Removing file '%s' failed.\n", |
Hung-Te Lin | c03d9b0 | 2013-01-29 02:38:40 +0800 | [diff] [blame] | 619 | param.name); |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 620 | return 1; |
| 621 | } |
| 622 | |
Gabe Black | e1bb49e | 2012-01-27 00:33:47 -0800 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 626 | static int cbfs_create(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 627 | { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 628 | struct cbfs_image image; |
| 629 | memset(&image, 0, sizeof(image)); |
| 630 | buffer_clone(&image.buffer, param.image_region); |
| 631 | |
| 632 | if (param.fmap) { |
| 633 | if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size || |
| 634 | param.baseaddress_assigned || |
| 635 | param.headeroffset_assigned || |
| 636 | param.cbfsoffset_assigned || |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 637 | param.bootblock) { |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 638 | 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] | 639 | return 1; |
| 640 | } |
| 641 | |
| 642 | return cbfs_image_create(&image, image.buffer.size); |
| 643 | } |
| 644 | |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 645 | if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) { |
Stefan Reinauer | 60a4a73 | 2013-03-28 16:46:07 -0700 | [diff] [blame] | 646 | ERROR("You need to specify -m/--machine arch.\n"); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 647 | return 1; |
| 648 | } |
| 649 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 650 | struct buffer bootblock; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 651 | if (!param.bootblock) { |
| 652 | DEBUG("-B not given, creating image without bootblock.\n"); |
| 653 | buffer_create(&bootblock, 0, "(dummy)"); |
| 654 | } else if (buffer_from_file(&bootblock, param.bootblock)) { |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 655 | return 1; |
| 656 | } |
| 657 | |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 658 | if (!param.alignment) |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 659 | param.alignment = CBFS_ALIGNMENT; |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 660 | |
| 661 | // 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] | 662 | if (!param.baseaddress_assigned) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 663 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 664 | // Make sure there's at least enough room for rel_offset |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 665 | param.baseaddress = param.size - |
| 666 | MAX(bootblock.size, sizeof(int32_t)); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 667 | DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n", |
| 668 | param.baseaddress); |
| 669 | } else { |
| 670 | param.baseaddress = 0; |
| 671 | DEBUG("bootblock starts at address 0x0.\n"); |
| 672 | } |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 673 | } |
| 674 | if (!param.headeroffset_assigned) { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 675 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 676 | param.headeroffset = param.baseaddress - |
| 677 | sizeof(struct cbfs_header); |
| 678 | DEBUG("x86 -> CBFS header before bootblock (%#x).\n", |
| 679 | param.headeroffset); |
| 680 | } else { |
| 681 | param.headeroffset = align_up(param.baseaddress + |
| 682 | bootblock.size, sizeof(uint32_t)); |
| 683 | DEBUG("CBFS header placed behind bootblock (%#x).\n", |
| 684 | param.headeroffset); |
| 685 | } |
| 686 | } |
| 687 | if (!param.cbfsoffset_assigned) { |
| 688 | if (param.arch == CBFS_ARCHITECTURE_X86) { |
| 689 | param.cbfsoffset = 0; |
| 690 | DEBUG("x86 -> CBFS entries start at address 0x0.\n"); |
| 691 | } else { |
| 692 | param.cbfsoffset = align_up(param.headeroffset + |
| 693 | sizeof(struct cbfs_header), |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 694 | CBFS_ALIGNMENT); |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 695 | DEBUG("CBFS entries start beind master header (%#x).\n", |
| 696 | param.cbfsoffset); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 700 | int ret = cbfs_legacy_image_create(&image, |
| 701 | param.arch, |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 702 | CBFS_ALIGNMENT, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 703 | &bootblock, |
| 704 | param.baseaddress, |
| 705 | param.headeroffset, |
| 706 | param.cbfsoffset); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 707 | buffer_delete(&bootblock); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 708 | return ret; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 711 | static int cbfs_layout(void) |
| 712 | { |
| 713 | const struct fmap *fmap = partitioned_file_get_fmap(param.image_file); |
| 714 | if (!fmap) { |
| 715 | LOG("This is a legacy image composed entirely of a single CBFS.\n"); |
| 716 | return 1; |
| 717 | } |
| 718 | |
| 719 | printf("This image contains the following sections that can be %s with this tool:\n", |
| 720 | param.show_immutable ? "accessed" : "manipulated"); |
| 721 | puts(""); |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 722 | for (unsigned i = 0; i < fmap->nareas; ++i) { |
| 723 | const struct fmap_area *current = fmap->areas + i; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 724 | |
| 725 | bool readonly = partitioned_file_fmap_count(param.image_file, |
| 726 | partitioned_file_fmap_select_children_of, current) || |
| 727 | region_is_flashmap((const char *)current->name); |
| 728 | if (!param.show_immutable && readonly) |
| 729 | continue; |
| 730 | |
| 731 | printf("'%s'", current->name); |
| 732 | |
| 733 | // Detect consecutive sections that describe the same region and |
| 734 | // show them as aliases. This cannot find equivalent entries |
| 735 | // that aren't adjacent; however, fmaptool doesn't generate |
| 736 | // FMAPs with such sections, so this convenience feature works |
| 737 | // for all but the strangest manually created FMAP binaries. |
| 738 | // TODO: This could be done by parsing the FMAP into some kind |
| 739 | // of tree that had duplicate lists in addition to child lists, |
| 740 | // which would allow covering that weird, unlikely case as well. |
| 741 | unsigned lookahead; |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 742 | for (lookahead = 1; i + lookahead < fmap->nareas; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 743 | ++lookahead) { |
| 744 | const struct fmap_area *consecutive = |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 745 | fmap->areas + i + lookahead; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 746 | if (consecutive->offset != current->offset || |
| 747 | consecutive->size != current->size) |
| 748 | break; |
| 749 | printf(", '%s'", consecutive->name); |
| 750 | } |
| 751 | if (lookahead > 1) |
| 752 | fputs(" are aliases for the same region", stdout); |
| 753 | |
| 754 | const char *qualifier = ""; |
| 755 | if (readonly) |
| 756 | qualifier = "read-only, "; |
| 757 | else if (region_is_modern_cbfs((const char *)current->name)) |
| 758 | qualifier = "CBFS, "; |
| 759 | printf(" (%ssize %u)\n", qualifier, current->size); |
| 760 | |
Kyösti Mälkki | 2e04259 | 2015-05-15 09:14:09 +0300 | [diff] [blame] | 761 | i += lookahead - 1; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 762 | } |
| 763 | puts(""); |
| 764 | |
| 765 | if (param.show_immutable) { |
| 766 | puts("It is at least possible to perform the read action on every section listed above."); |
| 767 | } else { |
| 768 | puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above."); |
| 769 | puts("To see the image's read-only sections as well, rerun with the -w option."); |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 775 | static int cbfs_print(void) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 776 | { |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 777 | struct cbfs_image image; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 778 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 779 | param.headeroffset)) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 780 | return 1; |
Hung-Te Lin | 3bb035b | 2013-01-29 02:15:49 +0800 | [diff] [blame] | 781 | cbfs_print_directory(&image); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 782 | return 0; |
| 783 | } |
| 784 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 785 | static int cbfs_extract(void) |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 786 | { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 787 | if (!param.filename) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 788 | ERROR("You need to specify -f/--filename.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 789 | return 1; |
| 790 | } |
| 791 | |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 792 | if (!param.name) { |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 793 | ERROR("You need to specify -n/--name.\n"); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 794 | return 1; |
| 795 | } |
| 796 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 797 | struct cbfs_image image; |
| 798 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 799 | param.headeroffset)) |
| 800 | return 1; |
| 801 | |
Aaron Durbin | 1762502 | 2015-10-27 13:17:52 -0500 | [diff] [blame] | 802 | return cbfs_export_entry(&image, param.name, param.filename, |
| 803 | param.arch); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static int cbfs_write(void) |
| 807 | { |
| 808 | if (!param.filename) { |
| 809 | ERROR("You need to specify a valid input -f/--file.\n"); |
| 810 | return 1; |
| 811 | } |
| 812 | if (!partitioned_file_is_partitioned(param.image_file)) { |
| 813 | 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] | 814 | return 1; |
| 815 | } |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 816 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 817 | if (region_is_modern_cbfs(param.region_name)) { |
| 818 | ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n", |
| 819 | param.region_name); |
| 820 | return 1; |
| 821 | } |
| 822 | |
| 823 | struct buffer new_content; |
| 824 | if (buffer_from_file(&new_content, param.filename)) |
| 825 | return 1; |
| 826 | |
| 827 | if (buffer_check_magic(&new_content, FMAP_SIGNATURE, |
| 828 | strlen(FMAP_SIGNATURE))) { |
| 829 | ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n", |
| 830 | param.filename); |
| 831 | buffer_delete(&new_content); |
| 832 | return 1; |
| 833 | } |
| 834 | if (buffer_check_magic(&new_content, CBFS_FILE_MAGIC, |
| 835 | strlen(CBFS_FILE_MAGIC))) { |
| 836 | ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n", |
| 837 | param.filename); |
| 838 | buffer_delete(&new_content); |
| 839 | return 1; |
| 840 | } |
| 841 | |
| 842 | unsigned offset = 0; |
| 843 | if (param.fill_partial_upward && param.fill_partial_downward) { |
| 844 | ERROR("You may only specify one of -u and -d.\n"); |
| 845 | buffer_delete(&new_content); |
| 846 | return 1; |
| 847 | } else if (!param.fill_partial_upward && !param.fill_partial_downward) { |
| 848 | if (new_content.size != param.image_region->size) { |
| 849 | 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", |
| 850 | new_content.size, param.image_region->size); |
| 851 | buffer_delete(&new_content); |
| 852 | return 1; |
| 853 | } |
| 854 | } else { |
| 855 | if (new_content.size > param.image_region->size) { |
| 856 | ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n", |
| 857 | new_content.size, param.image_region->size); |
| 858 | buffer_delete(&new_content); |
| 859 | return 1; |
| 860 | } |
| 861 | WARN("Written area will abut %s of target region: any unused space will keep its current contents\n", |
| 862 | param.fill_partial_upward ? "bottom" : "top"); |
| 863 | if (param.fill_partial_downward) |
| 864 | offset = param.image_region->size - new_content.size; |
| 865 | } |
| 866 | |
| 867 | memcpy(param.image_region->data + offset, new_content.data, |
| 868 | new_content.size); |
| 869 | buffer_delete(&new_content); |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static int cbfs_read(void) |
| 874 | { |
| 875 | if (!param.filename) { |
| 876 | ERROR("You need to specify a valid output -f/--file.\n"); |
| 877 | return 1; |
| 878 | } |
| 879 | if (!partitioned_file_is_partitioned(param.image_file)) { |
| 880 | ERROR("This operation isn't valid on legacy images having CBFS master headers\n"); |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | return buffer_write_file(param.image_region, param.filename); |
Aurelien Guillaume | fe7d6b9 | 2011-01-13 09:09:21 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 887 | static int cbfs_update_fit(void) |
| 888 | { |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 889 | if (!param.name) { |
| 890 | ERROR("You need to specify -n/--name.\n"); |
| 891 | return 1; |
| 892 | } |
| 893 | |
| 894 | if (param.fit_empty_entries <= 0) { |
| 895 | ERROR("Invalid number of fit entries " |
| 896 | "(-x/--empty-fits): %d\n", param.fit_empty_entries); |
| 897 | return 1; |
| 898 | } |
| 899 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 900 | struct buffer bootblock; |
Patrick Georgi | 6dd99fc | 2015-09-19 14:04:45 +0200 | [diff] [blame] | 901 | // The bootblock is part of the CBFS on x86 |
| 902 | buffer_clone(&bootblock, param.image_region); |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 903 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 904 | struct cbfs_image image; |
| 905 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 906 | param.headeroffset)) |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 907 | return 1; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 908 | |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 909 | if (fit_update_table(&bootblock, &image, param.name, |
| 910 | param.fit_empty_entries, convert_to_from_top_aligned)) |
| 911 | return 1; |
| 912 | |
| 913 | // The region to be written depends on the type of image, so we write it |
| 914 | // here rather than having main() write the CBFS region back as usual. |
| 915 | return !partitioned_file_write_region(param.image_file, &bootblock); |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 916 | } |
| 917 | |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 918 | static int cbfs_copy(void) |
| 919 | { |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 920 | if (!param.copyoffset_assigned) { |
Patrick Georgi | 821bcd6 | 2015-10-16 13:37:21 +0200 | [diff] [blame] | 921 | ERROR("You need to specify -D/--copy-offset.\n"); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 922 | return 1; |
| 923 | } |
| 924 | |
| 925 | if (!param.size) { |
| 926 | ERROR("You need to specify -s/--size.\n"); |
| 927 | return 1; |
| 928 | } |
| 929 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 930 | struct cbfs_image image; |
| 931 | if (cbfs_image_from_buffer(&image, param.image_region, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 932 | param.headeroffset)) |
| 933 | return 1; |
| 934 | |
| 935 | if (!cbfs_is_legacy_cbfs(&image)) { |
| 936 | ERROR("This operation is only valid on legacy images having CBFS master headers\n"); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 937 | return 1; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 938 | } |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 939 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 940 | return cbfs_copy_instance(&image, param.copyoffset, param.size); |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Stefan Reinauer | a1e4824 | 2011-10-21 14:24:57 -0700 | [diff] [blame] | 943 | static const struct command commands[] = { |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 944 | {"add", "H:r:f:n:t:c:b:a:vA:h?", cbfs_add, true, true}, |
| 945 | {"add-flat-binary", "H:r:f:n:l:e:c:b:vA:h?", cbfs_add_flat_binary, true, |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 946 | true}, |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 947 | {"add-payload", "H:r:f:n:t:c:b:C:I:vA:h?", cbfs_add_payload, true, true}, |
| 948 | {"add-stage", "a:H:r:f:n:t:c:b:P:S:yvA:h?", cbfs_add_stage, true, true}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 949 | {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true}, |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 950 | {"add-master-header", "H:r:vh?", cbfs_add_master_header, true, true}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 951 | {"copy", "H:D:s:h?", cbfs_copy, true, true}, |
Patrick Georgi | 45acb34 | 2015-07-14 22:18:23 +0200 | [diff] [blame] | 952 | {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true}, |
Aaron Durbin | 1762502 | 2015-10-27 13:17:52 -0500 | [diff] [blame] | 953 | {"extract", "H:r:m:n:f:vh?", cbfs_extract, true, false}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 954 | {"layout", "wvh?", cbfs_layout, false, false}, |
| 955 | {"print", "H:r:vh?", cbfs_print, true, false}, |
| 956 | {"read", "r:f:vh?", cbfs_read, true, false}, |
| 957 | {"remove", "H:r:n:vh?", cbfs_remove, true, true}, |
Sol Boucher | 32532ac | 2015-05-06 14:44:40 -0700 | [diff] [blame] | 958 | {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false}, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 959 | {"write", "r:f:udvh?", cbfs_write, true, true}, |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 960 | }; |
| 961 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 962 | static struct option long_options[] = { |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 963 | {"alignment", required_argument, 0, 'a' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 964 | {"base-address", required_argument, 0, 'b' }, |
| 965 | {"bootblock", required_argument, 0, 'B' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 966 | {"cmdline", required_argument, 0, 'C' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 967 | {"compression", required_argument, 0, 'c' }, |
| 968 | {"copy-offset", required_argument, 0, 'D' }, |
| 969 | {"empty-fits", required_argument, 0, 'x' }, |
| 970 | {"entry-point", required_argument, 0, 'e' }, |
| 971 | {"file", required_argument, 0, 'f' }, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 972 | {"fill-downward", no_argument, 0, 'd' }, |
| 973 | {"fill-upward", no_argument, 0, 'u' }, |
| 974 | {"flashmap", required_argument, 0, 'M' }, |
| 975 | {"fmap-regions", required_argument, 0, 'r' }, |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 976 | {"hash-algorithm",required_argument, 0, 'A' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 977 | {"header-offset", required_argument, 0, 'H' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 978 | {"help", no_argument, 0, 'h' }, |
Vadim Bendebury | 45e5997 | 2014-12-23 15:59:57 -0800 | [diff] [blame] | 979 | {"ignore-sec", required_argument, 0, 'S' }, |
| 980 | {"initrd", required_argument, 0, 'I' }, |
| 981 | {"int", required_argument, 0, 'i' }, |
| 982 | {"load-address", required_argument, 0, 'l' }, |
| 983 | {"machine", required_argument, 0, 'm' }, |
| 984 | {"name", required_argument, 0, 'n' }, |
| 985 | {"offset", required_argument, 0, 'o' }, |
| 986 | {"page-size", required_argument, 0, 'P' }, |
| 987 | {"size", required_argument, 0, 's' }, |
| 988 | {"top-aligned", required_argument, 0, 'T' }, |
| 989 | {"type", required_argument, 0, 't' }, |
| 990 | {"verbose", no_argument, 0, 'v' }, |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 991 | {"with-readonly", no_argument, 0, 'w' }, |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 992 | {"xip", no_argument, 0, 'y' }, |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 993 | {NULL, 0, 0, 0 } |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 994 | }; |
| 995 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 996 | static int dispatch_command(struct command command) |
| 997 | { |
| 998 | if (command.accesses_region) { |
| 999 | assert(param.image_file); |
| 1000 | |
| 1001 | if (partitioned_file_is_partitioned(param.image_file)) { |
| 1002 | LOG("Performing operation on '%s' region...\n", |
| 1003 | param.region_name); |
| 1004 | } |
| 1005 | if (!partitioned_file_read_region(param.image_region, |
| 1006 | param.image_file, param.region_name)) { |
| 1007 | ERROR("The image will be left unmodified.\n"); |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
| 1011 | if (command.modifies_region) { |
| 1012 | // We (intentionally) don't support overwriting the FMAP |
| 1013 | // section. If you find yourself wanting to do this, |
| 1014 | // consider creating a new image rather than performing |
| 1015 | // whatever hacky transformation you were planning. |
| 1016 | if (region_is_flashmap(param.region_name)) { |
| 1017 | ERROR("Image region '%s' is read-only because it contains the FMAP.\n", |
| 1018 | param.region_name); |
| 1019 | ERROR("The image will be left unmodified.\n"); |
| 1020 | return 1; |
| 1021 | } |
| 1022 | // We don't allow writing raw data to regions that |
| 1023 | // contain nested regions, since doing so would |
| 1024 | // overwrite all such subregions. |
| 1025 | if (partitioned_file_region_contains_nested( |
| 1026 | param.image_file, param.region_name)) { |
| 1027 | ERROR("Image region '%s' is read-only because it contains nested regions.\n", |
| 1028 | param.region_name); |
| 1029 | ERROR("The image will be left unmodified.\n"); |
| 1030 | return 1; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | if (command.function()) { |
| 1036 | if (partitioned_file_is_partitioned(param.image_file)) { |
| 1037 | ERROR("Failed while operating on '%s' region!\n", |
| 1038 | param.region_name); |
| 1039 | ERROR("The image will be left unmodified.\n"); |
| 1040 | } |
| 1041 | return 1; |
| 1042 | } |
| 1043 | |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1047 | static void usage(char *name) |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1048 | { |
| 1049 | printf |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 1050 | ("cbfstool: Management utility for CBFS formatted ROM images\n\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1051 | "USAGE:\n" " %s [-h]\n" |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1052 | " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1053 | " -H header_offset Do not search for header; use this offset*\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 1054 | " -T Output top-aligned memory address\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1055 | " -u Accept short data; fill upward/from bottom\n" |
| 1056 | " -d Accept short data; fill downward/from top\n" |
| 1057 | " -v Provide verbose output\n" |
| 1058 | " -h Display this help message\n\n" |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1059 | "COMMANDs:\n" |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1060 | " add [-r image,regions] -f FILE -n NAME -t TYPE [-A hash] \\\n" |
Alexandru Gagniuc | 38bc916 | 2015-09-07 00:05:44 -0700 | [diff] [blame] | 1061 | " [-c compression] [-b base-address | -a alignment] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1062 | "Add a component\n" |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1063 | " add-payload [-r image,regions] -f FILE -n NAME [-A hash] \\\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1064 | " [-c compression] [-b base-address] \\\n" |
| 1065 | " (linux specific: [-C cmdline] [-I initrd]) " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1066 | "Add a payload to the ROM\n" |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1067 | " add-stage [-r image,regions] -f FILE -n NAME [-A hash] \\\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1068 | " [-c compression] [-b base] [-S section-to-ignore] \\\n" |
| 1069 | " [-a alignment] [-y|--xip] [-P page-size] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1070 | "Add a stage to the ROM\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1071 | " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n" |
| 1072 | " [-A hash] -l load-address -e entry-point \\\n" |
| 1073 | " [-c compression] [-b base] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1074 | "Add a 32bit flat mode binary\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1075 | " add-int [-r image,regions] -i INTEGER -n NAME [-b base] " |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1076 | "Add a raw 64-bit integer value\n" |
Patrick Georgi | 59e52b9 | 2015-09-10 15:28:27 +0200 | [diff] [blame] | 1077 | " add-master-header [-r image,regions] " |
| 1078 | "Add a legacy CBFS master header\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1079 | " remove [-r image,regions] -n NAME " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1080 | "Remove a component\n" |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1081 | " copy -D new_header_offset -s region size \\\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1082 | " [-H source header offset] " |
| 1083 | "Create a copy (duplicate) cbfs instance*\n" |
| 1084 | " create -m ARCH -s size [-b bootblock offset] \\\n" |
Patrick Georgi | 83c2d12 | 2015-08-26 10:40:00 +0200 | [diff] [blame] | 1085 | " [-o CBFS offset] [-H header offset] [-B bootblock] " |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1086 | "Create a legacy ROM file with CBFS master header*\n" |
| 1087 | " create -M flashmap [-r list,of,regions,containing,cbfses] " |
| 1088 | "Create a new-style partitioned firmware image\n" |
| 1089 | " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n" |
| 1090 | " [-a align] [-T] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1091 | "Find a place for a file of that size\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1092 | " layout [-w] " |
| 1093 | "List mutable (or, with -w, readable) image regions\n" |
| 1094 | " print [-r image,regions] " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1095 | "Show the contents of the ROM\n" |
Aaron Durbin | 1762502 | 2015-10-27 13:17:52 -0500 | [diff] [blame] | 1096 | " extract [-r image,regions] [-m ARCH] -n NAME -f FILE " |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1097 | "Extracts a raw payload from ROM\n" |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1098 | " write -r image,regions -f file [-u | -d] " |
| 1099 | "Write file into same-size [or larger] raw region\n" |
| 1100 | " read [-r fmap-region] -f file " |
| 1101 | "Extract raw region contents into binary file\n" |
| 1102 | " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n" |
Martin Roth | 0eae364 | 2015-12-04 11:27:11 -0700 | [diff] [blame] | 1103 | " -x EMTPY_FIT_ENTRIES " |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 1104 | "Updates the FIT table with microcode entries\n" |
Peter Stuge | b347e0d | 2011-01-17 05:02:09 +0000 | [diff] [blame] | 1105 | "\n" |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 1106 | "OFFSETs:\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 1107 | " Numbers accompanying -b, -H, and -o switches* may be provided\n" |
| 1108 | " in two possible formats: if their value is greater than\n" |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 1109 | " 0x80000000, they are interpreted as a top-aligned x86 memory\n" |
| 1110 | " address; otherwise, they are treated as an offset into flash.\n" |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 1111 | "ARCHes:\n" |
Paul Burton | 3318692 | 2014-06-13 23:56:45 +0100 | [diff] [blame] | 1112 | " arm64, arm, mips, x86\n" |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1113 | "TYPEs:\n", name, name |
| 1114 | ); |
Stefan Reinauer | 0704058 | 2010-04-24 21:24:06 +0000 | [diff] [blame] | 1115 | print_supported_filetypes(); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1116 | |
| 1117 | printf( |
| 1118 | "\n* Note that these actions and switches are only valid when\n" |
| 1119 | " working with legacy images whose structure is described\n" |
| 1120 | " primarily by a CBFS master header. New-style images, in\n" |
| 1121 | " contrast, exclusively make use of an FMAP to describe their\n" |
| 1122 | " layout: this must minimally contain an '%s' section\n" |
| 1123 | " specifying the location of this FMAP itself and a '%s'\n" |
| 1124 | " section describing the primary CBFS. It should also be noted\n" |
| 1125 | " that, when working with such images, the -F and -r switches\n" |
| 1126 | " default to '%s' for convenience, and both the -b switch to\n" |
| 1127 | " CBFS operations and the output of the locate action become\n" |
Sol Boucher | 67d5998 | 2015-05-07 02:39:22 -0700 | [diff] [blame] | 1128 | " relative to the selected CBFS region's lowest address.\n" |
| 1129 | " The one exception to this rule is the top-aligned address,\n" |
| 1130 | " which is always relative to the end of the entire image\n" |
| 1131 | " rather than relative to the local region; this is true for\n" |
| 1132 | " for both input (sufficiently large) and output (-T) data.\n", |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1133 | SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS, |
| 1134 | SECTION_NAME_PRIMARY_CBFS |
| 1135 | ); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | int main(int argc, char **argv) |
| 1139 | { |
Mathias Krause | 41c229c | 2012-07-17 21:17:15 +0200 | [diff] [blame] | 1140 | size_t i; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1141 | int c; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (argc < 3) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1144 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1145 | return 1; |
| 1146 | } |
| 1147 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1148 | char *image_name = argv[1]; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1149 | char *cmd = argv[2]; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1150 | optind += 2; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1151 | |
| 1152 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
| 1153 | if (strcmp(cmd, commands[i].name) != 0) |
| 1154 | continue; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1155 | |
| 1156 | while (1) { |
| 1157 | char *suffix = NULL; |
| 1158 | int option_index = 0; |
| 1159 | |
| 1160 | c = getopt_long(argc, argv, commands[i].optstring, |
| 1161 | long_options, &option_index); |
| 1162 | if (c == -1) |
| 1163 | break; |
| 1164 | |
| 1165 | /* filter out illegal long options */ |
zbao | 062730d | 2013-01-08 10:10:16 +0800 | [diff] [blame] | 1166 | if (strchr(commands[i].optstring, c) == NULL) { |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1167 | /* TODO maybe print actual long option instead */ |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1168 | ERROR("%s: invalid option -- '%c'\n", |
| 1169 | argv[0], c); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1170 | c = '?'; |
| 1171 | } |
| 1172 | |
| 1173 | switch(c) { |
| 1174 | case 'n': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1175 | param.name = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1176 | break; |
| 1177 | case 't': |
| 1178 | if (intfiletype(optarg) != ((uint64_t) - 1)) |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1179 | param.type = intfiletype(optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1180 | else |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1181 | param.type = strtoul(optarg, NULL, 0); |
| 1182 | if (param.type == 0) |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1183 | WARN("Unknown type '%s' ignored\n", |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1184 | optarg); |
| 1185 | break; |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1186 | case 'c': { |
| 1187 | int algo = cbfs_parse_comp_algo(optarg); |
| 1188 | if (algo >= 0) |
| 1189 | param.compression = algo; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1190 | else |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1191 | WARN("Unknown compression '%s' ignored.\n", |
| 1192 | optarg); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1193 | break; |
Sol Boucher | ec42486 | 2015-05-07 21:00:05 -0700 | [diff] [blame] | 1194 | } |
Patrick Georgi | 89f2034 | 2015-10-01 15:54:04 +0200 | [diff] [blame] | 1195 | case 'A': { |
| 1196 | int algo = cbfs_parse_hash_algo(optarg); |
| 1197 | if (algo >= 0) |
| 1198 | param.hash = algo; |
| 1199 | else { |
| 1200 | ERROR("Unknown hash algorithm '%s'.\n", |
| 1201 | optarg); |
| 1202 | return 1; |
| 1203 | } |
| 1204 | break; |
| 1205 | } |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1206 | case 'M': |
| 1207 | param.fmap = optarg; |
| 1208 | break; |
| 1209 | case 'r': |
| 1210 | param.region_name = optarg; |
| 1211 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1212 | case 'b': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1213 | param.baseaddress = strtoul(optarg, NULL, 0); |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1214 | // baseaddress may be zero on non-x86, so we |
| 1215 | // need an explicit "baseaddress_assigned". |
| 1216 | param.baseaddress = strtoul(optarg, NULL, 0); |
| 1217 | param.baseaddress_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1218 | break; |
| 1219 | case 'l': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1220 | param.loadaddress = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1221 | break; |
| 1222 | case 'e': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1223 | param.entrypoint = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1224 | break; |
| 1225 | case 's': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1226 | param.size = strtoul(optarg, &suffix, 0); |
Stefan Reinauer | 3a043c4 | 2015-09-15 17:37:14 -0700 | [diff] [blame] | 1227 | if (tolower((int)suffix[0])=='k') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1228 | param.size *= 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1229 | } |
Stefan Reinauer | 3a043c4 | 2015-09-15 17:37:14 -0700 | [diff] [blame] | 1230 | if (tolower((int)suffix[0])=='m') { |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1231 | param.size *= 1024 * 1024; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1232 | } |
Patrick Georgi | e887ca5 | 2014-08-09 17:44:39 +0200 | [diff] [blame] | 1233 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1234 | case 'B': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1235 | param.bootblock = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1236 | break; |
Hung-Te Lin | f56c73f | 2013-01-29 09:45:12 +0800 | [diff] [blame] | 1237 | case 'H': |
| 1238 | param.headeroffset = strtoul( |
| 1239 | optarg, NULL, 0); |
| 1240 | param.headeroffset_assigned = 1; |
| 1241 | break; |
Vadim Bendebury | 5e273a4 | 2014-12-23 19:26:54 -0800 | [diff] [blame] | 1242 | case 'D': |
| 1243 | param.copyoffset = strtoul(optarg, NULL, 0); |
| 1244 | param.copyoffset_assigned = 1; |
Vadim Bendebury | 1161473 | 2015-01-08 16:36:35 -0800 | [diff] [blame] | 1245 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1246 | case 'a': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1247 | param.alignment = strtoul(optarg, NULL, 0); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1248 | break; |
Hung-Te Lin | e919837 | 2013-03-19 12:17:12 +0800 | [diff] [blame] | 1249 | case 'P': |
| 1250 | param.pagesize = strtoul(optarg, NULL, 0); |
| 1251 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1252 | case 'o': |
Julius Werner | efcee76 | 2014-11-10 13:14:24 -0800 | [diff] [blame] | 1253 | param.cbfsoffset = strtoul(optarg, NULL, 0); |
| 1254 | param.cbfsoffset_assigned = 1; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1255 | break; |
| 1256 | case 'f': |
Hung-Te Lin | d173962 | 2013-01-28 14:23:49 +0800 | [diff] [blame] | 1257 | param.filename = optarg; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1258 | break; |
Peter Stuge | 3bfd5b8 | 2013-07-09 19:39:13 +0200 | [diff] [blame] | 1259 | case 'i': |
| 1260 | param.u64val = strtoull(optarg, NULL, 0); |
| 1261 | break; |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1262 | case 'u': |
| 1263 | param.fill_partial_upward = true; |
| 1264 | break; |
| 1265 | case 'd': |
| 1266 | param.fill_partial_downward = true; |
| 1267 | break; |
| 1268 | case 'w': |
| 1269 | param.show_immutable = true; |
Hung-Te Lin | 215d1d7 | 2013-01-29 03:46:02 +0800 | [diff] [blame] | 1270 | break; |
Aaron Durbin | 6b0d0d6 | 2012-12-14 17:16:21 -0600 | [diff] [blame] | 1271 | case 'x': |
| 1272 | param.fit_empty_entries = strtol(optarg, NULL, 0); |
| 1273 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1274 | case 'v': |
| 1275 | verbose++; |
| 1276 | break; |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 1277 | case 'm': |
Alexandru Gagniuc | 35850ae | 2014-02-02 22:37:28 -0600 | [diff] [blame] | 1278 | param.arch = string_to_arch(optarg); |
David Hendricks | 90ca3b6 | 2012-11-16 14:48:22 -0800 | [diff] [blame] | 1279 | break; |
Patrick Georgi | de36d33 | 2013-08-27 20:22:21 +0200 | [diff] [blame] | 1280 | case 'I': |
| 1281 | param.initrd = optarg; |
| 1282 | break; |
| 1283 | case 'C': |
| 1284 | param.cmdline = optarg; |
| 1285 | break; |
Furquan Shaikh | 405304a | 2014-10-30 11:44:20 -0700 | [diff] [blame] | 1286 | case 'S': |
| 1287 | param.ignore_section = optarg; |
| 1288 | break; |
Aaron Durbin | 4be1674 | 2015-09-15 17:00:23 -0500 | [diff] [blame] | 1289 | case 'y': |
| 1290 | param.stage_xip = true; |
| 1291 | break; |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1292 | case 'h': |
| 1293 | case '?': |
| 1294 | usage(argv[0]); |
| 1295 | return 1; |
| 1296 | default: |
| 1297 | break; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1301 | if (commands[i].function == cbfs_create) { |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1302 | if (param.fmap) { |
| 1303 | struct buffer flashmap; |
| 1304 | if (buffer_from_file(&flashmap, param.fmap)) |
| 1305 | return 1; |
| 1306 | param.image_file = partitioned_file_create( |
| 1307 | image_name, &flashmap); |
| 1308 | buffer_delete(&flashmap); |
| 1309 | } else if (param.size) { |
| 1310 | param.image_file = partitioned_file_create_flat( |
| 1311 | image_name, param.size); |
| 1312 | } else { |
| 1313 | 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] | 1314 | return 1; |
| 1315 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1316 | } else { |
| 1317 | param.image_file = |
Patrick Georgi | 2f953d3 | 2015-09-11 18:34:39 +0200 | [diff] [blame] | 1318 | partitioned_file_reopen(image_name); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1319 | } |
| 1320 | if (!param.image_file) |
| 1321 | return 1; |
| 1322 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1323 | unsigned num_regions = 1; |
| 1324 | for (const char *list = strchr(param.region_name, ','); list; |
| 1325 | list = strchr(list + 1, ',')) |
| 1326 | ++num_regions; |
| 1327 | |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1328 | // If the action needs to read an image region, as indicated by |
| 1329 | // having accesses_region set in its command struct, that |
| 1330 | // region's buffer struct will be stored here and the client |
| 1331 | // will receive a pointer to it via param.image_region. It |
| 1332 | // need not write the buffer back to the image file itself, |
| 1333 | // since this behavior can be requested via its modifies_region |
| 1334 | // field. Additionally, it should never free the region buffer, |
| 1335 | // as that is performed automatically once it completes. |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1336 | struct buffer image_regions[num_regions]; |
| 1337 | memset(image_regions, 0, sizeof(image_regions)); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1338 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1339 | bool seen_primary_cbfs = false; |
| 1340 | char region_name_scratch[strlen(param.region_name) + 1]; |
| 1341 | strcpy(region_name_scratch, param.region_name); |
| 1342 | param.region_name = strtok(region_name_scratch, ","); |
| 1343 | for (unsigned region = 0; region < num_regions; ++region) { |
| 1344 | if (!param.region_name) { |
| 1345 | ERROR("Encountered illegal degenerate region name in -r list\n"); |
| 1346 | ERROR("The image will be left unmodified.\n"); |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1347 | partitioned_file_close(param.image_file); |
| 1348 | return 1; |
| 1349 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1350 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1351 | if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS) |
| 1352 | == 0) |
| 1353 | seen_primary_cbfs = true; |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1354 | |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1355 | param.image_region = image_regions + region; |
| 1356 | if (dispatch_command(commands[i])) { |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1357 | partitioned_file_close(param.image_file); |
| 1358 | return 1; |
| 1359 | } |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1360 | |
| 1361 | param.region_name = strtok(NULL, ","); |
| 1362 | } |
| 1363 | |
| 1364 | if (commands[i].function == cbfs_create && !seen_primary_cbfs) { |
| 1365 | ERROR("The creation -r list must include the mandatory '%s' section.\n", |
| 1366 | SECTION_NAME_PRIMARY_CBFS); |
| 1367 | ERROR("The image will be left unmodified.\n"); |
| 1368 | partitioned_file_close(param.image_file); |
| 1369 | return 1; |
| 1370 | } |
| 1371 | |
| 1372 | if (commands[i].modifies_region) { |
| 1373 | assert(param.image_file); |
| 1374 | assert(commands[i].accesses_region); |
| 1375 | for (unsigned region = 0; region < num_regions; |
| 1376 | ++region) { |
| 1377 | |
| 1378 | if (!partitioned_file_write_region( |
| 1379 | param.image_file, |
| 1380 | image_regions + region)) { |
| 1381 | partitioned_file_close( |
| 1382 | param.image_file); |
| 1383 | return 1; |
| 1384 | } |
| 1385 | } |
Sol Boucher | e3260a0 | 2015-03-25 13:40:08 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | partitioned_file_close(param.image_file); |
Sol Boucher | 67a0a86 | 2015-03-18 12:36:27 -0700 | [diff] [blame] | 1389 | return 0; |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Hung-Te Lin | 4d87d4e | 2013-01-28 14:39:43 +0800 | [diff] [blame] | 1392 | ERROR("Unknown command '%s'.\n", cmd); |
Stefan Reinauer | 6321758 | 2012-10-29 16:52:36 -0700 | [diff] [blame] | 1393 | usage(argv[0]); |
Stefan Reinauer | 3fec29c | 2009-09-22 15:58:19 +0000 | [diff] [blame] | 1394 | return 1; |
| 1395 | } |