default_image.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2004
  5. * DENX Software Engineering
  6. * Wolfgang Denk, wd@denx.de
  7. *
  8. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  9. * default_image specific code abstracted from mkimage.c
  10. * some functions added to address abstraction
  11. *
  12. * All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include "mkimage.h"
  30. #include <image.h>
  31. #include <u-boot/crc.h>
  32. static image_header_t header;
  33. static int image_check_image_types(uint8_t type)
  34. {
  35. if (((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT)) ||
  36. (type == IH_TYPE_KERNEL_NOLOAD))
  37. return EXIT_SUCCESS;
  38. else
  39. return EXIT_FAILURE;
  40. }
  41. static int image_check_params(struct mkimage_params *params)
  42. {
  43. return ((params->dflag && (params->fflag || params->lflag)) ||
  44. (params->fflag && (params->dflag || params->lflag)) ||
  45. (params->lflag && (params->dflag || params->fflag)));
  46. }
  47. static int image_verify_header(unsigned char *ptr, int image_size,
  48. struct mkimage_params *params)
  49. {
  50. uint32_t len;
  51. const unsigned char *data;
  52. uint32_t checksum;
  53. image_header_t header;
  54. image_header_t *hdr = &header;
  55. /*
  56. * create copy of header so that we can blank out the
  57. * checksum field for checking - this can't be done
  58. * on the PROT_READ mapped data.
  59. */
  60. memcpy(hdr, ptr, sizeof(image_header_t));
  61. if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
  62. fprintf(stderr,
  63. "%s: Bad Magic Number: \"%s\" is no valid image\n",
  64. params->cmdname, params->imagefile);
  65. return -FDT_ERR_BADMAGIC;
  66. }
  67. data = (const unsigned char *)hdr;
  68. len = sizeof(image_header_t);
  69. checksum = be32_to_cpu(hdr->ih_hcrc);
  70. hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
  71. if (crc32(0, data, len) != checksum) {
  72. fprintf(stderr,
  73. "%s: ERROR: \"%s\" has bad header checksum!\n",
  74. params->cmdname, params->imagefile);
  75. return -FDT_ERR_BADSTATE;
  76. }
  77. data = (const unsigned char *)ptr + sizeof(image_header_t);
  78. len = image_size - sizeof(image_header_t) ;
  79. checksum = be32_to_cpu(hdr->ih_dcrc);
  80. if (crc32(0, data, len) != checksum) {
  81. fprintf(stderr,
  82. "%s: ERROR: \"%s\" has corrupted data!\n",
  83. params->cmdname, params->imagefile);
  84. return -FDT_ERR_BADSTRUCTURE;
  85. }
  86. return 0;
  87. }
  88. static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
  89. struct mkimage_params *params)
  90. {
  91. uint32_t checksum;
  92. image_header_t * hdr = (image_header_t *)ptr;
  93. checksum = crc32(0,
  94. (const unsigned char *)(ptr +
  95. sizeof(image_header_t)),
  96. sbuf->st_size - sizeof(image_header_t));
  97. /* Build new header */
  98. image_set_magic(hdr, IH_MAGIC);
  99. image_set_time(hdr, sbuf->st_mtime);
  100. image_set_size(hdr, sbuf->st_size - sizeof(image_header_t));
  101. image_set_load(hdr, params->addr);
  102. image_set_ep(hdr, params->ep);
  103. image_set_dcrc(hdr, checksum);
  104. image_set_os(hdr, params->os);
  105. image_set_arch(hdr, params->arch);
  106. image_set_type(hdr, params->type);
  107. image_set_comp(hdr, params->comp);
  108. image_set_name(hdr, params->imagename);
  109. checksum = crc32(0, (const unsigned char *)hdr,
  110. sizeof(image_header_t));
  111. image_set_hcrc(hdr, checksum);
  112. }
  113. /*
  114. * Default image type parameters definition
  115. */
  116. static struct image_type_params defimage_params = {
  117. .name = "Default Image support",
  118. .header_size = sizeof(image_header_t),
  119. .hdr = (void*)&header,
  120. .check_image_type = image_check_image_types,
  121. .verify_header = image_verify_header,
  122. .print_header = image_print_contents,
  123. .set_header = image_set_header,
  124. .check_params = image_check_params,
  125. };
  126. void init_default_image_type(void)
  127. {
  128. mkimage_register(&defimage_params);
  129. }