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