-
Notifications
You must be signed in to change notification settings - Fork 14
/
pci_root.py
268 lines (211 loc) · 7.52 KB
/
pci_root.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import platform
from src.info import color_text
from src.util.debugger import Debugger as debugger
if platform.system().lower() == "darwin":
from src.dumps.macOS.ioreg import *
def _get_valid(slot):
try:
return tuple([
hex(
int(n, 16)) for n
in slot.split(":")[-1].split(".")
])
except Exception:
return (None, None)
# Original source:
# https://github.com/dortania/OpenCore-Legacy-Patcher/blob/ca859c7ad7ac2225af3b50626d88f3bfe014eaa8/resources/device_probe.py#L67-L93
def construct_pcip_osx(parent_entry, acpi, logger):
data = {
"PCI Path": "",
"ACPI Path": ""
}
paths = []
entry = parent_entry
while entry:
if IOObjectConformsTo(entry, b'IOPCIDevice'):
try:
bus, func = ([
hex(int(i, 16)) for i in
ioname_t_to_str(
IORegistryEntryGetLocationInPlane(
entry, b'IOService', None
)[1]
).split(',')
] + ['0x0'])[:2]
paths.append(
f'Pci({bus},{func})'
)
except ValueError:
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Failed! Ignoring! — (IOKit)",
"red"
))
logger.warning(
"Failed to construct PCI path for ambiguous PCI device (IOKit) – Non-critical, ignoring.",
__file__,
)
break
elif IOObjectConformsTo(entry, b'IOACPIPlatformDevice'):
paths.append(
f'PciRoot({hex(int(corefoundation_to_native(IORegistryEntryCreateCFProperty(entry, "_UID", kCFAllocatorDefault, kNilOptions)) or 0))})')
break
elif IOObjectConformsTo(entry, b'IOPCIBridge'):
pass
else:
paths = []
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Invalid! Unable to construct path! — (IOKit)",
"red"
))
logger.warning(
"Invalid PCI device – unable to construct PCI path (IOKit)",
__file__,
)
break
parent = IORegistryEntryGetParentEntry(entry, b'IOService', None)[1]
if entry != parent_entry:
IOObjectRelease(entry)
entry = parent
if paths:
data['PCI Path'] = '/'.join(reversed(paths))
if acpi:
data['ACPI Path'] = ''.join([("\\" if "sb" in a.lower(
) else ".") + a.split("@")[0] for a in acpi.split(':')[1].split('/')[1:]])
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Successfully constructed PCI/ACPI path(s)! — (IOKit)",
"green"
))
return data
def pci_from_acpi_win(wmi, instance_id, logger):
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Attempting to construct PCI/ACPI paths...",
"yellow"
))
try:
# Thank you to DhinakG for this.
# See: https://github.com/USBToolBox/tool/blob/ba3bb1238c0b552cb8066e29c5dc83b5e8faae32/Windows.py#L46
raw_path = (
wmi.query(
f"SELECT * FROM Win32_PnPEntity WHERE PNPDeviceID = '{instance_id}'"
)[0]
.GetDeviceProperties(["DEVPKEY_Device_LocationPaths"])[0][0]
.Data
)
except Exception as e:
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Failed to retrieve path of anonymous device! — (WMI)\n",
"red"
))
logger.error(
f"Failed to retrieve ACPI/PCI path of anonymous device (WMI)\n\t^^^^^^^^^{str(e)}"
)
return {}
if not raw_path:
return
data = {"PCI Path": "", "ACPI Path": ""}
devices = raw_path
for device in devices:
# A valid ACPI/PCI path shouldn't have
# `USB(...)` as any argument.
if "usb" in device.lower():
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: 'USB' component present in path - non-constructable! — (WMI)\n",
"red"
))
logger.warning(
"[USB WARNING]: Non-constructable ACPI/PCI path - ignoring.. (WMI)"
)
break
path = ""
for arg in device.split("#"):
if "acpi" in device.lower():
if "_SB" in arg:
path += "\_SB"
continue
try:
# Thank you to DhinakG for this.
_acpi, val = arg[:-1].split("(")
except Exception as e:
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Failed to parse path of anonymous device! — (WMI)\n",
"red"
))
logger.error(
f"Failed to parse ACPI/PCI path of anonymous device (WMI)\n\t^^^^^^^^^{str(e)}"
)
path = None
break
if _acpi.lower() == "pci":
path = None
break
path += f".{val}"
data["ACPI Path"] = path
continue
# Thank you to DhinakG for this.
#
# E.g: PCI(0301) -> ['PCI', '0301']
digit = arg[:-1].split("(")[1]
if not digit:
path = None
return
# Add PCIROOT (domain)
if "pciroot" in arg.lower():
path += f"PciRoot({hex(int(digit, 16))})"
continue
path += f"/Pci({hex(int(digit[0:2], 16))},{hex(int(digit[2:], 16))})"
data["PCI Path"] = path
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Successfully constructed PCI/ACPI path(s)! — (WMI)\n",
"green"
))
return data
def pci_from_acpi_linux(device_path, logger):
data = {}
acpi = ""
pci = ""
try:
acpi = open(f"{device_path}/firmware_node/path", "r").read().strip()
pci = open(f"{device_path}/uevent", "r").read().strip()
data["ACPI Path"] = acpi
except Exception as e:
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Failed to construct paths of anonymous device! — (SYS_FS)",
"red"
))
logger.error(
f"Failed to construct ACPI/PATH of anonymous device (SYS_FS)\n\t^^^^^^^^^{str(e)}"
)
return ""
if not acpi or not pci:
return ""
# Path to be yielded in the end.
# E.g: PciRoot(0x0)/Pci(0x2,0x0)
pcip = ""
# Parent PCI description
#
# <domain>:<bus>:<slot>.<function>
slot = ""
for line in pci.split("\n"):
if "pci_slot_name" in line.lower():
slot = line.split("=")[1]
break
if slot:
# Domain
pcip += f"PciRoot({hex(int(slot.split(':')[0], 16))})"
children = []
paths = [",".join(_get_valid(slot))]
for path in os.listdir("/sys/bus/pci/devices"):
if slot in os.listdir(f"/sys/bus/pci/devices/{path}"):
children.append(path)
for child in children:
paths.append(",".join(_get_valid(child)))
for comp in sorted(paths, reverse=True):
pcip += f"/Pci({comp})"
if pcip:
data["PCI Path"] = pcip
debugger.log_dbg(color_text(
"--> [PCI/ACPI]: Successfully constructed PCI/ACPI path(s)! — (SYS_FS)",
"green"
))
return data