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