blob: 252929628f21fd9976a6a06a7f7a25a6dbbe05aa [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.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000016 */
17
Patrick Georgib7b56dd82009-09-14 13:29:27 +000018#include <stdio.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070019#include <stdlib.h>
Stefan Reinauer336daa72009-12-21 15:09:01 +000020#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080021#include <strings.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070022#include <ctype.h>
Stefan Reinauer63217582012-10-29 16:52:36 -070023#include <unistd.h>
24#include <getopt.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000025#include "common.h"
26#include "cbfs.h"
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080027#include "cbfs_image.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070028#include "cbfs_sections.h"
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060029#include "fit.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070030#include "partitioned_file.h"
Aaron Durbin8a414a02015-10-01 17:02:45 -050031#include <commonlib/fsp1_1.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000032
Sol Boucher32532ac2015-05-06 14:44:40 -070033#define SECTION_WITH_FIT_TABLE "BOOTBLOCK"
34
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000035struct command {
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000036 const char *name;
Stefan Reinauer63217582012-10-29 16:52:36 -070037 const char *optstring;
38 int (*function) (void);
Sol Bouchere3260a02015-03-25 13:40:08 -070039 // Whether to populate param.image_region before invoking function
40 bool accesses_region;
41 // Whether to write that region's contents back to image_file at the end
42 bool modifies_region;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000043};
44
Hung-Te Lind1739622013-01-28 14:23:49 +080045static struct param {
Sol Bouchere3260a02015-03-25 13:40:08 -070046 partitioned_file_t *image_file;
47 struct buffer *image_region;
Sol Boucher67a0a862015-03-18 12:36:27 -070048 const char *name;
49 const char *filename;
50 const char *fmap;
51 const char *region_name;
52 const char *bootblock;
53 const char *ignore_section;
Peter Stuge3bfd5b82013-07-09 19:39:13 +020054 uint64_t u64val;
Hung-Te Lind1739622013-01-28 14:23:49 +080055 uint32_t type;
56 uint32_t baseaddress;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080057 uint32_t baseaddress_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080058 uint32_t loadaddress;
Vadim Bendebury5e273a42014-12-23 19:26:54 -080059 uint32_t copyoffset;
60 uint32_t copyoffset_assigned;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080061 uint32_t headeroffset;
62 uint32_t headeroffset_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080063 uint32_t entrypoint;
64 uint32_t size;
65 uint32_t alignment;
Hung-Te Line9198372013-03-19 12:17:12 +080066 uint32_t pagesize;
Julius Wernerefcee762014-11-10 13:14:24 -080067 uint32_t cbfsoffset;
68 uint32_t cbfsoffset_assigned;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060069 uint32_t arch;
Sol Boucher67a0a862015-03-18 12:36:27 -070070 bool fill_partial_upward;
71 bool fill_partial_downward;
72 bool show_immutable;
Aaron Durbin4be16742015-09-15 17:00:23 -050073 bool stage_xip;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060074 int fit_empty_entries;
Sol Boucher65336712015-05-07 21:00:05 -070075 enum comp_algo compression;
Patrick Georgi89f20342015-10-01 15:54:04 +020076 enum vb2_hash_algorithm hash;
Patrick Georgide36d332013-08-27 20:22:21 +020077 /* for linux payloads */
78 char *initrd;
79 char *cmdline;
Hung-Te Lind1739622013-01-28 14:23:49 +080080} param = {
81 /* All variables not listed are initialized as zero. */
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060082 .arch = CBFS_ARCHITECTURE_UNKNOWN,
Sol Boucher65336712015-05-07 21:00:05 -070083 .compression = CBFS_COMPRESS_NONE,
Patrick Georgi89f20342015-10-01 15:54:04 +020084 .hash = VB2_HASH_INVALID,
Vadim Bendebury458a12e2014-12-23 15:10:12 -080085 .headeroffset = ~0,
Sol Boucher67a0a862015-03-18 12:36:27 -070086 .region_name = SECTION_NAME_PRIMARY_CBFS,
Hung-Te Lind1739622013-01-28 14:23:49 +080087};
Stefan Reinauer63217582012-10-29 16:52:36 -070088
Sol Boucher67a0a862015-03-18 12:36:27 -070089static bool region_is_flashmap(const char *region)
90{
91 return partitioned_file_region_check_magic(param.image_file, region,
92 FMAP_SIGNATURE, strlen(FMAP_SIGNATURE));
93}
94
95/* @return Same as cbfs_is_valid_cbfs(), but for a named region. */
96static bool region_is_modern_cbfs(const char *region)
97{
98 return partitioned_file_region_check_magic(param.image_file, region,
99 CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC));
100}
101
Sol Boucher67d59982015-05-07 02:39:22 -0700102/*
103 * Converts between offsets from the start of the specified image region and
Patrick Georgi97311192015-12-07 23:14:07 +0100104 * "top-aligned" offsets from the top of the image region. Works in either
Sol Boucher67d59982015-05-07 02:39:22 -0700105 * direction: pass in one type of offset and receive the other type.
106 * N.B. A top-aligned offset is always a positive number, and should not be
107 * confused with a top-aliged *address*, which is its arithmetic inverse. */
108static unsigned convert_to_from_top_aligned(const struct buffer *region,
109 unsigned offset)
110{
111 assert(region);
112
Patrick Georgi97311192015-12-07 23:14:07 +0100113 /* cover the situation where a negative base address is given by the
114 * user. Callers of this function negate it, so it'll be a positive
115 * number smaller than the region.
116 */
117 if ((offset > 0) && (offset < region->size)) {
118 return region->size - offset;
119 }
120
Sol Boucher67d59982015-05-07 02:39:22 -0700121 size_t image_size = partitioned_file_total_size(param.image_file);
122 return image_size - region->offset - offset;
123}
124
Aaron Durbin4be16742015-09-15 17:00:23 -0500125static int do_cbfs_locate(int32_t *cbfs_addr, size_t metadata_size)
126{
127 if (!param.filename) {
128 ERROR("You need to specify -f/--filename.\n");
129 return 1;
130 }
131
132 if (!param.name) {
133 ERROR("You need to specify -n/--name.\n");
134 return 1;
135 }
136
137 struct cbfs_image image;
138 if (cbfs_image_from_buffer(&image, param.image_region,
139 param.headeroffset))
140 return 1;
141
142 if (cbfs_get_entry(&image, param.name))
143 WARN("'%s' already in CBFS.\n", param.name);
144
145 struct buffer buffer;
146 if (buffer_from_file(&buffer, param.filename) != 0) {
147 ERROR("Cannot load %s.\n", param.filename);
148 return 1;
149 }
150
151 /* Include cbfs_file size along with space for with name. */
152 metadata_size += cbfs_calculate_file_header_size(param.name);
153
154 int32_t address = cbfs_locate_entry(&image, buffer.size, param.pagesize,
155 param.alignment, metadata_size);
156 buffer_delete(&buffer);
157
158 if (address == -1) {
159 ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n",
160 param.name, param.pagesize, param.alignment);
161 return 1;
162 }
163
Aaron Durbin4be16742015-09-15 17:00:23 -0500164 *cbfs_addr = address;
165 return 0;
166}
167
Patrick Georgif8204972015-08-11 15:16:24 +0200168typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200169 struct cbfs_file *header);
Stefan Reinauer63217582012-10-29 16:52:36 -0700170
Sol Bouchere3260a02015-03-25 13:40:08 -0700171static int cbfs_add_integer_component(const char *name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200172 uint64_t u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800173 uint32_t offset,
174 uint32_t headeroffset) {
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200175 struct cbfs_image image;
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200176 struct cbfs_file *header = NULL;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200177 struct buffer buffer;
178 int i, ret = 1;
179
180 if (!name) {
181 ERROR("You need to specify -n/--name.\n");
182 return 1;
183 }
184
185 if (buffer_create(&buffer, 8, name) != 0)
186 return 1;
187
188 for (i = 0; i < 8; i++)
189 buffer.data[i] = (u64val >> i*8) & 0xff;
190
Sol Bouchere3260a02015-03-25 13:40:08 -0700191 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) {
192 ERROR("Selected image region is not a CBFS.\n");
193 goto done;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200194 }
195
196 if (cbfs_get_entry(&image, name)) {
197 ERROR("'%s' already in ROM image.\n", name);
198 goto done;
199 }
200
Sol Boucher67d59982015-05-07 02:39:22 -0700201 if (IS_TOP_ALIGNED_ADDRESS(offset))
202 offset = convert_to_from_top_aligned(param.image_region,
203 -offset);
204
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200205 header = cbfs_create_file_header(CBFS_COMPONENT_RAW,
206 buffer.size, name);
Patrick Georgif5252f32015-08-25 22:27:57 +0200207 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Sol Boucher0e539312015-03-05 15:38:03 -0800208 ERROR("Failed to add %llu into ROM image as '%s'.\n",
209 (long long unsigned)u64val, name);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200210 goto done;
211 }
212
Sol Bouchere3260a02015-03-25 13:40:08 -0700213 ret = 0;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200214
215done:
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200216 free(header);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200217 buffer_delete(&buffer);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200218 return ret;
219}
220
Patrick Georgi59e52b92015-09-10 15:28:27 +0200221static int cbfs_add_master_header(void)
222{
223 const char * const name = "cbfs master header";
224 struct cbfs_image image;
225 struct cbfs_file *header = NULL;
226 struct buffer buffer;
227 int ret = 1;
228
229 if (cbfs_image_from_buffer(&image, param.image_region,
230 param.headeroffset)) {
231 ERROR("Selected image region is not a CBFS.\n");
232 return 1;
233 }
234
235 if (cbfs_get_entry(&image, name)) {
236 ERROR("'%s' already in ROM image.\n", name);
237 return 1;
238 }
239
240 if (buffer_create(&buffer, sizeof(struct cbfs_header), name) != 0)
241 return 1;
242
243 struct cbfs_header *h = (struct cbfs_header *)buffer.data;
244 h->magic = htonl(CBFS_HEADER_MAGIC);
245 h->version = htonl(CBFS_HEADER_VERSION);
246 h->romsize = htonl(param.image_region->size);
247 /* The 4 bytes are left out for two reasons:
248 * 1. the cbfs master header pointer resides there
249 * 2. some cbfs implementations assume that an image that resides
250 * below 4GB has a bootblock and get confused when the end of the
251 * image is at 4GB == 0.
252 */
253 h->bootblocksize = htonl(4);
254 h->align = htonl(CBFS_ENTRY_ALIGNMENT);
255 /* offset relative to romsize above, which covers precisely the CBFS
256 * region.
257 */
258 h->offset = htonl(0);
259 h->architecture = htonl(CBFS_ARCHITECTURE_UNKNOWN);
260
261 header = cbfs_create_file_header(CBFS_COMPONENT_CBFSHEADER,
262 buffer_size(&buffer), name);
263 if (cbfs_add_entry(&image, &buffer, 0, header) != 0) {
264 ERROR("Failed to add cbfs master header into ROM image.\n");
265 goto done;
266 }
267
268 struct cbfs_file *entry;
269 if ((entry = cbfs_get_entry(&image, name)) == NULL) {
270 ERROR("'%s' not in ROM image?!?\n", name);
271 goto done;
272 }
273
274 uint32_t header_offset = CBFS_SUBHEADER(entry) -
275 buffer_get(&image.buffer);
276 header_offset = -(buffer_size(&image.buffer) - header_offset);
277
278 // TODO: when we have a BE target, we'll need to store this as BE
279 *(uint32_t *)(buffer_get(&image.buffer) +
280 buffer_size(&image.buffer) - 4) =
Aaron Durbin4b93a4f2015-09-21 13:10:13 -0500281 swab32(htonl(header_offset));
Patrick Georgi59e52b92015-09-10 15:28:27 +0200282
283 ret = 0;
284
285done:
286 free(header);
287 buffer_delete(&buffer);
288 return ret;
289}
290
Sol Bouchere3260a02015-03-25 13:40:08 -0700291static int cbfs_add_component(const char *filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800292 const char *name,
293 uint32_t type,
294 uint32_t offset,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800295 uint32_t headeroffset,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100296 convert_buffer_t convert)
297{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800298 if (!filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800299 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700300 return 1;
301 }
302
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800303 if (!name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800304 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700305 return 1;
306 }
307
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800308 if (type == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800309 ERROR("You need to specify a valid -t/--type.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700310 return 1;
311 }
312
Sol Bouchere3260a02015-03-25 13:40:08 -0700313 struct cbfs_image image;
Sol Boucher67a0a862015-03-18 12:36:27 -0700314 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset))
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800315 return 1;
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800316
Patrick Georgi4e54bf92015-08-11 14:35:39 +0200317 if (cbfs_get_entry(&image, name)) {
318 ERROR("'%s' already in ROM image.\n", name);
319 return 1;
320 }
321
Sol Bouchere3260a02015-03-25 13:40:08 -0700322 struct buffer buffer;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800323 if (buffer_from_file(&buffer, filename) != 0) {
324 ERROR("Could not load file '%s'.\n", filename);
Stefan Reinauer63217582012-10-29 16:52:36 -0700325 return 1;
326 }
327
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200328 struct cbfs_file *header =
329 cbfs_create_file_header(type, buffer.size, name);
Patrick Georgi4110abc2015-08-11 15:10:33 +0200330
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200331 if (convert && convert(&buffer, &offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800332 ERROR("Failed to parse file '%s'.\n", filename);
333 buffer_delete(&buffer);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000334 return 1;
Stefan Reinauerfbadc492011-10-14 12:44:14 -0700335 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700336
Patrick Georgi89f20342015-10-01 15:54:04 +0200337 if (param.hash != VB2_HASH_INVALID)
338 if (cbfs_add_file_hash(header, &buffer, param.hash) == -1) {
339 ERROR("couldn't add hash for '%s'\n", name);
340 free(header);
341 buffer_delete(&buffer);
342 return 1;
343 }
344
Sol Boucher67d59982015-05-07 02:39:22 -0700345 if (IS_TOP_ALIGNED_ADDRESS(offset))
346 offset = convert_to_from_top_aligned(param.image_region,
347 -offset);
348
Patrick Georgif5252f32015-08-25 22:27:57 +0200349 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800350 ERROR("Failed to add '%s' into ROM image.\n", filename);
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200351 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800352 buffer_delete(&buffer);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800353 return 1;
354 }
355
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200356 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800357 buffer_delete(&buffer);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000358 return 0;
359}
360
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700361static int cbfstool_convert_raw(struct buffer *buffer,
362 unused uint32_t *offset, struct cbfs_file *header)
363{
364 char *compressed;
365 int compressed_size;
366
367 comp_func_ptr compress = compression_function(param.compression);
368 if (!compress)
369 return -1;
370 compressed = calloc(buffer->size, 1);
371
372 if (compress(buffer->data, buffer->size,
373 compressed, &compressed_size)) {
374 WARN("Compression failed - disabled\n");
375 } else {
376 struct cbfs_file_attr_compression *attrs =
377 (struct cbfs_file_attr_compression *)
378 cbfs_add_file_attr(header,
379 CBFS_FILE_ATTR_TAG_COMPRESSION,
380 sizeof(struct cbfs_file_attr_compression));
381 if (attrs == NULL)
382 return -1;
383 attrs->compression = htonl(param.compression);
384 attrs->decompressed_size = htonl(buffer->size);
385
386 free(buffer->data);
387 buffer->data = compressed;
388 buffer->size = compressed_size;
389
390 header->len = htonl(buffer->size);
391 }
392 return 0;
393}
394
Aaron Durbin8a414a02015-10-01 17:02:45 -0500395static int cbfstool_convert_fsp(struct buffer *buffer,
396 uint32_t *offset, struct cbfs_file *header)
397{
398 uint32_t address;
399 struct buffer fsp;
400
401 address = *offset;
402
403 /* Ensure the address is a memory mapped one. */
404 if (!IS_TOP_ALIGNED_ADDRESS(address))
405 address = -convert_to_from_top_aligned(param.image_region,
406 address);
407
408 /* Create a copy of the buffer to attempt relocation. */
409 if (buffer_create(&fsp, buffer_size(buffer), "fsp"))
410 return -1;
411
412 memcpy(buffer_get(&fsp), buffer_get(buffer), buffer_size(buffer));
413
414 /* Replace the buffer contents w/ the relocated ones on success. */
415 if (fsp1_1_relocate(address, buffer_get(&fsp), buffer_size(&fsp)) > 0) {
416 buffer_delete(buffer);
417 buffer_clone(buffer, &fsp);
418 } else {
419 buffer_delete(&fsp);
420 WARN("FSP was not a 1.1 variant.\n");
421 }
422
423 /* Let the raw path handle all the cbfs metadata logic. */
424 return cbfstool_convert_raw(buffer, offset, header);
425}
426
Patrick Georgif8204972015-08-11 15:16:24 +0200427static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200428 struct cbfs_file *header)
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600429{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800430 struct buffer output;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600431 int ret;
Aaron Durbin4be16742015-09-15 17:00:23 -0500432
433 if (param.stage_xip) {
434 int32_t address;
435
436 if (do_cbfs_locate(&address, sizeof(struct cbfs_stage))) {
437 ERROR("Could not find location for XIP stage.\n");
438 return 1;
439 }
440
441 /* Pass in a top aligned address. */
442 address = -convert_to_from_top_aligned(param.image_region,
443 address);
444 *offset = address;
445
446 ret = parse_elf_to_xip_stage(buffer, &output, offset,
447 param.ignore_section);
448 } else
449 ret = parse_elf_to_stage(buffer, &output, param.compression,
450 offset, param.ignore_section);
451
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600452 if (ret != 0)
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800453 return -1;
454 buffer_delete(buffer);
455 // direct assign, no dupe.
456 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200457 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800458 return 0;
459}
460
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100461static int cbfstool_convert_mkpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200462 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100463{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800464 struct buffer output;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800465 int ret;
466 /* per default, try and see if payload is an ELF binary */
Sol Boucher65336712015-05-07 21:00:05 -0700467 ret = parse_elf_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800468
469 /* If it's not an ELF, see if it's a UEFI FV */
470 if (ret != 0)
Sol Boucher65336712015-05-07 21:00:05 -0700471 ret = parse_fv_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800472
Patrick Georgide36d332013-08-27 20:22:21 +0200473 /* If it's neither ELF nor UEFI Fv, try bzImage */
474 if (ret != 0)
475 ret = parse_bzImage_to_payload(buffer, &output,
Sol Boucher65336712015-05-07 21:00:05 -0700476 param.initrd, param.cmdline, param.compression);
Patrick Georgide36d332013-08-27 20:22:21 +0200477
Stefan Reinauer543a6822013-02-04 15:39:13 -0800478 /* Not a supported payload type */
479 if (ret != 0) {
480 ERROR("Not a supported payload type (ELF / FV).\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700481 buffer_delete(buffer);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800482 return -1;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800483 }
484
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800485 buffer_delete(buffer);
486 // direct assign, no dupe.
487 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200488 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800489 return 0;
490}
491
492static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200493 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100494{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800495 struct buffer output;
496 if (parse_flat_binary_to_payload(buffer, &output,
497 param.loadaddress,
498 param.entrypoint,
Sol Boucher65336712015-05-07 21:00:05 -0700499 param.compression) != 0) {
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800500 return -1;
501 }
502 buffer_delete(buffer);
503 // direct assign, no dupe.
504 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200505 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800506 return 0;
507}
508
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800509static int cbfs_add(void)
510{
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700511 int32_t address;
Aaron Durbin8a414a02015-10-01 17:02:45 -0500512 convert_buffer_t convert;
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700513
514 if (param.alignment && param.baseaddress) {
515 ERROR("Cannot specify both alignment and base address\n");
516 return 1;
517 }
518
Aaron Durbin8a414a02015-10-01 17:02:45 -0500519 convert = cbfstool_convert_raw;
520
521 /* Set the alignment to 4KiB minimum for FSP blobs when no base address
522 * is provided so that relocation can occur. */
523 if (param.type == CBFS_COMPONENT_FSP) {
524 if (!param.baseaddress_assigned)
525 param.alignment = 4*1024;
526 convert = cbfstool_convert_fsp;
527 }
528
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700529 if (param.alignment) {
Aaron Durbin9b9d4b32015-09-15 21:30:58 -0500530 /* CBFS compression file attribute is unconditionally added. */
531 size_t metadata_sz = sizeof(struct cbfs_file_attr_compression);
532 if (do_cbfs_locate(&address, metadata_sz))
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700533 return 1;
534 param.baseaddress = address;
535 }
536
Sol Bouchere3260a02015-03-25 13:40:08 -0700537 return cbfs_add_component(param.filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800538 param.name,
539 param.type,
540 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800541 param.headeroffset,
Aaron Durbin8a414a02015-10-01 17:02:45 -0500542 convert);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800543}
544
Stefan Reinauer63217582012-10-29 16:52:36 -0700545static int cbfs_add_stage(void)
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700546{
Aaron Durbin4be16742015-09-15 17:00:23 -0500547 if (param.stage_xip) {
548 if (param.baseaddress_assigned) {
549 ERROR("Cannot specify base address for XIP.\n");
550 return 1;
551 }
552
553 if (param.compression != CBFS_COMPRESS_NONE) {
554 ERROR("Cannot specify compression for XIP.\n");
555 return 1;
556 }
557 }
558
Sol Bouchere3260a02015-03-25 13:40:08 -0700559 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800560 param.name,
561 CBFS_COMPONENT_STAGE,
562 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800563 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800564 cbfstool_convert_mkstage);
565}
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700566
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800567static int cbfs_add_payload(void)
568{
Sol Bouchere3260a02015-03-25 13:40:08 -0700569 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800570 param.name,
571 CBFS_COMPONENT_PAYLOAD,
572 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800573 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800574 cbfstool_convert_mkpayload);
Stefan Reinauer63217582012-10-29 16:52:36 -0700575}
576
577static int cbfs_add_flat_binary(void)
578{
Hung-Te Lind1739622013-01-28 14:23:49 +0800579 if (param.loadaddress == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800580 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700581 "-l/--load-address.\n");
582 return 1;
583 }
Hung-Te Lind1739622013-01-28 14:23:49 +0800584 if (param.entrypoint == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800585 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700586 "-e/--entry-point.\n");
587 return 1;
588 }
Sol Bouchere3260a02015-03-25 13:40:08 -0700589 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800590 param.name,
591 CBFS_COMPONENT_PAYLOAD,
592 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800593 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800594 cbfstool_convert_mkflatpayload);
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700595}
596
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200597static int cbfs_add_integer(void)
598{
Sol Bouchere3260a02015-03-25 13:40:08 -0700599 return cbfs_add_integer_component(param.name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200600 param.u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800601 param.baseaddress,
602 param.headeroffset);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200603}
604
Stefan Reinauer63217582012-10-29 16:52:36 -0700605static int cbfs_remove(void)
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800606{
Hung-Te Lind1739622013-01-28 14:23:49 +0800607 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800608 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700609 return 1;
610 }
611
Sol Bouchere3260a02015-03-25 13:40:08 -0700612 struct cbfs_image image;
613 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700614 param.headeroffset))
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800615 return 1;
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800616
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800617 if (cbfs_remove_entry(&image, param.name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800618 ERROR("Removing file '%s' failed.\n",
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800619 param.name);
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800620 return 1;
621 }
622
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800623 return 0;
624}
625
Stefan Reinauer63217582012-10-29 16:52:36 -0700626static int cbfs_create(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000627{
Sol Boucher67a0a862015-03-18 12:36:27 -0700628 struct cbfs_image image;
629 memset(&image, 0, sizeof(image));
630 buffer_clone(&image.buffer, param.image_region);
631
632 if (param.fmap) {
633 if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size ||
634 param.baseaddress_assigned ||
635 param.headeroffset_assigned ||
636 param.cbfsoffset_assigned ||
Sol Boucher67a0a862015-03-18 12:36:27 -0700637 param.bootblock) {
Patrick Georgi45acb342015-07-14 22:18:23 +0200638 ERROR("Since -M was provided, -m, -s, -b, -o, -H, and -B should be omitted\n");
Sol Boucher67a0a862015-03-18 12:36:27 -0700639 return 1;
640 }
641
642 return cbfs_image_create(&image, image.buffer.size);
643 }
644
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600645 if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) {
Stefan Reinauer60a4a732013-03-28 16:46:07 -0700646 ERROR("You need to specify -m/--machine arch.\n");
David Hendricks90ca3b62012-11-16 14:48:22 -0800647 return 1;
648 }
649
Sol Bouchere3260a02015-03-25 13:40:08 -0700650 struct buffer bootblock;
Julius Wernerefcee762014-11-10 13:14:24 -0800651 if (!param.bootblock) {
652 DEBUG("-B not given, creating image without bootblock.\n");
653 buffer_create(&bootblock, 0, "(dummy)");
654 } else if (buffer_from_file(&bootblock, param.bootblock)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800655 return 1;
656 }
657
Julius Wernerefcee762014-11-10 13:14:24 -0800658 if (!param.alignment)
Patrick Georgi45acb342015-07-14 22:18:23 +0200659 param.alignment = CBFS_ALIGNMENT;
Julius Wernerefcee762014-11-10 13:14:24 -0800660
661 // Set default offsets. x86, as usual, needs to be a special snowflake.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800662 if (!param.baseaddress_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800663 if (param.arch == CBFS_ARCHITECTURE_X86) {
664 // Make sure there's at least enough room for rel_offset
Sol Boucher67a0a862015-03-18 12:36:27 -0700665 param.baseaddress = param.size -
666 MAX(bootblock.size, sizeof(int32_t));
Julius Wernerefcee762014-11-10 13:14:24 -0800667 DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n",
668 param.baseaddress);
669 } else {
670 param.baseaddress = 0;
671 DEBUG("bootblock starts at address 0x0.\n");
672 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800673 }
674 if (!param.headeroffset_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800675 if (param.arch == CBFS_ARCHITECTURE_X86) {
676 param.headeroffset = param.baseaddress -
677 sizeof(struct cbfs_header);
678 DEBUG("x86 -> CBFS header before bootblock (%#x).\n",
679 param.headeroffset);
680 } else {
681 param.headeroffset = align_up(param.baseaddress +
682 bootblock.size, sizeof(uint32_t));
683 DEBUG("CBFS header placed behind bootblock (%#x).\n",
684 param.headeroffset);
685 }
686 }
687 if (!param.cbfsoffset_assigned) {
688 if (param.arch == CBFS_ARCHITECTURE_X86) {
689 param.cbfsoffset = 0;
690 DEBUG("x86 -> CBFS entries start at address 0x0.\n");
691 } else {
692 param.cbfsoffset = align_up(param.headeroffset +
693 sizeof(struct cbfs_header),
Patrick Georgi45acb342015-07-14 22:18:23 +0200694 CBFS_ALIGNMENT);
Julius Wernerefcee762014-11-10 13:14:24 -0800695 DEBUG("CBFS entries start beind master header (%#x).\n",
696 param.cbfsoffset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800697 }
698 }
699
Sol Boucher67a0a862015-03-18 12:36:27 -0700700 int ret = cbfs_legacy_image_create(&image,
701 param.arch,
Patrick Georgi45acb342015-07-14 22:18:23 +0200702 CBFS_ALIGNMENT,
Sol Boucher67a0a862015-03-18 12:36:27 -0700703 &bootblock,
704 param.baseaddress,
705 param.headeroffset,
706 param.cbfsoffset);
Sol Bouchere3260a02015-03-25 13:40:08 -0700707 buffer_delete(&bootblock);
Sol Boucher67a0a862015-03-18 12:36:27 -0700708 return ret;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000709}
710
Sol Boucher67a0a862015-03-18 12:36:27 -0700711static int cbfs_layout(void)
712{
713 const struct fmap *fmap = partitioned_file_get_fmap(param.image_file);
714 if (!fmap) {
715 LOG("This is a legacy image composed entirely of a single CBFS.\n");
716 return 1;
717 }
718
719 printf("This image contains the following sections that can be %s with this tool:\n",
720 param.show_immutable ? "accessed" : "manipulated");
721 puts("");
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300722 for (unsigned i = 0; i < fmap->nareas; ++i) {
723 const struct fmap_area *current = fmap->areas + i;
Sol Boucher67a0a862015-03-18 12:36:27 -0700724
725 bool readonly = partitioned_file_fmap_count(param.image_file,
726 partitioned_file_fmap_select_children_of, current) ||
727 region_is_flashmap((const char *)current->name);
728 if (!param.show_immutable && readonly)
729 continue;
730
731 printf("'%s'", current->name);
732
733 // Detect consecutive sections that describe the same region and
734 // show them as aliases. This cannot find equivalent entries
735 // that aren't adjacent; however, fmaptool doesn't generate
736 // FMAPs with such sections, so this convenience feature works
737 // for all but the strangest manually created FMAP binaries.
738 // TODO: This could be done by parsing the FMAP into some kind
739 // of tree that had duplicate lists in addition to child lists,
740 // which would allow covering that weird, unlikely case as well.
741 unsigned lookahead;
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300742 for (lookahead = 1; i + lookahead < fmap->nareas;
Sol Boucher67a0a862015-03-18 12:36:27 -0700743 ++lookahead) {
744 const struct fmap_area *consecutive =
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300745 fmap->areas + i + lookahead;
Sol Boucher67a0a862015-03-18 12:36:27 -0700746 if (consecutive->offset != current->offset ||
747 consecutive->size != current->size)
748 break;
749 printf(", '%s'", consecutive->name);
750 }
751 if (lookahead > 1)
752 fputs(" are aliases for the same region", stdout);
753
754 const char *qualifier = "";
755 if (readonly)
756 qualifier = "read-only, ";
757 else if (region_is_modern_cbfs((const char *)current->name))
758 qualifier = "CBFS, ";
759 printf(" (%ssize %u)\n", qualifier, current->size);
760
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300761 i += lookahead - 1;
Sol Boucher67a0a862015-03-18 12:36:27 -0700762 }
763 puts("");
764
765 if (param.show_immutable) {
766 puts("It is at least possible to perform the read action on every section listed above.");
767 } else {
768 puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above.");
769 puts("To see the image's read-only sections as well, rerun with the -w option.");
770 }
771
772 return 0;
773}
774
Stefan Reinauer63217582012-10-29 16:52:36 -0700775static int cbfs_print(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000776{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800777 struct cbfs_image image;
Sol Bouchere3260a02015-03-25 13:40:08 -0700778 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700779 param.headeroffset))
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000780 return 1;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800781 cbfs_print_directory(&image);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000782 return 0;
783}
784
Stefan Reinauer63217582012-10-29 16:52:36 -0700785static int cbfs_extract(void)
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000786{
Hung-Te Lind1739622013-01-28 14:23:49 +0800787 if (!param.filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800788 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700789 return 1;
790 }
791
Hung-Te Lind1739622013-01-28 14:23:49 +0800792 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800793 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700794 return 1;
795 }
796
Sol Bouchere3260a02015-03-25 13:40:08 -0700797 struct cbfs_image image;
798 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700799 param.headeroffset))
800 return 1;
801
Aaron Durbin17625022015-10-27 13:17:52 -0500802 return cbfs_export_entry(&image, param.name, param.filename,
803 param.arch);
Sol Boucher67a0a862015-03-18 12:36:27 -0700804}
805
806static int cbfs_write(void)
807{
808 if (!param.filename) {
809 ERROR("You need to specify a valid input -f/--file.\n");
810 return 1;
811 }
812 if (!partitioned_file_is_partitioned(param.image_file)) {
813 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700814 return 1;
815 }
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000816
Sol Boucher67a0a862015-03-18 12:36:27 -0700817 if (region_is_modern_cbfs(param.region_name)) {
818 ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n",
819 param.region_name);
820 return 1;
821 }
822
823 struct buffer new_content;
824 if (buffer_from_file(&new_content, param.filename))
825 return 1;
826
827 if (buffer_check_magic(&new_content, FMAP_SIGNATURE,
828 strlen(FMAP_SIGNATURE))) {
829 ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n",
830 param.filename);
831 buffer_delete(&new_content);
832 return 1;
833 }
834 if (buffer_check_magic(&new_content, CBFS_FILE_MAGIC,
835 strlen(CBFS_FILE_MAGIC))) {
836 ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n",
837 param.filename);
838 buffer_delete(&new_content);
839 return 1;
840 }
841
842 unsigned offset = 0;
843 if (param.fill_partial_upward && param.fill_partial_downward) {
844 ERROR("You may only specify one of -u and -d.\n");
845 buffer_delete(&new_content);
846 return 1;
847 } else if (!param.fill_partial_upward && !param.fill_partial_downward) {
848 if (new_content.size != param.image_region->size) {
849 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",
850 new_content.size, param.image_region->size);
851 buffer_delete(&new_content);
852 return 1;
853 }
854 } else {
855 if (new_content.size > param.image_region->size) {
856 ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n",
857 new_content.size, param.image_region->size);
858 buffer_delete(&new_content);
859 return 1;
860 }
861 WARN("Written area will abut %s of target region: any unused space will keep its current contents\n",
862 param.fill_partial_upward ? "bottom" : "top");
863 if (param.fill_partial_downward)
864 offset = param.image_region->size - new_content.size;
865 }
866
867 memcpy(param.image_region->data + offset, new_content.data,
868 new_content.size);
869 buffer_delete(&new_content);
870 return 0;
871}
872
873static int cbfs_read(void)
874{
875 if (!param.filename) {
876 ERROR("You need to specify a valid output -f/--file.\n");
877 return 1;
878 }
879 if (!partitioned_file_is_partitioned(param.image_file)) {
880 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
881 return 1;
882 }
883
884 return buffer_write_file(param.image_region, param.filename);
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000885}
886
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600887static int cbfs_update_fit(void)
888{
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600889 if (!param.name) {
890 ERROR("You need to specify -n/--name.\n");
891 return 1;
892 }
893
894 if (param.fit_empty_entries <= 0) {
895 ERROR("Invalid number of fit entries "
896 "(-x/--empty-fits): %d\n", param.fit_empty_entries);
897 return 1;
898 }
899
Sol Boucher32532ac2015-05-06 14:44:40 -0700900 struct buffer bootblock;
Patrick Georgi6dd99fc2015-09-19 14:04:45 +0200901 // The bootblock is part of the CBFS on x86
902 buffer_clone(&bootblock, param.image_region);
Sol Boucher32532ac2015-05-06 14:44:40 -0700903
Sol Bouchere3260a02015-03-25 13:40:08 -0700904 struct cbfs_image image;
905 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700906 param.headeroffset))
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600907 return 1;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600908
Sol Boucher32532ac2015-05-06 14:44:40 -0700909 if (fit_update_table(&bootblock, &image, param.name,
910 param.fit_empty_entries, convert_to_from_top_aligned))
911 return 1;
912
913 // The region to be written depends on the type of image, so we write it
914 // here rather than having main() write the CBFS region back as usual.
915 return !partitioned_file_write_region(param.image_file, &bootblock);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600916}
917
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800918static int cbfs_copy(void)
919{
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800920 if (!param.copyoffset_assigned) {
Patrick Georgi821bcd62015-10-16 13:37:21 +0200921 ERROR("You need to specify -D/--copy-offset.\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800922 return 1;
923 }
924
925 if (!param.size) {
926 ERROR("You need to specify -s/--size.\n");
927 return 1;
928 }
929
Sol Bouchere3260a02015-03-25 13:40:08 -0700930 struct cbfs_image image;
931 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700932 param.headeroffset))
933 return 1;
934
935 if (!cbfs_is_legacy_cbfs(&image)) {
936 ERROR("This operation is only valid on legacy images having CBFS master headers\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800937 return 1;
Sol Bouchere3260a02015-03-25 13:40:08 -0700938 }
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800939
Sol Bouchere3260a02015-03-25 13:40:08 -0700940 return cbfs_copy_instance(&image, param.copyoffset, param.size);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800941}
942
Stefan Reinauera1e48242011-10-21 14:24:57 -0700943static const struct command commands[] = {
Patrick Georgi89f20342015-10-01 15:54:04 +0200944 {"add", "H:r:f:n:t:c:b:a:vA:h?", cbfs_add, true, true},
945 {"add-flat-binary", "H:r:f:n:l:e:c:b:vA:h?", cbfs_add_flat_binary, true,
Sol Bouchere3260a02015-03-25 13:40:08 -0700946 true},
Patrick Georgi89f20342015-10-01 15:54:04 +0200947 {"add-payload", "H:r:f:n:t:c:b:C:I:vA:h?", cbfs_add_payload, true, true},
948 {"add-stage", "a:H:r:f:n:t:c:b:P:S:yvA:h?", cbfs_add_stage, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700949 {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true},
Patrick Georgi59e52b92015-09-10 15:28:27 +0200950 {"add-master-header", "H:r:vh?", cbfs_add_master_header, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700951 {"copy", "H:D:s:h?", cbfs_copy, true, true},
Patrick Georgi45acb342015-07-14 22:18:23 +0200952 {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true},
Aaron Durbin17625022015-10-27 13:17:52 -0500953 {"extract", "H:r:m:n:f:vh?", cbfs_extract, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700954 {"layout", "wvh?", cbfs_layout, false, false},
955 {"print", "H:r:vh?", cbfs_print, true, false},
956 {"read", "r:f:vh?", cbfs_read, true, false},
957 {"remove", "H:r:n:vh?", cbfs_remove, true, true},
Sol Boucher32532ac2015-05-06 14:44:40 -0700958 {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700959 {"write", "r:f:udvh?", cbfs_write, true, true},
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000960};
961
Stefan Reinauer63217582012-10-29 16:52:36 -0700962static struct option long_options[] = {
Julius Wernerefcee762014-11-10 13:14:24 -0800963 {"alignment", required_argument, 0, 'a' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800964 {"base-address", required_argument, 0, 'b' },
965 {"bootblock", required_argument, 0, 'B' },
Julius Wernerefcee762014-11-10 13:14:24 -0800966 {"cmdline", required_argument, 0, 'C' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800967 {"compression", required_argument, 0, 'c' },
968 {"copy-offset", required_argument, 0, 'D' },
969 {"empty-fits", required_argument, 0, 'x' },
970 {"entry-point", required_argument, 0, 'e' },
971 {"file", required_argument, 0, 'f' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700972 {"fill-downward", no_argument, 0, 'd' },
973 {"fill-upward", no_argument, 0, 'u' },
974 {"flashmap", required_argument, 0, 'M' },
975 {"fmap-regions", required_argument, 0, 'r' },
Patrick Georgi89f20342015-10-01 15:54:04 +0200976 {"hash-algorithm",required_argument, 0, 'A' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800977 {"header-offset", required_argument, 0, 'H' },
Julius Wernerefcee762014-11-10 13:14:24 -0800978 {"help", no_argument, 0, 'h' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800979 {"ignore-sec", required_argument, 0, 'S' },
980 {"initrd", required_argument, 0, 'I' },
981 {"int", required_argument, 0, 'i' },
982 {"load-address", required_argument, 0, 'l' },
983 {"machine", required_argument, 0, 'm' },
984 {"name", required_argument, 0, 'n' },
985 {"offset", required_argument, 0, 'o' },
986 {"page-size", required_argument, 0, 'P' },
987 {"size", required_argument, 0, 's' },
988 {"top-aligned", required_argument, 0, 'T' },
989 {"type", required_argument, 0, 't' },
990 {"verbose", no_argument, 0, 'v' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700991 {"with-readonly", no_argument, 0, 'w' },
Aaron Durbin4be16742015-09-15 17:00:23 -0500992 {"xip", no_argument, 0, 'y' },
Julius Wernerefcee762014-11-10 13:14:24 -0800993 {NULL, 0, 0, 0 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700994};
995
Sol Boucher67a0a862015-03-18 12:36:27 -0700996static int dispatch_command(struct command command)
997{
998 if (command.accesses_region) {
999 assert(param.image_file);
1000
1001 if (partitioned_file_is_partitioned(param.image_file)) {
1002 LOG("Performing operation on '%s' region...\n",
1003 param.region_name);
1004 }
1005 if (!partitioned_file_read_region(param.image_region,
1006 param.image_file, param.region_name)) {
1007 ERROR("The image will be left unmodified.\n");
1008 return 1;
1009 }
1010
1011 if (command.modifies_region) {
1012 // We (intentionally) don't support overwriting the FMAP
1013 // section. If you find yourself wanting to do this,
1014 // consider creating a new image rather than performing
1015 // whatever hacky transformation you were planning.
1016 if (region_is_flashmap(param.region_name)) {
1017 ERROR("Image region '%s' is read-only because it contains the FMAP.\n",
1018 param.region_name);
1019 ERROR("The image will be left unmodified.\n");
1020 return 1;
1021 }
1022 // We don't allow writing raw data to regions that
1023 // contain nested regions, since doing so would
1024 // overwrite all such subregions.
1025 if (partitioned_file_region_contains_nested(
1026 param.image_file, param.region_name)) {
1027 ERROR("Image region '%s' is read-only because it contains nested regions.\n",
1028 param.region_name);
1029 ERROR("The image will be left unmodified.\n");
1030 return 1;
1031 }
1032 }
1033 }
1034
1035 if (command.function()) {
1036 if (partitioned_file_is_partitioned(param.image_file)) {
1037 ERROR("Failed while operating on '%s' region!\n",
1038 param.region_name);
1039 ERROR("The image will be left unmodified.\n");
1040 }
1041 return 1;
1042 }
1043
1044 return 0;
1045}
1046
Stefan Reinauer63217582012-10-29 16:52:36 -07001047static void usage(char *name)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001048{
1049 printf
Stefan Reinauer07040582010-04-24 21:24:06 +00001050 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001051 "USAGE:\n" " %s [-h]\n"
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001052 " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001053 " -H header_offset Do not search for header; use this offset*\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001054 " -T Output top-aligned memory address\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001055 " -u Accept short data; fill upward/from bottom\n"
1056 " -d Accept short data; fill downward/from top\n"
1057 " -v Provide verbose output\n"
1058 " -h Display this help message\n\n"
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001059 "COMMANDs:\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001060 " add [-r image,regions] -f FILE -n NAME -t TYPE [-A hash] \\\n"
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -07001061 " [-c compression] [-b base-address | -a alignment] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001062 "Add a component\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001063 " add-payload [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001064 " [-c compression] [-b base-address] \\\n"
1065 " (linux specific: [-C cmdline] [-I initrd]) "
Stefan Reinauer63217582012-10-29 16:52:36 -07001066 "Add a payload to the ROM\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001067 " add-stage [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001068 " [-c compression] [-b base] [-S section-to-ignore] \\\n"
1069 " [-a alignment] [-y|--xip] [-P page-size] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001070 "Add a stage to the ROM\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001071 " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n"
1072 " [-A hash] -l load-address -e entry-point \\\n"
1073 " [-c compression] [-b base] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001074 "Add a 32bit flat mode binary\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001075 " add-int [-r image,regions] -i INTEGER -n NAME [-b base] "
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001076 "Add a raw 64-bit integer value\n"
Patrick Georgi59e52b92015-09-10 15:28:27 +02001077 " add-master-header [-r image,regions] "
1078 "Add a legacy CBFS master header\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001079 " remove [-r image,regions] -n NAME "
Stefan Reinauer63217582012-10-29 16:52:36 -07001080 "Remove a component\n"
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001081 " copy -D new_header_offset -s region size \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001082 " [-H source header offset] "
1083 "Create a copy (duplicate) cbfs instance*\n"
1084 " create -m ARCH -s size [-b bootblock offset] \\\n"
Patrick Georgi83c2d122015-08-26 10:40:00 +02001085 " [-o CBFS offset] [-H header offset] [-B bootblock] "
Sol Boucher67a0a862015-03-18 12:36:27 -07001086 "Create a legacy ROM file with CBFS master header*\n"
1087 " create -M flashmap [-r list,of,regions,containing,cbfses] "
1088 "Create a new-style partitioned firmware image\n"
1089 " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n"
1090 " [-a align] [-T] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001091 "Find a place for a file of that size\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001092 " layout [-w] "
1093 "List mutable (or, with -w, readable) image regions\n"
1094 " print [-r image,regions] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001095 "Show the contents of the ROM\n"
Aaron Durbin17625022015-10-27 13:17:52 -05001096 " extract [-r image,regions] [-m ARCH] -n NAME -f FILE "
Stefan Reinauer63217582012-10-29 16:52:36 -07001097 "Extracts a raw payload from ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001098 " write -r image,regions -f file [-u | -d] "
1099 "Write file into same-size [or larger] raw region\n"
1100 " read [-r fmap-region] -f file "
1101 "Extract raw region contents into binary file\n"
1102 " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n"
Martin Roth0eae3642015-12-04 11:27:11 -07001103 " -x EMTPY_FIT_ENTRIES "
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001104 "Updates the FIT table with microcode entries\n"
Peter Stugeb347e0d2011-01-17 05:02:09 +00001105 "\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001106 "OFFSETs:\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001107 " Numbers accompanying -b, -H, and -o switches* may be provided\n"
1108 " in two possible formats: if their value is greater than\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001109 " 0x80000000, they are interpreted as a top-aligned x86 memory\n"
1110 " address; otherwise, they are treated as an offset into flash.\n"
David Hendricks90ca3b62012-11-16 14:48:22 -08001111 "ARCHes:\n"
Paul Burton33186922014-06-13 23:56:45 +01001112 " arm64, arm, mips, x86\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001113 "TYPEs:\n", name, name
1114 );
Stefan Reinauer07040582010-04-24 21:24:06 +00001115 print_supported_filetypes();
Sol Boucher67a0a862015-03-18 12:36:27 -07001116
1117 printf(
1118 "\n* Note that these actions and switches are only valid when\n"
1119 " working with legacy images whose structure is described\n"
1120 " primarily by a CBFS master header. New-style images, in\n"
1121 " contrast, exclusively make use of an FMAP to describe their\n"
1122 " layout: this must minimally contain an '%s' section\n"
1123 " specifying the location of this FMAP itself and a '%s'\n"
1124 " section describing the primary CBFS. It should also be noted\n"
1125 " that, when working with such images, the -F and -r switches\n"
1126 " default to '%s' for convenience, and both the -b switch to\n"
1127 " CBFS operations and the output of the locate action become\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001128 " relative to the selected CBFS region's lowest address.\n"
1129 " The one exception to this rule is the top-aligned address,\n"
1130 " which is always relative to the end of the entire image\n"
1131 " rather than relative to the local region; this is true for\n"
1132 " for both input (sufficiently large) and output (-T) data.\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001133 SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
1134 SECTION_NAME_PRIMARY_CBFS
1135 );
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001136}
1137
1138int main(int argc, char **argv)
1139{
Mathias Krause41c229c2012-07-17 21:17:15 +02001140 size_t i;
Stefan Reinauer63217582012-10-29 16:52:36 -07001141 int c;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001142
1143 if (argc < 3) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001144 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001145 return 1;
1146 }
1147
Sol Bouchere3260a02015-03-25 13:40:08 -07001148 char *image_name = argv[1];
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001149 char *cmd = argv[2];
Stefan Reinauer63217582012-10-29 16:52:36 -07001150 optind += 2;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001151
1152 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1153 if (strcmp(cmd, commands[i].name) != 0)
1154 continue;
Stefan Reinauer63217582012-10-29 16:52:36 -07001155
1156 while (1) {
1157 char *suffix = NULL;
1158 int option_index = 0;
1159
1160 c = getopt_long(argc, argv, commands[i].optstring,
1161 long_options, &option_index);
1162 if (c == -1)
1163 break;
1164
1165 /* filter out illegal long options */
zbao062730d2013-01-08 10:10:16 +08001166 if (strchr(commands[i].optstring, c) == NULL) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001167 /* TODO maybe print actual long option instead */
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001168 ERROR("%s: invalid option -- '%c'\n",
1169 argv[0], c);
Stefan Reinauer63217582012-10-29 16:52:36 -07001170 c = '?';
1171 }
1172
1173 switch(c) {
1174 case 'n':
Hung-Te Lind1739622013-01-28 14:23:49 +08001175 param.name = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001176 break;
1177 case 't':
1178 if (intfiletype(optarg) != ((uint64_t) - 1))
Hung-Te Lind1739622013-01-28 14:23:49 +08001179 param.type = intfiletype(optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001180 else
Hung-Te Lind1739622013-01-28 14:23:49 +08001181 param.type = strtoul(optarg, NULL, 0);
1182 if (param.type == 0)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001183 WARN("Unknown type '%s' ignored\n",
Stefan Reinauer63217582012-10-29 16:52:36 -07001184 optarg);
1185 break;
Sol Boucherec424862015-05-07 21:00:05 -07001186 case 'c': {
1187 int algo = cbfs_parse_comp_algo(optarg);
1188 if (algo >= 0)
1189 param.compression = algo;
Stefan Reinauer63217582012-10-29 16:52:36 -07001190 else
Sol Boucherec424862015-05-07 21:00:05 -07001191 WARN("Unknown compression '%s' ignored.\n",
1192 optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001193 break;
Sol Boucherec424862015-05-07 21:00:05 -07001194 }
Patrick Georgi89f20342015-10-01 15:54:04 +02001195 case 'A': {
1196 int algo = cbfs_parse_hash_algo(optarg);
1197 if (algo >= 0)
1198 param.hash = algo;
1199 else {
1200 ERROR("Unknown hash algorithm '%s'.\n",
1201 optarg);
1202 return 1;
1203 }
1204 break;
1205 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001206 case 'M':
1207 param.fmap = optarg;
1208 break;
1209 case 'r':
1210 param.region_name = optarg;
1211 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001212 case 'b':
Hung-Te Lind1739622013-01-28 14:23:49 +08001213 param.baseaddress = strtoul(optarg, NULL, 0);
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001214 // baseaddress may be zero on non-x86, so we
1215 // need an explicit "baseaddress_assigned".
1216 param.baseaddress = strtoul(optarg, NULL, 0);
1217 param.baseaddress_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001218 break;
1219 case 'l':
Hung-Te Lind1739622013-01-28 14:23:49 +08001220 param.loadaddress = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001221 break;
1222 case 'e':
Hung-Te Lind1739622013-01-28 14:23:49 +08001223 param.entrypoint = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001224 break;
1225 case 's':
Hung-Te Lind1739622013-01-28 14:23:49 +08001226 param.size = strtoul(optarg, &suffix, 0);
Stefan Reinauer3a043c42015-09-15 17:37:14 -07001227 if (tolower((int)suffix[0])=='k') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001228 param.size *= 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001229 }
Stefan Reinauer3a043c42015-09-15 17:37:14 -07001230 if (tolower((int)suffix[0])=='m') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001231 param.size *= 1024 * 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001232 }
Patrick Georgie887ca52014-08-09 17:44:39 +02001233 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001234 case 'B':
Hung-Te Lind1739622013-01-28 14:23:49 +08001235 param.bootblock = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001236 break;
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001237 case 'H':
1238 param.headeroffset = strtoul(
1239 optarg, NULL, 0);
1240 param.headeroffset_assigned = 1;
1241 break;
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001242 case 'D':
1243 param.copyoffset = strtoul(optarg, NULL, 0);
1244 param.copyoffset_assigned = 1;
Vadim Bendebury11614732015-01-08 16:36:35 -08001245 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001246 case 'a':
Hung-Te Lind1739622013-01-28 14:23:49 +08001247 param.alignment = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001248 break;
Hung-Te Line9198372013-03-19 12:17:12 +08001249 case 'P':
1250 param.pagesize = strtoul(optarg, NULL, 0);
1251 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001252 case 'o':
Julius Wernerefcee762014-11-10 13:14:24 -08001253 param.cbfsoffset = strtoul(optarg, NULL, 0);
1254 param.cbfsoffset_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001255 break;
1256 case 'f':
Hung-Te Lind1739622013-01-28 14:23:49 +08001257 param.filename = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001258 break;
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001259 case 'i':
1260 param.u64val = strtoull(optarg, NULL, 0);
1261 break;
Sol Boucher67a0a862015-03-18 12:36:27 -07001262 case 'u':
1263 param.fill_partial_upward = true;
1264 break;
1265 case 'd':
1266 param.fill_partial_downward = true;
1267 break;
1268 case 'w':
1269 param.show_immutable = true;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001270 break;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001271 case 'x':
1272 param.fit_empty_entries = strtol(optarg, NULL, 0);
1273 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001274 case 'v':
1275 verbose++;
1276 break;
David Hendricks90ca3b62012-11-16 14:48:22 -08001277 case 'm':
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -06001278 param.arch = string_to_arch(optarg);
David Hendricks90ca3b62012-11-16 14:48:22 -08001279 break;
Patrick Georgide36d332013-08-27 20:22:21 +02001280 case 'I':
1281 param.initrd = optarg;
1282 break;
1283 case 'C':
1284 param.cmdline = optarg;
1285 break;
Furquan Shaikh405304a2014-10-30 11:44:20 -07001286 case 'S':
1287 param.ignore_section = optarg;
1288 break;
Aaron Durbin4be16742015-09-15 17:00:23 -05001289 case 'y':
1290 param.stage_xip = true;
1291 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001292 case 'h':
1293 case '?':
1294 usage(argv[0]);
1295 return 1;
1296 default:
1297 break;
1298 }
1299 }
1300
Sol Bouchere3260a02015-03-25 13:40:08 -07001301 if (commands[i].function == cbfs_create) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001302 if (param.fmap) {
1303 struct buffer flashmap;
1304 if (buffer_from_file(&flashmap, param.fmap))
1305 return 1;
1306 param.image_file = partitioned_file_create(
1307 image_name, &flashmap);
1308 buffer_delete(&flashmap);
1309 } else if (param.size) {
1310 param.image_file = partitioned_file_create_flat(
1311 image_name, param.size);
1312 } else {
1313 ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001314 return 1;
1315 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001316 } else {
1317 param.image_file =
Patrick Georgi2f953d32015-09-11 18:34:39 +02001318 partitioned_file_reopen(image_name);
Sol Bouchere3260a02015-03-25 13:40:08 -07001319 }
1320 if (!param.image_file)
1321 return 1;
1322
Sol Boucher67a0a862015-03-18 12:36:27 -07001323 unsigned num_regions = 1;
1324 for (const char *list = strchr(param.region_name, ','); list;
1325 list = strchr(list + 1, ','))
1326 ++num_regions;
1327
Sol Bouchere3260a02015-03-25 13:40:08 -07001328 // If the action needs to read an image region, as indicated by
1329 // having accesses_region set in its command struct, that
1330 // region's buffer struct will be stored here and the client
1331 // will receive a pointer to it via param.image_region. It
1332 // need not write the buffer back to the image file itself,
1333 // since this behavior can be requested via its modifies_region
1334 // field. Additionally, it should never free the region buffer,
1335 // as that is performed automatically once it completes.
Sol Boucher67a0a862015-03-18 12:36:27 -07001336 struct buffer image_regions[num_regions];
1337 memset(image_regions, 0, sizeof(image_regions));
Sol Bouchere3260a02015-03-25 13:40:08 -07001338
Sol Boucher67a0a862015-03-18 12:36:27 -07001339 bool seen_primary_cbfs = false;
1340 char region_name_scratch[strlen(param.region_name) + 1];
1341 strcpy(region_name_scratch, param.region_name);
1342 param.region_name = strtok(region_name_scratch, ",");
1343 for (unsigned region = 0; region < num_regions; ++region) {
1344 if (!param.region_name) {
1345 ERROR("Encountered illegal degenerate region name in -r list\n");
1346 ERROR("The image will be left unmodified.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001347 partitioned_file_close(param.image_file);
1348 return 1;
1349 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001350
Sol Boucher67a0a862015-03-18 12:36:27 -07001351 if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)
1352 == 0)
1353 seen_primary_cbfs = true;
Sol Bouchere3260a02015-03-25 13:40:08 -07001354
Sol Boucher67a0a862015-03-18 12:36:27 -07001355 param.image_region = image_regions + region;
1356 if (dispatch_command(commands[i])) {
Sol Bouchere3260a02015-03-25 13:40:08 -07001357 partitioned_file_close(param.image_file);
1358 return 1;
1359 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001360
1361 param.region_name = strtok(NULL, ",");
1362 }
1363
1364 if (commands[i].function == cbfs_create && !seen_primary_cbfs) {
1365 ERROR("The creation -r list must include the mandatory '%s' section.\n",
1366 SECTION_NAME_PRIMARY_CBFS);
1367 ERROR("The image will be left unmodified.\n");
1368 partitioned_file_close(param.image_file);
1369 return 1;
1370 }
1371
1372 if (commands[i].modifies_region) {
1373 assert(param.image_file);
1374 assert(commands[i].accesses_region);
1375 for (unsigned region = 0; region < num_regions;
1376 ++region) {
1377
1378 if (!partitioned_file_write_region(
1379 param.image_file,
1380 image_regions + region)) {
1381 partitioned_file_close(
1382 param.image_file);
1383 return 1;
1384 }
1385 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001386 }
1387
1388 partitioned_file_close(param.image_file);
Sol Boucher67a0a862015-03-18 12:36:27 -07001389 return 0;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001390 }
1391
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001392 ERROR("Unknown command '%s'.\n", cmd);
Stefan Reinauer63217582012-10-29 16:52:36 -07001393 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001394 return 1;
1395}