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