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