fdt.c 7.4 KB

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