sec_firmware.c 13 KB

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