6874acc51aa32e2eea7b9d07231a06e916211624
[debian/elilo] / x86_64 / system.c
1 /*
2  *  Copyright (C) 2001-2003 Hewlett-Packard Co.
3  *      Contributed by Stephane Eranian <eranian@hpl.hp.com>
4  *      Contributed by Mike Johnston <johnston@intel.com>
5  *      Contributed by Chris Ahna <christopher.j.ahna@intel.com>
6  *      Contributed by Fenghua Yu <fenghua.yu@intel.com>
7  *      Contributed by Bibo Mao <bibo.mao@intel.com>
8  *      Contributed by chandramouli narayanan <mouli@linux.intel.com>
9  *      Edgar Hucek <hostmaster@ed-soft.at>
10  *      
11  *  Updated with code to fill bootparam converting EFI memory map to E820 
12  *  based on a Linux kernel patch provided by Edgar Hucek
13  *  - mouli 06/20/2007
14  *
15  * This file is part of the ELILO, the EFI Linux boot loader.
16  *
17  *  ELILO is free software; you can redistribute it and/or modify
18  *  it under the terms of the GNU General Public License as published by
19  *  the Free Software Foundation; either version 2, or (at your option)
20  *  any later version.
21  *
22  *  ELILO is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  *
27  *  You should have received a copy of the GNU General Public License
28  *  along with ELILO; see the file COPYING.  If not, write to the Free
29  *  Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30  *  02111-1307, USA.
31  *
32  * Please check out the elilo.txt for complete documentation on how
33  * to use this program.
34  */
35
36 /*
37  * This file contains all the x86_64 specific code expected by generic loader
38  */
39 #include <efi.h>
40 #include <efilib.h>
41
42 #include "elilo.h"
43 #include "loader.h"
44 #include "rmswitch.h"
45
46 extern loader_ops_t bzimage_loader, plain_loader, gzip_loader; 
47
48 /*
49  * Descriptor table base addresses & limits for Linux startup.
50  */
51
52 dt_addr_t gdt_addr = { 0x800, 0x94000 };
53 dt_addr_t idt_addr = { 0, 0 }; 
54
55 /*
56  * Initial GDT layout for Linux startup.
57  */
58
59 UINT16 init_gdt[] = {
60         /* gdt[0]: dummy */
61         0, 0, 0, 0, 
62         
63         /* gdt[1]: unused */
64         0, 0, 0, 0,
65
66         /* gdt[2]: code */
67         0xFFFF,         /* 4Gb - (0x100000*0x1000 = 4Gb) */
68         0x0000,         /* base address=0 */
69         0x9A00,         /* code read/exec */
70         0x00CF,         /* granularity=4096, 386 (+5th nibble of limit) */
71
72         /* gdt[3]: data */
73         0xFFFF,         /* 4Gb - (0x100000*0x1000 = 4Gb) */
74         0x0000,         /* base address=0 */
75         0x9200,         /* data read/write */
76         0x00CF,         /* granularity=4096, 386 (+5th nibble of limit) */
77 };
78
79 UINTN sizeof_init_gdt = sizeof init_gdt;
80
81 /*
82  * Highest available base memory address.
83  *
84  * For traditional kernels and loaders this is always at 0x90000.
85  * For updated kernels and loaders this is computed by taking the
86  * highest available base memory address and rounding down to the
87  * nearest 64 kB boundary and then subtracting 64 kB.
88  *
89  * A non-compressed kernel is automatically assumed to be an updated
90  * kernel.  A compressed kernel that has bit 6 (0x40) set in the
91  * loader_flags field is also assumed to be an updated kernel.
92  */
93
94 UINTN high_base_mem = 0x90000;
95
96 /*
97  * Highest available extended memory address.
98  *
99  * This is computed by taking the highest available extended memory
100  * address and rounding down to the nearest EFI_PAGE_SIZE (usually
101  * 4 kB) boundary.  
102  * This is only used for backward compatibility.
103  */
104
105 UINTN high_ext_mem = 32 * 1024 * 1024;
106
107 /* This starting address will hold true for all of the loader types for now */
108 VOID *kernel_start = (void *)DEFAULT_KERNEL_START;
109
110 /* The kernel may load elsewhere if EFI firmware reserves kernel_start */
111 VOID *kernel_load_address = DEFAULT_KERNEL_START; 
112
113 VOID *initrd_start = NULL;
114 UINTN initrd_size = 0;
115
116 INTN
117 sysdeps_init(EFI_HANDLE dev)
118 {
119         DBG_PRT((L"sysdeps_init()\n"));
120
121         /*
122          * Register our loader(s)...
123          */
124
125         loader_register(&bzimage_loader);
126         loader_register(&plain_loader);         
127         loader_register(&gzip_loader); 
128         return 0;
129 }
130
131 /*
132  * initrd_get_addr()
133  *      Compute a starting address for the initial RAMdisk image.
134  *      For now, this image is placed immediately after the end of
135  *      the kernel memory.  Inside the start_kernel() code, the
136  *      RAMdisk image will be relocated to the top of available
137  *      extended memory.
138  */
139 INTN
140 sysdeps_initrd_get_addr(kdesc_t *kd, memdesc_t *imem)
141 {
142         DBG_PRT((L"initrd_get_addr()\n"));
143
144         if (!kd || !imem) {
145                 ERR_PRT((L"kd="PTR_FMT" imem="PTR_FMT"", kd, imem));
146                 return -1;
147         }
148
149         VERB_PRT(3, Print(L"kstart="PTR_FMT"  kentry="PTR_FMT"  kend="PTR_FMT"\n", 
150                 kd->kstart, kd->kentry, kd->kend));
151
152         imem->start_addr = kd->kend;
153
154         VERB_PRT(3, Print(L"initrd start_addr="PTR_FMT" pgcnt=%d\n", 
155                 imem->start_addr, imem->pgcnt));
156
157         return 0;
158 }
159
160 VOID
161 sysdeps_free_boot_params(boot_params_t *bp)
162 {
163         mmap_desc_t md;
164
165         ZeroMem(&md, sizeof md);
166         md.md = (VOID *)(UINT64)bp->s.efi_mem_map;
167         free_memmap(&md);
168 }
169
170 static VOID find_bits(unsigned long mask, UINT8 *first, UINT8* len) {
171         unsigned char bit_pos = 0, bit_len = 0;
172         *first =0;
173         *len = 0;
174         if (mask == 0)
175                 return;
176         while (!(mask & 0x1)) {
177                 mask = mask >> 1;
178                 bit_pos++;
179         }
180         while (mask & 0x1) {
181                 mask = mask >> 1;
182                 bit_len++;
183         }
184         *first = bit_pos;
185         *len = bit_len;
186 }
187
188 /*
189  * Get video information.
190  */
191 static INTN get_video_info(boot_params_t * bp) {
192         EFI_GUID GopProtocol = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
193         EFI_GRAPHICS_OUTPUT_PROTOCOL *Gop_interface;
194         EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Gop_info;
195         EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE   *Gop_mode = NULL;
196         EFI_HANDLE *Gop_handle = NULL;
197         EFI_STATUS efi_status;
198         UINTN size = 0;
199         UINTN size1;
200         UINT8 i;
201
202         efi_status = uefi_call_wrapper(
203                         BS->LocateHandle,
204                         5,
205                         ByProtocol,
206                         &GopProtocol,
207                         NULL,
208                         &size,
209                         (VOID **)Gop_handle);
210         
211         if (EFI_ERROR(efi_status) && efi_status != EFI_BUFFER_TOO_SMALL) {
212                 ERR_PRT((L"LocateHandle GopProtocol failed."));
213                 return -1;
214         }
215         Gop_handle = alloc(size, 0);
216         efi_status = uefi_call_wrapper(
217                         BS->LocateHandle,
218                         5,
219                         ByProtocol,
220                         &GopProtocol,
221                         NULL,
222                         &size,
223                         (VOID **)Gop_handle);
224         if (EFI_ERROR(efi_status)) {
225                 ERR_PRT((L"LocateHandle GopProtocol failed."));
226                 free(Gop_handle);
227                 return -1;
228         }
229
230         for (i=0; i < size/sizeof(EFI_HANDLE); i++) {
231                 Gop_handle += i;
232                 efi_status = uefi_call_wrapper(
233                                 BS->HandleProtocol,
234                                 3,
235                                 *Gop_handle,
236                                 &GopProtocol,
237                                 &Gop_interface);
238
239                 if (EFI_ERROR(efi_status)) {
240                         continue;
241                 }
242                 Gop_mode = Gop_interface->Mode;
243                 efi_status = uefi_call_wrapper(
244                                 Gop_interface->QueryMode,
245                                 4,
246                                 Gop_interface,
247                                 Gop_mode->Mode,
248                                 &size1,
249                                 &Gop_info);
250                 if (!EFI_ERROR(efi_status))
251                         break;
252                 if (EFI_ERROR(efi_status)) {
253                         continue;
254                 }
255         }
256         if (EFI_ERROR(efi_status) || i > (size/sizeof(EFI_HANDLE))) {
257                 ERR_PRT((L"HandleProtocol GopProtocol failed."));
258                 free(Gop_handle);
259                 return -1;
260         }
261                 
262         bp->s.is_vga = 0x70;
263         bp->s.orig_cursor_col = 0;
264         bp->s.orig_cursor_row = 0;
265         bp->s.orig_video_page = 0;
266         bp->s.orig_video_mode = 0;
267         bp->s.orig_video_cols = 0;
268         bp->s.orig_video_rows = 0;
269         bp->s.orig_ega_bx = 0;
270         bp->s.orig_video_points = 0;
271
272         bp->s.lfb_width = Gop_info->HorizontalResolution;
273         bp->s.lfb_height = Gop_info->VerticalResolution;
274         bp->s.lfb_base = Gop_mode->FrameBufferBase;
275         bp->s.lfb_size = Gop_mode->FrameBufferSize;
276         bp->s.lfb_pages = 1;
277         bp->s.vesa_seg = 0;
278         bp->s.vesa_off = 0;
279         if (Gop_info->PixelFormat == PixelRedGreenBlueReserved8BitPerColor) {
280                 bp->s.lfb_depth = 32;
281                 bp->s.lfb_red_size = 8;
282                 bp->s.lfb_red_pos = 0;
283                 bp->s.lfb_green_size = 8;
284                 bp->s.lfb_green_pos = 8;
285                 bp->s.lfb_blue_size = 8;
286                 bp->s.lfb_blue_pos = 16;
287                 bp->s.lfb_rsvd_size = 8;
288                 bp->s.lfb_rsvd_pos = 24;
289                 bp->s.lfb_line_len = Gop_info->PixelsPerScanLine * 4;
290
291         } else if (Gop_info->PixelFormat == PixelBlueGreenRedReserved8BitPerColor) {
292                 bp->s.lfb_depth = 32;
293                 bp->s.lfb_red_size = 8;
294                 bp->s.lfb_red_pos = 16;
295                 bp->s.lfb_green_size = 8;
296                 bp->s.lfb_green_pos = 8;
297                 bp->s.lfb_blue_size = 8;
298                 bp->s.lfb_blue_pos = 0;
299                 bp->s.lfb_rsvd_size = 8;
300                 bp->s.lfb_rsvd_pos = 24;
301                 bp->s.lfb_line_len = Gop_info->PixelsPerScanLine * 4;
302         } else if (Gop_info->PixelFormat == PixelBitMask) {
303                 find_bits(Gop_info->PixelInformation.RedMask,
304                           &bp->s.lfb_red_pos, &bp->s.lfb_red_size);
305                 find_bits(Gop_info->PixelInformation.GreenMask,
306                           &bp->s.lfb_green_pos, &bp->s.lfb_green_size);
307                 find_bits(Gop_info->PixelInformation.BlueMask,
308                           &bp->s.lfb_blue_pos, &bp->s.lfb_blue_size);
309                 find_bits(Gop_info->PixelInformation.ReservedMask,
310                           &bp->s.lfb_rsvd_pos, &bp->s.lfb_rsvd_size);
311                 bp->s.lfb_depth = bp->s.lfb_red_size + bp->s.lfb_green_size +
312                                   bp->s.lfb_blue_size + bp->s.lfb_rsvd_size;
313                 bp->s.lfb_line_len = (Gop_info->PixelsPerScanLine * bp->s.lfb_depth) / 8;
314         } else {
315                 bp->s.lfb_depth = 4;
316                 bp->s.lfb_red_size = 0;
317                 bp->s.lfb_red_pos = 0;
318                 bp->s.lfb_green_size = 0;
319                 bp->s.lfb_green_pos = 0;
320                 bp->s.lfb_blue_size = 0;
321                 bp->s.lfb_blue_pos = 0;
322                 bp->s.lfb_rsvd_size = 0;
323                 bp->s.lfb_rsvd_pos = 0;
324                 bp->s.lfb_line_len = bp->s.lfb_width / 2;
325         }
326         return 0;
327 }
328
329 /* Convert EFI memory map to E820 map for the operating system 
330  * This code is based on a Linux kernel patch submitted by Edgar Hucek
331  */
332
333 /* Add a memory region to the e820 map */
334 static void add_memory_region (struct e820entry *e820_map,
335                                int *e820_nr_map,
336                                unsigned long long start,
337                                unsigned long size,
338                                unsigned int type)
339 {
340         int x = *e820_nr_map;
341
342         if (x == E820_MAX) {
343                 Print(L"Too many entries in the memory map!\n");
344                 return;
345         }
346
347         if ((x > 0) && e820_map[x-1].addr + e820_map[x-1].size == start
348             && e820_map[x-1].type == type)
349                 e820_map[x-1].size += size;
350         else {
351                 e820_map[x].addr = start;
352                 e820_map[x].size = size;
353                 e820_map[x].type = type;
354                 (*e820_nr_map)++;
355         }
356 }
357
358 void fill_e820map(boot_params_t *bp, mmap_desc_t *mdesc)
359 {
360         int nr_map, e820_nr_map = 0, i;
361         UINT64 start, end, size;
362         EFI_MEMORY_DESCRIPTOR   *md, *p;
363         struct e820entry *e820_map;
364
365         nr_map = mdesc->map_size/mdesc->desc_size;
366         e820_map = (struct e820entry *)bp->s.e820_map;
367                         
368         for (i = 0, p = mdesc->md; i < nr_map; i++)
369         {
370                 md = p;
371                 switch (md->Type) {
372                 case EfiACPIReclaimMemory:
373                         add_memory_region(e820_map, &e820_nr_map,
374                                           md->PhysicalStart,
375                                           md->NumberOfPages << EFI_PAGE_SHIFT,
376                                           E820_ACPI);
377                         break;
378                 case EfiRuntimeServicesCode:
379                         add_memory_region(e820_map, &e820_nr_map,
380                                           md->PhysicalStart,
381                                           md->NumberOfPages << EFI_PAGE_SHIFT,
382                                           E820_EXEC_CODE);
383                         break;
384                 case EfiRuntimeServicesData:
385                 case EfiReservedMemoryType:
386                 case EfiMemoryMappedIO:
387                 case EfiMemoryMappedIOPortSpace:
388                 case EfiUnusableMemory:
389                 case EfiPalCode:
390                         add_memory_region(e820_map, &e820_nr_map,
391                                           md->PhysicalStart,
392                                           md->NumberOfPages << EFI_PAGE_SHIFT,
393                                           E820_RESERVED);
394                         break;
395                 case EfiLoaderCode:
396                 case EfiLoaderData:
397                 case EfiBootServicesCode:
398                 case EfiBootServicesData:
399                 case EfiConventionalMemory:
400                         start = md->PhysicalStart;
401                         size = md->NumberOfPages << EFI_PAGE_SHIFT;
402                         end = start + size;
403                         /* Fix up for BIOS that claims RAM in 640K-1MB region */
404                         if (start < 0x100000ULL && end > 0xA0000ULL) {
405                                 if (start < 0xA0000ULL) {
406                                         /* start < 640K
407                                          * set memory map from start to 640K
408                                          */
409                                         add_memory_region(e820_map,
410                                                           &e820_nr_map,
411                                                           start,
412                                                           0xA0000ULL-start,
413                                                           E820_RAM);
414                                 }
415                                 if (end <= 0x100000ULL)
416                                         continue;
417                                 /* end > 1MB
418                                  * set memory map avoiding 640K to 1MB hole
419                                  */
420                                 start = 0x100000ULL;
421                                 size = end - start;
422                         }
423                         add_memory_region(e820_map, &e820_nr_map,
424                                           start, size, E820_RAM);
425                         break;
426                 case EfiACPIMemoryNVS:
427                         add_memory_region(e820_map, &e820_nr_map,
428                                           md->PhysicalStart,
429                                           md->NumberOfPages << EFI_PAGE_SHIFT,
430                                           E820_NVS);
431                         break;
432                 default:
433                         /* We should not hit this case */
434                         add_memory_region(e820_map, &e820_nr_map,
435                                           md->PhysicalStart,
436                                           md->NumberOfPages << EFI_PAGE_SHIFT,
437                                           E820_RESERVED);
438                         break;
439                 }
440                 p = NextMemoryDescriptor(p, mdesc->desc_size); 
441         }
442         bp->s.e820_nrmap = e820_nr_map;
443 }
444
445 /*
446  * x86_64 specific boot parameters initialization routine
447  */
448 INTN
449 sysdeps_create_boot_params(
450         boot_params_t *bp,
451         CHAR8 *cmdline,
452         memdesc_t *initrd,
453         memdesc_t *vmcode,
454         UINTN *cookie)
455 {
456         mmap_desc_t mdesc;
457         EFI_STATUS efi_status;
458         UINTN rows, cols;
459         UINT8 row, col;
460         UINT8 mode;
461         UINT16 hdr_version;
462
463         DBG_PRT((L"fill_boot_params()\n"));
464
465         if (!bp || !cmdline || !initrd || !cookie) {
466                 ERR_PRT((L"bp="PTR_FMT"  cmdline="PTR_FMT"  initrd="PTR_FMT" cookie="PTR_FMT"",
467                         bp, cmdline, initrd, cookie));
468
469                 if (param_start != NULL) {
470                         free(param_start);
471                         param_start = NULL;
472                         param_size = 0;
473                 }
474                 free_kmem();
475                 return -1;
476         }
477
478         /*
479          * Copy temporary boot sector and setup data storage to
480          * elilo allocated boot parameter storage.  We only need
481          * the first two sectors (1K).  The rest of the storage
482          * can be used by the command line.
483          */
484         if (param_start != NULL) {
485                 CopyMem(bp, param_start, 0x2000);
486                 free(param_start);
487                 param_start = NULL;
488                 param_size = 0;
489         }
490         /*
491          * Save off our header revision information.
492          */
493         hdr_version = (bp->s.hdr_major << 8) | bp->s.hdr_minor;
494
495         /*
496          * Clear out unused memory in boot sector image.
497          */
498         bp->s.unused_1 = 0;
499         bp->s.unused_2 = 0;
500         ZeroMem(&bp->s.unused_3, sizeof bp->s.unused_3);
501         ZeroMem(&bp->s.unused_4, sizeof bp->s.unused_4);
502         ZeroMem(&bp->s.unused_51, sizeof bp->s.unused_51);
503         ZeroMem(&bp->s.unused_52, sizeof bp->s.unused_52);
504         bp->s.unused_6 = 0;
505         bp->s.unused_7 = 0;
506         ZeroMem(bp->s.unused_8, sizeof bp->s.unused_8);
507
508         /*
509          * Tell kernel this was loaded by an advanced loader type.
510          * If this field is zero, the initrd_start and initrd_size
511          * fields are ignored by the kernel.
512          */
513
514         bp->s.loader_type = LDRTYPE_ELILO;
515
516         /*
517          * Setup command line information.
518          */
519
520         bp->s.cmdline_magik = CMDLINE_MAGIK;
521         bp->s.cmdline_offset = (UINT8 *)cmdline - (UINT8 *)bp;
522
523         /* 
524          * Clear out the cmdline_addr field so the kernel can find 
525          * the cmdline.
526          */
527         bp->s.cmdline_addr = 0x0;
528
529         /*
530          * Setup hard drive parameters.
531          * %%TBD - It should be okay to zero fill the hard drive
532          * info buffers.  The kernel should do its own detection.
533          */
534
535         ZeroMem(bp->s.hd0_info, sizeof bp->s.hd0_info);
536         ZeroMem(bp->s.hd1_info, sizeof bp->s.hd1_info);
537
538         /*
539          * Memory info.
540          */
541
542         bp->s.alt_mem_k = high_ext_mem / 1024;
543
544         if (bp->s.alt_mem_k <= 65535) 
545                 bp->s.ext_mem_k = (UINT16)bp->s.alt_mem_k;
546         else 
547                 bp->s.ext_mem_k = 65535;
548
549         /*
550          * Initial RAMdisk and root device stuff.
551          */
552
553         DBG_PRT((L"initrd->start_addr="PTR_FMT"  initrd->pgcnt=%d\n",
554                 initrd->start_addr, initrd->pgcnt));
555
556         /* These RAMdisk flags are not needed, just zero them. */
557         bp->s.ramdisk_flags = 0;
558
559         if (initrd->start_addr && initrd->pgcnt) {
560                 /* %%TBD - This will probably have to be changed. */
561                 bp->s.initrd_start = (UINT32)(UINT64)initrd->start_addr;
562                 bp->s.initrd_size = (UINT32)(initrd->size);
563                 /*
564                  * This is the RAMdisk root device for RedHat 2.2.x
565                  * kernels (major 0x01, minor 0x00).
566                  */
567
568                 bp->s.orig_root_dev = 0x0100;
569         } else {
570                 bp->s.initrd_start = 0;
571                 bp->s.initrd_size = 0;
572         }
573
574         /*
575          * APM BIOS info.
576          */
577         bp->s.apm_bios_ver = NO_APM_BIOS;
578         bp->s.bios_code_seg = 0;
579         bp->s.bios_entry_point = 0;
580         bp->s.bios_code_seg16 = 0;
581         bp->s.bios_data_seg = 0;
582         bp->s.apm_bios_flags = 0;
583         bp->s.bios_code_len = 0;
584         bp->s.bios_data_len = 0;
585
586         /*
587          * MCA BIOS info (misnomer).
588          */
589         bp->s.mca_info_len = 0;
590         ZeroMem(bp->s.mca_info_buf, sizeof bp->s.mca_info_buf);
591
592         /*
593          * Pointing device presence.  The kernel will detect this.
594          */
595         bp->s.aux_dev_info = NO_MOUSE;
596
597         /*
598          * EFI loader signature 
599          */
600         CopyMem(bp->s.efi_loader_sig, EFI_LOADER_SIG_X64, 4);
601
602         /*
603          * Kernel entry point.
604          */
605         bp->s.kernel_start = (UINT32)(UINT64)kernel_start;
606
607         /*
608          * When changing stuff in the parameter structure compare
609          * the offsets of the fields with the offsets used in the
610          * boot sector and setup source files.
611          *   arch/x86_64/boot/bootsect.S
612          *   arch/x86_64/boot/setup.S
613          *   arch/x86_64/kernel/setup.c
614          *   include/asm-x86_64/setup.h (2.5/2.6)
615          */
616
617 #define CHECK_OFFSET(n, o, f) \
618 { \
619         UINTN p = (UINT8 *)&bp->s.n - (UINT8 *)bp; \
620         UINTN q = (UINTN)(o); \
621         if (p != q) { \
622                 test |= 1; \
623                 Print(L"%20a:  %3xh  %3xh  ", #n, p, q); \
624                 if (*f) { \
625                         Print(f, bp->s.n); \
626                 } \
627                 Print(L"\n"); \
628         } \
629 }
630
631 #define WAIT_FOR_KEY() \
632 { \
633         EFI_INPUT_KEY key; \
634         while (uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key) != EFI_SUCCESS) { \
635                 ; \
636         } \
637 }
638         {
639                 UINTN test = 0;
640
641                 CHECK_OFFSET(orig_cursor_col, 0x00, L"%xh");
642                 CHECK_OFFSET(orig_cursor_row, 0x01, L"%xh");
643                 CHECK_OFFSET(ext_mem_k, 0x02, L"%xh");
644                 CHECK_OFFSET(orig_video_page, 0x04, L"%xh");
645                 CHECK_OFFSET(orig_video_mode, 0x06, L"%xh");
646                 CHECK_OFFSET(orig_video_cols, 0x07, L"%xh");
647                 CHECK_OFFSET(orig_ega_bx, 0x0A, L"%xh");
648                 CHECK_OFFSET(orig_video_rows, 0x0E, L"%xh");
649                 CHECK_OFFSET(is_vga, 0x0F, L"%xh");
650                 CHECK_OFFSET(orig_video_points, 0x10, L"%xh");
651                 CHECK_OFFSET(lfb_width, 0x12, L"%xh");
652                 CHECK_OFFSET(lfb_height, 0x14, L"%xh");
653                 CHECK_OFFSET(lfb_depth, 0x16, L"%xh");
654                 CHECK_OFFSET(lfb_base, 0x18, L"%xh");
655                 CHECK_OFFSET(lfb_size, 0x1C, L"%xh");
656                 CHECK_OFFSET(cmdline_magik, 0x20, L"%xh");
657                 CHECK_OFFSET(cmdline_offset, 0x22, L"%xh");
658                 CHECK_OFFSET(lfb_line_len, 0x24, L"%xh");
659                 CHECK_OFFSET(lfb_red_size, 0x26, L"%xh");
660                 CHECK_OFFSET(lfb_red_pos, 0x27, L"%xh");
661                 CHECK_OFFSET(lfb_green_size, 0x28, L"%xh");
662                 CHECK_OFFSET(lfb_green_pos, 0x29, L"%xh");
663                 CHECK_OFFSET(lfb_blue_size, 0x2A, L"%xh");
664                 CHECK_OFFSET(lfb_blue_pos, 0x2B, L"%xh");
665                 CHECK_OFFSET(lfb_rsvd_size, 0x2C, L"%xh");
666                 CHECK_OFFSET(lfb_rsvd_pos, 0x2D, L"%xh");
667                 CHECK_OFFSET(vesa_seg, 0x2E, L"%xh");
668                 CHECK_OFFSET(vesa_off, 0x30, L"%xh");
669                 CHECK_OFFSET(lfb_pages, 0x32, L"%xh");
670                 CHECK_OFFSET(lfb_reserved, 0x34, L"");
671                 CHECK_OFFSET(apm_bios_ver, 0x40, L"%xh");
672                 CHECK_OFFSET(bios_code_seg, 0x42, L"%xh");
673                 CHECK_OFFSET(bios_entry_point, 0x44, L"%xh");
674                 CHECK_OFFSET(bios_code_seg16, 0x48, L"%xh");
675                 CHECK_OFFSET(bios_data_seg, 0x4A, L"%xh");
676                 CHECK_OFFSET(apm_bios_flags, 0x4C, L"%xh");
677                 CHECK_OFFSET(bios_code_len, 0x4E, L"%xh");
678                 CHECK_OFFSET(bios_data_len, 0x52, L"%xh");
679                 CHECK_OFFSET(hd0_info, 0x80, L"");
680                 CHECK_OFFSET(hd1_info, 0x90, L"");
681                 CHECK_OFFSET(mca_info_len, 0xA0, L"%xh");
682                 CHECK_OFFSET(mca_info_buf, 0xA2, L"");
683                 CHECK_OFFSET(efi_loader_sig, 0x1C0, L"'%-4.4a'");
684                 CHECK_OFFSET(efi_sys_tbl, 0x1C4, L"%xh");
685                 CHECK_OFFSET(efi_mem_desc_size, 0x1C8, L"%xh");
686                 CHECK_OFFSET(efi_mem_desc_ver, 0x1CC, L"%xh");
687                 CHECK_OFFSET(efi_mem_map, 0x1D0, L"%xh");
688                 CHECK_OFFSET(efi_mem_map_size, 0x1D4, L"%xh");
689                 CHECK_OFFSET(efi_sys_tbl_hi, 0x1D8, L"%xh");
690                 CHECK_OFFSET(efi_mem_map_hi, 0x1DC, L"%xh");
691                 CHECK_OFFSET(alt_mem_k, 0x1E0, L"%xh");
692                 CHECK_OFFSET(setup_sectors, 0x1F1, L"%xh");
693                 CHECK_OFFSET(mount_root_rdonly, 0x1F2, L"%xh");
694                 CHECK_OFFSET(sys_size, 0x1F4, L"%xh");
695                 CHECK_OFFSET(swap_dev, 0x1F6, L"%xh");
696                 CHECK_OFFSET(ramdisk_flags, 0x1F8, L"%xh");
697                 CHECK_OFFSET(video_mode_flag, 0x1FA, L"%xh");
698                 CHECK_OFFSET(orig_root_dev, 0x1FC, L"%xh");
699                 CHECK_OFFSET(aux_dev_info, 0x1FF, L"%xh");
700                 CHECK_OFFSET(jump, 0x200, L"%xh");
701                 CHECK_OFFSET(setup_sig, 0x202, L"'%-4.4a'");
702                 CHECK_OFFSET(hdr_minor, 0x206, L"%xh");
703                 CHECK_OFFSET(hdr_major, 0x207, L"%xh");
704                 CHECK_OFFSET(rm_switch, 0x208, L"%xh");
705                 CHECK_OFFSET(start_sys_seg, 0x20C, L"%xh");
706                 CHECK_OFFSET(kernel_verstr_offset, 0x20E, L"%xh");
707                 CHECK_OFFSET(loader_type, 0x210, L"%xh");
708                 CHECK_OFFSET(loader_flags, 0x211, L"%xh");
709                 CHECK_OFFSET(setup_move_size, 0x212, L"%xh");
710                 CHECK_OFFSET(kernel_start, 0x214, L"%xh");
711                 CHECK_OFFSET(initrd_start, 0x218, L"%xh");
712                 CHECK_OFFSET(initrd_size, 0x21C, L"%xh");
713                 CHECK_OFFSET(bootsect_helper, 0x220, L"%xh");
714                 CHECK_OFFSET(heap_end_ptr, 0x224, L"%xh");
715                 CHECK_OFFSET(cmdline_addr, 0x228, L"%xh");
716
717                 if (test) {
718                         ERR_PRT((L"Boot sector and/or setup parameter alignment error."));
719                         free_kmem();
720                         return -1;
721                 }
722         }
723
724         /*
725          * Get video information.
726          * Do this last so that any other cursor positioning done
727          * in the fill routine gets accounted for.
728          */
729
730         if (!get_video_info(bp)) goto do_memmap;
731                 
732         /* Do the old text mode */
733         efi_status = uefi_call_wrapper(
734                 ST->ConOut->QueryMode,
735                 4,
736                 ST->ConOut,
737                 ST->ConOut->Mode->Mode,
738                 &cols,
739                 &rows);
740
741         if (EFI_ERROR(efi_status)) {
742                 ERR_PRT((L"QueryMode failed.  Fake it."));
743                 mode = 3;
744                 rows = 25;
745                 cols = 80;
746                 row = 24;
747                 col = 0;
748         } else {
749                 mode = (UINT8)ST->ConOut->Mode->Mode;
750                 col = (UINT8)ST->ConOut->Mode->CursorColumn;
751                 row = (UINT8)ST->ConOut->Mode->CursorRow;
752         }
753
754         bp->s.orig_cursor_col = col;
755         bp->s.orig_cursor_row = row;
756         bp->s.orig_video_page = 0;
757         bp->s.orig_video_mode = mode;
758         bp->s.orig_video_cols = (UINT8)cols;
759         bp->s.orig_video_rows = (UINT8)rows;
760
761         bp->s.orig_ega_bx = 0;
762         bp->s.is_vga = 0;
763         bp->s.orig_video_points = 16; 
764
765         bp->s.lfb_width = 0;
766         bp->s.lfb_height = 0;
767         bp->s.lfb_depth = 0;
768         bp->s.lfb_base = 0;
769         bp->s.lfb_size = 0;
770         bp->s.lfb_line_len = 0;
771         bp->s.lfb_red_size = 0;
772         bp->s.lfb_red_pos = 0;
773         bp->s.lfb_green_size = 0;
774         bp->s.lfb_green_pos = 0;
775         bp->s.lfb_blue_size = 0;
776         bp->s.lfb_blue_pos = 0;
777         bp->s.lfb_rsvd_size = 0;
778         bp->s.lfb_rsvd_pos = 0;
779         bp->s.lfb_pages = 0;
780         bp->s.vesa_seg = 0;
781         bp->s.vesa_off = 0;
782
783 do_memmap:
784         /*
785          * Get memory map description and cookie for ExitBootServices()
786          */
787
788         if (get_memmap(&mdesc)) {
789                 ERR_PRT((L"Could not get memory map."));
790                 free_kmem();
791                 return -1;
792         }
793         *cookie = mdesc.cookie;
794         bp->s.efi_mem_map = (UINT32)(unsigned long)mdesc.md;
795         bp->s.efi_mem_map_size = mdesc.map_size;
796         bp->s.efi_mem_desc_size = mdesc.desc_size;
797         bp->s.efi_mem_desc_ver = mdesc.desc_version;
798         bp->s.efi_sys_tbl = (UINT32)(unsigned long)systab;
799         bp->s.efi_mem_map_hi = (unsigned long)mdesc.md >> 32;
800         bp->s.efi_sys_tbl_hi = (unsigned long)systab >> 32;
801         /* Now that we have EFI memory map, convert it to E820 map 
802          * and update the bootparam accordingly
803          */
804         fill_e820map(bp, &mdesc);
805         
806         return 0;
807 }