fdt.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * FDT related Helper functions used by the EFI stub on multiple
  3. * architectures. This should be #included by the EFI stub
  4. * implementation files.
  5. *
  6. * Copyright 2013 Linaro Limited; author Roy Franz
  7. *
  8. * This file is part of the Linux kernel, and is made available
  9. * under the terms of the GNU General Public License version 2.
  10. *
  11. */
  12. #include <linux/efi.h>
  13. #include <linux/libfdt.h>
  14. #include <asm/efi.h>
  15. #include "efistub.h"
  16. efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt,
  17. unsigned long orig_fdt_size,
  18. void *fdt, int new_fdt_size, char *cmdline_ptr,
  19. u64 initrd_addr, u64 initrd_size,
  20. efi_memory_desc_t *memory_map,
  21. unsigned long map_size, unsigned long desc_size,
  22. u32 desc_ver)
  23. {
  24. int node, num_rsv;
  25. int status;
  26. u32 fdt_val32;
  27. u64 fdt_val64;
  28. /* Do some checks on provided FDT, if it exists*/
  29. if (orig_fdt) {
  30. if (fdt_check_header(orig_fdt)) {
  31. pr_efi_err(sys_table, "Device Tree header not valid!\n");
  32. return EFI_LOAD_ERROR;
  33. }
  34. /*
  35. * We don't get the size of the FDT if we get if from a
  36. * configuration table.
  37. */
  38. if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
  39. pr_efi_err(sys_table, "Truncated device tree! foo!\n");
  40. return EFI_LOAD_ERROR;
  41. }
  42. }
  43. if (orig_fdt)
  44. status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
  45. else
  46. status = fdt_create_empty_tree(fdt, new_fdt_size);
  47. if (status != 0)
  48. goto fdt_set_fail;
  49. /*
  50. * Delete all memory reserve map entries. When booting via UEFI,
  51. * kernel will use the UEFI memory map to find reserved regions.
  52. */
  53. num_rsv = fdt_num_mem_rsv(fdt);
  54. while (num_rsv-- > 0)
  55. fdt_del_mem_rsv(fdt, num_rsv);
  56. node = fdt_subnode_offset(fdt, 0, "chosen");
  57. if (node < 0) {
  58. node = fdt_add_subnode(fdt, 0, "chosen");
  59. if (node < 0) {
  60. status = node; /* node is error code when negative */
  61. goto fdt_set_fail;
  62. }
  63. }
  64. if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
  65. status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
  66. strlen(cmdline_ptr) + 1);
  67. if (status)
  68. goto fdt_set_fail;
  69. }
  70. /* Set initrd address/end in device tree, if present */
  71. if (initrd_size != 0) {
  72. u64 initrd_image_end;
  73. u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
  74. status = fdt_setprop(fdt, node, "linux,initrd-start",
  75. &initrd_image_start, sizeof(u64));
  76. if (status)
  77. goto fdt_set_fail;
  78. initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
  79. status = fdt_setprop(fdt, node, "linux,initrd-end",
  80. &initrd_image_end, sizeof(u64));
  81. if (status)
  82. goto fdt_set_fail;
  83. }
  84. /* Add FDT entries for EFI runtime services in chosen node. */
  85. node = fdt_subnode_offset(fdt, 0, "chosen");
  86. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table);
  87. status = fdt_setprop(fdt, node, "linux,uefi-system-table",
  88. &fdt_val64, sizeof(fdt_val64));
  89. if (status)
  90. goto fdt_set_fail;
  91. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)memory_map);
  92. status = fdt_setprop(fdt, node, "linux,uefi-mmap-start",
  93. &fdt_val64, sizeof(fdt_val64));
  94. if (status)
  95. goto fdt_set_fail;
  96. fdt_val32 = cpu_to_fdt32(map_size);
  97. status = fdt_setprop(fdt, node, "linux,uefi-mmap-size",
  98. &fdt_val32, sizeof(fdt_val32));
  99. if (status)
  100. goto fdt_set_fail;
  101. fdt_val32 = cpu_to_fdt32(desc_size);
  102. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size",
  103. &fdt_val32, sizeof(fdt_val32));
  104. if (status)
  105. goto fdt_set_fail;
  106. fdt_val32 = cpu_to_fdt32(desc_ver);
  107. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver",
  108. &fdt_val32, sizeof(fdt_val32));
  109. if (status)
  110. goto fdt_set_fail;
  111. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  112. efi_status_t efi_status;
  113. efi_status = efi_get_random_bytes(sys_table, sizeof(fdt_val64),
  114. (u8 *)&fdt_val64);
  115. if (efi_status == EFI_SUCCESS) {
  116. status = fdt_setprop(fdt, node, "kaslr-seed",
  117. &fdt_val64, sizeof(fdt_val64));
  118. if (status)
  119. goto fdt_set_fail;
  120. } else if (efi_status != EFI_NOT_FOUND) {
  121. return efi_status;
  122. }
  123. }
  124. return EFI_SUCCESS;
  125. fdt_set_fail:
  126. if (status == -FDT_ERR_NOSPACE)
  127. return EFI_BUFFER_TOO_SMALL;
  128. return EFI_LOAD_ERROR;
  129. }
  130. #ifndef EFI_FDT_ALIGN
  131. #define EFI_FDT_ALIGN EFI_PAGE_SIZE
  132. #endif
  133. /*
  134. * Allocate memory for a new FDT, then add EFI, commandline, and
  135. * initrd related fields to the FDT. This routine increases the
  136. * FDT allocation size until the allocated memory is large
  137. * enough. EFI allocations are in EFI_PAGE_SIZE granules,
  138. * which are fixed at 4K bytes, so in most cases the first
  139. * allocation should succeed.
  140. * EFI boot services are exited at the end of this function.
  141. * There must be no allocations between the get_memory_map()
  142. * call and the exit_boot_services() call, so the exiting of
  143. * boot services is very tightly tied to the creation of the FDT
  144. * with the final memory map in it.
  145. */
  146. efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
  147. void *handle,
  148. unsigned long *new_fdt_addr,
  149. unsigned long max_addr,
  150. u64 initrd_addr, u64 initrd_size,
  151. char *cmdline_ptr,
  152. unsigned long fdt_addr,
  153. unsigned long fdt_size)
  154. {
  155. unsigned long map_size, desc_size;
  156. u32 desc_ver;
  157. unsigned long mmap_key;
  158. efi_memory_desc_t *memory_map, *runtime_map;
  159. unsigned long new_fdt_size;
  160. efi_status_t status;
  161. int runtime_entry_count = 0;
  162. /*
  163. * Get a copy of the current memory map that we will use to prepare
  164. * the input for SetVirtualAddressMap(). We don't have to worry about
  165. * subsequent allocations adding entries, since they could not affect
  166. * the number of EFI_MEMORY_RUNTIME regions.
  167. */
  168. status = efi_get_memory_map(sys_table, &runtime_map, &map_size,
  169. &desc_size, &desc_ver, &mmap_key);
  170. if (status != EFI_SUCCESS) {
  171. pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
  172. return status;
  173. }
  174. pr_efi(sys_table,
  175. "Exiting boot services and installing virtual address map...\n");
  176. /*
  177. * Estimate size of new FDT, and allocate memory for it. We
  178. * will allocate a bigger buffer if this ends up being too
  179. * small, so a rough guess is OK here.
  180. */
  181. new_fdt_size = fdt_size + EFI_PAGE_SIZE;
  182. while (1) {
  183. status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
  184. new_fdt_addr, max_addr);
  185. if (status != EFI_SUCCESS) {
  186. pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
  187. goto fail;
  188. }
  189. /*
  190. * Now that we have done our final memory allocation (and free)
  191. * we can get the memory map key needed for
  192. * exit_boot_services().
  193. */
  194. status = efi_get_memory_map(sys_table, &memory_map, &map_size,
  195. &desc_size, &desc_ver, &mmap_key);
  196. if (status != EFI_SUCCESS)
  197. goto fail_free_new_fdt;
  198. status = update_fdt(sys_table,
  199. (void *)fdt_addr, fdt_size,
  200. (void *)*new_fdt_addr, new_fdt_size,
  201. cmdline_ptr, initrd_addr, initrd_size,
  202. memory_map, map_size, desc_size, desc_ver);
  203. /* Succeeding the first time is the expected case. */
  204. if (status == EFI_SUCCESS)
  205. break;
  206. if (status == EFI_BUFFER_TOO_SMALL) {
  207. /*
  208. * We need to allocate more space for the new
  209. * device tree, so free existing buffer that is
  210. * too small. Also free memory map, as we will need
  211. * to get new one that reflects the free/alloc we do
  212. * on the device tree buffer.
  213. */
  214. efi_free(sys_table, new_fdt_size, *new_fdt_addr);
  215. sys_table->boottime->free_pool(memory_map);
  216. new_fdt_size += EFI_PAGE_SIZE;
  217. } else {
  218. pr_efi_err(sys_table, "Unable to construct new device tree.\n");
  219. goto fail_free_mmap;
  220. }
  221. }
  222. /*
  223. * Update the memory map with virtual addresses. The function will also
  224. * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
  225. * entries so that we can pass it straight into SetVirtualAddressMap()
  226. */
  227. efi_get_virtmap(memory_map, map_size, desc_size, runtime_map,
  228. &runtime_entry_count);
  229. /* Now we are ready to exit_boot_services.*/
  230. status = sys_table->boottime->exit_boot_services(handle, mmap_key);
  231. if (status == EFI_SUCCESS) {
  232. efi_set_virtual_address_map_t *svam;
  233. /* Install the new virtual address map */
  234. svam = sys_table->runtime->set_virtual_address_map;
  235. status = svam(runtime_entry_count * desc_size, desc_size,
  236. desc_ver, runtime_map);
  237. /*
  238. * We are beyond the point of no return here, so if the call to
  239. * SetVirtualAddressMap() failed, we need to signal that to the
  240. * incoming kernel but proceed normally otherwise.
  241. */
  242. if (status != EFI_SUCCESS) {
  243. int l;
  244. /*
  245. * Set the virtual address field of all
  246. * EFI_MEMORY_RUNTIME entries to 0. This will signal
  247. * the incoming kernel that no virtual translation has
  248. * been installed.
  249. */
  250. for (l = 0; l < map_size; l += desc_size) {
  251. efi_memory_desc_t *p = (void *)memory_map + l;
  252. if (p->attribute & EFI_MEMORY_RUNTIME)
  253. p->virt_addr = 0;
  254. }
  255. }
  256. return EFI_SUCCESS;
  257. }
  258. pr_efi_err(sys_table, "Exit boot services failed.\n");
  259. fail_free_mmap:
  260. sys_table->boottime->free_pool(memory_map);
  261. fail_free_new_fdt:
  262. efi_free(sys_table, new_fdt_size, *new_fdt_addr);
  263. fail:
  264. sys_table->boottime->free_pool(runtime_map);
  265. return EFI_LOAD_ERROR;
  266. }
  267. void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size)
  268. {
  269. efi_guid_t fdt_guid = DEVICE_TREE_GUID;
  270. efi_config_table_t *tables;
  271. void *fdt;
  272. int i;
  273. tables = (efi_config_table_t *) sys_table->tables;
  274. fdt = NULL;
  275. for (i = 0; i < sys_table->nr_tables; i++)
  276. if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
  277. fdt = (void *) tables[i].table;
  278. if (fdt_check_header(fdt) != 0) {
  279. pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
  280. return NULL;
  281. }
  282. *fdt_size = fdt_totalsize(fdt);
  283. break;
  284. }
  285. return fdt;
  286. }