fdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #define EFI_DT_ADDR_CELLS_DEFAULT 2
  17. #define EFI_DT_SIZE_CELLS_DEFAULT 2
  18. static void fdt_update_cell_size(efi_system_table_t *sys_table, void *fdt)
  19. {
  20. int offset;
  21. offset = fdt_path_offset(fdt, "/");
  22. /* Set the #address-cells and #size-cells values for an empty tree */
  23. fdt_setprop_u32(fdt, offset, "#address-cells",
  24. EFI_DT_ADDR_CELLS_DEFAULT);
  25. fdt_setprop_u32(fdt, offset, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT);
  26. }
  27. static efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt,
  28. unsigned long orig_fdt_size,
  29. void *fdt, int new_fdt_size, char *cmdline_ptr,
  30. u64 initrd_addr, u64 initrd_size)
  31. {
  32. int node, num_rsv;
  33. int status;
  34. u32 fdt_val32;
  35. u64 fdt_val64;
  36. /* Do some checks on provided FDT, if it exists*/
  37. if (orig_fdt) {
  38. if (fdt_check_header(orig_fdt)) {
  39. pr_efi_err(sys_table, "Device Tree header not valid!\n");
  40. return EFI_LOAD_ERROR;
  41. }
  42. /*
  43. * We don't get the size of the FDT if we get if from a
  44. * configuration table.
  45. */
  46. if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
  47. pr_efi_err(sys_table, "Truncated device tree! foo!\n");
  48. return EFI_LOAD_ERROR;
  49. }
  50. }
  51. if (orig_fdt) {
  52. status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
  53. } else {
  54. status = fdt_create_empty_tree(fdt, new_fdt_size);
  55. if (status == 0) {
  56. /*
  57. * Any failure from the following function is non
  58. * critical
  59. */
  60. fdt_update_cell_size(sys_table, fdt);
  61. }
  62. }
  63. if (status != 0)
  64. goto fdt_set_fail;
  65. /*
  66. * Delete all memory reserve map entries. When booting via UEFI,
  67. * kernel will use the UEFI memory map to find reserved regions.
  68. */
  69. num_rsv = fdt_num_mem_rsv(fdt);
  70. while (num_rsv-- > 0)
  71. fdt_del_mem_rsv(fdt, num_rsv);
  72. node = fdt_subnode_offset(fdt, 0, "chosen");
  73. if (node < 0) {
  74. node = fdt_add_subnode(fdt, 0, "chosen");
  75. if (node < 0) {
  76. status = node; /* node is error code when negative */
  77. goto fdt_set_fail;
  78. }
  79. }
  80. if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
  81. status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
  82. strlen(cmdline_ptr) + 1);
  83. if (status)
  84. goto fdt_set_fail;
  85. }
  86. /* Set initrd address/end in device tree, if present */
  87. if (initrd_size != 0) {
  88. u64 initrd_image_end;
  89. u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
  90. status = fdt_setprop(fdt, node, "linux,initrd-start",
  91. &initrd_image_start, sizeof(u64));
  92. if (status)
  93. goto fdt_set_fail;
  94. initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
  95. status = fdt_setprop(fdt, node, "linux,initrd-end",
  96. &initrd_image_end, sizeof(u64));
  97. if (status)
  98. goto fdt_set_fail;
  99. }
  100. /* Add FDT entries for EFI runtime services in chosen node. */
  101. node = fdt_subnode_offset(fdt, 0, "chosen");
  102. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table);
  103. status = fdt_setprop(fdt, node, "linux,uefi-system-table",
  104. &fdt_val64, sizeof(fdt_val64));
  105. if (status)
  106. goto fdt_set_fail;
  107. fdt_val64 = U64_MAX; /* placeholder */
  108. status = fdt_setprop(fdt, node, "linux,uefi-mmap-start",
  109. &fdt_val64, sizeof(fdt_val64));
  110. if (status)
  111. goto fdt_set_fail;
  112. fdt_val32 = U32_MAX; /* placeholder */
  113. status = fdt_setprop(fdt, node, "linux,uefi-mmap-size",
  114. &fdt_val32, sizeof(fdt_val32));
  115. if (status)
  116. goto fdt_set_fail;
  117. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size",
  118. &fdt_val32, sizeof(fdt_val32));
  119. if (status)
  120. goto fdt_set_fail;
  121. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver",
  122. &fdt_val32, sizeof(fdt_val32));
  123. if (status)
  124. goto fdt_set_fail;
  125. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  126. efi_status_t efi_status;
  127. efi_status = efi_get_random_bytes(sys_table, sizeof(fdt_val64),
  128. (u8 *)&fdt_val64);
  129. if (efi_status == EFI_SUCCESS) {
  130. status = fdt_setprop(fdt, node, "kaslr-seed",
  131. &fdt_val64, sizeof(fdt_val64));
  132. if (status)
  133. goto fdt_set_fail;
  134. } else if (efi_status != EFI_NOT_FOUND) {
  135. return efi_status;
  136. }
  137. }
  138. /* shrink the FDT back to its minimum size */
  139. fdt_pack(fdt);
  140. return EFI_SUCCESS;
  141. fdt_set_fail:
  142. if (status == -FDT_ERR_NOSPACE)
  143. return EFI_BUFFER_TOO_SMALL;
  144. return EFI_LOAD_ERROR;
  145. }
  146. static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
  147. {
  148. int node = fdt_path_offset(fdt, "/chosen");
  149. u64 fdt_val64;
  150. u32 fdt_val32;
  151. int err;
  152. if (node < 0)
  153. return EFI_LOAD_ERROR;
  154. fdt_val64 = cpu_to_fdt64((unsigned long)*map->map);
  155. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-start",
  156. &fdt_val64, sizeof(fdt_val64));
  157. if (err)
  158. return EFI_LOAD_ERROR;
  159. fdt_val32 = cpu_to_fdt32(*map->map_size);
  160. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-size",
  161. &fdt_val32, sizeof(fdt_val32));
  162. if (err)
  163. return EFI_LOAD_ERROR;
  164. fdt_val32 = cpu_to_fdt32(*map->desc_size);
  165. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-size",
  166. &fdt_val32, sizeof(fdt_val32));
  167. if (err)
  168. return EFI_LOAD_ERROR;
  169. fdt_val32 = cpu_to_fdt32(*map->desc_ver);
  170. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-ver",
  171. &fdt_val32, sizeof(fdt_val32));
  172. if (err)
  173. return EFI_LOAD_ERROR;
  174. return EFI_SUCCESS;
  175. }
  176. #ifndef EFI_FDT_ALIGN
  177. #define EFI_FDT_ALIGN EFI_PAGE_SIZE
  178. #endif
  179. struct exit_boot_struct {
  180. efi_memory_desc_t *runtime_map;
  181. int *runtime_entry_count;
  182. void *new_fdt_addr;
  183. };
  184. static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
  185. struct efi_boot_memmap *map,
  186. void *priv)
  187. {
  188. struct exit_boot_struct *p = priv;
  189. /*
  190. * Update the memory map with virtual addresses. The function will also
  191. * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
  192. * entries so that we can pass it straight to SetVirtualAddressMap()
  193. */
  194. efi_get_virtmap(*map->map, *map->map_size, *map->desc_size,
  195. p->runtime_map, p->runtime_entry_count);
  196. return update_fdt_memmap(p->new_fdt_addr, map);
  197. }
  198. #ifndef MAX_FDT_SIZE
  199. #define MAX_FDT_SIZE SZ_2M
  200. #endif
  201. /*
  202. * Allocate memory for a new FDT, then add EFI, commandline, and
  203. * initrd related fields to the FDT. This routine increases the
  204. * FDT allocation size until the allocated memory is large
  205. * enough. EFI allocations are in EFI_PAGE_SIZE granules,
  206. * which are fixed at 4K bytes, so in most cases the first
  207. * allocation should succeed.
  208. * EFI boot services are exited at the end of this function.
  209. * There must be no allocations between the get_memory_map()
  210. * call and the exit_boot_services() call, so the exiting of
  211. * boot services is very tightly tied to the creation of the FDT
  212. * with the final memory map in it.
  213. */
  214. efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
  215. void *handle,
  216. unsigned long *new_fdt_addr,
  217. unsigned long max_addr,
  218. u64 initrd_addr, u64 initrd_size,
  219. char *cmdline_ptr,
  220. unsigned long fdt_addr,
  221. unsigned long fdt_size)
  222. {
  223. unsigned long map_size, desc_size, buff_size;
  224. u32 desc_ver;
  225. unsigned long mmap_key;
  226. efi_memory_desc_t *memory_map, *runtime_map;
  227. efi_status_t status;
  228. int runtime_entry_count = 0;
  229. struct efi_boot_memmap map;
  230. struct exit_boot_struct priv;
  231. map.map = &runtime_map;
  232. map.map_size = &map_size;
  233. map.desc_size = &desc_size;
  234. map.desc_ver = &desc_ver;
  235. map.key_ptr = &mmap_key;
  236. map.buff_size = &buff_size;
  237. /*
  238. * Get a copy of the current memory map that we will use to prepare
  239. * the input for SetVirtualAddressMap(). We don't have to worry about
  240. * subsequent allocations adding entries, since they could not affect
  241. * the number of EFI_MEMORY_RUNTIME regions.
  242. */
  243. status = efi_get_memory_map(sys_table, &map);
  244. if (status != EFI_SUCCESS) {
  245. pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
  246. return status;
  247. }
  248. pr_efi(sys_table,
  249. "Exiting boot services and installing virtual address map...\n");
  250. map.map = &memory_map;
  251. status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN,
  252. new_fdt_addr, max_addr);
  253. if (status != EFI_SUCCESS) {
  254. pr_efi_err(sys_table,
  255. "Unable to allocate memory for new device tree.\n");
  256. goto fail;
  257. }
  258. /*
  259. * Now that we have done our final memory allocation (and free)
  260. * we can get the memory map key needed for exit_boot_services().
  261. */
  262. status = efi_get_memory_map(sys_table, &map);
  263. if (status != EFI_SUCCESS)
  264. goto fail_free_new_fdt;
  265. status = update_fdt(sys_table, (void *)fdt_addr, fdt_size,
  266. (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr,
  267. initrd_addr, initrd_size);
  268. if (status != EFI_SUCCESS) {
  269. pr_efi_err(sys_table, "Unable to construct new device tree.\n");
  270. goto fail_free_new_fdt;
  271. }
  272. priv.runtime_map = runtime_map;
  273. priv.runtime_entry_count = &runtime_entry_count;
  274. priv.new_fdt_addr = (void *)*new_fdt_addr;
  275. status = efi_exit_boot_services(sys_table, handle, &map, &priv,
  276. exit_boot_func);
  277. if (status == EFI_SUCCESS) {
  278. efi_set_virtual_address_map_t *svam;
  279. if (novamap())
  280. return EFI_SUCCESS;
  281. /* Install the new virtual address map */
  282. svam = sys_table->runtime->set_virtual_address_map;
  283. status = svam(runtime_entry_count * desc_size, desc_size,
  284. desc_ver, runtime_map);
  285. /*
  286. * We are beyond the point of no return here, so if the call to
  287. * SetVirtualAddressMap() failed, we need to signal that to the
  288. * incoming kernel but proceed normally otherwise.
  289. */
  290. if (status != EFI_SUCCESS) {
  291. int l;
  292. /*
  293. * Set the virtual address field of all
  294. * EFI_MEMORY_RUNTIME entries to 0. This will signal
  295. * the incoming kernel that no virtual translation has
  296. * been installed.
  297. */
  298. for (l = 0; l < map_size; l += desc_size) {
  299. efi_memory_desc_t *p = (void *)memory_map + l;
  300. if (p->attribute & EFI_MEMORY_RUNTIME)
  301. p->virt_addr = 0;
  302. }
  303. }
  304. return EFI_SUCCESS;
  305. }
  306. pr_efi_err(sys_table, "Exit boot services failed.\n");
  307. fail_free_new_fdt:
  308. efi_free(sys_table, MAX_FDT_SIZE, *new_fdt_addr);
  309. fail:
  310. sys_table->boottime->free_pool(runtime_map);
  311. return EFI_LOAD_ERROR;
  312. }
  313. void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size)
  314. {
  315. efi_guid_t fdt_guid = DEVICE_TREE_GUID;
  316. efi_config_table_t *tables;
  317. void *fdt;
  318. int i;
  319. tables = (efi_config_table_t *) sys_table->tables;
  320. fdt = NULL;
  321. for (i = 0; i < sys_table->nr_tables; i++)
  322. if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
  323. fdt = (void *) tables[i].table;
  324. if (fdt_check_header(fdt) != 0) {
  325. pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
  326. return NULL;
  327. }
  328. *fdt_size = fdt_totalsize(fdt);
  329. break;
  330. }
  331. return fdt;
  332. }