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