optee_utils.c 6.1 KB

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