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