Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[linux-2.6-block.git] / drivers / firmware / efi / libstub / fdt.c
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 #include "efistub.h"
18
19 static 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)
23 {
24         int node, num_rsv;
25         int status;
26         u32 fdt_val32;
27         u64 fdt_val64;
28
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
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
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
101         fdt_val64 = U64_MAX; /* placeholder */
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
107         fdt_val32 = U32_MAX; /* placeholder */
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
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
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
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         }
137         return EFI_SUCCESS;
138
139 fdt_set_fail:
140         if (status == -FDT_ERR_NOSPACE)
141                 return EFI_BUFFER_TOO_SMALL;
142
143         return EFI_LOAD_ERROR;
144 }
145
146 static 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
183 #ifndef EFI_FDT_ALIGN
184 #define EFI_FDT_ALIGN EFI_PAGE_SIZE
185 #endif
186
187 struct exit_boot_struct {
188         efi_memory_desc_t *runtime_map;
189         int *runtime_entry_count;
190 };
191
192 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
193                                    struct efi_boot_memmap *map,
194                                    void *priv)
195 {
196         struct exit_boot_struct *p = priv;
197         /*
198          * Update the memory map with virtual addresses. The function will also
199          * populate @runtime_map with copies of just the EFI_MEMORY_RUNTIME
200          * entries so that we can pass it straight to SetVirtualAddressMap()
201          */
202         efi_get_virtmap(*map->map, *map->map_size, *map->desc_size,
203                         p->runtime_map, p->runtime_entry_count);
204
205         return EFI_SUCCESS;
206 }
207
208 /*
209  * Allocate memory for a new FDT, then add EFI, commandline, and
210  * initrd related fields to the FDT.  This routine increases the
211  * FDT allocation size until the allocated memory is large
212  * enough.  EFI allocations are in EFI_PAGE_SIZE granules,
213  * which are fixed at 4K bytes, so in most cases the first
214  * allocation should succeed.
215  * EFI boot services are exited at the end of this function.
216  * There must be no allocations between the get_memory_map()
217  * call and the exit_boot_services() call, so the exiting of
218  * boot services is very tightly tied to the creation of the FDT
219  * with the final memory map in it.
220  */
221
222 efi_status_t allocate_new_fdt_and_exit_boot(efi_system_table_t *sys_table,
223                                             void *handle,
224                                             unsigned long *new_fdt_addr,
225                                             unsigned long max_addr,
226                                             u64 initrd_addr, u64 initrd_size,
227                                             char *cmdline_ptr,
228                                             unsigned long fdt_addr,
229                                             unsigned long fdt_size)
230 {
231         unsigned long map_size, desc_size, buff_size;
232         u32 desc_ver;
233         unsigned long mmap_key;
234         efi_memory_desc_t *memory_map, *runtime_map;
235         unsigned long new_fdt_size;
236         efi_status_t status;
237         int runtime_entry_count = 0;
238         struct efi_boot_memmap map;
239         struct exit_boot_struct priv;
240
241         map.map =       &runtime_map;
242         map.map_size =  &map_size;
243         map.desc_size = &desc_size;
244         map.desc_ver =  &desc_ver;
245         map.key_ptr =   &mmap_key;
246         map.buff_size = &buff_size;
247
248         /*
249          * Get a copy of the current memory map that we will use to prepare
250          * the input for SetVirtualAddressMap(). We don't have to worry about
251          * subsequent allocations adding entries, since they could not affect
252          * the number of EFI_MEMORY_RUNTIME regions.
253          */
254         status = efi_get_memory_map(sys_table, &map);
255         if (status != EFI_SUCCESS) {
256                 pr_efi_err(sys_table, "Unable to retrieve UEFI memory map.\n");
257                 return status;
258         }
259
260         pr_efi(sys_table,
261                "Exiting boot services and installing virtual address map...\n");
262
263         map.map = &memory_map;
264         /*
265          * Estimate size of new FDT, and allocate memory for it. We
266          * will allocate a bigger buffer if this ends up being too
267          * small, so a rough guess is OK here.
268          */
269         new_fdt_size = fdt_size + EFI_PAGE_SIZE;
270         while (1) {
271                 status = efi_high_alloc(sys_table, new_fdt_size, EFI_FDT_ALIGN,
272                                         new_fdt_addr, max_addr);
273                 if (status != EFI_SUCCESS) {
274                         pr_efi_err(sys_table, "Unable to allocate memory for new device tree.\n");
275                         goto fail;
276                 }
277
278                 status = update_fdt(sys_table,
279                                     (void *)fdt_addr, fdt_size,
280                                     (void *)*new_fdt_addr, new_fdt_size,
281                                     cmdline_ptr, initrd_addr, initrd_size);
282
283                 /* Succeeding the first time is the expected case. */
284                 if (status == EFI_SUCCESS)
285                         break;
286
287                 if (status == EFI_BUFFER_TOO_SMALL) {
288                         /*
289                          * We need to allocate more space for the new
290                          * device tree, so free existing buffer that is
291                          * too small.
292                          */
293                         efi_free(sys_table, new_fdt_size, *new_fdt_addr);
294                         new_fdt_size += EFI_PAGE_SIZE;
295                 } else {
296                         pr_efi_err(sys_table, "Unable to construct new device tree.\n");
297                         goto fail_free_new_fdt;
298                 }
299         }
300
301         priv.runtime_map = runtime_map;
302         priv.runtime_entry_count = &runtime_entry_count;
303         status = efi_exit_boot_services(sys_table, handle, &map, &priv,
304                                         exit_boot_func);
305
306         if (status == EFI_SUCCESS) {
307                 efi_set_virtual_address_map_t *svam;
308
309                 status = update_fdt_memmap((void *)*new_fdt_addr, &map);
310                 if (status != EFI_SUCCESS) {
311                         /*
312                          * The kernel won't get far without the memory map, but
313                          * may still be able to print something meaningful so
314                          * return success here.
315                          */
316                         return EFI_SUCCESS;
317                 }
318
319                 /* Install the new virtual address map */
320                 svam = sys_table->runtime->set_virtual_address_map;
321                 status = svam(runtime_entry_count * desc_size, desc_size,
322                               desc_ver, runtime_map);
323
324                 /*
325                  * We are beyond the point of no return here, so if the call to
326                  * SetVirtualAddressMap() failed, we need to signal that to the
327                  * incoming kernel but proceed normally otherwise.
328                  */
329                 if (status != EFI_SUCCESS) {
330                         int l;
331
332                         /*
333                          * Set the virtual address field of all
334                          * EFI_MEMORY_RUNTIME entries to 0. This will signal
335                          * the incoming kernel that no virtual translation has
336                          * been installed.
337                          */
338                         for (l = 0; l < map_size; l += desc_size) {
339                                 efi_memory_desc_t *p = (void *)memory_map + l;
340
341                                 if (p->attribute & EFI_MEMORY_RUNTIME)
342                                         p->virt_addr = 0;
343                         }
344                 }
345                 return EFI_SUCCESS;
346         }
347
348         pr_efi_err(sys_table, "Exit boot services failed.\n");
349
350 fail_free_new_fdt:
351         efi_free(sys_table, new_fdt_size, *new_fdt_addr);
352
353 fail:
354         sys_table->boottime->free_pool(runtime_map);
355         return EFI_LOAD_ERROR;
356 }
357
358 void *get_fdt(efi_system_table_t *sys_table, unsigned long *fdt_size)
359 {
360         efi_guid_t fdt_guid = DEVICE_TREE_GUID;
361         efi_config_table_t *tables;
362         void *fdt;
363         int i;
364
365         tables = (efi_config_table_t *) sys_table->tables;
366         fdt = NULL;
367
368         for (i = 0; i < sys_table->nr_tables; i++)
369                 if (efi_guidcmp(tables[i].guid, fdt_guid) == 0) {
370                         fdt = (void *) tables[i].table;
371                         if (fdt_check_header(fdt) != 0) {
372                                 pr_efi_err(sys_table, "Invalid header detected on UEFI supplied FDT, ignoring ...\n");
373                                 return NULL;
374                         }
375                         *fdt_size = fdt_totalsize(fdt);
376                         break;
377          }
378
379         return fdt;
380 }