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