OSDev.org
https://forum.osdev.org/

how to get device io port or PCI numbers
https://forum.osdev.org/viewtopic.php?f=1&t=32650
Page 1 of 1

Author:  zq [ Tue Dec 26, 2017 5:01 pm ]
Post subject:  how to get device io port or PCI numbers

In real mode, I read/write a disk via int 13h, but in protected mode, we can only access it by io, of cource I can enum all the storage devices and check out which is the disk I boot from.

However, I would like to have a easier way to config out the io port or PCI bus&device$fuction number of the disk in real mode, so that after I enter protected mode, I can access it directly by it.

Author:  Octocontrabass [ Tue Dec 26, 2017 6:12 pm ]
Post subject:  Re: how to get device io port or PCI numbers

If the BIOS supports it, you can find that information using INT 0x13 AH=0x48. Here's a link to the specification.

If the BIOS doesn't support it, you have to enumerate all of the storage devices to find the boot disk.

Author:  zq [ Wed Dec 27, 2017 6:14 am ]
Post subject:  Re: how to get device io port or PCI numbers

Octocontrabass wrote:
If the BIOS supports it, you can find that information using INT 0x13 AH=0x48. Here's a link to the specification.

If the BIOS doesn't support it, you have to enumerate all of the storage devices to find the boot disk.


Thanks for your help, I tried with qemu and unfourtuantly it does not support EDD, maybe I need to deal with the enumeration.

Author:  Brendan [ Wed Dec 27, 2017 7:57 pm ]
Post subject:  Re: how to get device io port or PCI numbers

Hi,

zq wrote:
Octocontrabass wrote:
If the BIOS doesn't support it, you have to enumerate all of the storage devices to find the boot disk.


Thanks for your help, I tried with qemu and unfourtuantly it does not support EDD, maybe I need to deal with the enumeration.


In this case there's multiple scenarios where it's impossible for the OS to figure out what it booted from.

For a simple example, assume the user has eight USB flash devices plugged into a USB hub where all of the flash devices are identical clones and have a "write protect" switch set.

For a more bizarre example (that you can expect to fail even if EDD is fully supported), imagine if the user boots a hard disk image from network using Syslinux MEMDISK (which downloads a disk image into RAM, then hooks "int 0x13" to make the RAM look like an actual disk).


Cheers,

Brendan

Author:  BrightLight [ Fri Jan 05, 2018 3:50 am ]
Post subject:  Re: how to get device io port or PCI numbers

Just to give a more complete answer, there are mainly two ways of detecting the boot drive. First, you can have a kernel parameter that instructs it which drive to use. Or next, you can scan for all devices and search for boot files or other "magic" combinations that mark your boot device.

Scanning for all devices is not that hard. One way or another, you will have to detect all drives anyway. So you can scan the PCI bus for IDE/AHCI controllers, and then scan each IDE/AHCI controller for ATA/SATA/ATAPI/SATAPI devices. The same goes for the USB; you'd find USB controllers, scan each controller for USB hubs, and scan each USB hub for mass storage devices.

For Syslinux Memdisk, you're sure that if it exists, it is already the boot device. To detect Syslinux Memdisk (as I do in my code), you use BIOS INT 0x13 function 0x48 to get drive size. Then use the BIOS E820 memory map, and scan for a memory range that is the same size as the boot device, and is marked as "reserved." The start of that memory area represents the start of the memory-mapped disk. This works for Syslinux 4.07 at least, I haven't tried other versions, but probably works too.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/