blob: 793519aad67f4e18496a1e5c10b33f65b78468ec [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)
144 log_errx("strdup: ", msg);
145 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)
154 log_errx("malloc: ", msg);
155 return d;
156}
157
dp-arm90d2f0e2016-11-14 15:54:32 +0000158static image_desc_t *new_image_desc(const uuid_t *uuid,
159 const char *name, const char *cmdline_name)
dp-arm4972ec52016-05-25 16:20:20 +0100160{
dp-arm90d2f0e2016-11-14 15:54:32 +0000161 image_desc_t *desc;
162
163 desc = xmalloc(sizeof(*desc),
164 "failed to allocate memory for image descriptor");
165 memcpy(&desc->uuid, uuid, sizeof(uuid_t));
166 desc->name = xstrdup(name,
167 "failed to allocate memory for image name");
168 desc->cmdline_name = xstrdup(cmdline_name,
169 "failed to allocate memory for image command line name");
170 desc->action = DO_UNSPEC;
171 desc->action_arg = NULL;
172 return desc;
dp-arm4972ec52016-05-25 16:20:20 +0100173}
174
dp-arm90d2f0e2016-11-14 15:54:32 +0000175static void free_image_desc(image_desc_t *desc)
dp-arm4972ec52016-05-25 16:20:20 +0100176{
dp-arm90d2f0e2016-11-14 15:54:32 +0000177 free(desc->name);
178 free(desc->cmdline_name);
179 free(desc->action_arg);
180 free(desc);
dp-arm4972ec52016-05-25 16:20:20 +0100181}
182
dp-arm90d2f0e2016-11-14 15:54:32 +0000183static void add_image_desc(image_desc_t *desc)
dp-arm4972ec52016-05-25 16:20:20 +0100184{
dp-arm90d2f0e2016-11-14 15:54:32 +0000185 assert(lookup_image_desc_from_uuid(&desc->uuid) == NULL);
186 desc->next = image_desc_head;
187 image_desc_head = desc;
188 nr_image_descs++;
189}
dp-arm4972ec52016-05-25 16:20:20 +0100190
dp-arm90d2f0e2016-11-14 15:54:32 +0000191static void free_image_descs(void)
192{
193 image_desc_t *desc = image_desc_head, *tmp;
194
195 while (desc != NULL) {
196 tmp = desc->next;
197 free_image_desc(desc);
198 desc = tmp;
199 nr_image_descs--;
dp-arm4972ec52016-05-25 16:20:20 +0100200 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000201 assert(nr_image_descs == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100202}
203
dp-arm90d2f0e2016-11-14 15:54:32 +0000204static void fill_image_descs(void)
205{
206 toc_entry_t *toc_entry;
207
208 for (toc_entry = toc_entries;
209 toc_entry->cmdline_name != NULL;
210 toc_entry++) {
211 image_desc_t *desc;
212
213 desc = new_image_desc(&toc_entry->uuid,
214 toc_entry->name,
215 toc_entry->cmdline_name);
216 add_image_desc(desc);
217 }
218}
219
220static void add_image(image_t *image)
221{
222 assert(lookup_image_from_uuid(&image->uuid) == NULL);
223 image->next = image_head;
224 image_head = image;
225 nr_images++;
226}
227
228static void free_image(image_t *image)
229{
230 free(image->buffer);
231 free(image);
232}
233
dp-arm4972ec52016-05-25 16:20:20 +0100234static void remove_image(image_t *image)
235{
dp-arm90d2f0e2016-11-14 15:54:32 +0000236 image_t *tmp = image_head, *prev;
dp-arm4972ec52016-05-25 16:20:20 +0100237
dp-arm90d2f0e2016-11-14 15:54:32 +0000238 if (tmp == image) {
239 image_head = tmp->next;
240 free_image(tmp);
241 } else {
242 while (tmp != NULL && tmp != image) {
243 prev = tmp;
244 tmp = tmp->next;
dp-arm4972ec52016-05-25 16:20:20 +0100245 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000246 assert(tmp != NULL);
247 prev->next = tmp->next;
248 free_image(tmp);
dp-arm4972ec52016-05-25 16:20:20 +0100249 }
dp-arm4972ec52016-05-25 16:20:20 +0100250 nr_images--;
251}
252
253static void free_images(void)
254{
dp-arm90d2f0e2016-11-14 15:54:32 +0000255 image_t *image = image_head, *tmp;
dp-arm4972ec52016-05-25 16:20:20 +0100256
dp-arm90d2f0e2016-11-14 15:54:32 +0000257 while (image != NULL) {
258 tmp = image->next;
259 free_image(image);
260 image = tmp;
261 nr_images--;
dp-arm4972ec52016-05-25 16:20:20 +0100262 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000263 assert(nr_images == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100264}
265
dp-arm90d2f0e2016-11-14 15:54:32 +0000266static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
dp-arm4972ec52016-05-25 16:20:20 +0100267{
dp-arm90d2f0e2016-11-14 15:54:32 +0000268 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100269
dp-arm90d2f0e2016-11-14 15:54:32 +0000270 for (desc = image_desc_head; desc != NULL; desc = desc->next)
271 if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
272 return desc;
dp-arm4972ec52016-05-25 16:20:20 +0100273 return NULL;
274}
275
dp-arm90d2f0e2016-11-14 15:54:32 +0000276static image_desc_t *lookup_image_desc_from_opt(const char *opt)
277{
278 image_desc_t *desc;
279
280 for (desc = image_desc_head; desc != NULL; desc = desc->next)
281 if (strcmp(desc->cmdline_name, opt) == 0)
282 return desc;
283 return NULL;
284}
285
286static image_t *lookup_image_from_uuid(const uuid_t *uuid)
dp-arm715ef422016-08-30 14:18:58 +0100287{
288 image_t *image;
dp-arm715ef422016-08-30 14:18:58 +0100289
dp-arm90d2f0e2016-11-14 15:54:32 +0000290 for (image = image_head; image != NULL; image = image->next)
dp-arm715ef422016-08-30 14:18:58 +0100291 if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0)
292 return image;
dp-arm715ef422016-08-30 14:18:58 +0100293 return NULL;
294}
295
dp-arm516dfcb2016-11-03 13:59:26 +0000296static void uuid_to_str(char *s, size_t len, const uuid_t *u)
297{
298 assert(len >= (_UUID_STR_LEN + 1));
299
300 snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X",
301 u->time_low,
302 u->time_mid,
303 u->time_hi_and_version,
304 ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
305 ((uint16_t)u->node[0] << 8) | u->node[1],
306 ((uint16_t)u->node[2] << 8) | u->node[3],
307 ((uint16_t)u->node[4] << 8) | u->node[5]);
308}
309
310static void uuid_from_str(uuid_t *u, const char *s)
311{
312 int n;
313
314 if (s == NULL)
315 log_errx("UUID cannot be NULL");
316 if (strlen(s) != _UUID_STR_LEN)
317 log_errx("Invalid UUID: %s", s);
318
319 n = sscanf(s,
320 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
321 &u->time_low, &u->time_mid, &u->time_hi_and_version,
322 &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
323 &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
324 /*
325 * Given the format specifier above, we expect 11 items to be scanned
326 * for a properly formatted UUID.
327 */
328 if (n != 11)
329 log_errx("Invalid UUID: %s", s);
330}
331
dp-armc1f8e772016-11-04 10:52:25 +0000332static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
dp-arm4972ec52016-05-25 16:20:20 +0100333{
334 struct stat st;
335 FILE *fp;
336 char *buf, *bufend;
337 fip_toc_header_t *toc_header;
338 fip_toc_entry_t *toc_entry;
dp-arm4972ec52016-05-25 16:20:20 +0100339 int terminated = 0;
340
341 fp = fopen(filename, "r");
342 if (fp == NULL)
343 log_err("fopen %s", filename);
344
345 if (fstat(fileno(fp), &st) == -1)
346 log_err("fstat %s", filename);
347
dp-armdb0f5e92016-11-04 10:56:25 +0000348 buf = xmalloc(st.st_size, "failed to load file into memory");
dp-arm4972ec52016-05-25 16:20:20 +0100349 if (fread(buf, 1, st.st_size, fp) != st.st_size)
350 log_errx("Failed to read %s", filename);
351 bufend = buf + st.st_size;
352 fclose(fp);
353
354 if (st.st_size < sizeof(fip_toc_header_t))
355 log_errx("FIP %s is truncated", filename);
356
357 toc_header = (fip_toc_header_t *)buf;
358 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
359
360 if (toc_header->name != TOC_HEADER_NAME)
361 log_errx("%s is not a FIP file", filename);
362
363 /* Return the ToC header if the caller wants it. */
364 if (toc_header_out != NULL)
365 *toc_header_out = *toc_header;
366
367 /* Walk through each ToC entry in the file. */
368 while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
dp-arm516dfcb2016-11-03 13:59:26 +0000369 image_t *image;
370 image_desc_t *desc;
371
dp-arm4972ec52016-05-25 16:20:20 +0100372 /* Found the ToC terminator, we are done. */
373 if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
374 terminated = 1;
375 break;
376 }
377
378 /*
379 * Build a new image out of the ToC entry and add it to the
380 * table of images.
381 */
dp-armdb0f5e92016-11-04 10:56:25 +0000382 image = xmalloc(sizeof(*image),
383 "failed to allocate memory for image");
dp-arm4972ec52016-05-25 16:20:20 +0100384 memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000385 image->buffer = xmalloc(toc_entry->size,
386 "failed to allocate image buffer, is FIP file corrupted?");
dp-arm4972ec52016-05-25 16:20:20 +0100387 /* Overflow checks before memory copy. */
388 if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
389 log_errx("FIP %s is corrupted", filename);
390 if (toc_entry->size + toc_entry->offset_address > st.st_size)
391 log_errx("FIP %s is corrupted", filename);
392
393 memcpy(image->buffer, buf + toc_entry->offset_address,
394 toc_entry->size);
395 image->size = toc_entry->size;
396
dp-arm516dfcb2016-11-03 13:59:26 +0000397 /* If this is an unknown image, create a descriptor for it. */
398 desc = lookup_image_desc_from_uuid(&image->uuid);
399 if (desc == NULL) {
400 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
401
402 uuid_to_str(name, sizeof(name), &image->uuid);
403 snprintf(filename, sizeof(filename), "%s%s",
404 name, ".bin");
405 desc = new_image_desc(&image->uuid, name, "blob");
406 desc->action = DO_UNPACK;
407 desc->action_arg = xstrdup(filename,
408 "failed to allocate memory for blob filename");
409 add_image_desc(desc);
410 }
411
dp-arm4972ec52016-05-25 16:20:20 +0100412 add_image(image);
413
414 toc_entry++;
415 }
416
417 if (terminated == 0)
418 log_errx("FIP %s does not have a ToC terminator entry",
419 filename);
420 free(buf);
421 return 0;
422}
423
dp-armc1f8e772016-11-04 10:52:25 +0000424static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100425{
426 struct stat st;
427 image_t *image;
428 FILE *fp;
429
dp-arm715ef422016-08-30 14:18:58 +0100430 assert(uuid != NULL);
431
dp-arm4972ec52016-05-25 16:20:20 +0100432 fp = fopen(filename, "r");
433 if (fp == NULL)
434 log_err("fopen %s", filename);
435
436 if (fstat(fileno(fp), &st) == -1)
437 log_errx("fstat %s", filename);
438
dp-armdb0f5e92016-11-04 10:56:25 +0000439 image = xmalloc(sizeof(*image), "failed to allocate memory for image");
dp-arm715ef422016-08-30 14:18:58 +0100440 memcpy(&image->uuid, uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000441 image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
dp-arm4972ec52016-05-25 16:20:20 +0100442 if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
443 log_errx("Failed to read %s", filename);
444 image->size = st.st_size;
dp-arm4972ec52016-05-25 16:20:20 +0100445
446 fclose(fp);
447 return image;
448}
449
dp-armc1f8e772016-11-04 10:52:25 +0000450static int write_image_to_file(const image_t *image, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100451{
452 FILE *fp;
453
454 fp = fopen(filename, "w");
455 if (fp == NULL)
456 log_err("fopen");
457 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
458 log_errx("Failed to write %s", filename);
459 fclose(fp);
460 return 0;
461}
462
dp-arm90d2f0e2016-11-14 15:54:32 +0000463static struct option *add_opt(struct option *opts, size_t *nr_opts,
464 const char *name, int has_arg, int val)
dp-arm4972ec52016-05-25 16:20:20 +0100465{
dp-arm90d2f0e2016-11-14 15:54:32 +0000466 opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
467 if (opts == NULL)
468 log_err("realloc");
469 opts[*nr_opts].name = name;
470 opts[*nr_opts].has_arg = has_arg;
471 opts[*nr_opts].flag = NULL;
472 opts[*nr_opts].val = val;
473 ++*nr_opts;
474 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100475}
476
dp-arm90d2f0e2016-11-14 15:54:32 +0000477static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
478 int has_arg)
dp-arm4972ec52016-05-25 16:20:20 +0100479{
dp-arm90d2f0e2016-11-14 15:54:32 +0000480 image_desc_t *desc;
481
482 for (desc = image_desc_head; desc != NULL; desc = desc->next)
483 opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
484 OPT_TOC_ENTRY);
485 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100486}
487
dp-arm90d2f0e2016-11-14 15:54:32 +0000488static void md_print(const unsigned char *md, size_t len)
dp-arm12e893b2016-08-24 13:21:08 +0100489{
490 size_t i;
491
492 for (i = 0; i < len; i++)
493 printf("%02x", md[i]);
494}
495
dp-arm4972ec52016-05-25 16:20:20 +0100496static int info_cmd(int argc, char *argv[])
497{
498 image_t *image;
499 uint64_t image_offset;
dp-arm516dfcb2016-11-03 13:59:26 +0000500 uint64_t image_size;
dp-arm4972ec52016-05-25 16:20:20 +0100501 fip_toc_header_t toc_header;
dp-arm4972ec52016-05-25 16:20:20 +0100502
503 if (argc != 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100504 info_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100505 argc--, argv++;
506
507 parse_fip(argv[0], &toc_header);
508
509 if (verbose) {
510 log_dbgx("toc_header[name]: 0x%llX",
511 (unsigned long long)toc_header.name);
512 log_dbgx("toc_header[serial_number]: 0x%llX",
513 (unsigned long long)toc_header.serial_number);
514 log_dbgx("toc_header[flags]: 0x%llX",
515 (unsigned long long)toc_header.flags);
516 }
517
518 image_offset = sizeof(fip_toc_header_t) +
519 (sizeof(fip_toc_entry_t) * (nr_images + 1));
520
dp-arm90d2f0e2016-11-14 15:54:32 +0000521 for (image = image_head; image != NULL; image = image->next) {
522 image_desc_t *desc;
dp-arm715ef422016-08-30 14:18:58 +0100523
dp-arm90d2f0e2016-11-14 15:54:32 +0000524 desc = lookup_image_desc_from_uuid(&image->uuid);
dp-arm516dfcb2016-11-03 13:59:26 +0000525 assert(desc != NULL);
526 printf("%s: ", desc->name);
dp-arm4972ec52016-05-25 16:20:20 +0100527 image_size = image->size;
528 printf("offset=0x%llX, size=0x%llX",
529 (unsigned long long)image_offset,
530 (unsigned long long)image_size);
dp-arm90d2f0e2016-11-14 15:54:32 +0000531 if (desc != NULL)
dp-arm12e893b2016-08-24 13:21:08 +0100532 printf(", cmdline=\"--%s\"",
dp-arm90d2f0e2016-11-14 15:54:32 +0000533 desc->cmdline_name);
dp-arm12e893b2016-08-24 13:21:08 +0100534 if (verbose) {
535 unsigned char md[SHA256_DIGEST_LENGTH];
536
537 SHA256(image->buffer, image_size, md);
538 printf(", sha256=");
539 md_print(md, sizeof(md));
540 }
541 putchar('\n');
dp-arm4972ec52016-05-25 16:20:20 +0100542 image_offset += image_size;
543 }
544
545 free_images();
546 return 0;
547}
548
549static void info_usage(void)
550{
551 printf("fiptool info FIP_FILENAME\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100552 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100553}
554
dp-arm90d2f0e2016-11-14 15:54:32 +0000555static int pack_images(const char *filename, uint64_t toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100556{
557 FILE *fp;
558 image_t *image;
559 fip_toc_header_t *toc_header;
560 fip_toc_entry_t *toc_entry;
561 char *buf;
562 uint64_t entry_offset, buf_size, payload_size;
dp-arm4972ec52016-05-25 16:20:20 +0100563
564 /* Calculate total payload size and allocate scratch buffer. */
565 payload_size = 0;
dp-arm90d2f0e2016-11-14 15:54:32 +0000566 for (image = image_head; image != NULL; image = image->next)
567 payload_size += image->size;
dp-arm4972ec52016-05-25 16:20:20 +0100568
569 buf_size = sizeof(fip_toc_header_t) +
570 sizeof(fip_toc_entry_t) * (nr_images + 1);
571 buf = calloc(1, buf_size);
572 if (buf == NULL)
573 log_err("calloc");
574
575 /* Build up header and ToC entries from the image table. */
576 toc_header = (fip_toc_header_t *)buf;
577 toc_header->name = TOC_HEADER_NAME;
578 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
579 toc_header->flags = toc_flags;
580
581 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
582
583 entry_offset = buf_size;
dp-arm90d2f0e2016-11-14 15:54:32 +0000584 for (image = image_head; image != NULL; image = image->next) {
dp-arm4972ec52016-05-25 16:20:20 +0100585 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
586 toc_entry->offset_address = entry_offset;
587 toc_entry->size = image->size;
588 toc_entry->flags = 0;
589 entry_offset += toc_entry->size;
590 toc_entry++;
591 }
592
593 /* Append a null uuid entry to mark the end of ToC entries. */
594 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
595 toc_entry->offset_address = entry_offset;
596 toc_entry->size = 0;
597 toc_entry->flags = 0;
598
599 /* Generate the FIP file. */
600 fp = fopen(filename, "w");
601 if (fp == NULL)
602 log_err("fopen %s", filename);
603
604 if (verbose)
605 log_dbgx("Metadata size: %zu bytes", buf_size);
606
607 if (fwrite(buf, 1, buf_size, fp) != buf_size)
608 log_errx("Failed to write image to %s", filename);
609 free(buf);
610
611 if (verbose)
612 log_dbgx("Payload size: %zu bytes", payload_size);
613
dp-arm90d2f0e2016-11-14 15:54:32 +0000614 for (image = image_head; image != NULL; image = image->next)
dp-arm4972ec52016-05-25 16:20:20 +0100615 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
616 log_errx("Failed to write image to %s", filename);
dp-arm4972ec52016-05-25 16:20:20 +0100617
618 fclose(fp);
619 return 0;
620}
621
622/*
623 * This function is shared between the create and update subcommands.
624 * The difference between the two subcommands is that when the FIP file
625 * is created, the parsing of an existing FIP is skipped. This results
626 * in update_fip() creating the new FIP file from scratch because the
627 * internal image table is not populated.
628 */
629static void update_fip(void)
630{
dp-arm90d2f0e2016-11-14 15:54:32 +0000631 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100632
633 /* Add or replace images in the FIP file. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000634 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +0000635 image_t *new_image, *old_image;
636
dp-arm90d2f0e2016-11-14 15:54:32 +0000637 if (desc->action != DO_PACK)
dp-arm4972ec52016-05-25 16:20:20 +0100638 continue;
639
dp-arm90d2f0e2016-11-14 15:54:32 +0000640 new_image = read_image_from_file(&desc->uuid,
641 desc->action_arg);
642 old_image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100643 if (old_image != NULL) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000644 if (verbose) {
dp-arm516dfcb2016-11-03 13:59:26 +0000645 log_dbgx("Replacing %s with %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000646 desc->cmdline_name,
647 desc->action_arg);
648 }
649 remove_image(old_image);
650 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100651 } else {
652 if (verbose)
653 log_dbgx("Adding image %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000654 desc->action_arg);
dp-arm715ef422016-08-30 14:18:58 +0100655 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100656 }
dp-arm4972ec52016-05-25 16:20:20 +0100657 }
658}
659
dp-arm90d2f0e2016-11-14 15:54:32 +0000660static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100661{
662 unsigned long long flags;
663 char *endptr;
664
665 errno = 0;
666 flags = strtoull(arg, &endptr, 16);
667 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
668 log_errx("Invalid platform ToC flags: %s", arg);
669 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
670 *toc_flags |= flags << 32;
671}
672
dp-arm516dfcb2016-11-03 13:59:26 +0000673static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
674{
675 char *p;
676
677 for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
678 if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
679 p += strlen("uuid=");
680 uuid_from_str(uuid, p);
681 } else if (strncmp(p, "file=", strlen("file=")) == 0) {
682 p += strlen("file=");
683 snprintf(filename, len, "%s", p);
684 }
685 }
686}
687
dp-arm4972ec52016-05-25 16:20:20 +0100688static int create_cmd(int argc, char *argv[])
689{
dp-arm90d2f0e2016-11-14 15:54:32 +0000690 struct option *opts = NULL;
691 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100692 unsigned long long toc_flags = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100693
694 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100695 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100696
dp-arm90d2f0e2016-11-14 15:54:32 +0000697 opts = fill_common_opts(opts, &nr_opts, required_argument);
698 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100699 OPT_PLAT_TOC_FLAGS);
dp-arm516dfcb2016-11-03 13:59:26 +0000700 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000701 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100702
703 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000704 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100705
dp-arm516dfcb2016-11-03 13:59:26 +0000706 c = getopt_long(argc, argv, "b:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100707 if (c == -1)
708 break;
709
710 switch (c) {
711 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000712 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100713
dp-arm90d2f0e2016-11-14 15:54:32 +0000714 desc = lookup_image_desc_from_opt(opts[opt_index].name);
715 assert(desc != NULL);
716 if (desc->action != DO_UNSPEC)
717 free(desc->action_arg);
718 desc->action = DO_PACK;
719 desc->action_arg = xstrdup(optarg,
dp-armdb0f5e92016-11-04 10:56:25 +0000720 "failed to allocate memory for argument");
dp-arm4972ec52016-05-25 16:20:20 +0100721 break;
722 }
723 case OPT_PLAT_TOC_FLAGS:
724 parse_plat_toc_flags(optarg, &toc_flags);
725 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000726 case 'b': {
727 char name[_UUID_STR_LEN + 1];
728 char filename[PATH_MAX] = { 0 };
729 uuid_t uuid = { 0 };
730 image_desc_t *desc;
731
732 parse_blob_opt(optarg, &uuid,
733 filename, sizeof(filename));
734
735 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
736 filename[0] == '\0')
737 create_usage();
738
739 desc = lookup_image_desc_from_uuid(&uuid);
740 if (desc != NULL) {
741 if (desc->action != DO_UNSPEC)
742 free(desc->action_arg);
743 desc->action = DO_PACK;
744 desc->action_arg = xstrdup(filename,
745 "failed to allocate memory for argument");
746 } else {
747 uuid_to_str(name, sizeof(name), &uuid);
748 desc = new_image_desc(&uuid, name, "blob");
749 desc->action = DO_PACK;
750 desc->action_arg = xstrdup(filename,
751 "failed to allocate memory for argument");
752 add_image_desc(desc);
753 }
754 break;
755 }
dp-arm4972ec52016-05-25 16:20:20 +0100756 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100757 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100758 }
759 }
760 argc -= optind;
761 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000762 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100763
764 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100765 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100766
767 update_fip();
768
769 pack_images(argv[0], toc_flags);
770 free_images();
771 return 0;
772}
773
774static void create_usage(void)
775{
776 toc_entry_t *toc_entry = toc_entries;
777
dp-arm516dfcb2016-11-03 13:59:26 +0000778 printf("fiptool create [--blob uuid=...,file=...] "
779 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
780 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID "
781 "pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100782 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
783 "occupying bits 32-47 in 64-bit ToC header.\n");
784 fputc('\n', stderr);
785 printf("Specific images are packed with the following options:\n");
786 for (; toc_entry->cmdline_name != NULL; toc_entry++)
787 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
788 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100789 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100790}
791
792static int update_cmd(int argc, char *argv[])
793{
dp-arm90d2f0e2016-11-14 15:54:32 +0000794 struct option *opts = NULL;
795 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000796 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +0100797 fip_toc_header_t toc_header = { 0 };
798 unsigned long long toc_flags = 0;
799 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100800
801 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100802 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100803
dp-arm90d2f0e2016-11-14 15:54:32 +0000804 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000805 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000806 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
807 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100808 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000809 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100810
811 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000812 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100813
dp-arm516dfcb2016-11-03 13:59:26 +0000814 c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100815 if (c == -1)
816 break;
817
818 switch (c) {
819 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000820 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100821
dp-arm90d2f0e2016-11-14 15:54:32 +0000822 desc = lookup_image_desc_from_opt(opts[opt_index].name);
823 assert(desc != NULL);
824 if (desc->action != DO_UNSPEC)
825 free(desc->action_arg);
826 desc->action = DO_PACK;
827 desc->action_arg = xstrdup(optarg,
dp-armdb0f5e92016-11-04 10:56:25 +0000828 "failed to allocate memory for argument");
dp-arm4972ec52016-05-25 16:20:20 +0100829 break;
830 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000831 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100832 parse_plat_toc_flags(optarg, &toc_flags);
833 pflag = 1;
834 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000835 case 'b': {
836 char name[_UUID_STR_LEN + 1];
837 char filename[PATH_MAX] = { 0 };
838 uuid_t uuid = { 0 };
839 image_desc_t *desc;
840
841 parse_blob_opt(optarg, &uuid,
842 filename, sizeof(filename));
843
844 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
845 filename[0] == '\0')
846 update_usage();
847
848 desc = lookup_image_desc_from_uuid(&uuid);
849 if (desc != NULL) {
850 if (desc->action != DO_UNSPEC)
851 free(desc->action_arg);
852 desc->action = DO_PACK;
853 desc->action_arg = xstrdup(filename,
854 "failed to allocate memory for argument");
855 } else {
856 uuid_to_str(name, sizeof(name), &uuid);
857 desc = new_image_desc(&uuid, name, "blob");
858 desc->action = DO_PACK;
859 desc->action_arg = xstrdup(filename,
860 "failed to allocate memory for argument");
861 add_image_desc(desc);
862 }
863 break;
864 }
dp-arm4972ec52016-05-25 16:20:20 +0100865 case 'o':
866 snprintf(outfile, sizeof(outfile), "%s", optarg);
867 break;
868 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100869 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100870 }
871 }
872 argc -= optind;
873 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000874 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100875
876 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100877 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100878
879 if (outfile[0] == '\0')
880 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
881
882 if (access(outfile, F_OK) == 0)
883 parse_fip(argv[0], &toc_header);
884
885 if (pflag)
886 toc_header.flags &= ~(0xffffULL << 32);
887 toc_flags = (toc_header.flags |= toc_flags);
888
889 update_fip();
890
891 pack_images(outfile, toc_flags);
892 free_images();
893 return 0;
894}
895
896static void update_usage(void)
897{
898 toc_entry_t *toc_entry = toc_entries;
899
dp-arm516dfcb2016-11-03 13:59:26 +0000900 printf("fiptool update [--blob uuid=...,file=...] [--out FIP_FILENAME] "
dp-arm4972ec52016-05-25 16:20:20 +0100901 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
dp-arm516dfcb2016-11-03 13:59:26 +0000902 printf(" --blob uuid=...,file=...\tAdd or update an image "
903 "with the given UUID pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100904 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
905 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
906 "occupying bits 32-47 in 64-bit ToC header.\n");
907 fputc('\n', stderr);
908 printf("Specific images are packed with the following options:\n");
909 for (; toc_entry->cmdline_name != NULL; toc_entry++)
910 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
911 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100912 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100913}
914
915static int unpack_cmd(int argc, char *argv[])
916{
dp-arm90d2f0e2016-11-14 15:54:32 +0000917 struct option *opts = NULL;
918 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000919 char outdir[PATH_MAX] = { 0 };
dp-arm90d2f0e2016-11-14 15:54:32 +0000920 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100921 int fflag = 0;
922 int unpack_all = 1;
dp-arm4972ec52016-05-25 16:20:20 +0100923
924 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100925 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100926
dp-arm90d2f0e2016-11-14 15:54:32 +0000927 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000928 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000929 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
930 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
931 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100932
933 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000934 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100935
dp-arm516dfcb2016-11-03 13:59:26 +0000936 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100937 if (c == -1)
938 break;
939
940 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000941 case OPT_TOC_ENTRY: {
942 image_desc_t *desc;
943
944 desc = lookup_image_desc_from_opt(opts[opt_index].name);
945 assert(desc != NULL);
946 if (desc->action != DO_UNSPEC)
947 free(desc->action_arg);
948 desc->action = DO_UNPACK;
949 desc->action_arg = xstrdup(optarg,
dp-armdb0f5e92016-11-04 10:56:25 +0000950 "failed to allocate memory for argument");
dp-arm90d2f0e2016-11-14 15:54:32 +0000951 unpack_all = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100952 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000953 }
dp-arm516dfcb2016-11-03 13:59:26 +0000954 case 'b': {
955 char name[_UUID_STR_LEN + 1];
956 char filename[PATH_MAX] = { 0 };
957 uuid_t uuid = { 0 };
958 image_desc_t *desc;
959
960 parse_blob_opt(optarg, &uuid,
961 filename, sizeof(filename));
962
963 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
964 filename[0] == '\0')
965 unpack_usage();
966
967 desc = lookup_image_desc_from_uuid(&uuid);
968 if (desc != NULL) {
969 if (desc->action != DO_UNSPEC)
970 free(desc->action_arg);
971 desc->action = DO_UNPACK;
972 desc->action_arg = xstrdup(filename,
973 "failed to allocate memory for argument");
974 } else {
975 uuid_to_str(name, sizeof(name), &uuid);
976 desc = new_image_desc(&uuid, name, "blob");
977 desc->action = DO_UNPACK;
978 desc->action_arg = xstrdup(filename,
979 "failed to allocate memory for argument");
980 add_image_desc(desc);
981 }
982 unpack_all = 0;
983 break;
984 }
dp-arm4972ec52016-05-25 16:20:20 +0100985 case 'f':
986 fflag = 1;
987 break;
988 case 'o':
989 snprintf(outdir, sizeof(outdir), "%s", optarg);
990 break;
991 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100992 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100993 }
994 }
995 argc -= optind;
996 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000997 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100998
999 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001000 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001001
1002 parse_fip(argv[0], NULL);
1003
1004 if (outdir[0] != '\0')
1005 if (chdir(outdir) == -1)
1006 log_err("chdir %s", outdir);
1007
dp-arm4972ec52016-05-25 16:20:20 +01001008 /* Unpack all specified images. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001009 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +00001010 char file[PATH_MAX];
dp-arm715ef422016-08-30 14:18:58 +01001011 image_t *image;
1012
dp-arm90d2f0e2016-11-14 15:54:32 +00001013 if (!unpack_all && desc->action != DO_UNPACK)
dp-arm4972ec52016-05-25 16:20:20 +01001014 continue;
1015
1016 /* Build filename. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001017 if (desc->action_arg == NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001018 snprintf(file, sizeof(file), "%s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +00001019 desc->cmdline_name);
dp-arm4972ec52016-05-25 16:20:20 +01001020 else
1021 snprintf(file, sizeof(file), "%s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001022 desc->action_arg);
dp-arm4972ec52016-05-25 16:20:20 +01001023
dp-arm90d2f0e2016-11-14 15:54:32 +00001024 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001025 if (image == NULL) {
1026 if (!unpack_all)
dp-arm516dfcb2016-11-03 13:59:26 +00001027 log_warnx("%s does not exist in %s",
dp-arm715ef422016-08-30 14:18:58 +01001028 file, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001029 continue;
1030 }
1031
1032 if (access(file, F_OK) != 0 || fflag) {
1033 if (verbose)
1034 log_dbgx("Unpacking %s", file);
dp-arm715ef422016-08-30 14:18:58 +01001035 write_image_to_file(image, file);
dp-arm4972ec52016-05-25 16:20:20 +01001036 } else {
1037 log_warnx("File %s already exists, use --force to overwrite it",
1038 file);
1039 }
dp-arm4972ec52016-05-25 16:20:20 +01001040 }
1041
1042 free_images();
1043 return 0;
1044}
1045
1046static void unpack_usage(void)
1047{
1048 toc_entry_t *toc_entry = toc_entries;
1049
dp-arm516dfcb2016-11-03 13:59:26 +00001050 printf("fiptool unpack [--blob uuid=...,file=...] [--force] "
1051 "[--out <path>] [opts] FIP_FILENAME\n");
1052 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID "
1053 "to file.\n");
1054 printf(" --force\t\t\tIf the output file already exists, use --force to "
dp-arm4972ec52016-05-25 16:20:20 +01001055 "overwrite it.\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001056 printf(" --out path\t\t\tSet the output directory path.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001057 fputc('\n', stderr);
1058 printf("Specific images are unpacked with the following options:\n");
1059 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1060 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1061 toc_entry->name);
1062 fputc('\n', stderr);
1063 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001064 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001065}
1066
1067static int remove_cmd(int argc, char *argv[])
1068{
dp-arm90d2f0e2016-11-14 15:54:32 +00001069 struct option *opts = NULL;
1070 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +00001071 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +01001072 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +00001073 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +01001074 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001075
1076 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +01001077 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001078
dp-arm90d2f0e2016-11-14 15:54:32 +00001079 opts = fill_common_opts(opts, &nr_opts, no_argument);
dp-arm516dfcb2016-11-03 13:59:26 +00001080 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +00001081 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1082 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1083 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +01001084
1085 while (1) {
dp-armfe92b892016-11-07 11:13:54 +00001086 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001087
dp-arm516dfcb2016-11-03 13:59:26 +00001088 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +01001089 if (c == -1)
1090 break;
1091
1092 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +00001093 case OPT_TOC_ENTRY: {
1094 image_desc_t *desc;
1095
1096 desc = lookup_image_desc_from_opt(opts[opt_index].name);
1097 assert(desc != NULL);
1098 desc->action = DO_REMOVE;
1099 desc->action_arg = NULL;
dp-arm4972ec52016-05-25 16:20:20 +01001100 break;
dp-arm90d2f0e2016-11-14 15:54:32 +00001101 }
dp-arm516dfcb2016-11-03 13:59:26 +00001102 case 'b': {
1103 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1104 uuid_t uuid = { 0 };
1105 image_desc_t *desc;
1106
1107 parse_blob_opt(optarg, &uuid,
1108 filename, sizeof(filename));
1109
1110 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1111 remove_usage();
1112
1113 desc = lookup_image_desc_from_uuid(&uuid);
1114 if (desc != NULL) {
1115 desc->action = DO_REMOVE;
1116 desc->action_arg = NULL;
1117 } else {
1118 uuid_to_str(name, sizeof(name), &uuid);
1119 desc = new_image_desc(&uuid, name, "blob");
1120 desc->action = DO_REMOVE;
1121 desc->action_arg = NULL;
1122 add_image_desc(desc);
1123 }
1124 break;
1125 }
dp-arm4972ec52016-05-25 16:20:20 +01001126 case 'f':
1127 fflag = 1;
1128 break;
1129 case 'o':
1130 snprintf(outfile, sizeof(outfile), "%s", optarg);
1131 break;
1132 default:
dp-arm29f1b5c2016-09-15 09:58:50 +01001133 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001134 }
1135 }
1136 argc -= optind;
1137 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001138 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001139
1140 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001141 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001142
1143 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1144 log_errx("File %s already exists, use --force to overwrite it",
1145 outfile);
1146
1147 if (outfile[0] == '\0')
1148 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1149
1150 parse_fip(argv[0], &toc_header);
1151
dp-arm90d2f0e2016-11-14 15:54:32 +00001152 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +01001153 image_t *image;
1154
dp-arm90d2f0e2016-11-14 15:54:32 +00001155 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +01001156 continue;
dp-arm516dfcb2016-11-03 13:59:26 +00001157
dp-arm90d2f0e2016-11-14 15:54:32 +00001158 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001159 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +01001160 if (verbose)
dp-arm516dfcb2016-11-03 13:59:26 +00001161 log_dbgx("Removing %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001162 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +01001163 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +01001164 } else {
dp-arm516dfcb2016-11-03 13:59:26 +00001165 log_warnx("%s does not exist in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001166 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001167 }
1168 }
1169
1170 pack_images(outfile, toc_header.flags);
1171 free_images();
1172 return 0;
1173}
1174
1175static void remove_usage(void)
1176{
1177 toc_entry_t *toc_entry = toc_entries;
1178
dp-arm516dfcb2016-11-03 13:59:26 +00001179 printf("fiptool remove [--blob uuid=...] [--force] "
1180 "[--out FIP_FILENAME] [opts] FIP_FILENAME\n");
1181 printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001182 printf(" --force\t\tIf the output FIP file already exists, use --force to "
1183 "overwrite it.\n");
1184 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1185 fputc('\n', stderr);
1186 printf("Specific images are removed with the following options:\n");
1187 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1188 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
1189 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +01001190 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001191}
1192
1193static int version_cmd(int argc, char *argv[])
1194{
1195#ifdef VERSION
1196 puts(VERSION);
1197#else
1198 /* If built from fiptool directory, VERSION is not set. */
1199 puts("Unknown version");
1200#endif
1201 return 0;
1202}
1203
1204static void version_usage(void)
1205{
1206 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001207 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001208}
1209
1210static int help_cmd(int argc, char *argv[])
1211{
1212 int i;
1213
1214 if (argc < 2)
1215 usage();
1216 argc--, argv++;
1217
1218 for (i = 0; i < NELEM(cmds); i++) {
1219 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001220 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001221 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001222 }
1223 if (i == NELEM(cmds))
1224 printf("No help for subcommand '%s'\n", argv[0]);
1225 return 0;
1226}
1227
1228static void usage(void)
1229{
1230 printf("usage: [--verbose] fiptool <command> [<args>]\n");
1231 printf("Global options supported:\n");
1232 printf(" --verbose\tEnable verbose output for all commands.\n");
1233 fputc('\n', stderr);
1234 printf("Commands supported:\n");
1235 printf(" info\t\tList images contained in FIP.\n");
1236 printf(" create\tCreate a new FIP with the given images.\n");
1237 printf(" update\tUpdate an existing FIP with the given images.\n");
1238 printf(" unpack\tUnpack images from FIP.\n");
1239 printf(" remove\tRemove images from FIP.\n");
1240 printf(" version\tShow fiptool version.\n");
1241 printf(" help\t\tShow help for given command.\n");
1242 exit(1);
1243}
1244
1245int main(int argc, char *argv[])
1246{
1247 int i, ret = 0;
1248
dp-arm5cd10ae2016-11-07 10:45:59 +00001249 while (1) {
1250 int c, opt_index = 0;
1251 static struct option opts[] = {
1252 { "verbose", no_argument, NULL, 'v' },
1253 { NULL, no_argument, NULL, 0 }
1254 };
1255
1256 /*
1257 * Set POSIX mode so getopt stops at the first non-option
1258 * which is the subcommand.
1259 */
1260 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1261 if (c == -1)
1262 break;
dp-arm4972ec52016-05-25 16:20:20 +01001263
dp-arm5cd10ae2016-11-07 10:45:59 +00001264 switch (c) {
1265 case 'v':
1266 verbose = 1;
1267 break;
1268 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001269 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001270 }
dp-arm4972ec52016-05-25 16:20:20 +01001271 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001272 argc -= optind;
1273 argv += optind;
1274 /* Reset optind for subsequent getopt processing. */
1275 optind = 0;
1276
1277 if (argc == 0)
1278 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001279
dp-arm90d2f0e2016-11-14 15:54:32 +00001280 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001281 for (i = 0; i < NELEM(cmds); i++) {
1282 if (strcmp(cmds[i].name, argv[0]) == 0) {
1283 ret = cmds[i].handler(argc, argv);
1284 break;
1285 }
1286 }
1287 if (i == NELEM(cmds))
1288 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001289 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001290 return ret;
1291}