fdt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. fdt_val32 = cpu_to_fdt32(efi_get_secureboot(sys_table));
  139. status = fdt_setprop(fdt, node, "linux,uefi-secure-boot",
  140. &fdt_val32, sizeof(fdt_val32));
  141. if (status)
  142. goto fdt_set_fail;
  143. /* shrink the FDT back to its minimum size */
  144. fdt_pack(fdt);
  145. return EFI_SUCCESS;
  146. fdt_set_fail:
  147. if (status == -FDT_ERR_NOSPACE)
  148. return EFI_BUFFER_TOO_SMALL;
  149. return EFI_LOAD_ERROR;
  150. }
  151. static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map)
  152. {
  153. int node = fdt_path_offset(fdt, "/chosen");
  154. u64 fdt_val64;
  155. u32 fdt_val32;
  156. int err;
  157. if (node < 0)
  158. return EFI_LOAD_ERROR;
  159. fdt_val64 = cpu_to_fdt64((unsigned long)*map->map);
  160. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-start",
  161. &fdt_val64, sizeof(fdt_val64));
  162. if (err)
  163. return EFI_LOAD_ERROR;
  164. fdt_val32 = cpu_to_fdt32(*map->map_size);
  165. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-size",
  166. &fdt_val32, sizeof(fdt_val32));
  167. if (err)
  168. return EFI_LOAD_ERROR;
  169. fdt_val32 = cpu_to_fdt32(*map->desc_size);
  170. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-size",
  171. &fdt_val32, sizeof(fdt_val32));
  172. if (err)
  173. return EFI_LOAD_ERROR;
  174. fdt_val32 = cpu_to_fdt32(*map->desc_ver);
  175. err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-ver",
  176. &fdt_val32, sizeof(fdt_val32));
  177. if (err)
  178. return EFI_LOAD_ERROR;
  179. return EFI_SUCCESS;
  180. }
  181. #ifndef EFI_FDT_ALIGN
  182. #define EFI_FDT_ALIGN EFI_PAGE_SIZE
  183. #endif
  184. struct exit_boot_struct {
  185. efi_memory_desc_t *runtime_map;
  186. int *runtime_entry_count;
  187. void *new_fdt_addr;
  188. };
  189. static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
  190. struct efi_boot_memmap *map,
  191. void *priv)
  192. {
  193. struct exit_boot_struct *p = priv;
  194. /*
  195. * Update the memory map with virtual addresses. The function will also
  196. * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
  197. * entries so that we can pass it straight to SetVirtualAddressMap()
  198. */
  199. efi_get_virtmap(*map->map, *map->map_size, *map->desc_size,
  200. p->runtime_map, p->runtime_entry_count);
  201. return update_fdt_memmap(p->new_fdt_addr, map);
  202. }
  203. #ifndef MAX_FDT_SIZE
  204. #define MAX_FDT_SIZE SZ_2M
  205. #endif
  206. /*
  207. * Allocate memory for a new FDT, then add EFI, commandline, and
  208. * initrd related fields to the FDT. This routine increases the
  209. * FDT allocation size until the allocated memory is large
  210. * enough. EFI allocations are in EFI_PAGE_SIZE granules,
  211. * which are fixed at 4K bytes, so in most cases the first
  212. * allocation should succeed.
  213. * EFI boot services are exited at the end of this function.
  214. * There must be no allocations between the get_memory_map()
  215. * call and the exit_boot_services() call, so the exiting of
  216. * boot services is very tightly tied to the creation of the FDT
  217. * with the final memory map in it.
  218. */
  219. efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
  220. void *handle,
  221. unsigned long *new_fdt_addr,
  222. unsigned long max_addr,
  223. u64 initrd_addr, u64 initrd_size,
  224. char *cmdline_ptr,
  225. unsigned long fdt_addr,
  226. unsigned long fdt_size)
  227. {
  228. unsigned long map_size, desc_size, buff_size;
  229. u32 desc_ver;
  230. unsigned long mmap_key;
  231. efi_memory_desc_t *memory_map, *runtime_map;
  232. efi_status_t status;
  233. int runtime_entry_count = 0;
  234. struct efi_boot_memmap map;
  235. struct exit_boot_struct priv;
  236. map.map = &runtime_map;
  237. map.map_size = &map_size;
  238. map.desc_size = &desc_size;
  239. map.desc_ver = &desc_ver;
  240. map.key_ptr = &mmap_key;
  241. map.buff_size = &buff_size;
  242. /*
  243. * Get a copy of the current memory map that we will use to prepare
  244. * the input for SetVirtualAddressMap(). We don't have to worry about
  245. * subsequent allocations adding entries, since they could not affect
  246. * the number of EFI_MEMORY_RUNTIME regions.
  247. */
  248. status = efi_get_memory_map(sys_table, &map);
  249. if (status != EFI_SUCCESS) {
  250. pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
  251. return status;
  252. }
  253. pr_efi(sys_table,
  254. "Exiting boot services and installing virtual address map...\n");
  255. map.map = &memory_map;
  256. status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN,
  257. new_fdt_addr, max_addr);
  258. if (status != EFI_SUCCESS) {
  259. pr_efi_err(sys_table,
  260. "Unable to allocate memory for new device tree.\n");
  261. goto fail;
  262. }
  263. /*
  264. * Now that we have done our final memory allocation (and free)
  265. * we can get the memory map key needed for exit_boot_services().
  266. */
  267. status = efi_get_memory_map(sys_table, &map);
  268. if (status != EFI_SUCCESS)
  269. goto fail_free_new_fdt;
  270. status = update_fdt(sys_table, (void *)fdt_addr, fdt_size,
  271. (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr,
  272. initrd_addr, initrd_size);
  273. if (status != EFI_SUCCESS) {
  274. pr_efi_err(sys_table, "Unable to construct new device tree.\n");
  275. goto fail_free_new_fdt;
  276. }
  277. priv.runtime_map = runtime_map;
  278. priv.runtime_entry_count = &runtime_entry_count;
  279. priv.new_fdt_addr = (void *)*new_fdt_addr;
  280. status = efi_exit_boot_services(sys_table, handle, &map, &priv,
  281. exit_boot_func);
  282. if (status == EFI_SUCCESS) {
  283. efi_set_virtual_address_map_t *svam;
  284. if (novamap())
  285. return EFI_SUCCESS;
  286. /* Install the new virtual address map */
  287. svam = sys_table->runtime->set_virtual_address_map;
  288. status = svam(runtime_entry_count * desc_size, desc_size,
  289. desc_ver, runtime_map);
  290. /*
  291. * We are beyond the point of no return here, so if the call to
  292. * SetVirtualAddressMap() failed, we need to signal that to the
  293. * incoming kernel but proceed normally otherwise.
  294. */
  295. if (status != EFI_SUCCESS) {
  296. int l;
  297. /*
  298. * Set the virtual address field of all
  299. * EFI_MEMORY_RUNTIME entries to 0. This will signal
  300. * the incoming kernel that no virtual translation has
  301. * been installed.
  302. */
  303. for (l = 0; l < map_size; l += desc_size) {
  304. efi_memory_desc_t *p = (void *)memory_map + l;
  305. if (p->attribute & EFI_MEMORY_RUNTIME)
  306. p->virt_addr = 0;
  307. }
  308. }
  309. return EFI_SUCCESS;
  310. }
  311. pr_efi_err(sys_table, "Exit boot services failed.\n");
  312. fail_free_new_fdt:
  313. efi_free(sys_table, MAX_FDT_SIZE, *new_fdt_addr);
  314. fail:
  315. sys_table->boottime->free_pool(runtime_map);
  316. return EFI_LOAD_ERROR;
  317. }
  318. void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size)
  319. {
  320. efi_guid_t fdt_guid = DEVICE_TREE_GUID;
  321. efi_config_table_t *tables;
  322. void *fdt;
  323. int i;
  324. tables = (efi_config_table_t *) sys_table->tables;
  325. fdt = NULL;
  326. for (i = 0; i < sys_table->nr_tables; i++)
  327. if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
  328. fdt = (void *) tables[i].table;
  329. if (fdt_check_header(fdt) != 0) {
  330. pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
  331. return NULL;
  332. }
  333. *fdt_size = fdt_totalsize(fdt);
  334. break;
  335. }
  336. return fdt;
  337. }