stage1.S revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* -*-Asm-*- */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2004 Free Software Foundation, Inc.
*
* 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.
*/
#include <stage1.h>
/*
* defines for the code go here
*/
/* Absolute addresses
This makes the assembler generate the address without support
from the linker. (ELF can't relocate 16-bit addresses!) */
/* Print message string */
/* XXX: binutils-2.9.1.0.x doesn't produce a short opcode for this. */
.file "stage1.S"
.text
/* Tell GAS to generate 16-bit instructions so that this code works
in real mode. */
/*
* _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00
*/
/*
* parameter block.
*/
nop /* do I care about this ??? */
/*
* This space is for the BIOS parameter block!!!! Don't change
* the first jump, nor start the code anywhere but right after
* this area.
*/
. = _start + 4
/* scratch space */
mode:
.byte 0
.long 0
.long 0
.word 0
.byte 0
.byte 0
.word 0
/* more space... */
. = _start + STAGE1_BPBEND
/*
* End of BIOS parameter block.
*/
.byte 0
.word 0x8000
.long 1
.word 0x800
/* general setup */
cli /* we're not safe here! */
/*
* This is a workaround for buggy BIOSes which don't pass boot
* drive correctly. If GRUB is installed into a HDD, do
* "orb $0x80, %dl", otherwise "orb $0x00, %dl" (i.e. nop).
*/
.byte 0x00
/*
* ljmp to the next instruction because some bogus BIOSes
* jump to 07C0:0000 instead of 0000:7C00.
*/
/* set up %ds and %ss as offset from 0 */
/* set up the REAL stack */
sti /* we're safe again */
/*
* Check if we have a forced disk reference here
*/
je 1f
1:
/* save drive reference first thing! */
/* print a notification message on the screen */
/* do not probe LBA if the drive is a floppy */
/* check if LBA is supported */
int $0x13
/*
* %dl may have been clobbered by INT 13, AH=41H.
* This happens, for example, with AST BIOS 1.04.
*/
/* use CHS if fails */
/* check if AH=0x42 is supported if FORCE_LBA is zero */
/* save the total number of sectors */
/* set %si to the disk address packet */
/* set the mode to non-zero */
/* the size and the reserved byte */
/* the blocks */
/* the absolute address (low 32 bits) */
/* the segment of buffer address */
/*
* BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
* Call with %ah = 0x42
* %dl = drive number
* %ds:%si = segment:offset of disk address packet
* Return:
* %al = 0x0 on success; err code on failure
*/
int $0x13
/* LBA read is not supported, so fallback to CHS. */
/*
* Determine the hard disk geometry from the BIOS!
* We do this first, so that LS-120 IDE floppies work correctly.
*/
int $0x13
/*
* The call failed, so maybe use the floppy probe instead.
*/
/* Nope, we definitely have a hard disk, and we're screwed. */
/* set the mode to zero */
/* save number of heads */
/* save number of cylinders */
/* save number of sectors */
/* load logical sector start (bottom half) */
/* zero %edx */
/* divide by number of sectors */
/* save sector start */
/* save head start */
/* save cylinder start */
/* do we need too many cylinders? */
/*
* This is the loop for taking care of BIOS geometry translation (ugh!)
*/
/* get high bits of cylinder */
from 1-N, not 0-(N-1) ) */
/* restore %dx */
/* head number */
/*
* BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
* Call with %ah = 0x2
* %al = number of sectors
* %ch = cylinder
* %cl = sector (bits 6-7 are high bits of "cylinder")
* %dh = head
* %dl = drive (0x80 for hard disk, 0x0 for floppy disk)
* %es:%bx = segment:offset of buffer
* Return:
* %al = 0x0 on success; err code on failure
*/
int $0x13
/*
* We need to save %cx and %si because the startup code in
* stage2 uses them without initializing them.
*/
/* boot stage2 */
/* END OF MAIN LOOP */
/*
* BIOS Geometry translation error (past the end of the disk geometry!).
*/
/*
* Disk probe failure.
*/
/*
* Read error on the disk.
*/
/* go here when you need to stop the machine hard after an error condition */
/*
* message: write the string pointed to by %si
*
* WARNING: trashes %si, %ax, and %bx
*/
/*
* Use BIOS "int 10H Function 0Eh" to write character in teletype mode
* %ah = 0xe %al = character
* %bh = page %bl = foreground color (graphics modes)
*/
1:
int $0x10 /* display a byte */
/*
* Windows NT breaks compatibility by embedding a magic
* number here.
*/
.long 0
.word 0
/*
* This is where an MBR would go if on a hard disk. The code
* here isn't even referenced unless we're on a floppy. Kinda
* sneaky, huh?
*/
. = _start + STAGE1_PARTSTART
/*
* Perform floppy probe.
*/
/* reset floppy controller INT 13h AH=0 */
int $0x13
/* if number of sectors is 0, display error and die */
jne 1f
/*
* Floppy disk probe failure.
*/
1:
/* perform read */
int $0x13
/* if error, jump to "probe_loop" */
/* %cl is already the correct value! */
. = _start + STAGE1_PARTEND
/* the last 2 bytes in the sector 0 contain the signature */