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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
from enum import IntEnum
from construct import *
from io import BytesIO
from m1n1.utils import FourCC, chexdump
from m1n1.constructutils import ZPadding
from m1n1.fw.afk.epic import EPICCmd, EPICCategory
EPICSubHeaderVer2 = Struct(
"length" / Int32ul,
"version" / Default(Int8ul, 2),
"category" / EPICCategory,
"type" / Hex(Int16ul),
"timestamp" / Default(Int64ul, 0),
"unk1" / Default(Hex(Int32ul), 0),
"unk2" / Default(Hex(Int32ul), 0),
)
class AOPAudioPropKey(IntEnum):
IS_READY = 0x01
UNK_11 = 0x11
PLACEMENT = 0x1e
UNK_21 = 0x21
ORIENTATION = 0x2e
LOCATION_ID = 0x30
SERIAL_NO = 0x3e
VENDOR_ID = 0x5a
PRODUCT_ID = 0x5b
SERVICE_CONTROLLER = 0x64
DEVICE_COUNT = 0x65
VERSION = 0x67
class EPICCall:
@classmethod
def matches(cls, hdr, sub):
return int(sub.type) == cls.TYPE
def _args_fixup(self):
pass
def __init__(self, *args, **kwargs):
if args:
self.args = args[0]
else:
self.args = Container(**kwargs)
self._args_fixup()
self.rets = None
@classmethod
def from_stream(cls, f):
return cls(cls.ARGS.parse_stream(f))
def dump(self, logger=None):
if logger is None:
logger = print
args_fmt = [f"{k}={v}" for (k, v) in self.args.items() if k != "_io"]
rets_fmt = [f"{k}={v}" for (k, v) in self.rets.items() if k != "_io"]
logger(f"{type(self).__name__}({', '.join(args_fmt)}) -> ({', '.join(rets_fmt)})")
def read_resp(self, f):
self.rets = self.RETS.parse_stream(f)
CALLTYPES = []
def reg_calltype(calltype):
CALLTYPES.append(calltype)
return calltype
@reg_calltype
class GetHIDDescriptor(EPICCall):
TYPE = 0x1
ARGS = Struct(
"blank" / Const(0x0, Int32ul),
)
RETS = Struct(
"retcode" / Default(Hex(Int32ul), 0),
"descriptor" / HexDump(GreedyBytes),
)
@reg_calltype
class GetProperty(EPICCall):
TYPE = 0xa
ARGS = Struct(
"blank" / Const(0x0, Int32ul),
"key" / Enum(Int32ul, AOPAudioPropKey),
)
RETS = Struct(
#"blank" / Const(0x0, Int32ul),
"value" / GreedyBytes,
)
@reg_calltype
class WrappedCall(EPICCall):
SUBCLASSES = {}
TYPE = 0x20
HDR = Struct(
"blank" / Const(0x0, Int32ul),
"unk1" / Hex(Const(0xffffffff, Int32ul)),
"calltype" / Hex(Int32ul),
"blank2" / ZPadding(16),
"pad" / Hex(Int32ul),
"len" / Hex(Int64ul),
"residue" / HexDump(GreedyBytes),
)
@classmethod
def from_stream(cls, f):
payload = f.read()
subsub = cls.HDR.parse(payload)
calltype = int(subsub.calltype)
subcls = cls.SUBCLASSES.get(calltype, None)
if subcls is None:
raise ValueError(f"unknown calltype {calltype:#x}")
return subcls(subcls.ARGS.parse(payload))
@classmethod
def reg_subclass(cls, cls2):
cls.SUBCLASSES[int(cls2.CALLTYPE)] = cls2
return cls2
@classmethod
def matches(cls, hdr, sub):
return sub.category == EPICCategory.NOTIFY and sub.type == cls.TYPE
def check_retcode(self):
if self.rets.retcode:
self.dump()
raise ValueError(f"retcode {self.rets.retcode} in {str(type(self))} (call dumped, see above)")
@WrappedCall.reg_subclass
class AttachDevice(WrappedCall):
CALLTYPE = 0xc3_00_00_02
ARGS = Struct(
"blank" / Const(0x0, Int32ul),
"unk1" / Hex(Const(0xffffffff, Int32ul)),
"calltype" / Hex(Const(0xc3000002, Int32ul)),
"blank2" / ZPadding(16),
"pad" / Padding(4),
"len" / Hex(Const(0x2c, Int64ul)),
"devid" / FourCC,
"pad" / Padding(4),
)
RETS = Struct(
"retcode" / Default(Hex(Int32ul), 0),
"unk" / HexDump(GreedyBytes),
)
@WrappedCall.reg_subclass
class ProbeDevice(WrappedCall):
CALLTYPE = 0xc3_00_00_01
ARGS = Struct(
"blank" / Const(0x0, Int32ul),
"unk1" / Hex(Const(0xffffffff, Int32ul)),
"calltype" / Hex(Const(0xc3000001, Int32ul)),
"blank2" / ZPadding(16),
"pad" / Padding(4),
"len" / Hex(Const(0x28, Int64ul)),
"devno" / Int32ul,
)
RETS = Struct(
"retcode" / Default(Hex(Int32ul), 0),
"devid" / FourCC,
"blank2" / Const(0x0, Int32ul),
"unk1" / Const(8, Int32ul),
"blank3" / Const(0x0, Int32ul),
"unk2" / Hex(Const(0x01_0d_1c_20, Int32ul)),
"blank4" / Const(0x0, Int32ul),
"remainder" / HexDump(GreedyBytes),
)
PDMConfig = Struct(
"unk1" / Int32ul,
"clockSource" / FourCC,
"pdmFrequency" / Int32ul,
"unk3_clk" / Int32ul,
"unk4_clk" / Int32ul,
"unk5_clk" / Int32ul,
"channelPolaritySelect" / Hex(Int32ul),
"unk7" / Hex(Int32ul),
"unk8" / Hex(Int32ul),
"unk9" / Hex(Int16ul),
"ratios" / Struct(
"r1" / Int8ul,
"r2" / Int8ul,
"r3" / Int8ul,
"pad" / Default(Int8ul, 0),
),
"filterLengths" / Hex(Int32ul),
"coeff_bulk" / Int32ul,
#"coefficients" / Struct(
# "c1" / Int32sl[this._.ratios.r3 * 4 + 4],
# "c2" / Int32sl[this._.ratios.r2 * 4 + 4],
# "c3" / Int32sl[this._.ratios.r1 * 4 + 4],
#),
#"junk" / Padding(
# this.coeff_bulk * 4 - 48 \
# - (this.ratios.r1 + this.ratios.r2 + this.ratios.r3) * 16
#),
"coefficients" / Int32sl[
(this.ratios.r1 + this.ratios.r2 + this.ratios.r3) * 4 + 12
],
"junk" / Padding(
lambda this: max(0,
this.coeff_bulk * 4 - 48 \
- (this.ratios.r1 + this.ratios.r2 + this.ratios.r3) * 16
)
),
"unk10" / Int32ul, # maybe
"micTurnOnTimeMs" / Int32ul,
"blank" / ZPadding(16),
"unk11" / Int32ul,
"micSettleTimeMs" / Int32ul,
"blank2" / ZPadding(69),
)
DecimatorConfig = Struct(
"latency" / Int32ul,
"ratios" / Struct(
"r1" / Int8ul,
"r2" / Int8ul,
"r3" / Int8ul,
"pad" / Default(Int8ul, 0),
),
"filterLengths" / Hex(Int32ul),
"coeff_bulk" / Int32ul,
"coefficients" / Int32sl[
(this.ratios.r1 + this.ratios.r2 + this.ratios.r3) * 4 + 12
],
"junk" / Padding(
lambda this: max(0,
this.coeff_bulk * 4 - 48 \
- (this.ratios.r1 + this.ratios.r2 + this.ratios.r3) * 16
)
),
)
PowerSetting = Struct(
"devid" / FourCC,
"cookie" / Int32ul,
"pad" / Padding(4),
"blank" / ZPadding(8),
"target_pstate" / FourCC,
"unk2" / Int32ul,
"blank2" / ZPadding(20),
)
DEVPROPS = {
('hpai', 202): PowerSetting,
('lpai', 202): PowerSetting,
('hpai', 200): FourCC,
('lpai', 200): FourCC,
('pdm0', 200): PDMConfig,
('pdm0', 210): DecimatorConfig,
('lpai', 301): Struct(
"unk1" / Int32ul,
"unk2" / Int32ul,
"unk3" / Int32ul,
"unk4" / Int32ul,
),
}
@WrappedCall.reg_subclass
class GetDeviceProp(WrappedCall):
CALLTYPE = 0xc3_00_00_04
ARGS = Struct(
"blank" / Const(0x0, Int32ul),
"unk1" / Hex(Const(0xffffffff, Int32ul)),
"calltype" / Hex(Const(0xc3000004, Int32ul)),
"blank2" / ZPadding(16),
"pad" / Padding(4),
"len" / Hex(Const(0x30, Int64ul)),
"devid" / FourCC,
"modifier" / Int32ul,
"unk6" / Hex(Const(0x01, Int32ul)),
)
RETS = Struct(
"retcode" / Default(Hex(Int32ul), 0),
"len" / Optional(Int32ul),
"data" / Switch(lambda s: (s._params.devid, s._params.modifier),
DEVPROPS,
default=HexDump(GreedyBytes))
)
def read_resp(self, f):
self.rets = self.RETS.parse_stream(f,
devid=self.args.devid, modifier=self.args.modifier
)
@WrappedCall.reg_subclass
class SetDeviceProp(WrappedCall):
CALLTYPE = 0xc3_00_00_05
ARGS = Struct(
"blank" / Const(0x0, Int32ul),
"unk1" / Hex(Const(0xffffffff, Int32ul)),
"calltype" / Hex(Const(0xc3000005, Int32ul)),
"blank2" / ZPadding(16),
"pad" / Padding(4),
"len" / Hex(Int64ul), # len(this.data) + 0x30
"devid" / FourCC,
"modifier" / Int32ul,
"len2" / Hex(Int32ul), # len(this.data)
"data" / Switch(lambda s: (s.devid, s.modifier),
DEVPROPS,
default=HexDump(GreedyBytes))
)
RETS = Struct(
"retcode" / Default(Hex(Int32ul), 0),
"unk" / HexDump(GreedyBytes),
)
def _args_fixup(self):
data_len = len(self.ARGS.build(Container(len=0, len2=0, **self.args))) - 52
if 'len' not in self.args:
self.args.len = data_len + 0x30
if 'len2' not in self.args:
self.args.len2 = data_len
@reg_calltype
class IndirectCall(EPICCall):
ARGS = EPICCmd
RETS = EPICCmd
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.txbuf = None
self.rxbuf = None
@classmethod
def matches(cls, hdr, sub):
return sub.category == EPICCategory.COMMAND
def read_txbuf(self, ep):
cmd = self.args
ep.dart.invalidate_cache()
self.txbuf = ep.dart.ioread(0, cmd.txbuf, cmd.txlen)
# dump the command data for offline replays of traces
ep.log(f"===COMMAND TX DATA=== addr={cmd.txbuf:#x}")
chexdump(self.txbuf)
ep.log(f"===END DATA===")
def read_rxbuf(self, ep):
cmd = self.rets
ep.dart.invalidate_cache()
self.rxbuf = ep.dart.ioread(0, cmd.rxbuf, cmd.rxlen)
ep.log(f"===COMMAND RX DATA=== addr={cmd.rxbuf:#x}")
chexdump(self.rxbuf)
ep.log(f"===END DATA===")
def unwrap(self):
fd = BytesIO()
fd.write(b"\x00\x00\x00\x00")
fd.write(self.txbuf)
fd.seek(0)
wrapped = WrappedCall.from_stream(fd)
fd = BytesIO()
fd.write(b"\x00\x00\x00\x00")
fd.write(self.rxbuf)
fd.seek(0)
wrapped.read_resp(fd)
return wrapped
|