blob: 70921ce3e446c9adc1661621a29c17083a4e002c [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-armc1f8e772016-11-04 10:52:25 +0000296static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
dp-arm4972ec52016-05-25 16:20:20 +0100297{
298 struct stat st;
299 FILE *fp;
300 char *buf, *bufend;
301 fip_toc_header_t *toc_header;
302 fip_toc_entry_t *toc_entry;
303 image_t *image;
304 int terminated = 0;
305
306 fp = fopen(filename, "r");
307 if (fp == NULL)
308 log_err("fopen %s", filename);
309
310 if (fstat(fileno(fp), &st) == -1)
311 log_err("fstat %s", filename);
312
dp-armdb0f5e92016-11-04 10:56:25 +0000313 buf = xmalloc(st.st_size, "failed to load file into memory");
dp-arm4972ec52016-05-25 16:20:20 +0100314 if (fread(buf, 1, st.st_size, fp) != st.st_size)
315 log_errx("Failed to read %s", filename);
316 bufend = buf + st.st_size;
317 fclose(fp);
318
319 if (st.st_size < sizeof(fip_toc_header_t))
320 log_errx("FIP %s is truncated", filename);
321
322 toc_header = (fip_toc_header_t *)buf;
323 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
324
325 if (toc_header->name != TOC_HEADER_NAME)
326 log_errx("%s is not a FIP file", filename);
327
328 /* Return the ToC header if the caller wants it. */
329 if (toc_header_out != NULL)
330 *toc_header_out = *toc_header;
331
332 /* Walk through each ToC entry in the file. */
333 while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
334 /* Found the ToC terminator, we are done. */
335 if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
336 terminated = 1;
337 break;
338 }
339
340 /*
341 * Build a new image out of the ToC entry and add it to the
342 * table of images.
343 */
dp-armdb0f5e92016-11-04 10:56:25 +0000344 image = xmalloc(sizeof(*image),
345 "failed to allocate memory for image");
dp-arm4972ec52016-05-25 16:20:20 +0100346 memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000347 image->buffer = xmalloc(toc_entry->size,
348 "failed to allocate image buffer, is FIP file corrupted?");
dp-arm4972ec52016-05-25 16:20:20 +0100349 /* Overflow checks before memory copy. */
350 if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
351 log_errx("FIP %s is corrupted", filename);
352 if (toc_entry->size + toc_entry->offset_address > st.st_size)
353 log_errx("FIP %s is corrupted", filename);
354
355 memcpy(image->buffer, buf + toc_entry->offset_address,
356 toc_entry->size);
357 image->size = toc_entry->size;
358
dp-arm4972ec52016-05-25 16:20:20 +0100359 add_image(image);
360
361 toc_entry++;
362 }
363
364 if (terminated == 0)
365 log_errx("FIP %s does not have a ToC terminator entry",
366 filename);
367 free(buf);
368 return 0;
369}
370
dp-armc1f8e772016-11-04 10:52:25 +0000371static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100372{
373 struct stat st;
374 image_t *image;
375 FILE *fp;
376
dp-arm715ef422016-08-30 14:18:58 +0100377 assert(uuid != NULL);
378
dp-arm4972ec52016-05-25 16:20:20 +0100379 fp = fopen(filename, "r");
380 if (fp == NULL)
381 log_err("fopen %s", filename);
382
383 if (fstat(fileno(fp), &st) == -1)
384 log_errx("fstat %s", filename);
385
dp-armdb0f5e92016-11-04 10:56:25 +0000386 image = xmalloc(sizeof(*image), "failed to allocate memory for image");
dp-arm715ef422016-08-30 14:18:58 +0100387 memcpy(&image->uuid, uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000388 image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
dp-arm4972ec52016-05-25 16:20:20 +0100389 if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
390 log_errx("Failed to read %s", filename);
391 image->size = st.st_size;
dp-arm4972ec52016-05-25 16:20:20 +0100392
393 fclose(fp);
394 return image;
395}
396
dp-armc1f8e772016-11-04 10:52:25 +0000397static int write_image_to_file(const image_t *image, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100398{
399 FILE *fp;
400
401 fp = fopen(filename, "w");
402 if (fp == NULL)
403 log_err("fopen");
404 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
405 log_errx("Failed to write %s", filename);
406 fclose(fp);
407 return 0;
408}
409
dp-arm90d2f0e2016-11-14 15:54:32 +0000410static struct option *add_opt(struct option *opts, size_t *nr_opts,
411 const char *name, int has_arg, int val)
dp-arm4972ec52016-05-25 16:20:20 +0100412{
dp-arm90d2f0e2016-11-14 15:54:32 +0000413 opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
414 if (opts == NULL)
415 log_err("realloc");
416 opts[*nr_opts].name = name;
417 opts[*nr_opts].has_arg = has_arg;
418 opts[*nr_opts].flag = NULL;
419 opts[*nr_opts].val = val;
420 ++*nr_opts;
421 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100422}
423
dp-arm90d2f0e2016-11-14 15:54:32 +0000424static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
425 int has_arg)
dp-arm4972ec52016-05-25 16:20:20 +0100426{
dp-arm90d2f0e2016-11-14 15:54:32 +0000427 image_desc_t *desc;
428
429 for (desc = image_desc_head; desc != NULL; desc = desc->next)
430 opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
431 OPT_TOC_ENTRY);
432 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100433}
434
dp-arm90d2f0e2016-11-14 15:54:32 +0000435static void md_print(const unsigned char *md, size_t len)
dp-arm12e893b2016-08-24 13:21:08 +0100436{
437 size_t i;
438
439 for (i = 0; i < len; i++)
440 printf("%02x", md[i]);
441}
442
dp-arm4972ec52016-05-25 16:20:20 +0100443static int info_cmd(int argc, char *argv[])
444{
445 image_t *image;
446 uint64_t image_offset;
447 uint64_t image_size = 0;
448 fip_toc_header_t toc_header;
dp-arm4972ec52016-05-25 16:20:20 +0100449
450 if (argc != 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100451 info_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100452 argc--, argv++;
453
454 parse_fip(argv[0], &toc_header);
455
456 if (verbose) {
457 log_dbgx("toc_header[name]: 0x%llX",
458 (unsigned long long)toc_header.name);
459 log_dbgx("toc_header[serial_number]: 0x%llX",
460 (unsigned long long)toc_header.serial_number);
461 log_dbgx("toc_header[flags]: 0x%llX",
462 (unsigned long long)toc_header.flags);
463 }
464
465 image_offset = sizeof(fip_toc_header_t) +
466 (sizeof(fip_toc_entry_t) * (nr_images + 1));
467
dp-arm90d2f0e2016-11-14 15:54:32 +0000468 for (image = image_head; image != NULL; image = image->next) {
469 image_desc_t *desc;
dp-arm715ef422016-08-30 14:18:58 +0100470
dp-arm90d2f0e2016-11-14 15:54:32 +0000471 desc = lookup_image_desc_from_uuid(&image->uuid);
472 if (desc != NULL)
473 printf("%s: ", desc->name);
dp-arm4972ec52016-05-25 16:20:20 +0100474 else
475 printf("Unknown entry: ");
476 image_size = image->size;
477 printf("offset=0x%llX, size=0x%llX",
478 (unsigned long long)image_offset,
479 (unsigned long long)image_size);
dp-arm90d2f0e2016-11-14 15:54:32 +0000480 if (desc != NULL)
dp-arm12e893b2016-08-24 13:21:08 +0100481 printf(", cmdline=\"--%s\"",
dp-arm90d2f0e2016-11-14 15:54:32 +0000482 desc->cmdline_name);
dp-arm12e893b2016-08-24 13:21:08 +0100483 if (verbose) {
484 unsigned char md[SHA256_DIGEST_LENGTH];
485
486 SHA256(image->buffer, image_size, md);
487 printf(", sha256=");
488 md_print(md, sizeof(md));
489 }
490 putchar('\n');
dp-arm4972ec52016-05-25 16:20:20 +0100491 image_offset += image_size;
492 }
493
494 free_images();
495 return 0;
496}
497
498static void info_usage(void)
499{
500 printf("fiptool info FIP_FILENAME\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100501 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100502}
503
dp-arm90d2f0e2016-11-14 15:54:32 +0000504static int pack_images(const char *filename, uint64_t toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100505{
506 FILE *fp;
507 image_t *image;
508 fip_toc_header_t *toc_header;
509 fip_toc_entry_t *toc_entry;
510 char *buf;
511 uint64_t entry_offset, buf_size, payload_size;
dp-arm4972ec52016-05-25 16:20:20 +0100512
513 /* Calculate total payload size and allocate scratch buffer. */
514 payload_size = 0;
dp-arm90d2f0e2016-11-14 15:54:32 +0000515 for (image = image_head; image != NULL; image = image->next)
516 payload_size += image->size;
dp-arm4972ec52016-05-25 16:20:20 +0100517
518 buf_size = sizeof(fip_toc_header_t) +
519 sizeof(fip_toc_entry_t) * (nr_images + 1);
520 buf = calloc(1, buf_size);
521 if (buf == NULL)
522 log_err("calloc");
523
524 /* Build up header and ToC entries from the image table. */
525 toc_header = (fip_toc_header_t *)buf;
526 toc_header->name = TOC_HEADER_NAME;
527 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
528 toc_header->flags = toc_flags;
529
530 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
531
532 entry_offset = buf_size;
dp-arm90d2f0e2016-11-14 15:54:32 +0000533 for (image = image_head; image != NULL; image = image->next) {
dp-arm4972ec52016-05-25 16:20:20 +0100534 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
535 toc_entry->offset_address = entry_offset;
536 toc_entry->size = image->size;
537 toc_entry->flags = 0;
538 entry_offset += toc_entry->size;
539 toc_entry++;
540 }
541
542 /* Append a null uuid entry to mark the end of ToC entries. */
543 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
544 toc_entry->offset_address = entry_offset;
545 toc_entry->size = 0;
546 toc_entry->flags = 0;
547
548 /* Generate the FIP file. */
549 fp = fopen(filename, "w");
550 if (fp == NULL)
551 log_err("fopen %s", filename);
552
553 if (verbose)
554 log_dbgx("Metadata size: %zu bytes", buf_size);
555
556 if (fwrite(buf, 1, buf_size, fp) != buf_size)
557 log_errx("Failed to write image to %s", filename);
558 free(buf);
559
560 if (verbose)
561 log_dbgx("Payload size: %zu bytes", payload_size);
562
dp-arm90d2f0e2016-11-14 15:54:32 +0000563 for (image = image_head; image != NULL; image = image->next)
dp-arm4972ec52016-05-25 16:20:20 +0100564 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
565 log_errx("Failed to write image to %s", filename);
dp-arm4972ec52016-05-25 16:20:20 +0100566
567 fclose(fp);
568 return 0;
569}
570
571/*
572 * This function is shared between the create and update subcommands.
573 * The difference between the two subcommands is that when the FIP file
574 * is created, the parsing of an existing FIP is skipped. This results
575 * in update_fip() creating the new FIP file from scratch because the
576 * internal image table is not populated.
577 */
578static void update_fip(void)
579{
dp-arm90d2f0e2016-11-14 15:54:32 +0000580 image_desc_t *desc;
dp-arm715ef422016-08-30 14:18:58 +0100581 image_t *new_image, *old_image;
dp-arm4972ec52016-05-25 16:20:20 +0100582
583 /* Add or replace images in the FIP file. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000584 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
585 if (desc->action != DO_PACK)
dp-arm4972ec52016-05-25 16:20:20 +0100586 continue;
587
dp-arm90d2f0e2016-11-14 15:54:32 +0000588 new_image = read_image_from_file(&desc->uuid,
589 desc->action_arg);
590 old_image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100591 if (old_image != NULL) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000592 if (verbose) {
dp-arm4972ec52016-05-25 16:20:20 +0100593 log_dbgx("Replacing image %s.bin with %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000594 desc->cmdline_name,
595 desc->action_arg);
596 }
597 remove_image(old_image);
598 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100599 } else {
600 if (verbose)
601 log_dbgx("Adding image %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000602 desc->action_arg);
dp-arm715ef422016-08-30 14:18:58 +0100603 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100604 }
dp-arm4972ec52016-05-25 16:20:20 +0100605 }
606}
607
dp-arm90d2f0e2016-11-14 15:54:32 +0000608static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100609{
610 unsigned long long flags;
611 char *endptr;
612
613 errno = 0;
614 flags = strtoull(arg, &endptr, 16);
615 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
616 log_errx("Invalid platform ToC flags: %s", arg);
617 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
618 *toc_flags |= flags << 32;
619}
620
621static int create_cmd(int argc, char *argv[])
622{
dp-arm90d2f0e2016-11-14 15:54:32 +0000623 struct option *opts = NULL;
624 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100625 unsigned long long toc_flags = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100626
627 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100628 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100629
dp-arm90d2f0e2016-11-14 15:54:32 +0000630 opts = fill_common_opts(opts, &nr_opts, required_argument);
631 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100632 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000633 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100634
635 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000636 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100637
dp-armc1f8e772016-11-04 10:52:25 +0000638 c = getopt_long(argc, argv, "", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100639 if (c == -1)
640 break;
641
642 switch (c) {
643 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000644 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100645
dp-arm90d2f0e2016-11-14 15:54:32 +0000646 desc = lookup_image_desc_from_opt(opts[opt_index].name);
647 assert(desc != NULL);
648 if (desc->action != DO_UNSPEC)
649 free(desc->action_arg);
650 desc->action = DO_PACK;
651 desc->action_arg = xstrdup(optarg,
dp-armdb0f5e92016-11-04 10:56:25 +0000652 "failed to allocate memory for argument");
dp-arm4972ec52016-05-25 16:20:20 +0100653 break;
654 }
655 case OPT_PLAT_TOC_FLAGS:
656 parse_plat_toc_flags(optarg, &toc_flags);
657 break;
658 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100659 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100660 }
661 }
662 argc -= optind;
663 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000664 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100665
666 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100667 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100668
669 update_fip();
670
671 pack_images(argv[0], toc_flags);
672 free_images();
673 return 0;
674}
675
676static void create_usage(void)
677{
678 toc_entry_t *toc_entry = toc_entries;
679
dp-arm1b434b02016-08-23 14:31:41 +0100680 printf("fiptool create [--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
dp-arm4972ec52016-05-25 16:20:20 +0100681 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
682 "occupying bits 32-47 in 64-bit ToC header.\n");
683 fputc('\n', stderr);
684 printf("Specific images are packed with the following options:\n");
685 for (; toc_entry->cmdline_name != NULL; toc_entry++)
686 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
687 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100688 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100689}
690
691static int update_cmd(int argc, char *argv[])
692{
dp-arm90d2f0e2016-11-14 15:54:32 +0000693 struct option *opts = NULL;
694 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100695 char outfile[FILENAME_MAX] = { 0 };
696 fip_toc_header_t toc_header = { 0 };
697 unsigned long long toc_flags = 0;
698 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100699
700 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100701 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100702
dp-arm90d2f0e2016-11-14 15:54:32 +0000703 opts = fill_common_opts(opts, &nr_opts, required_argument);
704 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
705 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100706 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000707 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100708
709 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000710 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100711
712 c = getopt_long(argc, argv, "o:", opts, &opt_index);
713 if (c == -1)
714 break;
715
716 switch (c) {
717 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000718 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100719
dp-arm90d2f0e2016-11-14 15:54:32 +0000720 desc = lookup_image_desc_from_opt(opts[opt_index].name);
721 assert(desc != NULL);
722 if (desc->action != DO_UNSPEC)
723 free(desc->action_arg);
724 desc->action = DO_PACK;
725 desc->action_arg = xstrdup(optarg,
dp-armdb0f5e92016-11-04 10:56:25 +0000726 "failed to allocate memory for argument");
dp-arm4972ec52016-05-25 16:20:20 +0100727 break;
728 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000729 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100730 parse_plat_toc_flags(optarg, &toc_flags);
731 pflag = 1;
732 break;
dp-arm4972ec52016-05-25 16:20:20 +0100733 case 'o':
734 snprintf(outfile, sizeof(outfile), "%s", optarg);
735 break;
736 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100737 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100738 }
739 }
740 argc -= optind;
741 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000742 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100743
744 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100745 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100746
747 if (outfile[0] == '\0')
748 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
749
750 if (access(outfile, F_OK) == 0)
751 parse_fip(argv[0], &toc_header);
752
753 if (pflag)
754 toc_header.flags &= ~(0xffffULL << 32);
755 toc_flags = (toc_header.flags |= toc_flags);
756
757 update_fip();
758
759 pack_images(outfile, toc_flags);
760 free_images();
761 return 0;
762}
763
764static void update_usage(void)
765{
766 toc_entry_t *toc_entry = toc_entries;
767
dp-arm1b434b02016-08-23 14:31:41 +0100768 printf("fiptool update [--out FIP_FILENAME] "
dp-arm4972ec52016-05-25 16:20:20 +0100769 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
770 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
771 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
772 "occupying bits 32-47 in 64-bit ToC header.\n");
773 fputc('\n', stderr);
774 printf("Specific images are packed with the following options:\n");
775 for (; toc_entry->cmdline_name != NULL; toc_entry++)
776 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
777 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100778 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100779}
780
781static int unpack_cmd(int argc, char *argv[])
782{
dp-arm90d2f0e2016-11-14 15:54:32 +0000783 struct option *opts = NULL;
784 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100785 char file[FILENAME_MAX], outdir[PATH_MAX] = { 0 };
dp-arm90d2f0e2016-11-14 15:54:32 +0000786 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100787 int fflag = 0;
788 int unpack_all = 1;
dp-arm4972ec52016-05-25 16:20:20 +0100789
790 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100791 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100792
dp-arm90d2f0e2016-11-14 15:54:32 +0000793 opts = fill_common_opts(opts, &nr_opts, required_argument);
794 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
795 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
796 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100797
798 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000799 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100800
801 c = getopt_long(argc, argv, "fo:", opts, &opt_index);
802 if (c == -1)
803 break;
804
805 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000806 case OPT_TOC_ENTRY: {
807 image_desc_t *desc;
808
809 desc = lookup_image_desc_from_opt(opts[opt_index].name);
810 assert(desc != NULL);
811 if (desc->action != DO_UNSPEC)
812 free(desc->action_arg);
813 desc->action = DO_UNPACK;
814 desc->action_arg = xstrdup(optarg,
dp-armdb0f5e92016-11-04 10:56:25 +0000815 "failed to allocate memory for argument");
dp-arm90d2f0e2016-11-14 15:54:32 +0000816 unpack_all = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100817 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000818 }
dp-arm4972ec52016-05-25 16:20:20 +0100819 case 'f':
820 fflag = 1;
821 break;
822 case 'o':
823 snprintf(outdir, sizeof(outdir), "%s", optarg);
824 break;
825 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100826 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100827 }
828 }
829 argc -= optind;
830 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000831 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100832
833 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100834 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100835
836 parse_fip(argv[0], NULL);
837
838 if (outdir[0] != '\0')
839 if (chdir(outdir) == -1)
840 log_err("chdir %s", outdir);
841
dp-arm4972ec52016-05-25 16:20:20 +0100842 /* Unpack all specified images. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000843 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +0100844 image_t *image;
845
dp-arm90d2f0e2016-11-14 15:54:32 +0000846 if (!unpack_all && desc->action != DO_UNPACK)
dp-arm4972ec52016-05-25 16:20:20 +0100847 continue;
848
849 /* Build filename. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000850 if (desc->action_arg == NULL)
dp-arm4972ec52016-05-25 16:20:20 +0100851 snprintf(file, sizeof(file), "%s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +0000852 desc->cmdline_name);
dp-arm4972ec52016-05-25 16:20:20 +0100853 else
854 snprintf(file, sizeof(file), "%s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000855 desc->action_arg);
dp-arm4972ec52016-05-25 16:20:20 +0100856
dp-arm90d2f0e2016-11-14 15:54:32 +0000857 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100858 if (image == NULL) {
859 if (!unpack_all)
860 log_warnx("Requested image %s is not in %s",
861 file, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +0100862 continue;
863 }
864
865 if (access(file, F_OK) != 0 || fflag) {
866 if (verbose)
867 log_dbgx("Unpacking %s", file);
dp-arm715ef422016-08-30 14:18:58 +0100868 write_image_to_file(image, file);
dp-arm4972ec52016-05-25 16:20:20 +0100869 } else {
870 log_warnx("File %s already exists, use --force to overwrite it",
871 file);
872 }
dp-arm4972ec52016-05-25 16:20:20 +0100873 }
874
875 free_images();
876 return 0;
877}
878
879static void unpack_usage(void)
880{
881 toc_entry_t *toc_entry = toc_entries;
882
883 printf("fiptool unpack [--force] [--out <path>] [opts] FIP_FILENAME\n");
884 printf(" --force\tIf the output file already exists, use --force to "
885 "overwrite it.\n");
886 printf(" --out path\tSet the output directory path.\n");
887 fputc('\n', stderr);
888 printf("Specific images are unpacked with the following options:\n");
889 for (; toc_entry->cmdline_name != NULL; toc_entry++)
890 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
891 toc_entry->name);
892 fputc('\n', stderr);
893 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100894 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100895}
896
897static int remove_cmd(int argc, char *argv[])
898{
dp-arm90d2f0e2016-11-14 15:54:32 +0000899 struct option *opts = NULL;
900 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100901 char outfile[FILENAME_MAX] = { 0 };
902 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +0000903 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100904 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100905
906 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100907 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100908
dp-arm90d2f0e2016-11-14 15:54:32 +0000909 opts = fill_common_opts(opts, &nr_opts, no_argument);
910 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
911 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
912 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100913
914 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000915 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100916
917 c = getopt_long(argc, argv, "fo:", opts, &opt_index);
918 if (c == -1)
919 break;
920
921 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000922 case OPT_TOC_ENTRY: {
923 image_desc_t *desc;
924
925 desc = lookup_image_desc_from_opt(opts[opt_index].name);
926 assert(desc != NULL);
927 desc->action = DO_REMOVE;
928 desc->action_arg = NULL;
dp-arm4972ec52016-05-25 16:20:20 +0100929 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000930 }
dp-arm4972ec52016-05-25 16:20:20 +0100931 case 'f':
932 fflag = 1;
933 break;
934 case 'o':
935 snprintf(outfile, sizeof(outfile), "%s", optarg);
936 break;
937 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100938 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100939 }
940 }
941 argc -= optind;
942 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000943 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100944
945 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100946 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100947
948 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
949 log_errx("File %s already exists, use --force to overwrite it",
950 outfile);
951
952 if (outfile[0] == '\0')
953 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
954
955 parse_fip(argv[0], &toc_header);
956
dp-arm90d2f0e2016-11-14 15:54:32 +0000957 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +0100958 image_t *image;
959
dp-arm90d2f0e2016-11-14 15:54:32 +0000960 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +0100961 continue;
dp-arm90d2f0e2016-11-14 15:54:32 +0000962 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100963 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +0100964 if (verbose)
965 log_dbgx("Removing %s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +0000966 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +0100967 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +0100968 } else {
969 log_warnx("Requested image %s.bin is not in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000970 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +0100971 }
972 }
973
974 pack_images(outfile, toc_header.flags);
975 free_images();
976 return 0;
977}
978
979static void remove_usage(void)
980{
981 toc_entry_t *toc_entry = toc_entries;
982
983 printf("fiptool remove [--force] [--out FIP_FILENAME] [opts] FIP_FILENAME\n");
984 printf(" --force\t\tIf the output FIP file already exists, use --force to "
985 "overwrite it.\n");
986 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
987 fputc('\n', stderr);
988 printf("Specific images are removed with the following options:\n");
989 for (; toc_entry->cmdline_name != NULL; toc_entry++)
990 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
991 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100992 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100993}
994
995static int version_cmd(int argc, char *argv[])
996{
997#ifdef VERSION
998 puts(VERSION);
999#else
1000 /* If built from fiptool directory, VERSION is not set. */
1001 puts("Unknown version");
1002#endif
1003 return 0;
1004}
1005
1006static void version_usage(void)
1007{
1008 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001009 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001010}
1011
1012static int help_cmd(int argc, char *argv[])
1013{
1014 int i;
1015
1016 if (argc < 2)
1017 usage();
1018 argc--, argv++;
1019
1020 for (i = 0; i < NELEM(cmds); i++) {
1021 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001022 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001023 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001024 }
1025 if (i == NELEM(cmds))
1026 printf("No help for subcommand '%s'\n", argv[0]);
1027 return 0;
1028}
1029
1030static void usage(void)
1031{
1032 printf("usage: [--verbose] fiptool <command> [<args>]\n");
1033 printf("Global options supported:\n");
1034 printf(" --verbose\tEnable verbose output for all commands.\n");
1035 fputc('\n', stderr);
1036 printf("Commands supported:\n");
1037 printf(" info\t\tList images contained in FIP.\n");
1038 printf(" create\tCreate a new FIP with the given images.\n");
1039 printf(" update\tUpdate an existing FIP with the given images.\n");
1040 printf(" unpack\tUnpack images from FIP.\n");
1041 printf(" remove\tRemove images from FIP.\n");
1042 printf(" version\tShow fiptool version.\n");
1043 printf(" help\t\tShow help for given command.\n");
1044 exit(1);
1045}
1046
1047int main(int argc, char *argv[])
1048{
1049 int i, ret = 0;
1050
dp-arm5cd10ae2016-11-07 10:45:59 +00001051 while (1) {
1052 int c, opt_index = 0;
1053 static struct option opts[] = {
1054 { "verbose", no_argument, NULL, 'v' },
1055 { NULL, no_argument, NULL, 0 }
1056 };
1057
1058 /*
1059 * Set POSIX mode so getopt stops at the first non-option
1060 * which is the subcommand.
1061 */
1062 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1063 if (c == -1)
1064 break;
dp-arm4972ec52016-05-25 16:20:20 +01001065
dp-arm5cd10ae2016-11-07 10:45:59 +00001066 switch (c) {
1067 case 'v':
1068 verbose = 1;
1069 break;
1070 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001071 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001072 }
dp-arm4972ec52016-05-25 16:20:20 +01001073 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001074 argc -= optind;
1075 argv += optind;
1076 /* Reset optind for subsequent getopt processing. */
1077 optind = 0;
1078
1079 if (argc == 0)
1080 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001081
dp-arm90d2f0e2016-11-14 15:54:32 +00001082 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001083 for (i = 0; i < NELEM(cmds); i++) {
1084 if (strcmp(cmds[i].name, argv[0]) == 0) {
1085 ret = cmds[i].handler(argc, argv);
1086 break;
1087 }
1088 }
1089 if (i == NELEM(cmds))
1090 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001091 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001092 return ret;
1093}