blob: c4203ed99e3da8e55fcb93c4a1edd09d132530e6 [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
Peter Tyser2f8d3962009-03-13 18:54:51 -050024#include "os_support.h"
Heiko Schocher566a4942007-06-22 19:11:54 +020025#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <errno.h>
30#include <string.h>
Peter Tyser2f8d3962009-03-13 18:54:51 -050031#ifndef __MINGW32__
Heiko Schocher566a4942007-06-22 19:11:54 +020032#include <sys/mman.h>
Peter Tyser2f8d3962009-03-13 18:54:51 -050033#endif
Heiko Schocher566a4942007-06-22 19:11:54 +020034#include <sys/stat.h>
35#include "sha1.h"
36
37#ifndef __ASSEMBLY__
38#define __ASSEMBLY__ /* Dirty trick to get only #defines */
39#endif
40#include <config.h>
41#undef __ASSEMBLY__
42
43#ifndef O_BINARY /* should be define'd on __WIN32__ */
44#define O_BINARY 0
45#endif
46
47#ifndef MAP_FAILED
48#define MAP_FAILED (-1)
49#endif
50
51extern int errno;
52
53extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
54
55int main (int argc, char **argv)
56{
57 unsigned char output[20];
58 int i, len;
59
60 char *imagefile;
61 char *cmdname = *argv;
62 unsigned char *ptr;
63 unsigned char *data;
64 struct stat sbuf;
65 unsigned char *ptroff;
66 int ifd;
67 int off;
68
69 if (argc > 1) {
70 imagefile = argv[1];
71 ifd = open (imagefile, O_RDWR|O_BINARY);
72 if (ifd < 0) {
73 fprintf (stderr, "%s: Can't open %s: %s\n",
74 cmdname, imagefile, strerror(errno));
75 exit (EXIT_FAILURE);
76 }
77 if (fstat (ifd, &sbuf) < 0) {
78 fprintf (stderr, "%s: Can't stat %s: %s\n",
79 cmdname, imagefile, strerror(errno));
80 exit (EXIT_FAILURE);
81 }
82 len = sbuf.st_size;
83 ptr = (unsigned char *)mmap(0, len,
84 PROT_READ, MAP_SHARED, ifd, 0);
85 if (ptr == (unsigned char *)MAP_FAILED) {
86 fprintf (stderr, "%s: Can't read %s: %s\n",
87 cmdname, imagefile, strerror(errno));
88 exit (EXIT_FAILURE);
89 }
Wolfgang Denk4ef218f2007-07-10 00:01:28 +020090
Heiko Schocher566a4942007-06-22 19:11:54 +020091 /* create a copy, so we can blank out the sha1 sum */
92 data = malloc (len);
93 memcpy (data, ptr, len);
94 off = SHA1_SUM_POS;
95 ptroff = &data[len + off];
96 for (i = 0; i < SHA1_SUM_LEN; i++) {
97 ptroff[i] = 0;
98 }
Wolfgang Denk4ef218f2007-07-10 00:01:28 +020099
Heiko Schocher566a4942007-06-22 19:11:54 +0200100 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
101
102 printf ("U-Boot sum:\n");
Wolfgang Denk4ef218f2007-07-10 00:01:28 +0200103 for (i = 0; i < 20 ; i++) {
Heiko Schocher566a4942007-06-22 19:11:54 +0200104 printf ("%02X ", output[i]);
105 }
106 printf ("\n");
107 /* overwrite the sum in the bin file, with the actual */
108 lseek (ifd, SHA1_SUM_POS, SEEK_END);
109 if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
110 fprintf (stderr, "%s: Can't write %s: %s\n",
111 cmdname, imagefile, strerror(errno));
112 exit (EXIT_FAILURE);
113 }
Wolfgang Denk4ef218f2007-07-10 00:01:28 +0200114
Heiko Schocher566a4942007-06-22 19:11:54 +0200115 free (data);
116 (void) munmap((void *)ptr, len);
117 (void) close (ifd);
118 }
119
120 return EXIT_SUCCESS;
121}