sec_firmware.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2016 NXP Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <cpu_func.h>
  7. #include <errno.h>
  8. #include <fdt_support.h>
  9. #include <image.h>
  10. #include <log.h>
  11. #include <asm/cache.h>
  12. #include <asm/global_data.h>
  13. #include <asm/ptrace.h>
  14. #include <linux/kernel.h>
  15. #include <linux/arm-smccc.h>
  16. #include <asm/io.h>
  17. #include <asm/system.h>
  18. #include <asm/types.h>
  19. #include <asm/macro.h>
  20. #include <asm/armv8/sec_firmware.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. extern void c_runtime_cpu_setup(void);
  23. #define SEC_FIRMWARE_LOADED 0x1
  24. #define SEC_FIRMWARE_RUNNING 0x2
  25. #define SEC_FIRMWARE_ADDR_MASK (~0x3)
  26. /*
  27. * Secure firmware load addr
  28. * Flags used: 0x1 secure firmware has been loaded to secure memory
  29. * 0x2 secure firmware is running
  30. */
  31. phys_addr_t sec_firmware_addr;
  32. #ifndef SEC_FIRMWARE_FIT_IMAGE
  33. #define SEC_FIRMWARE_FIT_IMAGE "firmware"
  34. #endif
  35. #ifndef SEC_FIRMWARE_TARGET_EL
  36. #define SEC_FIRMWARE_TARGET_EL 2
  37. #endif
  38. static int sec_firmware_get_data(const void *sec_firmware_img,
  39. const void **data, size_t *size)
  40. {
  41. return fit_get_data_conf_prop(sec_firmware_img, SEC_FIRMWARE_FIT_IMAGE,
  42. data, size);
  43. }
  44. /*
  45. * SEC Firmware FIT image parser checks if the image is in FIT
  46. * format, verifies integrity of the image and calculates raw
  47. * image address and size values.
  48. *
  49. * Returns 0 on success and a negative errno on error task fail.
  50. */
  51. static int sec_firmware_parse_image(const void *sec_firmware_img,
  52. const void **raw_image_addr,
  53. size_t *raw_image_size)
  54. {
  55. int ret;
  56. ret = sec_firmware_get_data(sec_firmware_img, raw_image_addr,
  57. raw_image_size);
  58. if (ret)
  59. return ret;
  60. debug("SEC Firmware: raw_image_addr = 0x%p, raw_image_size = 0x%lx\n",
  61. *raw_image_addr, *raw_image_size);
  62. return 0;
  63. }
  64. /*
  65. * SEC Firmware FIT image parser to check if any loadable is
  66. * present. If present, verify integrity of the loadable and
  67. * copy loadable to address provided in (loadable_h, loadable_l).
  68. *
  69. * Returns 0 on success and a negative errno on error task fail.
  70. */
  71. static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
  72. u32 *loadable_l, u32 *loadable_h)
  73. {
  74. phys_addr_t sec_firmware_loadable_addr = 0;
  75. int conf_node_off, ld_node_off, images;
  76. const void *data;
  77. size_t size;
  78. ulong load;
  79. const char *name, *str, *type;
  80. int len;
  81. conf_node_off = fit_conf_get_node(sec_firmware_img, NULL);
  82. if (conf_node_off < 0) {
  83. puts("SEC Firmware: no config\n");
  84. return -ENOENT;
  85. }
  86. /* find the node holding the images information */
  87. images = fdt_path_offset(sec_firmware_img, FIT_IMAGES_PATH);
  88. if (images < 0) {
  89. printf("%s: Cannot find /images node: %d\n", __func__, images);
  90. return -1;
  91. }
  92. type = FIT_LOADABLE_PROP;
  93. name = fdt_getprop(sec_firmware_img, conf_node_off, type, &len);
  94. if (!name) {
  95. /* Loadables not present */
  96. return 0;
  97. }
  98. printf("SEC Firmware: '%s' present in config\n", type);
  99. for (str = name; str && ((str - name) < len);
  100. str = strchr(str, '\0') + 1) {
  101. printf("%s: '%s'\n", type, str);
  102. ld_node_off = fdt_subnode_offset(sec_firmware_img, images, str);
  103. if (ld_node_off < 0) {
  104. printf("cannot find image node '%s': %d\n", str,
  105. ld_node_off);
  106. return -EINVAL;
  107. }
  108. /* Verify secure firmware image */
  109. if (!(fit_image_verify(sec_firmware_img, ld_node_off))) {
  110. printf("SEC Loadable: Bad loadable image (bad CRC)\n");
  111. return -EINVAL;
  112. }
  113. if (fit_image_get_data(sec_firmware_img, ld_node_off,
  114. &data, &size)) {
  115. printf("SEC Loadable: Can't get subimage data/size");
  116. return -ENOENT;
  117. }
  118. /* Get load address, treated as load offset to secure memory */
  119. if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) {
  120. printf("SEC Loadable: Can't get subimage load");
  121. return -ENOENT;
  122. }
  123. /* Compute load address for loadable in secure memory */
  124. sec_firmware_loadable_addr = (sec_firmware_addr -
  125. gd->arch.tlb_size) + load;
  126. /* Copy loadable to secure memory and flush dcache */
  127. debug("%s copied to address 0x%p\n",
  128. FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr);
  129. memcpy((void *)sec_firmware_loadable_addr, data, size);
  130. flush_dcache_range(sec_firmware_loadable_addr,
  131. sec_firmware_loadable_addr + size);
  132. /* Populate loadable address only for Trusted OS */
  133. if (!strcmp(str, "trustedOS@1")) {
  134. /*
  135. * Populate address ptrs for loadable image with
  136. * loadbale addr
  137. */
  138. out_le32(loadable_l, (sec_firmware_loadable_addr &
  139. WORD_MASK));
  140. out_le32(loadable_h, (sec_firmware_loadable_addr >>
  141. WORD_SHIFT));
  142. }
  143. }
  144. return 0;
  145. }
  146. static int sec_firmware_copy_image(const char *title,
  147. u64 image_addr, u32 image_size, u64 sec_firmware)
  148. {
  149. debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
  150. memcpy((void *)sec_firmware, (void *)image_addr, image_size);
  151. flush_dcache_range(sec_firmware, sec_firmware + image_size);
  152. return 0;
  153. }
  154. /*
  155. * This function will parse the SEC Firmware image, and then load it
  156. * to secure memory. Also load any loadable if present along with SEC
  157. * Firmware image.
  158. */
  159. static int sec_firmware_load_image(const void *sec_firmware_img,
  160. u32 *loadable_l, u32 *loadable_h)
  161. {
  162. const void *raw_image_addr;
  163. size_t raw_image_size = 0;
  164. int ret;
  165. /*
  166. * The Excetpion Level must be EL3 to load and initialize
  167. * the SEC Firmware.
  168. */
  169. if (current_el() != 3) {
  170. ret = -EACCES;
  171. goto out;
  172. }
  173. #ifdef CFG_SYS_MEM_RESERVE_SECURE
  174. /*
  175. * The SEC Firmware must be stored in secure memory.
  176. * Append SEC Firmware to secure mmu table.
  177. */
  178. if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
  179. ret = -ENXIO;
  180. goto out;
  181. }
  182. sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
  183. gd->arch.tlb_size;
  184. #else
  185. #error "The CFG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
  186. #endif
  187. /* Align SEC Firmware base address to 4K */
  188. sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
  189. debug("SEC Firmware: Load address: 0x%llx\n",
  190. sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
  191. ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
  192. &raw_image_size);
  193. if (ret)
  194. goto out;
  195. /* TODO:
  196. * Check if the end addr of SEC Firmware has been extend the secure
  197. * memory.
  198. */
  199. /* Copy the secure firmware to secure memory */
  200. ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
  201. raw_image_size, sec_firmware_addr &
  202. SEC_FIRMWARE_ADDR_MASK);
  203. if (ret)
  204. goto out;
  205. /*
  206. * Check if any loadable are present along with firmware image, if
  207. * present load them.
  208. */
  209. ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
  210. loadable_h);
  211. if (ret)
  212. goto out;
  213. sec_firmware_addr |= SEC_FIRMWARE_LOADED;
  214. debug("SEC Firmware: Entry point: 0x%llx\n",
  215. sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
  216. return 0;
  217. out:
  218. printf("SEC Firmware: error (%d)\n", ret);
  219. sec_firmware_addr = 0;
  220. return ret;
  221. }
  222. static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
  223. {
  224. const void *entry = (void *)(sec_firmware_addr &
  225. SEC_FIRMWARE_ADDR_MASK);
  226. return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
  227. }
  228. /* Check the secure firmware FIT image */
  229. __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
  230. {
  231. if (fdt_check_header(sec_firmware_img)) {
  232. printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
  233. return false;
  234. }
  235. if (fit_check_format(sec_firmware_img, IMAGE_SIZE_INVAL)) {
  236. printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
  237. return false;
  238. }
  239. return true;
  240. }
  241. #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
  242. /*
  243. * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
  244. * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
  245. * number will be returned according to SMC Calling Conventions. But
  246. * when getting the NOT_SUPPORTED error number, we cannot ensure if
  247. * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
  248. * won't be supported by this framework.
  249. * And if the secure firmware isn't running, return NOT_SUPPORTED.
  250. *
  251. * The return value on success is PSCI version in format
  252. * major[31:16]:minor[15:0].
  253. */
  254. unsigned int sec_firmware_support_psci_version(void)
  255. {
  256. if (current_el() == SEC_FIRMWARE_TARGET_EL)
  257. return _sec_firmware_support_psci_version();
  258. return PSCI_INVALID_VER;
  259. }
  260. #endif
  261. /*
  262. * Check with sec_firmware if it supports random number generation
  263. * via HW RNG
  264. *
  265. * The return value will be true if it is supported
  266. */
  267. bool sec_firmware_support_hwrng(void)
  268. {
  269. #ifdef CONFIG_TFABOOT
  270. /* return true as TFA has one job ring reserved */
  271. return true;
  272. #endif
  273. if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
  274. return true;
  275. }
  276. return false;
  277. }
  278. /*
  279. * sec_firmware_get_random - Get a random number from SEC Firmware
  280. * @rand: random number buffer to be filled
  281. * @bytes: Number of bytes of random number to be supported
  282. * @eret: -1 in case of error, 0 for success
  283. */
  284. int sec_firmware_get_random(uint8_t *rand, int bytes)
  285. {
  286. struct arm_smccc_res res;
  287. unsigned long long num;
  288. int param1;
  289. if (!bytes || bytes > 8) {
  290. printf("Max Random bytes genration supported is 8\n");
  291. return -1;
  292. }
  293. if (bytes <= 4)
  294. param1 = 0;
  295. else
  296. param1 = 1;
  297. #define SIP_RNG_64 0xC200FF11
  298. arm_smccc_smc(SIP_RNG_64, param1, 0, 0, 0, 0, 0, 0, &res);
  299. if (res.a0)
  300. return -1;
  301. num = res.a1;
  302. memcpy(rand, &num, bytes);
  303. return 0;
  304. }
  305. /*
  306. * sec_firmware_init - Initialize the SEC Firmware
  307. * @sec_firmware_img: the SEC Firmware image address
  308. * @eret_hold_l: the address to hold exception return address low
  309. * @eret_hold_h: the address to hold exception return address high
  310. * @loadable_l: the address to hold loadable address low
  311. * @loadable_h: the address to hold loadable address high
  312. */
  313. int sec_firmware_init(const void *sec_firmware_img,
  314. u32 *eret_hold_l,
  315. u32 *eret_hold_h,
  316. u32 *loadable_l,
  317. u32 *loadable_h)
  318. {
  319. int ret;
  320. if (!sec_firmware_is_valid(sec_firmware_img))
  321. return -EINVAL;
  322. ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
  323. loadable_h);
  324. if (ret) {
  325. printf("SEC Firmware: Failed to load image\n");
  326. return ret;
  327. } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
  328. ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
  329. if (ret) {
  330. printf("SEC Firmware: Failed to initialize\n");
  331. return ret;
  332. }
  333. }
  334. debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
  335. current_el());
  336. /*
  337. * The PE will be turned into target EL when returned from
  338. * SEC Firmware.
  339. */
  340. if (current_el() != SEC_FIRMWARE_TARGET_EL)
  341. return -EACCES;
  342. sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
  343. /* Set exception table and enable caches if it isn't EL3 */
  344. if (current_el() != 3) {
  345. c_runtime_cpu_setup();
  346. enable_caches();
  347. }
  348. return 0;
  349. }
  350. /*
  351. * fdt_fix_kaslr - Add kalsr-seed node in Device tree
  352. * @fdt: Device tree
  353. * @eret: 0 in case of error, 1 for success
  354. */
  355. int fdt_fixup_kaslr(void *fdt)
  356. {
  357. int nodeoffset;
  358. int err, ret = 0;
  359. u8 rand[8];
  360. #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
  361. /* Check if random seed generation is supported */
  362. if (sec_firmware_support_hwrng() == false) {
  363. printf("WARNING: SEC firmware not running, no kaslr-seed\n");
  364. return 0;
  365. }
  366. err = sec_firmware_get_random(rand, 8);
  367. if (err < 0) {
  368. printf("WARNING: No random number to set kaslr-seed\n");
  369. return 0;
  370. }
  371. err = fdt_check_header(fdt);
  372. if (err < 0) {
  373. printf("fdt_chosen: %s\n", fdt_strerror(err));
  374. return 0;
  375. }
  376. /* find or create "/chosen" node. */
  377. nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
  378. if (nodeoffset < 0)
  379. return 0;
  380. err = fdt_setprop(fdt, nodeoffset, "kaslr-seed", rand,
  381. sizeof(rand));
  382. if (err < 0) {
  383. printf("WARNING: can't set kaslr-seed %s.\n",
  384. fdt_strerror(err));
  385. return 0;
  386. }
  387. ret = 1;
  388. #endif
  389. return ret;
  390. }