How do I write to hard disk?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

How do I write to hard disk?

Post by Caffeine »

Hi,

I am trying to implement a FAT32 file system into my OS, but have a few questions on how to implement it.

How do I write to my hard drive and how do I find out where on the hard drive the filesystem goes? Is there a way by using the grub mbi specificatons?

Thanks.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How do I write to hard disk?

Post by iansjack »

That's a big question.

If you are using a VM, rather than a real computer, you might like to look here: https://wiki.osdev.org/ATA_PIO_Mode It's fairly simple to write a basic ATA driver to read and write sectors on a hard disk. The filesystem goes on a partition on the disk; the partition table in sector 0 of the disk lists partitions and where to find them on the disk. It's easier, at fist, to use your host operating system to create the partition and filesystem on your disk rather than writing the code for yourself.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: How do I write to hard disk?

Post by Caffeine »

iansjack wrote:That's a big question.

If you are using a VM, rather than a real computer, you might like to look here: https://wiki.osdev.org/ATA_PIO_Mode It's fairly simple to write a basic ATA driver to read and write sectors on a hard disk. The filesystem goes on a partition on the disk; the partition table in sector 0 of the disk lists partitions and where to find them on the disk. It's easier, at fist, to use your host operating system to create the partition and filesystem on your disk rather than writing the code for yourself.
Thanks! I'll check out the page.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: How do I write to hard disk?

Post by Octocontrabass »

Multiboot can tell you which partition your kernel was loaded from. You'll have to parse the partition tables yourself, though.

Multiboot can't tell you exactly which device it loaded your kernel from. The best it can give you is the BIOS device number, and you're on your own to figure out how that translates into an actual storage device. Any reasonably modern PC supports EDD 3.0, which is the most accurate way to identify the device, but it's a BIOS call that requires you to either switch back to real mode or implement virtual 8086 mode.
Caffeine
Member
Member
Posts: 79
Joined: Mon Nov 15, 2021 9:48 pm

Re: How do I write to hard disk?

Post by Caffeine »

Octocontrabass wrote:Multiboot can tell you which partition your kernel was loaded from. You'll have to parse the partition tables yourself, though.

Multiboot can't tell you exactly which device it loaded your kernel from. The best it can give you is the BIOS device number, and you're on your own to figure out how that translates into an actual storage device. Any reasonably modern PC supports EDD 3.0, which is the most accurate way to identify the device, but it's a BIOS call that requires you to either switch back to real mode or implement virtual 8086 mode.
Thanks!
Post Reply