hard disk formatting

OS Design, Theory, and Programming

Moderator:Moderators

Post Reply
xixpsychoxix
Posts:59
Joined:Tue Oct 13, 2009 8:49 pm
hard disk formatting

Post by xixpsychoxix » Mon Mar 15, 2010 10:53 pm

I don't know if you were planning on covering hard disks, but i am trying to add the capability to use hard disks from my operating system and I'm not sure quite where to begin. I've found a very good site that does a good job of explaining FAT16 (this is what i want to use because it's simple, sort of...) but I'm not really sure how to put the theory into practice. anyone know of a good place to learn stuff about working with hard disks? Basically i need to learn how to format a hard disk, how the actual data that describes the format works. One of the major problems I have is how the MBR works. Is this where my stage1 loader would go, or does it go in the parameter block for the partition? It's all sort of confusing because the document was written for low-level programming, not necessarily from an operating system standpoint. any advice? (i like to ramble sry!)

User avatar
Mike
Site Admin
Posts:465
Joined:Sat Oct 20, 2007 7:58 pm
Contact:

Re: hard disk formatting

Post by Mike » Tue Mar 16, 2010 9:14 pm

Hello,

Typically, there are two "Stage 1" boot loaders on HDDs. One that is loaded by the BIOS (MBR Bootstrap) and one that the MBR loads (The OS's bootstrap.)

The BIOS loads the 1st sector of the HDD (The MBR) to 0x7c00 and executes it if the boot signature (0xaa55) is valid. The MBR bootstrap program reads the partition table (also located in the MBR) and gets the first sector of the active partition. It relocates itself to another address, and loads that sector to 0x7c00. It checks this sectors boot signature and executes it, passing in the drive number that was passed to it from the BIOS in DL. From here, the OS's bootstrap program takes control and continues through that OS's specific boot process.

The MBR bootstrap is just a flat binary real mode program loaded at 0x7c00, with the partition table located at 0:0x07be. You can create this in NASM like this:

Code: Select all

;your boot code

times 0x1be-($-$$) db 0

; partition table must be at 0:0x07be
; there are only 4 entries in the table, or 64 bytes

partition_table:

%rep MBR_NUM_ENTRIES
   istruc mbr_entry
      at mbr_entry.m_boot,		db 0
      at mbr_entry.m_startHead,	db 0
      at mbr_entry.m_startSect,	dw 0
      at mbr_entry.m_id,		db 0
      at mbr_entry.m_endHead,	db 0
      at mbr_entry.m_endSect,	dw 0
      at mbr_entry.m_partSect,	dd 0
      at mbr_entry.m_partSize,	dd 0
   iend
%endrep
dw BOOT_SIG
where BOOT_SIG is the boot signature (0xaa55) and MBR_NUM_ENTRIES is 4. If the m_boot entry in your MBR is 0x80, then it is an active partition. Read the sector at startSect to 0x7c00 and execute it.

There is no "parameter block". Technically the OS's bootstrap loader may not even contain one, the BPB is a filesystem detail, its never actually needed depending on the filesystem.

So, basically the BIOS loads and executes the MBR bootstrap. The MBR bootstrap reads and loads an active partition containing your FAT16 formatted partition and OS bootstrap code. Your FAT16 bootstrap code contains the BPB and loads your OS.
Lead Programmer for BrokenThorn Entertainment, Co.
Website: http://www.brokenthorn.com
Email: webmaster@brokenthorn.com

Post Reply