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