Skip to content

Commit

Permalink
new: cfgmgr32 implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
kernel-dev committed May 8, 2022
1 parent dd95ff5 commit e5a0202
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/cfgmgr32/core/cfgmgr32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from ctypes import WinDLL, c_ulong, c_char_p, byref, c_buffer, sizeof

cfgmgr = WinDLL("cfgmgr32.dll")


class CM32:
def __init__(self):
pass

def CM_Locate_DevNodeA(
self,
pdnDevInst=c_ulong(),
pDeviceID=c_char_p(),
ulFlags=c_ulong(0),
):
status = cfgmgr.CM_Locate_DevNodeA(
byref(pdnDevInst),
pDeviceID,
ulFlags,
)

# Something went wrong.
if status != 0x0:
return {
"status": "failure",
"code": status,
"reason": "error"
}
else:
return {
"status": "success",
"code": 0x0
}

def CM_Get_Parent(
self,
pdnDevInst=c_ulong(),
dnDevInst=c_ulong(),
):
status = cfgmgr.CM_Get_Parent(
byref(pdnDevInst),
dnDevInst,
c_ulong(0),
)

# Something went wrong.
if status != 0x0:
return {
"status": "failure",
"code": status,
"reason": "error"
}
else:
return {
"status": "success",
"code": 0x0
}

def CM_Get_DevNode_PropertyW(
self,
dnDevInst=c_ulong(),
propKey=None,
propType=c_ulong(),
propBuff=None,
propBuffSize=c_ulong(0)
):
if propKey == None:
return {}

status = cfgmgr.CM_Get_DevNode_PropertyW(
dnDevInst,
byref(propKey),
byref(propType),
propBuff,
byref(propBuffSize),
c_ulong(0),
)

# We ran out of memory
if status == 0x02:
return {
"status": "failure",
"code": status,
"reason": "out of memory"
}

# The buffer isn't big enough...
# we can just iterate again with a fixed-length buffer.
#
# More error codes here: https://github.com/tpn/winsdk-10/blob/master/Include/10.0.10240.0/um/cfgmgr32.h#L4508
if status == 0x1A or propBuff == None:
return self.CM_Get_DevNode_PropertyW(
dnDevInst,
propKey,
propType,
propBuff=c_buffer(b"", sizeof(c_ulong) * propBuffSize.value),
propBuffSize=propBuffSize,
)

# Something went wrong.
elif status != 0x0:
return {
"status": "failure",
"code": status,
"reason": "error"
}

else:
return {
"status": "success",
"data": {
"type": propType,
"buff": propBuff,
"size": propBuffSize
},
"devInst": dnDevInst,
"code": 0x0
}
24 changes: 24 additions & 0 deletions src/cfgmgr32/data/props.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Full list here: https://github.com/tpn/winsdk-10/blob/master/Include/10.0.16299.0/shared/devpkey.h
#
# Special thank you to [Flagers](https://github.com/flagersgit) for sharing this with me.
props = [
["name", 0xb725f130, 0x47ef, 0x101a, [0xa5, 0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac], 10],

["driver", 0xa45c254e, 0xdf1c, 0x4efd, [0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0], 11],

["compatible_ids", 0xa45c254e, 0xdf1c, 0x4efd, [0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0], 4],

["manufacturer", 0xa45c254e, 0xdf1c, 0x4efd, [0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0], 13],

["location_paths", 0xa45c254e, 0xdf1c, 0x4efd, [0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0], 37],

["model", 0x78c34fc8, 0x104a, 0x4aca, [0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57], 39],

["instance_id", 0x78c34fc8, 0x104a, 0x4aca, [0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57], 256],

["driver_desc", 0xa8b865dd, 0x2e3d, 0x4094, [0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6], 4],

["driver_inf_path", 0xa8b865dd, 0x2e3d, 0x4094, [0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6], 5],

["driver_provider", 0xa8b865dd, 0x2e3d, 0x4094, [0xad, 0x97, 0xe5, 0x93, 0xa7, 0xc, 0x75, 0xd6], 9]
]
12 changes: 12 additions & 0 deletions src/cfgmgr32/structs/devpropkey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ctypes import Structure, c_ulong
from .guid import GUID


class DEVPROPKEY(Structure):
"""
Source: https://github.com/tpn/winsdk-10/blob/master/Include/10.0.10240.0/um/devpropdef.h#L118-L124
"""
_fields_ = [
("fmtid", GUID),
("pid", c_ulong)
]
13 changes: 13 additions & 0 deletions src/cfgmgr32/structs/guid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ctypes import Structure, c_char, c_ulong, c_ushort


class GUID(Structure):
"""
Source: https://github.com/tpn/winsdk-10/blob/master/Include/10.0.10240.0/shared/guiddef.h#L22-L26
"""
_fields_ = [
("Data1", c_ulong),
("Data2", c_ushort),
("Data3", c_ushort),
("Data4", c_char * 8)
]
41 changes: 41 additions & 0 deletions src/cfgmgr32/util/get_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ctypes import c_ulong, c_ushort
from src.cfgmgr32.core.cfgmgr32 import CM32
from src.cfgmgr32.data.props import props
from src.cfgmgr32.structs.devpropkey import DEVPROPKEY
from src.cfgmgr32.structs.guid import GUID


def get_info(pdnDevInst=c_ulong(), cm32=CM32()):
re_data = {}

for prop in props:
try:
name = prop[0]
mGUID = GUID(
Data1=c_ulong(prop[1]),
Data2=c_ushort(prop[2]),
Data3=c_ushort(prop[3]),
Data4=bytes(prop[4]),
)

dpkey = DEVPROPKEY(
fmtid=mGUID,
pid=c_ulong(prop[5]),
)

data = cm32.CM_Get_DevNode_PropertyW(
pdnDevInst,
dpkey,
)

buff = data.get("data", {}).get("buff")

if not buff:
continue

re_data[name] = buff.raw.decode().replace(
" ", "").replace("\x00", "")
except Exception as e:
raise e

return re_data

0 comments on commit e5a0202

Please sign in to comment.