fdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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;
  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. node = fdt_subnode_offset(fdt, 0, "chosen");
  67. if (node < 0) {
  68. node = fdt_add_subnode(fdt, 0, "chosen");
  69. if (node < 0) {
  70. status = node; /* node is error code when negative */
  71. goto fdt_set_fail;
  72. }
  73. }
  74. if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
  75. status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
  76. strlen(cmdline_ptr) + 1);
  77. if (status)
  78. goto fdt_set_fail;
  79. }
  80. /* Set initrd address/end in device tree, if present */
  81. if (initrd_size != 0) {
  82. u64 initrd_image_end;
  83. u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
  84. status = fdt_setprop(fdt, node, "linux,initrd-start",
  85. &initrd_image_start, sizeof(u64));
  86. if (status)
  87. goto fdt_set_fail;
  88. initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
  89. status = fdt_setprop(fdt, node, "linux,initrd-end",
  90. &initrd_image_end, sizeof(u64));
  91. if (status)
  92. goto fdt_set_fail;
  93. }
  94. /* Add FDT entries for EFI runtime services in chosen node. */
  95. node = fdt_subnode_offset(fdt, 0, "chosen");
  96. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table);
  97. status = fdt_setprop(fdt, node, "linux,uefi-system-table",
  98. &fdt_val64, sizeof(fdt_val64));
  99. if (status)
  100. goto fdt_set_fail;
  101. fdt_val64 = cpu_to_fdt64((u64)(unsigned long)memory_map);
  102. status = fdt_setprop(fdt, node, "linux,uefi-mmap-start",
  103. &fdt_val64, sizeof(fdt_val64));
  104. if (status)
  105. goto fdt_set_fail;
  106. fdt_val32 = cpu_to_fdt32(map_size);
  107. status = fdt_setprop(fdt, node, "linux,uefi-mmap-size",
  108. &fdt_val32, sizeof(fdt_val32));
  109. if (status)
  110. goto fdt_set_fail;
  111. fdt_val32 = cpu_to_fdt32(desc_size);
  112. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size",
  113. &fdt_val32, sizeof(fdt_val32));
  114. if (status)
  115. goto fdt_set_fail;
  116. fdt_val32 = cpu_to_fdt32(desc_ver);
  117. status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver",
  118. &fdt_val32, sizeof(fdt_val32));
  119. if (status)
  120. goto fdt_set_fail;
  121. /*
  122. * Add kernel version banner so stub/kernel match can be
  123. * verified.
  124. */
  125. status = fdt_setprop_string(fdt, node, "linux,uefi-stub-kern-ver",
  126. linux_banner);
  127. if (status)
  128. goto fdt_set_fail;
  129. return EFI_SUCCESS;
  130. fdt_set_fail:
  131. if (status == -FDT_ERR_NOSPACE)
  132. return EFI_BUFFER_TOO_SMALL;
  133. return EFI_LOAD_ERROR;
  134. }
  135. #ifndef EFI_FDT_ALIGN
  136. #define EFI_FDT_ALIGN EFI_PAGE_SIZE
  137. #endif
  138. /*
  139. * Allocate memory for a new FDT, then add EFI, commandline, and
  140. * initrd related fields to the FDT. This routine increases the
  141. * FDT allocation size until the allocated memory is large
  142. * enough. EFI allocations are in EFI_PAGE_SIZE granules,
  143. * which are fixed at 4K bytes, so in most cases the first
  144. * allocation should succeed.
  145. * EFI boot services are exited at the end of this function.
  146. * There must be no allocations between the get_memory_map()
  147. * call and the exit_boot_services() call, so the exiting of
  148. * boot services is very tightly tied to the creation of the FDT
  149. * with the final memory map in it.
  150. */
  151. efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
  152. void *handle,
  153. unsigned long *new_fdt_addr,
  154. unsigned long max_addr,
  155. u64 initrd_addr, u64 initrd_size,
  156. char *cmdline_ptr,
  157. unsigned long fdt_addr,
  158. unsigned long fdt_size)
  159. {
  160. unsigned long map_size, desc_size;
  161. u32 desc_ver;
  162. unsigned long mmap_key;
  163. efi_memory_desc_t *memory_map;
  164. unsigned long new_fdt_size;
  165. efi_status_t status;
  166. /*
  167. * Estimate size of new FDT, and allocate memory for it. We
  168. * will allocate a bigger buffer if this ends up being too
  169. * small, so a rough guess is OK here.
  170. */
  171. new_fdt_size = fdt_size + EFI_PAGE_SIZE;
  172. while (1) {
  173. status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
  174. new_fdt_addr, max_addr);
  175. if (status != EFI_SUCCESS) {
  176. pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
  177. goto fail;
  178. }
  179. /*
  180. * Now that we have done our final memory allocation (and free)
  181. * we can get the memory map key needed for
  182. * exit_boot_services().
  183. */
  184. status = efi_get_memory_map(sys_table, &memory_map, &map_size,
  185. &desc_size, &desc_ver, &mmap_key);
  186. if (status != EFI_SUCCESS)
  187. goto fail_free_new_fdt;
  188. status = update_fdt(sys_table,
  189. (void *)fdt_addr, fdt_size,
  190. (void *)*new_fdt_addr, new_fdt_size,
  191. cmdline_ptr, initrd_addr, initrd_size,
  192. memory_map, map_size, desc_size, desc_ver);
  193. /* Succeeding the first time is the expected case. */
  194. if (status == EFI_SUCCESS)
  195. break;
  196. if (status == EFI_BUFFER_TOO_SMALL) {
  197. /*
  198. * We need to allocate more space for the new
  199. * device tree, so free existing buffer that is
  200. * too small. Also free memory map, as we will need
  201. * to get new one that reflects the free/alloc we do
  202. * on the device tree buffer.
  203. */
  204. efi_free(sys_table, new_fdt_size, *new_fdt_addr);
  205. sys_table->boottime->free_pool(memory_map);
  206. new_fdt_size += EFI_PAGE_SIZE;
  207. } else {
  208. pr_efi_err(sys_table, "Unable to constuct new device tree.\n");
  209. goto fail_free_mmap;
  210. }
  211. }
  212. /* Now we are ready to exit_boot_services.*/
  213. status = sys_table->boottime->exit_boot_services(handle, mmap_key);
  214. if (status == EFI_SUCCESS)
  215. return status;
  216. pr_efi_err(sys_table, "Exit boot services failed.\n");
  217. fail_free_mmap:
  218. sys_table->boottime->free_pool(memory_map);
  219. fail_free_new_fdt:
  220. efi_free(sys_table, new_fdt_size, *new_fdt_addr);
  221. fail:
  222. return EFI_LOAD_ERROR;
  223. }
  224. void *get_fdt(efi_system_table_t *sys_table)
  225. {
  226. efi_guid_t fdt_guid = DEVICE_TREE_GUID;
  227. efi_config_table_t *tables;
  228. void *fdt;
  229. int i;
  230. tables = (efi_config_table_t *) sys_table->tables;
  231. fdt = NULL;
  232. for (i = 0; i < sys_table->nr_tables; i++)
  233. if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
  234. fdt = (void *) tables[i].table;
  235. break;
  236. }
  237. return fdt;
  238. }