blob: 1041588d0b037aa7bdd5909644d352c5b20bab25 [file] [log] [blame]
Heiko Schocher633e03a2007-06-22 19:11:54 +02001/*
2 * (C) Copyright 2007
3 * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Heiko Schocher633e03a2007-06-22 19:11:54 +02006 */
7
Peter Tyserb06976d2009-03-13 18:54:51 -05008#include "os_support.h"
Heiko Schocher633e03a2007-06-22 19:11:54 +02009#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <fcntl.h>
13#include <errno.h>
14#include <string.h>
Heiko Schocher633e03a2007-06-22 19:11:54 +020015#include <sys/stat.h>
16#include "sha1.h"
17
Heiko Schocher633e03a2007-06-22 19:11:54 +020018int 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 Denka0453aa2007-07-10 00:01:28 +020053
Heiko Schocher633e03a2007-06-22 19:11:54 +020054 /* 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 Denka0453aa2007-07-10 00:01:28 +020062
Heiko Schocher633e03a2007-06-22 19:11:54 +020063 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
64
65 printf ("U-Boot sum:\n");
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020066 for (i = 0; i < 20 ; i++) {
67 printf ("%02X ", output[i]);
68 }
69 printf ("\n");
Heiko Schocher633e03a2007-06-22 19:11:54 +020070 /* 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 Denka0453aa2007-07-10 00:01:28 +020077
Heiko Schocher633e03a2007-06-22 19:11:54 +020078 free (data);
79 (void) munmap((void *)ptr, len);
80 (void) close (ifd);
81 }
82
83 return EXIT_SUCCESS;
84}