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 | { |
| 28 | fprintf(stderr, "Usage: %s -f fit file -k key file\n" |
| 29 | " -f ==> set fit file which should be checked'\n" |
| 30 | " -k ==> set key file which contains the key'\n", |
| 31 | cmdname); |
| 32 | exit(EXIT_FAILURE); |
| 33 | } |
| 34 | |
| 35 | int main(int argc, char **argv) |
| 36 | { |
| 37 | int ffd = -1; |
| 38 | int kfd = -1; |
| 39 | struct stat fsbuf; |
| 40 | struct stat ksbuf; |
| 41 | void *fit_blob; |
| 42 | char *fdtfile = NULL; |
| 43 | char *keyfile = NULL; |
Michael van der Westhuizen | 6437501 | 2014-05-20 15:58:58 +0200 | [diff] [blame] | 44 | char cmdname[256]; |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 45 | int ret; |
| 46 | void *key_blob; |
| 47 | int c; |
| 48 | |
Michael van der Westhuizen | 6437501 | 2014-05-20 15:58:58 +0200 | [diff] [blame] | 49 | strncpy(cmdname, *argv, sizeof(cmdname) - 1); |
| 50 | cmdname[sizeof(cmdname) - 1] = '\0'; |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 51 | while ((c = getopt(argc, argv, "f:k:")) != -1) |
| 52 | switch (c) { |
| 53 | case 'f': |
| 54 | fdtfile = optarg; |
| 55 | break; |
| 56 | case 'k': |
| 57 | keyfile = optarg; |
| 58 | break; |
| 59 | default: |
| 60 | usage(cmdname); |
| 61 | break; |
| 62 | } |
| 63 | |
Simon Glass | ba923ca | 2014-06-12 07:24:44 -0600 | [diff] [blame] | 64 | if (!fdtfile) { |
| 65 | fprintf(stderr, "%s: Missing fdt file\n", *argv); |
| 66 | usage(*argv); |
| 67 | } |
| 68 | if (!keyfile) { |
| 69 | fprintf(stderr, "%s: Missing key file\n", *argv); |
| 70 | usage(*argv); |
| 71 | } |
| 72 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 73 | ffd = mmap_fdt(cmdname, fdtfile, 0, &fit_blob, &fsbuf, false); |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 74 | if (ffd < 0) |
| 75 | return EXIT_FAILURE; |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 76 | kfd = mmap_fdt(cmdname, keyfile, 0, &key_blob, &ksbuf, false); |
Thomas Huth | 310ae37 | 2015-08-25 17:09:40 +0200 | [diff] [blame] | 77 | if (kfd < 0) |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 78 | return EXIT_FAILURE; |
| 79 | |
| 80 | image_set_host_blob(key_blob); |
| 81 | ret = fit_check_sign(fit_blob, key_blob); |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 82 | if (!ret) { |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 83 | ret = EXIT_SUCCESS; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 84 | fprintf(stderr, "Signature check OK\n"); |
| 85 | } else { |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 86 | ret = EXIT_FAILURE; |
Simon Glass | ce1400f | 2014-06-12 07:24:53 -0600 | [diff] [blame] | 87 | fprintf(stderr, "Signature check Bad (error %d)\n", ret); |
| 88 | } |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 89 | |
| 90 | (void) munmap((void *)fit_blob, fsbuf.st_size); |
| 91 | (void) munmap((void *)key_blob, ksbuf.st_size); |
| 92 | |
| 93 | close(ffd); |
| 94 | close(kfd); |
| 95 | exit(ret); |
| 96 | } |