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