]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
566a494f HS |
2 | /* |
3 | * (C) Copyright 2007 | |
4 | * Heiko Schocher, DENX Software Engineering, <[email protected]> | |
566a494f HS |
5 | */ |
6 | ||
2f8d396b | 7 | #include "os_support.h" |
566a494f HS |
8 | #include <stdio.h> |
9 | #include <stdlib.h> | |
10 | #include <unistd.h> | |
11 | #include <fcntl.h> | |
12 | #include <errno.h> | |
13 | #include <string.h> | |
566a494f | 14 | #include <sys/stat.h> |
2b9912e6 | 15 | #include <u-boot/sha1.h> |
566a494f | 16 | |
566a494f HS |
17 | int main (int argc, char **argv) |
18 | { | |
19 | unsigned char output[20]; | |
20 | int i, len; | |
21 | ||
22 | char *imagefile; | |
23 | char *cmdname = *argv; | |
24 | unsigned char *ptr; | |
25 | unsigned char *data; | |
26 | struct stat sbuf; | |
27 | unsigned char *ptroff; | |
28 | int ifd; | |
29 | int off; | |
30 | ||
31 | if (argc > 1) { | |
32 | imagefile = argv[1]; | |
33 | ifd = open (imagefile, O_RDWR|O_BINARY); | |
34 | if (ifd < 0) { | |
35 | fprintf (stderr, "%s: Can't open %s: %s\n", | |
36 | cmdname, imagefile, strerror(errno)); | |
37 | exit (EXIT_FAILURE); | |
38 | } | |
39 | if (fstat (ifd, &sbuf) < 0) { | |
40 | fprintf (stderr, "%s: Can't stat %s: %s\n", | |
41 | cmdname, imagefile, strerror(errno)); | |
42 | exit (EXIT_FAILURE); | |
43 | } | |
44 | len = sbuf.st_size; | |
45 | ptr = (unsigned char *)mmap(0, len, | |
46 | PROT_READ, MAP_SHARED, ifd, 0); | |
47 | if (ptr == (unsigned char *)MAP_FAILED) { | |
48 | fprintf (stderr, "%s: Can't read %s: %s\n", | |
49 | cmdname, imagefile, strerror(errno)); | |
50 | exit (EXIT_FAILURE); | |
51 | } | |
4ef218f6 | 52 | |
566a494f HS |
53 | /* create a copy, so we can blank out the sha1 sum */ |
54 | data = malloc (len); | |
55 | memcpy (data, ptr, len); | |
56 | off = SHA1_SUM_POS; | |
57 | ptroff = &data[len + off]; | |
58 | for (i = 0; i < SHA1_SUM_LEN; i++) { | |
59 | ptroff[i] = 0; | |
60 | } | |
4ef218f6 | 61 | |
566a494f HS |
62 | sha1_csum ((unsigned char *) data, len, (unsigned char *)output); |
63 | ||
64 | printf ("U-Boot sum:\n"); | |
93e14596 WD |
65 | for (i = 0; i < 20 ; i++) { |
66 | printf ("%02X ", output[i]); | |
67 | } | |
68 | printf ("\n"); | |
566a494f HS |
69 | /* overwrite the sum in the bin file, with the actual */ |
70 | lseek (ifd, SHA1_SUM_POS, SEEK_END); | |
71 | if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) { | |
72 | fprintf (stderr, "%s: Can't write %s: %s\n", | |
73 | cmdname, imagefile, strerror(errno)); | |
74 | exit (EXIT_FAILURE); | |
75 | } | |
4ef218f6 | 76 | |
566a494f HS |
77 | free (data); |
78 | (void) munmap((void *)ptr, len); | |
79 | (void) close (ifd); | |
80 | } | |
81 | ||
82 | return EXIT_SUCCESS; | |
83 | } |