1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/efi.h>
14#include <linux/libfdt.h>
15#include <asm/efi.h>
16
17#include "efistub.h"
18
19efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt,
20 unsigned long orig_fdt_size,
21 void *fdt, int new_fdt_size, char *cmdline_ptr,
22 u64 initrd_addr, u64 initrd_size,
23 efi_memory_desc_t *memory_map,
24 unsigned long map_size, unsigned long desc_size,
25 u32 desc_ver)
26{
27 int node, prev, num_rsv;
28 int status;
29 u32 fdt_val32;
30 u64 fdt_val64;
31
32
33 if (orig_fdt) {
34 if (fdt_check_header(orig_fdt)) {
35 pr_efi_err(sys_table, "Device Tree header not valid!\n");
36 return EFI_LOAD_ERROR;
37 }
38
39
40
41
42 if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) {
43 pr_efi_err(sys_table, "Truncated device tree! foo!\n");
44 return EFI_LOAD_ERROR;
45 }
46 }
47
48 if (orig_fdt)
49 status = fdt_open_into(orig_fdt, fdt, new_fdt_size);
50 else
51 status = fdt_create_empty_tree(fdt, new_fdt_size);
52
53 if (status != 0)
54 goto fdt_set_fail;
55
56
57
58
59
60 prev = 0;
61 for (;;) {
62 const char *type;
63 int len;
64
65 node = fdt_next_node(fdt, prev, NULL);
66 if (node < 0)
67 break;
68
69 type = fdt_getprop(fdt, node, "device_type", &len);
70 if (type && strncmp(type, "memory", len) == 0) {
71 fdt_del_node(fdt, node);
72 continue;
73 }
74
75 prev = node;
76 }
77
78
79
80
81
82 num_rsv = fdt_num_mem_rsv(fdt);
83 while (num_rsv-- > 0)
84 fdt_del_mem_rsv(fdt, num_rsv);
85
86 node = fdt_subnode_offset(fdt, 0, "chosen");
87 if (node < 0) {
88 node = fdt_add_subnode(fdt, 0, "chosen");
89 if (node < 0) {
90 status = node;
91 goto fdt_set_fail;
92 }
93 }
94
95 if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) {
96 status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr,
97 strlen(cmdline_ptr) + 1);
98 if (status)
99 goto fdt_set_fail;
100 }
101
102
103 if (initrd_size != 0) {
104 u64 initrd_image_end;
105 u64 initrd_image_start = cpu_to_fdt64(initrd_addr);
106
107 status = fdt_setprop(fdt, node, "linux,initrd-start",
108 &initrd_image_start, sizeof(u64));
109 if (status)
110 goto fdt_set_fail;
111 initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size);
112 status = fdt_setprop(fdt, node, "linux,initrd-end",
113 &initrd_image_end, sizeof(u64));
114 if (status)
115 goto fdt_set_fail;
116 }
117
118
119 node = fdt_subnode_offset(fdt, 0, "chosen");
120 fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table);
121 status = fdt_setprop(fdt, node, "linux,uefi-system-table",
122 &fdt_val64, sizeof(fdt_val64));
123 if (status)
124 goto fdt_set_fail;
125
126 fdt_val64 = cpu_to_fdt64((u64)(unsigned long)memory_map);
127 status = fdt_setprop(fdt, node, "linux,uefi-mmap-start",
128 &fdt_val64, sizeof(fdt_val64));
129 if (status)
130 goto fdt_set_fail;
131
132 fdt_val32 = cpu_to_fdt32(map_size);
133 status = fdt_setprop(fdt, node, "linux,uefi-mmap-size",
134 &fdt_val32, sizeof(fdt_val32));
135 if (status)
136 goto fdt_set_fail;
137
138 fdt_val32 = cpu_to_fdt32(desc_size);
139 status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-size",
140 &fdt_val32, sizeof(fdt_val32));
141 if (status)
142 goto fdt_set_fail;
143
144 fdt_val32 = cpu_to_fdt32(desc_ver);
145 status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver",
146 &fdt_val32, sizeof(fdt_val32));
147 if (status)
148 goto fdt_set_fail;
149
150
151
152
153
154 status = fdt_setprop_string(fdt, node, "linux,uefi-stub-kern-ver",
155 linux_banner);
156 if (status)
157 goto fdt_set_fail;
158
159 return EFI_SUCCESS;
160
161fdt_set_fail:
162 if (status == -FDT_ERR_NOSPACE)
163 return EFI_BUFFER_TOO_SMALL;
164
165 return EFI_LOAD_ERROR;
166}
167
168#ifndef EFI_FDT_ALIGN
169#define EFI_FDT_ALIGN EFI_PAGE_SIZE
170#endif
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
187 void *handle,
188 unsigned long *new_fdt_addr,
189 unsigned long max_addr,
190 u64 initrd_addr, u64 initrd_size,
191 char *cmdline_ptr,
192 unsigned long fdt_addr,
193 unsigned long fdt_size)
194{
195 unsigned long map_size, desc_size;
196 u32 desc_ver;
197 unsigned long mmap_key;
198 efi_memory_desc_t *memory_map, *runtime_map;
199 unsigned long new_fdt_size;
200 efi_status_t status;
201 int runtime_entry_count = 0;
202
203
204
205
206
207
208
209 status = efi_get_memory_map(sys_table, &runtime_map, &map_size,
210 &desc_size, &desc_ver, &mmap_key);
211 if (status != EFI_SUCCESS) {
212 pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
213 return status;
214 }
215
216 pr_efi(sys_table,
217 "Exiting boot services and installing virtual address map...\n");
218
219
220
221
222
223
224 new_fdt_size = fdt_size + EFI_PAGE_SIZE;
225 while (1) {
226 status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
227 new_fdt_addr, max_addr);
228 if (status != EFI_SUCCESS) {
229 pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
230 goto fail;
231 }
232
233
234
235
236
237
238 status = efi_get_memory_map(sys_table, &memory_map, &map_size,
239 &desc_size, &desc_ver, &mmap_key);
240 if (status != EFI_SUCCESS)
241 goto fail_free_new_fdt;
242
243 status = update_fdt(sys_table,
244 (void *)fdt_addr, fdt_size,
245 (void *)*new_fdt_addr, new_fdt_size,
246 cmdline_ptr, initrd_addr, initrd_size,
247 memory_map, map_size, desc_size, desc_ver);
248
249
250 if (status == EFI_SUCCESS)
251 break;
252
253 if (status == EFI_BUFFER_TOO_SMALL) {
254
255
256
257
258
259
260
261 efi_free(sys_table, new_fdt_size, *new_fdt_addr);
262 sys_table->boottime->free_pool(memory_map);
263 new_fdt_size += EFI_PAGE_SIZE;
264 } else {
265 pr_efi_err(sys_table, "Unable to constuct new device tree.\n");
266 goto fail_free_mmap;
267 }
268 }
269
270
271
272
273
274
275 efi_get_virtmap(memory_map, map_size, desc_size, runtime_map,
276 &runtime_entry_count);
277
278
279 status = sys_table->boottime->exit_boot_services(handle, mmap_key);
280
281 if (status == EFI_SUCCESS) {
282 efi_set_virtual_address_map_t *svam;
283
284
285 svam = sys_table->runtime->set_virtual_address_map;
286 status = svam(runtime_entry_count * desc_size, desc_size,
287 desc_ver, runtime_map);
288
289
290
291
292
293
294 if (status != EFI_SUCCESS) {
295 int l;
296
297
298
299
300
301
302
303 for (l = 0; l < map_size; l += desc_size) {
304 efi_memory_desc_t *p = (void *)memory_map + l;
305
306 if (p->attribute & EFI_MEMORY_RUNTIME)
307 p->virt_addr = 0;
308 }
309 }
310 return EFI_SUCCESS;
311 }
312
313 pr_efi_err(sys_table, "Exit boot services failed.\n");
314
315fail_free_mmap:
316 sys_table->boottime->free_pool(memory_map);
317
318fail_free_new_fdt:
319 efi_free(sys_table, new_fdt_size, *new_fdt_addr);
320
321fail:
322 sys_table->boottime->free_pool(runtime_map);
323 return EFI_LOAD_ERROR;
324}
325
326void *get_fdt(efi_system_table_t *sys_table)
327{
328 efi_guid_t fdt_guid = DEVICE_TREE_GUID;
329 efi_config_table_t *tables;
330 void *fdt;
331 int i;
332
333 tables = (efi_config_table_t *) sys_table->tables;
334 fdt = NULL;
335
336 for (i = 0; i < sys_table->nr_tables; i++)
337 if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
338 fdt = (void *) tables[i].table;
339 break;
340 }
341
342 return fdt;
343}
344