📄 pcibus.c
字号:
PWCHAR DelBuf;
DWORD RegEnum;
PCI_DEV_INFO Info;
PCI_COMMON_CONFIG PciCfg;
DWORD NumValues;
PCI_SLOT_NUMBER SlotNumber;
DWORD Length;
DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!PCIRegClean(%s)\r\n", EnumKey));
// Open key to be cleaned
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, EnumKey, 0, 0, &Key);
if (status != ERROR_SUCCESS) {
DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean RegOpenKeyEx(%s) returned %d.\r\n", EnumKey, status));
return FALSE;
}
// Find out how many sub-keys there are
status = RegQueryInfoKey(Key, NULL, NULL, NULL, &NumDevKeys, &LongSubKey, NULL, NULL, NULL, NULL, NULL, NULL);
if (status != ERROR_SUCCESS) {
DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean RegQueryInfoKey(Class) returned %d.\r\n", status));
RegCloseKey(Key);
return FALSE;
}
DEBUGMSG(ZONE_ENUM, (L"PCIBUS!PCIRegClean %d devices to check\r\n", NumDevKeys));
// Allocate buffer for keys to delete
LongSubKey++;
if (!(DelBuf = (PWCHAR)LocalAlloc(LPTR, (NumDevKeys * LongSubKey) * sizeof(WCHAR)))) {
DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean LocalAlloc failed.\r\n"));
RegCloseKey(Key);
return FALSE;
}
//
// Read the device list from the registry and check to make sure a device exists for each one.
//
for (RegEnum = 0; RegEnum < NumDevKeys; RegEnum++) {
// Get sub-key according to RegEnum
ValLen = LongSubKey;
status = RegEnumKeyEx(Key, RegEnum, &DelBuf[RegEnum * LongSubKey], &ValLen, NULL, NULL, NULL, NULL);
if (status != ERROR_SUCCESS) {
DEBUGMSG(ZONE_ERROR, (L"PCIBUS!PCIRegClean RegEnumKeyEx(%d) returned %d\r\n", RegEnum, status));
LocalFree(DelBuf);
RegCloseKey(Key);
return FALSE;
}
// Get information about device in registry
wcscpy(Info.RegPath, EnumKey);
wcscat(Info.RegPath, TEXT("\\"));
wcscat(Info.RegPath, &DelBuf[RegEnum * LongSubKey]);
NumValues = RegReadValues(Key, &DelBuf[RegEnum * LongSubKey], NULL, NULL,
PCIBUS_BUSNUMBER_VALNAME, PCIBUS_BUSNUMBER_VALTYPE, 0, &Info.Bus, sizeof(Info.Bus),
PCIBUS_DEVICENUMBER_VALNAME, PCIBUS_DEVICENUMBER_VALTYPE, 0, &Info.Device, sizeof(Info.Device),
PCIBUS_FUNCTIONNUMBER_VALNAME, PCIBUS_FUNCTIONNUMBER_VALTYPE, 0, &Info.Function, sizeof(Info.Function),
NULL);
if (NumValues != 3) {
// No device location, skip
DelBuf[RegEnum * LongSubKey] = (WCHAR)'\0';
continue;
}
DEBUGMSG(ZONE_ENUM, (L"PCIBUS!PCIRegClean: Examining '%s' (%d/%d/%d)\r\n", Info.RegPath, Info.Bus, Info.Device, Info.Function));
// Read device information
SlotNumber.u.bits.DeviceNumber = Info.Device;
SlotNumber.u.bits.FunctionNumber = Info.Function;
PciCfg.VendorID = 0xFFFF;
Length = HalGetBusData(PCIConfiguration, Info.Bus, SlotNumber.u.AsULONG, &PciCfg, sizeof(PciCfg) - sizeof(PciCfg.DeviceSpecific));
if (Length == 0 || PciCfg.VendorID == 0xFFFF) {
// Device does not exist, remove registry entry
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!PCIRegClean: No device exists (%d/%d/%d) for key '%s', removing.\r\n",
Info.Bus, Info.Device, Info.Function, Info.RegPath));
continue;
}
Info.Cfg = &PciCfg;
// Match device with registry information
if (RegMatchOne(&Info, Key, &DelBuf[RegEnum * LongSubKey]) != PCIBUS_MATCH_ALL) {
// Not an exact match (i.e. another card is in slot), remove registry entry
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!PCIRegClean: No device match (%d/%d/%d) for key '%s', removing.\r\n",
Info.Bus, Info.Device, Info.Function, Info.RegPath));
continue;
}
// Device matches, go onto next one
DelBuf[RegEnum * LongSubKey] = (WCHAR)'\0';
}
// Remove registry entries that didn't have matching device
//
// Note: this removes everything, including any instance information that was originally set in the
// registry. Unfortunately, even if we tried to keep that information around, if the device that
// was removed was again inserted, it wouldn't be the same instance. Effectively this instance is
// lost.
for (RegEnum = 0; RegEnum < NumDevKeys; RegEnum++) {
if (DelBuf[RegEnum * LongSubKey] != (WCHAR)'\0')
RegDeleteKey(Key, &DelBuf[RegEnum * LongSubKey]);
}
LocalFree(DelBuf);
RegCloseKey(Key);
return TRUE;
} // Enum
//
// Device/registry matching routine.
//
static DWORD
RegMatchOne(
PPCI_DEV_INFO pInfo,
HKEY Key,
LPCWSTR DevName
)
{
HKEY DevKey;
DWORD status;
DWORD ValType;
DWORD ValLen;
DWORD Match = 0;
DWORD BestMatch = 0;
WCHAR RegPath[DEVKEY_LEN];
DWORD ClassVal;
DWORD SubClassVal;
DWORD Val32;
DWORD ProgIFs[PCI_MAX_REG_LIST], ProgIFNum;
DWORD VendorIDs[PCI_MAX_REG_LIST], VendorIDNum;
DWORD DeviceIDs[PCI_MAX_REG_LIST], DeviceIDNum;
DWORD SubVendorIDs[PCI_MAX_REG_LIST], SubVendorIDNum;
DWORD SubSystemIDs[PCI_MAX_REG_LIST], SubSystemIDNum;
DWORD RevisionIDs[PCI_MAX_REG_LIST], RevisionIDNum;
DWORD ListNum[4], MaxListNum;
DWORD i;
DEBUGMSG(ZONE_FUNCTION, (L"PCIBUS!RegMatchOne+(%s, %d, %d, %d)\r\n", pInfo->RegPath, pInfo->Bus, pInfo->Device, pInfo->Function));
// Open sub-key
status = RegOpenKeyEx(Key, DevName, 0, 0, &DevKey);
if (status != ERROR_SUCCESS) {
DEBUGMSG(ZONE_ENUM|ZONE_ERROR, (TEXT("PCIBUS!RegMatchOne RegOpenKeyEx(%s) returned %d\r\n"), DevName, status));
return 0;
}
DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Checking key %s\\%s.\r\n"), pInfo->RegPath, DevName));
//
// Examine Class value, check against device's base class
//
ValLen = sizeof(ClassVal);
status = RegQueryValueEx(DevKey, PCIBUS_CLASS_VALNAME, NULL, &ValType, (PUCHAR)&ClassVal, &ValLen);
if (status != ERROR_SUCCESS) {
// No Class value found
goto RegMatchOne_Exit;
}
if ((UCHAR)ClassVal != pInfo->Cfg->BaseClass) {
// Class doesn't match
Match = 0;
goto RegMatchOne_Exit;
} else {
DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for Class (0x%X) found.\r\n"), ClassVal));
Match++;
}
// There is at least a partial match, construct path to device key
wcscpy(RegPath, pInfo->RegPath);
wcscat(RegPath, TEXT("\\"));
wcscat(RegPath, DevName);
//
// Examine SubClass value, check against device's sub-class
//
ValLen = sizeof(SubClassVal);
status = RegQueryValueEx(DevKey, PCIBUS_SUBCLASS_VALNAME, NULL, &ValType, (PUCHAR)&SubClassVal, &ValLen);
if (status != ERROR_SUCCESS) {
// No SubClass value found
goto RegMatchOne_Exit;
}
if ((UCHAR)SubClassVal != pInfo->Cfg->SubClass) {
// SubClass doesn't match
Match = 0;
goto RegMatchOne_Exit;
} else {
DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for SubClass (0x%X) found.\r\n"), SubClassVal));
Match++;
}
//
// Get ProgIF value or list and check against device's programing interface
//
ProgIFNum = PCI_MAX_REG_LIST;
if (!RegGetList(DevKey, PCIBUS_PROGIF_VALNAME, &ProgIFNum, ProgIFs) && (ProgIFNum == PCI_MAX_REG_LIST)) {
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list. Ignoring the additional items.\r\n",
RegPath, PCIBUS_PROGIF_VALNAME, PCI_MAX_REG_LIST));
}
if (!ProgIFNum) {
// No ProgIF value found
goto RegMatchOne_Exit;
}
for (i = 0; i < ProgIFNum; i++) {
if ((UCHAR)ProgIFs[i] == pInfo->Cfg->ProgIf) break;
}
if (i == ProgIFNum) {
// No match found
Match = 0;
goto RegMatchOne_Exit;
} else {
DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for ProgIF (0x%X) found.\r\n"), ProgIFs[i]));
Match++;
}
//
// Get Vendor ID, Device ID lists and look for match
//
VendorIDNum = PCI_MAX_REG_LIST;
if (!RegGetList(DevKey, PCIBUS_VENDORID_VALNAME, &VendorIDNum, VendorIDs) && (VendorIDNum == PCI_MAX_REG_LIST)) {
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list. Ignoring the additional items.\r\n",
RegPath, PCIBUS_VENDORID_VALNAME, PCI_MAX_REG_LIST));
}
if (!VendorIDNum) {
// No VendorID value found
goto RegMatchOne_Exit;
}
DeviceIDNum = PCI_MAX_REG_LIST;
if (!RegGetList(DevKey, PCIBUS_DEVICEID_VALNAME, &DeviceIDNum, DeviceIDs) && (DeviceIDNum == PCI_MAX_REG_LIST)) {
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list. Ignoring the additional items.\r\n",
RegPath, PCIBUS_DEVICEID_VALNAME, PCI_MAX_REG_LIST));
}
if (!DeviceIDNum) {
// No DeviceID value found
goto RegMatchOne_Exit;
}
if ((pInfo->Cfg->HeaderType & ~PCI_MULTIFUNCTION) == PCI_DEVICE_TYPE) {
// Get Subsystem Vendor ID and Subsystem ID lists only if h/w is a device
SubVendorIDNum = PCI_MAX_REG_LIST;
if (!RegGetList(DevKey, PCIBUS_SUBVENDORID_VALNAME, &SubVendorIDNum, SubVendorIDs) && (VendorIDNum == PCI_MAX_REG_LIST)) {
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list. Ignoring the additional items.\r\n",
RegPath, PCIBUS_SUBVENDORID_VALNAME, PCI_MAX_REG_LIST));
}
SubSystemIDNum = PCI_MAX_REG_LIST;
if (!RegGetList(DevKey, PCIBUS_SUBSYSTEMID_VALNAME, &SubSystemIDNum, SubSystemIDs) && (SubSystemIDNum == PCI_MAX_REG_LIST)) {
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list. Ignoring the additional items.\r\n",
RegPath, PCIBUS_SUBSYSTEMID_VALNAME, PCI_MAX_REG_LIST));
}
if (!SubVendorIDNum && SubSystemIDNum) {
// If the Subsystem ID exists, but not Subsytem Vendor ID, then ignore SubSystem ID
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: If registry value '%s\\%s' exists, then '%s\\%s' must exist. Ignoring.\r\n",
RegPath, PCIBUS_SUBSYSTEMID_VALNAME, RegPath, PCIBUS_SUBVENDORID_VALNAME));
SubSystemIDNum = 0;
}
} else {
SubVendorIDNum = 0;
SubSystemIDNum = 0;
}
// Find smallest list length
ListNum[0] = VendorIDNum;
ListNum[1] = DeviceIDNum;
ListNum[2] = SubVendorIDNum ? SubVendorIDNum : PCI_MAX_REG_LIST;
ListNum[3] = SubSystemIDNum ? SubSystemIDNum : PCI_MAX_REG_LIST;
MaxListNum = MinVal(ListNum, 4);
//
// Adjust list lengths
//
if (VendorIDNum > MaxListNum) {
VendorIDNum = MaxListNum;
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value list '%s\\%s' truncated to %d items.\r\n",
RegPath, PCIBUS_VENDORID_VALNAME, MaxListNum));
}
if (DeviceIDNum > MaxListNum) {
DeviceIDNum = MaxListNum;
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value list '%s\\%s' truncated to %d items.\r\n",
RegPath, PCIBUS_DEVICEID_VALNAME, MaxListNum));
}
if (SubVendorIDNum > MaxListNum) {
SubVendorIDNum = MaxListNum;
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value list '%s\\%s' truncated to %d items.\r\n",
RegPath, PCIBUS_SUBVENDORID_VALNAME, MaxListNum));
}
if (SubSystemIDNum > MaxListNum) {
SubSystemIDNum = MaxListNum;
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value list '%s\\%s' truncated to %d items.\r\n",
RegPath, PCIBUS_SUBSYSTEMID_VALNAME, MaxListNum));
}
// Walk list pairs looking for match
for (i = 0; i < MaxListNum; i++) {
if (((USHORT)VendorIDs[i] == pInfo->Cfg->VendorID) && ((USHORT)DeviceIDs[i] == pInfo->Cfg->DeviceID)) {
if (!SubVendorIDNum) break;
if ((USHORT)SubVendorIDs[i] == pInfo->Cfg->u.type0.SubVendorID) {
if (!SubSystemIDNum) break;
if ((USHORT)SubSystemIDs[i] == pInfo->Cfg->u.type0.SubSystemID) break;
}
}
}
if (i == MaxListNum) {
// No match found
Match = 0;
goto RegMatchOne_Exit;
} else {
DEBUGMSG(ZONE_ENUM,
(TEXT("PCIBUS!RegMatchOne: Match for VendorID (0x%X), DeviceID (0x%X) found.\r\n"), VendorIDs[i], DeviceIDs[i]));
#ifdef DEBUG
if (SubVendorIDNum) {
if (SubSystemIDNum) {
DEBUGMSG(ZONE_ENUM, (L"PCIBUS!RegMatchOne: Match for SubVendorID (0x%X), SubSystemID (0x%X) found.\r\n",
SubVendorIDs[i], SubSystemIDs[i]));
} else {
DEBUGMSG(ZONE_ENUM, (L"PCIBUS!RegMatchOne: Match for SubVendorID (0x%X) found.\r\n",
SubVendorIDs[i]));
}
}
#endif
Match++;
}
//
// Examine RevisionID value, check against device's revision identification
//
RevisionIDNum = PCI_MAX_REG_LIST;
if (!RegGetList(DevKey, PCIBUS_REVISIONID_VALNAME, &RevisionIDNum, RevisionIDs) && (RevisionIDNum == PCI_MAX_REG_LIST)) {
DEBUGMSG(ZONE_WARNING, (L"PCIBUS!RegMatchOne: Registry value '%s\\%s' has more than %d items in multi_sz list. Ignoring the additional items.\r\n",
RegPath, PCIBUS_REVISIONID_VALNAME, PCI_MAX_REG_LIST));
}
if (!RevisionIDNum) {
// No VendorID value found
goto RegMatchOne_Exit;
}
// Scan list for match
for (i = 0; i < RevisionIDNum; i++) {
if ((UCHAR)RevisionIDs[i] == pInfo->Cfg->RevisionID) break;
}
if (i == RevisionIDNum) {
// No match found
Match = 0;
goto RegMatchOne_Exit;
} else {
DEBUGMSG(ZONE_ENUM, (L"PCIBUS!RegMatchOne: Match for RevisionID (0x%X) found.\r\n", RevisionIDs[i]));
Match++;
}
//
// Examine BusNumber value, check against device's bus number
//
ValLen = sizeof(Val32);
status = RegQueryValueEx(DevKey, PCIBUS_BUSNUMBER_VALNAME, NULL, &ValType, (PUCHAR)&Val32, &ValLen);
if (status != ERROR_SUCCESS) {
// No BusNumber value found
goto RegMatchOne_Exit;
}
if (Val32 != pInfo->Bus) {
// Bus number doesn't match
Match = 0;
goto RegMatchOne_Exit;
} else {
DEBUGMSG(ZONE_ENUM, (TEXT("PCIBUS!RegMatchOne Match for BusNumber (0x%X) found.\r\n"), Val32));
Match++;
}
//
// Examine DeviceNumber value, check against device's device number
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -