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