fdt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt,
  16. unsigned long orig_fdt_size,
  17. void *fdt, int new_fdt_size, char *cmdline_ptr,
  18. u64 initrd_addr, u64 initrd_size,
  19. efi_memory_desc_t *memory_map,
  20. unsigned long map_size, unsigned long desc_size,
  21. u32 desc_ver)
  22. {
  23. int node, prev, num_rsv;
  24. int status;
  25. u32 fdt_val32;
  26. u64 fdt_val64;
  27. /* Do some checks on provided FDT, if it exists*/
  28. if (orig_fdt) {
  29. if (fdt_check_header(orig_fdt)) {
  30. pr_efi_err(sys_table, "Device Tree header not valid!\n");
  31. return EFI_LOAD_ERROR;
  32. }
  33. /*
  34. * We don't get the size of the FDT if we get if from a
  35. * configuration table.
  36. */
  37. if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
  38. pr_efi_err(sys_table, "Truncated device tree! foo!\n");
  39. return EFI_LOAD_ERROR;
  40. }
  41. }
  42. if (orig_fdt)
  43. status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
  44. else
  45. status = fdt_create_empty_tree(fdt, new_fdt_size);
  46. if (status != 0)
  47. goto fdt_set_fail;
  48. /*
  49. * Delete any memory nodes present. We must delete nodes which
  50. * early_init_dt_scan_memory may try to use.
  51. */
  52. prev = 0;
  53. for (;;) {
  54. const char *type;
  55. int len;
  56. node = fdt_next_node(fdt, prev, NULL);
  57. if (node < 0)
  58. break;
  59. type = fdt_getprop(fdt, node, "device_type", &len);
  60. if (type && strncmp(type, "memory", len) == 0) {
  61. fdt_del_node(fdt, node);
  62. continue;
  63. }
  64. prev = node;
  65. }
  66. /*
  67. * Delete all memory reserve map entries. When booting via UEFI,
  68. * kernel will use the UEFI memory map to find reserved regions.
  69. */
  70. num_rsv = fdt_num_mem_rsv(fdt);
  71. while (num_rsv-- > 0)
  72. fdt_del_mem_rsv(fdt, num_rsv);
  73. node = fdt_subnode_offset(fdt, 0, "chosen");
  74. if (node < 0) {
  75. node = fdt_add_subnode(fdt, 0, "chosen");
  76. if (node < 0) {
  77. status = node; /* node is error code when negative */
  78. goto fdt_set_fail;
  79. }
  80. }
  81. if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
  82. status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
  83. strlen(cmdline_ptr) + 1);
  84. if (status)
  85. goto fdt_set_fail;
  86. }
  87. /* Set initrd address/end in device tree, if present */
  88. if (initrd_size != 0) {
  89. u64 initrd_image_end;
  90. u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
  91. status = fdt_setprop(fdt, node, "linux,initrd-start",
  92. &initrd_image_start, sizeof(u64));
  93. if (status)
  94. goto fdt_set_fail;
  95. initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
  96. status = fdt_setprop(fdt, node, "linux,initrd-end",
  97. &initrd_image_end, sizeof(u64));
  98. if (status)
  99. goto fdt_set_fail;
  100. }
  101. /* Add FDT entries for EFI runtime services in chosen node. */
  102. node = fdt_subnode_offset(fdt, 0, "chosen");
  103. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table);
  104. status = fdt_setprop(fdt, node, "linux,uefi-system-table",
  105. &fdt_val64, sizeof(fdt_val64));
  106. if (status)
  107. goto fdt_set_fail;
  108. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)memory_map);
  109. status = fdt_setprop(fdt, node, "linux,uefi-mmap-start",
  110. &fdt_val64, sizeof(fdt_val64));
  111. if (status)
  112. goto fdt_set_fail;
  113. fdt_val32 = cpu_to_fdt32(map_size);
  114. status = fdt_setprop(fdt, node, "linux,uefi-mmap-size",
  115. &fdt_val32, sizeof(fdt_val32));
  116. if (status)
  117. goto fdt_set_fail;
  118. fdt_val32 = cpu_to_fdt32(desc_size);
  119. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size",
  120. &fdt_val32, sizeof(fdt_val32));
  121. if (status)
  122. goto fdt_set_fail;
  123. fdt_val32 = cpu_to_fdt32(desc_ver);
  124. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver",
  125. &fdt_val32, sizeof(fdt_val32));
  126. if (status)
  127. goto fdt_set_fail;
  128. /*
  129. * Add kernel version banner so stub/kernel match can be
  130. * verified.
  131. */
  132. status = fdt_setprop_string(fdt, node, "linux,uefi-stub-kern-ver",
  133. linux_banner);
  134. if (status)
  135. goto fdt_set_fail;
  136. return EFI_SUCCESS;
  137. fdt_set_fail:
  138. if (status == -FDT_ERR_NOSPACE)
  139. return EFI_BUFFER_TOO_SMALL;
  140. return EFI_LOAD_ERROR;
  141. }
  142. #ifndef EFI_FDT_ALIGN
  143. #define EFI_FDT_ALIGN EFI_PAGE_SIZE
  144. #endif
  145. /*
  146. * Allocate memory for a new FDT, then add EFI, commandline, and
  147. * initrd related fields to the FDT. This routine increases the
  148. * FDT allocation size until the allocated memory is large
  149. * enough. EFI allocations are in EFI_PAGE_SIZE granules,
  150. * which are fixed at 4K bytes, so in most cases the first
  151. * allocation should succeed.
  152. * EFI boot services are exited at the end of this function.
  153. * There must be no allocations between the get_memory_map()
  154. * call and the exit_boot_services() call, so the exiting of
  155. * boot services is very tightly tied to the creation of the FDT
  156. * with the final memory map in it.
  157. */
  158. efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
  159. void *handle,
  160. unsigned long *new_fdt_addr,
  161. unsigned long max_addr,
  162. u64 initrd_addr, u64 initrd_size,
  163. char *cmdline_ptr,
  164. unsigned long fdt_addr,
  165. unsigned long fdt_size)
  166. {
  167. unsigned long map_size, desc_size;
  168. u32 desc_ver;
  169. unsigned long mmap_key;
  170. efi_memory_desc_t *memory_map;
  171. unsigned long new_fdt_size;
  172. efi_status_t status;
  173. /*
  174. * Estimate size of new FDT, and allocate memory for it. We
  175. * will allocate a bigger buffer if this ends up being too
  176. * small, so a rough guess is OK here.
  177. */
  178. new_fdt_size = fdt_size + EFI_PAGE_SIZE;
  179. while (1) {
  180. status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
  181. new_fdt_addr, max_addr);
  182. if (status != EFI_SUCCESS) {
  183. pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
  184. goto fail;
  185. }
  186. /*
  187. * Now that we have done our final memory allocation (and free)
  188. * we can get the memory map key needed for
  189. * exit_boot_services().
  190. */
  191. status = efi_get_memory_map(sys_table, &memory_map, &map_size,
  192. &desc_size, &desc_ver, &mmap_key);
  193. if (status != EFI_SUCCESS)
  194. goto fail_free_new_fdt;
  195. status = update_fdt(sys_table,
  196. (void *)fdt_addr, fdt_size,
  197. (void *)*new_fdt_addr, new_fdt_size,
  198. cmdline_ptr, initrd_addr, initrd_size,
  199. memory_map, map_size, desc_size, desc_ver);
  200. /* Succeeding the first time is the expected case. */
  201. if (status == EFI_SUCCESS)
  202. break;
  203. if (status == EFI_BUFFER_TOO_SMALL) {
  204. /*
  205. * We need to allocate more space for the new
  206. * device tree, so free existing buffer that is
  207. * too small. Also free memory map, as we will need
  208. * to get new one that reflects the free/alloc we do
  209. * on the device tree buffer.
  210. */
  211. efi_free(sys_table, new_fdt_size, *new_fdt_addr);
  212. sys_table->boottime->free_pool(memory_map);
  213. new_fdt_size += EFI_PAGE_SIZE;
  214. } else {
  215. pr_efi_err(sys_table, "Unable to constuct new device tree.\n");
  216. goto fail_free_mmap;
  217. }
  218. }
  219. /* Now we are ready to exit_boot_services.*/
  220. status = sys_table->boottime->exit_boot_services(handle, mmap_key);
  221. if (status == EFI_SUCCESS)
  222. return status;
  223. pr_efi_err(sys_table, "Exit boot services failed.\n");
  224. fail_free_mmap:
  225. sys_table->boottime->free_pool(memory_map);
  226. fail_free_new_fdt:
  227. efi_free(sys_table, new_fdt_size, *new_fdt_addr);
  228. fail:
  229. return EFI_LOAD_ERROR;
  230. }
  231. void *get_fdt(efi_system_table_t *sys_table)
  232. {
  233. efi_guid_t fdt_guid = DEVICE_TREE_GUID;
  234. efi_config_table_t *tables;
  235. void *fdt;
  236. int i;
  237. tables = (efi_config_table_t *) sys_table->tables;
  238. fdt = NULL;
  239. for (i = 0; i < sys_table->nr_tables; i++)
  240. if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
  241. fdt = (void *) tables[i].table;
  242. break;
  243. }
  244. return fdt;
  245. }