blob: b79c0d6979b064987fde14c88265d79a2a5de846 [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
dp-arm90d2f0e2016-11-14 15:54:32 +0000205 assert(lookup_image_desc_from_uuid(&desc->uuid) == NULL);
Masahiro Yamadad224b452017-01-14 23:22:02 +0900206
207 while (*p)
208 p = &(*p)->next;
209
210 *p = desc;
dp-arm90d2f0e2016-11-14 15:54:32 +0000211 nr_image_descs++;
212}
dp-arm4972ec52016-05-25 16:20:20 +0100213
dp-arm90d2f0e2016-11-14 15:54:32 +0000214static void free_image_descs(void)
215{
216 image_desc_t *desc = image_desc_head, *tmp;
217
218 while (desc != NULL) {
219 tmp = desc->next;
220 free_image_desc(desc);
221 desc = tmp;
222 nr_image_descs--;
dp-arm4972ec52016-05-25 16:20:20 +0100223 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000224 assert(nr_image_descs == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100225}
226
dp-arm90d2f0e2016-11-14 15:54:32 +0000227static void fill_image_descs(void)
228{
229 toc_entry_t *toc_entry;
230
231 for (toc_entry = toc_entries;
232 toc_entry->cmdline_name != NULL;
233 toc_entry++) {
234 image_desc_t *desc;
235
236 desc = new_image_desc(&toc_entry->uuid,
237 toc_entry->name,
238 toc_entry->cmdline_name);
239 add_image_desc(desc);
240 }
241}
242
243static void add_image(image_t *image)
244{
Masahiro Yamadad224b452017-01-14 23:22:02 +0900245 image_t **p = &image_head;
246
dp-arm90d2f0e2016-11-14 15:54:32 +0000247 assert(lookup_image_from_uuid(&image->uuid) == NULL);
Masahiro Yamadad224b452017-01-14 23:22:02 +0900248
249 while (*p)
250 p = &(*p)->next;
251
252 *p = image;
253
dp-arm90d2f0e2016-11-14 15:54:32 +0000254 nr_images++;
255}
256
Masahiro Yamadad65e7fd2017-01-27 11:57:54 +0900257static void replace_image(image_t *image)
258{
259 image_t **p = &image_head;
260
261 while (*p) {
262 if (!memcmp(&(*p)->uuid, &image->uuid, sizeof(image->uuid)))
263 break;
264 p = &(*p)->next;
265 }
266
267 assert(*p != NULL);
268
269 image->next = (*p)->next;
270 *p = image;
271}
272
dp-arm90d2f0e2016-11-14 15:54:32 +0000273static void free_image(image_t *image)
274{
275 free(image->buffer);
276 free(image);
277}
278
dp-arm4972ec52016-05-25 16:20:20 +0100279static void remove_image(image_t *image)
280{
dp-arm90d2f0e2016-11-14 15:54:32 +0000281 image_t *tmp = image_head, *prev;
dp-arm4972ec52016-05-25 16:20:20 +0100282
dp-arm90d2f0e2016-11-14 15:54:32 +0000283 if (tmp == image) {
284 image_head = tmp->next;
285 free_image(tmp);
286 } else {
287 while (tmp != NULL && tmp != image) {
288 prev = tmp;
289 tmp = tmp->next;
dp-arm4972ec52016-05-25 16:20:20 +0100290 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000291 assert(tmp != NULL);
292 prev->next = tmp->next;
293 free_image(tmp);
dp-arm4972ec52016-05-25 16:20:20 +0100294 }
dp-arm4972ec52016-05-25 16:20:20 +0100295 nr_images--;
296}
297
298static void free_images(void)
299{
dp-arm90d2f0e2016-11-14 15:54:32 +0000300 image_t *image = image_head, *tmp;
dp-arm4972ec52016-05-25 16:20:20 +0100301
dp-arm90d2f0e2016-11-14 15:54:32 +0000302 while (image != NULL) {
303 tmp = image->next;
304 free_image(image);
305 image = tmp;
306 nr_images--;
dp-arm4972ec52016-05-25 16:20:20 +0100307 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000308 assert(nr_images == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100309}
310
dp-arm90d2f0e2016-11-14 15:54:32 +0000311static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
dp-arm4972ec52016-05-25 16:20:20 +0100312{
dp-arm90d2f0e2016-11-14 15:54:32 +0000313 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100314
dp-arm90d2f0e2016-11-14 15:54:32 +0000315 for (desc = image_desc_head; desc != NULL; desc = desc->next)
316 if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
317 return desc;
dp-arm4972ec52016-05-25 16:20:20 +0100318 return NULL;
319}
320
dp-arm90d2f0e2016-11-14 15:54:32 +0000321static image_desc_t *lookup_image_desc_from_opt(const char *opt)
322{
323 image_desc_t *desc;
324
325 for (desc = image_desc_head; desc != NULL; desc = desc->next)
326 if (strcmp(desc->cmdline_name, opt) == 0)
327 return desc;
328 return NULL;
329}
330
331static image_t *lookup_image_from_uuid(const uuid_t *uuid)
dp-arm715ef422016-08-30 14:18:58 +0100332{
333 image_t *image;
dp-arm715ef422016-08-30 14:18:58 +0100334
dp-arm90d2f0e2016-11-14 15:54:32 +0000335 for (image = image_head; image != NULL; image = image->next)
dp-arm715ef422016-08-30 14:18:58 +0100336 if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0)
337 return image;
dp-arm715ef422016-08-30 14:18:58 +0100338 return NULL;
339}
340
dp-arm516dfcb2016-11-03 13:59:26 +0000341static void uuid_to_str(char *s, size_t len, const uuid_t *u)
342{
343 assert(len >= (_UUID_STR_LEN + 1));
344
345 snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X",
346 u->time_low,
347 u->time_mid,
348 u->time_hi_and_version,
349 ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
350 ((uint16_t)u->node[0] << 8) | u->node[1],
351 ((uint16_t)u->node[2] << 8) | u->node[3],
352 ((uint16_t)u->node[4] << 8) | u->node[5]);
353}
354
355static void uuid_from_str(uuid_t *u, const char *s)
356{
357 int n;
358
359 if (s == NULL)
360 log_errx("UUID cannot be NULL");
361 if (strlen(s) != _UUID_STR_LEN)
362 log_errx("Invalid UUID: %s", s);
363
364 n = sscanf(s,
365 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
366 &u->time_low, &u->time_mid, &u->time_hi_and_version,
367 &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
368 &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
369 /*
370 * Given the format specifier above, we expect 11 items to be scanned
371 * for a properly formatted UUID.
372 */
373 if (n != 11)
374 log_errx("Invalid UUID: %s", s);
375}
376
dp-armc1f8e772016-11-04 10:52:25 +0000377static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
dp-arm4972ec52016-05-25 16:20:20 +0100378{
379 struct stat st;
380 FILE *fp;
381 char *buf, *bufend;
382 fip_toc_header_t *toc_header;
383 fip_toc_entry_t *toc_entry;
dp-arm4972ec52016-05-25 16:20:20 +0100384 int terminated = 0;
385
386 fp = fopen(filename, "r");
387 if (fp == NULL)
388 log_err("fopen %s", filename);
389
390 if (fstat(fileno(fp), &st) == -1)
391 log_err("fstat %s", filename);
392
dp-armdb0f5e92016-11-04 10:56:25 +0000393 buf = xmalloc(st.st_size, "failed to load file into memory");
dp-arm4972ec52016-05-25 16:20:20 +0100394 if (fread(buf, 1, st.st_size, fp) != st.st_size)
395 log_errx("Failed to read %s", filename);
396 bufend = buf + st.st_size;
397 fclose(fp);
398
399 if (st.st_size < sizeof(fip_toc_header_t))
400 log_errx("FIP %s is truncated", filename);
401
402 toc_header = (fip_toc_header_t *)buf;
403 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
404
405 if (toc_header->name != TOC_HEADER_NAME)
406 log_errx("%s is not a FIP file", filename);
407
408 /* Return the ToC header if the caller wants it. */
409 if (toc_header_out != NULL)
410 *toc_header_out = *toc_header;
411
412 /* Walk through each ToC entry in the file. */
413 while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
dp-arm516dfcb2016-11-03 13:59:26 +0000414 image_t *image;
415 image_desc_t *desc;
416
dp-arm4972ec52016-05-25 16:20:20 +0100417 /* Found the ToC terminator, we are done. */
418 if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
419 terminated = 1;
420 break;
421 }
422
423 /*
424 * Build a new image out of the ToC entry and add it to the
425 * table of images.
426 */
Masahiro Yamadad224b452017-01-14 23:22:02 +0900427 image = xzalloc(sizeof(*image),
dp-armdb0f5e92016-11-04 10:56:25 +0000428 "failed to allocate memory for image");
dp-arm4972ec52016-05-25 16:20:20 +0100429 memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000430 image->buffer = xmalloc(toc_entry->size,
431 "failed to allocate image buffer, is FIP file corrupted?");
dp-arm4972ec52016-05-25 16:20:20 +0100432 /* Overflow checks before memory copy. */
433 if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
434 log_errx("FIP %s is corrupted", filename);
435 if (toc_entry->size + toc_entry->offset_address > st.st_size)
436 log_errx("FIP %s is corrupted", filename);
437
438 memcpy(image->buffer, buf + toc_entry->offset_address,
439 toc_entry->size);
440 image->size = toc_entry->size;
441
dp-arm516dfcb2016-11-03 13:59:26 +0000442 /* If this is an unknown image, create a descriptor for it. */
443 desc = lookup_image_desc_from_uuid(&image->uuid);
444 if (desc == NULL) {
445 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
446
447 uuid_to_str(name, sizeof(name), &image->uuid);
448 snprintf(filename, sizeof(filename), "%s%s",
449 name, ".bin");
450 desc = new_image_desc(&image->uuid, name, "blob");
451 desc->action = DO_UNPACK;
452 desc->action_arg = xstrdup(filename,
453 "failed to allocate memory for blob filename");
454 add_image_desc(desc);
455 }
456
dp-arm4972ec52016-05-25 16:20:20 +0100457 add_image(image);
458
459 toc_entry++;
460 }
461
462 if (terminated == 0)
463 log_errx("FIP %s does not have a ToC terminator entry",
464 filename);
465 free(buf);
466 return 0;
467}
468
dp-armc1f8e772016-11-04 10:52:25 +0000469static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100470{
471 struct stat st;
472 image_t *image;
473 FILE *fp;
474
dp-arm715ef422016-08-30 14:18:58 +0100475 assert(uuid != NULL);
476
dp-arm4972ec52016-05-25 16:20:20 +0100477 fp = fopen(filename, "r");
478 if (fp == NULL)
479 log_err("fopen %s", filename);
480
481 if (fstat(fileno(fp), &st) == -1)
482 log_errx("fstat %s", filename);
483
Masahiro Yamadad224b452017-01-14 23:22:02 +0900484 image = xzalloc(sizeof(*image), "failed to allocate memory for image");
dp-arm715ef422016-08-30 14:18:58 +0100485 memcpy(&image->uuid, uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000486 image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
dp-arm4972ec52016-05-25 16:20:20 +0100487 if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
488 log_errx("Failed to read %s", filename);
489 image->size = st.st_size;
dp-arm4972ec52016-05-25 16:20:20 +0100490
491 fclose(fp);
492 return image;
493}
494
dp-armc1f8e772016-11-04 10:52:25 +0000495static int write_image_to_file(const image_t *image, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100496{
497 FILE *fp;
498
499 fp = fopen(filename, "w");
500 if (fp == NULL)
501 log_err("fopen");
502 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
503 log_errx("Failed to write %s", filename);
504 fclose(fp);
505 return 0;
506}
507
dp-arm90d2f0e2016-11-14 15:54:32 +0000508static struct option *add_opt(struct option *opts, size_t *nr_opts,
509 const char *name, int has_arg, int val)
dp-arm4972ec52016-05-25 16:20:20 +0100510{
dp-arm90d2f0e2016-11-14 15:54:32 +0000511 opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
512 if (opts == NULL)
513 log_err("realloc");
514 opts[*nr_opts].name = name;
515 opts[*nr_opts].has_arg = has_arg;
516 opts[*nr_opts].flag = NULL;
517 opts[*nr_opts].val = val;
518 ++*nr_opts;
519 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100520}
521
dp-arm90d2f0e2016-11-14 15:54:32 +0000522static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
523 int has_arg)
dp-arm4972ec52016-05-25 16:20:20 +0100524{
dp-arm90d2f0e2016-11-14 15:54:32 +0000525 image_desc_t *desc;
526
527 for (desc = image_desc_head; desc != NULL; desc = desc->next)
528 opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
529 OPT_TOC_ENTRY);
530 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100531}
532
dp-arm90d2f0e2016-11-14 15:54:32 +0000533static void md_print(const unsigned char *md, size_t len)
dp-arm12e893b2016-08-24 13:21:08 +0100534{
535 size_t i;
536
537 for (i = 0; i < len; i++)
538 printf("%02x", md[i]);
539}
540
dp-arm4972ec52016-05-25 16:20:20 +0100541static int info_cmd(int argc, char *argv[])
542{
543 image_t *image;
544 uint64_t image_offset;
dp-arm516dfcb2016-11-03 13:59:26 +0000545 uint64_t image_size;
dp-arm4972ec52016-05-25 16:20:20 +0100546 fip_toc_header_t toc_header;
dp-arm4972ec52016-05-25 16:20:20 +0100547
548 if (argc != 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100549 info_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100550 argc--, argv++;
551
552 parse_fip(argv[0], &toc_header);
553
554 if (verbose) {
555 log_dbgx("toc_header[name]: 0x%llX",
556 (unsigned long long)toc_header.name);
557 log_dbgx("toc_header[serial_number]: 0x%llX",
558 (unsigned long long)toc_header.serial_number);
559 log_dbgx("toc_header[flags]: 0x%llX",
560 (unsigned long long)toc_header.flags);
561 }
562
563 image_offset = sizeof(fip_toc_header_t) +
564 (sizeof(fip_toc_entry_t) * (nr_images + 1));
565
dp-arm90d2f0e2016-11-14 15:54:32 +0000566 for (image = image_head; image != NULL; image = image->next) {
567 image_desc_t *desc;
dp-arm715ef422016-08-30 14:18:58 +0100568
dp-arm90d2f0e2016-11-14 15:54:32 +0000569 desc = lookup_image_desc_from_uuid(&image->uuid);
dp-arm516dfcb2016-11-03 13:59:26 +0000570 assert(desc != NULL);
571 printf("%s: ", desc->name);
dp-arm4972ec52016-05-25 16:20:20 +0100572 image_size = image->size;
Masahiro Yamadaeee39312017-01-15 23:20:00 +0900573 printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
574 (unsigned long long)image_offset,
575 (unsigned long long)image_size,
576 desc->cmdline_name);
dp-arm12e893b2016-08-24 13:21:08 +0100577 if (verbose) {
578 unsigned char md[SHA256_DIGEST_LENGTH];
579
580 SHA256(image->buffer, image_size, md);
581 printf(", sha256=");
582 md_print(md, sizeof(md));
583 }
584 putchar('\n');
dp-arm4972ec52016-05-25 16:20:20 +0100585 image_offset += image_size;
586 }
587
588 free_images();
589 return 0;
590}
591
592static void info_usage(void)
593{
594 printf("fiptool info FIP_FILENAME\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100595 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100596}
597
dp-arm90d2f0e2016-11-14 15:54:32 +0000598static int pack_images(const char *filename, uint64_t toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100599{
600 FILE *fp;
601 image_t *image;
602 fip_toc_header_t *toc_header;
603 fip_toc_entry_t *toc_entry;
604 char *buf;
605 uint64_t entry_offset, buf_size, payload_size;
dp-arm4972ec52016-05-25 16:20:20 +0100606
607 /* Calculate total payload size and allocate scratch buffer. */
608 payload_size = 0;
dp-arm90d2f0e2016-11-14 15:54:32 +0000609 for (image = image_head; image != NULL; image = image->next)
610 payload_size += image->size;
dp-arm4972ec52016-05-25 16:20:20 +0100611
612 buf_size = sizeof(fip_toc_header_t) +
613 sizeof(fip_toc_entry_t) * (nr_images + 1);
614 buf = calloc(1, buf_size);
615 if (buf == NULL)
616 log_err("calloc");
617
618 /* Build up header and ToC entries from the image table. */
619 toc_header = (fip_toc_header_t *)buf;
620 toc_header->name = TOC_HEADER_NAME;
621 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
622 toc_header->flags = toc_flags;
623
624 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
625
626 entry_offset = buf_size;
dp-arm90d2f0e2016-11-14 15:54:32 +0000627 for (image = image_head; image != NULL; image = image->next) {
dp-arm4972ec52016-05-25 16:20:20 +0100628 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
629 toc_entry->offset_address = entry_offset;
630 toc_entry->size = image->size;
631 toc_entry->flags = 0;
632 entry_offset += toc_entry->size;
633 toc_entry++;
634 }
635
636 /* Append a null uuid entry to mark the end of ToC entries. */
637 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
638 toc_entry->offset_address = entry_offset;
639 toc_entry->size = 0;
640 toc_entry->flags = 0;
641
642 /* Generate the FIP file. */
643 fp = fopen(filename, "w");
644 if (fp == NULL)
645 log_err("fopen %s", filename);
646
647 if (verbose)
648 log_dbgx("Metadata size: %zu bytes", buf_size);
649
650 if (fwrite(buf, 1, buf_size, fp) != buf_size)
651 log_errx("Failed to write image to %s", filename);
652 free(buf);
653
654 if (verbose)
655 log_dbgx("Payload size: %zu bytes", payload_size);
656
dp-arm90d2f0e2016-11-14 15:54:32 +0000657 for (image = image_head; image != NULL; image = image->next)
dp-arm4972ec52016-05-25 16:20:20 +0100658 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
659 log_errx("Failed to write image to %s", filename);
dp-arm4972ec52016-05-25 16:20:20 +0100660
661 fclose(fp);
662 return 0;
663}
664
665/*
666 * This function is shared between the create and update subcommands.
667 * The difference between the two subcommands is that when the FIP file
668 * is created, the parsing of an existing FIP is skipped. This results
669 * in update_fip() creating the new FIP file from scratch because the
670 * internal image table is not populated.
671 */
672static void update_fip(void)
673{
dp-arm90d2f0e2016-11-14 15:54:32 +0000674 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100675
676 /* Add or replace images in the FIP file. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000677 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +0000678 image_t *new_image, *old_image;
679
dp-arm90d2f0e2016-11-14 15:54:32 +0000680 if (desc->action != DO_PACK)
dp-arm4972ec52016-05-25 16:20:20 +0100681 continue;
682
dp-arm90d2f0e2016-11-14 15:54:32 +0000683 new_image = read_image_from_file(&desc->uuid,
684 desc->action_arg);
685 old_image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100686 if (old_image != NULL) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000687 if (verbose) {
dp-arm516dfcb2016-11-03 13:59:26 +0000688 log_dbgx("Replacing %s with %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000689 desc->cmdline_name,
690 desc->action_arg);
691 }
Masahiro Yamadad65e7fd2017-01-27 11:57:54 +0900692 replace_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100693 } else {
694 if (verbose)
695 log_dbgx("Adding image %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000696 desc->action_arg);
dp-arm715ef422016-08-30 14:18:58 +0100697 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100698 }
dp-arm4972ec52016-05-25 16:20:20 +0100699 }
700}
701
dp-arm90d2f0e2016-11-14 15:54:32 +0000702static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100703{
704 unsigned long long flags;
705 char *endptr;
706
707 errno = 0;
708 flags = strtoull(arg, &endptr, 16);
709 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
710 log_errx("Invalid platform ToC flags: %s", arg);
711 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
712 *toc_flags |= flags << 32;
713}
714
dp-arm516dfcb2016-11-03 13:59:26 +0000715static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
716{
717 char *p;
718
719 for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
720 if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
721 p += strlen("uuid=");
722 uuid_from_str(uuid, p);
723 } else if (strncmp(p, "file=", strlen("file=")) == 0) {
724 p += strlen("file=");
725 snprintf(filename, len, "%s", p);
726 }
727 }
728}
729
dp-arm4972ec52016-05-25 16:20:20 +0100730static int create_cmd(int argc, char *argv[])
731{
dp-arm90d2f0e2016-11-14 15:54:32 +0000732 struct option *opts = NULL;
733 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100734 unsigned long long toc_flags = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100735
736 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100737 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100738
dp-arm90d2f0e2016-11-14 15:54:32 +0000739 opts = fill_common_opts(opts, &nr_opts, required_argument);
740 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100741 OPT_PLAT_TOC_FLAGS);
dp-arm516dfcb2016-11-03 13:59:26 +0000742 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000743 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100744
745 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000746 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100747
dp-arm516dfcb2016-11-03 13:59:26 +0000748 c = getopt_long(argc, argv, "b:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100749 if (c == -1)
750 break;
751
752 switch (c) {
753 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000754 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100755
dp-arm90d2f0e2016-11-14 15:54:32 +0000756 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000757 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100758 break;
759 }
760 case OPT_PLAT_TOC_FLAGS:
761 parse_plat_toc_flags(optarg, &toc_flags);
762 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000763 case 'b': {
764 char name[_UUID_STR_LEN + 1];
765 char filename[PATH_MAX] = { 0 };
766 uuid_t uuid = { 0 };
767 image_desc_t *desc;
768
769 parse_blob_opt(optarg, &uuid,
770 filename, sizeof(filename));
771
772 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
773 filename[0] == '\0')
774 create_usage();
775
776 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000777 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000778 uuid_to_str(name, sizeof(name), &uuid);
779 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000780 add_image_desc(desc);
781 }
dp-armfb732312016-12-30 09:55:48 +0000782 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000783 break;
784 }
dp-arm4972ec52016-05-25 16:20:20 +0100785 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100786 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100787 }
788 }
789 argc -= optind;
790 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000791 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100792
793 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100794 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100795
796 update_fip();
797
798 pack_images(argv[0], toc_flags);
799 free_images();
800 return 0;
801}
802
803static void create_usage(void)
804{
805 toc_entry_t *toc_entry = toc_entries;
806
dp-arm516dfcb2016-11-03 13:59:26 +0000807 printf("fiptool create [--blob uuid=...,file=...] "
808 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
809 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID "
810 "pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100811 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
812 "occupying bits 32-47 in 64-bit ToC header.\n");
813 fputc('\n', stderr);
814 printf("Specific images are packed with the following options:\n");
815 for (; toc_entry->cmdline_name != NULL; toc_entry++)
816 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
817 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100818 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100819}
820
821static int update_cmd(int argc, char *argv[])
822{
dp-arm90d2f0e2016-11-14 15:54:32 +0000823 struct option *opts = NULL;
824 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000825 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +0100826 fip_toc_header_t toc_header = { 0 };
827 unsigned long long toc_flags = 0;
828 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100829
830 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100831 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100832
dp-arm90d2f0e2016-11-14 15:54:32 +0000833 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000834 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000835 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
836 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100837 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000838 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100839
840 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000841 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100842
dp-arm516dfcb2016-11-03 13:59:26 +0000843 c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100844 if (c == -1)
845 break;
846
847 switch (c) {
848 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000849 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100850
dp-arm90d2f0e2016-11-14 15:54:32 +0000851 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000852 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100853 break;
854 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000855 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100856 parse_plat_toc_flags(optarg, &toc_flags);
857 pflag = 1;
858 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000859 case 'b': {
860 char name[_UUID_STR_LEN + 1];
861 char filename[PATH_MAX] = { 0 };
862 uuid_t uuid = { 0 };
863 image_desc_t *desc;
864
865 parse_blob_opt(optarg, &uuid,
866 filename, sizeof(filename));
867
868 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
869 filename[0] == '\0')
870 update_usage();
871
872 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000873 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000874 uuid_to_str(name, sizeof(name), &uuid);
875 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000876 add_image_desc(desc);
877 }
dp-armfb732312016-12-30 09:55:48 +0000878 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000879 break;
880 }
dp-arm4972ec52016-05-25 16:20:20 +0100881 case 'o':
882 snprintf(outfile, sizeof(outfile), "%s", optarg);
883 break;
884 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100885 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100886 }
887 }
888 argc -= optind;
889 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000890 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100891
892 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100893 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100894
895 if (outfile[0] == '\0')
896 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
897
Masahiro Yamadaebb6e372016-12-25 12:41:41 +0900898 if (access(argv[0], F_OK) == 0)
dp-arm4972ec52016-05-25 16:20:20 +0100899 parse_fip(argv[0], &toc_header);
900
901 if (pflag)
902 toc_header.flags &= ~(0xffffULL << 32);
903 toc_flags = (toc_header.flags |= toc_flags);
904
905 update_fip();
906
907 pack_images(outfile, toc_flags);
908 free_images();
909 return 0;
910}
911
912static void update_usage(void)
913{
914 toc_entry_t *toc_entry = toc_entries;
915
dp-arm516dfcb2016-11-03 13:59:26 +0000916 printf("fiptool update [--blob uuid=...,file=...] [--out FIP_FILENAME] "
dp-arm4972ec52016-05-25 16:20:20 +0100917 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\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
dp-arm516dfcb2016-11-03 13:59:26 +00001053 printf("fiptool unpack [--blob uuid=...,file=...] [--force] "
1054 "[--out <path>] [opts] FIP_FILENAME\n");
1055 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID "
1056 "to file.\n");
1057 printf(" --force\t\t\tIf the output file already exists, use --force to "
dp-arm4972ec52016-05-25 16:20:20 +01001058 "overwrite it.\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001059 printf(" --out path\t\t\tSet the output directory path.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001060 fputc('\n', stderr);
1061 printf("Specific images are unpacked with the following options:\n");
1062 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1063 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1064 toc_entry->name);
1065 fputc('\n', stderr);
1066 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001067 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001068}
1069
1070static int remove_cmd(int argc, char *argv[])
1071{
dp-arm90d2f0e2016-11-14 15:54:32 +00001072 struct option *opts = NULL;
1073 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +00001074 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +01001075 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +00001076 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +01001077 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001078
1079 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +01001080 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001081
dp-arm90d2f0e2016-11-14 15:54:32 +00001082 opts = fill_common_opts(opts, &nr_opts, no_argument);
dp-arm516dfcb2016-11-03 13:59:26 +00001083 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +00001084 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1085 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1086 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +01001087
1088 while (1) {
dp-armfe92b892016-11-07 11:13:54 +00001089 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001090
dp-arm516dfcb2016-11-03 13:59:26 +00001091 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +01001092 if (c == -1)
1093 break;
1094
1095 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +00001096 case OPT_TOC_ENTRY: {
1097 image_desc_t *desc;
1098
1099 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +00001100 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm4972ec52016-05-25 16:20:20 +01001101 break;
dp-arm90d2f0e2016-11-14 15:54:32 +00001102 }
dp-arm516dfcb2016-11-03 13:59:26 +00001103 case 'b': {
1104 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1105 uuid_t uuid = { 0 };
1106 image_desc_t *desc;
1107
1108 parse_blob_opt(optarg, &uuid,
1109 filename, sizeof(filename));
1110
1111 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1112 remove_usage();
1113
1114 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +00001115 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +00001116 uuid_to_str(name, sizeof(name), &uuid);
1117 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +00001118 add_image_desc(desc);
1119 }
dp-armfb732312016-12-30 09:55:48 +00001120 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm516dfcb2016-11-03 13:59:26 +00001121 break;
1122 }
dp-arm4972ec52016-05-25 16:20:20 +01001123 case 'f':
1124 fflag = 1;
1125 break;
1126 case 'o':
1127 snprintf(outfile, sizeof(outfile), "%s", optarg);
1128 break;
1129 default:
dp-arm29f1b5c2016-09-15 09:58:50 +01001130 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001131 }
1132 }
1133 argc -= optind;
1134 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001135 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001136
1137 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001138 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001139
1140 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1141 log_errx("File %s already exists, use --force to overwrite it",
1142 outfile);
1143
1144 if (outfile[0] == '\0')
1145 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1146
1147 parse_fip(argv[0], &toc_header);
1148
dp-arm90d2f0e2016-11-14 15:54:32 +00001149 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +01001150 image_t *image;
1151
dp-arm90d2f0e2016-11-14 15:54:32 +00001152 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +01001153 continue;
dp-arm516dfcb2016-11-03 13:59:26 +00001154
dp-arm90d2f0e2016-11-14 15:54:32 +00001155 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001156 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +01001157 if (verbose)
dp-arm516dfcb2016-11-03 13:59:26 +00001158 log_dbgx("Removing %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001159 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +01001160 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +01001161 } else {
dp-arm516dfcb2016-11-03 13:59:26 +00001162 log_warnx("%s does not exist in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001163 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001164 }
1165 }
1166
1167 pack_images(outfile, toc_header.flags);
1168 free_images();
1169 return 0;
1170}
1171
1172static void remove_usage(void)
1173{
1174 toc_entry_t *toc_entry = toc_entries;
1175
dp-arm516dfcb2016-11-03 13:59:26 +00001176 printf("fiptool remove [--blob uuid=...] [--force] "
1177 "[--out FIP_FILENAME] [opts] FIP_FILENAME\n");
1178 printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001179 printf(" --force\t\tIf the output FIP file already exists, use --force to "
1180 "overwrite it.\n");
1181 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1182 fputc('\n', stderr);
1183 printf("Specific images are removed with the following options:\n");
1184 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1185 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
1186 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +01001187 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001188}
1189
1190static int version_cmd(int argc, char *argv[])
1191{
1192#ifdef VERSION
1193 puts(VERSION);
1194#else
1195 /* If built from fiptool directory, VERSION is not set. */
1196 puts("Unknown version");
1197#endif
1198 return 0;
1199}
1200
1201static void version_usage(void)
1202{
1203 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001204 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001205}
1206
1207static int help_cmd(int argc, char *argv[])
1208{
1209 int i;
1210
1211 if (argc < 2)
1212 usage();
1213 argc--, argv++;
1214
1215 for (i = 0; i < NELEM(cmds); i++) {
1216 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001217 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001218 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001219 }
1220 if (i == NELEM(cmds))
1221 printf("No help for subcommand '%s'\n", argv[0]);
1222 return 0;
1223}
1224
1225static void usage(void)
1226{
1227 printf("usage: [--verbose] fiptool <command> [<args>]\n");
1228 printf("Global options supported:\n");
1229 printf(" --verbose\tEnable verbose output for all commands.\n");
1230 fputc('\n', stderr);
1231 printf("Commands supported:\n");
1232 printf(" info\t\tList images contained in FIP.\n");
1233 printf(" create\tCreate a new FIP with the given images.\n");
1234 printf(" update\tUpdate an existing FIP with the given images.\n");
1235 printf(" unpack\tUnpack images from FIP.\n");
1236 printf(" remove\tRemove images from FIP.\n");
1237 printf(" version\tShow fiptool version.\n");
1238 printf(" help\t\tShow help for given command.\n");
1239 exit(1);
1240}
1241
1242int main(int argc, char *argv[])
1243{
1244 int i, ret = 0;
1245
dp-arm5cd10ae2016-11-07 10:45:59 +00001246 while (1) {
1247 int c, opt_index = 0;
1248 static struct option opts[] = {
1249 { "verbose", no_argument, NULL, 'v' },
1250 { NULL, no_argument, NULL, 0 }
1251 };
1252
1253 /*
1254 * Set POSIX mode so getopt stops at the first non-option
1255 * which is the subcommand.
1256 */
1257 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1258 if (c == -1)
1259 break;
dp-arm4972ec52016-05-25 16:20:20 +01001260
dp-arm5cd10ae2016-11-07 10:45:59 +00001261 switch (c) {
1262 case 'v':
1263 verbose = 1;
1264 break;
1265 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001266 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001267 }
dp-arm4972ec52016-05-25 16:20:20 +01001268 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001269 argc -= optind;
1270 argv += optind;
1271 /* Reset optind for subsequent getopt processing. */
1272 optind = 0;
1273
1274 if (argc == 0)
1275 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001276
dp-arm90d2f0e2016-11-14 15:54:32 +00001277 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001278 for (i = 0; i < NELEM(cmds); i++) {
1279 if (strcmp(cmds[i].name, argv[0]) == 0) {
1280 ret = cmds[i].handler(argc, argv);
1281 break;
1282 }
1283 }
1284 if (i == NELEM(cmds))
1285 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001286 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001287 return ret;
1288}