fdt.c 9.7 KB

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