fdt.c 9.8 KB

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