xref: /openbmc/u-boot/tools/ubsha1.c (revision 1a459660)
1566a494fSHeiko Schocher /*
2566a494fSHeiko Schocher  * (C) Copyright 2007
3566a494fSHeiko Schocher  * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
4566a494fSHeiko Schocher  *
5*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
6566a494fSHeiko Schocher  */
7566a494fSHeiko Schocher 
82f8d396bSPeter Tyser #include "os_support.h"
9566a494fSHeiko Schocher #include <stdio.h>
10566a494fSHeiko Schocher #include <stdlib.h>
11566a494fSHeiko Schocher #include <unistd.h>
12566a494fSHeiko Schocher #include <fcntl.h>
13566a494fSHeiko Schocher #include <errno.h>
14566a494fSHeiko Schocher #include <string.h>
15566a494fSHeiko Schocher #include <sys/stat.h>
16566a494fSHeiko Schocher #include "sha1.h"
17566a494fSHeiko Schocher 
18566a494fSHeiko Schocher int main (int argc, char **argv)
19566a494fSHeiko Schocher {
20566a494fSHeiko Schocher 	unsigned char output[20];
21566a494fSHeiko Schocher 	int i, len;
22566a494fSHeiko Schocher 
23566a494fSHeiko Schocher 	char	*imagefile;
24566a494fSHeiko Schocher 	char	*cmdname = *argv;
25566a494fSHeiko Schocher 	unsigned char	*ptr;
26566a494fSHeiko Schocher 	unsigned char	*data;
27566a494fSHeiko Schocher 	struct stat sbuf;
28566a494fSHeiko Schocher 	unsigned char	*ptroff;
29566a494fSHeiko Schocher 	int	ifd;
30566a494fSHeiko Schocher 	int	off;
31566a494fSHeiko Schocher 
32566a494fSHeiko Schocher 	if (argc > 1) {
33566a494fSHeiko Schocher 		imagefile = argv[1];
34566a494fSHeiko Schocher 		ifd = open (imagefile, O_RDWR|O_BINARY);
35566a494fSHeiko Schocher 		if (ifd < 0) {
36566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't open %s: %s\n",
37566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
38566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
39566a494fSHeiko Schocher 		}
40566a494fSHeiko Schocher 		if (fstat (ifd, &sbuf) < 0) {
41566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't stat %s: %s\n",
42566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
43566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
44566a494fSHeiko Schocher 		}
45566a494fSHeiko Schocher 		len = sbuf.st_size;
46566a494fSHeiko Schocher 		ptr = (unsigned char *)mmap(0, len,
47566a494fSHeiko Schocher 				    PROT_READ, MAP_SHARED, ifd, 0);
48566a494fSHeiko Schocher 		if (ptr == (unsigned char *)MAP_FAILED) {
49566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't read %s: %s\n",
50566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
51566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
52566a494fSHeiko Schocher 		}
53566a494fSHeiko Schocher 
54566a494fSHeiko Schocher 		/* create a copy, so we can blank out the sha1 sum */
55566a494fSHeiko Schocher 		data = malloc (len);
56566a494fSHeiko Schocher 		memcpy (data, ptr, len);
57566a494fSHeiko Schocher 		off = SHA1_SUM_POS;
58566a494fSHeiko Schocher 		ptroff = &data[len +  off];
59566a494fSHeiko Schocher 		for (i = 0; i < SHA1_SUM_LEN; i++) {
60566a494fSHeiko Schocher 			ptroff[i] = 0;
61566a494fSHeiko Schocher 		}
62566a494fSHeiko Schocher 
63566a494fSHeiko Schocher 		sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
64566a494fSHeiko Schocher 
65566a494fSHeiko Schocher 		printf ("U-Boot sum:\n");
664ef218f6SWolfgang Denk 	        for (i = 0; i < 20 ; i++) {
67566a494fSHeiko Schocher 	            printf ("%02X ", output[i]);
68566a494fSHeiko Schocher 	        }
69566a494fSHeiko Schocher 	        printf ("\n");
70566a494fSHeiko Schocher 		/* overwrite the sum in the bin file, with the actual */
71566a494fSHeiko Schocher 		lseek (ifd, SHA1_SUM_POS, SEEK_END);
72566a494fSHeiko Schocher 		if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
73566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't write %s: %s\n",
74566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
75566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
76566a494fSHeiko Schocher 		}
77566a494fSHeiko Schocher 
78566a494fSHeiko Schocher 		free (data);
79566a494fSHeiko Schocher 		(void) munmap((void *)ptr, len);
80566a494fSHeiko Schocher 		(void) close (ifd);
81566a494fSHeiko Schocher 	}
82566a494fSHeiko Schocher 
83566a494fSHeiko Schocher 	return EXIT_SUCCESS;
84566a494fSHeiko Schocher }
85