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
34int main (int argc, char **argv)
35{
36 unsigned char output[20];
37 int i, len;
38
39 char *imagefile;
40 char *cmdname = *argv;
41 unsigned char *ptr;
42 unsigned char *data;
43 struct stat sbuf;
44 unsigned char *ptroff;
45 int ifd;
46 int off;
47
48 if (argc > 1) {
49 imagefile = argv[1];
50 ifd = open (imagefile, O_RDWR|O_BINARY);
51 if (ifd < 0) {
52 fprintf (stderr, "%s: Can't open %s: %s\n",
53 cmdname, imagefile, strerror(errno));
54 exit (EXIT_FAILURE);
55 }
56 if (fstat (ifd, &sbuf) < 0) {
57 fprintf (stderr, "%s: Can't stat %s: %s\n",
58 cmdname, imagefile, strerror(errno));
59 exit (EXIT_FAILURE);
60 }
61 len = sbuf.st_size;
62 ptr = (unsigned char *)mmap(0, len,
63 PROT_READ, MAP_SHARED, ifd, 0);
64 if (ptr == (unsigned char *)MAP_FAILED) {
65 fprintf (stderr, "%s: Can't read %s: %s\n",
66 cmdname, imagefile, strerror(errno));
67 exit (EXIT_FAILURE);
68 }
69
70
71 data = malloc (len);
72 memcpy (data, ptr, len);
73 off = SHA1_SUM_POS;
74 ptroff = &data[len + off];
75 for (i = 0; i < SHA1_SUM_LEN; i++) {
76 ptroff[i] = 0;
77 }
78
79 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
80
81 printf ("U-Boot sum:\n");
82 for (i = 0; i < 20 ; i++) {
83 printf ("%02X ", output[i]);
84 }
85 printf ("\n");
86
87 lseek (ifd, SHA1_SUM_POS, SEEK_END);
88 if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
89 fprintf (stderr, "%s: Can't write %s: %s\n",
90 cmdname, imagefile, strerror(errno));
91 exit (EXIT_FAILURE);
92 }
93
94 free (data);
95 (void) munmap((void *)ptr, len);
96 (void) close (ifd);
97 }
98
99 return EXIT_SUCCESS;
100}
101