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