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