Blame view

tools/fit_check_sign.c 2.01 KB
29a23f9d6   Heiko Schocher   tools, fit_check_...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  /*
   * (C) Copyright 2014
   * DENX Software Engineering
   * Heiko Schocher <hs@denx.de>
   *
   * Based on:
   * (C) Copyright 2008 Semihalf
   *
   * (C) Copyright 2000-2004
   * DENX Software Engineering
   * Wolfgang Denk, wd@denx.de
   *
   * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
   *		FIT image specific code abstracted from mkimage.c
   *		some functions added to address abstraction
   *
   * All rights reserved.
   *
   * SPDX-License-Identifier:	GPL-2.0+
   */
  
  #include "mkimage.h"
  #include "fit_common.h"
  #include <image.h>
  #include <u-boot/crc.h>
  
  void usage(char *cmdname)
  {
  	fprintf(stderr, "Usage: %s -f fit file -k key file
  "
  			 "          -f ==> set fit file which should be checked'
  "
  			 "          -k ==> set key file which contains the key'
  ",
  		cmdname);
  	exit(EXIT_FAILURE);
  }
  
  int main(int argc, char **argv)
  {
  	int ffd = -1;
  	int kfd = -1;
  	struct stat fsbuf;
  	struct stat ksbuf;
  	void *fit_blob;
  	char *fdtfile = NULL;
  	char *keyfile = NULL;
64375014c   Michael van der Westhuizen   Prevent a stack o...
48
  	char cmdname[256];
29a23f9d6   Heiko Schocher   tools, fit_check_...
49
50
51
  	int ret;
  	void *key_blob;
  	int c;
64375014c   Michael van der Westhuizen   Prevent a stack o...
52
53
  	strncpy(cmdname, *argv, sizeof(cmdname) - 1);
  	cmdname[sizeof(cmdname) - 1] = '\0';
29a23f9d6   Heiko Schocher   tools, fit_check_...
54
55
56
57
58
59
60
61
62
63
64
65
  	while ((c = getopt(argc, argv, "f:k:")) != -1)
  		switch (c) {
  		case 'f':
  			fdtfile = optarg;
  			break;
  		case 'k':
  			keyfile = optarg;
  			break;
  		default:
  			usage(cmdname);
  			break;
  	}
ba923cab0   Simon Glass   tools: Check argu...
66
67
68
69
70
71
72
73
74
75
  	if (!fdtfile) {
  		fprintf(stderr, "%s: Missing fdt file
  ", *argv);
  		usage(*argv);
  	}
  	if (!keyfile) {
  		fprintf(stderr, "%s: Missing key file
  ", *argv);
  		usage(*argv);
  	}
a94681156   Simon Glass   mkimage: Automati...
76
  	ffd = mmap_fdt(cmdname, fdtfile, 0, &fit_blob, &fsbuf, false);
29a23f9d6   Heiko Schocher   tools, fit_check_...
77
78
  	if (ffd < 0)
  		return EXIT_FAILURE;
a94681156   Simon Glass   mkimage: Automati...
79
  	kfd = mmap_fdt(cmdname, keyfile, 0, &key_blob, &ksbuf, false);
310ae37ed   Thomas Huth   Fix bad return va...
80
  	if (kfd < 0)
29a23f9d6   Heiko Schocher   tools, fit_check_...
81
82
83
84
  		return EXIT_FAILURE;
  
  	image_set_host_blob(key_blob);
  	ret = fit_check_sign(fit_blob, key_blob);
ce1400f69   Simon Glass   Enhance fit_check...
85
  	if (!ret) {
29a23f9d6   Heiko Schocher   tools, fit_check_...
86
  		ret = EXIT_SUCCESS;
ce1400f69   Simon Glass   Enhance fit_check...
87
88
89
  		fprintf(stderr, "Signature check OK
  ");
  	} else {
29a23f9d6   Heiko Schocher   tools, fit_check_...
90
  		ret = EXIT_FAILURE;
ce1400f69   Simon Glass   Enhance fit_check...
91
92
93
  		fprintf(stderr, "Signature check Bad (error %d)
  ", ret);
  	}
29a23f9d6   Heiko Schocher   tools, fit_check_...
94
95
96
97
98
99
100
101
  
  	(void) munmap((void *)fit_blob, fsbuf.st_size);
  	(void) munmap((void *)key_blob, ksbuf.st_size);
  
  	close(ffd);
  	close(kfd);
  	exit(ret);
  }