xref: /openbmc/u-boot/tools/fit_check_sign.c (revision cd23aac4)
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;
45 	char cmdname[50];
46 	int ret;
47 	void *key_blob;
48 	int c;
49 
50 	strcpy(cmdname, *argv);
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 
64 	ffd = mmap_fdt(cmdname, fdtfile, &fit_blob, &fsbuf, 0);
65 	if (ffd < 0)
66 		return EXIT_FAILURE;
67 	kfd = mmap_fdt(cmdname, keyfile, &key_blob, &ksbuf, 0);
68 	if (ffd < 0)
69 		return EXIT_FAILURE;
70 
71 	image_set_host_blob(key_blob);
72 	ret = fit_check_sign(fit_blob, key_blob);
73 
74 	if (ret)
75 		ret = EXIT_SUCCESS;
76 	else
77 		ret = EXIT_FAILURE;
78 
79 	(void) munmap((void *)fit_blob, fsbuf.st_size);
80 	(void) munmap((void *)key_blob, ksbuf.st_size);
81 
82 	close(ffd);
83 	close(kfd);
84 	exit(ret);
85 }
86