blob: 9375d5cf72de95505da6ee607fd97bba21d261af [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherd7b42322014-03-03 12:19:30 +01002/*
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 Schocherd7b42322014-03-03 12:19:30 +010019 */
20
21#include "mkimage.h"
22#include "fit_common.h"
23#include <image.h>
24#include <u-boot/crc.h>
25
26void 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
35int 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;
Simon Glass05712322020-03-18 11:44:03 -060044 char *config_name = NULL;
Michael van der Westhuizen33a95392014-05-20 15:58:58 +020045 char cmdname[256];
Heiko Schocherd7b42322014-03-03 12:19:30 +010046 int ret;
47 void *key_blob;
48 int c;
49
Michael van der Westhuizen33a95392014-05-20 15:58:58 +020050 strncpy(cmdname, *argv, sizeof(cmdname) - 1);
51 cmdname[sizeof(cmdname) - 1] = '\0';
Simon Glass05712322020-03-18 11:44:03 -060052 while ((c = getopt(argc, argv, "f:k:c:")) != -1)
Heiko Schocherd7b42322014-03-03 12:19:30 +010053 switch (c) {
54 case 'f':
55 fdtfile = optarg;
56 break;
57 case 'k':
58 keyfile = optarg;
59 break;
Simon Glass05712322020-03-18 11:44:03 -060060 case 'c':
61 config_name = optarg;
62 break;
Heiko Schocherd7b42322014-03-03 12:19:30 +010063 default:
64 usage(cmdname);
65 break;
66 }
67
Simon Glassd609a8f2014-06-12 07:24:44 -060068 if (!fdtfile) {
69 fprintf(stderr, "%s: Missing fdt file\n", *argv);
70 usage(*argv);
71 }
72 if (!keyfile) {
73 fprintf(stderr, "%s: Missing key file\n", *argv);
74 usage(*argv);
75 }
76
Luca Boccassibff733f2019-05-14 19:35:02 +010077 ffd = mmap_fdt(cmdname, fdtfile, 0, &fit_blob, &fsbuf, false, true);
Heiko Schocherd7b42322014-03-03 12:19:30 +010078 if (ffd < 0)
79 return EXIT_FAILURE;
Luca Boccassibff733f2019-05-14 19:35:02 +010080 kfd = mmap_fdt(cmdname, keyfile, 0, &key_blob, &ksbuf, false, true);
Thomas Huth7a169ca2015-08-25 17:09:40 +020081 if (kfd < 0)
Heiko Schocherd7b42322014-03-03 12:19:30 +010082 return EXIT_FAILURE;
83
84 image_set_host_blob(key_blob);
Simon Glass05712322020-03-18 11:44:03 -060085 ret = fit_check_sign(fit_blob, key_blob, config_name);
Simon Glassa51991d2014-06-12 07:24:53 -060086 if (!ret) {
Heiko Schocherd7b42322014-03-03 12:19:30 +010087 ret = EXIT_SUCCESS;
Simon Glassa51991d2014-06-12 07:24:53 -060088 fprintf(stderr, "Signature check OK\n");
89 } else {
Heiko Schocherd7b42322014-03-03 12:19:30 +010090 ret = EXIT_FAILURE;
Simon Glassa51991d2014-06-12 07:24:53 -060091 fprintf(stderr, "Signature check Bad (error %d)\n", ret);
92 }
Heiko Schocherd7b42322014-03-03 12:19:30 +010093
94 (void) munmap((void *)fit_blob, fsbuf.st_size);
95 (void) munmap((void *)key_blob, ksbuf.st_size);
96
97 close(ffd);
98 close(kfd);
99 exit(ret);
100}