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