blob: 35747d3d680ff5edf6131bf5651f0560958caff9 [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.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010019 * Foundation, Inc.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000020 */
21
Patrick Georgib7b56dd82009-09-14 13:29:27 +000022#include <stdio.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070023#include <stdlib.h>
Stefan Reinauer336daa72009-12-21 15:09:01 +000024#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080025#include <strings.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070026#include <ctype.h>
Stefan Reinauer63217582012-10-29 16:52:36 -070027#include <unistd.h>
28#include <getopt.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000029#include "common.h"
30#include "cbfs.h"
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080031#include "cbfs_image.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070032#include "cbfs_sections.h"
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060033#include "fit.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070034#include "partitioned_file.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;
44 // Whether to write that region's contents back to image_file at the end
45 bool modifies_region;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000046};
47
Hung-Te Lind1739622013-01-28 14:23:49 +080048static struct param {
Sol Bouchere3260a02015-03-25 13:40:08 -070049 partitioned_file_t *image_file;
50 struct buffer *image_region;
Sol Boucher67a0a862015-03-18 12:36:27 -070051 const char *name;
52 const char *filename;
53 const char *fmap;
54 const char *region_name;
55 const char *bootblock;
56 const char *ignore_section;
Peter Stuge3bfd5b82013-07-09 19:39:13 +020057 uint64_t u64val;
Hung-Te Lind1739622013-01-28 14:23:49 +080058 uint32_t type;
59 uint32_t baseaddress;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080060 uint32_t baseaddress_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080061 uint32_t loadaddress;
Vadim Bendebury5e273a42014-12-23 19:26:54 -080062 uint32_t copyoffset;
63 uint32_t copyoffset_assigned;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080064 uint32_t headeroffset;
65 uint32_t headeroffset_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080066 uint32_t entrypoint;
67 uint32_t size;
68 uint32_t alignment;
Hung-Te Line9198372013-03-19 12:17:12 +080069 uint32_t pagesize;
Julius Wernerefcee762014-11-10 13:14:24 -080070 uint32_t cbfsoffset;
71 uint32_t cbfsoffset_assigned;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060072 uint32_t arch;
Sol Boucher67a0a862015-03-18 12:36:27 -070073 bool fill_partial_upward;
74 bool fill_partial_downward;
75 bool show_immutable;
Aaron Durbin4be16742015-09-15 17:00:23 -050076 bool stage_xip;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060077 int fit_empty_entries;
Sol Boucher65336712015-05-07 21:00:05 -070078 enum comp_algo compression;
Patrick Georgide36d332013-08-27 20:22:21 +020079 /* for linux payloads */
80 char *initrd;
81 char *cmdline;
Hung-Te Lind1739622013-01-28 14:23:49 +080082} param = {
83 /* All variables not listed are initialized as zero. */
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060084 .arch = CBFS_ARCHITECTURE_UNKNOWN,
Sol Boucher65336712015-05-07 21:00:05 -070085 .compression = CBFS_COMPRESS_NONE,
Vadim Bendebury458a12e2014-12-23 15:10:12 -080086 .headeroffset = ~0,
Sol Boucher67a0a862015-03-18 12:36:27 -070087 .region_name = SECTION_NAME_PRIMARY_CBFS,
Hung-Te Lind1739622013-01-28 14:23:49 +080088};
Stefan Reinauer63217582012-10-29 16:52:36 -070089
Sol Boucher67a0a862015-03-18 12:36:27 -070090static bool region_is_flashmap(const char *region)
91{
92 return partitioned_file_region_check_magic(param.image_file, region,
93 FMAP_SIGNATURE, strlen(FMAP_SIGNATURE));
94}
95
96/* @return Same as cbfs_is_valid_cbfs(), but for a named region. */
97static bool region_is_modern_cbfs(const char *region)
98{
99 return partitioned_file_region_check_magic(param.image_file, region,
100 CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC));
101}
102
Sol Boucher67d59982015-05-07 02:39:22 -0700103/*
104 * Converts between offsets from the start of the specified image region and
105 * "top-aligned" offsets from the top of the entire flash image. Works in either
106 * direction: pass in one type of offset and receive the other type.
107 * N.B. A top-aligned offset is always a positive number, and should not be
108 * confused with a top-aliged *address*, which is its arithmetic inverse. */
109static unsigned convert_to_from_top_aligned(const struct buffer *region,
110 unsigned offset)
111{
112 assert(region);
113
114 size_t image_size = partitioned_file_total_size(param.image_file);
115 return image_size - region->offset - offset;
116}
117
Aaron Durbin4be16742015-09-15 17:00:23 -0500118static int do_cbfs_locate(int32_t *cbfs_addr, size_t metadata_size)
119{
120 if (!param.filename) {
121 ERROR("You need to specify -f/--filename.\n");
122 return 1;
123 }
124
125 if (!param.name) {
126 ERROR("You need to specify -n/--name.\n");
127 return 1;
128 }
129
130 struct cbfs_image image;
131 if (cbfs_image_from_buffer(&image, param.image_region,
132 param.headeroffset))
133 return 1;
134
135 if (cbfs_get_entry(&image, param.name))
136 WARN("'%s' already in CBFS.\n", param.name);
137
138 struct buffer buffer;
139 if (buffer_from_file(&buffer, param.filename) != 0) {
140 ERROR("Cannot load %s.\n", param.filename);
141 return 1;
142 }
143
144 /* Include cbfs_file size along with space for with name. */
145 metadata_size += cbfs_calculate_file_header_size(param.name);
146
147 int32_t address = cbfs_locate_entry(&image, buffer.size, param.pagesize,
148 param.alignment, metadata_size);
149 buffer_delete(&buffer);
150
151 if (address == -1) {
152 ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n",
153 param.name, param.pagesize, param.alignment);
154 return 1;
155 }
156
Aaron Durbin4be16742015-09-15 17:00:23 -0500157 *cbfs_addr = address;
158 return 0;
159}
160
Patrick Georgif8204972015-08-11 15:16:24 +0200161typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200162 struct cbfs_file *header);
Stefan Reinauer63217582012-10-29 16:52:36 -0700163
Sol Bouchere3260a02015-03-25 13:40:08 -0700164static int cbfs_add_integer_component(const char *name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200165 uint64_t u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800166 uint32_t offset,
167 uint32_t headeroffset) {
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200168 struct cbfs_image image;
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200169 struct cbfs_file *header = NULL;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200170 struct buffer buffer;
171 int i, ret = 1;
172
173 if (!name) {
174 ERROR("You need to specify -n/--name.\n");
175 return 1;
176 }
177
178 if (buffer_create(&buffer, 8, name) != 0)
179 return 1;
180
181 for (i = 0; i < 8; i++)
182 buffer.data[i] = (u64val >> i*8) & 0xff;
183
Sol Bouchere3260a02015-03-25 13:40:08 -0700184 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) {
185 ERROR("Selected image region is not a CBFS.\n");
186 goto done;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200187 }
188
189 if (cbfs_get_entry(&image, name)) {
190 ERROR("'%s' already in ROM image.\n", name);
191 goto done;
192 }
193
Sol Boucher67d59982015-05-07 02:39:22 -0700194 if (IS_TOP_ALIGNED_ADDRESS(offset))
195 offset = convert_to_from_top_aligned(param.image_region,
196 -offset);
197
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200198 header = cbfs_create_file_header(CBFS_COMPONENT_RAW,
199 buffer.size, name);
Patrick Georgif5252f32015-08-25 22:27:57 +0200200 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Sol Boucher0e539312015-03-05 15:38:03 -0800201 ERROR("Failed to add %llu into ROM image as '%s'.\n",
202 (long long unsigned)u64val, name);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200203 goto done;
204 }
205
Sol Bouchere3260a02015-03-25 13:40:08 -0700206 ret = 0;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200207
208done:
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200209 free(header);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200210 buffer_delete(&buffer);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200211 return ret;
212}
213
Patrick Georgi59e52b92015-09-10 15:28:27 +0200214static int cbfs_add_master_header(void)
215{
216 const char * const name = "cbfs master header";
217 struct cbfs_image image;
218 struct cbfs_file *header = NULL;
219 struct buffer buffer;
220 int ret = 1;
221
222 if (cbfs_image_from_buffer(&image, param.image_region,
223 param.headeroffset)) {
224 ERROR("Selected image region is not a CBFS.\n");
225 return 1;
226 }
227
228 if (cbfs_get_entry(&image, name)) {
229 ERROR("'%s' already in ROM image.\n", name);
230 return 1;
231 }
232
233 if (buffer_create(&buffer, sizeof(struct cbfs_header), name) != 0)
234 return 1;
235
236 struct cbfs_header *h = (struct cbfs_header *)buffer.data;
237 h->magic = htonl(CBFS_HEADER_MAGIC);
238 h->version = htonl(CBFS_HEADER_VERSION);
239 h->romsize = htonl(param.image_region->size);
240 /* The 4 bytes are left out for two reasons:
241 * 1. the cbfs master header pointer resides there
242 * 2. some cbfs implementations assume that an image that resides
243 * below 4GB has a bootblock and get confused when the end of the
244 * image is at 4GB == 0.
245 */
246 h->bootblocksize = htonl(4);
247 h->align = htonl(CBFS_ENTRY_ALIGNMENT);
248 /* offset relative to romsize above, which covers precisely the CBFS
249 * region.
250 */
251 h->offset = htonl(0);
252 h->architecture = htonl(CBFS_ARCHITECTURE_UNKNOWN);
253
254 header = cbfs_create_file_header(CBFS_COMPONENT_CBFSHEADER,
255 buffer_size(&buffer), name);
256 if (cbfs_add_entry(&image, &buffer, 0, header) != 0) {
257 ERROR("Failed to add cbfs master header into ROM image.\n");
258 goto done;
259 }
260
261 struct cbfs_file *entry;
262 if ((entry = cbfs_get_entry(&image, name)) == NULL) {
263 ERROR("'%s' not in ROM image?!?\n", name);
264 goto done;
265 }
266
267 uint32_t header_offset = CBFS_SUBHEADER(entry) -
268 buffer_get(&image.buffer);
269 header_offset = -(buffer_size(&image.buffer) - header_offset);
270
271 // TODO: when we have a BE target, we'll need to store this as BE
272 *(uint32_t *)(buffer_get(&image.buffer) +
273 buffer_size(&image.buffer) - 4) =
Aaron Durbin4b93a4f2015-09-21 13:10:13 -0500274 swab32(htonl(header_offset));
Patrick Georgi59e52b92015-09-10 15:28:27 +0200275
276 ret = 0;
277
278done:
279 free(header);
280 buffer_delete(&buffer);
281 return ret;
282}
283
Sol Bouchere3260a02015-03-25 13:40:08 -0700284static int cbfs_add_component(const char *filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800285 const char *name,
286 uint32_t type,
287 uint32_t offset,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800288 uint32_t headeroffset,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100289 convert_buffer_t convert)
290{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800291 if (!filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800292 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700293 return 1;
294 }
295
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800296 if (!name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800297 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700298 return 1;
299 }
300
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800301 if (type == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800302 ERROR("You need to specify a valid -t/--type.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700303 return 1;
304 }
305
Sol Bouchere3260a02015-03-25 13:40:08 -0700306 struct cbfs_image image;
Sol Boucher67a0a862015-03-18 12:36:27 -0700307 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset))
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800308 return 1;
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800309
Patrick Georgi4e54bf92015-08-11 14:35:39 +0200310 if (cbfs_get_entry(&image, name)) {
311 ERROR("'%s' already in ROM image.\n", name);
312 return 1;
313 }
314
Sol Bouchere3260a02015-03-25 13:40:08 -0700315 struct buffer buffer;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800316 if (buffer_from_file(&buffer, filename) != 0) {
317 ERROR("Could not load file '%s'.\n", filename);
Stefan Reinauer63217582012-10-29 16:52:36 -0700318 return 1;
319 }
320
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200321 struct cbfs_file *header =
322 cbfs_create_file_header(type, buffer.size, name);
Patrick Georgi4110abc2015-08-11 15:10:33 +0200323
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200324 if (convert && convert(&buffer, &offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800325 ERROR("Failed to parse file '%s'.\n", filename);
326 buffer_delete(&buffer);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000327 return 1;
Stefan Reinauerfbadc492011-10-14 12:44:14 -0700328 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700329
Sol Boucher67d59982015-05-07 02:39:22 -0700330 if (IS_TOP_ALIGNED_ADDRESS(offset))
331 offset = convert_to_from_top_aligned(param.image_region,
332 -offset);
333
Patrick Georgif5252f32015-08-25 22:27:57 +0200334 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800335 ERROR("Failed to add '%s' into ROM image.\n", filename);
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200336 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800337 buffer_delete(&buffer);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800338 return 1;
339 }
340
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200341 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800342 buffer_delete(&buffer);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000343 return 0;
344}
345
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700346static int cbfstool_convert_raw(struct buffer *buffer,
347 unused uint32_t *offset, struct cbfs_file *header)
348{
349 char *compressed;
350 int compressed_size;
351
352 comp_func_ptr compress = compression_function(param.compression);
353 if (!compress)
354 return -1;
355 compressed = calloc(buffer->size, 1);
356
357 if (compress(buffer->data, buffer->size,
358 compressed, &compressed_size)) {
359 WARN("Compression failed - disabled\n");
360 } else {
361 struct cbfs_file_attr_compression *attrs =
362 (struct cbfs_file_attr_compression *)
363 cbfs_add_file_attr(header,
364 CBFS_FILE_ATTR_TAG_COMPRESSION,
365 sizeof(struct cbfs_file_attr_compression));
366 if (attrs == NULL)
367 return -1;
368 attrs->compression = htonl(param.compression);
369 attrs->decompressed_size = htonl(buffer->size);
370
371 free(buffer->data);
372 buffer->data = compressed;
373 buffer->size = compressed_size;
374
375 header->len = htonl(buffer->size);
376 }
377 return 0;
378}
379
Patrick Georgif8204972015-08-11 15:16:24 +0200380static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200381 struct cbfs_file *header)
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600382{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800383 struct buffer output;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600384 int ret;
Aaron Durbin4be16742015-09-15 17:00:23 -0500385
386 if (param.stage_xip) {
387 int32_t address;
388
389 if (do_cbfs_locate(&address, sizeof(struct cbfs_stage))) {
390 ERROR("Could not find location for XIP stage.\n");
391 return 1;
392 }
393
394 /* Pass in a top aligned address. */
395 address = -convert_to_from_top_aligned(param.image_region,
396 address);
397 *offset = address;
398
399 ret = parse_elf_to_xip_stage(buffer, &output, offset,
400 param.ignore_section);
401 } else
402 ret = parse_elf_to_stage(buffer, &output, param.compression,
403 offset, param.ignore_section);
404
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600405 if (ret != 0)
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800406 return -1;
407 buffer_delete(buffer);
408 // direct assign, no dupe.
409 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200410 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800411 return 0;
412}
413
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100414static int cbfstool_convert_mkpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200415 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100416{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800417 struct buffer output;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800418 int ret;
419 /* per default, try and see if payload is an ELF binary */
Sol Boucher65336712015-05-07 21:00:05 -0700420 ret = parse_elf_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800421
422 /* If it's not an ELF, see if it's a UEFI FV */
423 if (ret != 0)
Sol Boucher65336712015-05-07 21:00:05 -0700424 ret = parse_fv_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800425
Patrick Georgide36d332013-08-27 20:22:21 +0200426 /* If it's neither ELF nor UEFI Fv, try bzImage */
427 if (ret != 0)
428 ret = parse_bzImage_to_payload(buffer, &output,
Sol Boucher65336712015-05-07 21:00:05 -0700429 param.initrd, param.cmdline, param.compression);
Patrick Georgide36d332013-08-27 20:22:21 +0200430
Stefan Reinauer543a6822013-02-04 15:39:13 -0800431 /* Not a supported payload type */
432 if (ret != 0) {
433 ERROR("Not a supported payload type (ELF / FV).\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700434 buffer_delete(buffer);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800435 return -1;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800436 }
437
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800438 buffer_delete(buffer);
439 // direct assign, no dupe.
440 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200441 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800442 return 0;
443}
444
445static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200446 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100447{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800448 struct buffer output;
449 if (parse_flat_binary_to_payload(buffer, &output,
450 param.loadaddress,
451 param.entrypoint,
Sol Boucher65336712015-05-07 21:00:05 -0700452 param.compression) != 0) {
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800453 return -1;
454 }
455 buffer_delete(buffer);
456 // direct assign, no dupe.
457 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200458 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800459 return 0;
460}
461
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800462static int cbfs_add(void)
463{
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700464 int32_t address;
465
466 if (param.alignment && param.baseaddress) {
467 ERROR("Cannot specify both alignment and base address\n");
468 return 1;
469 }
470
471 if (param.alignment) {
Aaron Durbin9b9d4b32015-09-15 21:30:58 -0500472 /* CBFS compression file attribute is unconditionally added. */
473 size_t metadata_sz = sizeof(struct cbfs_file_attr_compression);
474 if (do_cbfs_locate(&address, metadata_sz))
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700475 return 1;
476 param.baseaddress = address;
477 }
478
Sol Bouchere3260a02015-03-25 13:40:08 -0700479 return cbfs_add_component(param.filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800480 param.name,
481 param.type,
482 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800483 param.headeroffset,
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700484 cbfstool_convert_raw);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800485}
486
Stefan Reinauer63217582012-10-29 16:52:36 -0700487static int cbfs_add_stage(void)
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700488{
Aaron Durbin4be16742015-09-15 17:00:23 -0500489 if (param.stage_xip) {
490 if (param.baseaddress_assigned) {
491 ERROR("Cannot specify base address for XIP.\n");
492 return 1;
493 }
494
495 if (param.compression != CBFS_COMPRESS_NONE) {
496 ERROR("Cannot specify compression for XIP.\n");
497 return 1;
498 }
499 }
500
Sol Bouchere3260a02015-03-25 13:40:08 -0700501 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800502 param.name,
503 CBFS_COMPONENT_STAGE,
504 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800505 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800506 cbfstool_convert_mkstage);
507}
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700508
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800509static int cbfs_add_payload(void)
510{
Sol Bouchere3260a02015-03-25 13:40:08 -0700511 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800512 param.name,
513 CBFS_COMPONENT_PAYLOAD,
514 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800515 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800516 cbfstool_convert_mkpayload);
Stefan Reinauer63217582012-10-29 16:52:36 -0700517}
518
519static int cbfs_add_flat_binary(void)
520{
Hung-Te Lind1739622013-01-28 14:23:49 +0800521 if (param.loadaddress == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800522 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700523 "-l/--load-address.\n");
524 return 1;
525 }
Hung-Te Lind1739622013-01-28 14:23:49 +0800526 if (param.entrypoint == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800527 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700528 "-e/--entry-point.\n");
529 return 1;
530 }
Sol Bouchere3260a02015-03-25 13:40:08 -0700531 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800532 param.name,
533 CBFS_COMPONENT_PAYLOAD,
534 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800535 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800536 cbfstool_convert_mkflatpayload);
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700537}
538
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200539static int cbfs_add_integer(void)
540{
Sol Bouchere3260a02015-03-25 13:40:08 -0700541 return cbfs_add_integer_component(param.name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200542 param.u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800543 param.baseaddress,
544 param.headeroffset);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200545}
546
Stefan Reinauer63217582012-10-29 16:52:36 -0700547static int cbfs_remove(void)
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800548{
Hung-Te Lind1739622013-01-28 14:23:49 +0800549 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800550 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700551 return 1;
552 }
553
Sol Bouchere3260a02015-03-25 13:40:08 -0700554 struct cbfs_image image;
555 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700556 param.headeroffset))
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800557 return 1;
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800558
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800559 if (cbfs_remove_entry(&image, param.name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800560 ERROR("Removing file '%s' failed.\n",
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800561 param.name);
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800562 return 1;
563 }
564
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800565 return 0;
566}
567
Stefan Reinauer63217582012-10-29 16:52:36 -0700568static int cbfs_create(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000569{
Sol Boucher67a0a862015-03-18 12:36:27 -0700570 struct cbfs_image image;
571 memset(&image, 0, sizeof(image));
572 buffer_clone(&image.buffer, param.image_region);
573
574 if (param.fmap) {
575 if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size ||
576 param.baseaddress_assigned ||
577 param.headeroffset_assigned ||
578 param.cbfsoffset_assigned ||
Sol Boucher67a0a862015-03-18 12:36:27 -0700579 param.bootblock) {
Patrick Georgi45acb342015-07-14 22:18:23 +0200580 ERROR("Since -M was provided, -m, -s, -b, -o, -H, and -B should be omitted\n");
Sol Boucher67a0a862015-03-18 12:36:27 -0700581 return 1;
582 }
583
584 return cbfs_image_create(&image, image.buffer.size);
585 }
586
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600587 if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) {
Stefan Reinauer60a4a732013-03-28 16:46:07 -0700588 ERROR("You need to specify -m/--machine arch.\n");
David Hendricks90ca3b62012-11-16 14:48:22 -0800589 return 1;
590 }
591
Sol Bouchere3260a02015-03-25 13:40:08 -0700592 struct buffer bootblock;
Julius Wernerefcee762014-11-10 13:14:24 -0800593 if (!param.bootblock) {
594 DEBUG("-B not given, creating image without bootblock.\n");
595 buffer_create(&bootblock, 0, "(dummy)");
596 } else if (buffer_from_file(&bootblock, param.bootblock)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800597 return 1;
598 }
599
Julius Wernerefcee762014-11-10 13:14:24 -0800600 if (!param.alignment)
Patrick Georgi45acb342015-07-14 22:18:23 +0200601 param.alignment = CBFS_ALIGNMENT;
Julius Wernerefcee762014-11-10 13:14:24 -0800602
603 // Set default offsets. x86, as usual, needs to be a special snowflake.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800604 if (!param.baseaddress_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800605 if (param.arch == CBFS_ARCHITECTURE_X86) {
606 // Make sure there's at least enough room for rel_offset
Sol Boucher67a0a862015-03-18 12:36:27 -0700607 param.baseaddress = param.size -
608 MAX(bootblock.size, sizeof(int32_t));
Julius Wernerefcee762014-11-10 13:14:24 -0800609 DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n",
610 param.baseaddress);
611 } else {
612 param.baseaddress = 0;
613 DEBUG("bootblock starts at address 0x0.\n");
614 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800615 }
616 if (!param.headeroffset_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800617 if (param.arch == CBFS_ARCHITECTURE_X86) {
618 param.headeroffset = param.baseaddress -
619 sizeof(struct cbfs_header);
620 DEBUG("x86 -> CBFS header before bootblock (%#x).\n",
621 param.headeroffset);
622 } else {
623 param.headeroffset = align_up(param.baseaddress +
624 bootblock.size, sizeof(uint32_t));
625 DEBUG("CBFS header placed behind bootblock (%#x).\n",
626 param.headeroffset);
627 }
628 }
629 if (!param.cbfsoffset_assigned) {
630 if (param.arch == CBFS_ARCHITECTURE_X86) {
631 param.cbfsoffset = 0;
632 DEBUG("x86 -> CBFS entries start at address 0x0.\n");
633 } else {
634 param.cbfsoffset = align_up(param.headeroffset +
635 sizeof(struct cbfs_header),
Patrick Georgi45acb342015-07-14 22:18:23 +0200636 CBFS_ALIGNMENT);
Julius Wernerefcee762014-11-10 13:14:24 -0800637 DEBUG("CBFS entries start beind master header (%#x).\n",
638 param.cbfsoffset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800639 }
640 }
641
Sol Boucher67a0a862015-03-18 12:36:27 -0700642 int ret = cbfs_legacy_image_create(&image,
643 param.arch,
Patrick Georgi45acb342015-07-14 22:18:23 +0200644 CBFS_ALIGNMENT,
Sol Boucher67a0a862015-03-18 12:36:27 -0700645 &bootblock,
646 param.baseaddress,
647 param.headeroffset,
648 param.cbfsoffset);
Sol Bouchere3260a02015-03-25 13:40:08 -0700649 buffer_delete(&bootblock);
Sol Boucher67a0a862015-03-18 12:36:27 -0700650 return ret;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000651}
652
Sol Boucher67a0a862015-03-18 12:36:27 -0700653static int cbfs_layout(void)
654{
655 const struct fmap *fmap = partitioned_file_get_fmap(param.image_file);
656 if (!fmap) {
657 LOG("This is a legacy image composed entirely of a single CBFS.\n");
658 return 1;
659 }
660
661 printf("This image contains the following sections that can be %s with this tool:\n",
662 param.show_immutable ? "accessed" : "manipulated");
663 puts("");
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300664 for (unsigned i = 0; i < fmap->nareas; ++i) {
665 const struct fmap_area *current = fmap->areas + i;
Sol Boucher67a0a862015-03-18 12:36:27 -0700666
667 bool readonly = partitioned_file_fmap_count(param.image_file,
668 partitioned_file_fmap_select_children_of, current) ||
669 region_is_flashmap((const char *)current->name);
670 if (!param.show_immutable && readonly)
671 continue;
672
673 printf("'%s'", current->name);
674
675 // Detect consecutive sections that describe the same region and
676 // show them as aliases. This cannot find equivalent entries
677 // that aren't adjacent; however, fmaptool doesn't generate
678 // FMAPs with such sections, so this convenience feature works
679 // for all but the strangest manually created FMAP binaries.
680 // TODO: This could be done by parsing the FMAP into some kind
681 // of tree that had duplicate lists in addition to child lists,
682 // which would allow covering that weird, unlikely case as well.
683 unsigned lookahead;
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300684 for (lookahead = 1; i + lookahead < fmap->nareas;
Sol Boucher67a0a862015-03-18 12:36:27 -0700685 ++lookahead) {
686 const struct fmap_area *consecutive =
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300687 fmap->areas + i + lookahead;
Sol Boucher67a0a862015-03-18 12:36:27 -0700688 if (consecutive->offset != current->offset ||
689 consecutive->size != current->size)
690 break;
691 printf(", '%s'", consecutive->name);
692 }
693 if (lookahead > 1)
694 fputs(" are aliases for the same region", stdout);
695
696 const char *qualifier = "";
697 if (readonly)
698 qualifier = "read-only, ";
699 else if (region_is_modern_cbfs((const char *)current->name))
700 qualifier = "CBFS, ";
701 printf(" (%ssize %u)\n", qualifier, current->size);
702
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300703 i += lookahead - 1;
Sol Boucher67a0a862015-03-18 12:36:27 -0700704 }
705 puts("");
706
707 if (param.show_immutable) {
708 puts("It is at least possible to perform the read action on every section listed above.");
709 } else {
710 puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above.");
711 puts("To see the image's read-only sections as well, rerun with the -w option.");
712 }
713
714 return 0;
715}
716
Stefan Reinauer63217582012-10-29 16:52:36 -0700717static int cbfs_print(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000718{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800719 struct cbfs_image image;
Sol Bouchere3260a02015-03-25 13:40:08 -0700720 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700721 param.headeroffset))
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000722 return 1;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800723 cbfs_print_directory(&image);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000724 return 0;
725}
726
Stefan Reinauer63217582012-10-29 16:52:36 -0700727static int cbfs_extract(void)
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000728{
Hung-Te Lind1739622013-01-28 14:23:49 +0800729 if (!param.filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800730 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700731 return 1;
732 }
733
Hung-Te Lind1739622013-01-28 14:23:49 +0800734 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800735 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700736 return 1;
737 }
738
Sol Bouchere3260a02015-03-25 13:40:08 -0700739 struct cbfs_image image;
740 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700741 param.headeroffset))
742 return 1;
743
744 return cbfs_export_entry(&image, param.name, param.filename);
745}
746
747static int cbfs_write(void)
748{
749 if (!param.filename) {
750 ERROR("You need to specify a valid input -f/--file.\n");
751 return 1;
752 }
753 if (!partitioned_file_is_partitioned(param.image_file)) {
754 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700755 return 1;
756 }
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000757
Sol Boucher67a0a862015-03-18 12:36:27 -0700758 if (region_is_modern_cbfs(param.region_name)) {
759 ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n",
760 param.region_name);
761 return 1;
762 }
763
764 struct buffer new_content;
765 if (buffer_from_file(&new_content, param.filename))
766 return 1;
767
768 if (buffer_check_magic(&new_content, FMAP_SIGNATURE,
769 strlen(FMAP_SIGNATURE))) {
770 ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n",
771 param.filename);
772 buffer_delete(&new_content);
773 return 1;
774 }
775 if (buffer_check_magic(&new_content, CBFS_FILE_MAGIC,
776 strlen(CBFS_FILE_MAGIC))) {
777 ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n",
778 param.filename);
779 buffer_delete(&new_content);
780 return 1;
781 }
782
783 unsigned offset = 0;
784 if (param.fill_partial_upward && param.fill_partial_downward) {
785 ERROR("You may only specify one of -u and -d.\n");
786 buffer_delete(&new_content);
787 return 1;
788 } else if (!param.fill_partial_upward && !param.fill_partial_downward) {
789 if (new_content.size != param.image_region->size) {
790 ERROR("File to add is %zu bytes and would not fill %zu-byte target region (did you mean to pass either -u or -d?)\n",
791 new_content.size, param.image_region->size);
792 buffer_delete(&new_content);
793 return 1;
794 }
795 } else {
796 if (new_content.size > param.image_region->size) {
797 ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n",
798 new_content.size, param.image_region->size);
799 buffer_delete(&new_content);
800 return 1;
801 }
802 WARN("Written area will abut %s of target region: any unused space will keep its current contents\n",
803 param.fill_partial_upward ? "bottom" : "top");
804 if (param.fill_partial_downward)
805 offset = param.image_region->size - new_content.size;
806 }
807
808 memcpy(param.image_region->data + offset, new_content.data,
809 new_content.size);
810 buffer_delete(&new_content);
811 return 0;
812}
813
814static int cbfs_read(void)
815{
816 if (!param.filename) {
817 ERROR("You need to specify a valid output -f/--file.\n");
818 return 1;
819 }
820 if (!partitioned_file_is_partitioned(param.image_file)) {
821 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
822 return 1;
823 }
824
825 return buffer_write_file(param.image_region, param.filename);
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000826}
827
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600828static int cbfs_update_fit(void)
829{
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600830 if (!param.name) {
831 ERROR("You need to specify -n/--name.\n");
832 return 1;
833 }
834
835 if (param.fit_empty_entries <= 0) {
836 ERROR("Invalid number of fit entries "
837 "(-x/--empty-fits): %d\n", param.fit_empty_entries);
838 return 1;
839 }
840
Sol Boucher32532ac2015-05-06 14:44:40 -0700841 struct buffer bootblock;
Patrick Georgi6dd99fc2015-09-19 14:04:45 +0200842 // The bootblock is part of the CBFS on x86
843 buffer_clone(&bootblock, param.image_region);
Sol Boucher32532ac2015-05-06 14:44:40 -0700844
Sol Bouchere3260a02015-03-25 13:40:08 -0700845 struct cbfs_image image;
846 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700847 param.headeroffset))
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600848 return 1;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600849
Sol Boucher32532ac2015-05-06 14:44:40 -0700850 if (fit_update_table(&bootblock, &image, param.name,
851 param.fit_empty_entries, convert_to_from_top_aligned))
852 return 1;
853
854 // The region to be written depends on the type of image, so we write it
855 // here rather than having main() write the CBFS region back as usual.
856 return !partitioned_file_write_region(param.image_file, &bootblock);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600857}
858
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800859static int cbfs_copy(void)
860{
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800861 if (!param.copyoffset_assigned) {
862 ERROR("You need to specify -D/--copy_offset.\n");
863 return 1;
864 }
865
866 if (!param.size) {
867 ERROR("You need to specify -s/--size.\n");
868 return 1;
869 }
870
Sol Bouchere3260a02015-03-25 13:40:08 -0700871 struct cbfs_image image;
872 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700873 param.headeroffset))
874 return 1;
875
876 if (!cbfs_is_legacy_cbfs(&image)) {
877 ERROR("This operation is only valid on legacy images having CBFS master headers\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800878 return 1;
Sol Bouchere3260a02015-03-25 13:40:08 -0700879 }
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800880
Sol Bouchere3260a02015-03-25 13:40:08 -0700881 return cbfs_copy_instance(&image, param.copyoffset, param.size);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800882}
883
Stefan Reinauera1e48242011-10-21 14:24:57 -0700884static const struct command commands[] = {
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700885 {"add", "H:r:f:n:t:c:b:a:vh?", cbfs_add, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700886 {"add-flat-binary", "H:r:f:n:l:e:c:b:vh?", cbfs_add_flat_binary, true,
Sol Bouchere3260a02015-03-25 13:40:08 -0700887 true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700888 {"add-payload", "H:r:f:n:t:c:b:C:I:vh?", cbfs_add_payload, true, true},
Aaron Durbin4be16742015-09-15 17:00:23 -0500889 {"add-stage", "a:H:r:f:n:t:c:b:P:S:yvh?", cbfs_add_stage, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700890 {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true},
Patrick Georgi59e52b92015-09-10 15:28:27 +0200891 {"add-master-header", "H:r:vh?", cbfs_add_master_header, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700892 {"copy", "H:D:s:h?", cbfs_copy, true, true},
Patrick Georgi45acb342015-07-14 22:18:23 +0200893 {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700894 {"extract", "H:r:n:f:vh?", cbfs_extract, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700895 {"layout", "wvh?", cbfs_layout, false, false},
896 {"print", "H:r:vh?", cbfs_print, true, false},
897 {"read", "r:f:vh?", cbfs_read, true, false},
898 {"remove", "H:r:n:vh?", cbfs_remove, true, true},
Sol Boucher32532ac2015-05-06 14:44:40 -0700899 {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700900 {"write", "r:f:udvh?", cbfs_write, true, true},
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000901};
902
Stefan Reinauer63217582012-10-29 16:52:36 -0700903static struct option long_options[] = {
Julius Wernerefcee762014-11-10 13:14:24 -0800904 {"alignment", required_argument, 0, 'a' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800905 {"base-address", required_argument, 0, 'b' },
906 {"bootblock", required_argument, 0, 'B' },
Julius Wernerefcee762014-11-10 13:14:24 -0800907 {"cmdline", required_argument, 0, 'C' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800908 {"compression", required_argument, 0, 'c' },
909 {"copy-offset", required_argument, 0, 'D' },
910 {"empty-fits", required_argument, 0, 'x' },
911 {"entry-point", required_argument, 0, 'e' },
912 {"file", required_argument, 0, 'f' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700913 {"fill-downward", no_argument, 0, 'd' },
914 {"fill-upward", no_argument, 0, 'u' },
915 {"flashmap", required_argument, 0, 'M' },
916 {"fmap-regions", required_argument, 0, 'r' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800917 {"header-offset", required_argument, 0, 'H' },
Julius Wernerefcee762014-11-10 13:14:24 -0800918 {"help", no_argument, 0, 'h' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800919 {"ignore-sec", required_argument, 0, 'S' },
920 {"initrd", required_argument, 0, 'I' },
921 {"int", required_argument, 0, 'i' },
922 {"load-address", required_argument, 0, 'l' },
923 {"machine", required_argument, 0, 'm' },
924 {"name", required_argument, 0, 'n' },
925 {"offset", required_argument, 0, 'o' },
926 {"page-size", required_argument, 0, 'P' },
927 {"size", required_argument, 0, 's' },
928 {"top-aligned", required_argument, 0, 'T' },
929 {"type", required_argument, 0, 't' },
930 {"verbose", no_argument, 0, 'v' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700931 {"with-readonly", no_argument, 0, 'w' },
Aaron Durbin4be16742015-09-15 17:00:23 -0500932 {"xip", no_argument, 0, 'y' },
Julius Wernerefcee762014-11-10 13:14:24 -0800933 {NULL, 0, 0, 0 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700934};
935
Sol Boucher67a0a862015-03-18 12:36:27 -0700936static int dispatch_command(struct command command)
937{
938 if (command.accesses_region) {
939 assert(param.image_file);
940
941 if (partitioned_file_is_partitioned(param.image_file)) {
942 LOG("Performing operation on '%s' region...\n",
943 param.region_name);
944 }
945 if (!partitioned_file_read_region(param.image_region,
946 param.image_file, param.region_name)) {
947 ERROR("The image will be left unmodified.\n");
948 return 1;
949 }
950
951 if (command.modifies_region) {
952 // We (intentionally) don't support overwriting the FMAP
953 // section. If you find yourself wanting to do this,
954 // consider creating a new image rather than performing
955 // whatever hacky transformation you were planning.
956 if (region_is_flashmap(param.region_name)) {
957 ERROR("Image region '%s' is read-only because it contains the FMAP.\n",
958 param.region_name);
959 ERROR("The image will be left unmodified.\n");
960 return 1;
961 }
962 // We don't allow writing raw data to regions that
963 // contain nested regions, since doing so would
964 // overwrite all such subregions.
965 if (partitioned_file_region_contains_nested(
966 param.image_file, param.region_name)) {
967 ERROR("Image region '%s' is read-only because it contains nested regions.\n",
968 param.region_name);
969 ERROR("The image will be left unmodified.\n");
970 return 1;
971 }
972 }
973 }
974
975 if (command.function()) {
976 if (partitioned_file_is_partitioned(param.image_file)) {
977 ERROR("Failed while operating on '%s' region!\n",
978 param.region_name);
979 ERROR("The image will be left unmodified.\n");
980 }
981 return 1;
982 }
983
984 return 0;
985}
986
Stefan Reinauer63217582012-10-29 16:52:36 -0700987static void usage(char *name)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000988{
989 printf
Stefan Reinauer07040582010-04-24 21:24:06 +0000990 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
Stefan Reinauer63217582012-10-29 16:52:36 -0700991 "USAGE:\n" " %s [-h]\n"
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800992 " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700993 " -H header_offset Do not search for header; use this offset*\n"
Sol Boucher67d59982015-05-07 02:39:22 -0700994 " -T Output top-aligned memory address\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700995 " -u Accept short data; fill upward/from bottom\n"
996 " -d Accept short data; fill downward/from top\n"
997 " -v Provide verbose output\n"
998 " -h Display this help message\n\n"
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000999 "COMMANDs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001000 " add [-r image,regions] -f FILE -n NAME -t TYPE \\\n"
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -07001001 " [-c compression] [-b base-address | -a alignment] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001002 "Add a component\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001003 " add-payload [-r image,regions] -f FILE -n NAME \\\n"
1004 " [-c compression] [-b base-address] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001005 "Add a payload to the ROM\n"
Patrick Georgide36d332013-08-27 20:22:21 +02001006 " (linux specific: [-C cmdline] [-I initrd])\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001007 " add-stage [-r image,regions] -f FILE -n NAME \\\n"
1008 " [-c compression] [-b base] [-S section-to-ignore] "
Aaron Durbin4be16742015-09-15 17:00:23 -05001009 " [-a alignment] [-y|--xip] [-P page-size]"
Stefan Reinauer63217582012-10-29 16:52:36 -07001010 "Add a stage to the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001011 " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n"
1012 " -l load-address -e entry-point [-c compression] \\\n"
1013 " [-b base] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001014 "Add a 32bit flat mode binary\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001015 " add-int [-r image,regions] -i INTEGER -n NAME [-b base] "
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001016 "Add a raw 64-bit integer value\n"
Patrick Georgi59e52b92015-09-10 15:28:27 +02001017 " add-master-header [-r image,regions] "
1018 "Add a legacy CBFS master header\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001019 " remove [-r image,regions] -n NAME "
Stefan Reinauer63217582012-10-29 16:52:36 -07001020 "Remove a component\n"
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001021 " copy -D new_header_offset -s region size \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001022 " [-H source header offset] "
1023 "Create a copy (duplicate) cbfs instance*\n"
1024 " create -m ARCH -s size [-b bootblock offset] \\\n"
Patrick Georgi83c2d122015-08-26 10:40:00 +02001025 " [-o CBFS offset] [-H header offset] [-B bootblock] "
Sol Boucher67a0a862015-03-18 12:36:27 -07001026 "Create a legacy ROM file with CBFS master header*\n"
1027 " create -M flashmap [-r list,of,regions,containing,cbfses] "
1028 "Create a new-style partitioned firmware image\n"
1029 " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n"
1030 " [-a align] [-T] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001031 "Find a place for a file of that size\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001032 " layout [-w] "
1033 "List mutable (or, with -w, readable) image regions\n"
1034 " print [-r image,regions] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001035 "Show the contents of the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001036 " extract [-r image,regions] -n NAME -f FILE "
Stefan Reinauer63217582012-10-29 16:52:36 -07001037 "Extracts a raw payload from ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001038 " write -r image,regions -f file [-u | -d] "
1039 "Write file into same-size [or larger] raw region\n"
1040 " read [-r fmap-region] -f file "
1041 "Extract raw region contents into binary file\n"
1042 " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n"
1043 " -x EMTPY_FIT_ENTRIES "
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001044 "Updates the FIT table with microcode entries\n"
Peter Stugeb347e0d2011-01-17 05:02:09 +00001045 "\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001046 "OFFSETs:\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001047 " Numbers accompanying -b, -H, and -o switches* may be provided\n"
1048 " in two possible formats: if their value is greater than\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001049 " 0x80000000, they are interpreted as a top-aligned x86 memory\n"
1050 " address; otherwise, they are treated as an offset into flash.\n"
David Hendricks90ca3b62012-11-16 14:48:22 -08001051 "ARCHes:\n"
Paul Burton33186922014-06-13 23:56:45 +01001052 " arm64, arm, mips, x86\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001053 "TYPEs:\n", name, name
1054 );
Stefan Reinauer07040582010-04-24 21:24:06 +00001055 print_supported_filetypes();
Sol Boucher67a0a862015-03-18 12:36:27 -07001056
1057 printf(
1058 "\n* Note that these actions and switches are only valid when\n"
1059 " working with legacy images whose structure is described\n"
1060 " primarily by a CBFS master header. New-style images, in\n"
1061 " contrast, exclusively make use of an FMAP to describe their\n"
1062 " layout: this must minimally contain an '%s' section\n"
1063 " specifying the location of this FMAP itself and a '%s'\n"
1064 " section describing the primary CBFS. It should also be noted\n"
1065 " that, when working with such images, the -F and -r switches\n"
1066 " default to '%s' for convenience, and both the -b switch to\n"
1067 " CBFS operations and the output of the locate action become\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001068 " relative to the selected CBFS region's lowest address.\n"
1069 " The one exception to this rule is the top-aligned address,\n"
1070 " which is always relative to the end of the entire image\n"
1071 " rather than relative to the local region; this is true for\n"
1072 " for both input (sufficiently large) and output (-T) data.\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001073 SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
1074 SECTION_NAME_PRIMARY_CBFS
1075 );
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001076}
1077
1078int main(int argc, char **argv)
1079{
Mathias Krause41c229c2012-07-17 21:17:15 +02001080 size_t i;
Stefan Reinauer63217582012-10-29 16:52:36 -07001081 int c;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001082
1083 if (argc < 3) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001084 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001085 return 1;
1086 }
1087
Sol Bouchere3260a02015-03-25 13:40:08 -07001088 char *image_name = argv[1];
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001089 char *cmd = argv[2];
Stefan Reinauer63217582012-10-29 16:52:36 -07001090 optind += 2;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001091
1092 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1093 if (strcmp(cmd, commands[i].name) != 0)
1094 continue;
Stefan Reinauer63217582012-10-29 16:52:36 -07001095
1096 while (1) {
1097 char *suffix = NULL;
1098 int option_index = 0;
1099
1100 c = getopt_long(argc, argv, commands[i].optstring,
1101 long_options, &option_index);
1102 if (c == -1)
1103 break;
1104
1105 /* filter out illegal long options */
zbao062730d2013-01-08 10:10:16 +08001106 if (strchr(commands[i].optstring, c) == NULL) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001107 /* TODO maybe print actual long option instead */
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001108 ERROR("%s: invalid option -- '%c'\n",
1109 argv[0], c);
Stefan Reinauer63217582012-10-29 16:52:36 -07001110 c = '?';
1111 }
1112
1113 switch(c) {
1114 case 'n':
Hung-Te Lind1739622013-01-28 14:23:49 +08001115 param.name = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001116 break;
1117 case 't':
1118 if (intfiletype(optarg) != ((uint64_t) - 1))
Hung-Te Lind1739622013-01-28 14:23:49 +08001119 param.type = intfiletype(optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001120 else
Hung-Te Lind1739622013-01-28 14:23:49 +08001121 param.type = strtoul(optarg, NULL, 0);
1122 if (param.type == 0)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001123 WARN("Unknown type '%s' ignored\n",
Stefan Reinauer63217582012-10-29 16:52:36 -07001124 optarg);
1125 break;
Sol Boucherec424862015-05-07 21:00:05 -07001126 case 'c': {
1127 int algo = cbfs_parse_comp_algo(optarg);
1128 if (algo >= 0)
1129 param.compression = algo;
Stefan Reinauer63217582012-10-29 16:52:36 -07001130 else
Sol Boucherec424862015-05-07 21:00:05 -07001131 WARN("Unknown compression '%s' ignored.\n",
1132 optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001133 break;
Sol Boucherec424862015-05-07 21:00:05 -07001134 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001135 case 'M':
1136 param.fmap = optarg;
1137 break;
1138 case 'r':
1139 param.region_name = optarg;
1140 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001141 case 'b':
Hung-Te Lind1739622013-01-28 14:23:49 +08001142 param.baseaddress = strtoul(optarg, NULL, 0);
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001143 // baseaddress may be zero on non-x86, so we
1144 // need an explicit "baseaddress_assigned".
1145 param.baseaddress = strtoul(optarg, NULL, 0);
1146 param.baseaddress_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001147 break;
1148 case 'l':
Hung-Te Lind1739622013-01-28 14:23:49 +08001149 param.loadaddress = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001150 break;
1151 case 'e':
Hung-Te Lind1739622013-01-28 14:23:49 +08001152 param.entrypoint = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001153 break;
1154 case 's':
Hung-Te Lind1739622013-01-28 14:23:49 +08001155 param.size = strtoul(optarg, &suffix, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001156 if (tolower(suffix[0])=='k') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001157 param.size *= 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001158 }
1159 if (tolower(suffix[0])=='m') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001160 param.size *= 1024 * 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001161 }
Patrick Georgie887ca52014-08-09 17:44:39 +02001162 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001163 case 'B':
Hung-Te Lind1739622013-01-28 14:23:49 +08001164 param.bootblock = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001165 break;
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001166 case 'H':
1167 param.headeroffset = strtoul(
1168 optarg, NULL, 0);
1169 param.headeroffset_assigned = 1;
1170 break;
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001171 case 'D':
1172 param.copyoffset = strtoul(optarg, NULL, 0);
1173 param.copyoffset_assigned = 1;
Vadim Bendebury11614732015-01-08 16:36:35 -08001174 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001175 case 'a':
Hung-Te Lind1739622013-01-28 14:23:49 +08001176 param.alignment = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001177 break;
Hung-Te Line9198372013-03-19 12:17:12 +08001178 case 'P':
1179 param.pagesize = strtoul(optarg, NULL, 0);
1180 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001181 case 'o':
Julius Wernerefcee762014-11-10 13:14:24 -08001182 param.cbfsoffset = strtoul(optarg, NULL, 0);
1183 param.cbfsoffset_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001184 break;
1185 case 'f':
Hung-Te Lind1739622013-01-28 14:23:49 +08001186 param.filename = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001187 break;
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001188 case 'i':
1189 param.u64val = strtoull(optarg, NULL, 0);
1190 break;
Sol Boucher67a0a862015-03-18 12:36:27 -07001191 case 'u':
1192 param.fill_partial_upward = true;
1193 break;
1194 case 'd':
1195 param.fill_partial_downward = true;
1196 break;
1197 case 'w':
1198 param.show_immutable = true;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001199 break;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001200 case 'x':
1201 param.fit_empty_entries = strtol(optarg, NULL, 0);
1202 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001203 case 'v':
1204 verbose++;
1205 break;
David Hendricks90ca3b62012-11-16 14:48:22 -08001206 case 'm':
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -06001207 param.arch = string_to_arch(optarg);
David Hendricks90ca3b62012-11-16 14:48:22 -08001208 break;
Patrick Georgide36d332013-08-27 20:22:21 +02001209 case 'I':
1210 param.initrd = optarg;
1211 break;
1212 case 'C':
1213 param.cmdline = optarg;
1214 break;
Furquan Shaikh405304a2014-10-30 11:44:20 -07001215 case 'S':
1216 param.ignore_section = optarg;
1217 break;
Aaron Durbin4be16742015-09-15 17:00:23 -05001218 case 'y':
1219 param.stage_xip = true;
1220 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001221 case 'h':
1222 case '?':
1223 usage(argv[0]);
1224 return 1;
1225 default:
1226 break;
1227 }
1228 }
1229
Sol Bouchere3260a02015-03-25 13:40:08 -07001230 if (commands[i].function == cbfs_create) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001231 if (param.fmap) {
1232 struct buffer flashmap;
1233 if (buffer_from_file(&flashmap, param.fmap))
1234 return 1;
1235 param.image_file = partitioned_file_create(
1236 image_name, &flashmap);
1237 buffer_delete(&flashmap);
1238 } else if (param.size) {
1239 param.image_file = partitioned_file_create_flat(
1240 image_name, param.size);
1241 } else {
1242 ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001243 return 1;
1244 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001245 } else {
1246 param.image_file =
Patrick Georgi2f953d32015-09-11 18:34:39 +02001247 partitioned_file_reopen(image_name);
Sol Bouchere3260a02015-03-25 13:40:08 -07001248 }
1249 if (!param.image_file)
1250 return 1;
1251
Sol Boucher67a0a862015-03-18 12:36:27 -07001252 unsigned num_regions = 1;
1253 for (const char *list = strchr(param.region_name, ','); list;
1254 list = strchr(list + 1, ','))
1255 ++num_regions;
1256
Sol Bouchere3260a02015-03-25 13:40:08 -07001257 // If the action needs to read an image region, as indicated by
1258 // having accesses_region set in its command struct, that
1259 // region's buffer struct will be stored here and the client
1260 // will receive a pointer to it via param.image_region. It
1261 // need not write the buffer back to the image file itself,
1262 // since this behavior can be requested via its modifies_region
1263 // field. Additionally, it should never free the region buffer,
1264 // as that is performed automatically once it completes.
Sol Boucher67a0a862015-03-18 12:36:27 -07001265 struct buffer image_regions[num_regions];
1266 memset(image_regions, 0, sizeof(image_regions));
Sol Bouchere3260a02015-03-25 13:40:08 -07001267
Sol Boucher67a0a862015-03-18 12:36:27 -07001268 bool seen_primary_cbfs = false;
1269 char region_name_scratch[strlen(param.region_name) + 1];
1270 strcpy(region_name_scratch, param.region_name);
1271 param.region_name = strtok(region_name_scratch, ",");
1272 for (unsigned region = 0; region < num_regions; ++region) {
1273 if (!param.region_name) {
1274 ERROR("Encountered illegal degenerate region name in -r list\n");
1275 ERROR("The image will be left unmodified.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001276 partitioned_file_close(param.image_file);
1277 return 1;
1278 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001279
Sol Boucher67a0a862015-03-18 12:36:27 -07001280 if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)
1281 == 0)
1282 seen_primary_cbfs = true;
Sol Bouchere3260a02015-03-25 13:40:08 -07001283
Sol Boucher67a0a862015-03-18 12:36:27 -07001284 param.image_region = image_regions + region;
1285 if (dispatch_command(commands[i])) {
Sol Bouchere3260a02015-03-25 13:40:08 -07001286 partitioned_file_close(param.image_file);
1287 return 1;
1288 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001289
1290 param.region_name = strtok(NULL, ",");
1291 }
1292
1293 if (commands[i].function == cbfs_create && !seen_primary_cbfs) {
1294 ERROR("The creation -r list must include the mandatory '%s' section.\n",
1295 SECTION_NAME_PRIMARY_CBFS);
1296 ERROR("The image will be left unmodified.\n");
1297 partitioned_file_close(param.image_file);
1298 return 1;
1299 }
1300
1301 if (commands[i].modifies_region) {
1302 assert(param.image_file);
1303 assert(commands[i].accesses_region);
1304 for (unsigned region = 0; region < num_regions;
1305 ++region) {
1306
1307 if (!partitioned_file_write_region(
1308 param.image_file,
1309 image_regions + region)) {
1310 partitioned_file_close(
1311 param.image_file);
1312 return 1;
1313 }
1314 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001315 }
1316
1317 partitioned_file_close(param.image_file);
Sol Boucher67a0a862015-03-18 12:36:27 -07001318 return 0;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001319 }
1320
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001321 ERROR("Unknown command '%s'.\n", cmd);
Stefan Reinauer63217582012-10-29 16:52:36 -07001322 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001323 return 1;
1324}