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