optee_utils.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2017-2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <lib/optee_utils.h>
  9. #include <platform_def.h>
  10. /*
  11. * load_addr_hi and load_addr_lo: image load address.
  12. * image_id: 0 - pager, 1 - paged
  13. * size: image size in bytes.
  14. */
  15. typedef struct optee_image {
  16. uint32_t load_addr_hi;
  17. uint32_t load_addr_lo;
  18. uint32_t image_id;
  19. uint32_t size;
  20. } optee_image_t;
  21. #define OPTEE_PAGER_IMAGE_ID 0
  22. #define OPTEE_PAGED_IMAGE_ID 1
  23. #define OPTEE_MAX_NUM_IMAGES 2u
  24. #define TEE_MAGIC_NUM_OPTEE 0x4554504f
  25. /*
  26. * magic: header magic number.
  27. * version: OPTEE header version:
  28. * 1 - not supported
  29. * 2 - supported
  30. * arch: OPTEE os architecture type: 0 - AARCH32, 1 - AARCH64.
  31. * flags: unused currently.
  32. * nb_images: number of images.
  33. */
  34. typedef struct optee_header {
  35. uint32_t magic;
  36. uint8_t version;
  37. uint8_t arch;
  38. uint16_t flags;
  39. uint32_t nb_images;
  40. optee_image_t optee_image_list[];
  41. } optee_header_t;
  42. /*******************************************************************************
  43. * Check if it is a valid tee header
  44. * Return true if valid
  45. * Return false if invalid
  46. ******************************************************************************/
  47. static bool tee_validate_header(optee_header_t *header)
  48. {
  49. if ((header->magic == TEE_MAGIC_NUM_OPTEE) &&
  50. (header->version == 2u) &&
  51. (header->nb_images > 0u) &&
  52. (header->nb_images <= OPTEE_MAX_NUM_IMAGES)) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. bool optee_header_is_valid(uintptr_t header_base)
  58. {
  59. return tee_validate_header((optee_header_t *)header_base);
  60. }
  61. /*******************************************************************************
  62. * Parse the OPTEE image
  63. * Return 0 on success or a negative error code otherwise.
  64. ******************************************************************************/
  65. static int parse_optee_image(image_info_t *image_info,
  66. optee_image_t *image)
  67. {
  68. uintptr_t init_load_addr, free_end, requested_end;
  69. size_t init_size;
  70. init_load_addr = ((uint64_t)image->load_addr_hi << 32) |
  71. image->load_addr_lo;
  72. init_size = image->size;
  73. /*
  74. * image->load_addr_hi & image->load_addr_lo set to UINT32_MAX indicate
  75. * loader decided address; take our pre-mapped area for current image
  76. * since arm-tf could not allocate memory dynamically
  77. */
  78. if ((image->load_addr_hi == UINT32_MAX) &&
  79. (image->load_addr_lo == UINT32_MAX)) {
  80. init_load_addr = image_info->image_base;
  81. }
  82. /* Check that the default end address doesn't overflow */
  83. if (check_uptr_overflow(image_info->image_base,
  84. image_info->image_max_size - 1))
  85. return -1;
  86. free_end = image_info->image_base + (image_info->image_max_size - 1);
  87. /* Check that the image end address doesn't overflow */
  88. if (check_uptr_overflow(init_load_addr, init_size - 1))
  89. return -1;
  90. requested_end = init_load_addr + (init_size - 1);
  91. /*
  92. * Check that the requested RAM location is within reserved
  93. * space for OPTEE.
  94. */
  95. if (!((init_load_addr >= image_info->image_base) &&
  96. (requested_end <= free_end))) {
  97. WARN("The load address in optee header %p - %p is not in reserved area: %p - %p.\n",
  98. (void *)init_load_addr,
  99. (void *)(init_load_addr + init_size),
  100. (void *)image_info->image_base,
  101. (void *)(image_info->image_base +
  102. image_info->image_max_size));
  103. return -1;
  104. }
  105. /*
  106. * Remove the skip attr from image_info, the image will be loaded.
  107. * The default attr in image_info is "IMAGE_ATTRIB_SKIP_LOADING", which
  108. * mean the image will not be loaded. Here, we parse the header image to
  109. * know that the extra image need to be loaded, so remove the skip attr.
  110. */
  111. image_info->h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
  112. /* Update image base and size of image_info */
  113. image_info->image_base = init_load_addr;
  114. image_info->image_size = init_size;
  115. return 0;
  116. }
  117. /*******************************************************************************
  118. * Parse the OPTEE header
  119. * Return 0 on success or a negative error code otherwise.
  120. ******************************************************************************/
  121. int parse_optee_header(entry_point_info_t *header_ep,
  122. image_info_t *pager_image_info,
  123. image_info_t *paged_image_info)
  124. {
  125. optee_header_t *header;
  126. uint32_t num;
  127. int ret;
  128. assert(header_ep);
  129. header = (optee_header_t *)header_ep->pc;
  130. assert(header);
  131. /* Print the OPTEE header information */
  132. INFO("OPTEE ep=0x%x\n", (unsigned int)header_ep->pc);
  133. INFO("OPTEE header info:\n");
  134. INFO(" magic=0x%x\n", header->magic);
  135. INFO(" version=0x%x\n", header->version);
  136. INFO(" arch=0x%x\n", header->arch);
  137. INFO(" flags=0x%x\n", header->flags);
  138. INFO(" nb_images=0x%x\n", header->nb_images);
  139. /*
  140. * OPTEE image has 3 types:
  141. *
  142. * 1. Plain OPTEE bin without header.
  143. * Original bin without header, return directly,
  144. * BL32_EXTRA1_IMAGE_ID and BL32_EXTRA2_IMAGE_ID will be skipped.
  145. *
  146. * 2. OPTEE bin with header bin, but no paging.
  147. * Header available and nb_images = 1, remove skip attr for
  148. * BL32_EXTRA1_IMAGE_ID. BL32_EXTRA1_IMAGE_ID will be loaded,
  149. * and BL32_EXTRA2_IMAGE_ID be skipped.
  150. *
  151. * 3. OPTEE image with paging support.
  152. * Header available and nb_images = 2, there are 3 bins: header,
  153. * pager and pageable. Remove skip attr for BL32_EXTRA1_IMAGE_ID
  154. * and BL32_EXTRA2_IMAGE_ID to load pager and paged bin.
  155. */
  156. if (!tee_validate_header(header)) {
  157. INFO("Invalid OPTEE header, set legacy mode.\n");
  158. #ifdef __aarch64__
  159. header_ep->args.arg0 = MODE_RW_64;
  160. #else
  161. header_ep->args.arg0 = MODE_RW_32;
  162. #endif
  163. return 0;
  164. }
  165. /* Parse OPTEE image */
  166. for (num = 0U; num < header->nb_images; num++) {
  167. if (header->optee_image_list[num].image_id ==
  168. OPTEE_PAGER_IMAGE_ID) {
  169. ret = parse_optee_image(pager_image_info,
  170. &header->optee_image_list[num]);
  171. } else if (header->optee_image_list[num].image_id ==
  172. OPTEE_PAGED_IMAGE_ID) {
  173. if (paged_image_info == NULL) {
  174. if (header->optee_image_list[num].size != 0U) {
  175. ERROR("Paged image is not supported\n");
  176. return -1;
  177. }
  178. continue;
  179. } else {
  180. ret = parse_optee_image(paged_image_info,
  181. &header->optee_image_list[num]);
  182. }
  183. } else {
  184. ERROR("Parse optee image failed.\n");
  185. return -1;
  186. }
  187. if (ret != 0)
  188. return -1;
  189. }
  190. /*
  191. * Update "pc" value which should comes from pager image. After the
  192. * header image is parsed, it will be unuseful, and the actual
  193. * execution image after BL31 is pager image.
  194. */
  195. header_ep->pc = pager_image_info->image_base;
  196. /*
  197. * The paged load address and size are populated in
  198. * header image arguments so that can be read by the
  199. * BL32 SPD.
  200. */
  201. if (paged_image_info != NULL) {
  202. header_ep->args.arg1 = paged_image_info->image_base;
  203. header_ep->args.arg2 = paged_image_info->image_size;
  204. }
  205. /* Set OPTEE runtime arch - aarch32/aarch64 */
  206. if (header->arch == 0) {
  207. header_ep->args.arg0 = MODE_RW_32;
  208. } else {
  209. #ifdef __aarch64__
  210. header_ep->args.arg0 = MODE_RW_64;
  211. #else
  212. ERROR("Cannot boot an AArch64 OP-TEE\n");
  213. return -1;
  214. #endif
  215. }
  216. return 0;
  217. }