我之前有篇文章有些利用第三方工具包來擷取AHCI base address,這次我要講的是利用WinRing0這個開源dll來完成這個功能。
請看WinRing0API的說明:
Support Functions for PCI Access
C++ (OlsDef.h) *Macro
// Bus Number, Device Number and Function Number to PCI Device Address#define PciBusDevFunc(Bus, Dev, Func) ((Bus&0xFF)<<8) | ((Dev&0x1F)<<3) | (Func&7)
// PCI Device Address to Bus Number
#define PciGetBus(address) ((address>>8) & 0xFF)
// PCI Device Address to Device Number#define PciGetDev(address) ((address>>3) & 0x1F)
// PCI Device Address to Function Number
#define PciGetFunc(address) (address&7)C# (OpenLibSys.cs)
// Bus Number, Device Number and Function Number to PCI Device Addresspublic uint PciBusDevFunc(uint bus, uint dev, uint func)
{
return ((bus&0xFF)<<8) | ((dev&0x1F)<<3) | (func&7);
}
// PCI Device Address to Bus Numberpublic uint PciGetBus(uint address)
{
return ((address>>8) & 0xFF);
}
// PCI Device Address to Device Numberpublic uint PciGetDev(uint address)
{
return ((address>>3) & 0x1F);
}
// PCI Device Address to Function Numberpublic uint PciGetFunc(uint address)
{
return (address&7);
}PCI Device Address
bit description
0- 2 Function Number
3- 7 Device Number
8-15 PCI Bus Number
16-31 Reserved
Requirements
WinRing0 1.0 or later
啦啦啦 看到了吧 我們可以用OpenLibSys.cs中現成的
PciBusDevFunc(uint bus, uint dev, uint func)
方法,就可以得到AHCI base address啦!有木有很激動?!
附上調用的代碼
/**getAHCIbaseAddress**/
public string getAHCIbaseAddress()
{
uint address = ols.PciBusDevFunc(0x00, 0x1F, 0x02);
//add by kelsey
string ahciBaseAddress = "";
// Host Bridge
if (address != 0xFFFFFFFF)
{
for (int i = 0; i < 256; i += 16)
{
//str += i.ToString("X2") + "|";
for (int j = 0; j < 16; j++)
{
if (i == 32 && j == 4)
{
ahciBaseAddress = (ols.ReadPciConfigDword(address, (byte)(i + j))).ToString("X2");
break;
}
}
}
Console.WriteLine("ahciBaseAddress ==" + ahciBaseAddress);
}
return ahciBaseAddress;
}