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