Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2007 |
| 4 | * Heiko Schocher, DENX Software Engineering, <hs@denx.de> |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Peter Tyser | b06976d | 2009-03-13 18:54:51 -0500 | [diff] [blame] | 7 | #include "os_support.h" |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <errno.h> |
| 13 | #include <string.h> |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 14 | #include <sys/stat.h> |
Jeroen Hofstee | bfe88fe | 2014-06-12 22:27:12 +0200 | [diff] [blame] | 15 | #include <u-boot/sha1.h> |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 16 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 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 | } |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 52 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 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 | } |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 61 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 62 | sha1_csum ((unsigned char *) data, len, (unsigned char *)output); |
| 63 | |
| 64 | printf ("U-Boot sum:\n"); |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 65 | for (i = 0; i < 20 ; i++) { |
| 66 | printf ("%02X ", output[i]); |
| 67 | } |
| 68 | printf ("\n"); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 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 | } |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 76 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 77 | free (data); |
| 78 | (void) munmap((void *)ptr, len); |
| 79 | (void) close (ifd); |
| 80 | } |
| 81 | |
| 82 | return EXIT_SUCCESS; |
| 83 | } |