-
Notifications
You must be signed in to change notification settings - Fork 1
/
goodix_message.lua
286 lines (251 loc) · 10.1 KB
/
goodix_message.lua
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
protocol = Proto("goodix", "Goodix Fingerprint Sensor Message Protocol")
cmd0_field = ProtoField.uint8("goodix.cmd0", "cmd0", base.HEX, nil, 0xF0)
cmd1_field = ProtoField.uint8("goodix.cmd1", "cmd1", base.HEX, nil, 0x0E)
cmd_lsb = ProtoField.bool("goodix.cmd_lsb", "cmd LSB", 8, nil, 0x01) -- Always false afaik, but dissecting just in case.
len = ProtoField.uint16("goodix.len", "Length", base.DEC)
cksum = ProtoField.uint8("goodix.cksum", "Checksum", base.HEX)
ack_bool = ProtoField.bool("goodix.ack_bool", "Unknown bool", 2, nil, 0x02)
ack_true = ProtoField.bool("goodix.ack_true", "Always true", 2, nil, 0x01)
ack_cmd = ProtoField.uint8("goodix.ack_cmd", "ACKed Command", base.HEX)
firmware_version = ProtoField.stringz("goodix.firmware_version", "Firmware Version")
enabled = ProtoField.bool("goodix.enabled", "Enabled")
mcu_state_image = ProtoField.bool("goodix.mcu_state.is_image_valid", "isImageValid", 8, nil, 0x01) -- Meaning unknown
mcu_state_tls = ProtoField.bool("goodix.mcu_state.is_tls_connected", "isTlsConnected", 8, nil, 0x02)
mcu_state_spi = ProtoField.bool("goodix.mcu_state.is_spi_send", "isSpiSend", 8, nil, 0x04) -- Meaning unknown
mcu_state_locked = ProtoField.bool("goodix.mcu_state.is_locked", "isLocked", 8, nil, 0x08) -- Meaning unknown
mcu_state_sensor_crc32_valid = ProtoField.bool("goodix.mcu_state.sensor_crc32_valid", "Correct crc32 on the last image from the sensor")
reset_flag_sensor = ProtoField.bool("goodix.reset_flag.sensor", "Reset Sensor", 8, nil, 0x01)
reset_flag_mcu = ProtoField.bool("goodix.reset_flag.mcu", "Soft Reset MCU", 8, nil, 0x02)
reset_flag_sensor_copy = ProtoField.bool("goodix.reset_flag.sensor_copy", "Reset Sensor (copy)", 8, nil, 0x04) -- Driver always sets this at the same time as reset_flag.sensor, firmware ignores this one
sensor_reset_success = ProtoField.bool("goodix.sensor_reset_success", "Sensor reset success") -- False if a timeout occours getting a response from the sensor
sensor_reset_number = ProtoField.uint16("goodix.sensor_reset_number", "Sensor reset number") -- Contents unknown, but it's a LE short sent if the sensor reset succeeds
reg_multiple = ProtoField.bool("goodix.reg.multiple", "Multiple addresses") -- Only false is used by driver, no dissection implemented for true
reg_address = ProtoField.uint16("goodix.reg.addr", "Base Address", base.HEX)
reg_len = ProtoField.uint8("goodix.reg.len", "Length")
pwrdown_scan_freq = ProtoField.uint16("goodix.powerdown_scan_frequency", "Powerdown Scan Frequecy")
config_sensor_chip = ProtoField.uint8("goodix.config_sensor_chip", "Sensor Chip", base.RANGE_STRING, {
{0, 0, "GF3208"},
{1, 1, "GF3288"},
{2, 2, "GF3266"},
}, 0xF0)
protocol.fields = {
cmd0_field, cmd1_field, cmd_lsb, len, cksum,
ack_cmd, ack_true, ack_bool,
firmware_version,
enabled,
mcu_state_image, mcu_state_tls, mcu_state_spi, mcu_state_locked, mcu_state_sensor_crc32_valid,
reset_flag_sensor, reset_flag_mcu, reset_flag_sensor_copy,
sensor_reset_success, sensor_reset_number,
reg_multiple, reg_address, reg_len,
pwrdown_scan_freq,
config_sensor_chip
}
function extract_cmd0_cmd1(cmd)
return bit.rshift(cmd, 4), bit.rshift(cmd%16, 1)
end
function get_cmd_name(cmd)
cmd0, cmd1 = extract_cmd0_cmd1(cmd)
if commands[cmd0][cmd1] ~= nil then
return commands[cmd0][cmd1].name
else
return string.format("%s.%x", commands[cmd0].category_name, cmd1)
end
end
-- Nested table, keyed by [cmd0][cmd1].
commands = {
[0x0] = {
category_name = "NOP",
[0x0] = {
name = "nop",
dissect_command = function(tree, buf)
-- This packet has a fixed, non-standard checksum of 0x88
-- Its purpose is unknown -- REd firmware does nothing when it recieves one.
end,
}
},
[0x2] = {
category_name = "Ima",
},
[0x3] = {
category_name = "FDT",
},
[0x4] = {
category_name = "FF",
},
[0x5] = {
category_name = "NAV",
},
[0x6] = {
category_name = "Sle",
},
[0x7] = {
category_name = "IDL",
},
[0x8] = {
category_name = "REG",
[1] = {
name = "Read Sensor Register",
dissect_command = function(tree, buf)
tree:add_le(reg_multiple, buf(0, 1))
tree:add_le(reg_address, buf(1, 2))
tree:add_le(reg_len, buf(3, 1)):append_text(" bytes")
end,
dissect_reply = function(tree, buf)
-- Reply is just the bytes requested
end,
},
},
[0x9] = {
category_name = "CHIP",
-- Operations on the sensor chip (not the MCU)
[0] = {
name = "Upload Config",
dissect_command = function(tree, buf)
tree:add_le(config_sensor_chip, buf(0, 1))
end,
dissect_reply = function(tree, buf)
end,
},
[2] = {
name = "Set Powerdown Scan Frequency",
dissect_command = function(tree, buf)
-- I believe this is for a feature (POV/persistance of vision) where the sensor continues scanning while the laptop is asleep, and sends it to the laptop once it wakes up
tree:add_le(pwrdown_scan_freq, buf(0, 2)) -- Units unknown, though mine is 100, so ms would make sense?
end,
dissect_reply = function(tree, buf)
-- TODO check
end,
},
[3] = {
name = "Enable Chip",
dissect_command = function(tree, buf)
tree:add_le(enabled, buf(0, 1))
end,
},
},
[0xA] = {
category_name = "OTHER",
[1] = {
name = "Reset",
dissect_command = function(tree, buf)
tree:add_le(reset_flag_sensor, buf(0, 1))
tree:add_le(reset_flag_mcu, buf(0, 1))
tree:add_le(reset_flag_sensor_copy, buf(0, 1))
end,
dissect_reply = function(tree, buf)
tree:add_le(sensor_reset_success, buf(0, 1))
tree:add_le(sensor_reset_number, buf(1, 2))
end,
},
[3] = {
name = "Read OTP",
-- I believe OTP refers to one-time-programmable memory, which is written with calibration values at the factory
dissect_command = function(tree, buf)
-- Request is empty
end,
dissect_reply = function(tree, buf)
-- The OTP (32 bytes for my sensor model, I believe it differs with others)
end,
},
[4] = {
name = "Firmware Version",
dissect_command = function(tree, buf)
end,
dissect_reply = function(tree, buf)
tree:add_le(firmware_version, buf())
end,
},
[7] = {
name = "Query MCU State",
dissect_command = function(tree, buf)
-- TODO what's the the 0x55
end,
dissect_reply = function(tree, buf)
tree:add_le(mcu_state_image, buf(0, 1))
tree:add_le(mcu_state_tls, buf(0, 1))
tree:add_le(mcu_state_spi, buf(0, 1))
tree:add_le(mcu_state_locked, buf(0, 1))
tree:add_le(mcu_state_sensor_crc32_valid, buf(3, 1))
end,
},
},
[0xB] = {
category_name = "MSG",
[0] = {
name = "Ack",
dissect_reply = function(tree, buf)
tree:add_le(ack_true, buf(1, 1))
tree:add_le(ack_bool, buf(1, 1)) -- commands 0-5 (inclusive) are ignored if this is true.
tree:add_le(ack_cmd, buf(0, 1)):append_text(" (" .. get_cmd_name(buf(0,1):le_uint()) .. ")")
end,
},
},
[0xC] = {
category_name = "NOTI",
},
[0xD] = {
category_name = "TLSCONN",
[0] = {
name = "Request TLS Connection",
dissect_command = function(tree, buf)
-- No args.
-- MCU doesn't do a normal reply (except the ack), but it triggers it to send a TLS Client Hello as a V2 encrypted packet
-- Until sending "TLS Successfully Established", any messages other than TLSCONN.* and "Query MCU State" are ignored.
end,
},
[1] = {
name = "Resend Image data?",
dissect_command = function(tree, buf)
-- Seemingly gives the same response over TLS as sending Ima.0 does,
-- but without reading a new image from the sensor. Not seen used,
-- untested.
end,
},
[2] = {
name = "TLS Successfully Established",
dissect_command = function(tree, buf)
-- No args, no reply.
end,
},
[0xE] = {
category_name = "PROD",
},
[0xF] = {
category_name = "UPFW",
},
}
}
function protocol.dissector(buffer, pinfo, tree)
length = buffer:len()
if length == 0 then return end
pinfo.cols.protocol = "Goodix"
local subtree = tree:add(protocol, buffer(), "Goodix Message Protocol")
body_buf = buffer(3, buffer:len()-4):tvb()
subtree:add_le(cmd0_field, buffer(0,1))
subtree:add_le(cmd1_field, buffer(0,1))
subtree:add_le(cmd_lsb, buffer(0,1))
subtree:add_le(len, buffer(1,2)):append_text(" bytes (including checksum)")
subtree:add_le(cksum, buffer(buffer:len()-1,1))
from_host = pinfo.src == Address.ip("1.1.1.1") or tostring(pinfo.src) == "host"
local cmd_subtree = subtree:add(protocol, body_buf())
cmd_val = buffer(0, 1):le_uint()
cmd0_val, cmd1_val = extract_cmd0_cmd1(cmd_val)
if from_host then
summary = "Command: " .. get_cmd_name(cmd_val)
if commands[cmd0_val][cmd1_val] ~= nil then
commands[cmd0_val][cmd1_val].dissect_command(cmd_subtree, body_buf)
end
else
summary = "Reply: " .. get_cmd_name(cmd_val)
if commands[cmd0_val][cmd1_val] ~= nil then
commands[cmd0_val][cmd1_val].dissect_reply(cmd_subtree, body_buf)
end
end
cmd_subtree.text = summary
pinfo.cols.info = summary
end
DissectorTable.get("tls.port"):add(1, protocol)
DissectorTable.get("tls.port"):add(1, protocol)
DissectorTable.get("usb.protocol"):add_for_decode_as(protocol)
DissectorTable.get("usb.product"):add_for_decode_as(protocol)
DissectorTable.get("usb.device"):add_for_decode_as(protocol)