3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2000,2001,2002,2005 Free Software Foundation, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include <grub/boot.h>
22
#include <grub/machine/boot.h>
25
* defines for the code go here
29
This makes the assembler generate the address without support
30
from the linker. (ELF can't relocate 16-bit addresses!) */
31
#define ABS(x) (x-_start+0x7c00)
33
/* Print message string */
34
#define MSG(x) movw $ABS(x), %si; call message
36
/* XXX: binutils-2.9.1.0.x doesn't produce a short opcode for this. */
37
#define MOV_MEM_TO_AL(x) .byte 0xa0; .word x
43
/* Tell GAS to generate 16-bit instructions so that this code works
47
.globl _start; _start:
49
* _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00
53
* Beginning of the sector is compatible with the FAT/HPFS BIOS
58
nop /* do I care about this ??? */
61
* This space is for the BIOS parameter block!!!! Don't change
62
* the first jump, nor start the code anywhere but right after
86
. = _start + GRUB_BOOT_MACHINE_BPB_END
89
* End of BIOS parameter block.
93
.byte GRUB_BOOT_VERSION_MAJOR, GRUB_BOOT_VERSION_MINOR
95
.byte 0xff /* the disk to load kernel from */
96
/* 0xff means use the boot drive */
100
.word GRUB_BOOT_MACHINE_KERNEL_ADDR
104
.word GRUB_BOOT_MACHINE_KERNEL_SEG
109
cli /* we're not safe here! */
112
* This is a workaround for buggy BIOSes which don't pass boot
113
* drive correctly. If GRUB is installed into a HDD, check if
114
* DL is masked correctly. If not, assume that the BIOS passed
115
* a bogus value and set DL to 0x80, since this is the only
116
* possible boot drive. If GRUB is installed into a floppy,
117
* this does nothing (only jump).
127
* ljmp to the next instruction because some bogus BIOSes
128
* jump to 07C0:0000 instead of 0000:7C00.
130
ljmp $0, $ABS(real_start)
134
/* set up %ds and %ss as offset from 0 */
139
/* set up the REAL stack */
140
movw $GRUB_BOOT_MACHINE_STACK_SEG, %sp
142
sti /* we're safe again */
145
* Check if we have a forced disk reference here
147
MOV_MEM_TO_AL(ABS(boot_drive)) /* movb ABS(boot_drive), %al */
152
/* save drive reference first thing! */
155
/* print a notification message on the screen */
156
MSG(notification_string)
158
/* do not probe LBA if the drive is a floppy */
159
testb $GRUB_BOOT_MACHINE_BIOS_HD_FLAG, %dl
162
/* check if LBA is supported */
168
* %dl may have been clobbered by INT 13, AH=41H.
169
* This happens, for example, with AST BIOS 1.04.
174
/* use CHS if fails */
179
/* check if AH=0x42 is supported if FORCE_LBA is zero */
180
MOV_MEM_TO_AL(ABS(force_lba)) /* movb ABS(force_lba), %al */
187
/* set %si to the disk address packet */
188
movw $ABS(disk_address_packet), %si
190
/* set the mode to non-zero */
193
movl ABS(kernel_sector), %ebx
195
/* the size and the reserved byte */
201
/* the absolute address (low 32 bits) */
204
/* the segment of buffer address */
205
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, 6(%si)
212
* BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
213
* Call with %ah = 0x42
215
* %ds:%si = segment:offset of disk address packet
217
* %al = 0x0 on success; err code on failure
223
/* LBA read is not supported, so fallback to CHS. */
226
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
231
* Determine the hard disk geometry from the BIOS!
232
* We do this first, so that LS-120 IDE floppies work correctly.
239
* The call failed, so maybe use the floppy probe instead.
241
testb $GRUB_BOOT_MACHINE_BIOS_HD_FLAG, %dl
244
/* Nope, we definitely have a hard disk, and we're screwed. */
249
movw $ABS(sectors), %si
251
/* set the mode to zero */
254
/* save number of heads */
266
/* save number of cylinders */
274
/* save number of sectors */
278
/* load logical sector start (bottom half) */
279
movl ABS(kernel_sector), %eax
284
/* divide by number of sectors */
287
/* save sector start */
290
xorl %edx, %edx /* zero %edx */
291
divl 4(%si) /* divide by number of heads */
293
/* save head start */
296
/* save cylinder start */
299
/* do we need too many cylinders? */
304
* This is the loop for taking care of BIOS geometry translation (ugh!)
307
/* get high bits of cylinder */
310
shlb $6, %dl /* shift left by 6 bits */
311
movb 10(%si), %cl /* get sector */
313
incb %cl /* normalize sector (sectors go
314
from 1-N, not 0-(N-1) ) */
315
orb %dl, %cl /* composite together */
316
movb 12(%si), %ch /* sector+hcyl in cl, cylinder in ch */
325
* BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
326
* Call with %ah = 0x2
327
* %al = number of sectors
329
* %cl = sector (bits 6-7 are high bits of "cylinder")
331
* %dl = drive (0x80 for hard disk, 0x0 for floppy disk)
332
* %es:%bx = segment:offset of buffer
334
* %al = 0x0 on success; err code on failure
337
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
338
movw %bx, %es /* load %es segment with disk buffer */
340
xorw %bx, %bx /* %bx = 0, put it at 0 in the segment */
341
movw $0x0201, %ax /* function 2 */
349
movw ABS(kernel_segment), %es
352
* We need to save %cx and %si because the startup code in
353
* kernel uses them without initializing them.
372
jmp *(kernel_address)
374
/* END OF MAIN LOOP */
377
* BIOS Geometry translation error (past the end of the disk geometry!).
380
MSG(geometry_error_string)
384
* Disk probe failure.
387
MSG(hd_probe_error_string)
391
* Read error on the disk.
394
MSG(read_error_string)
397
MSG(general_error_string)
399
/* go here when you need to stop the machine hard after an error condition */
402
notification_string: .string "GRUB "
403
geometry_error_string: .string "Geom"
404
hd_probe_error_string: .string "Hard Disk"
405
read_error_string: .string "Read"
406
general_error_string: .string " Error"
409
* message: write the string pointed to by %si
411
* WARNING: trashes %si, %ax, and %bx
415
* Use BIOS "int 10H Function 0Eh" to write character in teletype mode
416
* %ah = 0xe %al = character
417
* %bh = page %bl = foreground color (graphics modes)
422
int $0x10 /* display a byte */
426
jne 1b /* if not end of string, jmp to display */
430
* Windows NT breaks compatibility by embedding a magic
434
. = _start + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
440
* This is where an MBR would go if on a hard disk. The code
441
* here isn't even referenced unless we're on a floppy. Kinda
446
. = _start + GRUB_BOOT_MACHINE_PART_START
449
.byte 36, 18, 15, 9, 0
453
* Perform floppy probe.
456
movw $ABS(probe_values-1), %si
459
/* reset floppy controller INT 13h AH=0 */
466
/* if number of sectors is 0, display error and die */
471
* Floppy disk probe failure.
473
MSG(fd_probe_error_string)
476
fd_probe_error_string: .string "Floppy"
480
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
486
/* if error, jump to "probe_loop" */
489
/* %cl is already the correct value! */
495
. = _start + GRUB_BOOT_MACHINE_PART_END
497
/* the last 2 bytes in the sector 0 contain the signature */
498
.word GRUB_BOOT_MACHINE_SIGNATURE