1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/efi.h>
14#include <asm/efi.h>
15
16#include "efistub.h"
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#define EFI_READ_CHUNK_SIZE (1024 * 1024)
32
33static unsigned long __chunk_size = EFI_READ_CHUNK_SIZE;
34
35#define EFI_MMAP_NR_SLACK_SLOTS 8
36
37struct file_info {
38 efi_file_handle_t *handle;
39 u64 size;
40};
41
42void efi_printk(efi_system_table_t *sys_table_arg, char *str)
43{
44 char *s8;
45
46 for (s8 = str; *s8; s8++) {
47 efi_char16_t ch[2] = { 0 };
48
49 ch[0] = *s8;
50 if (*s8 == '\n') {
51 efi_char16_t nl[2] = { '\r', 0 };
52 efi_char16_printk(sys_table_arg, nl);
53 }
54
55 efi_char16_printk(sys_table_arg, ch);
56 }
57}
58
59static inline bool mmap_has_headroom(unsigned long buff_size,
60 unsigned long map_size,
61 unsigned long desc_size)
62{
63 unsigned long slack = buff_size - map_size;
64
65 return slack / desc_size >= EFI_MMAP_NR_SLACK_SLOTS;
66}
67
68efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
69 struct efi_boot_memmap *map)
70{
71 efi_memory_desc_t *m = NULL;
72 efi_status_t status;
73 unsigned long key;
74 u32 desc_version;
75
76 *map->desc_size = sizeof(*m);
77 *map->map_size = *map->desc_size * 32;
78 *map->buff_size = *map->map_size;
79again:
80 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
81 *map->map_size, (void **)&m);
82 if (status != EFI_SUCCESS)
83 goto fail;
84
85 *map->desc_size = 0;
86 key = 0;
87 status = efi_call_early(get_memory_map, map->map_size, m,
88 &key, map->desc_size, &desc_version);
89 if (status == EFI_BUFFER_TOO_SMALL ||
90 !mmap_has_headroom(*map->buff_size, *map->map_size,
91 *map->desc_size)) {
92 efi_call_early(free_pool, m);
93
94
95
96
97
98
99
100 *map->map_size += *map->desc_size * EFI_MMAP_NR_SLACK_SLOTS;
101 *map->buff_size = *map->map_size;
102 goto again;
103 }
104
105 if (status != EFI_SUCCESS)
106 efi_call_early(free_pool, m);
107
108 if (map->key_ptr && status == EFI_SUCCESS)
109 *map->key_ptr = key;
110 if (map->desc_ver && status == EFI_SUCCESS)
111 *map->desc_ver = desc_version;
112
113fail:
114 *map->map = m;
115 return status;
116}
117
118
119unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
120{
121 efi_status_t status;
122 unsigned long map_size, buff_size;
123 unsigned long membase = EFI_ERROR;
124 struct efi_memory_map map;
125 efi_memory_desc_t *md;
126 struct efi_boot_memmap boot_map;
127
128 boot_map.map = (efi_memory_desc_t **)&map.map;
129 boot_map.map_size = &map_size;
130 boot_map.desc_size = &map.desc_size;
131 boot_map.desc_ver = NULL;
132 boot_map.key_ptr = NULL;
133 boot_map.buff_size = &buff_size;
134
135 status = efi_get_memory_map(sys_table_arg, &boot_map);
136 if (status != EFI_SUCCESS)
137 return membase;
138
139 map.map_end = map.map + map_size;
140
141 for_each_efi_memory_desc_in_map(&map, md) {
142 if (md->attribute & EFI_MEMORY_WB) {
143 if (membase > md->phys_addr)
144 membase = md->phys_addr;
145 }
146 }
147
148 efi_call_early(free_pool, map.map);
149
150 return membase;
151}
152
153
154
155
156efi_status_t efi_high_alloc(efi_system_table_t *sys_table_arg,
157 unsigned long size, unsigned long align,
158 unsigned long *addr, unsigned long max)
159{
160 unsigned long map_size, desc_size, buff_size;
161 efi_memory_desc_t *map;
162 efi_status_t status;
163 unsigned long nr_pages;
164 u64 max_addr = 0;
165 int i;
166 struct efi_boot_memmap boot_map;
167
168 boot_map.map = ↦
169 boot_map.map_size = &map_size;
170 boot_map.desc_size = &desc_size;
171 boot_map.desc_ver = NULL;
172 boot_map.key_ptr = NULL;
173 boot_map.buff_size = &buff_size;
174
175 status = efi_get_memory_map(sys_table_arg, &boot_map);
176 if (status != EFI_SUCCESS)
177 goto fail;
178
179
180
181
182
183
184
185 if (align < EFI_ALLOC_ALIGN)
186 align = EFI_ALLOC_ALIGN;
187
188 size = round_up(size, EFI_ALLOC_ALIGN);
189 nr_pages = size / EFI_PAGE_SIZE;
190again:
191 for (i = 0; i < map_size / desc_size; i++) {
192 efi_memory_desc_t *desc;
193 unsigned long m = (unsigned long)map;
194 u64 start, end;
195
196 desc = (efi_memory_desc_t *)(m + (i * desc_size));
197 if (desc->type != EFI_CONVENTIONAL_MEMORY)
198 continue;
199
200 if (desc->num_pages < nr_pages)
201 continue;
202
203 start = desc->phys_addr;
204 end = start + desc->num_pages * EFI_PAGE_SIZE;
205
206 if (end > max)
207 end = max;
208
209 if ((start + size) > end)
210 continue;
211
212 if (round_down(end - size, align) < start)
213 continue;
214
215 start = round_down(end - size, align);
216
217
218
219
220
221 if (start == 0x0)
222 continue;
223
224 if (start > max_addr)
225 max_addr = start;
226 }
227
228 if (!max_addr)
229 status = EFI_NOT_FOUND;
230 else {
231 status = efi_call_early(allocate_pages,
232 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
233 nr_pages, &max_addr);
234 if (status != EFI_SUCCESS) {
235 max = max_addr;
236 max_addr = 0;
237 goto again;
238 }
239
240 *addr = max_addr;
241 }
242
243 efi_call_early(free_pool, map);
244fail:
245 return status;
246}
247
248
249
250
251efi_status_t efi_low_alloc(efi_system_table_t *sys_table_arg,
252 unsigned long size, unsigned long align,
253 unsigned long *addr)
254{
255 unsigned long map_size, desc_size, buff_size;
256 efi_memory_desc_t *map;
257 efi_status_t status;
258 unsigned long nr_pages;
259 int i;
260 struct efi_boot_memmap boot_map;
261
262 boot_map.map = ↦
263 boot_map.map_size = &map_size;
264 boot_map.desc_size = &desc_size;
265 boot_map.desc_ver = NULL;
266 boot_map.key_ptr = NULL;
267 boot_map.buff_size = &buff_size;
268
269 status = efi_get_memory_map(sys_table_arg, &boot_map);
270 if (status != EFI_SUCCESS)
271 goto fail;
272
273
274
275
276
277
278
279 if (align < EFI_ALLOC_ALIGN)
280 align = EFI_ALLOC_ALIGN;
281
282 size = round_up(size, EFI_ALLOC_ALIGN);
283 nr_pages = size / EFI_PAGE_SIZE;
284 for (i = 0; i < map_size / desc_size; i++) {
285 efi_memory_desc_t *desc;
286 unsigned long m = (unsigned long)map;
287 u64 start, end;
288
289 desc = (efi_memory_desc_t *)(m + (i * desc_size));
290
291 if (desc->type != EFI_CONVENTIONAL_MEMORY)
292 continue;
293
294 if (desc->num_pages < nr_pages)
295 continue;
296
297 start = desc->phys_addr;
298 end = start + desc->num_pages * EFI_PAGE_SIZE;
299
300
301
302
303
304
305 if (start == 0x0)
306 start += 8;
307
308 start = round_up(start, align);
309 if ((start + size) > end)
310 continue;
311
312 status = efi_call_early(allocate_pages,
313 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
314 nr_pages, &start);
315 if (status == EFI_SUCCESS) {
316 *addr = start;
317 break;
318 }
319 }
320
321 if (i == map_size / desc_size)
322 status = EFI_NOT_FOUND;
323
324 efi_call_early(free_pool, map);
325fail:
326 return status;
327}
328
329void efi_free(efi_system_table_t *sys_table_arg, unsigned long size,
330 unsigned long addr)
331{
332 unsigned long nr_pages;
333
334 if (!size)
335 return;
336
337 nr_pages = round_up(size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
338 efi_call_early(free_pages, addr, nr_pages);
339}
340
341
342
343
344
345
346
347
348
349efi_status_t efi_parse_options(char *cmdline)
350{
351 char *str;
352
353
354
355
356
357 str = strstr(cmdline, "efi=");
358 if (!str)
359 return EFI_SUCCESS;
360
361
362 str += strlen("efi=");
363
364
365
366
367
368 while (*str) {
369 if (!strncmp(str, "nochunk", 7)) {
370 str += strlen("nochunk");
371 __chunk_size = -1UL;
372 }
373
374
375 while (*str && *str != ',')
376 str++;
377
378 if (*str == ',')
379 str++;
380 }
381
382 return EFI_SUCCESS;
383}
384
385
386
387
388
389
390
391efi_status_t handle_cmdline_files(efi_system_table_t *sys_table_arg,
392 efi_loaded_image_t *image,
393 char *cmd_line, char *option_string,
394 unsigned long max_addr,
395 unsigned long *load_addr,
396 unsigned long *load_size)
397{
398 struct file_info *files;
399 unsigned long file_addr;
400 u64 file_size_total;
401 efi_file_handle_t *fh = NULL;
402 efi_status_t status;
403 int nr_files;
404 char *str;
405 int i, j, k;
406
407 file_addr = 0;
408 file_size_total = 0;
409
410 str = cmd_line;
411
412 j = 0;
413
414 if (!load_addr || !load_size)
415 return EFI_INVALID_PARAMETER;
416
417 *load_addr = 0;
418 *load_size = 0;
419
420 if (!str || !*str)
421 return EFI_SUCCESS;
422
423 for (nr_files = 0; *str; nr_files++) {
424 str = strstr(str, option_string);
425 if (!str)
426 break;
427
428 str += strlen(option_string);
429
430
431 while (*str == '/' || *str == '\\')
432 str++;
433
434 while (*str && *str != ' ' && *str != '\n')
435 str++;
436 }
437
438 if (!nr_files)
439 return EFI_SUCCESS;
440
441 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
442 nr_files * sizeof(*files), (void **)&files);
443 if (status != EFI_SUCCESS) {
444 pr_efi_err(sys_table_arg, "Failed to alloc mem for file handle list\n");
445 goto fail;
446 }
447
448 str = cmd_line;
449 for (i = 0; i < nr_files; i++) {
450 struct file_info *file;
451 efi_char16_t filename_16[256];
452 efi_char16_t *p;
453
454 str = strstr(str, option_string);
455 if (!str)
456 break;
457
458 str += strlen(option_string);
459
460 file = &files[i];
461 p = filename_16;
462
463
464 while (*str == '/' || *str == '\\')
465 str++;
466
467 while (*str && *str != ' ' && *str != '\n') {
468 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
469 break;
470
471 if (*str == '/') {
472 *p++ = '\\';
473 str++;
474 } else {
475 *p++ = *str++;
476 }
477 }
478
479 *p = '\0';
480
481
482 if (!i) {
483 status = efi_open_volume(sys_table_arg, image,
484 (void **)&fh);
485 if (status != EFI_SUCCESS)
486 goto free_files;
487 }
488
489 status = efi_file_size(sys_table_arg, fh, filename_16,
490 (void **)&file->handle, &file->size);
491 if (status != EFI_SUCCESS)
492 goto close_handles;
493
494 file_size_total += file->size;
495 }
496
497 if (file_size_total) {
498 unsigned long addr;
499
500
501
502
503
504
505 status = efi_high_alloc(sys_table_arg, file_size_total, 0x1000,
506 &file_addr, max_addr);
507 if (status != EFI_SUCCESS) {
508 pr_efi_err(sys_table_arg, "Failed to alloc highmem for files\n");
509 goto close_handles;
510 }
511
512
513 if (file_addr > max_addr) {
514 pr_efi_err(sys_table_arg, "We've run out of free low memory\n");
515 status = EFI_INVALID_PARAMETER;
516 goto free_file_total;
517 }
518
519 addr = file_addr;
520 for (j = 0; j < nr_files; j++) {
521 unsigned long size;
522
523 size = files[j].size;
524 while (size) {
525 unsigned long chunksize;
526 if (size > __chunk_size)
527 chunksize = __chunk_size;
528 else
529 chunksize = size;
530
531 status = efi_file_read(files[j].handle,
532 &chunksize,
533 (void *)addr);
534 if (status != EFI_SUCCESS) {
535 pr_efi_err(sys_table_arg, "Failed to read file\n");
536 goto free_file_total;
537 }
538 addr += chunksize;
539 size -= chunksize;
540 }
541
542 efi_file_close(files[j].handle);
543 }
544
545 }
546
547 efi_call_early(free_pool, files);
548
549 *load_addr = file_addr;
550 *load_size = file_size_total;
551
552 return status;
553
554free_file_total:
555 efi_free(sys_table_arg, file_size_total, file_addr);
556
557close_handles:
558 for (k = j; k < i; k++)
559 efi_file_close(files[k].handle);
560free_files:
561 efi_call_early(free_pool, files);
562fail:
563 *load_addr = 0;
564 *load_size = 0;
565
566 return status;
567}
568
569
570
571
572
573
574
575
576
577
578efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg,
579 unsigned long *image_addr,
580 unsigned long image_size,
581 unsigned long alloc_size,
582 unsigned long preferred_addr,
583 unsigned long alignment)
584{
585 unsigned long cur_image_addr;
586 unsigned long new_addr = 0;
587 efi_status_t status;
588 unsigned long nr_pages;
589 efi_physical_addr_t efi_addr = preferred_addr;
590
591 if (!image_addr || !image_size || !alloc_size)
592 return EFI_INVALID_PARAMETER;
593 if (alloc_size < image_size)
594 return EFI_INVALID_PARAMETER;
595
596 cur_image_addr = *image_addr;
597
598
599
600
601
602
603
604
605
606 nr_pages = round_up(alloc_size, EFI_ALLOC_ALIGN) / EFI_PAGE_SIZE;
607 status = efi_call_early(allocate_pages,
608 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
609 nr_pages, &efi_addr);
610 new_addr = efi_addr;
611
612
613
614
615 if (status != EFI_SUCCESS) {
616 status = efi_low_alloc(sys_table_arg, alloc_size, alignment,
617 &new_addr);
618 }
619 if (status != EFI_SUCCESS) {
620 pr_efi_err(sys_table_arg, "Failed to allocate usable memory for kernel.\n");
621 return status;
622 }
623
624
625
626
627
628 memcpy((void *)new_addr, (void *)cur_image_addr, image_size);
629
630
631 *image_addr = new_addr;
632
633 return status;
634}
635
636
637
638
639
640static int efi_utf8_bytes(u16 c)
641{
642 return 1 + (c >= 0x80) + (c >= 0x800);
643}
644
645
646
647
648static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n)
649{
650 unsigned int c;
651
652 while (n--) {
653 c = *src++;
654 if (n && c >= 0xd800 && c <= 0xdbff &&
655 *src >= 0xdc00 && *src <= 0xdfff) {
656 c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff);
657 src++;
658 n--;
659 }
660 if (c >= 0xd800 && c <= 0xdfff)
661 c = 0xfffd;
662 if (c < 0x80) {
663 *dst++ = c;
664 continue;
665 }
666 if (c < 0x800) {
667 *dst++ = 0xc0 + (c >> 6);
668 goto t1;
669 }
670 if (c < 0x10000) {
671 *dst++ = 0xe0 + (c >> 12);
672 goto t2;
673 }
674 *dst++ = 0xf0 + (c >> 18);
675 *dst++ = 0x80 + ((c >> 12) & 0x3f);
676 t2:
677 *dst++ = 0x80 + ((c >> 6) & 0x3f);
678 t1:
679 *dst++ = 0x80 + (c & 0x3f);
680 }
681
682 return dst;
683}
684
685#ifndef MAX_CMDLINE_ADDRESS
686#define MAX_CMDLINE_ADDRESS ULONG_MAX
687#endif
688
689
690
691
692
693
694char *efi_convert_cmdline(efi_system_table_t *sys_table_arg,
695 efi_loaded_image_t *image,
696 int *cmd_line_len)
697{
698 const u16 *s2;
699 u8 *s1 = NULL;
700 unsigned long cmdline_addr = 0;
701 int load_options_chars = image->load_options_size / 2;
702 const u16 *options = image->load_options;
703 int options_bytes = 0;
704 int options_chars = 0;
705 efi_status_t status;
706 u16 zero = 0;
707
708 if (options) {
709 s2 = options;
710 while (*s2 && *s2 != '\n'
711 && options_chars < load_options_chars) {
712 options_bytes += efi_utf8_bytes(*s2++);
713 options_chars++;
714 }
715 }
716
717 if (!options_chars) {
718
719 options = &zero;
720 }
721
722 options_bytes++;
723
724 status = efi_high_alloc(sys_table_arg, options_bytes, 0,
725 &cmdline_addr, MAX_CMDLINE_ADDRESS);
726 if (status != EFI_SUCCESS)
727 return NULL;
728
729 s1 = (u8 *)cmdline_addr;
730 s2 = (const u16 *)options;
731
732 s1 = efi_utf16_to_utf8(s1, s2, options_chars);
733 *s1 = '\0';
734
735 *cmd_line_len = options_bytes;
736 return (char *)cmdline_addr;
737}
738
739
740
741
742
743
744
745
746
747efi_status_t efi_exit_boot_services(efi_system_table_t *sys_table_arg,
748 void *handle,
749 struct efi_boot_memmap *map,
750 void *priv,
751 efi_exit_boot_map_processing priv_func)
752{
753 efi_status_t status;
754
755 status = efi_get_memory_map(sys_table_arg, map);
756
757 if (status != EFI_SUCCESS)
758 goto fail;
759
760 status = priv_func(sys_table_arg, map, priv);
761 if (status != EFI_SUCCESS)
762 goto free_map;
763
764 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
765
766 if (status == EFI_INVALID_PARAMETER) {
767
768
769
770
771
772
773
774
775
776
777
778
779
780 *map->map_size = *map->buff_size;
781 status = efi_call_early(get_memory_map,
782 map->map_size,
783 *map->map,
784 map->key_ptr,
785 map->desc_size,
786 map->desc_ver);
787
788
789 if (status != EFI_SUCCESS)
790 goto fail;
791
792 status = priv_func(sys_table_arg, map, priv);
793
794 if (status != EFI_SUCCESS)
795 goto fail;
796
797 status = efi_call_early(exit_boot_services, handle, *map->key_ptr);
798 }
799
800
801 if (status != EFI_SUCCESS)
802 goto fail;
803
804 return EFI_SUCCESS;
805
806free_map:
807 efi_call_early(free_pool, *map->map);
808fail:
809 return status;
810}
811