Page 1 of 1

How to read the disk in 64 bit mode?

Posted: Sat Sep 10, 2022 3:11 am
by Cyao
I have only read the disk from real mode using bios before, and now I want to read the hard disk from 64 bit mode.

I looked on some wiki pages and got very confused on how the disk reading stuff works

Does anyone got some recommendations on where I should start? Thanks in advance!

Re: How to read the disk in 64 bit mode?

Posted: Sat Sep 10, 2022 4:37 am
by tretornesp
You can try to create a simple SATA device driver.

The process is kind of complex and it is way harder to explain than to
show, so i'll attach some code for you.

First, find the devices. You will need to obtain the RSDP. (i get it from limine services)

Having the RSDP, you will have to find the MCFG header, required for iterating PCI bus.

Related code for this is:

https://github.com/TretornESP/bloodmoon ... dev/acpi.c function

Code: Select all

init_acpi
.
(get_rsdp_address() is a function that talks to limine, replace it with whatever
you like).

Then enumerate PCI devices
https://github.com/TretornESP/bloodmoon ... /dev/pci.c

Code: Select all

function enumerate_pci(
)
and search for class: 1, subclass: 6 and program interface: 1 wich are AHCI devices.

https://github.com/TretornESP/bloodmoon ... /devices.c function

Code: Select all

register_devices()
For each one, initialice the ahci device.

https://github.com/TretornESP/bloodmoon ... dev/ahci.c function

Code: Select all

init_ahci()
And store some reference to each available port categorizing it as SATA or ATAPI.

Now when you call your read function, you need to select the desired target device and use the function:

https://github.com/TretornESP/bloodmoon ... dev/ahci.c function

Code: Select all

read_port()
I'm sorry but it is a really lenghty process and i don't really know how to explain it better. Hope this helps!

Have a nice day.

Re: How to read the disk in 64 bit mode?

Posted: Sat Sep 10, 2022 6:47 am
by Cyao
Oh that's a lot harder then I expected, gonna still use the BIOS atm, but thanks a lot for the explanation!

Re: How to read the disk in 64 bit mode?

Posted: Sat Sep 10, 2022 2:39 pm
by Octocontrabass
What kind of hard disk do you want to access? If you're trying to read a disk in a virtual machine, you can make things easier by changing how the VM is configured. You don't need to support PCIe or AHCI if your VM isn't configured to emulate those.

Re: How to read the disk in 64 bit mode?

Posted: Sun Sep 11, 2022 2:33 am
by Cyao
Im just trying to access the boot disk (using -drive file=boot.iso,format=raw,media=disk on qemu) for reading and writing some data

Re: How to read the disk in 64 bit mode?

Posted: Sun Sep 11, 2022 4:25 am
by iansjack
A simple ATA PIO mode driver will do the job: https://wiki.osdev.org/ATA_PIO_Mode

Re: How to read the disk in 64 bit mode?

Posted: Sun Sep 11, 2022 8:47 am
by Cyao
Ok thanks!