Skip to contentSkip to conversation

grub-emu state of the art

DISCUSSION Eric Salomé 4 participants 9 messages May 3, 2006 – May 26, 2006 VFS

lore
Share & tools
lore
#0 lore raw

GRUB 2 Wiki
ToDoList : Implement ModuleLoading in grub-emu

I worked on that : Architecture i386/pc - Emulation on Linux/Ubuntu grub-emu is loading all current modules, though some won't do anything in emulation mode. The modules .mod are the same as those used for boot time.

Little changes in source code, though I had to adapt asm files to passing parameters in stack (I compile without -mregparm=3 -mrtd options as I didn't want to recompile the whole C library with those passing parameters conventions).
... when I be done with grub 2 debugging I'll just have to recompile with these options (if you like them a lot) and it will work just fine (boot time only).

ToBeAwardedOf:
Don't use gcc 3.3 with grub 2's current source files : compiled code is wrong and subject to unattended results.
Runs fine with gcc 4.0. I have still a doubt about gcc 3.4.

LeftToDo :

- see if every modification is useful, as I tried a few things before I got a good result, and send a diff patch.
- I need a "patched" symlist.c to compile with grub-emu (could be generated by a script, but I am too lazy).
- let gdb know about dynamically loaded modules ;-), but I guess it won't be the easy part.

Best Regards,
_________________________________________
Eric Salomé - Paris, France

Quoted 9 lines

-----Message d'origine-----
De : Eric Salomé
Envoyé : samedi 29 avril 2006 23:00
À : The development of GRUB 2
Objet : grub-emu state of the art

Hi,

I need a working grub-emu to continue the port of a system of my own

from

Quoted 3 lines

grub to grub 2.

I am currently working on a i386 pc and had hard time the first days

with

Quoted 1 line

the 1.93 delivery : grub-emu would run (on Linux based Ubuntu OS) and

come

Quoted 6 lines

with a rescue mode prompt, but was not likely to load and init modules such as "normal.mod" (even by hand).

Am I doing something wrong or is it the current state of grub-emu ?

In the latter case, I'd be glad to help a little and see what can be

done

Quoted 5 lines

to have a fully functional grub-emu.

Best regards.
_________________________________________
Eric Salomé - Paris, France

#1 lore raw
Quoted 3 lines

- let gdb know about dynamically loaded modules ;-), but I guess it won't be the easy part.

That depends how modules are loaded in grub-emu. With standard libdl it couldn't be problem.

But I remember a situation when we run gdb on UML(user mode linux) kernel. When we loaded a kernel module there (using kernel dynamic loader), gdb was able to print adress where it is loaded, and also it was able to load symbol table with given memory offset. But we had to read the address by our eyes and type the offset (exactly same number) to gdb by hand. Later we used expect script to do this.

Signature · 2 lines
                                            Tomas 'Ebi' Ebenlendr
                                            http://get.to/ebik
#2 lore raw

RE : RE : grub-emu state of the art : GDB interface

Quoted 9 lines

-----Message d'origine-----
De : [email protected] [mailto:grub-devel- [email protected]] De la part de Tomáš Ebenlendr Envoyé : jeudi 4 mai 2006 11:24
À : The development of GRUB 2
Objet : Re: RE : grub-emu state of the art

Quoted 5 lines
Quoted 2 lines

- let gdb know about dynamically loaded modules ;-), but I guess it won't be the easy part.

That depends how modules are loaded in grub-emu. With standard libdl it couldn't be problem.

Of course, they are loaded with kern/dl.c in grub-emu, to comply with what is done in grub2 images.
I thought we could add code compiled only with GRUB_UTIL to supply load maps the way libdl does, during the grub_load_segments(), etc. process and merely call _dl_debug_state() for gdb to know about newly loaded symbols before the call to mod->init(mod) is done. But that's a lot to do and I was wondering if someone reading us just didn't do that yet in another lifetime.

Quoted 7 lines

But I remember a situation when we run gdb on UML(user mode linux) kernel. When we loaded a kernel module there (using kernel dynamic loader), gdb was able to print adress where it is loaded, and also it was able to load symbol table with given memory offset. But we had to read the address by our eyes and type the offset (exactly same number) to gdb by hand. Later we used expect script to do this.

That's about just what I am doing right now.
There is a difference of importance : grub's modules are relocated ELF file, not shared ELF file (.so) and my gdb just *don't* load symbol table from relocated ELF file (why has it to be so dummy, that still question me, anyway shared ELF files contains more info for gdb to use apparently, including source lines references). So believe it or not, this ended up to this for now (in case someone would be interested) :
- I have added debug lines in the code (set debug=symbols to activate) to read the address of loaded segments.
- I relink modules I debug with the -shared option instead of the -r option of ld to produce module.mod + "same" module.so files.
- I command gdb to read symbols from .so files at startup (add-symbol-file commands with the correct addresses for .text, .rodata, .data and .bss segments) and that's it, ready to go.

Next step would be to automate the all process.

You can then loads modules and debug them without the need to change Makefiles to include modules in the main kernel.

Eric Salomé - Paris, France

#3 lore raw

grub-emu : Dynamic loading of modules & GDB interface

Hi Lubomir, Marco, Hi Folks,

I've come to a "steady" point on my work about grub-emu dynamic loading of modules. I am open to new ideas as they come.
Your comments are welcomed.

Here are the diff from grub 1.93 relating all the changes I have made so far.
Some may be useful to others. Fell free to add them to grub if needed.

(1) - Enable dynamically loading of modules in grub-emu for the i386-pc.
(2) - Provide changes for GDB to operate properly when loading
modules
in grub-emu. GRUB/GDB interface for dynamic loading of
modules.
(I believe that there is a few line changes
that would be helpful for Lubomir Kundrak scripts. At least those few in dl.c that store the name of each ELF
section
in mod->seg->name when loading modules). See also Nota Bene.
(3) - Add two or three functions I needed for other modules. You might not need those changes.
(4) - Minor/Other changes

You can browse the changes looking at "FILE" at start of line. or searching for (1), (2), (3), (4) tokens or look for "Nota Bene".

There is one file outside Grub distribution (see FILE gdb-6.1/gdb/solib-svr4.c)

Have a nice day.
Best Regards,

Eric Salomé - Paris, France

PS: Is there anyone working on bringing a VBE interface to grub-emu ?

FILE conf/i386-pc.rmk

(1) grub-emu includes kern/i386/dl.c and symlist2.c to be able to properly load modules.

(1) I don't think it possible to dyn load modules in grub-emu
with
functions passing parameters in registers. So I compile
without
related options and I have adapted ASM files (those I know
of)
for both cases.
Note that it still is possible later to do a final build run before/after distribution with the -mrtd -mregparm=3
options.
So that everybody is happy (I hope).

#define REGPARM is used in ASM files to let them know they are been compiled with or without passing parameters in registers. (see setjmp.S for an example).

(1),(2) Add compile options for different cases

(2) file kern/dl-gdb.c (NEW) is an example of grub_gdb...()
functions

	    (GRUB/GDB interface for dynamically loading of modules)
	    to build  "module.elf 0x321213 -s .data 0x321435 ..." string
in
	    mod->elf for example, or to send a "signal" to gdb to

advertize
for loads /unloads of modules.

../src/grub-1.93/conf/i386-pc.rmk renamed from conf/i386-pc.rmk +0 -0
diff -EbwBruN ./conf/i386-pc.rmk ../../src/grub-1.93/conf/i386-pc.rmk

17:20:24.000000000 +0200
@@ -1,7 +1,25 @@
# -*- makefile -*-

+# For final built, passing parameters in registers
+# That's about 2 % shorter code, and may be 10 % faster.
+
+#COMMON_ASFLAGS = -nostdinc -fno-builtin -DREGPARM +#COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32
+
+# For building grub-emu to be run with a modified GDB (6.1) that allows +# ELF sections to be loaded at non-contigus memory locations +
COMMON_ASFLAGS = -nostdinc -fno-builtin
-COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32 +COMMON_CFLAGS = -fno-builtin -m32 -DGRUB_COMPAT_GDB
+
+# For standard building of grub-emu when no modified GDB is available +# See following links for help in debugging with GRUB / GDB +# http://NetBSD.sk/~lkundrak/misc/grub2-gdb/howto.html
+
+#COMMON_ASFLAGS = -nostdinc -fno-builtin
+#COMMON_CFLAGS = -fno-builtin -m32
+
+
COMMON_LDFLAGS = -melf_i386

# Images.
@@ -89,6 +107,7 @@
fs/jfs.c fs/minix.c fs/sfs.c fs/ufs.c fs/xfs.c fs/hfsplus.c
\
io/gzio.c
\
kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c
\
+ kern/dl-gdb.c
\
normal/execute.c kern/file.c kern/fs.c normal/lexer.c
\
kern/loader.c kern/main.c kern/misc.c kern/parser.c
\
grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c
\
@@ -99,8 +118,9 @@
partmap/acorn.c partmap/gpt.c
\
util/console.c util/grub-emu.c util/misc.c
\
util/i386/pc/biosdisk.c util/i386/pc/getroot.c
\
- util/i386/pc/misc.c grub_emu_init.c
+ util/i386/pc/misc.c grub_emu_init.c kern/i386/dl.c symlist2.c

+grub_emu_CFLAGS = $(COMMON_CFLAGS)
grub_emu_LDFLAGS = $(LIBCURSES)

FILE normal/i386/setjmp.S

(1) Now use the #define REGPARM to tell if parameters are
expected
in registers, or in the stack.

diff -EbwBruN ./normal/i386/setjmp.S
../../src/grub-1.93/normal/i386/setjmp.S
--- ./normal/i386/setjmp.S 2004-04-04 15:46:03.000000000 +0200
+++ ../../src/grub-1.93/normal/i386/setjmp.S 2006-05-08
23:14:41.000000000 +0200
@@ -27,6 +27,9 @@
* int grub_setjmp (grub_jmp_buf env)
*/
FUNCTION(grub_setjmp)
+#ifndef REGPARM
+ movl 4(%esp), %eax /* Get Param */
+#endif
movl %ebx, 0(%eax) /* EBX */
movl %esi, 4(%eax) /* ESI */
movl %edi, 8(%eax) /* EDI */
@@ -42,6 +45,10 @@
* int grub_longjmp (grub_jmp_buf env, int val)
*/
FUNCTION(grub_longjmp)
+#ifndef REGPARM
+ movl 4(%esp), %eax /* Get Param */
+ movl 8(%esp), %edx /* Get Param */
+#endif
movl 0(%eax), %ebx
movl 4(%eax), %esi
movl 8(%eax), %edi

FILE kern/i386/pc/startup.S

(1) ASM file moved to startup.S.regparm
A new ASM file is also provided startup.S.memparm to be used when you compile without -mrtd -mregparm=3 options.

diff -EbwBruN ./kern/i386/pc/startup.S ../../src/grub-1.93/kern/i386/pc/startup.S
--- ./kern/i386/pc/startup.S 2005-10-15 19:28:36.000000000 +0200
+++ ../../src/grub-1.93/kern/i386/pc/startup.S 2006-05-08
23:11:49.000000000 +0200
@@ -17,2154 +17,9 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

+#ifdef REGPARM
+#include "startup.S.regparm"
+#else
+#include "startup.S.memparm"
+#endif

