xref: /openbmc/u-boot/tools/ubsha1.c (revision 566a494f)
1*566a494fSHeiko Schocher /*
2*566a494fSHeiko Schocher  * (C) Copyright 2007
3*566a494fSHeiko Schocher  * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
4*566a494fSHeiko Schocher  *
5*566a494fSHeiko Schocher  * See file CREDITS for list of people who contributed to this
6*566a494fSHeiko Schocher  * project.
7*566a494fSHeiko Schocher  *
8*566a494fSHeiko Schocher  * This program is free software; you can redistribute it and/or
9*566a494fSHeiko Schocher  * modify it under the terms of the GNU General Public License as
10*566a494fSHeiko Schocher  * published by the Free Software Foundation; either version 2 of
11*566a494fSHeiko Schocher  * the License, or (at your option) any later version.
12*566a494fSHeiko Schocher  *
13*566a494fSHeiko Schocher  * This program is distributed in the hope that it will be useful,
14*566a494fSHeiko Schocher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15*566a494fSHeiko Schocher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*566a494fSHeiko Schocher  * GNU General Public License for more details.
17*566a494fSHeiko Schocher  *
18*566a494fSHeiko Schocher  * You should have received a copy of the GNU General Public License
19*566a494fSHeiko Schocher  * along with this program; if not, write to the Free Software
20*566a494fSHeiko Schocher  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21*566a494fSHeiko Schocher  * MA 02111-1307 USA
22*566a494fSHeiko Schocher  */
23*566a494fSHeiko Schocher 
24*566a494fSHeiko Schocher #include <stdio.h>
25*566a494fSHeiko Schocher #include <stdlib.h>
26*566a494fSHeiko Schocher #include <unistd.h>
27*566a494fSHeiko Schocher #include <fcntl.h>
28*566a494fSHeiko Schocher #include <errno.h>
29*566a494fSHeiko Schocher #include <string.h>
30*566a494fSHeiko Schocher #include <sys/mman.h>
31*566a494fSHeiko Schocher #include <sys/stat.h>
32*566a494fSHeiko Schocher #include "sha1.h"
33*566a494fSHeiko Schocher 
34*566a494fSHeiko Schocher #ifndef __ASSEMBLY__
35*566a494fSHeiko Schocher #define	__ASSEMBLY__		/* Dirty trick to get only #defines	*/
36*566a494fSHeiko Schocher #endif
37*566a494fSHeiko Schocher #include <config.h>
38*566a494fSHeiko Schocher #undef	__ASSEMBLY__
39*566a494fSHeiko Schocher 
40*566a494fSHeiko Schocher #ifndef	O_BINARY		/* should be define'd on __WIN32__ */
41*566a494fSHeiko Schocher #define O_BINARY	0
42*566a494fSHeiko Schocher #endif
43*566a494fSHeiko Schocher 
44*566a494fSHeiko Schocher #ifndef MAP_FAILED
45*566a494fSHeiko Schocher #define MAP_FAILED (-1)
46*566a494fSHeiko Schocher #endif
47*566a494fSHeiko Schocher 
48*566a494fSHeiko Schocher extern int errno;
49*566a494fSHeiko Schocher 
50*566a494fSHeiko Schocher extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
51*566a494fSHeiko Schocher 
52*566a494fSHeiko Schocher int main (int argc, char **argv)
53*566a494fSHeiko Schocher {
54*566a494fSHeiko Schocher 	unsigned char output[20];
55*566a494fSHeiko Schocher 	int i, len;
56*566a494fSHeiko Schocher 
57*566a494fSHeiko Schocher 	char	*imagefile;
58*566a494fSHeiko Schocher 	char	*cmdname = *argv;
59*566a494fSHeiko Schocher 	unsigned char	*ptr;
60*566a494fSHeiko Schocher 	unsigned char	*data;
61*566a494fSHeiko Schocher 	struct stat sbuf;
62*566a494fSHeiko Schocher 	unsigned char	*ptroff;
63*566a494fSHeiko Schocher 	int	ifd;
64*566a494fSHeiko Schocher 	int	off;
65*566a494fSHeiko Schocher 
66*566a494fSHeiko Schocher 	if (argc > 1) {
67*566a494fSHeiko Schocher 		imagefile = argv[1];
68*566a494fSHeiko Schocher 		ifd = open (imagefile, O_RDWR|O_BINARY);
69*566a494fSHeiko Schocher 		if (ifd < 0) {
70*566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't open %s: %s\n",
71*566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
72*566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
73*566a494fSHeiko Schocher 		}
74*566a494fSHeiko Schocher 		if (fstat (ifd, &sbuf) < 0) {
75*566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't stat %s: %s\n",
76*566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
77*566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
78*566a494fSHeiko Schocher 		}
79*566a494fSHeiko Schocher 		len = sbuf.st_size;
80*566a494fSHeiko Schocher 		ptr = (unsigned char *)mmap(0, len,
81*566a494fSHeiko Schocher 				    PROT_READ, MAP_SHARED, ifd, 0);
82*566a494fSHeiko Schocher 		if (ptr == (unsigned char *)MAP_FAILED) {
83*566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't read %s: %s\n",
84*566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
85*566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
86*566a494fSHeiko Schocher 		}
87*566a494fSHeiko Schocher 
88*566a494fSHeiko Schocher 		/* create a copy, so we can blank out the sha1 sum */
89*566a494fSHeiko Schocher 		data = malloc (len);
90*566a494fSHeiko Schocher 		memcpy (data, ptr, len);
91*566a494fSHeiko Schocher 		off = SHA1_SUM_POS;
92*566a494fSHeiko Schocher 		ptroff = &data[len +  off];
93*566a494fSHeiko Schocher 		for (i = 0; i < SHA1_SUM_LEN; i++) {
94*566a494fSHeiko Schocher 			ptroff[i] = 0;
95*566a494fSHeiko Schocher 		}
96*566a494fSHeiko Schocher 
97*566a494fSHeiko Schocher 		sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
98*566a494fSHeiko Schocher 
99*566a494fSHeiko Schocher 		printf ("U-Boot sum:\n");
100*566a494fSHeiko Schocher 	        for (i = 0; i < 20 ; i++)
101*566a494fSHeiko Schocher 	        {
102*566a494fSHeiko Schocher 	            printf ("%02X ", output[i]);
103*566a494fSHeiko Schocher 	        }
104*566a494fSHeiko Schocher 	        printf ("\n");
105*566a494fSHeiko Schocher 		/* overwrite the sum in the bin file, with the actual */
106*566a494fSHeiko Schocher 		lseek (ifd, SHA1_SUM_POS, SEEK_END);
107*566a494fSHeiko Schocher 		if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
108*566a494fSHeiko Schocher 			fprintf (stderr, "%s: Can't write %s: %s\n",
109*566a494fSHeiko Schocher 				cmdname, imagefile, strerror(errno));
110*566a494fSHeiko Schocher 			exit (EXIT_FAILURE);
111*566a494fSHeiko Schocher 		}
112*566a494fSHeiko Schocher 
113*566a494fSHeiko Schocher 		free (data);
114*566a494fSHeiko Schocher 		(void) munmap((void *)ptr, len);
115*566a494fSHeiko Schocher 		(void) close (ifd);
116*566a494fSHeiko Schocher 	}
117*566a494fSHeiko Schocher 
118*566a494fSHeiko Schocher 	return EXIT_SUCCESS;
119*566a494fSHeiko Schocher }
120