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