Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
| 4 | * DENX Software Engineering |
| 5 | * Heiko Schocher <hs@denx.de> |
| 6 | * |
| 7 | * Based on: |
| 8 | * (C) Copyright 2008 Semihalf |
| 9 | * |
| 10 | * (C) Copyright 2000-2004 |
| 11 | * DENX Software Engineering |
| 12 | * Wolfgang Denk, wd@denx.de |
| 13 | * |
| 14 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 15 | * FIT image specific code abstracted from mkimage.c |
| 16 | * some functions added to address abstraction |
| 17 | * |
| 18 | * All rights reserved. |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "mkimage.h" |
| 22 | #include "fit_common.h" |
| 23 | #include <image.h> |
| 24 | #include <u-boot/crc.h> |
| 25 | |
| 26 | void usage(char *cmdname) |
| 27 | { |
Ilies CHERGUI | 2b139b3 | 2021-01-17 15:52:16 +0000 | [diff] [blame] | 28 | fprintf(stderr, "Usage: %s -f fit file -k key file -c config name\n" |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 29 | " -f ==> set fit file which should be checked'\n" |
Simon Glass | 195f789 | 2021-11-12 12:28:03 -0700 | [diff] [blame] | 30 | " -k ==> set key .dtb file which contains the key'\n" |
Ilies CHERGUI | 2b139b3 | 2021-01-17 15:52:16 +0000 | [diff] [blame] | 31 | " -c ==> set the configuration name'\n", |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 32 | cmdname); |
| 33 | exit(EXIT_FAILURE); |
| 34 | } |
| 35 | |
| 36 | int main(int argc, char **argv) |
| 37 | { |
| 38 | int ffd = -1; |
| 39 | int kfd = -1; |
| 40 | struct stat fsbuf; |
| 41 | struct stat ksbuf; |
| 42 | void *fit_blob; |
| 43 | char *fdtfile = NULL; |
| 44 | char *keyfile = NULL; |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 45 | char *config_name = NULL; |
Michael van der Westhuizen | 6437501 | 2014-05-20 15:58:58 +0200 | [diff] [blame] | 46 | char cmdname[256]; |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 47 | int ret; |
| 48 | void *key_blob; |
| 49 | int c; |
| 50 | |
Michael van der Westhuizen | 6437501 | 2014-05-20 15:58:58 +0200 | [diff] [blame] | 51 | strncpy(cmdname, *argv, sizeof(cmdname) - 1); |
| 52 | cmdname[sizeof(cmdname) - 1] = '\0'; |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 53 | while ((c = getopt(argc, argv, "f:k:c:")) != -1) |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 54 | switch (c) { |
| 55 | case 'f': |
| 56 | fdtfile = optarg; |
| 57 | break; |
| 58 | case 'k': |
| 59 | keyfile = optarg; |
| 60 | break; |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 61 | case 'c': |
| 62 | config_name = optarg; |
| 63 | break; |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 64 | default: |
| 65 | usage(cmdname); |
| 66 | break; |
| 67 | } |
| 68 | |
Simon Glass | ba923ca | 2014-06-12 07:24:44 -0600 | [diff] [blame] | 69 | if (!fdtfile) { |
| 70 | fprintf(stderr, "%s: Missing fdt file\n", *argv); |
| 71 | usage(*argv); |
| 72 | } |
| 73 | if (!keyfile) { |
| 74 | fprintf(stderr, "%s: Missing key file\n", *argv); |
| 75 | usage(*argv); |
| 76 | } |
| 77 | |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 78 | ffd = mmap_fdt(cmdname, fdtfile, 0, &fit_blob, &fsbuf, false, true); |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 79 | if (ffd < 0) |
| 80 | return EXIT_FAILURE; |
Luca Boccassi | 7d57485 | 2019-05-14 19:35:02 +0100 | [diff] [blame] | 81 | kfd = mmap_fdt(cmdname, keyfile, 0, &key_blob, &ksbuf, false, true); |
Thomas Huth | 310ae37 | 2015-08-25 17:09:40 +0200 | [diff] [blame] | 82 | if (kfd < 0) |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 83 | return EXIT_FAILURE; |
| 84 | |
| 85 | image_set_host_blob(key_blob); |
Simon Glass | c3aa81e | 2020-03-18 11:44:03 -0600 | [diff] [blame] | 86 | ret = fit_check_sign(fit_blob, key_blob, config_name); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 87 | if (!ret) { |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 88 | ret = EXIT_SUCCESS; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 89 | fprintf(stderr, "Signature check OK\n"); |
| 90 | } else { |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 91 | ret = EXIT_FAILURE; |
Simon Glass | 195f789 | 2021-11-12 12:28:03 -0700 | [diff] [blame] | 92 | fprintf(stderr, "Signature check bad (error %d)\n", ret); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 93 | } |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 94 | |
| 95 | (void) munmap((void *)fit_blob, fsbuf.st_size); |
| 96 | (void) munmap((void *)key_blob, ksbuf.st_size); |
| 97 | |
| 98 | close(ffd); |
| 99 | close(kfd); |
| 100 | exit(ret); |
| 101 | } |