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