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