fdt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "dragonstub/elfloader.h"
  2. #include "dragonstub/printk.h"
  3. #include "efidef.h"
  4. #include <dragonstub/dragonstub.h>
  5. #include <libfdt.h>
  6. #include <libfdt_internal.h>
  7. struct exit_boot_struct {
  8. struct efi_boot_memmap *boot_memmap;
  9. efi_memory_desc_t *runtime_map;
  10. int runtime_entry_count;
  11. void *new_fdt_addr;
  12. };
  13. #define EFI_DT_ADDR_CELLS_DEFAULT 2
  14. #define EFI_DT_SIZE_CELLS_DEFAULT 2
  15. static void fdt_update_cell_size(void *fdt)
  16. {
  17. int offset;
  18. offset = fdt_path_offset(fdt, "/");
  19. /* Set the #address-cells and #size-cells values for an empty tree */
  20. fdt_setprop_u32(fdt, offset, "#address-cells",
  21. EFI_DT_ADDR_CELLS_DEFAULT);
  22. fdt_setprop_u32(fdt, offset, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT);
  23. }
  24. static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
  25. {
  26. int node = fdt_path_offset(fdt, "/chosen");
  27. u64 fdt_val64;
  28. u32 fdt_val32;
  29. int err;
  30. if (node < 0)
  31. return EFI_LOAD_ERROR;
  32. fdt_val64 = cpu_to_fdt64((unsigned long)map->map);
  33. err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-start",
  34. fdt_val64);
  35. if (err)
  36. return EFI_LOAD_ERROR;
  37. fdt_val32 = cpu_to_fdt32(map->map_size);
  38. err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-size",
  39. fdt_val32);
  40. if (err)
  41. return EFI_LOAD_ERROR;
  42. fdt_val32 = cpu_to_fdt32(map->desc_size);
  43. err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-size",
  44. fdt_val32);
  45. if (err)
  46. return EFI_LOAD_ERROR;
  47. fdt_val32 = cpu_to_fdt32(map->desc_ver);
  48. err = fdt_setprop_inplace_var(fdt, node, "linux,uefi-mmap-desc-ver",
  49. fdt_val32);
  50. if (err)
  51. return EFI_LOAD_ERROR;
  52. return EFI_SUCCESS;
  53. }
  54. static efi_status_t update_fdt(void *orig_fdt, unsigned long orig_fdt_size,
  55. void *fdt, int new_fdt_size, char *cmdline_ptr)
  56. {
  57. int node, num_rsv;
  58. int status;
  59. u32 fdt_val32;
  60. u64 fdt_val64;
  61. /* Do some checks on provided FDT, if it exists: */
  62. if (orig_fdt) {
  63. if (fdt_check_header(orig_fdt)) {
  64. efi_err("Device Tree header not valid!\n");
  65. return EFI_LOAD_ERROR;
  66. }
  67. /*
  68. * We don't get the size of the FDT if we get if from a
  69. * configuration table:
  70. */
  71. if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
  72. efi_err("Truncated device tree! foo!\n");
  73. return EFI_LOAD_ERROR;
  74. }
  75. }
  76. if (orig_fdt) {
  77. status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
  78. } else {
  79. status = fdt_create_empty_tree(fdt, new_fdt_size);
  80. if (status == 0) {
  81. /*
  82. * Any failure from the following function is
  83. * non-critical:
  84. */
  85. fdt_update_cell_size(fdt);
  86. }
  87. }
  88. if (status != 0)
  89. goto fdt_set_fail;
  90. /*
  91. * Delete all memory reserve map entries. When booting via UEFI,
  92. * kernel will use the UEFI memory map to find reserved regions.
  93. */
  94. num_rsv = fdt_num_mem_rsv(fdt);
  95. while (num_rsv-- > 0)
  96. fdt_del_mem_rsv(fdt, num_rsv);
  97. node = fdt_subnode_offset(fdt, 0, "chosen");
  98. if (node < 0) {
  99. node = fdt_add_subnode(fdt, 0, "chosen");
  100. if (node < 0) {
  101. /* 'node' is an error code when negative: */
  102. status = node;
  103. goto fdt_set_fail;
  104. }
  105. }
  106. if (cmdline_ptr != NULL && strlen(cmdline_ptr) > 0) {
  107. status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
  108. strlen(cmdline_ptr) + 1);
  109. if (status)
  110. goto fdt_set_fail;
  111. }
  112. /* Add FDT entries for EFI runtime services in chosen node. */
  113. node = fdt_subnode_offset(fdt, 0, "chosen");
  114. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)ST);
  115. status = fdt_setprop_var(fdt, node, "linux,uefi-system-table",
  116. fdt_val64);
  117. if (status)
  118. goto fdt_set_fail;
  119. fdt_val64 = UINT64_MAX; /* placeholder */
  120. status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-start", fdt_val64);
  121. if (status)
  122. goto fdt_set_fail;
  123. fdt_val32 = UINT32_MAX; /* placeholder */
  124. status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-size", fdt_val32);
  125. if (status)
  126. goto fdt_set_fail;
  127. status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-size",
  128. fdt_val32);
  129. if (status)
  130. goto fdt_set_fail;
  131. status = fdt_setprop_var(fdt, node, "linux,uefi-mmap-desc-ver",
  132. fdt_val32);
  133. if (status)
  134. goto fdt_set_fail;
  135. bool enalbed_ramdomize_base = false;
  136. #ifdef CONFIG_RANDOMIZE_BASE
  137. enalbed_ramdomize_base = true;
  138. #endif
  139. if (enalbed_ramdomize_base && !efi_nokaslr) {
  140. efi_status_t efi_status;
  141. efi_status = efi_get_random_bytes(sizeof(fdt_val64),
  142. (u8 *)&fdt_val64);
  143. if (efi_status == EFI_SUCCESS) {
  144. status = fdt_setprop_var(fdt, node, "kaslr-seed",
  145. fdt_val64);
  146. if (status)
  147. goto fdt_set_fail;
  148. }
  149. }
  150. /* Shrink the FDT back to its minimum size: */
  151. fdt_pack(fdt);
  152. return EFI_SUCCESS;
  153. fdt_set_fail:
  154. if (status == -FDT_ERR_NOSPACE)
  155. return EFI_BUFFER_TOO_SMALL;
  156. return EFI_LOAD_ERROR;
  157. }
  158. static efi_status_t exit_boot_func(struct efi_boot_memmap *map, void *priv)
  159. {
  160. struct exit_boot_struct *p = priv;
  161. p->boot_memmap = map;
  162. /*
  163. * Update the memory map with virtual addresses. The function will also
  164. * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
  165. * entries so that we can pass it straight to SetVirtualAddressMap()
  166. */
  167. efi_get_virtmap(map->map, map->map_size, map->desc_size, p->runtime_map,
  168. &p->runtime_entry_count);
  169. return update_fdt_memmap(p->new_fdt_addr, map);
  170. }
  171. /*
  172. * Allocate memory for a new FDT, then add EFI and commandline related fields
  173. * to the FDT. This routine increases the FDT allocation size until the
  174. * allocated memory is large enough. EFI allocations are in EFI_PAGE_SIZE
  175. * granules, which are fixed at 4K bytes, so in most cases the first allocation
  176. * should succeed. EFI boot services are exited at the end of this function.
  177. * There must be no allocations between the get_memory_map() call and the
  178. * exit_boot_services() call, so the exiting of boot services is very tightly
  179. * tied to the creation of the FDT with the final memory map in it.
  180. */
  181. static efi_status_t allocate_new_fdt_and_exit_boot(void *handle,
  182. efi_loaded_image_t *image,
  183. unsigned long *new_fdt_addr,
  184. char *cmdline_ptr)
  185. {
  186. unsigned long desc_size;
  187. u32 desc_ver;
  188. efi_status_t status;
  189. struct exit_boot_struct priv = { 0 };
  190. unsigned long fdt_addr = 0;
  191. unsigned long fdt_size = 0;
  192. if (!efi_novamap) {
  193. status = efi_alloc_virtmap(&priv.runtime_map, &desc_size,
  194. &desc_ver);
  195. if (status != EFI_SUCCESS) {
  196. efi_err("Unable to retrieve UEFI memory map.\n");
  197. return status;
  198. }
  199. }
  200. /*
  201. * Unauthenticated device tree data is a security hazard, so ignore
  202. * 'dtb=' unless UEFI Secure Boot is disabled. We assume that secure
  203. * boot is enabled if we can't determine its state.
  204. */
  205. bool config_efi_armstub_dtb_loader = false;
  206. #ifdef CONFIG_EFI_ARMSTUB_DTB_LOADER
  207. config_efi_armstub_dtb_loader = true;
  208. #endif
  209. print_efi_secureboot_mode(efi_get_secureboot());
  210. if (!config_efi_armstub_dtb_loader ||
  211. efi_get_secureboot() != efi_secureboot_mode_disabled) {
  212. if (strstr(cmdline_ptr, "dtb="))
  213. efi_err("Ignoring DTB from command line.\n");
  214. } else {
  215. efi_todo("Load DTB from command line\n");
  216. // status = efi_load_dtb(image, &fdt_addr, &fdt_size);
  217. // if (status != EFI_SUCCESS && status != EFI_NOT_READY) {
  218. // efi_err("Failed to load device tree!\n");
  219. // goto fail;
  220. // }
  221. }
  222. if (fdt_addr) {
  223. efi_info("Using DTB from command line\n");
  224. } else {
  225. /* Look for a device tree configuration table entry. */
  226. fdt_addr = (uintptr_t)get_fdt(&fdt_size);
  227. if (fdt_addr)
  228. efi_info("Using DTB from configuration table\n");
  229. }
  230. if (!fdt_addr)
  231. efi_info("Generating empty DTB\n");
  232. status = efi_allocate_pages(MAX_FDT_SIZE, new_fdt_addr, ULONG_MAX);
  233. if (status != EFI_SUCCESS) {
  234. efi_err("Unable to allocate memory for new device tree.\n");
  235. goto fail;
  236. }
  237. efi_debug("New FDT address: 0x%lx\n", *new_fdt_addr);
  238. efi_info("Generating new FDT...\n");
  239. status = update_fdt((void *)fdt_addr, fdt_size, (void *)*new_fdt_addr,
  240. MAX_FDT_SIZE, cmdline_ptr);
  241. if (status != EFI_SUCCESS) {
  242. efi_err("Unable to construct new device tree.\n");
  243. goto fail_free_new_fdt;
  244. }
  245. priv.new_fdt_addr = (void *)*new_fdt_addr;
  246. efi_info("Exiting boot services...\n");
  247. status = efi_exit_boot_services(handle, &priv, exit_boot_func);
  248. if (status == EFI_SUCCESS) {
  249. efi_set_virtual_address_map_t *svam;
  250. if (efi_novamap)
  251. return EFI_SUCCESS;
  252. /* Install the new virtual address map */
  253. svam = ST->RuntimeServices->SetVirtualAddressMap;
  254. status = svam(priv.runtime_entry_count * desc_size, desc_size,
  255. desc_ver, priv.runtime_map);
  256. /*
  257. * We are beyond the point of no return here, so if the call to
  258. * SetVirtualAddressMap() failed, we need to signal that to the
  259. * incoming kernel but proceed normally otherwise.
  260. */
  261. if (status != EFI_SUCCESS) {
  262. efi_memory_desc_t *p;
  263. int l;
  264. /*
  265. * Set the virtual address field of all
  266. * EFI_MEMORY_RUNTIME entries to U64_MAX. This will
  267. * signal the incoming kernel that no virtual
  268. * translation has been installed.
  269. */
  270. for (l = 0; l < priv.boot_memmap->map_size;
  271. l += priv.boot_memmap->desc_size) {
  272. p = (void *)priv.boot_memmap->map + l;
  273. if (p->Attribute & EFI_MEMORY_RUNTIME)
  274. p->VirtualStart = UINT64_MAX;
  275. }
  276. }
  277. return EFI_SUCCESS;
  278. }
  279. efi_err("Exit boot services failed.\n");
  280. fail_free_new_fdt:
  281. efi_free(MAX_FDT_SIZE, *new_fdt_addr);
  282. fail:
  283. efi_free(fdt_size, fdt_addr);
  284. efi_bs_call(FreePool, priv.runtime_map);
  285. return EFI_LOAD_ERROR;
  286. }
  287. efi_status_t efi_boot_kernel(efi_handle_t handle,
  288. efi_loaded_image_t *loaded_image,
  289. struct payload_info *payload_info,
  290. char *cmdline_ptr)
  291. {
  292. unsigned long fdt_addr;
  293. efi_status_t status;
  294. efi_info("Loading ELF payload...\n");
  295. // 加载ELF
  296. status = load_elf(payload_info);
  297. if (status != EFI_SUCCESS) {
  298. efi_err("Failed to load ELF payload, efi error code: %d\n",
  299. status);
  300. return status;
  301. }
  302. efi_debug("kernel entry point: 0x%lx\n", payload_info->kernel_entry);
  303. status = allocate_new_fdt_and_exit_boot(handle, loaded_image, &fdt_addr,
  304. cmdline_ptr);
  305. if (status != EFI_SUCCESS) {
  306. efi_err("Failed to update FDT and exit boot services\n");
  307. return status;
  308. }
  309. #ifdef CONFIG_ARM
  310. efi_handle_post_ebs_state();
  311. #endif
  312. efi_enter_kernel(payload_info, fdt_addr,
  313. fdt_totalsize((void *)fdt_addr));
  314. /* not reached */
  315. }
  316. void *get_fdt(unsigned long *fdt_size)
  317. {
  318. void *fdt;
  319. fdt = get_efi_config_table(DEVICE_TREE_GUID);
  320. if (!fdt)
  321. return NULL;
  322. if (fdt_check_header(fdt) != 0) {
  323. efi_err("Invalid header detected on UEFI supplied FDT, ignoring ...\n");
  324. return NULL;
  325. }
  326. *fdt_size = fdt_totalsize(fdt);
  327. return fdt;
  328. }