3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 1999,2000,2001,2002,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6
* GRUB 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 3 of the License, or
9
* (at your option) any later version.
11
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
20
#include <grub/symbol.h>
21
#include <grub/boot.h>
22
#include <grub/machine/boot.h>
25
* defines for the code go here
28
/* Print message string */
29
#define MSG(x) movw $x, %si; call LOCAL(message)
35
/* Tell GAS to generate 16-bit instructions so that this code works
43
* _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00
47
* Beginning of the sector is compatible with the FAT/HPFS BIOS
52
nop /* do I care about this ??? */
55
* This space is for the BIOS parameter block!!!! Don't change
56
* the first jump, nor start the code anywhere but right after
60
. = _start + GRUB_BOOT_MACHINE_BPB_START
81
. = _start + GRUB_BOOT_MACHINE_BPB_END
84
* End of BIOS parameter block.
88
.word GRUB_BOOT_MACHINE_KERNEL_ADDR
90
. = _start + GRUB_BOOT_MACHINE_KERNEL_SECTOR
94
. = _start + GRUB_BOOT_MACHINE_BOOT_DRIVE
96
.byte 0xff /* the disk to load kernel from */
97
/* 0xff means use the boot drive */
102
cli /* we're not safe here! */
105
* This is a workaround for buggy BIOSes which don't pass boot
106
* drive correctly. If GRUB is installed into a HDD, check if
107
* DL is masked correctly. If not, assume that the BIOS passed
108
* a bogus value and set DL to 0x80, since this is the only
109
* possible boot drive. If GRUB is installed into a floppy,
110
* this does nothing (only jump).
112
. = _start + GRUB_BOOT_MACHINE_DRIVE_CHECK
114
jmp 1f /* grub-setup may overwrite this jump */
121
* ljmp to the next instruction because some bogus BIOSes
122
* jump to 07C0:0000 instead of 0000:7C00.
128
/* set up %ds and %ss as offset from 0 */
133
/* set up the REAL stack */
134
movw $GRUB_BOOT_MACHINE_STACK_SEG, %sp
136
sti /* we're safe again */
139
* Check if we have a forced disk reference here
146
/* save drive reference first thing! */
149
/* print a notification message on the screen */
150
MSG(notification_string)
152
/* set %si to the disk address packet */
153
movw $disk_address_packet, %si
155
/* do not probe LBA if the drive is a floppy */
156
testb $GRUB_BOOT_MACHINE_BIOS_HD_FLAG, %dl
159
/* check if LBA is supported */
165
* %dl may have been clobbered by INT 13, AH=41H.
166
* This happens, for example, with AST BIOS 1.04.
171
/* use CHS if fails */
184
/* set the mode to non-zero */
190
/* the size and the reserved byte */
193
/* the absolute address */
194
movl kernel_sector, %ebx
196
movl kernel_sector + 4, %ebx
199
/* the segment of buffer address */
200
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, 6(%si)
203
* BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
204
* Call with %ah = 0x42
206
* %ds:%si = segment:offset of disk address packet
208
* %al = 0x0 on success; err code on failure
214
/* LBA read is not supported, so fallback to CHS. */
217
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
218
jmp LOCAL(copy_buffer)
222
* Determine the hard disk geometry from the BIOS!
223
* We do this first, so that LS-120 IDE floppies work correctly.
227
jnc LOCAL(final_init)
230
* The call failed, so maybe use the floppy probe instead.
232
testb $GRUB_BOOT_MACHINE_BIOS_HD_FLAG, %dl
233
jz LOCAL(floppy_probe)
235
/* Nope, we definitely have a hard disk, and we're screwed. */
236
jmp LOCAL(hd_probe_error)
239
/* set the mode to zero */
243
/* save number of heads */
252
/* save number of cylinders */
259
/* save number of sectors */
263
/* load logical sector start (top half) */
264
movl kernel_sector + 4, %eax
267
jnz LOCAL(geometry_error)
269
/* load logical sector start (bottom half) */
270
movl kernel_sector, %eax
275
/* divide by number of sectors */
278
/* save sector start */
281
xorw %dx, %dx /* zero %edx */
282
divl 4(%si) /* divide by number of heads */
284
/* do we need too many cylinders? */
286
jge LOCAL(geometry_error)
288
/* normalize sector start (1-based) */
291
/* low bits of cylinder start */
294
/* high bits of cylinder start */
299
/* save head start */
309
* BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
310
* Call with %ah = 0x2
311
* %al = number of sectors
313
* %cl = sector (bits 6-7 are high bits of "cylinder")
315
* %dl = drive (0x80 for hard disk, 0x0 for floppy disk)
316
* %es:%bx = segment:offset of buffer
318
* %al = 0x0 on success; err code on failure
321
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
322
movw %bx, %es /* load %es segment with disk buffer */
324
xorw %bx, %bx /* %bx = 0, put it at 0 in the segment */
325
movw $0x0201, %ax /* function 2 */
334
* We need to save %cx and %si because the startup code in
335
* kernel uses them without initializing them.
343
movw $GRUB_BOOT_MACHINE_KERNEL_ADDR, %di
355
jmp *(kernel_address)
357
/* END OF MAIN LOOP */
360
* BIOS Geometry translation error (past the end of the disk geometry!).
362
LOCAL(geometry_error):
363
MSG(geometry_error_string)
364
jmp LOCAL(general_error)
367
* Disk probe failure.
369
LOCAL(hd_probe_error):
370
MSG(hd_probe_error_string)
371
jmp LOCAL(general_error)
374
* Read error on the disk.
377
MSG(read_error_string)
379
LOCAL(general_error):
380
MSG(general_error_string)
382
/* go here when you need to stop the machine hard after an error condition */
383
/* tell the BIOS a boot failure, which may result in no effect */
388
notification_string: .asciz "GRUB "
389
geometry_error_string: .asciz "Geom"
390
hd_probe_error_string: .asciz "Hard Disk"
391
read_error_string: .asciz "Read"
392
general_error_string: .asciz " Error\r\n"
395
* message: write the string pointed to by %si
397
* WARNING: trashes %si, %ax, and %bx
401
* Use BIOS "int 10H Function 0Eh" to write character in teletype mode
402
* %ah = 0xe %al = character
403
* %bh = page %bl = foreground color (graphics modes)
408
int $0x10 /* display a byte */
412
jne 1b /* if not end of string, jmp to display */
416
* Windows NT breaks compatibility by embedding a magic
420
. = _start + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
426
* This is where an MBR would go if on a hard disk. The code
427
* here isn't even referenced unless we're on a floppy. Kinda
431
. = _start + GRUB_BOOT_MACHINE_PART_START
435
.byte 36, 18, 15, 9, 0
439
* Perform floppy probe.
442
movw $probe_values - 1, %si
445
/* reset floppy controller INT 13h AH=0 */
452
/* if number of sectors is 0, display error and die */
457
* Floppy disk probe failure.
459
MSG(fd_probe_error_string)
460
jmp LOCAL(general_error)
463
fd_probe_error_string: .asciz "Floppy"
467
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
473
/* if error, jump to "LOCAL(probe_loop)" */
476
/* %cl is already the correct value! */
480
jmp LOCAL(final_init)
482
. = _start + GRUB_BOOT_MACHINE_PART_END
484
/* the last 2 bytes in the sector 0 contain the signature */
485
.word GRUB_BOOT_MACHINE_SIGNATURE