2018-05-07 05:58:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2007-06-23 01:11:54 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2007
|
|
|
|
* Heiko Schocher, DENX Software Engineering, <hs@denx.de>
|
|
|
|
*/
|
|
|
|
|
2009-03-14 07:54:51 +08:00
|
|
|
#include "os_support.h"
|
2007-06-23 01:11:54 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2014-06-13 04:27:12 +08:00
|
|
|
#include <u-boot/sha1.h>
|
2007-06-23 01:11:54 +08:00
|
|
|
|
|
|
|
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\n",
|
|
|
|
cmdname, imagefile, strerror(errno));
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (fstat (ifd, &sbuf) < 0) {
|
|
|
|
fprintf (stderr, "%s: Can't stat %s: %s\n",
|
|
|
|
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\n",
|
|
|
|
cmdname, imagefile, strerror(errno));
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-07-10 06:01:28 +08:00
|
|
|
|
2007-06-23 01:11:54 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2007-07-10 06:01:28 +08:00
|
|
|
|
2007-06-23 01:11:54 +08:00
|
|
|
sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
|
|
|
|
|
|
|
|
printf ("U-Boot sum:\n");
|
2013-10-04 23:43:24 +08:00
|
|
|
for (i = 0; i < 20 ; i++) {
|
|
|
|
printf ("%02X ", output[i]);
|
|
|
|
}
|
|
|
|
printf ("\n");
|
2007-06-23 01:11:54 +08:00
|
|
|
/* 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\n",
|
|
|
|
cmdname, imagefile, strerror(errno));
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2007-07-10 06:01:28 +08:00
|
|
|
|
2007-06-23 01:11:54 +08:00
|
|
|
free (data);
|
|
|
|
(void) munmap((void *)ptr, len);
|
|
|
|
(void) close (ifd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|