2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A/* The code segment of the protected mode. */
2N/A#define CODE_SEGMENT 0x08
2N/A
2N/A/* The data segment of the protected mode. */
2N/A#define DATA_SEGMENT 0x10
2N/A
2N/A#define PSEUDO_REAL_CSEG 0x18
2N/A
2N/A#define PSEUDO_REAL_DSEG 0x20
2N/A
2N/A#include "relocator_common.S"
2N/A
2N/A .p2align 4 /* force 16-byte alignment */
2N/A
2N/AVARIABLE(grub_relocator16_start)
2N/A PREAMBLE
2N/A
2N/A movl %esi, %eax
2N/A movw %ax, (LOCAL (cs_base_bytes12) - LOCAL (base)) (RSI, 1)
2N/A shrl $16, %eax
2N/A movb %al, (LOCAL (cs_base_byte3) - LOCAL (base)) (RSI, 1)
2N/A
2N/A RELOAD_GDT
2N/A .code32
2N/A /* Update other registers. */
2N/A movl $DATA_SEGMENT, %eax
2N/A movl %eax, %ds
2N/A movl %eax, %es
2N/A movl %eax, %fs
2N/A movl %eax, %gs
2N/A movl %eax, %ss
2N/A
2N/A DISABLE_PAGING
2N/A
2N/A#ifdef __x86_64__
2N/A /* Disable amd64. */
2N/A movl $GRUB_MEMORY_CPU_AMD64_MSR, %ecx
2N/A rdmsr
2N/A andl $(~GRUB_MEMORY_CPU_AMD64_MSR_ON), %eax
2N/A wrmsr
2N/A#endif
2N/A
2N/A /* Turn off PAE. */
2N/A movl %cr4, %eax
2N/A andl $(~GRUB_MEMORY_CPU_CR4_PAE_ON), %eax
2N/A movl %eax, %cr4
2N/A
2N/A /* Update other registers. */
2N/A movl $PSEUDO_REAL_DSEG, %eax
2N/A movl %eax, %ds
2N/A movl %eax, %es
2N/A movl %eax, %fs
2N/A movl %eax, %gs
2N/A movl %eax, %ss
2N/A
2N/A movl %esi, %eax
2N/A shrl $4, %eax
2N/A movw %ax, (LOCAL (segment) - LOCAL (base)) (%esi, 1)
2N/A
2N/A /* jump to a 16 bit segment */
2N/A ljmp $PSEUDO_REAL_CSEG, $(LOCAL (cont2) - LOCAL(base))
2N/ALOCAL(cont2):
2N/A .code16
2N/A
2N/A /* clear the PE bit of CR0 */
2N/A movl %cr0, %eax
2N/A andl $(~GRUB_MEMORY_CPU_CR0_PE_ON), %eax
2N/A movl %eax, %cr0
2N/A
2N/A /* flush prefetch queue, reload %cs */
2N/A /* ljmp */
2N/A .byte 0xea
2N/A .word LOCAL(cont3)-LOCAL(base)
2N/ALOCAL(segment):
2N/A .word 0
2N/A
2N/ALOCAL(cont3):
2N/A
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_keep_a20_enabled)
2N/A .word 0
2N/A test %ax, %ax
2N/A jnz LOCAL(gate_a20_done)
2N/A
2N/A /* first of all, test if already in a good state */
2N/A call LOCAL(gate_a20_check_state)
2N/A testb %al, %al
2N/A jz LOCAL(gate_a20_done)
2N/A
2N/A /* second, try a BIOS call */
2N/A movw $0x2400, %ax
2N/A int $0x15
2N/A
2N/A call LOCAL(gate_a20_check_state)
2N/A testb %al, %al
2N/A jz LOCAL(gate_a20_done)
2N/A
2N/A /*
2N/A * In macbook, the keyboard test would hang the machine, so we move
2N/A * this forward.
2N/A */
2N/A /* fourth, try the system control port A */
2N/A inb $0x92
2N/A andb $(~0x03), %al
2N/A outb $0x92
2N/A
2N/A /* When turning off Gate A20, do not check the state strictly,
2N/A because a failure is not fatal usually, and Gate A20 is always
2N/A on some modern machines. */
2N/A jmp LOCAL(gate_a20_done)
2N/A
2N/ALOCAL(gate_a20_check_state):
2N/A /* iterate the checking for a while */
2N/A movw $100, %cx
2N/A1:
2N/A call 3f
2N/A testb %al, %al
2N/A jz 2f
2N/A loop 1b
2N/A2:
2N/A ret
2N/A
2N/A3:
2N/A xorw %ax, %ax
2N/A movw %ax, %ds
2N/A decw %ax
2N/A movw %ax, %es
2N/A xorw %ax, %ax
2N/A
2N/A movw $0x8000, %ax
2N/A /* compare the byte at ADDR with that at 0x100000 + ADDR */
2N/A movw %ax, %si
2N/A addw $0x10, %ax
2N/A movw %ax, %di
2N/A
2N/A /* save the original byte in DL */
2N/A movb %ds:(%si), %dl
2N/A movb %es:(%di), %al
2N/A /* try to set one less value at ADDR */
2N/A movb %al, %dh
2N/A decb %dh
2N/A movb %dh, %ds:(%si)
2N/A /* serialize */
2N/A outb %al, $0x80
2N/A outb %al, $0x80
2N/A /* obtain the value at 0x100000 + ADDR in CH */
2N/A movb %es:(%di), %dh
2N/A /* this result is 1 if A20 is on or 0 if it is off */
2N/A subb %dh, %al
2N/A xorb $1, %al
2N/A /* restore the original */
2N/A movb %dl, %es:(%di)
2N/A ret
2N/A
2N/ALOCAL(gate_a20_done):
2N/A /* we are in real mode now
2N/A * set up the real mode segment registers : DS, SS, ES
2N/A */
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_ds)
2N/A .word 0
2N/A movw %ax, %ds
2N/A
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_es)
2N/A .word 0
2N/A movw %ax, %es
2N/A
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_fs)
2N/A .word 0
2N/A movw %ax, %fs
2N/A
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_gs)
2N/A .word 0
2N/A movw %ax, %gs
2N/A
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_ss)
2N/A .word 0
2N/A movw %ax, %ss
2N/A
2N/A /* movw imm16, %ax. */
2N/A .byte 0xb8
2N/AVARIABLE(grub_relocator16_sp)
2N/A .word 0
2N/A movzwl %ax, %esp
2N/A
2N/A /* movw imm32, %eax. */
2N/A .byte 0x66, 0xb8
2N/AVARIABLE(grub_relocator16_esi)
2N/A .long 0
2N/A movl %eax, %esi
2N/A
2N/A /* movw imm32, %edx. */
2N/A .byte 0x66, 0xba
2N/AVARIABLE(grub_relocator16_edx)
2N/A .long 0
2N/A
2N/A /* movw imm32, %ebx. */
2N/A .byte 0x66, 0xbb
2N/AVARIABLE(grub_relocator16_ebx)
2N/A .long 0
2N/A
2N/A /* Cleared direction flag is of no problem with any current
2N/A payload and makes this implementation easier. */
2N/A cld
2N/A
2N/A /* ljmp */
2N/A .byte 0xea
2N/AVARIABLE(grub_relocator16_ip)
2N/A .word 0
2N/AVARIABLE(grub_relocator16_cs)
2N/A .word 0
2N/A
2N/A .code32
2N/A
2N/A /* GDT. Copied from loader/i386/linux.c. */
2N/A .p2align 4
2N/ALOCAL(gdt):
2N/A .word 0, 0
2N/A .byte 0, 0, 0, 0
2N/A
2N/A /* -- code segment --
2N/A * base = 0x00000000, limit = 0xFFFFF (4 KiB Granularity), present
2N/A * type = 32bit code execute/read, DPL = 0
2N/A */
2N/A .word 0xFFFF, 0
2N/A .byte 0, 0x9A, 0xCF, 0
2N/A
2N/A /* -- data segment --
2N/A * base = 0x00000000, limit 0xFFFFF (4 KiB Granularity), present
2N/A * type = 32 bit data read/write, DPL = 0
2N/A */
2N/A .word 0xFFFF, 0
2N/A .byte 0, 0x92, 0xCF, 0
2N/A
2N/A /* -- 16 bit real mode CS --
2N/A * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
2N/A * type = 16 bit code execute/read only/conforming, DPL = 0
2N/A */
2N/A .word 0xFFFF
2N/ALOCAL(cs_base_bytes12):
2N/A .word 0
2N/ALOCAL(cs_base_byte3):
2N/A .byte 0
2N/A
2N/A .byte 0x9E, 0, 0
2N/A
2N/A /* -- 16 bit real mode DS --
2N/A * base = 0x00000000, limit 0x0FFFF (1 B Granularity), present
2N/A * type = 16 bit data read/write, DPL = 0
2N/A */
2N/A .word 0xFFFF, 0
2N/A .byte 0, 0x92, 0, 0
2N/ALOCAL(gdt_end):
2N/A
2N/AVARIABLE(grub_relocator16_end)