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>
7 * This file is part of the ELILO, the EFI Linux boot loader.
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)
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.
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
24 * Please check out the elilo.txt for complete documentation on how
25 * to use this program.
29 * This file contains all the IA-32 specific code expected by generic loader
38 extern loader_ops_t bzimage_loader, plain_loader, gzip_loader;
41 * Descriptor table base addresses & limits for Linux startup.
44 dt_addr_t gdt_addr = { 0x800, 0x94000 };
45 dt_addr_t idt_addr = { 0, 0 };
48 * Initial GDT layout for Linux startup.
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) */
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) */
71 UINTN sizeof_init_gdt = sizeof init_gdt;
74 * Highest available base memory address.
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.
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.
86 UINTN high_base_mem = 0x90000;
89 * Highest available extended memory address.
91 * This is computed by taking the highest available extended memory
92 * address and rounding down to the nearest EFI_PAGE_SIZE (usually
94 * This is only used for backward compatibility.
97 UINTN high_ext_mem = 32 * 1024 * 1024;
99 /* This starting address will hold true for all of the loader types for now */
100 VOID *kernel_start = (VOID *)0x100000; /* 1M */
102 VOID *initrd_start = NULL;
103 UINTN initrd_size = 0;
106 sysdeps_init(EFI_HANDLE dev)
108 DBG_PRT((L"sysdeps_init()\n"));
111 * Register our loader(s)...
114 loader_register(&bzimage_loader);
115 loader_register(&plain_loader);
116 loader_register(&gzip_loader);
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
129 sysdeps_initrd_get_addr(kdesc_t *kd, memdesc_t *imem)
131 DBG_PRT((L"initrd_get_addr()\n"));
134 ERR_PRT((L"kd=0x%x imem=0x%x", kd, imem));
138 VERB_PRT(3, Print(L"kstart=0x%x kentry=0x%x kend=0x%x\n",
139 kd->kstart, kd->kentry, kd->kend));
141 imem->start_addr = kd->kend;
143 VERB_PRT(3, Print(L"initrd start_addr=0x%x pgcnt=%d\n",
144 imem->start_addr, imem->pgcnt));
150 sysdeps_free_boot_params(boot_params_t *bp)
154 ZeroMem(&md, sizeof md);
155 md.md = (VOID *)bp->s.efi_mem_map;
160 * IA-32 specific boot parameters initialization routine
163 sysdeps_create_boot_params(
167 memdesc_t *vmcode, /* no use for ia32 now*/
171 EFI_STATUS efi_status;
177 DBG_PRT((L"fill_boot_params()\n"));
179 if (!bp || !cmdline || !initrd || !cookie) {
180 ERR_PRT((L"bp=0x%x cmdline=0x%x initrd=0x%x cookie=0x%x",
181 bp, cmdline, initrd, cookie));
183 if (param_start != NULL) {
193 * Copy temporary boot sector and setup data storage to
194 * elilo allocated boot parameter storage. We only need
195 * the first two sectors (1K). The rest of the storage
196 * can be used by the command line.
198 if (param_start != NULL) {
199 CopyMem(bp, param_start, 0x2000);
205 * Save off our header revision information.
207 hdr_version = (bp->s.hdr_major << 8) | bp->s.hdr_minor;
210 * Clear out unused memory in boot sector image.
214 ZeroMem(bp->s.unused_3, sizeof bp->s.unused_3);
215 ZeroMem(bp->s.unused_4, sizeof bp->s.unused_4);
216 ZeroMem(bp->s.unused_5, sizeof bp->s.unused_5);
221 * Tell kernel this was loaded by an advanced loader type.
222 * If this field is zero, the initrd_start and initrd_size
223 * fields are ignored by the kernel.
226 bp->s.loader_type = LDRTYPE_ELILO;
229 * Setup command line information.
232 bp->s.cmdline_magik = CMDLINE_MAGIK;
233 bp->s.cmdline_offset = (UINT8 *)cmdline - (UINT8 *)bp;
236 * Clear out the cmdline_addr field so the kernel can find
239 bp->s.cmdline_addr = 0x0;
242 * Setup hard drive parameters.
243 * %%TBD - It should be okay to zero fill the hard drive
244 * info buffers. The kernel should do its own detection.
247 ZeroMem(bp->s.hd0_info, sizeof bp->s.hd0_info);
248 ZeroMem(bp->s.hd1_info, sizeof bp->s.hd1_info);
254 bp->s.alt_mem_k = high_ext_mem / 1024;
256 if (bp->s.alt_mem_k <= 65535)
257 bp->s.ext_mem_k = (UINT16)bp->s.alt_mem_k;
259 bp->s.ext_mem_k = 65535;
262 * Initial RAMdisk and root device stuff.
265 DBG_PRT((L"initrd->start_addr=0x%x initrd->pgcnt=%d\n",
266 initrd->start_addr, initrd->pgcnt));
268 /* These RAMdisk flags are not needed, just zero them. */
269 bp->s.ramdisk_flags = 0;
271 if (initrd->start_addr && initrd->pgcnt) {
272 /* %%TBD - This will probably have to be changed. */
273 bp->s.initrd_start = (UINT32)initrd->start_addr;
274 bp->s.initrd_size = (UINT32)(initrd->size);
277 * This is the RAMdisk root device for RedHat 2.2.x
278 * kernels (major 0x01, minor 0x00).
281 bp->s.orig_root_dev = 0x0100;
283 bp->s.initrd_start = 0;
284 bp->s.initrd_size = 0;
290 bp->s.apm_bios_ver = NO_APM_BIOS;
291 bp->s.bios_code_seg = 0;
292 bp->s.bios_entry_point = 0;
293 bp->s.bios_code_seg16 = 0;
294 bp->s.bios_data_seg = 0;
295 bp->s.apm_bios_flags = 0;
296 bp->s.bios_code_len = 0;
297 bp->s.bios_data_len = 0;
300 * MCA BIOS info (misnomer).
302 bp->s.mca_info_len = 0;
303 ZeroMem(bp->s.mca_info_buf, sizeof bp->s.mca_info_buf);
306 * Pointing device presence. The kernel will detect this.
308 bp->s.aux_dev_info = NO_MOUSE;
311 * EFI loader signature
313 CopyMem(bp->s.efi_loader_sig, EFI_LOADER_SIG, 4);
316 * Kernel entry point.
318 bp->s.kernel_start = (UINT32)kernel_start;
321 * When changing stuff in the parameter structure compare
322 * the offsets of the fields with the offsets used in the
323 * boot sector and setup source files.
324 * arch/i386/boot/bootsect.S
325 * arch/i386/boot/setup.S
326 * arch/i386/kernel/setup.c
327 * include/asm-i386/setup.h (2.5/2.6)
330 #define CHECK_OFFSET(n, o, f) \
332 UINTN p = (UINT8 *)&bp->s.n - (UINT8 *)bp; \
333 UINTN q = (UINTN)(o); \
336 Print(L"%20a: %3xh %3xh ", #n, p, q); \
344 #define WAIT_FOR_KEY() \
347 while (ST->ConIn->ReadKeyStroke(ST->ConIn, &key) != EFI_SUCCESS) { \
354 CHECK_OFFSET(orig_cursor_col, 0x00, L"%xh");
355 CHECK_OFFSET(orig_cursor_row, 0x01, L"%xh");
356 CHECK_OFFSET(ext_mem_k, 0x02, L"%xh");
357 CHECK_OFFSET(orig_video_page, 0x04, L"%xh");
358 CHECK_OFFSET(orig_video_mode, 0x06, L"%xh");
359 CHECK_OFFSET(orig_video_cols, 0x07, L"%xh");
360 CHECK_OFFSET(orig_ega_bx, 0x0A, L"%xh");
361 CHECK_OFFSET(orig_video_rows, 0x0E, L"%xh");
362 CHECK_OFFSET(is_vga, 0x0F, L"%xh");
363 CHECK_OFFSET(orig_video_points, 0x10, L"%xh");
364 CHECK_OFFSET(lfb_width, 0x12, L"%xh");
365 CHECK_OFFSET(lfb_height, 0x14, L"%xh");
366 CHECK_OFFSET(lfb_depth, 0x16, L"%xh");
367 CHECK_OFFSET(lfb_base, 0x18, L"%xh");
368 CHECK_OFFSET(lfb_size, 0x1C, L"%xh");
369 CHECK_OFFSET(cmdline_magik, 0x20, L"%xh");
370 CHECK_OFFSET(cmdline_offset, 0x22, L"%xh");
371 CHECK_OFFSET(lfb_line_len, 0x24, L"%xh");
372 CHECK_OFFSET(lfb_red_size, 0x26, L"%xh");
373 CHECK_OFFSET(lfb_red_pos, 0x27, L"%xh");
374 CHECK_OFFSET(lfb_green_size, 0x28, L"%xh");
375 CHECK_OFFSET(lfb_green_pos, 0x29, L"%xh");
376 CHECK_OFFSET(lfb_blue_size, 0x2A, L"%xh");
377 CHECK_OFFSET(lfb_blue_pos, 0x2B, L"%xh");
378 CHECK_OFFSET(lfb_rsvd_size, 0x2C, L"%xh");
379 CHECK_OFFSET(lfb_rsvd_pos, 0x2D, L"%xh");
380 CHECK_OFFSET(vesa_seg, 0x2E, L"%xh");
381 CHECK_OFFSET(vesa_off, 0x30, L"%xh");
382 CHECK_OFFSET(lfb_pages, 0x32, L"%xh");
383 CHECK_OFFSET(lfb_reserved, 0x34, L"");
384 CHECK_OFFSET(apm_bios_ver, 0x40, L"%xh");
385 CHECK_OFFSET(bios_code_seg, 0x42, L"%xh");
386 CHECK_OFFSET(bios_entry_point, 0x44, L"%xh");
387 CHECK_OFFSET(bios_code_seg16, 0x48, L"%xh");
388 CHECK_OFFSET(bios_data_seg, 0x4A, L"%xh");
389 CHECK_OFFSET(apm_bios_flags, 0x4C, L"%xh");
390 CHECK_OFFSET(bios_code_len, 0x4E, L"%xh");
391 CHECK_OFFSET(bios_data_len, 0x52, L"%xh");
392 CHECK_OFFSET(hd0_info, 0x80, L"");
393 CHECK_OFFSET(hd1_info, 0x90, L"");
394 CHECK_OFFSET(mca_info_len, 0xA0, L"%xh");
395 CHECK_OFFSET(mca_info_buf, 0xA2, L"");
396 CHECK_OFFSET(efi_loader_sig, 0x1C0, L"'%-4.4a'");
397 CHECK_OFFSET(efi_sys_tbl, 0x1C4, L"%xh");
398 CHECK_OFFSET(efi_mem_desc_size, 0x1C8, L"%xh");
399 CHECK_OFFSET(efi_mem_desc_ver, 0x1CC, L"%xh");
400 CHECK_OFFSET(efi_mem_map, 0x1D0, L"%xh");
401 CHECK_OFFSET(efi_mem_map_size, 0x1D4, L"%xh");
402 CHECK_OFFSET(loader_start, 0x1D8, L"%xh");
403 CHECK_OFFSET(loader_size, 0x1DC, L"%xh");
404 CHECK_OFFSET(alt_mem_k, 0x1E0, L"%xh");
405 CHECK_OFFSET(setup_sectors, 0x1F1, L"%xh");
406 CHECK_OFFSET(mount_root_rdonly, 0x1F2, L"%xh");
407 CHECK_OFFSET(sys_size, 0x1F4, L"%xh");
408 CHECK_OFFSET(swap_dev, 0x1F6, L"%xh");
409 CHECK_OFFSET(ramdisk_flags, 0x1F8, L"%xh");
410 CHECK_OFFSET(video_mode_flag, 0x1FA, L"%xh");
411 CHECK_OFFSET(orig_root_dev, 0x1FC, L"%xh");
412 CHECK_OFFSET(aux_dev_info, 0x1FF, L"%xh");
413 CHECK_OFFSET(jump, 0x200, L"%xh");
414 CHECK_OFFSET(setup_sig, 0x202, L"'%-4.4a'");
415 CHECK_OFFSET(hdr_minor, 0x206, L"%xh");
416 CHECK_OFFSET(hdr_major, 0x207, L"%xh");
417 CHECK_OFFSET(rm_switch, 0x208, L"%xh");
418 CHECK_OFFSET(start_sys_seg, 0x20C, L"%xh");
419 CHECK_OFFSET(kernel_verstr_offset, 0x20E, L"%xh");
420 CHECK_OFFSET(loader_type, 0x210, L"%xh");
421 CHECK_OFFSET(loader_flags, 0x211, L"%xh");
422 CHECK_OFFSET(setup_move_size, 0x212, L"%xh");
423 CHECK_OFFSET(kernel_start, 0x214, L"%xh");
424 CHECK_OFFSET(initrd_start, 0x218, L"%xh");
425 CHECK_OFFSET(initrd_size, 0x21C, L"%xh");
426 CHECK_OFFSET(bootsect_helper, 0x220, L"%xh");
427 CHECK_OFFSET(heap_end_ptr, 0x224, L"%xh");
428 CHECK_OFFSET(cmdline_addr, 0x228, L"%xh");
431 ERR_PRT((L"Boot sector and/or setup parameter alignment error."));
438 * Get video information.
439 * Do this last so that any other cursor positioning done
440 * in the fill routine gets accounted for.
443 efi_status = ST->ConOut->QueryMode(
445 ST->ConOut->Mode->Mode,
449 if (EFI_ERROR(efi_status)) {
450 ERR_PRT((L"QueryMode failed. Fake it."));
457 mode = (UINT8)ST->ConOut->Mode->Mode;
458 col = (UINT8)ST->ConOut->Mode->CursorColumn;
459 row = (UINT8)ST->ConOut->Mode->CursorRow;
462 bp->s.orig_cursor_col = col;
463 bp->s.orig_cursor_row = row;
464 bp->s.orig_video_page = 0;
465 bp->s.orig_video_mode = mode;
466 bp->s.orig_video_cols = (UINT8)cols;
467 bp->s.orig_video_rows = (UINT8)rows;
469 bp->s.orig_ega_bx = 0;
471 bp->s.orig_video_points = 16;
474 bp->s.lfb_height = 0;
478 bp->s.lfb_line_len = 0;
479 bp->s.lfb_red_size = 0;
480 bp->s.lfb_red_pos = 0;
481 bp->s.lfb_green_size = 0;
482 bp->s.lfb_green_pos = 0;
483 bp->s.lfb_blue_size = 0;
484 bp->s.lfb_blue_pos = 0;
485 bp->s.lfb_rsvd_size = 0;
486 bp->s.lfb_rsvd_pos = 0;
492 * Get memory map description and cookie for ExitBootServices()
495 if (get_memmap(&mdesc)) {
496 ERR_PRT((L"Could not get memory map."));
500 *cookie = mdesc.cookie;
501 bp->s.efi_mem_map = (UINTN)mdesc.md;
502 bp->s.efi_mem_map_size = mdesc.map_size;
503 bp->s.efi_mem_desc_size = mdesc.desc_size;
504 bp->s.efi_mem_desc_ver = mdesc.desc_version;
505 bp->s.efi_sys_tbl = (UINTN)systab;