How to read the disk in 64 bit mode?

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
Cyao
Member
Member
Posts: 78
Joined: Tue Jun 07, 2022 11:23 am
Libera.chat IRC: Cyao
Location: France
Contact:

How to read the disk in 64 bit mode?

Post 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!
tretornesp
Posts: 18
Joined: Mon Aug 15, 2022 12:30 pm

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

Post 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.
Cyao
Member
Member
Posts: 78
Joined: Tue Jun 07, 2022 11:23 am
Libera.chat IRC: Cyao
Location: France
Contact:

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

Post by Cyao »

Oh that's a lot harder then I expected, gonna still use the BIOS atm, but thanks a lot for the explanation!
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

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

Post 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.
Cyao
Member
Member
Posts: 78
Joined: Tue Jun 07, 2022 11:23 am
Libera.chat IRC: Cyao
Location: France
Contact:

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

Post 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
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post by iansjack »

A simple ATA PIO mode driver will do the job: https://wiki.osdev.org/ATA_PIO_Mode
Cyao
Member
Member
Posts: 78
Joined: Tue Jun 07, 2022 11:23 am
Libera.chat IRC: Cyao
Location: France
Contact:

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

Post by Cyao »

Ok thanks!
Post Reply