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