GRUB HOW-TOs:
GRUB understands the FAT (DOS/Win) filesystems, ext2 and Reiserfs filesystems (Linux), and FFS (BSD). GRUB will use a configuration file if it can find one. The config file can specify automatic loading or display a boot menu. GRUB also has a command-line shell, which lets you configure and install it, or load a kernel 'by hand'. It supports loading a kernel as a group of modules, e.g. monolithic kernel plus compressed RAM disk image, or microkernel plus vital servers.This loader starts up with a command-line shell similar to DOS, which lets you navigate a FAT12 or FAT16 filesystem, list directories, and chose a kernel file to be loaded. It can load DOS .COM and .EXE files, and DJGPP COFF files, and it builds with the freely-available Turbo C. If memory below 1 meg + 64K is left untouched, you can return to the loader to chose a new kernel to boot.
This boot code is no longer present in kernel version 2.6.
Your boot code should initialize the following registers:
The most common disk sector size is 512 bytes. The code loaded by the BIOS must be smaller than this, so you will probably need to write this code in asm. If you can't do everything you want in 512 bytes, you need a two-stage bootloader.
offset | size | description |
---|---|---|
0 | 1 byte | partition flag byte (0=not bootable, 80h=bootable, or 'active') |
1 | 1 | partition start head (H) |
2 | 1 | b7:6 = b9:8 of partition start cylinder (C) b5:0 = partition start sector (S) |
3 | 1 | b7:0 of partition start C |
4 | 1 | OS/filesystem type indicator byte |
5 | 1 | partition end H |
6 | 1 | b7:6 = b9:8 of partition end C b5:0 = partition end S |
7 | 1 | b7:0 of partition end C |
8 | 4 bytes | 32-bit LBA first sector of partition |
12 | 4 | 32-bit LBA number of sectors in partition |
Under both DOS and Linux, the utility that creates or modifies the partition table is called fdisk. Under DOS, the undocumented command FDISK /MBR will (re-)install the DOS MBR code, leaving the partition table untouched. This is useful is you mess up, or if you want to un-install a non-DOS bootloader such as LILO. If your BIOS has 'boot sector virus protection', it may interfere with attempts to install or modify the MBR using DOS FDISK.
No MBR is involved if the system boots from a floppy disk. Floppies do not have partitions or MBRs; only a boot sector. It is also possible to have a hard drive without an MBR, but this is uncommon.
Commented dis-assembly of DOS 7.0 (Win95a) MBR. This MBR uses CHS disk functions only.
mov dx,3F2h mov al,0 out dx,alGet memory size with BIOS calls. Because of problems with CMOS and direct probing, this should be the preferred method of getting memory sizes on the PC.
Get memory size from CMOS:
; read extended memory size to AX ; won't report more than 63.999 meg (65535/1024) of extended memory mov al,18h out 70h,al in al,71h mov ah,al mov al,17h out 70h,al in al,71h mov [_ext_mem],ax ; in K ; read conventional memory size to AX mov al,16h out 70h,al in al,71h mov ah,al mov al,15h out 70h,al in al,71h mov [_conv_mem],ax ; in KINT 13h AH=08h gives unreliable sectors-per-track values for floppy disks. The FAT filesystems store the sectors-per-track value in the BIOS parameter block (BPB). For other filesystems, either assume a value (e.g. 18 sectors per track for 1.44 meg floppies), or use direct probing:
Real mode code to check for 32-bit CPU:
pushf pushf pop bx ; old FLAGS -> BX mov ax,bx xor ah,70h ; try changing b14 (NT)... push ax ; ... or b13:b12 (IOPL) popf pushf pop ax ; new FLAGS -> AX popf xor ah,bh ; 32-bit CPU if we changed NT... and ah,70h ; ...or IOPL jne is_32bit_cpuCheck for Virtual 8086 mode (Windows DOS box or EMM386 loaded):
smsw ax ; smsw is a 286+ instruction; V86 mode is a 386+ feature test al,1 ; look at PE bit of MSW (CR0) jne in_v86_modeYou can't switch directly to pmode from V86 mode, because the LGDT and LIDT instructions, and writing the CR0 register (MSW) are privileged operations. If the system is in V86 mode because of a memory manager like EMM386, VCPI can be used to switch to paged protected mode:
NASM code to enable A20 and verify it's on. Your boot code should use INT 15h AH=89h (enter pmode) and INT 15h AH=87h (copy to extended memory) to control A20. Use code such as this only if these BIOS calls fail.
A FAT 12 bootsector. Finds, loads, and runs a 16-bit binary file that is stored in the root directory. Includes graphical test program.
Other bootloaders with source code:
http://www.nondot.org/sabre/os/articles/TheBootProcess/
Hale Landis' site: http://www.ata-atapi.com
Floppy and IDE sectors are almost always 512 bytes, but SCSI devices may have larger sectors. I don't know how (or if) DOS can boot from a device with larger sectors.) - can you have multiple MBRs? to boot from an extended partition? - Network booting with BOOTP, TFTP, and/or Intel PXE - El Torito CD-ROM boot - Microsoft's new MBR format?