ubsha1.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * (C) Copyright 2007
  3. * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  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. int main (int argc, char **argv)
  17. {
  18. unsigned char output[20];
  19. int i, len;
  20. char *imagefile;
  21. char *cmdname = *argv;
  22. unsigned char *ptr;
  23. unsigned char *data;
  24. struct stat sbuf;
  25. unsigned char *ptroff;
  26. int ifd;
  27. int off;
  28. if (argc > 1) {
  29. imagefile = argv[1];
  30. ifd = open (imagefile, O_RDWR|O_BINARY);
  31. if (ifd < 0) {
  32. fprintf (stderr, "%s: Can't open %s: %s\n",
  33. cmdname, imagefile, strerror(errno));
  34. exit (EXIT_FAILURE);
  35. }
  36. if (fstat (ifd, &sbuf) < 0) {
  37. fprintf (stderr, "%s: Can't stat %s: %s\n",
  38. cmdname, imagefile, strerror(errno));
  39. exit (EXIT_FAILURE);
  40. }
  41. len = sbuf.st_size;
  42. ptr = (unsigned char *)mmap(0, len,
  43. PROT_READ, MAP_SHARED, ifd, 0);
  44. if (ptr == (unsigned char *)MAP_FAILED) {
  45. fprintf (stderr, "%s: Can't read %s: %s\n",
  46. cmdname, imagefile, strerror(errno));
  47. exit (EXIT_FAILURE);
  48. }
  49. /* create a copy, so we can blank out the sha1 sum */
  50. data = malloc (len);
  51. memcpy (data, ptr, len);
  52. off = SHA1_SUM_POS;
  53. ptroff = &data[len + off];
  54. for (i = 0; i < SHA1_SUM_LEN; i++) {
  55. ptroff[i] = 0;
  56. }
  57. sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
  58. printf ("U-Boot sum:\n");
  59. for (i = 0; i < 20 ; i++) {
  60. printf ("%02X ", output[i]);
  61. }
  62. printf ("\n");
  63. /* overwrite the sum in the bin file, with the actual */
  64. lseek (ifd, SHA1_SUM_POS, SEEK_END);
  65. if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
  66. fprintf (stderr, "%s: Can't write %s: %s\n",
  67. cmdname, imagefile, strerror(errno));
  68. exit (EXIT_FAILURE);
  69. }
  70. free (data);
  71. (void) munmap((void *)ptr, len);
  72. (void) close (ifd);
  73. }
  74. return EXIT_SUCCESS;
  75. }