efi: Add system table pointer argument to shared functions.
[firefly-linux-kernel-4.4.55.git] / drivers / firmware / efi / efi-stub-helper.c
1 /*
2  * 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 2011 Intel Corporation; author Matt Fleming
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 #define EFI_READ_CHUNK_SIZE     (1024 * 1024)
13
14 struct initrd {
15         efi_file_handle_t *handle;
16         u64 size;
17 };
18
19
20
21
22 static void efi_char16_printk(efi_system_table_t *sys_table_arg,
23                               efi_char16_t *str)
24 {
25         struct efi_simple_text_output_protocol *out;
26
27         out = (struct efi_simple_text_output_protocol *)sys_table_arg->con_out;
28         efi_call_phys2(out->output_string, out, str);
29 }
30
31 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
32 {
33         char *s8;
34
35         for (s8 = str; *s8; s8++) {
36                 efi_char16_t ch[2] = { 0 };
37
38                 ch[0] = *s8;
39                 if (*s8 == '\n') {
40                         efi_char16_t nl[2] = { '\r', 0 };
41                         efi_char16_printk(sys_table_arg, nl);
42                 }
43
44                 efi_char16_printk(sys_table_arg, ch);
45         }
46 }
47
48
49 static efi_status_t __get_map(efi_system_table_t *sys_table_arg,
50                               efi_memory_desc_t **map,
51                               unsigned long *map_size,
52                               unsigned long *desc_size)
53 {
54         efi_memory_desc_t *m = NULL;
55         efi_status_t status;
56         unsigned long key;
57         u32 desc_version;
58
59         *map_size = sizeof(*m) * 32;
60 again:
61         /*
62          * Add an additional efi_memory_desc_t because we're doing an
63          * allocation which may be in a new descriptor region.
64          */
65         *map_size += sizeof(*m);
66         status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
67                                 EFI_LOADER_DATA, *map_size, (void **)&m);
68         if (status != EFI_SUCCESS)
69                 goto fail;
70
71         status = efi_call_phys5(sys_table_arg->boottime->get_memory_map,
72                                 map_size, m, &key, desc_size, &desc_version);
73         if (status == EFI_BUFFER_TOO_SMALL) {
74                 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
75                 goto again;
76         }
77
78         if (status != EFI_SUCCESS)
79                 efi_call_phys1(sys_table_arg->boottime->free_pool, m);
80
81 fail:
82         *map = m;
83         return status;
84 }
85
86 /*
87  * Allocate at the highest possible address that is not above 'max'.
88  */
89 static efi_status_t high_alloc(efi_system_table_t *sys_table_arg,
90                                unsigned long size, unsigned long align,
91                                unsigned long *addr, unsigned long max)
92 {
93         unsigned long map_size, desc_size;
94         efi_memory_desc_t *map;
95         efi_status_t status;
96         unsigned long nr_pages;
97         u64 max_addr = 0;
98         int i;
99
100         status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
101         if (status != EFI_SUCCESS)
102                 goto fail;
103
104         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
105 again:
106         for (i = 0; i < map_size / desc_size; i++) {
107                 efi_memory_desc_t *desc;
108                 unsigned long m = (unsigned long)map;
109                 u64 start, end;
110
111                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
112                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
113                         continue;
114
115                 if (desc->num_pages < nr_pages)
116                         continue;
117
118                 start = desc->phys_addr;
119                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
120
121                 if ((start + size) > end || (start + size) > max)
122                         continue;
123
124                 if (end - size > max)
125                         end = max;
126
127                 if (round_down(end - size, align) < start)
128                         continue;
129
130                 start = round_down(end - size, align);
131
132                 /*
133                  * Don't allocate at 0x0. It will confuse code that
134                  * checks pointers against NULL.
135                  */
136                 if (start == 0x0)
137                         continue;
138
139                 if (start > max_addr)
140                         max_addr = start;
141         }
142
143         if (!max_addr)
144                 status = EFI_NOT_FOUND;
145         else {
146                 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
147                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
148                                         nr_pages, &max_addr);
149                 if (status != EFI_SUCCESS) {
150                         max = max_addr;
151                         max_addr = 0;
152                         goto again;
153                 }
154
155                 *addr = max_addr;
156         }
157
158 free_pool:
159         efi_call_phys1(sys_table_arg->boottime->free_pool, map);
160
161 fail:
162         return status;
163 }
164
165 /*
166  * Allocate at the lowest possible address.
167  */
168 static efi_status_t low_alloc(efi_system_table_t *sys_table_arg,
169                 unsigned long size, unsigned long align,
170                               unsigned long *addr)
171 {
172         unsigned long map_size, desc_size;
173         efi_memory_desc_t *map;
174         efi_status_t status;
175         unsigned long nr_pages;
176         int i;
177
178         status = __get_map(sys_table_arg, &map, &map_size, &desc_size);
179         if (status != EFI_SUCCESS)
180                 goto fail;
181
182         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
183         for (i = 0; i < map_size / desc_size; i++) {
184                 efi_memory_desc_t *desc;
185                 unsigned long m = (unsigned long)map;
186                 u64 start, end;
187
188                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
189
190                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
191                         continue;
192
193                 if (desc->num_pages < nr_pages)
194                         continue;
195
196                 start = desc->phys_addr;
197                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
198
199                 /*
200                  * Don't allocate at 0x0. It will confuse code that
201                  * checks pointers against NULL. Skip the first 8
202                  * bytes so we start at a nice even number.
203                  */
204                 if (start == 0x0)
205                         start += 8;
206
207                 start = round_up(start, align);
208                 if ((start + size) > end)
209                         continue;
210
211                 status = efi_call_phys4(sys_table_arg->boottime->allocate_pages,
212                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
213                                         nr_pages, &start);
214                 if (status == EFI_SUCCESS) {
215                         *addr = start;
216                         break;
217                 }
218         }
219
220         if (i == map_size / desc_size)
221                 status = EFI_NOT_FOUND;
222
223 free_pool:
224         efi_call_phys1(sys_table_arg->boottime->free_pool, map);
225 fail:
226         return status;
227 }
228
229 static void low_free(efi_system_table_t *sys_table_arg, unsigned long size,
230                      unsigned long addr)
231 {
232         unsigned long nr_pages;
233
234         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
235         efi_call_phys2(sys_table_arg->boottime->free_pages, addr, nr_pages);
236 }
237
238
239 /*
240  * Check the cmdline for a LILO-style initrd= arguments.
241  *
242  * We only support loading an initrd from the same filesystem as the
243  * kernel image.
244  */
245 static efi_status_t handle_ramdisks(efi_system_table_t *sys_table_arg,
246                                     efi_loaded_image_t *image,
247                                     struct setup_header *hdr)
248 {
249         struct initrd *initrds;
250         unsigned long initrd_addr;
251         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
252         u64 initrd_total;
253         efi_file_io_interface_t *io;
254         efi_file_handle_t *fh;
255         efi_status_t status;
256         int nr_initrds;
257         char *str;
258         int i, j, k;
259
260         initrd_addr = 0;
261         initrd_total = 0;
262
263         str = (char *)(unsigned long)hdr->cmd_line_ptr;
264
265         j = 0;                  /* See close_handles */
266
267         if (!str || !*str)
268                 return EFI_SUCCESS;
269
270         for (nr_initrds = 0; *str; nr_initrds++) {
271                 str = strstr(str, "initrd=");
272                 if (!str)
273                         break;
274
275                 str += 7;
276
277                 /* Skip any leading slashes */
278                 while (*str == '/' || *str == '\\')
279                         str++;
280
281                 while (*str && *str != ' ' && *str != '\n')
282                         str++;
283         }
284
285         if (!nr_initrds)
286                 return EFI_SUCCESS;
287
288         status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
289                                 EFI_LOADER_DATA,
290                                 nr_initrds * sizeof(*initrds),
291                                 &initrds);
292         if (status != EFI_SUCCESS) {
293                 efi_printk(sys_table_arg, "Failed to alloc mem for initrds\n");
294                 goto fail;
295         }
296
297         str = (char *)(unsigned long)hdr->cmd_line_ptr;
298         for (i = 0; i < nr_initrds; i++) {
299                 struct initrd *initrd;
300                 efi_file_handle_t *h;
301                 efi_file_info_t *info;
302                 efi_char16_t filename_16[256];
303                 unsigned long info_sz;
304                 efi_guid_t info_guid = EFI_FILE_INFO_ID;
305                 efi_char16_t *p;
306                 u64 file_sz;
307
308                 str = strstr(str, "initrd=");
309                 if (!str)
310                         break;
311
312                 str += 7;
313
314                 initrd = &initrds[i];
315                 p = filename_16;
316
317                 /* Skip any leading slashes */
318                 while (*str == '/' || *str == '\\')
319                         str++;
320
321                 while (*str && *str != ' ' && *str != '\n') {
322                         if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
323                                 break;
324
325                         if (*str == '/') {
326                                 *p++ = '\\';
327                                 *str++;
328                         } else {
329                                 *p++ = *str++;
330                         }
331                 }
332
333                 *p = '\0';
334
335                 /* Only open the volume once. */
336                 if (!i) {
337                         efi_boot_services_t *boottime;
338
339                         boottime = sys_table_arg->boottime;
340
341                         status = efi_call_phys3(boottime->handle_protocol,
342                                         image->device_handle, &fs_proto, &io);
343                         if (status != EFI_SUCCESS) {
344                                 efi_printk(sys_table_arg, "Failed to handle fs_proto\n");
345                                 goto free_initrds;
346                         }
347
348                         status = efi_call_phys2(io->open_volume, io, &fh);
349                         if (status != EFI_SUCCESS) {
350                                 efi_printk(sys_table_arg, "Failed to open volume\n");
351                                 goto free_initrds;
352                         }
353                 }
354
355                 status = efi_call_phys5(fh->open, fh, &h, filename_16,
356                                         EFI_FILE_MODE_READ, (u64)0);
357                 if (status != EFI_SUCCESS) {
358                         efi_printk(sys_table_arg, "Failed to open initrd file: ");
359                         efi_char16_printk(sys_table_arg, filename_16);
360                         efi_printk(sys_table_arg, "\n");
361                         goto close_handles;
362                 }
363
364                 initrd->handle = h;
365
366                 info_sz = 0;
367                 status = efi_call_phys4(h->get_info, h, &info_guid,
368                                         &info_sz, NULL);
369                 if (status != EFI_BUFFER_TOO_SMALL) {
370                         efi_printk(sys_table_arg, "Failed to get initrd info size\n");
371                         goto close_handles;
372                 }
373
374 grow:
375                 status = efi_call_phys3(sys_table_arg->boottime->allocate_pool,
376                                         EFI_LOADER_DATA, info_sz, &info);
377                 if (status != EFI_SUCCESS) {
378                         efi_printk(sys_table_arg, "Failed to alloc mem for initrd info\n");
379                         goto close_handles;
380                 }
381
382                 status = efi_call_phys4(h->get_info, h, &info_guid,
383                                         &info_sz, info);
384                 if (status == EFI_BUFFER_TOO_SMALL) {
385                         efi_call_phys1(sys_table_arg->boottime->free_pool,
386                                        info);
387                         goto grow;
388                 }
389
390                 file_sz = info->file_size;
391                 efi_call_phys1(sys_table_arg->boottime->free_pool, info);
392
393                 if (status != EFI_SUCCESS) {
394                         efi_printk(sys_table_arg, "Failed to get initrd info\n");
395                         goto close_handles;
396                 }
397
398                 initrd->size = file_sz;
399                 initrd_total += file_sz;
400         }
401
402         if (initrd_total) {
403                 unsigned long addr;
404
405                 /*
406                  * Multiple initrd's need to be at consecutive
407                  * addresses in memory, so allocate enough memory for
408                  * all the initrd's.
409                  */
410                 status = high_alloc(sys_table_arg, initrd_total, 0x1000,
411                                    &initrd_addr, hdr->initrd_addr_max);
412                 if (status != EFI_SUCCESS) {
413                         efi_printk(sys_table_arg, "Failed to alloc highmem for initrds\n");
414                         goto close_handles;
415                 }
416
417                 /* We've run out of free low memory. */
418                 if (initrd_addr > hdr->initrd_addr_max) {
419                         efi_printk(sys_table_arg, "We've run out of free low memory\n");
420                         status = EFI_INVALID_PARAMETER;
421                         goto free_initrd_total;
422                 }
423
424                 addr = initrd_addr;
425                 for (j = 0; j < nr_initrds; j++) {
426                         u64 size;
427
428                         size = initrds[j].size;
429                         while (size) {
430                                 u64 chunksize;
431                                 if (size > EFI_READ_CHUNK_SIZE)
432                                         chunksize = EFI_READ_CHUNK_SIZE;
433                                 else
434                                         chunksize = size;
435                                 status = efi_call_phys3(fh->read,
436                                                         initrds[j].handle,
437                                                         &chunksize, addr);
438                                 if (status != EFI_SUCCESS) {
439                                         efi_printk(sys_table_arg, "Failed to read initrd\n");
440                                         goto free_initrd_total;
441                                 }
442                                 addr += chunksize;
443                                 size -= chunksize;
444                         }
445
446                         efi_call_phys1(fh->close, initrds[j].handle);
447                 }
448
449         }
450
451         efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
452
453         hdr->ramdisk_image = initrd_addr;
454         hdr->ramdisk_size = initrd_total;
455
456         return status;
457
458 free_initrd_total:
459         low_free(sys_table_arg, initrd_total, initrd_addr);
460
461 close_handles:
462         for (k = j; k < i; k++)
463                 efi_call_phys1(fh->close, initrds[k].handle);
464 free_initrds:
465         efi_call_phys1(sys_table_arg->boottime->free_pool, initrds);
466 fail:
467         hdr->ramdisk_image = 0;
468         hdr->ramdisk_size = 0;
469
470         return status;
471 }