image.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #ifndef USE_HOSTCC
  26. # include <common.h>
  27. # include <watchdog.h>
  28. #else
  29. # include "mkimage.h"
  30. #endif
  31. #include <image.h>
  32. unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
  33. int image_check_hcrc (image_header_t *hdr)
  34. {
  35. ulong hcrc;
  36. ulong len = image_get_header_size ();
  37. image_header_t header;
  38. /* Copy header so we can blank CRC field for re-calculation */
  39. memmove (&header, (char *)hdr, image_get_header_size ());
  40. image_set_hcrc (&header, 0);
  41. hcrc = crc32 (0, (unsigned char *)&header, len);
  42. return (hcrc == image_get_hcrc (hdr));
  43. }
  44. int image_check_dcrc (image_header_t *hdr)
  45. {
  46. ulong data = image_get_data (hdr);
  47. ulong len = image_get_data_size (hdr);
  48. ulong dcrc = crc32 (0, (unsigned char *)data, len);
  49. return (dcrc == image_get_dcrc (hdr));
  50. }
  51. int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
  52. {
  53. ulong dcrc = 0;
  54. ulong len = image_get_data_size (hdr);
  55. ulong data = image_get_data (hdr);
  56. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  57. ulong cdata = data;
  58. ulong edata = cdata + len;
  59. while (cdata < edata) {
  60. ulong chunk = edata - cdata;
  61. if (chunk > chunksz)
  62. chunk = chunksz;
  63. dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
  64. cdata += chunk;
  65. WATCHDOG_RESET ();
  66. }
  67. #else
  68. dcrc = crc32 (0, (unsigned char *)data, len);
  69. #endif
  70. return (dcrc == image_get_dcrc (hdr));
  71. }
  72. int getenv_verify (void)
  73. {
  74. char *s = getenv ("verify");
  75. return (s && (*s == 'n')) ? 0 : 1;
  76. }