sec_firmware.c 12 KB

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