dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1 | /* |
Masahiro Yamada | e3a2b31 | 2017-05-08 18:29:03 +0900 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <sys/types.h> |
| 8 | #include <sys/stat.h> |
| 9 | |
| 10 | #include <assert.h> |
| 11 | #include <errno.h> |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 12 | #include <limits.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <stdint.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Masahiro Yamada | e3a2b31 | 2017-05-08 18:29:03 +0900 | [diff] [blame] | 18 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 19 | #include "fiptool.h" |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 20 | #include "tbbr_config.h" |
| 21 | |
| 22 | #define OPT_TOC_ENTRY 0 |
| 23 | #define OPT_PLAT_TOC_FLAGS 1 |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 24 | #define OPT_ALIGN 2 |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 25 | |
| 26 | static int info_cmd(int argc, char *argv[]); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 27 | static void info_usage(int); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 28 | static int create_cmd(int argc, char *argv[]); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 29 | static void create_usage(int); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 30 | static int update_cmd(int argc, char *argv[]); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 31 | static void update_usage(int); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 32 | static int unpack_cmd(int argc, char *argv[]); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 33 | static void unpack_usage(int); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 34 | static int remove_cmd(int argc, char *argv[]); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 35 | static void remove_usage(int); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 36 | static int version_cmd(int argc, char *argv[]); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 37 | static void version_usage(int); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 38 | static int help_cmd(int argc, char *argv[]); |
| 39 | static void usage(void); |
| 40 | |
| 41 | /* Available subcommands. */ |
| 42 | static cmd_t cmds[] = { |
| 43 | { .name = "info", .handler = info_cmd, .usage = info_usage }, |
| 44 | { .name = "create", .handler = create_cmd, .usage = create_usage }, |
| 45 | { .name = "update", .handler = update_cmd, .usage = update_usage }, |
| 46 | { .name = "unpack", .handler = unpack_cmd, .usage = unpack_usage }, |
| 47 | { .name = "remove", .handler = remove_cmd, .usage = remove_usage }, |
| 48 | { .name = "version", .handler = version_cmd, .usage = version_usage }, |
| 49 | { .name = "help", .handler = help_cmd, .usage = NULL }, |
| 50 | }; |
| 51 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 52 | static image_desc_t *image_desc_head; |
| 53 | static size_t nr_image_descs; |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 54 | static const uuid_t uuid_null; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 55 | static int verbose; |
| 56 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 57 | static void vlog(int prio, const char *msg, va_list ap) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 58 | { |
| 59 | char *prefix[] = { "DEBUG", "WARN", "ERROR" }; |
| 60 | |
| 61 | fprintf(stderr, "%s: ", prefix[prio]); |
| 62 | vfprintf(stderr, msg, ap); |
| 63 | fputc('\n', stderr); |
| 64 | } |
| 65 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 66 | static void log_dbgx(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 67 | { |
| 68 | va_list ap; |
| 69 | |
| 70 | va_start(ap, msg); |
| 71 | vlog(LOG_DBG, msg, ap); |
| 72 | va_end(ap); |
| 73 | } |
| 74 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 75 | static void log_warnx(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 76 | { |
| 77 | va_list ap; |
| 78 | |
| 79 | va_start(ap, msg); |
| 80 | vlog(LOG_WARN, msg, ap); |
| 81 | va_end(ap); |
| 82 | } |
| 83 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 84 | static void log_err(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 85 | { |
| 86 | char buf[512]; |
| 87 | va_list ap; |
| 88 | |
| 89 | va_start(ap, msg); |
| 90 | snprintf(buf, sizeof(buf), "%s: %s", msg, strerror(errno)); |
| 91 | vlog(LOG_ERR, buf, ap); |
| 92 | va_end(ap); |
| 93 | exit(1); |
| 94 | } |
| 95 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 96 | static void log_errx(const char *msg, ...) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 97 | { |
| 98 | va_list ap; |
| 99 | |
| 100 | va_start(ap, msg); |
| 101 | vlog(LOG_ERR, msg, ap); |
| 102 | va_end(ap); |
| 103 | exit(1); |
| 104 | } |
| 105 | |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 106 | static char *xstrdup(const char *s, const char *msg) |
| 107 | { |
| 108 | char *d; |
| 109 | |
| 110 | d = strdup(s); |
| 111 | if (d == NULL) |
dp-arm | 5ba3854 | 2016-12-21 14:59:30 +0000 | [diff] [blame] | 112 | log_errx("strdup: %s", msg); |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 113 | return d; |
| 114 | } |
| 115 | |
| 116 | static void *xmalloc(size_t size, const char *msg) |
| 117 | { |
| 118 | void *d; |
| 119 | |
| 120 | d = malloc(size); |
| 121 | if (d == NULL) |
dp-arm | 5ba3854 | 2016-12-21 14:59:30 +0000 | [diff] [blame] | 122 | log_errx("malloc: %s", msg); |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 123 | return d; |
| 124 | } |
| 125 | |
Masahiro Yamada | 03fdf69 | 2017-01-15 00:50:41 +0900 | [diff] [blame] | 126 | static void *xzalloc(size_t size, const char *msg) |
| 127 | { |
| 128 | return memset(xmalloc(size, msg), 0, size); |
| 129 | } |
| 130 | |
Masahiro Yamada | 23f9b9e | 2017-01-27 03:54:02 +0900 | [diff] [blame] | 131 | static void xfwrite(void *buf, size_t size, FILE *fp, const char *filename) |
| 132 | { |
| 133 | if (fwrite(buf, 1, size, fp) != size) |
| 134 | log_errx("Failed to write %s", filename); |
| 135 | } |
| 136 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 137 | static image_desc_t *new_image_desc(const uuid_t *uuid, |
| 138 | const char *name, const char *cmdline_name) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 139 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 140 | image_desc_t *desc; |
| 141 | |
Masahiro Yamada | 03fdf69 | 2017-01-15 00:50:41 +0900 | [diff] [blame] | 142 | desc = xzalloc(sizeof(*desc), |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 143 | "failed to allocate memory for image descriptor"); |
| 144 | memcpy(&desc->uuid, uuid, sizeof(uuid_t)); |
| 145 | desc->name = xstrdup(name, |
| 146 | "failed to allocate memory for image name"); |
| 147 | desc->cmdline_name = xstrdup(cmdline_name, |
| 148 | "failed to allocate memory for image command line name"); |
| 149 | desc->action = DO_UNSPEC; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 150 | return desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 151 | } |
| 152 | |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 153 | static void set_image_desc_action(image_desc_t *desc, int action, |
| 154 | const char *arg) |
| 155 | { |
| 156 | assert(desc != NULL); |
| 157 | |
Evan Lloyd | 04dc344 | 2017-05-25 19:06:47 +0100 | [diff] [blame] | 158 | if (desc->action_arg != (char *)DO_UNSPEC) |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 159 | free(desc->action_arg); |
| 160 | desc->action = action; |
| 161 | desc->action_arg = NULL; |
| 162 | if (arg != NULL) |
| 163 | desc->action_arg = xstrdup(arg, |
| 164 | "failed to allocate memory for argument"); |
| 165 | } |
| 166 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 167 | static void free_image_desc(image_desc_t *desc) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 168 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 169 | free(desc->name); |
| 170 | free(desc->cmdline_name); |
| 171 | free(desc->action_arg); |
Jonathan Wright | 242cd3c | 2018-05-03 15:05:09 +0100 | [diff] [blame] | 172 | if (desc->image) { |
| 173 | free(desc->image->buffer); |
| 174 | free(desc->image); |
| 175 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 176 | free(desc); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 177 | } |
| 178 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 179 | static void add_image_desc(image_desc_t *desc) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 180 | { |
Masahiro Yamada | d224b45 | 2017-01-14 23:22:02 +0900 | [diff] [blame] | 181 | image_desc_t **p = &image_desc_head; |
| 182 | |
Masahiro Yamada | d224b45 | 2017-01-14 23:22:02 +0900 | [diff] [blame] | 183 | while (*p) |
| 184 | p = &(*p)->next; |
| 185 | |
Masahiro Yamada | c2a7d9c | 2017-01-27 12:53:13 +0900 | [diff] [blame] | 186 | assert(*p == NULL); |
Masahiro Yamada | d224b45 | 2017-01-14 23:22:02 +0900 | [diff] [blame] | 187 | *p = desc; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 188 | nr_image_descs++; |
| 189 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 190 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 191 | static void free_image_descs(void) |
| 192 | { |
| 193 | image_desc_t *desc = image_desc_head, *tmp; |
| 194 | |
| 195 | while (desc != NULL) { |
| 196 | tmp = desc->next; |
| 197 | free_image_desc(desc); |
| 198 | desc = tmp; |
| 199 | nr_image_descs--; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 200 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 201 | assert(nr_image_descs == 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 202 | } |
| 203 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 204 | static void fill_image_descs(void) |
| 205 | { |
| 206 | toc_entry_t *toc_entry; |
| 207 | |
| 208 | for (toc_entry = toc_entries; |
| 209 | toc_entry->cmdline_name != NULL; |
| 210 | toc_entry++) { |
| 211 | image_desc_t *desc; |
| 212 | |
| 213 | desc = new_image_desc(&toc_entry->uuid, |
| 214 | toc_entry->name, |
| 215 | toc_entry->cmdline_name); |
| 216 | add_image_desc(desc); |
| 217 | } |
Pankaj Gupta | e4aa1bd | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 218 | #ifdef PLAT_DEF_FIP_UUID |
| 219 | for (toc_entry = plat_def_toc_entries; |
| 220 | toc_entry->cmdline_name != NULL; |
| 221 | toc_entry++) { |
| 222 | image_desc_t *desc; |
| 223 | |
| 224 | desc = new_image_desc(&toc_entry->uuid, |
| 225 | toc_entry->name, |
| 226 | toc_entry->cmdline_name); |
| 227 | add_image_desc(desc); |
| 228 | } |
| 229 | #endif |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 230 | } |
| 231 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 232 | static image_desc_t *lookup_image_desc_from_uuid(const uuid_t *uuid) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 233 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 234 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 235 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 236 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 237 | if (memcmp(&desc->uuid, uuid, sizeof(uuid_t)) == 0) |
| 238 | return desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 239 | return NULL; |
| 240 | } |
| 241 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 242 | static image_desc_t *lookup_image_desc_from_opt(const char *opt) |
| 243 | { |
| 244 | image_desc_t *desc; |
| 245 | |
| 246 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 247 | if (strcmp(desc->cmdline_name, opt) == 0) |
| 248 | return desc; |
| 249 | return NULL; |
| 250 | } |
| 251 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 252 | static void uuid_to_str(char *s, size_t len, const uuid_t *u) |
| 253 | { |
| 254 | assert(len >= (_UUID_STR_LEN + 1)); |
| 255 | |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 256 | snprintf(s, len, |
| 257 | "%02X%02X%02X%02X-%02X%02X-%02X%02X-%04X-%04X%04X%04X", |
| 258 | u->time_low[0], u->time_low[1], u->time_low[2], u->time_low[3], |
| 259 | u->time_mid[0], u->time_mid[1], |
| 260 | u->time_hi_and_version[0], u->time_hi_and_version[1], |
| 261 | (u->clock_seq_hi_and_reserved << 8) | u->clock_seq_low, |
| 262 | (u->node[0] << 8) | u->node[1], |
| 263 | (u->node[2] << 8) | u->node[3], |
| 264 | (u->node[4] << 8) | u->node[5]); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | static void uuid_from_str(uuid_t *u, const char *s) |
| 268 | { |
| 269 | int n; |
| 270 | |
| 271 | if (s == NULL) |
| 272 | log_errx("UUID cannot be NULL"); |
| 273 | if (strlen(s) != _UUID_STR_LEN) |
| 274 | log_errx("Invalid UUID: %s", s); |
| 275 | |
| 276 | n = sscanf(s, |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 277 | "%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", |
| 278 | &u->time_low[0], &u->time_low[1], &u->time_low[2], &u->time_low[3], |
| 279 | &u->time_mid[0], &u->time_mid[1], |
| 280 | &u->time_hi_and_version[0], &u->time_hi_and_version[1], |
| 281 | &u->clock_seq_hi_and_reserved, &u->clock_seq_low, |
| 282 | &u->node[0], &u->node[1], |
| 283 | &u->node[2], &u->node[3], |
| 284 | &u->node[4], &u->node[5]); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 285 | /* |
Andre Przywara | f41e53c | 2019-01-29 09:25:14 +0000 | [diff] [blame] | 286 | * Given the format specifier above, we expect 16 items to be scanned |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 287 | * for a properly formatted UUID. |
| 288 | */ |
Andre Przywara | f41e53c | 2019-01-29 09:25:14 +0000 | [diff] [blame] | 289 | if (n != 16) |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 290 | log_errx("Invalid UUID: %s", s); |
| 291 | } |
| 292 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 293 | static int parse_fip(const char *filename, fip_toc_header_t *toc_header_out) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 294 | { |
Evan Lloyd | 04dc344 | 2017-05-25 19:06:47 +0100 | [diff] [blame] | 295 | struct BLD_PLAT_STAT st; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 296 | FILE *fp; |
| 297 | char *buf, *bufend; |
| 298 | fip_toc_header_t *toc_header; |
| 299 | fip_toc_entry_t *toc_entry; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 300 | int terminated = 0; |
| 301 | |
Evan Lloyd | b193939 | 2017-01-13 14:13:09 +0000 | [diff] [blame] | 302 | fp = fopen(filename, "rb"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 303 | if (fp == NULL) |
| 304 | log_err("fopen %s", filename); |
| 305 | |
| 306 | if (fstat(fileno(fp), &st) == -1) |
| 307 | log_err("fstat %s", filename); |
| 308 | |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 309 | buf = xmalloc(st.st_size, "failed to load file into memory"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 310 | if (fread(buf, 1, st.st_size, fp) != st.st_size) |
| 311 | log_errx("Failed to read %s", filename); |
| 312 | bufend = buf + st.st_size; |
| 313 | fclose(fp); |
| 314 | |
| 315 | if (st.st_size < sizeof(fip_toc_header_t)) |
| 316 | log_errx("FIP %s is truncated", filename); |
| 317 | |
| 318 | toc_header = (fip_toc_header_t *)buf; |
| 319 | toc_entry = (fip_toc_entry_t *)(toc_header + 1); |
| 320 | |
| 321 | if (toc_header->name != TOC_HEADER_NAME) |
| 322 | log_errx("%s is not a FIP file", filename); |
| 323 | |
| 324 | /* Return the ToC header if the caller wants it. */ |
| 325 | if (toc_header_out != NULL) |
| 326 | *toc_header_out = *toc_header; |
| 327 | |
| 328 | /* Walk through each ToC entry in the file. */ |
| 329 | while ((char *)toc_entry + sizeof(*toc_entry) - 1 < bufend) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 330 | image_t *image; |
| 331 | image_desc_t *desc; |
| 332 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 333 | /* Found the ToC terminator, we are done. */ |
| 334 | if (memcmp(&toc_entry->uuid, &uuid_null, sizeof(uuid_t)) == 0) { |
| 335 | terminated = 1; |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Build a new image out of the ToC entry and add it to the |
| 341 | * table of images. |
| 342 | */ |
Masahiro Yamada | d224b45 | 2017-01-14 23:22:02 +0900 | [diff] [blame] | 343 | image = xzalloc(sizeof(*image), |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 344 | "failed to allocate memory for image"); |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 345 | image->toc_e = *toc_entry; |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 346 | image->buffer = xmalloc(toc_entry->size, |
| 347 | "failed to allocate image buffer, is FIP file corrupted?"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 348 | /* Overflow checks before memory copy. */ |
| 349 | if (toc_entry->size > (uint64_t)-1 - toc_entry->offset_address) |
| 350 | log_errx("FIP %s is corrupted", filename); |
| 351 | if (toc_entry->size + toc_entry->offset_address > st.st_size) |
| 352 | log_errx("FIP %s is corrupted", filename); |
| 353 | |
| 354 | memcpy(image->buffer, buf + toc_entry->offset_address, |
| 355 | toc_entry->size); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 356 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 357 | /* If this is an unknown image, create a descriptor for it. */ |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 358 | desc = lookup_image_desc_from_uuid(&toc_entry->uuid); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 359 | if (desc == NULL) { |
| 360 | char name[_UUID_STR_LEN + 1], filename[PATH_MAX]; |
| 361 | |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 362 | uuid_to_str(name, sizeof(name), &toc_entry->uuid); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 363 | snprintf(filename, sizeof(filename), "%s%s", |
| 364 | name, ".bin"); |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 365 | desc = new_image_desc(&toc_entry->uuid, name, "blob"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 366 | desc->action = DO_UNPACK; |
| 367 | desc->action_arg = xstrdup(filename, |
| 368 | "failed to allocate memory for blob filename"); |
| 369 | add_image_desc(desc); |
| 370 | } |
| 371 | |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 372 | assert(desc->image == NULL); |
| 373 | desc->image = image; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 374 | |
| 375 | toc_entry++; |
| 376 | } |
| 377 | |
| 378 | if (terminated == 0) |
| 379 | log_errx("FIP %s does not have a ToC terminator entry", |
| 380 | filename); |
| 381 | free(buf); |
| 382 | return 0; |
| 383 | } |
| 384 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 385 | static image_t *read_image_from_file(const uuid_t *uuid, const char *filename) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 386 | { |
Evan Lloyd | 04dc344 | 2017-05-25 19:06:47 +0100 | [diff] [blame] | 387 | struct BLD_PLAT_STAT st; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 388 | image_t *image; |
| 389 | FILE *fp; |
| 390 | |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 391 | assert(uuid != NULL); |
Evan Lloyd | 04dc344 | 2017-05-25 19:06:47 +0100 | [diff] [blame] | 392 | assert(filename != NULL); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 393 | |
Evan Lloyd | b193939 | 2017-01-13 14:13:09 +0000 | [diff] [blame] | 394 | fp = fopen(filename, "rb"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 395 | if (fp == NULL) |
| 396 | log_err("fopen %s", filename); |
| 397 | |
| 398 | if (fstat(fileno(fp), &st) == -1) |
| 399 | log_errx("fstat %s", filename); |
| 400 | |
Masahiro Yamada | d224b45 | 2017-01-14 23:22:02 +0900 | [diff] [blame] | 401 | image = xzalloc(sizeof(*image), "failed to allocate memory for image"); |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 402 | image->toc_e.uuid = *uuid; |
dp-arm | db0f5e9 | 2016-11-04 10:56:25 +0000 | [diff] [blame] | 403 | image->buffer = xmalloc(st.st_size, "failed to allocate image buffer"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 404 | if (fread(image->buffer, 1, st.st_size, fp) != st.st_size) |
| 405 | log_errx("Failed to read %s", filename); |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 406 | image->toc_e.size = st.st_size; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 407 | |
| 408 | fclose(fp); |
| 409 | return image; |
| 410 | } |
| 411 | |
dp-arm | c1f8e77 | 2016-11-04 10:52:25 +0000 | [diff] [blame] | 412 | static int write_image_to_file(const image_t *image, const char *filename) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 413 | { |
| 414 | FILE *fp; |
| 415 | |
Evan Lloyd | b193939 | 2017-01-13 14:13:09 +0000 | [diff] [blame] | 416 | fp = fopen(filename, "wb"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 417 | if (fp == NULL) |
| 418 | log_err("fopen"); |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 419 | xfwrite(image->buffer, image->toc_e.size, fp, filename); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 420 | fclose(fp); |
| 421 | return 0; |
| 422 | } |
| 423 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 424 | static struct option *add_opt(struct option *opts, size_t *nr_opts, |
| 425 | const char *name, int has_arg, int val) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 426 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 427 | opts = realloc(opts, (*nr_opts + 1) * sizeof(*opts)); |
| 428 | if (opts == NULL) |
| 429 | log_err("realloc"); |
| 430 | opts[*nr_opts].name = name; |
| 431 | opts[*nr_opts].has_arg = has_arg; |
| 432 | opts[*nr_opts].flag = NULL; |
| 433 | opts[*nr_opts].val = val; |
| 434 | ++*nr_opts; |
| 435 | return opts; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 436 | } |
| 437 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 438 | static struct option *fill_common_opts(struct option *opts, size_t *nr_opts, |
| 439 | int has_arg) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 440 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 441 | image_desc_t *desc; |
| 442 | |
| 443 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 444 | opts = add_opt(opts, nr_opts, desc->cmdline_name, has_arg, |
| 445 | OPT_TOC_ENTRY); |
| 446 | return opts; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 447 | } |
| 448 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 449 | static void md_print(const unsigned char *md, size_t len) |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 450 | { |
| 451 | size_t i; |
| 452 | |
| 453 | for (i = 0; i < len; i++) |
| 454 | printf("%02x", md[i]); |
| 455 | } |
| 456 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 457 | static int info_cmd(int argc, char *argv[]) |
| 458 | { |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 459 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 460 | fip_toc_header_t toc_header; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 461 | |
| 462 | if (argc != 2) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 463 | info_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 464 | argc--, argv++; |
| 465 | |
| 466 | parse_fip(argv[0], &toc_header); |
| 467 | |
| 468 | if (verbose) { |
| 469 | log_dbgx("toc_header[name]: 0x%llX", |
| 470 | (unsigned long long)toc_header.name); |
| 471 | log_dbgx("toc_header[serial_number]: 0x%llX", |
| 472 | (unsigned long long)toc_header.serial_number); |
| 473 | log_dbgx("toc_header[flags]: 0x%llX", |
| 474 | (unsigned long long)toc_header.flags); |
| 475 | } |
| 476 | |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 477 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
| 478 | image_t *image = desc->image; |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 479 | |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 480 | if (image == NULL) |
| 481 | continue; |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 482 | printf("%s: offset=0x%llX, size=0x%llX, cmdline=\"--%s\"", |
| 483 | desc->name, |
| 484 | (unsigned long long)image->toc_e.offset_address, |
| 485 | (unsigned long long)image->toc_e.size, |
Masahiro Yamada | eee3931 | 2017-01-15 23:20:00 +0900 | [diff] [blame] | 486 | desc->cmdline_name); |
Evan Lloyd | 04dc344 | 2017-05-25 19:06:47 +0100 | [diff] [blame] | 487 | #ifndef _MSC_VER /* We don't have SHA256 for Visual Studio. */ |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 488 | if (verbose) { |
| 489 | unsigned char md[SHA256_DIGEST_LENGTH]; |
| 490 | |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 491 | SHA256(image->buffer, image->toc_e.size, md); |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 492 | printf(", sha256="); |
| 493 | md_print(md, sizeof(md)); |
| 494 | } |
Evan Lloyd | 04dc344 | 2017-05-25 19:06:47 +0100 | [diff] [blame] | 495 | #endif |
dp-arm | 12e893b | 2016-08-24 13:21:08 +0100 | [diff] [blame] | 496 | putchar('\n'); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 497 | } |
| 498 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 502 | static void info_usage(int exit_status) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 503 | { |
| 504 | printf("fiptool info FIP_FILENAME\n"); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 505 | exit(exit_status); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 508 | static int pack_images(const char *filename, uint64_t toc_flags, unsigned long align) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 509 | { |
| 510 | FILE *fp; |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 511 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 512 | fip_toc_header_t *toc_header; |
| 513 | fip_toc_entry_t *toc_entry; |
| 514 | char *buf; |
Roberto Vargas | 8193576 | 2017-12-19 11:56:57 +0000 | [diff] [blame] | 515 | uint64_t entry_offset, buf_size, payload_size = 0, pad_size; |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 516 | size_t nr_images = 0; |
| 517 | |
| 518 | for (desc = image_desc_head; desc != NULL; desc = desc->next) |
| 519 | if (desc->image != NULL) |
| 520 | nr_images++; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 521 | |
| 522 | buf_size = sizeof(fip_toc_header_t) + |
| 523 | sizeof(fip_toc_entry_t) * (nr_images + 1); |
| 524 | buf = calloc(1, buf_size); |
| 525 | if (buf == NULL) |
| 526 | log_err("calloc"); |
| 527 | |
| 528 | /* Build up header and ToC entries from the image table. */ |
| 529 | toc_header = (fip_toc_header_t *)buf; |
| 530 | toc_header->name = TOC_HEADER_NAME; |
| 531 | toc_header->serial_number = TOC_HEADER_SERIAL_NUMBER; |
| 532 | toc_header->flags = toc_flags; |
| 533 | |
| 534 | toc_entry = (fip_toc_entry_t *)(toc_header + 1); |
| 535 | |
| 536 | entry_offset = buf_size; |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 537 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
| 538 | image_t *image = desc->image; |
| 539 | |
Manish V Badarkhe | 0624d73 | 2021-12-18 11:26:25 +0000 | [diff] [blame] | 540 | if (image == NULL || (image->toc_e.size == 0ULL)) |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 541 | continue; |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 542 | payload_size += image->toc_e.size; |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 543 | entry_offset = (entry_offset + align - 1) & ~(align - 1); |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 544 | image->toc_e.offset_address = entry_offset; |
| 545 | *toc_entry++ = image->toc_e; |
| 546 | entry_offset += image->toc_e.size; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 547 | } |
| 548 | |
Roberto Vargas | 8193576 | 2017-12-19 11:56:57 +0000 | [diff] [blame] | 549 | /* |
| 550 | * Append a null uuid entry to mark the end of ToC entries. |
| 551 | * NOTE the offset address for the last toc_entry must match the fip |
| 552 | * size. |
| 553 | */ |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 554 | memset(toc_entry, 0, sizeof(*toc_entry)); |
Roberto Vargas | 8193576 | 2017-12-19 11:56:57 +0000 | [diff] [blame] | 555 | toc_entry->offset_address = (entry_offset + align - 1) & ~(align - 1); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 556 | |
| 557 | /* Generate the FIP file. */ |
Evan Lloyd | b193939 | 2017-01-13 14:13:09 +0000 | [diff] [blame] | 558 | fp = fopen(filename, "wb"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 559 | if (fp == NULL) |
| 560 | log_err("fopen %s", filename); |
| 561 | |
| 562 | if (verbose) |
| 563 | log_dbgx("Metadata size: %zu bytes", buf_size); |
| 564 | |
Masahiro Yamada | 23f9b9e | 2017-01-27 03:54:02 +0900 | [diff] [blame] | 565 | xfwrite(buf, buf_size, fp, filename); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 566 | |
| 567 | if (verbose) |
| 568 | log_dbgx("Payload size: %zu bytes", payload_size); |
| 569 | |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 570 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
| 571 | image_t *image = desc->image; |
| 572 | |
| 573 | if (image == NULL) |
| 574 | continue; |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 575 | if (fseek(fp, image->toc_e.offset_address, SEEK_SET)) |
| 576 | log_errx("Failed to set file position"); |
| 577 | |
Masahiro Yamada | 2fe0dad | 2017-01-27 03:56:58 +0900 | [diff] [blame] | 578 | xfwrite(image->buffer, image->toc_e.size, fp, filename); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 579 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 580 | |
Roberto Vargas | 8193576 | 2017-12-19 11:56:57 +0000 | [diff] [blame] | 581 | if (fseek(fp, entry_offset, SEEK_SET)) |
| 582 | log_errx("Failed to set file position"); |
| 583 | |
| 584 | pad_size = toc_entry->offset_address - entry_offset; |
| 585 | while (pad_size--) |
| 586 | fputc(0x0, fp); |
| 587 | |
Andreas Färber | 860b5dc | 2018-01-27 16:46:59 +0100 | [diff] [blame] | 588 | free(buf); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 589 | fclose(fp); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * This function is shared between the create and update subcommands. |
| 595 | * The difference between the two subcommands is that when the FIP file |
| 596 | * is created, the parsing of an existing FIP is skipped. This results |
| 597 | * in update_fip() creating the new FIP file from scratch because the |
| 598 | * internal image table is not populated. |
| 599 | */ |
| 600 | static void update_fip(void) |
| 601 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 602 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 603 | |
| 604 | /* Add or replace images in the FIP file. */ |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 605 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 606 | image_t *image; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 607 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 608 | if (desc->action != DO_PACK) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 609 | continue; |
| 610 | |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 611 | image = read_image_from_file(&desc->uuid, |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 612 | desc->action_arg); |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 613 | if (desc->image != NULL) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 614 | if (verbose) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 615 | log_dbgx("Replacing %s with %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 616 | desc->cmdline_name, |
| 617 | desc->action_arg); |
| 618 | } |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 619 | free(desc->image); |
| 620 | desc->image = image; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 621 | } else { |
| 622 | if (verbose) |
| 623 | log_dbgx("Adding image %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 624 | desc->action_arg); |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 625 | desc->image = image; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 626 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 630 | static void parse_plat_toc_flags(const char *arg, unsigned long long *toc_flags) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 631 | { |
| 632 | unsigned long long flags; |
| 633 | char *endptr; |
| 634 | |
| 635 | errno = 0; |
| 636 | flags = strtoull(arg, &endptr, 16); |
| 637 | if (*endptr != '\0' || flags > UINT16_MAX || errno != 0) |
| 638 | log_errx("Invalid platform ToC flags: %s", arg); |
| 639 | /* Platform ToC flags is a 16-bit field occupying bits [32-47]. */ |
| 640 | *toc_flags |= flags << 32; |
| 641 | } |
| 642 | |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 643 | static int is_power_of_2(unsigned long x) |
| 644 | { |
| 645 | return x && !(x & (x - 1)); |
| 646 | } |
| 647 | |
| 648 | static unsigned long get_image_align(char *arg) |
| 649 | { |
| 650 | char *endptr; |
| 651 | unsigned long align; |
| 652 | |
| 653 | errno = 0; |
Andreas Färber | 242a7b7 | 2017-04-21 19:39:10 +0200 | [diff] [blame] | 654 | align = strtoul(arg, &endptr, 0); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 655 | if (*endptr != '\0' || !is_power_of_2(align) || errno != 0) |
| 656 | log_errx("Invalid alignment: %s", arg); |
| 657 | |
| 658 | return align; |
| 659 | } |
| 660 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 661 | static void parse_blob_opt(char *arg, uuid_t *uuid, char *filename, size_t len) |
| 662 | { |
| 663 | char *p; |
| 664 | |
| 665 | for (p = strtok(arg, ","); p != NULL; p = strtok(NULL, ",")) { |
| 666 | if (strncmp(p, "uuid=", strlen("uuid=")) == 0) { |
| 667 | p += strlen("uuid="); |
| 668 | uuid_from_str(uuid, p); |
| 669 | } else if (strncmp(p, "file=", strlen("file=")) == 0) { |
| 670 | p += strlen("file="); |
| 671 | snprintf(filename, len, "%s", p); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 676 | static int create_cmd(int argc, char *argv[]) |
| 677 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 678 | struct option *opts = NULL; |
| 679 | size_t nr_opts = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 680 | unsigned long long toc_flags = 0; |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 681 | unsigned long align = 1; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 682 | |
| 683 | if (argc < 2) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 684 | create_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 685 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 686 | opts = fill_common_opts(opts, &nr_opts, required_argument); |
| 687 | opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument, |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 688 | OPT_PLAT_TOC_FLAGS); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 689 | opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 690 | opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 691 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 692 | |
| 693 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 694 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 695 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 696 | c = getopt_long(argc, argv, "b:", opts, &opt_index); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 697 | if (c == -1) |
| 698 | break; |
| 699 | |
| 700 | switch (c) { |
| 701 | case OPT_TOC_ENTRY: { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 702 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 703 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 704 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 705 | set_image_desc_action(desc, DO_PACK, optarg); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | case OPT_PLAT_TOC_FLAGS: |
| 709 | parse_plat_toc_flags(optarg, &toc_flags); |
| 710 | break; |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 711 | case OPT_ALIGN: |
| 712 | align = get_image_align(optarg); |
| 713 | break; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 714 | case 'b': { |
| 715 | char name[_UUID_STR_LEN + 1]; |
| 716 | char filename[PATH_MAX] = { 0 }; |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 717 | uuid_t uuid = uuid_null; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 718 | image_desc_t *desc; |
| 719 | |
| 720 | parse_blob_opt(optarg, &uuid, |
| 721 | filename, sizeof(filename)); |
| 722 | |
| 723 | if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 || |
| 724 | filename[0] == '\0') |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 725 | create_usage(EXIT_FAILURE); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 726 | |
| 727 | desc = lookup_image_desc_from_uuid(&uuid); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 728 | if (desc == NULL) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 729 | uuid_to_str(name, sizeof(name), &uuid); |
| 730 | desc = new_image_desc(&uuid, name, "blob"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 731 | add_image_desc(desc); |
| 732 | } |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 733 | set_image_desc_action(desc, DO_PACK, filename); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 734 | break; |
| 735 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 736 | default: |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 737 | create_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | argc -= optind; |
| 741 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 742 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 743 | |
| 744 | if (argc == 0) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 745 | create_usage(EXIT_SUCCESS); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 746 | |
| 747 | update_fip(); |
| 748 | |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 749 | pack_images(argv[0], toc_flags, align); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 750 | return 0; |
| 751 | } |
| 752 | |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 753 | static void create_usage(int exit_status) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 754 | { |
| 755 | toc_entry_t *toc_entry = toc_entries; |
| 756 | |
Masahiro Yamada | 7abd0a2 | 2017-01-14 11:04:36 +0900 | [diff] [blame] | 757 | printf("fiptool create [opts] FIP_FILENAME\n"); |
| 758 | printf("\n"); |
| 759 | printf("Options:\n"); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 760 | printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n"); |
Masahiro Yamada | 1eee6a8 | 2017-02-02 16:37:37 +0900 | [diff] [blame] | 761 | printf(" --blob uuid=...,file=...\tAdd an image with the given UUID pointed to by file.\n"); |
| 762 | printf(" --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n"); |
Masahiro Yamada | 1b90015 | 2017-02-02 16:34:14 +0900 | [diff] [blame] | 763 | printf("\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 764 | printf("Specific images are packed with the following options:\n"); |
| 765 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 766 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 767 | toc_entry->name); |
Pankaj Gupta | e4aa1bd | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 768 | #ifdef PLAT_DEF_FIP_UUID |
| 769 | toc_entry = plat_def_toc_entries; |
| 770 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 771 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 772 | toc_entry->name); |
| 773 | #endif |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 774 | exit(exit_status); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | static int update_cmd(int argc, char *argv[]) |
| 778 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 779 | struct option *opts = NULL; |
| 780 | size_t nr_opts = 0; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 781 | char outfile[PATH_MAX] = { 0 }; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 782 | fip_toc_header_t toc_header = { 0 }; |
| 783 | unsigned long long toc_flags = 0; |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 784 | unsigned long align = 1; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 785 | int pflag = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 786 | |
| 787 | if (argc < 2) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 788 | update_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 789 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 790 | opts = fill_common_opts(opts, &nr_opts, required_argument); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 791 | opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 792 | opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 793 | opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); |
| 794 | opts = add_opt(opts, &nr_opts, "plat-toc-flags", required_argument, |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 795 | OPT_PLAT_TOC_FLAGS); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 796 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 797 | |
| 798 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 799 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 800 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 801 | c = getopt_long(argc, argv, "b:o:", opts, &opt_index); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 802 | if (c == -1) |
| 803 | break; |
| 804 | |
| 805 | switch (c) { |
| 806 | case OPT_TOC_ENTRY: { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 807 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 808 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 809 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 810 | set_image_desc_action(desc, DO_PACK, optarg); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 811 | break; |
| 812 | } |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 813 | case OPT_PLAT_TOC_FLAGS: |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 814 | parse_plat_toc_flags(optarg, &toc_flags); |
| 815 | pflag = 1; |
| 816 | break; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 817 | case 'b': { |
| 818 | char name[_UUID_STR_LEN + 1]; |
| 819 | char filename[PATH_MAX] = { 0 }; |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 820 | uuid_t uuid = uuid_null; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 821 | image_desc_t *desc; |
| 822 | |
| 823 | parse_blob_opt(optarg, &uuid, |
| 824 | filename, sizeof(filename)); |
| 825 | |
| 826 | if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 || |
| 827 | filename[0] == '\0') |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 828 | update_usage(EXIT_FAILURE); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 829 | |
| 830 | desc = lookup_image_desc_from_uuid(&uuid); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 831 | if (desc == NULL) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 832 | uuid_to_str(name, sizeof(name), &uuid); |
| 833 | desc = new_image_desc(&uuid, name, "blob"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 834 | add_image_desc(desc); |
| 835 | } |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 836 | set_image_desc_action(desc, DO_PACK, filename); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 837 | break; |
| 838 | } |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 839 | case OPT_ALIGN: |
| 840 | align = get_image_align(optarg); |
| 841 | break; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 842 | case 'o': |
| 843 | snprintf(outfile, sizeof(outfile), "%s", optarg); |
| 844 | break; |
| 845 | default: |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 846 | update_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | argc -= optind; |
| 850 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 851 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 852 | |
| 853 | if (argc == 0) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 854 | update_usage(EXIT_SUCCESS); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 855 | |
| 856 | if (outfile[0] == '\0') |
| 857 | snprintf(outfile, sizeof(outfile), "%s", argv[0]); |
| 858 | |
Masahiro Yamada | ebb6e37 | 2016-12-25 12:41:41 +0900 | [diff] [blame] | 859 | if (access(argv[0], F_OK) == 0) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 860 | parse_fip(argv[0], &toc_header); |
| 861 | |
| 862 | if (pflag) |
| 863 | toc_header.flags &= ~(0xffffULL << 32); |
| 864 | toc_flags = (toc_header.flags |= toc_flags); |
| 865 | |
| 866 | update_fip(); |
| 867 | |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 868 | pack_images(outfile, toc_flags, align); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 869 | return 0; |
| 870 | } |
| 871 | |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 872 | static void update_usage(int exit_status) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 873 | { |
| 874 | toc_entry_t *toc_entry = toc_entries; |
| 875 | |
Masahiro Yamada | 7abd0a2 | 2017-01-14 11:04:36 +0900 | [diff] [blame] | 876 | printf("fiptool update [opts] FIP_FILENAME\n"); |
| 877 | printf("\n"); |
| 878 | printf("Options:\n"); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 879 | printf(" --align <value>\t\tEach image is aligned to <value> (default: 1).\n"); |
Masahiro Yamada | 1eee6a8 | 2017-02-02 16:37:37 +0900 | [diff] [blame] | 880 | printf(" --blob uuid=...,file=...\tAdd or update an image with the given UUID pointed to by file.\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 881 | printf(" --out FIP_FILENAME\t\tSet an alternative output FIP file.\n"); |
Masahiro Yamada | 1eee6a8 | 2017-02-02 16:37:37 +0900 | [diff] [blame] | 882 | printf(" --plat-toc-flags <value>\t16-bit platform specific flag field occupying bits 32-47 in 64-bit ToC header.\n"); |
Masahiro Yamada | 1b90015 | 2017-02-02 16:34:14 +0900 | [diff] [blame] | 883 | printf("\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 884 | printf("Specific images are packed with the following options:\n"); |
| 885 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 886 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 887 | toc_entry->name); |
Pankaj Gupta | e4aa1bd | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 888 | #ifdef PLAT_DEF_FIP_UUID |
| 889 | toc_entry = plat_def_toc_entries; |
| 890 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 891 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 892 | toc_entry->name); |
| 893 | #endif |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 894 | exit(exit_status); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static int unpack_cmd(int argc, char *argv[]) |
| 898 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 899 | struct option *opts = NULL; |
| 900 | size_t nr_opts = 0; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 901 | char outdir[PATH_MAX] = { 0 }; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 902 | image_desc_t *desc; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 903 | int fflag = 0; |
| 904 | int unpack_all = 1; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 905 | |
| 906 | if (argc < 2) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 907 | unpack_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 908 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 909 | opts = fill_common_opts(opts, &nr_opts, required_argument); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 910 | opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 911 | opts = add_opt(opts, &nr_opts, "force", no_argument, 'f'); |
| 912 | opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); |
| 913 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 914 | |
| 915 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 916 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 917 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 918 | c = getopt_long(argc, argv, "b:fo:", opts, &opt_index); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 919 | if (c == -1) |
| 920 | break; |
| 921 | |
| 922 | switch (c) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 923 | case OPT_TOC_ENTRY: { |
| 924 | image_desc_t *desc; |
| 925 | |
| 926 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 927 | set_image_desc_action(desc, DO_UNPACK, optarg); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 928 | unpack_all = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 929 | break; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 930 | } |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 931 | case 'b': { |
| 932 | char name[_UUID_STR_LEN + 1]; |
| 933 | char filename[PATH_MAX] = { 0 }; |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 934 | uuid_t uuid = uuid_null; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 935 | image_desc_t *desc; |
| 936 | |
| 937 | parse_blob_opt(optarg, &uuid, |
| 938 | filename, sizeof(filename)); |
| 939 | |
| 940 | if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0 || |
| 941 | filename[0] == '\0') |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 942 | unpack_usage(EXIT_FAILURE); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 943 | |
| 944 | desc = lookup_image_desc_from_uuid(&uuid); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 945 | if (desc == NULL) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 946 | uuid_to_str(name, sizeof(name), &uuid); |
| 947 | desc = new_image_desc(&uuid, name, "blob"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 948 | add_image_desc(desc); |
| 949 | } |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 950 | set_image_desc_action(desc, DO_UNPACK, filename); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 951 | unpack_all = 0; |
| 952 | break; |
| 953 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 954 | case 'f': |
| 955 | fflag = 1; |
| 956 | break; |
| 957 | case 'o': |
| 958 | snprintf(outdir, sizeof(outdir), "%s", optarg); |
| 959 | break; |
| 960 | default: |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 961 | unpack_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | argc -= optind; |
| 965 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 966 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 967 | |
| 968 | if (argc == 0) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 969 | unpack_usage(EXIT_SUCCESS); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 970 | |
| 971 | parse_fip(argv[0], NULL); |
| 972 | |
| 973 | if (outdir[0] != '\0') |
| 974 | if (chdir(outdir) == -1) |
| 975 | log_err("chdir %s", outdir); |
| 976 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 977 | /* Unpack all specified images. */ |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 978 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 979 | char file[PATH_MAX]; |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 980 | image_t *image = desc->image; |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 981 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 982 | if (!unpack_all && desc->action != DO_UNPACK) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 983 | continue; |
| 984 | |
| 985 | /* Build filename. */ |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 986 | if (desc->action_arg == NULL) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 987 | snprintf(file, sizeof(file), "%s.bin", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 988 | desc->cmdline_name); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 989 | else |
| 990 | snprintf(file, sizeof(file), "%s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 991 | desc->action_arg); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 992 | |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 993 | if (image == NULL) { |
| 994 | if (!unpack_all) |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 995 | log_warnx("%s does not exist in %s", |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 996 | file, argv[0]); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 997 | continue; |
| 998 | } |
| 999 | |
| 1000 | if (access(file, F_OK) != 0 || fflag) { |
| 1001 | if (verbose) |
| 1002 | log_dbgx("Unpacking %s", file); |
dp-arm | 715ef42 | 2016-08-30 14:18:58 +0100 | [diff] [blame] | 1003 | write_image_to_file(image, file); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1004 | } else { |
| 1005 | log_warnx("File %s already exists, use --force to overwrite it", |
| 1006 | file); |
| 1007 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1008 | } |
| 1009 | |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1010 | return 0; |
| 1011 | } |
| 1012 | |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1013 | static void unpack_usage(int exit_status) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1014 | { |
| 1015 | toc_entry_t *toc_entry = toc_entries; |
| 1016 | |
Masahiro Yamada | 7abd0a2 | 2017-01-14 11:04:36 +0900 | [diff] [blame] | 1017 | printf("fiptool unpack [opts] FIP_FILENAME\n"); |
| 1018 | printf("\n"); |
| 1019 | printf("Options:\n"); |
Masahiro Yamada | 1eee6a8 | 2017-02-02 16:37:37 +0900 | [diff] [blame] | 1020 | printf(" --blob uuid=...,file=...\tUnpack an image with the given UUID to file.\n"); |
| 1021 | printf(" --force\t\t\tIf the output file already exists, use --force to overwrite it.\n"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1022 | printf(" --out path\t\t\tSet the output directory path.\n"); |
Masahiro Yamada | 1b90015 | 2017-02-02 16:34:14 +0900 | [diff] [blame] | 1023 | printf("\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1024 | printf("Specific images are unpacked with the following options:\n"); |
| 1025 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 1026 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 1027 | toc_entry->name); |
Pankaj Gupta | e4aa1bd | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 1028 | #ifdef PLAT_DEF_FIP_UUID |
| 1029 | toc_entry = plat_def_toc_entries; |
| 1030 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 1031 | printf(" --%-16s FILENAME\t%s\n", toc_entry->cmdline_name, |
| 1032 | toc_entry->name); |
| 1033 | #endif |
Masahiro Yamada | 1b90015 | 2017-02-02 16:34:14 +0900 | [diff] [blame] | 1034 | printf("\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1035 | printf("If no options are provided, all images will be unpacked.\n"); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1036 | exit(exit_status); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static int remove_cmd(int argc, char *argv[]) |
| 1040 | { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1041 | struct option *opts = NULL; |
| 1042 | size_t nr_opts = 0; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1043 | char outfile[PATH_MAX] = { 0 }; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1044 | fip_toc_header_t toc_header; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1045 | image_desc_t *desc; |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 1046 | unsigned long align = 1; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1047 | int fflag = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1048 | |
| 1049 | if (argc < 2) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1050 | remove_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1051 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1052 | opts = fill_common_opts(opts, &nr_opts, no_argument); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 1053 | opts = add_opt(opts, &nr_opts, "align", required_argument, OPT_ALIGN); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1054 | opts = add_opt(opts, &nr_opts, "blob", required_argument, 'b'); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1055 | opts = add_opt(opts, &nr_opts, "force", no_argument, 'f'); |
| 1056 | opts = add_opt(opts, &nr_opts, "out", required_argument, 'o'); |
| 1057 | opts = add_opt(opts, &nr_opts, NULL, 0, 0); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1058 | |
| 1059 | while (1) { |
dp-arm | fe92b89 | 2016-11-07 11:13:54 +0000 | [diff] [blame] | 1060 | int c, opt_index = 0; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1061 | |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1062 | c = getopt_long(argc, argv, "b:fo:", opts, &opt_index); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1063 | if (c == -1) |
| 1064 | break; |
| 1065 | |
| 1066 | switch (c) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1067 | case OPT_TOC_ENTRY: { |
| 1068 | image_desc_t *desc; |
| 1069 | |
| 1070 | desc = lookup_image_desc_from_opt(opts[opt_index].name); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 1071 | set_image_desc_action(desc, DO_REMOVE, NULL); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1072 | break; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1073 | } |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 1074 | case OPT_ALIGN: |
| 1075 | align = get_image_align(optarg); |
| 1076 | break; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1077 | case 'b': { |
| 1078 | char name[_UUID_STR_LEN + 1], filename[PATH_MAX]; |
Roberto Vargas | eace8f1 | 2018-04-26 13:36:53 +0100 | [diff] [blame] | 1079 | uuid_t uuid = uuid_null; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1080 | image_desc_t *desc; |
| 1081 | |
| 1082 | parse_blob_opt(optarg, &uuid, |
| 1083 | filename, sizeof(filename)); |
| 1084 | |
| 1085 | if (memcmp(&uuid, &uuid_null, sizeof(uuid_t)) == 0) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1086 | remove_usage(EXIT_FAILURE); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1087 | |
| 1088 | desc = lookup_image_desc_from_uuid(&uuid); |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 1089 | if (desc == NULL) { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1090 | uuid_to_str(name, sizeof(name), &uuid); |
| 1091 | desc = new_image_desc(&uuid, name, "blob"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1092 | add_image_desc(desc); |
| 1093 | } |
dp-arm | fb73231 | 2016-12-30 09:55:48 +0000 | [diff] [blame] | 1094 | set_image_desc_action(desc, DO_REMOVE, NULL); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1095 | break; |
| 1096 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1097 | case 'f': |
| 1098 | fflag = 1; |
| 1099 | break; |
| 1100 | case 'o': |
| 1101 | snprintf(outfile, sizeof(outfile), "%s", optarg); |
| 1102 | break; |
| 1103 | default: |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1104 | remove_usage(EXIT_FAILURE); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | argc -= optind; |
| 1108 | argv += optind; |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1109 | free(opts); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1110 | |
| 1111 | if (argc == 0) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1112 | remove_usage(EXIT_SUCCESS); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1113 | |
| 1114 | if (outfile[0] != '\0' && access(outfile, F_OK) == 0 && !fflag) |
| 1115 | log_errx("File %s already exists, use --force to overwrite it", |
| 1116 | outfile); |
| 1117 | |
| 1118 | if (outfile[0] == '\0') |
| 1119 | snprintf(outfile, sizeof(outfile), "%s", argv[0]); |
| 1120 | |
| 1121 | parse_fip(argv[0], &toc_header); |
| 1122 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1123 | for (desc = image_desc_head; desc != NULL; desc = desc->next) { |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1124 | if (desc->action != DO_REMOVE) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1125 | continue; |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1126 | |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 1127 | if (desc->image != NULL) { |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1128 | if (verbose) |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1129 | log_dbgx("Removing %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1130 | desc->cmdline_name); |
dp-arm | afa1efa | 2017-02-14 15:22:13 +0000 | [diff] [blame] | 1131 | free(desc->image); |
| 1132 | desc->image = NULL; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1133 | } else { |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1134 | log_warnx("%s does not exist in %s", |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1135 | desc->cmdline_name, argv[0]); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 1139 | pack_images(outfile, toc_header.flags, align); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1140 | return 0; |
| 1141 | } |
| 1142 | |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1143 | static void remove_usage(int exit_status) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1144 | { |
| 1145 | toc_entry_t *toc_entry = toc_entries; |
| 1146 | |
Masahiro Yamada | 7abd0a2 | 2017-01-14 11:04:36 +0900 | [diff] [blame] | 1147 | printf("fiptool remove [opts] FIP_FILENAME\n"); |
| 1148 | printf("\n"); |
| 1149 | printf("Options:\n"); |
Masahiro Yamada | 4d87eb4 | 2016-12-25 13:52:22 +0900 | [diff] [blame] | 1150 | printf(" --align <value>\tEach image is aligned to <value> (default: 1).\n"); |
dp-arm | 516dfcb | 2016-11-03 13:59:26 +0000 | [diff] [blame] | 1151 | printf(" --blob uuid=...\tRemove an image with the given UUID.\n"); |
Masahiro Yamada | 1eee6a8 | 2017-02-02 16:37:37 +0900 | [diff] [blame] | 1152 | printf(" --force\t\tIf the output FIP file already exists, use --force to overwrite it.\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1153 | printf(" --out FIP_FILENAME\tSet an alternative output FIP file.\n"); |
Masahiro Yamada | 1b90015 | 2017-02-02 16:34:14 +0900 | [diff] [blame] | 1154 | printf("\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1155 | printf("Specific images are removed with the following options:\n"); |
| 1156 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 1157 | printf(" --%-16s\t%s\n", toc_entry->cmdline_name, |
| 1158 | toc_entry->name); |
Pankaj Gupta | e4aa1bd | 2020-12-09 14:02:38 +0530 | [diff] [blame] | 1159 | #ifdef PLAT_DEF_FIP_UUID |
| 1160 | toc_entry = plat_def_toc_entries; |
| 1161 | for (; toc_entry->cmdline_name != NULL; toc_entry++) |
| 1162 | printf(" --%-16s\t%s\n", toc_entry->cmdline_name, |
| 1163 | toc_entry->name); |
| 1164 | #endif |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1165 | exit(exit_status); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | static int version_cmd(int argc, char *argv[]) |
| 1169 | { |
| 1170 | #ifdef VERSION |
| 1171 | puts(VERSION); |
| 1172 | #else |
| 1173 | /* If built from fiptool directory, VERSION is not set. */ |
| 1174 | puts("Unknown version"); |
| 1175 | #endif |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1179 | static void version_usage(int exit_status) |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1180 | { |
| 1181 | printf("fiptool version\n"); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1182 | exit(exit_status); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | static int help_cmd(int argc, char *argv[]) |
| 1186 | { |
| 1187 | int i; |
| 1188 | |
| 1189 | if (argc < 2) |
| 1190 | usage(); |
| 1191 | argc--, argv++; |
| 1192 | |
| 1193 | for (i = 0; i < NELEM(cmds); i++) { |
| 1194 | if (strcmp(cmds[i].name, argv[0]) == 0 && |
dp-arm | 29f1b5c | 2016-09-15 09:58:50 +0100 | [diff] [blame] | 1195 | cmds[i].usage != NULL) |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1196 | cmds[i].usage(EXIT_SUCCESS); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1197 | } |
| 1198 | if (i == NELEM(cmds)) |
| 1199 | printf("No help for subcommand '%s'\n", argv[0]); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static void usage(void) |
| 1204 | { |
Masahiro Yamada | 252c336 | 2017-01-13 02:13:06 +0900 | [diff] [blame] | 1205 | printf("usage: fiptool [--verbose] <command> [<args>]\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1206 | printf("Global options supported:\n"); |
| 1207 | printf(" --verbose\tEnable verbose output for all commands.\n"); |
Masahiro Yamada | 1b90015 | 2017-02-02 16:34:14 +0900 | [diff] [blame] | 1208 | printf("\n"); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1209 | printf("Commands supported:\n"); |
| 1210 | printf(" info\t\tList images contained in FIP.\n"); |
| 1211 | printf(" create\tCreate a new FIP with the given images.\n"); |
| 1212 | printf(" update\tUpdate an existing FIP with the given images.\n"); |
| 1213 | printf(" unpack\tUnpack images from FIP.\n"); |
| 1214 | printf(" remove\tRemove images from FIP.\n"); |
| 1215 | printf(" version\tShow fiptool version.\n"); |
| 1216 | printf(" help\t\tShow help for given command.\n"); |
Leonardo Sandoval | fc6d385 | 2020-06-29 18:09:24 -0500 | [diff] [blame] | 1217 | exit(EXIT_SUCCESS); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | int main(int argc, char *argv[]) |
| 1221 | { |
| 1222 | int i, ret = 0; |
| 1223 | |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1224 | while (1) { |
| 1225 | int c, opt_index = 0; |
| 1226 | static struct option opts[] = { |
| 1227 | { "verbose", no_argument, NULL, 'v' }, |
| 1228 | { NULL, no_argument, NULL, 0 } |
| 1229 | }; |
| 1230 | |
| 1231 | /* |
| 1232 | * Set POSIX mode so getopt stops at the first non-option |
| 1233 | * which is the subcommand. |
| 1234 | */ |
| 1235 | c = getopt_long(argc, argv, "+v", opts, &opt_index); |
| 1236 | if (c == -1) |
| 1237 | break; |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1238 | |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1239 | switch (c) { |
| 1240 | case 'v': |
| 1241 | verbose = 1; |
| 1242 | break; |
| 1243 | default: |
Masahiro Yamada | 48a2497 | 2016-10-26 13:24:26 +0900 | [diff] [blame] | 1244 | usage(); |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1245 | } |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1246 | } |
dp-arm | 5cd10ae | 2016-11-07 10:45:59 +0000 | [diff] [blame] | 1247 | argc -= optind; |
| 1248 | argv += optind; |
| 1249 | /* Reset optind for subsequent getopt processing. */ |
| 1250 | optind = 0; |
| 1251 | |
| 1252 | if (argc == 0) |
| 1253 | usage(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1254 | |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1255 | fill_image_descs(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1256 | for (i = 0; i < NELEM(cmds); i++) { |
| 1257 | if (strcmp(cmds[i].name, argv[0]) == 0) { |
| 1258 | ret = cmds[i].handler(argc, argv); |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | if (i == NELEM(cmds)) |
| 1263 | usage(); |
dp-arm | 90d2f0e | 2016-11-14 15:54:32 +0000 | [diff] [blame] | 1264 | free_image_descs(); |
dp-arm | 4972ec5 | 2016-05-25 16:20:20 +0100 | [diff] [blame] | 1265 | return ret; |
| 1266 | } |