File grub-a20.patch of Package trustedgrub

Index: TrustedGRUB-1.1.3/stage2/asm.S
===================================================================
--- TrustedGRUB-1.1.3.orig/stage2/asm.S
+++ TrustedGRUB-1.1.3/stage2/asm.S
@@ -1805,42 +1805,83 @@ nopm:	DATA32	call	EXT_C(real_to_prot)
  *
  * 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.  :-(
+ * Try to disable the A20 gate by all means. (The argument is ignored)
+ * On success (the memory world is free), a -1 is returned,  0 on failure.
+ * It may also eat any keystrokes in the keyboard buffer.  :-(
  */
 
 ENTRY(gateA20)
+	pushl	%ebx
+	pushl	%edx
+	call	testA20
+	jnz	1f
+	call	A20_BIOS
+	call	testA20
+	jnz	1f
+	call	A20_PORT92
+	call	testA20
+	jnz	1f
+	call	A20_KBDCTL
+	call	testA20
+	jnz	1f
+	movl	$0,%eax
+	jmp	2f
+1:	movl	$-1,%eax
+2:	popl	%edx
+	popl	%ebx
+	ret
+
+testA20:
+	movl	0x500,%eax
+	movl	0x100500,%ebx
+	notl	%eax
+	movl	%eax,0x100500
+	cmpl	%eax,0x500
+	pushfl
+	movl	%ebx,0x100500
+	notl	%eax
+	movl	%eax,0x500
+	popfl
+	ret
+
+A20_BIOS:	
 	/* first, try a BIOS call */
-	pushl	%ebp
-	movl	8(%esp), %edx
 	
 	call	EXT_C(prot_to_real)
 	
 	.code16
-	movw	$0x2400, %ax
-	testw	%dx, %dx
-	jz	1f
-	incw	%ax
+	movw	$0x2401, %ax
 1:	stc
 	int	$0x15
-	jnc	2f
-
-	/* set non-zero if failed */
-	movb	$1, %ah
-
-	/* save the status */
-2:	movb	%ah, %dl
 
 	DATA32	call	EXT_C(real_to_prot)
 	.code32
-
-	popl	%ebp
-	testb	%dl, %dl
-	jnz	3f
 	ret
 
-3:	/* use keyboard controller */
+
+A20_PORT92:
+	/*
+	 * try to switch gateA20 using PORT92, the "Fast A20 and Init"
+	 * register
+	 */
+	mov     $0x92, %dx
+	inb     %dx, %al
+	/* skip the port92 code if it's unimplemented (read returns 0xff) */
+	cmpb    $0xff, %al
+	jz      6f
+
+	/* set bit1, the ALT_A20_GATE bit */
+	orb     $2, %al
+/*	and     $0xfd, %al */
+
+	/* clear the INIT_NOW bit; don't accidently reset the machine */
+	and     $0xfe, %al
+	outb    %al, %dx
+6:	ret
+
+
+A20_KBDCTL:	
+	/* use keyboard controller */
 	pushl	%eax
 
 	call    gloop1
@@ -1854,11 +1895,7 @@ gloopint1:
 	jnz	gloopint1
 
 	movb	$KB_OUTPUT_MASK, %al
-	cmpb	$0, 0x8(%esp)
-	jz	gdoit
-
 	orb	$KB_A20_ENABLE, %al
-gdoit:
 	outb	$K_RDWR
 
 	call	gloop1