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