blob: 06d0cef1d4085d7d13e6d4ecd8472639037254c0 [file] [log] [blame]
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00001/*
Patrick Georgib7b56dd82009-09-14 13:29:27 +00002 * cbfstool, CLI utility for CBFS file manipulation
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00003 *
Patrick Georgib7b56dd82009-09-14 13:29:27 +00004 * Copyright (C) 2009 coresystems GmbH
5 * written by Patrick Georgi <patrick.georgi@coresystems.de>
David Hendricks90ca3b62012-11-16 14:48:22 -08006 * Copyright (C) 2012 Google, Inc.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010019 * Foundation, Inc.
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000020 */
21
Patrick Georgib7b56dd82009-09-14 13:29:27 +000022#include <stdio.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070023#include <stdlib.h>
Stefan Reinauer336daa72009-12-21 15:09:01 +000024#include <string.h>
Sol Boucher0e539312015-03-05 15:38:03 -080025#include <strings.h>
Stefan Reinauera1e48242011-10-21 14:24:57 -070026#include <ctype.h>
Stefan Reinauer63217582012-10-29 16:52:36 -070027#include <unistd.h>
28#include <getopt.h>
Patrick Georgib7b56dd82009-09-14 13:29:27 +000029#include "common.h"
30#include "cbfs.h"
Hung-Te Lin3bb035b2013-01-29 02:15:49 +080031#include "cbfs_image.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070032#include "cbfs_sections.h"
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060033#include "fit.h"
Sol Bouchere3260a02015-03-25 13:40:08 -070034#include "partitioned_file.h"
Ronald G. Minnich5d01ec02009-03-31 11:57:36 +000035
Sol Boucher32532ac2015-05-06 14:44:40 -070036#define SECTION_WITH_FIT_TABLE "BOOTBLOCK"
37
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000038struct command {
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000039 const char *name;
Stefan Reinauer63217582012-10-29 16:52:36 -070040 const char *optstring;
41 int (*function) (void);
Sol Bouchere3260a02015-03-25 13:40:08 -070042 // Whether to populate param.image_region before invoking function
43 bool accesses_region;
44 // Whether to write that region's contents back to image_file at the end
45 bool modifies_region;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +000046};
47
Hung-Te Lind1739622013-01-28 14:23:49 +080048static struct param {
Sol Bouchere3260a02015-03-25 13:40:08 -070049 partitioned_file_t *image_file;
50 struct buffer *image_region;
Sol Boucher67a0a862015-03-18 12:36:27 -070051 const char *name;
52 const char *filename;
53 const char *fmap;
54 const char *region_name;
55 const char *bootblock;
56 const char *ignore_section;
Peter Stuge3bfd5b82013-07-09 19:39:13 +020057 uint64_t u64val;
Hung-Te Lind1739622013-01-28 14:23:49 +080058 uint32_t type;
59 uint32_t baseaddress;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080060 uint32_t baseaddress_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080061 uint32_t loadaddress;
Vadim Bendebury5e273a42014-12-23 19:26:54 -080062 uint32_t copyoffset;
63 uint32_t copyoffset_assigned;
Hung-Te Linf56c73f2013-01-29 09:45:12 +080064 uint32_t headeroffset;
65 uint32_t headeroffset_assigned;
Hung-Te Lind1739622013-01-28 14:23:49 +080066 uint32_t entrypoint;
67 uint32_t size;
68 uint32_t alignment;
Hung-Te Line9198372013-03-19 12:17:12 +080069 uint32_t pagesize;
Julius Wernerefcee762014-11-10 13:14:24 -080070 uint32_t cbfsoffset;
71 uint32_t cbfsoffset_assigned;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060072 uint32_t arch;
Sol Boucher67a0a862015-03-18 12:36:27 -070073 bool top_aligned;
74 bool fill_partial_upward;
75 bool fill_partial_downward;
76 bool show_immutable;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -060077 int fit_empty_entries;
Sol Boucher6310ccc2015-05-07 21:12:28 -070078 enum comp_algo algo;
Patrick Georgide36d332013-08-27 20:22:21 +020079 /* for linux payloads */
80 char *initrd;
81 char *cmdline;
Hung-Te Lind1739622013-01-28 14:23:49 +080082} param = {
83 /* All variables not listed are initialized as zero. */
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -060084 .arch = CBFS_ARCHITECTURE_UNKNOWN,
Hung-Te Lind1739622013-01-28 14:23:49 +080085 .algo = CBFS_COMPRESS_NONE,
Vadim Bendebury458a12e2014-12-23 15:10:12 -080086 .headeroffset = ~0,
Sol Boucher67a0a862015-03-18 12:36:27 -070087 .region_name = SECTION_NAME_PRIMARY_CBFS,
Hung-Te Lind1739622013-01-28 14:23:49 +080088};
Stefan Reinauer63217582012-10-29 16:52:36 -070089
Sol Boucher67a0a862015-03-18 12:36:27 -070090static bool region_is_flashmap(const char *region)
91{
92 return partitioned_file_region_check_magic(param.image_file, region,
93 FMAP_SIGNATURE, strlen(FMAP_SIGNATURE));
94}
95
96/* @return Same as cbfs_is_valid_cbfs(), but for a named region. */
97static bool region_is_modern_cbfs(const char *region)
98{
99 return partitioned_file_region_check_magic(param.image_file, region,
100 CBFS_FILE_MAGIC, strlen(CBFS_FILE_MAGIC));
101}
102
Sol Boucher67d59982015-05-07 02:39:22 -0700103/*
104 * Converts between offsets from the start of the specified image region and
105 * "top-aligned" offsets from the top of the entire flash image. Works in either
106 * direction: pass in one type of offset and receive the other type.
107 * N.B. A top-aligned offset is always a positive number, and should not be
108 * confused with a top-aliged *address*, which is its arithmetic inverse. */
109static unsigned convert_to_from_top_aligned(const struct buffer *region,
110 unsigned offset)
111{
112 assert(region);
113
114 size_t image_size = partitioned_file_total_size(param.image_file);
115 return image_size - region->offset - offset;
116}
117
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800118typedef int (*convert_buffer_t)(struct buffer *buffer, uint32_t *offset);
Stefan Reinauer63217582012-10-29 16:52:36 -0700119
Sol Bouchere3260a02015-03-25 13:40:08 -0700120static int cbfs_add_integer_component(const char *name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200121 uint64_t u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800122 uint32_t offset,
123 uint32_t headeroffset) {
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200124 struct cbfs_image image;
125 struct buffer buffer;
126 int i, ret = 1;
127
128 if (!name) {
129 ERROR("You need to specify -n/--name.\n");
130 return 1;
131 }
132
133 if (buffer_create(&buffer, 8, name) != 0)
134 return 1;
135
136 for (i = 0; i < 8; i++)
137 buffer.data[i] = (u64val >> i*8) & 0xff;
138
Sol Bouchere3260a02015-03-25 13:40:08 -0700139 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset)) {
140 ERROR("Selected image region is not a CBFS.\n");
141 goto done;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200142 }
143
144 if (cbfs_get_entry(&image, name)) {
145 ERROR("'%s' already in ROM image.\n", name);
146 goto done;
147 }
148
Sol Boucher67d59982015-05-07 02:39:22 -0700149 if (IS_TOP_ALIGNED_ADDRESS(offset))
150 offset = convert_to_from_top_aligned(param.image_region,
151 -offset);
152
Sol Boucher0e539312015-03-05 15:38:03 -0800153 if (cbfs_add_entry(&image, &buffer, name, CBFS_COMPONENT_RAW, offset) !=
154 0) {
155 ERROR("Failed to add %llu into ROM image as '%s'.\n",
156 (long long unsigned)u64val, name);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200157 goto done;
158 }
159
Sol Bouchere3260a02015-03-25 13:40:08 -0700160 ret = 0;
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200161
162done:
163 buffer_delete(&buffer);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200164 return ret;
165}
166
Sol Bouchere3260a02015-03-25 13:40:08 -0700167static int cbfs_add_component(const char *filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800168 const char *name,
169 uint32_t type,
170 uint32_t offset,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800171 uint32_t headeroffset,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100172 convert_buffer_t convert)
173{
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800174 if (!filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800175 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700176 return 1;
177 }
178
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800179 if (!name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800180 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700181 return 1;
182 }
183
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800184 if (type == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800185 ERROR("You need to specify a valid -t/--type.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700186 return 1;
187 }
188
Sol Bouchere3260a02015-03-25 13:40:08 -0700189 struct cbfs_image image;
Sol Boucher67a0a862015-03-18 12:36:27 -0700190 if (cbfs_image_from_buffer(&image, param.image_region, headeroffset))
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800191 return 1;
Stefan Reinauer8f50e532013-11-13 14:34:57 -0800192
Sol Bouchere3260a02015-03-25 13:40:08 -0700193 struct buffer buffer;
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800194 if (buffer_from_file(&buffer, filename) != 0) {
195 ERROR("Could not load file '%s'.\n", filename);
Stefan Reinauer63217582012-10-29 16:52:36 -0700196 return 1;
197 }
198
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800199 if (convert && convert(&buffer, &offset) != 0) {
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800200 ERROR("Failed to parse file '%s'.\n", filename);
201 buffer_delete(&buffer);
Patrick Georgi56f5fb72009-09-30 11:21:18 +0000202 return 1;
Stefan Reinauerfbadc492011-10-14 12:44:14 -0700203 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700204
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800205 if (cbfs_get_entry(&image, name)) {
206 ERROR("'%s' already in ROM image.\n", name);
207 buffer_delete(&buffer);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800208 return 1;
209 }
210
Sol Boucher67d59982015-05-07 02:39:22 -0700211 if (IS_TOP_ALIGNED_ADDRESS(offset))
212 offset = convert_to_from_top_aligned(param.image_region,
213 -offset);
214
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800215 if (cbfs_add_entry(&image, &buffer, name, type, offset) != 0) {
216 ERROR("Failed to add '%s' into ROM image.\n", filename);
217 buffer_delete(&buffer);
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800218 return 1;
219 }
220
221 buffer_delete(&buffer);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000222 return 0;
223}
224
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600225static int cbfstool_convert_mkstage(struct buffer *buffer, uint32_t *offset)
226{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800227 struct buffer output;
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600228 int ret;
Sol Boucher0e539312015-03-05 15:38:03 -0800229 ret = parse_elf_to_stage(buffer, &output, param.algo, offset,
230 param.ignore_section);
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600231 if (ret != 0)
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800232 return -1;
233 buffer_delete(buffer);
234 // direct assign, no dupe.
235 memcpy(buffer, &output, sizeof(*buffer));
236 return 0;
237}
238
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100239static int cbfstool_convert_mkpayload(struct buffer *buffer,
240 unused uint32_t *offset)
241{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800242 struct buffer output;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800243 int ret;
244 /* per default, try and see if payload is an ELF binary */
Sol Boucher0e539312015-03-05 15:38:03 -0800245 ret = parse_elf_to_payload(buffer, &output, param.algo);
Stefan Reinauer543a6822013-02-04 15:39:13 -0800246
247 /* If it's not an ELF, see if it's a UEFI FV */
248 if (ret != 0)
249 ret = parse_fv_to_payload(buffer, &output, param.algo);
250
Patrick Georgide36d332013-08-27 20:22:21 +0200251 /* If it's neither ELF nor UEFI Fv, try bzImage */
252 if (ret != 0)
253 ret = parse_bzImage_to_payload(buffer, &output,
254 param.initrd, param.cmdline, param.algo);
255
Stefan Reinauer543a6822013-02-04 15:39:13 -0800256 /* Not a supported payload type */
257 if (ret != 0) {
258 ERROR("Not a supported payload type (ELF / FV).\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700259 buffer_delete(buffer);
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800260 return -1;
Stefan Reinauer543a6822013-02-04 15:39:13 -0800261 }
262
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800263 buffer_delete(buffer);
264 // direct assign, no dupe.
265 memcpy(buffer, &output, sizeof(*buffer));
266 return 0;
267}
268
269static int cbfstool_convert_mkflatpayload(struct buffer *buffer,
Stefan Reinauer2dd161f2015-03-04 00:55:03 +0100270 unused uint32_t *offset)
271{
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800272 struct buffer output;
273 if (parse_flat_binary_to_payload(buffer, &output,
274 param.loadaddress,
275 param.entrypoint,
276 param.algo) != 0) {
277 return -1;
278 }
279 buffer_delete(buffer);
280 // direct assign, no dupe.
281 memcpy(buffer, &output, sizeof(*buffer));
282 return 0;
283}
284
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800285static int cbfs_add(void)
286{
Sol Bouchere3260a02015-03-25 13:40:08 -0700287 return cbfs_add_component(param.filename,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800288 param.name,
289 param.type,
290 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800291 param.headeroffset,
Hung-Te Lin5f3eb262013-01-29 10:24:00 +0800292 NULL);
293}
294
Stefan Reinauer63217582012-10-29 16:52:36 -0700295static int cbfs_add_stage(void)
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700296{
Sol Bouchere3260a02015-03-25 13:40:08 -0700297 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800298 param.name,
299 CBFS_COMPONENT_STAGE,
300 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800301 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800302 cbfstool_convert_mkstage);
303}
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700304
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800305static int cbfs_add_payload(void)
306{
Sol Bouchere3260a02015-03-25 13:40:08 -0700307 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800308 param.name,
309 CBFS_COMPONENT_PAYLOAD,
310 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800311 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800312 cbfstool_convert_mkpayload);
Stefan Reinauer63217582012-10-29 16:52:36 -0700313}
314
315static int cbfs_add_flat_binary(void)
316{
Hung-Te Lind1739622013-01-28 14:23:49 +0800317 if (param.loadaddress == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800318 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700319 "-l/--load-address.\n");
320 return 1;
321 }
Hung-Te Lind1739622013-01-28 14:23:49 +0800322 if (param.entrypoint == 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800323 ERROR("You need to specify a valid "
Stefan Reinauer63217582012-10-29 16:52:36 -0700324 "-e/--entry-point.\n");
325 return 1;
326 }
Sol Bouchere3260a02015-03-25 13:40:08 -0700327 return cbfs_add_component(param.filename,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800328 param.name,
329 CBFS_COMPONENT_PAYLOAD,
330 param.baseaddress,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800331 param.headeroffset,
Hung-Te Linc13e4bf2013-01-29 15:22:11 +0800332 cbfstool_convert_mkflatpayload);
Stefan Reinauer20848ee2012-10-22 16:04:13 -0700333}
334
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200335static int cbfs_add_integer(void)
336{
Sol Bouchere3260a02015-03-25 13:40:08 -0700337 return cbfs_add_integer_component(param.name,
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200338 param.u64val,
Vadim Bendebury458a12e2014-12-23 15:10:12 -0800339 param.baseaddress,
340 param.headeroffset);
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200341}
342
Stefan Reinauer63217582012-10-29 16:52:36 -0700343static int cbfs_remove(void)
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800344{
Hung-Te Lind1739622013-01-28 14:23:49 +0800345 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800346 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700347 return 1;
348 }
349
Sol Bouchere3260a02015-03-25 13:40:08 -0700350 struct cbfs_image image;
351 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700352 param.headeroffset))
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800353 return 1;
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800354
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800355 if (cbfs_remove_entry(&image, param.name) != 0) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800356 ERROR("Removing file '%s' failed.\n",
Hung-Te Linc03d9b02013-01-29 02:38:40 +0800357 param.name);
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800358 return 1;
359 }
360
Gabe Blacke1bb49e2012-01-27 00:33:47 -0800361 return 0;
362}
363
Stefan Reinauer63217582012-10-29 16:52:36 -0700364static int cbfs_create(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000365{
Sol Boucher67a0a862015-03-18 12:36:27 -0700366 struct cbfs_image image;
367 memset(&image, 0, sizeof(image));
368 buffer_clone(&image.buffer, param.image_region);
369
370 if (param.fmap) {
371 if (param.arch != CBFS_ARCHITECTURE_UNKNOWN || param.size ||
372 param.baseaddress_assigned ||
373 param.headeroffset_assigned ||
374 param.cbfsoffset_assigned ||
375 param.alignment ||
376 param.bootblock) {
377 ERROR("Since -M was provided, -m, -s, -b, -o, -H, -a, and -B should be omitted\n");
378 return 1;
379 }
380
381 return cbfs_image_create(&image, image.buffer.size);
382 }
383
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -0600384 if (param.arch == CBFS_ARCHITECTURE_UNKNOWN) {
Stefan Reinauer60a4a732013-03-28 16:46:07 -0700385 ERROR("You need to specify -m/--machine arch.\n");
David Hendricks90ca3b62012-11-16 14:48:22 -0800386 return 1;
387 }
388
Sol Bouchere3260a02015-03-25 13:40:08 -0700389 struct buffer bootblock;
Julius Wernerefcee762014-11-10 13:14:24 -0800390 if (!param.bootblock) {
391 DEBUG("-B not given, creating image without bootblock.\n");
392 buffer_create(&bootblock, 0, "(dummy)");
393 } else if (buffer_from_file(&bootblock, param.bootblock)) {
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800394 return 1;
395 }
396
Julius Wernerefcee762014-11-10 13:14:24 -0800397 if (!param.alignment)
398 param.alignment = 64; // default CBFS entry alignment
399
400 // Set default offsets. x86, as usual, needs to be a special snowflake.
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800401 if (!param.baseaddress_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800402 if (param.arch == CBFS_ARCHITECTURE_X86) {
403 // Make sure there's at least enough room for rel_offset
Sol Boucher67a0a862015-03-18 12:36:27 -0700404 param.baseaddress = param.size -
405 MAX(bootblock.size, sizeof(int32_t));
Julius Wernerefcee762014-11-10 13:14:24 -0800406 DEBUG("x86 -> bootblock lies at end of ROM (%#x).\n",
407 param.baseaddress);
408 } else {
409 param.baseaddress = 0;
410 DEBUG("bootblock starts at address 0x0.\n");
411 }
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800412 }
413 if (!param.headeroffset_assigned) {
Julius Wernerefcee762014-11-10 13:14:24 -0800414 if (param.arch == CBFS_ARCHITECTURE_X86) {
415 param.headeroffset = param.baseaddress -
416 sizeof(struct cbfs_header);
417 DEBUG("x86 -> CBFS header before bootblock (%#x).\n",
418 param.headeroffset);
419 } else {
420 param.headeroffset = align_up(param.baseaddress +
421 bootblock.size, sizeof(uint32_t));
422 DEBUG("CBFS header placed behind bootblock (%#x).\n",
423 param.headeroffset);
424 }
425 }
426 if (!param.cbfsoffset_assigned) {
427 if (param.arch == CBFS_ARCHITECTURE_X86) {
428 param.cbfsoffset = 0;
429 DEBUG("x86 -> CBFS entries start at address 0x0.\n");
430 } else {
431 param.cbfsoffset = align_up(param.headeroffset +
432 sizeof(struct cbfs_header),
433 param.alignment);
434 DEBUG("CBFS entries start beind master header (%#x).\n",
435 param.cbfsoffset);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800436 }
437 }
438
Sol Boucher67a0a862015-03-18 12:36:27 -0700439 int ret = cbfs_legacy_image_create(&image,
440 param.arch,
441 param.alignment,
442 &bootblock,
443 param.baseaddress,
444 param.headeroffset,
445 param.cbfsoffset);
Sol Bouchere3260a02015-03-25 13:40:08 -0700446 buffer_delete(&bootblock);
Sol Boucher67a0a862015-03-18 12:36:27 -0700447 return ret;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000448}
449
Stefan Reinauer63217582012-10-29 16:52:36 -0700450static int cbfs_locate(void)
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000451{
Hung-Te Lind1739622013-01-28 14:23:49 +0800452 if (!param.filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800453 ERROR("You need to specify -f/--filename.\n");
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000454 return 1;
455 }
456
Hung-Te Lind1739622013-01-28 14:23:49 +0800457 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800458 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700459 return 1;
460 }
461
Sol Bouchere3260a02015-03-25 13:40:08 -0700462 struct cbfs_image image;
463 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700464 param.headeroffset))
465 return 1;
466
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800467 if (cbfs_get_entry(&image, param.name))
468 WARN("'%s' already in CBFS.\n", param.name);
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000469
Sol Bouchere3260a02015-03-25 13:40:08 -0700470 struct buffer buffer;
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800471 if (buffer_from_file(&buffer, param.filename) != 0) {
472 ERROR("Cannot load %s.\n", param.filename);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800473 return 1;
474 }
475
Sol Bouchere3260a02015-03-25 13:40:08 -0700476 int32_t address = cbfs_locate_entry(&image, param.name, buffer.size,
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800477 param.pagesize, param.alignment);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800478 buffer_delete(&buffer);
479
480 if (address == -1) {
Hung-Te Line4ea2ca2013-03-19 12:24:43 +0800481 ERROR("'%s' can't fit in CBFS for page-size %#x, align %#x.\n",
482 param.name, param.pagesize, param.alignment);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800483 return 1;
484 }
485
486 if (param.top_aligned)
Sol Boucher67d59982015-05-07 02:39:22 -0700487 address = -convert_to_from_top_aligned(param.image_region,
488 address);
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800489
Hung-Te Lin215d1d72013-01-29 03:46:02 +0800490 printf("0x%x\n", address);
491 return 0;
Patrick Georgi0da38dd2009-11-09 17:18:02 +0000492}
493
Sol Boucher67a0a862015-03-18 12:36:27 -0700494static int cbfs_layout(void)
495{
496 const struct fmap *fmap = partitioned_file_get_fmap(param.image_file);
497 if (!fmap) {
498 LOG("This is a legacy image composed entirely of a single CBFS.\n");
499 return 1;
500 }
501
502 printf("This image contains the following sections that can be %s with this tool:\n",
503 param.show_immutable ? "accessed" : "manipulated");
504 puts("");
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300505 for (unsigned i = 0; i < fmap->nareas; ++i) {
506 const struct fmap_area *current = fmap->areas + i;
Sol Boucher67a0a862015-03-18 12:36:27 -0700507
508 bool readonly = partitioned_file_fmap_count(param.image_file,
509 partitioned_file_fmap_select_children_of, current) ||
510 region_is_flashmap((const char *)current->name);
511 if (!param.show_immutable && readonly)
512 continue;
513
514 printf("'%s'", current->name);
515
516 // Detect consecutive sections that describe the same region and
517 // show them as aliases. This cannot find equivalent entries
518 // that aren't adjacent; however, fmaptool doesn't generate
519 // FMAPs with such sections, so this convenience feature works
520 // for all but the strangest manually created FMAP binaries.
521 // TODO: This could be done by parsing the FMAP into some kind
522 // of tree that had duplicate lists in addition to child lists,
523 // which would allow covering that weird, unlikely case as well.
524 unsigned lookahead;
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300525 for (lookahead = 1; i + lookahead < fmap->nareas;
Sol Boucher67a0a862015-03-18 12:36:27 -0700526 ++lookahead) {
527 const struct fmap_area *consecutive =
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300528 fmap->areas + i + lookahead;
Sol Boucher67a0a862015-03-18 12:36:27 -0700529 if (consecutive->offset != current->offset ||
530 consecutive->size != current->size)
531 break;
532 printf(", '%s'", consecutive->name);
533 }
534 if (lookahead > 1)
535 fputs(" are aliases for the same region", stdout);
536
537 const char *qualifier = "";
538 if (readonly)
539 qualifier = "read-only, ";
540 else if (region_is_modern_cbfs((const char *)current->name))
541 qualifier = "CBFS, ";
542 printf(" (%ssize %u)\n", qualifier, current->size);
543
Kyösti Mälkki2e042592015-05-15 09:14:09 +0300544 i += lookahead - 1;
Sol Boucher67a0a862015-03-18 12:36:27 -0700545 }
546 puts("");
547
548 if (param.show_immutable) {
549 puts("It is at least possible to perform the read action on every section listed above.");
550 } else {
551 puts("It is possible to perform either the write action or the CBFS add/remove actions on every section listed above.");
552 puts("To see the image's read-only sections as well, rerun with the -w option.");
553 }
554
555 return 0;
556}
557
Stefan Reinauer63217582012-10-29 16:52:36 -0700558static int cbfs_print(void)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000559{
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800560 struct cbfs_image image;
Sol Bouchere3260a02015-03-25 13:40:08 -0700561 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700562 param.headeroffset))
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000563 return 1;
Hung-Te Lin3bb035b2013-01-29 02:15:49 +0800564 cbfs_print_directory(&image);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000565 return 0;
566}
567
Stefan Reinauer63217582012-10-29 16:52:36 -0700568static int cbfs_extract(void)
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000569{
Hung-Te Lind1739622013-01-28 14:23:49 +0800570 if (!param.filename) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800571 ERROR("You need to specify -f/--filename.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700572 return 1;
573 }
574
Hung-Te Lind1739622013-01-28 14:23:49 +0800575 if (!param.name) {
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800576 ERROR("You need to specify -n/--name.\n");
Stefan Reinauer63217582012-10-29 16:52:36 -0700577 return 1;
578 }
579
Sol Bouchere3260a02015-03-25 13:40:08 -0700580 struct cbfs_image image;
581 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700582 param.headeroffset))
583 return 1;
584
585 return cbfs_export_entry(&image, param.name, param.filename);
586}
587
588static int cbfs_write(void)
589{
590 if (!param.filename) {
591 ERROR("You need to specify a valid input -f/--file.\n");
592 return 1;
593 }
594 if (!partitioned_file_is_partitioned(param.image_file)) {
595 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
Sol Bouchere3260a02015-03-25 13:40:08 -0700596 return 1;
597 }
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000598
Sol Boucher67a0a862015-03-18 12:36:27 -0700599 if (region_is_modern_cbfs(param.region_name)) {
600 ERROR("Target image region '%s' is a CBFS and must be manipulated using add and remove\n",
601 param.region_name);
602 return 1;
603 }
604
605 struct buffer new_content;
606 if (buffer_from_file(&new_content, param.filename))
607 return 1;
608
609 if (buffer_check_magic(&new_content, FMAP_SIGNATURE,
610 strlen(FMAP_SIGNATURE))) {
611 ERROR("File '%s' appears to be an FMAP and cannot be added to an existing image\n",
612 param.filename);
613 buffer_delete(&new_content);
614 return 1;
615 }
616 if (buffer_check_magic(&new_content, CBFS_FILE_MAGIC,
617 strlen(CBFS_FILE_MAGIC))) {
618 ERROR("File '%s' appears to be a CBFS and cannot be inserted into a raw region\n",
619 param.filename);
620 buffer_delete(&new_content);
621 return 1;
622 }
623
624 unsigned offset = 0;
625 if (param.fill_partial_upward && param.fill_partial_downward) {
626 ERROR("You may only specify one of -u and -d.\n");
627 buffer_delete(&new_content);
628 return 1;
629 } else if (!param.fill_partial_upward && !param.fill_partial_downward) {
630 if (new_content.size != param.image_region->size) {
631 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",
632 new_content.size, param.image_region->size);
633 buffer_delete(&new_content);
634 return 1;
635 }
636 } else {
637 if (new_content.size > param.image_region->size) {
638 ERROR("File to add is %zu bytes and would overflow %zu-byte target region\n",
639 new_content.size, param.image_region->size);
640 buffer_delete(&new_content);
641 return 1;
642 }
643 WARN("Written area will abut %s of target region: any unused space will keep its current contents\n",
644 param.fill_partial_upward ? "bottom" : "top");
645 if (param.fill_partial_downward)
646 offset = param.image_region->size - new_content.size;
647 }
648
649 memcpy(param.image_region->data + offset, new_content.data,
650 new_content.size);
651 buffer_delete(&new_content);
652 return 0;
653}
654
655static int cbfs_read(void)
656{
657 if (!param.filename) {
658 ERROR("You need to specify a valid output -f/--file.\n");
659 return 1;
660 }
661 if (!partitioned_file_is_partitioned(param.image_file)) {
662 ERROR("This operation isn't valid on legacy images having CBFS master headers\n");
663 return 1;
664 }
665
666 return buffer_write_file(param.image_region, param.filename);
Aurelien Guillaumefe7d6b92011-01-13 09:09:21 +0000667}
668
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600669static int cbfs_update_fit(void)
670{
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600671 if (!param.name) {
672 ERROR("You need to specify -n/--name.\n");
673 return 1;
674 }
675
676 if (param.fit_empty_entries <= 0) {
677 ERROR("Invalid number of fit entries "
678 "(-x/--empty-fits): %d\n", param.fit_empty_entries);
679 return 1;
680 }
681
Sol Boucher32532ac2015-05-06 14:44:40 -0700682 // Decide which region to read/write the FIT table from/to.
683 struct buffer bootblock;
684 if (partitioned_file_is_partitioned(param.image_file)) {
685 if (!partitioned_file_read_region(&bootblock, param.image_file,
686 SECTION_WITH_FIT_TABLE))
687 return 1;
688 } else {
689 // In legacy images, the bootblock is part of the CBFS.
690 buffer_clone(&bootblock, param.image_region);
691 }
692
Sol Bouchere3260a02015-03-25 13:40:08 -0700693 struct cbfs_image image;
694 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700695 param.headeroffset))
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600696 return 1;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600697
Sol Boucher32532ac2015-05-06 14:44:40 -0700698 if (fit_update_table(&bootblock, &image, param.name,
699 param.fit_empty_entries, convert_to_from_top_aligned))
700 return 1;
701
702 // The region to be written depends on the type of image, so we write it
703 // here rather than having main() write the CBFS region back as usual.
704 return !partitioned_file_write_region(param.image_file, &bootblock);
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600705}
706
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800707static int cbfs_copy(void)
708{
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800709 if (!param.copyoffset_assigned) {
710 ERROR("You need to specify -D/--copy_offset.\n");
711 return 1;
712 }
713
714 if (!param.size) {
715 ERROR("You need to specify -s/--size.\n");
716 return 1;
717 }
718
Sol Bouchere3260a02015-03-25 13:40:08 -0700719 struct cbfs_image image;
720 if (cbfs_image_from_buffer(&image, param.image_region,
Sol Boucher67a0a862015-03-18 12:36:27 -0700721 param.headeroffset))
722 return 1;
723
724 if (!cbfs_is_legacy_cbfs(&image)) {
725 ERROR("This operation is only valid on legacy images having CBFS master headers\n");
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800726 return 1;
Sol Bouchere3260a02015-03-25 13:40:08 -0700727 }
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800728
Sol Bouchere3260a02015-03-25 13:40:08 -0700729 return cbfs_copy_instance(&image, param.copyoffset, param.size);
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800730}
731
Sol Boucher67a0a862015-03-18 12:36:27 -0700732static bool cbfs_is_legacy_format(struct buffer *buffer)
733{
734 // Legacy CBFSes are those containing the deprecated CBFS master header.
735 return cbfs_find_header(buffer->data, buffer->size, -1);
736}
737
Stefan Reinauera1e48242011-10-21 14:24:57 -0700738static const struct command commands[] = {
Sol Boucher67a0a862015-03-18 12:36:27 -0700739 {"add", "H:r:f:n:t:b:vh?", cbfs_add, true, true},
740 {"add-flat-binary", "H:r:f:n:l:e:c:b:vh?", cbfs_add_flat_binary, true,
Sol Bouchere3260a02015-03-25 13:40:08 -0700741 true},
Sol Boucher67a0a862015-03-18 12:36:27 -0700742 {"add-payload", "H:r:f:n:t:c:b:C:I:vh?", cbfs_add_payload, true, true},
743 {"add-stage", "H:r:f:n:t:c:b:S:vh?", cbfs_add_stage, true, true},
744 {"add-int", "H:r:i:n:b:vh?", cbfs_add_integer, true, true},
745 {"copy", "H:D:s:h?", cbfs_copy, true, true},
746 {"create", "M:r:s:B:b:H:a:o:m:vh?", cbfs_create, true, true},
747 {"extract", "H:r:n:f:vh?", cbfs_extract, true, false},
748 {"locate", "H:r:f:n:P:a:Tvh?", cbfs_locate, true, false},
749 {"layout", "wvh?", cbfs_layout, false, false},
750 {"print", "H:r:vh?", cbfs_print, true, false},
751 {"read", "r:f:vh?", cbfs_read, true, false},
752 {"remove", "H:r:n:vh?", cbfs_remove, true, true},
Sol Boucher32532ac2015-05-06 14:44:40 -0700753 {"update-fit", "H:r:n:x:vh?", cbfs_update_fit, true, false},
Sol Boucher67a0a862015-03-18 12:36:27 -0700754 {"write", "r:f:udvh?", cbfs_write, true, true},
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000755};
756
Stefan Reinauer63217582012-10-29 16:52:36 -0700757static struct option long_options[] = {
Julius Wernerefcee762014-11-10 13:14:24 -0800758 {"alignment", required_argument, 0, 'a' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800759 {"base-address", required_argument, 0, 'b' },
760 {"bootblock", required_argument, 0, 'B' },
Julius Wernerefcee762014-11-10 13:14:24 -0800761 {"cmdline", required_argument, 0, 'C' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800762 {"compression", required_argument, 0, 'c' },
763 {"copy-offset", required_argument, 0, 'D' },
764 {"empty-fits", required_argument, 0, 'x' },
765 {"entry-point", required_argument, 0, 'e' },
766 {"file", required_argument, 0, 'f' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700767 {"fill-downward", no_argument, 0, 'd' },
768 {"fill-upward", no_argument, 0, 'u' },
769 {"flashmap", required_argument, 0, 'M' },
770 {"fmap-regions", required_argument, 0, 'r' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800771 {"header-offset", required_argument, 0, 'H' },
Julius Wernerefcee762014-11-10 13:14:24 -0800772 {"help", no_argument, 0, 'h' },
Vadim Bendebury45e59972014-12-23 15:59:57 -0800773 {"ignore-sec", required_argument, 0, 'S' },
774 {"initrd", required_argument, 0, 'I' },
775 {"int", required_argument, 0, 'i' },
776 {"load-address", required_argument, 0, 'l' },
777 {"machine", required_argument, 0, 'm' },
778 {"name", required_argument, 0, 'n' },
779 {"offset", required_argument, 0, 'o' },
780 {"page-size", required_argument, 0, 'P' },
781 {"size", required_argument, 0, 's' },
782 {"top-aligned", required_argument, 0, 'T' },
783 {"type", required_argument, 0, 't' },
784 {"verbose", no_argument, 0, 'v' },
Sol Boucher67a0a862015-03-18 12:36:27 -0700785 {"with-readonly", no_argument, 0, 'w' },
Julius Wernerefcee762014-11-10 13:14:24 -0800786 {NULL, 0, 0, 0 }
Stefan Reinauer63217582012-10-29 16:52:36 -0700787};
788
Sol Boucher67a0a862015-03-18 12:36:27 -0700789static int dispatch_command(struct command command)
790{
791 if (command.accesses_region) {
792 assert(param.image_file);
793
794 if (partitioned_file_is_partitioned(param.image_file)) {
795 LOG("Performing operation on '%s' region...\n",
796 param.region_name);
797 }
798 if (!partitioned_file_read_region(param.image_region,
799 param.image_file, param.region_name)) {
800 ERROR("The image will be left unmodified.\n");
801 return 1;
802 }
803
804 if (command.modifies_region) {
805 // We (intentionally) don't support overwriting the FMAP
806 // section. If you find yourself wanting to do this,
807 // consider creating a new image rather than performing
808 // whatever hacky transformation you were planning.
809 if (region_is_flashmap(param.region_name)) {
810 ERROR("Image region '%s' is read-only because it contains the FMAP.\n",
811 param.region_name);
812 ERROR("The image will be left unmodified.\n");
813 return 1;
814 }
815 // We don't allow writing raw data to regions that
816 // contain nested regions, since doing so would
817 // overwrite all such subregions.
818 if (partitioned_file_region_contains_nested(
819 param.image_file, param.region_name)) {
820 ERROR("Image region '%s' is read-only because it contains nested regions.\n",
821 param.region_name);
822 ERROR("The image will be left unmodified.\n");
823 return 1;
824 }
825 }
826 }
827
828 if (command.function()) {
829 if (partitioned_file_is_partitioned(param.image_file)) {
830 ERROR("Failed while operating on '%s' region!\n",
831 param.region_name);
832 ERROR("The image will be left unmodified.\n");
833 }
834 return 1;
835 }
836
837 return 0;
838}
839
Stefan Reinauer63217582012-10-29 16:52:36 -0700840static void usage(char *name)
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000841{
842 printf
Stefan Reinauer07040582010-04-24 21:24:06 +0000843 ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
Stefan Reinauer63217582012-10-29 16:52:36 -0700844 "USAGE:\n" " %s [-h]\n"
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800845 " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700846 " -H header_offset Do not search for header; use this offset*\n"
Sol Boucher67d59982015-05-07 02:39:22 -0700847 " -T Output top-aligned memory address\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700848 " -u Accept short data; fill upward/from bottom\n"
849 " -d Accept short data; fill downward/from top\n"
850 " -v Provide verbose output\n"
851 " -h Display this help message\n\n"
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000852 "COMMANDs:\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700853 " add [-r image,regions] -f FILE -n NAME -t TYPE \\\n"
854 " [-b base-address] "
Stefan Reinauer63217582012-10-29 16:52:36 -0700855 "Add a component\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700856 " add-payload [-r image,regions] -f FILE -n NAME \\\n"
857 " [-c compression] [-b base-address] "
Stefan Reinauer63217582012-10-29 16:52:36 -0700858 "Add a payload to the ROM\n"
Patrick Georgide36d332013-08-27 20:22:21 +0200859 " (linux specific: [-C cmdline] [-I initrd])\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700860 " add-stage [-r image,regions] -f FILE -n NAME \\\n"
861 " [-c compression] [-b base] [-S section-to-ignore] "
Stefan Reinauer63217582012-10-29 16:52:36 -0700862 "Add a stage to the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700863 " add-flat-binary [-r image,regions] -f FILE -n NAME \\\n"
864 " -l load-address -e entry-point [-c compression] \\\n"
865 " [-b base] "
Stefan Reinauer63217582012-10-29 16:52:36 -0700866 "Add a 32bit flat mode binary\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700867 " add-int [-r image,regions] -i INTEGER -n NAME [-b base] "
Peter Stuge3bfd5b82013-07-09 19:39:13 +0200868 "Add a raw 64-bit integer value\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700869 " remove [-r image,regions] -n NAME "
Stefan Reinauer63217582012-10-29 16:52:36 -0700870 "Remove a component\n"
Vadim Bendebury5e273a42014-12-23 19:26:54 -0800871 " copy -D new_header_offset -s region size \\\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700872 " [-H source header offset] "
873 "Create a copy (duplicate) cbfs instance*\n"
874 " create -m ARCH -s size [-b bootblock offset] \\\n"
875 " [-o CBFS offset] [-H header offset] [-B bootblock] \\\n"
876 " [-a align] "
877 "Create a legacy ROM file with CBFS master header*\n"
878 " create -M flashmap [-r list,of,regions,containing,cbfses] "
879 "Create a new-style partitioned firmware image\n"
880 " locate [-r image,regions] -f FILE -n NAME [-P page-size] \\\n"
881 " [-a align] [-T] "
Stefan Reinauer63217582012-10-29 16:52:36 -0700882 "Find a place for a file of that size\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700883 " layout [-w] "
884 "List mutable (or, with -w, readable) image regions\n"
885 " print [-r image,regions] "
Stefan Reinauer63217582012-10-29 16:52:36 -0700886 "Show the contents of the ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700887 " extract [-r image,regions] -n NAME -f FILE "
Stefan Reinauer63217582012-10-29 16:52:36 -0700888 "Extracts a raw payload from ROM\n"
Sol Boucher67a0a862015-03-18 12:36:27 -0700889 " write -r image,regions -f file [-u | -d] "
890 "Write file into same-size [or larger] raw region\n"
891 " read [-r fmap-region] -f file "
892 "Extract raw region contents into binary file\n"
893 " update-fit [-r image,regions] -n MICROCODE_BLOB_NAME \\\n"
894 " -x EMTPY_FIT_ENTRIES "
Aaron Durbin6b0d0d62012-12-14 17:16:21 -0600895 "Updates the FIT table with microcode entries\n"
Peter Stugeb347e0d2011-01-17 05:02:09 +0000896 "\n"
Sol Boucher0e539312015-03-05 15:38:03 -0800897 "OFFSETs:\n"
Sol Boucher67d59982015-05-07 02:39:22 -0700898 " Numbers accompanying -b, -H, and -o switches* may be provided\n"
899 " in two possible formats: if their value is greater than\n"
Sol Boucher0e539312015-03-05 15:38:03 -0800900 " 0x80000000, they are interpreted as a top-aligned x86 memory\n"
901 " address; otherwise, they are treated as an offset into flash.\n"
David Hendricks90ca3b62012-11-16 14:48:22 -0800902 "ARCHes:\n"
Paul Burton33186922014-06-13 23:56:45 +0100903 " arm64, arm, mips, x86\n"
Stefan Reinauer63217582012-10-29 16:52:36 -0700904 "TYPEs:\n", name, name
905 );
Stefan Reinauer07040582010-04-24 21:24:06 +0000906 print_supported_filetypes();
Sol Boucher67a0a862015-03-18 12:36:27 -0700907
908 printf(
909 "\n* Note that these actions and switches are only valid when\n"
910 " working with legacy images whose structure is described\n"
911 " primarily by a CBFS master header. New-style images, in\n"
912 " contrast, exclusively make use of an FMAP to describe their\n"
913 " layout: this must minimally contain an '%s' section\n"
914 " specifying the location of this FMAP itself and a '%s'\n"
915 " section describing the primary CBFS. It should also be noted\n"
916 " that, when working with such images, the -F and -r switches\n"
917 " default to '%s' for convenience, and both the -b switch to\n"
918 " CBFS operations and the output of the locate action become\n"
Sol Boucher67d59982015-05-07 02:39:22 -0700919 " relative to the selected CBFS region's lowest address.\n"
920 " The one exception to this rule is the top-aligned address,\n"
921 " which is always relative to the end of the entire image\n"
922 " rather than relative to the local region; this is true for\n"
923 " for both input (sufficiently large) and output (-T) data.\n",
Sol Boucher67a0a862015-03-18 12:36:27 -0700924 SECTION_NAME_FMAP, SECTION_NAME_PRIMARY_CBFS,
925 SECTION_NAME_PRIMARY_CBFS
926 );
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000927}
928
929int main(int argc, char **argv)
930{
Mathias Krause41c229c2012-07-17 21:17:15 +0200931 size_t i;
Stefan Reinauer63217582012-10-29 16:52:36 -0700932 int c;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000933
934 if (argc < 3) {
Stefan Reinauer63217582012-10-29 16:52:36 -0700935 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000936 return 1;
937 }
938
Sol Bouchere3260a02015-03-25 13:40:08 -0700939 char *image_name = argv[1];
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000940 char *cmd = argv[2];
Stefan Reinauer63217582012-10-29 16:52:36 -0700941 optind += 2;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +0000942
943 for (i = 0; i < ARRAY_SIZE(commands); i++) {
944 if (strcmp(cmd, commands[i].name) != 0)
945 continue;
Stefan Reinauer63217582012-10-29 16:52:36 -0700946
947 while (1) {
948 char *suffix = NULL;
949 int option_index = 0;
950
951 c = getopt_long(argc, argv, commands[i].optstring,
952 long_options, &option_index);
953 if (c == -1)
954 break;
955
956 /* filter out illegal long options */
zbao062730d2013-01-08 10:10:16 +0800957 if (strchr(commands[i].optstring, c) == NULL) {
Stefan Reinauer63217582012-10-29 16:52:36 -0700958 /* TODO maybe print actual long option instead */
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800959 ERROR("%s: invalid option -- '%c'\n",
960 argv[0], c);
Stefan Reinauer63217582012-10-29 16:52:36 -0700961 c = '?';
962 }
963
964 switch(c) {
965 case 'n':
Hung-Te Lind1739622013-01-28 14:23:49 +0800966 param.name = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -0700967 break;
968 case 't':
969 if (intfiletype(optarg) != ((uint64_t) - 1))
Hung-Te Lind1739622013-01-28 14:23:49 +0800970 param.type = intfiletype(optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -0700971 else
Hung-Te Lind1739622013-01-28 14:23:49 +0800972 param.type = strtoul(optarg, NULL, 0);
973 if (param.type == 0)
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800974 WARN("Unknown type '%s' ignored\n",
Stefan Reinauer63217582012-10-29 16:52:36 -0700975 optarg);
976 break;
977 case 'c':
978 if (!strncasecmp(optarg, "lzma", 5))
Hung-Te Lind1739622013-01-28 14:23:49 +0800979 param.algo = CBFS_COMPRESS_LZMA;
Stefan Reinauer63217582012-10-29 16:52:36 -0700980 else if (!strncasecmp(optarg, "none", 5))
Hung-Te Lind1739622013-01-28 14:23:49 +0800981 param.algo = CBFS_COMPRESS_NONE;
Stefan Reinauer63217582012-10-29 16:52:36 -0700982 else
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +0800983 WARN("Unknown compression '%s'"
984 " ignored.\n", optarg);
Stefan Reinauer63217582012-10-29 16:52:36 -0700985 break;
Sol Boucher67a0a862015-03-18 12:36:27 -0700986 case 'M':
987 param.fmap = optarg;
988 break;
989 case 'r':
990 param.region_name = optarg;
991 break;
Stefan Reinauer63217582012-10-29 16:52:36 -0700992 case 'b':
Hung-Te Lind1739622013-01-28 14:23:49 +0800993 param.baseaddress = strtoul(optarg, NULL, 0);
Hung-Te Linf56c73f2013-01-29 09:45:12 +0800994 // baseaddress may be zero on non-x86, so we
995 // need an explicit "baseaddress_assigned".
996 param.baseaddress = strtoul(optarg, NULL, 0);
997 param.baseaddress_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -0700998 break;
999 case 'l':
Hung-Te Lind1739622013-01-28 14:23:49 +08001000 param.loadaddress = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001001 break;
1002 case 'e':
Hung-Te Lind1739622013-01-28 14:23:49 +08001003 param.entrypoint = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001004 break;
1005 case 's':
Hung-Te Lind1739622013-01-28 14:23:49 +08001006 param.size = strtoul(optarg, &suffix, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001007 if (tolower(suffix[0])=='k') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001008 param.size *= 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001009 }
1010 if (tolower(suffix[0])=='m') {
Hung-Te Lind1739622013-01-28 14:23:49 +08001011 param.size *= 1024 * 1024;
Stefan Reinauer63217582012-10-29 16:52:36 -07001012 }
Patrick Georgie887ca52014-08-09 17:44:39 +02001013 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001014 case 'B':
Hung-Te Lind1739622013-01-28 14:23:49 +08001015 param.bootblock = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001016 break;
Hung-Te Linf56c73f2013-01-29 09:45:12 +08001017 case 'H':
1018 param.headeroffset = strtoul(
1019 optarg, NULL, 0);
1020 param.headeroffset_assigned = 1;
1021 break;
Vadim Bendebury5e273a42014-12-23 19:26:54 -08001022 case 'D':
1023 param.copyoffset = strtoul(optarg, NULL, 0);
1024 param.copyoffset_assigned = 1;
Vadim Bendebury11614732015-01-08 16:36:35 -08001025 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001026 case 'a':
Hung-Te Lind1739622013-01-28 14:23:49 +08001027 param.alignment = strtoul(optarg, NULL, 0);
Stefan Reinauer63217582012-10-29 16:52:36 -07001028 break;
Hung-Te Line9198372013-03-19 12:17:12 +08001029 case 'P':
1030 param.pagesize = strtoul(optarg, NULL, 0);
1031 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001032 case 'o':
Julius Wernerefcee762014-11-10 13:14:24 -08001033 param.cbfsoffset = strtoul(optarg, NULL, 0);
1034 param.cbfsoffset_assigned = 1;
Stefan Reinauer63217582012-10-29 16:52:36 -07001035 break;
1036 case 'f':
Hung-Te Lind1739622013-01-28 14:23:49 +08001037 param.filename = optarg;
Stefan Reinauer63217582012-10-29 16:52:36 -07001038 break;
Peter Stuge3bfd5b82013-07-09 19:39:13 +02001039 case 'i':
1040 param.u64val = strtoull(optarg, NULL, 0);
1041 break;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001042 case 'T':
Sol Boucher67a0a862015-03-18 12:36:27 -07001043 param.top_aligned = true;
1044 break;
1045 case 'u':
1046 param.fill_partial_upward = true;
1047 break;
1048 case 'd':
1049 param.fill_partial_downward = true;
1050 break;
1051 case 'w':
1052 param.show_immutable = true;
Hung-Te Lin215d1d72013-01-29 03:46:02 +08001053 break;
Aaron Durbin6b0d0d62012-12-14 17:16:21 -06001054 case 'x':
1055 param.fit_empty_entries = strtol(optarg, NULL, 0);
1056 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001057 case 'v':
1058 verbose++;
1059 break;
David Hendricks90ca3b62012-11-16 14:48:22 -08001060 case 'm':
Alexandru Gagniuc35850ae2014-02-02 22:37:28 -06001061 param.arch = string_to_arch(optarg);
David Hendricks90ca3b62012-11-16 14:48:22 -08001062 break;
Patrick Georgide36d332013-08-27 20:22:21 +02001063 case 'I':
1064 param.initrd = optarg;
1065 break;
1066 case 'C':
1067 param.cmdline = optarg;
1068 break;
Furquan Shaikh405304a2014-10-30 11:44:20 -07001069 case 'S':
1070 param.ignore_section = optarg;
1071 break;
Stefan Reinauer63217582012-10-29 16:52:36 -07001072 case 'h':
1073 case '?':
1074 usage(argv[0]);
1075 return 1;
1076 default:
1077 break;
1078 }
1079 }
1080
Sol Bouchere3260a02015-03-25 13:40:08 -07001081 if (commands[i].function == cbfs_create) {
Sol Boucher67a0a862015-03-18 12:36:27 -07001082 if (param.fmap) {
1083 struct buffer flashmap;
1084 if (buffer_from_file(&flashmap, param.fmap))
1085 return 1;
1086 param.image_file = partitioned_file_create(
1087 image_name, &flashmap);
1088 buffer_delete(&flashmap);
1089 } else if (param.size) {
1090 param.image_file = partitioned_file_create_flat(
1091 image_name, param.size);
1092 } else {
1093 ERROR("You need to specify a valid -M/--flashmap or -s/--size.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001094 return 1;
1095 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001096 } else {
1097 param.image_file =
1098 partitioned_file_reopen(image_name,
Sol Boucher67a0a862015-03-18 12:36:27 -07001099 cbfs_is_legacy_format);
Sol Bouchere3260a02015-03-25 13:40:08 -07001100 }
1101 if (!param.image_file)
1102 return 1;
1103
Sol Boucher67a0a862015-03-18 12:36:27 -07001104 unsigned num_regions = 1;
1105 for (const char *list = strchr(param.region_name, ','); list;
1106 list = strchr(list + 1, ','))
1107 ++num_regions;
1108
Sol Bouchere3260a02015-03-25 13:40:08 -07001109 // If the action needs to read an image region, as indicated by
1110 // having accesses_region set in its command struct, that
1111 // region's buffer struct will be stored here and the client
1112 // will receive a pointer to it via param.image_region. It
1113 // need not write the buffer back to the image file itself,
1114 // since this behavior can be requested via its modifies_region
1115 // field. Additionally, it should never free the region buffer,
1116 // as that is performed automatically once it completes.
Sol Boucher67a0a862015-03-18 12:36:27 -07001117 struct buffer image_regions[num_regions];
1118 memset(image_regions, 0, sizeof(image_regions));
Sol Bouchere3260a02015-03-25 13:40:08 -07001119
Sol Boucher67a0a862015-03-18 12:36:27 -07001120 bool seen_primary_cbfs = false;
1121 char region_name_scratch[strlen(param.region_name) + 1];
1122 strcpy(region_name_scratch, param.region_name);
1123 param.region_name = strtok(region_name_scratch, ",");
1124 for (unsigned region = 0; region < num_regions; ++region) {
1125 if (!param.region_name) {
1126 ERROR("Encountered illegal degenerate region name in -r list\n");
1127 ERROR("The image will be left unmodified.\n");
Sol Bouchere3260a02015-03-25 13:40:08 -07001128 partitioned_file_close(param.image_file);
1129 return 1;
1130 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001131
Sol Boucher67a0a862015-03-18 12:36:27 -07001132 if (strcmp(param.region_name, SECTION_NAME_PRIMARY_CBFS)
1133 == 0)
1134 seen_primary_cbfs = true;
Sol Bouchere3260a02015-03-25 13:40:08 -07001135
Sol Boucher67a0a862015-03-18 12:36:27 -07001136 param.image_region = image_regions + region;
1137 if (dispatch_command(commands[i])) {
Sol Bouchere3260a02015-03-25 13:40:08 -07001138 partitioned_file_close(param.image_file);
1139 return 1;
1140 }
Sol Boucher67a0a862015-03-18 12:36:27 -07001141
1142 param.region_name = strtok(NULL, ",");
1143 }
1144
1145 if (commands[i].function == cbfs_create && !seen_primary_cbfs) {
1146 ERROR("The creation -r list must include the mandatory '%s' section.\n",
1147 SECTION_NAME_PRIMARY_CBFS);
1148 ERROR("The image will be left unmodified.\n");
1149 partitioned_file_close(param.image_file);
1150 return 1;
1151 }
1152
1153 if (commands[i].modifies_region) {
1154 assert(param.image_file);
1155 assert(commands[i].accesses_region);
1156 for (unsigned region = 0; region < num_regions;
1157 ++region) {
1158
1159 if (!partitioned_file_write_region(
1160 param.image_file,
1161 image_regions + region)) {
1162 partitioned_file_close(
1163 param.image_file);
1164 return 1;
1165 }
1166 }
Sol Bouchere3260a02015-03-25 13:40:08 -07001167 }
1168
1169 partitioned_file_close(param.image_file);
Sol Boucher67a0a862015-03-18 12:36:27 -07001170 return 0;
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001171 }
1172
Hung-Te Lin4d87d4e2013-01-28 14:39:43 +08001173 ERROR("Unknown command '%s'.\n", cmd);
Stefan Reinauer63217582012-10-29 16:52:36 -07001174 usage(argv[0]);
Stefan Reinauer3fec29c2009-09-22 15:58:19 +00001175 return 1;
1176}