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