]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/firmware/efi/libstub/efi-stub-helper.c
Merge branch 'overlayfs-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mszere...
[karo-tx-linux.git] / drivers / firmware / efi / libstub / 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
13 #include <linux/efi.h>
14 #include <asm/efi.h>
15
16 #include "efistub.h"
17
18 /*
19  * Some firmware implementations have problems reading files in one go.
20  * A read chunk size of 1MB seems to work for most platforms.
21  *
22  * Unfortunately, reading files in chunks triggers *other* bugs on some
23  * platforms, so we provide a way to disable this workaround, which can
24  * be done by passing "efi=nochunk" on the EFI boot stub command line.
25  *
26  * If you experience issues with initrd images being corrupt it's worth
27  * trying efi=nochunk, but chunking is enabled by default because there
28  * are far more machines that require the workaround than those that
29  * break with it enabled.
30  */
31 #define EFI_READ_CHUNK_SIZE     (1024 * 1024)
32
33 static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
34
35 /*
36  * Allow the platform to override the allocation granularity: this allows
37  * systems that have the capability to run with a larger page size to deal
38  * with the allocations for initrd and fdt more efficiently.
39  */
40 #ifndef EFI_ALLOC_ALIGN
41 #define EFI_ALLOC_ALIGN         EFI_PAGE_SIZE
42 #endif
43
44 struct file_info {
45         efi_file_handle_t *handle;
46         u64 size;
47 };
48
49 void efi_printk(efi_system_table_t *sys_table_arg, char *str)
50 {
51         char *s8;
52
53         for (s8 = str; *s8; s8++) {
54                 efi_char16_t ch[2] = { 0 };
55
56                 ch[0] = *s8;
57                 if (*s8 == '\n') {
58                         efi_char16_t nl[2] = { '\r', 0 };
59                         efi_char16_printk(sys_table_arg, nl);
60                 }
61
62                 efi_char16_printk(sys_table_arg, ch);
63         }
64 }
65
66 efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
67                                 efi_memory_desc_t **map,
68                                 unsigned long *map_size,
69                                 unsigned long *desc_size,
70                                 u32 *desc_ver,
71                                 unsigned long *key_ptr)
72 {
73         efi_memory_desc_t *m = NULL;
74         efi_status_t status;
75         unsigned long key;
76         u32 desc_version;
77
78         *map_size = 0;
79         *desc_size = 0;
80         key = 0;
81         status = efi_call_early(get_memory_map, map_size, NULL,
82                                 &key, desc_size, &desc_version);
83         if (status != EFI_BUFFER_TOO_SMALL)
84                 return EFI_LOAD_ERROR;
85
86         /*
87          * Add an additional efi_memory_desc_t because we're doing an
88          * allocation which may be in a new descriptor region.
89          */
90         *map_size += *desc_size;
91         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
92                                 *map_size, (void **)&m);
93         if (status != EFI_SUCCESS)
94                 goto fail;
95
96         status = efi_call_early(get_memory_map, map_size, m,
97                                 &key, desc_size, &desc_version);
98         if (status == EFI_BUFFER_TOO_SMALL) {
99                 efi_call_early(free_pool, m);
100                 return EFI_LOAD_ERROR;
101         }
102
103         if (status != EFI_SUCCESS)
104                 efi_call_early(free_pool, m);
105
106         if (key_ptr && status == EFI_SUCCESS)
107                 *key_ptr = key;
108         if (desc_ver && status == EFI_SUCCESS)
109                 *desc_ver = desc_version;
110
111 fail:
112         *map = m;
113         return status;
114 }
115
116
117 unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
118 {
119         efi_status_t status;
120         unsigned long map_size;
121         unsigned long membase  = EFI_ERROR;
122         struct efi_memory_map map;
123         efi_memory_desc_t *md;
124
125         status = efi_get_memory_map(sys_table_arg, (efi_memory_desc_t **)&map.map,
126                                     &map_size, &map.desc_size, NULL, NULL);
127         if (status != EFI_SUCCESS)
128                 return membase;
129
130         map.map_end = map.map + map_size;
131
132         for_each_efi_memory_desc(&map, md)
133                 if (md->attribute & EFI_MEMORY_WB)
134                         if (membase > md->phys_addr)
135                                 membase = md->phys_addr;
136
137         efi_call_early(free_pool, map.map);
138
139         return membase;
140 }
141
142 /*
143  * Allocate at the highest possible address that is not above 'max'.
144  */
145 efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
146                             unsigned long size, unsigned long align,
147                             unsigned long *addr, unsigned long max)
148 {
149         unsigned long map_size, desc_size;
150         efi_memory_desc_t *map;
151         efi_status_t status;
152         unsigned long nr_pages;
153         u64 max_addr = 0;
154         int i;
155
156         status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
157                                     NULL, NULL);
158         if (status != EFI_SUCCESS)
159                 goto fail;
160
161         /*
162          * Enforce minimum alignment that EFI requires when requesting
163          * a specific address.  We are doing page-based allocations,
164          * so we must be aligned to a page.
165          */
166         if (align < EFI_ALLOC_ALIGN)
167                 align = EFI_ALLOC_ALIGN;
168
169         nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
170 again:
171         for (i = 0; i < map_size / desc_size; i++) {
172                 efi_memory_desc_t *desc;
173                 unsigned long m = (unsigned long)map;
174                 u64 start, end;
175
176                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
177                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
178                         continue;
179
180                 if (desc->num_pages < nr_pages)
181                         continue;
182
183                 start = desc->phys_addr;
184                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
185
186                 if ((start + size) > end || (start + size) > max)
187                         continue;
188
189                 if (end - size > max)
190                         end = max;
191
192                 if (round_down(end - size, align) < start)
193                         continue;
194
195                 start = round_down(end - size, align);
196
197                 /*
198                  * Don't allocate at 0x0. It will confuse code that
199                  * checks pointers against NULL.
200                  */
201                 if (start == 0x0)
202                         continue;
203
204                 if (start > max_addr)
205                         max_addr = start;
206         }
207
208         if (!max_addr)
209                 status = EFI_NOT_FOUND;
210         else {
211                 status = efi_call_early(allocate_pages,
212                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
213                                         nr_pages, &max_addr);
214                 if (status != EFI_SUCCESS) {
215                         max = max_addr;
216                         max_addr = 0;
217                         goto again;
218                 }
219
220                 *addr = max_addr;
221         }
222
223         efi_call_early(free_pool, map);
224 fail:
225         return status;
226 }
227
228 /*
229  * Allocate at the lowest possible address.
230  */
231 efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
232                            unsigned long size, unsigned long align,
233                            unsigned long *addr)
234 {
235         unsigned long map_size, desc_size;
236         efi_memory_desc_t *map;
237         efi_status_t status;
238         unsigned long nr_pages;
239         int i;
240
241         status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
242                                     NULL, NULL);
243         if (status != EFI_SUCCESS)
244                 goto fail;
245
246         /*
247          * Enforce minimum alignment that EFI requires when requesting
248          * a specific address.  We are doing page-based allocations,
249          * so we must be aligned to a page.
250          */
251         if (align < EFI_ALLOC_ALIGN)
252                 align = EFI_ALLOC_ALIGN;
253
254         nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
255         for (i = 0; i < map_size / desc_size; i++) {
256                 efi_memory_desc_t *desc;
257                 unsigned long m = (unsigned long)map;
258                 u64 start, end;
259
260                 desc = (efi_memory_desc_t *)(m + (i * desc_size));
261
262                 if (desc->type != EFI_CONVENTIONAL_MEMORY)
263                         continue;
264
265                 if (desc->num_pages < nr_pages)
266                         continue;
267
268                 start = desc->phys_addr;
269                 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
270
271                 /*
272                  * Don't allocate at 0x0. It will confuse code that
273                  * checks pointers against NULL. Skip the first 8
274                  * bytes so we start at a nice even number.
275                  */
276                 if (start == 0x0)
277                         start += 8;
278
279                 start = round_up(start, align);
280                 if ((start + size) > end)
281                         continue;
282
283                 status = efi_call_early(allocate_pages,
284                                         EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
285                                         nr_pages, &start);
286                 if (status == EFI_SUCCESS) {
287                         *addr = start;
288                         break;
289                 }
290         }
291
292         if (i == map_size / desc_size)
293                 status = EFI_NOT_FOUND;
294
295         efi_call_early(free_pool, map);
296 fail:
297         return status;
298 }
299
300 void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
301               unsigned long addr)
302 {
303         unsigned long nr_pages;
304
305         if (!size)
306                 return;
307
308         nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
309         efi_call_early(free_pages, addr, nr_pages);
310 }
311
312 /*
313  * Parse the ASCII string 'cmdline' for EFI options, denoted by the efi=
314  * option, e.g. efi=nochunk.
315  *
316  * It should be noted that efi= is parsed in two very different
317  * environments, first in the early boot environment of the EFI boot
318  * stub, and subsequently during the kernel boot.
319  */
320 efi_status_t efi_parse_options(char *cmdline)
321 {
322         char *str;
323
324         /*
325          * If no EFI parameters were specified on the cmdline we've got
326          * nothing to do.
327          */
328         str = strstr(cmdline, "efi=");
329         if (!str)
330                 return EFI_SUCCESS;
331
332         /* Skip ahead to first argument */
333         str += strlen("efi=");
334
335         /*
336          * Remember, because efi= is also used by the kernel we need to
337          * skip over arguments we don't understand.
338          */
339         while (*str) {
340                 if (!strncmp(str, "nochunk", 7)) {
341                         str += strlen("nochunk");
342                         __chunk_size = -1UL;
343                 }
344
345                 /* Group words together, delimited by "," */
346                 while (*str && *str != ',')
347                         str++;
348
349                 if (*str == ',')
350                         str++;
351         }
352
353         return EFI_SUCCESS;
354 }
355
356 /*
357  * Check the cmdline for a LILO-style file= arguments.
358  *
359  * We only support loading a file from the same filesystem as
360  * the kernel image.
361  */
362 efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
363                                   efi_loaded_image_t *image,
364                                   char *cmd_line, char *option_string,
365                                   unsigned long max_addr,
366                                   unsigned long *load_addr,
367                                   unsigned long *load_size)
368 {
369         struct file_info *files;
370         unsigned long file_addr;
371         u64 file_size_total;
372         efi_file_handle_t *fh = NULL;
373         efi_status_t status;
374         int nr_files;
375         char *str;
376         int i, j, k;
377
378         file_addr = 0;
379         file_size_total = 0;
380
381         str = cmd_line;
382
383         j = 0;                  /* See close_handles */
384
385         if (!load_addr || !load_size)
386                 return EFI_INVALID_PARAMETER;
387
388         *load_addr = 0;
389         *load_size = 0;
390
391         if (!str || !*str)
392                 return EFI_SUCCESS;
393
394         for (nr_files = 0; *str; nr_files++) {
395                 str = strstr(str, option_string);
396                 if (!str)
397                         break;
398
399                 str += strlen(option_string);
400
401                 /* Skip any leading slashes */
402                 while (*str == '/' || *str == '\\')
403                         str++;
404
405                 while (*str && *str != ' ' && *str != '\n')
406                         str++;
407         }
408
409         if (!nr_files)
410                 return EFI_SUCCESS;
411
412         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
413                                 nr_files * sizeof(*files), (void **)&files);
414         if (status != EFI_SUCCESS) {
415                 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
416                 goto fail;
417         }
418
419         str = cmd_line;
420         for (i = 0; i < nr_files; i++) {
421                 struct file_info *file;
422                 efi_char16_t filename_16[256];
423                 efi_char16_t *p;
424
425                 str = strstr(str, option_string);
426                 if (!str)
427                         break;
428
429                 str += strlen(option_string);
430
431                 file = &files[i];
432                 p = filename_16;
433
434                 /* Skip any leading slashes */
435                 while (*str == '/' || *str == '\\')
436                         str++;
437
438                 while (*str && *str != ' ' && *str != '\n') {
439                         if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
440                                 break;
441
442                         if (*str == '/') {
443                                 *p++ = '\\';
444                                 str++;
445                         } else {
446                                 *p++ = *str++;
447                         }
448                 }
449
450                 *p = '\0';
451
452                 /* Only open the volume once. */
453                 if (!i) {
454                         status = efi_open_volume(sys_table_arg, image,
455                                                  (void **)&fh);
456                         if (status != EFI_SUCCESS)
457                                 goto free_files;
458                 }
459
460                 status = efi_file_size(sys_table_arg, fh, filename_16,
461                                        (void **)&file->handle, &file->size);
462                 if (status != EFI_SUCCESS)
463                         goto close_handles;
464
465                 file_size_total += file->size;
466         }
467
468         if (file_size_total) {
469                 unsigned long addr;
470
471                 /*
472                  * Multiple files need to be at consecutive addresses in memory,
473                  * so allocate enough memory for all the files.  This is used
474                  * for loading multiple files.
475                  */
476                 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
477                                     &file_addr, max_addr);
478                 if (status != EFI_SUCCESS) {
479                         pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
480                         goto close_handles;
481                 }
482
483                 /* We've run out of free low memory. */
484                 if (file_addr > max_addr) {
485                         pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
486                         status = EFI_INVALID_PARAMETER;
487                         goto free_file_total;
488                 }
489
490                 addr = file_addr;
491                 for (j = 0; j < nr_files; j++) {
492                         unsigned long size;
493
494                         size = files[j].size;
495                         while (size) {
496                                 unsigned long chunksize;
497                                 if (size > __chunk_size)
498                                         chunksize = __chunk_size;
499                                 else
500                                         chunksize = size;
501
502                                 status = efi_file_read(files[j].handle,
503                                                        &chunksize,
504                                                        (void *)addr);
505                                 if (status != EFI_SUCCESS) {
506                                         pr_efi_err(sys_table_arg, "Failed to read file\n");
507                                         goto free_file_total;
508                                 }
509                                 addr += chunksize;
510                                 size -= chunksize;
511                         }
512
513                         efi_file_close(files[j].handle);
514                 }
515
516         }
517
518         efi_call_early(free_pool, files);
519
520         *load_addr = file_addr;
521         *load_size = file_size_total;
522
523         return status;
524
525 free_file_total:
526         efi_free(sys_table_arg, file_size_total, file_addr);
527
528 close_handles:
529         for (k = j; k < i; k++)
530                 efi_file_close(files[k].handle);
531 free_files:
532         efi_call_early(free_pool, files);
533 fail:
534         *load_addr = 0;
535         *load_size = 0;
536
537         return status;
538 }
539 /*
540  * Relocate a kernel image, either compressed or uncompressed.
541  * In the ARM64 case, all kernel images are currently
542  * uncompressed, and as such when we relocate it we need to
543  * allocate additional space for the BSS segment. Any low
544  * memory that this function should avoid needs to be
545  * unavailable in the EFI memory map, as if the preferred
546  * address is not available the lowest available address will
547  * be used.
548  */
549 efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
550                                  unsigned long *image_addr,
551                                  unsigned long image_size,
552                                  unsigned long alloc_size,
553                                  unsigned long preferred_addr,
554                                  unsigned long alignment)
555 {
556         unsigned long cur_image_addr;
557         unsigned long new_addr = 0;
558         efi_status_t status;
559         unsigned long nr_pages;
560         efi_physical_addr_t efi_addr = preferred_addr;
561
562         if (!image_addr || !image_size || !alloc_size)
563                 return EFI_INVALID_PARAMETER;
564         if (alloc_size < image_size)
565                 return EFI_INVALID_PARAMETER;
566
567         cur_image_addr = *image_addr;
568
569         /*
570          * The EFI firmware loader could have placed the kernel image
571          * anywhere in memory, but the kernel has restrictions on the
572          * max physical address it can run at.  Some architectures
573          * also have a prefered address, so first try to relocate
574          * to the preferred address.  If that fails, allocate as low
575          * as possible while respecting the required alignment.
576          */
577         nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
578         status = efi_call_early(allocate_pages,
579                                 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
580                                 nr_pages, &efi_addr);
581         new_addr = efi_addr;
582         /*
583          * If preferred address allocation failed allocate as low as
584          * possible.
585          */
586         if (status != EFI_SUCCESS) {
587                 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
588                                        &new_addr);
589         }
590         if (status != EFI_SUCCESS) {
591                 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
592                 return status;
593         }
594
595         /*
596          * We know source/dest won't overlap since both memory ranges
597          * have been allocated by UEFI, so we can safely use memcpy.
598          */
599         memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
600
601         /* Return the new address of the relocated image. */
602         *image_addr = new_addr;
603
604         return status;
605 }
606
607 /*
608  * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
609  * This overestimates for surrogates, but that is okay.
610  */
611 static int efi_utf8_bytes(u16 c)
612 {
613         return 1 + (c >= 0x80) + (c >= 0x800);
614 }
615
616 /*
617  * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
618  */
619 static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
620 {
621         unsigned int c;
622
623         while (n--) {
624                 c = *src++;
625                 if (n && c >= 0xd800 && c <= 0xdbff &&
626                     *src >= 0xdc00 && *src <= 0xdfff) {
627                         c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
628                         src++;
629                         n--;
630                 }
631                 if (c >= 0xd800 && c <= 0xdfff)
632                         c = 0xfffd; /* Unmatched surrogate */
633                 if (c < 0x80) {
634                         *dst++ = c;
635                         continue;
636                 }
637                 if (c < 0x800) {
638                         *dst++ = 0xc0 + (c >> 6);
639                         goto t1;
640                 }
641                 if (c < 0x10000) {
642                         *dst++ = 0xe0 + (c >> 12);
643                         goto t2;
644                 }
645                 *dst++ = 0xf0 + (c >> 18);
646                 *dst++ = 0x80 + ((c >> 12) & 0x3f);
647         t2:
648                 *dst++ = 0x80 + ((c >> 6) & 0x3f);
649         t1:
650                 *dst++ = 0x80 + (c & 0x3f);
651         }
652
653         return dst;
654 }
655
656 /*
657  * Convert the unicode UEFI command line to ASCII to pass to kernel.
658  * Size of memory allocated return in *cmd_line_len.
659  * Returns NULL on error.
660  */
661 char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
662                           efi_loaded_image_t *image,
663                           int *cmd_line_len)
664 {
665         const u16 *s2;
666         u8 *s1 = NULL;
667         unsigned long cmdline_addr = 0;
668         int load_options_chars = image->load_options_size / 2; /* UTF-16 */
669         const u16 *options = image->load_options;
670         int options_bytes = 0;  /* UTF-8 bytes */
671         int options_chars = 0;  /* UTF-16 chars */
672         efi_status_t status;
673         u16 zero = 0;
674
675         if (options) {
676                 s2 = options;
677                 while (*s2 && *s2 != '\n'
678                        && options_chars < load_options_chars) {
679                         options_bytes += efi_utf8_bytes(*s2++);
680                         options_chars++;
681                 }
682         }
683
684         if (!options_chars) {
685                 /* No command line options, so return empty string*/
686                 options = &zero;
687         }
688
689         options_bytes++;        /* NUL termination */
690
691         status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
692         if (status != EFI_SUCCESS)
693                 return NULL;
694
695         s1 = (u8 *)cmdline_addr;
696         s2 = (const u16 *)options;
697
698         s1 = efi_utf16_to_utf8(s1, s2, options_chars);
699         *s1 = '\0';
700
701         *cmd_line_len = options_bytes;
702         return (char *)cmdline_addr;
703 }