Blame view

tools/ubsha1.c 1.9 KB
566a494f5   Heiko Schocher   [PCS440EP] u...
1
2
3
4
  /*
   * (C) Copyright 2007
   * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
566a494f5   Heiko Schocher   [PCS440EP] u...
6
   */
2f8d396b9   Peter Tyser   Add support for b...
7
  #include "os_support.h"
566a494f5   Heiko Schocher   [PCS440EP] u...
8
9
10
11
12
13
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <errno.h>
  #include <string.h>
566a494f5   Heiko Schocher   [PCS440EP] u...
14
  #include <sys/stat.h>
2b9912e6a   Jeroen Hofstee   includes: move op...
15
  #include <u-boot/sha1.h>
566a494f5   Heiko Schocher   [PCS440EP] u...
16

566a494f5   Heiko Schocher   [PCS440EP] u...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  int main (int argc, char **argv)
  {
  	unsigned char output[20];
  	int i, len;
  
  	char	*imagefile;
  	char	*cmdname = *argv;
  	unsigned char	*ptr;
  	unsigned char	*data;
  	struct stat sbuf;
  	unsigned char	*ptroff;
  	int	ifd;
  	int	off;
  
  	if (argc > 1) {
  		imagefile = argv[1];
  		ifd = open (imagefile, O_RDWR|O_BINARY);
  		if (ifd < 0) {
  			fprintf (stderr, "%s: Can't open %s: %s
  ",
  				cmdname, imagefile, strerror(errno));
  			exit (EXIT_FAILURE);
  		}
  		if (fstat (ifd, &sbuf) < 0) {
  			fprintf (stderr, "%s: Can't stat %s: %s
  ",
  				cmdname, imagefile, strerror(errno));
  			exit (EXIT_FAILURE);
  		}
  		len = sbuf.st_size;
  		ptr = (unsigned char *)mmap(0, len,
  				    PROT_READ, MAP_SHARED, ifd, 0);
  		if (ptr == (unsigned char *)MAP_FAILED) {
  			fprintf (stderr, "%s: Can't read %s: %s
  ",
  				cmdname, imagefile, strerror(errno));
  			exit (EXIT_FAILURE);
  		}
4ef218f6f   Wolfgang Denk   Coding style clea...
55

566a494f5   Heiko Schocher   [PCS440EP] u...
56
57
58
59
60
61
62
63
  		/* create a copy, so we can blank out the sha1 sum */
  		data = malloc (len);
  		memcpy (data, ptr, len);
  		off = SHA1_SUM_POS;
  		ptroff = &data[len +  off];
  		for (i = 0; i < SHA1_SUM_LEN; i++) {
  			ptroff[i] = 0;
  		}
4ef218f6f   Wolfgang Denk   Coding style clea...
64

566a494f5   Heiko Schocher   [PCS440EP] u...
65
66
67
68
  		sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
  
  		printf ("U-Boot sum:
  ");
93e145964   Wolfgang Denk   Coding Style clea...
69
70
71
72
73
  		for (i = 0; i < 20 ; i++) {
  		    printf ("%02X ", output[i]);
  		}
  		printf ("
  ");
566a494f5   Heiko Schocher   [PCS440EP] u...
74
75
76
77
78
79
80
81
  		/* overwrite the sum in the bin file, with the actual */
  		lseek (ifd, SHA1_SUM_POS, SEEK_END);
  		if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
  			fprintf (stderr, "%s: Can't write %s: %s
  ",
  				cmdname, imagefile, strerror(errno));
  			exit (EXIT_FAILURE);
  		}
4ef218f6f   Wolfgang Denk   Coding style clea...
82

566a494f5   Heiko Schocher   [PCS440EP] u...
83
84
85
86
87
88
89
  		free (data);
  		(void) munmap((void *)ptr, len);
  		(void) close (ifd);
  	}
  
  	return EXIT_SUCCESS;
  }