ubsha1.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef __MINGW32__
  31. #include <sys/mman.h>
  32. #endif
  33. #include <sys/stat.h>
  34. #include "sha1.h"
  35. #ifndef __ASSEMBLY__
  36. #define __ASSEMBLY__ /* Dirty trick to get only #defines */
  37. #endif
  38. #include <config.h>
  39. #undef __ASSEMBLY__
  40. #ifndef O_BINARY /* should be define'd on __WIN32__ */
  41. #define O_BINARY 0
  42. #endif
  43. #ifndef MAP_FAILED
  44. #define MAP_FAILED (-1)
  45. #endif
  46. extern int errno;
  47. extern void sha1_csum (unsigned char *input, int ilen, unsigned char output[20]);
  48. int main (int argc, char **argv)
  49. {
  50. unsigned char output[20];
  51. int i, len;
  52. char *imagefile;
  53. char *cmdname = *argv;
  54. unsigned char *ptr;
  55. unsigned char *data;
  56. struct stat sbuf;
  57. unsigned char *ptroff;
  58. int ifd;
  59. int off;
  60. if (argc > 1) {
  61. imagefile = argv[1];
  62. ifd = open (imagefile, O_RDWR|O_BINARY);
  63. if (ifd < 0) {
  64. fprintf (stderr, "%s: Can't open %s: %s\n",
  65. cmdname, imagefile, strerror(errno));
  66. exit (EXIT_FAILURE);
  67. }
  68. if (fstat (ifd, &sbuf) < 0) {
  69. fprintf (stderr, "%s: Can't stat %s: %s\n",
  70. cmdname, imagefile, strerror(errno));
  71. exit (EXIT_FAILURE);
  72. }
  73. len = sbuf.st_size;
  74. ptr = (unsigned char *)mmap(0, len,
  75. PROT_READ, MAP_SHARED, ifd, 0);
  76. if (ptr == (unsigned char *)MAP_FAILED) {
  77. fprintf (stderr, "%s: Can't read %s: %s\n",
  78. cmdname, imagefile, strerror(errno));
  79. exit (EXIT_FAILURE);
  80. }
  81. /* create a copy, so we can blank out the sha1 sum */
  82. data = malloc (len);
  83. memcpy (data, ptr, len);
  84. off = SHA1_SUM_POS;
  85. ptroff = &data[len + off];
  86. for (i = 0; i < SHA1_SUM_LEN; i++) {
  87. ptroff[i] = 0;
  88. }
  89. sha1_csum ((unsigned char *) data, len, (unsigned char *)output);
  90. printf ("U-Boot sum:\n");
  91. for (i = 0; i < 20 ; i++) {
  92. printf ("%02X ", output[i]);
  93. }
  94. printf ("\n");
  95. /* overwrite the sum in the bin file, with the actual */
  96. lseek (ifd, SHA1_SUM_POS, SEEK_END);
  97. if (write (ifd, output, SHA1_SUM_LEN) != SHA1_SUM_LEN) {
  98. fprintf (stderr, "%s: Can't write %s: %s\n",
  99. cmdname, imagefile, strerror(errno));
  100. exit (EXIT_FAILURE);
  101. }
  102. free (data);
  103. (void) munmap((void *)ptr, len);
  104. (void) close (ifd);
  105. }
  106. return EXIT_SUCCESS;
  107. }