sec_firmware.c 13 KB

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