sec_firmware.c 12 KB

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