default_image.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 Semihalf
  4. *
  5. * (C) Copyright 2000-2004
  6. * DENX Software Engineering
  7. * Wolfgang Denk, wd@denx.de
  8. *
  9. * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
  10. * default_image specific code abstracted from mkimage.c
  11. * some functions added to address abstraction
  12. *
  13. * All rights reserved.
  14. */
  15. #include "imagetool.h"
  16. #include "mkimage.h"
  17. #include <u-boot/crc.h>
  18. #include <image.h>
  19. #include <tee/optee.h>
  20. #include <u-boot/crc.h>
  21. #include <imximage.h>
  22. static struct legacy_img_hdr header;
  23. static int image_check_image_types(uint8_t type)
  24. {
  25. if (((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT)) ||
  26. (type == IH_TYPE_KERNEL_NOLOAD) || (type == IH_TYPE_FIRMWARE_IVT) ||
  27. (type == IH_TYPE_FDT_LEGACY))
  28. return EXIT_SUCCESS;
  29. else
  30. return EXIT_FAILURE;
  31. }
  32. static int image_check_params(struct image_tool_params *params)
  33. {
  34. return ((params->dflag && (params->fflag || params->lflag)) ||
  35. (params->fflag && (params->dflag || params->lflag)) ||
  36. (params->lflag && (params->dflag || params->fflag)));
  37. }
  38. static void image_print_header(const void *ptr, struct image_tool_params *params)
  39. {
  40. image_print_contents(ptr);
  41. }
  42. static int image_verify_header(unsigned char *ptr, int image_size,
  43. struct image_tool_params *params)
  44. {
  45. uint32_t len;
  46. const unsigned char *data;
  47. uint32_t checksum;
  48. struct legacy_img_hdr header;
  49. struct legacy_img_hdr *hdr = &header;
  50. if (image_size < sizeof(struct legacy_img_hdr)) {
  51. debug("%s: Bad image size: \"%s\" is no valid image\n",
  52. params->cmdname, params->imagefile);
  53. return -FDT_ERR_BADSTRUCTURE;
  54. }
  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(struct legacy_img_hdr));
  61. if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
  62. debug("%s: Bad Magic Number: \"%s\" is no valid image\n",
  63. params->cmdname, params->imagefile);
  64. return -FDT_ERR_BADMAGIC;
  65. }
  66. data = (const unsigned char *)hdr;
  67. len = sizeof(struct legacy_img_hdr);
  68. checksum = be32_to_cpu(hdr->ih_hcrc);
  69. hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
  70. if (crc32(0, data, len) != checksum) {
  71. debug("%s: ERROR: \"%s\" has bad header checksum!\n",
  72. params->cmdname, params->imagefile);
  73. return -FDT_ERR_BADSTATE;
  74. }
  75. data = (const unsigned char *)ptr + sizeof(struct legacy_img_hdr);
  76. len = image_get_data_size(hdr);
  77. if (image_get_type(hdr) == IH_TYPE_FIRMWARE_IVT)
  78. /* Add size of CSF minus IVT */
  79. len -= 0x2060 - sizeof(flash_header_v2_t);
  80. if (image_size - sizeof(struct legacy_img_hdr) < len) {
  81. debug("%s: Bad image size: \"%s\" is no valid image\n",
  82. params->cmdname, params->imagefile);
  83. return -FDT_ERR_BADSTRUCTURE;
  84. }
  85. checksum = be32_to_cpu(hdr->ih_dcrc);
  86. if (crc32(0, data, len) != checksum) {
  87. debug("%s: ERROR: \"%s\" has corrupted data!\n",
  88. params->cmdname, params->imagefile);
  89. return -FDT_ERR_BADSTRUCTURE;
  90. }
  91. return 0;
  92. }
  93. static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
  94. struct image_tool_params *params)
  95. {
  96. uint32_t checksum;
  97. time_t time;
  98. uint32_t imagesize;
  99. uint32_t ep;
  100. uint32_t addr;
  101. int type;
  102. struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)ptr;
  103. checksum = crc32(0,
  104. (const unsigned char *)(ptr +
  105. sizeof(struct legacy_img_hdr)),
  106. sbuf->st_size - sizeof(struct legacy_img_hdr));
  107. time = imagetool_get_source_date(params->cmdname, sbuf->st_mtime);
  108. ep = params->ep;
  109. addr = params->addr;
  110. if (params->type == IH_TYPE_FIRMWARE_IVT)
  111. /* Add size of CSF minus IVT */
  112. imagesize = sbuf->st_size - sizeof(struct legacy_img_hdr)
  113. + 0x2060 - sizeof(flash_header_v2_t);
  114. else
  115. imagesize = sbuf->st_size - sizeof(struct legacy_img_hdr);
  116. if (params->type == IH_TYPE_FDT_LEGACY)
  117. type = IH_TYPE_FLATDT;
  118. else
  119. type = params->type;
  120. if (params->os == IH_OS_TEE) {
  121. addr = optee_image_get_load_addr(hdr);
  122. ep = optee_image_get_entry_point(hdr);
  123. }
  124. /* Build new header */
  125. image_set_magic(hdr, IH_MAGIC);
  126. image_set_time(hdr, time);
  127. image_set_size(hdr, imagesize);
  128. image_set_load(hdr, addr);
  129. image_set_ep(hdr, ep);
  130. image_set_dcrc(hdr, checksum);
  131. image_set_os(hdr, params->os);
  132. image_set_arch(hdr, params->arch);
  133. image_set_type(hdr, type);
  134. image_set_comp(hdr, params->comp);
  135. image_set_name(hdr, params->imagename);
  136. checksum = crc32(0, (const unsigned char *)hdr,
  137. sizeof(struct legacy_img_hdr));
  138. image_set_hcrc(hdr, checksum);
  139. }
  140. static int image_extract_subimage(void *ptr, struct image_tool_params *params)
  141. {
  142. const struct legacy_img_hdr *hdr = (const struct legacy_img_hdr *)ptr;
  143. ulong file_data;
  144. ulong file_len;
  145. if (image_check_type(hdr, IH_TYPE_MULTI)) {
  146. ulong idx = params->pflag;
  147. ulong count;
  148. /* get the number of data files present in the image */
  149. count = image_multi_count(hdr);
  150. /* retrieve the "data file" at the idx position */
  151. image_multi_getimg(hdr, idx, &file_data, &file_len);
  152. if ((file_len == 0) || (idx >= count)) {
  153. fprintf(stderr, "%s: No such data file %ld in \"%s\"\n",
  154. params->cmdname, idx, params->imagefile);
  155. return -1;
  156. }
  157. } else {
  158. file_data = image_get_data(hdr);
  159. file_len = image_get_size(hdr);
  160. }
  161. /* save the "data file" into the file system */
  162. return imagetool_save_subimage(params->outfile, file_data, file_len);
  163. }
  164. /*
  165. * Default image type parameters definition
  166. */
  167. U_BOOT_IMAGE_TYPE(
  168. defimage,
  169. "Default Image support",
  170. sizeof(struct legacy_img_hdr),
  171. (void *)&header,
  172. image_check_params,
  173. image_verify_header,
  174. image_print_header,
  175. image_set_header,
  176. image_extract_subimage,
  177. image_check_image_types,
  178. NULL,
  179. NULL
  180. );