blob: 90a6f3f59d2da956b6342284ed66bffa9e8671f9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher633e03a2007-06-22 19:11:54 +02002/*
3 * (C) Copyright 2007
4 * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
Heiko Schocher633e03a2007-06-22 19:11:54 +02005 */
6
Peter Tyserb06976d2009-03-13 18:54:51 -05007#include "os_support.h"
Heiko Schocher633e03a2007-06-22 19:11:54 +02008#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <fcntl.h>
12#include <errno.h>
13#include <string.h>
Heiko Schocher633e03a2007-06-22 19:11:54 +020014#include <sys/stat.h>
Jeroen Hofsteebfe88fe2014-06-12 22:27:12 +020015#include <u-boot/sha1.h>
Heiko Schocher633e03a2007-06-22 19:11:54 +020016
Heiko Schocher633e03a2007-06-22 19:11:54 +020017int 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 Denka0453aa2007-07-10 00:01:28 +020052
Heiko Schocher633e03a2007-06-22 19:11:54 +020053 /* 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 Denka0453aa2007-07-10 00:01:28 +020061
Heiko Schocher633e03a2007-06-22 19:11:54 +020062 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
63
64 printf ("U-Boot sum:\n");
Wolfgang Denkec7fbf52013-10-04 17:43:24 +020065 for (i = 0; i < 20 ; i++) {
66 printf ("%02X ", output[i]);
67 }
68 printf ("\n");
Heiko Schocher633e03a2007-06-22 19:11:54 +020069 /* 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 Denka0453aa2007-07-10 00:01:28 +020076
Heiko Schocher633e03a2007-06-22 19:11:54 +020077 free (data);
78 (void) munmap((void *)ptr, len);
79 (void) close (ifd);
80 }
81
82 return EXIT_SUCCESS;
83}