stop_machine: Atomically queue and wake stopper threads
[muen/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
13 #include <asm/efi.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
16 #include <asm/desc.h>
17
18 #include "../string.h"
19 #include "eboot.h"
20
21 static efi_system_table_t *sys_table;
22
23 static struct efi_config *efi_early;
24
25 __pure const struct efi_config *__efi_early(void)
26 {
27         return efi_early;
28 }
29
30 #define BOOT_SERVICES(bits)                                             \
31 static void setup_boot_services##bits(struct efi_config *c)             \
32 {                                                                       \
33         efi_system_table_##bits##_t *table;                             \
34                                                                         \
35         table = (typeof(table))sys_table;                               \
36                                                                         \
37         c->runtime_services = table->runtime;                           \
38         c->boot_services = table->boottime;                             \
39         c->text_output = table->con_out;                                \
40 }
41 BOOT_SERVICES(32);
42 BOOT_SERVICES(64);
43
44 static inline efi_status_t __open_volume32(void *__image, void **__fh)
45 {
46         efi_file_io_interface_t *io;
47         efi_loaded_image_32_t *image = __image;
48         efi_file_handle_32_t *fh;
49         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
50         efi_status_t status;
51         void *handle = (void *)(unsigned long)image->device_handle;
52         unsigned long func;
53
54         status = efi_call_early(handle_protocol, handle,
55                                 &fs_proto, (void **)&io);
56         if (status != EFI_SUCCESS) {
57                 efi_printk(sys_table, "Failed to handle fs_proto\n");
58                 return status;
59         }
60
61         func = (unsigned long)io->open_volume;
62         status = efi_early->call(func, io, &fh);
63         if (status != EFI_SUCCESS)
64                 efi_printk(sys_table, "Failed to open volume\n");
65
66         *__fh = fh;
67         return status;
68 }
69
70 static inline efi_status_t __open_volume64(void *__image, void **__fh)
71 {
72         efi_file_io_interface_t *io;
73         efi_loaded_image_64_t *image = __image;
74         efi_file_handle_64_t *fh;
75         efi_guid_t fs_proto = EFI_FILE_SYSTEM_GUID;
76         efi_status_t status;
77         void *handle = (void *)(unsigned long)image->device_handle;
78         unsigned long func;
79
80         status = efi_call_early(handle_protocol, handle,
81                                 &fs_proto, (void **)&io);
82         if (status != EFI_SUCCESS) {
83                 efi_printk(sys_table, "Failed to handle fs_proto\n");
84                 return status;
85         }
86
87         func = (unsigned long)io->open_volume;
88         status = efi_early->call(func, io, &fh);
89         if (status != EFI_SUCCESS)
90                 efi_printk(sys_table, "Failed to open volume\n");
91
92         *__fh = fh;
93         return status;
94 }
95
96 efi_status_t
97 efi_open_volume(efi_system_table_t *sys_table, void *__image, void **__fh)
98 {
99         if (efi_early->is64)
100                 return __open_volume64(__image, __fh);
101
102         return __open_volume32(__image, __fh);
103 }
104
105 void efi_char16_printk(efi_system_table_t *table, efi_char16_t *str)
106 {
107         efi_call_proto(efi_simple_text_output_protocol, output_string,
108                        efi_early->text_output, str);
109 }
110
111 static efi_status_t
112 __setup_efi_pci(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
113 {
114         struct pci_setup_rom *rom = NULL;
115         efi_status_t status;
116         unsigned long size;
117         uint64_t romsize;
118         void *romimage;
119
120         /*
121          * Some firmware images contain EFI function pointers at the place where
122          * the romimage and romsize fields are supposed to be. Typically the EFI
123          * code is mapped at high addresses, translating to an unrealistically
124          * large romsize. The UEFI spec limits the size of option ROMs to 16
125          * MiB so we reject any ROMs over 16 MiB in size to catch this.
126          */
127         romimage = (void *)(unsigned long)efi_table_attr(efi_pci_io_protocol,
128                                                          romimage, pci);
129         romsize = efi_table_attr(efi_pci_io_protocol, romsize, pci);
130         if (!romimage || !romsize || romsize > SZ_16M)
131                 return EFI_INVALID_PARAMETER;
132
133         size = romsize + sizeof(*rom);
134
135         status = efi_call_early(allocate_pool, EFI_LOADER_DATA, size, &rom);
136         if (status != EFI_SUCCESS) {
137                 efi_printk(sys_table, "Failed to alloc mem for rom\n");
138                 return status;
139         }
140
141         memset(rom, 0, sizeof(*rom));
142
143         rom->data.type = SETUP_PCI;
144         rom->data.len = size - sizeof(struct setup_data);
145         rom->data.next = 0;
146         rom->pcilen = pci->romsize;
147         *__rom = rom;
148
149         status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
150                                 EfiPciIoWidthUint16, PCI_VENDOR_ID, 1,
151                                 &rom->vendor);
152
153         if (status != EFI_SUCCESS) {
154                 efi_printk(sys_table, "Failed to read rom->vendor\n");
155                 goto free_struct;
156         }
157
158         status = efi_call_proto(efi_pci_io_protocol, pci.read, pci,
159                                 EfiPciIoWidthUint16, PCI_DEVICE_ID, 1,
160                                 &rom->devid);
161
162         if (status != EFI_SUCCESS) {
163                 efi_printk(sys_table, "Failed to read rom->devid\n");
164                 goto free_struct;
165         }
166
167         status = efi_call_proto(efi_pci_io_protocol, get_location, pci,
168                                 &rom->segment, &rom->bus, &rom->device,
169                                 &rom->function);
170
171         if (status != EFI_SUCCESS)
172                 goto free_struct;
173
174         memcpy(rom->romdata, romimage, romsize);
175         return status;
176
177 free_struct:
178         efi_call_early(free_pool, rom);
179         return status;
180 }
181
182 static void
183 setup_efi_pci32(struct boot_params *params, void **pci_handle,
184                 unsigned long size)
185 {
186         efi_pci_io_protocol_t *pci = NULL;
187         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
188         u32 *handles = (u32 *)(unsigned long)pci_handle;
189         efi_status_t status;
190         unsigned long nr_pci;
191         struct setup_data *data;
192         int i;
193
194         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
195
196         while (data && data->next)
197                 data = (struct setup_data *)(unsigned long)data->next;
198
199         nr_pci = size / sizeof(u32);
200         for (i = 0; i < nr_pci; i++) {
201                 struct pci_setup_rom *rom = NULL;
202                 u32 h = handles[i];
203
204                 status = efi_call_early(handle_protocol, h,
205                                         &pci_proto, (void **)&pci);
206
207                 if (status != EFI_SUCCESS)
208                         continue;
209
210                 if (!pci)
211                         continue;
212
213                 status = __setup_efi_pci(pci, &rom);
214                 if (status != EFI_SUCCESS)
215                         continue;
216
217                 if (data)
218                         data->next = (unsigned long)rom;
219                 else
220                         params->hdr.setup_data = (unsigned long)rom;
221
222                 data = (struct setup_data *)rom;
223
224         }
225 }
226
227 static void
228 setup_efi_pci64(struct boot_params *params, void **pci_handle,
229                 unsigned long size)
230 {
231         efi_pci_io_protocol_t *pci = NULL;
232         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
233         u64 *handles = (u64 *)(unsigned long)pci_handle;
234         efi_status_t status;
235         unsigned long nr_pci;
236         struct setup_data *data;
237         int i;
238
239         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
240
241         while (data && data->next)
242                 data = (struct setup_data *)(unsigned long)data->next;
243
244         nr_pci = size / sizeof(u64);
245         for (i = 0; i < nr_pci; i++) {
246                 struct pci_setup_rom *rom = NULL;
247                 u64 h = handles[i];
248
249                 status = efi_call_early(handle_protocol, h,
250                                         &pci_proto, (void **)&pci);
251
252                 if (status != EFI_SUCCESS)
253                         continue;
254
255                 if (!pci)
256                         continue;
257
258                 status = __setup_efi_pci(pci, &rom);
259                 if (status != EFI_SUCCESS)
260                         continue;
261
262                 if (data)
263                         data->next = (unsigned long)rom;
264                 else
265                         params->hdr.setup_data = (unsigned long)rom;
266
267                 data = (struct setup_data *)rom;
268
269         }
270 }
271
272 /*
273  * There's no way to return an informative status from this function,
274  * because any analysis (and printing of error messages) needs to be
275  * done directly at the EFI function call-site.
276  *
277  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
278  * just didn't find any PCI devices, but there's no way to tell outside
279  * the context of the call.
280  */
281 static void setup_efi_pci(struct boot_params *params)
282 {
283         efi_status_t status;
284         void **pci_handle = NULL;
285         efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
286         unsigned long size = 0;
287
288         status = efi_call_early(locate_handle,
289                                 EFI_LOCATE_BY_PROTOCOL,
290                                 &pci_proto, NULL, &size, pci_handle);
291
292         if (status == EFI_BUFFER_TOO_SMALL) {
293                 status = efi_call_early(allocate_pool,
294                                         EFI_LOADER_DATA,
295                                         size, (void **)&pci_handle);
296
297                 if (status != EFI_SUCCESS) {
298                         efi_printk(sys_table, "Failed to alloc mem for pci_handle\n");
299                         return;
300                 }
301
302                 status = efi_call_early(locate_handle,
303                                         EFI_LOCATE_BY_PROTOCOL, &pci_proto,
304                                         NULL, &size, pci_handle);
305         }
306
307         if (status != EFI_SUCCESS)
308                 goto free_handle;
309
310         if (efi_early->is64)
311                 setup_efi_pci64(params, pci_handle, size);
312         else
313                 setup_efi_pci32(params, pci_handle, size);
314
315 free_handle:
316         efi_call_early(free_pool, pci_handle);
317 }
318
319 static void retrieve_apple_device_properties(struct boot_params *boot_params)
320 {
321         efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
322         struct setup_data *data, *new;
323         efi_status_t status;
324         u32 size = 0;
325         void *p;
326
327         status = efi_call_early(locate_protocol, &guid, NULL, &p);
328         if (status != EFI_SUCCESS)
329                 return;
330
331         if (efi_table_attr(apple_properties_protocol, version, p) != 0x10000) {
332                 efi_printk(sys_table, "Unsupported properties proto version\n");
333                 return;
334         }
335
336         efi_call_proto(apple_properties_protocol, get_all, p, NULL, &size);
337         if (!size)
338                 return;
339
340         do {
341                 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
342                                         size + sizeof(struct setup_data), &new);
343                 if (status != EFI_SUCCESS) {
344                         efi_printk(sys_table,
345                                         "Failed to alloc mem for properties\n");
346                         return;
347                 }
348
349                 status = efi_call_proto(apple_properties_protocol, get_all, p,
350                                         new->data, &size);
351
352                 if (status == EFI_BUFFER_TOO_SMALL)
353                         efi_call_early(free_pool, new);
354         } while (status == EFI_BUFFER_TOO_SMALL);
355
356         new->type = SETUP_APPLE_PROPERTIES;
357         new->len  = size;
358         new->next = 0;
359
360         data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
361         if (!data)
362                 boot_params->hdr.setup_data = (unsigned long)new;
363         else {
364                 while (data->next)
365                         data = (struct setup_data *)(unsigned long)data->next;
366                 data->next = (unsigned long)new;
367         }
368 }
369
370 static const efi_char16_t apple[] = L"Apple";
371
372 static void setup_quirks(struct boot_params *boot_params)
373 {
374         efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
375                 efi_table_attr(efi_system_table, fw_vendor, sys_table);
376
377         if (!memcmp(fw_vendor, apple, sizeof(apple))) {
378                 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
379                         retrieve_apple_device_properties(boot_params);
380         }
381 }
382
383 static efi_status_t
384 setup_uga32(void **uga_handle, unsigned long size, u32 *width, u32 *height)
385 {
386         struct efi_uga_draw_protocol *uga = NULL, *first_uga;
387         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
388         unsigned long nr_ugas;
389         u32 *handles = (u32 *)uga_handle;
390         efi_status_t status = EFI_INVALID_PARAMETER;
391         int i;
392
393         first_uga = NULL;
394         nr_ugas = size / sizeof(u32);
395         for (i = 0; i < nr_ugas; i++) {
396                 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
397                 u32 w, h, depth, refresh;
398                 void *pciio;
399                 u32 handle = handles[i];
400
401                 status = efi_call_early(handle_protocol, handle,
402                                         &uga_proto, (void **)&uga);
403                 if (status != EFI_SUCCESS)
404                         continue;
405
406                 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
407
408                 status = efi_early->call((unsigned long)uga->get_mode, uga,
409                                          &w, &h, &depth, &refresh);
410                 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
411                         *width = w;
412                         *height = h;
413
414                         /*
415                          * Once we've found a UGA supporting PCIIO,
416                          * don't bother looking any further.
417                          */
418                         if (pciio)
419                                 break;
420
421                         first_uga = uga;
422                 }
423         }
424
425         return status;
426 }
427
428 static efi_status_t
429 setup_uga64(void **uga_handle, unsigned long size, u32 *width, u32 *height)
430 {
431         struct efi_uga_draw_protocol *uga = NULL, *first_uga;
432         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
433         unsigned long nr_ugas;
434         u64 *handles = (u64 *)uga_handle;
435         efi_status_t status = EFI_INVALID_PARAMETER;
436         int i;
437
438         first_uga = NULL;
439         nr_ugas = size / sizeof(u64);
440         for (i = 0; i < nr_ugas; i++) {
441                 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
442                 u32 w, h, depth, refresh;
443                 void *pciio;
444                 u64 handle = handles[i];
445
446                 status = efi_call_early(handle_protocol, handle,
447                                         &uga_proto, (void **)&uga);
448                 if (status != EFI_SUCCESS)
449                         continue;
450
451                 efi_call_early(handle_protocol, handle, &pciio_proto, &pciio);
452
453                 status = efi_early->call((unsigned long)uga->get_mode, uga,
454                                          &w, &h, &depth, &refresh);
455                 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
456                         *width = w;
457                         *height = h;
458
459                         /*
460                          * Once we've found a UGA supporting PCIIO,
461                          * don't bother looking any further.
462                          */
463                         if (pciio)
464                                 break;
465
466                         first_uga = uga;
467                 }
468         }
469
470         return status;
471 }
472
473 /*
474  * See if we have Universal Graphics Adapter (UGA) protocol
475  */
476 static efi_status_t setup_uga(struct screen_info *si, efi_guid_t *uga_proto,
477                               unsigned long size)
478 {
479         efi_status_t status;
480         u32 width, height;
481         void **uga_handle = NULL;
482
483         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
484                                 size, (void **)&uga_handle);
485         if (status != EFI_SUCCESS)
486                 return status;
487
488         status = efi_call_early(locate_handle,
489                                 EFI_LOCATE_BY_PROTOCOL,
490                                 uga_proto, NULL, &size, uga_handle);
491         if (status != EFI_SUCCESS)
492                 goto free_handle;
493
494         height = 0;
495         width = 0;
496
497         if (efi_early->is64)
498                 status = setup_uga64(uga_handle, size, &width, &height);
499         else
500                 status = setup_uga32(uga_handle, size, &width, &height);
501
502         if (!width && !height)
503                 goto free_handle;
504
505         /* EFI framebuffer */
506         si->orig_video_isVGA = VIDEO_TYPE_EFI;
507
508         si->lfb_depth = 32;
509         si->lfb_width = width;
510         si->lfb_height = height;
511
512         si->red_size = 8;
513         si->red_pos = 16;
514         si->green_size = 8;
515         si->green_pos = 8;
516         si->blue_size = 8;
517         si->blue_pos = 0;
518         si->rsvd_size = 8;
519         si->rsvd_pos = 24;
520
521 free_handle:
522         efi_call_early(free_pool, uga_handle);
523         return status;
524 }
525
526 void setup_graphics(struct boot_params *boot_params)
527 {
528         efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
529         struct screen_info *si;
530         efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
531         efi_status_t status;
532         unsigned long size;
533         void **gop_handle = NULL;
534         void **uga_handle = NULL;
535
536         si = &boot_params->screen_info;
537         memset(si, 0, sizeof(*si));
538
539         size = 0;
540         status = efi_call_early(locate_handle,
541                                 EFI_LOCATE_BY_PROTOCOL,
542                                 &graphics_proto, NULL, &size, gop_handle);
543         if (status == EFI_BUFFER_TOO_SMALL)
544                 status = efi_setup_gop(NULL, si, &graphics_proto, size);
545
546         if (status != EFI_SUCCESS) {
547                 size = 0;
548                 status = efi_call_early(locate_handle,
549                                         EFI_LOCATE_BY_PROTOCOL,
550                                         &uga_proto, NULL, &size, uga_handle);
551                 if (status == EFI_BUFFER_TOO_SMALL)
552                         setup_uga(si, &uga_proto, size);
553         }
554 }
555
556 /*
557  * Because the x86 boot code expects to be passed a boot_params we
558  * need to create one ourselves (usually the bootloader would create
559  * one for us).
560  *
561  * The caller is responsible for filling out ->code32_start in the
562  * returned boot_params.
563  */
564 struct boot_params *make_boot_params(struct efi_config *c)
565 {
566         struct boot_params *boot_params;
567         struct apm_bios_info *bi;
568         struct setup_header *hdr;
569         efi_loaded_image_t *image;
570         void *options, *handle;
571         efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
572         int options_size = 0;
573         efi_status_t status;
574         char *cmdline_ptr;
575         u16 *s2;
576         u8 *s1;
577         int i;
578         unsigned long ramdisk_addr;
579         unsigned long ramdisk_size;
580
581         efi_early = c;
582         sys_table = (efi_system_table_t *)(unsigned long)efi_early->table;
583         handle = (void *)(unsigned long)efi_early->image_handle;
584
585         /* Check if we were booted by the EFI firmware */
586         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
587                 return NULL;
588
589         if (efi_early->is64)
590                 setup_boot_services64(efi_early);
591         else
592                 setup_boot_services32(efi_early);
593
594         status = efi_call_early(handle_protocol, handle,
595                                 &proto, (void *)&image);
596         if (status != EFI_SUCCESS) {
597                 efi_printk(sys_table, "Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
598                 return NULL;
599         }
600
601         status = efi_low_alloc(sys_table, 0x4000, 1,
602                                (unsigned long *)&boot_params);
603         if (status != EFI_SUCCESS) {
604                 efi_printk(sys_table, "Failed to alloc lowmem for boot params\n");
605                 return NULL;
606         }
607
608         memset(boot_params, 0x0, 0x4000);
609
610         hdr = &boot_params->hdr;
611         bi = &boot_params->apm_bios_info;
612
613         /* Copy the second sector to boot_params */
614         memcpy(&hdr->jump, image->image_base + 512, 512);
615
616         /*
617          * Fill out some of the header fields ourselves because the
618          * EFI firmware loader doesn't load the first sector.
619          */
620         hdr->root_flags = 1;
621         hdr->vid_mode = 0xffff;
622         hdr->boot_flag = 0xAA55;
623
624         hdr->type_of_loader = 0x21;
625
626         /* Convert unicode cmdline to ascii */
627         cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size);
628         if (!cmdline_ptr)
629                 goto fail;
630         hdr->cmd_line_ptr = (unsigned long)cmdline_ptr;
631         /* Fill in upper bits of command line address, NOP on 32 bit  */
632         boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32;
633
634         hdr->ramdisk_image = 0;
635         hdr->ramdisk_size = 0;
636
637         /* Clear APM BIOS info */
638         memset(bi, 0, sizeof(*bi));
639
640         status = efi_parse_options(cmdline_ptr);
641         if (status != EFI_SUCCESS)
642                 goto fail2;
643
644         status = handle_cmdline_files(sys_table, image,
645                                       (char *)(unsigned long)hdr->cmd_line_ptr,
646                                       "initrd=", hdr->initrd_addr_max,
647                                       &ramdisk_addr, &ramdisk_size);
648
649         if (status != EFI_SUCCESS &&
650             hdr->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G) {
651                 efi_printk(sys_table, "Trying to load files to higher address\n");
652                 status = handle_cmdline_files(sys_table, image,
653                                       (char *)(unsigned long)hdr->cmd_line_ptr,
654                                       "initrd=", -1UL,
655                                       &ramdisk_addr, &ramdisk_size);
656         }
657
658         if (status != EFI_SUCCESS)
659                 goto fail2;
660         hdr->ramdisk_image = ramdisk_addr & 0xffffffff;
661         hdr->ramdisk_size  = ramdisk_size & 0xffffffff;
662         boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32;
663         boot_params->ext_ramdisk_size  = (u64)ramdisk_size >> 32;
664
665         return boot_params;
666 fail2:
667         efi_free(sys_table, options_size, hdr->cmd_line_ptr);
668 fail:
669         efi_free(sys_table, 0x4000, (unsigned long)boot_params);
670         return NULL;
671 }
672
673 static void add_e820ext(struct boot_params *params,
674                         struct setup_data *e820ext, u32 nr_entries)
675 {
676         struct setup_data *data;
677         efi_status_t status;
678         unsigned long size;
679
680         e820ext->type = SETUP_E820_EXT;
681         e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
682         e820ext->next = 0;
683
684         data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
685
686         while (data && data->next)
687                 data = (struct setup_data *)(unsigned long)data->next;
688
689         if (data)
690                 data->next = (unsigned long)e820ext;
691         else
692                 params->hdr.setup_data = (unsigned long)e820ext;
693 }
694
695 static efi_status_t setup_e820(struct boot_params *params,
696                                struct setup_data *e820ext, u32 e820ext_size)
697 {
698         struct boot_e820_entry *entry = params->e820_table;
699         struct efi_info *efi = &params->efi_info;
700         struct boot_e820_entry *prev = NULL;
701         u32 nr_entries;
702         u32 nr_desc;
703         int i;
704
705         nr_entries = 0;
706         nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
707
708         for (i = 0; i < nr_desc; i++) {
709                 efi_memory_desc_t *d;
710                 unsigned int e820_type = 0;
711                 unsigned long m = efi->efi_memmap;
712
713 #ifdef CONFIG_X86_64
714                 m |= (u64)efi->efi_memmap_hi << 32;
715 #endif
716
717                 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
718                 switch (d->type) {
719                 case EFI_RESERVED_TYPE:
720                 case EFI_RUNTIME_SERVICES_CODE:
721                 case EFI_RUNTIME_SERVICES_DATA:
722                 case EFI_MEMORY_MAPPED_IO:
723                 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
724                 case EFI_PAL_CODE:
725                         e820_type = E820_TYPE_RESERVED;
726                         break;
727
728                 case EFI_UNUSABLE_MEMORY:
729                         e820_type = E820_TYPE_UNUSABLE;
730                         break;
731
732                 case EFI_ACPI_RECLAIM_MEMORY:
733                         e820_type = E820_TYPE_ACPI;
734                         break;
735
736                 case EFI_LOADER_CODE:
737                 case EFI_LOADER_DATA:
738                 case EFI_BOOT_SERVICES_CODE:
739                 case EFI_BOOT_SERVICES_DATA:
740                 case EFI_CONVENTIONAL_MEMORY:
741                         e820_type = E820_TYPE_RAM;
742                         break;
743
744                 case EFI_ACPI_MEMORY_NVS:
745                         e820_type = E820_TYPE_NVS;
746                         break;
747
748                 case EFI_PERSISTENT_MEMORY:
749                         e820_type = E820_TYPE_PMEM;
750                         break;
751
752                 default:
753                         continue;
754                 }
755
756                 /* Merge adjacent mappings */
757                 if (prev && prev->type == e820_type &&
758                     (prev->addr + prev->size) == d->phys_addr) {
759                         prev->size += d->num_pages << 12;
760                         continue;
761                 }
762
763                 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
764                         u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
765                                    sizeof(struct setup_data);
766
767                         if (!e820ext || e820ext_size < need)
768                                 return EFI_BUFFER_TOO_SMALL;
769
770                         /* boot_params map full, switch to e820 extended */
771                         entry = (struct boot_e820_entry *)e820ext->data;
772                 }
773
774                 entry->addr = d->phys_addr;
775                 entry->size = d->num_pages << PAGE_SHIFT;
776                 entry->type = e820_type;
777                 prev = entry++;
778                 nr_entries++;
779         }
780
781         if (nr_entries > ARRAY_SIZE(params->e820_table)) {
782                 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
783
784                 add_e820ext(params, e820ext, nr_e820ext);
785                 nr_entries -= nr_e820ext;
786         }
787
788         params->e820_entries = (u8)nr_entries;
789
790         return EFI_SUCCESS;
791 }
792
793 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
794                                   u32 *e820ext_size)
795 {
796         efi_status_t status;
797         unsigned long size;
798
799         size = sizeof(struct setup_data) +
800                 sizeof(struct e820_entry) * nr_desc;
801
802         if (*e820ext) {
803                 efi_call_early(free_pool, *e820ext);
804                 *e820ext = NULL;
805                 *e820ext_size = 0;
806         }
807
808         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
809                                 size, (void **)e820ext);
810         if (status == EFI_SUCCESS)
811                 *e820ext_size = size;
812
813         return status;
814 }
815
816 struct exit_boot_struct {
817         struct boot_params *boot_params;
818         struct efi_info *efi;
819         struct setup_data *e820ext;
820         __u32 e820ext_size;
821         bool is64;
822 };
823
824 static efi_status_t exit_boot_func(efi_system_table_t *sys_table_arg,
825                                    struct efi_boot_memmap *map,
826                                    void *priv)
827 {
828         static bool first = true;
829         const char *signature;
830         __u32 nr_desc;
831         efi_status_t status;
832         struct exit_boot_struct *p = priv;
833
834         if (first) {
835                 nr_desc = *map->buff_size / *map->desc_size;
836                 if (nr_desc > ARRAY_SIZE(p->boot_params->e820_table)) {
837                         u32 nr_e820ext = nr_desc -
838                                         ARRAY_SIZE(p->boot_params->e820_table);
839
840                         status = alloc_e820ext(nr_e820ext, &p->e820ext,
841                                                &p->e820ext_size);
842                         if (status != EFI_SUCCESS)
843                                 return status;
844                 }
845                 first = false;
846         }
847
848         signature = p->is64 ? EFI64_LOADER_SIGNATURE : EFI32_LOADER_SIGNATURE;
849         memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
850
851         p->efi->efi_systab = (unsigned long)sys_table_arg;
852         p->efi->efi_memdesc_size = *map->desc_size;
853         p->efi->efi_memdesc_version = *map->desc_ver;
854         p->efi->efi_memmap = (unsigned long)*map->map;
855         p->efi->efi_memmap_size = *map->map_size;
856
857 #ifdef CONFIG_X86_64
858         p->efi->efi_systab_hi = (unsigned long)sys_table_arg >> 32;
859         p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32;
860 #endif
861
862         return EFI_SUCCESS;
863 }
864
865 static efi_status_t exit_boot(struct boot_params *boot_params,
866                               void *handle, bool is64)
867 {
868         unsigned long map_sz, key, desc_size, buff_size;
869         efi_memory_desc_t *mem_map;
870         struct setup_data *e820ext;
871         __u32 e820ext_size;
872         efi_status_t status;
873         __u32 desc_version;
874         struct efi_boot_memmap map;
875         struct exit_boot_struct priv;
876
877         map.map =               &mem_map;
878         map.map_size =          &map_sz;
879         map.desc_size =         &desc_size;
880         map.desc_ver =          &desc_version;
881         map.key_ptr =           &key;
882         map.buff_size =         &buff_size;
883         priv.boot_params =      boot_params;
884         priv.efi =              &boot_params->efi_info;
885         priv.e820ext =          NULL;
886         priv.e820ext_size =     0;
887         priv.is64 =             is64;
888
889         /* Might as well exit boot services now */
890         status = efi_exit_boot_services(sys_table, handle, &map, &priv,
891                                         exit_boot_func);
892         if (status != EFI_SUCCESS)
893                 return status;
894
895         e820ext = priv.e820ext;
896         e820ext_size = priv.e820ext_size;
897         /* Historic? */
898         boot_params->alt_mem_k = 32 * 1024;
899
900         status = setup_e820(boot_params, e820ext, e820ext_size);
901         if (status != EFI_SUCCESS)
902                 return status;
903
904         return EFI_SUCCESS;
905 }
906
907 /*
908  * On success we return a pointer to a boot_params structure, and NULL
909  * on failure.
910  */
911 struct boot_params *efi_main(struct efi_config *c,
912                              struct boot_params *boot_params)
913 {
914         struct desc_ptr *gdt = NULL;
915         efi_loaded_image_t *image;
916         struct setup_header *hdr = &boot_params->hdr;
917         efi_status_t status;
918         struct desc_struct *desc;
919         void *handle;
920         efi_system_table_t *_table;
921         bool is64;
922
923         efi_early = c;
924
925         _table = (efi_system_table_t *)(unsigned long)efi_early->table;
926         handle = (void *)(unsigned long)efi_early->image_handle;
927         is64 = efi_early->is64;
928
929         sys_table = _table;
930
931         /* Check if we were booted by the EFI firmware */
932         if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
933                 goto fail;
934
935         if (is64)
936                 setup_boot_services64(efi_early);
937         else
938                 setup_boot_services32(efi_early);
939
940         /*
941          * If the boot loader gave us a value for secure_boot then we use that,
942          * otherwise we ask the BIOS.
943          */
944         if (boot_params->secure_boot == efi_secureboot_mode_unset)
945                 boot_params->secure_boot = efi_get_secureboot(sys_table);
946
947         /* Ask the firmware to clear memory on unclean shutdown */
948         efi_enable_reset_attack_mitigation(sys_table);
949         efi_retrieve_tpm2_eventlog(sys_table);
950
951         setup_graphics(boot_params);
952
953         setup_efi_pci(boot_params);
954
955         setup_quirks(boot_params);
956
957         status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
958                                 sizeof(*gdt), (void **)&gdt);
959         if (status != EFI_SUCCESS) {
960                 efi_printk(sys_table, "Failed to alloc mem for gdt structure\n");
961                 goto fail;
962         }
963
964         gdt->size = 0x800;
965         status = efi_low_alloc(sys_table, gdt->size, 8,
966                            (unsigned long *)&gdt->address);
967         if (status != EFI_SUCCESS) {
968                 efi_printk(sys_table, "Failed to alloc mem for gdt\n");
969                 goto fail;
970         }
971
972         /*
973          * If the kernel isn't already loaded at the preferred load
974          * address, relocate it.
975          */
976         if (hdr->pref_address != hdr->code32_start) {
977                 unsigned long bzimage_addr = hdr->code32_start;
978                 status = efi_relocate_kernel(sys_table, &bzimage_addr,
979                                              hdr->init_size, hdr->init_size,
980                                              hdr->pref_address,
981                                              hdr->kernel_alignment);
982                 if (status != EFI_SUCCESS) {
983                         efi_printk(sys_table, "efi_relocate_kernel() failed!\n");
984                         goto fail;
985                 }
986
987                 hdr->pref_address = hdr->code32_start;
988                 hdr->code32_start = bzimage_addr;
989         }
990
991         status = exit_boot(boot_params, handle, is64);
992         if (status != EFI_SUCCESS) {
993                 efi_printk(sys_table, "exit_boot() failed!\n");
994                 goto fail;
995         }
996
997         memset((char *)gdt->address, 0x0, gdt->size);
998         desc = (struct desc_struct *)gdt->address;
999
1000         /* The first GDT is a dummy. */
1001         desc++;
1002
1003         if (IS_ENABLED(CONFIG_X86_64)) {
1004                 /* __KERNEL32_CS */
1005                 desc->limit0 = 0xffff;
1006                 desc->base0 = 0x0000;
1007                 desc->base1 = 0x0000;
1008                 desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1009                 desc->s = DESC_TYPE_CODE_DATA;
1010                 desc->dpl = 0;
1011                 desc->p = 1;
1012                 desc->limit1 = 0xf;
1013                 desc->avl = 0;
1014                 desc->l = 0;
1015                 desc->d = SEG_OP_SIZE_32BIT;
1016                 desc->g = SEG_GRANULARITY_4KB;
1017                 desc->base2 = 0x00;
1018                 desc++;
1019         } else {
1020                 /* Second entry is unused on 32-bit */
1021                 desc++;
1022         }
1023
1024         /* __KERNEL_CS */
1025         desc->limit0 = 0xffff;
1026         desc->base0 = 0x0000;
1027         desc->base1 = 0x0000;
1028         desc->type = SEG_TYPE_CODE | SEG_TYPE_EXEC_READ;
1029         desc->s = DESC_TYPE_CODE_DATA;
1030         desc->dpl = 0;
1031         desc->p = 1;
1032         desc->limit1 = 0xf;
1033         desc->avl = 0;
1034         if (IS_ENABLED(CONFIG_X86_64)) {
1035                 desc->l = 1;
1036                 desc->d = 0;
1037         } else {
1038                 desc->l = 0;
1039                 desc->d = SEG_OP_SIZE_32BIT;
1040         }
1041         desc->g = SEG_GRANULARITY_4KB;
1042         desc->base2 = 0x00;
1043         desc++;
1044
1045         /* __KERNEL_DS */
1046         desc->limit0 = 0xffff;
1047         desc->base0 = 0x0000;
1048         desc->base1 = 0x0000;
1049         desc->type = SEG_TYPE_DATA | SEG_TYPE_READ_WRITE;
1050         desc->s = DESC_TYPE_CODE_DATA;
1051         desc->dpl = 0;
1052         desc->p = 1;
1053         desc->limit1 = 0xf;
1054         desc->avl = 0;
1055         desc->l = 0;
1056         desc->d = SEG_OP_SIZE_32BIT;
1057         desc->g = SEG_GRANULARITY_4KB;
1058         desc->base2 = 0x00;
1059         desc++;
1060
1061         if (IS_ENABLED(CONFIG_X86_64)) {
1062                 /* Task segment value */
1063                 desc->limit0 = 0x0000;
1064                 desc->base0 = 0x0000;
1065                 desc->base1 = 0x0000;
1066                 desc->type = SEG_TYPE_TSS;
1067                 desc->s = 0;
1068                 desc->dpl = 0;
1069                 desc->p = 1;
1070                 desc->limit1 = 0x0;
1071                 desc->avl = 0;
1072                 desc->l = 0;
1073                 desc->d = 0;
1074                 desc->g = SEG_GRANULARITY_4KB;
1075                 desc->base2 = 0x00;
1076                 desc++;
1077         }
1078
1079         asm volatile("cli");
1080         asm volatile ("lgdt %0" : : "m" (*gdt));
1081
1082         return boot_params;
1083 fail:
1084         efi_printk(sys_table, "efi_main() failed!\n");
1085         return NULL;
1086 }