blob: 9b654b11b2accfe69933ab71d648e78746273d34 [file] [log] [blame]
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00001/*
Patrick Georgib7b56dd82009-09-14 13:29:27 +00002 * cbfstool, CLI utility for CBFS file manipulation
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00003 *
Patrick Georgib7b56dd82009-09-14 13:29:27 +00004 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
David Hendricks90ca3b62012-11-16 14:48:22 -08006 * Copyright (C) 2012 Google, Inc.
Werner Zehe9995f12016-01-14 13:22:37 +01007 * Copyright (C) 2016 Siemens AG
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00008 *
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. Minnich5d01ec02009-03-31 11:57:36 +000017 */
18
Patrick Georgib7b56dd82009-09-14 13:29:27 +000019#include <stdio.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070020#include <stdlib.h>
Stefan Reinauer336daa72009-12-21 15:09:01 +000021#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080022#include <strings.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070023#include <ctype.h>
Stefan Reinauer63217582012-10-29 16:52:36 -070024#include <unistd.h>
25#include <getopt.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000026#include "common.h"
27#include "cbfs.h"
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080028#include "cbfs_image.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070029#include "cbfs_sections.h"
Aaron Durbinfacf1492017-12-18 14:50:22 -070030#include "elfparsing.h"
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060031#include "fit.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070032#include "partitioned_file.h"
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -070033#include <commonlib/fsp.h>
Nico Huber607796a2017-01-17 13:01:25 +010034#include <commonlib/endian.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000035
Sol Boucher32532ac2015-05-06 14:44:40 -070036#define SECTION_WITH_FIT_TABLE "BOOTBLOCK"
37
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000038struct command {
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000039 const char *name;
Stefan Reinauer63217582012-10-29 16:52:36 -070040 const char *optstring;
41 int (*function) (void);
Sol Bouchere3260a02015-03-25 13:40:08 -070042 // Whether to populate param.image_region before invoking function
43 bool accesses_region;
Vadim Bendebury75599462015-12-09 09:39:31 -080044 // 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 Bouchere3260a02015-03-25 13:40:08 -070048 bool modifies_region;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000049};
50
Hung-Te Lind1739622013-01-28 14:23:49 +080051static struct param {
Sol Bouchere3260a02015-03-25 13:40:08 -070052 partitioned_file_t *image_file;
53 struct buffer *image_region;
Sol Boucher67a0a862015-03-18 12:36:27 -070054 const char *name;
55 const char *filename;
56 const char *fmap;
57 const char *region_name;
Patrick Georgibd0bb232015-11-20 21:48:18 +010058 const char *source_region;
Sol Boucher67a0a862015-03-18 12:36:27 -070059 const char *bootblock;
60 const char *ignore_section;
Peter Stuge3bfd5b82013-07-09 19:39:13 +020061 uint64_t u64val;
Hung-Te Lind1739622013-01-28 14:23:49 +080062 uint32_t type;
63 uint32_t baseaddress;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080064 uint32_t baseaddress_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080065 uint32_t loadaddress;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080066 uint32_t headeroffset;
67 uint32_t headeroffset_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080068 uint32_t entrypoint;
69 uint32_t size;
70 uint32_t alignment;
Hung-Te Line9198372013-03-19 12:17:12 +080071 uint32_t pagesize;
Julius Wernerefcee762014-11-10 13:14:24 -080072 uint32_t cbfsoffset;
73 uint32_t cbfsoffset_assigned;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060074 uint32_t arch;
Daisuke Nojiriff906fb2017-10-30 17:38:04 -070075 uint32_t padding;
Patrick Georgi16b3e4b2016-12-12 18:38:01 +010076 bool u64val_assigned;
Sol Boucher67a0a862015-03-18 12:36:27 -070077 bool fill_partial_upward;
78 bool fill_partial_downward;
79 bool show_immutable;
Aaron Durbin4be16742015-09-15 17:00:23 -050080 bool stage_xip;
Werner Zehe9995f12016-01-14 13:22:37 +010081 bool autogen_attr;
Aaron Durbin5dc628a2016-01-26 15:35:34 -060082 bool machine_parseable;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060083 int fit_empty_entries;
Sol Boucher65336712015-05-07 21:00:05 -070084 enum comp_algo compression;
Patrick Georgiecaa5702017-01-11 18:38:11 +010085 int precompression;
Patrick Georgi89f20342015-10-01 15:54:04 +020086 enum vb2_hash_algorithm hash;
Patrick Georgide36d332013-08-27 20:22:21 +020087 /* for linux payloads */
88 char *initrd;
89 char *cmdline;
Patrick Georgi01fbc3a2016-10-12 16:46:13 +020090 int force;
Hung-Te Lind1739622013-01-28 14:23:49 +080091} param = {
92 /* All variables not listed are initialized as zero. */
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060093 .arch = CBFS_ARCHITECTURE_UNKNOWN,
Sol Boucher65336712015-05-07 21:00:05 -070094 .compression = CBFS_COMPRESS_NONE,
Patrick Georgi89f20342015-10-01 15:54:04 +020095 .hash = VB2_HASH_INVALID,
Vadim Bendebury458a12e2014-12-23 15:10:12 -080096 .headeroffset = ~0,
Sol Boucher67a0a862015-03-18 12:36:27 -070097 .region_name = SECTION_NAME_PRIMARY_CBFS,
Patrick Georgid9edb182016-12-06 18:55:26 +010098 .u64val = -1,
Hung-Te Lind1739622013-01-28 14:23:49 +080099};
Stefan Reinauer63217582012-10-29 16:52:36 -0700100
Sol Boucher67a0a862015-03-18 12:36:27 -0700101static 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. */
108static 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 Boucher67d59982015-05-07 02:39:22 -0700114/*
115 * Converts between offsets from the start of the specified image region and
Aaron Durbinab00d772016-05-04 16:07:15 -0500116 * "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 */
119static 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 Georgi97311192015-12-07 23:14:07 +0100131 * "top-aligned" offsets from the top of the image region. Works in either
Sol Boucher67d59982015-05-07 02:39:22 -0700132 * 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. */
135static unsigned convert_to_from_top_aligned(const struct buffer *region,
136 unsigned offset)
137{
138 assert(region);
139
Patrick Georgi97311192015-12-07 23:14:07 +0100140 /* 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 Durbinab00d772016-05-04 16:07:15 -0500148 return convert_to_from_absolute_top_aligned(region, offset);
Sol Boucher67d59982015-05-07 02:39:22 -0700149}
150
Aaron Durbinfacf1492017-12-18 14:50:22 -0700151static int do_cbfs_locate(int32_t *cbfs_addr, size_t metadata_size,
152 size_t data_size)
Aaron Durbin4be16742015-09-15 17:00:23 -0500153{
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 Durbinfacf1492017-12-18 14:50:22 -0700172 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 Durbin4be16742015-09-15 17:00:23 -0500180 }
181
Aaron Durbinfacf1492017-12-18 14:50:22 -0700182 DEBUG("File size is %zd (0x%zx)\n", data_size, data_size);
183
Aaron Durbin4be16742015-09-15 17:00:23 -0500184 /* Include cbfs_file size along with space for with name. */
185 metadata_size += cbfs_calculate_file_header_size(param.name);
Werner Zehe9995f12016-01-14 13:22:37 +0100186 /* 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 Durbin4be16742015-09-15 17:00:23 -0500193
Werner Zehc7b2b7c2016-01-22 19:43:01 +0100194 /* 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 Durbinfacf1492017-12-18 14:50:22 -0700198 int32_t address = cbfs_locate_entry(&image, data_size, param.pagesize,
Aaron Durbin4be16742015-09-15 17:00:23 -0500199 param.alignment, metadata_size);
Aaron Durbin4be16742015-09-15 17:00:23 -0500200
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 Durbin4be16742015-09-15 17:00:23 -0500207 *cbfs_addr = address;
208 return 0;
209}
210
Patrick Georgif8204972015-08-11 15:16:24 +0200211typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200212 struct cbfs_file *header);
Stefan Reinauer63217582012-10-29 16:52:36 -0700213
Sol Bouchere3260a02015-03-25 13:40:08 -0700214static int cbfs_add_integer_component(const char *name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200215 uint64_t u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800216 uint32_t offset,
217 uint32_t headeroffset) {
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200218 struct cbfs_image image;
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200219 struct cbfs_file *header = NULL;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200220 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 Bouchere3260a02015-03-25 13:40:08 -0700234 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) {
235 ERROR("Selected image region is not a CBFS.\n");
236 goto done;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200237 }
238
239 if (cbfs_get_entry(&image, name)) {
240 ERROR("'%s' already in ROM image.\n", name);
241 goto done;
242 }
243
Sol Boucher67d59982015-05-07 02:39:22 -0700244 if (IS_TOP_ALIGNED_ADDRESS(offset))
245 offset = convert_to_from_top_aligned(param.image_region,
246 -offset);
247
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200248 header = cbfs_create_file_header(CBFS_COMPONENT_RAW,
249 buffer.size, name);
Patrick Georgif5252f32015-08-25 22:27:57 +0200250 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Sol Boucher0e539312015-03-05 15:38:03 -0800251 ERROR("Failed to add %llu into ROM image as '%s'.\n",
252 (long long unsigned)u64val, name);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200253 goto done;
254 }
255
Sol Bouchere3260a02015-03-25 13:40:08 -0700256 ret = 0;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200257
258done:
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200259 free(header);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200260 buffer_delete(&buffer);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200261 return ret;
262}
263
Patrick Georgi59e52b92015-09-10 15:28:27 +0200264static 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 Durbinbb826ef2016-01-04 13:56:00 -0600271 size_t offset;
272 size_t size;
Patrick Georgi59e52b92015-09-10 15:28:27 +0200273
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 Georgi59e52b92015-09-10 15:28:27 +0200291 /* 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 Durbinbb826ef2016-01-04 13:56:00 -0600299 /* 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 Georgi59e52b92015-09-10 15:28:27 +0200304 */
Aaron Durbinbb826ef2016-01-04 13:56:00 -0600305 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 Georgi59e52b92015-09-10 15:28:27 +0200310 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 Durbin4b93a4f2015-09-21 13:10:13 -0500332 swab32(htonl(header_offset));
Patrick Georgi59e52b92015-09-10 15:28:27 +0200333
334 ret = 0;
335
336done:
337 free(header);
338 buffer_delete(&buffer);
339 return ret;
340}
341
Sol Bouchere3260a02015-03-25 13:40:08 -0700342static int cbfs_add_component(const char *filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800343 const char *name,
344 uint32_t type,
345 uint32_t offset,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800346 uint32_t headeroffset,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100347 convert_buffer_t convert)
348{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800349 if (!filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800350 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700351 return 1;
352 }
353
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800354 if (!name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800355 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700356 return 1;
357 }
358
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800359 if (type == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800360 ERROR("You need to specify a valid -t/--type.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700361 return 1;
362 }
363
Sol Bouchere3260a02015-03-25 13:40:08 -0700364 struct cbfs_image image;
Sol Boucher67a0a862015-03-18 12:36:27 -0700365 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset))
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800366 return 1;
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800367
Patrick Georgi4e54bf92015-08-11 14:35:39 +0200368 if (cbfs_get_entry(&image, name)) {
369 ERROR("'%s' already in ROM image.\n", name);
370 return 1;
371 }
372
Sol Bouchere3260a02015-03-25 13:40:08 -0700373 struct buffer buffer;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800374 if (buffer_from_file(&buffer, filename) != 0) {
375 ERROR("Could not load file '%s'.\n", filename);
Stefan Reinauer63217582012-10-29 16:52:36 -0700376 return 1;
377 }
378
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200379 struct cbfs_file *header =
380 cbfs_create_file_header(type, buffer.size, name);
Patrick Georgi4110abc2015-08-11 15:10:33 +0200381
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200382 if (convert && convert(&buffer, &offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800383 ERROR("Failed to parse file '%s'.\n", filename);
384 buffer_delete(&buffer);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000385 return 1;
Stefan Reinauerfbadc492011-10-14 12:44:14 -0700386 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700387
Patrick Georgi89f20342015-10-01 15:54:04 +0200388 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 Zehe9995f12016-01-14 13:22:37 +0100396 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 Rudolph4f5bed52018-05-02 09:44:08 +0200413 else if (type == CBFS_COMPONENT_SELF)
Werner Zehe9995f12016-01-14 13:22:37 +0100414 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 Nojiriff906fb2017-10-30 17:38:04 -0700432 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 Boucher67d59982015-05-07 02:39:22 -0700444 if (IS_TOP_ALIGNED_ADDRESS(offset))
445 offset = convert_to_from_top_aligned(param.image_region,
446 -offset);
447
Patrick Georgif5252f32015-08-25 22:27:57 +0200448 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800449 ERROR("Failed to add '%s' into ROM image.\n", filename);
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200450 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800451 buffer_delete(&buffer);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800452 return 1;
453 }
454
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200455 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800456 buffer_delete(&buffer);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000457 return 0;
458}
459
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700460static int cbfstool_convert_raw(struct buffer *buffer,
461 unused uint32_t *offset, struct cbfs_file *header)
462{
463 char *compressed;
Patrick Georgiecaa5702017-01-11 18:38:11 +0100464 int decompressed_size, compressed_size;
465 comp_func_ptr compress;
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700466
Patrick Georgiecaa5702017-01-11 18:38:11 +0100467 decompressed_size = buffer->size;
468 if (param.precompression) {
Nico Huber607796a2017-01-17 13:01:25 +0100469 param.compression = read_le32(buffer->data);
470 decompressed_size = read_le32(buffer->data + sizeof(uint32_t));
Patrick Georgiecaa5702017-01-11 18:38:11 +0100471 compressed_size = buffer->size - 8;
472 compressed = malloc(compressed_size);
473 if (!compressed)
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700474 return -1;
Patrick Georgiecaa5702017-01-11 18:38:11 +0100475 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 Nojiri8984a632015-07-09 15:07:45 -0700483
Patrick Georgiecaa5702017-01-11 18:38:11 +0100484 if (compress(buffer->data, buffer->size,
485 compressed, &compressed_size)) {
486 WARN("Compression failed - disabled\n");
487 free(compressed);
488 return 0;
489 }
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700490 }
Patrick Georgiecaa5702017-01-11 18:38:11 +0100491
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 Nojiri8984a632015-07-09 15:07:45 -0700509 return 0;
510}
511
Aaron Durbin8a414a02015-10-01 17:02:45 -0500512static 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 Durbin493ec922016-05-16 15:18:45 -0500517 int do_relocation = 1;
Aaron Durbin8a414a02015-10-01 17:02:45 -0500518
519 address = *offset;
520
Furquan Shaikh61c1a052016-05-09 11:53:34 -0700521 /*
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 Durbin493ec922016-05-16 15:18:45 -0500533 if (param.baseaddress_assigned == 0) {
534 INFO("Honoring pre-linked FSP module.\n");
535 do_relocation = 0;
536 } else {
537 address = param.baseaddress;
Furquan Shaikh61c1a052016-05-09 11:53:34 -0700538 }
539
Furquan Shaikh61c1a052016-05-09 11:53:34 -0700540 /*
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 Durbin8a414a02015-10-01 17:02:45 -0500553
Aaron Durbin493ec922016-05-16 15:18:45 -0500554 /*
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 Durbin8a414a02015-10-01 17:02:45 -0500561 /* 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 Shaikhb0c2fe02016-05-09 12:23:01 -0700568 if (fsp_component_relocate(address, buffer_get(&fsp), buffer_size(&fsp))
569 > 0) {
Aaron Durbin8a414a02015-10-01 17:02:45 -0500570 buffer_delete(buffer);
571 buffer_clone(buffer, &fsp);
572 } else {
573 buffer_delete(&fsp);
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -0700574 WARN("Invalid FSP variant.\n");
Aaron Durbin8a414a02015-10-01 17:02:45 -0500575 }
576
577 /* Let the raw path handle all the cbfs metadata logic. */
578 return cbfstool_convert_raw(buffer, offset, header);
579}
580
Patrick Georgif8204972015-08-11 15:16:24 +0200581static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200582 struct cbfs_file *header)
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600583{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800584 struct buffer output;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600585 int ret;
Aaron Durbin4be16742015-09-15 17:00:23 -0500586
587 if (param.stage_xip) {
588 int32_t address;
Aaron Durbinfacf1492017-12-18 14:50:22 -0700589 size_t data_size;
Aaron Durbin4be16742015-09-15 17:00:23 -0500590
Aaron Durbinfacf1492017-12-18 14:50:22 -0700591 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 Durbin4be16742015-09-15 17:00:23 -0500598 ERROR("Could not find location for XIP stage.\n");
599 return 1;
600 }
601
Aaron Durbinab00d772016-05-04 16:07:15 -0500602 /*
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 Durbin4be16742015-09-15 17:00:23 -0500609 *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 Gagniuc35850ae2014-02-02 22:37:28 -0600617 if (ret != 0)
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800618 return -1;
619 buffer_delete(buffer);
620 // direct assign, no dupe.
621 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200622 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800623 return 0;
624}
625
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100626static int cbfstool_convert_mkpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200627 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100628{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800629 struct buffer output;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800630 int ret;
631 /* per default, try and see if payload is an ELF binary */
Sol Boucher65336712015-05-07 21:00:05 -0700632 ret = parse_elf_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800633
Patrick Rudolph7ee05ed2018-04-26 09:35:13 +0200634 /* 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 Reinauer543a6822013-02-04 15:39:13 -0800642 if (ret != 0)
Sol Boucher65336712015-05-07 21:00:05 -0700643 ret = parse_fv_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800644
Patrick Georgide36d332013-08-27 20:22:21 +0200645 /* If it's neither ELF nor UEFI Fv, try bzImage */
646 if (ret != 0)
647 ret = parse_bzImage_to_payload(buffer, &output,
Sol Boucher65336712015-05-07 21:00:05 -0700648 param.initrd, param.cmdline, param.compression);
Patrick Georgide36d332013-08-27 20:22:21 +0200649
Stefan Reinauer543a6822013-02-04 15:39:13 -0800650 /* Not a supported payload type */
651 if (ret != 0) {
652 ERROR("Not a supported payload type (ELF / FV).\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700653 buffer_delete(buffer);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800654 return -1;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800655 }
656
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800657 buffer_delete(buffer);
658 // direct assign, no dupe.
659 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200660 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800661 return 0;
662}
663
664static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200665 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100666{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800667 struct buffer output;
668 if (parse_flat_binary_to_payload(buffer, &output,
669 param.loadaddress,
670 param.entrypoint,
Sol Boucher65336712015-05-07 21:00:05 -0700671 param.compression) != 0) {
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800672 return -1;
673 }
674 buffer_delete(buffer);
675 // direct assign, no dupe.
676 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200677 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800678 return 0;
679}
680
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800681static int cbfs_add(void)
682{
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700683 int32_t address;
Aaron Durbin8a414a02015-10-01 17:02:45 -0500684 convert_buffer_t convert;
Patrick Georgi9a17d042016-01-20 18:45:07 +0100685 uint32_t local_baseaddress = param.baseaddress;
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700686
687 if (param.alignment && param.baseaddress) {
688 ERROR("Cannot specify both alignment and base address\n");
689 return 1;
690 }
691
Aaron Durbin8a414a02015-10-01 17:02:45 -0500692 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 HAOUAS5bc61da2018-05-29 22:27:51 +0200697 if (!param.baseaddress_assigned)
Aaron Durbin8a414a02015-10-01 17:02:45 -0500698 param.alignment = 4*1024;
699 convert = cbfstool_convert_fsp;
Furquan Shaikh61c1a052016-05-09 11:53:34 -0700700 } else if (param.stage_xip) {
701 ERROR("cbfs add supports xip only for FSP component type\n");
702 return 1;
Aaron Durbin8a414a02015-10-01 17:02:45 -0500703 }
704
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700705 if (param.alignment) {
Aaron Durbin9b9d4b32015-09-15 21:30:58 -0500706 /* CBFS compression file attribute is unconditionally added. */
707 size_t metadata_sz = sizeof(struct cbfs_file_attr_compression);
Aaron Durbinfacf1492017-12-18 14:50:22 -0700708 if (do_cbfs_locate(&address, metadata_sz, 0))
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700709 return 1;
Patrick Georgi9a17d042016-01-20 18:45:07 +0100710 local_baseaddress = address;
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700711 }
712
Sol Bouchere3260a02015-03-25 13:40:08 -0700713 return cbfs_add_component(param.filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800714 param.name,
715 param.type,
Patrick Georgi9a17d042016-01-20 18:45:07 +0100716 local_baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800717 param.headeroffset,
Aaron Durbin8a414a02015-10-01 17:02:45 -0500718 convert);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800719}
720
Stefan Reinauer63217582012-10-29 16:52:36 -0700721static int cbfs_add_stage(void)
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700722{
Aaron Durbin4be16742015-09-15 17:00:23 -0500723 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 Bouchere3260a02015-03-25 13:40:08 -0700735 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800736 param.name,
737 CBFS_COMPONENT_STAGE,
738 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800739 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800740 cbfstool_convert_mkstage);
741}
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700742
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800743static int cbfs_add_payload(void)
744{
Sol Bouchere3260a02015-03-25 13:40:08 -0700745 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800746 param.name,
Patrick Rudolph4f5bed52018-05-02 09:44:08 +0200747 CBFS_COMPONENT_SELF,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800748 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800749 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800750 cbfstool_convert_mkpayload);
Stefan Reinauer63217582012-10-29 16:52:36 -0700751}
752
753static int cbfs_add_flat_binary(void)
754{
Hung-Te Lind1739622013-01-28 14:23:49 +0800755 if (param.loadaddress == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800756 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700757 "-l/--load-address.\n");
758 return 1;
759 }
Hung-Te Lind1739622013-01-28 14:23:49 +0800760 if (param.entrypoint == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800761 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700762 "-e/--entry-point.\n");
763 return 1;
764 }
Sol Bouchere3260a02015-03-25 13:40:08 -0700765 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800766 param.name,
Patrick Rudolph4f5bed52018-05-02 09:44:08 +0200767 CBFS_COMPONENT_SELF,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800768 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800769 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800770 cbfstool_convert_mkflatpayload);
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700771}
772
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200773static int cbfs_add_integer(void)
774{
Patrick Georgi16b3e4b2016-12-12 18:38:01 +0100775 if (!param.u64val_assigned) {
776 ERROR("You need to specify a value to write.\n");
777 return 1;
778 }
Sol Bouchere3260a02015-03-25 13:40:08 -0700779 return cbfs_add_integer_component(param.name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200780 param.u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800781 param.baseaddress,
782 param.headeroffset);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200783}
784
Stefan Reinauer63217582012-10-29 16:52:36 -0700785static int cbfs_remove(void)
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800786{
Hung-Te Lind1739622013-01-28 14:23:49 +0800787 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800788 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700789 return 1;
790 }
791
Sol Bouchere3260a02015-03-25 13:40:08 -0700792 struct cbfs_image image;
793 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700794 param.headeroffset))
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800795 return 1;
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800796
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800797 if (cbfs_remove_entry(&image, param.name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800798 ERROR("Removing file '%s' failed.\n",
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800799 param.name);
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800800 return 1;
801 }
802
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800803 return 0;
804}
805
Stefan Reinauer63217582012-10-29 16:52:36 -0700806static int cbfs_create(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000807{
Sol Boucher67a0a862015-03-18 12:36:27 -0700808 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 Boucher67a0a862015-03-18 12:36:27 -0700817 param.bootblock) {
Patrick Georgi45acb342015-07-14 22:18:23 +0200818 ERROR("Since -M was provided, -m, -s, -b, -o, -H, and -B should be omitted\n");
Sol Boucher67a0a862015-03-18 12:36:27 -0700819 return 1;
820 }
821
822 return cbfs_image_create(&image, image.buffer.size);
823 }
824
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600825 if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) {
Stefan Reinauer60a4a732013-03-28 16:46:07 -0700826 ERROR("You need to specify -m/--machine arch.\n");
David Hendricks90ca3b62012-11-16 14:48:22 -0800827 return 1;
828 }
829
Sol Bouchere3260a02015-03-25 13:40:08 -0700830 struct buffer bootblock;
Julius Wernerefcee762014-11-10 13:14:24 -0800831 if (!param.bootblock) {
832 DEBUG("-B not given, creating image without bootblock.\n");
Patrick Georgi6b2d2db2016-12-14 16:11:58 +0100833 if (buffer_create(&bootblock, 0, "(dummy)") != 0)
834 return 1;
Julius Wernerefcee762014-11-10 13:14:24 -0800835 } else if (buffer_from_file(&bootblock, param.bootblock)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800836 return 1;
837 }
838
Julius Wernerefcee762014-11-10 13:14:24 -0800839 if (!param.alignment)
Patrick Georgi45acb342015-07-14 22:18:23 +0200840 param.alignment = CBFS_ALIGNMENT;
Julius Wernerefcee762014-11-10 13:14:24 -0800841
842 // Set default offsets. x86, as usual, needs to be a special snowflake.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800843 if (!param.baseaddress_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800844 if (param.arch == CBFS_ARCHITECTURE_X86) {
845 // Make sure there's at least enough room for rel_offset
Sol Boucher67a0a862015-03-18 12:36:27 -0700846 param.baseaddress = param.size -
847 MAX(bootblock.size, sizeof(int32_t));
Julius Wernerefcee762014-11-10 13:14:24 -0800848 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 Linf56c73f2013-01-29 09:45:12 +0800854 }
855 if (!param.headeroffset_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800856 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 Georgi45acb342015-07-14 22:18:23 +0200875 CBFS_ALIGNMENT);
Julius Wernerefcee762014-11-10 13:14:24 -0800876 DEBUG("CBFS entries start beind master header (%#x).\n",
877 param.cbfsoffset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800878 }
879 }
880
Sol Boucher67a0a862015-03-18 12:36:27 -0700881 int ret = cbfs_legacy_image_create(&image,
882 param.arch,
Patrick Georgi45acb342015-07-14 22:18:23 +0200883 CBFS_ALIGNMENT,
Sol Boucher67a0a862015-03-18 12:36:27 -0700884 &bootblock,
885 param.baseaddress,
886 param.headeroffset,
887 param.cbfsoffset);
Sol Bouchere3260a02015-03-25 13:40:08 -0700888 buffer_delete(&bootblock);
Sol Boucher67a0a862015-03-18 12:36:27 -0700889 return ret;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000890}
891
Sol Boucher67a0a862015-03-18 12:36:27 -0700892static 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älkki2e042592015-05-15 09:14:09 +0300903 for (unsigned i = 0; i < fmap->nareas; ++i) {
904 const struct fmap_area *current = fmap->areas + i;
Sol Boucher67a0a862015-03-18 12:36:27 -0700905
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älkki2e042592015-05-15 09:14:09 +0300923 for (lookahead = 1; i + lookahead < fmap->nareas;
Sol Boucher67a0a862015-03-18 12:36:27 -0700924 ++lookahead) {
925 const struct fmap_area *consecutive =
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300926 fmap->areas + i + lookahead;
Sol Boucher67a0a862015-03-18 12:36:27 -0700927 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 Zeh7850b582018-04-26 09:26:38 +0200940 printf(" (%ssize %u, offset %u)\n", qualifier, current->size,
941 current->offset);
Sol Boucher67a0a862015-03-18 12:36:27 -0700942
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300943 i += lookahead - 1;
Sol Boucher67a0a862015-03-18 12:36:27 -0700944 }
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 Reinauer63217582012-10-29 16:52:36 -0700957static int cbfs_print(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000958{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800959 struct cbfs_image image;
Sol Bouchere3260a02015-03-25 13:40:08 -0700960 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700961 param.headeroffset))
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000962 return 1;
Aaron Durbin5dc628a2016-01-26 15:35:34 -0600963 if (param.machine_parseable)
964 return cbfs_print_parseable_directory(&image);
965 else
966 return cbfs_print_directory(&image);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000967}
968
Stefan Reinauer63217582012-10-29 16:52:36 -0700969static int cbfs_extract(void)
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000970{
Hung-Te Lind1739622013-01-28 14:23:49 +0800971 if (!param.filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800972 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700973 return 1;
974 }
975
Hung-Te Lind1739622013-01-28 14:23:49 +0800976 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800977 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700978 return 1;
979 }
980
Sol Bouchere3260a02015-03-25 13:40:08 -0700981 struct cbfs_image image;
982 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700983 param.headeroffset))
984 return 1;
985
Aaron Durbin17625022015-10-27 13:17:52 -0500986 return cbfs_export_entry(&image, param.name, param.filename,
987 param.arch);
Sol Boucher67a0a862015-03-18 12:36:27 -0700988}
989
990static 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 Bouchere3260a02015-03-25 13:40:08 -0700998 return 1;
999 }
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +00001000
Patrick Georgi01fbc3a2016-10-12 16:46:13 +02001001 if (!param.force && region_is_modern_cbfs(param.region_name)) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001002 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 Georgi01fbc3a2016-10-12 16:46:13 +02001018 if (!param.force && buffer_check_magic(&new_content, CBFS_FILE_MAGIC,
Sol Boucher67a0a862015-03-18 12:36:27 -07001019 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 Georgid9edb182016-12-06 18:55:26 +01001045 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 Boucher67a0a862015-03-18 12:36:27 -07001057 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
1067static 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 Guillaumefe7d6b92011-01-13 09:09:21 +00001079}
1080
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001081static int cbfs_update_fit(void)
1082{
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001083 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 Boucher32532ac2015-05-06 14:44:40 -07001094 struct buffer bootblock;
Patrick Georgi6dd99fc2015-09-19 14:04:45 +02001095 // The bootblock is part of the CBFS on x86
1096 buffer_clone(&bootblock, param.image_region);
Sol Boucher32532ac2015-05-06 14:44:40 -07001097
Sol Bouchere3260a02015-03-25 13:40:08 -07001098 struct cbfs_image image;
1099 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -07001100 param.headeroffset))
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001101 return 1;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001102
Sol Boucher32532ac2015-05-06 14:44:40 -07001103 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 Durbin6b0d0d62012-12-14 17:16:21 -06001110}
1111
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001112static int cbfs_copy(void)
1113{
Patrick Georgi214e4af2015-11-20 19:22:50 +01001114 struct cbfs_image src_image;
Patrick Georgibd0bb232015-11-20 21:48:18 +01001115 struct buffer src_buf;
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001116
Patrick Georgibd0bb232015-11-20 21:48:18 +01001117 if (!param.source_region) {
1118 ERROR("You need to specify -R/--source-region.\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001119 return 1;
Sol Bouchere3260a02015-03-25 13:40:08 -07001120 }
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001121
Patrick Georgibd0bb232015-11-20 21:48:18 +01001122 /* 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 Bendebury5e273a42014-12-23 19:26:54 -08001133}
1134
Aaron Durbin71c60ca2016-01-26 17:08:56 -06001135static 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 Georgi5d982d72017-09-19 14:39:58 +02001145static 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 Georgi12631a42017-09-20 11:59:18 +02001159static 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 Reinauera1e48242011-10-21 14:24:57 -07001176static const struct command commands[] = {
Daisuke Nojiriff906fb2017-10-30 17:38:04 -07001177 {"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 Zehe9995f12016-01-14 13:22:37 +01001179 true, true},
Nico Huber93323b32018-05-16 13:18:32 +02001180 {"add-payload", "H:r:f:n:c:b:C:I:p:vA:gh?", cbfs_add_payload,
Werner Zehe9995f12016-01-14 13:22:37 +01001181 true, true},
Daisuke Nojiriff906fb2017-10-30 17:38:04 -07001182 {"add-stage", "a:H:r:f:n:t:c:b:P:S:p:yvA:gh?", cbfs_add_stage,
Werner Zehe9995f12016-01-14 13:22:37 +01001183 true, true},
1184 {"add-int", "H:r:i:n:b:vgh?", cbfs_add_integer, true, true},
Patrick Georgi59e52b92015-09-10 15:28:27 +02001185 {"add-master-header", "H:r:vh?", cbfs_add_master_header, true, true},
Aaron Durbin71c60ca2016-01-26 17:08:56 -06001186 {"compact", "r:h?", cbfs_compact, true, true},
Patrick Georgibd0bb232015-11-20 21:48:18 +01001187 {"copy", "r:R:h?", cbfs_copy, true, true},
Patrick Georgi45acb342015-07-14 22:18:23 +02001188 {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true},
Aaron Durbin17625022015-10-27 13:17:52 -05001189 {"extract", "H:r:m:n:f:vh?", cbfs_extract, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -07001190 {"layout", "wvh?", cbfs_layout, false, false},
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001191 {"print", "H:r:vkh?", cbfs_print, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -07001192 {"read", "r:f:vh?", cbfs_read, true, false},
1193 {"remove", "H:r:n:vh?", cbfs_remove, true, true},
Vadim Bendebury75599462015-12-09 09:39:31 -08001194 {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, true},
Patrick Georgid9edb182016-12-06 18:55:26 +01001195 {"write", "r:f:i:Fudvh?", cbfs_write, true, true},
Patrick Georgi5d982d72017-09-19 14:39:58 +02001196 {"expand", "r:h?", cbfs_expand, true, true},
Patrick Georgi12631a42017-09-20 11:59:18 +02001197 {"truncate", "r:h?", cbfs_truncate, true, true},
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001198};
1199
Stefan Reinauer63217582012-10-29 16:52:36 -07001200static struct option long_options[] = {
Julius Wernerefcee762014-11-10 13:14:24 -08001201 {"alignment", required_argument, 0, 'a' },
Vadim Bendebury45e59972014-12-23 15:59:57 -08001202 {"base-address", required_argument, 0, 'b' },
1203 {"bootblock", required_argument, 0, 'B' },
Julius Wernerefcee762014-11-10 13:14:24 -08001204 {"cmdline", required_argument, 0, 'C' },
Vadim Bendebury45e59972014-12-23 15:59:57 -08001205 {"compression", required_argument, 0, 'c' },
Vadim Bendebury45e59972014-12-23 15:59:57 -08001206 {"empty-fits", required_argument, 0, 'x' },
1207 {"entry-point", required_argument, 0, 'e' },
1208 {"file", required_argument, 0, 'f' },
Sol Boucher67a0a862015-03-18 12:36:27 -07001209 {"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 Georgi01fbc3a2016-10-12 16:46:13 +02001213 {"force", no_argument, 0, 'F' },
Patrick Georgibd0bb232015-11-20 21:48:18 +01001214 {"source-region", required_argument, 0, 'R' },
Patrick Georgi89f20342015-10-01 15:54:04 +02001215 {"hash-algorithm",required_argument, 0, 'A' },
Vadim Bendebury45e59972014-12-23 15:59:57 -08001216 {"header-offset", required_argument, 0, 'H' },
Julius Wernerefcee762014-11-10 13:14:24 -08001217 {"help", no_argument, 0, 'h' },
Vadim Bendebury45e59972014-12-23 15:59:57 -08001218 {"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 Nojiriff906fb2017-10-30 17:38:04 -07001225 {"padding", required_argument, 0, 'p' },
Vadim Bendebury45e59972014-12-23 15:59:57 -08001226 {"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 Boucher67a0a862015-03-18 12:36:27 -07001231 {"with-readonly", no_argument, 0, 'w' },
Aaron Durbin4be16742015-09-15 17:00:23 -05001232 {"xip", no_argument, 0, 'y' },
Werner Zehe9995f12016-01-14 13:22:37 +01001233 {"gen-attribute", no_argument, 0, 'g' },
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001234 {"mach-parseable",no_argument, 0, 'k' },
Julius Wernerefcee762014-11-10 13:14:24 -08001235 {NULL, 0, 0, 0 }
Stefan Reinauer63217582012-10-29 16:52:36 -07001236};
1237
Sol Boucher67a0a862015-03-18 12:36:27 -07001238static 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 Georgi2ed72952016-12-16 14:51:53 +01001244 INFO("Performing operation on '%s' region...\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001245 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 Reinauer63217582012-10-29 16:52:36 -07001289static void usage(char *name)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001290{
1291 printf
Stefan Reinauer07040582010-04-24 21:24:06 +00001292 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001293 "USAGE:\n" " %s [-h]\n"
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001294 " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001295 " -H header_offset Do not search for header; use this offset*\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001296 " -T Output top-aligned memory address\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001297 " -u Accept short data; fill upward/from bottom\n"
1298 " -d Accept short data; fill downward/from top\n"
Patrick Georgi01fbc3a2016-10-12 16:46:13 +02001299 " -F Force action\n"
Patrick Georgi5b6bdcc2016-10-13 18:24:10 +02001300 " -g Generate position and alignment arguments\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001301 " -v Provide verbose output\n"
1302 " -h Display this help message\n\n"
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001303 "COMMANDs:\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001304 " add [-r image,regions] -f FILE -n NAME -t TYPE [-A hash] \\\n"
Furquan Shaikh61c1a052016-05-09 11:53:34 -07001305 " [-c compression] [-b base-address | -a alignment] \\\n"
Daisuke Nojiriff906fb2017-10-30 17:38:04 -07001306 " [-p padding size] [-y|--xip if TYPE is FSP] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001307 "Add a component\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001308 " add-payload [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001309 " [-c compression] [-b base-address] \\\n"
1310 " (linux specific: [-C cmdline] [-I initrd]) "
Stefan Reinauer63217582012-10-29 16:52:36 -07001311 "Add a payload to the ROM\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001312 " add-stage [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001313 " [-c compression] [-b base] [-S section-to-ignore] \\\n"
1314 " [-a alignment] [-y|--xip] [-P page-size] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001315 "Add a stage to the ROM\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001316 " 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 Reinauer63217582012-10-29 16:52:36 -07001319 "Add a 32bit flat mode binary\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001320 " add-int [-r image,regions] -i INTEGER -n NAME [-b base] "
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001321 "Add a raw 64-bit integer value\n"
Patrick Georgi59e52b92015-09-10 15:28:27 +02001322 " add-master-header [-r image,regions] "
1323 "Add a legacy CBFS master header\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001324 " remove [-r image,regions] -n NAME "
Stefan Reinauer63217582012-10-29 16:52:36 -07001325 "Remove a component\n"
Aaron Durbin71c60ca2016-01-26 17:08:56 -06001326 " compact -r image,regions "
1327 "Defragment CBFS image.\n"
Patrick Georgibd0bb232015-11-20 21:48:18 +01001328 " copy -r image,regions -R source-region "
1329 "Create a copy (duplicate) cbfs instance in fmap\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001330 " create -m ARCH -s size [-b bootblock offset] \\\n"
Patrick Georgi83c2d122015-08-26 10:40:00 +02001331 " [-o CBFS offset] [-H header offset] [-B bootblock] "
Sol Boucher67a0a862015-03-18 12:36:27 -07001332 "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 Reinauer63217582012-10-29 16:52:36 -07001337 "Find a place for a file of that size\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001338 " layout [-w] "
1339 "List mutable (or, with -w, readable) image regions\n"
1340 " print [-r image,regions] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001341 "Show the contents of the ROM\n"
Aaron Durbin17625022015-10-27 13:17:52 -05001342 " extract [-r image,regions] [-m ARCH] -n NAME -f FILE "
Patrick Georgi485225e2016-01-13 09:52:29 +01001343 "Extracts a raw payload from ROM\n"
Patrick Georgid9edb182016-12-06 18:55:26 +01001344 " write [-F] -r image,regions -f file [-u | -d] [-i int] "
Sol Boucher67a0a862015-03-18 12:36:27 -07001345 "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 Georgi12631a42017-09-20 11:59:18 +02001348 " truncate [-r fmap-region] "
1349 "Truncate CBFS and print new size on stdout\n"
Patrick Georgi5d982d72017-09-19 14:39:58 +02001350 " expand [-r fmap-region] "
1351 "Expand CBFS to span entire region\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001352 " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001353 " -x EMTPY_FIT_ENTRIES "
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001354 "Updates the FIT table with microcode entries\n"
Peter Stugeb347e0d2011-01-17 05:02:09 +00001355 "\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001356 "OFFSETs:\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001357 " Numbers accompanying -b, -H, and -o switches* may be provided\n"
1358 " in two possible formats: if their value is greater than\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001359 " 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äferfbc66b92018-04-08 15:05:09 +02001361 "ARCHes:\n", name, name
Stefan Reinauer63217582012-10-29 16:52:36 -07001362 );
Jonathan Neuschäferfbc66b92018-04-08 15:05:09 +02001363 print_supported_architectures();
1364
1365 printf("TYPEs:\n");
Stefan Reinauer07040582010-04-24 21:24:06 +00001366 print_supported_filetypes();
Sol Boucher67a0a862015-03-18 12:36:27 -07001367
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 Boucher67d59982015-05-07 02:39:22 -07001379 " 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 Boucher67a0a862015-03-18 12:36:27 -07001384 SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
1385 SECTION_NAME_PRIMARY_CBFS
1386 );
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001387}
1388
1389int main(int argc, char **argv)
1390{
Mathias Krause41c229c2012-07-17 21:17:15 +02001391 size_t i;
Stefan Reinauer63217582012-10-29 16:52:36 -07001392 int c;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001393
1394 if (argc < 3) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001395 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001396 return 1;
1397 }
1398
Sol Bouchere3260a02015-03-25 13:40:08 -07001399 char *image_name = argv[1];
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001400 char *cmd = argv[2];
Stefan Reinauer63217582012-10-29 16:52:36 -07001401 optind += 2;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001402
1403 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1404 if (strcmp(cmd, commands[i].name) != 0)
1405 continue;
Stefan Reinauer63217582012-10-29 16:52:36 -07001406
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 Huber54073102016-08-01 23:18:29 +02001413 if (c == -1) {
1414 if (optind < argc) {
1415 ERROR("%s: excessive argument -- '%s'"
1416 "\n", argv[0], argv[optind]);
1417 return 1;
1418 }
Stefan Reinauer63217582012-10-29 16:52:36 -07001419 break;
Nico Huber54073102016-08-01 23:18:29 +02001420 }
Stefan Reinauer63217582012-10-29 16:52:36 -07001421
1422 /* filter out illegal long options */
zbao062730d2013-01-08 10:10:16 +08001423 if (strchr(commands[i].optstring, c) == NULL) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001424 /* TODO maybe print actual long option instead */
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001425 ERROR("%s: invalid option -- '%c'\n",
1426 argv[0], c);
Stefan Reinauer63217582012-10-29 16:52:36 -07001427 c = '?';
1428 }
1429
1430 switch(c) {
1431 case 'n':
Hung-Te Lind1739622013-01-28 14:23:49 +08001432 param.name = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001433 break;
1434 case 't':
1435 if (intfiletype(optarg) != ((uint64_t) - 1))
Hung-Te Lind1739622013-01-28 14:23:49 +08001436 param.type = intfiletype(optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001437 else
Hung-Te Lind1739622013-01-28 14:23:49 +08001438 param.type = strtoul(optarg, NULL, 0);
1439 if (param.type == 0)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001440 WARN("Unknown type '%s' ignored\n",
Stefan Reinauer63217582012-10-29 16:52:36 -07001441 optarg);
1442 break;
Sol Boucherec424862015-05-07 21:00:05 -07001443 case 'c': {
Patrick Georgiecaa5702017-01-11 18:38:11 +01001444 if (strcmp(optarg, "precompression") == 0) {
1445 param.precompression = 1;
1446 break;
1447 }
Sol Boucherec424862015-05-07 21:00:05 -07001448 int algo = cbfs_parse_comp_algo(optarg);
1449 if (algo >= 0)
1450 param.compression = algo;
Stefan Reinauer63217582012-10-29 16:52:36 -07001451 else
Sol Boucherec424862015-05-07 21:00:05 -07001452 WARN("Unknown compression '%s' ignored.\n",
1453 optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001454 break;
Sol Boucherec424862015-05-07 21:00:05 -07001455 }
Patrick Georgi89f20342015-10-01 15:54:04 +02001456 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 Boucher67a0a862015-03-18 12:36:27 -07001467 case 'M':
1468 param.fmap = optarg;
1469 break;
1470 case 'r':
1471 param.region_name = optarg;
1472 break;
Patrick Georgibd0bb232015-11-20 21:48:18 +01001473 case 'R':
1474 param.source_region = optarg;
1475 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001476 case 'b':
Nico Huber9ade7172016-08-01 21:37:42 +02001477 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 Linf56c73f2013-01-29 09:45:12 +08001483 // baseaddress may be zero on non-x86, so we
1484 // need an explicit "baseaddress_assigned".
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001485 param.baseaddress_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001486 break;
1487 case 'l':
Nico Huber9ade7172016-08-01 21:37:42 +02001488 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 Reinauer63217582012-10-29 16:52:36 -07001494 break;
1495 case 'e':
Nico Huber9ade7172016-08-01 21:37:42 +02001496 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 Reinauer63217582012-10-29 16:52:36 -07001502 break;
1503 case 's':
Hung-Te Lind1739622013-01-28 14:23:49 +08001504 param.size = strtoul(optarg, &suffix, 0);
Nico Huber9ade7172016-08-01 21:37:42 +02001505 if (!*optarg) {
1506 ERROR("Empty size specified.\n");
1507 return 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001508 }
Nico Huber9ade7172016-08-01 21:37:42 +02001509 switch (tolower((int)suffix[0])) {
1510 case 'k':
1511 param.size *= 1024;
1512 break;
1513 case 'm':
Hung-Te Lind1739622013-01-28 14:23:49 +08001514 param.size *= 1024 * 1024;
Nico Huber9ade7172016-08-01 21:37:42 +02001515 break;
1516 case '\0':
1517 break;
1518 default:
1519 ERROR("Invalid suffix for size '%s'.\n",
1520 optarg);
1521 return 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001522 }
Patrick Georgie887ca52014-08-09 17:44:39 +02001523 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001524 case 'B':
Hung-Te Lind1739622013-01-28 14:23:49 +08001525 param.bootblock = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001526 break;
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001527 case 'H':
1528 param.headeroffset = strtoul(
Nico Huber9ade7172016-08-01 21:37:42 +02001529 optarg, &suffix, 0);
1530 if (!*optarg || (suffix && *suffix)) {
1531 ERROR("Invalid header offset '%s'.\n",
1532 optarg);
1533 return 1;
1534 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001535 param.headeroffset_assigned = 1;
1536 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001537 case 'a':
Nico Huber9ade7172016-08-01 21:37:42 +02001538 param.alignment = strtoul(optarg, &suffix, 0);
1539 if (!*optarg || (suffix && *suffix)) {
1540 ERROR("Invalid alignment '%s'.\n",
1541 optarg);
1542 return 1;
1543 }
Stefan Reinauer63217582012-10-29 16:52:36 -07001544 break;
Daisuke Nojiriff906fb2017-10-30 17:38:04 -07001545 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 Line9198372013-03-19 12:17:12 +08001553 case 'P':
Nico Huber9ade7172016-08-01 21:37:42 +02001554 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 Line9198372013-03-19 12:17:12 +08001560 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001561 case 'o':
Nico Huber9ade7172016-08-01 21:37:42 +02001562 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 Wernerefcee762014-11-10 13:14:24 -08001568 param.cbfsoffset_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001569 break;
1570 case 'f':
Hung-Te Lind1739622013-01-28 14:23:49 +08001571 param.filename = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001572 break;
Patrick Georgi01fbc3a2016-10-12 16:46:13 +02001573 case 'F':
1574 param.force = 1;
1575 break;
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001576 case 'i':
Nico Huber9ade7172016-08-01 21:37:42 +02001577 param.u64val = strtoull(optarg, &suffix, 0);
Patrick Georgi16b3e4b2016-12-12 18:38:01 +01001578 param.u64val_assigned = 1;
Nico Huber9ade7172016-08-01 21:37:42 +02001579 if (!*optarg || (suffix && *suffix)) {
1580 ERROR("Invalid int parameter '%s'.\n",
1581 optarg);
1582 return 1;
1583 }
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001584 break;
Sol Boucher67a0a862015-03-18 12:36:27 -07001585 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 Lin215d1d72013-01-29 03:46:02 +08001593 break;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001594 case 'x':
Nico Huber9ade7172016-08-01 21:37:42 +02001595 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 Durbin6b0d0d62012-12-14 17:16:21 -06001602 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001603 case 'v':
1604 verbose++;
1605 break;
David Hendricks90ca3b62012-11-16 14:48:22 -08001606 case 'm':
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -06001607 param.arch = string_to_arch(optarg);
David Hendricks90ca3b62012-11-16 14:48:22 -08001608 break;
Patrick Georgide36d332013-08-27 20:22:21 +02001609 case 'I':
1610 param.initrd = optarg;
1611 break;
1612 case 'C':
1613 param.cmdline = optarg;
1614 break;
Furquan Shaikh405304a2014-10-30 11:44:20 -07001615 case 'S':
1616 param.ignore_section = optarg;
1617 break;
Aaron Durbin4be16742015-09-15 17:00:23 -05001618 case 'y':
1619 param.stage_xip = true;
1620 break;
Werner Zehe9995f12016-01-14 13:22:37 +01001621 case 'g':
1622 param.autogen_attr = true;
1623 break;
Aaron Durbin5dc628a2016-01-26 15:35:34 -06001624 case 'k':
1625 param.machine_parseable = true;
1626 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001627 case 'h':
1628 case '?':
1629 usage(argv[0]);
1630 return 1;
1631 default:
1632 break;
1633 }
1634 }
1635
Sol Bouchere3260a02015-03-25 13:40:08 -07001636 if (commands[i].function == cbfs_create) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001637 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 Bouchere3260a02015-03-25 13:40:08 -07001649 return 1;
1650 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001651 } else {
Vadim Bendebury75599462015-12-09 09:39:31 -08001652 bool write_access = commands[i].modifies_region;
1653
Sol Bouchere3260a02015-03-25 13:40:08 -07001654 param.image_file =
Vadim Bendebury75599462015-12-09 09:39:31 -08001655 partitioned_file_reopen(image_name,
1656 write_access);
Sol Bouchere3260a02015-03-25 13:40:08 -07001657 }
1658 if (!param.image_file)
1659 return 1;
1660
Sol Boucher67a0a862015-03-18 12:36:27 -07001661 unsigned num_regions = 1;
1662 for (const char *list = strchr(param.region_name, ','); list;
1663 list = strchr(list + 1, ','))
1664 ++num_regions;
1665
Sol Bouchere3260a02015-03-25 13:40:08 -07001666 // 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 Boucher67a0a862015-03-18 12:36:27 -07001674 struct buffer image_regions[num_regions];
1675 memset(image_regions, 0, sizeof(image_regions));
Sol Bouchere3260a02015-03-25 13:40:08 -07001676
Sol Boucher67a0a862015-03-18 12:36:27 -07001677 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 Bouchere3260a02015-03-25 13:40:08 -07001685 partitioned_file_close(param.image_file);
1686 return 1;
1687 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001688
Sol Boucher67a0a862015-03-18 12:36:27 -07001689 if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)
1690 == 0)
1691 seen_primary_cbfs = true;
Sol Bouchere3260a02015-03-25 13:40:08 -07001692
Sol Boucher67a0a862015-03-18 12:36:27 -07001693 param.image_region = image_regions + region;
1694 if (dispatch_command(commands[i])) {
Sol Bouchere3260a02015-03-25 13:40:08 -07001695 partitioned_file_close(param.image_file);
1696 return 1;
1697 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001698
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 Boucher67a0a862015-03-18 12:36:27 -07001712 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 Bouchere3260a02015-03-25 13:40:08 -07001723 }
1724
1725 partitioned_file_close(param.image_file);
Sol Boucher67a0a862015-03-18 12:36:27 -07001726 return 0;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001727 }
1728
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001729 ERROR("Unknown command '%s'.\n", cmd);
Stefan Reinauer63217582012-10-29 16:52:36 -07001730 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001731 return 1;
1732}