]>
Commit | Line | Data |
---|---|---|
263b4a30 RF |
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 | ||
bd669475 AB |
13 | #include <linux/efi.h> |
14 | #include <linux/libfdt.h> | |
15 | #include <asm/efi.h> | |
16 | ||
f3cdfd23 AB |
17 | #include "efistub.h" |
18 | ||
ae8a442d SG |
19 | #define EFI_DT_ADDR_CELLS_DEFAULT 2 |
20 | #define EFI_DT_SIZE_CELLS_DEFAULT 2 | |
21 | ||
22 | static void fdt_update_cell_size(efi_system_table_t *sys_table, void *fdt) | |
23 | { | |
24 | int offset; | |
25 | ||
26 | offset = fdt_path_offset(fdt, "/"); | |
27 | /* Set the #address-cells and #size-cells values for an empty tree */ | |
28 | ||
29 | fdt_setprop_u32(fdt, offset, "#address-cells", | |
30 | EFI_DT_ADDR_CELLS_DEFAULT); | |
31 | ||
32 | fdt_setprop_u32(fdt, offset, "#size-cells", EFI_DT_SIZE_CELLS_DEFAULT); | |
33 | } | |
34 | ||
abfb7b68 AB |
35 | static efi_status_t update_fdt(efi_system_table_t *sys_table, void *orig_fdt, |
36 | unsigned long orig_fdt_size, | |
37 | void *fdt, int new_fdt_size, char *cmdline_ptr, | |
38 | u64 initrd_addr, u64 initrd_size) | |
263b4a30 | 39 | { |
500899c2 | 40 | int node, num_rsv; |
263b4a30 RF |
41 | int status; |
42 | u32 fdt_val32; | |
43 | u64 fdt_val64; | |
44 | ||
263b4a30 RF |
45 | /* Do some checks on provided FDT, if it exists*/ |
46 | if (orig_fdt) { | |
47 | if (fdt_check_header(orig_fdt)) { | |
48 | pr_efi_err(sys_table, "Device Tree header not valid!\n"); | |
49 | return EFI_LOAD_ERROR; | |
50 | } | |
51 | /* | |
52 | * We don't get the size of the FDT if we get if from a | |
53 | * configuration table. | |
54 | */ | |
55 | if (orig_fdt_size && fdt_totalsize(orig_fdt) > orig_fdt_size) { | |
56 | pr_efi_err(sys_table, "Truncated device tree! foo!\n"); | |
57 | return EFI_LOAD_ERROR; | |
58 | } | |
59 | } | |
60 | ||
ae8a442d | 61 | if (orig_fdt) { |
263b4a30 | 62 | status = fdt_open_into(orig_fdt, fdt, new_fdt_size); |
ae8a442d | 63 | } else { |
263b4a30 | 64 | status = fdt_create_empty_tree(fdt, new_fdt_size); |
ae8a442d SG |
65 | if (status == 0) { |
66 | /* | |
67 | * Any failure from the following function is non | |
68 | * critical | |
69 | */ | |
70 | fdt_update_cell_size(sys_table, fdt); | |
71 | } | |
72 | } | |
263b4a30 RF |
73 | |
74 | if (status != 0) | |
75 | goto fdt_set_fail; | |
76 | ||
0ceac9e0 MS |
77 | /* |
78 | * Delete all memory reserve map entries. When booting via UEFI, | |
79 | * kernel will use the UEFI memory map to find reserved regions. | |
80 | */ | |
81 | num_rsv = fdt_num_mem_rsv(fdt); | |
82 | while (num_rsv-- > 0) | |
83 | fdt_del_mem_rsv(fdt, num_rsv); | |
84 | ||
263b4a30 RF |
85 | node = fdt_subnode_offset(fdt, 0, "chosen"); |
86 | if (node < 0) { | |
87 | node = fdt_add_subnode(fdt, 0, "chosen"); | |
88 | if (node < 0) { | |
89 | status = node; /* node is error code when negative */ | |
90 | goto fdt_set_fail; | |
91 | } | |
92 | } | |
93 | ||
94 | if ((cmdline_ptr != NULL) && (strlen(cmdline_ptr) > 0)) { | |
95 | status = fdt_setprop(fdt, node, "bootargs", cmdline_ptr, | |
96 | strlen(cmdline_ptr) + 1); | |
97 | if (status) | |
98 | goto fdt_set_fail; | |
99 | } | |
100 | ||
101 | /* Set initrd address/end in device tree, if present */ | |
102 | if (initrd_size != 0) { | |
103 | u64 initrd_image_end; | |
104 | u64 initrd_image_start = cpu_to_fdt64(initrd_addr); | |
105 | ||
106 | status = fdt_setprop(fdt, node, "linux,initrd-start", | |
107 | &initrd_image_start, sizeof(u64)); | |
108 | if (status) | |
109 | goto fdt_set_fail; | |
110 | initrd_image_end = cpu_to_fdt64(initrd_addr + initrd_size); | |
111 | status = fdt_setprop(fdt, node, "linux,initrd-end", | |
112 | &initrd_image_end, sizeof(u64)); | |
113 | if (status) | |
114 | goto fdt_set_fail; | |
115 | } | |
116 | ||
117 | /* Add FDT entries for EFI runtime services in chosen node. */ | |
118 | node = fdt_subnode_offset(fdt, 0, "chosen"); | |
119 | fdt_val64 = cpu_to_fdt64((u64)(unsigned long)sys_table); | |
120 | status = fdt_setprop(fdt, node, "linux,uefi-system-table", | |
121 | &fdt_val64, sizeof(fdt_val64)); | |
122 | if (status) | |
123 | goto fdt_set_fail; | |
124 | ||
abfb7b68 | 125 | fdt_val64 = U64_MAX; /* placeholder */ |
263b4a30 RF |
126 | status = fdt_setprop(fdt, node, "linux,uefi-mmap-start", |
127 | &fdt_val64, sizeof(fdt_val64)); | |
128 | if (status) | |
129 | goto fdt_set_fail; | |
130 | ||
abfb7b68 | 131 | fdt_val32 = U32_MAX; /* placeholder */ |
263b4a30 RF |
132 | status = fdt_setprop(fdt, node, "linux,uefi-mmap-size", |
133 | &fdt_val32, sizeof(fdt_val32)); | |
134 | if (status) | |
135 | goto fdt_set_fail; | |
136 | ||
263b4a30 RF |
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 | ||
263b4a30 RF |
142 | status = fdt_setprop(fdt, node, "linux,uefi-mmap-desc-ver", |
143 | &fdt_val32, sizeof(fdt_val32)); | |
144 | if (status) | |
145 | goto fdt_set_fail; | |
146 | ||
2b5fe07a AB |
147 | if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { |
148 | efi_status_t efi_status; | |
149 | ||
150 | efi_status = efi_get_random_bytes(sys_table, sizeof(fdt_val64), | |
151 | (u8 *)&fdt_val64); | |
152 | if (efi_status == EFI_SUCCESS) { | |
153 | status = fdt_setprop(fdt, node, "kaslr-seed", | |
154 | &fdt_val64, sizeof(fdt_val64)); | |
155 | if (status) | |
156 | goto fdt_set_fail; | |
157 | } else if (efi_status != EFI_NOT_FOUND) { | |
158 | return efi_status; | |
159 | } | |
160 | } | |
72a58a63 AB |
161 | |
162 | /* shrink the FDT back to its minimum size */ | |
163 | fdt_pack(fdt); | |
164 | ||
263b4a30 RF |
165 | return EFI_SUCCESS; |
166 | ||
167 | fdt_set_fail: | |
168 | if (status == -FDT_ERR_NOSPACE) | |
169 | return EFI_BUFFER_TOO_SMALL; | |
170 | ||
171 | return EFI_LOAD_ERROR; | |
172 | } | |
173 | ||
abfb7b68 AB |
174 | static efi_status_t update_fdt_memmap(void *fdt, struct efi_boot_memmap *map) |
175 | { | |
176 | int node = fdt_path_offset(fdt, "/chosen"); | |
177 | u64 fdt_val64; | |
178 | u32 fdt_val32; | |
179 | int err; | |
180 | ||
181 | if (node < 0) | |
182 | return EFI_LOAD_ERROR; | |
183 | ||
184 | fdt_val64 = cpu_to_fdt64((unsigned long)*map->map); | |
185 | err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-start", | |
186 | &fdt_val64, sizeof(fdt_val64)); | |
187 | if (err) | |
188 | return EFI_LOAD_ERROR; | |
189 | ||
190 | fdt_val32 = cpu_to_fdt32(*map->map_size); | |
191 | err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-size", | |
192 | &fdt_val32, sizeof(fdt_val32)); | |
193 | if (err) | |
194 | return EFI_LOAD_ERROR; | |
195 | ||
196 | fdt_val32 = cpu_to_fdt32(*map->desc_size); | |
197 | err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-size", | |
198 | &fdt_val32, sizeof(fdt_val32)); | |
199 | if (err) | |
200 | return EFI_LOAD_ERROR; | |
201 | ||
202 | fdt_val32 = cpu_to_fdt32(*map->desc_ver); | |
203 | err = fdt_setprop_inplace(fdt, node, "linux,uefi-mmap-desc-ver", | |
204 | &fdt_val32, sizeof(fdt_val32)); | |
205 | if (err) | |
206 | return EFI_LOAD_ERROR; | |
207 | ||
208 | return EFI_SUCCESS; | |
209 | } | |
210 | ||
263b4a30 RF |
211 | #ifndef EFI_FDT_ALIGN |
212 | #define EFI_FDT_ALIGN EFI_PAGE_SIZE | |
213 | #endif | |
214 | ||
ed9cc156 JH |
215 | struct exit_boot_struct { |
216 | efi_memory_desc_t *runtime_map; | |
217 | int *runtime_entry_count; | |
c8f325a5 | 218 | void *new_fdt_addr; |
ed9cc156 JH |
219 | }; |
220 | ||
221 | static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg, | |
222 | struct efi_boot_memmap *map, | |
223 | void *priv) | |
224 | { | |
225 | struct exit_boot_struct *p = priv; | |
226 | /* | |
227 | * Update the memory map with virtual addresses. The function will also | |
228 | * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME | |
229 | * entries so that we can pass it straight to SetVirtualAddressMap() | |
230 | */ | |
231 | efi_get_virtmap(*map->map, *map->map_size, *map->desc_size, | |
232 | p->runtime_map, p->runtime_entry_count); | |
233 | ||
c8f325a5 | 234 | return update_fdt_memmap(p->new_fdt_addr, map); |
ed9cc156 JH |
235 | } |
236 | ||
24d7c494 AB |
237 | #ifndef MAX_FDT_SIZE |
238 | #define MAX_FDT_SIZE SZ_2M | |
239 | #endif | |
240 | ||
263b4a30 RF |
241 | /* |
242 | * Allocate memory for a new FDT, then add EFI, commandline, and | |
243 | * initrd related fields to the FDT. This routine increases the | |
244 | * FDT allocation size until the allocated memory is large | |
245 | * enough. EFI allocations are in EFI_PAGE_SIZE granules, | |
246 | * which are fixed at 4K bytes, so in most cases the first | |
247 | * allocation should succeed. | |
248 | * EFI boot services are exited at the end of this function. | |
249 | * There must be no allocations between the get_memory_map() | |
250 | * call and the exit_boot_services() call, so the exiting of | |
251 | * boot services is very tightly tied to the creation of the FDT | |
252 | * with the final memory map in it. | |
253 | */ | |
254 | ||
255 | efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table, | |
256 | void *handle, | |
257 | unsigned long *new_fdt_addr, | |
258 | unsigned long max_addr, | |
259 | u64 initrd_addr, u64 initrd_size, | |
260 | char *cmdline_ptr, | |
261 | unsigned long fdt_addr, | |
262 | unsigned long fdt_size) | |
263 | { | |
dadb57ab | 264 | unsigned long map_size, desc_size, buff_size; |
263b4a30 RF |
265 | u32 desc_ver; |
266 | unsigned long mmap_key; | |
f3cdfd23 | 267 | efi_memory_desc_t *memory_map, *runtime_map; |
263b4a30 | 268 | efi_status_t status; |
f3cdfd23 | 269 | int runtime_entry_count = 0; |
dadb57ab | 270 | struct efi_boot_memmap map; |
ed9cc156 | 271 | struct exit_boot_struct priv; |
dadb57ab JH |
272 | |
273 | map.map = &runtime_map; | |
274 | map.map_size = &map_size; | |
275 | map.desc_size = &desc_size; | |
276 | map.desc_ver = &desc_ver; | |
277 | map.key_ptr = &mmap_key; | |
278 | map.buff_size = &buff_size; | |
f3cdfd23 AB |
279 | |
280 | /* | |
281 | * Get a copy of the current memory map that we will use to prepare | |
282 | * the input for SetVirtualAddressMap(). We don't have to worry about | |
283 | * subsequent allocations adding entries, since they could not affect | |
284 | * the number of EFI_MEMORY_RUNTIME regions. | |
285 | */ | |
dadb57ab | 286 | status = efi_get_memory_map(sys_table, &map); |
f3cdfd23 AB |
287 | if (status != EFI_SUCCESS) { |
288 | pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n"); | |
289 | return status; | |
290 | } | |
291 | ||
292 | pr_efi(sys_table, | |
293 | "Exiting boot services and installing virtual address map...\n"); | |
263b4a30 | 294 | |
dadb57ab | 295 | map.map = &memory_map; |
24d7c494 AB |
296 | status = efi_high_alloc(sys_table, MAX_FDT_SIZE, EFI_FDT_ALIGN, |
297 | new_fdt_addr, max_addr); | |
298 | if (status != EFI_SUCCESS) { | |
299 | pr_efi_err(sys_table, | |
300 | "Unable to allocate memory for new device tree.\n"); | |
301 | goto fail; | |
302 | } | |
303 | ||
263b4a30 | 304 | /* |
24d7c494 AB |
305 | * Now that we have done our final memory allocation (and free) |
306 | * we can get the memory map key needed for exit_boot_services(). | |
263b4a30 | 307 | */ |
24d7c494 AB |
308 | status = efi_get_memory_map(sys_table, &map); |
309 | if (status != EFI_SUCCESS) | |
310 | goto fail_free_new_fdt; | |
263b4a30 | 311 | |
24d7c494 AB |
312 | status = update_fdt(sys_table, (void *)fdt_addr, fdt_size, |
313 | (void *)*new_fdt_addr, MAX_FDT_SIZE, cmdline_ptr, | |
314 | initrd_addr, initrd_size); | |
263b4a30 | 315 | |
24d7c494 AB |
316 | if (status != EFI_SUCCESS) { |
317 | pr_efi_err(sys_table, "Unable to construct new device tree.\n"); | |
318 | goto fail_free_new_fdt; | |
263b4a30 RF |
319 | } |
320 | ||
ed9cc156 JH |
321 | priv.runtime_map = runtime_map; |
322 | priv.runtime_entry_count = &runtime_entry_count; | |
c8f325a5 | 323 | priv.new_fdt_addr = (void *)*new_fdt_addr; |
ed9cc156 JH |
324 | status = efi_exit_boot_services(sys_table, handle, &map, &priv, |
325 | exit_boot_func); | |
263b4a30 | 326 | |
f3cdfd23 AB |
327 | if (status == EFI_SUCCESS) { |
328 | efi_set_virtual_address_map_t *svam; | |
263b4a30 | 329 | |
f3cdfd23 AB |
330 | /* Install the new virtual address map */ |
331 | svam = sys_table->runtime->set_virtual_address_map; | |
332 | status = svam(runtime_entry_count * desc_size, desc_size, | |
333 | desc_ver, runtime_map); | |
334 | ||
335 | /* | |
336 | * We are beyond the point of no return here, so if the call to | |
337 | * SetVirtualAddressMap() failed, we need to signal that to the | |
338 | * incoming kernel but proceed normally otherwise. | |
339 | */ | |
340 | if (status != EFI_SUCCESS) { | |
341 | int l; | |
342 | ||
343 | /* | |
344 | * Set the virtual address field of all | |
345 | * EFI_MEMORY_RUNTIME entries to 0. This will signal | |
346 | * the incoming kernel that no virtual translation has | |
347 | * been installed. | |
348 | */ | |
349 | for (l = 0; l < map_size; l += desc_size) { | |
350 | efi_memory_desc_t *p = (void *)memory_map + l; | |
351 | ||
352 | if (p->attribute & EFI_MEMORY_RUNTIME) | |
353 | p->virt_addr = 0; | |
354 | } | |
355 | } | |
356 | return EFI_SUCCESS; | |
357 | } | |
263b4a30 RF |
358 | |
359 | pr_efi_err(sys_table, "Exit boot services failed.\n"); | |
360 | ||
263b4a30 | 361 | fail_free_new_fdt: |
24d7c494 | 362 | efi_free(sys_table, MAX_FDT_SIZE, *new_fdt_addr); |
263b4a30 RF |
363 | |
364 | fail: | |
f3cdfd23 | 365 | sys_table->boottime->free_pool(runtime_map); |
263b4a30 RF |
366 | return EFI_LOAD_ERROR; |
367 | } | |
368 | ||
a643375f | 369 | void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size) |
263b4a30 RF |
370 | { |
371 | efi_guid_t fdt_guid = DEVICE_TREE_GUID; | |
372 | efi_config_table_t *tables; | |
373 | void *fdt; | |
374 | int i; | |
375 | ||
376 | tables = (efi_config_table_t *) sys_table->tables; | |
377 | fdt = NULL; | |
378 | ||
379 | for (i = 0; i < sys_table->nr_tables; i++) | |
380 | if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) { | |
381 | fdt = (void *) tables[i].table; | |
a643375f AB |
382 | if (fdt_check_header(fdt) != 0) { |
383 | pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n"); | |
384 | return NULL; | |
385 | } | |
386 | *fdt_size = fdt_totalsize(fdt); | |
263b4a30 RF |
387 | break; |
388 | } | |
389 | ||
390 | return fdt; | |
391 | } |