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