xref: /openbmc/u-boot/tools/ubsha1.c (revision 2f8d396b)
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 
24*2f8d396bSPeter Tyser #include "os_support.h"
25566a494fSHeiko Schocher #include <stdio.h>
26566a494fSHeiko Schocher #include <stdlib.h>
27566a494fSHeiko Schocher #include <unistd.h>
28566a494fSHeiko Schocher #include <fcntl.h>
29566a494fSHeiko Schocher #include <errno.h>
30566a494fSHeiko Schocher #include <string.h>
31*2f8d396bSPeter Tyser #ifndef __MINGW32__
32566a494fSHeiko Schocher #include <sys/mman.h>
33*2f8d396bSPeter Tyser #endif
34566a494fSHeiko Schocher #include <sys/stat.h>
35566a494fSHeiko Schocher #include "sha1.h"
36566a494fSHeiko Schocher 
37566a494fSHeiko Schocher #ifndef __ASSEMBLY__
38566a494fSHeiko Schocher #define	__ASSEMBLY__		/* Dirty trick to get only #defines	*/
39566a494fSHeiko Schocher #endif
40566a494fSHeiko Schocher #include <config.h>
41566a494fSHeiko Schocher #undef	__ASSEMBLY__
42566a494fSHeiko Schocher 
43566a494fSHeiko Schocher #ifndef	O_BINARY		/* should be define'd on __WIN32__ */
44566a494fSHeiko Schocher #define O_BINARY	0
45566a494fSHeiko Schocher #endif
46566a494fSHeiko Schocher 
47566a494fSHeiko Schocher #ifndef MAP_FAILED
48566a494fSHeiko Schocher #define MAP_FAILED (-1)
49566a494fSHeiko Schocher #endif
50566a494fSHeiko Schocher 
51566a494fSHeiko Schocher extern int errno;
52566a494fSHeiko Schocher 
53566a494fSHeiko Schocher extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
54566a494fSHeiko Schocher 
55566a494fSHeiko Schocher int main (int argc, char **argv)
56566a494fSHeiko Schocher {
57566a494fSHeiko Schocher 	unsigned char output[20];
58566a494fSHeiko Schocher 	int i, len;
59566a494fSHeiko Schocher 
60566a494fSHeiko Schocher 	char	*imagefile;
61566a494fSHeiko Schocher 	char	*cmdname = *argv;
62566a494fSHeiko Schocher 	unsigned char	*ptr;
63566a494fSHeiko Schocher 	unsigned char	*data;
64566a494fSHeiko Schocher 	struct stat sbuf;
65566a494fSHeiko Schocher 	unsigned char	*ptroff;
66566a494fSHeiko Schocher 	int	ifd;
67566a494fSHeiko Schocher 	int	off;
68566a494fSHeiko Schocher 
69566a494fSHeiko Schocher 	if (argc > 1) {
70566a494fSHeiko Schocher 		imagefile = argv[1];
71566a494fSHeiko Schocher 		ifd = open (imagefile, O_RDWR|O_BINARY);
72566a494fSHeiko Schocher 		if (ifd < 0) {
73566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't open %s: %s\n",
74566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
75566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
76566a494fSHeiko Schocher 		}
77566a494fSHeiko Schocher 		if (fstat (ifd, &sbuf) < 0) {
78566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't stat %s: %s\n",
79566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
80566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
81566a494fSHeiko Schocher 		}
82566a494fSHeiko Schocher 		len = sbuf.st_size;
83566a494fSHeiko Schocher 		ptr = (unsigned char *)mmap(0, len,
84566a494fSHeiko Schocher 				    PROT_READ, MAP_SHARED, ifd, 0);
85566a494fSHeiko Schocher 		if (ptr == (unsigned char *)MAP_FAILED) {
86566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't read %s: %s\n",
87566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
88566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
89566a494fSHeiko Schocher 		}
90566a494fSHeiko Schocher 
91566a494fSHeiko Schocher 		/* create a copy, so we can blank out the sha1 sum */
92566a494fSHeiko Schocher 		data = malloc (len);
93566a494fSHeiko Schocher 		memcpy (data, ptr, len);
94566a494fSHeiko Schocher 		off = SHA1_SUM_POS;
95566a494fSHeiko Schocher 		ptroff = &data[len +  off];
96566a494fSHeiko Schocher 		for (i = 0; i < SHA1_SUM_LEN; i++) {
97566a494fSHeiko Schocher 			ptroff[i] = 0;
98566a494fSHeiko Schocher 		}
99566a494fSHeiko Schocher 
100566a494fSHeiko Schocher 		sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
101566a494fSHeiko Schocher 
102566a494fSHeiko Schocher 		printf ("U-Boot sum:\n");
1034ef218f6SWolfgang Denk 	        for (i = 0; i < 20 ; i++) {
104566a494fSHeiko Schocher 	            printf ("%02X ", output[i]);
105566a494fSHeiko Schocher 	        }
106566a494fSHeiko Schocher 	        printf ("\n");
107566a494fSHeiko Schocher 		/* overwrite the sum in the bin file, with the actual */
108566a494fSHeiko Schocher 		lseek (ifd, SHA1_SUM_POS, SEEK_END);
109566a494fSHeiko Schocher 		if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
110566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't write %s: %s\n",
111566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
112566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
113566a494fSHeiko Schocher 		}
114566a494fSHeiko Schocher 
115566a494fSHeiko Schocher 		free (data);
116566a494fSHeiko Schocher 		(void) munmap((void *)ptr, len);
117566a494fSHeiko Schocher 		(void) close (ifd);
118566a494fSHeiko Schocher 	}
119566a494fSHeiko Schocher 
120566a494fSHeiko Schocher 	return EXIT_SUCCESS;
121566a494fSHeiko Schocher }
122