1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
7 #include "os_support.h"
15 #include <u-boot/sha1.h>
17 int main (int argc
, char **argv
)
19 unsigned char output
[20];
23 char *cmdname
= *argv
;
27 unsigned char *ptroff
;
33 ifd
= open (imagefile
, O_RDWR
|O_BINARY
);
35 fprintf (stderr
, "%s: Can't open %s: %s\n",
36 cmdname
, imagefile
, strerror(errno
));
39 if (fstat (ifd
, &sbuf
) < 0) {
40 fprintf (stderr
, "%s: Can't stat %s: %s\n",
41 cmdname
, imagefile
, strerror(errno
));
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
));
53 /* create a copy, so we can blank out the sha1 sum */
55 memcpy (data
, ptr
, len
);
57 ptroff
= &data
[len
+ off
];
58 for (i
= 0; i
< SHA1_SUM_LEN
; i
++) {
62 sha1_csum ((unsigned char *) data
, len
, (unsigned char *)output
);
64 printf ("U-Boot sum:\n");
65 for (i
= 0; i
< 20 ; i
++) {
66 printf ("%02X ", output
[i
]);
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
));
78 (void) munmap((void *)ptr
, len
);