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