efi/arm64: efistub: Move shared dependencies to <asm/efi.h>
[linux-2.6-block.git] / drivers / firmware / efi / 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 */
12#define EFI_READ_CHUNK_SIZE (1024 * 1024)
13
9bb40191
RF
14/* error code which can't be mistaken for valid address */
15#define EFI_ERROR (~0UL)
16
17
36f8961c 18struct file_info {
7721da4c
RF
19 efi_file_handle_t *handle;
20 u64 size;
21};
22
876dc36a 23static void efi_printk(efi_system_table_t *sys_table_arg, char *str)
7721da4c
RF
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 };
876dc36a 33 efi_char16_printk(sys_table_arg, nl);
7721da4c
RF
34 }
35
876dc36a 36 efi_char16_printk(sys_table_arg, ch);
7721da4c
RF
37 }
38}
39
f966ea02
RF
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
7721da4c 43
86cc653b
RF
44static 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,
1c089c65
RF
47 unsigned long *desc_size,
48 u32 *desc_ver,
49 unsigned long *key_ptr)
7721da4c
RF
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;
57again:
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);
204b0a1a
MF
63 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
64 *map_size, (void **)&m);
7721da4c
RF
65 if (status != EFI_SUCCESS)
66 goto fail;
67
54b52d87
MF
68 *desc_size = 0;
69 key = 0;
204b0a1a
MF
70 status = efi_call_early(get_memory_map, map_size, m,
71 &key, desc_size, &desc_version);
7721da4c 72 if (status == EFI_BUFFER_TOO_SMALL) {
204b0a1a 73 efi_call_early(free_pool, m);
7721da4c
RF
74 goto again;
75 }
76
77 if (status != EFI_SUCCESS)
204b0a1a 78 efi_call_early(free_pool, m);
54b52d87 79
1c089c65
RF
80 if (key_ptr && status == EFI_SUCCESS)
81 *key_ptr = key;
82 if (desc_ver && status == EFI_SUCCESS)
83 *desc_ver = desc_version;
7721da4c
RF
84
85fail:
86 *map = m;
87 return status;
88}
89
9bb40191
RF
90
91static 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
7721da4c
RF
116/*
117 * Allocate at the highest possible address that is not above 'max'.
118 */
40e4530a 119static efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
876dc36a
RF
120 unsigned long size, unsigned long align,
121 unsigned long *addr, unsigned long max)
7721da4c
RF
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
1c089c65
RF
130 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
131 NULL, NULL);
7721da4c
RF
132 if (status != EFI_SUCCESS)
133 goto fail;
134
38dd9c02
RF
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
7721da4c
RF
143 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
144again:
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 {
204b0a1a
MF
185 status = efi_call_early(allocate_pages,
186 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
187 nr_pages, &max_addr);
7721da4c
RF
188 if (status != EFI_SUCCESS) {
189 max = max_addr;
190 max_addr = 0;
191 goto again;
192 }
193
194 *addr = max_addr;
195 }
196
204b0a1a 197 efi_call_early(free_pool, map);
7721da4c
RF
198fail:
199 return status;
200}
201
202/*
203 * Allocate at the lowest possible address.
204 */
40e4530a
RF
205static efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
206 unsigned long size, unsigned long align,
7721da4c
RF
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
1c089c65
RF
215 status = efi_get_memory_map(sys_table_arg, &map, &map_size, &desc_size,
216 NULL, NULL);
7721da4c
RF
217 if (status != EFI_SUCCESS)
218 goto fail;
219
38dd9c02
RF
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
7721da4c
RF
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
204b0a1a
MF
257 status = efi_call_early(allocate_pages,
258 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
259 nr_pages, &start);
7721da4c
RF
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
204b0a1a 269 efi_call_early(free_pool, map);
7721da4c
RF
270fail:
271 return status;
272}
273
40e4530a 274static void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
876dc36a 275 unsigned long addr)
7721da4c
RF
276{
277 unsigned long nr_pages;
278
0e1cadb0
RF
279 if (!size)
280 return;
281
7721da4c 282 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
204b0a1a 283 efi_call_early(free_pages, addr, nr_pages);
7721da4c
RF
284}
285
286
287/*
36f8961c 288 * Check the cmdline for a LILO-style file= arguments.
7721da4c 289 *
36f8961c
RF
290 * We only support loading a file from the same filesystem as
291 * the kernel image.
7721da4c 292 */
46f4582e
RF
293static 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)
7721da4c 299{
36f8961c
RF
300 struct file_info *files;
301 unsigned long file_addr;
36f8961c 302 u64 file_size_total;
9403e462 303 efi_file_handle_t *fh = NULL;
7721da4c 304 efi_status_t status;
36f8961c 305 int nr_files;
7721da4c
RF
306 char *str;
307 int i, j, k;
308
36f8961c
RF
309 file_addr = 0;
310 file_size_total = 0;
7721da4c 311
46f4582e 312 str = cmd_line;
7721da4c
RF
313
314 j = 0; /* See close_handles */
315
46f4582e
RF
316 if (!load_addr || !load_size)
317 return EFI_INVALID_PARAMETER;
318
319 *load_addr = 0;
320 *load_size = 0;
321
7721da4c
RF
322 if (!str || !*str)
323 return EFI_SUCCESS;
324
36f8961c 325 for (nr_files = 0; *str; nr_files++) {
46f4582e 326 str = strstr(str, option_string);
7721da4c
RF
327 if (!str)
328 break;
329
46f4582e 330 str += strlen(option_string);
7721da4c
RF
331
332 /* Skip any leading slashes */
333 while (*str == '/' || *str == '\\')
334 str++;
335
336 while (*str && *str != ' ' && *str != '\n')
337 str++;
338 }
339
36f8961c 340 if (!nr_files)
7721da4c
RF
341 return EFI_SUCCESS;
342
204b0a1a
MF
343 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
344 nr_files * sizeof(*files), (void **)&files);
7721da4c 345 if (status != EFI_SUCCESS) {
f966ea02 346 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
7721da4c
RF
347 goto fail;
348 }
349
46f4582e 350 str = cmd_line;
36f8961c
RF
351 for (i = 0; i < nr_files; i++) {
352 struct file_info *file;
7721da4c 353 efi_char16_t filename_16[256];
7721da4c 354 efi_char16_t *p;
7721da4c 355
46f4582e 356 str = strstr(str, option_string);
7721da4c
RF
357 if (!str)
358 break;
359
46f4582e 360 str += strlen(option_string);
7721da4c 361
36f8961c 362 file = &files[i];
7721da4c
RF
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++ = '\\';
4e283088 375 str++;
7721da4c
RF
376 } else {
377 *p++ = *str++;
378 }
379 }
380
381 *p = '\0';
382
383 /* Only open the volume once. */
384 if (!i) {
54b52d87
MF
385 status = efi_open_volume(sys_table_arg, image,
386 (void **)&fh);
387 if (status != EFI_SUCCESS)
36f8961c 388 goto free_files;
7721da4c
RF
389 }
390
54b52d87
MF
391 status = efi_file_size(sys_table_arg, fh, filename_16,
392 (void **)&file->handle, &file->size);
393 if (status != EFI_SUCCESS)
7721da4c 394 goto close_handles;
7721da4c 395
54b52d87 396 file_size_total += file->size;
7721da4c
RF
397 }
398
36f8961c 399 if (file_size_total) {
7721da4c
RF
400 unsigned long addr;
401
402 /*
36f8961c
RF
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.
7721da4c 406 */
36f8961c
RF
407 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
408 &file_addr, max_addr);
7721da4c 409 if (status != EFI_SUCCESS) {
f966ea02 410 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
7721da4c
RF
411 goto close_handles;
412 }
413
414 /* We've run out of free low memory. */
36f8961c 415 if (file_addr > max_addr) {
f966ea02 416 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
7721da4c 417 status = EFI_INVALID_PARAMETER;
36f8961c 418 goto free_file_total;
7721da4c
RF
419 }
420
36f8961c
RF
421 addr = file_addr;
422 for (j = 0; j < nr_files; j++) {
6a5fe770 423 unsigned long size;
7721da4c 424
36f8961c 425 size = files[j].size;
7721da4c 426 while (size) {
6a5fe770 427 unsigned long chunksize;
7721da4c
RF
428 if (size > EFI_READ_CHUNK_SIZE)
429 chunksize = EFI_READ_CHUNK_SIZE;
430 else
431 chunksize = size;
54b52d87 432
47514c99 433 status = efi_file_read(files[j].handle,
54b52d87
MF
434 &chunksize,
435 (void *)addr);
7721da4c 436 if (status != EFI_SUCCESS) {
f966ea02 437 pr_efi_err(sys_table_arg, "Failed to read file\n");
36f8961c 438 goto free_file_total;
7721da4c
RF
439 }
440 addr += chunksize;
441 size -= chunksize;
442 }
443
47514c99 444 efi_file_close(files[j].handle);
7721da4c
RF
445 }
446
447 }
448
204b0a1a 449 efi_call_early(free_pool, files);
7721da4c 450
36f8961c
RF
451 *load_addr = file_addr;
452 *load_size = file_size_total;
7721da4c
RF
453
454 return status;
455
36f8961c
RF
456free_file_total:
457 efi_free(sys_table_arg, file_size_total, file_addr);
7721da4c
RF
458
459close_handles:
460 for (k = j; k < i; k++)
47514c99 461 efi_file_close(files[k].handle);
36f8961c 462free_files:
204b0a1a 463 efi_call_early(free_pool, files);
7721da4c 464fail:
46f4582e
RF
465 *load_addr = 0;
466 *load_size = 0;
7721da4c
RF
467
468 return status;
469}
4a9f3a7c
RF
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 */
480static 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)
c6866d72 486{
4a9f3a7c
RF
487 unsigned long cur_image_addr;
488 unsigned long new_addr = 0;
c6866d72 489 efi_status_t status;
4a9f3a7c
RF
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;
c6866d72
RF
499
500 /*
501 * The EFI firmware loader could have placed the kernel image
4a9f3a7c
RF
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.
c6866d72 507 */
4a9f3a7c 508 nr_pages = round_up(alloc_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
204b0a1a
MF
509 status = efi_call_early(allocate_pages,
510 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
511 nr_pages, &efi_addr);
4a9f3a7c
RF
512 new_addr = efi_addr;
513 /*
514 * If preferred address allocation failed allocate as low as
515 * possible.
516 */
c6866d72 517 if (status != EFI_SUCCESS) {
4a9f3a7c
RF
518 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
519 &new_addr);
520 }
521 if (status != EFI_SUCCESS) {
f966ea02 522 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
4a9f3a7c 523 return status;
c6866d72
RF
524 }
525
4a9f3a7c
RF
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);
c6866d72 531
4a9f3a7c
RF
532 /* Return the new address of the relocated image. */
533 *image_addr = new_addr;
c6866d72
RF
534
535 return status;
536}
5fef3870 537
c625d1c2
PA
538/*
539 * Get the number of UTF-8 bytes corresponding to an UTF-16 character.
540 * This overestimates for surrogates, but that is okay.
541 */
542static int efi_utf8_bytes(u16 c)
543{
544 return 1 + (c >= 0x80) + (c >= 0x800);
545}
546
547/*
548 * Convert an UTF-16 string, not necessarily null terminated, to UTF-8.
549 */
550static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
551{
552 unsigned int c;
553
554 while (n--) {
555 c = *src++;
556 if (n && c >= 0xd800 && c <= 0xdbff &&
557 *src >= 0xdc00 && *src <= 0xdfff) {
558 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
559 src++;
560 n--;
561 }
562 if (c >= 0xd800 && c <= 0xdfff)
563 c = 0xfffd; /* Unmatched surrogate */
564 if (c < 0x80) {
565 *dst++ = c;
566 continue;
567 }
568 if (c < 0x800) {
569 *dst++ = 0xc0 + (c >> 6);
570 goto t1;
571 }
572 if (c < 0x10000) {
573 *dst++ = 0xe0 + (c >> 12);
574 goto t2;
575 }
576 *dst++ = 0xf0 + (c >> 18);
577 *dst++ = 0x80 + ((c >> 12) & 0x3f);
578 t2:
579 *dst++ = 0x80 + ((c >> 6) & 0x3f);
580 t1:
581 *dst++ = 0x80 + (c & 0x3f);
582 }
583
584 return dst;
585}
586
5fef3870
RF
587/*
588 * Convert the unicode UEFI command line to ASCII to pass to kernel.
589 * Size of memory allocated return in *cmd_line_len.
590 * Returns NULL on error.
591 */
c625d1c2
PA
592static char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
593 efi_loaded_image_t *image,
594 int *cmd_line_len)
5fef3870 595{
c625d1c2 596 const u16 *s2;
5fef3870
RF
597 u8 *s1 = NULL;
598 unsigned long cmdline_addr = 0;
c625d1c2
PA
599 int load_options_chars = image->load_options_size / 2; /* UTF-16 */
600 const u16 *options = image->load_options;
601 int options_bytes = 0; /* UTF-8 bytes */
602 int options_chars = 0; /* UTF-16 chars */
5fef3870 603 efi_status_t status;
5fef3870
RF
604 u16 zero = 0;
605
606 if (options) {
607 s2 = options;
c625d1c2
PA
608 while (*s2 && *s2 != '\n'
609 && options_chars < load_options_chars) {
610 options_bytes += efi_utf8_bytes(*s2++);
611 options_chars++;
5fef3870
RF
612 }
613 }
614
c625d1c2 615 if (!options_chars) {
5fef3870 616 /* No command line options, so return empty string*/
5fef3870
RF
617 options = &zero;
618 }
619
c625d1c2 620 options_bytes++; /* NUL termination */
9403e462 621
c625d1c2 622 status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr);
5fef3870
RF
623 if (status != EFI_SUCCESS)
624 return NULL;
625
626 s1 = (u8 *)cmdline_addr;
c625d1c2 627 s2 = (const u16 *)options;
5fef3870 628
c625d1c2 629 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
5fef3870
RF
630 *s1 = '\0';
631
c625d1c2 632 *cmd_line_len = options_bytes;
5fef3870
RF
633 return (char *)cmdline_addr;
634}