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