blob: 7a5c2cd72321db9d8ac80c31051c923ee74ccb5c [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{
dp-arm90d2f0e2016-11-14 15:54:32 +0000279 image_t *tmp = image_head, *prev;
dp-arm4972ec52016-05-25 16:20:20 +0100280
dp-arm90d2f0e2016-11-14 15:54:32 +0000281 if (tmp == image) {
282 image_head = tmp->next;
283 free_image(tmp);
284 } else {
285 while (tmp != NULL && tmp != image) {
286 prev = tmp;
287 tmp = tmp->next;
dp-arm4972ec52016-05-25 16:20:20 +0100288 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000289 assert(tmp != NULL);
290 prev->next = tmp->next;
291 free_image(tmp);
dp-arm4972ec52016-05-25 16:20:20 +0100292 }
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
dp-arm516dfcb2016-11-03 13:59:26 +0000805 printf("fiptool create [--blob uuid=...,file=...] "
806 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
807 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID "
808 "pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100809 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
810 "occupying bits 32-47 in 64-bit ToC header.\n");
811 fputc('\n', stderr);
812 printf("Specific images are packed with the following options:\n");
813 for (; toc_entry->cmdline_name != NULL; toc_entry++)
814 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
815 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100816 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100817}
818
819static int update_cmd(int argc, char *argv[])
820{
dp-arm90d2f0e2016-11-14 15:54:32 +0000821 struct option *opts = NULL;
822 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000823 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +0100824 fip_toc_header_t toc_header = { 0 };
825 unsigned long long toc_flags = 0;
826 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100827
828 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100829 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100830
dp-arm90d2f0e2016-11-14 15:54:32 +0000831 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000832 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000833 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
834 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100835 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000836 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100837
838 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000839 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100840
dp-arm516dfcb2016-11-03 13:59:26 +0000841 c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100842 if (c == -1)
843 break;
844
845 switch (c) {
846 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000847 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100848
dp-arm90d2f0e2016-11-14 15:54:32 +0000849 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000850 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100851 break;
852 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000853 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100854 parse_plat_toc_flags(optarg, &toc_flags);
855 pflag = 1;
856 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000857 case 'b': {
858 char name[_UUID_STR_LEN + 1];
859 char filename[PATH_MAX] = { 0 };
860 uuid_t uuid = { 0 };
861 image_desc_t *desc;
862
863 parse_blob_opt(optarg, &uuid,
864 filename, sizeof(filename));
865
866 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
867 filename[0] == '\0')
868 update_usage();
869
870 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000871 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000872 uuid_to_str(name, sizeof(name), &uuid);
873 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000874 add_image_desc(desc);
875 }
dp-armfb732312016-12-30 09:55:48 +0000876 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000877 break;
878 }
dp-arm4972ec52016-05-25 16:20:20 +0100879 case 'o':
880 snprintf(outfile, sizeof(outfile), "%s", optarg);
881 break;
882 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100883 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100884 }
885 }
886 argc -= optind;
887 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000888 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100889
890 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100891 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100892
893 if (outfile[0] == '\0')
894 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
895
Masahiro Yamadaebb6e372016-12-25 12:41:41 +0900896 if (access(argv[0], F_OK) == 0)
dp-arm4972ec52016-05-25 16:20:20 +0100897 parse_fip(argv[0], &toc_header);
898
899 if (pflag)
900 toc_header.flags &= ~(0xffffULL << 32);
901 toc_flags = (toc_header.flags |= toc_flags);
902
903 update_fip();
904
905 pack_images(outfile, toc_flags);
906 free_images();
907 return 0;
908}
909
910static void update_usage(void)
911{
912 toc_entry_t *toc_entry = toc_entries;
913
dp-arm516dfcb2016-11-03 13:59:26 +0000914 printf("fiptool update [--blob uuid=...,file=...] [--out FIP_FILENAME] "
dp-arm4972ec52016-05-25 16:20:20 +0100915 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
dp-arm516dfcb2016-11-03 13:59:26 +0000916 printf(" --blob uuid=...,file=...\tAdd or update an image "
917 "with the given UUID pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100918 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
919 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
920 "occupying bits 32-47 in 64-bit ToC header.\n");
921 fputc('\n', stderr);
922 printf("Specific images are packed with the following options:\n");
923 for (; toc_entry->cmdline_name != NULL; toc_entry++)
924 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
925 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100926 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100927}
928
929static int unpack_cmd(int argc, char *argv[])
930{
dp-arm90d2f0e2016-11-14 15:54:32 +0000931 struct option *opts = NULL;
932 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000933 char outdir[PATH_MAX] = { 0 };
dp-arm90d2f0e2016-11-14 15:54:32 +0000934 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100935 int fflag = 0;
936 int unpack_all = 1;
dp-arm4972ec52016-05-25 16:20:20 +0100937
938 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100939 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100940
dp-arm90d2f0e2016-11-14 15:54:32 +0000941 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000942 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000943 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
944 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
945 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100946
947 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000948 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100949
dp-arm516dfcb2016-11-03 13:59:26 +0000950 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100951 if (c == -1)
952 break;
953
954 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000955 case OPT_TOC_ENTRY: {
956 image_desc_t *desc;
957
958 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000959 set_image_desc_action(desc, DO_UNPACK, optarg);
dp-arm90d2f0e2016-11-14 15:54:32 +0000960 unpack_all = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100961 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000962 }
dp-arm516dfcb2016-11-03 13:59:26 +0000963 case 'b': {
964 char name[_UUID_STR_LEN + 1];
965 char filename[PATH_MAX] = { 0 };
966 uuid_t uuid = { 0 };
967 image_desc_t *desc;
968
969 parse_blob_opt(optarg, &uuid,
970 filename, sizeof(filename));
971
972 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
973 filename[0] == '\0')
974 unpack_usage();
975
976 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000977 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000978 uuid_to_str(name, sizeof(name), &uuid);
979 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000980 add_image_desc(desc);
981 }
dp-armfb732312016-12-30 09:55:48 +0000982 set_image_desc_action(desc, DO_UNPACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000983 unpack_all = 0;
984 break;
985 }
dp-arm4972ec52016-05-25 16:20:20 +0100986 case 'f':
987 fflag = 1;
988 break;
989 case 'o':
990 snprintf(outdir, sizeof(outdir), "%s", optarg);
991 break;
992 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100993 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100994 }
995 }
996 argc -= optind;
997 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000998 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100999
1000 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001001 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001002
1003 parse_fip(argv[0], NULL);
1004
1005 if (outdir[0] != '\0')
1006 if (chdir(outdir) == -1)
1007 log_err("chdir %s", outdir);
1008
dp-arm4972ec52016-05-25 16:20:20 +01001009 /* Unpack all specified images. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001010 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +00001011 char file[PATH_MAX];
dp-arm715ef422016-08-30 14:18:58 +01001012 image_t *image;
1013
dp-arm90d2f0e2016-11-14 15:54:32 +00001014 if (!unpack_all && desc->action != DO_UNPACK)
dp-arm4972ec52016-05-25 16:20:20 +01001015 continue;
1016
1017 /* Build filename. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001018 if (desc->action_arg == NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001019 snprintf(file, sizeof(file), "%s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +00001020 desc->cmdline_name);
dp-arm4972ec52016-05-25 16:20:20 +01001021 else
1022 snprintf(file, sizeof(file), "%s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001023 desc->action_arg);
dp-arm4972ec52016-05-25 16:20:20 +01001024
dp-arm90d2f0e2016-11-14 15:54:32 +00001025 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001026 if (image == NULL) {
1027 if (!unpack_all)
dp-arm516dfcb2016-11-03 13:59:26 +00001028 log_warnx("%s does not exist in %s",
dp-arm715ef422016-08-30 14:18:58 +01001029 file, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001030 continue;
1031 }
1032
1033 if (access(file, F_OK) != 0 || fflag) {
1034 if (verbose)
1035 log_dbgx("Unpacking %s", file);
dp-arm715ef422016-08-30 14:18:58 +01001036 write_image_to_file(image, file);
dp-arm4972ec52016-05-25 16:20:20 +01001037 } else {
1038 log_warnx("File %s already exists, use --force to overwrite it",
1039 file);
1040 }
dp-arm4972ec52016-05-25 16:20:20 +01001041 }
1042
1043 free_images();
1044 return 0;
1045}
1046
1047static void unpack_usage(void)
1048{
1049 toc_entry_t *toc_entry = toc_entries;
1050
dp-arm516dfcb2016-11-03 13:59:26 +00001051 printf("fiptool unpack [--blob uuid=...,file=...] [--force] "
1052 "[--out <path>] [opts] FIP_FILENAME\n");
1053 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID "
1054 "to file.\n");
1055 printf(" --force\t\t\tIf the output file already exists, use --force to "
dp-arm4972ec52016-05-25 16:20:20 +01001056 "overwrite it.\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001057 printf(" --out path\t\t\tSet the output directory path.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001058 fputc('\n', stderr);
1059 printf("Specific images are unpacked with the following options:\n");
1060 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1061 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1062 toc_entry->name);
1063 fputc('\n', stderr);
1064 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001065 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001066}
1067
1068static int remove_cmd(int argc, char *argv[])
1069{
dp-arm90d2f0e2016-11-14 15:54:32 +00001070 struct option *opts = NULL;
1071 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +00001072 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +01001073 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +00001074 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +01001075 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001076
1077 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +01001078 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001079
dp-arm90d2f0e2016-11-14 15:54:32 +00001080 opts = fill_common_opts(opts, &nr_opts, no_argument);
dp-arm516dfcb2016-11-03 13:59:26 +00001081 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +00001082 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1083 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1084 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +01001085
1086 while (1) {
dp-armfe92b892016-11-07 11:13:54 +00001087 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001088
dp-arm516dfcb2016-11-03 13:59:26 +00001089 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +01001090 if (c == -1)
1091 break;
1092
1093 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +00001094 case OPT_TOC_ENTRY: {
1095 image_desc_t *desc;
1096
1097 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +00001098 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm4972ec52016-05-25 16:20:20 +01001099 break;
dp-arm90d2f0e2016-11-14 15:54:32 +00001100 }
dp-arm516dfcb2016-11-03 13:59:26 +00001101 case 'b': {
1102 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1103 uuid_t uuid = { 0 };
1104 image_desc_t *desc;
1105
1106 parse_blob_opt(optarg, &uuid,
1107 filename, sizeof(filename));
1108
1109 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1110 remove_usage();
1111
1112 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +00001113 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +00001114 uuid_to_str(name, sizeof(name), &uuid);
1115 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +00001116 add_image_desc(desc);
1117 }
dp-armfb732312016-12-30 09:55:48 +00001118 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm516dfcb2016-11-03 13:59:26 +00001119 break;
1120 }
dp-arm4972ec52016-05-25 16:20:20 +01001121 case 'f':
1122 fflag = 1;
1123 break;
1124 case 'o':
1125 snprintf(outfile, sizeof(outfile), "%s", optarg);
1126 break;
1127 default:
dp-arm29f1b5c2016-09-15 09:58:50 +01001128 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001129 }
1130 }
1131 argc -= optind;
1132 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001133 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001134
1135 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001136 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001137
1138 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1139 log_errx("File %s already exists, use --force to overwrite it",
1140 outfile);
1141
1142 if (outfile[0] == '\0')
1143 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1144
1145 parse_fip(argv[0], &toc_header);
1146
dp-arm90d2f0e2016-11-14 15:54:32 +00001147 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +01001148 image_t *image;
1149
dp-arm90d2f0e2016-11-14 15:54:32 +00001150 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +01001151 continue;
dp-arm516dfcb2016-11-03 13:59:26 +00001152
dp-arm90d2f0e2016-11-14 15:54:32 +00001153 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001154 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +01001155 if (verbose)
dp-arm516dfcb2016-11-03 13:59:26 +00001156 log_dbgx("Removing %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001157 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +01001158 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +01001159 } else {
dp-arm516dfcb2016-11-03 13:59:26 +00001160 log_warnx("%s does not exist in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001161 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001162 }
1163 }
1164
1165 pack_images(outfile, toc_header.flags);
1166 free_images();
1167 return 0;
1168}
1169
1170static void remove_usage(void)
1171{
1172 toc_entry_t *toc_entry = toc_entries;
1173
dp-arm516dfcb2016-11-03 13:59:26 +00001174 printf("fiptool remove [--blob uuid=...] [--force] "
1175 "[--out FIP_FILENAME] [opts] FIP_FILENAME\n");
1176 printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001177 printf(" --force\t\tIf the output FIP file already exists, use --force to "
1178 "overwrite it.\n");
1179 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1180 fputc('\n', stderr);
1181 printf("Specific images are removed with the following options:\n");
1182 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1183 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
1184 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +01001185 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001186}
1187
1188static int version_cmd(int argc, char *argv[])
1189{
1190#ifdef VERSION
1191 puts(VERSION);
1192#else
1193 /* If built from fiptool directory, VERSION is not set. */
1194 puts("Unknown version");
1195#endif
1196 return 0;
1197}
1198
1199static void version_usage(void)
1200{
1201 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001202 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001203}
1204
1205static int help_cmd(int argc, char *argv[])
1206{
1207 int i;
1208
1209 if (argc < 2)
1210 usage();
1211 argc--, argv++;
1212
1213 for (i = 0; i < NELEM(cmds); i++) {
1214 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001215 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001216 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001217 }
1218 if (i == NELEM(cmds))
1219 printf("No help for subcommand '%s'\n", argv[0]);
1220 return 0;
1221}
1222
1223static void usage(void)
1224{
1225 printf("usage: [--verbose] fiptool <command> [<args>]\n");
1226 printf("Global options supported:\n");
1227 printf(" --verbose\tEnable verbose output for all commands.\n");
1228 fputc('\n', stderr);
1229 printf("Commands supported:\n");
1230 printf(" info\t\tList images contained in FIP.\n");
1231 printf(" create\tCreate a new FIP with the given images.\n");
1232 printf(" update\tUpdate an existing FIP with the given images.\n");
1233 printf(" unpack\tUnpack images from FIP.\n");
1234 printf(" remove\tRemove images from FIP.\n");
1235 printf(" version\tShow fiptool version.\n");
1236 printf(" help\t\tShow help for given command.\n");
1237 exit(1);
1238}
1239
1240int main(int argc, char *argv[])
1241{
1242 int i, ret = 0;
1243
dp-arm5cd10ae2016-11-07 10:45:59 +00001244 while (1) {
1245 int c, opt_index = 0;
1246 static struct option opts[] = {
1247 { "verbose", no_argument, NULL, 'v' },
1248 { NULL, no_argument, NULL, 0 }
1249 };
1250
1251 /*
1252 * Set POSIX mode so getopt stops at the first non-option
1253 * which is the subcommand.
1254 */
1255 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1256 if (c == -1)
1257 break;
dp-arm4972ec52016-05-25 16:20:20 +01001258
dp-arm5cd10ae2016-11-07 10:45:59 +00001259 switch (c) {
1260 case 'v':
1261 verbose = 1;
1262 break;
1263 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001264 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001265 }
dp-arm4972ec52016-05-25 16:20:20 +01001266 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001267 argc -= optind;
1268 argv += optind;
1269 /* Reset optind for subsequent getopt processing. */
1270 optind = 0;
1271
1272 if (argc == 0)
1273 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001274
dp-arm90d2f0e2016-11-14 15:54:32 +00001275 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001276 for (i = 0; i < NELEM(cmds); i++) {
1277 if (strcmp(cmds[i].name, argv[0]) == 0) {
1278 ret = cmds[i].handler(argc, argv);
1279 break;
1280 }
1281 }
1282 if (i == NELEM(cmds))
1283 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001284 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001285 return ret;
1286}