xref: /openbmc/u-boot/tools/ubsha1.c (revision 4ef218f6)
1566a494fSHeiko Schocher /*
2566a494fSHeiko Schocher  * (C) Copyright 2007
3566a494fSHeiko Schocher  * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
4566a494fSHeiko Schocher  *
5566a494fSHeiko Schocher  * See file CREDITS for list of people who contributed to this
6566a494fSHeiko Schocher  * project.
7566a494fSHeiko Schocher  *
8566a494fSHeiko Schocher  * This program is free software; you can redistribute it and/or
9566a494fSHeiko Schocher  * modify it under the terms of the GNU General Public License as
10566a494fSHeiko Schocher  * published by the Free Software Foundation; either version 2 of
11566a494fSHeiko Schocher  * the License, or (at your option) any later version.
12566a494fSHeiko Schocher  *
13566a494fSHeiko Schocher  * This program is distributed in the hope that it will be useful,
14566a494fSHeiko Schocher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15566a494fSHeiko Schocher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16566a494fSHeiko Schocher  * GNU General Public License for more details.
17566a494fSHeiko Schocher  *
18566a494fSHeiko Schocher  * You should have received a copy of the GNU General Public License
19566a494fSHeiko Schocher  * along with this program; if not, write to the Free Software
20566a494fSHeiko Schocher  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21566a494fSHeiko Schocher  * MA 02111-1307 USA
22566a494fSHeiko Schocher  */
23566a494fSHeiko Schocher 
24566a494fSHeiko Schocher #include <stdio.h>
25566a494fSHeiko Schocher #include <stdlib.h>
26566a494fSHeiko Schocher #include <unistd.h>
27566a494fSHeiko Schocher #include <fcntl.h>
28566a494fSHeiko Schocher #include <errno.h>
29566a494fSHeiko Schocher #include <string.h>
30566a494fSHeiko Schocher #include <sys/mman.h>
31566a494fSHeiko Schocher #include <sys/stat.h>
32566a494fSHeiko Schocher #include "sha1.h"
33566a494fSHeiko Schocher 
34566a494fSHeiko Schocher #ifndef __ASSEMBLY__
35566a494fSHeiko Schocher #define	__ASSEMBLY__		/* Dirty trick to get only #defines	*/
36566a494fSHeiko Schocher #endif
37566a494fSHeiko Schocher #include <config.h>
38566a494fSHeiko Schocher #undef	__ASSEMBLY__
39566a494fSHeiko Schocher 
40566a494fSHeiko Schocher #ifndef	O_BINARY		/* should be define'd on __WIN32__ */
41566a494fSHeiko Schocher #define O_BINARY	0
42566a494fSHeiko Schocher #endif
43566a494fSHeiko Schocher 
44566a494fSHeiko Schocher #ifndef MAP_FAILED
45566a494fSHeiko Schocher #define MAP_FAILED (-1)
46566a494fSHeiko Schocher #endif
47566a494fSHeiko Schocher 
48566a494fSHeiko Schocher extern int errno;
49566a494fSHeiko Schocher 
50566a494fSHeiko Schocher extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
51566a494fSHeiko Schocher 
52566a494fSHeiko Schocher int main (int argc, char **argv)
53566a494fSHeiko Schocher {
54566a494fSHeiko Schocher 	unsigned char output[20];
55566a494fSHeiko Schocher 	int i, len;
56566a494fSHeiko Schocher 
57566a494fSHeiko Schocher 	char	*imagefile;
58566a494fSHeiko Schocher 	char	*cmdname = *argv;
59566a494fSHeiko Schocher 	unsigned char	*ptr;
60566a494fSHeiko Schocher 	unsigned char	*data;
61566a494fSHeiko Schocher 	struct stat sbuf;
62566a494fSHeiko Schocher 	unsigned char	*ptroff;
63566a494fSHeiko Schocher 	int	ifd;
64566a494fSHeiko Schocher 	int	off;
65566a494fSHeiko Schocher 
66566a494fSHeiko Schocher 	if (argc > 1) {
67566a494fSHeiko Schocher 		imagefile = argv[1];
68566a494fSHeiko Schocher 		ifd = open (imagefile, O_RDWR|O_BINARY);
69566a494fSHeiko Schocher 		if (ifd < 0) {
70566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't open %s: %s\n",
71566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
72566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
73566a494fSHeiko Schocher 		}
74566a494fSHeiko Schocher 		if (fstat (ifd, &sbuf) < 0) {
75566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't stat %s: %s\n",
76566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
77566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
78566a494fSHeiko Schocher 		}
79566a494fSHeiko Schocher 		len = sbuf.st_size;
80566a494fSHeiko Schocher 		ptr = (unsigned char *)mmap(0, len,
81566a494fSHeiko Schocher 				    PROT_READ, MAP_SHARED, ifd, 0);
82566a494fSHeiko Schocher 		if (ptr == (unsigned char *)MAP_FAILED) {
83566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't read %s: %s\n",
84566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
85566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
86566a494fSHeiko Schocher 		}
87566a494fSHeiko Schocher 
88566a494fSHeiko Schocher 		/* create a copy, so we can blank out the sha1 sum */
89566a494fSHeiko Schocher 		data = malloc (len);
90566a494fSHeiko Schocher 		memcpy (data, ptr, len);
91566a494fSHeiko Schocher 		off = SHA1_SUM_POS;
92566a494fSHeiko Schocher 		ptroff = &data[len +  off];
93566a494fSHeiko Schocher 		for (i = 0; i < SHA1_SUM_LEN; i++) {
94566a494fSHeiko Schocher 			ptroff[i] = 0;
95566a494fSHeiko Schocher 		}
96566a494fSHeiko Schocher 
97566a494fSHeiko Schocher 		sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
98566a494fSHeiko Schocher 
99566a494fSHeiko Schocher 		printf ("U-Boot sum:\n");
100*4ef218f6SWolfgang Denk 	        for (i = 0; i < 20 ; i++) {
101566a494fSHeiko Schocher 	            printf ("%02X ", output[i]);
102566a494fSHeiko Schocher 	        }
103566a494fSHeiko Schocher 	        printf ("\n");
104566a494fSHeiko Schocher 		/* overwrite the sum in the bin file, with the actual */
105566a494fSHeiko Schocher 		lseek (ifd, SHA1_SUM_POS, SEEK_END);
106566a494fSHeiko Schocher 		if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
107566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't write %s: %s\n",
108566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
109566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
110566a494fSHeiko Schocher 		}
111566a494fSHeiko Schocher 
112566a494fSHeiko Schocher 		free (data);
113566a494fSHeiko Schocher 		(void) munmap((void *)ptr, len);
114566a494fSHeiko Schocher 		(void) close (ifd);
115566a494fSHeiko Schocher 	}
116566a494fSHeiko Schocher 
117566a494fSHeiko Schocher 	return EXIT_SUCCESS;
118566a494fSHeiko Schocher }
119