sec_firmware.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2016 NXP Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <linux/kernel.h>
  9. #include <asm/io.h>
  10. #include <asm/system.h>
  11. #include <asm/types.h>
  12. #include <asm/macro.h>
  13. #include <asm/armv8/sec_firmware.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. extern void c_runtime_cpu_setup(void);
  16. #define SEC_FIRMWARE_LOADED 0x1
  17. #define SEC_FIRMWARE_RUNNING 0x2
  18. #define SEC_FIRMWARE_ADDR_MASK (~0x3)
  19. /*
  20. * Secure firmware load addr
  21. * Flags used: 0x1 secure firmware has been loaded to secure memory
  22. * 0x2 secure firmware is running
  23. */
  24. phys_addr_t sec_firmware_addr;
  25. #ifndef SEC_FIRMWARE_FIT_IMAGE
  26. #define SEC_FIRMWARE_FIT_IMAGE "firmware"
  27. #endif
  28. #ifndef SEC_FIRMEWARE_FIT_CNF_NAME
  29. #define SEC_FIRMEWARE_FIT_CNF_NAME "config@1"
  30. #endif
  31. #ifndef SEC_FIRMWARE_TARGET_EL
  32. #define SEC_FIRMWARE_TARGET_EL 2
  33. #endif
  34. static int sec_firmware_get_data(const void *sec_firmware_img,
  35. const void **data, size_t *size)
  36. {
  37. int conf_node_off, fw_node_off;
  38. char *conf_node_name = NULL;
  39. char *desc;
  40. int ret;
  41. conf_node_name = SEC_FIRMEWARE_FIT_CNF_NAME;
  42. conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
  43. if (conf_node_off < 0) {
  44. printf("SEC Firmware: %s: no such config\n", conf_node_name);
  45. return -ENOENT;
  46. }
  47. fw_node_off = fit_conf_get_prop_node(sec_firmware_img, conf_node_off,
  48. SEC_FIRMWARE_FIT_IMAGE);
  49. if (fw_node_off < 0) {
  50. printf("SEC Firmware: No '%s' in config\n",
  51. SEC_FIRMWARE_FIT_IMAGE);
  52. return -ENOLINK;
  53. }
  54. /* Verify secure firmware image */
  55. if (!(fit_image_verify(sec_firmware_img, fw_node_off))) {
  56. printf("SEC Firmware: Bad firmware image (bad CRC)\n");
  57. return -EINVAL;
  58. }
  59. if (fit_image_get_data(sec_firmware_img, fw_node_off, data, size)) {
  60. printf("SEC Firmware: Can't get %s subimage data/size",
  61. SEC_FIRMWARE_FIT_IMAGE);
  62. return -ENOENT;
  63. }
  64. ret = fit_get_desc(sec_firmware_img, fw_node_off, &desc);
  65. if (ret)
  66. printf("SEC Firmware: Can't get description\n");
  67. else
  68. printf("%s\n", desc);
  69. return ret;
  70. }
  71. /*
  72. * SEC Firmware FIT image parser checks if the image is in FIT
  73. * format, verifies integrity of the image and calculates raw
  74. * image address and size values.
  75. *
  76. * Returns 0 on success and a negative errno on error task fail.
  77. */
  78. static int sec_firmware_parse_image(const void *sec_firmware_img,
  79. const void **raw_image_addr,
  80. size_t *raw_image_size)
  81. {
  82. int ret;
  83. ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
  84. raw_image_size);
  85. if (ret)
  86. return ret;
  87. debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
  88. *raw_image_addr, *raw_image_size);
  89. return 0;
  90. }
  91. static int sec_firmware_copy_image(const char *title,
  92. u64 image_addr, u32 image_size, u64 sec_firmware)
  93. {
  94. debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
  95. memcpy((void *)sec_firmware, (void *)image_addr, image_size);
  96. flush_dcache_range(sec_firmware, sec_firmware + image_size);
  97. return 0;
  98. }
  99. /*
  100. * This function will parse the SEC Firmware image, and then load it
  101. * to secure memory.
  102. */
  103. static int sec_firmware_load_image(const void *sec_firmware_img)
  104. {
  105. const void *raw_image_addr;
  106. size_t raw_image_size = 0;
  107. int ret;
  108. /*
  109. * The Excetpion Level must be EL3 to load and initialize
  110. * the SEC Firmware.
  111. */
  112. if (current_el() != 3) {
  113. ret = -EACCES;
  114. goto out;
  115. }
  116. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  117. /*
  118. * The SEC Firmware must be stored in secure memory.
  119. * Append SEC Firmware to secure mmu table.
  120. */
  121. if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
  122. ret = -ENXIO;
  123. goto out;
  124. }
  125. sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
  126. gd->arch.tlb_size;
  127. #else
  128. #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
  129. #endif
  130. /* Align SEC Firmware base address to 4K */
  131. sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
  132. debug("SEC Firmware: Load address: 0x%llx\n",
  133. sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
  134. ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
  135. &raw_image_size);
  136. if (ret)
  137. goto out;
  138. /* TODO:
  139. * Check if the end addr of SEC Firmware has been extend the secure
  140. * memory.
  141. */
  142. /* Copy the secure firmware to secure memory */
  143. ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
  144. raw_image_size, sec_firmware_addr &
  145. SEC_FIRMWARE_ADDR_MASK);
  146. if (ret)
  147. goto out;
  148. sec_firmware_addr |= SEC_FIRMWARE_LOADED;
  149. debug("SEC Firmware: Entry point: 0x%llx\n",
  150. sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
  151. return 0;
  152. out:
  153. printf("SEC Firmware: error (%d)\n", ret);
  154. sec_firmware_addr = 0;
  155. return ret;
  156. }
  157. static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
  158. {
  159. const void *entry = (void *)(sec_firmware_addr &
  160. SEC_FIRMWARE_ADDR_MASK);
  161. return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
  162. }
  163. /* Check the secure firmware FIT image */
  164. __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
  165. {
  166. if (fdt_check_header(sec_firmware_img)) {
  167. printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
  168. return false;
  169. }
  170. if (!fit_check_format(sec_firmware_img)) {
  171. printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
  172. return false;
  173. }
  174. return true;
  175. }
  176. #ifdef CONFIG_ARMV8_PSCI
  177. /*
  178. * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
  179. * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
  180. * number will be returned according to SMC Calling Conventions. But
  181. * when getting the NOT_SUPPORTED error number, we cannot ensure if
  182. * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
  183. * won't be supported by this framework.
  184. * And if the secure firmware isn't running, return NOT_SUPPORTED.
  185. *
  186. * The return value on success is PSCI version in format
  187. * major[31:16]:minor[15:0].
  188. */
  189. unsigned int sec_firmware_support_psci_version(void)
  190. {
  191. if (sec_firmware_addr & SEC_FIRMWARE_RUNNING)
  192. return _sec_firmware_support_psci_version();
  193. return 0xffffffff;
  194. }
  195. #endif
  196. /*
  197. * sec_firmware_init - Initialize the SEC Firmware
  198. * @sec_firmware_img: the SEC Firmware image address
  199. * @eret_hold_l: the address to hold exception return address low
  200. * @eret_hold_h: the address to hold exception return address high
  201. */
  202. int sec_firmware_init(const void *sec_firmware_img,
  203. u32 *eret_hold_l,
  204. u32 *eret_hold_h)
  205. {
  206. int ret;
  207. if (!sec_firmware_is_valid(sec_firmware_img))
  208. return -EINVAL;
  209. ret = sec_firmware_load_image(sec_firmware_img);
  210. if (ret) {
  211. printf("SEC Firmware: Failed to load image\n");
  212. return ret;
  213. } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
  214. ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
  215. if (ret) {
  216. printf("SEC Firmware: Failed to initialize\n");
  217. return ret;
  218. }
  219. }
  220. debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
  221. current_el());
  222. /*
  223. * The PE will be turned into target EL when returned from
  224. * SEC Firmware.
  225. */
  226. if (current_el() != SEC_FIRMWARE_TARGET_EL)
  227. return -EACCES;
  228. sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
  229. /* Set exception table and enable caches if it isn't EL3 */
  230. if (current_el() != 3) {
  231. c_runtime_cpu_setup();
  232. enable_caches();
  233. }
  234. return 0;
  235. }