ubsha1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * (C) Copyright 2007
  3. * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include "os_support.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include <string.h>
  30. #include <sys/stat.h>
  31. #include "sha1.h"
  32. int main (int argc, char **argv)
  33. {
  34. unsigned char output[20];
  35. int i, len;
  36. char *imagefile;
  37. char *cmdname = *argv;
  38. unsigned char *ptr;
  39. unsigned char *data;
  40. struct stat sbuf;
  41. unsigned char *ptroff;
  42. int ifd;
  43. int off;
  44. if (argc > 1) {
  45. imagefile = argv[1];
  46. ifd = open (imagefile, O_RDWR|O_BINARY);
  47. if (ifd < 0) {
  48. fprintf (stderr, "%s: Can't open %s: %s\n",
  49. cmdname, imagefile, strerror(errno));
  50. exit (EXIT_FAILURE);
  51. }
  52. if (fstat (ifd, &sbuf) < 0) {
  53. fprintf (stderr, "%s: Can't stat %s: %s\n",
  54. cmdname, imagefile, strerror(errno));
  55. exit (EXIT_FAILURE);
  56. }
  57. len = sbuf.st_size;
  58. ptr = (unsigned char *)mmap(0, len,
  59. PROT_READ, MAP_SHARED, ifd, 0);
  60. if (ptr == (unsigned char *)MAP_FAILED) {
  61. fprintf (stderr, "%s: Can't read %s: %s\n",
  62. cmdname, imagefile, strerror(errno));
  63. exit (EXIT_FAILURE);
  64. }
  65. /* create a copy, so we can blank out the sha1 sum */
  66. data = malloc (len);
  67. memcpy (data, ptr, len);
  68. off = SHA1_SUM_POS;
  69. ptroff = &data[len + off];
  70. for (i = 0; i < SHA1_SUM_LEN; i++) {
  71. ptroff[i] = 0;
  72. }
  73. sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
  74. printf ("U-Boot sum:\n");
  75. for (i = 0; i < 20 ; i++) {
  76. printf ("%02X ", output[i]);
  77. }
  78. printf ("\n");
  79. /* overwrite the sum in the bin file, with the actual */
  80. lseek (ifd, SHA1_SUM_POS, SEEK_END);
  81. if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
  82. fprintf (stderr, "%s: Can't write %s: %s\n",
  83. cmdname, imagefile, strerror(errno));
  84. exit (EXIT_FAILURE);
  85. }
  86. free (data);
  87. (void) munmap((void *)ptr, len);
  88. (void) close (ifd);
  89. }
  90. return EXIT_SUCCESS;
  91. }