blob: 46b872679414aca733d1ef36e880eb9c6b3723ba [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
257static void free_image(image_t *image)
258{
259 free(image->buffer);
260 free(image);
261}
262
dp-arm4972ec52016-05-25 16:20:20 +0100263static void remove_image(image_t *image)
264{
dp-arm90d2f0e2016-11-14 15:54:32 +0000265 image_t *tmp = image_head, *prev;
dp-arm4972ec52016-05-25 16:20:20 +0100266
dp-arm90d2f0e2016-11-14 15:54:32 +0000267 if (tmp == image) {
268 image_head = tmp->next;
269 free_image(tmp);
270 } else {
271 while (tmp != NULL && tmp != image) {
272 prev = tmp;
273 tmp = tmp->next;
dp-arm4972ec52016-05-25 16:20:20 +0100274 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000275 assert(tmp != NULL);
276 prev->next = tmp->next;
277 free_image(tmp);
dp-arm4972ec52016-05-25 16:20:20 +0100278 }
dp-arm4972ec52016-05-25 16:20:20 +0100279 nr_images--;
280}
281
282static void free_images(void)
283{
dp-arm90d2f0e2016-11-14 15:54:32 +0000284 image_t *image = image_head, *tmp;
dp-arm4972ec52016-05-25 16:20:20 +0100285
dp-arm90d2f0e2016-11-14 15:54:32 +0000286 while (image != NULL) {
287 tmp = image->next;
288 free_image(image);
289 image = tmp;
290 nr_images--;
dp-arm4972ec52016-05-25 16:20:20 +0100291 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000292 assert(nr_images == 0);
dp-arm4972ec52016-05-25 16:20:20 +0100293}
294
dp-arm90d2f0e2016-11-14 15:54:32 +0000295static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid)
dp-arm4972ec52016-05-25 16:20:20 +0100296{
dp-arm90d2f0e2016-11-14 15:54:32 +0000297 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100298
dp-arm90d2f0e2016-11-14 15:54:32 +0000299 for (desc = image_desc_head; desc != NULL; desc = desc->next)
300 if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0)
301 return desc;
dp-arm4972ec52016-05-25 16:20:20 +0100302 return NULL;
303}
304
dp-arm90d2f0e2016-11-14 15:54:32 +0000305static image_desc_t *lookup_image_desc_from_opt(const char *opt)
306{
307 image_desc_t *desc;
308
309 for (desc = image_desc_head; desc != NULL; desc = desc->next)
310 if (strcmp(desc->cmdline_name, opt) == 0)
311 return desc;
312 return NULL;
313}
314
315static image_t *lookup_image_from_uuid(const uuid_t *uuid)
dp-arm715ef422016-08-30 14:18:58 +0100316{
317 image_t *image;
dp-arm715ef422016-08-30 14:18:58 +0100318
dp-arm90d2f0e2016-11-14 15:54:32 +0000319 for (image = image_head; image != NULL; image = image->next)
dp-arm715ef422016-08-30 14:18:58 +0100320 if (memcmp(&image->uuid, uuid, sizeof(uuid_t)) == 0)
321 return image;
dp-arm715ef422016-08-30 14:18:58 +0100322 return NULL;
323}
324
dp-arm516dfcb2016-11-03 13:59:26 +0000325static void uuid_to_str(char *s, size_t len, const uuid_t *u)
326{
327 assert(len >= (_UUID_STR_LEN + 1));
328
329 snprintf(s, len, "%08X-%04X-%04X-%04X-%04X%04X%04X",
330 u->time_low,
331 u->time_mid,
332 u->time_hi_and_version,
333 ((uint16_t)u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low,
334 ((uint16_t)u->node[0] << 8) | u->node[1],
335 ((uint16_t)u->node[2] << 8) | u->node[3],
336 ((uint16_t)u->node[4] << 8) | u->node[5]);
337}
338
339static void uuid_from_str(uuid_t *u, const char *s)
340{
341 int n;
342
343 if (s == NULL)
344 log_errx("UUID cannot be NULL");
345 if (strlen(s) != _UUID_STR_LEN)
346 log_errx("Invalid UUID: %s", s);
347
348 n = sscanf(s,
349 "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
350 &u->time_low, &u->time_mid, &u->time_hi_and_version,
351 &u->clock_seq_hi_and_reserved, &u->clock_seq_low, &u->node[0],
352 &u->node[1], &u->node[2], &u->node[3], &u->node[4], &u->node[5]);
353 /*
354 * Given the format specifier above, we expect 11 items to be scanned
355 * for a properly formatted UUID.
356 */
357 if (n != 11)
358 log_errx("Invalid UUID: %s", s);
359}
360
dp-armc1f8e772016-11-04 10:52:25 +0000361static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out)
dp-arm4972ec52016-05-25 16:20:20 +0100362{
363 struct stat st;
364 FILE *fp;
365 char *buf, *bufend;
366 fip_toc_header_t *toc_header;
367 fip_toc_entry_t *toc_entry;
dp-arm4972ec52016-05-25 16:20:20 +0100368 int terminated = 0;
369
370 fp = fopen(filename, "r");
371 if (fp == NULL)
372 log_err("fopen %s", filename);
373
374 if (fstat(fileno(fp), &st) == -1)
375 log_err("fstat %s", filename);
376
dp-armdb0f5e92016-11-04 10:56:25 +0000377 buf = xmalloc(st.st_size, "failed to load file into memory");
dp-arm4972ec52016-05-25 16:20:20 +0100378 if (fread(buf, 1, st.st_size, fp) != st.st_size)
379 log_errx("Failed to read %s", filename);
380 bufend = buf + st.st_size;
381 fclose(fp);
382
383 if (st.st_size < sizeof(fip_toc_header_t))
384 log_errx("FIP %s is truncated", filename);
385
386 toc_header = (fip_toc_header_t *)buf;
387 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
388
389 if (toc_header->name != TOC_HEADER_NAME)
390 log_errx("%s is not a FIP file", filename);
391
392 /* Return the ToC header if the caller wants it. */
393 if (toc_header_out != NULL)
394 *toc_header_out = *toc_header;
395
396 /* Walk through each ToC entry in the file. */
397 while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) {
dp-arm516dfcb2016-11-03 13:59:26 +0000398 image_t *image;
399 image_desc_t *desc;
400
dp-arm4972ec52016-05-25 16:20:20 +0100401 /* Found the ToC terminator, we are done. */
402 if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) {
403 terminated = 1;
404 break;
405 }
406
407 /*
408 * Build a new image out of the ToC entry and add it to the
409 * table of images.
410 */
Masahiro Yamadad224b452017-01-14 23:22:02 +0900411 image = xzalloc(sizeof(*image),
dp-armdb0f5e92016-11-04 10:56:25 +0000412 "failed to allocate memory for image");
dp-arm4972ec52016-05-25 16:20:20 +0100413 memcpy(&image->uuid, &toc_entry->uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000414 image->buffer = xmalloc(toc_entry->size,
415 "failed to allocate image buffer, is FIP file corrupted?");
dp-arm4972ec52016-05-25 16:20:20 +0100416 /* Overflow checks before memory copy. */
417 if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address)
418 log_errx("FIP %s is corrupted", filename);
419 if (toc_entry->size + toc_entry->offset_address > st.st_size)
420 log_errx("FIP %s is corrupted", filename);
421
422 memcpy(image->buffer, buf + toc_entry->offset_address,
423 toc_entry->size);
424 image->size = toc_entry->size;
425
dp-arm516dfcb2016-11-03 13:59:26 +0000426 /* If this is an unknown image, create a descriptor for it. */
427 desc = lookup_image_desc_from_uuid(&image->uuid);
428 if (desc == NULL) {
429 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
430
431 uuid_to_str(name, sizeof(name), &image->uuid);
432 snprintf(filename, sizeof(filename), "%s%s",
433 name, ".bin");
434 desc = new_image_desc(&image->uuid, name, "blob");
435 desc->action = DO_UNPACK;
436 desc->action_arg = xstrdup(filename,
437 "failed to allocate memory for blob filename");
438 add_image_desc(desc);
439 }
440
dp-arm4972ec52016-05-25 16:20:20 +0100441 add_image(image);
442
443 toc_entry++;
444 }
445
446 if (terminated == 0)
447 log_errx("FIP %s does not have a ToC terminator entry",
448 filename);
449 free(buf);
450 return 0;
451}
452
dp-armc1f8e772016-11-04 10:52:25 +0000453static image_t *read_image_from_file(const uuid_t *uuid, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100454{
455 struct stat st;
456 image_t *image;
457 FILE *fp;
458
dp-arm715ef422016-08-30 14:18:58 +0100459 assert(uuid != NULL);
460
dp-arm4972ec52016-05-25 16:20:20 +0100461 fp = fopen(filename, "r");
462 if (fp == NULL)
463 log_err("fopen %s", filename);
464
465 if (fstat(fileno(fp), &st) == -1)
466 log_errx("fstat %s", filename);
467
Masahiro Yamadad224b452017-01-14 23:22:02 +0900468 image = xzalloc(sizeof(*image), "failed to allocate memory for image");
dp-arm715ef422016-08-30 14:18:58 +0100469 memcpy(&image->uuid, uuid, sizeof(uuid_t));
dp-armdb0f5e92016-11-04 10:56:25 +0000470 image->buffer = xmalloc(st.st_size, "failed to allocate image buffer");
dp-arm4972ec52016-05-25 16:20:20 +0100471 if (fread(image->buffer, 1, st.st_size, fp) != st.st_size)
472 log_errx("Failed to read %s", filename);
473 image->size = st.st_size;
dp-arm4972ec52016-05-25 16:20:20 +0100474
475 fclose(fp);
476 return image;
477}
478
dp-armc1f8e772016-11-04 10:52:25 +0000479static int write_image_to_file(const image_t *image, const char *filename)
dp-arm4972ec52016-05-25 16:20:20 +0100480{
481 FILE *fp;
482
483 fp = fopen(filename, "w");
484 if (fp == NULL)
485 log_err("fopen");
486 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
487 log_errx("Failed to write %s", filename);
488 fclose(fp);
489 return 0;
490}
491
dp-arm90d2f0e2016-11-14 15:54:32 +0000492static struct option *add_opt(struct option *opts, size_t *nr_opts,
493 const char *name, int has_arg, int val)
dp-arm4972ec52016-05-25 16:20:20 +0100494{
dp-arm90d2f0e2016-11-14 15:54:32 +0000495 opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts));
496 if (opts == NULL)
497 log_err("realloc");
498 opts[*nr_opts].name = name;
499 opts[*nr_opts].has_arg = has_arg;
500 opts[*nr_opts].flag = NULL;
501 opts[*nr_opts].val = val;
502 ++*nr_opts;
503 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100504}
505
dp-arm90d2f0e2016-11-14 15:54:32 +0000506static struct option *fill_common_opts(struct option *opts, size_t *nr_opts,
507 int has_arg)
dp-arm4972ec52016-05-25 16:20:20 +0100508{
dp-arm90d2f0e2016-11-14 15:54:32 +0000509 image_desc_t *desc;
510
511 for (desc = image_desc_head; desc != NULL; desc = desc->next)
512 opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg,
513 OPT_TOC_ENTRY);
514 return opts;
dp-arm4972ec52016-05-25 16:20:20 +0100515}
516
dp-arm90d2f0e2016-11-14 15:54:32 +0000517static void md_print(const unsigned char *md, size_t len)
dp-arm12e893b2016-08-24 13:21:08 +0100518{
519 size_t i;
520
521 for (i = 0; i < len; i++)
522 printf("%02x", md[i]);
523}
524
dp-arm4972ec52016-05-25 16:20:20 +0100525static int info_cmd(int argc, char *argv[])
526{
527 image_t *image;
528 uint64_t image_offset;
dp-arm516dfcb2016-11-03 13:59:26 +0000529 uint64_t image_size;
dp-arm4972ec52016-05-25 16:20:20 +0100530 fip_toc_header_t toc_header;
dp-arm4972ec52016-05-25 16:20:20 +0100531
532 if (argc != 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100533 info_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100534 argc--, argv++;
535
536 parse_fip(argv[0], &toc_header);
537
538 if (verbose) {
539 log_dbgx("toc_header[name]: 0x%llX",
540 (unsigned long long)toc_header.name);
541 log_dbgx("toc_header[serial_number]: 0x%llX",
542 (unsigned long long)toc_header.serial_number);
543 log_dbgx("toc_header[flags]: 0x%llX",
544 (unsigned long long)toc_header.flags);
545 }
546
547 image_offset = sizeof(fip_toc_header_t) +
548 (sizeof(fip_toc_entry_t) * (nr_images + 1));
549
dp-arm90d2f0e2016-11-14 15:54:32 +0000550 for (image = image_head; image != NULL; image = image->next) {
551 image_desc_t *desc;
dp-arm715ef422016-08-30 14:18:58 +0100552
dp-arm90d2f0e2016-11-14 15:54:32 +0000553 desc = lookup_image_desc_from_uuid(&image->uuid);
dp-arm516dfcb2016-11-03 13:59:26 +0000554 assert(desc != NULL);
555 printf("%s: ", desc->name);
dp-arm4972ec52016-05-25 16:20:20 +0100556 image_size = image->size;
Masahiro Yamadaeee39312017-01-15 23:20:00 +0900557 printf("offset=0x%llX, size=0x%llX, cmdline=\"--%s\"",
558 (unsigned long long)image_offset,
559 (unsigned long long)image_size,
560 desc->cmdline_name);
dp-arm12e893b2016-08-24 13:21:08 +0100561 if (verbose) {
562 unsigned char md[SHA256_DIGEST_LENGTH];
563
564 SHA256(image->buffer, image_size, md);
565 printf(", sha256=");
566 md_print(md, sizeof(md));
567 }
568 putchar('\n');
dp-arm4972ec52016-05-25 16:20:20 +0100569 image_offset += image_size;
570 }
571
572 free_images();
573 return 0;
574}
575
576static void info_usage(void)
577{
578 printf("fiptool info FIP_FILENAME\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100579 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100580}
581
dp-arm90d2f0e2016-11-14 15:54:32 +0000582static int pack_images(const char *filename, uint64_t toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100583{
584 FILE *fp;
585 image_t *image;
586 fip_toc_header_t *toc_header;
587 fip_toc_entry_t *toc_entry;
588 char *buf;
589 uint64_t entry_offset, buf_size, payload_size;
dp-arm4972ec52016-05-25 16:20:20 +0100590
591 /* Calculate total payload size and allocate scratch buffer. */
592 payload_size = 0;
dp-arm90d2f0e2016-11-14 15:54:32 +0000593 for (image = image_head; image != NULL; image = image->next)
594 payload_size += image->size;
dp-arm4972ec52016-05-25 16:20:20 +0100595
596 buf_size = sizeof(fip_toc_header_t) +
597 sizeof(fip_toc_entry_t) * (nr_images + 1);
598 buf = calloc(1, buf_size);
599 if (buf == NULL)
600 log_err("calloc");
601
602 /* Build up header and ToC entries from the image table. */
603 toc_header = (fip_toc_header_t *)buf;
604 toc_header->name = TOC_HEADER_NAME;
605 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
606 toc_header->flags = toc_flags;
607
608 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
609
610 entry_offset = buf_size;
dp-arm90d2f0e2016-11-14 15:54:32 +0000611 for (image = image_head; image != NULL; image = image->next) {
dp-arm4972ec52016-05-25 16:20:20 +0100612 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
613 toc_entry->offset_address = entry_offset;
614 toc_entry->size = image->size;
615 toc_entry->flags = 0;
616 entry_offset += toc_entry->size;
617 toc_entry++;
618 }
619
620 /* Append a null uuid entry to mark the end of ToC entries. */
621 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
622 toc_entry->offset_address = entry_offset;
623 toc_entry->size = 0;
624 toc_entry->flags = 0;
625
626 /* Generate the FIP file. */
627 fp = fopen(filename, "w");
628 if (fp == NULL)
629 log_err("fopen %s", filename);
630
631 if (verbose)
632 log_dbgx("Metadata size: %zu bytes", buf_size);
633
634 if (fwrite(buf, 1, buf_size, fp) != buf_size)
635 log_errx("Failed to write image to %s", filename);
636 free(buf);
637
638 if (verbose)
639 log_dbgx("Payload size: %zu bytes", payload_size);
640
dp-arm90d2f0e2016-11-14 15:54:32 +0000641 for (image = image_head; image != NULL; image = image->next)
dp-arm4972ec52016-05-25 16:20:20 +0100642 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
643 log_errx("Failed to write image to %s", filename);
dp-arm4972ec52016-05-25 16:20:20 +0100644
645 fclose(fp);
646 return 0;
647}
648
649/*
650 * This function is shared between the create and update subcommands.
651 * The difference between the two subcommands is that when the FIP file
652 * is created, the parsing of an existing FIP is skipped. This results
653 * in update_fip() creating the new FIP file from scratch because the
654 * internal image table is not populated.
655 */
656static void update_fip(void)
657{
dp-arm90d2f0e2016-11-14 15:54:32 +0000658 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100659
660 /* Add or replace images in the FIP file. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000661 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +0000662 image_t *new_image, *old_image;
663
dp-arm90d2f0e2016-11-14 15:54:32 +0000664 if (desc->action != DO_PACK)
dp-arm4972ec52016-05-25 16:20:20 +0100665 continue;
666
dp-arm90d2f0e2016-11-14 15:54:32 +0000667 new_image = read_image_from_file(&desc->uuid,
668 desc->action_arg);
669 old_image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100670 if (old_image != NULL) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000671 if (verbose) {
dp-arm516dfcb2016-11-03 13:59:26 +0000672 log_dbgx("Replacing %s with %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000673 desc->cmdline_name,
674 desc->action_arg);
675 }
676 remove_image(old_image);
677 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100678 } else {
679 if (verbose)
680 log_dbgx("Adding image %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000681 desc->action_arg);
dp-arm715ef422016-08-30 14:18:58 +0100682 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100683 }
dp-arm4972ec52016-05-25 16:20:20 +0100684 }
685}
686
dp-arm90d2f0e2016-11-14 15:54:32 +0000687static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100688{
689 unsigned long long flags;
690 char *endptr;
691
692 errno = 0;
693 flags = strtoull(arg, &endptr, 16);
694 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
695 log_errx("Invalid platform ToC flags: %s", arg);
696 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
697 *toc_flags |= flags << 32;
698}
699
dp-arm516dfcb2016-11-03 13:59:26 +0000700static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
701{
702 char *p;
703
704 for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
705 if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
706 p += strlen("uuid=");
707 uuid_from_str(uuid, p);
708 } else if (strncmp(p, "file=", strlen("file=")) == 0) {
709 p += strlen("file=");
710 snprintf(filename, len, "%s", p);
711 }
712 }
713}
714
dp-arm4972ec52016-05-25 16:20:20 +0100715static int create_cmd(int argc, char *argv[])
716{
dp-arm90d2f0e2016-11-14 15:54:32 +0000717 struct option *opts = NULL;
718 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100719 unsigned long long toc_flags = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100720
721 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100722 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100723
dp-arm90d2f0e2016-11-14 15:54:32 +0000724 opts = fill_common_opts(opts, &nr_opts, required_argument);
725 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100726 OPT_PLAT_TOC_FLAGS);
dp-arm516dfcb2016-11-03 13:59:26 +0000727 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000728 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100729
730 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000731 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100732
dp-arm516dfcb2016-11-03 13:59:26 +0000733 c = getopt_long(argc, argv, "b:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100734 if (c == -1)
735 break;
736
737 switch (c) {
738 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000739 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100740
dp-arm90d2f0e2016-11-14 15:54:32 +0000741 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000742 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100743 break;
744 }
745 case OPT_PLAT_TOC_FLAGS:
746 parse_plat_toc_flags(optarg, &toc_flags);
747 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000748 case 'b': {
749 char name[_UUID_STR_LEN + 1];
750 char filename[PATH_MAX] = { 0 };
751 uuid_t uuid = { 0 };
752 image_desc_t *desc;
753
754 parse_blob_opt(optarg, &uuid,
755 filename, sizeof(filename));
756
757 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
758 filename[0] == '\0')
759 create_usage();
760
761 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000762 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000763 uuid_to_str(name, sizeof(name), &uuid);
764 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000765 add_image_desc(desc);
766 }
dp-armfb732312016-12-30 09:55:48 +0000767 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000768 break;
769 }
dp-arm4972ec52016-05-25 16:20:20 +0100770 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100771 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100772 }
773 }
774 argc -= optind;
775 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000776 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100777
778 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100779 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100780
781 update_fip();
782
783 pack_images(argv[0], toc_flags);
784 free_images();
785 return 0;
786}
787
788static void create_usage(void)
789{
790 toc_entry_t *toc_entry = toc_entries;
791
dp-arm516dfcb2016-11-03 13:59:26 +0000792 printf("fiptool create [--blob uuid=...,file=...] "
793 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
794 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID "
795 "pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100796 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
797 "occupying bits 32-47 in 64-bit ToC header.\n");
798 fputc('\n', stderr);
799 printf("Specific images are packed with the following options:\n");
800 for (; toc_entry->cmdline_name != NULL; toc_entry++)
801 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
802 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100803 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100804}
805
806static int update_cmd(int argc, char *argv[])
807{
dp-arm90d2f0e2016-11-14 15:54:32 +0000808 struct option *opts = NULL;
809 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000810 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +0100811 fip_toc_header_t toc_header = { 0 };
812 unsigned long long toc_flags = 0;
813 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100814
815 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100816 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100817
dp-arm90d2f0e2016-11-14 15:54:32 +0000818 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000819 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000820 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
821 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100822 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000823 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100824
825 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000826 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100827
dp-arm516dfcb2016-11-03 13:59:26 +0000828 c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100829 if (c == -1)
830 break;
831
832 switch (c) {
833 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000834 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100835
dp-arm90d2f0e2016-11-14 15:54:32 +0000836 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000837 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100838 break;
839 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000840 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100841 parse_plat_toc_flags(optarg, &toc_flags);
842 pflag = 1;
843 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000844 case 'b': {
845 char name[_UUID_STR_LEN + 1];
846 char filename[PATH_MAX] = { 0 };
847 uuid_t uuid = { 0 };
848 image_desc_t *desc;
849
850 parse_blob_opt(optarg, &uuid,
851 filename, sizeof(filename));
852
853 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
854 filename[0] == '\0')
855 update_usage();
856
857 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000858 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000859 uuid_to_str(name, sizeof(name), &uuid);
860 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000861 add_image_desc(desc);
862 }
dp-armfb732312016-12-30 09:55:48 +0000863 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000864 break;
865 }
dp-arm4972ec52016-05-25 16:20:20 +0100866 case 'o':
867 snprintf(outfile, sizeof(outfile), "%s", optarg);
868 break;
869 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100870 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100871 }
872 }
873 argc -= optind;
874 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000875 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100876
877 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100878 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100879
880 if (outfile[0] == '\0')
881 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
882
Masahiro Yamadaebb6e372016-12-25 12:41:41 +0900883 if (access(argv[0], F_OK) == 0)
dp-arm4972ec52016-05-25 16:20:20 +0100884 parse_fip(argv[0], &toc_header);
885
886 if (pflag)
887 toc_header.flags &= ~(0xffffULL << 32);
888 toc_flags = (toc_header.flags |= toc_flags);
889
890 update_fip();
891
892 pack_images(outfile, toc_flags);
893 free_images();
894 return 0;
895}
896
897static void update_usage(void)
898{
899 toc_entry_t *toc_entry = toc_entries;
900
dp-arm516dfcb2016-11-03 13:59:26 +0000901 printf("fiptool update [--blob uuid=...,file=...] [--out FIP_FILENAME] "
dp-arm4972ec52016-05-25 16:20:20 +0100902 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
dp-arm516dfcb2016-11-03 13:59:26 +0000903 printf(" --blob uuid=...,file=...\tAdd or update an image "
904 "with the given UUID pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100905 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
906 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
907 "occupying bits 32-47 in 64-bit ToC header.\n");
908 fputc('\n', stderr);
909 printf("Specific images are packed with the following options:\n");
910 for (; toc_entry->cmdline_name != NULL; toc_entry++)
911 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
912 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100913 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100914}
915
916static int unpack_cmd(int argc, char *argv[])
917{
dp-arm90d2f0e2016-11-14 15:54:32 +0000918 struct option *opts = NULL;
919 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000920 char outdir[PATH_MAX] = { 0 };
dp-arm90d2f0e2016-11-14 15:54:32 +0000921 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100922 int fflag = 0;
923 int unpack_all = 1;
dp-arm4972ec52016-05-25 16:20:20 +0100924
925 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100926 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100927
dp-arm90d2f0e2016-11-14 15:54:32 +0000928 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000929 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000930 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
931 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
932 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100933
934 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000935 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100936
dp-arm516dfcb2016-11-03 13:59:26 +0000937 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100938 if (c == -1)
939 break;
940
941 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000942 case OPT_TOC_ENTRY: {
943 image_desc_t *desc;
944
945 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000946 set_image_desc_action(desc, DO_UNPACK, optarg);
dp-arm90d2f0e2016-11-14 15:54:32 +0000947 unpack_all = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100948 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000949 }
dp-arm516dfcb2016-11-03 13:59:26 +0000950 case 'b': {
951 char name[_UUID_STR_LEN + 1];
952 char filename[PATH_MAX] = { 0 };
953 uuid_t uuid = { 0 };
954 image_desc_t *desc;
955
956 parse_blob_opt(optarg, &uuid,
957 filename, sizeof(filename));
958
959 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
960 filename[0] == '\0')
961 unpack_usage();
962
963 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000964 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000965 uuid_to_str(name, sizeof(name), &uuid);
966 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000967 add_image_desc(desc);
968 }
dp-armfb732312016-12-30 09:55:48 +0000969 set_image_desc_action(desc, DO_UNPACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000970 unpack_all = 0;
971 break;
972 }
dp-arm4972ec52016-05-25 16:20:20 +0100973 case 'f':
974 fflag = 1;
975 break;
976 case 'o':
977 snprintf(outdir, sizeof(outdir), "%s", optarg);
978 break;
979 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100980 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100981 }
982 }
983 argc -= optind;
984 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000985 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100986
987 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100988 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100989
990 parse_fip(argv[0], NULL);
991
992 if (outdir[0] != '\0')
993 if (chdir(outdir) == -1)
994 log_err("chdir %s", outdir);
995
dp-arm4972ec52016-05-25 16:20:20 +0100996 /* Unpack all specified images. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000997 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +0000998 char file[PATH_MAX];
dp-arm715ef422016-08-30 14:18:58 +0100999 image_t *image;
1000
dp-arm90d2f0e2016-11-14 15:54:32 +00001001 if (!unpack_all && desc->action != DO_UNPACK)
dp-arm4972ec52016-05-25 16:20:20 +01001002 continue;
1003
1004 /* Build filename. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001005 if (desc->action_arg == NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001006 snprintf(file, sizeof(file), "%s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +00001007 desc->cmdline_name);
dp-arm4972ec52016-05-25 16:20:20 +01001008 else
1009 snprintf(file, sizeof(file), "%s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001010 desc->action_arg);
dp-arm4972ec52016-05-25 16:20:20 +01001011
dp-arm90d2f0e2016-11-14 15:54:32 +00001012 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001013 if (image == NULL) {
1014 if (!unpack_all)
dp-arm516dfcb2016-11-03 13:59:26 +00001015 log_warnx("%s does not exist in %s",
dp-arm715ef422016-08-30 14:18:58 +01001016 file, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001017 continue;
1018 }
1019
1020 if (access(file, F_OK) != 0 || fflag) {
1021 if (verbose)
1022 log_dbgx("Unpacking %s", file);
dp-arm715ef422016-08-30 14:18:58 +01001023 write_image_to_file(image, file);
dp-arm4972ec52016-05-25 16:20:20 +01001024 } else {
1025 log_warnx("File %s already exists, use --force to overwrite it",
1026 file);
1027 }
dp-arm4972ec52016-05-25 16:20:20 +01001028 }
1029
1030 free_images();
1031 return 0;
1032}
1033
1034static void unpack_usage(void)
1035{
1036 toc_entry_t *toc_entry = toc_entries;
1037
dp-arm516dfcb2016-11-03 13:59:26 +00001038 printf("fiptool unpack [--blob uuid=...,file=...] [--force] "
1039 "[--out <path>] [opts] FIP_FILENAME\n");
1040 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID "
1041 "to file.\n");
1042 printf(" --force\t\t\tIf the output file already exists, use --force to "
dp-arm4972ec52016-05-25 16:20:20 +01001043 "overwrite it.\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001044 printf(" --out path\t\t\tSet the output directory path.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001045 fputc('\n', stderr);
1046 printf("Specific images are unpacked with the following options:\n");
1047 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1048 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1049 toc_entry->name);
1050 fputc('\n', stderr);
1051 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001052 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001053}
1054
1055static int remove_cmd(int argc, char *argv[])
1056{
dp-arm90d2f0e2016-11-14 15:54:32 +00001057 struct option *opts = NULL;
1058 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +00001059 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +01001060 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +00001061 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +01001062 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001063
1064 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +01001065 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001066
dp-arm90d2f0e2016-11-14 15:54:32 +00001067 opts = fill_common_opts(opts, &nr_opts, no_argument);
dp-arm516dfcb2016-11-03 13:59:26 +00001068 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +00001069 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1070 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1071 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +01001072
1073 while (1) {
dp-armfe92b892016-11-07 11:13:54 +00001074 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001075
dp-arm516dfcb2016-11-03 13:59:26 +00001076 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +01001077 if (c == -1)
1078 break;
1079
1080 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +00001081 case OPT_TOC_ENTRY: {
1082 image_desc_t *desc;
1083
1084 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +00001085 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm4972ec52016-05-25 16:20:20 +01001086 break;
dp-arm90d2f0e2016-11-14 15:54:32 +00001087 }
dp-arm516dfcb2016-11-03 13:59:26 +00001088 case 'b': {
1089 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1090 uuid_t uuid = { 0 };
1091 image_desc_t *desc;
1092
1093 parse_blob_opt(optarg, &uuid,
1094 filename, sizeof(filename));
1095
1096 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1097 remove_usage();
1098
1099 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +00001100 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +00001101 uuid_to_str(name, sizeof(name), &uuid);
1102 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +00001103 add_image_desc(desc);
1104 }
dp-armfb732312016-12-30 09:55:48 +00001105 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm516dfcb2016-11-03 13:59:26 +00001106 break;
1107 }
dp-arm4972ec52016-05-25 16:20:20 +01001108 case 'f':
1109 fflag = 1;
1110 break;
1111 case 'o':
1112 snprintf(outfile, sizeof(outfile), "%s", optarg);
1113 break;
1114 default:
dp-arm29f1b5c2016-09-15 09:58:50 +01001115 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001116 }
1117 }
1118 argc -= optind;
1119 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001120 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001121
1122 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001123 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001124
1125 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1126 log_errx("File %s already exists, use --force to overwrite it",
1127 outfile);
1128
1129 if (outfile[0] == '\0')
1130 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1131
1132 parse_fip(argv[0], &toc_header);
1133
dp-arm90d2f0e2016-11-14 15:54:32 +00001134 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +01001135 image_t *image;
1136
dp-arm90d2f0e2016-11-14 15:54:32 +00001137 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +01001138 continue;
dp-arm516dfcb2016-11-03 13:59:26 +00001139
dp-arm90d2f0e2016-11-14 15:54:32 +00001140 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001141 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +01001142 if (verbose)
dp-arm516dfcb2016-11-03 13:59:26 +00001143 log_dbgx("Removing %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001144 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +01001145 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +01001146 } else {
dp-arm516dfcb2016-11-03 13:59:26 +00001147 log_warnx("%s does not exist in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001148 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001149 }
1150 }
1151
1152 pack_images(outfile, toc_header.flags);
1153 free_images();
1154 return 0;
1155}
1156
1157static void remove_usage(void)
1158{
1159 toc_entry_t *toc_entry = toc_entries;
1160
dp-arm516dfcb2016-11-03 13:59:26 +00001161 printf("fiptool remove [--blob uuid=...] [--force] "
1162 "[--out FIP_FILENAME] [opts] FIP_FILENAME\n");
1163 printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001164 printf(" --force\t\tIf the output FIP file already exists, use --force to "
1165 "overwrite it.\n");
1166 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1167 fputc('\n', stderr);
1168 printf("Specific images are removed with the following options:\n");
1169 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1170 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
1171 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +01001172 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001173}
1174
1175static int version_cmd(int argc, char *argv[])
1176{
1177#ifdef VERSION
1178 puts(VERSION);
1179#else
1180 /* If built from fiptool directory, VERSION is not set. */
1181 puts("Unknown version");
1182#endif
1183 return 0;
1184}
1185
1186static void version_usage(void)
1187{
1188 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001189 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001190}
1191
1192static int help_cmd(int argc, char *argv[])
1193{
1194 int i;
1195
1196 if (argc < 2)
1197 usage();
1198 argc--, argv++;
1199
1200 for (i = 0; i < NELEM(cmds); i++) {
1201 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001202 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001203 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001204 }
1205 if (i == NELEM(cmds))
1206 printf("No help for subcommand '%s'\n", argv[0]);
1207 return 0;
1208}
1209
1210static void usage(void)
1211{
1212 printf("usage: [--verbose] fiptool <command> [<args>]\n");
1213 printf("Global options supported:\n");
1214 printf(" --verbose\tEnable verbose output for all commands.\n");
1215 fputc('\n', stderr);
1216 printf("Commands supported:\n");
1217 printf(" info\t\tList images contained in FIP.\n");
1218 printf(" create\tCreate a new FIP with the given images.\n");
1219 printf(" update\tUpdate an existing FIP with the given images.\n");
1220 printf(" unpack\tUnpack images from FIP.\n");
1221 printf(" remove\tRemove images from FIP.\n");
1222 printf(" version\tShow fiptool version.\n");
1223 printf(" help\t\tShow help for given command.\n");
1224 exit(1);
1225}
1226
1227int main(int argc, char *argv[])
1228{
1229 int i, ret = 0;
1230
dp-arm5cd10ae2016-11-07 10:45:59 +00001231 while (1) {
1232 int c, opt_index = 0;
1233 static struct option opts[] = {
1234 { "verbose", no_argument, NULL, 'v' },
1235 { NULL, no_argument, NULL, 0 }
1236 };
1237
1238 /*
1239 * Set POSIX mode so getopt stops at the first non-option
1240 * which is the subcommand.
1241 */
1242 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1243 if (c == -1)
1244 break;
dp-arm4972ec52016-05-25 16:20:20 +01001245
dp-arm5cd10ae2016-11-07 10:45:59 +00001246 switch (c) {
1247 case 'v':
1248 verbose = 1;
1249 break;
1250 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001251 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001252 }
dp-arm4972ec52016-05-25 16:20:20 +01001253 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001254 argc -= optind;
1255 argv += optind;
1256 /* Reset optind for subsequent getopt processing. */
1257 optind = 0;
1258
1259 if (argc == 0)
1260 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001261
dp-arm90d2f0e2016-11-14 15:54:32 +00001262 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001263 for (i = 0; i < NELEM(cmds); i++) {
1264 if (strcmp(cmds[i].name, argv[0]) == 0) {
1265 ret = cmds[i].handler(argc, argv);
1266 break;
1267 }
1268 }
1269 if (i == NELEM(cmds))
1270 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001271 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001272 return ret;
1273}