blob: bf589a5a27816f3290831fecaf988092ffd6dff1 [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
Sol Boucher67a0a862015-03-18 12:36:27 -0700884static bool cbfs_is_legacy_format(struct buffer *buffer)
885{
886 // Legacy CBFSes are those containing the deprecated CBFS master header.
887 return cbfs_find_header(buffer->data, buffer->size, -1);
888}
889
Stefan Reinauera1e48242011-10-21 14:24:57 -0700890static const struct command commands[] = {
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700891 {"add", "H:r:f:n:t:c:b:a:vh?", cbfs_add, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700892 {"add-flat-binary", "H:r:f:n:l:e:c:b:vh?", cbfs_add_flat_binary, true,
Sol Bouchere3260a02015-03-25 13:40:08 -0700893 true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700894 {"add-payload", "H:r:f:n:t:c:b:C:I:vh?", cbfs_add_payload, true, true},
Aaron Durbin4be16742015-09-15 17:00:23 -0500895 {"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 -0700896 {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true},
Patrick Georgi59e52b92015-09-10 15:28:27 +0200897 {"add-master-header", "H:r:vh?", cbfs_add_master_header, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700898 {"copy", "H:D:s:h?", cbfs_copy, true, true},
Patrick Georgi45acb342015-07-14 22:18:23 +0200899 {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700900 {"extract", "H:r:n:f:vh?", cbfs_extract, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700901 {"layout", "wvh?", cbfs_layout, false, false},
902 {"print", "H:r:vh?", cbfs_print, true, false},
903 {"read", "r:f:vh?", cbfs_read, true, false},
904 {"remove", "H:r:n:vh?", cbfs_remove, true, true},
Sol Boucher32532ac2015-05-06 14:44:40 -0700905 {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700906 {"write", "r:f:udvh?", cbfs_write, true, true},
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000907};
908
Stefan Reinauer63217582012-10-29 16:52:36 -0700909static struct option long_options[] = {
Julius Wernerefcee762014-11-10 13:14:24 -0800910 {"alignment", required_argument, 0, 'a' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800911 {"base-address", required_argument, 0, 'b' },
912 {"bootblock", required_argument, 0, 'B' },
Julius Wernerefcee762014-11-10 13:14:24 -0800913 {"cmdline", required_argument, 0, 'C' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800914 {"compression", required_argument, 0, 'c' },
915 {"copy-offset", required_argument, 0, 'D' },
916 {"empty-fits", required_argument, 0, 'x' },
917 {"entry-point", required_argument, 0, 'e' },
918 {"file", required_argument, 0, 'f' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700919 {"fill-downward", no_argument, 0, 'd' },
920 {"fill-upward", no_argument, 0, 'u' },
921 {"flashmap", required_argument, 0, 'M' },
922 {"fmap-regions", required_argument, 0, 'r' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800923 {"header-offset", required_argument, 0, 'H' },
Julius Wernerefcee762014-11-10 13:14:24 -0800924 {"help", no_argument, 0, 'h' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800925 {"ignore-sec", required_argument, 0, 'S' },
926 {"initrd", required_argument, 0, 'I' },
927 {"int", required_argument, 0, 'i' },
928 {"load-address", required_argument, 0, 'l' },
929 {"machine", required_argument, 0, 'm' },
930 {"name", required_argument, 0, 'n' },
931 {"offset", required_argument, 0, 'o' },
932 {"page-size", required_argument, 0, 'P' },
933 {"size", required_argument, 0, 's' },
934 {"top-aligned", required_argument, 0, 'T' },
935 {"type", required_argument, 0, 't' },
936 {"verbose", no_argument, 0, 'v' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700937 {"with-readonly", no_argument, 0, 'w' },
Aaron Durbin4be16742015-09-15 17:00:23 -0500938 {"xip", no_argument, 0, 'y' },
Julius Wernerefcee762014-11-10 13:14:24 -0800939 {NULL, 0, 0, 0 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700940};
941
Sol Boucher67a0a862015-03-18 12:36:27 -0700942static int dispatch_command(struct command command)
943{
944 if (command.accesses_region) {
945 assert(param.image_file);
946
947 if (partitioned_file_is_partitioned(param.image_file)) {
948 LOG("Performing operation on '%s' region...\n",
949 param.region_name);
950 }
951 if (!partitioned_file_read_region(param.image_region,
952 param.image_file, param.region_name)) {
953 ERROR("The image will be left unmodified.\n");
954 return 1;
955 }
956
957 if (command.modifies_region) {
958 // We (intentionally) don't support overwriting the FMAP
959 // section. If you find yourself wanting to do this,
960 // consider creating a new image rather than performing
961 // whatever hacky transformation you were planning.
962 if (region_is_flashmap(param.region_name)) {
963 ERROR("Image region '%s' is read-only because it contains the FMAP.\n",
964 param.region_name);
965 ERROR("The image will be left unmodified.\n");
966 return 1;
967 }
968 // We don't allow writing raw data to regions that
969 // contain nested regions, since doing so would
970 // overwrite all such subregions.
971 if (partitioned_file_region_contains_nested(
972 param.image_file, param.region_name)) {
973 ERROR("Image region '%s' is read-only because it contains nested regions.\n",
974 param.region_name);
975 ERROR("The image will be left unmodified.\n");
976 return 1;
977 }
978 }
979 }
980
981 if (command.function()) {
982 if (partitioned_file_is_partitioned(param.image_file)) {
983 ERROR("Failed while operating on '%s' region!\n",
984 param.region_name);
985 ERROR("The image will be left unmodified.\n");
986 }
987 return 1;
988 }
989
990 return 0;
991}
992
Stefan Reinauer63217582012-10-29 16:52:36 -0700993static void usage(char *name)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000994{
995 printf
Stefan Reinauer07040582010-04-24 21:24:06 +0000996 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
Stefan Reinauer63217582012-10-29 16:52:36 -0700997 "USAGE:\n" " %s [-h]\n"
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800998 " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700999 " -H header_offset Do not search for header; use this offset*\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001000 " -T Output top-aligned memory address\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001001 " -u Accept short data; fill upward/from bottom\n"
1002 " -d Accept short data; fill downward/from top\n"
1003 " -v Provide verbose output\n"
1004 " -h Display this help message\n\n"
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001005 "COMMANDs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001006 " add [-r image,regions] -f FILE -n NAME -t TYPE \\\n"
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -07001007 " [-c compression] [-b base-address | -a alignment] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001008 "Add a component\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001009 " add-payload [-r image,regions] -f FILE -n NAME \\\n"
1010 " [-c compression] [-b base-address] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001011 "Add a payload to the ROM\n"
Patrick Georgide36d332013-08-27 20:22:21 +02001012 " (linux specific: [-C cmdline] [-I initrd])\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001013 " add-stage [-r image,regions] -f FILE -n NAME \\\n"
1014 " [-c compression] [-b base] [-S section-to-ignore] "
Aaron Durbin4be16742015-09-15 17:00:23 -05001015 " [-a alignment] [-y|--xip] [-P page-size]"
Stefan Reinauer63217582012-10-29 16:52:36 -07001016 "Add a stage to the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001017 " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n"
1018 " -l load-address -e entry-point [-c compression] \\\n"
1019 " [-b base] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001020 "Add a 32bit flat mode binary\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001021 " add-int [-r image,regions] -i INTEGER -n NAME [-b base] "
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001022 "Add a raw 64-bit integer value\n"
Patrick Georgi59e52b92015-09-10 15:28:27 +02001023 " add-master-header [-r image,regions] "
1024 "Add a legacy CBFS master header\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001025 " remove [-r image,regions] -n NAME "
Stefan Reinauer63217582012-10-29 16:52:36 -07001026 "Remove a component\n"
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001027 " copy -D new_header_offset -s region size \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001028 " [-H source header offset] "
1029 "Create a copy (duplicate) cbfs instance*\n"
1030 " create -m ARCH -s size [-b bootblock offset] \\\n"
Patrick Georgi83c2d122015-08-26 10:40:00 +02001031 " [-o CBFS offset] [-H header offset] [-B bootblock] "
Sol Boucher67a0a862015-03-18 12:36:27 -07001032 "Create a legacy ROM file with CBFS master header*\n"
1033 " create -M flashmap [-r list,of,regions,containing,cbfses] "
1034 "Create a new-style partitioned firmware image\n"
1035 " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n"
1036 " [-a align] [-T] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001037 "Find a place for a file of that size\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001038 " layout [-w] "
1039 "List mutable (or, with -w, readable) image regions\n"
1040 " print [-r image,regions] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001041 "Show the contents of the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001042 " extract [-r image,regions] -n NAME -f FILE "
Stefan Reinauer63217582012-10-29 16:52:36 -07001043 "Extracts a raw payload from ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001044 " write -r image,regions -f file [-u | -d] "
1045 "Write file into same-size [or larger] raw region\n"
1046 " read [-r fmap-region] -f file "
1047 "Extract raw region contents into binary file\n"
1048 " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n"
1049 " -x EMTPY_FIT_ENTRIES "
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001050 "Updates the FIT table with microcode entries\n"
Peter Stugeb347e0d2011-01-17 05:02:09 +00001051 "\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001052 "OFFSETs:\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001053 " Numbers accompanying -b, -H, and -o switches* may be provided\n"
1054 " in two possible formats: if their value is greater than\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001055 " 0x80000000, they are interpreted as a top-aligned x86 memory\n"
1056 " address; otherwise, they are treated as an offset into flash.\n"
David Hendricks90ca3b62012-11-16 14:48:22 -08001057 "ARCHes:\n"
Paul Burton33186922014-06-13 23:56:45 +01001058 " arm64, arm, mips, x86\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001059 "TYPEs:\n", name, name
1060 );
Stefan Reinauer07040582010-04-24 21:24:06 +00001061 print_supported_filetypes();
Sol Boucher67a0a862015-03-18 12:36:27 -07001062
1063 printf(
1064 "\n* Note that these actions and switches are only valid when\n"
1065 " working with legacy images whose structure is described\n"
1066 " primarily by a CBFS master header. New-style images, in\n"
1067 " contrast, exclusively make use of an FMAP to describe their\n"
1068 " layout: this must minimally contain an '%s' section\n"
1069 " specifying the location of this FMAP itself and a '%s'\n"
1070 " section describing the primary CBFS. It should also be noted\n"
1071 " that, when working with such images, the -F and -r switches\n"
1072 " default to '%s' for convenience, and both the -b switch to\n"
1073 " CBFS operations and the output of the locate action become\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001074 " relative to the selected CBFS region's lowest address.\n"
1075 " The one exception to this rule is the top-aligned address,\n"
1076 " which is always relative to the end of the entire image\n"
1077 " rather than relative to the local region; this is true for\n"
1078 " for both input (sufficiently large) and output (-T) data.\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001079 SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
1080 SECTION_NAME_PRIMARY_CBFS
1081 );
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001082}
1083
1084int main(int argc, char **argv)
1085{
Mathias Krause41c229c2012-07-17 21:17:15 +02001086 size_t i;
Stefan Reinauer63217582012-10-29 16:52:36 -07001087 int c;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001088
1089 if (argc < 3) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001090 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001091 return 1;
1092 }
1093
Sol Bouchere3260a02015-03-25 13:40:08 -07001094 char *image_name = argv[1];
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001095 char *cmd = argv[2];
Stefan Reinauer63217582012-10-29 16:52:36 -07001096 optind += 2;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001097
1098 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1099 if (strcmp(cmd, commands[i].name) != 0)
1100 continue;
Stefan Reinauer63217582012-10-29 16:52:36 -07001101
1102 while (1) {
1103 char *suffix = NULL;
1104 int option_index = 0;
1105
1106 c = getopt_long(argc, argv, commands[i].optstring,
1107 long_options, &option_index);
1108 if (c == -1)
1109 break;
1110
1111 /* filter out illegal long options */
zbao062730d2013-01-08 10:10:16 +08001112 if (strchr(commands[i].optstring, c) == NULL) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001113 /* TODO maybe print actual long option instead */
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001114 ERROR("%s: invalid option -- '%c'\n",
1115 argv[0], c);
Stefan Reinauer63217582012-10-29 16:52:36 -07001116 c = '?';
1117 }
1118
1119 switch(c) {
1120 case 'n':
Hung-Te Lind1739622013-01-28 14:23:49 +08001121 param.name = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001122 break;
1123 case 't':
1124 if (intfiletype(optarg) != ((uint64_t) - 1))
Hung-Te Lind1739622013-01-28 14:23:49 +08001125 param.type = intfiletype(optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001126 else
Hung-Te Lind1739622013-01-28 14:23:49 +08001127 param.type = strtoul(optarg, NULL, 0);
1128 if (param.type == 0)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001129 WARN("Unknown type '%s' ignored\n",
Stefan Reinauer63217582012-10-29 16:52:36 -07001130 optarg);
1131 break;
Sol Boucherec424862015-05-07 21:00:05 -07001132 case 'c': {
1133 int algo = cbfs_parse_comp_algo(optarg);
1134 if (algo >= 0)
1135 param.compression = algo;
Stefan Reinauer63217582012-10-29 16:52:36 -07001136 else
Sol Boucherec424862015-05-07 21:00:05 -07001137 WARN("Unknown compression '%s' ignored.\n",
1138 optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001139 break;
Sol Boucherec424862015-05-07 21:00:05 -07001140 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001141 case 'M':
1142 param.fmap = optarg;
1143 break;
1144 case 'r':
1145 param.region_name = optarg;
1146 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001147 case 'b':
Hung-Te Lind1739622013-01-28 14:23:49 +08001148 param.baseaddress = strtoul(optarg, NULL, 0);
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001149 // baseaddress may be zero on non-x86, so we
1150 // need an explicit "baseaddress_assigned".
1151 param.baseaddress = strtoul(optarg, NULL, 0);
1152 param.baseaddress_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001153 break;
1154 case 'l':
Hung-Te Lind1739622013-01-28 14:23:49 +08001155 param.loadaddress = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001156 break;
1157 case 'e':
Hung-Te Lind1739622013-01-28 14:23:49 +08001158 param.entrypoint = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001159 break;
1160 case 's':
Hung-Te Lind1739622013-01-28 14:23:49 +08001161 param.size = strtoul(optarg, &suffix, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001162 if (tolower(suffix[0])=='k') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001163 param.size *= 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001164 }
1165 if (tolower(suffix[0])=='m') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001166 param.size *= 1024 * 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001167 }
Patrick Georgie887ca52014-08-09 17:44:39 +02001168 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001169 case 'B':
Hung-Te Lind1739622013-01-28 14:23:49 +08001170 param.bootblock = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001171 break;
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001172 case 'H':
1173 param.headeroffset = strtoul(
1174 optarg, NULL, 0);
1175 param.headeroffset_assigned = 1;
1176 break;
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001177 case 'D':
1178 param.copyoffset = strtoul(optarg, NULL, 0);
1179 param.copyoffset_assigned = 1;
Vadim Bendebury11614732015-01-08 16:36:35 -08001180 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001181 case 'a':
Hung-Te Lind1739622013-01-28 14:23:49 +08001182 param.alignment = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001183 break;
Hung-Te Line9198372013-03-19 12:17:12 +08001184 case 'P':
1185 param.pagesize = strtoul(optarg, NULL, 0);
1186 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001187 case 'o':
Julius Wernerefcee762014-11-10 13:14:24 -08001188 param.cbfsoffset = strtoul(optarg, NULL, 0);
1189 param.cbfsoffset_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001190 break;
1191 case 'f':
Hung-Te Lind1739622013-01-28 14:23:49 +08001192 param.filename = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001193 break;
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001194 case 'i':
1195 param.u64val = strtoull(optarg, NULL, 0);
1196 break;
Sol Boucher67a0a862015-03-18 12:36:27 -07001197 case 'u':
1198 param.fill_partial_upward = true;
1199 break;
1200 case 'd':
1201 param.fill_partial_downward = true;
1202 break;
1203 case 'w':
1204 param.show_immutable = true;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001205 break;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001206 case 'x':
1207 param.fit_empty_entries = strtol(optarg, NULL, 0);
1208 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001209 case 'v':
1210 verbose++;
1211 break;
David Hendricks90ca3b62012-11-16 14:48:22 -08001212 case 'm':
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -06001213 param.arch = string_to_arch(optarg);
David Hendricks90ca3b62012-11-16 14:48:22 -08001214 break;
Patrick Georgide36d332013-08-27 20:22:21 +02001215 case 'I':
1216 param.initrd = optarg;
1217 break;
1218 case 'C':
1219 param.cmdline = optarg;
1220 break;
Furquan Shaikh405304a2014-10-30 11:44:20 -07001221 case 'S':
1222 param.ignore_section = optarg;
1223 break;
Aaron Durbin4be16742015-09-15 17:00:23 -05001224 case 'y':
1225 param.stage_xip = true;
1226 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001227 case 'h':
1228 case '?':
1229 usage(argv[0]);
1230 return 1;
1231 default:
1232 break;
1233 }
1234 }
1235
Sol Bouchere3260a02015-03-25 13:40:08 -07001236 if (commands[i].function == cbfs_create) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001237 if (param.fmap) {
1238 struct buffer flashmap;
1239 if (buffer_from_file(&flashmap, param.fmap))
1240 return 1;
1241 param.image_file = partitioned_file_create(
1242 image_name, &flashmap);
1243 buffer_delete(&flashmap);
1244 } else if (param.size) {
1245 param.image_file = partitioned_file_create_flat(
1246 image_name, param.size);
1247 } else {
1248 ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001249 return 1;
1250 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001251 } else {
1252 param.image_file =
1253 partitioned_file_reopen(image_name,
Sol Boucher67a0a862015-03-18 12:36:27 -07001254 cbfs_is_legacy_format);
Sol Bouchere3260a02015-03-25 13:40:08 -07001255 }
1256 if (!param.image_file)
1257 return 1;
1258
Sol Boucher67a0a862015-03-18 12:36:27 -07001259 unsigned num_regions = 1;
1260 for (const char *list = strchr(param.region_name, ','); list;
1261 list = strchr(list + 1, ','))
1262 ++num_regions;
1263
Sol Bouchere3260a02015-03-25 13:40:08 -07001264 // If the action needs to read an image region, as indicated by
1265 // having accesses_region set in its command struct, that
1266 // region's buffer struct will be stored here and the client
1267 // will receive a pointer to it via param.image_region. It
1268 // need not write the buffer back to the image file itself,
1269 // since this behavior can be requested via its modifies_region
1270 // field. Additionally, it should never free the region buffer,
1271 // as that is performed automatically once it completes.
Sol Boucher67a0a862015-03-18 12:36:27 -07001272 struct buffer image_regions[num_regions];
1273 memset(image_regions, 0, sizeof(image_regions));
Sol Bouchere3260a02015-03-25 13:40:08 -07001274
Sol Boucher67a0a862015-03-18 12:36:27 -07001275 bool seen_primary_cbfs = false;
1276 char region_name_scratch[strlen(param.region_name) + 1];
1277 strcpy(region_name_scratch, param.region_name);
1278 param.region_name = strtok(region_name_scratch, ",");
1279 for (unsigned region = 0; region < num_regions; ++region) {
1280 if (!param.region_name) {
1281 ERROR("Encountered illegal degenerate region name in -r list\n");
1282 ERROR("The image will be left unmodified.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001283 partitioned_file_close(param.image_file);
1284 return 1;
1285 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001286
Sol Boucher67a0a862015-03-18 12:36:27 -07001287 if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)
1288 == 0)
1289 seen_primary_cbfs = true;
Sol Bouchere3260a02015-03-25 13:40:08 -07001290
Sol Boucher67a0a862015-03-18 12:36:27 -07001291 param.image_region = image_regions + region;
1292 if (dispatch_command(commands[i])) {
Sol Bouchere3260a02015-03-25 13:40:08 -07001293 partitioned_file_close(param.image_file);
1294 return 1;
1295 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001296
1297 param.region_name = strtok(NULL, ",");
1298 }
1299
1300 if (commands[i].function == cbfs_create && !seen_primary_cbfs) {
1301 ERROR("The creation -r list must include the mandatory '%s' section.\n",
1302 SECTION_NAME_PRIMARY_CBFS);
1303 ERROR("The image will be left unmodified.\n");
1304 partitioned_file_close(param.image_file);
1305 return 1;
1306 }
1307
1308 if (commands[i].modifies_region) {
1309 assert(param.image_file);
1310 assert(commands[i].accesses_region);
1311 for (unsigned region = 0; region < num_regions;
1312 ++region) {
1313
1314 if (!partitioned_file_write_region(
1315 param.image_file,
1316 image_regions + region)) {
1317 partitioned_file_close(
1318 param.image_file);
1319 return 1;
1320 }
1321 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001322 }
1323
1324 partitioned_file_close(param.image_file);
Sol Boucher67a0a862015-03-18 12:36:27 -07001325 return 0;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001326 }
1327
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001328 ERROR("Unknown command '%s'.\n", cmd);
Stefan Reinauer63217582012-10-29 16:52:36 -07001329 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001330 return 1;
1331}