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