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