default_image.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 image_header_t 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. return EXIT_SUCCESS;
  28. else
  29. return EXIT_FAILURE;
  30. }
  31. static int image_check_params(struct image_tool_params *params)
  32. {
  33. return ((params->dflag && (params->fflag || params->lflag)) ||
  34. (params->fflag && (params->dflag || params->lflag)) ||
  35. (params->lflag && (params->dflag || params->fflag)));
  36. }
  37. static int image_verify_header(unsigned char *ptr, int image_size,
  38. struct image_tool_params *params)
  39. {
  40. uint32_t len;
  41. const unsigned char *data;
  42. uint32_t checksum;
  43. image_header_t header;
  44. image_header_t *hdr = &header;
  45. /*
  46. * create copy of header so that we can blank out the
  47. * checksum field for checking - this can't be done
  48. * on the PROT_READ mapped data.
  49. */
  50. memcpy(hdr, ptr, sizeof(image_header_t));
  51. if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
  52. debug("%s: Bad Magic Number: \"%s\" is no valid image\n",
  53. params->cmdname, params->imagefile);
  54. return -FDT_ERR_BADMAGIC;
  55. }
  56. data = (const unsigned char *)hdr;
  57. len = sizeof(image_header_t);
  58. checksum = be32_to_cpu(hdr->ih_hcrc);
  59. hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
  60. if (crc32(0, data, len) != checksum) {
  61. debug("%s: ERROR: \"%s\" has bad header checksum!\n",
  62. params->cmdname, params->imagefile);
  63. return -FDT_ERR_BADSTATE;
  64. }
  65. data = (const unsigned char *)ptr + sizeof(image_header_t);
  66. len = image_size - sizeof(image_header_t) ;
  67. checksum = be32_to_cpu(hdr->ih_dcrc);
  68. if (crc32(0, data, len) != checksum) {
  69. debug("%s: ERROR: \"%s\" has corrupted data!\n",
  70. params->cmdname, params->imagefile);
  71. return -FDT_ERR_BADSTRUCTURE;
  72. }
  73. return 0;
  74. }
  75. static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
  76. struct image_tool_params *params)
  77. {
  78. uint32_t checksum;
  79. time_t time;
  80. uint32_t imagesize;
  81. uint32_t ep;
  82. uint32_t addr;
  83. image_header_t * hdr = (image_header_t *)ptr;
  84. checksum = crc32(0,
  85. (const unsigned char *)(ptr +
  86. sizeof(image_header_t)),
  87. sbuf->st_size - sizeof(image_header_t));
  88. time = imagetool_get_source_date(params->cmdname, sbuf->st_mtime);
  89. ep = params->ep;
  90. addr = params->addr;
  91. if (params->type == IH_TYPE_FIRMWARE_IVT)
  92. /* Add size of CSF minus IVT */
  93. imagesize = sbuf->st_size - sizeof(image_header_t)
  94. + 0x2060 - sizeof(flash_header_v2_t);
  95. else
  96. imagesize = sbuf->st_size - sizeof(image_header_t);
  97. if (params->os == IH_OS_TEE) {
  98. addr = optee_image_get_load_addr(hdr);
  99. ep = optee_image_get_entry_point(hdr);
  100. }
  101. /* Build new header */
  102. image_set_magic(hdr, IH_MAGIC);
  103. image_set_time(hdr, time);
  104. image_set_size(hdr, imagesize);
  105. image_set_load(hdr, addr);
  106. image_set_ep(hdr, ep);
  107. image_set_dcrc(hdr, checksum);
  108. image_set_os(hdr, params->os);
  109. image_set_arch(hdr, params->arch);
  110. image_set_type(hdr, params->type);
  111. image_set_comp(hdr, params->comp);
  112. image_set_name(hdr, params->imagename);
  113. checksum = crc32(0, (const unsigned char *)hdr,
  114. sizeof(image_header_t));
  115. image_set_hcrc(hdr, checksum);
  116. }
  117. static int image_extract_subimage(void *ptr, struct image_tool_params *params)
  118. {
  119. const image_header_t *hdr = (const image_header_t *)ptr;
  120. ulong file_data;
  121. ulong file_len;
  122. if (image_check_type(hdr, IH_TYPE_MULTI)) {
  123. ulong idx = params->pflag;
  124. ulong count;
  125. /* get the number of data files present in the image */
  126. count = image_multi_count(hdr);
  127. /* retrieve the "data file" at the idx position */
  128. image_multi_getimg(hdr, idx, &file_data, &file_len);
  129. if ((file_len == 0) || (idx >= count)) {
  130. fprintf(stderr, "%s: No such data file %ld in \"%s\"\n",
  131. params->cmdname, idx, params->imagefile);
  132. return -1;
  133. }
  134. } else {
  135. file_data = image_get_data(hdr);
  136. file_len = image_get_size(hdr);
  137. }
  138. /* save the "data file" into the file system */
  139. return imagetool_save_subimage(params->outfile, file_data, file_len);
  140. }
  141. /*
  142. * Default image type parameters definition
  143. */
  144. U_BOOT_IMAGE_TYPE(
  145. defimage,
  146. "Default Image support",
  147. sizeof(image_header_t),
  148. (void *)&header,
  149. image_check_params,
  150. image_verify_header,
  151. image_print_contents,
  152. image_set_header,
  153. image_extract_subimage,
  154. image_check_image_types,
  155. NULL,
  156. NULL
  157. );