Page 1 of 1

Writing HDD Boot Sector

Posted: Mon Dec 10, 2007 2:03 pm
by bgpros6
I've been looking and searching for any clue of how to write a HDD boot sector and i read that the HDD boot sector is way longer then a floppy one..... I was wondering if anybody has some sort of tutorial to write one becasue there are none on the internet that i can find.

THX :D

Posted: Tue Dec 11, 2007 2:42 am
by bewing
I started writing up a tutorial on it, but I didn't complete it. If you want more info than what I write here, I can email you my incomplete tutorial.

There are TWO "boot sectors" on a HDD.
The first is called the MBR. It is the very first sector on the disk (LBA 0, or "cyl0, head0, sect1"). It is technically 512 bytes long. But the last two bytes must be 0x55 0xAA for the BIOS to recognize it as a valid MBR. The 64 bytes before that are 4 16byte partition table entries. Before that are (usually) 10 reserved bytes for an OS defined disk ID number. The first four hundred and something bytes are a program that you get to write. It runs in real mode. The BIOS loads it at physical memory location 0x7c00 -- that is, a little below 32k. The function of the MBR is to scan the partition table, locate the bootable partition, and load the beginning of that bootable partition as a "boot sector" (described next). I personally say that it is smartest for the MBR to load approximately 8K of "boot sector" and to load it at a memory address of 0x500 -- but my suggestion is completely non-standard. Typically, stupid MBRs only load 512 bytes of boot sector, and load it at address 0x7c00 (YES, this IS exactly the same address where the MBR gets loaded -- usually the MBR must relocate itself before loading the partition's boot sector). You MUST pass to the boot sector program the "disk number" in DL -- but this is usually automatic. DOS/Windoze (and probably other OSes) also expect a pointer to the partition table entry that got booted to be passed in SI.
It is possible to write the MBR so that it can interact with the user to boot multiple OSes off multiple partitions of multiple drives (not just the bootable partition of the "C" drive). This is called "Dual Booting".

As I said, the thing that is really called a "boot sector" on an HD is the OS-specific partition-specific program that is stored at the beginning of each bootable partition on the disk. If you control the MBR, then you can have it load as much boot sector as you want. If you don't control the MBR, then you can probably expect that only the first 512 bytes of the boot sector will be loaded by the MBR, and the boot sector will have to load the rest of itself, by itself. Also, the MBR may require that the first 512b of the boot sector end with 0x55 0xAA, and as I said, it is typically loaded at 0x7c00. This is stupid, since it breaks up a very important piece of contiguous memory, on the "first memory page" (the first 64K of memory). The first K is untouchable at boot time, because it contains the BIOS interrupt table. The next 256 bytes (from 0x400 to 0x4ff) are used by the BIOS also, and the BIOS trashes bytes in that area continuously. All the rest of memory from 0x500 to 0x7ffff is usable. Almost 512K. There may be up to another 128K available, but it depends on the size of the EBDA (a very long story in itself).