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