Imported Upstream version 3.4
[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
37 #include "rmswitch.h"
38
39 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
40 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
41
42 /* extern loader_ops_t plain_loader, gzip_loader; */
43
44 efi_ia32_boot_params_t efi_ia32_bp;
45
46
47 /*
48  * Descriptor table base addresses & limits for Linux startup.
49  */
50
51 dt_addr_t gdt_addr = { 0x800, 0x94000 };
52 dt_addr_t idt_addr = { 0, 0 };
53
54 /*
55  * Initial GDT layout for Linux startup.
56  */
57
58 UINT16 init_gdt[] = {
59         /* gdt[0]: dummy */
60         0, 0, 0, 0, 
61         
62         /* gdt[1]: unused */
63         0, 0, 0, 0,
64
65         /* gdt[2]: code */
66         0xFFFF,         /* 4Gb - (0x100000*0x1000 = 4Gb) */
67         0x0000,         /* base address=0 */
68         0x9A00,         /* code read/exec */
69         0x00CF,         /* granularity=4096, 386 (+5th nibble of limit) */
70
71         /* gdt[3]: data */
72         0xFFFF,         /* 4Gb - (0x100000*0x1000 = 4Gb) */
73         0x0000,         /* base address=0 */
74         0x9200,         /* data read/write */
75         0x00CF,         /* granularity=4096, 386 (+5th nibble of limit) */
76 };
77
78 UINTN sizeof_init_gdt = sizeof init_gdt;
79
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.  The ia32 Linux kernel can only support up to
102  * 2 GB (AFAIK).
103  */
104
105 UINTN high_ext_mem = 32 * 1024 * 1024;
106
107 /*
108  * Starting location and size of runtime memory blocks.
109  */
110
111 boot_params_t *param_start = NULL;
112 UINTN param_size = 0;
113
114 VOID *kernel_start = (VOID *)0x100000;  /* 1M */
115 UINTN kernel_size = 0x200000;           /* 2M (largest x86 kernel image) */
116
117 VOID *initrd_start = NULL;
118 UINTN initrd_size = 0;
119
120 /*
121  * Boot parameters can be relocated if TRUE.
122  * Boot parameters must be placed at 0x90000 if FALSE.
123  *
124  * This will be set to TRUE if bit 6 (0x40) is set in the loader_flags
125  * field in a compressed x86 boot format kernel.  This will also be set
126  * to TRUE if the kernel is an uncompressed ELF32 image.
127  *
128  * To remote boot w/ the universal network driver and a 16-bit UNDI
129  * this must be set to TRUE.
130  */
131
132 BOOLEAN can_reloc_boot_params = FALSE;
133
134 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
135 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136 static INTN
137 probe_bzImage_boot(CHAR16 *kname)
138 {
139         EFI_STATUS efi_status;
140         UINTN size;
141         fops_fd_t fd;
142         UINT8 bootsect[512];
143
144         DBG_PRT((L"probe_bzImage_boot()\n"));
145
146         if (!kname) {
147                 ERR_PRT((L"kname == %xh", kname));
148                 free_kmem();
149                 return -1;
150         }
151
152         /*
153          * Open kernel image.
154          */
155
156         DBG_PRT((L"opening %s...\n", kname));
157
158         efi_status = fops_open(kname, &fd);
159
160         if (EFI_ERROR(efi_status)) {
161                 ERR_PRT((L"Could not open %s.", kname));
162                 free_kmem();
163                 return -1;
164         }
165
166         /*
167          * Read boot sector.
168          */
169
170         DBG_PRT((L"\nreading boot sector...\n"));
171
172         size = sizeof bootsect;
173         efi_status = fops_read(fd, bootsect, &size);
174
175         if (EFI_ERROR(efi_status) || size != sizeof bootsect) {
176                 ERR_PRT((L"Could not read boot sector from %s.", kname));
177                 fops_close(fd);
178                 free_kmem();
179                 return -1;
180         }
181
182         /*
183          * Verify boot sector signature.
184          */
185
186         if (bootsect[0x1FE] != 0x55 || bootsect[0x1FF] != 0xAA) {
187                 ERR_PRT((L"%s is not a bzImage kernel image.\n", kname));
188                 fops_close(fd);
189                 free_kmem();
190                 return -1;
191         }
192
193         /*
194          * Check for out of range setup data size.
195          * Will almost always be 7, but we will accept 1 to 64.
196          */
197
198         DBG_PRT((L"bootsect[1F1h] == %d setup sectors\n", bootsect[0x1F1]));
199
200         if (bootsect[0x1F1] < 1 || bootsect[0x1F1] > 64) {
201                 ERR_PRT((L"%s is not a valid bzImage kernel image.",
202                         kname));
203
204                 fops_close(fd);
205                 free_kmem();
206                 return -1;
207         }
208
209         /*
210          * Allocate and read setup data.
211          */
212
213         DBG_PRT((L"reading setup data...\n"));
214
215         param_size = (bootsect[0x1F1] + 1) * 512;
216         //param_start = alloc(param_size, EfiBootServicesData);
217         param_start = alloc(param_size, EfiLoaderData);
218
219         DBG_PRT((L"param_size=%d param_start=%x", param_size, param_start));
220
221         if (!param_start) {
222                 ERR_PRT((L"Could not allocate %d bytes of setup data.",
223                         param_size));
224
225                 fops_close(fd);
226                 free_kmem();
227                 return -1;
228         }
229
230         CopyMem(param_start, bootsect, sizeof bootsect);
231
232         size = param_size - 512;
233         efi_status = fops_read(fd, ((UINT8 *)param_start) + 512, &size);
234
235         if (EFI_ERROR(efi_status) || size != param_size - 512) {
236                 ERR_PRT((L"Could not read %d bytes of setup data.",
237                         param_size - 512));
238
239                 free(param_start);
240                 param_start = NULL;
241                 param_size = 0;
242                 fops_close(fd);
243                 free_kmem();
244                 return -1;
245         }
246
247         /*
248          * Check for setup data signature.
249          */
250
251         { UINT8 *c = ((UINT8 *)param_start)+514;
252         DBG_PRT((L"param_start(c=%x): %c-%c-%c-%c", c, (CHAR16)c[0],(CHAR16) c[1], (CHAR16)c[2], (CHAR16)c[3]));
253         }
254         if (CompareMem(((UINT8 *)param_start) + 514, "HdrS", 4)) {
255                 ERR_PRT((L"%s does not have a setup signature.",
256                         kname));
257
258                 free(param_start);
259                 param_start = NULL;
260                 param_size = 0;
261                 fops_close(fd);
262                 free_kmem();
263                 return -1;
264         }
265
266         /*
267          * Allocate memory for kernel.
268          */
269
270         if (alloc_kmem(kernel_start, EFI_SIZE_TO_PAGES(kernel_size))) {
271                 ERR_PRT((L"Could not allocate kernel memory."));
272                 return -1;
273         } else {
274                 VERB_PRT(3, Print(L"kernel_start: 0x%x  kernel_size: %d\n", kernel_start, kernel_size));
275         }
276
277         /*
278          * Now read the rest of the kernel image into memory.
279          */
280
281         DBG_PRT((L"reading kernel image...\n"));
282
283         size = kernel_size;
284
285         efi_status = fops_read(fd, kernel_start, &size);
286
287         if (EFI_ERROR(efi_status) || size < 0x10000) {
288                 ERR_PRT((L"Error reading kernel image %s.", kname));
289                 free(param_start);
290                 param_start = NULL;
291                 param_size = 0;
292                 fops_close(fd);
293                 free_kmem();
294                 return -1;
295         }
296
297         DBG_PRT((L"kernel image read:  %d bytes, %d Kbytes\n", size, size / 1024));
298
299         /*
300          * Boot sector, setup data and kernel image loaded.
301          */
302
303         fops_close(fd);
304         return 0;
305 }
306
307 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
308 static INTN
309 load_bzImage_boot(CHAR16 *kname, kdesc_t *kd)
310 {
311         DBG_PRT((L"load_bzImage_boot()\n"));
312
313         if (!kname || !kd) {
314                 ERR_PRT((L"kname=0x%x  kd=0x%x", kname, kd));
315
316                 free(param_start);
317                 param_start = NULL;
318                 param_size = 0;
319                 free_kmem();
320                 return -1;
321         }
322
323         kd->kstart = kd->kentry = kernel_start;
324         kd->kend = ((UINT8 *)kd->kstart) + kernel_size;
325
326         DBG_PRT((L"kstart=0x%x  kentry=0x%x  kend=0x%x\n", kd->kstart, kd->kentry, kd->kend));
327
328         return 0;
329 }
330
331 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
332 static loader_ops_t loader_bzImage_boot = {
333         NULL,
334         L"loader_bzImage_boot",
335         &probe_bzImage_boot,
336         &load_bzImage_boot
337 };
338
339 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
340 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
341 INTN
342 sysdeps_init(EFI_HANDLE dev)
343 {
344
345         DBG_PRT((L"sysdeps_init()\n"));
346
347         /*
348          * Register our loader(s)...
349          */
350
351         loader_register(&loader_bzImage_boot);
352         /* loader_register(&plain_loader); */   
353         /* loader_register(&gzip_loader); */
354
355
356         return 0;
357 }
358
359 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
360 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
361 /*
362  * initrd_get_addr()
363  *      Compute a starting address for the initial RAMdisk image.
364  *      For now, this image is placed immediately after the end of
365  *      the kernel memory.  Inside the start_kernel() code, the
366  *      RAMdisk image will be relocated to the top of available
367  *      extended memory.
368  */
369 INTN
370 sysdeps_initrd_get_addr(kdesc_t *kd, memdesc_t *imem)
371 {
372         DBG_PRT((L"initrd_get_addr()\n"));
373
374         if (!kd || !imem) {
375                 ERR_PRT((L"kd=0x%x imem=0x%x", kd, imem));
376                 return -1;
377         }
378
379         VERB_PRT(3, Print(L"kstart=0x%x  kentry=0x%x  kend=0x%x\n", 
380                 kd->kstart, kd->kentry, kd->kend));
381
382         imem->start_addr = kd->kend;
383
384         VERB_PRT(3, Print(L"initrd start_addr=0x%x pgcnt=%d\n", imem->start_addr, imem->pgcnt));
385
386         return 0;
387 }
388
389 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
390 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
391 VOID
392 sysdeps_free_boot_params(boot_params_t *bp)
393 {
394         mmap_desc_t md;
395
396         ZeroMem(&md, sizeof md);
397         md.md = (VOID *)bp->s.efi_mem_map;
398         free_memmap(&md);
399 }
400
401 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
402 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
403 /*
404  * IA-32 specific boot parameters initialization routine
405  */
406 INTN
407 sysdeps_create_boot_params(
408         boot_params_t *bp,
409         CHAR8 *cmdline,
410         memdesc_t *initrd,
411         UINTN *cookie)
412 {
413         mmap_desc_t mdesc;
414         EFI_STATUS efi_status;
415         UINTN rows, cols;
416         UINT8 row, col;
417         UINT8 mode;
418         UINT16 hdr_version;
419
420         DBG_PRT((L"fill_boot_params()\n"));
421
422         if (!bp || !cmdline || !initrd || !cookie) {
423                 ERR_PRT((L"bp=0x%x  cmdline=0x%x  initrd=0x%x cookie=0x%x",
424                         bp, cmdline, initrd, cookie));
425
426                 free(param_start);
427                 param_start = NULL;
428                 param_size = 0;
429                 free_kmem();
430                 return -1;
431         }
432
433         /*
434          * Copy temporary boot sector and setup data storage to
435          * elilo allocated boot parameter storage.  We only need
436          * the first two sectors (1K).  The rest of the storage
437          * can be used by the command line.
438          */
439
440         CopyMem(bp, param_start, 0x2000);
441
442         free(param_start);
443         param_start = NULL;
444         param_size = 0;
445
446         /*
447          * Save off our header revision information.
448          */
449
450         hdr_version = (bp->s.hdr_major << 8) | bp->s.hdr_minor;
451
452         /*
453          * Clear out unused memory in boot sector image.
454          */
455
456         bp->s.unused_1 = 0;
457         bp->s.unused_2 = 0;
458         ZeroMem(bp->s.unused_3, sizeof bp->s.unused_3);
459         ZeroMem(bp->s.unused_4, sizeof bp->s.unused_4);
460         ZeroMem(bp->s.unused_5, sizeof bp->s.unused_5);
461         bp->s.unused_6 = 0;
462
463         /*
464          * Tell kernel this was loaded by an advanced loader type.
465          * If this field is zero, the initrd_start and initrd_size
466          * fields are ignored by the kernel.
467          */
468
469         bp->s.loader_type = LDRTYPE_ELILO;
470
471         /*
472          * Setup command line information.
473          */
474
475         bp->s.cmdline_magik = CMDLINE_MAGIK;
476         bp->s.cmdline_offset = (UINT8 *)cmdline - (UINT8 *)bp;
477
478         /*
479          * Setup hard drive parameters.
480          * %%TBD - It should be okay to zero fill the hard drive
481          * info buffers.  The kernel should do its own detection.
482          */
483
484         ZeroMem(bp->s.hd0_info, sizeof bp->s.hd0_info);
485         ZeroMem(bp->s.hd1_info, sizeof bp->s.hd1_info);
486
487 #if 0
488         CopyMem(bp->s.hd0_info, *((VOID **)(0x41 * 4)),
489                 sizeof bp->s.hd0_info);
490
491         CopyMem(bp->s.hd1_info, *((VOID **)(0x46 * 4)),
492                 sizeof bp->s.hd1_info);
493 #endif
494
495         /*
496          * Memory info.
497          */
498
499         bp->s.alt_mem_k = high_ext_mem / 1024;
500
501         if (bp->s.alt_mem_k <= 65535) {
502                 bp->s.ext_mem_k = (UINT16)bp->s.alt_mem_k;
503         } else {
504                 bp->s.ext_mem_k = 65535;
505         }
506
507         if (hdr_version < 0x0202)
508                 bp->s.base_mem_size = high_base_mem;
509
510         /*
511          * Initial RAMdisk and root device stuff.
512          */
513
514         DBG_PRT((L"initrd->start_addr=0x%x  initrd->pgcnt=%d\n",
515                 initrd->start_addr, initrd->pgcnt));
516
517         /* These RAMdisk flags are not needed, just zero them. */
518         bp->s.ramdisk_flags = 0;
519
520         if (initrd->start_addr && initrd->pgcnt) {
521                 /* %%TBD - This will probably have to be changed. */
522                 bp->s.initrd_start = (UINT32)initrd->start_addr;
523                 bp->s.initrd_size = (UINT32)(initrd->pgcnt * EFI_PAGE_SIZE);
524
525                 /*
526                  * This is the RAMdisk root device for RedHat 2.2.x
527                  * kernels (major 0x01, minor 0x00).
528                  * %%TBD - Will this work for other distributions and
529                  * 2.3.x and 2.4.x kernels?  I do not know, yet.
530                  */
531
532                 bp->s.orig_root_dev = 0x0100;
533         } else {
534                 bp->s.initrd_start = 0;
535                 bp->s.initrd_size = 0;
536
537                 /* Do not change the root device if there is no RAMdisk. */
538                 /* bp->s.orig_root_dev = 0; */
539         }
540
541         /*
542          * APM BIOS info.
543          */
544
545 /* %%TBD - How to do Int 15h calls to get this info? */
546         bp->s.apm_bios_ver = NO_APM_BIOS;
547         bp->s.bios_code_seg = 0;
548         bp->s.bios_entry_point = 0;
549         bp->s.bios_code_seg16 = 0;
550         bp->s.bios_data_seg = 0;
551         bp->s.apm_bios_flags = 0;
552         bp->s.bios_code_len = 0;
553         bp->s.bios_data_len = 0;
554
555         /*
556          * MCA BIOS info (misnomer).
557          */
558
559 /* %%TBD - How to do Int 15h call to get this info? */
560         bp->s.mca_info_len = 0;
561         ZeroMem(bp->s.mca_info_buf, sizeof bp->s.mca_info_buf);
562
563         /*
564          * Pointing device presence.
565          */
566
567 /* %%TBD - How to do Int 11h call to get this info? */
568         bp->s.aux_dev_info = NO_MOUSE;
569
570         /*
571          * EFI loader signature and address of EFI system table.
572          */
573
574         CopyMem(bp->s.efi_loader_sig, EFI_LOADER_SIG, 4);
575         bp->s.efi_sys_tbl = 0; /* %%TBD */
576
577         /*
578          * Kernel entry point.
579          */
580
581         bp->s.kernel_start = (UINT32)kernel_start;
582
583         /*
584          * When changing stuff in the parameter structure compare
585          * the offsets of the fields with the offsets used in the
586          * boot sector and setup source files.
587          *   arch/i386/boot/bootsect.S
588          *   arch/i386/boot/setup.S
589          *   arch/i386/kernel/setup.c
590          */
591
592 #define CHECK_OFFSET(n, o, f) \
593 { \
594         UINTN p = (UINT8 *)&bp->s.n - (UINT8 *)bp; \
595         UINTN q = (UINTN)(o); \
596         if (p != q) { \
597                 test |= 1; \
598                 Print(L"%20a:  %3xh  %3xh  ", #n, p, q); \
599                 if (*f) { \
600                         Print(f, bp->s.n); \
601                 } \
602                 Print(L"\n"); \
603         } \
604 }
605
606 #define WAIT_FOR_KEY() \
607 { \
608         EFI_INPUT_KEY key; \
609         while (ST->ConIn->ReadKeyStroke(ST->ConIn, &key) != EFI_SUCCESS) { \
610                 ; \
611         } \
612 }
613
614         {
615                 UINTN test = 0;
616
617                 CHECK_OFFSET(orig_cursor_col, 0x00, L"%xh");
618                 CHECK_OFFSET(orig_cursor_row, 0x01, L"%xh");
619                 CHECK_OFFSET(ext_mem_k, 0x02, L"%xh");
620                 CHECK_OFFSET(orig_video_page, 0x04, L"%xh");
621                 CHECK_OFFSET(orig_video_mode, 0x06, L"%xh");
622                 CHECK_OFFSET(orig_video_cols, 0x07, L"%xh");
623                 CHECK_OFFSET(orig_ega_bx, 0x0A, L"%xh");
624                 CHECK_OFFSET(orig_video_rows, 0x0E, L"%xh");
625                 CHECK_OFFSET(is_vga, 0x0F, L"%xh");
626                 CHECK_OFFSET(orig_video_points, 0x10, L"%xh");
627                 CHECK_OFFSET(lfb_width, 0x12, L"%xh");
628                 CHECK_OFFSET(lfb_height, 0x14, L"%xh");
629                 CHECK_OFFSET(lfb_depth, 0x16, L"%xh");
630                 CHECK_OFFSET(lfb_base, 0x18, L"%xh");
631                 CHECK_OFFSET(lfb_size, 0x1C, L"%xh");
632                 CHECK_OFFSET(cmdline_magik, 0x20, L"%xh");
633                 CHECK_OFFSET(cmdline_offset, 0x22, L"%xh");
634                 CHECK_OFFSET(lfb_line_len, 0x24, L"%xh");
635                 CHECK_OFFSET(lfb_red_size, 0x26, L"%xh");
636                 CHECK_OFFSET(lfb_red_pos, 0x27, L"%xh");
637                 CHECK_OFFSET(lfb_green_size, 0x28, L"%xh");
638                 CHECK_OFFSET(lfb_green_pos, 0x29, L"%xh");
639                 CHECK_OFFSET(lfb_blue_size, 0x2A, L"%xh");
640                 CHECK_OFFSET(lfb_blue_pos, 0x2B, L"%xh");
641                 CHECK_OFFSET(lfb_rsvd_size, 0x2C, L"%xh");
642                 CHECK_OFFSET(lfb_rsvd_pos, 0x2D, L"%xh");
643                 CHECK_OFFSET(vesa_seg, 0x2E, L"%xh");
644                 CHECK_OFFSET(vesa_off, 0x30, L"%xh");
645                 CHECK_OFFSET(lfb_pages, 0x32, L"%xh");
646                 CHECK_OFFSET(lfb_reserved, 0x34, L"");
647                 CHECK_OFFSET(apm_bios_ver, 0x40, L"%xh");
648                 CHECK_OFFSET(bios_code_seg, 0x42, L"%xh");
649                 CHECK_OFFSET(bios_entry_point, 0x44, L"%xh");
650                 CHECK_OFFSET(bios_code_seg16, 0x48, L"%xh");
651                 CHECK_OFFSET(bios_data_seg, 0x4A, L"%xh");
652                 CHECK_OFFSET(apm_bios_flags, 0x4C, L"%xh");
653                 CHECK_OFFSET(bios_code_len, 0x4E, L"%xh");
654                 CHECK_OFFSET(bios_data_len, 0x52, L"%xh");
655                 CHECK_OFFSET(hd0_info, 0x80, L"");
656                 CHECK_OFFSET(hd1_info, 0x90, L"");
657                 CHECK_OFFSET(mca_info_len, 0xA0, L"%xh");
658                 CHECK_OFFSET(mca_info_buf, 0xA2, L"");
659                 CHECK_OFFSET(efi_loader_sig, 0x1C0, L"'%-4.4a'");
660                 CHECK_OFFSET(efi_sys_tbl, 0x1C4, L"%xh");
661                 CHECK_OFFSET(efi_mem_desc_size, 0x1C8, L"%xh");
662                 CHECK_OFFSET(efi_mem_desc_ver, 0x1CC, L"%xh");
663                 CHECK_OFFSET(efi_mem_map, 0x1D0, L"%xh");
664                 CHECK_OFFSET(efi_mem_map_size, 0x1D4, L"%xh");
665                 CHECK_OFFSET(loader_start, 0x1D8, L"%xh");
666                 CHECK_OFFSET(loader_size, 0x1DC, L"%xh");
667                 CHECK_OFFSET(alt_mem_k, 0x1E0, L"%xh");
668                 CHECK_OFFSET(setup_sectors, 0x1F1, L"%xh");
669                 CHECK_OFFSET(mount_root_rdonly, 0x1F2, L"%xh");
670                 CHECK_OFFSET(sys_size, 0x1F4, L"%xh");
671                 CHECK_OFFSET(swap_dev, 0x1F6, L"%xh");
672                 CHECK_OFFSET(ramdisk_flags, 0x1F8, L"%xh");
673                 CHECK_OFFSET(video_mode_flag, 0x1FA, L"%xh");
674                 CHECK_OFFSET(orig_root_dev, 0x1FC, L"%xh");
675                 CHECK_OFFSET(aux_dev_info, 0x1FF, L"%xh");
676                 CHECK_OFFSET(jump, 0x200, L"%xh");
677                 CHECK_OFFSET(setup_sig, 0x202, L"'%-4.4a'");
678                 CHECK_OFFSET(hdr_minor, 0x206, L"%xh");
679                 CHECK_OFFSET(hdr_major, 0x207, L"%xh");
680                 CHECK_OFFSET(rm_switch, 0x208, L"%xh");
681                 CHECK_OFFSET(start_sys_seg, 0x20C, L"%xh");
682                 CHECK_OFFSET(kernel_verstr_offset, 0x20E, L"%xh");
683                 CHECK_OFFSET(loader_type, 0x210, L"%xh");
684                 CHECK_OFFSET(loader_flags, 0x211, L"%xh");
685                 CHECK_OFFSET(setup_move_size, 0x212, L"%xh");
686                 CHECK_OFFSET(kernel_start, 0x214, L"%xh");
687                 CHECK_OFFSET(initrd_start, 0x218, L"%xh");
688                 CHECK_OFFSET(initrd_size, 0x21C, L"%xh");
689                 CHECK_OFFSET(bootsect_helper, 0x220, L"%xh");
690                 CHECK_OFFSET(heap_end_ptr, 0x224, L"%xh");
691                 CHECK_OFFSET(base_mem_size, 0x226, L"%xh");
692
693                 if (test) {
694                         ERR_PRT((L"Boot sector and/or setup parameter alignment error."));
695                         free_kmem();
696                         return -1;
697                 }
698         }
699
700         /*
701          * Get video information.
702          * Do this last so that any other cursor positioning done
703          * in the fill routine gets accounted for.
704          */
705
706         efi_status = ST->ConOut->QueryMode(
707                 ST->ConOut,
708                 ST->ConOut->Mode->Mode,
709                 &cols,
710                 &rows);
711
712         if (EFI_ERROR(efi_status)) {
713                 ERR_PRT((L"QueryMode failed.  Fake it."));
714
715                 mode = 3;
716                 rows = 25;
717                 cols = 80;
718                 row = 24;
719                 col = 0;
720         } else {
721                 mode = (UINT8)ST->ConOut->Mode->Mode;
722                 col = (UINT8)ST->ConOut->Mode->CursorColumn;
723                 row = (UINT8)ST->ConOut->Mode->CursorRow;
724         }
725
726         bp->s.orig_cursor_col = col;
727         bp->s.orig_cursor_row = row;
728         bp->s.orig_video_page = 0;
729         bp->s.orig_video_mode = mode;
730         bp->s.orig_video_cols = (UINT8)cols;
731         bp->s.orig_video_rows = (UINT8)rows;
732
733 /* %%TBD - How to do Int 10h calls to get video info? */
734         bp->s.orig_ega_bx = 0;
735         bp->s.is_vga = 0;
736         bp->s.orig_video_points = 0;
737
738 /* %%TBD - How to do Int 10h calls to get frame buffer info? */
739         bp->s.lfb_width = 0;
740         bp->s.lfb_height = 0;
741         bp->s.lfb_depth = 0;
742         bp->s.lfb_base = 0;
743         bp->s.lfb_size = 0;
744         bp->s.lfb_line_len = 0;
745         bp->s.lfb_red_size = 0;
746         bp->s.lfb_red_pos = 0;
747         bp->s.lfb_green_size = 0;
748         bp->s.lfb_green_pos = 0;
749         bp->s.lfb_blue_size = 0;
750         bp->s.lfb_blue_pos = 0;
751         bp->s.lfb_rsvd_size = 0;
752         bp->s.lfb_rsvd_pos = 0;
753         bp->s.lfb_pages = 0;
754         bp->s.vesa_seg = 0;
755         bp->s.vesa_off = 0;
756
757         /*
758          * Get memory map description and cookie for ExitBootServices()
759          */
760
761         if (get_memmap(&mdesc)) {
762                 ERR_PRT((L"Could not get memory map."));
763                 free_kmem();
764                 return -1;
765         }
766
767         *cookie = mdesc.cookie;
768         bp->s.efi_mem_map = (UINTN)mdesc.md;
769         bp->s.efi_mem_map_size = mdesc.map_size;
770         bp->s.efi_mem_desc_size = mdesc.desc_size;
771         bp->s.efi_mem_desc_ver = mdesc.desc_version;
772         bp->s.efi_sys_tbl = (UINTN)systab;
773         
774         /*
775          * my_ia32_boot_params and get ready to slap them into 0x00104c00
776          */
777
778         efi_ia32_bp.size= sizeof(efi_ia32_bp);
779         efi_ia32_bp.command_line = (UINT32) cmdline;
780         efi_ia32_bp.efi_sys_tbl = bp->s.efi_sys_tbl;
781         efi_ia32_bp.efi_mem_map = bp->s.efi_mem_map;
782         efi_ia32_bp.efi_mem_map_size = bp->s.efi_mem_map_size;
783         efi_ia32_bp.efi_mem_desc_size = bp->s.efi_mem_desc_size;
784         efi_ia32_bp.efi_mem_desc_version = bp->s.efi_mem_desc_ver;
785         efi_ia32_bp.initrd_start = (UINTN)initrd->start_addr;
786         efi_ia32_bp.initrd_size = initrd->pgcnt * EFI_PAGE_SIZE;
787         efi_ia32_bp.loader_start = 0;
788         efi_ia32_bp.loader_size = 0;
789         efi_ia32_bp.kernel_start = bp->s.kernel_start;
790         efi_ia32_bp.kernel_size = kernel_size;
791         efi_ia32_bp.num_cols = cols;
792         efi_ia32_bp.num_rows = rows;
793         efi_ia32_bp.orig_x = col;
794         efi_ia32_bp.orig_y = row;
795
796
797         return 0;
798 }