blob: 5d87c9f0d31d814ce9b21ebe521c686764e366ba [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"
Aaron Durbin8a414a02015-10-01 17:02:45 -050035#include <commonlib/fsp1_1.h>
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000036
Sol Boucher32532ac2015-05-06 14:44:40 -070037#define SECTION_WITH_FIT_TABLE "BOOTBLOCK"
38
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000039struct command {
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000040 const char *name;
Stefan Reinauer63217582012-10-29 16:52:36 -070041 const char *optstring;
42 int (*function) (void);
Sol Bouchere3260a02015-03-25 13:40:08 -070043 // Whether to populate param.image_region before invoking function
44 bool accesses_region;
45 // Whether to write that region's contents back to image_file at the end
46 bool modifies_region;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000047};
48
Hung-Te Lind1739622013-01-28 14:23:49 +080049static struct param {
Sol Bouchere3260a02015-03-25 13:40:08 -070050 partitioned_file_t *image_file;
51 struct buffer *image_region;
Sol Boucher67a0a862015-03-18 12:36:27 -070052 const char *name;
53 const char *filename;
54 const char *fmap;
55 const char *region_name;
56 const char *bootblock;
57 const char *ignore_section;
Peter Stuge3bfd5b82013-07-09 19:39:13 +020058 uint64_t u64val;
Hung-Te Lind1739622013-01-28 14:23:49 +080059 uint32_t type;
60 uint32_t baseaddress;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080061 uint32_t baseaddress_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080062 uint32_t loadaddress;
Vadim Bendebury5e273a42014-12-23 19:26:54 -080063 uint32_t copyoffset;
64 uint32_t copyoffset_assigned;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080065 uint32_t headeroffset;
66 uint32_t headeroffset_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080067 uint32_t entrypoint;
68 uint32_t size;
69 uint32_t alignment;
Hung-Te Line9198372013-03-19 12:17:12 +080070 uint32_t pagesize;
Julius Wernerefcee762014-11-10 13:14:24 -080071 uint32_t cbfsoffset;
72 uint32_t cbfsoffset_assigned;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060073 uint32_t arch;
Sol Boucher67a0a862015-03-18 12:36:27 -070074 bool fill_partial_upward;
75 bool fill_partial_downward;
76 bool show_immutable;
Aaron Durbin4be16742015-09-15 17:00:23 -050077 bool stage_xip;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060078 int fit_empty_entries;
Sol Boucher65336712015-05-07 21:00:05 -070079 enum comp_algo compression;
Patrick Georgi89f20342015-10-01 15:54:04 +020080 enum vb2_hash_algorithm hash;
Patrick Georgide36d332013-08-27 20:22:21 +020081 /* for linux payloads */
82 char *initrd;
83 char *cmdline;
Hung-Te Lind1739622013-01-28 14:23:49 +080084} param = {
85 /* All variables not listed are initialized as zero. */
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060086 .arch = CBFS_ARCHITECTURE_UNKNOWN,
Sol Boucher65336712015-05-07 21:00:05 -070087 .compression = CBFS_COMPRESS_NONE,
Patrick Georgi89f20342015-10-01 15:54:04 +020088 .hash = VB2_HASH_INVALID,
Vadim Bendebury458a12e2014-12-23 15:10:12 -080089 .headeroffset = ~0,
Sol Boucher67a0a862015-03-18 12:36:27 -070090 .region_name = SECTION_NAME_PRIMARY_CBFS,
Hung-Te Lind1739622013-01-28 14:23:49 +080091};
Stefan Reinauer63217582012-10-29 16:52:36 -070092
Sol Boucher67a0a862015-03-18 12:36:27 -070093static bool region_is_flashmap(const char *region)
94{
95 return partitioned_file_region_check_magic(param.image_file, region,
96 FMAP_SIGNATURE, strlen(FMAP_SIGNATURE));
97}
98
99/* @return Same as cbfs_is_valid_cbfs(), but for a named region. */
100static bool region_is_modern_cbfs(const char *region)
101{
102 return partitioned_file_region_check_magic(param.image_file, region,
103 CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC));
104}
105
Sol Boucher67d59982015-05-07 02:39:22 -0700106/*
107 * Converts between offsets from the start of the specified image region and
108 * "top-aligned" offsets from the top of the entire flash image. Works in either
109 * direction: pass in one type of offset and receive the other type.
110 * N.B. A top-aligned offset is always a positive number, and should not be
111 * confused with a top-aliged *address*, which is its arithmetic inverse. */
112static unsigned convert_to_from_top_aligned(const struct buffer *region,
113 unsigned offset)
114{
115 assert(region);
116
117 size_t image_size = partitioned_file_total_size(param.image_file);
118 return image_size - region->offset - offset;
119}
120
Aaron Durbin4be16742015-09-15 17:00:23 -0500121static int do_cbfs_locate(int32_t *cbfs_addr, size_t metadata_size)
122{
123 if (!param.filename) {
124 ERROR("You need to specify -f/--filename.\n");
125 return 1;
126 }
127
128 if (!param.name) {
129 ERROR("You need to specify -n/--name.\n");
130 return 1;
131 }
132
133 struct cbfs_image image;
134 if (cbfs_image_from_buffer(&image, param.image_region,
135 param.headeroffset))
136 return 1;
137
138 if (cbfs_get_entry(&image, param.name))
139 WARN("'%s' already in CBFS.\n", param.name);
140
141 struct buffer buffer;
142 if (buffer_from_file(&buffer, param.filename) != 0) {
143 ERROR("Cannot load %s.\n", param.filename);
144 return 1;
145 }
146
147 /* Include cbfs_file size along with space for with name. */
148 metadata_size += cbfs_calculate_file_header_size(param.name);
149
150 int32_t address = cbfs_locate_entry(&image, buffer.size, param.pagesize,
151 param.alignment, metadata_size);
152 buffer_delete(&buffer);
153
154 if (address == -1) {
155 ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n",
156 param.name, param.pagesize, param.alignment);
157 return 1;
158 }
159
Aaron Durbin4be16742015-09-15 17:00:23 -0500160 *cbfs_addr = address;
161 return 0;
162}
163
Patrick Georgif8204972015-08-11 15:16:24 +0200164typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200165 struct cbfs_file *header);
Stefan Reinauer63217582012-10-29 16:52:36 -0700166
Sol Bouchere3260a02015-03-25 13:40:08 -0700167static int cbfs_add_integer_component(const char *name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200168 uint64_t u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800169 uint32_t offset,
170 uint32_t headeroffset) {
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200171 struct cbfs_image image;
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200172 struct cbfs_file *header = NULL;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200173 struct buffer buffer;
174 int i, ret = 1;
175
176 if (!name) {
177 ERROR("You need to specify -n/--name.\n");
178 return 1;
179 }
180
181 if (buffer_create(&buffer, 8, name) != 0)
182 return 1;
183
184 for (i = 0; i < 8; i++)
185 buffer.data[i] = (u64val >> i*8) & 0xff;
186
Sol Bouchere3260a02015-03-25 13:40:08 -0700187 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) {
188 ERROR("Selected image region is not a CBFS.\n");
189 goto done;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200190 }
191
192 if (cbfs_get_entry(&image, name)) {
193 ERROR("'%s' already in ROM image.\n", name);
194 goto done;
195 }
196
Sol Boucher67d59982015-05-07 02:39:22 -0700197 if (IS_TOP_ALIGNED_ADDRESS(offset))
198 offset = convert_to_from_top_aligned(param.image_region,
199 -offset);
200
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200201 header = cbfs_create_file_header(CBFS_COMPONENT_RAW,
202 buffer.size, name);
Patrick Georgif5252f32015-08-25 22:27:57 +0200203 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Sol Boucher0e539312015-03-05 15:38:03 -0800204 ERROR("Failed to add %llu into ROM image as '%s'.\n",
205 (long long unsigned)u64val, name);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200206 goto done;
207 }
208
Sol Bouchere3260a02015-03-25 13:40:08 -0700209 ret = 0;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200210
211done:
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200212 free(header);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200213 buffer_delete(&buffer);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200214 return ret;
215}
216
Patrick Georgi59e52b92015-09-10 15:28:27 +0200217static int cbfs_add_master_header(void)
218{
219 const char * const name = "cbfs master header";
220 struct cbfs_image image;
221 struct cbfs_file *header = NULL;
222 struct buffer buffer;
223 int ret = 1;
224
225 if (cbfs_image_from_buffer(&image, param.image_region,
226 param.headeroffset)) {
227 ERROR("Selected image region is not a CBFS.\n");
228 return 1;
229 }
230
231 if (cbfs_get_entry(&image, name)) {
232 ERROR("'%s' already in ROM image.\n", name);
233 return 1;
234 }
235
236 if (buffer_create(&buffer, sizeof(struct cbfs_header), name) != 0)
237 return 1;
238
239 struct cbfs_header *h = (struct cbfs_header *)buffer.data;
240 h->magic = htonl(CBFS_HEADER_MAGIC);
241 h->version = htonl(CBFS_HEADER_VERSION);
242 h->romsize = htonl(param.image_region->size);
243 /* The 4 bytes are left out for two reasons:
244 * 1. the cbfs master header pointer resides there
245 * 2. some cbfs implementations assume that an image that resides
246 * below 4GB has a bootblock and get confused when the end of the
247 * image is at 4GB == 0.
248 */
249 h->bootblocksize = htonl(4);
250 h->align = htonl(CBFS_ENTRY_ALIGNMENT);
251 /* offset relative to romsize above, which covers precisely the CBFS
252 * region.
253 */
254 h->offset = htonl(0);
255 h->architecture = htonl(CBFS_ARCHITECTURE_UNKNOWN);
256
257 header = cbfs_create_file_header(CBFS_COMPONENT_CBFSHEADER,
258 buffer_size(&buffer), name);
259 if (cbfs_add_entry(&image, &buffer, 0, header) != 0) {
260 ERROR("Failed to add cbfs master header into ROM image.\n");
261 goto done;
262 }
263
264 struct cbfs_file *entry;
265 if ((entry = cbfs_get_entry(&image, name)) == NULL) {
266 ERROR("'%s' not in ROM image?!?\n", name);
267 goto done;
268 }
269
270 uint32_t header_offset = CBFS_SUBHEADER(entry) -
271 buffer_get(&image.buffer);
272 header_offset = -(buffer_size(&image.buffer) - header_offset);
273
274 // TODO: when we have a BE target, we'll need to store this as BE
275 *(uint32_t *)(buffer_get(&image.buffer) +
276 buffer_size(&image.buffer) - 4) =
Aaron Durbin4b93a4f2015-09-21 13:10:13 -0500277 swab32(htonl(header_offset));
Patrick Georgi59e52b92015-09-10 15:28:27 +0200278
279 ret = 0;
280
281done:
282 free(header);
283 buffer_delete(&buffer);
284 return ret;
285}
286
Sol Bouchere3260a02015-03-25 13:40:08 -0700287static int cbfs_add_component(const char *filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800288 const char *name,
289 uint32_t type,
290 uint32_t offset,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800291 uint32_t headeroffset,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100292 convert_buffer_t convert)
293{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800294 if (!filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800295 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700296 return 1;
297 }
298
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800299 if (!name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800300 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700301 return 1;
302 }
303
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800304 if (type == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800305 ERROR("You need to specify a valid -t/--type.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700306 return 1;
307 }
308
Sol Bouchere3260a02015-03-25 13:40:08 -0700309 struct cbfs_image image;
Sol Boucher67a0a862015-03-18 12:36:27 -0700310 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset))
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800311 return 1;
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800312
Patrick Georgi4e54bf92015-08-11 14:35:39 +0200313 if (cbfs_get_entry(&image, name)) {
314 ERROR("'%s' already in ROM image.\n", name);
315 return 1;
316 }
317
Sol Bouchere3260a02015-03-25 13:40:08 -0700318 struct buffer buffer;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800319 if (buffer_from_file(&buffer, filename) != 0) {
320 ERROR("Could not load file '%s'.\n", filename);
Stefan Reinauer63217582012-10-29 16:52:36 -0700321 return 1;
322 }
323
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200324 struct cbfs_file *header =
325 cbfs_create_file_header(type, buffer.size, name);
Patrick Georgi4110abc2015-08-11 15:10:33 +0200326
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200327 if (convert && convert(&buffer, &offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800328 ERROR("Failed to parse file '%s'.\n", filename);
329 buffer_delete(&buffer);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000330 return 1;
Stefan Reinauerfbadc492011-10-14 12:44:14 -0700331 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700332
Patrick Georgi89f20342015-10-01 15:54:04 +0200333 if (param.hash != VB2_HASH_INVALID)
334 if (cbfs_add_file_hash(header, &buffer, param.hash) == -1) {
335 ERROR("couldn't add hash for '%s'\n", name);
336 free(header);
337 buffer_delete(&buffer);
338 return 1;
339 }
340
Sol Boucher67d59982015-05-07 02:39:22 -0700341 if (IS_TOP_ALIGNED_ADDRESS(offset))
342 offset = convert_to_from_top_aligned(param.image_region,
343 -offset);
344
Patrick Georgif5252f32015-08-25 22:27:57 +0200345 if (cbfs_add_entry(&image, &buffer, offset, header) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800346 ERROR("Failed to add '%s' into ROM image.\n", filename);
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200347 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800348 buffer_delete(&buffer);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800349 return 1;
350 }
351
Patrick Georgi3ba501b2015-08-25 13:48:10 +0200352 free(header);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800353 buffer_delete(&buffer);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000354 return 0;
355}
356
Daisuke Nojiri8984a632015-07-09 15:07:45 -0700357static int cbfstool_convert_raw(struct buffer *buffer,
358 unused uint32_t *offset, struct cbfs_file *header)
359{
360 char *compressed;
361 int compressed_size;
362
363 comp_func_ptr compress = compression_function(param.compression);
364 if (!compress)
365 return -1;
366 compressed = calloc(buffer->size, 1);
367
368 if (compress(buffer->data, buffer->size,
369 compressed, &compressed_size)) {
370 WARN("Compression failed - disabled\n");
371 } else {
372 struct cbfs_file_attr_compression *attrs =
373 (struct cbfs_file_attr_compression *)
374 cbfs_add_file_attr(header,
375 CBFS_FILE_ATTR_TAG_COMPRESSION,
376 sizeof(struct cbfs_file_attr_compression));
377 if (attrs == NULL)
378 return -1;
379 attrs->compression = htonl(param.compression);
380 attrs->decompressed_size = htonl(buffer->size);
381
382 free(buffer->data);
383 buffer->data = compressed;
384 buffer->size = compressed_size;
385
386 header->len = htonl(buffer->size);
387 }
388 return 0;
389}
390
Aaron Durbin8a414a02015-10-01 17:02:45 -0500391static int cbfstool_convert_fsp(struct buffer *buffer,
392 uint32_t *offset, struct cbfs_file *header)
393{
394 uint32_t address;
395 struct buffer fsp;
396
397 address = *offset;
398
399 /* Ensure the address is a memory mapped one. */
400 if (!IS_TOP_ALIGNED_ADDRESS(address))
401 address = -convert_to_from_top_aligned(param.image_region,
402 address);
403
404 /* Create a copy of the buffer to attempt relocation. */
405 if (buffer_create(&fsp, buffer_size(buffer), "fsp"))
406 return -1;
407
408 memcpy(buffer_get(&fsp), buffer_get(buffer), buffer_size(buffer));
409
410 /* Replace the buffer contents w/ the relocated ones on success. */
411 if (fsp1_1_relocate(address, buffer_get(&fsp), buffer_size(&fsp)) > 0) {
412 buffer_delete(buffer);
413 buffer_clone(buffer, &fsp);
414 } else {
415 buffer_delete(&fsp);
416 WARN("FSP was not a 1.1 variant.\n");
417 }
418
419 /* Let the raw path handle all the cbfs metadata logic. */
420 return cbfstool_convert_raw(buffer, offset, header);
421}
422
Patrick Georgif8204972015-08-11 15:16:24 +0200423static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200424 struct cbfs_file *header)
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600425{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800426 struct buffer output;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600427 int ret;
Aaron Durbin4be16742015-09-15 17:00:23 -0500428
429 if (param.stage_xip) {
430 int32_t address;
431
432 if (do_cbfs_locate(&address, sizeof(struct cbfs_stage))) {
433 ERROR("Could not find location for XIP stage.\n");
434 return 1;
435 }
436
437 /* Pass in a top aligned address. */
438 address = -convert_to_from_top_aligned(param.image_region,
439 address);
440 *offset = address;
441
442 ret = parse_elf_to_xip_stage(buffer, &output, offset,
443 param.ignore_section);
444 } else
445 ret = parse_elf_to_stage(buffer, &output, param.compression,
446 offset, param.ignore_section);
447
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600448 if (ret != 0)
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800449 return -1;
450 buffer_delete(buffer);
451 // direct assign, no dupe.
452 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200453 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800454 return 0;
455}
456
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100457static int cbfstool_convert_mkpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200458 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100459{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800460 struct buffer output;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800461 int ret;
462 /* per default, try and see if payload is an ELF binary */
Sol Boucher65336712015-05-07 21:00:05 -0700463 ret = parse_elf_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800464
465 /* If it's not an ELF, see if it's a UEFI FV */
466 if (ret != 0)
Sol Boucher65336712015-05-07 21:00:05 -0700467 ret = parse_fv_to_payload(buffer, &output, param.compression);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800468
Patrick Georgide36d332013-08-27 20:22:21 +0200469 /* If it's neither ELF nor UEFI Fv, try bzImage */
470 if (ret != 0)
471 ret = parse_bzImage_to_payload(buffer, &output,
Sol Boucher65336712015-05-07 21:00:05 -0700472 param.initrd, param.cmdline, param.compression);
Patrick Georgide36d332013-08-27 20:22:21 +0200473
Stefan Reinauer543a6822013-02-04 15:39:13 -0800474 /* Not a supported payload type */
475 if (ret != 0) {
476 ERROR("Not a supported payload type (ELF / FV).\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700477 buffer_delete(buffer);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800478 return -1;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800479 }
480
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800481 buffer_delete(buffer);
482 // direct assign, no dupe.
483 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200484 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800485 return 0;
486}
487
488static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
Patrick Georgi056f6a12015-08-25 15:53:52 +0200489 unused uint32_t *offset, struct cbfs_file *header)
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100490{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800491 struct buffer output;
492 if (parse_flat_binary_to_payload(buffer, &output,
493 param.loadaddress,
494 param.entrypoint,
Sol Boucher65336712015-05-07 21:00:05 -0700495 param.compression) != 0) {
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800496 return -1;
497 }
498 buffer_delete(buffer);
499 // direct assign, no dupe.
500 memcpy(buffer, &output, sizeof(*buffer));
Patrick Georgidc9dbc02015-08-25 20:17:14 +0200501 header->len = htonl(output.size);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800502 return 0;
503}
504
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800505static int cbfs_add(void)
506{
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700507 int32_t address;
Aaron Durbin8a414a02015-10-01 17:02:45 -0500508 convert_buffer_t convert;
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700509
510 if (param.alignment && param.baseaddress) {
511 ERROR("Cannot specify both alignment and base address\n");
512 return 1;
513 }
514
Aaron Durbin8a414a02015-10-01 17:02:45 -0500515 convert = cbfstool_convert_raw;
516
517 /* Set the alignment to 4KiB minimum for FSP blobs when no base address
518 * is provided so that relocation can occur. */
519 if (param.type == CBFS_COMPONENT_FSP) {
520 if (!param.baseaddress_assigned)
521 param.alignment = 4*1024;
522 convert = cbfstool_convert_fsp;
523 }
524
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700525 if (param.alignment) {
Aaron Durbin9b9d4b32015-09-15 21:30:58 -0500526 /* CBFS compression file attribute is unconditionally added. */
527 size_t metadata_sz = sizeof(struct cbfs_file_attr_compression);
528 if (do_cbfs_locate(&address, metadata_sz))
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -0700529 return 1;
530 param.baseaddress = address;
531 }
532
Sol Bouchere3260a02015-03-25 13:40:08 -0700533 return cbfs_add_component(param.filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800534 param.name,
535 param.type,
536 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800537 param.headeroffset,
Aaron Durbin8a414a02015-10-01 17:02:45 -0500538 convert);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800539}
540
Stefan Reinauer63217582012-10-29 16:52:36 -0700541static int cbfs_add_stage(void)
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700542{
Aaron Durbin4be16742015-09-15 17:00:23 -0500543 if (param.stage_xip) {
544 if (param.baseaddress_assigned) {
545 ERROR("Cannot specify base address for XIP.\n");
546 return 1;
547 }
548
549 if (param.compression != CBFS_COMPRESS_NONE) {
550 ERROR("Cannot specify compression for XIP.\n");
551 return 1;
552 }
553 }
554
Sol Bouchere3260a02015-03-25 13:40:08 -0700555 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800556 param.name,
557 CBFS_COMPONENT_STAGE,
558 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800559 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800560 cbfstool_convert_mkstage);
561}
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700562
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800563static int cbfs_add_payload(void)
564{
Sol Bouchere3260a02015-03-25 13:40:08 -0700565 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800566 param.name,
567 CBFS_COMPONENT_PAYLOAD,
568 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800569 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800570 cbfstool_convert_mkpayload);
Stefan Reinauer63217582012-10-29 16:52:36 -0700571}
572
573static int cbfs_add_flat_binary(void)
574{
Hung-Te Lind1739622013-01-28 14:23:49 +0800575 if (param.loadaddress == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800576 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700577 "-l/--load-address.\n");
578 return 1;
579 }
Hung-Te Lind1739622013-01-28 14:23:49 +0800580 if (param.entrypoint == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800581 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700582 "-e/--entry-point.\n");
583 return 1;
584 }
Sol Bouchere3260a02015-03-25 13:40:08 -0700585 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800586 param.name,
587 CBFS_COMPONENT_PAYLOAD,
588 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800589 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800590 cbfstool_convert_mkflatpayload);
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700591}
592
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200593static int cbfs_add_integer(void)
594{
Sol Bouchere3260a02015-03-25 13:40:08 -0700595 return cbfs_add_integer_component(param.name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200596 param.u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800597 param.baseaddress,
598 param.headeroffset);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200599}
600
Stefan Reinauer63217582012-10-29 16:52:36 -0700601static int cbfs_remove(void)
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800602{
Hung-Te Lind1739622013-01-28 14:23:49 +0800603 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800604 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700605 return 1;
606 }
607
Sol Bouchere3260a02015-03-25 13:40:08 -0700608 struct cbfs_image image;
609 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700610 param.headeroffset))
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800611 return 1;
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800612
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800613 if (cbfs_remove_entry(&image, param.name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800614 ERROR("Removing file '%s' failed.\n",
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800615 param.name);
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800616 return 1;
617 }
618
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800619 return 0;
620}
621
Stefan Reinauer63217582012-10-29 16:52:36 -0700622static int cbfs_create(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000623{
Sol Boucher67a0a862015-03-18 12:36:27 -0700624 struct cbfs_image image;
625 memset(&image, 0, sizeof(image));
626 buffer_clone(&image.buffer, param.image_region);
627
628 if (param.fmap) {
629 if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size ||
630 param.baseaddress_assigned ||
631 param.headeroffset_assigned ||
632 param.cbfsoffset_assigned ||
Sol Boucher67a0a862015-03-18 12:36:27 -0700633 param.bootblock) {
Patrick Georgi45acb342015-07-14 22:18:23 +0200634 ERROR("Since -M was provided, -m, -s, -b, -o, -H, and -B should be omitted\n");
Sol Boucher67a0a862015-03-18 12:36:27 -0700635 return 1;
636 }
637
638 return cbfs_image_create(&image, image.buffer.size);
639 }
640
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600641 if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) {
Stefan Reinauer60a4a732013-03-28 16:46:07 -0700642 ERROR("You need to specify -m/--machine arch.\n");
David Hendricks90ca3b62012-11-16 14:48:22 -0800643 return 1;
644 }
645
Sol Bouchere3260a02015-03-25 13:40:08 -0700646 struct buffer bootblock;
Julius Wernerefcee762014-11-10 13:14:24 -0800647 if (!param.bootblock) {
648 DEBUG("-B not given, creating image without bootblock.\n");
649 buffer_create(&bootblock, 0, "(dummy)");
650 } else if (buffer_from_file(&bootblock, param.bootblock)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800651 return 1;
652 }
653
Julius Wernerefcee762014-11-10 13:14:24 -0800654 if (!param.alignment)
Patrick Georgi45acb342015-07-14 22:18:23 +0200655 param.alignment = CBFS_ALIGNMENT;
Julius Wernerefcee762014-11-10 13:14:24 -0800656
657 // Set default offsets. x86, as usual, needs to be a special snowflake.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800658 if (!param.baseaddress_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800659 if (param.arch == CBFS_ARCHITECTURE_X86) {
660 // Make sure there's at least enough room for rel_offset
Sol Boucher67a0a862015-03-18 12:36:27 -0700661 param.baseaddress = param.size -
662 MAX(bootblock.size, sizeof(int32_t));
Julius Wernerefcee762014-11-10 13:14:24 -0800663 DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n",
664 param.baseaddress);
665 } else {
666 param.baseaddress = 0;
667 DEBUG("bootblock starts at address 0x0.\n");
668 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800669 }
670 if (!param.headeroffset_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800671 if (param.arch == CBFS_ARCHITECTURE_X86) {
672 param.headeroffset = param.baseaddress -
673 sizeof(struct cbfs_header);
674 DEBUG("x86 -> CBFS header before bootblock (%#x).\n",
675 param.headeroffset);
676 } else {
677 param.headeroffset = align_up(param.baseaddress +
678 bootblock.size, sizeof(uint32_t));
679 DEBUG("CBFS header placed behind bootblock (%#x).\n",
680 param.headeroffset);
681 }
682 }
683 if (!param.cbfsoffset_assigned) {
684 if (param.arch == CBFS_ARCHITECTURE_X86) {
685 param.cbfsoffset = 0;
686 DEBUG("x86 -> CBFS entries start at address 0x0.\n");
687 } else {
688 param.cbfsoffset = align_up(param.headeroffset +
689 sizeof(struct cbfs_header),
Patrick Georgi45acb342015-07-14 22:18:23 +0200690 CBFS_ALIGNMENT);
Julius Wernerefcee762014-11-10 13:14:24 -0800691 DEBUG("CBFS entries start beind master header (%#x).\n",
692 param.cbfsoffset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800693 }
694 }
695
Sol Boucher67a0a862015-03-18 12:36:27 -0700696 int ret = cbfs_legacy_image_create(&image,
697 param.arch,
Patrick Georgi45acb342015-07-14 22:18:23 +0200698 CBFS_ALIGNMENT,
Sol Boucher67a0a862015-03-18 12:36:27 -0700699 &bootblock,
700 param.baseaddress,
701 param.headeroffset,
702 param.cbfsoffset);
Sol Bouchere3260a02015-03-25 13:40:08 -0700703 buffer_delete(&bootblock);
Sol Boucher67a0a862015-03-18 12:36:27 -0700704 return ret;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000705}
706
Sol Boucher67a0a862015-03-18 12:36:27 -0700707static int cbfs_layout(void)
708{
709 const struct fmap *fmap = partitioned_file_get_fmap(param.image_file);
710 if (!fmap) {
711 LOG("This is a legacy image composed entirely of a single CBFS.\n");
712 return 1;
713 }
714
715 printf("This image contains the following sections that can be %s with this tool:\n",
716 param.show_immutable ? "accessed" : "manipulated");
717 puts("");
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300718 for (unsigned i = 0; i < fmap->nareas; ++i) {
719 const struct fmap_area *current = fmap->areas + i;
Sol Boucher67a0a862015-03-18 12:36:27 -0700720
721 bool readonly = partitioned_file_fmap_count(param.image_file,
722 partitioned_file_fmap_select_children_of, current) ||
723 region_is_flashmap((const char *)current->name);
724 if (!param.show_immutable && readonly)
725 continue;
726
727 printf("'%s'", current->name);
728
729 // Detect consecutive sections that describe the same region and
730 // show them as aliases. This cannot find equivalent entries
731 // that aren't adjacent; however, fmaptool doesn't generate
732 // FMAPs with such sections, so this convenience feature works
733 // for all but the strangest manually created FMAP binaries.
734 // TODO: This could be done by parsing the FMAP into some kind
735 // of tree that had duplicate lists in addition to child lists,
736 // which would allow covering that weird, unlikely case as well.
737 unsigned lookahead;
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300738 for (lookahead = 1; i + lookahead < fmap->nareas;
Sol Boucher67a0a862015-03-18 12:36:27 -0700739 ++lookahead) {
740 const struct fmap_area *consecutive =
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300741 fmap->areas + i + lookahead;
Sol Boucher67a0a862015-03-18 12:36:27 -0700742 if (consecutive->offset != current->offset ||
743 consecutive->size != current->size)
744 break;
745 printf(", '%s'", consecutive->name);
746 }
747 if (lookahead > 1)
748 fputs(" are aliases for the same region", stdout);
749
750 const char *qualifier = "";
751 if (readonly)
752 qualifier = "read-only, ";
753 else if (region_is_modern_cbfs((const char *)current->name))
754 qualifier = "CBFS, ";
755 printf(" (%ssize %u)\n", qualifier, current->size);
756
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300757 i += lookahead - 1;
Sol Boucher67a0a862015-03-18 12:36:27 -0700758 }
759 puts("");
760
761 if (param.show_immutable) {
762 puts("It is at least possible to perform the read action on every section listed above.");
763 } else {
764 puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above.");
765 puts("To see the image's read-only sections as well, rerun with the -w option.");
766 }
767
768 return 0;
769}
770
Stefan Reinauer63217582012-10-29 16:52:36 -0700771static int cbfs_print(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000772{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800773 struct cbfs_image image;
Sol Bouchere3260a02015-03-25 13:40:08 -0700774 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700775 param.headeroffset))
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000776 return 1;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800777 cbfs_print_directory(&image);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000778 return 0;
779}
780
Stefan Reinauer63217582012-10-29 16:52:36 -0700781static int cbfs_extract(void)
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000782{
Hung-Te Lind1739622013-01-28 14:23:49 +0800783 if (!param.filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800784 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700785 return 1;
786 }
787
Hung-Te Lind1739622013-01-28 14:23:49 +0800788 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800789 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700790 return 1;
791 }
792
Sol Bouchere3260a02015-03-25 13:40:08 -0700793 struct cbfs_image image;
794 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700795 param.headeroffset))
796 return 1;
797
798 return cbfs_export_entry(&image, param.name, param.filename);
799}
800
801static int cbfs_write(void)
802{
803 if (!param.filename) {
804 ERROR("You need to specify a valid input -f/--file.\n");
805 return 1;
806 }
807 if (!partitioned_file_is_partitioned(param.image_file)) {
808 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700809 return 1;
810 }
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000811
Sol Boucher67a0a862015-03-18 12:36:27 -0700812 if (region_is_modern_cbfs(param.region_name)) {
813 ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n",
814 param.region_name);
815 return 1;
816 }
817
818 struct buffer new_content;
819 if (buffer_from_file(&new_content, param.filename))
820 return 1;
821
822 if (buffer_check_magic(&new_content, FMAP_SIGNATURE,
823 strlen(FMAP_SIGNATURE))) {
824 ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n",
825 param.filename);
826 buffer_delete(&new_content);
827 return 1;
828 }
829 if (buffer_check_magic(&new_content, CBFS_FILE_MAGIC,
830 strlen(CBFS_FILE_MAGIC))) {
831 ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n",
832 param.filename);
833 buffer_delete(&new_content);
834 return 1;
835 }
836
837 unsigned offset = 0;
838 if (param.fill_partial_upward && param.fill_partial_downward) {
839 ERROR("You may only specify one of -u and -d.\n");
840 buffer_delete(&new_content);
841 return 1;
842 } else if (!param.fill_partial_upward && !param.fill_partial_downward) {
843 if (new_content.size != param.image_region->size) {
844 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",
845 new_content.size, param.image_region->size);
846 buffer_delete(&new_content);
847 return 1;
848 }
849 } else {
850 if (new_content.size > param.image_region->size) {
851 ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n",
852 new_content.size, param.image_region->size);
853 buffer_delete(&new_content);
854 return 1;
855 }
856 WARN("Written area will abut %s of target region: any unused space will keep its current contents\n",
857 param.fill_partial_upward ? "bottom" : "top");
858 if (param.fill_partial_downward)
859 offset = param.image_region->size - new_content.size;
860 }
861
862 memcpy(param.image_region->data + offset, new_content.data,
863 new_content.size);
864 buffer_delete(&new_content);
865 return 0;
866}
867
868static int cbfs_read(void)
869{
870 if (!param.filename) {
871 ERROR("You need to specify a valid output -f/--file.\n");
872 return 1;
873 }
874 if (!partitioned_file_is_partitioned(param.image_file)) {
875 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
876 return 1;
877 }
878
879 return buffer_write_file(param.image_region, param.filename);
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000880}
881
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600882static int cbfs_update_fit(void)
883{
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600884 if (!param.name) {
885 ERROR("You need to specify -n/--name.\n");
886 return 1;
887 }
888
889 if (param.fit_empty_entries <= 0) {
890 ERROR("Invalid number of fit entries "
891 "(-x/--empty-fits): %d\n", param.fit_empty_entries);
892 return 1;
893 }
894
Sol Boucher32532ac2015-05-06 14:44:40 -0700895 struct buffer bootblock;
Patrick Georgi6dd99fc2015-09-19 14:04:45 +0200896 // The bootblock is part of the CBFS on x86
897 buffer_clone(&bootblock, param.image_region);
Sol Boucher32532ac2015-05-06 14:44:40 -0700898
Sol Bouchere3260a02015-03-25 13:40:08 -0700899 struct cbfs_image image;
900 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700901 param.headeroffset))
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600902 return 1;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600903
Sol Boucher32532ac2015-05-06 14:44:40 -0700904 if (fit_update_table(&bootblock, &image, param.name,
905 param.fit_empty_entries, convert_to_from_top_aligned))
906 return 1;
907
908 // The region to be written depends on the type of image, so we write it
909 // here rather than having main() write the CBFS region back as usual.
910 return !partitioned_file_write_region(param.image_file, &bootblock);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600911}
912
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800913static int cbfs_copy(void)
914{
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800915 if (!param.copyoffset_assigned) {
Patrick Georgi821bcd62015-10-16 13:37:21 +0200916 ERROR("You need to specify -D/--copy-offset.\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800917 return 1;
918 }
919
920 if (!param.size) {
921 ERROR("You need to specify -s/--size.\n");
922 return 1;
923 }
924
Sol Bouchere3260a02015-03-25 13:40:08 -0700925 struct cbfs_image image;
926 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700927 param.headeroffset))
928 return 1;
929
930 if (!cbfs_is_legacy_cbfs(&image)) {
931 ERROR("This operation is only valid on legacy images having CBFS master headers\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800932 return 1;
Sol Bouchere3260a02015-03-25 13:40:08 -0700933 }
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800934
Sol Bouchere3260a02015-03-25 13:40:08 -0700935 return cbfs_copy_instance(&image, param.copyoffset, param.size);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800936}
937
Stefan Reinauera1e48242011-10-21 14:24:57 -0700938static const struct command commands[] = {
Patrick Georgi89f20342015-10-01 15:54:04 +0200939 {"add", "H:r:f:n:t:c:b:a:vA:h?", cbfs_add, true, true},
940 {"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 -0700941 true},
Patrick Georgi89f20342015-10-01 15:54:04 +0200942 {"add-payload", "H:r:f:n:t:c:b:C:I:vA:h?", cbfs_add_payload, true, true},
943 {"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 -0700944 {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true},
Patrick Georgi59e52b92015-09-10 15:28:27 +0200945 {"add-master-header", "H:r:vh?", cbfs_add_master_header, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700946 {"copy", "H:D:s:h?", cbfs_copy, true, true},
Patrick Georgi45acb342015-07-14 22:18:23 +0200947 {"create", "M:r:s:B:b:H:o:m:vh?", cbfs_create, true, true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700948 {"extract", "H:r:n:f:vh?", cbfs_extract, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700949 {"layout", "wvh?", cbfs_layout, false, false},
950 {"print", "H:r:vh?", cbfs_print, true, false},
951 {"read", "r:f:vh?", cbfs_read, true, false},
952 {"remove", "H:r:n:vh?", cbfs_remove, true, true},
Sol Boucher32532ac2015-05-06 14:44:40 -0700953 {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700954 {"write", "r:f:udvh?", cbfs_write, true, true},
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000955};
956
Stefan Reinauer63217582012-10-29 16:52:36 -0700957static struct option long_options[] = {
Julius Wernerefcee762014-11-10 13:14:24 -0800958 {"alignment", required_argument, 0, 'a' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800959 {"base-address", required_argument, 0, 'b' },
960 {"bootblock", required_argument, 0, 'B' },
Julius Wernerefcee762014-11-10 13:14:24 -0800961 {"cmdline", required_argument, 0, 'C' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800962 {"compression", required_argument, 0, 'c' },
963 {"copy-offset", required_argument, 0, 'D' },
964 {"empty-fits", required_argument, 0, 'x' },
965 {"entry-point", required_argument, 0, 'e' },
966 {"file", required_argument, 0, 'f' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700967 {"fill-downward", no_argument, 0, 'd' },
968 {"fill-upward", no_argument, 0, 'u' },
969 {"flashmap", required_argument, 0, 'M' },
970 {"fmap-regions", required_argument, 0, 'r' },
Patrick Georgi89f20342015-10-01 15:54:04 +0200971 {"hash-algorithm",required_argument, 0, 'A' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800972 {"header-offset", required_argument, 0, 'H' },
Julius Wernerefcee762014-11-10 13:14:24 -0800973 {"help", no_argument, 0, 'h' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800974 {"ignore-sec", required_argument, 0, 'S' },
975 {"initrd", required_argument, 0, 'I' },
976 {"int", required_argument, 0, 'i' },
977 {"load-address", required_argument, 0, 'l' },
978 {"machine", required_argument, 0, 'm' },
979 {"name", required_argument, 0, 'n' },
980 {"offset", required_argument, 0, 'o' },
981 {"page-size", required_argument, 0, 'P' },
982 {"size", required_argument, 0, 's' },
983 {"top-aligned", required_argument, 0, 'T' },
984 {"type", required_argument, 0, 't' },
985 {"verbose", no_argument, 0, 'v' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700986 {"with-readonly", no_argument, 0, 'w' },
Aaron Durbin4be16742015-09-15 17:00:23 -0500987 {"xip", no_argument, 0, 'y' },
Julius Wernerefcee762014-11-10 13:14:24 -0800988 {NULL, 0, 0, 0 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700989};
990
Sol Boucher67a0a862015-03-18 12:36:27 -0700991static int dispatch_command(struct command command)
992{
993 if (command.accesses_region) {
994 assert(param.image_file);
995
996 if (partitioned_file_is_partitioned(param.image_file)) {
997 LOG("Performing operation on '%s' region...\n",
998 param.region_name);
999 }
1000 if (!partitioned_file_read_region(param.image_region,
1001 param.image_file, param.region_name)) {
1002 ERROR("The image will be left unmodified.\n");
1003 return 1;
1004 }
1005
1006 if (command.modifies_region) {
1007 // We (intentionally) don't support overwriting the FMAP
1008 // section. If you find yourself wanting to do this,
1009 // consider creating a new image rather than performing
1010 // whatever hacky transformation you were planning.
1011 if (region_is_flashmap(param.region_name)) {
1012 ERROR("Image region '%s' is read-only because it contains the FMAP.\n",
1013 param.region_name);
1014 ERROR("The image will be left unmodified.\n");
1015 return 1;
1016 }
1017 // We don't allow writing raw data to regions that
1018 // contain nested regions, since doing so would
1019 // overwrite all such subregions.
1020 if (partitioned_file_region_contains_nested(
1021 param.image_file, param.region_name)) {
1022 ERROR("Image region '%s' is read-only because it contains nested regions.\n",
1023 param.region_name);
1024 ERROR("The image will be left unmodified.\n");
1025 return 1;
1026 }
1027 }
1028 }
1029
1030 if (command.function()) {
1031 if (partitioned_file_is_partitioned(param.image_file)) {
1032 ERROR("Failed while operating on '%s' region!\n",
1033 param.region_name);
1034 ERROR("The image will be left unmodified.\n");
1035 }
1036 return 1;
1037 }
1038
1039 return 0;
1040}
1041
Stefan Reinauer63217582012-10-29 16:52:36 -07001042static void usage(char *name)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001043{
1044 printf
Stefan Reinauer07040582010-04-24 21:24:06 +00001045 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001046 "USAGE:\n" " %s [-h]\n"
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001047 " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001048 " -H header_offset Do not search for header; use this offset*\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001049 " -T Output top-aligned memory address\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001050 " -u Accept short data; fill upward/from bottom\n"
1051 " -d Accept short data; fill downward/from top\n"
1052 " -v Provide verbose output\n"
1053 " -h Display this help message\n\n"
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001054 "COMMANDs:\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001055 " add [-r image,regions] -f FILE -n NAME -t TYPE [-A hash] \\\n"
Alexandru Gagniuc38bc9162015-09-07 00:05:44 -07001056 " [-c compression] [-b base-address | -a alignment] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001057 "Add a component\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001058 " add-payload [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001059 " [-c compression] [-b base-address] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001060 "Add a payload to the ROM\n"
Patrick Georgide36d332013-08-27 20:22:21 +02001061 " (linux specific: [-C cmdline] [-I initrd])\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001062 " add-stage [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001063 " [-c compression] [-b base] [-S section-to-ignore] "
Aaron Durbin4be16742015-09-15 17:00:23 -05001064 " [-a alignment] [-y|--xip] [-P page-size]"
Stefan Reinauer63217582012-10-29 16:52:36 -07001065 "Add a stage to the ROM\n"
Patrick Georgi89f20342015-10-01 15:54:04 +02001066 " add-flat-binary [-r image,regions] -f FILE -n NAME [-A hash] \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001067 " -l load-address -e entry-point [-c compression] \\\n"
1068 " [-b base] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001069 "Add a 32bit flat mode binary\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001070 " add-int [-r image,regions] -i INTEGER -n NAME [-b base] "
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001071 "Add a raw 64-bit integer value\n"
Patrick Georgi59e52b92015-09-10 15:28:27 +02001072 " add-master-header [-r image,regions] "
1073 "Add a legacy CBFS master header\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001074 " remove [-r image,regions] -n NAME "
Stefan Reinauer63217582012-10-29 16:52:36 -07001075 "Remove a component\n"
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001076 " copy -D new_header_offset -s region size \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001077 " [-H source header offset] "
1078 "Create a copy (duplicate) cbfs instance*\n"
1079 " create -m ARCH -s size [-b bootblock offset] \\\n"
Patrick Georgi83c2d122015-08-26 10:40:00 +02001080 " [-o CBFS offset] [-H header offset] [-B bootblock] "
Sol Boucher67a0a862015-03-18 12:36:27 -07001081 "Create a legacy ROM file with CBFS master header*\n"
1082 " create -M flashmap [-r list,of,regions,containing,cbfses] "
1083 "Create a new-style partitioned firmware image\n"
1084 " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n"
1085 " [-a align] [-T] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001086 "Find a place for a file of that size\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001087 " layout [-w] "
1088 "List mutable (or, with -w, readable) image regions\n"
1089 " print [-r image,regions] "
Stefan Reinauer63217582012-10-29 16:52:36 -07001090 "Show the contents of the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001091 " extract [-r image,regions] -n NAME -f FILE "
Stefan Reinauer63217582012-10-29 16:52:36 -07001092 "Extracts a raw payload from ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -07001093 " write -r image,regions -f file [-u | -d] "
1094 "Write file into same-size [or larger] raw region\n"
1095 " read [-r fmap-region] -f file "
1096 "Extract raw region contents into binary file\n"
1097 " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n"
1098 " -x EMTPY_FIT_ENTRIES "
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001099 "Updates the FIT table with microcode entries\n"
Peter Stugeb347e0d2011-01-17 05:02:09 +00001100 "\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001101 "OFFSETs:\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001102 " Numbers accompanying -b, -H, and -o switches* may be provided\n"
1103 " in two possible formats: if their value is greater than\n"
Sol Boucher0e539312015-03-05 15:38:03 -08001104 " 0x80000000, they are interpreted as a top-aligned x86 memory\n"
1105 " address; otherwise, they are treated as an offset into flash.\n"
David Hendricks90ca3b62012-11-16 14:48:22 -08001106 "ARCHes:\n"
Paul Burton33186922014-06-13 23:56:45 +01001107 " arm64, arm, mips, x86\n"
Stefan Reinauer63217582012-10-29 16:52:36 -07001108 "TYPEs:\n", name, name
1109 );
Stefan Reinauer07040582010-04-24 21:24:06 +00001110 print_supported_filetypes();
Sol Boucher67a0a862015-03-18 12:36:27 -07001111
1112 printf(
1113 "\n* Note that these actions and switches are only valid when\n"
1114 " working with legacy images whose structure is described\n"
1115 " primarily by a CBFS master header. New-style images, in\n"
1116 " contrast, exclusively make use of an FMAP to describe their\n"
1117 " layout: this must minimally contain an '%s' section\n"
1118 " specifying the location of this FMAP itself and a '%s'\n"
1119 " section describing the primary CBFS. It should also be noted\n"
1120 " that, when working with such images, the -F and -r switches\n"
1121 " default to '%s' for convenience, and both the -b switch to\n"
1122 " CBFS operations and the output of the locate action become\n"
Sol Boucher67d59982015-05-07 02:39:22 -07001123 " relative to the selected CBFS region's lowest address.\n"
1124 " The one exception to this rule is the top-aligned address,\n"
1125 " which is always relative to the end of the entire image\n"
1126 " rather than relative to the local region; this is true for\n"
1127 " for both input (sufficiently large) and output (-T) data.\n",
Sol Boucher67a0a862015-03-18 12:36:27 -07001128 SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
1129 SECTION_NAME_PRIMARY_CBFS
1130 );
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001131}
1132
1133int main(int argc, char **argv)
1134{
Mathias Krause41c229c2012-07-17 21:17:15 +02001135 size_t i;
Stefan Reinauer63217582012-10-29 16:52:36 -07001136 int c;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001137
1138 if (argc < 3) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001139 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001140 return 1;
1141 }
1142
Sol Bouchere3260a02015-03-25 13:40:08 -07001143 char *image_name = argv[1];
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001144 char *cmd = argv[2];
Stefan Reinauer63217582012-10-29 16:52:36 -07001145 optind += 2;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001146
1147 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1148 if (strcmp(cmd, commands[i].name) != 0)
1149 continue;
Stefan Reinauer63217582012-10-29 16:52:36 -07001150
1151 while (1) {
1152 char *suffix = NULL;
1153 int option_index = 0;
1154
1155 c = getopt_long(argc, argv, commands[i].optstring,
1156 long_options, &option_index);
1157 if (c == -1)
1158 break;
1159
1160 /* filter out illegal long options */
zbao062730d2013-01-08 10:10:16 +08001161 if (strchr(commands[i].optstring, c) == NULL) {
Stefan Reinauer63217582012-10-29 16:52:36 -07001162 /* TODO maybe print actual long option instead */
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001163 ERROR("%s: invalid option -- '%c'\n",
1164 argv[0], c);
Stefan Reinauer63217582012-10-29 16:52:36 -07001165 c = '?';
1166 }
1167
1168 switch(c) {
1169 case 'n':
Hung-Te Lind1739622013-01-28 14:23:49 +08001170 param.name = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001171 break;
1172 case 't':
1173 if (intfiletype(optarg) != ((uint64_t) - 1))
Hung-Te Lind1739622013-01-28 14:23:49 +08001174 param.type = intfiletype(optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001175 else
Hung-Te Lind1739622013-01-28 14:23:49 +08001176 param.type = strtoul(optarg, NULL, 0);
1177 if (param.type == 0)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001178 WARN("Unknown type '%s' ignored\n",
Stefan Reinauer63217582012-10-29 16:52:36 -07001179 optarg);
1180 break;
Sol Boucherec424862015-05-07 21:00:05 -07001181 case 'c': {
1182 int algo = cbfs_parse_comp_algo(optarg);
1183 if (algo >= 0)
1184 param.compression = algo;
Stefan Reinauer63217582012-10-29 16:52:36 -07001185 else
Sol Boucherec424862015-05-07 21:00:05 -07001186 WARN("Unknown compression '%s' ignored.\n",
1187 optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -07001188 break;
Sol Boucherec424862015-05-07 21:00:05 -07001189 }
Patrick Georgi89f20342015-10-01 15:54:04 +02001190 case 'A': {
1191 int algo = cbfs_parse_hash_algo(optarg);
1192 if (algo >= 0)
1193 param.hash = algo;
1194 else {
1195 ERROR("Unknown hash algorithm '%s'.\n",
1196 optarg);
1197 return 1;
1198 }
1199 break;
1200 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001201 case 'M':
1202 param.fmap = optarg;
1203 break;
1204 case 'r':
1205 param.region_name = optarg;
1206 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001207 case 'b':
Hung-Te Lind1739622013-01-28 14:23:49 +08001208 param.baseaddress = strtoul(optarg, NULL, 0);
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001209 // baseaddress may be zero on non-x86, so we
1210 // need an explicit "baseaddress_assigned".
1211 param.baseaddress = strtoul(optarg, NULL, 0);
1212 param.baseaddress_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001213 break;
1214 case 'l':
Hung-Te Lind1739622013-01-28 14:23:49 +08001215 param.loadaddress = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001216 break;
1217 case 'e':
Hung-Te Lind1739622013-01-28 14:23:49 +08001218 param.entrypoint = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001219 break;
1220 case 's':
Hung-Te Lind1739622013-01-28 14:23:49 +08001221 param.size = strtoul(optarg, &suffix, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001222 if (tolower(suffix[0])=='k') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001223 param.size *= 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001224 }
1225 if (tolower(suffix[0])=='m') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001226 param.size *= 1024 * 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001227 }
Patrick Georgie887ca52014-08-09 17:44:39 +02001228 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001229 case 'B':
Hung-Te Lind1739622013-01-28 14:23:49 +08001230 param.bootblock = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001231 break;
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001232 case 'H':
1233 param.headeroffset = strtoul(
1234 optarg, NULL, 0);
1235 param.headeroffset_assigned = 1;
1236 break;
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001237 case 'D':
1238 param.copyoffset = strtoul(optarg, NULL, 0);
1239 param.copyoffset_assigned = 1;
Vadim Bendebury11614732015-01-08 16:36:35 -08001240 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001241 case 'a':
Hung-Te Lind1739622013-01-28 14:23:49 +08001242 param.alignment = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001243 break;
Hung-Te Line9198372013-03-19 12:17:12 +08001244 case 'P':
1245 param.pagesize = strtoul(optarg, NULL, 0);
1246 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001247 case 'o':
Julius Wernerefcee762014-11-10 13:14:24 -08001248 param.cbfsoffset = strtoul(optarg, NULL, 0);
1249 param.cbfsoffset_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001250 break;
1251 case 'f':
Hung-Te Lind1739622013-01-28 14:23:49 +08001252 param.filename = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001253 break;
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001254 case 'i':
1255 param.u64val = strtoull(optarg, NULL, 0);
1256 break;
Sol Boucher67a0a862015-03-18 12:36:27 -07001257 case 'u':
1258 param.fill_partial_upward = true;
1259 break;
1260 case 'd':
1261 param.fill_partial_downward = true;
1262 break;
1263 case 'w':
1264 param.show_immutable = true;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001265 break;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001266 case 'x':
1267 param.fit_empty_entries = strtol(optarg, NULL, 0);
1268 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001269 case 'v':
1270 verbose++;
1271 break;
David Hendricks90ca3b62012-11-16 14:48:22 -08001272 case 'm':
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -06001273 param.arch = string_to_arch(optarg);
David Hendricks90ca3b62012-11-16 14:48:22 -08001274 break;
Patrick Georgide36d332013-08-27 20:22:21 +02001275 case 'I':
1276 param.initrd = optarg;
1277 break;
1278 case 'C':
1279 param.cmdline = optarg;
1280 break;
Furquan Shaikh405304a2014-10-30 11:44:20 -07001281 case 'S':
1282 param.ignore_section = optarg;
1283 break;
Aaron Durbin4be16742015-09-15 17:00:23 -05001284 case 'y':
1285 param.stage_xip = true;
1286 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001287 case 'h':
1288 case '?':
1289 usage(argv[0]);
1290 return 1;
1291 default:
1292 break;
1293 }
1294 }
1295
Sol Bouchere3260a02015-03-25 13:40:08 -07001296 if (commands[i].function == cbfs_create) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001297 if (param.fmap) {
1298 struct buffer flashmap;
1299 if (buffer_from_file(&flashmap, param.fmap))
1300 return 1;
1301 param.image_file = partitioned_file_create(
1302 image_name, &flashmap);
1303 buffer_delete(&flashmap);
1304 } else if (param.size) {
1305 param.image_file = partitioned_file_create_flat(
1306 image_name, param.size);
1307 } else {
1308 ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001309 return 1;
1310 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001311 } else {
1312 param.image_file =
Patrick Georgi2f953d32015-09-11 18:34:39 +02001313 partitioned_file_reopen(image_name);
Sol Bouchere3260a02015-03-25 13:40:08 -07001314 }
1315 if (!param.image_file)
1316 return 1;
1317
Sol Boucher67a0a862015-03-18 12:36:27 -07001318 unsigned num_regions = 1;
1319 for (const char *list = strchr(param.region_name, ','); list;
1320 list = strchr(list + 1, ','))
1321 ++num_regions;
1322
Sol Bouchere3260a02015-03-25 13:40:08 -07001323 // If the action needs to read an image region, as indicated by
1324 // having accesses_region set in its command struct, that
1325 // region's buffer struct will be stored here and the client
1326 // will receive a pointer to it via param.image_region. It
1327 // need not write the buffer back to the image file itself,
1328 // since this behavior can be requested via its modifies_region
1329 // field. Additionally, it should never free the region buffer,
1330 // as that is performed automatically once it completes.
Sol Boucher67a0a862015-03-18 12:36:27 -07001331 struct buffer image_regions[num_regions];
1332 memset(image_regions, 0, sizeof(image_regions));
Sol Bouchere3260a02015-03-25 13:40:08 -07001333
Sol Boucher67a0a862015-03-18 12:36:27 -07001334 bool seen_primary_cbfs = false;
1335 char region_name_scratch[strlen(param.region_name) + 1];
1336 strcpy(region_name_scratch, param.region_name);
1337 param.region_name = strtok(region_name_scratch, ",");
1338 for (unsigned region = 0; region < num_regions; ++region) {
1339 if (!param.region_name) {
1340 ERROR("Encountered illegal degenerate region name in -r list\n");
1341 ERROR("The image will be left unmodified.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001342 partitioned_file_close(param.image_file);
1343 return 1;
1344 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001345
Sol Boucher67a0a862015-03-18 12:36:27 -07001346 if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)
1347 == 0)
1348 seen_primary_cbfs = true;
Sol Bouchere3260a02015-03-25 13:40:08 -07001349
Sol Boucher67a0a862015-03-18 12:36:27 -07001350 param.image_region = image_regions + region;
1351 if (dispatch_command(commands[i])) {
Sol Bouchere3260a02015-03-25 13:40:08 -07001352 partitioned_file_close(param.image_file);
1353 return 1;
1354 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001355
1356 param.region_name = strtok(NULL, ",");
1357 }
1358
1359 if (commands[i].function == cbfs_create && !seen_primary_cbfs) {
1360 ERROR("The creation -r list must include the mandatory '%s' section.\n",
1361 SECTION_NAME_PRIMARY_CBFS);
1362 ERROR("The image will be left unmodified.\n");
1363 partitioned_file_close(param.image_file);
1364 return 1;
1365 }
1366
1367 if (commands[i].modifies_region) {
1368 assert(param.image_file);
1369 assert(commands[i].accesses_region);
1370 for (unsigned region = 0; region < num_regions;
1371 ++region) {
1372
1373 if (!partitioned_file_write_region(
1374 param.image_file,
1375 image_regions + region)) {
1376 partitioned_file_close(
1377 param.image_file);
1378 return 1;
1379 }
1380 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001381 }
1382
1383 partitioned_file_close(param.image_file);
Sol Boucher67a0a862015-03-18 12:36:27 -07001384 return 0;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001385 }
1386
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001387 ERROR("Unknown command '%s'.\n", cmd);
Stefan Reinauer63217582012-10-29 16:52:36 -07001388 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001389 return 1;
1390}