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