blob: 0040018bbb04aff658b3b488e17e67b2e214ab25 [file] [log] [blame]
dp-arm4972ec52016-05-25 16:20:20 +01001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/types.h>
32#include <sys/stat.h>
33
34#include <assert.h>
35#include <errno.h>
36#include <getopt.h>
37#include <limits.h>
38#include <stdarg.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
44
dp-arm12e893b2016-08-24 13:21:08 +010045#include <openssl/sha.h>
46
dp-arm4972ec52016-05-25 16:20:20 +010047#include "fiptool.h"
48#include "firmware_image_package.h"
49#include "tbbr_config.h"
50
51#define OPT_TOC_ENTRY 0
52#define OPT_PLAT_TOC_FLAGS 1
53
dp-arm90d2f0e2016-11-14 15:54:32 +000054static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid);
55static image_t *lookup_image_from_uuid(const uuid_t *uuid);
dp-arm4972ec52016-05-25 16:20:20 +010056static int info_cmd(int argc, char *argv[]);
57static void info_usage(void);
58static int create_cmd(int argc, char *argv[]);
59static void create_usage(void);
60static int update_cmd(int argc, char *argv[]);
61static void update_usage(void);
62static int unpack_cmd(int argc, char *argv[]);
63static void unpack_usage(void);
64static int remove_cmd(int argc, char *argv[]);
65static void remove_usage(void);
66static int version_cmd(int argc, char *argv[]);
67static void version_usage(void);
68static int help_cmd(int argc, char *argv[]);
69static void usage(void);
70
71/* Available subcommands. */
72static cmd_t cmds[] = {
73 { .name = "info", .handler = info_cmd, .usage = info_usage },
74 { .name = "create", .handler = create_cmd, .usage = create_usage },
75 { .name = "update", .handler = update_cmd, .usage = update_usage },
76 { .name = "unpack", .handler = unpack_cmd, .usage = unpack_usage },
77 { .name = "remove", .handler = remove_cmd, .usage = remove_usage },
78 { .name = "version", .handler = version_cmd, .usage = version_usage },
79 { .name = "help", .handler = help_cmd, .usage = NULL },
80};
81
dp-arm90d2f0e2016-11-14 15:54:32 +000082static image_desc_t *image_desc_head;
83static size_t nr_image_descs;
84static image_t *image_head;
dp-arm4972ec52016-05-25 16:20:20 +010085static size_t nr_images;
86static uuid_t uuid_null = { 0 };
87static int verbose;
88
dp-armc1f8e772016-11-04 10:52:25 +000089static void vlog(int prio, const char *msg, va_list ap)
dp-arm4972ec52016-05-25 16:20:20 +010090{
91 char *prefix[] = { "DEBUG", "WARN", "ERROR" };
92
93 fprintf(stderr, "%s: ", prefix[prio]);
94 vfprintf(stderr, msg, ap);
95 fputc('\n', stderr);
96}
97
dp-armc1f8e772016-11-04 10:52:25 +000098static void log_dbgx(const char *msg, ...)
dp-arm4972ec52016-05-25 16:20:20 +010099{
100 va_list ap;
101
102 va_start(ap, msg);
103 vlog(LOG_DBG, msg, ap);
104 va_end(ap);
105}
106
dp-armc1f8e772016-11-04 10:52:25 +0000107static void log_warnx(const char *msg, ...)
dp-arm4972ec52016-05-25 16:20:20 +0100108{
109 va_list ap;
110
111 va_start(ap, msg);
112 vlog(LOG_WARN, msg, ap);
113 va_end(ap);
114}
115
dp-armc1f8e772016-11-04 10:52:25 +0000116static void log_err(const char *msg, ...)
dp-arm4972ec52016-05-25 16:20:20 +0100117{
118 char buf[512];
119 va_list ap;
120
121 va_start(ap, msg);
122 snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno));
123 vlog(LOG_ERR, buf, ap);
124 va_end(ap);
125 exit(1);
126}
127
dp-armc1f8e772016-11-04 10:52:25 +0000128static void log_errx(const char *msg, ...)
dp-arm4972ec52016-05-25 16:20:20 +0100129{
130 va_list ap;
131
132 va_start(ap, msg);
133 vlog(LOG_ERR, msg, ap);
134 va_end(ap);
135 exit(1);
136}
137
dp-armdb0f5e92016-11-04 10:56:25 +0000138static char *xstrdup(const char *s, const char *msg)
139{
140 char *d;
141
142 d = strdup(s);
143 if (d == NULL)
dp-arm5ba38542016-12-21 14:59:30 +0000144 log_errx("strdup: %s", msg);
dp-armdb0f5e92016-11-04 10:56:25 +0000145 return d;
146}
147
148static void *xmalloc(size_t size, const char *msg)
149{
150 void *d;
151
152 d = malloc(size);
153 if (d == NULL)
dp-arm5ba38542016-12-21 14:59:30 +0000154 log_errx("malloc: %s", msg);
dp-armdb0f5e92016-11-04 10:56:25 +0000155 return d;
156}
157
Masahiro Yamada03fdf692017-01-15 00:50:41 +0900158static void *xzalloc(size_t size, const char *msg)
159{
160 return memset(xmalloc(size, msg), 0, size);
161}
162
dp-arm90d2f0e2016-11-14 15:54:32 +0000163static image_desc_t *new_image_desc(const uuid_t *uuid,
164 const char *name, const char *cmdline_name)
dp-arm4972ec52016-05-25 16:20:20 +0100165{
dp-arm90d2f0e2016-11-14 15:54:32 +0000166 image_desc_t *desc;
167
Masahiro Yamada03fdf692017-01-15 00:50:41 +0900168 desc = xzalloc(sizeof(*desc),
dp-arm90d2f0e2016-11-14 15:54:32 +0000169 "failed to allocate memory for image descriptor");
170 memcpy(&desc->uuid, uuid, sizeof(uuid_t));
171 desc->name = xstrdup(name,
172 "failed to allocate memory for image name");
173 desc->cmdline_name = xstrdup(cmdline_name,
174 "failed to allocate memory for image command line name");
175 desc->action = DO_UNSPEC;
dp-arm90d2f0e2016-11-14 15:54:32 +0000176 return desc;
dp-arm4972ec52016-05-25 16:20:20 +0100177}
178
dp-armfb732312016-12-30 09:55:48 +0000179static void set_image_desc_action(image_desc_t *desc, int action,
180 const char *arg)
181{
182 assert(desc != NULL);
183
184 if (desc->action_arg != DO_UNSPEC)
185 free(desc->action_arg);
186 desc->action = action;
187 desc->action_arg = NULL;
188 if (arg != NULL)
189 desc->action_arg = xstrdup(arg,
190 "failed to allocate memory for argument");
191}
192
dp-arm90d2f0e2016-11-14 15:54:32 +0000193static void free_image_desc(image_desc_t *desc)
dp-arm4972ec52016-05-25 16:20:20 +0100194{
dp-arm90d2f0e2016-11-14 15:54:32 +0000195 free(desc->name);
196 free(desc->cmdline_name);
197 free(desc->action_arg);
198 free(desc);
dp-arm4972ec52016-05-25 16:20:20 +0100199}
200
dp-arm90d2f0e2016-11-14 15:54:32 +0000201static void add_image_desc(image_desc_t *desc)
dp-arm4972ec52016-05-25 16:20:20 +0100202{
Masahiro Yamadad224b452017-01-14 23:22:02 +0900203 image_desc_t **p = &image_desc_head;
204
Masahiro Yamadad224b452017-01-14 23:22:02 +0900205 while (*p)
206 p = &(*p)->next;
207
Masahiro Yamadac2a7d9c2017-01-27 12:53:13 +0900208 assert(*p == NULL);
Masahiro Yamadad224b452017-01-14 23:22:02 +0900209 *p = desc;
dp-arm90d2f0e2016-11-14 15:54:32 +0000210 nr_image_descs++;
211}
dp-arm4972ec52016-05-25 16:20:20 +0100212
dp-arm90d2f0e2016-11-14 15:54:32 +0000213static void free_image_descs(void)
214{
215 image_desc_t *desc = image_desc_head, *tmp;
216
217 while (desc != NULL) {
218 tmp = desc->next;
219 free_image_desc(desc);
220 desc = tmp;
221 nr_image_descs--;
dp-arm4972ec52016-05-25 16:20:20 +0100222 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000223 assert(nr_image_descs == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100224}
225
dp-arm90d2f0e2016-11-14 15:54:32 +0000226static void fill_image_descs(void)
227{
228 toc_entry_t *toc_entry;
229
230 for (toc_entry = toc_entries;
231 toc_entry->cmdline_name != NULL;
232 toc_entry++) {
233 image_desc_t *desc;
234
235 desc = new_image_desc(&toc_entry->uuid,
236 toc_entry->name,
237 toc_entry->cmdline_name);
238 add_image_desc(desc);
239 }
240}
241
242static void add_image(image_t *image)
243{
Masahiro Yamadad224b452017-01-14 23:22:02 +0900244 image_t **p = &image_head;
245
Masahiro Yamadad224b452017-01-14 23:22:02 +0900246 while (*p)
247 p = &(*p)->next;
248
Masahiro Yamadac2a7d9c2017-01-27 12:53:13 +0900249 assert(*p == NULL);
Masahiro Yamadad224b452017-01-14 23:22:02 +0900250 *p = image;
251
dp-arm90d2f0e2016-11-14 15:54:32 +0000252 nr_images++;
253}
254
Masahiro Yamadad65e7fd2017-01-27 11:57:54 +0900255static void replace_image(image_t *image)
256{
257 image_t **p = &image_head;
258
259 while (*p) {
260 if (!memcmp(&(*p)->uuid, &image->uuid, sizeof(image->uuid)))
261 break;
262 p = &(*p)->next;
263 }
264
265 assert(*p != NULL);
266
267 image->next = (*p)->next;
268 *p = image;
269}
270
dp-arm90d2f0e2016-11-14 15:54:32 +0000271static void free_image(image_t *image)
272{
273 free(image->buffer);
274 free(image);
275}
276
dp-arm4972ec52016-05-25 16:20:20 +0100277static void remove_image(image_t *image)
278{
Masahiro Yamadac0d2d982017-01-27 13:31:40 +0900279 image_t *tmp, **p = &image_head;
dp-arm4972ec52016-05-25 16:20:20 +0100280
Masahiro Yamadac0d2d982017-01-27 13:31:40 +0900281 while (*p) {
282 if (*p == image)
283 break;
284 p = &(*p)->next;
dp-arm4972ec52016-05-25 16:20:20 +0100285 }
Masahiro Yamadac0d2d982017-01-27 13:31:40 +0900286
287 assert(*p != NULL);
288
289 tmp = *p;
290 *p = tmp->next;
291 free_image(tmp);
292
dp-arm4972ec52016-05-25 16:20:20 +0100293 nr_images--;
294}
295
296static void free_images(void)
297{
dp-arm90d2f0e2016-11-14 15:54:32 +0000298 image_t *image = image_head, *tmp;
dp-arm4972ec52016-05-25 16:20:20 +0100299
dp-arm90d2f0e2016-11-14 15:54:32 +0000300 while (image != NULL) {
301 tmp = image->next;
302 free_image(image);
303 image = tmp;
304 nr_images--;
dp-arm4972ec52016-05-25 16:20:20 +0100305 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000306 assert(nr_images == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100307}
308
dp-arm90d2f0e2016-11-14 15:54:32 +0000309static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
dp-arm4972ec52016-05-25 16:20:20 +0100310{
dp-arm90d2f0e2016-11-14 15:54:32 +0000311 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100312
dp-arm90d2f0e2016-11-14 15:54:32 +0000313 for (desc = image_desc_head; desc != NULL; desc = desc->next)
314 if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
315 return desc;
dp-arm4972ec52016-05-25 16:20:20 +0100316 return NULL;
317}
318
dp-arm90d2f0e2016-11-14 15:54:32 +0000319static image_desc_t *lookup_image_desc_from_opt(const char *opt)
320{
321 image_desc_t *desc;
322
323 for (desc = image_desc_head; desc != NULL; desc = desc->next)
324 if (strcmp(desc->cmdline_name, opt) == 0)
325 return desc;
326 return NULL;
327}
328
329static image_t *lookup_image_from_uuid(const uuid_t *uuid)
dp-arm715ef422016-08-30 14:18:58 +0100330{
331 image_t *image;
dp-arm715ef422016-08-30 14:18:58 +0100332
dp-arm90d2f0e2016-11-14 15:54:32 +0000333 for (image = image_head; image != NULL; image = image->next)
dp-arm715ef422016-08-30 14:18:58 +0100334 if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0)
335 return image;
dp-arm715ef422016-08-30 14:18:58 +0100336 return NULL;
337}
338
dp-arm516dfcb2016-11-03 13:59:26 +0000339static void uuid_to_str(char *s, size_t len, const uuid_t *u)
340{
341 assert(len >= (_UUID_STR_LEN + 1));
342
343 snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X",
344 u->time_low,
345 u->time_mid,
346 u->time_hi_and_version,
347 ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
348 ((uint16_t)u->node[0] << 8) | u->node[1],
349 ((uint16_t)u->node[2] << 8) | u->node[3],
350 ((uint16_t)u->node[4] << 8) | u->node[5]);
351}
352
353static void uuid_from_str(uuid_t *u, const char *s)
354{
355 int n;
356
357 if (s == NULL)
358 log_errx("UUID cannot be NULL");
359 if (strlen(s) != _UUID_STR_LEN)
360 log_errx("Invalid UUID: %s", s);
361
362 n = sscanf(s,
363 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
364 &u->time_low, &u->time_mid, &u->time_hi_and_version,
365 &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
366 &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
367 /*
368 * Given the format specifier above, we expect 11 items to be scanned
369 * for a properly formatted UUID.
370 */
371 if (n != 11)
372 log_errx("Invalid UUID: %s", s);
373}
374
dp-armc1f8e772016-11-04 10:52:25 +0000375static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
dp-arm4972ec52016-05-25 16:20:20 +0100376{
377 struct stat st;
378 FILE *fp;
379 char *buf, *bufend;
380 fip_toc_header_t *toc_header;
381 fip_toc_entry_t *toc_entry;
dp-arm4972ec52016-05-25 16:20:20 +0100382 int terminated = 0;
383
384 fp = fopen(filename, "r");
385 if (fp == NULL)
386 log_err("fopen %s", filename);
387
388 if (fstat(fileno(fp), &st) == -1)
389 log_err("fstat %s", filename);
390
dp-armdb0f5e92016-11-04 10:56:25 +0000391 buf = xmalloc(st.st_size, "failed to load file into memory");
dp-arm4972ec52016-05-25 16:20:20 +0100392 if (fread(buf, 1, st.st_size, fp) != st.st_size)
393 log_errx("Failed to read %s", filename);
394 bufend = buf + st.st_size;
395 fclose(fp);
396
397 if (st.st_size < sizeof(fip_toc_header_t))
398 log_errx("FIP %s is truncated", filename);
399
400 toc_header = (fip_toc_header_t *)buf;
401 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
402
403 if (toc_header->name != TOC_HEADER_NAME)
404 log_errx("%s is not a FIP file", filename);
405
406 /* Return the ToC header if the caller wants it. */
407 if (toc_header_out != NULL)
408 *toc_header_out = *toc_header;
409
410 /* Walk through each ToC entry in the file. */
411 while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
dp-arm516dfcb2016-11-03 13:59:26 +0000412 image_t *image;
413 image_desc_t *desc;
414
dp-arm4972ec52016-05-25 16:20:20 +0100415 /* Found the ToC terminator, we are done. */
416 if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
417 terminated = 1;
418 break;
419 }
420
421 /*
422 * Build a new image out of the ToC entry and add it to the
423 * table of images.
424 */
Masahiro Yamadad224b452017-01-14 23:22:02 +0900425 image = xzalloc(sizeof(*image),
dp-armdb0f5e92016-11-04 10:56:25 +0000426 "failed to allocate memory for image");
dp-arm4972ec52016-05-25 16:20:20 +0100427 memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000428 image->buffer = xmalloc(toc_entry->size,
429 "failed to allocate image buffer, is FIP file corrupted?");
dp-arm4972ec52016-05-25 16:20:20 +0100430 /* Overflow checks before memory copy. */
431 if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
432 log_errx("FIP %s is corrupted", filename);
433 if (toc_entry->size + toc_entry->offset_address > st.st_size)
434 log_errx("FIP %s is corrupted", filename);
435
436 memcpy(image->buffer, buf + toc_entry->offset_address,
437 toc_entry->size);
438 image->size = toc_entry->size;
439
dp-arm516dfcb2016-11-03 13:59:26 +0000440 /* If this is an unknown image, create a descriptor for it. */
441 desc = lookup_image_desc_from_uuid(&image->uuid);
442 if (desc == NULL) {
443 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
444
445 uuid_to_str(name, sizeof(name), &image->uuid);
446 snprintf(filename, sizeof(filename), "%s%s",
447 name, ".bin");
448 desc = new_image_desc(&image->uuid, name, "blob");
449 desc->action = DO_UNPACK;
450 desc->action_arg = xstrdup(filename,
451 "failed to allocate memory for blob filename");
452 add_image_desc(desc);
453 }
454
dp-arm4972ec52016-05-25 16:20:20 +0100455 add_image(image);
456
457 toc_entry++;
458 }
459
460 if (terminated == 0)
461 log_errx("FIP %s does not have a ToC terminator entry",
462 filename);
463 free(buf);
464 return 0;
465}
466
dp-armc1f8e772016-11-04 10:52:25 +0000467static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100468{
469 struct stat st;
470 image_t *image;
471 FILE *fp;
472
dp-arm715ef422016-08-30 14:18:58 +0100473 assert(uuid != NULL);
474
dp-arm4972ec52016-05-25 16:20:20 +0100475 fp = fopen(filename, "r");
476 if (fp == NULL)
477 log_err("fopen %s", filename);
478
479 if (fstat(fileno(fp), &st) == -1)
480 log_errx("fstat %s", filename);
481
Masahiro Yamadad224b452017-01-14 23:22:02 +0900482 image = xzalloc(sizeof(*image), "failed to allocate memory for image");
dp-arm715ef422016-08-30 14:18:58 +0100483 memcpy(&image->uuid, uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000484 image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
dp-arm4972ec52016-05-25 16:20:20 +0100485 if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
486 log_errx("Failed to read %s", filename);
487 image->size = st.st_size;
dp-arm4972ec52016-05-25 16:20:20 +0100488
489 fclose(fp);
490 return image;
491}
492
dp-armc1f8e772016-11-04 10:52:25 +0000493static int write_image_to_file(const image_t *image, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100494{
495 FILE *fp;
496
497 fp = fopen(filename, "w");
498 if (fp == NULL)
499 log_err("fopen");
500 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
501 log_errx("Failed to write %s", filename);
502 fclose(fp);
503 return 0;
504}
505
dp-arm90d2f0e2016-11-14 15:54:32 +0000506static struct option *add_opt(struct option *opts, size_t *nr_opts,
507 const char *name, int has_arg, int val)
dp-arm4972ec52016-05-25 16:20:20 +0100508{
dp-arm90d2f0e2016-11-14 15:54:32 +0000509 opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
510 if (opts == NULL)
511 log_err("realloc");
512 opts[*nr_opts].name = name;
513 opts[*nr_opts].has_arg = has_arg;
514 opts[*nr_opts].flag = NULL;
515 opts[*nr_opts].val = val;
516 ++*nr_opts;
517 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100518}
519
dp-arm90d2f0e2016-11-14 15:54:32 +0000520static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
521 int has_arg)
dp-arm4972ec52016-05-25 16:20:20 +0100522{
dp-arm90d2f0e2016-11-14 15:54:32 +0000523 image_desc_t *desc;
524
525 for (desc = image_desc_head; desc != NULL; desc = desc->next)
526 opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
527 OPT_TOC_ENTRY);
528 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100529}
530
dp-arm90d2f0e2016-11-14 15:54:32 +0000531static void md_print(const unsigned char *md, size_t len)
dp-arm12e893b2016-08-24 13:21:08 +0100532{
533 size_t i;
534
535 for (i = 0; i < len; i++)
536 printf("%02x", md[i]);
537}
538
dp-arm4972ec52016-05-25 16:20:20 +0100539static int info_cmd(int argc, char *argv[])
540{
541 image_t *image;
542 uint64_t image_offset;
dp-arm516dfcb2016-11-03 13:59:26 +0000543 uint64_t image_size;
dp-arm4972ec52016-05-25 16:20:20 +0100544 fip_toc_header_t toc_header;
dp-arm4972ec52016-05-25 16:20:20 +0100545
546 if (argc != 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100547 info_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100548 argc--, argv++;
549
550 parse_fip(argv[0], &toc_header);
551
552 if (verbose) {
553 log_dbgx("toc_header[name]: 0x%llX",
554 (unsigned long long)toc_header.name);
555 log_dbgx("toc_header[serial_number]: 0x%llX",
556 (unsigned long long)toc_header.serial_number);
557 log_dbgx("toc_header[flags]: 0x%llX",
558 (unsigned long long)toc_header.flags);
559 }
560
561 image_offset = sizeof(fip_toc_header_t) +
562 (sizeof(fip_toc_entry_t) * (nr_images + 1));
563
dp-arm90d2f0e2016-11-14 15:54:32 +0000564 for (image = image_head; image != NULL; image = image->next) {
565 image_desc_t *desc;
dp-arm715ef422016-08-30 14:18:58 +0100566
dp-arm90d2f0e2016-11-14 15:54:32 +0000567 desc = lookup_image_desc_from_uuid(&image->uuid);
dp-arm516dfcb2016-11-03 13:59:26 +0000568 assert(desc != NULL);
569 printf("%s: ", desc->name);
dp-arm4972ec52016-05-25 16:20:20 +0100570 image_size = image->size;
Masahiro Yamadaeee39312017-01-15 23:20:00 +0900571 printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
572 (unsigned long long)image_offset,
573 (unsigned long long)image_size,
574 desc->cmdline_name);
dp-arm12e893b2016-08-24 13:21:08 +0100575 if (verbose) {
576 unsigned char md[SHA256_DIGEST_LENGTH];
577
578 SHA256(image->buffer, image_size, md);
579 printf(", sha256=");
580 md_print(md, sizeof(md));
581 }
582 putchar('\n');
dp-arm4972ec52016-05-25 16:20:20 +0100583 image_offset += image_size;
584 }
585
586 free_images();
587 return 0;
588}
589
590static void info_usage(void)
591{
592 printf("fiptool info FIP_FILENAME\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100593 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100594}
595
dp-arm90d2f0e2016-11-14 15:54:32 +0000596static int pack_images(const char *filename, uint64_t toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100597{
598 FILE *fp;
599 image_t *image;
600 fip_toc_header_t *toc_header;
601 fip_toc_entry_t *toc_entry;
602 char *buf;
603 uint64_t entry_offset, buf_size, payload_size;
dp-arm4972ec52016-05-25 16:20:20 +0100604
605 /* Calculate total payload size and allocate scratch buffer. */
606 payload_size = 0;
dp-arm90d2f0e2016-11-14 15:54:32 +0000607 for (image = image_head; image != NULL; image = image->next)
608 payload_size += image->size;
dp-arm4972ec52016-05-25 16:20:20 +0100609
610 buf_size = sizeof(fip_toc_header_t) +
611 sizeof(fip_toc_entry_t) * (nr_images + 1);
612 buf = calloc(1, buf_size);
613 if (buf == NULL)
614 log_err("calloc");
615
616 /* Build up header and ToC entries from the image table. */
617 toc_header = (fip_toc_header_t *)buf;
618 toc_header->name = TOC_HEADER_NAME;
619 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
620 toc_header->flags = toc_flags;
621
622 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
623
624 entry_offset = buf_size;
dp-arm90d2f0e2016-11-14 15:54:32 +0000625 for (image = image_head; image != NULL; image = image->next) {
dp-arm4972ec52016-05-25 16:20:20 +0100626 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
627 toc_entry->offset_address = entry_offset;
628 toc_entry->size = image->size;
629 toc_entry->flags = 0;
630 entry_offset += toc_entry->size;
631 toc_entry++;
632 }
633
634 /* Append a null uuid entry to mark the end of ToC entries. */
635 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
636 toc_entry->offset_address = entry_offset;
637 toc_entry->size = 0;
638 toc_entry->flags = 0;
639
640 /* Generate the FIP file. */
641 fp = fopen(filename, "w");
642 if (fp == NULL)
643 log_err("fopen %s", filename);
644
645 if (verbose)
646 log_dbgx("Metadata size: %zu bytes", buf_size);
647
648 if (fwrite(buf, 1, buf_size, fp) != buf_size)
649 log_errx("Failed to write image to %s", filename);
650 free(buf);
651
652 if (verbose)
653 log_dbgx("Payload size: %zu bytes", payload_size);
654
dp-arm90d2f0e2016-11-14 15:54:32 +0000655 for (image = image_head; image != NULL; image = image->next)
dp-arm4972ec52016-05-25 16:20:20 +0100656 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
657 log_errx("Failed to write image to %s", filename);
dp-arm4972ec52016-05-25 16:20:20 +0100658
659 fclose(fp);
660 return 0;
661}
662
663/*
664 * This function is shared between the create and update subcommands.
665 * The difference between the two subcommands is that when the FIP file
666 * is created, the parsing of an existing FIP is skipped. This results
667 * in update_fip() creating the new FIP file from scratch because the
668 * internal image table is not populated.
669 */
670static void update_fip(void)
671{
dp-arm90d2f0e2016-11-14 15:54:32 +0000672 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100673
674 /* Add or replace images in the FIP file. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000675 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +0000676 image_t *new_image, *old_image;
677
dp-arm90d2f0e2016-11-14 15:54:32 +0000678 if (desc->action != DO_PACK)
dp-arm4972ec52016-05-25 16:20:20 +0100679 continue;
680
dp-arm90d2f0e2016-11-14 15:54:32 +0000681 new_image = read_image_from_file(&desc->uuid,
682 desc->action_arg);
683 old_image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100684 if (old_image != NULL) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000685 if (verbose) {
dp-arm516dfcb2016-11-03 13:59:26 +0000686 log_dbgx("Replacing %s with %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000687 desc->cmdline_name,
688 desc->action_arg);
689 }
Masahiro Yamadad65e7fd2017-01-27 11:57:54 +0900690 replace_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100691 } else {
692 if (verbose)
693 log_dbgx("Adding image %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000694 desc->action_arg);
dp-arm715ef422016-08-30 14:18:58 +0100695 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100696 }
dp-arm4972ec52016-05-25 16:20:20 +0100697 }
698}
699
dp-arm90d2f0e2016-11-14 15:54:32 +0000700static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100701{
702 unsigned long long flags;
703 char *endptr;
704
705 errno = 0;
706 flags = strtoull(arg, &endptr, 16);
707 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
708 log_errx("Invalid platform ToC flags: %s", arg);
709 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
710 *toc_flags |= flags << 32;
711}
712
dp-arm516dfcb2016-11-03 13:59:26 +0000713static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
714{
715 char *p;
716
717 for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
718 if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
719 p += strlen("uuid=");
720 uuid_from_str(uuid, p);
721 } else if (strncmp(p, "file=", strlen("file=")) == 0) {
722 p += strlen("file=");
723 snprintf(filename, len, "%s", p);
724 }
725 }
726}
727
dp-arm4972ec52016-05-25 16:20:20 +0100728static int create_cmd(int argc, char *argv[])
729{
dp-arm90d2f0e2016-11-14 15:54:32 +0000730 struct option *opts = NULL;
731 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100732 unsigned long long toc_flags = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100733
734 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100735 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100736
dp-arm90d2f0e2016-11-14 15:54:32 +0000737 opts = fill_common_opts(opts, &nr_opts, required_argument);
738 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100739 OPT_PLAT_TOC_FLAGS);
dp-arm516dfcb2016-11-03 13:59:26 +0000740 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000741 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100742
743 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000744 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100745
dp-arm516dfcb2016-11-03 13:59:26 +0000746 c = getopt_long(argc, argv, "b:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100747 if (c == -1)
748 break;
749
750 switch (c) {
751 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000752 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100753
dp-arm90d2f0e2016-11-14 15:54:32 +0000754 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000755 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100756 break;
757 }
758 case OPT_PLAT_TOC_FLAGS:
759 parse_plat_toc_flags(optarg, &toc_flags);
760 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000761 case 'b': {
762 char name[_UUID_STR_LEN + 1];
763 char filename[PATH_MAX] = { 0 };
764 uuid_t uuid = { 0 };
765 image_desc_t *desc;
766
767 parse_blob_opt(optarg, &uuid,
768 filename, sizeof(filename));
769
770 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
771 filename[0] == '\0')
772 create_usage();
773
774 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000775 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000776 uuid_to_str(name, sizeof(name), &uuid);
777 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000778 add_image_desc(desc);
779 }
dp-armfb732312016-12-30 09:55:48 +0000780 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000781 break;
782 }
dp-arm4972ec52016-05-25 16:20:20 +0100783 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100784 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100785 }
786 }
787 argc -= optind;
788 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000789 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100790
791 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100792 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100793
794 update_fip();
795
796 pack_images(argv[0], toc_flags);
797 free_images();
798 return 0;
799}
800
801static void create_usage(void)
802{
803 toc_entry_t *toc_entry = toc_entries;
804
Masahiro Yamada7abd0a22017-01-14 11:04:36 +0900805 printf("fiptool create [opts] FIP_FILENAME\n");
806 printf("\n");
807 printf("Options:\n");
dp-arm516dfcb2016-11-03 13:59:26 +0000808 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID "
809 "pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100810 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
811 "occupying bits 32-47 in 64-bit ToC header.\n");
812 fputc('\n', stderr);
813 printf("Specific images are packed with the following options:\n");
814 for (; toc_entry->cmdline_name != NULL; toc_entry++)
815 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
816 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100817 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100818}
819
820static int update_cmd(int argc, char *argv[])
821{
dp-arm90d2f0e2016-11-14 15:54:32 +0000822 struct option *opts = NULL;
823 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000824 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +0100825 fip_toc_header_t toc_header = { 0 };
826 unsigned long long toc_flags = 0;
827 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100828
829 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100830 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100831
dp-arm90d2f0e2016-11-14 15:54:32 +0000832 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000833 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000834 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
835 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100836 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000837 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100838
839 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000840 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100841
dp-arm516dfcb2016-11-03 13:59:26 +0000842 c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100843 if (c == -1)
844 break;
845
846 switch (c) {
847 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000848 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100849
dp-arm90d2f0e2016-11-14 15:54:32 +0000850 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000851 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100852 break;
853 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000854 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100855 parse_plat_toc_flags(optarg, &toc_flags);
856 pflag = 1;
857 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000858 case 'b': {
859 char name[_UUID_STR_LEN + 1];
860 char filename[PATH_MAX] = { 0 };
861 uuid_t uuid = { 0 };
862 image_desc_t *desc;
863
864 parse_blob_opt(optarg, &uuid,
865 filename, sizeof(filename));
866
867 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
868 filename[0] == '\0')
869 update_usage();
870
871 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000872 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000873 uuid_to_str(name, sizeof(name), &uuid);
874 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000875 add_image_desc(desc);
876 }
dp-armfb732312016-12-30 09:55:48 +0000877 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000878 break;
879 }
dp-arm4972ec52016-05-25 16:20:20 +0100880 case 'o':
881 snprintf(outfile, sizeof(outfile), "%s", optarg);
882 break;
883 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100884 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100885 }
886 }
887 argc -= optind;
888 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000889 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100890
891 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100892 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100893
894 if (outfile[0] == '\0')
895 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
896
Masahiro Yamadaebb6e372016-12-25 12:41:41 +0900897 if (access(argv[0], F_OK) == 0)
dp-arm4972ec52016-05-25 16:20:20 +0100898 parse_fip(argv[0], &toc_header);
899
900 if (pflag)
901 toc_header.flags &= ~(0xffffULL << 32);
902 toc_flags = (toc_header.flags |= toc_flags);
903
904 update_fip();
905
906 pack_images(outfile, toc_flags);
907 free_images();
908 return 0;
909}
910
911static void update_usage(void)
912{
913 toc_entry_t *toc_entry = toc_entries;
914
Masahiro Yamada7abd0a22017-01-14 11:04:36 +0900915 printf("fiptool update [opts] FIP_FILENAME\n");
916 printf("\n");
917 printf("Options:\n");
dp-arm516dfcb2016-11-03 13:59:26 +0000918 printf(" --blob uuid=...,file=...\tAdd or update an image "
919 "with the given UUID pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100920 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
921 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
922 "occupying bits 32-47 in 64-bit ToC header.\n");
923 fputc('\n', stderr);
924 printf("Specific images are packed with the following options:\n");
925 for (; toc_entry->cmdline_name != NULL; toc_entry++)
926 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
927 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100928 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100929}
930
931static int unpack_cmd(int argc, char *argv[])
932{
dp-arm90d2f0e2016-11-14 15:54:32 +0000933 struct option *opts = NULL;
934 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000935 char outdir[PATH_MAX] = { 0 };
dp-arm90d2f0e2016-11-14 15:54:32 +0000936 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100937 int fflag = 0;
938 int unpack_all = 1;
dp-arm4972ec52016-05-25 16:20:20 +0100939
940 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100941 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100942
dp-arm90d2f0e2016-11-14 15:54:32 +0000943 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000944 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000945 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
946 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
947 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100948
949 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000950 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100951
dp-arm516dfcb2016-11-03 13:59:26 +0000952 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100953 if (c == -1)
954 break;
955
956 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000957 case OPT_TOC_ENTRY: {
958 image_desc_t *desc;
959
960 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000961 set_image_desc_action(desc, DO_UNPACK, optarg);
dp-arm90d2f0e2016-11-14 15:54:32 +0000962 unpack_all = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100963 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000964 }
dp-arm516dfcb2016-11-03 13:59:26 +0000965 case 'b': {
966 char name[_UUID_STR_LEN + 1];
967 char filename[PATH_MAX] = { 0 };
968 uuid_t uuid = { 0 };
969 image_desc_t *desc;
970
971 parse_blob_opt(optarg, &uuid,
972 filename, sizeof(filename));
973
974 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
975 filename[0] == '\0')
976 unpack_usage();
977
978 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000979 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000980 uuid_to_str(name, sizeof(name), &uuid);
981 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000982 add_image_desc(desc);
983 }
dp-armfb732312016-12-30 09:55:48 +0000984 set_image_desc_action(desc, DO_UNPACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000985 unpack_all = 0;
986 break;
987 }
dp-arm4972ec52016-05-25 16:20:20 +0100988 case 'f':
989 fflag = 1;
990 break;
991 case 'o':
992 snprintf(outdir, sizeof(outdir), "%s", optarg);
993 break;
994 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100995 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100996 }
997 }
998 argc -= optind;
999 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001000 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001001
1002 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001003 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001004
1005 parse_fip(argv[0], NULL);
1006
1007 if (outdir[0] != '\0')
1008 if (chdir(outdir) == -1)
1009 log_err("chdir %s", outdir);
1010
dp-arm4972ec52016-05-25 16:20:20 +01001011 /* Unpack all specified images. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001012 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +00001013 char file[PATH_MAX];
dp-arm715ef422016-08-30 14:18:58 +01001014 image_t *image;
1015
dp-arm90d2f0e2016-11-14 15:54:32 +00001016 if (!unpack_all && desc->action != DO_UNPACK)
dp-arm4972ec52016-05-25 16:20:20 +01001017 continue;
1018
1019 /* Build filename. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001020 if (desc->action_arg == NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001021 snprintf(file, sizeof(file), "%s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +00001022 desc->cmdline_name);
dp-arm4972ec52016-05-25 16:20:20 +01001023 else
1024 snprintf(file, sizeof(file), "%s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001025 desc->action_arg);
dp-arm4972ec52016-05-25 16:20:20 +01001026
dp-arm90d2f0e2016-11-14 15:54:32 +00001027 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001028 if (image == NULL) {
1029 if (!unpack_all)
dp-arm516dfcb2016-11-03 13:59:26 +00001030 log_warnx("%s does not exist in %s",
dp-arm715ef422016-08-30 14:18:58 +01001031 file, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001032 continue;
1033 }
1034
1035 if (access(file, F_OK) != 0 || fflag) {
1036 if (verbose)
1037 log_dbgx("Unpacking %s", file);
dp-arm715ef422016-08-30 14:18:58 +01001038 write_image_to_file(image, file);
dp-arm4972ec52016-05-25 16:20:20 +01001039 } else {
1040 log_warnx("File %s already exists, use --force to overwrite it",
1041 file);
1042 }
dp-arm4972ec52016-05-25 16:20:20 +01001043 }
1044
1045 free_images();
1046 return 0;
1047}
1048
1049static void unpack_usage(void)
1050{
1051 toc_entry_t *toc_entry = toc_entries;
1052
Masahiro Yamada7abd0a22017-01-14 11:04:36 +09001053 printf("fiptool unpack [opts] FIP_FILENAME\n");
1054 printf("\n");
1055 printf("Options:\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001056 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID "
1057 "to file.\n");
1058 printf(" --force\t\t\tIf the output file already exists, use --force to "
dp-arm4972ec52016-05-25 16:20:20 +01001059 "overwrite it.\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001060 printf(" --out path\t\t\tSet the output directory path.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001061 fputc('\n', stderr);
1062 printf("Specific images are unpacked with the following options:\n");
1063 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1064 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1065 toc_entry->name);
1066 fputc('\n', stderr);
1067 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001068 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001069}
1070
1071static int remove_cmd(int argc, char *argv[])
1072{
dp-arm90d2f0e2016-11-14 15:54:32 +00001073 struct option *opts = NULL;
1074 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +00001075 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +01001076 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +00001077 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +01001078 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001079
1080 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +01001081 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001082
dp-arm90d2f0e2016-11-14 15:54:32 +00001083 opts = fill_common_opts(opts, &nr_opts, no_argument);
dp-arm516dfcb2016-11-03 13:59:26 +00001084 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +00001085 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1086 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1087 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +01001088
1089 while (1) {
dp-armfe92b892016-11-07 11:13:54 +00001090 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001091
dp-arm516dfcb2016-11-03 13:59:26 +00001092 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +01001093 if (c == -1)
1094 break;
1095
1096 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +00001097 case OPT_TOC_ENTRY: {
1098 image_desc_t *desc;
1099
1100 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +00001101 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm4972ec52016-05-25 16:20:20 +01001102 break;
dp-arm90d2f0e2016-11-14 15:54:32 +00001103 }
dp-arm516dfcb2016-11-03 13:59:26 +00001104 case 'b': {
1105 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1106 uuid_t uuid = { 0 };
1107 image_desc_t *desc;
1108
1109 parse_blob_opt(optarg, &uuid,
1110 filename, sizeof(filename));
1111
1112 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1113 remove_usage();
1114
1115 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +00001116 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +00001117 uuid_to_str(name, sizeof(name), &uuid);
1118 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +00001119 add_image_desc(desc);
1120 }
dp-armfb732312016-12-30 09:55:48 +00001121 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm516dfcb2016-11-03 13:59:26 +00001122 break;
1123 }
dp-arm4972ec52016-05-25 16:20:20 +01001124 case 'f':
1125 fflag = 1;
1126 break;
1127 case 'o':
1128 snprintf(outfile, sizeof(outfile), "%s", optarg);
1129 break;
1130 default:
dp-arm29f1b5c2016-09-15 09:58:50 +01001131 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001132 }
1133 }
1134 argc -= optind;
1135 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001136 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001137
1138 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001139 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001140
1141 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1142 log_errx("File %s already exists, use --force to overwrite it",
1143 outfile);
1144
1145 if (outfile[0] == '\0')
1146 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1147
1148 parse_fip(argv[0], &toc_header);
1149
dp-arm90d2f0e2016-11-14 15:54:32 +00001150 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +01001151 image_t *image;
1152
dp-arm90d2f0e2016-11-14 15:54:32 +00001153 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +01001154 continue;
dp-arm516dfcb2016-11-03 13:59:26 +00001155
dp-arm90d2f0e2016-11-14 15:54:32 +00001156 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001157 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +01001158 if (verbose)
dp-arm516dfcb2016-11-03 13:59:26 +00001159 log_dbgx("Removing %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001160 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +01001161 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +01001162 } else {
dp-arm516dfcb2016-11-03 13:59:26 +00001163 log_warnx("%s does not exist in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001164 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001165 }
1166 }
1167
1168 pack_images(outfile, toc_header.flags);
1169 free_images();
1170 return 0;
1171}
1172
1173static void remove_usage(void)
1174{
1175 toc_entry_t *toc_entry = toc_entries;
1176
Masahiro Yamada7abd0a22017-01-14 11:04:36 +09001177 printf("fiptool remove [opts] FIP_FILENAME\n");
1178 printf("\n");
1179 printf("Options:\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001180 printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001181 printf(" --force\t\tIf the output FIP file already exists, use --force to "
1182 "overwrite it.\n");
1183 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1184 fputc('\n', stderr);
1185 printf("Specific images are removed with the following options:\n");
1186 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1187 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
1188 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +01001189 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001190}
1191
1192static int version_cmd(int argc, char *argv[])
1193{
1194#ifdef VERSION
1195 puts(VERSION);
1196#else
1197 /* If built from fiptool directory, VERSION is not set. */
1198 puts("Unknown version");
1199#endif
1200 return 0;
1201}
1202
1203static void version_usage(void)
1204{
1205 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001206 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001207}
1208
1209static int help_cmd(int argc, char *argv[])
1210{
1211 int i;
1212
1213 if (argc < 2)
1214 usage();
1215 argc--, argv++;
1216
1217 for (i = 0; i < NELEM(cmds); i++) {
1218 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001219 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001220 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001221 }
1222 if (i == NELEM(cmds))
1223 printf("No help for subcommand '%s'\n", argv[0]);
1224 return 0;
1225}
1226
1227static void usage(void)
1228{
Masahiro Yamada252c3362017-01-13 02:13:06 +09001229 printf("usage: fiptool [--verbose] <command> [<args>]\n");
dp-arm4972ec52016-05-25 16:20:20 +01001230 printf("Global options supported:\n");
1231 printf(" --verbose\tEnable verbose output for all commands.\n");
1232 fputc('\n', stderr);
1233 printf("Commands supported:\n");
1234 printf(" info\t\tList images contained in FIP.\n");
1235 printf(" create\tCreate a new FIP with the given images.\n");
1236 printf(" update\tUpdate an existing FIP with the given images.\n");
1237 printf(" unpack\tUnpack images from FIP.\n");
1238 printf(" remove\tRemove images from FIP.\n");
1239 printf(" version\tShow fiptool version.\n");
1240 printf(" help\t\tShow help for given command.\n");
1241 exit(1);
1242}
1243
1244int main(int argc, char *argv[])
1245{
1246 int i, ret = 0;
1247
dp-arm5cd10ae2016-11-07 10:45:59 +00001248 while (1) {
1249 int c, opt_index = 0;
1250 static struct option opts[] = {
1251 { "verbose", no_argument, NULL, 'v' },
1252 { NULL, no_argument, NULL, 0 }
1253 };
1254
1255 /*
1256 * Set POSIX mode so getopt stops at the first non-option
1257 * which is the subcommand.
1258 */
1259 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1260 if (c == -1)
1261 break;
dp-arm4972ec52016-05-25 16:20:20 +01001262
dp-arm5cd10ae2016-11-07 10:45:59 +00001263 switch (c) {
1264 case 'v':
1265 verbose = 1;
1266 break;
1267 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001268 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001269 }
dp-arm4972ec52016-05-25 16:20:20 +01001270 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001271 argc -= optind;
1272 argv += optind;
1273 /* Reset optind for subsequent getopt processing. */
1274 optind = 0;
1275
1276 if (argc == 0)
1277 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001278
dp-arm90d2f0e2016-11-14 15:54:32 +00001279 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001280 for (i = 0; i < NELEM(cmds); i++) {
1281 if (strcmp(cmds[i].name, argv[0]) == 0) {
1282 ret = cmds[i].handler(argc, argv);
1283 break;
1284 }
1285 }
1286 if (i == NELEM(cmds))
1287 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001288 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001289 return ret;
1290}