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