1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "os_support.h"
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <errno.h>
30#include <string.h>
31#include <sys/stat.h>
32#include "sha1.h"
33
34#ifndef __ASSEMBLY__
35#define __ASSEMBLY__
36#endif
37#include <config.h>
38#undef __ASSEMBLY__
39
40extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
41
42int main (int argc, char **argv)
43{
44 unsigned char output[20];
45 int i, len;
46
47 char *imagefile;
48 char *cmdname = *argv;
49 unsigned char *ptr;
50 unsigned char *data;
51 struct stat sbuf;
52 unsigned char *ptroff;
53 int ifd;
54 int off;
55
56 if (argc > 1) {
57 imagefile = argv[1];
58 ifd = open (imagefile, O_RDWR|O_BINARY);
59 if (ifd < 0) {
60 fprintf (stderr, "%s: Can't open %s: %s\n",
61 cmdname, imagefile, strerror(errno));
62 exit (EXIT_FAILURE);
63 }
64 if (fstat (ifd, &sbuf) < 0) {
65 fprintf (stderr, "%s: Can't stat %s: %s\n",
66 cmdname, imagefile, strerror(errno));
67 exit (EXIT_FAILURE);
68 }
69 len = sbuf.st_size;
70 ptr = (unsigned char *)mmap(0, len,
71 PROT_READ, MAP_SHARED, ifd, 0);
72 if (ptr == (unsigned char *)MAP_FAILED) {
73 fprintf (stderr, "%s: Can't read %s: %s\n",
74 cmdname, imagefile, strerror(errno));
75 exit (EXIT_FAILURE);
76 }
77
78
79 data = malloc (len);
80 memcpy (data, ptr, len);
81 off = SHA1_SUM_POS;
82 ptroff = &data[len + off];
83 for (i = 0; i < SHA1_SUM_LEN; i++) {
84 ptroff[i] = 0;
85 }
86
87 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
88
89 printf ("U-Boot sum:\n");
90 for (i = 0; i < 20 ; i++) {
91 printf ("%02X ", output[i]);
92 }
93 printf ("\n");
94
95 lseek (ifd, SHA1_SUM_POS, SEEK_END);
96 if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
97 fprintf (stderr, "%s: Can't write %s: %s\n",
98 cmdname, imagefile, strerror(errno));
99 exit (EXIT_FAILURE);
100 }
101
102 free (data);
103 (void) munmap((void *)ptr, len);
104 (void) close (ifd);
105 }
106
107 return EXIT_SUCCESS;
108}
109