binman: Add support for PowerPC mpc85xx 'bootpg + resetvec' entry
[platform/kernel/u-boot.git] / tools / ubsha1.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007
4  * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
5  */
6
7 #include "os_support.h"
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <u-boot/sha1.h>
16
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                 }
52
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                 }
61
62                 sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
63
64                 printf ("U-Boot sum:\n");
65                 for (i = 0; i < 20 ; i++) {
66                     printf ("%02X ", output[i]);
67                 }
68                 printf ("\n");
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                 }
76
77                 free (data);
78                 (void) munmap((void *)ptr, len);
79                 (void) close (ifd);
80         }
81
82         return EXIT_SUCCESS;
83 }