sec_firmware.c 12 KB

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