ubsha1.c 3.0 KB

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