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