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