-/*
- * Note: These functions defined in this file may be called from C.
- * Be careful of that you must not modify some registers. Quote
- * from gcc-2.95.2/gcc/config/i386/i386.h:
-

-   1 for registers not available across function calls.
-   These must include the FIXED_REGISTERS and also any
-   registers that can be used without being saved.
-   The latter must include the registers where values are returned
-   and the register where structure-value addresses are passed.
-   Aside from that, you can include as many other registers as you

like.
-
- ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg -{ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
- */
-
-/*
- * Note: GRUB is compiled with the options -mrtd and -mregparm=3.
- * So the first three arguments are passed in %eax, %edx, and
%ecx,
- * respectively, and if a function has a fixed number of
arguments
- * and the number if greater than three, the function must return
- * with "ret $N" where N is ((the number of arguments) - 3) * 4.
- */
-
-#include <config.h>
-#include <grub/symbol.h>
-#include <grub/boot.h>
-#include <grub/machine/boot.h>
-#include <grub/machine/memory.h>
-#include <grub/machine/console.h>
-#include <grub/machine/linux.h>
-#include <grub/machine/kernel.h>
-#include <grub/machine/multiboot.h>
-
-#define ABS(x) ((x) - EXT_C(start) + GRUB_BOOT_MACHINE_KERNEL_ADDR +
0x200)
-
- .file "startup.S"
-
- .text
-
- /* Tell GAS to generate 16-bit instructions so that this code
works
- in real mode. */
- .code16
-
- .globl start, _start
-start:
-_start:
- /*
- * Guarantee that "main" is loaded at 0x0:0x8200.
- */
- ljmp $0, $ABS(codestart)
-
- /*
- * Compatibility version number
- *
- * These MUST be at byte offset 6 and 7 of the executable
- * DO NOT MOVE !!!
- */
- . = EXT_C(start) + 0x6
- .byte GRUB_BOOT_VERSION_MAJOR, GRUB_BOOT_VERSION_MINOR
-
- /*
- * This is a special data area 8 bytes from the beginning.
- */
-
- . = EXT_C(start) + 0x8
-
-VARIABLE(grub_total_module_size)
- .long 0
-VARIABLE(grub_kernel_image_size)
- .long 0
-VARIABLE(grub_compressed_size)
- .long 0
-VARIABLE(grub_install_dos_part)
- .long 0xFFFFFFFF
-VARIABLE(grub_install_bsd_part)
- .long 0xFFFFFFFF
-VARIABLE(grub_prefix)
- .string "/boot/grub"
-
- /*
- * Leave some breathing room for the prefix.
- */
-
- . = EXT_C(start) + 0x50
-
-/*
- * Support for booting GRUB from a Multiboot boot loader (e.g. GRUB itself).
- * This uses the a.out kludge to load raw binary to the area starting at 1MB,
- * and relocates itself after loaded.
- */
-multiboot_header:
- /* magic */
- .long 0x1BADB002
- /* flags */
- .long (1 << 16)
- /* checksum */
- .long -0x1BADB002 - (1 << 16)
- /* header addr */
- .long multiboot_header - _start + 0x100000 + 0x200
- /* load addr */
- .long 0x100000
- /* load end addr */
- .long 0
- /* bss end addr */
- .long 0
- /* entry addr */
- .long multiboot_entry - _start + 0x100000 + 0x200
-
-multiboot_entry:
- .code32
- /* obtain the boot device */
- movl 12(%ebx), %edx
-
- /* relocate the code */
- movl $(GRUB_KERNEL_MACHINE_RAW_SIZE + 0x200), %ecx
- addl EXT_C(grub_compressed_size) - _start + 0x100000 + 0x200,
%ecx
- movl $0x100000, %esi
- movl $GRUB_BOOT_MACHINE_KERNEL_ADDR, %edi
- cld
- rep
- movsb
- /* jump to the real address */
- movl $multiboot_trampoline, %eax
- jmp *%eax
-
-multiboot_trampoline:
- /* fill the boot information */
- movl %edx, %eax
- shrl $8, %eax
- xorl %ebx, %ebx
- cmpb $0xFF, %ah
- je 1f
- movb %ah, %bl
- movl %ebx, EXT_C(grub_install_dos_part)
-1:
- cmpb $0xFF, %al
- je 2f
- movb %al, %bl
- movl %ebx, EXT_C(grub_install_bsd_part)
-2:
- shrl $24, %edx
- /* enter the usual booting */
- call prot_to_real
- .code16
-
-/* the real mode code continues... */
-codestart:
- cli /* we're not safe here! */
-
- /* set up %ds, %ss, and %es */
- xorw %ax, %ax
- movw %ax, %ds
- movw %ax, %ss
- movw %ax, %es
-
- /* set up the real mode/BIOS stack */
- movl $GRUB_MEMORY_MACHINE_REAL_STACK, %ebp
- movl %ebp, %esp
-
- sti /* we're safe again */
-
- /* save boot drive reference */
- ADDR32 movb %dl, EXT_C(grub_boot_drive)
-
- /* reset disk system (%ah = 0) */
- int $0x13
-
- /* transition to protected mode */
- DATA32 call real_to_prot
-
- /* The ".code32" directive takes GAS out of 16-bit mode. */
- .code32
-
- incl %eax
- call EXT_C(grub_gate_a20)
-
- /* decompress the compressed part and put the result at 1MB */
- movl $0x100000, %esi
- movl $(START_SYMBOL + GRUB_KERNEL_MACHINE_RAW_SIZE), %edi
-
- pushl %esi
- pushl EXT_C(grub_compressed_size)
- pushl %edi
- call lzo1x_decompress
- addl $12, %esp
-
- /* copy back the decompressed part */
- movl %eax, %ecx
- cld
- rep
- movsb
-
- /* copy modules before cleaning out the bss */
- movl EXT_C(grub_total_module_size), %ecx
- movl EXT_C(grub_kernel_image_size), %esi
- addl %ecx, %esi
- addl $START_SYMBOL, %esi
- decl %esi
- movl $END_SYMBOL, %edi
- addl %ecx, %edi
- decl %edi
- std
- rep
- movsb
-
- /* clean out the bss */
- movl $BSS_START_SYMBOL, %edi
-
- /* compute the bss length */
- movl $END_SYMBOL, %ecx
- subl %edi, %ecx
-
- /* clean out */
- xorl %eax, %eax
- cld
- rep
- stosb
-
- /*
- * Call the start of main body of C code.
- */
- call EXT_C(grub_main)
-
-
-/*
- * This is the area for all of the special variables.
- */
-
- .p2align 2 /* force 4-byte alignment */
-
-protstack:
- .long GRUB_MEMORY_MACHINE_PROT_STACK
-
-VARIABLE(grub_boot_drive)
- .long 0
-
-VARIABLE(grub_start_addr)
- .long START_SYMBOL
-
-VARIABLE(grub_end_addr)
- .long END_SYMBOL
-
-VARIABLE(grub_apm_bios_info)
- .word 0 /* version */
- .word 0 /* cseg */
- .long 0 /* offset */
- .word 0 /* cseg_16 */
- .word 0 /* dseg_16 */
- .word 0 /* cseg_len */
- .word 0 /* cseg_16_len */
- .word 0 /* dseg_16_len */
-
-/*
- * This is the Global Descriptor Table
- *
- * An entry, a "Segment Descriptor", looks like this:
- *
- * 31 24 19 16 7 0
- * ------------------------------------------------------------

- * |             | |B| |A|       | |   |1|0|E|W|A|            |
- * | BASE 31..24 |G|/|L|V| LIMIT |P|DPL|  TYPE   | BASE 23:16 |  4
- * |             | |D| |L| 19..16| |   |1|1|C|R|A|            |
- * ------------------------------------------------------------
- * |                             |                            |
- * |        BASE 15..0           |       LIMIT 15..0          |  0
- * |                             |                            |

- * ------------------------------------------------------------
- *
- * Note the ordering of the data items is reversed from the above
- * description.
- */
-
- .p2align 2 /* force 4-byte alignment */
-gdt:
- .word 0, 0
- .byte 0, 0, 0, 0
-
- /* -- code segment --
- * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity),
present
- * type = 32bit code execute/read, DPL = 0
- */
- .word 0xFFFF, 0
- .byte 0, 0x9A, 0xCF, 0
-
- /* -- data segment --
- * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
- * type = 32 bit data read/write, DPL = 0
- */
- .word 0xFFFF, 0
- .byte 0, 0x92, 0xCF, 0
-
- /* -- 16 bit real mode CS --
- * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
- * type = 16 bit code execute/read only/conforming, DPL = 0
- */
- .word 0xFFFF, 0
- .byte 0, 0x9E, 0, 0
-
- /* -- 16 bit real mode DS --
- * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
- * type = 16 bit data read/write, DPL = 0
- */
- .word 0xFFFF, 0
- .byte 0, 0x92, 0, 0
-
-
-/* this is the GDT descriptor */
-gdtdesc:
- .word 0x27 /* limit */
- .long gdt /* addr */
-
-
-/*
- * These next two routines, "real_to_prot" and "prot_to_real" are structured
- * in a very specific way. Be very careful when changing them.
- *
- * NOTE: Use of either one messes up %eax and %ebp.
- */
-
-real_to_prot:
- .code16
- cli
-
- /* load the GDT register */
- DATA32 ADDR32 lgdt gdtdesc
-
- /* turn on protected mode */
- movl %cr0, %eax
- orl $GRUB_MEMORY_MACHINE_CR0_PE_ON, %eax
- movl %eax, %cr0
-
- /* jump to relocation, flush prefetch queue, and reload %cs */
- DATA32 ljmp $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
-
- .code32
-protcseg:
- /* reload other segment registers */
- movw $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw %ax, %gs
- movw %ax, %ss
-
- /* put the return address in a known safe location */
- movl (%esp), %eax
- movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
-
- /* get protected mode stack */
- movl protstack, %eax
- movl %eax, %esp
- movl %eax, %ebp
-
- /* get return address onto the right stack */
- movl GRUB_MEMORY_MACHINE_REAL_STACK, %eax
- movl %eax, (%esp)
-
- /* zero %eax */
- xorl %eax, %eax
-
- /* return on the old (or initialized) stack! */
- ret
-
-
-prot_to_real:
- /* just in case, set GDT */
- lgdt gdtdesc
-
- /* save the protected mode stack */
- movl %esp, %eax
- movl %eax, protstack
-
- /* get the return address */
- movl (%esp), %eax
- movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
-
- /* set up new stack */
- movl $GRUB_MEMORY_MACHINE_REAL_STACK, %eax
- movl %eax, %esp
- movl %eax, %ebp
-
- /* set up segment limits */
- movw $GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw %ax, %gs
- movw %ax, %ss
-
- /* this might be an extra step */
- /* jump to a 16 bit segment */
- ljmp $GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
-
-tmpcseg:
- .code16
-
- /* clear the PE bit of CR0 */
- movl %cr0, %eax
- andl $(~GRUB_MEMORY_MACHINE_CR0_PE_ON), %eax
- movl %eax, %cr0
-
- /* flush prefetch queue, reload %cs */
- DATA32 ljmp $0, $realcseg
-
-realcseg:
- /* we are in real mode now
- * set up the real mode segment registers : DS, SS, ES
- */
- /* zero %eax */
- xorl %eax, %eax
-
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw %ax, %gs
- movw %ax, %ss
-
- /* restore interrupts */
- sti
-
- /* return on new stack! */
- DATA32 ret
-
- .code32
-
-
-/*
- * grub_gate_a20(int on)
- *
- * Gate address-line 20 for high memory.
- *
- * This routine is probably overconservative in what it does, but so what?
- *
- * It also eats any keystrokes in the keyboard buffer. :-(
- */
-
-FUNCTION(grub_gate_a20)
- movl %eax, %edx
-
-gate_a20_test_current_state:
- /* first of all, test if already in a good state */
- call gate_a20_check_state
- cmpb %al, %dl
- jnz gate_a20_try_bios
- ret
-
-gate_a20_try_bios:
- /* second, try a BIOS call */
- pushl %ebp
- call prot_to_real
-
- .code16
- movw $0x2400, %ax
- testb %dl, %dl
- jz 1f
- incw %ax
-1: int $0x15
-
- DATA32 call real_to_prot
- .code32
-
- popl %ebp
- call gate_a20_check_state
- cmpb %al, %dl
- jnz gate_a20_try_keyboard_controller
- ret
-
-gate_a20_flush_keyboard_buffer:
- inb $0x64
- andb $0x02, %al
- jnz gate_a20_flush_keyboard_buffer
-2:
- inb $0x64
- andb $0x01, %al
- jz 3f
- inb $0x60
- jmp 2b
-3:
- ret
-
-gate_a20_try_keyboard_controller:
- /* third, try the keyboard controller */
- call gate_a20_flush_keyboard_buffer
-
- movb $0xd1, %al
- outb $0x64
-4:
- inb $0x64
- andb $0x02, %al
- jnz 4b
-
- movb $0xdd, %al
- testb %dl, %dl
- jz 5f
- orb $0x02, %al
-5: outb $0x60
- call gate_a20_flush_keyboard_buffer
-
- /* output a dummy command (USB keyboard hack) */
- movb $0xff, %al
- outb $0x64
- call gate_a20_flush_keyboard_buffer
-
- call gate_a20_check_state
- cmpb %al, %dl
- jnz gate_a20_try_system_control_port_a
- ret
-
-gate_a20_try_system_control_port_a:
- /* fourth, try the system control port A */
- inb $0x92
- andb $(~0x03), %al
- testb %dl, %dl
- jz 6f
- orb $0x02, %al
-6: outb $0x92
-
- /* When turning off Gate A20, do not check the state strictly,
- because a failure is not fatal usually, and Gate A20 is
always
- on some modern machines. */
- testb %dl, %dl
- jz 7f
- call gate_a20_check_state
- cmpb %al, %dl
- /* everything failed, so restart from the beginning */
- jnz gate_a20_try_bios
-7: ret
-
-gate_a20_check_state:
- /* iterate the checking for a while */
- movl $100, %ecx
-1:
- call 3f
- cmpb %al, %dl
- jz 2f
- loop 1b
-2:
- ret
-3:
- pushl %ebx
- pushl %ecx
- xorl %eax, %eax
- /* compare the byte at 0x8000 with that at 0x108000 */
- movl $GRUB_BOOT_MACHINE_KERNEL_ADDR, %ebx
- pushl %ebx
- /* save the original byte in CL */
- movb (%ebx), %cl
- /* store the value at 0x108000 in AL */
- addl $0x100000, %ebx
- movb (%ebx), %al
- /* try to set one less value at 0x8000 */
- popl %ebx
- movb %al, %ch
- decb %ch
- movb %ch, (%ebx)
- /* serialize */
- outb %al, $0x80
- outb %al, $0x80
- /* obtain the value at 0x108000 in CH */
- pushl %ebx
- addl $0x100000, %ebx
- movb (%ebx), %ch
- /* this result is 1 if A20 is on or 0 if it is off */
- subb %ch, %al
- xorb $1, %al
- /* restore the original */
- popl %ebx
- movb %cl, (%ebx)
- popl %ecx
- popl %ebx
- ret
-
-#include "lzo1x.S"
-
-
-/*
- * This call is special... it never returns... in fact it should simply
- * hang at this point!
- */
-
-FUNCTION(grub_stop)
- call prot_to_real
-
- /*
- * This next part is sort of evil. It takes advantage of the
- * byte ordering on the x86 to work in either 16-bit or 32-bit
- * mode, so think about it before changing it.
- */
-
-FUNCTION(grub_hard_stop)
- hlt
- jmp EXT_C(grub_hard_stop)
-
-
-/*
- * grub_stop_floppy()
- *
- * Stop the floppy drive from spinning, so that other software is
- * jumped to with a known state.
- */
-FUNCTION(grub_stop_floppy)
- movw $0x3F2, %dx
- xorb %al, %al
- outb %al, %dx
- ret
-
-/*
- * grub_reboot()
- *
- * Reboot the system. At the moment, rely on BIOS.
- */
-FUNCTION(grub_reboot)
- call prot_to_real
- .code16
- /* cold boot */
- movw $0x0472, %di
- movw %ax, (%di)
- ljmp $0xFFFF, $0x0000
- .code32
-
-/*
- * grub_halt(int no_apm)
- *
- * Halt the system, using APM if possible. If NO_APM is true, don't use
- * APM even if it is available.
- */
-FUNCTION(grub_halt)
- /* see if zero */
- testl %eax, %eax
- jnz EXT_C(grub_stop)
-
- call prot_to_real
- .code16
-
- /* detect APM */
- movw $0x5300, %ax
- xorw %bx, %bx
- int $0x15
- jc EXT_C(grub_hard_stop)
- /* don't check %bx for buggy BIOSes... */
-
- /* disconnect APM first */
- movw $0x5304, %ax
- xorw %bx, %bx
- int $0x15
-
- /* connect APM */
- movw $0x5301, %ax
- xorw %bx, %bx
- int $0x15
- jc EXT_C(grub_hard_stop)
-
- /* set APM protocol level - 1.1 or bust. (this covers APM 1.2
also) */
- movw $0x530E, %ax
- xorw %bx, %bx
- movw $0x0101, %cx
- int $0x15
- jc EXT_C(grub_hard_stop)
-
- /* set the power state to off */
- movw $0x5307, %ax
- movw $1, %bx
- movw $3, %cx
- int $0x15
-
- /* shouldn't reach here */
- jmp EXT_C(grub_hard_stop)
- .code32
-
-
-/*
- * void grub_chainloader_real_boot (int drive, void *part_addr)
- *
- * This starts another boot loader.
- */
-
-FUNCTION(grub_chainloader_real_boot)
- pushl %edx
- pushl %eax
-
- call EXT_C(grub_dl_unload_all)
-
- /* set up to pass boot drive */
- popl %edx
-
- /* ESI must point to a partition table entry */
- popl %esi
-
- /* Turn off Gate A20 */
- xorl %eax, %eax
- call EXT_C(grub_gate_a20)
-
- call prot_to_real
- .code16
- ljmp $0, $GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR
- .code32
-
-
-/*
- * void grub_linux_boot_zimage (void)
- */
-VARIABLE(grub_linux_prot_size)
- .long 0
-VARIABLE(grub_linux_tmp_addr)
- .long 0
-VARIABLE(grub_linux_real_addr)
- .long 0
-
-FUNCTION(grub_linux_boot_zimage)
- /* copy the kernel */
- movl EXT_C(grub_linux_prot_size), %ecx
- addl $3, %ecx
- shrl $2, %ecx
- movl $GRUB_LINUX_BZIMAGE_ADDR, %esi
- movl $GRUB_LINUX_ZIMAGE_ADDR, %edi
- cld
- rep
- movsl
-
-FUNCTION(grub_linux_boot_bzimage)
- call EXT_C(grub_dl_unload_all)
-
- movl EXT_C(grub_linux_real_addr), %ebx
-
- /* copy the real mode code */
- movl EXT_C(grub_linux_tmp_addr), %esi
- movl %ebx, %edi
- movl $GRUB_LINUX_SETUP_MOVE_SIZE, %ecx
- cld
- rep
- movsb
-
- /* change %ebx to the segment address */
- shrl $4, %ebx
- movl %ebx, %eax
- addl $0x20, %eax
- movw %ax, linux_setup_seg
-
- /* XXX new stack pointer in safe area for calling functions */
- movl $0x4000, %esp
- call EXT_C(grub_stop_floppy)
-
- /* final setup for linux boot */
- call prot_to_real
- .code16
-
- cli
- movw %bx, %ss
- movw $GRUB_LINUX_SETUP_STACK, %sp
-
- movw %bx, %ds
- movw %bx, %es
- movw %bx, %fs
- movw %bx, %gs
-
- /* ljmp */
- .byte 0xea
- .word 0
-linux_setup_seg:
- .word 0
- .code32
-
-
-/*
- * This starts the multiboot kernel.
- */
-
-FUNCTION(grub_multiboot_real_boot)
- /* Push the entry address on the stack. */
- pushl %eax
- /* Move the address of the multiboot information structure to
ebx. */
- movl %edx,%ebx
-
- /* Unload all modules and stop the floppy driver. */
- call EXT_C(grub_dl_unload_all)
- call EXT_C(grub_stop_floppy)
-
- /* Interrupts should be disabled. */
- cli
-
- /* Move the magic value into eax and jump to the kernel. */
- movl $GRUB_MB_MAGIC2,%eax
- popl %ecx
- jmp *%ecx
-
-
-/*
- * int grub_biosdisk_rw_int13_extensions (int ah, int drive, void
*dap)
- *

- *   Call IBM/MS INT13 Extensions (int 13 %ah=AH) for DRIVE. DAP
- *   is passed for disk address packet. If an error occurs, return
- *   non-zero, otherwise zero.

- */
-
-FUNCTION(grub_biosdisk_rw_int13_extensions)
- pushl %ebp
- pushl %esi
-
- /* compute the address of disk_address_packet */
- movw %cx, %si
- xorw %cx, %cx
- shrl $4, %ecx /* save the segment to cx */
-
- /* ah */
- movb %al, %dh
- /* enter real mode */
- call prot_to_real
-
- .code16
- movb %dh, %ah
- movw %cx, %ds
- int $0x13 /* do the operation */
- movb %ah, %dl /* save return value */
- /* clear the data segment */
- xorw %ax, %ax
- movw %ax, %ds
- /* back to protected mode */
- DATA32 call real_to_prot
- .code32
-
- movb %dl, %al /* return value in %eax */
-
- popl %esi
- popl %ebp
-
- ret
-
-/*
- * int grub_biosdisk_rw_standard (int ah, int drive, int coff, int
hoff,
- * int soff, int nsec, int segment)
- *

- *   Call standard and old INT13 (int 13 %ah=AH) for DRIVE. Read/write
- *   NSEC sectors from COFF/HOFF/SOFF into SEGMENT. If an error occurs,
- *   return non-zero, otherwise zero.

- */
-
-FUNCTION(grub_biosdisk_rw_standard)
- pushl %ebp
- movl %esp, %ebp
-
- pushl %ebx
- pushl %edi
- pushl %esi
-
- /* set up CHS information */
-
- /* set %ch to low eight bits of cylinder */
- xchgb %cl, %ch
- /* set bits 6-7 of %cl to high two bits of cylinder */
- shlb $6, %cl
- /* set bits 0-5 of %cl to sector */
- addb 0xc(%ebp), %cl
- /* set %dh to head */
- movb 0x8(%ebp), %dh
- /* set %ah to AH */
- movb %al, %ah
- /* set %al to NSEC */
- movb 0x10(%ebp), %al
- /* save %ax in %di */
- movw %ax, %di
- /* save SEGMENT in %bx */
- movw 0x14(%ebp), %bx
-
- /* enter real mode */
- call prot_to_real
-
- .code16
- movw %bx, %es
- xorw %bx, %bx
- movw $3, %si /* attempt at least three times */
-
-1:
- movw %di, %ax
- int $0x13 /* do the operation */
- jnc 2f /* check if successful */
-
- movb %ah, %bl /* save return value */
- /* if fail, reset the disk system */
- xorw %ax, %ax
- int $0x13
-
- decw %si
- cmpw $0, %si
- je 2f
- xorb %bl, %bl
- jmp 1b /* retry */
-2:
- /* back to protected mode */
- DATA32 call real_to_prot
- .code32
-
- movb %bl, %al /* return value in %eax */
-
- popl %esi
- popl %edi
- popl %ebx
- popl %ebp
-
- ret $(4 * 4)
-
-
-/*
- * int grub_biosdisk_check_int13_extensions (int drive)
- *
- * Check if LBA is supported for DRIVE. If it is supported, then
return
- * the major version of extensions, otherwise zero.
- */
- -FUNCTION(grub_biosdisk_check_int13_extensions)
- pushl %ebp
- pushl %ebx
-
- /* drive */
- movb %al, %dl
- /* enter real mode */
- call prot_to_real
-
- .code16
- movb $0x41, %ah
- movw $0x55aa, %bx
- int $0x13 /* do the operation */
-
- /* check the result */
- jc 1f
- cmpw $0xaa55, %bx
- jne 1f
-
- movb %ah, %bl /* save the major version into %bl */
-
- /* check if AH=0x42 is supported */
- andw $1, %cx
- jnz 2f
-
-1:
- xorb %bl, %bl
-2:
- /* back to protected mode */
- DATA32 call real_to_prot
- .code32
-
- movb %bl, %al /* return value in %eax */
-
- popl %ebx
- popl %ebp
-
- ret
-
-
-/*
- * int grub_biosdisk_get_diskinfo_int13_extensions (int drive, void
*drp)
- *
- * Return the geometry of DRIVE in a drive parameters, DRP. If an
error
- * occurs, then return non-zero, otherwise zero.
- */
- -FUNCTION(grub_biosdisk_get_diskinfo_int13_extensions)
- pushl %ebp
- pushl %ebx
- pushl %esi
-
- /* compute the address of drive parameters */
- movw %dx, %si
- xorw %dx, %dx
- shrl $4, %edx
- movw %dx, %bx /* save the segment into %bx */
- /* drive */
- movb %al, %dl
- /* enter real mode */
- call prot_to_real
-
- .code16
- movb $0x48, %ah
- movw %bx, %ds
- int $0x13 /* do the operation */
- movb %ah, %bl /* save return value in %bl */
- /* clear the data segment */
- xorw %ax, %ax
- movw %ax, %ds
- /* back to protected mode */
- DATA32 call real_to_prot
- .code32
-
- movb %bl, %al /* return value in %eax */
-
- popl %esi
- popl %ebx
- popl %ebp
-
- ret
-
-
-/*
- * int grub_biosdisk_get_diskinfo_standard (int drive,
- * unsigned long *cylinders,

- * unsigned long *heads,
- * unsigned long *sectors)
- *
- * Return the geometry of DRIVE in CYLINDERS, HEADS and SECTORS. If
an
- * error occurs, then return non-zero, otherwise zero.
- */
-
-FUNCTION(grub_biosdisk_get_diskinfo_standard)
- pushl %ebp
- pushl %ebx
- pushl %edi
-
- /* push CYLINDERS */
- pushl %edx
- /* push HEADS */
- pushl %ecx
- /* SECTORS is on the stack */
-
- /* drive */
- movb %al, %dl
- /* enter real mode */
- call prot_to_real
-
- .code16
- movb $0x8, %ah
- int $0x13 /* do the operation */
- /* check if successful */
- testb %ah, %ah
- jnz 1f
- /* bogus BIOSes may not return an error number */
- testb $0x3f, %cl /* 0 sectors means no disk */
- jnz 1f /* if non-zero, then succeed */
- /* XXX 0x60 is one of the unused error numbers */
- movb $0x60, %ah
-1:
- movb %ah, %bl /* save return value in %bl */
- /* back to protected mode */
- DATA32 call real_to_prot
- .code32
-
- /* pop HEADS */
- popl %edi
- movb %dh, %al
- incl %eax /* the number of heads is counted from zero */
- movl %eax, (%edi)
-
- /* pop CYLINDERS */
- popl %edi
- movb %ch, %al
- movb %cl, %ah
- shrb $6, %ah /* the number of cylinders is counted from zero
*/
- incl %eax
- movl %eax, (%edi)
-
- /* SECTORS */
- movl 0x10(%esp), %edi
- andb $0x3f, %cl
- movzbl %cl, %eax
- movl %eax, (%edi)
-
- xorl %eax, %eax
- movb %bl, %al /* return value in %eax */
-
- popl %edi
- popl %ebx
- popl %ebp
-
- ret $4
-
-
-/*
- * int grub_biosdisk_get_num_floppies (void)
- */
-FUNCTION(grub_biosdisk_get_num_floppies)
- pushl %ebp
-
- xorl %edx, %edx
- call prot_to_real
-
- .code16
- /* reset the disk system first */
- int $0x13
-1:
- stc
-
- /* call GET DISK TYPE */
- movb $0x15, %ah
- int $0x13
-
- jc 2f
-
- /* check if this drive exists */
- testb $0x3, %ah
- jz 2f
-
- incb %dl
- cmpb $2, %dl
- jne 1b
-2:
- DATA32 call real_to_prot
- .code32
-
- movl %edx, %eax
- popl %ebp
- ret
-
-
-/*
- *
- * grub_get_memsize(i) : return the memory size in KB. i == 0 for conventional
- * memory, i == 1 for extended memory
- * BIOS call "INT 12H" to get conventional memory size
- * BIOS call "INT 15H, AH=88H" to get extended memory size
- * Both have the return value in AX.
- *
- */
-
-FUNCTION(grub_get_memsize)
- pushl %ebp
-
- movl %eax, %edx
-
- call prot_to_real /* enter real mode */
- .code16
-
- testl %edx, %edx
- jnz xext
-
- int $0x12
- jmp xdone
-
-xext:
- movb $0x88, %ah
- int $0x15
-
-xdone:
- movw %ax, %dx
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
-
- popl %ebp
- ret
-
-
-/*
- *
- * grub_get_eisa_mmap() : return packed EISA memory map, lower 16 bits is
- * memory between 1M and 16M in 1K parts, upper 16 bits is
- * memory above 16M in 64K parts. If error, return zero.
- * BIOS call "INT 15H, AH=E801H" to get EISA memory map,
- * AX = memory between 1M and 16M in 1K parts.
- * BX = memory above 16M in 64K parts.
- *
- */
-
-FUNCTION(grub_get_eisa_mmap)
- pushl %ebp
- pushl %ebx
-
- call prot_to_real /* enter real mode */
- .code16
-
- movw $0xe801, %ax
- int $0x15
-
- shll $16, %ebx
- movw %ax, %bx
-
- DATA32 call real_to_prot
- .code32
-
- cmpb $0x86, %bh
- je xnoteisa
-
- movl %ebx, %eax
-
-xnoteisa:
- popl %ebx
- popl %ebp
- ret
-
-/*
- *
- * grub_get_mmap_entry(addr, cont) : address and old continuation value (zero to
- * start), for the Query System Address Map BIOS call.
- *
- * Sets the first 4-byte int value of "addr" to the size returned by
- * the call. If the call fails, sets it to zero.
- *
- * Returns: new (non-zero) continuation value, 0 if done.
- */
-
-FUNCTION(grub_get_mmap_entry)
- pushl %ebp
- pushl %ebx
- pushl %edi
- pushl %esi
-
- /* push ADDR */
- pushl %eax
-
- /* place address (+4) in ES:DI */
- addl $4, %eax
- movl %eax, %edi
- andl $0xf, %edi
- shrl $4, %eax
- movl %eax, %esi
-
- /* set continuation value */
- movl %edx, %ebx
-
- /* set default maximum buffer size */
- movl $0x14, %ecx
-
- /* set EDX to 'SMAP' */
- movl $0x534d4150, %edx
-
- call prot_to_real /* enter real mode */
- .code16
-
- movw %si, %es
- movl $0xe820, %eax
- int $0x15
-
- DATA32 jc xnosmap
-
- cmpl $0x534d4150, %eax
- jne xnosmap
-
- cmpl $0x14, %ecx
- jl xnosmap
-
- cmpl $0x400, %ecx
- jg xnosmap
-
- jmp xsmap
-
-xnosmap:
- xorl %ecx, %ecx
-
-xsmap:
- DATA32 call real_to_prot
- .code32
-
- /* write length of buffer (zero if error) into ADDR */
- popl %eax
- movl %ecx, (%eax)
-
- /* set return value to continuation */
- movl %ebx, %eax
-
- popl %esi
- popl %edi
- popl %ebx
- popl %ebp
- ret
-
-
-/*
- * void grub_console_real_putchar (int c)
- *
- * Put the character C on the console. Because GRUB wants to write a
- * character with an attribute, this implementation is a bit tricky.
- * If C is a control character (CR, LF, BEL, BS), use INT 10, AH = 0Eh
- * (TELETYPE OUTPUT). Otherwise, save the original position, put a space,
- * save the current position, restore the original position, write the
- * character and the attribute, and restore the current position.
- *
- * The reason why this is so complicated is that there is no easy way to
- * get the height of the screen, and the TELETYPE OUPUT BIOS call doesn't
- * support setting a background attribute.
- */
-FUNCTION(grub_console_real_putchar)
- movl %eax, %edx
- pusha
- movb EXT_C(grub_console_cur_color), %bl
-
- call prot_to_real
- .code16
- movb %dl, %al
- xorb %bh, %bh
-
- /* use teletype output if control character */
- cmpb $0x7, %al
- je 1f
- cmpb $0x8, %al
- je 1f
- cmpb $0xa, %al
- je 1f
- cmpb $0xd, %al
- je 1f
-
- /* save the character and the attribute on the stack */
- pushw %ax
- pushw %bx
-
- /* get the current position */
- movb $0x3, %ah
- int $0x10
-
- /* check the column with the width */
- cmpb $79, %dl
- jl 2f
-
- /* print CR and LF, if next write will exceed the width */
- movw $0x0e0d, %ax
- int $0x10
- movb $0x0a, %al
- int $0x10
-
- /* get the current position */
- movb $0x3, %ah
- int $0x10
-
-2:
- /* restore the character and the attribute */
- popw %bx
- popw %ax
-
- /* write the character with the attribute */
- movb $0x9, %ah
- movw $1, %cx
- int $0x10
-
- /* move the cursor forward */
- incb %dl
- movb $0x2, %ah
- int $0x10
-
- jmp 3f
-
-1: movw $1, %bx
- movb $0xe, %ah
- int $0x10
-
-3: DATA32 call real_to_prot
- .code32
-
- popa
- ret
-
-
-/*
- * int grub_console_getkey (void)
- * BIOS call "INT 16H Function 00H" to read character from keyboard
- * Call with %ah = 0x0
- * Return: %ah = keyboard scan code
- * %al = ASCII character
- */
-
-/* this table is used in translate_keycode below */ -translation_table:
- .word GRUB_CONSOLE_KEY_LEFT, 2
- .word GRUB_CONSOLE_KEY_RIGHT, 6
- .word GRUB_CONSOLE_KEY_UP, 16
- .word GRUB_CONSOLE_KEY_DOWN, 14
- .word GRUB_CONSOLE_KEY_HOME, 1
- .word GRUB_CONSOLE_KEY_END, 5
- .word GRUB_CONSOLE_KEY_DC, 4
- .word GRUB_CONSOLE_KEY_BACKSPACE, 8
- .word GRUB_CONSOLE_KEY_PPAGE, 7
- .word GRUB_CONSOLE_KEY_NPAGE, 3
- .word 0
-
-/*
- * translate_keycode translates the key code %dx to an ascii code.
- */
- .code16
-
-translate_keycode:
- pushw %bx
- pushw %si
-
- movw $ABS(translation_table), %si
-
-1: lodsw
- /* check if this is the end */
- testw %ax, %ax
- jz 2f
- /* load the ascii code into %ax */
- movw %ax, %bx
- lodsw
- /* check if this matches the key code */
- cmpw %bx, %dx
- jne 1b
- /* translate %dx, if successful */
- movw %ax, %dx
-
-2: popw %si
- popw %bx
- ret
-
- .code32
-
-FUNCTION(grub_console_getkey)
- pushl %ebp
-
- call prot_to_real
- .code16
-
- int $0x16
-
- movw %ax, %dx /* real_to_prot uses %eax */
- call translate_keycode
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
-
- popl %ebp
- ret
-
-
-/*
- * int grub_console_checkkey (void)
- * if there is a character pending, return it; otherwise return -1
- * BIOS call "INT 16H Function 01H" to check whether a character is pending
- * Call with %ah = 0x1
- * Return:
- * If key waiting to be input:
- * %ah = keyboard scan code
- * %al = ASCII character
- * Zero flag = clear
- * else
- * Zero flag = set
- */
-FUNCTION(grub_console_checkkey)
- pushl %ebp
- xorl %edx, %edx
-
- call prot_to_real /* enter real mode */
- .code16
-
- movb $0x1, %ah
- int $0x16
-
- jz notpending
-
- movw %ax, %dx
- DATA32 jmp pending
-
-notpending:
- decl %edx
-
-pending:
- DATA32 call real_to_prot
- .code32
-
- movl %edx, %eax
-
- popl %ebp
- ret
-
-
-/*
- * grub_uint16_t grub_console_getxy (void)
- * BIOS call "INT 10H Function 03h" to get cursor position
- * Call with %ah = 0x03
- * %bh = page

- *      Returns         %ch = starting scan line
- *                      %cl = ending scan line
- *                      %dh = row (0 is top)
- *                      %dl = column (0 is left)

- */
-
-
-FUNCTION(grub_console_getxy)
- pushl %ebp
- pushl %ebx /* save EBX */
-
- call prot_to_real
- .code16
-
- xorb %bh, %bh /* set page to 0 */
- movb $0x3, %ah
- int $0x10 /* get cursor position */
-
- DATA32 call real_to_prot
- .code32
-
- movb %dl, %ah
- movb %dh, %al
-
- popl %ebx
- popl %ebp
- ret
-
-
-/*
- * void grub_console_gotoxy(grub_uint8_t x, grub_uint8_t y)
- * BIOS call "INT 10H Function 02h" to set cursor position
- * Call with %ah = 0x02
- * %bh = page
- * %dh = row (0 is top)
- * %dl = column (0 is left)
- */
-
-
-FUNCTION(grub_console_gotoxy)
- pushl %ebp
- pushl %ebx /* save EBX */
-
- movb %dl, %dh /* %dh = y */
- movb %al, %dl /* %dl = x */
-
- call prot_to_real
- .code16
-
- xorb %bh, %bh /* set page to 0 */
- movb $0x2, %ah
- int $0x10 /* set cursor position */
-
- DATA32 call real_to_prot
- .code32
-
- popl %ebx
- popl %ebp
- ret
-
-
-/*
- * void grub_console_cls (void)
- * BIOS call "INT 10H Function 09h" to write character and attribute
- * Call with %ah = 0x09

- *                      %al = (character)
- *                      %bh = (page number)
- *                      %bl = (attribute)
- *                      %cx = (number of times)

- */
-
-FUNCTION(grub_console_cls)
- pushl %ebp
- pushl %ebx /* save EBX */
-
- call prot_to_real
- .code16
-
- /* move the cursor to the beginning */
- movb $0x02, %ah
- xorb %bh, %bh
- xorw %dx, %dx
- int $0x10
-
- /* write spaces to the entire screen */
- movw $0x0920, %ax
- movw $0x07, %bx
- movw $(80 * 25), %cx
- int $0x10
-
- /* move back the cursor */
- movb $0x02, %ah
- int $0x10
-
- DATA32 call real_to_prot
- .code32
-
- popl %ebx
- popl %ebp
- ret
-
-
-/*
- * void grub_console_setcursor (int on)
- * BIOS call "INT 10H Function 01h" to set cursor type

- *      Call with       %ah = 0x01
- *                      %ch = cursor starting scanline
- *                      %cl = cursor ending scanline

- */
-
-console_cursor_state:
- .byte 1
-console_cursor_shape:
- .word 0
-
-FUNCTION(grub_console_setcursor)
- pushl %ebp
- pushl %ebx
-
- /* push ON */
- pushl %eax
-
- /* check if the standard cursor shape has already been saved */
- movw console_cursor_shape, %ax
- testw %ax, %ax
- jne 1f
-
- call prot_to_real
- .code16
-
- movb $0x03, %ah
- xorb %bh, %bh
- int $0x10
-
- DATA32 call real_to_prot
- .code32
-
- movw %cx, console_cursor_shape
-1:
- /* set %cx to the designated cursor shape */
- movw $0x2000, %cx
- popl %eax
- testl %eax, %eax
- jz 2f
- movw console_cursor_shape, %cx
-2:
- call prot_to_real
- .code16
-
- movb $0x1, %ah
- int $0x10
-
- DATA32 call real_to_prot
- .code32
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_getrtsecs()
- * if a seconds value can be read, read it and return it (BCD),
- * otherwise return 0xFF
- * BIOS call "INT 1AH Function 02H" to check whether a character is pending
- * Call with %ah = 0x2
- * Return:
- * If RT Clock can give correct values
- * %ch = hour (BCD)
- * %cl = minutes (BCD)
- * %dh = seconds (BCD)
- * %dl = daylight savings time (00h std, 01h
daylight)
- * Carry flag = clear
- * else
- * Carry flag = set
- * (this indicates that the clock is updating,
or
- * that it isn't running)
- */
-FUNCTION(grub_getrtsecs)
- pushl %ebp
-
- call prot_to_real /* enter real mode */
- .code16
-
- clc
- movb $0x2, %ah
- int $0x1a
-
- DATA32 jnc gottime
- movb $0xff, %dh
-
-gottime:
- DATA32 call real_to_prot
- .code32
-
- movb %dh, %al
-
- popl %ebp
- ret
-
-
-/*
- * grub_get_rtc()
- * return the real time in ticks, of which there are about
- * 18-20 per second
- */
-FUNCTION(grub_get_rtc)
- pushl %ebp
-
- call prot_to_real /* enter real mode */
- .code16
-
- /* %ax is already zero */
- int $0x1a
-
- DATA32 call real_to_prot
- .code32
-
- movl %ecx, %eax
- shll $16, %eax
- movw %dx, %ax
-
- popl %ebp
- ret
-
-
-/*
- * unsigned char grub_vga_set_mode (unsigned char mode)
- */
-FUNCTION(grub_vga_set_mode)
- pushl %ebp
- pushl %ebx
- movl %eax, %ecx
-
- call prot_to_real
- .code16
- /* get current mode */
- xorw %bx, %bx
- movb $0x0f, %ah
- int $0x10
- movb %al, %dl
-
- /* set the new mode */
- movb %cl, %al
- xorb %ah, %ah
- int $0x10
-
- DATA32 call real_to_prot
- .code32
-
- movb %dl, %al
- popl %ebx
- popl %ebp
- ret
-
-
-/*
- * unsigned char *grub_vga_get_font (void)
- */
-FUNCTION(grub_vga_get_font)
- pushl %ebp
- pushl %ebx
-
- call prot_to_real
- .code16
- movw $0x1130, %ax
- movb $0x06, %bh
- int $0x10
- movw %es, %bx
- movw %bp, %dx
- DATA32 call real_to_prot
- .code32
-
- movzwl %bx, %ecx
- shll $4, %ecx
- movw %dx, %ax
- addl %ecx, %eax
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_get_controller_info (struct grub_vbe_info_block *controller_info)
- *
- * Register allocations for parameters:
- * %eax *controller_info
- */
-FUNCTION(grub_vbe_get_controller_info)
- pushl %ebp
- pushl %edi
-
- movw %ax, %di /* Store *controller_info to %edx:%di.
*/
- xorw %ax, %ax
- shrl $4, %eax
- mov %eax, %edx /* prot_to_real destroys %eax. */
-
- call prot_to_real
- .code16
-
- pushw %es
-
- movw %dx, %es /* *controller_info is now on %es:%di.
*/
- movw $0x4f00, %ax
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- popw %es
-
- DATA32 call real_to_prot
- .code32
-
- movl %edx, %eax
- andl $0x0FFFF, %eax /* Return value in %eax. */
-
- popl %edi
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_get_mode_info (grub_uint32_t mode,
- * struct
grub_vbe_mode_info_block *mode_info)
- *
- * Register allocations for parameters:
- * %eax mode
- * %edx *mode_info
- */
-FUNCTION(grub_vbe_get_mode_info)
- pushl %ebp
- pushl %edi
-
- movl %eax, %ecx /* Store mode number to %ecx. */
-

-	movw    %dx, %di	/* Store *mode_info to %edx:%di.  */
-	xorw    %dx, %dx
-	shrl    $4, %edx

-
- call prot_to_real
- .code16
-
- pushw %es
-

-	movw    %dx, %es	/* *mode_info is now on %es:%di.  */
-	movw    $0x4f01, %ax
-	int     $0x10

-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- popw %es
-
- DATA32 call real_to_prot
- .code32
-
- movl %edx, %eax
- andl $0x0FFFF, %eax /* Return value in %eax. */
-
- popl %edi
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_set_mode (grub_uint32_t mode,
- * struct grub_vbe_crtc_info_block
*crtc_info)
- *
- * Register allocations for parameters:
- * %eax mode
- * %edx *crtc_info
- */
-FUNCTION(grub_vbe_set_mode)
- pushl %ebp
- pushl %ebx
-
- movl %eax, %ebx /* Store mode in %ebx. */
-

-	movw    %dx, %di	/* Store *crtc_info to %edx:%di.  */
-	xorw    %dx, %dx
-	shrl    $4, %edx

-
- call prot_to_real
- .code16
-
- pushw %es
-
- movw %dx, %es /* *crtc_info is now on %es:%di. */
-
- movw $0x4f02, %ax
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- popw %es
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_get_mode (grub_uint32_t *mode)
- *
- * Register allocations for parameters:
- * %eax *mode
- */
-FUNCTION(grub_vbe_get_mode)
- pushl %ebp
- pushl %ebx
- pushl %eax /* Push *mode to stack. */
-
- call prot_to_real
- .code16
-
- movw $0x4f03, %ax
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- popl %edi /* Pops *mode from stack to %edi. */
- andl $0xFFFF, %ebx
- movl %ebx, (%edi)
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_set_memory_window (grub_uint32_t window,
- * grub_uint32_t
position);
- *
- * Register allocations for parameters:
- * %eax window
- * %edx position
- */
-FUNCTION(grub_vbe_set_memory_window)
- pushl %ebp
- pushl %ebx
-
- movl %eax, %ebx
-
- call prot_to_real
- .code16
-
- movw $0x4f05, %ax
- andw $0x00ff, %bx /* BL = window, BH = 0, Set memory
window. */
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_get_memory_window (grub_uint32_t window,
- * grub_uint32_t
*position);
- *
- * Register allocations for parameters:
- * %eax window
- * %edx *position
- */
-FUNCTION(grub_vbe_get_memory_window)
- pushl %ebp
- pushl %ebx
- pushl %edx /* Push *position to stack. */
-
- movl %eax, %ebx /* Store window in %ebx. */
-
- call prot_to_real
- .code16
-
- movw $0x4f05, %ax
- andw $0x00ff, %bx /* BL = window. */
- orw $0x0100, %bx /* BH = 1, Get memory window. */
- int $0x10
-
- movw %ax, %bx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- popl %edi /* pops *position from stack to %edi.
*/
- andl $0xFFFF, %edx
- movl %edx, (%edi) /* Return position to caller. */
-
- movw %bx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_set_scanline_length (grub_uint32_t length)
- *
- * Register allocations for parameters:
- * %eax length
- */
-FUNCTION(grub_vbe_set_scanline_length)
- pushl %ebp
- pushl %ebx
-
- movl %eax, %ecx /* Store length in %ecx. */
-
- call prot_to_real
- .code16
-
- movw $0x4f06, %ax
- movw $0x0002, %bx /* BL = 2, Set Scan Line in Bytes. */
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_get_scanline_length (grub_uint32_t *length)
- *
- * Register allocations for parameters:
- * %eax *length
- */
-FUNCTION(grub_vbe_get_scanline_length)
- pushl %ebp
- pushl %ebx
- pushl %edx /* Push *length to stack. */
-
- call prot_to_real
- .code16
-
- movw $0x4f06, %ax
- movw $0x0001, %bx /* BL = 1, Get Scan Line Length (in
bytes). */
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- popl %edi /* Pops *length from stack to %edi. */
- andl $0xFFFF, %ebx
- movl %ebx, (%edi) /* Return length to caller. */
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_set_display_start (grub_uint32_t x,
- * grub_uint32_t y)
- *
- * Register allocations for parameters:
- * %eax x
- * %edx y
- */
-FUNCTION(grub_vbe_set_display_start)
- pushl %ebp
- pushl %ebx
-
- movl %eax, %ecx /* Store x in %ecx. */
-
- call prot_to_real
- .code16
-
- movw $0x4f07, %ax
- movw $0x0080, %bx /* BL = 80h, Set Display Start
- during Vertical Retrace. */
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_get_display_start (grub_uint32_t *x,
- * grub_uint32_t *y)
- *
- * Register allocations for parameters:
- * %eax *x
- * %edx *y
- */
-FUNCTION(grub_vbe_get_display_start)
- pushl %ebp
- pushl %ebx
- pushl %eax /* Push *x to stack. */
- pushl %edx /* Push *y to stack. */
-
- call prot_to_real
- .code16
-
- movw $0x4f07, %ax
- movw $0x0001, %bx /* BL = 1, Get Display Start. */
- int $0x10
-
- movw %ax, %bx /* real_to_prot destroys %eax. */
-
- DATA32 call real_to_prot
- .code32
-
- popl %edi /* Pops *y from stack to %edi. */
- andl $0xFFFF, %edx
- movl %edx, (%edi) /* Return y-position to caller. */
-
- popl %edi /* Pops *x from stack to %edi. */
- andl $0xFFFF, %ecx
- movl %ecx, (%edi) /* Return x-position to caller. */
-
- movw %bx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret
-
-/*
- * grub_vbe_status_t grub_vbe_set_palette_data (grub_uint32_t color_count,
- * grub_uint32_t
start_index,
- * struct
grub_vbe_palette_data *palette_data)
- *
- * Register allocations for parameters:
- * %eax color_count
- * %edx start_index
- * %ecx *palette_data
- */
-FUNCTION(grub_vbe_set_palette_data)
- pushl %ebp
- pushl %ebx
-
- movl %eax, %ebx /* Store color_count in %ebx. */
-

-	movw    %cx, %di	/* Store *palette_data to %ecx:%di.  */
-	xorw    %cx, %cx
-	shrl    $4, %ecx

-
- call prot_to_real
- .code16
-
- pushw %es
-
- movw %cx, %es /* *palette_data is now on %es:%di. */
- movw %bx, %cx /* color_count is now on %cx. */
-
- movw $0x4f09, %ax
- xorw %bx, %bx /* BL = 0, Set Palette Data. */
- int $0x10
-
- movw %ax, %dx /* real_to_prot destroys %eax. */
-
- popw %es
-
- DATA32 call real_to_prot
- .code32
-
- movw %dx, %ax
- andl $0xFFFF, %eax /* Return value in %eax. */
-
- popl %ebx
- popl %ebp
- ret

FILE kern/i386/pc/startup.S.memparm (NEW)

(1) Same as old startup.S file, except parameters are passed in
stack
for global functions.

diff -EbwBruN ./kern/i386/pc/startup.S.memparm ../../src/grub-1.93/kern/i386/pc/startup.S.memparm
--- ./kern/i386/pc/startup.S.memparm 1970-01-01 01:00:00.000000000
+0100
+++ ../../src/grub-1.93/kern/i386/pc/startup.S.memparm 2006-05-04
00:04:26.000000000 +0200
@@ -0,0 +1,2209 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2005 Free Software Foundation, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *

+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

+ */
+
+
+/*
+ * Note: These functions defined in this file may be called from C.
+ * Be careful of that you must not modify some registers. Quote
+ * from gcc-2.95.2/gcc/config/i386/i386.h:
+

+   1 for registers not available across function calls.
+   These must include the FIXED_REGISTERS and also any
+   registers that can be used without being saved.
+   The latter must include the registers where values are returned
+   and the register where structure-value addresses are passed.
+   Aside from that, you can include as many other registers as you

like.
+
+ ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg +{ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
+ */
+
+/*
+ * Note: GRUB is compiled with the options -mrtd and -mregparm=3.
+ * So the first three arguments are passed in %eax, %edx, and
%ecx,
+ * respectively, and if a function has a fixed number of
arguments
+ * and the number if greater than three, the function must return
+ * with "ret $N" where N is ((the number of arguments) - 3) * 4.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+#include <grub/boot.h>
+#include <grub/machine/boot.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/console.h>
+#include <grub/machine/linux.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/multiboot.h>
+
+#define ABS(x) ((x) - EXT_C(start) + GRUB_BOOT_MACHINE_KERNEL_ADDR +
0x200)
+
+ .file "startup.S"
+
+ .text
+
+ /* Tell GAS to generate 16-bit instructions so that this code
works
+ in real mode. */
+ .code16
+
+ .globl start, _start
+start:
+_start:
+ /*
+ * Guarantee that "main" is loaded at 0x0:0x8200.
+ */
+ ljmp $0, $ABS(codestart)
+
+ /*
+ * Compatibility version number
+ *
+ * These MUST be at byte offset 6 and 7 of the executable
+ * DO NOT MOVE !!!
+ */
+ . = EXT_C(start) + 0x6
+ .byte GRUB_BOOT_VERSION_MAJOR, GRUB_BOOT_VERSION_MINOR
+
+ /*
+ * This is a special data area 8 bytes from the beginning.
+ */
+
+ . = EXT_C(start) + 0x8
+
+VARIABLE(grub_total_module_size)
+ .long 0
+VARIABLE(grub_kernel_image_size)
+ .long 0
+VARIABLE(grub_compressed_size)
+ .long 0
+VARIABLE(grub_install_dos_part)
+ .long 0xFFFFFFFF
+VARIABLE(grub_install_bsd_part)
+ .long 0xFFFFFFFF
+VARIABLE(grub_prefix)
+ .string "/boot/grub"
+
+ /*
+ * Leave some breathing room for the prefix.
+ */
+
+ . = EXT_C(start) + 0x50
+
+/*
+ * Support for booting GRUB from a Multiboot boot loader (e.g. GRUB itself).
+ * This uses the a.out kludge to load raw binary to the area starting at 1MB,
+ * and relocates itself after loaded.
+ */
+multiboot_header:
+ /* magic */
+ .long 0x1BADB002
+ /* flags */
+ .long (1 << 16)
+ /* checksum */
+ .long -0x1BADB002 - (1 << 16)
+ /* header addr */
+ .long multiboot_header - _start + 0x100000 + 0x200
+ /* load addr */
+ .long 0x100000
+ /* load end addr */
+ .long 0
+ /* bss end addr */
+ .long 0
+ /* entry addr */
+ .long multiboot_entry - _start + 0x100000 + 0x200
+
+multiboot_entry:
+ .code32
+ /* obtain the boot device */
+ movl 12(%ebx), %edx
+
+ /* relocate the code */
+ movl $(GRUB_KERNEL_MACHINE_RAW_SIZE + 0x200), %ecx
+ addl EXT_C(grub_compressed_size) - _start + 0x100000 + 0x200,
%ecx
+ movl $0x100000, %esi
+ movl $GRUB_BOOT_MACHINE_KERNEL_ADDR, %edi
+ cld
+ rep
+ movsb
+ /* jump to the real address */
+ movl $multiboot_trampoline, %eax
+ jmp *%eax
+
+multiboot_trampoline:
+ /* fill the boot information */
+ movl %edx, %eax
+ shrl $8, %eax
+ xorl %ebx, %ebx
+ cmpb $0xFF, %ah
+ je 1f
+ movb %ah, %bl
+ movl %ebx, EXT_C(grub_install_dos_part)
+1:
+ cmpb $0xFF, %al
+ je 2f
+ movb %al, %bl
+ movl %ebx, EXT_C(grub_install_bsd_part)
+2:
+ shrl $24, %edx
+ /* enter the usual booting */
+ call prot_to_real
+ .code16
+
+/* the real mode code continues... */
+codestart:
+ cli /* we're not safe here! */
+
+ /* set up %ds, %ss, and %es */
+ xorw %ax, %ax
+ movw %ax, %ds
+ movw %ax, %ss
+ movw %ax, %es
+
+ /* set up the real mode/BIOS stack */
+ movl $GRUB_MEMORY_MACHINE_REAL_STACK, %ebp
+ movl %ebp, %esp
+
+ sti /* we're safe again */
+
+ /* save boot drive reference */
+ ADDR32 movb %dl, EXT_C(grub_boot_drive)
+
+ /* reset disk system (%ah = 0) */
+ int $0x13
+
+ /* transition to protected mode */
+ DATA32 call real_to_prot
+
+ /* The ".code32" directive takes GAS out of 16-bit mode. */
+ .code32
+
+ incl %eax
+ call EXT_C(grub_gate_a20)
+
+ /* decompress the compressed part and put the result at 1MB */
+ movl $0x100000, %esi
+ movl $(START_SYMBOL + GRUB_KERNEL_MACHINE_RAW_SIZE), %edi
+
+ pushl %esi
+ pushl EXT_C(grub_compressed_size)
+ pushl %edi
+ call lzo1x_decompress
+ addl $12, %esp
+
+ /* copy back the decompressed part */
+ movl %eax, %ecx
+ cld
+ rep
+ movsb
+
+ /* copy modules before cleaning out the bss */
+ movl EXT_C(grub_total_module_size), %ecx
+ movl EXT_C(grub_kernel_image_size), %esi
+ addl %ecx, %esi
+ addl $START_SYMBOL, %esi
+ decl %esi
+ movl $END_SYMBOL, %edi
+ addl %ecx, %edi
+ decl %edi
+ std
+ rep
+ movsb
+
+ /* clean out the bss */
+ movl $BSS_START_SYMBOL, %edi
+
+ /* compute the bss length */
+ movl $END_SYMBOL, %ecx
+ subl %edi, %ecx
+
+ /* clean out */
+ xorl %eax, %eax
+ cld
+ rep
+ stosb
+
+ /*
+ * Call the start of main body of C code.
+ */
+ call EXT_C(grub_main)
+
+
+/*
+ * This is the area for all of the special variables.
+ */
+
+ .p2align 2 /* force 4-byte alignment */
+
+protstack:
+ .long GRUB_MEMORY_MACHINE_PROT_STACK
+
+VARIABLE(grub_boot_drive)
+ .long 0
+
+VARIABLE(grub_start_addr)
+ .long START_SYMBOL
+
+VARIABLE(grub_end_addr)
+ .long END_SYMBOL
+
+VARIABLE(grub_apm_bios_info)
+ .word 0 /* version */
+ .word 0 /* cseg */
+ .long 0 /* offset */
+ .word 0 /* cseg_16 */
+ .word 0 /* dseg_16 */
+ .word 0 /* cseg_len */
+ .word 0 /* cseg_16_len */
+ .word 0 /* dseg_16_len */
+
+/*
+ * This is the Global Descriptor Table
+ *
+ * An entry, a "Segment Descriptor", looks like this:
+ *
+ * 31 24 19 16 7 0
+ * ------------------------------------------------------------

+ * |             | |B| |A|       | |   |1|0|E|W|A|            |
+ * | BASE 31..24 |G|/|L|V| LIMIT |P|DPL|  TYPE   | BASE 23:16 |  4
+ * |             | |D| |L| 19..16| |   |1|1|C|R|A|            |
+ * ------------------------------------------------------------
+ * |                             |                            |
+ * |        BASE 15..0           |       LIMIT 15..0          |  0
+ * |                             |                            |

+ * ------------------------------------------------------------
+ *
+ * Note the ordering of the data items is reversed from the above
+ * description.
+ */
+
+ .p2align 2 /* force 4-byte alignment */
+gdt:
+ .word 0, 0
+ .byte 0, 0, 0, 0
+
+ /* -- code segment --
+ * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity),
present
+ * type = 32bit code execute/read, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x9A, 0xCF, 0
+
+ /* -- data segment --
+ * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
+ * type = 32 bit data read/write, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x92, 0xCF, 0
+
+ /* -- 16 bit real mode CS --
+ * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
+ * type = 16 bit code execute/read only/conforming, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x9E, 0, 0
+
+ /* -- 16 bit real mode DS --
+ * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
+ * type = 16 bit data read/write, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x92, 0, 0
+
+
+/* this is the GDT descriptor */
+gdtdesc:
+ .word 0x27 /* limit */
+ .long gdt /* addr */
+
+
+/*
+ * These next two routines, "real_to_prot" and "prot_to_real" are structured
+ * in a very specific way. Be very careful when changing them.
+ *
+ * NOTE: Use of either one messes up %eax and %ebp.
+ */
+
+real_to_prot:
+ .code16
+ cli
+
+ /* load the GDT register */
+ DATA32 ADDR32 lgdt gdtdesc
+
+ /* turn on protected mode */
+ movl %cr0, %eax
+ orl $GRUB_MEMORY_MACHINE_CR0_PE_ON, %eax
+ movl %eax, %cr0
+
+ /* jump to relocation, flush prefetch queue, and reload %cs */
+ DATA32 ljmp $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
+
+ .code32
+protcseg:
+ /* reload other segment registers */
+ movw $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* put the return address in a known safe location */
+ movl (%esp), %eax
+ movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
+
+ /* get protected mode stack */
+ movl protstack, %eax
+ movl %eax, %esp
+ movl %eax, %ebp
+
+ /* get return address onto the right stack */
+ movl GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+ movl %eax, (%esp)
+
+ /* zero %eax */
+ xorl %eax, %eax
+
+ /* return on the old (or initialized) stack! */
+ ret
+
+
+prot_to_real:
+ /* just in case, set GDT */
+ lgdt gdtdesc
+
+ /* save the protected mode stack */
+ movl %esp, %eax
+ movl %eax, protstack
+
+ /* get the return address */
+ movl (%esp), %eax
+ movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
+
+ /* set up new stack */
+ movl $GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+ movl %eax, %esp
+ movl %eax, %ebp
+
+ /* set up segment limits */
+ movw $GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* this might be an extra step */
+ /* jump to a 16 bit segment */
+ ljmp $GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
+
+tmpcseg:
+ .code16
+
+ /* clear the PE bit of CR0 */
+ movl %cr0, %eax
+ andl $(~GRUB_MEMORY_MACHINE_CR0_PE_ON), %eax
+ movl %eax, %cr0
+
+ /* flush prefetch queue, reload %cs */
+ DATA32 ljmp $0, $realcseg
+
+realcseg:
+ /* we are in real mode now
+ * set up the real mode segment registers : DS, SS, ES
+ */
+ /* zero %eax */
+ xorl %eax, %eax
+
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* restore interrupts */
+ sti
+
+ /* return on new stack! */
+ DATA32 ret
+
+ .code32
+
+
+/*
+ * grub_gate_a20(int on)
+ *
+ * Gate address-line 20 for high memory.
+ *
+ * This routine is probably overconservative in what it does, but so what?
+ *
+ * It also eats any keystrokes in the keyboard buffer. :-(
+ */
+
+FUNCTION(grub_gate_a20)
+ movl %eax, %edx
+
+gate_a20_test_current_state:
+ /* first of all, test if already in a good state */
+ call gate_a20_check_state
+ cmpb %al, %dl
+ jnz gate_a20_try_bios
+ ret
+
+gate_a20_try_bios:
+ /* second, try a BIOS call */
+ pushl %ebp
+ call prot_to_real
+
+ .code16
+ movw $0x2400, %ax
+ testb %dl, %dl
+ jz 1f
+ incw %ax
+1: int $0x15
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %ebp
+ call gate_a20_check_state
+ cmpb %al, %dl
+ jnz gate_a20_try_keyboard_controller
+ ret
+
+gate_a20_flush_keyboard_buffer:
+ inb $0x64
+ andb $0x02, %al
+ jnz gate_a20_flush_keyboard_buffer
+2:
+ inb $0x64
+ andb $0x01, %al
+ jz 3f
+ inb $0x60
+ jmp 2b
+3:
+ ret
+
+gate_a20_try_keyboard_controller:
+ /* third, try the keyboard controller */
+ call gate_a20_flush_keyboard_buffer
+
+ movb $0xd1, %al
+ outb $0x64
+4:
+ inb $0x64
+ andb $0x02, %al
+ jnz 4b
+
+ movb $0xdd, %al
+ testb %dl, %dl
+ jz 5f
+ orb $0x02, %al
+5: outb $0x60
+ call gate_a20_flush_keyboard_buffer
+
+ /* output a dummy command (USB keyboard hack) */
+ movb $0xff, %al
+ outb $0x64
+ call gate_a20_flush_keyboard_buffer
+
+ call gate_a20_check_state
+ cmpb %al, %dl
+ jnz gate_a20_try_system_control_port_a
+ ret
+
+gate_a20_try_system_control_port_a:
+ /* fourth, try the system control port A */
+ inb $0x92
+ andb $(~0x03), %al
+ testb %dl, %dl
+ jz 6f
+ orb $0x02, %al
+6: outb $0x92
+
+ /* When turning off Gate A20, do not check the state strictly,
+ because a failure is not fatal usually, and Gate A20 is
always
+ on some modern machines. */
+ testb %dl, %dl
+ jz 7f
+ call gate_a20_check_state
+ cmpb %al, %dl
+ /* everything failed, so restart from the beginning */
+ jnz gate_a20_try_bios
+7: ret
+
+gate_a20_check_state:
+ /* iterate the checking for a while */
+ movl $100, %ecx
+1:
+ call 3f
+ cmpb %al, %dl
+ jz 2f
+ loop 1b
+2:
+ ret
+3:
+ pushl %ebx
+ pushl %ecx
+ xorl %eax, %eax
+ /* compare the byte at 0x8000 with that at 0x108000 */
+ movl $GRUB_BOOT_MACHINE_KERNEL_ADDR, %ebx
+ pushl %ebx
+ /* save the original byte in CL */
+ movb (%ebx), %cl
+ /* store the value at 0x108000 in AL */
+ addl $0x100000, %ebx
+ movb (%ebx), %al
+ /* try to set one less value at 0x8000 */
+ popl %ebx
+ movb %al, %ch
+ decb %ch
+ movb %ch, (%ebx)
+ /* serialize */
+ outb %al, $0x80
+ outb %al, $0x80
+ /* obtain the value at 0x108000 in CH */
+ pushl %ebx
+ addl $0x100000, %ebx
+ movb (%ebx), %ch
+ /* this result is 1 if A20 is on or 0 if it is off */
+ subb %ch, %al
+ xorb $1, %al
+ /* restore the original */
+ popl %ebx
+ movb %cl, (%ebx)
+ popl %ecx
+ popl %ebx
+ ret
+
+#include "lzo1x.S"
+
+
+/*
+ * This call is special... it never returns... in fact it should simply
+ * hang at this point!
+ */
+
+FUNCTION(grub_stop)
+ call prot_to_real
+
+ /*
+ * This next part is sort of evil. It takes advantage of the
+ * byte ordering on the x86 to work in either 16-bit or 32-bit
+ * mode, so think about it before changing it.
+ */
+
+FUNCTION(grub_hard_stop)
+ hlt
+ jmp EXT_C(grub_hard_stop)
+
+
+/*
+ * grub_stop_floppy()
+ *
+ * Stop the floppy drive from spinning, so that other software is
+ * jumped to with a known state.
+ */
+FUNCTION(grub_stop_floppy)
+ movw $0x3F2, %dx
+ xorb %al, %al
+ outb %al, %dx
+ ret
+
+/*
+ * grub_reboot()
+ *
+ * Reboot the system. At the moment, rely on BIOS.
+ */
+FUNCTION(grub_reboot)
+ call prot_to_real
+ .code16
+ /* cold boot */
+ movw $0x0472, %di
+ movw %ax, (%di)
+ ljmp $0xFFFF, $0x0000
+ .code32
+
+/*
+ * grub_halt(int no_apm)
+ *
+ * Halt the system, using APM if possible. If NO_APM is true, don't use
+ * APM even if it is available.
+ */
+FUNCTION(grub_halt)
+ /* see if zero */
+ mov 4(%esp), %eax
+ testl %eax, %eax
+ jnz EXT_C(grub_stop)
+
+ call prot_to_real
+ .code16
+
+ /* detect APM */
+ movw $0x5300, %ax
+ xorw %bx, %bx
+ int $0x15
+ jc EXT_C(grub_hard_stop)
+ /* don't check %bx for buggy BIOSes... */
+
+ /* disconnect APM first */
+ movw $0x5304, %ax
+ xorw %bx, %bx
+ int $0x15
+
+ /* connect APM */
+ movw $0x5301, %ax
+ xorw %bx, %bx
+ int $0x15
+ jc EXT_C(grub_hard_stop)
+
+ /* set APM protocol level - 1.1 or bust. (this covers APM 1.2
also) */
+ movw $0x530E, %ax
+ xorw %bx, %bx
+ movw $0x0101, %cx
+ int $0x15
+ jc EXT_C(grub_hard_stop)
+
+ /* set the power state to off */
+ movw $0x5307, %ax
+ movw $1, %bx
+ movw $3, %cx
+ int $0x15
+
+ /* shouldn't reach here */
+ jmp EXT_C(grub_hard_stop)
+ .code32
+
+
+/*
+ * void grub_chainloader_real_boot (int drive, void *part_addr)
+ *
+ * This starts another boot loader.
+ */
+
+FUNCTION(grub_chainloader_real_boot)
+ pushl %edx
+ pushl %eax
+
+ call EXT_C(grub_dl_unload_all)
+
+ /* set up to pass boot drive */
+ mov 4(%esp),%edx
+
+ /* ESI must point to a partition table entry */
+ mov 8(%esp),%esi
+
+ /* Turn off Gate A20 */
+ xorl %eax, %eax
+ call EXT_C(grub_gate_a20)
+
+ call prot_to_real
+ .code16
+ ljmp $0, $GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR
+ .code32
+
+
+/*
+ * void grub_linux_boot_zimage (void)
+ */
+VARIABLE(grub_linux_prot_size)
+ .long 0
+VARIABLE(grub_linux_tmp_addr)
+ .long 0
+VARIABLE(grub_linux_real_addr)
+ .long 0
+
+FUNCTION(grub_linux_boot_zimage)
+ /* copy the kernel */
+ movl EXT_C(grub_linux_prot_size), %ecx
+ addl $3, %ecx
+ shrl $2, %ecx
+ movl $GRUB_LINUX_BZIMAGE_ADDR, %esi
+ movl $GRUB_LINUX_ZIMAGE_ADDR, %edi
+ cld
+ rep
+ movsl
+
+FUNCTION(grub_linux_boot_bzimage)
+ call EXT_C(grub_dl_unload_all)
+
+ movl EXT_C(grub_linux_real_addr), %ebx
+
+ /* copy the real mode code */
+ movl EXT_C(grub_linux_tmp_addr), %esi
+ movl %ebx, %edi
+ movl $GRUB_LINUX_SETUP_MOVE_SIZE, %ecx
+ cld
+ rep
+ movsb
+
+ /* change %ebx to the segment address */
+ shrl $4, %ebx
+ movl %ebx, %eax
+ addl $0x20, %eax
+ movw %ax, linux_setup_seg
+
+ /* XXX new stack pointer in safe area for calling functions */
+ movl $0x4000, %esp
+ call EXT_C(grub_stop_floppy)
+
+ /* final setup for linux boot */
+ call prot_to_real
+ .code16
+
+ cli
+ movw %bx, %ss
+ movw $GRUB_LINUX_SETUP_STACK, %sp
+
+ movw %bx, %ds
+ movw %bx, %es
+ movw %bx, %fs
+ movw %bx, %gs
+
+ /* ljmp */
+ .byte 0xea
+ .word 0
+linux_setup_seg:
+ .word 0
+ .code32
+
+
+/*
+ * This starts the multiboot kernel.
+ */
+
+FUNCTION(grub_multiboot_real_boot)
+ /* Push the entry address on the stack. */
+ /* Move the address of the multiboot information structure to
ebx. */
+ movl 8(%esp),%ebx
+
+ /* Unload all modules and stop the floppy driver. */
+ call EXT_C(grub_dl_unload_all)
+ call EXT_C(grub_stop_floppy)
+
+ /* Interrupts should be disabled. */
+ cli
+
+ /* Move the magic value into eax and jump to the kernel. */
+ movl $GRUB_MB_MAGIC2,%eax
+ movl 4(%esp),%ecx
+ jmp *%ecx
+
+
+/*
+ * int grub_biosdisk_rw_int13_extensions (int ah, int drive, void
*dap)
+ *

+ *   Call IBM/MS INT13 Extensions (int 13 %ah=AH) for DRIVE. DAP
+ *   is passed for disk address packet. If an error occurs, return
+ *   non-zero, otherwise zero.

+ */
+
+FUNCTION(grub_biosdisk_rw_int13_extensions)
+ movw 0x4(%esp), %ax
+ movw 0x8(%esp), %dx
+ mov 0xC(%esp), %ecx
+ pushl %ebp
+ pushl %esi
+
+ /* compute the address of disk_address_packet */
+ movw %cx, %si
+ xorw %cx, %cx
+ shrl $4, %ecx /* save the segment to cx */
+
+ /* ah */
+ movb %al, %dh
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb %dh, %ah
+ movw %cx, %ds
+ int $0x13 /* do the operation */
+ movb %ah, %dl /* save return value */
+ /* clear the data segment */
+ xorw %ax, %ax
+ movw %ax, %ds
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %dl, %al /* return value in %eax */
+
+ popl %esi
+ popl %ebp
+
+ ret
+
+/*
+ * int grub_biosdisk_rw_standard (int ah, int drive, int coff, int
hoff,
+ * int soff, int nsec, int segment)
+ *

+ *   Call standard and old INT13 (int 13 %ah=AH) for DRIVE. Read/write
+ *   NSEC sectors from COFF/HOFF/SOFF into SEGMENT. If an error occurs,
+ *   return non-zero, otherwise zero.

+ */
+
+FUNCTION(grub_biosdisk_rw_standard)
+ pushl %ebp
+ movl %esp, %ebp
+
+ pushl %ebx
+ pushl %edi
+ pushl %esi
+
+ /* set up CHS information */
+ movw 0x8(%ebp), %ax
+ movw 0xC(%ebp), %dx
+ movw 0x10(%ebp), %cx
+
+ /* set %ch to low eight bits of cylinder */
+ xchgb %cl, %ch
+ /* set bits 6-7 of %cl to high two bits of cylinder */
+ shlb $6, %cl
+ /* set bits 0-5 of %cl to sector */
+ addb 0x18(%ebp), %cl
+ /* set %dh to head */
+ movb 0x14(%ebp), %dh
+ /* set %ah to AH */
+ movb %al, %ah
+ /* set %al to NSEC */
+ movb 0x1c(%ebp), %al
+ /* save %ax in %di */
+ movw %ax, %di
+ /* save SEGMENT in %bx */
+ movw 0x20(%ebp), %bx
+
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movw %bx, %es
+ xorw %bx, %bx
+ movw $3, %si /* attempt at least three times */
+
+1:
+ movw %di, %ax
+ int $0x13 /* do the operation */
+ jnc 2f /* check if successful */
+
+ movb %ah, %bl /* save return value */
+ /* if fail, reset the disk system */
+ xorw %ax, %ax
+ int $0x13
+
+ decw %si
+ cmpw $0, %si
+ je 2f
+ xorb %bl, %bl
+ jmp 1b /* retry */
+2:
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %bl, %al /* return value in %eax */
+
+ popl %esi
+ popl %edi
+ popl %ebx
+ popl %ebp
+
+ ret $(4 * 4)
+
+
+/*
+ * int grub_biosdisk_check_int13_extensions (int drive)
+ *
+ * Check if LBA is supported for DRIVE. If it is supported, then
return
+ * the major version of extensions, otherwise zero.
+ */
+ +FUNCTION(grub_biosdisk_check_int13_extensions)
+ movw 0x4(%esp), %ax
+ pushl %ebp
+ pushl %ebx
+
+ /* drive */
+ movb %al, %dl
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb $0x41, %ah
+ movw $0x55aa, %bx
+ int $0x13 /* do the operation */
+
+ /* check the result */
+ jc 1f
+ cmpw $0xaa55, %bx
+ jne 1f
+
+ movb %ah, %bl /* save the major version into %bl */
+
+ /* check if AH=0x42 is supported */
+ andw $1, %cx
+ jnz 2f
+
+1:
+ xorb %bl, %bl
+2:
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %bl, %al /* return value in %eax */
+
+ popl %ebx
+ popl %ebp
+
+ ret
+
+
+/*
+ * int grub_biosdisk_get_diskinfo_int13_extensions (int drive, void
*drp)
+ *
+ * Return the geometry of DRIVE in a drive parameters, DRP. If an
error
+ * occurs, then return non-zero, otherwise zero.
+ */
+ +FUNCTION(grub_biosdisk_get_diskinfo_int13_extensions)
+ movw 0x4(%esp), %ax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+ pushl %esi
+
+ /* compute the address of drive parameters */
+ movw %dx, %si
+ xorw %dx, %dx
+ shrl $4, %edx
+ movw %dx, %bx /* save the segment into %bx */
+ /* drive */
+ movb %al, %dl
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb $0x48, %ah
+ movw %bx, %ds
+ int $0x13 /* do the operation */
+ movb %ah, %bl /* save return value in %bl */
+ /* clear the data segment */
+ xorw %ax, %ax
+ movw %ax, %ds
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %bl, %al /* return value in %eax */
+
+ popl %esi
+ popl %ebx
+ popl %ebp
+
+ ret
+
+
+/*
+ * int grub_biosdisk_get_diskinfo_standard (int drive,
+ * unsigned long *cylinders,

+ * unsigned long *heads,
+ * unsigned long *sectors)
+ *
+ * Return the geometry of DRIVE in CYLINDERS, HEADS and SECTORS. If
an
+ * error occurs, then return non-zero, otherwise zero.
+ */
+
+FUNCTION(grub_biosdisk_get_diskinfo_standard)
+ movw 0x4(%esp), %ax
+ mov 0x8(%esp), %edx
+ mov 0xC(%esp), %ecx
+ pushl %ebp
+ pushl %ebx
+ pushl %edi
+
+ /* push CYLINDERS */
+ pushl %edx
+ /* push HEADS */
+ pushl %ecx
+ /* SECTORS is on the stack */
+
+ /* drive */
+ movb %al, %dl
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb $0x8, %ah
+ int $0x13 /* do the operation */
+ /* check if successful */
+ testb %ah, %ah
+ jnz 1f
+ /* bogus BIOSes may not return an error number */
+ testb $0x3f, %cl /* 0 sectors means no disk */
+ jnz 1f /* if non-zero, then succeed */
+ /* XXX 0x60 is one of the unused error numbers */
+ movb $0x60, %ah
+1:
+ movb %ah, %bl /* save return value in %bl */
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ /* pop HEADS */
+ popl %edi
+ movb %dh, %al
+ incl %eax /* the number of heads is counted from zero */
+ movl %eax, (%edi)
+
+ /* pop CYLINDERS */
+ popl %edi
+ movb %ch, %al
+ movb %cl, %ah
+ shrb $6, %ah /* the number of cylinders is counted from zero
*/
+ incl %eax
+ movl %eax, (%edi)
+
+ /* SECTORS */
+ movl 0x1c(%esp), %edi
+ andb $0x3f, %cl
+ movzbl %cl, %eax
+ movl %eax, (%edi)
+
+ xorl %eax, %eax
+ movb %bl, %al /* return value in %eax */
+
+ popl %edi
+ popl %ebx
+ popl %ebp
+
+ ret $4
+
+
+/*
+ * int grub_biosdisk_get_num_floppies (void)
+ */
+FUNCTION(grub_biosdisk_get_num_floppies)
+ pushl %ebp
+
+ xorl %edx, %edx
+ call prot_to_real
+
+ .code16
+ /* reset the disk system first */
+ int $0x13
+1:
+ stc
+
+ /* call GET DISK TYPE */
+ movb $0x15, %ah
+ int $0x13
+
+ jc 2f
+
+ /* check if this drive exists */
+ testb $0x3, %ah
+ jz 2f
+
+ incb %dl
+ cmpb $2, %dl
+ jne 1b
+2:
+ DATA32 call real_to_prot
+ .code32
+
+ movl %edx, %eax
+ popl %ebp
+ ret
+
+
+/*
+ *
+ * grub_get_memsize(i) : return the memory size in KB. i == 0 for conventional
+ * memory, i == 1 for extended memory
+ * BIOS call "INT 12H" to get conventional memory size
+ * BIOS call "INT 15H, AH=88H" to get extended memory size
+ * Both have the return value in AX.
+ *
+ */
+
+FUNCTION(grub_get_memsize)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+
+ movl %eax, %edx
+
+ call prot_to_real /* enter real mode */
+ .code16
+
+ testl %edx, %edx
+ jnz xext
+
+ int $0x12
+ jmp xdone
+
+xext:
+ movb $0x88, %ah
+ int $0x15
+
+xdone:
+ movw %ax, %dx
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+
+ popl %ebp
+ ret
+
+
+/*
+ *
+ * grub_get_eisa_mmap() : return packed EISA memory map, lower 16 bits is
+ * memory between 1M and 16M in 1K parts, upper 16 bits is
+ * memory above 16M in 64K parts. If error, return zero.
+ * BIOS call "INT 15H, AH=E801H" to get EISA memory map,
+ * AX = memory between 1M and 16M in 1K parts.
+ * BX = memory above 16M in 64K parts.
+ *
+ */
+
+FUNCTION(grub_get_eisa_mmap)
+ pushl %ebp
+ pushl %ebx
+
+ call prot_to_real /* enter real mode */
+ .code16
+
+ movw $0xe801, %ax
+ int $0x15
+
+ shll $16, %ebx
+ movw %ax, %bx
+
+ DATA32 call real_to_prot
+ .code32
+
+ cmpb $0x86, %bh
+ je xnoteisa
+
+ movl %ebx, %eax
+
+xnoteisa:
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ *
+ * grub_get_mmap_entry(addr, cont) : address and old continuation value (zero to
+ * start), for the Query System Address Map BIOS call.
+ *
+ * Sets the first 4-byte int value of "addr" to the size returned by
+ * the call. If the call fails, sets it to zero.
+ *
+ * Returns: new (non-zero) continuation value, 0 if done.
+ */
+
+FUNCTION(grub_get_mmap_entry)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+ pushl %edi
+ pushl %esi
+
+ /* push ADDR */
+ pushl %eax
+
+ /* place address (+4) in ES:DI */
+ addl $4, %eax
+ movl %eax, %edi
+ andl $0xf, %edi
+ shrl $4, %eax
+ movl %eax, %esi
+
+ /* set continuation value */
+ movl %edx, %ebx
+
+ /* set default maximum buffer size */
+ movl $0x14, %ecx
+
+ /* set EDX to 'SMAP' */
+ movl $0x534d4150, %edx
+
+ call prot_to_real /* enter real mode */
+ .code16
+
+ movw %si, %es
+ movl $0xe820, %eax
+ int $0x15
+
+ DATA32 jc xnosmap
+
+ cmpl $0x534d4150, %eax
+ jne xnosmap
+
+ cmpl $0x14, %ecx
+ jl xnosmap
+
+ cmpl $0x400, %ecx
+ jg xnosmap
+
+ jmp xsmap
+
+xnosmap:
+ xorl %ecx, %ecx
+
+xsmap:
+ DATA32 call real_to_prot
+ .code32
+
+ /* write length of buffer (zero if error) into ADDR */
+ popl %eax
+ movl %ecx, (%eax)
+
+ /* set return value to continuation */
+ movl %ebx, %eax
+
+ popl %esi
+ popl %edi
+ popl %ebx
+ popl %ebp
+ ret
+
+
+/*
+ * void grub_console_real_putchar (int c)
+ *
+ * Put the character C on the console. Because GRUB wants to write a
+ * character with an attribute, this implementation is a bit tricky.
+ * If C is a control character (CR, LF, BEL, BS), use INT 10, AH = 0Eh
+ * (TELETYPE OUTPUT). Otherwise, save the original position, put a space,
+ * save the current position, restore the original position, write the
+ * character and the attribute, and restore the current position.
+ *
+ * The reason why this is so complicated is that there is no easy way to
+ * get the height of the screen, and the TELETYPE OUPUT BIOS call doesn't
+ * support setting a background attribute.
+ */
+FUNCTION(grub_console_real_putchar)
+ mov 0x4(%esp), %eax
+ movl %eax, %edx
+ pusha
+ movb EXT_C(grub_console_cur_color), %bl
+
+ call prot_to_real
+ .code16
+ movb %dl, %al
+ xorb %bh, %bh
+
+ /* use teletype output if control character */
+ cmpb $0x7, %al
+ je 1f
+ cmpb $0x8, %al
+ je 1f
+ cmpb $0xa, %al
+ je 1f
+ cmpb $0xd, %al
+ je 1f
+
+ /* save the character and the attribute on the stack */
+ pushw %ax
+ pushw %bx
+
+ /* get the current position */
+ movb $0x3, %ah
+ int $0x10
+
+ /* check the column with the width */
+ cmpb $79, %dl
+ jl 2f
+
+ /* print CR and LF, if next write will exceed the width */
+ movw $0x0e0d, %ax
+ int $0x10
+ movb $0x0a, %al
+ int $0x10
+
+ /* get the current position */
+ movb $0x3, %ah
+ int $0x10
+
+2:
+ /* restore the character and the attribute */
+ popw %bx
+ popw %ax
+
+ /* write the character with the attribute */
+ movb $0x9, %ah
+ movw $1, %cx
+ int $0x10
+
+ /* move the cursor forward */
+ incb %dl
+ movb $0x2, %ah
+ int $0x10
+
+ jmp 3f
+
+1: movw $1, %bx
+ movb $0xe, %ah
+ int $0x10
+
+3: DATA32 call real_to_prot
+ .code32
+
+ popa
+ ret
+
+
+/*
+ * int grub_console_getkey (void)
+ * BIOS call "INT 16H Function 00H" to read character from keyboard
+ * Call with %ah = 0x0
+ * Return: %ah = keyboard scan code
+ * %al = ASCII character
+ */
+
+/* this table is used in translate_keycode below */ +translation_table:
+ .word GRUB_CONSOLE_KEY_LEFT, 2
+ .word GRUB_CONSOLE_KEY_RIGHT, 6
+ .word GRUB_CONSOLE_KEY_UP, 16
+ .word GRUB_CONSOLE_KEY_DOWN, 14
+ .word GRUB_CONSOLE_KEY_HOME, 1
+ .word GRUB_CONSOLE_KEY_END, 5
+ .word GRUB_CONSOLE_KEY_DC, 4
+ .word GRUB_CONSOLE_KEY_BACKSPACE, 8
+ .word GRUB_CONSOLE_KEY_PPAGE, 7
+ .word GRUB_CONSOLE_KEY_NPAGE, 3
+ .word 0
+
+/*
+ * translate_keycode translates the key code %dx to an ascii code.
+ */
+ .code16
+
+translate_keycode:
+ pushw %bx
+ pushw %si
+
+ movw $ABS(translation_table), %si
+
+1: lodsw
+ /* check if this is the end */
+ testw %ax, %ax
+ jz 2f
+ /* load the ascii code into %ax */
+ movw %ax, %bx
+ lodsw
+ /* check if this matches the key code */
+ cmpw %bx, %dx
+ jne 1b
+ /* translate %dx, if successful */
+ movw %ax, %dx
+
+2: popw %si
+ popw %bx
+ ret
+
+ .code32
+
+FUNCTION(grub_console_getkey)
+ pushl %ebp
+
+ call prot_to_real
+ .code16
+
+ int $0x16
+
+ movw %ax, %dx /* real_to_prot uses %eax */
+ call translate_keycode
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+
+ popl %ebp
+ ret
+
+
+/*
+ * int grub_console_checkkey (void)
+ * if there is a character pending, return it; otherwise return -1
+ * BIOS call "INT 16H Function 01H" to check whether a character is pending
+ * Call with %ah = 0x1
+ * Return:
+ * If key waiting to be input:
+ * %ah = keyboard scan code
+ * %al = ASCII character
+ * Zero flag = clear
+ * else
+ * Zero flag = set
+ */
+FUNCTION(grub_console_checkkey)
+ pushl %ebp
+ xorl %edx, %edx
+
+ call prot_to_real /* enter real mode */
+ .code16
+
+ movb $0x1, %ah
+ int $0x16
+
+ jz notpending
+
+ movw %ax, %dx
+ DATA32 jmp pending
+
+notpending:
+ decl %edx
+
+pending:
+ DATA32 call real_to_prot
+ .code32
+
+ movl %edx, %eax
+
+ popl %ebp
+ ret
+
+
+/*
+ * grub_uint16_t grub_console_getxy (void)
+ * BIOS call "INT 10H Function 03h" to get cursor position
+ * Call with %ah = 0x03
+ * %bh = page

+ *      Returns         %ch = starting scan line
+ *                      %cl = ending scan line
+ *                      %dh = row (0 is top)
+ *                      %dl = column (0 is left)

+ */
+
+
+FUNCTION(grub_console_getxy)
+ pushl %ebp
+ pushl %ebx /* save EBX */
+
+ call prot_to_real
+ .code16
+
+ xorb %bh, %bh /* set page to 0 */
+ movb $0x3, %ah
+ int $0x10 /* get cursor position */
+
+ DATA32 call real_to_prot
+ .code32
+
+ movb %dl, %ah
+ movb %dh, %al
+
+ popl %ebx
+ popl %ebp
+ ret
+
+
+/*
+ * void grub_console_gotoxy(grub_uint8_t x, grub_uint8_t y)
+ * BIOS call "INT 10H Function 02h" to set cursor position
+ * Call with %ah = 0x02
+ * %bh = page
+ * %dh = row (0 is top)
+ * %dl = column (0 is left)
+ */
+
+
+FUNCTION(grub_console_gotoxy)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx /* save EBX */
+
+ movb %dl, %dh /* %dh = y */
+ movb %al, %dl /* %dl = x */
+
+ call prot_to_real
+ .code16
+
+ xorb %bh, %bh /* set page to 0 */
+ movb $0x2, %ah
+ int $0x10 /* set cursor position */
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %ebx
+ popl %ebp
+ ret
+
+
+/*
+ * void grub_console_cls (void)
+ * BIOS call "INT 10H Function 09h" to write character and attribute
+ * Call with %ah = 0x09

+ *                      %al = (character)
+ *                      %bh = (page number)
+ *                      %bl = (attribute)
+ *                      %cx = (number of times)

+ */
+
+FUNCTION(grub_console_cls)
+ pushl %ebp
+ pushl %ebx /* save EBX */
+
+ call prot_to_real
+ .code16
+
+ /* move the cursor to the beginning */
+ movb $0x02, %ah
+ xorb %bh, %bh
+ xorw %dx, %dx
+ int $0x10
+
+ /* write spaces to the entire screen */
+ movw $0x0920, %ax
+ movw $0x07, %bx
+ movw $(80 * 25), %cx
+ int $0x10
+
+ /* move back the cursor */
+ movb $0x02, %ah
+ int $0x10
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %ebx
+ popl %ebp
+ ret
+
+
+/*
+ * void grub_console_setcursor (int on)
+ * BIOS call "INT 10H Function 01h" to set cursor type

+ *      Call with       %ah = 0x01
+ *                      %ch = cursor starting scanline
+ *                      %cl = cursor ending scanline

+ */
+
+console_cursor_state:
+ .byte 1
+console_cursor_shape:
+ .word 0
+
+FUNCTION(grub_console_setcursor)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+ pushl %ebx
+
+ /* push ON */
+ pushl %eax
+
+ /* check if the standard cursor shape has already been saved */
+ movw console_cursor_shape, %ax
+ testw %ax, %ax
+ jne 1f
+
+ call prot_to_real
+ .code16
+
+ movb $0x03, %ah
+ xorb %bh, %bh
+ int $0x10
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %cx, console_cursor_shape
+1:
+ /* set %cx to the designated cursor shape */
+ movw $0x2000, %cx
+ popl %eax
+ testl %eax, %eax
+ jz 2f
+ movw console_cursor_shape, %cx
+2:
+ call prot_to_real
+ .code16
+
+ movb $0x1, %ah
+ int $0x10
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_getrtsecs()
+ * if a seconds value can be read, read it and return it (BCD),
+ * otherwise return 0xFF
+ * BIOS call "INT 1AH Function 02H" to check whether a character is pending
+ * Call with %ah = 0x2
+ * Return:
+ * If RT Clock can give correct values
+ * %ch = hour (BCD)
+ * %cl = minutes (BCD)
+ * %dh = seconds (BCD)
+ * %dl = daylight savings time (00h std, 01h
daylight)
+ * Carry flag = clear
+ * else
+ * Carry flag = set
+ * (this indicates that the clock is updating,
or
+ * that it isn't running)
+ */
+FUNCTION(grub_getrtsecs)
+ pushl %ebp
+
+ call prot_to_real /* enter real mode */
+ .code16
+
+ clc
+ movb $0x2, %ah
+ int $0x1a
+
+ DATA32 jnc gottime
+ movb $0xff, %dh
+
+gottime:
+ DATA32 call real_to_prot
+ .code32
+
+ movb %dh, %al
+
+ popl %ebp
+ ret
+
+
+/*
+ * grub_get_rtc()
+ * return the real time in ticks, of which there are about
+ * 18-20 per second
+ */
+FUNCTION(grub_get_rtc)
+ pushl %ebp
+
+ call prot_to_real /* enter real mode */
+ .code16
+
+ /* %ax is already zero */
+ int $0x1a
+
+ DATA32 call real_to_prot
+ .code32
+
+ movl %ecx, %eax
+ shll $16, %eax
+ movw %dx, %ax
+
+ popl %ebp
+ ret
+
+
+/*
+ * unsigned char grub_vga_set_mode (unsigned char mode)
+ */
+FUNCTION(grub_vga_set_mode)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+ pushl %ebx
+ movl %eax, %ecx
+
+ call prot_to_real
+ .code16
+ /* get current mode */
+ xorw %bx, %bx
+ movb $0x0f, %ah
+ int $0x10
+ movb %al, %dl
+
+ /* set the new mode */
+ movb %cl, %al
+ xorb %ah, %ah
+ int $0x10
+
+ DATA32 call real_to_prot
+ .code32
+
+ movb %dl, %al
+ popl %ebx
+ popl %ebp
+ ret
+
+
+/*
+ * unsigned char *grub_vga_get_font (void)
+ */
+FUNCTION(grub_vga_get_font)
+ pushl %ebp
+ pushl %ebx
+
+ call prot_to_real
+ .code16
+ movw $0x1130, %ax
+ movb $0x06, %bh
+ int $0x10
+ movw %es, %bx
+ movw %bp, %dx
+ DATA32 call real_to_prot
+ .code32
+
+ movzwl %bx, %ecx
+ shll $4, %ecx
+ movw %dx, %ax
+ addl %ecx, %eax
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_get_controller_info (struct grub_vbe_info_block *controller_info)
+ *
+ * Register allocations for parameters:
+ * %eax *controller_info
+ */
+FUNCTION(grub_vbe_get_controller_info)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+ pushl %edi
+
+ movw %ax, %di /* Store *controller_info to %edx:%di.
*/
+ xorw %ax, %ax
+ shrl $4, %eax
+ mov %eax, %edx /* prot_to_real destroys %eax. */
+
+ call prot_to_real
+ .code16
+
+ pushw %es
+
+ movw %dx, %es /* *controller_info is now on %es:%di.
*/
+ movw $0x4f00, %ax
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ popw %es
+
+ DATA32 call real_to_prot
+ .code32
+
+ movl %edx, %eax
+ andl $0x0FFFF, %eax /* Return value in %eax. */
+
+ popl %edi
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_get_mode_info (grub_uint32_t mode,
+ * struct
grub_vbe_mode_info_block *mode_info)
+ *
+ * Register allocations for parameters:
+ * %eax mode
+ * %edx *mode_info
+ */
+FUNCTION(grub_vbe_get_mode_info)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %edi
+
+ movl %eax, %ecx /* Store mode number to %ecx. */
+

+	movw    %dx, %di	/* Store *mode_info to %edx:%di.  */
+	xorw    %dx, %dx
+	shrl    $4, %edx

+
+ call prot_to_real
+ .code16
+
+ pushw %es
+

+	movw    %dx, %es	/* *mode_info is now on %es:%di.  */
+	movw    $0x4f01, %ax
+	int     $0x10

+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ popw %es
+
+ DATA32 call real_to_prot
+ .code32
+
+ movl %edx, %eax
+ andl $0x0FFFF, %eax /* Return value in %eax. */
+
+ popl %edi
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_set_mode (grub_uint32_t mode,
+ * struct grub_vbe_crtc_info_block
*crtc_info)
+ *
+ * Register allocations for parameters:
+ * %eax mode
+ * %edx *crtc_info
+ */
+FUNCTION(grub_vbe_set_mode)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+
+ movl %eax, %ebx /* Store mode in %ebx. */
+

+	movw    %dx, %di	/* Store *crtc_info to %edx:%di.  */
+	xorw    %dx, %dx
+	shrl    $4, %edx

+
+ call prot_to_real
+ .code16
+
+ pushw %es
+
+ movw %dx, %es /* *crtc_info is now on %es:%di. */
+
+ movw $0x4f02, %ax
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ popw %es
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_get_mode (grub_uint32_t *mode)
+ *
+ * Register allocations for parameters:
+ * %eax *mode
+ */
+FUNCTION(grub_vbe_get_mode)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+ pushl %ebx
+ pushl %eax /* Push *mode to stack. */
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f03, %ax
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %edi /* Pops *mode from stack to %edi. */
+ andl $0xFFFF, %ebx
+ movl %ebx, (%edi)
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_set_memory_window (grub_uint32_t window,
+ * grub_uint32_t
position);
+ *
+ * Register allocations for parameters:
+ * %eax window
+ * %edx position
+ */
+FUNCTION(grub_vbe_set_memory_window)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+
+ movl %eax, %ebx
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f05, %ax
+ andw $0x00ff, %bx /* BL = window, BH = 0, Set memory
window. */
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_get_memory_window (grub_uint32_t window,
+ * grub_uint32_t
*position);
+ *
+ * Register allocations for parameters:
+ * %eax window
+ * %edx *position
+ */
+FUNCTION(grub_vbe_get_memory_window)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+ pushl %edx /* Push *position to stack. */
+
+ movl %eax, %ebx /* Store window in %ebx. */
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f05, %ax
+ andw $0x00ff, %bx /* BL = window. */
+ orw $0x0100, %bx /* BH = 1, Get memory window. */
+ int $0x10
+
+ movw %ax, %bx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %edi /* pops *position from stack to %edi.
*/
+ andl $0xFFFF, %edx
+ movl %edx, (%edi) /* Return position to caller. */
+
+ movw %bx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_set_scanline_length (grub_uint32_t length)
+ *
+ * Register allocations for parameters:
+ * %eax length
+ */
+FUNCTION(grub_vbe_set_scanline_length)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+ pushl %ebx
+
+ movl %eax, %ecx /* Store length in %ecx. */
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f06, %ax
+ movw $0x0002, %bx /* BL = 2, Set Scan Line in Bytes. */
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_get_scanline_length (grub_uint32_t *length)
+ *
+ * Register allocations for parameters:
+ * %eax *length
+ */
+FUNCTION(grub_vbe_get_scanline_length)
+ mov 0x4(%esp), %eax
+ pushl %ebp
+ pushl %ebx
+ pushl %edx /* Push *length to stack. */
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f06, %ax
+ movw $0x0001, %bx /* BL = 1, Get Scan Line Length (in
bytes). */
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %edi /* Pops *length from stack to %edi. */
+ andl $0xFFFF, %ebx
+ movl %ebx, (%edi) /* Return length to caller. */
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_set_display_start (grub_uint32_t x,
+ * grub_uint32_t y)
+ *
+ * Register allocations for parameters:
+ * %eax x
+ * %edx y
+ */
+FUNCTION(grub_vbe_set_display_start)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+
+ movl %eax, %ecx /* Store x in %ecx. */
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f07, %ax
+ movw $0x0080, %bx /* BL = 80h, Set Display Start
+ during Vertical Retrace. */
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_get_display_start (grub_uint32_t *x,
+ * grub_uint32_t *y)
+ *
+ * Register allocations for parameters:
+ * %eax *x
+ * %edx *y
+ */
+FUNCTION(grub_vbe_get_display_start)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ pushl %ebp
+ pushl %ebx
+ pushl %eax /* Push *x to stack. */
+ pushl %edx /* Push *y to stack. */
+
+ call prot_to_real
+ .code16
+
+ movw $0x4f07, %ax
+ movw $0x0001, %bx /* BL = 1, Get Display Start. */
+ int $0x10
+
+ movw %ax, %bx /* real_to_prot destroys %eax. */
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %edi /* Pops *y from stack to %edi. */
+ andl $0xFFFF, %edx
+ movl %edx, (%edi) /* Return y-position to caller. */
+
+ popl %edi /* Pops *x from stack to %edi. */
+ andl $0xFFFF, %ecx
+ movl %ecx, (%edi) /* Return x-position to caller. */
+
+ movw %bx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret
+
+/*
+ * grub_vbe_status_t grub_vbe_set_palette_data (grub_uint32_t color_count,
+ * grub_uint32_t
start_index,
+ * struct
grub_vbe_palette_data *palette_data)
+ *
+ * Register allocations for parameters:
+ * %eax color_count
+ * %edx start_index
+ * %ecx *palette_data
+ */
+FUNCTION(grub_vbe_set_palette_data)
+ mov 0x4(%esp), %eax
+ mov 0x8(%esp), %edx
+ mov 0xC(%esp), %ecx
+ pushl %ebp
+ pushl %ebx
+
+ movl %eax, %ebx /* Store color_count in %ebx. */
+

+	movw    %cx, %di	/* Store *palette_data to %ecx:%di.  */
+	xorw    %cx, %cx
+	shrl    $4, %ecx

+
+ call prot_to_real
+ .code16
+
+ pushw %es
+
+ movw %cx, %es /* *palette_data is now on %es:%di. */
+ movw %bx, %cx /* color_count is now on %cx. */
+
+ movw $0x4f09, %ax
+ xorw %bx, %bx /* BL = 0, Set Palette Data. */
+ int $0x10
+
+ movw %ax, %dx /* real_to_prot destroys %eax. */
+
+ popw %es
+
+ DATA32 call real_to_prot
+ .code32
+
+ movw %dx, %ax
+ andl $0xFFFF, %eax /* Return value in %eax. */
+
+ popl %ebx
+ popl %ebp
+ ret

FILE kern/i386/pc/startup.S.regparm (NEW)

(1) The old startup.S file. Only renamed.

diff -EbwBruN ./kern/i386/pc/startup.S.regparm ../../src/grub-1.93/kern/i386/pc/startup.S.regparm
--- ./kern/i386/pc/startup.S.regparm 1970-01-01 01:00:00.000000000
+0100
+++ ../../src/grub-1.93/kern/i386/pc/startup.S.regparm 2006-05-03
22:39:20.000000000 +0200
@@ -0,0 +1,2170 @@
+/*
+ * GRUB -- GRand Unified Bootloader
+ * Copyright (C) 1999,2000,2001,2002,2003,2005 Free Software Foundation, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *

+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

+ */
+
+
+/*
+ * Note: These functions defined in this file may be called from C.
+ * Be careful of that you must not modify some registers. Quote
+ * from gcc-2.95.2/gcc/config/i386/i386.h:
+

+   1 for registers not available across function calls.
+   These must include the FIXED_REGISTERS and also any
+   registers that can be used without being saved.
+   The latter must include the registers where values are returned
+   and the register where structure-value addresses are passed.
+   Aside from that, you can include as many other registers as you

like.
+
+ ax,dx,cx,bx,si,di,bp,sp,st,st1,st2,st3,st4,st5,st6,st7,arg +{ 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
+ */
+
+/*
+ * Note: GRUB is compiled with the options -mrtd and -mregparm=3.
+ * So the first three arguments are passed in %eax, %edx, and
%ecx,
+ * respectively, and if a function has a fixed number of
arguments
+ * and the number if greater than three, the function must return
+ * with "ret $N" where N is ((the number of arguments) - 3) * 4.
+ */
+
+#include <config.h>
+#include <grub/symbol.h>
+#include <grub/boot.h>
+#include <grub/machine/boot.h>
+#include <grub/machine/memory.h>
+#include <grub/machine/console.h>
+#include <grub/machine/linux.h>
+#include <grub/machine/kernel.h>
+#include <grub/machine/multiboot.h>
+
+#define ABS(x) ((x) - EXT_C(start) + GRUB_BOOT_MACHINE_KERNEL_ADDR +
0x200)
+
+ .file "startup.S"
+
+ .text
+
+ /* Tell GAS to generate 16-bit instructions so that this code
works
+ in real mode. */
+ .code16
+
+ .globl start, _start
+start:
+_start:
+ /*
+ * Guarantee that "main" is loaded at 0x0:0x8200.
+ */
+ ljmp $0, $ABS(codestart)
+
+ /*
+ * Compatibility version number
+ *
+ * These MUST be at byte offset 6 and 7 of the executable
+ * DO NOT MOVE !!!
+ */
+ . = EXT_C(start) + 0x6
+ .byte GRUB_BOOT_VERSION_MAJOR, GRUB_BOOT_VERSION_MINOR
+
+ /*
+ * This is a special data area 8 bytes from the beginning.
+ */
+
+ . = EXT_C(start) + 0x8
+
+VARIABLE(grub_total_module_size)
+ .long 0
+VARIABLE(grub_kernel_image_size)
+ .long 0
+VARIABLE(grub_compressed_size)
+ .long 0
+VARIABLE(grub_install_dos_part)
+ .long 0xFFFFFFFF
+VARIABLE(grub_install_bsd_part)
+ .long 0xFFFFFFFF
+VARIABLE(grub_prefix)
+ .string "/boot/grub"
+
+ /*
+ * Leave some breathing room for the prefix.
+ */
+
+ . = EXT_C(start) + 0x50
+
+/*
+ * Support for booting GRUB from a Multiboot boot loader (e.g. GRUB itself).
+ * This uses the a.out kludge to load raw binary to the area starting at 1MB,
+ * and relocates itself after loaded.
+ */
+multiboot_header:
+ /* magic */
+ .long 0x1BADB002
+ /* flags */
+ .long (1 << 16)
+ /* checksum */
+ .long -0x1BADB002 - (1 << 16)
+ /* header addr */
+ .long multiboot_header - _start + 0x100000 + 0x200
+ /* load addr */
+ .long 0x100000
+ /* load end addr */
+ .long 0
+ /* bss end addr */
+ .long 0
+ /* entry addr */
+ .long multiboot_entry - _start + 0x100000 + 0x200
+
+multiboot_entry:
+ .code32
+ /* obtain the boot device */
+ movl 12(%ebx), %edx
+
+ /* relocate the code */
+ movl $(GRUB_KERNEL_MACHINE_RAW_SIZE + 0x200), %ecx
+ addl EXT_C(grub_compressed_size) - _start + 0x100000 + 0x200,
%ecx
+ movl $0x100000, %esi
+ movl $GRUB_BOOT_MACHINE_KERNEL_ADDR, %edi
+ cld
+ rep
+ movsb
+ /* jump to the real address */
+ movl $multiboot_trampoline, %eax
+ jmp *%eax
+
+multiboot_trampoline:
+ /* fill the boot information */
+ movl %edx, %eax
+ shrl $8, %eax
+ xorl %ebx, %ebx
+ cmpb $0xFF, %ah
+ je 1f
+ movb %ah, %bl
+ movl %ebx, EXT_C(grub_install_dos_part)
+1:
+ cmpb $0xFF, %al
+ je 2f
+ movb %al, %bl
+ movl %ebx, EXT_C(grub_install_bsd_part)
+2:
+ shrl $24, %edx
+ /* enter the usual booting */
+ call prot_to_real
+ .code16
+
+/* the real mode code continues... */
+codestart:
+ cli /* we're not safe here! */
+
+ /* set up %ds, %ss, and %es */
+ xorw %ax, %ax
+ movw %ax, %ds
+ movw %ax, %ss
+ movw %ax, %es
+
+ /* set up the real mode/BIOS stack */
+ movl $GRUB_MEMORY_MACHINE_REAL_STACK, %ebp
+ movl %ebp, %esp
+
+ sti /* we're safe again */
+
+ /* save boot drive reference */
+ ADDR32 movb %dl, EXT_C(grub_boot_drive)
+
+ /* reset disk system (%ah = 0) */
+ int $0x13
+
+ /* transition to protected mode */
+ DATA32 call real_to_prot
+
+ /* The ".code32" directive takes GAS out of 16-bit mode. */
+ .code32
+
+ incl %eax
+ call EXT_C(grub_gate_a20)
+
+ /* decompress the compressed part and put the result at 1MB */
+ movl $0x100000, %esi
+ movl $(START_SYMBOL + GRUB_KERNEL_MACHINE_RAW_SIZE), %edi
+
+ pushl %esi
+ pushl EXT_C(grub_compressed_size)
+ pushl %edi
+ call lzo1x_decompress
+ addl $12, %esp
+
+ /* copy back the decompressed part */
+ movl %eax, %ecx
+ cld
+ rep
+ movsb
+
+ /* copy modules before cleaning out the bss */
+ movl EXT_C(grub_total_module_size), %ecx
+ movl EXT_C(grub_kernel_image_size), %esi
+ addl %ecx, %esi
+ addl $START_SYMBOL, %esi
+ decl %esi
+ movl $END_SYMBOL, %edi
+ addl %ecx, %edi
+ decl %edi
+ std
+ rep
+ movsb
+
+ /* clean out the bss */
+ movl $BSS_START_SYMBOL, %edi
+
+ /* compute the bss length */
+ movl $END_SYMBOL, %ecx
+ subl %edi, %ecx
+
+ /* clean out */
+ xorl %eax, %eax
+ cld
+ rep
+ stosb
+
+ /*
+ * Call the start of main body of C code.
+ */
+ call EXT_C(grub_main)
+
+
+/*
+ * This is the area for all of the special variables.
+ */
+
+ .p2align 2 /* force 4-byte alignment */
+
+protstack:
+ .long GRUB_MEMORY_MACHINE_PROT_STACK
+
+VARIABLE(grub_boot_drive)
+ .long 0
+
+VARIABLE(grub_start_addr)
+ .long START_SYMBOL
+
+VARIABLE(grub_end_addr)
+ .long END_SYMBOL
+
+VARIABLE(grub_apm_bios_info)
+ .word 0 /* version */
+ .word 0 /* cseg */
+ .long 0 /* offset */
+ .word 0 /* cseg_16 */
+ .word 0 /* dseg_16 */
+ .word 0 /* cseg_len */
+ .word 0 /* cseg_16_len */
+ .word 0 /* dseg_16_len */
+
+/*
+ * This is the Global Descriptor Table
+ *
+ * An entry, a "Segment Descriptor", looks like this:
+ *
+ * 31 24 19 16 7 0
+ * ------------------------------------------------------------

+ * |             | |B| |A|       | |   |1|0|E|W|A|            |
+ * | BASE 31..24 |G|/|L|V| LIMIT |P|DPL|  TYPE   | BASE 23:16 |  4
+ * |             | |D| |L| 19..16| |   |1|1|C|R|A|            |
+ * ------------------------------------------------------------
+ * |                             |                            |
+ * |        BASE 15..0           |       LIMIT 15..0          |  0
+ * |                             |                            |

+ * ------------------------------------------------------------
+ *
+ * Note the ordering of the data items is reversed from the above
+ * description.
+ */
+
+ .p2align 2 /* force 4-byte alignment */
+gdt:
+ .word 0, 0
+ .byte 0, 0, 0, 0
+
+ /* -- code segment --
+ * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity),
present
+ * type = 32bit code execute/read, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x9A, 0xCF, 0
+
+ /* -- data segment --
+ * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
+ * type = 32 bit data read/write, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x92, 0xCF, 0
+
+ /* -- 16 bit real mode CS --
+ * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
+ * type = 16 bit code execute/read only/conforming, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x9E, 0, 0
+
+ /* -- 16 bit real mode DS --
+ * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
+ * type = 16 bit data read/write, DPL = 0
+ */
+ .word 0xFFFF, 0
+ .byte 0, 0x92, 0, 0
+
+
+/* this is the GDT descriptor */
+gdtdesc:
+ .word 0x27 /* limit */
+ .long gdt /* addr */
+
+
+/*
+ * These next two routines, "real_to_prot" and "prot_to_real" are structured
+ * in a very specific way. Be very careful when changing them.
+ *
+ * NOTE: Use of either one messes up %eax and %ebp.
+ */
+
+real_to_prot:
+ .code16
+ cli
+
+ /* load the GDT register */
+ DATA32 ADDR32 lgdt gdtdesc
+
+ /* turn on protected mode */
+ movl %cr0, %eax
+ orl $GRUB_MEMORY_MACHINE_CR0_PE_ON, %eax
+ movl %eax, %cr0
+
+ /* jump to relocation, flush prefetch queue, and reload %cs */
+ DATA32 ljmp $GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $protcseg
+
+ .code32
+protcseg:
+ /* reload other segment registers */
+ movw $GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* put the return address in a known safe location */
+ movl (%esp), %eax
+ movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
+
+ /* get protected mode stack */
+ movl protstack, %eax
+ movl %eax, %esp
+ movl %eax, %ebp
+
+ /* get return address onto the right stack */
+ movl GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+ movl %eax, (%esp)
+
+ /* zero %eax */
+ xorl %eax, %eax
+
+ /* return on the old (or initialized) stack! */
+ ret
+
+
+prot_to_real:
+ /* just in case, set GDT */
+ lgdt gdtdesc
+
+ /* save the protected mode stack */
+ movl %esp, %eax
+ movl %eax, protstack
+
+ /* get the return address */
+ movl (%esp), %eax
+ movl %eax, GRUB_MEMORY_MACHINE_REAL_STACK
+
+ /* set up new stack */
+ movl $GRUB_MEMORY_MACHINE_REAL_STACK, %eax
+ movl %eax, %esp
+ movl %eax, %ebp
+
+ /* set up segment limits */
+ movw $GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG, %ax
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* this might be an extra step */
+ /* jump to a 16 bit segment */
+ ljmp $GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG, $tmpcseg
+
+tmpcseg:
+ .code16
+
+ /* clear the PE bit of CR0 */
+ movl %cr0, %eax
+ andl $(~GRUB_MEMORY_MACHINE_CR0_PE_ON), %eax
+ movl %eax, %cr0
+
+ /* flush prefetch queue, reload %cs */
+ DATA32 ljmp $0, $realcseg
+
+realcseg:
+ /* we are in real mode now
+ * set up the real mode segment registers : DS, SS, ES
+ */
+ /* zero %eax */
+ xorl %eax, %eax
+
+ movw %ax, %ds
+ movw %ax, %es
+ movw %ax, %fs
+ movw %ax, %gs
+ movw %ax, %ss
+
+ /* restore interrupts */
+ sti
+
+ /* return on new stack! */
+ DATA32 ret
+
+ .code32
+
+
+/*
+ * grub_gate_a20(int on)
+ *
+ * Gate address-line 20 for high memory.
+ *
+ * This routine is probably overconservative in what it does, but so what?
+ *
+ * It also eats any keystrokes in the keyboard buffer. :-(
+ */
+
+FUNCTION(grub_gate_a20)
+ movl %eax, %edx
+
+gate_a20_test_current_state:
+ /* first of all, test if already in a good state */
+ call gate_a20_check_state
+ cmpb %al, %dl
+ jnz gate_a20_try_bios
+ ret
+
+gate_a20_try_bios:
+ /* second, try a BIOS call */
+ pushl %ebp
+ call prot_to_real
+
+ .code16
+ movw $0x2400, %ax
+ testb %dl, %dl
+ jz 1f
+ incw %ax
+1: int $0x15
+
+ DATA32 call real_to_prot
+ .code32
+
+ popl %ebp
+ call gate_a20_check_state
+ cmpb %al, %dl
+ jnz gate_a20_try_keyboard_controller
+ ret
+
+gate_a20_flush_keyboard_buffer:
+ inb $0x64
+ andb $0x02, %al
+ jnz gate_a20_flush_keyboard_buffer
+2:
+ inb $0x64
+ andb $0x01, %al
+ jz 3f
+ inb $0x60
+ jmp 2b
+3:
+ ret
+
+gate_a20_try_keyboard_controller:
+ /* third, try the keyboard controller */
+ call gate_a20_flush_keyboard_buffer
+
+ movb $0xd1, %al
+ outb $0x64
+4:
+ inb $0x64
+ andb $0x02, %al
+ jnz 4b
+
+ movb $0xdd, %al
+ testb %dl, %dl
+ jz 5f
+ orb $0x02, %al
+5: outb $0x60
+ call gate_a20_flush_keyboard_buffer
+
+ /* output a dummy command (USB keyboard hack) */
+ movb $0xff, %al
+ outb $0x64
+ call gate_a20_flush_keyboard_buffer
+
+ call gate_a20_check_state
+ cmpb %al, %dl
+ jnz gate_a20_try_system_control_port_a
+ ret
+
+gate_a20_try_system_control_port_a:
+ /* fourth, try the system control port A */
+ inb $0x92
+ andb $(~0x03), %al
+ testb %dl, %dl
+ jz 6f
+ orb $0x02, %al
+6: outb $0x92
+
+ /* When turning off Gate A20, do not check the state strictly,
+ because a failure is not fatal usually, and Gate A20 is
always
+ on some modern machines. */
+ testb %dl, %dl
+ jz 7f
+ call gate_a20_check_state
+ cmpb %al, %dl
+ /* everything failed, so restart from the beginning */
+ jnz gate_a20_try_bios
+7: ret
+
+gate_a20_check_state:
+ /* iterate the checking for a while */
+ movl $100, %ecx
+1:
+ call 3f
+ cmpb %al, %dl
+ jz 2f
+ loop 1b
+2:
+ ret
+3:
+ pushl %ebx
+ pushl %ecx
+ xorl %eax, %eax
+ /* compare the byte at 0x8000 with that at 0x108000 */
+ movl $GRUB_BOOT_MACHINE_KERNEL_ADDR, %ebx
+ pushl %ebx
+ /* save the original byte in CL */
+ movb (%ebx), %cl
+ /* store the value at 0x108000 in AL */
+ addl $0x100000, %ebx
+ movb (%ebx), %al
+ /* try to set one less value at 0x8000 */
+ popl %ebx
+ movb %al, %ch
+ decb %ch
+ movb %ch, (%ebx)
+ /* serialize */
+ outb %al, $0x80
+ outb %al, $0x80
+ /* obtain the value at 0x108000 in CH */
+ pushl %ebx
+ addl $0x100000, %ebx
+ movb (%ebx), %ch
+ /* this result is 1 if A20 is on or 0 if it is off */
+ subb %ch, %al
+ xorb $1, %al
+ /* restore the original */
+ popl %ebx
+ movb %cl, (%ebx)
+ popl %ecx
+ popl %ebx
+ ret
+
+#include "lzo1x.S"
+
+
+/*
+ * This call is special... it never returns... in fact it should simply
+ * hang at this point!
+ */
+
+FUNCTION(grub_stop)
+ call prot_to_real
+
+ /*
+ * This next part is sort of evil. It takes advantage of the
+ * byte ordering on the x86 to work in either 16-bit or 32-bit
+ * mode, so think about it before changing it.
+ */
+
+FUNCTION(grub_hard_stop)
+ hlt
+ jmp EXT_C(grub_hard_stop)
+
+
+/*
+ * grub_stop_floppy()
+ *
+ * Stop the floppy drive from spinning, so that other software is
+ * jumped to with a known state.
+ */
+FUNCTION(grub_stop_floppy)
+ movw $0x3F2, %dx
+ xorb %al, %al
+ outb %al, %dx
+ ret
+
+/*
+ * grub_reboot()
+ *
+ * Reboot the system. At the moment, rely on BIOS.
+ */
+FUNCTION(grub_reboot)
+ call prot_to_real
+ .code16
+ /* cold boot */
+ movw $0x0472, %di
+ movw %ax, (%di)
+ ljmp $0xFFFF, $0x0000
+ .code32
+
+/*
+ * grub_halt(int no_apm)
+ *
+ * Halt the system, using APM if possible. If NO_APM is true, don't use
+ * APM even if it is available.
+ */
+FUNCTION(grub_halt)
+ /* see if zero */
+ testl %eax, %eax
+ jnz EXT_C(grub_stop)
+
+ call prot_to_real
+ .code16
+
+ /* detect APM */
+ movw $0x5300, %ax
+ xorw %bx, %bx
+ int $0x15
+ jc EXT_C(grub_hard_stop)
+ /* don't check %bx for buggy BIOSes... */
+
+ /* disconnect APM first */
+ movw $0x5304, %ax
+ xorw %bx, %bx
+ int $0x15
+
+ /* connect APM */
+ movw $0x5301, %ax
+ xorw %bx, %bx
+ int $0x15
+ jc EXT_C(grub_hard_stop)
+
+ /* set APM protocol level - 1.1 or bust. (this covers APM 1.2
also) */
+ movw $0x530E, %ax
+ xorw %bx, %bx
+ movw $0x0101, %cx
+ int $0x15
+ jc EXT_C(grub_hard_stop)
+
+ /* set the power state to off */
+ movw $0x5307, %ax
+ movw $1, %bx
+ movw $3, %cx
+ int $0x15
+
+ /* shouldn't reach here */
+ jmp EXT_C(grub_hard_stop)
+ .code32
+
+
+/*
+ * void grub_chainloader_real_boot (int drive, void *part_addr)
+ *
+ * This starts another boot loader.
+ */
+
+FUNCTION(grub_chainloader_real_boot)
+ pushl %edx
+ pushl %eax
+
+ call EXT_C(grub_dl_unload_all)
+
+ /* set up to pass boot drive */
+ popl %edx
+
+ /* ESI must point to a partition table entry */
+ popl %esi
+
+ /* Turn off Gate A20 */
+ xorl %eax, %eax
+ call EXT_C(grub_gate_a20)
+
+ call prot_to_real
+ .code16
+ ljmp $0, $GRUB_MEMORY_MACHINE_BOOT_LOADER_ADDR
+ .code32
+
+
+/*
+ * void grub_linux_boot_zimage (void)
+ */
+VARIABLE(grub_linux_prot_size)
+ .long 0
+VARIABLE(grub_linux_tmp_addr)
+ .long 0
+VARIABLE(grub_linux_real_addr)
+ .long 0
+
+FUNCTION(grub_linux_boot_zimage)
+ /* copy the kernel */
+ movl EXT_C(grub_linux_prot_size), %ecx
+ addl $3, %ecx
+ shrl $2, %ecx
+ movl $GRUB_LINUX_BZIMAGE_ADDR, %esi
+ movl $GRUB_LINUX_ZIMAGE_ADDR, %edi
+ cld
+ rep
+ movsl
+
+FUNCTION(grub_linux_boot_bzimage)
+ call EXT_C(grub_dl_unload_all)
+
+ movl EXT_C(grub_linux_real_addr), %ebx
+
+ /* copy the real mode code */
+ movl EXT_C(grub_linux_tmp_addr), %esi
+ movl %ebx, %edi
+ movl $GRUB_LINUX_SETUP_MOVE_SIZE, %ecx
+ cld
+ rep
+ movsb
+
+ /* change %ebx to the segment address */
+ shrl $4, %ebx
+ movl %ebx, %eax
+ addl $0x20, %eax
+ movw %ax, linux_setup_seg
+
+ /* XXX new stack pointer in safe area for calling functions */
+ movl $0x4000, %esp
+ call EXT_C(grub_stop_floppy)
+
+ /* final setup for linux boot */
+ call prot_to_real
+ .code16
+
+ cli
+ movw %bx, %ss
+ movw $GRUB_LINUX_SETUP_STACK, %sp
+
+ movw %bx, %ds
+ movw %bx, %es
+ movw %bx, %fs
+ movw %bx, %gs
+
+ /* ljmp */
+ .byte 0xea
+ .word 0
+linux_setup_seg:
+ .word 0
+ .code32
+
+
+/*
+ * This starts the multiboot kernel.
+ */
+
+FUNCTION(grub_multiboot_real_boot)
+ /* Push the entry address on the stack. */
+ pushl %eax
+ /* Move the address of the multiboot information structure to
ebx. */
+ movl %edx,%ebx
+
+ /* Unload all modules and stop the floppy driver. */
+ call EXT_C(grub_dl_unload_all)
+ call EXT_C(grub_stop_floppy)
+
+ /* Interrupts should be disabled. */
+ cli
+
+ /* Move the magic value into eax and jump to the kernel. */
+ movl $GRUB_MB_MAGIC2,%eax
+ popl %ecx
+ jmp *%ecx
+
+
+/*
+ * int grub_biosdisk_rw_int13_extensions (int ah, int drive, void
*dap)
+ *

+ *   Call IBM/MS INT13 Extensions (int 13 %ah=AH) for DRIVE. DAP
+ *   is passed for disk address packet. If an error occurs, return
+ *   non-zero, otherwise zero.

+ */
+
+FUNCTION(grub_biosdisk_rw_int13_extensions)
+ pushl %ebp
+ pushl %esi
+
+ /* compute the address of disk_address_packet */
+ movw %cx, %si
+ xorw %cx, %cx
+ shrl $4, %ecx /* save the segment to cx */
+
+ /* ah */
+ movb %al, %dh
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb %dh, %ah
+ movw %cx, %ds
+ int $0x13 /* do the operation */
+ movb %ah, %dl /* save return value */
+ /* clear the data segment */
+ xorw %ax, %ax
+ movw %ax, %ds
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %dl, %al /* return value in %eax */
+
+ popl %esi
+ popl %ebp
+
+ ret
+
+/*
+ * int grub_biosdisk_rw_standard (int ah, int drive, int coff, int
hoff,
+ * int soff, int nsec, int segment)
+ *

+ *   Call standard and old INT13 (int 13 %ah=AH) for DRIVE. Read/write
+ *   NSEC sectors from COFF/HOFF/SOFF into SEGMENT. If an error occurs,
+ *   return non-zero, otherwise zero.

+ */
+
+FUNCTION(grub_biosdisk_rw_standard)
+ pushl %ebp
+ movl %esp, %ebp
+
+ pushl %ebx
+ pushl %edi
+ pushl %esi
+
+ /* set up CHS information */
+
+ /* set %ch to low eight bits of cylinder */
+ xchgb %cl, %ch
+ /* set bits 6-7 of %cl to high two bits of cylinder */
+ shlb $6, %cl
+ /* set bits 0-5 of %cl to sector */
+ addb 0xc(%ebp), %cl
+ /* set %dh to head */
+ movb 0x8(%ebp), %dh
+ /* set %ah to AH */
+ movb %al, %ah
+ /* set %al to NSEC */
+ movb 0x10(%ebp), %al
+ /* save %ax in %di */
+ movw %ax, %di
+ /* save SEGMENT in %bx */
+ movw 0x14(%ebp), %bx
+
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movw %bx, %es
+ xorw %bx, %bx
+ movw $3, %si /* attempt at least three times */
+
+1:
+ movw %di, %ax
+ int $0x13 /* do the operation */
+ jnc 2f /* check if successful */
+
+ movb %ah, %bl /* save return value */
+ /* if fail, reset the disk system */
+ xorw %ax, %ax
+ int $0x13
+
+ decw %si
+ cmpw $0, %si
+ je 2f
+ xorb %bl, %bl
+ jmp 1b /* retry */
+2:
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %bl, %al /* return value in %eax */
+
+ popl %esi
+ popl %edi
+ popl %ebx
+ popl %ebp
+
+ ret $(4 * 4)
+
+
+/*
+ * int grub_biosdisk_check_int13_extensions (int drive)
+ *
+ * Check if LBA is supported for DRIVE. If it is supported, then
return
+ * the major version of extensions, otherwise zero.
+ */
+ +FUNCTION(grub_biosdisk_check_int13_extensions)
+ pushl %ebp
+ pushl %ebx
+
+ /* drive */
+ movb %al, %dl
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb $0x41, %ah
+ movw $0x55aa, %bx
+ int $0x13 /* do the operation */
+
+ /* check the result */
+ jc 1f
+ cmpw $0xaa55, %bx
+ jne 1f
+
+ movb %ah, %bl /* save the major version into %bl */
+
+ /* check if AH=0x42 is supported */
+ andw $1, %cx
+ jnz 2f
+
+1:
+ xorb %bl, %bl
+2:
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %bl, %al /* return value in %eax */
+
+ popl %ebx
+ popl %ebp
+
+ ret
+
+
+/*
+ * int grub_biosdisk_get_diskinfo_int13_extensions (int drive, void
*drp)
+ *
+ * Return the geometry of DRIVE in a drive parameters, DRP. If an
error
+ * occurs, then return non-zero, otherwise zero.
+ */
+ +FUNCTION(grub_biosdisk_get_diskinfo_int13_extensions)
+ pushl %ebp
+ pushl %ebx
+ pushl %esi
+
+ /* compute the address of drive parameters */
+ movw %dx, %si
+ xorw %dx, %dx
+ shrl $4, %edx
+ movw %dx, %bx /* save the segment into %bx */
+ /* drive */
+ movb %al, %dl
+ /* enter real mode */
+ call prot_to_real
+
+ .code16
+ movb $0x48, %ah
+ movw %bx, %ds
+ int $0x13 /* do the operation */
+ movb %ah, %bl /* save return value in %bl */
+ /* clear the data segment */
+ xorw %ax, %ax
+ movw %ax, %ds
+ /* back to protected mode */
+ DATA32 call real_to_prot
+ .code32
+
+ movb %bl, %al /* return value in %eax */
+
+ popl %esi
+ popl %ebx
+ popl %ebp
+
+ ret
+
+
+/*
+ * int grub_biosdisk_get_diskinfo_standard (int drive,
+ * unsigned long *cylinders,

1,194 lines not shown · raw message

../src/grub-1.93/symlist2.c renamed from symlist2.c +0 -0
diff -EbwBruN ./symlist2.c ../../src/grub-1.93/symlist2.c

226 lines not shown · raw message

../src/grub-1.93/util/grub-emu.c renamed from util/grub-emu.c +0 -0
diff -EbwBruN ./util/grub-emu.c ../../src/grub-1.93/util/grub-emu.c

42 lines not shown · raw message

../src/grub-1.93/util/misc.c renamed from util/misc.c +0 -0
diff -EbwBruN ./util/misc.c ../../src/grub-1.93/util/misc.c

51 lines not shown · raw message

../src/grub-1.93/genmk.rb renamed from genmk.rb +0 -0
diff -EbwBruN ./genmk.rb ../../src/grub-1.93/genmk.rb

47 lines not shown · raw message

../src/grub-1.93/include/grub/dl.h renamed from include/grub/dl.h +0 -0
diff -EbwBruN ./include/grub/dl.h ../../src/grub-1.93/include/grub/dl.h

216 lines not shown · raw message

../src/grub-1.93/kern/dl.c renamed from kern/dl.c +0 -0
diff -EbwBruN ./kern/dl.c ../../src/grub-1.93/kern/dl.c

160 lines not shown · raw message

../src/grub-1.93/kern/dl-gdb.c renamed from kern/dl-gdb.c +0 -0
diff -EbwBruN ./kern/dl-gdb.c ../../src/grub-1.93/kern/dl-gdb.c

311 lines not shown · raw message

../src/grub-1.93/kern/main.c renamed from kern/main.c +0 -0
diff -EbwBruN ./kern/main.c ../../src/grub-1.93/kern/main.c

13 lines not shown · raw message

../src/grub-1.93/kern/misc.c renamed from kern/misc.c +0 -0
diff -EbwBruN ./kern/misc.c ../../src/grub-1.93/kern/misc.c

12 lines not shown · raw message

../src/grub-1.93/kern/mm.c renamed from kern/mm.c +0 -0
diff -EbwBruN ./kern/mm.c ../../src/grub-1.93/kern/mm.c

16 lines not shown · raw message

../src/grub-1.93/kern/rescue.c renamed from kern/rescue.c +0 -0
diff -EbwBruN ./kern/rescue.c ../../src/grub-1.93/kern/rescue.c

38 lines not shown · raw message

../src/grub-1.93/include/grub/mm.h renamed from include/grub/mm.h +0 -0
diff -EbwBruN ./include/grub/mm.h ../../src/grub-1.93/include/grub/mm.h

41 lines not shown · raw message

../src/grub-1.93/fs/ext2.c renamed from fs/ext2.c +0 -0
diff -EbwBruN ./fs/ext2.c ../../src/grub-1.93/fs/ext2.c

10 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#4 lore raw
In reply to Eric Salomé
Quoted 2 lines · Eric Salomé

GRUB 2 Wiki
ToDoList : Implement ModuleLoading in grub-emu

Right, and I even added this item. Sorry, I just misunderstood your previous email.

Quoted 4 lines

I worked on that : Architecture i386/pc - Emulation on Linux/Ubuntu grub-emu is loading all current modules, though some won't do anything in emulation mode. The modules .mod are the same as those used for boot time.

Nice!

Quoted 4 lines

Little changes in source code, though I had to adapt asm files to passing parameters in stack (I compile without -mregparm=3 -mrtd options as I didn't want to recompile the whole C library with those passing parameters conventions).

It would be nice to get this to work for the regular modules too. In that case we do not need two set of modules. Besides that, it should also work on the PPC, for example. Which is a lot harder.

BTW, how did you implement this? I once wrote a hack to do exactly the same by using mmap to allocate executable memory. On the PPC this memory needs to be allocated close to the grub-emu binary because otherwise the relocations would fail.

Quoted 3 lines

... when I be done with grub 2 debugging I'll just have to recompile with these options (if you like them a lot) and it will work just fine (boot time only).

It's not only that we like them. They make the size of the binaries smaller. And it is a requirement for the assembler code.

Quoted 4 lines

ToBeAwardedOf:
Don't use gcc 3.3 with grub 2's current source files : compiled code is wrong and subject to unattended results.
Runs fine with gcc 4.0. I have still a doubt about gcc 3.4.

Wrong in what way? We have some workaround for specific gcc bugs. Although these are sometimes hard to locate, sometimes it might be worth the effort.

Quoted 4 lines

LeftToDo :

- see if every modification is useful, as I tried a few things before I got a good result, and send a diff patch.

Please describe how things work in detail so we can discuss possible issues.

Quoted 2 lines

- I need a "patched" symlist.c to compile with grub-emu (could be generated by a script, but I am too lazy).

:-)

Quoted 2 lines

- let gdb know about dynamically loaded modules ;-), but I guess it won't be the easy part.

Yeah, I can imagine that. But in case specific modules have to be debugged, we can just compile them in as happens currently. This is what I do now.

The big advantage of having module support in grub-emu is as a debugging tool for module loading itself. It will make porting GRUB to another architecture a lot easier. And besides that, debugging will be a lot easier too.

Thanks,
Marco

#5 lore raw
Quoted 8 lines
Quoted 4 lines

Little changes in source code, though I had to adapt asm files to passing parameters in stack (I compile without -mregparm=3 -mrtd options as I didn't want to recompile the whole C library with those passing parameters conventions).

It would be nice to get this to work for the regular modules too. In that case we do not need two set of modules. Besides that, it should also work on the PPC, for example. Which is a lot harder.

I tried that a year ago and I failed: There are lots of hooks, and you don't know if passed pointer is pointer to function from module (with -mregparm) or from main binary (w/o -mregparm).

The only solution that comes to my mind now is to have all function declaration with -mregparm gcc directives. Then all grub functions will be with -mregparm and all standard library functions without. Other option will be recompiling standard libraries or creating some wrappers for them. But again, we must be careful when manipulating pointers to functions...

I was also thinking about throwing the -mregparm stuff away, but other developers here want it.

Signature · 2 lines
                                            Tomas 'Ebi' Ebenlendr
                                            http://get.to/ebik
#6 lore raw
Quoted 13 lines · Tomáš Ebenlendr
Quoted 8 lines
Quoted 4 lines

Little changes in source code, though I had to adapt asm files to passing parameters in stack (I compile without -mregparm=3 -mrtd options as I didn't want to recompile the whole C library with those passing parameters conventions).

It would be nice to get this to work for the regular modules too. In that case we do not need two set of modules. Besides that, it should also work on the PPC, for example. Which is a lot harder.

I tried that a year ago and I failed: There are lots of hooks, and you don't know if passed pointer is pointer to function from module (with -mregparm) or from main binary (w/o -mregparm).

Quoted 6 lines

The only solution that comes to my mind now is to have all function declaration with -mregparm gcc directives. Then all grub functions will be with -mregparm and all standard library functions without. Other option will be recompiling standard libraries or creating some wrappers for them. But again, we must be careful when manipulating pointers to functions...

Right, which is already being done for the modules. I agree it will be hard to make this work in the right way. It's more important to get modules loading working first, without disturbing anything else.

Quoted 2 lines

I was also thinking about throwing the -mregparm stuff away, but other developers here want it.

Right.

--
Marco

#7 lore raw
In reply to Marco Gerards

Hi folks,

This email to explain how I worked on grub-emu module loading, the choices I have made (for now) and the diff from grub-1.93 delivery. Marco asked me for details (well, that will make it a pretty long email :-)).

I worked only on the i386-pc architecture but I believe same steps will lead to a working grub-emu with module loading on powerpc architecture (though I can't do that myself).

I’ve got results : My modules are loading fine, but That may be by chance :-)

(I still have great doubts about memory management).

So take a close look at what I’ve done before agreeing/committing changes and yell if I’ve done anything foolish.

There is still work to do, as you’ll see :

Have it compile

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::

First, I had to deal with the main problem on PC:

Current delivery of grub-1.93 requires kernel and modules to be compiled with -mregparm=3 -mrtd options.

Parameters are passed to functions using registers instead of using memory in stack. Assembler code presume that asm functions are called that way too.

This is not the standard way libc functions are used to.

Also, note that even with -mregparm=3 and -mrtd options, parameters are passed using stack to functions with an undefined number of parameters (such as grub_printf : Those functions in turn expected their parameters in stack). If you don't provide good prototype to functions in your modules that leads to unexpected results.

To have a grub-emu compile with these options :

a. Either you need to compile a new "libc" library using same options

b. Either you can provide grub-emu source files with a "foolyou"
prototype include file for all C functions called in libc (there are currently not too many), saying for example :

off_t lseek(int fildes, off_t offset, int whence, ...);

The ending ... will ask the C compiler to pass all parameters in stack (the way the libc wants it for all functions).

And try to compile (I really didn't try that, I was too afraid the compiler would voice me about mismatchs in prototype between Linux standard include files and my "foolme" prototype file.

I believe that things would be worst with time, since other modules to come would like to use more library calls (in libc and in other standard libs).

I'd rather made the following choices :

---------------------------------------

1. Provide new asm code that support for passing parameters in stack, instead of from registers.

2. Change compile options so that the all Grub 2 delivery would now be using passing parameters using stack (and thus I get ride of libc incompatibility)

3. Keep support for "optimized" code (using -mregparm=3 -mrtd) just in case someone feels like he can't live without it ;-).

New Makefile (i386-pc.mk) shows :

#COMMON_ASFLAGS = -nostdinc -fno-builtin -DREGPARM

#COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32

COMMON_ASFLAGS = -nostdinc -fno-builtin

COMMON_CFLAGS = $(CFLAGS) -m32

Using the first two lines does create optimized and shorter code for kernel and modules (but you won’t be able to launch grub-emu).

The difference is about 210316 bytes against 212684 (for all the modules I have) : 1 % shorter.

Using the last two lines, the good news is you can use same modules for grub 2 and for grub-emu, and it compiles fine.

Don’t forget to add the following lines also, to be sure grub-emu is compiled with the same options as the (COMMON) modules (that’s only for ease of mind) :

grub_emu_CFLAGS = $(COMMON_CFLAGS)

grub_emu_LDFLAGS = $(COMMON_LDFLAGS) $(LIBCURSES)

I provided a new kern/i386/pc/startup.S :

#ifdef REGPARM

#include "startup.S.regparm"

#else

#include "startup.S.memparm"

#endif

startup.S.regparm is the « old » startup.S file.

Startup.S.memparm is the new one passing parameters using stack for global functions called by C. That’s a “first trial” quickly written that is not fully optimized but seems to be bug free.

Had also to do little changes in normal/i386/pc/setjmp.S (easy to read from the diff I attached to this email) and I guess that this is the only (similar) change that will have to be made in powerpc related files.

Include a complete dynamic loader for grub-emu

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::

I didn’t want to use the standard libld.so to load modules :

a) modules are “relocated” ELF files, while dynamic loader on Linux deals with “shared” .so ELF files.

b) emulation means we better try to use same code than the one used in Grub 2.

So I just let included kern/dl.c to the grub-emu code.

Had to get ride of template functions in util/grub-emu.c such as

grub_arch_dl_check_header (void *ehdr)

grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)

And use the real ones (for i386-pc !!) from kern/i386/dl.c.

I am also using grub_register_exported_symbols from Grub 2’s instead of a null function.

Beware : I didn’t know what to do about grub_arch_modules_addr (void)

so I let it return 0 not knowing the consequences. I guess I’ll have to
come back to that point later on.

Makefiles : I don’t have Ruby running on my pc, so I provided conf/i386-pc.mk diff, but the main thing to be retained is, I just added two dependencies :

kern/i386/dl.c and symlist2.c to be compiled with grub-emu.

Have a Complete grub-emu (no undefined symbols when loading modules)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::

Well, I guess I was very loosy on that part.

The main purpose of my work was to get grub-emu load modules, whether or not it’s useful somehow.

So I kind of “skip” that part and “gracefully” ;-) ended using a symlist2.c file that shows all the work that remains to do :

I added two “null” items (function / var), in util/misc.c (but they may better be in util/grub-emu.c) :

int grub_null()

{

    return 0;

}

int grub_null_var = 0;

And I just “connected” every undefined symbol in grub-emu to those two items, using a “special” symlist2.c file (yes I can do such messy work) :

      {"grub_vga_set_mode", grub_null},

      {"grub_vga_get_font", grub_null},

      {"grub_vbe_get_controller_info", grub_null},

      {"grub_vbe_get_mode_info", grub_null},

      {"grub_vbe_set_mode", grub_null},

      {"grub_vbe_get_mode", grub_null},

      {"grub_vbe_set_memory_window", grub_null},

      {"grub_vbe_get_memory_window", grub_null},

      {"grub_vbe_set_scanline_length", grub_null},

      {"grub_vbe_get_scanline_length", grub_null},

      {"grub_vbe_set_display_start", grub_null},

      {"grub_vbe_get_display_start", grub_null},

      {"grub_vbe_set_palette_data", grub_null},

I guess that vbe module won’t be of any use with that “fixup”.

In fact vbetest command will crash grub-emu.

But watch this :

      {"grub_lower_mem", &grub_null_var},

      {"grub_upper_mem", &grub_null_var},

      {"grub_os_area_addr", &grub_null_var},

      {"grub_os_area_size", &grub_null_var},

      {"grub_linux_prot_size", &grub_null_var},

      {"grub_linux_tmp_addr", &grub_null_var},

      {"grub_linux_real_addr", &grub_null_var},

 

      {"grub_linux_boot_zimage", grub_null},

      {"grub_linux_boot_bzimage", grub_null},

      {"grub_chainloader_real_boot", grub_null},

      {"grub_multiboot_real_boot", grub_null},

Did you say grub-emu main purpose was to boot some kind of OS ?

LeftToDo : I guess the makefile for grub-emu has to be changed (a lot ?) and that emulation needs to be completed else have a look of what get me into this and tell me a better fixup.

Some functions are only (from I have seen in Grub 2 code) coded in kern/i386/pc/startup.S, and I can’t include that file into grub-emu.

So, either some of the useful functions have to be “relocated” in some other source file, either these functions needs to have a coding to be used in grub-emu (emulation mode) somewhere within util/.

Anyway, for now, modules are loading fine with this “fixup”.

Loading Modules

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::

I thought I will have to change memory allocation to allow for module loading.

Malloc is not what is expected to load code into memory and run it.

mmap is the normal way to do that under Linux.

In fact, one of my modules loads a sub-module in a very special way (specific dynamic loader), so I needed to code a new function :

Grub_mmap(int size) (that takes only one parameter for now) :

static char * heapstart=(char*) 0x01200000;

void *

grub_mmap (grub_size_t size)

{

void * heap;

heap=(void *)mmap(heapstart, size,

PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1,
0);

    heapstart+=size; 

    printf("Using mmap (size %d)\n", size);

    return heap;

}

But surprisingly enough, I didn’t need to use it to load modules with kern/dl.c.

Beware : I am still wondering if I am not loading modules on top of the kernel code in grub-emu !?!, though it’s been a week that I am using loading module with grub-emu and didn’t crash yet.

Getting Crazy enough to fix some code in Grub 2, just in case.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::

Well, at that point, every thing is setup to “work”.

But it didn’t at first trial.

Had numerous SIGVSEG failed and mess up in malloc/free grub’s routines.

I tried to debug, finding out where the bug would be located, and finally used a different compiler (went to gcc-4.0), but kept some changes in grub 2’s code just in case (not knowing if these changes really matter for your compiler).

The mess came from two major points of the code (from what I’ve found out) :

Call to (mod->init) (mod); to initiate modules after loading

Well, I guess that this is important, especially when you compile with –mregparm=3 –mrtd to specify if the function you are calling takes a fixed number of parameters (to be passed by registers) or an undefined number of parameters (to be passed in the stack). If you don’t specify that, you let the compiler choose his way of doing it … and that might well be the wrong way. The function you called should (has to) be prototyped exactly the same.

It’s crucial for this line of code, because the function you call relies on the fact the parameter mod is a pointer to a module structure.

This structure doesn’t contain a MAGIC number (I believe it should !) to check on, before doing anything foolish such as … doing things on mod->dep.

I fixed :

static void grub_mod_init (grub_dl_t mod) __attribute__ ((used));

(I suppressed __attribute__ “unused” on parameter mod)

I gave a patch for this kind of call :

typedef void FINIT(void * );

typedef FINIT * PFINIT;

    ((PFINIT)mod->init) (mod);

(just in case, though the compiler should have correctly guessed it from struct grub_dl).

but anyway that sort of patch should be applied everywhere you make a *fct() call to something if *fct is not properly prototyped in include files.

Call to setjmp(grub_exit_env)

In some cases (I didn’t try to reproduce since I’ve moved to gcc 4.0) the setjmp function received *grub_exit_env instead of grub_exit_env. So I’ve coded this patch :

if (grub_setjmp (&grub_exit_env[0]) == 0)

instead of

if (grub_setjmp (grub_exit_env) == 0)

though it is less portable.

Up to you to see if you keep these “patches”. I didn’t tried to reproduce the errors without those patches since I went to gcc 4.0.

Some other changes

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::

Added a grub_break() function : That’s temporary to let me set easy breakpoints with gdb, from modules.

Added grub_dprintf("symbols", …) lines to let me see the loading process and have a table of which section is loaded at where.

I’d like to talk about how to let gdb know about newly loaded modules, but I’ll answer to Tomas 'Ebi' Ebenlendr’s email separately.

I don’t like the diff format I sent, so don’t hesitate to give me the right command to send you a diff (“patch”) with the format you like best.

Best regards,

_________________________________________

Eric Salomé - Paris, France

Quoted 20 lines

-----Message d'origine-----

De : [email protected] [mailto:grub-devel-

[email protected]] De la part de Marco Gerards

Envoyé : vendredi 5 mai 2006 13:39

À : The development of GRUB 2

Objet : Re: RE : grub-emu state of the art

Quoted 1 line · Eric Salomé

GRUB 2 Wiki

Quoted 13 lines
Quoted 1 line

ToDoList : Implement ModuleLoading in grub-emu

Right, and I even added this item. Sorry, I just misunderstood your

previous email.

Quoted 3 lines

I worked on that : Architecture i386/pc - Emulation on Linux/Ubuntu

grub-emu is loading all current modules, though some won't do

anything

Quoted 1 line
Quoted 1 line

in emulation mode. The modules .mod are the same as those used for

boot

Quoted 11 lines
Quoted 1 line

time.

Nice!

Quoted 3 lines

Little changes in source code, though I had to adapt asm files to

passing parameters in stack (I compile without -mregparm=3 -mrtd

options

Quoted 14 lines
Quoted 3 lines

as I didn't want to recompile the whole C library with those passing

parameters conventions).

It would be nice to get this to work for the regular modules too. In

that case we do not need two set of modules. Besides that, it should

also work on the PPC, for example. Which is a lot harder.

Quoted 13 lines

BTW, how did you implement this? I once wrote a hack to do exactly

the same by using mmap to allocate executable memory. On the PPC this

memory needs to be allocated close to the grub-emu binary because

otherwise the relocations would fail.

Quoted 3 lines

... when I be done with grub 2 debugging I'll just have to recompile

with these options (if you like them a lot) and it will work just

fine

Quoted 4 lines
Quoted 1 line

(boot time only).

Quoted 9 lines

It's not only that we like them. They make the size of the binaries

smaller. And it is a requirement for the assembler code.

Quoted 3 lines

ToBeAwardedOf:

Don't use gcc 3.3 with grub 2's current source files : compiled code

is

Quoted 6 lines
Quoted 3 lines

wrong and subject to unattended results.

Runs fine with gcc 4.0. I have still a doubt about gcc 3.4.

Quoted 13 lines

Wrong in what way? We have some workaround for specific gcc bugs.

Although these are sometimes hard to locate, sometimes it might be

worth the effort.

Quoted 5 lines

LeftToDo :

- see if every modification is useful, as I tried a few things

before I

Quoted 56 lines
Quoted 1 line

got a good result, and send a diff patch.

Please describe how things work in detail so we can discuss possible

issues.

Quoted 3 lines

- I need a "patched" symlist.c to compile with grub-emu (could be

generated by a script, but I am too lazy).

:-)

Quoted 3 lines

- let gdb know about dynamically loaded modules ;-), but I guess it

won't be the easy part.

Yeah, I can imagine that. But in case specific modules have to be

debugged, we can just compile them in as happens currently. This is

what I do now.

The big advantage of having module support in grub-emu is as a

debugging tool for module loading itself. It will make porting GRUB

to another architecture a lot easier. And besides that, debugging

will be a lot easier too.

Thanks,

Marco

Quoted 7 lines

_______________________________________________

Grub-devel mailing list

[email protected]

http://lists.gnu.org/mailman/listinfo/grub-devel

diff -n -r ./conf/i386-pc.mk ../../src/grub-1.93/conf/i386-pc.mk a2 3
#COMMON_ASFLAGS = -nostdinc -fno-builtin -DREGPARM #COMMON_CFLAGS = -fno-builtin -mrtd -mregparm=3 -m32

d4 1
a4 2
COMMON_CFLAGS = $(CFLAGS) -m32

a6 3
grub_emu_CFLAGS = $(COMMON_CFLAGS)
grub_emu_LDFLAGS = $(COMMON_LDFLAGS) $(LIBCURSES)

d786 3
a788 4

	util/i386/pc/misc.c grub_emu_init.c				\
	symlist2.c kern/i386/dl.c

CLEANFILES += grub-emu grub_emu-commands_boot.o grub_emu-commands_cat.o grub_emu-commands_cmp.o grub_emu-commands_configfile.o grub_emu-commands_default.o grub_emu-commands_help.o grub_emu-commands_terminal.o grub_emu-commands_ls.o grub_emu-commands_test.o grub_emu-commands_search.o grub_emu-commands_timeout.o grub_emu-commands_i386_pc_halt.o grub_emu-commands_i386_pc_reboot.o grub_emu-disk_loopback.o grub_emu-fs_affs.o grub_emu-fs_ext2.o grub_emu-fs_fat.o grub_emu-fs_fshelp.o grub_emu-fs_hfs.o grub_emu-fs_iso9660.o grub_emu-fs_jfs.o grub_emu-fs_minix.o grub_emu-fs_sfs.o grub_emu-fs_ufs.o grub_emu-fs_xfs.o grub_emu-fs_hfsplus.o grub_emu-io_gzio.o grub_emu-kern_device.o grub_emu-kern_disk.o grub_emu-kern_dl.o grub_emu-kern_env.o grub_emu-kern_err.o grub_emu-normal_execute.o grub_emu-kern_file.o grub_emu-kern_fs.o grub_emu-normal_lexer.o grub_emu-kern_loader.o grub_emu-kern_main.o grub_emu-kern_misc.o grub_emu-kern_parser.o grub_emu-grub_script_tab.o grub_emu-kern_partition.o grub_emu-kern_rescue.o grub_emu-kern_term.o grub_emu-normal_arg.o grub_emu-normal_cmdline.o grub_emu-normal_command.o grub_emu-normal_function.o grub_emu-normal_completion.o grub_emu-normal_context.o grub_emu-normal_main.o grub_emu-normal_menu.o grub_emu-normal_menu_entry.o grub_emu-normal_misc.o grub_emu-normal_script.o grub_emu-partmap_amiga.o grub_emu-partmap_apple.o grub_emu-partmap_pc.o grub_emu-partmap_sun.o grub_emu-partmap_acorn.o grub_emu-partmap_gpt.o grub_emu-util_console.o grub_emu-util_grub_emu.o grub_emu-util_misc.o grub_emu-util_i386_pc_biosdisk.o grub_emu-util_i386_pc_getroot.o grub_emu-util_i386_pc_misc.o grub_emu-grub_emu_init.o grub_emu-symlist.o grub_emu-symlist2.o grub_emu-kern_i386_dl.o MOSTLYCLEANFILES += grub_emu-commands_boot.d grub_emu-commands_cat.d grub_emu-commands_cmp.d grub_emu-commands_configfile.d grub_emu-commands_default.d grub_emu-commands_help.d grub_emu-commands_terminal.d grub_emu-commands_ls.d grub_emu-commands_test.d grub_emu-commands_search.d grub_emu-commands_timeout.d grub_emu-commands_i386_pc_halt.d grub_emu-commands_i386_pc_reboot.d grub_emu-disk_loopback.d grub_emu-fs_affs.d grub_emu-fs_ext2.d grub_emu-fs_fat.d grub_emu-fs_fshelp.d grub_emu-fs_hfs.d grub_emu-fs_iso9660.d grub_emu-fs_jfs.d grub_emu-fs_minix.d grub_emu-fs_sfs.d grub_emu-fs_ufs.d grub_emu-fs_xfs.d grub_emu-fs_hfsplus.d grub_emu-io_gzio.d grub_emu-kern_device.d grub_emu-kern_disk.d grub_emu-kern_dl.d grub_emu-kern_env.d grub_emu-kern_err.d grub_emu-normal_execute.d grub_emu-kern_file.d grub_emu-kern_fs.d grub_emu-normal_lexer.d grub_emu-kern_loader.d grub_emu-kern_main.d grub_emu-kern_misc.d grub_emu-kern_parser.d grub_emu-grub_script_tab.d grub_emu-kern_partition.d grub_emu-kern_rescue.d grub_emu-kern_term.d grub_emu-normal_arg.d grub_emu-normal_cmdline.d grub_emu-normal_command.d grub_emu-normal_function.d grub_emu-normal_completion.d grub_emu-normal_context.d grub_emu-normal_main.d grub_emu-normal_menu.d grub_emu-normal_menu_entry.d grub_emu-normal_misc.d grub_emu-normal_script.d grub_emu-partmap_amiga.d grub_emu-partmap_apple.d grub_emu-partmap_pc.d grub_emu-partmap_sun.d grub_emu-partmap_acorn.d grub_emu-partmap_gpt.d grub_emu-util_console.d grub_emu-util_grub_emu.d grub_emu-util_misc.d grub_emu-util_i386_pc_biosdisk.d grub_emu-util_i386_pc_getroot.d grub_emu-util_i386_pc_misc.d grub_emu-grub_emu_init.d grub_emu-symlist2.d grub_emu-kern_i386_dl.d d790 2
a791 2
grub-emu: grub_emu-symlist2.o grub_emu-commands_boot.o grub_emu-commands_cat.o grub_emu-commands_cmp.o grub_emu-commands_configfile.o grub_emu-commands_default.o grub_emu-commands_help.o grub_emu-commands_terminal.o grub_emu-commands_ls.o grub_emu-commands_test.o grub_emu-commands_search.o grub_emu-commands_timeout.o grub_emu-commands_i386_pc_halt.o grub_emu-commands_i386_pc_reboot.o grub_emu-disk_loopback.o grub_emu-fs_affs.o grub_emu-fs_ext2.o grub_emu-fs_fat.o grub_emu-fs_fshelp.o grub_emu-fs_hfs.o grub_emu-fs_iso9660.o grub_emu-fs_jfs.o grub_emu-fs_minix.o grub_emu-fs_sfs.o grub_emu-fs_ufs.o grub_emu-fs_xfs.o grub_emu-fs_hfsplus.o grub_emu-io_gzio.o grub_emu-kern_device.o grub_emu-kern_disk.o grub_emu-kern_dl.o grub_emu-kern_env.o grub_emu-kern_err.o grub_emu-normal_execute.o grub_emu-kern_file.o grub_emu-kern_fs.o grub_emu-normal_lexer.o grub_emu-kern_loader.o grub_emu-kern_main.o grub_emu-kern_misc.o grub_emu-kern_parser.o grub_emu-grub_script_tab.o grub_emu-kern_partition.o grub_emu-kern_rescue.o grub_emu-kern_term.o grub_emu-normal_arg.o grub_emu-normal_cmdline.o grub_emu-normal_command.o grub_emu-normal_function.o grub_emu-normal_completion.o grub_emu-normal_context.o grub_emu-normal_main.o grub_emu-normal_menu.o grub_emu-normal_menu_entry.o grub_emu-normal_misc.o grub_emu-normal_script.o grub_emu-partmap_amiga.o grub_emu-partmap_apple.o grub_emu-partmap_pc.o grub_emu-partmap_sun.o grub_emu-partmap_acorn.o grub_emu-partmap_gpt.o grub_emu-util_console.o grub_emu-util_grub_emu.o grub_emu-util_misc.o grub_emu-util_i386_pc_biosdisk.o grub_emu-util_i386_pc_getroot.o grub_emu-util_i386_pc_misc.o grub_emu-grub_emu_init.o grub_emu-kern_i386_dl.o

	$(BUILD_CC) -o $@ $^ $(BUILD_CFLAGS) $(BUILD_LDFLAGS) $(grub_emu_LDFLAGS)
a1024 8
grub_emu-kern_i386_dl.o: kern/i386/dl.c
	$(BUILD_CC) -Ikern -I$(srcdir)/kern -I$(srcdir)/kern/i386 $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) -DGRUB_UTIL=1 $(grub_emu_CFLAGS) -c -o $@ $<

grub_emu-kern_i386_dl.d: kern/i386/dl.c

	set -e; 	  $(BUILD_CC) -Ikern -I$(srcdir)/kern -I$(srcdir)/kern/i386 $(BUILD_CPPFLAGS) $(BUILD_CFLAGS) -DGRUB_UTIL=1 $(grub_emu_CFLAGS) -M $< 	  | sed 's,dl\.o[ :]*,grub_emu-kern__i386_dl.o $@ : ,g' > $@; 	  [ -s $@ ] || rm -f $@

-include grub_emu-kern_i386_dl.d

a1336 7
grub_emu-symlist2.o: symlist2.c

	$(CC) -I. -I$(srcdir)/. $(CPPFLAGS)  $(CFLAGS) $(grub_emu_CFLAGS) -c -o $@ $<

grub_emu-symlist2.d: symlist2.c

	set -e; 	  $(CC) -I. -I$(srcdir)/. $(CPPFLAGS)  $(CFLAGS) $(grub_emu_CFLAGS) -M $< 	  | sed 's,symlist2\.o[ :]*,grub_emu-symlist2.o $@ : ,g' > $@; 	  [ -s $@ ] || rm -f $@

-include grub_emu-symlist2.d
d1338 1
diff -n -r ./fs/ext2.c ../../src/grub-1.93/fs/ext2.c
d174 1
a174 1
static grub_dl_t my_mod = (grub_dl_t) 0;
diff -n -r ./include/grub/dl.h ../../src/grub-1.93/include/grub/dl.h d29 1
a29 1
static void grub_mod_init (grub_dl_t mod) __attribute__ ((used)); \
d34 1
a34 1
grub_mod_init (grub_dl_t mod)
diff -n -r ./include/grub/misc.h ../../src/grub-1.93/include/grub/misc.h a78 1
void EXPORT_FUNC(grub_break) (const char * fct, int i);
diff -n -r ./include/grub/mm.h ../../src/grub-1.93/include/grub/mm.h a31 1
void *EXPORT_FUNC(grub_mmap) (grub_size_t size);
diff -n -r ./include/grub/util/misc.h ../../src/grub-1.93/include/grub/util/misc.h a47 1
void grub_break (const char * fct, int index);
diff -n -r ./kern/dl.c ../../src/grub-1.93/kern/dl.c
d319 1
a319 1
grub_err_t
a361 4

	      else {
	        grub_dprintf ("symbols",
			"%s: Resolved Symbol %s\n", mod->name, name);
	      }
d371 1
a371 3
	    if (grub_dl_register_symbol (name, (void *) sym->st_value, mod)) {
	      grub_dprintf("symbols",
		      "Err registering %s (%d)\n", name, grub_errno);
a372 4
	    } else {
	      grub_dprintf("symbols",
		      "%s: Registering %s\n", mod->name, name);
	    }
d379 1
a379 3
	    if (grub_dl_register_symbol (name, (void *) sym->st_value, mod)) {
	      grub_dprintf("symbols",
		      "Err registering %s (%d)\n", name, grub_errno);
a380 4
	    } else {
	      grub_dprintf("symbols",
		      "%s: Registering %s\n", mod->name, name);
	    }

a405 2
typedef void FINIT(void * );
typedef FINIT * PFINIT;
d410 1
a410 1

    ((PFINIT)mod->init) (mod);
a514 3
      grub_dprintf("symbols", 
	      "%s: Section %d loaded at 0x%x (size %d)\n",
	      mod->name, seg->section, seg->addr, seg->size);
d535 1
a535 1
      /* return 0; */

diff -n -r ./kern/i386/pc/startup.S ../../src/grub-1.93/kern/i386/pc/startup.S a19 5
#ifdef REGPARM
#include "startup.S.regparm"
#else
#include "startup.S.memparm"
#endif
d21 2150
Only in ../../src/grub-1.93/kern/i386/pc: startup.S.memparm
Only in ../../src/grub-1.93/kern/i386/pc: startup.S.regparm
diff -n -r ./kern/misc.c ../../src/grub-1.93/kern/misc.c
a877 5

extern int grub_printf (const char * fmt, ...);
void grub_break (const char * fct, int index) {

    grub_printf ("%s: Break at %d\n", fct, index);
}

diff -n -r ./kern/mm.c ../../src/grub-1.93/kern/mm.c a289 6
void *
grub_mmap (grub_size_t size)
{
return grub_memalign (0, size);
}

diff -n -r ./normal/i386/setjmp.S ../../src/grub-1.93/normal/i386/setjmp.S a29 3

#ifndef REGPARM
	movl	4(%esp), %eax	/* Get Param */
#endif
a44 4
#ifndef REGPARM
	movl	4(%esp), %eax	/* Get Param */
	movl	8(%esp), %edx	/* Get Param */

#endif
diff -n -r ./normal/main.c ../../src/grub-1.93/normal/main.c d236 1
a236 1
if (grub_setjmp (&grub_exit_env[0]) == 0)
Only in ../../src/grub-1.93/: symlist2.c
diff -n -r ./util/grub-emu.c ../../src/grub-1.93/util/grub-emu.c d59 18
a76 1
return 000;
diff -n -r ./util/misc.c ../../src/grub-1.93/util/misc.c a214 14
#include <sys/mman.h>

static char * heapstart=(char*) 0x01200000;
void *
grub_mmap (grub_size_t size)

{
    void * heap;
    heap=(void *)mmap(heapstart, size,
	    	PROT_EXEC|PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
    heapstart+=size; 
    printf("Using mmap (size %d)\n", size);
    return heap;
}

d218 1
a218 1
if (ptr) free (ptr);
d247 5
a273 7

int grub_null()

{
    return 0;
}

int grub_null_var = 0;

a52 3
extern int grub_null();
extern int grub_null_var;

d138 2
a140 1

      {"grub_mmap", grub_mmap},

d174 2
a175 2
{"grub_console_checkkey", grub_checkkey},
{"grub_console_getkey", grub_getkey},
d178 4
a181 5

      {"grub_break", grub_break},
      {"grub_linux_boot_zimage", grub_null},
      {"grub_linux_boot_bzimage", grub_null},
      {"grub_chainloader_real_boot", grub_null},
      {"grub_multiboot_real_boot", grub_null},
d183 13
a195 13
      {"grub_vga_set_mode", grub_null},
      {"grub_vga_get_font", grub_null},
      {"grub_vbe_get_controller_info", grub_null},
      {"grub_vbe_get_mode_info", grub_null},
      {"grub_vbe_set_mode", grub_null},
      {"grub_vbe_get_mode", grub_null},
      {"grub_vbe_set_memory_window", grub_null},
      {"grub_vbe_get_memory_window", grub_null},
      {"grub_vbe_set_scanline_length", grub_null},
      {"grub_vbe_get_scanline_length", grub_null},
      {"grub_vbe_set_display_start", grub_null},
      {"grub_vbe_get_display_start", grub_null},
      {"grub_vbe_set_palette_data", grub_null},
d199 7
a205 7
      {"grub_lower_mem", &grub_null_var},
      {"grub_upper_mem", &grub_null_var},
      {"grub_os_area_addr", &grub_null_var},
      {"grub_os_area_size", &grub_null_var},
      {"grub_linux_prot_size", &grub_null_var},
      {"grub_linux_tmp_addr", &grub_null_var},
      {"grub_linux_real_addr", &grub_null_var},
#8 lore raw
Quoted 17 lines · Eric Salomé

Hi folks,

This email to explain how I worked on grub-emu module loading, the choices I have made (for now) and the diff from grub-1.93 delivery. Marco asked me for details (well, that will make it a pretty long email :-)).

I worked only on the i386-pc architecture but I believe same steps will lead to a working grub-emu with module loading on powerpc architecture (though I can't do that myself).

Quoted 4 lines

I’ve got results : My modules are loading fine, but That may be by chance :-)

(I still have great doubts about memory management).

I did not look at your attachment so I am not sure what your are speaking about but I wonder why you mention memory management ? Memory management in grub-emu isn't done with the usual malloc and free ?