sec_firmware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. /*
  92. * SEC Firmware FIT image parser to check if any loadable is
  93. * present. If present, verify integrity of the loadable and
  94. * copy loadable to address provided in (loadable_h, loadable_l).
  95. *
  96. * Returns 0 on success and a negative errno on error task fail.
  97. */
  98. static int sec_firmware_check_copy_loadable(const void *sec_firmware_img,
  99. u32 *loadable_l, u32 *loadable_h)
  100. {
  101. phys_addr_t sec_firmware_loadable_addr = 0;
  102. int conf_node_off, ld_node_off;
  103. char *conf_node_name = NULL;
  104. const void *data;
  105. size_t size;
  106. ulong load;
  107. conf_node_name = SEC_FIRMEWARE_FIT_CNF_NAME;
  108. conf_node_off = fit_conf_get_node(sec_firmware_img, conf_node_name);
  109. if (conf_node_off < 0) {
  110. printf("SEC Firmware: %s: no such config\n", conf_node_name);
  111. return -ENOENT;
  112. }
  113. ld_node_off = fit_conf_get_prop_node(sec_firmware_img, conf_node_off,
  114. FIT_LOADABLE_PROP);
  115. if (ld_node_off >= 0) {
  116. printf("SEC Firmware: '%s' present in config\n",
  117. FIT_LOADABLE_PROP);
  118. /* Verify secure firmware image */
  119. if (!(fit_image_verify(sec_firmware_img, ld_node_off))) {
  120. printf("SEC Loadable: Bad loadable image (bad CRC)\n");
  121. return -EINVAL;
  122. }
  123. if (fit_image_get_data(sec_firmware_img, ld_node_off,
  124. &data, &size)) {
  125. printf("SEC Loadable: Can't get subimage data/size");
  126. return -ENOENT;
  127. }
  128. /* Get load address, treated as load offset to secure memory */
  129. if (fit_image_get_load(sec_firmware_img, ld_node_off, &load)) {
  130. printf("SEC Loadable: Can't get subimage load");
  131. return -ENOENT;
  132. }
  133. /* Compute load address for loadable in secure memory */
  134. sec_firmware_loadable_addr = (sec_firmware_addr -
  135. gd->arch.tlb_size) + load;
  136. /* Copy loadable to secure memory and flush dcache */
  137. debug("%s copied to address 0x%p\n",
  138. FIT_LOADABLE_PROP, (void *)sec_firmware_loadable_addr);
  139. memcpy((void *)sec_firmware_loadable_addr, data, size);
  140. flush_dcache_range(sec_firmware_loadable_addr,
  141. sec_firmware_loadable_addr + size);
  142. }
  143. /* Populate address ptrs for loadable image with loadbale addr */
  144. out_le32(loadable_l, (sec_firmware_loadable_addr & WORD_MASK));
  145. out_le32(loadable_h, (sec_firmware_loadable_addr >> WORD_SHIFT));
  146. return 0;
  147. }
  148. static int sec_firmware_copy_image(const char *title,
  149. u64 image_addr, u32 image_size, u64 sec_firmware)
  150. {
  151. debug("%s copied to address 0x%p\n", title, (void *)sec_firmware);
  152. memcpy((void *)sec_firmware, (void *)image_addr, image_size);
  153. flush_dcache_range(sec_firmware, sec_firmware + image_size);
  154. return 0;
  155. }
  156. /*
  157. * This function will parse the SEC Firmware image, and then load it
  158. * to secure memory. Also load any loadable if present along with SEC
  159. * Firmware image.
  160. */
  161. static int sec_firmware_load_image(const void *sec_firmware_img,
  162. u32 *loadable_l, u32 *loadable_h)
  163. {
  164. const void *raw_image_addr;
  165. size_t raw_image_size = 0;
  166. int ret;
  167. /*
  168. * The Excetpion Level must be EL3 to load and initialize
  169. * the SEC Firmware.
  170. */
  171. if (current_el() != 3) {
  172. ret = -EACCES;
  173. goto out;
  174. }
  175. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  176. /*
  177. * The SEC Firmware must be stored in secure memory.
  178. * Append SEC Firmware to secure mmu table.
  179. */
  180. if (!(gd->arch.secure_ram & MEM_RESERVE_SECURE_MAINTAINED)) {
  181. ret = -ENXIO;
  182. goto out;
  183. }
  184. sec_firmware_addr = (gd->arch.secure_ram & MEM_RESERVE_SECURE_ADDR_MASK) +
  185. gd->arch.tlb_size;
  186. #else
  187. #error "The CONFIG_SYS_MEM_RESERVE_SECURE must be defined when enabled SEC Firmware support"
  188. #endif
  189. /* Align SEC Firmware base address to 4K */
  190. sec_firmware_addr = (sec_firmware_addr + 0xfff) & ~0xfff;
  191. debug("SEC Firmware: Load address: 0x%llx\n",
  192. sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
  193. ret = sec_firmware_parse_image(sec_firmware_img, &raw_image_addr,
  194. &raw_image_size);
  195. if (ret)
  196. goto out;
  197. /* TODO:
  198. * Check if the end addr of SEC Firmware has been extend the secure
  199. * memory.
  200. */
  201. /* Copy the secure firmware to secure memory */
  202. ret = sec_firmware_copy_image("SEC Firmware", (u64)raw_image_addr,
  203. raw_image_size, sec_firmware_addr &
  204. SEC_FIRMWARE_ADDR_MASK);
  205. if (ret)
  206. goto out;
  207. /*
  208. * Check if any loadable are present along with firmware image, if
  209. * present load them.
  210. */
  211. ret = sec_firmware_check_copy_loadable(sec_firmware_img, loadable_l,
  212. loadable_h);
  213. if (ret)
  214. goto out;
  215. sec_firmware_addr |= SEC_FIRMWARE_LOADED;
  216. debug("SEC Firmware: Entry point: 0x%llx\n",
  217. sec_firmware_addr & SEC_FIRMWARE_ADDR_MASK);
  218. return 0;
  219. out:
  220. printf("SEC Firmware: error (%d)\n", ret);
  221. sec_firmware_addr = 0;
  222. return ret;
  223. }
  224. static int sec_firmware_entry(u32 *eret_hold_l, u32 *eret_hold_h)
  225. {
  226. const void *entry = (void *)(sec_firmware_addr &
  227. SEC_FIRMWARE_ADDR_MASK);
  228. return _sec_firmware_entry(entry, eret_hold_l, eret_hold_h);
  229. }
  230. /* Check the secure firmware FIT image */
  231. __weak bool sec_firmware_is_valid(const void *sec_firmware_img)
  232. {
  233. if (fdt_check_header(sec_firmware_img)) {
  234. printf("SEC Firmware: Bad firmware image (not a FIT image)\n");
  235. return false;
  236. }
  237. if (!fit_check_format(sec_firmware_img)) {
  238. printf("SEC Firmware: Bad firmware image (bad FIT header)\n");
  239. return false;
  240. }
  241. return true;
  242. }
  243. #ifdef CONFIG_SEC_FIRMWARE_ARMV8_PSCI
  244. /*
  245. * The PSCI_VERSION function is added from PSCI v0.2. When the PSCI
  246. * v0.1 received this function, the NOT_SUPPORTED (0xffff_ffff) error
  247. * number will be returned according to SMC Calling Conventions. But
  248. * when getting the NOT_SUPPORTED error number, we cannot ensure if
  249. * the PSCI version is v0.1 or other error occurred. So, PSCI v0.1
  250. * won't be supported by this framework.
  251. * And if the secure firmware isn't running, return NOT_SUPPORTED.
  252. *
  253. * The return value on success is PSCI version in format
  254. * major[31:16]:minor[15:0].
  255. */
  256. unsigned int sec_firmware_support_psci_version(void)
  257. {
  258. if (current_el() == SEC_FIRMWARE_TARGET_EL)
  259. return _sec_firmware_support_psci_version();
  260. return PSCI_INVALID_VER;
  261. }
  262. #endif
  263. /*
  264. * Check with sec_firmware if it supports random number generation
  265. * via HW RNG
  266. *
  267. * The return value will be true if it is supported
  268. */
  269. bool sec_firmware_support_hwrng(void)
  270. {
  271. uint8_t rand[8];
  272. if (sec_firmware_addr & SEC_FIRMWARE_RUNNING) {
  273. if (!sec_firmware_get_random(rand, 8))
  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. unsigned long long num;
  287. struct pt_regs regs;
  288. int param1;
  289. if (!bytes || bytes > 8) {
  290. printf("Max Random bytes genration supported is 8\n");
  291. return -1;
  292. }
  293. #define SIP_RNG_64 0xC200FF11
  294. regs.regs[0] = SIP_RNG_64;
  295. if (bytes <= 4)
  296. param1 = 0;
  297. else
  298. param1 = 1;
  299. regs.regs[1] = param1;
  300. smc_call(&regs);
  301. if (regs.regs[0])
  302. return -1;
  303. num = regs.regs[1];
  304. memcpy(rand, &num, bytes);
  305. return 0;
  306. }
  307. /*
  308. * sec_firmware_init - Initialize the SEC Firmware
  309. * @sec_firmware_img: the SEC Firmware image address
  310. * @eret_hold_l: the address to hold exception return address low
  311. * @eret_hold_h: the address to hold exception return address high
  312. * @loadable_l: the address to hold loadable address low
  313. * @loadable_h: the address to hold loadable address high
  314. */
  315. int sec_firmware_init(const void *sec_firmware_img,
  316. u32 *eret_hold_l,
  317. u32 *eret_hold_h,
  318. u32 *loadable_l,
  319. u32 *loadable_h)
  320. {
  321. int ret;
  322. if (!sec_firmware_is_valid(sec_firmware_img))
  323. return -EINVAL;
  324. ret = sec_firmware_load_image(sec_firmware_img, loadable_l,
  325. loadable_h);
  326. if (ret) {
  327. printf("SEC Firmware: Failed to load image\n");
  328. return ret;
  329. } else if (sec_firmware_addr & SEC_FIRMWARE_LOADED) {
  330. ret = sec_firmware_entry(eret_hold_l, eret_hold_h);
  331. if (ret) {
  332. printf("SEC Firmware: Failed to initialize\n");
  333. return ret;
  334. }
  335. }
  336. debug("SEC Firmware: Return from SEC Firmware: current_el = %d\n",
  337. current_el());
  338. /*
  339. * The PE will be turned into target EL when returned from
  340. * SEC Firmware.
  341. */
  342. if (current_el() != SEC_FIRMWARE_TARGET_EL)
  343. return -EACCES;
  344. sec_firmware_addr |= SEC_FIRMWARE_RUNNING;
  345. /* Set exception table and enable caches if it isn't EL3 */
  346. if (current_el() != 3) {
  347. c_runtime_cpu_setup();
  348. enable_caches();
  349. }
  350. return 0;
  351. }
  352. /*
  353. * fdt_fix_kaslr - Add kalsr-seed node in Device tree
  354. * @fdt: Device tree
  355. * @eret: 0 in case of error, 1 for success
  356. */
  357. int fdt_fixup_kaslr(void *fdt)
  358. {
  359. int nodeoffset;
  360. int err, ret = 0;
  361. u8 rand[8];
  362. #if defined(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT)
  363. /* Check if random seed generation is supported */
  364. if (sec_firmware_support_hwrng() == false)
  365. return 0;
  366. ret = sec_firmware_get_random(rand, 8);
  367. if (ret < 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. }