Merge remote-tracking branch 'asoc/topic/wm8750' into asoc-next
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / boot / compressed / eboot.c
1 /* -----------------------------------------------------------------------
2 *
3 * Copyright 2011 Intel Corporation; author Matt Fleming
4 *
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2.
7 *
8 * ----------------------------------------------------------------------- */
9
10 #include <linux/efi.h>
11 #include <asm/efi.h>
12 #include <asm/setup.h>
13 #include <asm/desc.h>
14
15 #undef memcpy /* Use memcpy from misc.c */
16
17 #include "eboot.h"
18
19 static efi_system_table_t *sys_table;
20
21 static void efi_printk(char *str)
22 {
23 char *s8;
24
25 for (s8 = str; *s8; s8++) {
26 struct efi_simple_text_output_protocol *out;
27 efi_char16_t ch[2] = { 0 };
28
29 ch[0] = *s8;
30 out = (struct efi_simple_text_output_protocol *)sys_table->con_out;
31
32 if (*s8 == '\n') {
33 efi_char16_t nl[2] = { '\r', 0 };
34 efi_call_phys2(out->output_string, out, nl);
35 }
36
37 efi_call_phys2(out->output_string, out, ch);
38 }
39 }
40
41 static efi_status_t __get_map(efi_memory_desc_t **map, unsigned long *map_size,
42 unsigned long *desc_size)
43 {
44 efi_memory_desc_t *m = NULL;
45 efi_status_t status;
46 unsigned long key;
47 u32 desc_version;
48
49 *map_size = sizeof(*m) * 32;
50 again:
51 /*
52 * Add an additional efi_memory_desc_t because we're doing an
53 * allocation which may be in a new descriptor region.
54 */
55 *map_size += sizeof(*m);
56 status = efi_call_phys3(sys_table->boottime->allocate_pool,
57 EFI_LOADER_DATA, *map_size, (void **)&m);
58 if (status != EFI_SUCCESS)
59 goto fail;
60
61 status = efi_call_phys5(sys_table->boottime->get_memory_map, map_size,
62 m, &key, desc_size, &desc_version);
63 if (status == EFI_BUFFER_TOO_SMALL) {
64 efi_call_phys1(sys_table->boottime->free_pool, m);
65 goto again;
66 }
67
68 if (status != EFI_SUCCESS)
69 efi_call_phys1(sys_table->boottime->free_pool, m);
70
71 fail:
72 *map = m;
73 return status;
74 }
75
76 /*
77 * Allocate at the highest possible address that is not above 'max'.
78 */
79 static efi_status_t high_alloc(unsigned long size, unsigned long align,
80 unsigned long *addr, unsigned long max)
81 {
82 unsigned long map_size, desc_size;
83 efi_memory_desc_t *map;
84 efi_status_t status;
85 unsigned long nr_pages;
86 u64 max_addr = 0;
87 int i;
88
89 status = __get_map(&map, &map_size, &desc_size);
90 if (status != EFI_SUCCESS)
91 goto fail;
92
93 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
94 again:
95 for (i = 0; i < map_size / desc_size; i++) {
96 efi_memory_desc_t *desc;
97 unsigned long m = (unsigned long)map;
98 u64 start, end;
99
100 desc = (efi_memory_desc_t *)(m + (i * desc_size));
101 if (desc->type != EFI_CONVENTIONAL_MEMORY)
102 continue;
103
104 if (desc->num_pages < nr_pages)
105 continue;
106
107 start = desc->phys_addr;
108 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
109
110 if ((start + size) > end || (start + size) > max)
111 continue;
112
113 if (end - size > max)
114 end = max;
115
116 if (round_down(end - size, align) < start)
117 continue;
118
119 start = round_down(end - size, align);
120
121 /*
122 * Don't allocate at 0x0. It will confuse code that
123 * checks pointers against NULL.
124 */
125 if (start == 0x0)
126 continue;
127
128 if (start > max_addr)
129 max_addr = start;
130 }
131
132 if (!max_addr)
133 status = EFI_NOT_FOUND;
134 else {
135 status = efi_call_phys4(sys_table->boottime->allocate_pages,
136 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
137 nr_pages, &max_addr);
138 if (status != EFI_SUCCESS) {
139 max = max_addr;
140 max_addr = 0;
141 goto again;
142 }
143
144 *addr = max_addr;
145 }
146
147 free_pool:
148 efi_call_phys1(sys_table->boottime->free_pool, map);
149
150 fail:
151 return status;
152 }
153
154 /*
155 * Allocate at the lowest possible address.
156 */
157 static efi_status_t low_alloc(unsigned long size, unsigned long align,
158 unsigned long *addr)
159 {
160 unsigned long map_size, desc_size;
161 efi_memory_desc_t *map;
162 efi_status_t status;
163 unsigned long nr_pages;
164 int i;
165
166 status = __get_map(&map, &map_size, &desc_size);
167 if (status != EFI_SUCCESS)
168 goto fail;
169
170 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
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
178 if (desc->type != EFI_CONVENTIONAL_MEMORY)
179 continue;
180
181 if (desc->num_pages < nr_pages)
182 continue;
183
184 start = desc->phys_addr;
185 end = start + desc->num_pages * (1UL << EFI_PAGE_SHIFT);
186
187 /*
188 * Don't allocate at 0x0. It will confuse code that
189 * checks pointers against NULL. Skip the first 8
190 * bytes so we start at a nice even number.
191 */
192 if (start == 0x0)
193 start += 8;
194
195 start = round_up(start, align);
196 if ((start + size) > end)
197 continue;
198
199 status = efi_call_phys4(sys_table->boottime->allocate_pages,
200 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
201 nr_pages, &start);
202 if (status == EFI_SUCCESS) {
203 *addr = start;
204 break;
205 }
206 }
207
208 if (i == map_size / desc_size)
209 status = EFI_NOT_FOUND;
210
211 free_pool:
212 efi_call_phys1(sys_table->boottime->free_pool, map);
213 fail:
214 return status;
215 }
216
217 static void low_free(unsigned long size, unsigned long addr)
218 {
219 unsigned long nr_pages;
220
221 nr_pages = round_up(size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
222 efi_call_phys2(sys_table->boottime->free_pages, addr, size);
223 }
224
225 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
226 {
227 u8 first, len;
228
229 first = 0;
230 len = 0;
231
232 if (mask) {
233 while (!(mask & 0x1)) {
234 mask = mask >> 1;
235 first++;
236 }
237
238 while (mask & 0x1) {
239 mask = mask >> 1;
240 len++;
241 }
242 }
243
244 *pos = first;
245 *size = len;
246 }
247
248 /*
249 * See if we have Graphics Output Protocol
250 */
251 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
252 unsigned long size)
253 {
254 struct efi_graphics_output_protocol *gop, *first_gop;
255 struct efi_pixel_bitmask pixel_info;
256 unsigned long nr_gops;
257 efi_status_t status;
258 void **gop_handle;
259 u16 width, height;
260 u32 fb_base, fb_size;
261 u32 pixels_per_scan_line;
262 int pixel_format;
263 int i;
264
265 status = efi_call_phys3(sys_table->boottime->allocate_pool,
266 EFI_LOADER_DATA, size, &gop_handle);
267 if (status != EFI_SUCCESS)
268 return status;
269
270 status = efi_call_phys5(sys_table->boottime->locate_handle,
271 EFI_LOCATE_BY_PROTOCOL, proto,
272 NULL, &size, gop_handle);
273 if (status != EFI_SUCCESS)
274 goto free_handle;
275
276 first_gop = NULL;
277
278 nr_gops = size / sizeof(void *);
279 for (i = 0; i < nr_gops; i++) {
280 struct efi_graphics_output_mode_info *info;
281 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
282 bool conout_found = false;
283 void *dummy;
284 void *h = gop_handle[i];
285
286 status = efi_call_phys3(sys_table->boottime->handle_protocol,
287 h, proto, &gop);
288 if (status != EFI_SUCCESS)
289 continue;
290
291 status = efi_call_phys3(sys_table->boottime->handle_protocol,
292 h, &conout_proto, &dummy);
293
294 if (status == EFI_SUCCESS)
295 conout_found = true;
296
297 status = efi_call_phys4(gop->query_mode, gop,
298 gop->mode->mode, &size, &info);
299 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
300 /*
301 * Systems that use the UEFI Console Splitter may
302 * provide multiple GOP devices, not all of which are
303 * backed by real hardware. The workaround is to search
304 * for a GOP implementing the ConOut protocol, and if
305 * one isn't found, to just fall back to the first GOP.
306 */
307 width = info->horizontal_resolution;
308 height = info->vertical_resolution;
309 fb_base = gop->mode->frame_buffer_base;
310 fb_size = gop->mode->frame_buffer_size;
311 pixel_format = info->pixel_format;
312 pixel_info = info->pixel_information;
313 pixels_per_scan_line = info->pixels_per_scan_line;
314
315 /*
316 * Once we've found a GOP supporting ConOut,
317 * don't bother looking any further.
318 */
319 if (conout_found)
320 break;
321
322 first_gop = gop;
323 }
324 }
325
326 /* Did we find any GOPs? */
327 if (!first_gop)
328 goto free_handle;
329
330 /* EFI framebuffer */
331 si->orig_video_isVGA = VIDEO_TYPE_EFI;
332
333 si->lfb_width = width;
334 si->lfb_height = height;
335 si->lfb_base = fb_base;
336 si->pages = 1;
337
338 if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
339 si->lfb_depth = 32;
340 si->lfb_linelength = pixels_per_scan_line * 4;
341 si->red_size = 8;
342 si->red_pos = 0;
343 si->green_size = 8;
344 si->green_pos = 8;
345 si->blue_size = 8;
346 si->blue_pos = 16;
347 si->rsvd_size = 8;
348 si->rsvd_pos = 24;
349 } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
350 si->lfb_depth = 32;
351 si->lfb_linelength = pixels_per_scan_line * 4;
352 si->red_size = 8;
353 si->red_pos = 16;
354 si->green_size = 8;
355 si->green_pos = 8;
356 si->blue_size = 8;
357 si->blue_pos = 0;
358 si->rsvd_size = 8;
359 si->rsvd_pos = 24;
360 } else if (pixel_format == PIXEL_BIT_MASK) {
361 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
362 find_bits(pixel_info.green_mask, &si->green_pos,
363 &si->green_size);
364 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
365 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
366 &si->rsvd_size);
367 si->lfb_depth = si->red_size + si->green_size +
368 si->blue_size + si->rsvd_size;
369 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
370 } else {
371 si->lfb_depth = 4;
372 si->lfb_linelength = si->lfb_width / 2;
373 si->red_size = 0;
374 si->red_pos = 0;
375 si->green_size = 0;
376 si->green_pos = 0;
377 si->blue_size = 0;
378 si->blue_pos = 0;
379 si->rsvd_size = 0;
380 si->rsvd_pos = 0;
381 }
382
383 si->lfb_size = si->lfb_linelength * si->lfb_height;
384
385 si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
386
387 free_handle:
388 efi_call_phys1(sys_table->boottime->free_pool, gop_handle);
389 return status;
390 }
391
392 /*
393 * See if we have Universal Graphics Adapter (UGA) protocol
394 */
395 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
396 unsigned long size)
397 {
398 struct efi_uga_draw_protocol *uga, *first_uga;
399 unsigned long nr_ugas;
400 efi_status_t status;
401 u32 width, height;
402 void **uga_handle = NULL;
403 int i;
404
405 status = efi_call_phys3(sys_table->boottime->allocate_pool,
406 EFI_LOADER_DATA, size, &uga_handle);
407 if (status != EFI_SUCCESS)
408 return status;
409
410 status = efi_call_phys5(sys_table->boottime->locate_handle,
411 EFI_LOCATE_BY_PROTOCOL, uga_proto,
412 NULL, &size, uga_handle);
413 if (status != EFI_SUCCESS)
414 goto free_handle;
415
416 first_uga = NULL;
417
418 nr_ugas = size / sizeof(void *);
419 for (i = 0; i < nr_ugas; i++) {
420 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
421 void *handle = uga_handle[i];
422 u32 w, h, depth, refresh;
423 void *pciio;
424
425 status = efi_call_phys3(sys_table->boottime->handle_protocol,
426 handle, uga_proto, &uga);
427 if (status != EFI_SUCCESS)
428 continue;
429
430 efi_call_phys3(sys_table->boottime->handle_protocol,
431 handle, &pciio_proto, &pciio);
432
433 status = efi_call_phys5(uga->get_mode, uga, &w, &h,
434 &depth, &refresh);
435 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
436 width = w;
437 height = h;
438
439 /*
440 * Once we've found a UGA supporting PCIIO,
441 * don't bother looking any further.
442 */
443 if (pciio)
444 break;
445
446 first_uga = uga;
447 }
448 }
449
450 if (!first_uga)
451 goto free_handle;
452
453 /* EFI framebuffer */
454 si->orig_video_isVGA = VIDEO_TYPE_EFI;
455
456 si->lfb_depth = 32;
457 si->lfb_width = width;
458 si->lfb_height = height;
459
460 si->red_size = 8;
461 si->red_pos = 16;
462 si->green_size = 8;
463 si->green_pos = 8;
464 si->blue_size = 8;
465 si->blue_pos = 0;
466 si->rsvd_size = 8;
467 si->rsvd_pos = 24;
468
469
470 free_handle:
471 efi_call_phys1(sys_table->boottime->free_pool, uga_handle);
472 return status;
473 }
474
475 void setup_graphics(struct boot_params *boot_params)
476 {
477 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
478 struct screen_info *si;
479 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
480 efi_status_t status;
481 unsigned long size;
482 void **gop_handle = NULL;
483 void **uga_handle = NULL;
484
485 si = &boot_params->screen_info;
486 memset(si, 0, sizeof(*si));
487
488 size = 0;
489 status = efi_call_phys5(sys_table->boottime->locate_handle,
490 EFI_LOCATE_BY_PROTOCOL, &graphics_proto,
491 NULL, &size, gop_handle);
492 if (status == EFI_BUFFER_TOO_SMALL)
493 status = setup_gop(si, &graphics_proto, size);
494
495 if (status != EFI_SUCCESS) {
496 size = 0;
497 status = efi_call_phys5(sys_table->boottime->locate_handle,
498 EFI_LOCATE_BY_PROTOCOL, &uga_proto,
499 NULL, &size, uga_handle);
500 if (status == EFI_BUFFER_TOO_SMALL)
501 setup_uga(si, &uga_proto, size);
502 }
503 }
504
505 struct initrd {
506 efi_file_handle_t *handle;
507 u64 size;
508 };
509
510 /*
511 * Check the cmdline for a LILO-style initrd= arguments.
512 *
513 * We only support loading an initrd from the same filesystem as the
514 * kernel image.
515 */
516 static efi_status_t handle_ramdisks(efi_loaded_image_t *image,
517 struct setup_header *hdr)
518 {
519 struct initrd *initrds;
520 unsigned long initrd_addr;
521 efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
522 u64 initrd_total;
523 efi_file_io_interface_t *io;
524 efi_file_handle_t *fh;
525 efi_status_t status;
526 int nr_initrds;
527 char *str;
528 int i, j, k;
529
530 initrd_addr = 0;
531 initrd_total = 0;
532
533 str = (char *)(unsigned long)hdr->cmd_line_ptr;
534
535 j = 0; /* See close_handles */
536
537 if (!str || !*str)
538 return EFI_SUCCESS;
539
540 for (nr_initrds = 0; *str; nr_initrds++) {
541 str = strstr(str, "initrd=");
542 if (!str)
543 break;
544
545 str += 7;
546
547 /* Skip any leading slashes */
548 while (*str == '/' || *str == '\\')
549 str++;
550
551 while (*str && *str != ' ' && *str != '\n')
552 str++;
553 }
554
555 if (!nr_initrds)
556 return EFI_SUCCESS;
557
558 status = efi_call_phys3(sys_table->boottime->allocate_pool,
559 EFI_LOADER_DATA,
560 nr_initrds * sizeof(*initrds),
561 &initrds);
562 if (status != EFI_SUCCESS) {
563 efi_printk("Failed to alloc mem for initrds\n");
564 goto fail;
565 }
566
567 str = (char *)(unsigned long)hdr->cmd_line_ptr;
568 for (i = 0; i < nr_initrds; i++) {
569 struct initrd *initrd;
570 efi_file_handle_t *h;
571 efi_file_info_t *info;
572 efi_char16_t filename_16[256];
573 unsigned long info_sz;
574 efi_guid_t info_guid = EFI_FILE_INFO_ID;
575 efi_char16_t *p;
576 u64 file_sz;
577
578 str = strstr(str, "initrd=");
579 if (!str)
580 break;
581
582 str += 7;
583
584 initrd = &initrds[i];
585 p = filename_16;
586
587 /* Skip any leading slashes */
588 while (*str == '/' || *str == '\\')
589 str++;
590
591 while (*str && *str != ' ' && *str != '\n') {
592 if ((u8 *)p >= (u8 *)filename_16 + sizeof(filename_16))
593 break;
594
595 *p++ = *str++;
596 }
597
598 *p = '\0';
599
600 /* Only open the volume once. */
601 if (!i) {
602 efi_boot_services_t *boottime;
603
604 boottime = sys_table->boottime;
605
606 status = efi_call_phys3(boottime->handle_protocol,
607 image->device_handle, &fs_proto, &io);
608 if (status != EFI_SUCCESS) {
609 efi_printk("Failed to handle fs_proto\n");
610 goto free_initrds;
611 }
612
613 status = efi_call_phys2(io->open_volume, io, &fh);
614 if (status != EFI_SUCCESS) {
615 efi_printk("Failed to open volume\n");
616 goto free_initrds;
617 }
618 }
619
620 status = efi_call_phys5(fh->open, fh, &h, filename_16,
621 EFI_FILE_MODE_READ, (u64)0);
622 if (status != EFI_SUCCESS) {
623 efi_printk("Failed to open initrd file\n");
624 goto close_handles;
625 }
626
627 initrd->handle = h;
628
629 info_sz = 0;
630 status = efi_call_phys4(h->get_info, h, &info_guid,
631 &info_sz, NULL);
632 if (status != EFI_BUFFER_TOO_SMALL) {
633 efi_printk("Failed to get initrd info size\n");
634 goto close_handles;
635 }
636
637 grow:
638 status = efi_call_phys3(sys_table->boottime->allocate_pool,
639 EFI_LOADER_DATA, info_sz, &info);
640 if (status != EFI_SUCCESS) {
641 efi_printk("Failed to alloc mem for initrd info\n");
642 goto close_handles;
643 }
644
645 status = efi_call_phys4(h->get_info, h, &info_guid,
646 &info_sz, info);
647 if (status == EFI_BUFFER_TOO_SMALL) {
648 efi_call_phys1(sys_table->boottime->free_pool, info);
649 goto grow;
650 }
651
652 file_sz = info->file_size;
653 efi_call_phys1(sys_table->boottime->free_pool, info);
654
655 if (status != EFI_SUCCESS) {
656 efi_printk("Failed to get initrd info\n");
657 goto close_handles;
658 }
659
660 initrd->size = file_sz;
661 initrd_total += file_sz;
662 }
663
664 if (initrd_total) {
665 unsigned long addr;
666
667 /*
668 * Multiple initrd's need to be at consecutive
669 * addresses in memory, so allocate enough memory for
670 * all the initrd's.
671 */
672 status = high_alloc(initrd_total, 0x1000,
673 &initrd_addr, hdr->initrd_addr_max);
674 if (status != EFI_SUCCESS) {
675 efi_printk("Failed to alloc highmem for initrds\n");
676 goto close_handles;
677 }
678
679 /* We've run out of free low memory. */
680 if (initrd_addr > hdr->initrd_addr_max) {
681 efi_printk("We've run out of free low memory\n");
682 status = EFI_INVALID_PARAMETER;
683 goto free_initrd_total;
684 }
685
686 addr = initrd_addr;
687 for (j = 0; j < nr_initrds; j++) {
688 u64 size;
689
690 size = initrds[j].size;
691 while (size) {
692 u64 chunksize;
693 if (size > EFI_READ_CHUNK_SIZE)
694 chunksize = EFI_READ_CHUNK_SIZE;
695 else
696 chunksize = size;
697 status = efi_call_phys3(fh->read,
698 initrds[j].handle,
699 &chunksize, addr);
700 if (status != EFI_SUCCESS) {
701 efi_printk("Failed to read initrd\n");
702 goto free_initrd_total;
703 }
704 addr += chunksize;
705 size -= chunksize;
706 }
707
708 efi_call_phys1(fh->close, initrds[j].handle);
709 }
710
711 }
712
713 efi_call_phys1(sys_table->boottime->free_pool, initrds);
714
715 hdr->ramdisk_image = initrd_addr;
716 hdr->ramdisk_size = initrd_total;
717
718 return status;
719
720 free_initrd_total:
721 low_free(initrd_total, initrd_addr);
722
723 close_handles:
724 for (k = j; k < i; k++)
725 efi_call_phys1(fh->close, initrds[k].handle);
726 free_initrds:
727 efi_call_phys1(sys_table->boottime->free_pool, initrds);
728 fail:
729 hdr->ramdisk_image = 0;
730 hdr->ramdisk_size = 0;
731
732 return status;
733 }
734
735 /*
736 * Because the x86 boot code expects to be passed a boot_params we
737 * need to create one ourselves (usually the bootloader would create
738 * one for us).
739 */
740 struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table)
741 {
742 struct boot_params *boot_params;
743 struct sys_desc_table *sdt;
744 struct apm_bios_info *bi;
745 struct setup_header *hdr;
746 struct efi_info *efi;
747 efi_loaded_image_t *image;
748 void *options;
749 u32 load_options_size;
750 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
751 int options_size = 0;
752 efi_status_t status;
753 unsigned long cmdline;
754 u16 *s2;
755 u8 *s1;
756 int i;
757
758 sys_table = _table;
759
760 /* Check if we were booted by the EFI firmware */
761 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
762 return NULL;
763
764 status = efi_call_phys3(sys_table->boottime->handle_protocol,
765 handle, &proto, (void *)&image);
766 if (status != EFI_SUCCESS) {
767 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
768 return NULL;
769 }
770
771 status = low_alloc(0x4000, 1, (unsigned long *)&boot_params);
772 if (status != EFI_SUCCESS) {
773 efi_printk("Failed to alloc lowmem for boot params\n");
774 return NULL;
775 }
776
777 memset(boot_params, 0x0, 0x4000);
778
779 hdr = &boot_params->hdr;
780 efi = &boot_params->efi_info;
781 bi = &boot_params->apm_bios_info;
782 sdt = &boot_params->sys_desc_table;
783
784 /* Copy the second sector to boot_params */
785 memcpy(&hdr->jump, image->image_base + 512, 512);
786
787 /*
788 * Fill out some of the header fields ourselves because the
789 * EFI firmware loader doesn't load the first sector.
790 */
791 hdr->root_flags = 1;
792 hdr->vid_mode = 0xffff;
793 hdr->boot_flag = 0xAA55;
794
795 hdr->code32_start = (__u64)(unsigned long)image->image_base;
796
797 hdr->type_of_loader = 0x21;
798
799 /* Convert unicode cmdline to ascii */
800 options = image->load_options;
801 load_options_size = image->load_options_size / 2; /* ASCII */
802 cmdline = 0;
803 s2 = (u16 *)options;
804
805 if (s2) {
806 while (*s2 && *s2 != '\n' && options_size < load_options_size) {
807 s2++;
808 options_size++;
809 }
810
811 if (options_size) {
812 if (options_size > hdr->cmdline_size)
813 options_size = hdr->cmdline_size;
814
815 options_size++; /* NUL termination */
816
817 status = low_alloc(options_size, 1, &cmdline);
818 if (status != EFI_SUCCESS) {
819 efi_printk("Failed to alloc mem for cmdline\n");
820 goto fail;
821 }
822
823 s1 = (u8 *)(unsigned long)cmdline;
824 s2 = (u16 *)options;
825
826 for (i = 0; i < options_size - 1; i++)
827 *s1++ = *s2++;
828
829 *s1 = '\0';
830 }
831 }
832
833 hdr->cmd_line_ptr = cmdline;
834
835 hdr->ramdisk_image = 0;
836 hdr->ramdisk_size = 0;
837
838 /* Clear APM BIOS info */
839 memset(bi, 0, sizeof(*bi));
840
841 memset(sdt, 0, sizeof(*sdt));
842
843 status = handle_ramdisks(image, hdr);
844 if (status != EFI_SUCCESS)
845 goto fail2;
846
847 return boot_params;
848 fail2:
849 if (options_size)
850 low_free(options_size, hdr->cmd_line_ptr);
851 fail:
852 low_free(0x4000, (unsigned long)boot_params);
853 return NULL;
854 }
855
856 static efi_status_t exit_boot(struct boot_params *boot_params,
857 void *handle)
858 {
859 struct efi_info *efi = &boot_params->efi_info;
860 struct e820entry *e820_map = &boot_params->e820_map[0];
861 struct e820entry *prev = NULL;
862 unsigned long size, key, desc_size, _size;
863 efi_memory_desc_t *mem_map;
864 efi_status_t status;
865 __u32 desc_version;
866 u8 nr_entries;
867 int i;
868
869 size = sizeof(*mem_map) * 32;
870
871 again:
872 size += sizeof(*mem_map);
873 _size = size;
874 status = low_alloc(size, 1, (unsigned long *)&mem_map);
875 if (status != EFI_SUCCESS)
876 return status;
877
878 status = efi_call_phys5(sys_table->boottime->get_memory_map, &size,
879 mem_map, &key, &desc_size, &desc_version);
880 if (status == EFI_BUFFER_TOO_SMALL) {
881 low_free(_size, (unsigned long)mem_map);
882 goto again;
883 }
884
885 if (status != EFI_SUCCESS)
886 goto free_mem_map;
887
888 memcpy(&efi->efi_loader_signature, EFI_LOADER_SIGNATURE, sizeof(__u32));
889 efi->efi_systab = (unsigned long)sys_table;
890 efi->efi_memdesc_size = desc_size;
891 efi->efi_memdesc_version = desc_version;
892 efi->efi_memmap = (unsigned long)mem_map;
893 efi->efi_memmap_size = size;
894
895 #ifdef CONFIG_X86_64
896 efi->efi_systab_hi = (unsigned long)sys_table >> 32;
897 efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
898 #endif
899
900 /* Might as well exit boot services now */
901 status = efi_call_phys2(sys_table->boottime->exit_boot_services,
902 handle, key);
903 if (status != EFI_SUCCESS)
904 goto free_mem_map;
905
906 /* Historic? */
907 boot_params->alt_mem_k = 32 * 1024;
908
909 /*
910 * Convert the EFI memory map to E820.
911 */
912 nr_entries = 0;
913 for (i = 0; i < size / desc_size; i++) {
914 efi_memory_desc_t *d;
915 unsigned int e820_type = 0;
916 unsigned long m = (unsigned long)mem_map;
917
918 d = (efi_memory_desc_t *)(m + (i * desc_size));
919 switch (d->type) {
920 case EFI_RESERVED_TYPE:
921 case EFI_RUNTIME_SERVICES_CODE:
922 case EFI_RUNTIME_SERVICES_DATA:
923 case EFI_MEMORY_MAPPED_IO:
924 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
925 case EFI_PAL_CODE:
926 e820_type = E820_RESERVED;
927 break;
928
929 case EFI_UNUSABLE_MEMORY:
930 e820_type = E820_UNUSABLE;
931 break;
932
933 case EFI_ACPI_RECLAIM_MEMORY:
934 e820_type = E820_ACPI;
935 break;
936
937 case EFI_LOADER_CODE:
938 case EFI_LOADER_DATA:
939 case EFI_BOOT_SERVICES_CODE:
940 case EFI_BOOT_SERVICES_DATA:
941 case EFI_CONVENTIONAL_MEMORY:
942 e820_type = E820_RAM;
943 break;
944
945 case EFI_ACPI_MEMORY_NVS:
946 e820_type = E820_NVS;
947 break;
948
949 default:
950 continue;
951 }
952
953 /* Merge adjacent mappings */
954 if (prev && prev->type == e820_type &&
955 (prev->addr + prev->size) == d->phys_addr)
956 prev->size += d->num_pages << 12;
957 else {
958 e820_map->addr = d->phys_addr;
959 e820_map->size = d->num_pages << 12;
960 e820_map->type = e820_type;
961 prev = e820_map++;
962 nr_entries++;
963 }
964 }
965
966 boot_params->e820_entries = nr_entries;
967
968 return EFI_SUCCESS;
969
970 free_mem_map:
971 low_free(_size, (unsigned long)mem_map);
972 return status;
973 }
974
975 static efi_status_t relocate_kernel(struct setup_header *hdr)
976 {
977 unsigned long start, nr_pages;
978 efi_status_t status;
979
980 /*
981 * The EFI firmware loader could have placed the kernel image
982 * anywhere in memory, but the kernel has various restrictions
983 * on the max physical address it can run at. Attempt to move
984 * the kernel to boot_params.pref_address, or as low as
985 * possible.
986 */
987 start = hdr->pref_address;
988 nr_pages = round_up(hdr->init_size, EFI_PAGE_SIZE) / EFI_PAGE_SIZE;
989
990 status = efi_call_phys4(sys_table->boottime->allocate_pages,
991 EFI_ALLOCATE_ADDRESS, EFI_LOADER_DATA,
992 nr_pages, &start);
993 if (status != EFI_SUCCESS) {
994 status = low_alloc(hdr->init_size, hdr->kernel_alignment,
995 &start);
996 if (status != EFI_SUCCESS)
997 efi_printk("Failed to alloc mem for kernel\n");
998 }
999
1000 if (status == EFI_SUCCESS)
1001 memcpy((void *)start, (void *)(unsigned long)hdr->code32_start,
1002 hdr->init_size);
1003
1004 hdr->pref_address = hdr->code32_start;
1005 hdr->code32_start = (__u32)start;
1006
1007 return status;
1008 }
1009
1010 /*
1011 * On success we return a pointer to a boot_params structure, and NULL
1012 * on failure.
1013 */
1014 struct boot_params *efi_main(void *handle, efi_system_table_t *_table,
1015 struct boot_params *boot_params)
1016 {
1017 struct desc_ptr *gdt, *idt;
1018 efi_loaded_image_t *image;
1019 struct setup_header *hdr = &boot_params->hdr;
1020 efi_status_t status;
1021 struct desc_struct *desc;
1022
1023 sys_table = _table;
1024
1025 /* Check if we were booted by the EFI firmware */
1026 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1027 goto fail;
1028
1029 setup_graphics(boot_params);
1030
1031 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1032 EFI_LOADER_DATA, sizeof(*gdt),
1033 (void **)&gdt);
1034 if (status != EFI_SUCCESS) {
1035 efi_printk("Failed to alloc mem for gdt structure\n");
1036 goto fail;
1037 }
1038
1039 gdt->size = 0x800;
1040 status = low_alloc(gdt->size, 8, (unsigned long *)&gdt->address);
1041 if (status != EFI_SUCCESS) {
1042 efi_printk("Failed to alloc mem for gdt\n");
1043 goto fail;
1044 }
1045
1046 status = efi_call_phys3(sys_table->boottime->allocate_pool,
1047 EFI_LOADER_DATA, sizeof(*idt),
1048 (void **)&idt);
1049 if (status != EFI_SUCCESS) {
1050 efi_printk("Failed to alloc mem for idt structure\n");
1051 goto fail;
1052 }
1053
1054 idt->size = 0;
1055 idt->address = 0;
1056
1057 /*
1058 * If the kernel isn't already loaded at the preferred load
1059 * address, relocate it.
1060 */
1061 if (hdr->pref_address != hdr->code32_start) {
1062 status = relocate_kernel(hdr);
1063
1064 if (status != EFI_SUCCESS)
1065 goto fail;
1066 }
1067
1068 status = exit_boot(boot_params, handle);
1069 if (status != EFI_SUCCESS)
1070 goto fail;
1071
1072 memset((char *)gdt->address, 0x0, gdt->size);
1073 desc = (struct desc_struct *)gdt->address;
1074
1075 /* The first GDT is a dummy and the second is unused. */
1076 desc += 2;
1077
1078 desc->limit0 = 0xffff;
1079 desc->base0 = 0x0000;
1080 desc->base1 = 0x0000;
1081 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1082 desc->s = DESC_TYPE_CODE_DATA;
1083 desc->dpl = 0;
1084 desc->p = 1;
1085 desc->limit = 0xf;
1086 desc->avl = 0;
1087 desc->l = 0;
1088 desc->d = SEG_OP_SIZE_32BIT;
1089 desc->g = SEG_GRANULARITY_4KB;
1090 desc->base2 = 0x00;
1091
1092 desc++;
1093 desc->limit0 = 0xffff;
1094 desc->base0 = 0x0000;
1095 desc->base1 = 0x0000;
1096 desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1097 desc->s = DESC_TYPE_CODE_DATA;
1098 desc->dpl = 0;
1099 desc->p = 1;
1100 desc->limit = 0xf;
1101 desc->avl = 0;
1102 desc->l = 0;
1103 desc->d = SEG_OP_SIZE_32BIT;
1104 desc->g = SEG_GRANULARITY_4KB;
1105 desc->base2 = 0x00;
1106
1107 #ifdef CONFIG_X86_64
1108 /* Task segment value */
1109 desc++;
1110 desc->limit0 = 0x0000;
1111 desc->base0 = 0x0000;
1112 desc->base1 = 0x0000;
1113 desc->type = SEG_TYPE_TSS;
1114 desc->s = 0;
1115 desc->dpl = 0;
1116 desc->p = 1;
1117 desc->limit = 0x0;
1118 desc->avl = 0;
1119 desc->l = 0;
1120 desc->d = 0;
1121 desc->g = SEG_GRANULARITY_4KB;
1122 desc->base2 = 0x00;
1123 #endif /* CONFIG_X86_64 */
1124
1125 asm volatile ("lidt %0" : : "m" (*idt));
1126 asm volatile ("lgdt %0" : : "m" (*gdt));
1127
1128 asm volatile("cli");
1129
1130 return boot_params;
1131 fail:
1132 return NULL;
1133 }