blob: 6145e26d4b3d5e69ae768f373ca692e29628c46a [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;
557 printf("offset=0x%llX, size=0x%llX",
558 (unsigned long long)image_offset,
559 (unsigned long long)image_size);
dp-arm90d2f0e2016-11-14 15:54:32 +0000560 if (desc != NULL)
dp-arm12e893b2016-08-24 13:21:08 +0100561 printf(", cmdline=\"--%s\"",
dp-arm90d2f0e2016-11-14 15:54:32 +0000562 desc->cmdline_name);
dp-arm12e893b2016-08-24 13:21:08 +0100563 if (verbose) {
564 unsigned char md[SHA256_DIGEST_LENGTH];
565
566 SHA256(image->buffer, image_size, md);
567 printf(", sha256=");
568 md_print(md, sizeof(md));
569 }
570 putchar('\n');
dp-arm4972ec52016-05-25 16:20:20 +0100571 image_offset += image_size;
572 }
573
574 free_images();
575 return 0;
576}
577
578static void info_usage(void)
579{
580 printf("fiptool info FIP_FILENAME\n");
dp-arm29f1b5c2016-09-15 09:58:50 +0100581 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100582}
583
dp-arm90d2f0e2016-11-14 15:54:32 +0000584static int pack_images(const char *filename, uint64_t toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100585{
586 FILE *fp;
587 image_t *image;
588 fip_toc_header_t *toc_header;
589 fip_toc_entry_t *toc_entry;
590 char *buf;
591 uint64_t entry_offset, buf_size, payload_size;
dp-arm4972ec52016-05-25 16:20:20 +0100592
593 /* Calculate total payload size and allocate scratch buffer. */
594 payload_size = 0;
dp-arm90d2f0e2016-11-14 15:54:32 +0000595 for (image = image_head; image != NULL; image = image->next)
596 payload_size += image->size;
dp-arm4972ec52016-05-25 16:20:20 +0100597
598 buf_size = sizeof(fip_toc_header_t) +
599 sizeof(fip_toc_entry_t) * (nr_images + 1);
600 buf = calloc(1, buf_size);
601 if (buf == NULL)
602 log_err("calloc");
603
604 /* Build up header and ToC entries from the image table. */
605 toc_header = (fip_toc_header_t *)buf;
606 toc_header->name = TOC_HEADER_NAME;
607 toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER;
608 toc_header->flags = toc_flags;
609
610 toc_entry = (fip_toc_entry_t *)(toc_header + 1);
611
612 entry_offset = buf_size;
dp-arm90d2f0e2016-11-14 15:54:32 +0000613 for (image = image_head; image != NULL; image = image->next) {
dp-arm4972ec52016-05-25 16:20:20 +0100614 memcpy(&toc_entry->uuid, &image->uuid, sizeof(uuid_t));
615 toc_entry->offset_address = entry_offset;
616 toc_entry->size = image->size;
617 toc_entry->flags = 0;
618 entry_offset += toc_entry->size;
619 toc_entry++;
620 }
621
622 /* Append a null uuid entry to mark the end of ToC entries. */
623 memcpy(&toc_entry->uuid, &uuid_null, sizeof(uuid_t));
624 toc_entry->offset_address = entry_offset;
625 toc_entry->size = 0;
626 toc_entry->flags = 0;
627
628 /* Generate the FIP file. */
629 fp = fopen(filename, "w");
630 if (fp == NULL)
631 log_err("fopen %s", filename);
632
633 if (verbose)
634 log_dbgx("Metadata size: %zu bytes", buf_size);
635
636 if (fwrite(buf, 1, buf_size, fp) != buf_size)
637 log_errx("Failed to write image to %s", filename);
638 free(buf);
639
640 if (verbose)
641 log_dbgx("Payload size: %zu bytes", payload_size);
642
dp-arm90d2f0e2016-11-14 15:54:32 +0000643 for (image = image_head; image != NULL; image = image->next)
dp-arm4972ec52016-05-25 16:20:20 +0100644 if (fwrite(image->buffer, 1, image->size, fp) != image->size)
645 log_errx("Failed to write image to %s", filename);
dp-arm4972ec52016-05-25 16:20:20 +0100646
647 fclose(fp);
648 return 0;
649}
650
651/*
652 * This function is shared between the create and update subcommands.
653 * The difference between the two subcommands is that when the FIP file
654 * is created, the parsing of an existing FIP is skipped. This results
655 * in update_fip() creating the new FIP file from scratch because the
656 * internal image table is not populated.
657 */
658static void update_fip(void)
659{
dp-arm90d2f0e2016-11-14 15:54:32 +0000660 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100661
662 /* Add or replace images in the FIP file. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000663 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +0000664 image_t *new_image, *old_image;
665
dp-arm90d2f0e2016-11-14 15:54:32 +0000666 if (desc->action != DO_PACK)
dp-arm4972ec52016-05-25 16:20:20 +0100667 continue;
668
dp-arm90d2f0e2016-11-14 15:54:32 +0000669 new_image = read_image_from_file(&desc->uuid,
670 desc->action_arg);
671 old_image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +0100672 if (old_image != NULL) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000673 if (verbose) {
dp-arm516dfcb2016-11-03 13:59:26 +0000674 log_dbgx("Replacing %s with %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000675 desc->cmdline_name,
676 desc->action_arg);
677 }
678 remove_image(old_image);
679 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100680 } else {
681 if (verbose)
682 log_dbgx("Adding image %s",
dp-arm90d2f0e2016-11-14 15:54:32 +0000683 desc->action_arg);
dp-arm715ef422016-08-30 14:18:58 +0100684 add_image(new_image);
dp-arm4972ec52016-05-25 16:20:20 +0100685 }
dp-arm4972ec52016-05-25 16:20:20 +0100686 }
687}
688
dp-arm90d2f0e2016-11-14 15:54:32 +0000689static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags)
dp-arm4972ec52016-05-25 16:20:20 +0100690{
691 unsigned long long flags;
692 char *endptr;
693
694 errno = 0;
695 flags = strtoull(arg, &endptr, 16);
696 if (*endptr != '\0' || flags > UINT16_MAX || errno != 0)
697 log_errx("Invalid platform ToC flags: %s", arg);
698 /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */
699 *toc_flags |= flags << 32;
700}
701
dp-arm516dfcb2016-11-03 13:59:26 +0000702static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len)
703{
704 char *p;
705
706 for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) {
707 if (strncmp(p, "uuid=", strlen("uuid=")) == 0) {
708 p += strlen("uuid=");
709 uuid_from_str(uuid, p);
710 } else if (strncmp(p, "file=", strlen("file=")) == 0) {
711 p += strlen("file=");
712 snprintf(filename, len, "%s", p);
713 }
714 }
715}
716
dp-arm4972ec52016-05-25 16:20:20 +0100717static int create_cmd(int argc, char *argv[])
718{
dp-arm90d2f0e2016-11-14 15:54:32 +0000719 struct option *opts = NULL;
720 size_t nr_opts = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100721 unsigned long long toc_flags = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100722
723 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100724 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100725
dp-arm90d2f0e2016-11-14 15:54:32 +0000726 opts = fill_common_opts(opts, &nr_opts, required_argument);
727 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100728 OPT_PLAT_TOC_FLAGS);
dp-arm516dfcb2016-11-03 13:59:26 +0000729 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000730 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100731
732 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000733 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100734
dp-arm516dfcb2016-11-03 13:59:26 +0000735 c = getopt_long(argc, argv, "b:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100736 if (c == -1)
737 break;
738
739 switch (c) {
740 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000741 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100742
dp-arm90d2f0e2016-11-14 15:54:32 +0000743 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000744 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100745 break;
746 }
747 case OPT_PLAT_TOC_FLAGS:
748 parse_plat_toc_flags(optarg, &toc_flags);
749 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000750 case 'b': {
751 char name[_UUID_STR_LEN + 1];
752 char filename[PATH_MAX] = { 0 };
753 uuid_t uuid = { 0 };
754 image_desc_t *desc;
755
756 parse_blob_opt(optarg, &uuid,
757 filename, sizeof(filename));
758
759 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
760 filename[0] == '\0')
761 create_usage();
762
763 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000764 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000765 uuid_to_str(name, sizeof(name), &uuid);
766 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000767 add_image_desc(desc);
768 }
dp-armfb732312016-12-30 09:55:48 +0000769 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000770 break;
771 }
dp-arm4972ec52016-05-25 16:20:20 +0100772 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100773 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100774 }
775 }
776 argc -= optind;
777 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000778 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100779
780 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100781 create_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100782
783 update_fip();
784
785 pack_images(argv[0], toc_flags);
786 free_images();
787 return 0;
788}
789
790static void create_usage(void)
791{
792 toc_entry_t *toc_entry = toc_entries;
793
dp-arm516dfcb2016-11-03 13:59:26 +0000794 printf("fiptool create [--blob uuid=...,file=...] "
795 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
796 printf(" --blob uuid=...,file=...\tAdd an image with the given UUID "
797 "pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100798 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
799 "occupying bits 32-47 in 64-bit ToC header.\n");
800 fputc('\n', stderr);
801 printf("Specific images are packed with the following options:\n");
802 for (; toc_entry->cmdline_name != NULL; toc_entry++)
803 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
804 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100805 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100806}
807
808static int update_cmd(int argc, char *argv[])
809{
dp-arm90d2f0e2016-11-14 15:54:32 +0000810 struct option *opts = NULL;
811 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000812 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +0100813 fip_toc_header_t toc_header = { 0 };
814 unsigned long long toc_flags = 0;
815 int pflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100816
817 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100818 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100819
dp-arm90d2f0e2016-11-14 15:54:32 +0000820 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000821 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000822 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
823 opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument,
dp-arm4972ec52016-05-25 16:20:20 +0100824 OPT_PLAT_TOC_FLAGS);
dp-arm90d2f0e2016-11-14 15:54:32 +0000825 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100826
827 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000828 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100829
dp-arm516dfcb2016-11-03 13:59:26 +0000830 c = getopt_long(argc, argv, "b:o:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100831 if (c == -1)
832 break;
833
834 switch (c) {
835 case OPT_TOC_ENTRY: {
dp-arm90d2f0e2016-11-14 15:54:32 +0000836 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100837
dp-arm90d2f0e2016-11-14 15:54:32 +0000838 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000839 set_image_desc_action(desc, DO_PACK, optarg);
dp-arm4972ec52016-05-25 16:20:20 +0100840 break;
841 }
dp-arm90d2f0e2016-11-14 15:54:32 +0000842 case OPT_PLAT_TOC_FLAGS:
dp-arm4972ec52016-05-25 16:20:20 +0100843 parse_plat_toc_flags(optarg, &toc_flags);
844 pflag = 1;
845 break;
dp-arm516dfcb2016-11-03 13:59:26 +0000846 case 'b': {
847 char name[_UUID_STR_LEN + 1];
848 char filename[PATH_MAX] = { 0 };
849 uuid_t uuid = { 0 };
850 image_desc_t *desc;
851
852 parse_blob_opt(optarg, &uuid,
853 filename, sizeof(filename));
854
855 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
856 filename[0] == '\0')
857 update_usage();
858
859 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000860 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000861 uuid_to_str(name, sizeof(name), &uuid);
862 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000863 add_image_desc(desc);
864 }
dp-armfb732312016-12-30 09:55:48 +0000865 set_image_desc_action(desc, DO_PACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000866 break;
867 }
dp-arm4972ec52016-05-25 16:20:20 +0100868 case 'o':
869 snprintf(outfile, sizeof(outfile), "%s", optarg);
870 break;
871 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100872 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100873 }
874 }
875 argc -= optind;
876 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000877 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100878
879 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100880 update_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100881
882 if (outfile[0] == '\0')
883 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
884
885 if (access(outfile, F_OK) == 0)
886 parse_fip(argv[0], &toc_header);
887
888 if (pflag)
889 toc_header.flags &= ~(0xffffULL << 32);
890 toc_flags = (toc_header.flags |= toc_flags);
891
892 update_fip();
893
894 pack_images(outfile, toc_flags);
895 free_images();
896 return 0;
897}
898
899static void update_usage(void)
900{
901 toc_entry_t *toc_entry = toc_entries;
902
dp-arm516dfcb2016-11-03 13:59:26 +0000903 printf("fiptool update [--blob uuid=...,file=...] [--out FIP_FILENAME] "
dp-arm4972ec52016-05-25 16:20:20 +0100904 "[--plat-toc-flags <value>] [opts] FIP_FILENAME\n");
dp-arm516dfcb2016-11-03 13:59:26 +0000905 printf(" --blob uuid=...,file=...\tAdd or update an image "
906 "with the given UUID pointed to by file.\n");
dp-arm4972ec52016-05-25 16:20:20 +0100907 printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n");
908 printf(" --plat-toc-flags <value>\t16-bit platform specific flag field "
909 "occupying bits 32-47 in 64-bit ToC header.\n");
910 fputc('\n', stderr);
911 printf("Specific images are packed with the following options:\n");
912 for (; toc_entry->cmdline_name != NULL; toc_entry++)
913 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
914 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +0100915 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +0100916}
917
918static int unpack_cmd(int argc, char *argv[])
919{
dp-arm90d2f0e2016-11-14 15:54:32 +0000920 struct option *opts = NULL;
921 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +0000922 char outdir[PATH_MAX] = { 0 };
dp-arm90d2f0e2016-11-14 15:54:32 +0000923 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +0100924 int fflag = 0;
925 int unpack_all = 1;
dp-arm4972ec52016-05-25 16:20:20 +0100926
927 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +0100928 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100929
dp-arm90d2f0e2016-11-14 15:54:32 +0000930 opts = fill_common_opts(opts, &nr_opts, required_argument);
dp-arm516dfcb2016-11-03 13:59:26 +0000931 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +0000932 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
933 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
934 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +0100935
936 while (1) {
dp-armfe92b892016-11-07 11:13:54 +0000937 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100938
dp-arm516dfcb2016-11-03 13:59:26 +0000939 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +0100940 if (c == -1)
941 break;
942
943 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +0000944 case OPT_TOC_ENTRY: {
945 image_desc_t *desc;
946
947 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +0000948 set_image_desc_action(desc, DO_UNPACK, optarg);
dp-arm90d2f0e2016-11-14 15:54:32 +0000949 unpack_all = 0;
dp-arm4972ec52016-05-25 16:20:20 +0100950 break;
dp-arm90d2f0e2016-11-14 15:54:32 +0000951 }
dp-arm516dfcb2016-11-03 13:59:26 +0000952 case 'b': {
953 char name[_UUID_STR_LEN + 1];
954 char filename[PATH_MAX] = { 0 };
955 uuid_t uuid = { 0 };
956 image_desc_t *desc;
957
958 parse_blob_opt(optarg, &uuid,
959 filename, sizeof(filename));
960
961 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 ||
962 filename[0] == '\0')
963 unpack_usage();
964
965 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +0000966 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +0000967 uuid_to_str(name, sizeof(name), &uuid);
968 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +0000969 add_image_desc(desc);
970 }
dp-armfb732312016-12-30 09:55:48 +0000971 set_image_desc_action(desc, DO_UNPACK, filename);
dp-arm516dfcb2016-11-03 13:59:26 +0000972 unpack_all = 0;
973 break;
974 }
dp-arm4972ec52016-05-25 16:20:20 +0100975 case 'f':
976 fflag = 1;
977 break;
978 case 'o':
979 snprintf(outdir, sizeof(outdir), "%s", optarg);
980 break;
981 default:
dp-arm29f1b5c2016-09-15 09:58:50 +0100982 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100983 }
984 }
985 argc -= optind;
986 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +0000987 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +0100988
989 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +0100990 unpack_usage();
dp-arm4972ec52016-05-25 16:20:20 +0100991
992 parse_fip(argv[0], NULL);
993
994 if (outdir[0] != '\0')
995 if (chdir(outdir) == -1)
996 log_err("chdir %s", outdir);
997
dp-arm4972ec52016-05-25 16:20:20 +0100998 /* Unpack all specified images. */
dp-arm90d2f0e2016-11-14 15:54:32 +0000999 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm516dfcb2016-11-03 13:59:26 +00001000 char file[PATH_MAX];
dp-arm715ef422016-08-30 14:18:58 +01001001 image_t *image;
1002
dp-arm90d2f0e2016-11-14 15:54:32 +00001003 if (!unpack_all && desc->action != DO_UNPACK)
dp-arm4972ec52016-05-25 16:20:20 +01001004 continue;
1005
1006 /* Build filename. */
dp-arm90d2f0e2016-11-14 15:54:32 +00001007 if (desc->action_arg == NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001008 snprintf(file, sizeof(file), "%s.bin",
dp-arm90d2f0e2016-11-14 15:54:32 +00001009 desc->cmdline_name);
dp-arm4972ec52016-05-25 16:20:20 +01001010 else
1011 snprintf(file, sizeof(file), "%s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001012 desc->action_arg);
dp-arm4972ec52016-05-25 16:20:20 +01001013
dp-arm90d2f0e2016-11-14 15:54:32 +00001014 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001015 if (image == NULL) {
1016 if (!unpack_all)
dp-arm516dfcb2016-11-03 13:59:26 +00001017 log_warnx("%s does not exist in %s",
dp-arm715ef422016-08-30 14:18:58 +01001018 file, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001019 continue;
1020 }
1021
1022 if (access(file, F_OK) != 0 || fflag) {
1023 if (verbose)
1024 log_dbgx("Unpacking %s", file);
dp-arm715ef422016-08-30 14:18:58 +01001025 write_image_to_file(image, file);
dp-arm4972ec52016-05-25 16:20:20 +01001026 } else {
1027 log_warnx("File %s already exists, use --force to overwrite it",
1028 file);
1029 }
dp-arm4972ec52016-05-25 16:20:20 +01001030 }
1031
1032 free_images();
1033 return 0;
1034}
1035
1036static void unpack_usage(void)
1037{
1038 toc_entry_t *toc_entry = toc_entries;
1039
dp-arm516dfcb2016-11-03 13:59:26 +00001040 printf("fiptool unpack [--blob uuid=...,file=...] [--force] "
1041 "[--out <path>] [opts] FIP_FILENAME\n");
1042 printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID "
1043 "to file.\n");
1044 printf(" --force\t\t\tIf the output file already exists, use --force to "
dp-arm4972ec52016-05-25 16:20:20 +01001045 "overwrite it.\n");
dp-arm516dfcb2016-11-03 13:59:26 +00001046 printf(" --out path\t\t\tSet the output directory path.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001047 fputc('\n', stderr);
1048 printf("Specific images are unpacked with the following options:\n");
1049 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1050 printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name,
1051 toc_entry->name);
1052 fputc('\n', stderr);
1053 printf("If no options are provided, all images will be unpacked.\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001054 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001055}
1056
1057static int remove_cmd(int argc, char *argv[])
1058{
dp-arm90d2f0e2016-11-14 15:54:32 +00001059 struct option *opts = NULL;
1060 size_t nr_opts = 0;
dp-arm516dfcb2016-11-03 13:59:26 +00001061 char outfile[PATH_MAX] = { 0 };
dp-arm4972ec52016-05-25 16:20:20 +01001062 fip_toc_header_t toc_header;
dp-arm90d2f0e2016-11-14 15:54:32 +00001063 image_desc_t *desc;
dp-arm4972ec52016-05-25 16:20:20 +01001064 int fflag = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001065
1066 if (argc < 2)
dp-arm29f1b5c2016-09-15 09:58:50 +01001067 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001068
dp-arm90d2f0e2016-11-14 15:54:32 +00001069 opts = fill_common_opts(opts, &nr_opts, no_argument);
dp-arm516dfcb2016-11-03 13:59:26 +00001070 opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b');
dp-arm90d2f0e2016-11-14 15:54:32 +00001071 opts = add_opt(opts, &nr_opts, "force", no_argument, 'f');
1072 opts = add_opt(opts, &nr_opts, "out", required_argument, 'o');
1073 opts = add_opt(opts, &nr_opts, NULL, 0, 0);
dp-arm4972ec52016-05-25 16:20:20 +01001074
1075 while (1) {
dp-armfe92b892016-11-07 11:13:54 +00001076 int c, opt_index = 0;
dp-arm4972ec52016-05-25 16:20:20 +01001077
dp-arm516dfcb2016-11-03 13:59:26 +00001078 c = getopt_long(argc, argv, "b:fo:", opts, &opt_index);
dp-arm4972ec52016-05-25 16:20:20 +01001079 if (c == -1)
1080 break;
1081
1082 switch (c) {
dp-arm90d2f0e2016-11-14 15:54:32 +00001083 case OPT_TOC_ENTRY: {
1084 image_desc_t *desc;
1085
1086 desc = lookup_image_desc_from_opt(opts[opt_index].name);
dp-armfb732312016-12-30 09:55:48 +00001087 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm4972ec52016-05-25 16:20:20 +01001088 break;
dp-arm90d2f0e2016-11-14 15:54:32 +00001089 }
dp-arm516dfcb2016-11-03 13:59:26 +00001090 case 'b': {
1091 char name[_UUID_STR_LEN + 1], filename[PATH_MAX];
1092 uuid_t uuid = { 0 };
1093 image_desc_t *desc;
1094
1095 parse_blob_opt(optarg, &uuid,
1096 filename, sizeof(filename));
1097
1098 if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0)
1099 remove_usage();
1100
1101 desc = lookup_image_desc_from_uuid(&uuid);
dp-armfb732312016-12-30 09:55:48 +00001102 if (desc == NULL) {
dp-arm516dfcb2016-11-03 13:59:26 +00001103 uuid_to_str(name, sizeof(name), &uuid);
1104 desc = new_image_desc(&uuid, name, "blob");
dp-arm516dfcb2016-11-03 13:59:26 +00001105 add_image_desc(desc);
1106 }
dp-armfb732312016-12-30 09:55:48 +00001107 set_image_desc_action(desc, DO_REMOVE, NULL);
dp-arm516dfcb2016-11-03 13:59:26 +00001108 break;
1109 }
dp-arm4972ec52016-05-25 16:20:20 +01001110 case 'f':
1111 fflag = 1;
1112 break;
1113 case 'o':
1114 snprintf(outfile, sizeof(outfile), "%s", optarg);
1115 break;
1116 default:
dp-arm29f1b5c2016-09-15 09:58:50 +01001117 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001118 }
1119 }
1120 argc -= optind;
1121 argv += optind;
dp-arm90d2f0e2016-11-14 15:54:32 +00001122 free(opts);
dp-arm4972ec52016-05-25 16:20:20 +01001123
1124 if (argc == 0)
dp-arm29f1b5c2016-09-15 09:58:50 +01001125 remove_usage();
dp-arm4972ec52016-05-25 16:20:20 +01001126
1127 if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag)
1128 log_errx("File %s already exists, use --force to overwrite it",
1129 outfile);
1130
1131 if (outfile[0] == '\0')
1132 snprintf(outfile, sizeof(outfile), "%s", argv[0]);
1133
1134 parse_fip(argv[0], &toc_header);
1135
dp-arm90d2f0e2016-11-14 15:54:32 +00001136 for (desc = image_desc_head; desc != NULL; desc = desc->next) {
dp-arm715ef422016-08-30 14:18:58 +01001137 image_t *image;
1138
dp-arm90d2f0e2016-11-14 15:54:32 +00001139 if (desc->action != DO_REMOVE)
dp-arm4972ec52016-05-25 16:20:20 +01001140 continue;
dp-arm516dfcb2016-11-03 13:59:26 +00001141
dp-arm90d2f0e2016-11-14 15:54:32 +00001142 image = lookup_image_from_uuid(&desc->uuid);
dp-arm715ef422016-08-30 14:18:58 +01001143 if (image != NULL) {
dp-arm4972ec52016-05-25 16:20:20 +01001144 if (verbose)
dp-arm516dfcb2016-11-03 13:59:26 +00001145 log_dbgx("Removing %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001146 desc->cmdline_name);
dp-arm715ef422016-08-30 14:18:58 +01001147 remove_image(image);
dp-arm4972ec52016-05-25 16:20:20 +01001148 } else {
dp-arm516dfcb2016-11-03 13:59:26 +00001149 log_warnx("%s does not exist in %s",
dp-arm90d2f0e2016-11-14 15:54:32 +00001150 desc->cmdline_name, argv[0]);
dp-arm4972ec52016-05-25 16:20:20 +01001151 }
1152 }
1153
1154 pack_images(outfile, toc_header.flags);
1155 free_images();
1156 return 0;
1157}
1158
1159static void remove_usage(void)
1160{
1161 toc_entry_t *toc_entry = toc_entries;
1162
dp-arm516dfcb2016-11-03 13:59:26 +00001163 printf("fiptool remove [--blob uuid=...] [--force] "
1164 "[--out FIP_FILENAME] [opts] FIP_FILENAME\n");
1165 printf(" --blob uuid=...\tRemove an image with the given UUID.\n");
dp-arm4972ec52016-05-25 16:20:20 +01001166 printf(" --force\t\tIf the output FIP file already exists, use --force to "
1167 "overwrite it.\n");
1168 printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n");
1169 fputc('\n', stderr);
1170 printf("Specific images are removed with the following options:\n");
1171 for (; toc_entry->cmdline_name != NULL; toc_entry++)
1172 printf(" --%-16s\t%s\n", toc_entry->cmdline_name,
1173 toc_entry->name);
dp-arm29f1b5c2016-09-15 09:58:50 +01001174 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001175}
1176
1177static int version_cmd(int argc, char *argv[])
1178{
1179#ifdef VERSION
1180 puts(VERSION);
1181#else
1182 /* If built from fiptool directory, VERSION is not set. */
1183 puts("Unknown version");
1184#endif
1185 return 0;
1186}
1187
1188static void version_usage(void)
1189{
1190 printf("fiptool version\n");
dp-arm29f1b5c2016-09-15 09:58:50 +01001191 exit(1);
dp-arm4972ec52016-05-25 16:20:20 +01001192}
1193
1194static int help_cmd(int argc, char *argv[])
1195{
1196 int i;
1197
1198 if (argc < 2)
1199 usage();
1200 argc--, argv++;
1201
1202 for (i = 0; i < NELEM(cmds); i++) {
1203 if (strcmp(cmds[i].name, argv[0]) == 0 &&
dp-arm29f1b5c2016-09-15 09:58:50 +01001204 cmds[i].usage != NULL)
dp-arm4972ec52016-05-25 16:20:20 +01001205 cmds[i].usage();
dp-arm4972ec52016-05-25 16:20:20 +01001206 }
1207 if (i == NELEM(cmds))
1208 printf("No help for subcommand '%s'\n", argv[0]);
1209 return 0;
1210}
1211
1212static void usage(void)
1213{
1214 printf("usage: [--verbose] fiptool <command> [<args>]\n");
1215 printf("Global options supported:\n");
1216 printf(" --verbose\tEnable verbose output for all commands.\n");
1217 fputc('\n', stderr);
1218 printf("Commands supported:\n");
1219 printf(" info\t\tList images contained in FIP.\n");
1220 printf(" create\tCreate a new FIP with the given images.\n");
1221 printf(" update\tUpdate an existing FIP with the given images.\n");
1222 printf(" unpack\tUnpack images from FIP.\n");
1223 printf(" remove\tRemove images from FIP.\n");
1224 printf(" version\tShow fiptool version.\n");
1225 printf(" help\t\tShow help for given command.\n");
1226 exit(1);
1227}
1228
1229int main(int argc, char *argv[])
1230{
1231 int i, ret = 0;
1232
dp-arm5cd10ae2016-11-07 10:45:59 +00001233 while (1) {
1234 int c, opt_index = 0;
1235 static struct option opts[] = {
1236 { "verbose", no_argument, NULL, 'v' },
1237 { NULL, no_argument, NULL, 0 }
1238 };
1239
1240 /*
1241 * Set POSIX mode so getopt stops at the first non-option
1242 * which is the subcommand.
1243 */
1244 c = getopt_long(argc, argv, "+v", opts, &opt_index);
1245 if (c == -1)
1246 break;
dp-arm4972ec52016-05-25 16:20:20 +01001247
dp-arm5cd10ae2016-11-07 10:45:59 +00001248 switch (c) {
1249 case 'v':
1250 verbose = 1;
1251 break;
1252 default:
Masahiro Yamada48a24972016-10-26 13:24:26 +09001253 usage();
dp-arm5cd10ae2016-11-07 10:45:59 +00001254 }
dp-arm4972ec52016-05-25 16:20:20 +01001255 }
dp-arm5cd10ae2016-11-07 10:45:59 +00001256 argc -= optind;
1257 argv += optind;
1258 /* Reset optind for subsequent getopt processing. */
1259 optind = 0;
1260
1261 if (argc == 0)
1262 usage();
dp-arm4972ec52016-05-25 16:20:20 +01001263
dp-arm90d2f0e2016-11-14 15:54:32 +00001264 fill_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001265 for (i = 0; i < NELEM(cmds); i++) {
1266 if (strcmp(cmds[i].name, argv[0]) == 0) {
1267 ret = cmds[i].handler(argc, argv);
1268 break;
1269 }
1270 }
1271 if (i == NELEM(cmds))
1272 usage();
dp-arm90d2f0e2016-11-14 15:54:32 +00001273 free_image_descs();
dp-arm4972ec52016-05-25 16:20:20 +01001274 return ret;
1275}