a168dd20511f6c1dc9c45c0b4ae911c0e29dd8cf
[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 /* error code which can't be mistaken for valid address */
15 #define EFI_ERROR       (~0UL)
16
17
18 struct file_info {
19         efi_file_handle_t *handle;
20         u64 size;
21 };
22
23 static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
24 {
25         char *s8;
26
27         for (s8 = str; *s8; s8++) {
28                 efi_char16_t ch[2] = { 0 };
29
30                 ch[0] = *s8;
31                 if (*s8 == '\n') {
32                         efi_char16_t nl[2] = { '\r', 0 };
33                         efi_char16_printk(sys_table_arg, nl);
34                 }
35
36                 efi_char16_printk(sys_table_arg, ch);
37         }
38 }
39
40 #define pr_efi(sys_table, msg)     efi_printk(sys_table, "EFI stub: "msg)
41 #define pr_efi_err(sys_table, msg) efi_printk(sys_table, "EFI stub: ERROR: "msg)
42
43
44 static efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
45                                        efi_memory_desc_t **map,
46                                        unsigned long *map_size,
47                                        unsigned long *desc_size,
48                                        u32 *desc_ver,
49                                        unsigned long *key_ptr)
50 {
51         efi_memory_desc_t *m = NULL;
52         efi_status_t status;
53         unsigned long key;
54         u32 desc_version;
55
56         *map_size = sizeof(*m) * 32;
57 again:
58         /*
59          * Add an additional efi_memory_desc_t because we're doing an
60          * allocation which may be in a new descriptor region.
61          */
62         *map_size += sizeof(*m);
63         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
64                                 *map_size, (void **)&m);
65         if (status != EFI_SUCCESS)
66                 goto fail;
67
68         *desc_size = 0;
69         key = 0;
70         status = efi_call_early(get_memory_map, map_size, m,
71                                 &key, desc_size, &desc_version);
72         if (status == EFI_BUFFER_TOO_SMALL) {
73                 efi_call_early(free_pool, m);
74                 goto again;
75         }
76
77         if (status != EFI_SUCCESS)
78                 efi_call_early(free_pool, m);
79
80         if (key_ptr && status == EFI_SUCCESS)
81                 *key_ptr = key;
82         if (desc_ver && status == EFI_SUCCESS)
83                 *desc_ver = desc_version;
84
85 fail:
86         *map = m;
87         return status;
88 }
89
90
91 static unsigned long __init get_dram_base(efi_system_table_t *sys_table_arg)
92 {
93         efi_status_t status;
94         unsigned long map_size;
95         unsigned long membase  = EFI_ERROR;
96         struct efi_memory_map map;
97         efi_memory_desc_t *md;
98
99         status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
100                                     &map_size, &map.desc_size, NULL, NULL);
101         if (status != EFI_SUCCESS)
102                 return membase;
103
104         map.map_end = map.map + map_size;
105
106         for_each_efi_memory_desc(&map, md)
107                 if (md->attribute & EFI_MEMORY_WB)
108                         if (membase > md->phys_addr)
109                                 membase = md->phys_addr;
110
111         efi_call_early(free_pool, map.map);
112
113         return membase;
114 }
115
116 /*
117  * Allocate at the highest possible address that is not above 'max'.
118  */
119 static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
120                                unsigned long size, unsigned long align,
121                                unsigned long *addr, unsigned long max)
122 {
123         unsigned long map_size, desc_size;
124         efi_memory_desc_t *map;
125         efi_status_t status;
126         unsigned long nr_pages;
127         u64 max_addr = 0;
128         int i;
129
130         status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
131                                     NULL, NULL);
132         if (status != EFI_SUCCESS)
133                 goto fail;
134
135         /*
136          * Enforce minimum alignment that EFI requires when requesting
137          * a specific address.  We are doing page-based allocations,
138          * so we must be aligned to a page.
139          */
140         if (align < EFI_PAGE_SIZE)
141                 align = EFI_PAGE_SIZE;
142
143         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
144 again:
145         for (i = 0; i < map_size / desc_size; i++) {
146                 efi_memory_desc_t *desc;
147                 unsigned long m = (unsigned long)map;
148                 u64 start, end;
149
150                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
151                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
152                         continue;
153
154                 if (desc->num_pages < nr_pages)
155                         continue;
156
157                 start = desc->phys_addr;
158                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
159
160                 if ((start + size) > end || (start + size) > max)
161                         continue;
162
163                 if (end - size > max)
164                         end = max;
165
166                 if (round_down(end - size, align) < start)
167                         continue;
168
169                 start = round_down(end - size, align);
170
171                 /*
172                  * Don't allocate at 0x0. It will confuse code that
173                  * checks pointers against NULL.
174                  */
175                 if (start == 0x0)
176                         continue;
177
178                 if (start > max_addr)
179                         max_addr = start;
180         }
181
182         if (!max_addr)
183                 status = EFI_NOT_FOUND;
184         else {
185                 status = efi_call_early(allocate_pages,
186                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
187                                         nr_pages, &max_addr);
188                 if (status != EFI_SUCCESS) {
189                         max = max_addr;
190                         max_addr = 0;
191                         goto again;
192                 }
193
194                 *addr = max_addr;
195         }
196
197         efi_call_early(free_pool, map);
198 fail:
199         return status;
200 }
201
202 /*
203  * Allocate at the lowest possible address.
204  */
205 static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
206                               unsigned long size, unsigned long align,
207                               unsigned long *addr)
208 {
209         unsigned long map_size, desc_size;
210         efi_memory_desc_t *map;
211         efi_status_t status;
212         unsigned long nr_pages;
213         int i;
214
215         status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
216                                     NULL, NULL);
217         if (status != EFI_SUCCESS)
218                 goto fail;
219
220         /*
221          * Enforce minimum alignment that EFI requires when requesting
222          * a specific address.  We are doing page-based allocations,
223          * so we must be aligned to a page.
224          */
225         if (align < EFI_PAGE_SIZE)
226                 align = EFI_PAGE_SIZE;
227
228         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
229         for (i = 0; i < map_size / desc_size; i++) {
230                 efi_memory_desc_t *desc;
231                 unsigned long m = (unsigned long)map;
232                 u64 start, end;
233
234                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
235
236                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
237                         continue;
238
239                 if (desc->num_pages < nr_pages)
240                         continue;
241
242                 start = desc->phys_addr;
243                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
244
245                 /*
246                  * Don't allocate at 0x0. It will confuse code that
247                  * checks pointers against NULL. Skip the first 8
248                  * bytes so we start at a nice even number.
249                  */
250                 if (start == 0x0)
251                         start += 8;
252
253                 start = round_up(start, align);
254                 if ((start + size) > end)
255                         continue;
256
257                 status = efi_call_early(allocate_pages,
258                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
259                                         nr_pages, &start);
260                 if (status == EFI_SUCCESS) {
261                         *addr = start;
262                         break;
263                 }
264         }
265
266         if (i == map_size / desc_size)
267                 status = EFI_NOT_FOUND;
268
269         efi_call_early(free_pool, map);
270 fail:
271         return status;
272 }
273
274 static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
275                      unsigned long addr)
276 {
277         unsigned long nr_pages;
278
279         if (!size)
280                 return;
281
282         nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
283         efi_call_early(free_pages, addr, nr_pages);
284 }
285
286
287 /*
288  * Check the cmdline for a LILO-style file= arguments.
289  *
290  * We only support loading a file from the same filesystem as
291  * the kernel image.
292  */
293 static efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
294                                          efi_loaded_image_t *image,
295                                          char *cmd_line, char *option_string,
296                                          unsigned long max_addr,
297                                          unsigned long *load_addr,
298                                          unsigned long *load_size)
299 {
300         struct file_info *files;
301         unsigned long file_addr;
302         u64 file_size_total;
303         efi_file_handle_t *fh = NULL;
304         efi_status_t status;
305         int nr_files;
306         char *str;
307         int i, j, k;
308
309         file_addr = 0;
310         file_size_total = 0;
311
312         str = cmd_line;
313
314         j = 0;                  /* See close_handles */
315
316         if (!load_addr || !load_size)
317                 return EFI_INVALID_PARAMETER;
318
319         *load_addr = 0;
320         *load_size = 0;
321
322         if (!str || !*str)
323                 return EFI_SUCCESS;
324
325         for (nr_files = 0; *str; nr_files++) {
326                 str = strstr(str, option_string);
327                 if (!str)
328                         break;
329
330                 str += strlen(option_string);
331
332                 /* Skip any leading slashes */
333                 while (*str == '/' || *str == '\\')
334                         str++;
335
336                 while (*str && *str != ' ' && *str != '\n')
337                         str++;
338         }
339
340         if (!nr_files)
341                 return EFI_SUCCESS;
342
343         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
344                                 nr_files * sizeof(*files), (void **)&files);
345         if (status != EFI_SUCCESS) {
346                 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
347                 goto fail;
348         }
349
350         str = cmd_line;
351         for (i = 0; i < nr_files; i++) {
352                 struct file_info *file;
353                 efi_char16_t filename_16[256];
354                 efi_char16_t *p;
355
356                 str = strstr(str, option_string);
357                 if (!str)
358                         break;
359
360                 str += strlen(option_string);
361
362                 file = &files[i];
363                 p = filename_16;
364
365                 /* Skip any leading slashes */
366                 while (*str == '/' || *str == '\\')
367                         str++;
368
369                 while (*str && *str != ' ' && *str != '\n') {
370                         if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
371                                 break;
372
373                         if (*str == '/') {
374                                 *p++ = '\\';
375                                 str++;
376                         } else {
377                                 *p++ = *str++;
378                         }
379                 }
380
381                 *p = '\0';
382
383                 /* Only open the volume once. */
384                 if (!i) {
385                         status = efi_open_volume(sys_table_arg, image,
386                                                  (void **)&fh);
387                         if (status != EFI_SUCCESS)
388                                 goto free_files;
389                 }
390
391                 status = efi_file_size(sys_table_arg, fh, filename_16,
392                                        (void **)&file->handle, &file->size);
393                 if (status != EFI_SUCCESS)
394                         goto close_handles;
395
396                 file_size_total += file->size;
397         }
398
399         if (file_size_total) {
400                 unsigned long addr;
401
402                 /*
403                  * Multiple files need to be at consecutive addresses in memory,
404                  * so allocate enough memory for all the files.  This is used
405                  * for loading multiple files.
406                  */
407                 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
408                                     &file_addr, max_addr);
409                 if (status != EFI_SUCCESS) {
410                         pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
411                         goto close_handles;
412                 }
413
414                 /* We've run out of free low memory. */
415                 if (file_addr > max_addr) {
416                         pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
417                         status = EFI_INVALID_PARAMETER;
418                         goto free_file_total;
419                 }
420
421                 addr = file_addr;
422                 for (j = 0; j < nr_files; j++) {
423                         unsigned long size;
424
425                         size = files[j].size;
426                         while (size) {
427                                 unsigned long chunksize;
428                                 if (size > EFI_READ_CHUNK_SIZE)
429                                         chunksize = EFI_READ_CHUNK_SIZE;
430                                 else
431                                         chunksize = size;
432
433                                 status = efi_file_read(files[j].handle,
434                                                        &chunksize,
435                                                        (void *)addr);
436                                 if (status != EFI_SUCCESS) {
437                                         pr_efi_err(sys_table_arg, "Failed to read file\n");
438                                         goto free_file_total;
439                                 }
440                                 addr += chunksize;
441                                 size -= chunksize;
442                         }
443
444                         efi_file_close(files[j].handle);
445                 }
446
447         }
448
449         efi_call_early(free_pool, files);
450
451         *load_addr = file_addr;
452         *load_size = file_size_total;
453
454         return status;
455
456 free_file_total:
457         efi_free(sys_table_arg, file_size_total, file_addr);
458
459 close_handles:
460         for (k = j; k < i; k++)
461                 efi_file_close(files[k].handle);
462 free_files:
463         efi_call_early(free_pool, files);
464 fail:
465         *load_addr = 0;
466         *load_size = 0;
467
468         return status;
469 }
470 /*
471  * Relocate a kernel image, either compressed or uncompressed.
472  * In the ARM64 case, all kernel images are currently
473  * uncompressed, and as such when we relocate it we need to
474  * allocate additional space for the BSS segment. Any low
475  * memory that this function should avoid needs to be
476  * unavailable in the EFI memory map, as if the preferred
477  * address is not available the lowest available address will
478  * be used.
479  */
480 static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
481                                         unsigned long *image_addr,
482                                         unsigned long image_size,
483                                         unsigned long alloc_size,
484                                         unsigned long preferred_addr,
485                                         unsigned long alignment)
486 {
487         unsigned long cur_image_addr;
488         unsigned long new_addr = 0;
489         efi_status_t status;
490         unsigned long nr_pages;
491         efi_physical_addr_t efi_addr = preferred_addr;
492
493         if (!image_addr || !image_size || !alloc_size)
494                 return EFI_INVALID_PARAMETER;
495         if (alloc_size < image_size)
496                 return EFI_INVALID_PARAMETER;
497
498         cur_image_addr = *image_addr;
499
500         /*
501          * The EFI firmware loader could have placed the kernel image
502          * anywhere in memory, but the kernel has restrictions on the
503          * max physical address it can run at.  Some architectures
504          * also have a prefered address, so first try to relocate
505          * to the preferred address.  If that fails, allocate as low
506          * as possible while respecting the required alignment.
507          */
508         nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
509         status = efi_call_early(allocate_pages,
510                                 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
511                                 nr_pages, &efi_addr);
512         new_addr = efi_addr;
513         /*
514          * If preferred address allocation failed allocate as low as
515          * possible.
516          */
517         if (status != EFI_SUCCESS) {
518                 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
519                                        &new_addr);
520         }
521         if (status != EFI_SUCCESS) {
522                 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
523                 return status;
524         }
525
526         /*
527          * We know source/dest won't overlap since both memory ranges
528          * have been allocated by UEFI, so we can safely use memcpy.
529          */
530         memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
531
532         /* Return the new address of the relocated image. */
533         *image_addr = new_addr;
534
535         return status;
536 }
537
538 /*
539  * Convert the unicode UEFI command line to ASCII to pass to kernel.
540  * Size of memory allocated return in *cmd_line_len.
541  * Returns NULL on error.
542  */
543 static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg,
544                                       efi_loaded_image_t *image,
545                                       int *cmd_line_len)
546 {
547         u16 *s2;
548         u8 *s1 = NULL;
549         unsigned long cmdline_addr = 0;
550         int load_options_size = image->load_options_size / 2; /* ASCII */
551         void *options = image->load_options;
552         int options_size = 0;
553         efi_status_t status;
554         int i;
555         u16 zero = 0;
556
557         if (options) {
558                 s2 = options;
559                 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
560                         s2++;
561                         options_size++;
562                 }
563         }
564
565         if (options_size == 0) {
566                 /* No command line options, so return empty string*/
567                 options_size = 1;
568                 options = &zero;
569         }
570
571         options_size++;  /* NUL termination */
572
573         status = efi_low_alloc(sys_table_arg, options_size, 0, &cmdline_addr);
574         if (status != EFI_SUCCESS)
575                 return NULL;
576
577         s1 = (u8 *)cmdline_addr;
578         s2 = (u16 *)options;
579
580         for (i = 0; i < options_size - 1; i++)
581                 *s1++ = *s2++;
582
583         *s1 = '\0';
584
585         *cmd_line_len = options_size;
586         return (char *)cmdline_addr;
587 }