ubsha1.c 1.9 KB

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