1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 2566a494fSHeiko Schocher /* 3566a494fSHeiko Schocher * (C) Copyright 2007 4566a494fSHeiko Schocher * Heiko Schocher, DENX Software Engineering, <hs@denx.de> 5566a494fSHeiko Schocher */ 6566a494fSHeiko Schocher 72f8d396bSPeter Tyser #include "os_support.h" 8566a494fSHeiko Schocher #include <stdio.h> 9566a494fSHeiko Schocher #include <stdlib.h> 10566a494fSHeiko Schocher #include <unistd.h> 11566a494fSHeiko Schocher #include <fcntl.h> 12566a494fSHeiko Schocher #include <errno.h> 13566a494fSHeiko Schocher #include <string.h> 14566a494fSHeiko Schocher #include <sys/stat.h> 152b9912e6SJeroen Hofstee #include <u-boot/sha1.h> 16566a494fSHeiko Schocher 17566a494fSHeiko Schocher int main (int argc, char **argv) 18566a494fSHeiko Schocher { 19566a494fSHeiko Schocher unsigned char output[20]; 20566a494fSHeiko Schocher int i, len; 21566a494fSHeiko Schocher 22566a494fSHeiko Schocher char *imagefile; 23566a494fSHeiko Schocher char *cmdname = *argv; 24566a494fSHeiko Schocher unsigned char *ptr; 25566a494fSHeiko Schocher unsigned char *data; 26566a494fSHeiko Schocher struct stat sbuf; 27566a494fSHeiko Schocher unsigned char *ptroff; 28566a494fSHeiko Schocher int ifd; 29566a494fSHeiko Schocher int off; 30566a494fSHeiko Schocher 31566a494fSHeiko Schocher if (argc > 1) { 32566a494fSHeiko Schocher imagefile = argv[1]; 33566a494fSHeiko Schocher ifd = open (imagefile, O_RDWR|O_BINARY); 34566a494fSHeiko Schocher if (ifd < 0) { 35566a494fSHeiko Schocher fprintf (stderr, "%s: Can't open %s: %s\n", 36566a494fSHeiko Schocher cmdname, imagefile, strerror(errno)); 37566a494fSHeiko Schocher exit (EXIT_FAILURE); 38566a494fSHeiko Schocher } 39566a494fSHeiko Schocher if (fstat (ifd, &sbuf) < 0) { 40566a494fSHeiko Schocher fprintf (stderr, "%s: Can't stat %s: %s\n", 41566a494fSHeiko Schocher cmdname, imagefile, strerror(errno)); 42566a494fSHeiko Schocher exit (EXIT_FAILURE); 43566a494fSHeiko Schocher } 44566a494fSHeiko Schocher len = sbuf.st_size; 45566a494fSHeiko Schocher ptr = (unsigned char *)mmap(0, len, 46566a494fSHeiko Schocher PROT_READ, MAP_SHARED, ifd, 0); 47566a494fSHeiko Schocher if (ptr == (unsigned char *)MAP_FAILED) { 48566a494fSHeiko Schocher fprintf (stderr, "%s: Can't read %s: %s\n", 49566a494fSHeiko Schocher cmdname, imagefile, strerror(errno)); 50566a494fSHeiko Schocher exit (EXIT_FAILURE); 51566a494fSHeiko Schocher } 52566a494fSHeiko Schocher 53566a494fSHeiko Schocher /* create a copy, so we can blank out the sha1 sum */ 54566a494fSHeiko Schocher data = malloc (len); 55566a494fSHeiko Schocher memcpy (data, ptr, len); 56566a494fSHeiko Schocher off = SHA1_SUM_POS; 57566a494fSHeiko Schocher ptroff = &data[len + off]; 58566a494fSHeiko Schocher for (i = 0; i < SHA1_SUM_LEN; i++) { 59566a494fSHeiko Schocher ptroff[i] = 0; 60566a494fSHeiko Schocher } 61566a494fSHeiko Schocher 62566a494fSHeiko Schocher sha1_csum ((unsigned char *) data, len, (unsigned char *)output); 63566a494fSHeiko Schocher 64566a494fSHeiko Schocher printf ("U-Boot sum:\n"); 654ef218f6SWolfgang Denk for (i = 0; i < 20 ; i++) { 66566a494fSHeiko Schocher printf ("%02X ", output[i]); 67566a494fSHeiko Schocher } 68566a494fSHeiko Schocher printf ("\n"); 69566a494fSHeiko Schocher /* overwrite the sum in the bin file, with the actual */ 70566a494fSHeiko Schocher lseek (ifd, SHA1_SUM_POS, SEEK_END); 71566a494fSHeiko Schocher if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) { 72566a494fSHeiko Schocher fprintf (stderr, "%s: Can't write %s: %s\n", 73566a494fSHeiko Schocher cmdname, imagefile, strerror(errno)); 74566a494fSHeiko Schocher exit (EXIT_FAILURE); 75566a494fSHeiko Schocher } 76566a494fSHeiko Schocher 77566a494fSHeiko Schocher free (data); 78566a494fSHeiko Schocher (void) munmap((void *)ptr, len); 79566a494fSHeiko Schocher (void) close (ifd); 80566a494fSHeiko Schocher } 81566a494fSHeiko Schocher 82566a494fSHeiko Schocher return EXIT_SUCCESS; 83566a494fSHeiko Schocher } 84