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