sec_firmware.c 13 KB

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