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)
30
#define ERR(x) movw $x, %si; jmp LOCAL(error_message)
36
/* Tell GAS to generate 16-bit instructions so that this code works
44
* _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00
48
* Beginning of the sector is compatible with the FAT/HPFS BIOS
53
nop /* do I care about this ??? */
56
* This space is for the BIOS parameter block!!!! Don't change
57
* the first jump, nor start the code anywhere but right after
61
. = _start + GRUB_BOOT_MACHINE_BPB_START
82
. = _start + GRUB_BOOT_MACHINE_BPB_END
85
* End of BIOS parameter block.
89
.word GRUB_BOOT_MACHINE_KERNEL_ADDR
91
. = _start + GRUB_BOOT_MACHINE_KERNEL_SECTOR
95
. = _start + GRUB_BOOT_MACHINE_BOOT_DRIVE
97
.byte 0xff /* the disk to load kernel from */
98
/* 0xff means use the boot drive */
103
cli /* we're not safe here! */
106
* This is a workaround for buggy BIOSes which don't pass boot
107
* drive correctly. If GRUB is installed into a HDD, check if
108
* DL is masked correctly. If not, assume that the BIOS passed
109
* a bogus value and set DL to 0x80, since this is the only
110
* possible boot drive. If GRUB is installed into a floppy,
111
* this does nothing (only jump).
113
. = _start + GRUB_BOOT_MACHINE_DRIVE_CHECK
115
jmp 3f /* grub-setup may overwrite this jump */
119
/* Ignore %dl different from 0-0x0f and 0x80-0x8f. */
126
* ljmp to the next instruction because some bogus BIOSes
127
* jump to 07C0:0000 instead of 0000:7C00.
133
/* set up %ds and %ss as offset from 0 */
138
/* set up the REAL stack */
139
movw $GRUB_BOOT_MACHINE_STACK_SEG, %sp
141
sti /* we're safe again */
144
* Check if we have a forced disk reference here
151
/* save drive reference first thing! */
154
/* print a notification message on the screen */
155
MSG(notification_string)
157
/* set %si to the disk address packet */
158
movw $disk_address_packet, %si
160
/* check if LBA is supported */
166
* %dl may have been clobbered by INT 13, AH=41H.
167
* This happens, for example, with AST BIOS 1.04.
172
/* use CHS if fails */
185
/* set the mode to non-zero */
191
/* the size and the reserved byte */
194
/* the absolute address */
195
movl kernel_sector, %ebx
197
movl kernel_sector + 4, %ebx
200
/* the segment of buffer address */
201
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, 6(%si)
204
* BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
205
* Call with %ah = 0x42
207
* %ds:%si = segment:offset of disk address packet
209
* %al = 0x0 on success; err code on failure
215
/* LBA read is not supported, so fallback to CHS. */
218
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
219
jmp LOCAL(copy_buffer)
223
* Determine the hard disk geometry from the BIOS!
224
* We do this first, so that LS-120 IDE floppies work correctly.
228
jnc LOCAL(final_init)
231
* The call failed, so maybe use the floppy probe instead.
233
testb $GRUB_BOOT_MACHINE_BIOS_HD_FLAG, %dl
234
jz LOCAL(floppy_probe)
236
/* Nope, we definitely have a hard disk, and we're screwed. */
237
ERR(hd_probe_error_string)
240
/* set the mode to zero */
244
/* save number of heads */
253
/* save number of cylinders */
260
/* save number of sectors */
264
/* load logical sector start (top half) */
265
movl kernel_sector + 4, %eax
268
jnz LOCAL(geometry_error)
270
/* load logical sector start (bottom half) */
271
movl kernel_sector, %eax
276
/* divide by number of sectors */
279
/* save sector start */
282
xorw %dx, %dx /* zero %edx */
283
divl 4(%si) /* divide by number of heads */
285
/* do we need too many cylinders? */
287
jge LOCAL(geometry_error)
289
/* normalize sector start (1-based) */
292
/* low bits of cylinder start */
295
/* high bits of cylinder start */
300
/* save head start */
310
* BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
311
* Call with %ah = 0x2
312
* %al = number of sectors
314
* %cl = sector (bits 6-7 are high bits of "cylinder")
316
* %dl = drive (0x80 for hard disk, 0x0 for floppy disk)
317
* %es:%bx = segment:offset of buffer
319
* %al = 0x0 on success; err code on failure
322
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
323
movw %bx, %es /* load %es segment with disk buffer */
325
xorw %bx, %bx /* %bx = 0, put it at 0 in the segment */
326
movw $0x0201, %ax /* function 2 */
335
* We need to save %cx and %si because the startup code in
336
* kernel uses them without initializing them.
344
movw $GRUB_BOOT_MACHINE_KERNEL_ADDR, %di
356
jmp *(kernel_address)
358
/* END OF MAIN LOOP */
361
* BIOS Geometry translation error (past the end of the disk geometry!).
363
LOCAL(geometry_error):
364
ERR(geometry_error_string)
367
* Read error on the disk.
370
movw $read_error_string, %si
371
LOCAL(error_message):
373
LOCAL(general_error):
374
MSG(general_error_string)
376
/* go here when you need to stop the machine hard after an error condition */
377
/* tell the BIOS a boot failure, which may result in no effect */
382
notification_string: .asciz "GRUB "
383
geometry_error_string: .asciz "Geom"
384
hd_probe_error_string: .asciz "Hard Disk"
385
read_error_string: .asciz "Read"
386
general_error_string: .asciz " Error\r\n"
389
* message: write the string pointed to by %si
391
* WARNING: trashes %si, %ax, and %bx
395
* Use BIOS "int 10H Function 0Eh" to write character in teletype mode
396
* %ah = 0xe %al = character
397
* %bh = page %bl = foreground color (graphics modes)
402
int $0x10 /* display a byte */
406
jne 1b /* if not end of string, jmp to display */
410
* Windows NT breaks compatibility by embedding a magic
414
. = _start + GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
420
* This is where an MBR would go if on a hard disk. The code
421
* here isn't even referenced unless we're on a floppy. Kinda
425
. = _start + GRUB_BOOT_MACHINE_PART_START
429
.byte 36, 18, 15, 9, 0
433
* Perform floppy probe.
436
movw $probe_values - 1, %si
439
/* reset floppy controller INT 13h AH=0 */
446
/* if number of sectors is 0, display error and die */
451
* Floppy disk probe failure.
453
MSG(fd_probe_error_string)
454
jmp LOCAL(general_error)
457
fd_probe_error_string: .asciz "Floppy"
461
movw $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
467
/* if error, jump to "LOCAL(probe_loop)" */
470
/* %cl is already the correct value! */
474
jmp LOCAL(final_init)
476
. = _start + GRUB_BOOT_MACHINE_PART_END
478
/* the last 2 bytes in the sector 0 contain the signature */
479
.word GRUB_BOOT_MACHINE_SIGNATURE