Merge tag 'stable/for-linus-3.18-b-rc1-tag' of git://git.kernel.org/pub/scm/linux...
[cascardo/linux.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 struct efi_config *efi_early;
23
24 #define efi_call_early(f, ...)                                          \
25         efi_early->call(efi_early->f, __VA_ARGS__);
26
27 #define BOOT_SERVICES(bits)                                             \
28 static void setup_boot_services##bits(struct efi_config *c)             \
29 {                                                                       \
30         efi_system_table_##bits##_t *table;                             \
31         efi_boot_services_##bits##_t *bt;                               \
32                                                                         \
33         table = (typeof(table))sys_table;                               \
34                                                                         \
35         c->text_output = table->con_out;                                \
36                                                                         \
37         bt = (typeof(bt))(unsigned long)(table->boottime);              \
38                                                                         \
39         c->allocate_pool = bt->allocate_pool;                           \
40         c->allocate_pages = bt->allocate_pages;                         \
41         c->get_memory_map = bt->get_memory_map;                         \
42         c->free_pool = bt->free_pool;                                   \
43         c->free_pages = bt->free_pages;                                 \
44         c->locate_handle = bt->locate_handle;                           \
45         c->handle_protocol = bt->handle_protocol;                       \
46         c->exit_boot_services = bt->exit_boot_services;                 \
47 }
48 BOOT_SERVICES(32);
49 BOOT_SERVICES(64);
50
51 void efi_char16_printk(efi_system_table_t *, efi_char16_t *);
52
53 static efi_status_t
54 __file_size32(void *__fh, efi_char16_t *filename_16,
55               void **handle, u64 *file_sz)
56 {
57         efi_file_handle_32_t *h, *fh = __fh;
58         efi_file_info_t *info;
59         efi_status_t status;
60         efi_guid_t info_guid = EFI_FILE_INFO_ID;
61         u32 info_sz;
62
63         status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
64                                  EFI_FILE_MODE_READ, (u64)0);
65         if (status != EFI_SUCCESS) {
66                 efi_printk(sys_table, "Failed to open file: ");
67                 efi_char16_printk(sys_table, filename_16);
68                 efi_printk(sys_table, "\n");
69                 return status;
70         }
71
72         *handle = h;
73
74         info_sz = 0;
75         status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
76                                  &info_sz, NULL);
77         if (status != EFI_BUFFER_TOO_SMALL) {
78                 efi_printk(sys_table, "Failed to get file info size\n");
79                 return status;
80         }
81
82 grow:
83         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
84                                 info_sz, (void **)&info);
85         if (status != EFI_SUCCESS) {
86                 efi_printk(sys_table, "Failed to alloc mem for file info\n");
87                 return status;
88         }
89
90         status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
91                                  &info_sz, info);
92         if (status == EFI_BUFFER_TOO_SMALL) {
93                 efi_call_early(free_pool, info);
94                 goto grow;
95         }
96
97         *file_sz = info->file_size;
98         efi_call_early(free_pool, info);
99
100         if (status != EFI_SUCCESS)
101                 efi_printk(sys_table, "Failed to get initrd info\n");
102
103         return status;
104 }
105
106 static efi_status_t
107 __file_size64(void *__fh, efi_char16_t *filename_16,
108               void **handle, u64 *file_sz)
109 {
110         efi_file_handle_64_t *h, *fh = __fh;
111         efi_file_info_t *info;
112         efi_status_t status;
113         efi_guid_t info_guid = EFI_FILE_INFO_ID;
114         u64 info_sz;
115
116         status = efi_early->call((unsigned long)fh->open, fh, &h, filename_16,
117                                  EFI_FILE_MODE_READ, (u64)0);
118         if (status != EFI_SUCCESS) {
119                 efi_printk(sys_table, "Failed to open file: ");
120                 efi_char16_printk(sys_table, filename_16);
121                 efi_printk(sys_table, "\n");
122                 return status;
123         }
124
125         *handle = h;
126
127         info_sz = 0;
128         status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
129                                  &info_sz, NULL);
130         if (status != EFI_BUFFER_TOO_SMALL) {
131                 efi_printk(sys_table, "Failed to get file info size\n");
132                 return status;
133         }
134
135 grow:
136         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
137                                 info_sz, (void **)&info);
138         if (status != EFI_SUCCESS) {
139                 efi_printk(sys_table, "Failed to alloc mem for file info\n");
140                 return status;
141         }
142
143         status = efi_early->call((unsigned long)h->get_info, h, &info_guid,
144                                  &info_sz, info);
145         if (status == EFI_BUFFER_TOO_SMALL) {
146                 efi_call_early(free_pool, info);
147                 goto grow;
148         }
149
150         *file_sz = info->file_size;
151         efi_call_early(free_pool, info);
152
153         if (status != EFI_SUCCESS)
154                 efi_printk(sys_table, "Failed to get initrd info\n");
155
156         return status;
157 }
158 efi_status_t
159 efi_file_size(efi_system_table_t *sys_table, void *__fh,
160               efi_char16_t *filename_16, void **handle, u64 *file_sz)
161 {
162         if (efi_early->is64)
163                 return __file_size64(__fh, filename_16, handle, file_sz);
164
165         return __file_size32(__fh, filename_16, handle, file_sz);
166 }
167
168 efi_status_t
169 efi_file_read(void *handle, unsigned long *size, void *addr)
170 {
171         unsigned long func;
172
173         if (efi_early->is64) {
174                 efi_file_handle_64_t *fh = handle;
175
176                 func = (unsigned long)fh->read;
177                 return efi_early->call(func, handle, size, addr);
178         } else {
179                 efi_file_handle_32_t *fh = handle;
180
181                 func = (unsigned long)fh->read;
182                 return efi_early->call(func, handle, size, addr);
183         }
184 }
185
186 efi_status_t efi_file_close(void *handle)
187 {
188         if (efi_early->is64) {
189                 efi_file_handle_64_t *fh = handle;
190
191                 return efi_early->call((unsigned long)fh->close, handle);
192         } else {
193                 efi_file_handle_32_t *fh = handle;
194
195                 return efi_early->call((unsigned long)fh->close, handle);
196         }
197 }
198
199 static inline efi_status_t __open_volume32(void *__image, void **__fh)
200 {
201         efi_file_io_interface_t *io;
202         efi_loaded_image_32_t *image = __image;
203         efi_file_handle_32_t *fh;
204         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
205         efi_status_t status;
206         void *handle = (void *)(unsigned long)image->device_handle;
207         unsigned long func;
208
209         status = efi_call_early(handle_protocol, handle,
210                                 &fs_proto, (void **)&io);
211         if (status != EFI_SUCCESS) {
212                 efi_printk(sys_table, "Failed to handle fs_proto\n");
213                 return status;
214         }
215
216         func = (unsigned long)io->open_volume;
217         status = efi_early->call(func, io, &fh);
218         if (status != EFI_SUCCESS)
219                 efi_printk(sys_table, "Failed to open volume\n");
220
221         *__fh = fh;
222         return status;
223 }
224
225 static inline efi_status_t __open_volume64(void *__image, void **__fh)
226 {
227         efi_file_io_interface_t *io;
228         efi_loaded_image_64_t *image = __image;
229         efi_file_handle_64_t *fh;
230         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
231         efi_status_t status;
232         void *handle = (void *)(unsigned long)image->device_handle;
233         unsigned long func;
234
235         status = efi_call_early(handle_protocol, handle,
236                                 &fs_proto, (void **)&io);
237         if (status != EFI_SUCCESS) {
238                 efi_printk(sys_table, "Failed to handle fs_proto\n");
239                 return status;
240         }
241
242         func = (unsigned long)io->open_volume;
243         status = efi_early->call(func, io, &fh);
244         if (status != EFI_SUCCESS)
245                 efi_printk(sys_table, "Failed to open volume\n");
246
247         *__fh = fh;
248         return status;
249 }
250
251 efi_status_t
252 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
253 {
254         if (efi_early->is64)
255                 return __open_volume64(__image, __fh);
256
257         return __open_volume32(__image, __fh);
258 }
259
260 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
261 {
262         unsigned long output_string;
263         size_t offset;
264
265         if (efi_early->is64) {
266                 struct efi_simple_text_output_protocol_64 *out;
267                 u64 *func;
268
269                 offset = offsetof(typeof(*out), output_string);
270                 output_string = efi_early->text_output + offset;
271                 out = (typeof(out))(unsigned long)efi_early->text_output;
272                 func = (u64 *)output_string;
273
274                 efi_early->call(*func, out, str);
275         } else {
276                 struct efi_simple_text_output_protocol_32 *out;
277                 u32 *func;
278
279                 offset = offsetof(typeof(*out), output_string);
280                 output_string = efi_early->text_output + offset;
281                 out = (typeof(out))(unsigned long)efi_early->text_output;
282                 func = (u32 *)output_string;
283
284                 efi_early->call(*func, out, str);
285         }
286 }
287
288 #include "../../../../drivers/firmware/efi/libstub/efi-stub-helper.c"
289
290 static void find_bits(unsigned long mask, u8 *pos, u8 *size)
291 {
292         u8 first, len;
293
294         first = 0;
295         len = 0;
296
297         if (mask) {
298                 while (!(mask & 0x1)) {
299                         mask = mask >> 1;
300                         first++;
301                 }
302
303                 while (mask & 0x1) {
304                         mask = mask >> 1;
305                         len++;
306                 }
307         }
308
309         *pos = first;
310         *size = len;
311 }
312
313 static efi_status_t
314 __setup_efi_pci32(efi_pci_io_protocol_32 *pci, struct pci_setup_rom **__rom)
315 {
316         struct pci_setup_rom *rom = NULL;
317         efi_status_t status;
318         unsigned long size;
319         uint64_t attributes;
320
321         status = efi_early->call(pci->attributes, pci,
322                                  EfiPciIoAttributeOperationGet, 0, 0,
323                                  &attributes);
324         if (status != EFI_SUCCESS)
325                 return status;
326
327         if (!pci->romimage || !pci->romsize)
328                 return EFI_INVALID_PARAMETER;
329
330         size = pci->romsize + sizeof(*rom);
331
332         status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
333         if (status != EFI_SUCCESS) {
334                 efi_printk(sys_table, "Failed to alloc mem for rom\n");
335                 return status;
336         }
337
338         memset(rom, 0, sizeof(*rom));
339
340         rom->data.type = SETUP_PCI;
341         rom->data.len = size - sizeof(struct setup_data);
342         rom->data.next = 0;
343         rom->pcilen = pci->romsize;
344         *__rom = rom;
345
346         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
347                                  PCI_VENDOR_ID, 1, &(rom->vendor));
348
349         if (status != EFI_SUCCESS) {
350                 efi_printk(sys_table, "Failed to read rom->vendor\n");
351                 goto free_struct;
352         }
353
354         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
355                                  PCI_DEVICE_ID, 1, &(rom->devid));
356
357         if (status != EFI_SUCCESS) {
358                 efi_printk(sys_table, "Failed to read rom->devid\n");
359                 goto free_struct;
360         }
361
362         status = efi_early->call(pci->get_location, pci, &(rom->segment),
363                                  &(rom->bus), &(rom->device), &(rom->function));
364
365         if (status != EFI_SUCCESS)
366                 goto free_struct;
367
368         memcpy(rom->romdata, pci->romimage, pci->romsize);
369         return status;
370
371 free_struct:
372         efi_call_early(free_pool, rom);
373         return status;
374 }
375
376 static void
377 setup_efi_pci32(struct boot_params *params, void **pci_handle,
378                 unsigned long size)
379 {
380         efi_pci_io_protocol_32 *pci = NULL;
381         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
382         u32 *handles = (u32 *)(unsigned long)pci_handle;
383         efi_status_t status;
384         unsigned long nr_pci;
385         struct setup_data *data;
386         int i;
387
388         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
389
390         while (data && data->next)
391                 data = (struct setup_data *)(unsigned long)data->next;
392
393         nr_pci = size / sizeof(u32);
394         for (i = 0; i < nr_pci; i++) {
395                 struct pci_setup_rom *rom = NULL;
396                 u32 h = handles[i];
397
398                 status = efi_call_early(handle_protocol, h,
399                                         &pci_proto, (void **)&pci);
400
401                 if (status != EFI_SUCCESS)
402                         continue;
403
404                 if (!pci)
405                         continue;
406
407                 status = __setup_efi_pci32(pci, &rom);
408                 if (status != EFI_SUCCESS)
409                         continue;
410
411                 if (data)
412                         data->next = (unsigned long)rom;
413                 else
414                         params->hdr.setup_data = (unsigned long)rom;
415
416                 data = (struct setup_data *)rom;
417
418         }
419 }
420
421 static efi_status_t
422 __setup_efi_pci64(efi_pci_io_protocol_64 *pci, struct pci_setup_rom **__rom)
423 {
424         struct pci_setup_rom *rom;
425         efi_status_t status;
426         unsigned long size;
427         uint64_t attributes;
428
429         status = efi_early->call(pci->attributes, pci,
430                                  EfiPciIoAttributeOperationGet, 0,
431                                  &attributes);
432         if (status != EFI_SUCCESS)
433                 return status;
434
435         if (!pci->romimage || !pci->romsize)
436                 return EFI_INVALID_PARAMETER;
437
438         size = pci->romsize + sizeof(*rom);
439
440         status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
441         if (status != EFI_SUCCESS) {
442                 efi_printk(sys_table, "Failed to alloc mem for rom\n");
443                 return status;
444         }
445
446         rom->data.type = SETUP_PCI;
447         rom->data.len = size - sizeof(struct setup_data);
448         rom->data.next = 0;
449         rom->pcilen = pci->romsize;
450         *__rom = rom;
451
452         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
453                                  PCI_VENDOR_ID, 1, &(rom->vendor));
454
455         if (status != EFI_SUCCESS) {
456                 efi_printk(sys_table, "Failed to read rom->vendor\n");
457                 goto free_struct;
458         }
459
460         status = efi_early->call(pci->pci.read, pci, EfiPciIoWidthUint16,
461                                  PCI_DEVICE_ID, 1, &(rom->devid));
462
463         if (status != EFI_SUCCESS) {
464                 efi_printk(sys_table, "Failed to read rom->devid\n");
465                 goto free_struct;
466         }
467
468         status = efi_early->call(pci->get_location, pci, &(rom->segment),
469                                  &(rom->bus), &(rom->device), &(rom->function));
470
471         if (status != EFI_SUCCESS)
472                 goto free_struct;
473
474         memcpy(rom->romdata, pci->romimage, pci->romsize);
475         return status;
476
477 free_struct:
478         efi_call_early(free_pool, rom);
479         return status;
480
481 }
482
483 static void
484 setup_efi_pci64(struct boot_params *params, void **pci_handle,
485                 unsigned long size)
486 {
487         efi_pci_io_protocol_64 *pci = NULL;
488         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
489         u64 *handles = (u64 *)(unsigned long)pci_handle;
490         efi_status_t status;
491         unsigned long nr_pci;
492         struct setup_data *data;
493         int i;
494
495         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
496
497         while (data && data->next)
498                 data = (struct setup_data *)(unsigned long)data->next;
499
500         nr_pci = size / sizeof(u64);
501         for (i = 0; i < nr_pci; i++) {
502                 struct pci_setup_rom *rom = NULL;
503                 u64 h = handles[i];
504
505                 status = efi_call_early(handle_protocol, h,
506                                         &pci_proto, (void **)&pci);
507
508                 if (status != EFI_SUCCESS)
509                         continue;
510
511                 if (!pci)
512                         continue;
513
514                 status = __setup_efi_pci64(pci, &rom);
515                 if (status != EFI_SUCCESS)
516                         continue;
517
518                 if (data)
519                         data->next = (unsigned long)rom;
520                 else
521                         params->hdr.setup_data = (unsigned long)rom;
522
523                 data = (struct setup_data *)rom;
524
525         }
526 }
527
528 /*
529  * There's no way to return an informative status from this function,
530  * because any analysis (and printing of error messages) needs to be
531  * done directly at the EFI function call-site.
532  *
533  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
534  * just didn't find any PCI devices, but there's no way to tell outside
535  * the context of the call.
536  */
537 static void setup_efi_pci(struct boot_params *params)
538 {
539         efi_status_t status;
540         void **pci_handle = NULL;
541         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
542         unsigned long size = 0;
543
544         status = efi_call_early(locate_handle,
545                                 EFI_LOCATE_BY_PROTOCOL,
546                                 &pci_proto, NULL, &size, pci_handle);
547
548         if (status == EFI_BUFFER_TOO_SMALL) {
549                 status = efi_call_early(allocate_pool,
550                                         EFI_LOADER_DATA,
551                                         size, (void **)&pci_handle);
552
553                 if (status != EFI_SUCCESS) {
554                         efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
555                         return;
556                 }
557
558                 status = efi_call_early(locate_handle,
559                                         EFI_LOCATE_BY_PROTOCOL, &pci_proto,
560                                         NULL, &size, pci_handle);
561         }
562
563         if (status != EFI_SUCCESS)
564                 goto free_handle;
565
566         if (efi_early->is64)
567                 setup_efi_pci64(params, pci_handle, size);
568         else
569                 setup_efi_pci32(params, pci_handle, size);
570
571 free_handle:
572         efi_call_early(free_pool, pci_handle);
573 }
574
575 static void
576 setup_pixel_info(struct screen_info *si, u32 pixels_per_scan_line,
577                  struct efi_pixel_bitmask pixel_info, int pixel_format)
578 {
579         if (pixel_format == PIXEL_RGB_RESERVED_8BIT_PER_COLOR) {
580                 si->lfb_depth = 32;
581                 si->lfb_linelength = pixels_per_scan_line * 4;
582                 si->red_size = 8;
583                 si->red_pos = 0;
584                 si->green_size = 8;
585                 si->green_pos = 8;
586                 si->blue_size = 8;
587                 si->blue_pos = 16;
588                 si->rsvd_size = 8;
589                 si->rsvd_pos = 24;
590         } else if (pixel_format == PIXEL_BGR_RESERVED_8BIT_PER_COLOR) {
591                 si->lfb_depth = 32;
592                 si->lfb_linelength = pixels_per_scan_line * 4;
593                 si->red_size = 8;
594                 si->red_pos = 16;
595                 si->green_size = 8;
596                 si->green_pos = 8;
597                 si->blue_size = 8;
598                 si->blue_pos = 0;
599                 si->rsvd_size = 8;
600                 si->rsvd_pos = 24;
601         } else if (pixel_format == PIXEL_BIT_MASK) {
602                 find_bits(pixel_info.red_mask, &si->red_pos, &si->red_size);
603                 find_bits(pixel_info.green_mask, &si->green_pos,
604                           &si->green_size);
605                 find_bits(pixel_info.blue_mask, &si->blue_pos, &si->blue_size);
606                 find_bits(pixel_info.reserved_mask, &si->rsvd_pos,
607                           &si->rsvd_size);
608                 si->lfb_depth = si->red_size + si->green_size +
609                         si->blue_size + si->rsvd_size;
610                 si->lfb_linelength = (pixels_per_scan_line * si->lfb_depth) / 8;
611         } else {
612                 si->lfb_depth = 4;
613                 si->lfb_linelength = si->lfb_width / 2;
614                 si->red_size = 0;
615                 si->red_pos = 0;
616                 si->green_size = 0;
617                 si->green_pos = 0;
618                 si->blue_size = 0;
619                 si->blue_pos = 0;
620                 si->rsvd_size = 0;
621                 si->rsvd_pos = 0;
622         }
623 }
624
625 static efi_status_t
626 __gop_query32(struct efi_graphics_output_protocol_32 *gop32,
627               struct efi_graphics_output_mode_info **info,
628               unsigned long *size, u32 *fb_base)
629 {
630         struct efi_graphics_output_protocol_mode_32 *mode;
631         efi_status_t status;
632         unsigned long m;
633
634         m = gop32->mode;
635         mode = (struct efi_graphics_output_protocol_mode_32 *)m;
636
637         status = efi_early->call(gop32->query_mode, gop32,
638                                  mode->mode, size, info);
639         if (status != EFI_SUCCESS)
640                 return status;
641
642         *fb_base = mode->frame_buffer_base;
643         return status;
644 }
645
646 static efi_status_t
647 setup_gop32(struct screen_info *si, efi_guid_t *proto,
648             unsigned long size, void **gop_handle)
649 {
650         struct efi_graphics_output_protocol_32 *gop32, *first_gop;
651         unsigned long nr_gops;
652         u16 width, height;
653         u32 pixels_per_scan_line;
654         u32 fb_base;
655         struct efi_pixel_bitmask pixel_info;
656         int pixel_format;
657         efi_status_t status;
658         u32 *handles = (u32 *)(unsigned long)gop_handle;
659         int i;
660
661         first_gop = NULL;
662         gop32 = NULL;
663
664         nr_gops = size / sizeof(u32);
665         for (i = 0; i < nr_gops; i++) {
666                 struct efi_graphics_output_mode_info *info = NULL;
667                 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
668                 bool conout_found = false;
669                 void *dummy = NULL;
670                 u32 h = handles[i];
671
672                 status = efi_call_early(handle_protocol, h,
673                                         proto, (void **)&gop32);
674                 if (status != EFI_SUCCESS)
675                         continue;
676
677                 status = efi_call_early(handle_protocol, h,
678                                         &conout_proto, &dummy);
679                 if (status == EFI_SUCCESS)
680                         conout_found = true;
681
682                 status = __gop_query32(gop32, &info, &size, &fb_base);
683                 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
684                         /*
685                          * Systems that use the UEFI Console Splitter may
686                          * provide multiple GOP devices, not all of which are
687                          * backed by real hardware. The workaround is to search
688                          * for a GOP implementing the ConOut protocol, and if
689                          * one isn't found, to just fall back to the first GOP.
690                          */
691                         width = info->horizontal_resolution;
692                         height = info->vertical_resolution;
693                         pixel_format = info->pixel_format;
694                         pixel_info = info->pixel_information;
695                         pixels_per_scan_line = info->pixels_per_scan_line;
696
697                         /*
698                          * Once we've found a GOP supporting ConOut,
699                          * don't bother looking any further.
700                          */
701                         first_gop = gop32;
702                         if (conout_found)
703                                 break;
704                 }
705         }
706
707         /* Did we find any GOPs? */
708         if (!first_gop)
709                 goto out;
710
711         /* EFI framebuffer */
712         si->orig_video_isVGA = VIDEO_TYPE_EFI;
713
714         si->lfb_width = width;
715         si->lfb_height = height;
716         si->lfb_base = fb_base;
717         si->pages = 1;
718
719         setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
720
721         si->lfb_size = si->lfb_linelength * si->lfb_height;
722
723         si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
724 out:
725         return status;
726 }
727
728 static efi_status_t
729 __gop_query64(struct efi_graphics_output_protocol_64 *gop64,
730               struct efi_graphics_output_mode_info **info,
731               unsigned long *size, u32 *fb_base)
732 {
733         struct efi_graphics_output_protocol_mode_64 *mode;
734         efi_status_t status;
735         unsigned long m;
736
737         m = gop64->mode;
738         mode = (struct efi_graphics_output_protocol_mode_64 *)m;
739
740         status = efi_early->call(gop64->query_mode, gop64,
741                                  mode->mode, size, info);
742         if (status != EFI_SUCCESS)
743                 return status;
744
745         *fb_base = mode->frame_buffer_base;
746         return status;
747 }
748
749 static efi_status_t
750 setup_gop64(struct screen_info *si, efi_guid_t *proto,
751             unsigned long size, void **gop_handle)
752 {
753         struct efi_graphics_output_protocol_64 *gop64, *first_gop;
754         unsigned long nr_gops;
755         u16 width, height;
756         u32 pixels_per_scan_line;
757         u32 fb_base;
758         struct efi_pixel_bitmask pixel_info;
759         int pixel_format;
760         efi_status_t status;
761         u64 *handles = (u64 *)(unsigned long)gop_handle;
762         int i;
763
764         first_gop = NULL;
765         gop64 = NULL;
766
767         nr_gops = size / sizeof(u64);
768         for (i = 0; i < nr_gops; i++) {
769                 struct efi_graphics_output_mode_info *info = NULL;
770                 efi_guid_t conout_proto = EFI_CONSOLE_OUT_DEVICE_GUID;
771                 bool conout_found = false;
772                 void *dummy = NULL;
773                 u64 h = handles[i];
774
775                 status = efi_call_early(handle_protocol, h,
776                                         proto, (void **)&gop64);
777                 if (status != EFI_SUCCESS)
778                         continue;
779
780                 status = efi_call_early(handle_protocol, h,
781                                         &conout_proto, &dummy);
782                 if (status == EFI_SUCCESS)
783                         conout_found = true;
784
785                 status = __gop_query64(gop64, &info, &size, &fb_base);
786                 if (status == EFI_SUCCESS && (!first_gop || conout_found)) {
787                         /*
788                          * Systems that use the UEFI Console Splitter may
789                          * provide multiple GOP devices, not all of which are
790                          * backed by real hardware. The workaround is to search
791                          * for a GOP implementing the ConOut protocol, and if
792                          * one isn't found, to just fall back to the first GOP.
793                          */
794                         width = info->horizontal_resolution;
795                         height = info->vertical_resolution;
796                         pixel_format = info->pixel_format;
797                         pixel_info = info->pixel_information;
798                         pixels_per_scan_line = info->pixels_per_scan_line;
799
800                         /*
801                          * Once we've found a GOP supporting ConOut,
802                          * don't bother looking any further.
803                          */
804                         first_gop = gop64;
805                         if (conout_found)
806                                 break;
807                 }
808         }
809
810         /* Did we find any GOPs? */
811         if (!first_gop)
812                 goto out;
813
814         /* EFI framebuffer */
815         si->orig_video_isVGA = VIDEO_TYPE_EFI;
816
817         si->lfb_width = width;
818         si->lfb_height = height;
819         si->lfb_base = fb_base;
820         si->pages = 1;
821
822         setup_pixel_info(si, pixels_per_scan_line, pixel_info, pixel_format);
823
824         si->lfb_size = si->lfb_linelength * si->lfb_height;
825
826         si->capabilities |= VIDEO_CAPABILITY_SKIP_QUIRKS;
827 out:
828         return status;
829 }
830
831 /*
832  * See if we have Graphics Output Protocol
833  */
834 static efi_status_t setup_gop(struct screen_info *si, efi_guid_t *proto,
835                               unsigned long size)
836 {
837         efi_status_t status;
838         void **gop_handle = NULL;
839
840         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
841                                 size, (void **)&gop_handle);
842         if (status != EFI_SUCCESS)
843                 return status;
844
845         status = efi_call_early(locate_handle,
846                                 EFI_LOCATE_BY_PROTOCOL,
847                                 proto, NULL, &size, gop_handle);
848         if (status != EFI_SUCCESS)
849                 goto free_handle;
850
851         if (efi_early->is64)
852                 status = setup_gop64(si, proto, size, gop_handle);
853         else
854                 status = setup_gop32(si, proto, size, gop_handle);
855
856 free_handle:
857         efi_call_early(free_pool, gop_handle);
858         return status;
859 }
860
861 static efi_status_t
862 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
863 {
864         struct efi_uga_draw_protocol *uga = NULL, *first_uga;
865         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
866         unsigned long nr_ugas;
867         u32 *handles = (u32 *)uga_handle;;
868         efi_status_t status;
869         int i;
870
871         first_uga = NULL;
872         nr_ugas = size / sizeof(u32);
873         for (i = 0; i < nr_ugas; i++) {
874                 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
875                 u32 w, h, depth, refresh;
876                 void *pciio;
877                 u32 handle = handles[i];
878
879                 status = efi_call_early(handle_protocol, handle,
880                                         &uga_proto, (void **)&uga);
881                 if (status != EFI_SUCCESS)
882                         continue;
883
884                 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
885
886                 status = efi_early->call((unsigned long)uga->get_mode, uga,
887                                          &w, &h, &depth, &refresh);
888                 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
889                         *width = w;
890                         *height = h;
891
892                         /*
893                          * Once we've found a UGA supporting PCIIO,
894                          * don't bother looking any further.
895                          */
896                         if (pciio)
897                                 break;
898
899                         first_uga = uga;
900                 }
901         }
902
903         return status;
904 }
905
906 static efi_status_t
907 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
908 {
909         struct efi_uga_draw_protocol *uga = NULL, *first_uga;
910         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
911         unsigned long nr_ugas;
912         u64 *handles = (u64 *)uga_handle;;
913         efi_status_t status;
914         int i;
915
916         first_uga = NULL;
917         nr_ugas = size / sizeof(u64);
918         for (i = 0; i < nr_ugas; i++) {
919                 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
920                 u32 w, h, depth, refresh;
921                 void *pciio;
922                 u64 handle = handles[i];
923
924                 status = efi_call_early(handle_protocol, handle,
925                                         &uga_proto, (void **)&uga);
926                 if (status != EFI_SUCCESS)
927                         continue;
928
929                 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
930
931                 status = efi_early->call((unsigned long)uga->get_mode, uga,
932                                          &w, &h, &depth, &refresh);
933                 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
934                         *width = w;
935                         *height = h;
936
937                         /*
938                          * Once we've found a UGA supporting PCIIO,
939                          * don't bother looking any further.
940                          */
941                         if (pciio)
942                                 break;
943
944                         first_uga = uga;
945                 }
946         }
947
948         return status;
949 }
950
951 /*
952  * See if we have Universal Graphics Adapter (UGA) protocol
953  */
954 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
955                               unsigned long size)
956 {
957         efi_status_t status;
958         u32 width, height;
959         void **uga_handle = NULL;
960
961         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
962                                 size, (void **)&uga_handle);
963         if (status != EFI_SUCCESS)
964                 return status;
965
966         status = efi_call_early(locate_handle,
967                                 EFI_LOCATE_BY_PROTOCOL,
968                                 uga_proto, NULL, &size, uga_handle);
969         if (status != EFI_SUCCESS)
970                 goto free_handle;
971
972         height = 0;
973         width = 0;
974
975         if (efi_early->is64)
976                 status = setup_uga64(uga_handle, size, &width, &height);
977         else
978                 status = setup_uga32(uga_handle, size, &width, &height);
979
980         if (!width && !height)
981                 goto free_handle;
982
983         /* EFI framebuffer */
984         si->orig_video_isVGA = VIDEO_TYPE_EFI;
985
986         si->lfb_depth = 32;
987         si->lfb_width = width;
988         si->lfb_height = height;
989
990         si->red_size = 8;
991         si->red_pos = 16;
992         si->green_size = 8;
993         si->green_pos = 8;
994         si->blue_size = 8;
995         si->blue_pos = 0;
996         si->rsvd_size = 8;
997         si->rsvd_pos = 24;
998
999 free_handle:
1000         efi_call_early(free_pool, uga_handle);
1001         return status;
1002 }
1003
1004 void setup_graphics(struct boot_params *boot_params)
1005 {
1006         efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
1007         struct screen_info *si;
1008         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
1009         efi_status_t status;
1010         unsigned long size;
1011         void **gop_handle = NULL;
1012         void **uga_handle = NULL;
1013
1014         si = &boot_params->screen_info;
1015         memset(si, 0, sizeof(*si));
1016
1017         size = 0;
1018         status = efi_call_early(locate_handle,
1019                                 EFI_LOCATE_BY_PROTOCOL,
1020                                 &graphics_proto, NULL, &size, gop_handle);
1021         if (status == EFI_BUFFER_TOO_SMALL)
1022                 status = setup_gop(si, &graphics_proto, size);
1023
1024         if (status != EFI_SUCCESS) {
1025                 size = 0;
1026                 status = efi_call_early(locate_handle,
1027                                         EFI_LOCATE_BY_PROTOCOL,
1028                                         &uga_proto, NULL, &size, uga_handle);
1029                 if (status == EFI_BUFFER_TOO_SMALL)
1030                         setup_uga(si, &uga_proto, size);
1031         }
1032 }
1033
1034 /*
1035  * Because the x86 boot code expects to be passed a boot_params we
1036  * need to create one ourselves (usually the bootloader would create
1037  * one for us).
1038  *
1039  * The caller is responsible for filling out ->code32_start in the
1040  * returned boot_params.
1041  */
1042 struct boot_params *make_boot_params(struct efi_config *c)
1043 {
1044         struct boot_params *boot_params;
1045         struct sys_desc_table *sdt;
1046         struct apm_bios_info *bi;
1047         struct setup_header *hdr;
1048         struct efi_info *efi;
1049         efi_loaded_image_t *image;
1050         void *options, *handle;
1051         efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
1052         int options_size = 0;
1053         efi_status_t status;
1054         char *cmdline_ptr;
1055         u16 *s2;
1056         u8 *s1;
1057         int i;
1058         unsigned long ramdisk_addr;
1059         unsigned long ramdisk_size;
1060
1061         efi_early = c;
1062         sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
1063         handle = (void *)(unsigned long)efi_early->image_handle;
1064
1065         /* Check if we were booted by the EFI firmware */
1066         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1067                 return NULL;
1068
1069         if (efi_early->is64)
1070                 setup_boot_services64(efi_early);
1071         else
1072                 setup_boot_services32(efi_early);
1073
1074         status = efi_call_early(handle_protocol, handle,
1075                                 &proto, (void *)&image);
1076         if (status != EFI_SUCCESS) {
1077                 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
1078                 return NULL;
1079         }
1080
1081         status = efi_low_alloc(sys_table, 0x4000, 1,
1082                                (unsigned long *)&boot_params);
1083         if (status != EFI_SUCCESS) {
1084                 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
1085                 return NULL;
1086         }
1087
1088         memset(boot_params, 0x0, 0x4000);
1089
1090         hdr = &boot_params->hdr;
1091         efi = &boot_params->efi_info;
1092         bi = &boot_params->apm_bios_info;
1093         sdt = &boot_params->sys_desc_table;
1094
1095         /* Copy the second sector to boot_params */
1096         memcpy(&hdr->jump, image->image_base + 512, 512);
1097
1098         /*
1099          * Fill out some of the header fields ourselves because the
1100          * EFI firmware loader doesn't load the first sector.
1101          */
1102         hdr->root_flags = 1;
1103         hdr->vid_mode = 0xffff;
1104         hdr->boot_flag = 0xAA55;
1105
1106         hdr->type_of_loader = 0x21;
1107
1108         /* Convert unicode cmdline to ascii */
1109         cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
1110         if (!cmdline_ptr)
1111                 goto fail;
1112         hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
1113
1114         hdr->ramdisk_image = 0;
1115         hdr->ramdisk_size = 0;
1116
1117         /* Clear APM BIOS info */
1118         memset(bi, 0, sizeof(*bi));
1119
1120         memset(sdt, 0, sizeof(*sdt));
1121
1122         status = efi_parse_options(cmdline_ptr);
1123         if (status != EFI_SUCCESS)
1124                 goto fail2;
1125
1126         status = handle_cmdline_files(sys_table, image,
1127                                       (char *)(unsigned long)hdr->cmd_line_ptr,
1128                                       "initrd=", hdr->initrd_addr_max,
1129                                       &ramdisk_addr, &ramdisk_size);
1130
1131         if (status != EFI_SUCCESS &&
1132             hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
1133                 efi_printk(sys_table, "Trying to load files to higher address\n");
1134                 status = handle_cmdline_files(sys_table, image,
1135                                       (char *)(unsigned long)hdr->cmd_line_ptr,
1136                                       "initrd=", -1UL,
1137                                       &ramdisk_addr, &ramdisk_size);
1138         }
1139
1140         if (status != EFI_SUCCESS)
1141                 goto fail2;
1142         hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
1143         hdr->ramdisk_size  = ramdisk_size & 0xffffffff;
1144         boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
1145         boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
1146
1147         return boot_params;
1148 fail2:
1149         efi_free(sys_table, options_size, hdr->cmd_line_ptr);
1150 fail:
1151         efi_free(sys_table, 0x4000, (unsigned long)boot_params);
1152         return NULL;
1153 }
1154
1155 static void add_e820ext(struct boot_params *params,
1156                         struct setup_data *e820ext, u32 nr_entries)
1157 {
1158         struct setup_data *data;
1159         efi_status_t status;
1160         unsigned long size;
1161
1162         e820ext->type = SETUP_E820_EXT;
1163         e820ext->len = nr_entries * sizeof(struct e820entry);
1164         e820ext->next = 0;
1165
1166         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
1167
1168         while (data && data->next)
1169                 data = (struct setup_data *)(unsigned long)data->next;
1170
1171         if (data)
1172                 data->next = (unsigned long)e820ext;
1173         else
1174                 params->hdr.setup_data = (unsigned long)e820ext;
1175 }
1176
1177 static efi_status_t setup_e820(struct boot_params *params,
1178                                struct setup_data *e820ext, u32 e820ext_size)
1179 {
1180         struct e820entry *e820_map = &params->e820_map[0];
1181         struct efi_info *efi = &params->efi_info;
1182         struct e820entry *prev = NULL;
1183         u32 nr_entries;
1184         u32 nr_desc;
1185         int i;
1186
1187         nr_entries = 0;
1188         nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
1189
1190         for (i = 0; i < nr_desc; i++) {
1191                 efi_memory_desc_t *d;
1192                 unsigned int e820_type = 0;
1193                 unsigned long m = efi->efi_memmap;
1194
1195                 d = (efi_memory_desc_t *)(m + (i * efi->efi_memdesc_size));
1196                 switch (d->type) {
1197                 case EFI_RESERVED_TYPE:
1198                 case EFI_RUNTIME_SERVICES_CODE:
1199                 case EFI_RUNTIME_SERVICES_DATA:
1200                 case EFI_MEMORY_MAPPED_IO:
1201                 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
1202                 case EFI_PAL_CODE:
1203                         e820_type = E820_RESERVED;
1204                         break;
1205
1206                 case EFI_UNUSABLE_MEMORY:
1207                         e820_type = E820_UNUSABLE;
1208                         break;
1209
1210                 case EFI_ACPI_RECLAIM_MEMORY:
1211                         e820_type = E820_ACPI;
1212                         break;
1213
1214                 case EFI_LOADER_CODE:
1215                 case EFI_LOADER_DATA:
1216                 case EFI_BOOT_SERVICES_CODE:
1217                 case EFI_BOOT_SERVICES_DATA:
1218                 case EFI_CONVENTIONAL_MEMORY:
1219                         e820_type = E820_RAM;
1220                         break;
1221
1222                 case EFI_ACPI_MEMORY_NVS:
1223                         e820_type = E820_NVS;
1224                         break;
1225
1226                 default:
1227                         continue;
1228                 }
1229
1230                 /* Merge adjacent mappings */
1231                 if (prev && prev->type == e820_type &&
1232                     (prev->addr + prev->size) == d->phys_addr) {
1233                         prev->size += d->num_pages << 12;
1234                         continue;
1235                 }
1236
1237                 if (nr_entries == ARRAY_SIZE(params->e820_map)) {
1238                         u32 need = (nr_desc - i) * sizeof(struct e820entry) +
1239                                    sizeof(struct setup_data);
1240
1241                         if (!e820ext || e820ext_size < need)
1242                                 return EFI_BUFFER_TOO_SMALL;
1243
1244                         /* boot_params map full, switch to e820 extended */
1245                         e820_map = (struct e820entry *)e820ext->data;
1246                 }
1247
1248                 e820_map->addr = d->phys_addr;
1249                 e820_map->size = d->num_pages << PAGE_SHIFT;
1250                 e820_map->type = e820_type;
1251                 prev = e820_map++;
1252                 nr_entries++;
1253         }
1254
1255         if (nr_entries > ARRAY_SIZE(params->e820_map)) {
1256                 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_map);
1257
1258                 add_e820ext(params, e820ext, nr_e820ext);
1259                 nr_entries -= nr_e820ext;
1260         }
1261
1262         params->e820_entries = (u8)nr_entries;
1263
1264         return EFI_SUCCESS;
1265 }
1266
1267 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
1268                                   u32 *e820ext_size)
1269 {
1270         efi_status_t status;
1271         unsigned long size;
1272
1273         size = sizeof(struct setup_data) +
1274                 sizeof(struct e820entry) * nr_desc;
1275
1276         if (*e820ext) {
1277                 efi_call_early(free_pool, *e820ext);
1278                 *e820ext = NULL;
1279                 *e820ext_size = 0;
1280         }
1281
1282         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1283                                 size, (void **)e820ext);
1284         if (status == EFI_SUCCESS)
1285                 *e820ext_size = size;
1286
1287         return status;
1288 }
1289
1290 static efi_status_t exit_boot(struct boot_params *boot_params,
1291                               void *handle, bool is64)
1292 {
1293         struct efi_info *efi = &boot_params->efi_info;
1294         unsigned long map_sz, key, desc_size;
1295         efi_memory_desc_t *mem_map;
1296         struct setup_data *e820ext;
1297         const char *signature;
1298         __u32 e820ext_size;
1299         __u32 nr_desc, prev_nr_desc;
1300         efi_status_t status;
1301         __u32 desc_version;
1302         bool called_exit = false;
1303         u8 nr_entries;
1304         int i;
1305
1306         nr_desc = 0;
1307         e820ext = NULL;
1308         e820ext_size = 0;
1309
1310 get_map:
1311         status = efi_get_memory_map(sys_table, &mem_map, &map_sz, &desc_size,
1312                                     &desc_version, &key);
1313
1314         if (status != EFI_SUCCESS)
1315                 return status;
1316
1317         prev_nr_desc = nr_desc;
1318         nr_desc = map_sz / desc_size;
1319         if (nr_desc > prev_nr_desc &&
1320             nr_desc > ARRAY_SIZE(boot_params->e820_map)) {
1321                 u32 nr_e820ext = nr_desc - ARRAY_SIZE(boot_params->e820_map);
1322
1323                 status = alloc_e820ext(nr_e820ext, &e820ext, &e820ext_size);
1324                 if (status != EFI_SUCCESS)
1325                         goto free_mem_map;
1326
1327                 efi_call_early(free_pool, mem_map);
1328                 goto get_map; /* Allocated memory, get map again */
1329         }
1330
1331         signature = is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
1332         memcpy(&efi->efi_loader_signature, signature, sizeof(__u32));
1333
1334         efi->efi_systab = (unsigned long)sys_table;
1335         efi->efi_memdesc_size = desc_size;
1336         efi->efi_memdesc_version = desc_version;
1337         efi->efi_memmap = (unsigned long)mem_map;
1338         efi->efi_memmap_size = map_sz;
1339
1340 #ifdef CONFIG_X86_64
1341         efi->efi_systab_hi = (unsigned long)sys_table >> 32;
1342         efi->efi_memmap_hi = (unsigned long)mem_map >> 32;
1343 #endif
1344
1345         /* Might as well exit boot services now */
1346         status = efi_call_early(exit_boot_services, handle, key);
1347         if (status != EFI_SUCCESS) {
1348                 /*
1349                  * ExitBootServices() will fail if any of the event
1350                  * handlers change the memory map. In which case, we
1351                  * must be prepared to retry, but only once so that
1352                  * we're guaranteed to exit on repeated failures instead
1353                  * of spinning forever.
1354                  */
1355                 if (called_exit)
1356                         goto free_mem_map;
1357
1358                 called_exit = true;
1359                 efi_call_early(free_pool, mem_map);
1360                 goto get_map;
1361         }
1362
1363         /* Historic? */
1364         boot_params->alt_mem_k = 32 * 1024;
1365
1366         status = setup_e820(boot_params, e820ext, e820ext_size);
1367         if (status != EFI_SUCCESS)
1368                 return status;
1369
1370         return EFI_SUCCESS;
1371
1372 free_mem_map:
1373         efi_call_early(free_pool, mem_map);
1374         return status;
1375 }
1376
1377 /*
1378  * On success we return a pointer to a boot_params structure, and NULL
1379  * on failure.
1380  */
1381 struct boot_params *efi_main(struct efi_config *c,
1382                              struct boot_params *boot_params)
1383 {
1384         struct desc_ptr *gdt = NULL;
1385         efi_loaded_image_t *image;
1386         struct setup_header *hdr = &boot_params->hdr;
1387         efi_status_t status;
1388         struct desc_struct *desc;
1389         void *handle;
1390         efi_system_table_t *_table;
1391         bool is64;
1392
1393         efi_early = c;
1394
1395         _table = (efi_system_table_t *)(unsigned long)efi_early->table;
1396         handle = (void *)(unsigned long)efi_early->image_handle;
1397         is64 = efi_early->is64;
1398
1399         sys_table = _table;
1400
1401         /* Check if we were booted by the EFI firmware */
1402         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
1403                 goto fail;
1404
1405         if (is64)
1406                 setup_boot_services64(efi_early);
1407         else
1408                 setup_boot_services32(efi_early);
1409
1410         setup_graphics(boot_params);
1411
1412         setup_efi_pci(boot_params);
1413
1414         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
1415                                 sizeof(*gdt), (void **)&gdt);
1416         if (status != EFI_SUCCESS) {
1417                 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
1418                 goto fail;
1419         }
1420
1421         gdt->size = 0x800;
1422         status = efi_low_alloc(sys_table, gdt->size, 8,
1423                            (unsigned long *)&gdt->address);
1424         if (status != EFI_SUCCESS) {
1425                 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
1426                 goto fail;
1427         }
1428
1429         /*
1430          * If the kernel isn't already loaded at the preferred load
1431          * address, relocate it.
1432          */
1433         if (hdr->pref_address != hdr->code32_start) {
1434                 unsigned long bzimage_addr = hdr->code32_start;
1435                 status = efi_relocate_kernel(sys_table, &bzimage_addr,
1436                                              hdr->init_size, hdr->init_size,
1437                                              hdr->pref_address,
1438                                              hdr->kernel_alignment);
1439                 if (status != EFI_SUCCESS) {
1440                         efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
1441                         goto fail;
1442                 }
1443
1444                 hdr->pref_address = hdr->code32_start;
1445                 hdr->code32_start = bzimage_addr;
1446         }
1447
1448         status = exit_boot(boot_params, handle, is64);
1449         if (status != EFI_SUCCESS) {
1450                 efi_printk(sys_table, "exit_boot() failed!\n");
1451                 goto fail;
1452         }
1453
1454         memset((char *)gdt->address, 0x0, gdt->size);
1455         desc = (struct desc_struct *)gdt->address;
1456
1457         /* The first GDT is a dummy and the second is unused. */
1458         desc += 2;
1459
1460         desc->limit0 = 0xffff;
1461         desc->base0 = 0x0000;
1462         desc->base1 = 0x0000;
1463         desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1464         desc->s = DESC_TYPE_CODE_DATA;
1465         desc->dpl = 0;
1466         desc->p = 1;
1467         desc->limit = 0xf;
1468         desc->avl = 0;
1469         desc->l = 0;
1470         desc->d = SEG_OP_SIZE_32BIT;
1471         desc->g = SEG_GRANULARITY_4KB;
1472         desc->base2 = 0x00;
1473
1474         desc++;
1475         desc->limit0 = 0xffff;
1476         desc->base0 = 0x0000;
1477         desc->base1 = 0x0000;
1478         desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1479         desc->s = DESC_TYPE_CODE_DATA;
1480         desc->dpl = 0;
1481         desc->p = 1;
1482         desc->limit = 0xf;
1483         desc->avl = 0;
1484         desc->l = 0;
1485         desc->d = SEG_OP_SIZE_32BIT;
1486         desc->g = SEG_GRANULARITY_4KB;
1487         desc->base2 = 0x00;
1488
1489 #ifdef CONFIG_X86_64
1490         /* Task segment value */
1491         desc++;
1492         desc->limit0 = 0x0000;
1493         desc->base0 = 0x0000;
1494         desc->base1 = 0x0000;
1495         desc->type = SEG_TYPE_TSS;
1496         desc->s = 0;
1497         desc->dpl = 0;
1498         desc->p = 1;
1499         desc->limit = 0x0;
1500         desc->avl = 0;
1501         desc->l = 0;
1502         desc->d = 0;
1503         desc->g = SEG_GRANULARITY_4KB;
1504         desc->base2 = 0x00;
1505 #endif /* CONFIG_X86_64 */
1506
1507         asm volatile("cli");
1508         asm volatile ("lgdt %0" : : "m" (*gdt));
1509
1510         return boot_params;
1511 fail:
1512         efi_printk(sys_table, "efi_main() failed!\n");
1513         return NULL;
1514 }