Line | Count | Source (jump to first uncovered line) |
1 | | /* auto-generated, do not modify */ |
2 | | #include "config.h" |
3 | | |
4 | | #include "fu-usb-struct.h" |
5 | | #include "fu-byte-array.h" |
6 | | #include "fu-mem-private.h" |
7 | | #include "fu-string.h" |
8 | | |
9 | | #ifdef G_LOG_DOMAIN |
10 | | #undef G_LOG_DOMAIN |
11 | | #endif |
12 | 0 | #define G_LOG_DOMAIN "FuStruct" |
13 | | |
14 | | |
15 | | |
16 | | |
17 | | const gchar * |
18 | | fu_usb_class_to_string(FuUsbClass val) |
19 | 0 | { |
20 | 0 | if (val == FU_USB_CLASS_INTERFACE_DESC) |
21 | 0 | return "interface-desc"; |
22 | 0 | if (val == FU_USB_CLASS_AUDIO) |
23 | 0 | return "audio"; |
24 | 0 | if (val == FU_USB_CLASS_COMMUNICATIONS) |
25 | 0 | return "communications"; |
26 | 0 | if (val == FU_USB_CLASS_HID) |
27 | 0 | return "hid"; |
28 | 0 | if (val == FU_USB_CLASS_PHYSICAL) |
29 | 0 | return "physical"; |
30 | 0 | if (val == FU_USB_CLASS_IMAGE) |
31 | 0 | return "image"; |
32 | 0 | if (val == FU_USB_CLASS_PRINTER) |
33 | 0 | return "printer"; |
34 | 0 | if (val == FU_USB_CLASS_MASS_STORAGE) |
35 | 0 | return "mass-storage"; |
36 | 0 | if (val == FU_USB_CLASS_HUB) |
37 | 0 | return "hub"; |
38 | 0 | if (val == FU_USB_CLASS_CDC_DATA) |
39 | 0 | return "cdc-data"; |
40 | 0 | if (val == FU_USB_CLASS_SMART_CARD) |
41 | 0 | return "smart-card"; |
42 | 0 | if (val == FU_USB_CLASS_CONTENT_SECURITY) |
43 | 0 | return "content-security"; |
44 | 0 | if (val == FU_USB_CLASS_VIDEO) |
45 | 0 | return "video"; |
46 | 0 | if (val == FU_USB_CLASS_PERSONAL_HEALTHCARE) |
47 | 0 | return "personal-healthcare"; |
48 | 0 | if (val == FU_USB_CLASS_AUDIO_VIDEO) |
49 | 0 | return "audio-video"; |
50 | 0 | if (val == FU_USB_CLASS_BILLBOARD) |
51 | 0 | return "billboard"; |
52 | 0 | if (val == FU_USB_CLASS_DIAGNOSTIC) |
53 | 0 | return "diagnostic"; |
54 | 0 | if (val == FU_USB_CLASS_WIRELESS_CONTROLLER) |
55 | 0 | return "wireless-controller"; |
56 | 0 | if (val == FU_USB_CLASS_MISCELLANEOUS) |
57 | 0 | return "miscellaneous"; |
58 | 0 | if (val == FU_USB_CLASS_APPLICATION_SPECIFIC) |
59 | 0 | return "application-specific"; |
60 | 0 | if (val == FU_USB_CLASS_VENDOR_SPECIFIC) |
61 | 0 | return "vendor-specific"; |
62 | 0 | return NULL; |
63 | 0 | } |
64 | | |
65 | | |
66 | | const gchar * |
67 | | fu_usb_descriptor_kind_to_string(FuUsbDescriptorKind val) |
68 | 0 | { |
69 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_INVALID) |
70 | 0 | return "invalid"; |
71 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_DEVICE) |
72 | 0 | return "device"; |
73 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_CONFIG) |
74 | 0 | return "config"; |
75 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_STRING) |
76 | 0 | return "string"; |
77 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_INTERFACE) |
78 | 0 | return "interface"; |
79 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_ENDPOINT) |
80 | 0 | return "endpoint"; |
81 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_INTERFACE_ASSOCIATION) |
82 | 0 | return "interface-association"; |
83 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_BOS) |
84 | 0 | return "bos"; |
85 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_DEVICE_CAPABILITY) |
86 | 0 | return "device-capability"; |
87 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_HID) |
88 | 0 | return "hid"; |
89 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_REPORT) |
90 | 0 | return "report"; |
91 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_PHYSICAL) |
92 | 0 | return "physical"; |
93 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_HUB) |
94 | 0 | return "hub"; |
95 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_SUPERSPEED_HUB) |
96 | 0 | return "superspeed-hub"; |
97 | 0 | if (val == FU_USB_DESCRIPTOR_KIND_SS_ENDPOINT_COMPANION) |
98 | 0 | return "ss-endpoint-companion"; |
99 | 0 | return NULL; |
100 | 0 | } |
101 | | FuUsbDescriptorKind |
102 | | fu_usb_descriptor_kind_from_string(const gchar *val) |
103 | 0 | { |
104 | 0 | if (g_strcmp0(val, "invalid") == 0) |
105 | 0 | return FU_USB_DESCRIPTOR_KIND_INVALID; |
106 | 0 | if (g_strcmp0(val, "device") == 0) |
107 | 0 | return FU_USB_DESCRIPTOR_KIND_DEVICE; |
108 | 0 | if (g_strcmp0(val, "config") == 0) |
109 | 0 | return FU_USB_DESCRIPTOR_KIND_CONFIG; |
110 | 0 | if (g_strcmp0(val, "string") == 0) |
111 | 0 | return FU_USB_DESCRIPTOR_KIND_STRING; |
112 | 0 | if (g_strcmp0(val, "interface") == 0) |
113 | 0 | return FU_USB_DESCRIPTOR_KIND_INTERFACE; |
114 | 0 | if (g_strcmp0(val, "endpoint") == 0) |
115 | 0 | return FU_USB_DESCRIPTOR_KIND_ENDPOINT; |
116 | 0 | if (g_strcmp0(val, "interface-association") == 0) |
117 | 0 | return FU_USB_DESCRIPTOR_KIND_INTERFACE_ASSOCIATION; |
118 | 0 | if (g_strcmp0(val, "bos") == 0) |
119 | 0 | return FU_USB_DESCRIPTOR_KIND_BOS; |
120 | 0 | if (g_strcmp0(val, "device-capability") == 0) |
121 | 0 | return FU_USB_DESCRIPTOR_KIND_DEVICE_CAPABILITY; |
122 | 0 | if (g_strcmp0(val, "hid") == 0) |
123 | 0 | return FU_USB_DESCRIPTOR_KIND_HID; |
124 | 0 | if (g_strcmp0(val, "report") == 0) |
125 | 0 | return FU_USB_DESCRIPTOR_KIND_REPORT; |
126 | 0 | if (g_strcmp0(val, "physical") == 0) |
127 | 0 | return FU_USB_DESCRIPTOR_KIND_PHYSICAL; |
128 | 0 | if (g_strcmp0(val, "hub") == 0) |
129 | 0 | return FU_USB_DESCRIPTOR_KIND_HUB; |
130 | 0 | if (g_strcmp0(val, "superspeed-hub") == 0) |
131 | 0 | return FU_USB_DESCRIPTOR_KIND_SUPERSPEED_HUB; |
132 | 0 | if (g_strcmp0(val, "ss-endpoint-companion") == 0) |
133 | 0 | return FU_USB_DESCRIPTOR_KIND_SS_ENDPOINT_COMPANION; |
134 | 0 | return FU_USB_DESCRIPTOR_KIND_INVALID; |
135 | 0 | } |
136 | | /* getters */ |
137 | | /** |
138 | | * fu_usb_base_hdr_get_length: (skip): |
139 | | **/ |
140 | | guint8 |
141 | | fu_usb_base_hdr_get_length(const FuUsbBaseHdr *st) |
142 | 0 | { |
143 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
144 | 0 | return st->data[0]; |
145 | 0 | } |
146 | | /** |
147 | | * fu_usb_base_hdr_get_descriptor_type: (skip): |
148 | | **/ |
149 | | FuUsbDescriptorKind |
150 | | fu_usb_base_hdr_get_descriptor_type(const FuUsbBaseHdr *st) |
151 | 0 | { |
152 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
153 | 0 | return st->data[1]; |
154 | 0 | } |
155 | | |
156 | | /* setters */ |
157 | | /** |
158 | | * fu_usb_base_hdr_to_string: (skip): |
159 | | **/ |
160 | | static gchar * |
161 | | fu_usb_base_hdr_to_string(const FuUsbBaseHdr *st) |
162 | 0 | { |
163 | 0 | g_autoptr(GString) str = g_string_new("FuUsbBaseHdr:\n"); |
164 | 0 | g_return_val_if_fail(st != NULL, NULL); |
165 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
166 | 0 | (guint) fu_usb_base_hdr_get_length(st)); |
167 | 0 | { |
168 | 0 | const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_base_hdr_get_descriptor_type(st)); |
169 | 0 | if (tmp != NULL) { |
170 | 0 | g_string_append_printf(str, " descriptor_type: 0x%x [%s]\n", (guint) fu_usb_base_hdr_get_descriptor_type(st), tmp); |
171 | 0 | } else { |
172 | 0 | g_string_append_printf(str, " descriptor_type: 0x%x\n", (guint) fu_usb_base_hdr_get_descriptor_type(st)); |
173 | 0 | } |
174 | 0 | } |
175 | 0 | if (str->len > 0) |
176 | 0 | g_string_set_size(str, str->len - 1); |
177 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
178 | 0 | } |
179 | | static gboolean |
180 | | fu_usb_base_hdr_validate_internal(FuUsbBaseHdr *st, GError **error) |
181 | 0 | { |
182 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
183 | 0 | return TRUE; |
184 | 0 | } |
185 | | static gboolean |
186 | | fu_usb_base_hdr_parse_internal(FuUsbBaseHdr *st, GError **error) |
187 | 0 | { |
188 | 0 | if (!fu_usb_base_hdr_validate_internal(st, error)) |
189 | 0 | return FALSE; |
190 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
191 | 0 | g_autofree gchar *str = fu_usb_base_hdr_to_string(st); |
192 | 0 | g_debug("%s", str); |
193 | 0 | } |
194 | 0 | return TRUE; |
195 | 0 | } |
196 | | |
197 | | /** |
198 | | * fu_usb_base_hdr_parse: (skip): |
199 | | **/ |
200 | | FuUsbBaseHdr * |
201 | | fu_usb_base_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error) |
202 | 0 | { |
203 | 0 | g_autoptr(GByteArray) st = g_byte_array_new(); |
204 | 0 | g_return_val_if_fail(buf != NULL, NULL); |
205 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
206 | 0 | if (!fu_memchk_read(bufsz, offset, 2, error)) { |
207 | 0 | g_prefix_error(error, "invalid struct FuUsbBaseHdr: "); |
208 | 0 | return NULL; |
209 | 0 | } |
210 | 0 | g_byte_array_append(st, buf + offset, 2); |
211 | 0 | if (!fu_usb_base_hdr_parse_internal(st, error)) |
212 | 0 | return NULL; |
213 | 0 | return g_steal_pointer(&st); |
214 | 0 | } |
215 | | /** |
216 | | * fu_usb_base_hdr_parse_stream: (skip): |
217 | | **/ |
218 | | FuUsbBaseHdr * |
219 | | fu_usb_base_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
220 | 0 | { |
221 | 0 | g_autoptr(GByteArray) st = NULL; |
222 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 2, NULL, error); |
223 | 0 | if (st == NULL) { |
224 | 0 | g_prefix_error(error, "FuUsbBaseHdr failed read of 0x%x: ", (guint) 2); |
225 | 0 | return NULL; |
226 | 0 | } |
227 | 0 | if (st->len != 2) { |
228 | 0 | g_set_error(error, |
229 | 0 | FWUPD_ERROR, |
230 | 0 | FWUPD_ERROR_INVALID_DATA, |
231 | 0 | "FuUsbBaseHdr requested 0x%x and got 0x%x", |
232 | 0 | (guint) 2, |
233 | 0 | (guint) st->len); |
234 | 0 | return NULL; |
235 | 0 | } |
236 | 0 | if (!fu_usb_base_hdr_parse_internal(st, error)) |
237 | 0 | return NULL; |
238 | 0 | return g_steal_pointer(&st); |
239 | 0 | } |
240 | | /* getters */ |
241 | | /** |
242 | | * fu_usb_device_hdr_get_length: (skip): |
243 | | **/ |
244 | | guint8 |
245 | | fu_usb_device_hdr_get_length(const FuUsbDeviceHdr *st) |
246 | 0 | { |
247 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
248 | 0 | return st->data[0]; |
249 | 0 | } |
250 | | /** |
251 | | * fu_usb_device_hdr_get_descriptor_type: (skip): |
252 | | **/ |
253 | | static FuUsbDescriptorKind |
254 | | fu_usb_device_hdr_get_descriptor_type(const FuUsbDeviceHdr *st) |
255 | 0 | { |
256 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
257 | 0 | return st->data[1]; |
258 | 0 | } |
259 | | /** |
260 | | * fu_usb_device_hdr_get_usb: (skip): |
261 | | **/ |
262 | | guint16 |
263 | | fu_usb_device_hdr_get_usb(const FuUsbDeviceHdr *st) |
264 | 0 | { |
265 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
266 | 0 | return fu_memread_uint16(st->data + 2, G_LITTLE_ENDIAN); |
267 | 0 | } |
268 | | /** |
269 | | * fu_usb_device_hdr_get_device_class: (skip): |
270 | | **/ |
271 | | FuUsbClass |
272 | | fu_usb_device_hdr_get_device_class(const FuUsbDeviceHdr *st) |
273 | 0 | { |
274 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
275 | 0 | return st->data[4]; |
276 | 0 | } |
277 | | /** |
278 | | * fu_usb_device_hdr_get_device_sub_class: (skip): |
279 | | **/ |
280 | | guint8 |
281 | | fu_usb_device_hdr_get_device_sub_class(const FuUsbDeviceHdr *st) |
282 | 0 | { |
283 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
284 | 0 | return st->data[5]; |
285 | 0 | } |
286 | | /** |
287 | | * fu_usb_device_hdr_get_device_protocol: (skip): |
288 | | **/ |
289 | | guint8 |
290 | | fu_usb_device_hdr_get_device_protocol(const FuUsbDeviceHdr *st) |
291 | 0 | { |
292 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
293 | 0 | return st->data[6]; |
294 | 0 | } |
295 | | /** |
296 | | * fu_usb_device_hdr_get_max_packet_size0: (skip): |
297 | | **/ |
298 | | guint8 |
299 | | fu_usb_device_hdr_get_max_packet_size0(const FuUsbDeviceHdr *st) |
300 | 0 | { |
301 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
302 | 0 | return st->data[7]; |
303 | 0 | } |
304 | | /** |
305 | | * fu_usb_device_hdr_get_vendor: (skip): |
306 | | **/ |
307 | | guint16 |
308 | | fu_usb_device_hdr_get_vendor(const FuUsbDeviceHdr *st) |
309 | 0 | { |
310 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
311 | 0 | return fu_memread_uint16(st->data + 8, G_LITTLE_ENDIAN); |
312 | 0 | } |
313 | | /** |
314 | | * fu_usb_device_hdr_get_product: (skip): |
315 | | **/ |
316 | | guint16 |
317 | | fu_usb_device_hdr_get_product(const FuUsbDeviceHdr *st) |
318 | 0 | { |
319 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
320 | 0 | return fu_memread_uint16(st->data + 10, G_LITTLE_ENDIAN); |
321 | 0 | } |
322 | | /** |
323 | | * fu_usb_device_hdr_get_device: (skip): |
324 | | **/ |
325 | | guint16 |
326 | | fu_usb_device_hdr_get_device(const FuUsbDeviceHdr *st) |
327 | 0 | { |
328 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
329 | 0 | return fu_memread_uint16(st->data + 12, G_LITTLE_ENDIAN); |
330 | 0 | } |
331 | | /** |
332 | | * fu_usb_device_hdr_get_manufacturer_idx: (skip): |
333 | | **/ |
334 | | guint8 |
335 | | fu_usb_device_hdr_get_manufacturer_idx(const FuUsbDeviceHdr *st) |
336 | 0 | { |
337 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
338 | 0 | return st->data[14]; |
339 | 0 | } |
340 | | /** |
341 | | * fu_usb_device_hdr_get_product_idx: (skip): |
342 | | **/ |
343 | | guint8 |
344 | | fu_usb_device_hdr_get_product_idx(const FuUsbDeviceHdr *st) |
345 | 0 | { |
346 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
347 | 0 | return st->data[15]; |
348 | 0 | } |
349 | | /** |
350 | | * fu_usb_device_hdr_get_serial_number_idx: (skip): |
351 | | **/ |
352 | | guint8 |
353 | | fu_usb_device_hdr_get_serial_number_idx(const FuUsbDeviceHdr *st) |
354 | 0 | { |
355 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
356 | 0 | return st->data[16]; |
357 | 0 | } |
358 | | /** |
359 | | * fu_usb_device_hdr_get_num_configurations: (skip): |
360 | | **/ |
361 | | guint8 |
362 | | fu_usb_device_hdr_get_num_configurations(const FuUsbDeviceHdr *st) |
363 | 0 | { |
364 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
365 | 0 | return st->data[17]; |
366 | 0 | } |
367 | | |
368 | | /* setters */ |
369 | | /** |
370 | | * fu_usb_device_hdr_to_string: (skip): |
371 | | **/ |
372 | | static gchar * |
373 | | fu_usb_device_hdr_to_string(const FuUsbDeviceHdr *st) |
374 | 0 | { |
375 | 0 | g_autoptr(GString) str = g_string_new("FuUsbDeviceHdr:\n"); |
376 | 0 | g_return_val_if_fail(st != NULL, NULL); |
377 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
378 | 0 | (guint) fu_usb_device_hdr_get_length(st)); |
379 | 0 | g_string_append_printf(str, " usb: 0x%x\n", |
380 | 0 | (guint) fu_usb_device_hdr_get_usb(st)); |
381 | 0 | { |
382 | 0 | const gchar *tmp = fu_usb_class_to_string(fu_usb_device_hdr_get_device_class(st)); |
383 | 0 | if (tmp != NULL) { |
384 | 0 | g_string_append_printf(str, " device_class: 0x%x [%s]\n", (guint) fu_usb_device_hdr_get_device_class(st), tmp); |
385 | 0 | } else { |
386 | 0 | g_string_append_printf(str, " device_class: 0x%x\n", (guint) fu_usb_device_hdr_get_device_class(st)); |
387 | 0 | } |
388 | 0 | } |
389 | 0 | g_string_append_printf(str, " device_sub_class: 0x%x\n", |
390 | 0 | (guint) fu_usb_device_hdr_get_device_sub_class(st)); |
391 | 0 | g_string_append_printf(str, " device_protocol: 0x%x\n", |
392 | 0 | (guint) fu_usb_device_hdr_get_device_protocol(st)); |
393 | 0 | g_string_append_printf(str, " max_packet_size0: 0x%x\n", |
394 | 0 | (guint) fu_usb_device_hdr_get_max_packet_size0(st)); |
395 | 0 | g_string_append_printf(str, " vendor: 0x%x\n", |
396 | 0 | (guint) fu_usb_device_hdr_get_vendor(st)); |
397 | 0 | g_string_append_printf(str, " product: 0x%x\n", |
398 | 0 | (guint) fu_usb_device_hdr_get_product(st)); |
399 | 0 | g_string_append_printf(str, " device: 0x%x\n", |
400 | 0 | (guint) fu_usb_device_hdr_get_device(st)); |
401 | 0 | g_string_append_printf(str, " manufacturer_idx: 0x%x\n", |
402 | 0 | (guint) fu_usb_device_hdr_get_manufacturer_idx(st)); |
403 | 0 | g_string_append_printf(str, " product_idx: 0x%x\n", |
404 | 0 | (guint) fu_usb_device_hdr_get_product_idx(st)); |
405 | 0 | g_string_append_printf(str, " serial_number_idx: 0x%x\n", |
406 | 0 | (guint) fu_usb_device_hdr_get_serial_number_idx(st)); |
407 | 0 | g_string_append_printf(str, " num_configurations: 0x%x\n", |
408 | 0 | (guint) fu_usb_device_hdr_get_num_configurations(st)); |
409 | 0 | if (str->len > 0) |
410 | 0 | g_string_set_size(str, str->len - 1); |
411 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
412 | 0 | } |
413 | | static gboolean |
414 | | fu_usb_device_hdr_validate_internal(FuUsbDeviceHdr *st, GError **error) |
415 | 0 | { |
416 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
417 | 0 | if (fu_usb_device_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_DEVICE) { |
418 | 0 | g_set_error_literal(error, |
419 | 0 | FWUPD_ERROR, |
420 | 0 | FWUPD_ERROR_INVALID_DATA, |
421 | 0 | "constant FuUsbDeviceHdr.descriptor_type was not valid"); |
422 | 0 | return FALSE; |
423 | 0 | } |
424 | 0 | return TRUE; |
425 | 0 | } |
426 | | static gboolean |
427 | | fu_usb_device_hdr_parse_internal(FuUsbDeviceHdr *st, GError **error) |
428 | 0 | { |
429 | 0 | if (!fu_usb_device_hdr_validate_internal(st, error)) |
430 | 0 | return FALSE; |
431 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
432 | 0 | g_autofree gchar *str = fu_usb_device_hdr_to_string(st); |
433 | 0 | g_debug("%s", str); |
434 | 0 | } |
435 | 0 | return TRUE; |
436 | 0 | } |
437 | | /** |
438 | | * fu_usb_device_hdr_parse_stream: (skip): |
439 | | **/ |
440 | | FuUsbDeviceHdr * |
441 | | fu_usb_device_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
442 | 0 | { |
443 | 0 | g_autoptr(GByteArray) st = NULL; |
444 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 18, NULL, error); |
445 | 0 | if (st == NULL) { |
446 | 0 | g_prefix_error(error, "FuUsbDeviceHdr failed read of 0x%x: ", (guint) 18); |
447 | 0 | return NULL; |
448 | 0 | } |
449 | 0 | if (st->len != 18) { |
450 | 0 | g_set_error(error, |
451 | 0 | FWUPD_ERROR, |
452 | 0 | FWUPD_ERROR_INVALID_DATA, |
453 | 0 | "FuUsbDeviceHdr requested 0x%x and got 0x%x", |
454 | 0 | (guint) 18, |
455 | 0 | (guint) st->len); |
456 | 0 | return NULL; |
457 | 0 | } |
458 | 0 | if (!fu_usb_device_hdr_parse_internal(st, error)) |
459 | 0 | return NULL; |
460 | 0 | return g_steal_pointer(&st); |
461 | 0 | } |
462 | | /* getters */ |
463 | | /** |
464 | | * fu_usb_descriptor_hdr_get_length: (skip): |
465 | | **/ |
466 | | guint8 |
467 | | fu_usb_descriptor_hdr_get_length(const FuUsbDescriptorHdr *st) |
468 | 0 | { |
469 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
470 | 0 | return st->data[0]; |
471 | 0 | } |
472 | | /** |
473 | | * fu_usb_descriptor_hdr_get_descriptor_type: (skip): |
474 | | **/ |
475 | | static FuUsbDescriptorKind |
476 | | fu_usb_descriptor_hdr_get_descriptor_type(const FuUsbDescriptorHdr *st) |
477 | 0 | { |
478 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
479 | 0 | return st->data[1]; |
480 | 0 | } |
481 | | /** |
482 | | * fu_usb_descriptor_hdr_get_total_length: (skip): |
483 | | **/ |
484 | | guint16 |
485 | | fu_usb_descriptor_hdr_get_total_length(const FuUsbDescriptorHdr *st) |
486 | 0 | { |
487 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
488 | 0 | return fu_memread_uint16(st->data + 2, G_LITTLE_ENDIAN); |
489 | 0 | } |
490 | | /** |
491 | | * fu_usb_descriptor_hdr_get_num_interfaces: (skip): |
492 | | **/ |
493 | | guint8 |
494 | | fu_usb_descriptor_hdr_get_num_interfaces(const FuUsbDescriptorHdr *st) |
495 | 0 | { |
496 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
497 | 0 | return st->data[4]; |
498 | 0 | } |
499 | | /** |
500 | | * fu_usb_descriptor_hdr_get_configuration_value: (skip): |
501 | | **/ |
502 | | guint8 |
503 | | fu_usb_descriptor_hdr_get_configuration_value(const FuUsbDescriptorHdr *st) |
504 | 0 | { |
505 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
506 | 0 | return st->data[5]; |
507 | 0 | } |
508 | | /** |
509 | | * fu_usb_descriptor_hdr_get_configuration: (skip): |
510 | | **/ |
511 | | guint8 |
512 | | fu_usb_descriptor_hdr_get_configuration(const FuUsbDescriptorHdr *st) |
513 | 0 | { |
514 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
515 | 0 | return st->data[6]; |
516 | 0 | } |
517 | | /** |
518 | | * fu_usb_descriptor_hdr_get_attributes: (skip): |
519 | | **/ |
520 | | guint8 |
521 | | fu_usb_descriptor_hdr_get_attributes(const FuUsbDescriptorHdr *st) |
522 | 0 | { |
523 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
524 | 0 | return st->data[7]; |
525 | 0 | } |
526 | | /** |
527 | | * fu_usb_descriptor_hdr_get_max_power: (skip): |
528 | | **/ |
529 | | guint8 |
530 | | fu_usb_descriptor_hdr_get_max_power(const FuUsbDescriptorHdr *st) |
531 | 0 | { |
532 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
533 | 0 | return st->data[8]; |
534 | 0 | } |
535 | | |
536 | | /* setters */ |
537 | | /** |
538 | | * fu_usb_descriptor_hdr_to_string: (skip): |
539 | | **/ |
540 | | static gchar * |
541 | | fu_usb_descriptor_hdr_to_string(const FuUsbDescriptorHdr *st) |
542 | 0 | { |
543 | 0 | g_autoptr(GString) str = g_string_new("FuUsbDescriptorHdr:\n"); |
544 | 0 | g_return_val_if_fail(st != NULL, NULL); |
545 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
546 | 0 | (guint) fu_usb_descriptor_hdr_get_length(st)); |
547 | 0 | g_string_append_printf(str, " total_length: 0x%x\n", |
548 | 0 | (guint) fu_usb_descriptor_hdr_get_total_length(st)); |
549 | 0 | g_string_append_printf(str, " num_interfaces: 0x%x\n", |
550 | 0 | (guint) fu_usb_descriptor_hdr_get_num_interfaces(st)); |
551 | 0 | g_string_append_printf(str, " configuration_value: 0x%x\n", |
552 | 0 | (guint) fu_usb_descriptor_hdr_get_configuration_value(st)); |
553 | 0 | g_string_append_printf(str, " configuration: 0x%x\n", |
554 | 0 | (guint) fu_usb_descriptor_hdr_get_configuration(st)); |
555 | 0 | g_string_append_printf(str, " attributes: 0x%x\n", |
556 | 0 | (guint) fu_usb_descriptor_hdr_get_attributes(st)); |
557 | 0 | g_string_append_printf(str, " max_power: 0x%x\n", |
558 | 0 | (guint) fu_usb_descriptor_hdr_get_max_power(st)); |
559 | 0 | if (str->len > 0) |
560 | 0 | g_string_set_size(str, str->len - 1); |
561 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
562 | 0 | } |
563 | | static gboolean |
564 | | fu_usb_descriptor_hdr_validate_internal(FuUsbDescriptorHdr *st, GError **error) |
565 | 0 | { |
566 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
567 | 0 | if (fu_usb_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_CONFIG) { |
568 | 0 | g_set_error_literal(error, |
569 | 0 | FWUPD_ERROR, |
570 | 0 | FWUPD_ERROR_INVALID_DATA, |
571 | 0 | "constant FuUsbDescriptorHdr.descriptor_type was not valid"); |
572 | 0 | return FALSE; |
573 | 0 | } |
574 | 0 | return TRUE; |
575 | 0 | } |
576 | | static gboolean |
577 | | fu_usb_descriptor_hdr_parse_internal(FuUsbDescriptorHdr *st, GError **error) |
578 | 0 | { |
579 | 0 | if (!fu_usb_descriptor_hdr_validate_internal(st, error)) |
580 | 0 | return FALSE; |
581 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
582 | 0 | g_autofree gchar *str = fu_usb_descriptor_hdr_to_string(st); |
583 | 0 | g_debug("%s", str); |
584 | 0 | } |
585 | 0 | return TRUE; |
586 | 0 | } |
587 | | /** |
588 | | * fu_usb_descriptor_hdr_parse_stream: (skip): |
589 | | **/ |
590 | | FuUsbDescriptorHdr * |
591 | | fu_usb_descriptor_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
592 | 0 | { |
593 | 0 | g_autoptr(GByteArray) st = NULL; |
594 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error); |
595 | 0 | if (st == NULL) { |
596 | 0 | g_prefix_error(error, "FuUsbDescriptorHdr failed read of 0x%x: ", (guint) 9); |
597 | 0 | return NULL; |
598 | 0 | } |
599 | 0 | if (st->len != 9) { |
600 | 0 | g_set_error(error, |
601 | 0 | FWUPD_ERROR, |
602 | 0 | FWUPD_ERROR_INVALID_DATA, |
603 | 0 | "FuUsbDescriptorHdr requested 0x%x and got 0x%x", |
604 | 0 | (guint) 9, |
605 | 0 | (guint) st->len); |
606 | 0 | return NULL; |
607 | 0 | } |
608 | 0 | if (!fu_usb_descriptor_hdr_parse_internal(st, error)) |
609 | 0 | return NULL; |
610 | 0 | return g_steal_pointer(&st); |
611 | 0 | } |
612 | | /* getters */ |
613 | | /** |
614 | | * fu_usb_hid_descriptor_hdr_get_length: (skip): |
615 | | **/ |
616 | | guint8 |
617 | | fu_usb_hid_descriptor_hdr_get_length(const FuUsbHidDescriptorHdr *st) |
618 | 0 | { |
619 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
620 | 0 | return st->data[0]; |
621 | 0 | } |
622 | | /** |
623 | | * fu_usb_hid_descriptor_hdr_get_descriptor_type: (skip): |
624 | | **/ |
625 | | static FuUsbDescriptorKind |
626 | | fu_usb_hid_descriptor_hdr_get_descriptor_type(const FuUsbHidDescriptorHdr *st) |
627 | 0 | { |
628 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
629 | 0 | return st->data[1]; |
630 | 0 | } |
631 | | /** |
632 | | * fu_usb_hid_descriptor_hdr_get_hid: (skip): |
633 | | **/ |
634 | | guint16 |
635 | | fu_usb_hid_descriptor_hdr_get_hid(const FuUsbHidDescriptorHdr *st) |
636 | 0 | { |
637 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
638 | 0 | return fu_memread_uint16(st->data + 2, G_LITTLE_ENDIAN); |
639 | 0 | } |
640 | | /** |
641 | | * fu_usb_hid_descriptor_hdr_get_country_code: (skip): |
642 | | **/ |
643 | | guint8 |
644 | | fu_usb_hid_descriptor_hdr_get_country_code(const FuUsbHidDescriptorHdr *st) |
645 | 0 | { |
646 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
647 | 0 | return st->data[4]; |
648 | 0 | } |
649 | | /** |
650 | | * fu_usb_hid_descriptor_hdr_get_num_descriptors: (skip): |
651 | | **/ |
652 | | guint8 |
653 | | fu_usb_hid_descriptor_hdr_get_num_descriptors(const FuUsbHidDescriptorHdr *st) |
654 | 0 | { |
655 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
656 | 0 | return st->data[5]; |
657 | 0 | } |
658 | | /** |
659 | | * fu_usb_hid_descriptor_hdr_get_class_descriptor_type: (skip): |
660 | | **/ |
661 | | guint8 |
662 | | fu_usb_hid_descriptor_hdr_get_class_descriptor_type(const FuUsbHidDescriptorHdr *st) |
663 | 0 | { |
664 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
665 | 0 | return st->data[6]; |
666 | 0 | } |
667 | | /** |
668 | | * fu_usb_hid_descriptor_hdr_get_class_descriptor_length: (skip): |
669 | | **/ |
670 | | guint16 |
671 | | fu_usb_hid_descriptor_hdr_get_class_descriptor_length(const FuUsbHidDescriptorHdr *st) |
672 | 0 | { |
673 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
674 | 0 | return fu_memread_uint16(st->data + 7, G_LITTLE_ENDIAN); |
675 | 0 | } |
676 | | |
677 | | /* setters */ |
678 | | /** |
679 | | * fu_usb_hid_descriptor_hdr_to_string: (skip): |
680 | | **/ |
681 | | static gchar * |
682 | | fu_usb_hid_descriptor_hdr_to_string(const FuUsbHidDescriptorHdr *st) |
683 | 0 | { |
684 | 0 | g_autoptr(GString) str = g_string_new("FuUsbHidDescriptorHdr:\n"); |
685 | 0 | g_return_val_if_fail(st != NULL, NULL); |
686 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
687 | 0 | (guint) fu_usb_hid_descriptor_hdr_get_length(st)); |
688 | 0 | g_string_append_printf(str, " hid: 0x%x\n", |
689 | 0 | (guint) fu_usb_hid_descriptor_hdr_get_hid(st)); |
690 | 0 | g_string_append_printf(str, " country_code: 0x%x\n", |
691 | 0 | (guint) fu_usb_hid_descriptor_hdr_get_country_code(st)); |
692 | 0 | g_string_append_printf(str, " num_descriptors: 0x%x\n", |
693 | 0 | (guint) fu_usb_hid_descriptor_hdr_get_num_descriptors(st)); |
694 | 0 | g_string_append_printf(str, " class_descriptor_type: 0x%x\n", |
695 | 0 | (guint) fu_usb_hid_descriptor_hdr_get_class_descriptor_type(st)); |
696 | 0 | g_string_append_printf(str, " class_descriptor_length: 0x%x\n", |
697 | 0 | (guint) fu_usb_hid_descriptor_hdr_get_class_descriptor_length(st)); |
698 | 0 | if (str->len > 0) |
699 | 0 | g_string_set_size(str, str->len - 1); |
700 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
701 | 0 | } |
702 | | static gboolean |
703 | | fu_usb_hid_descriptor_hdr_validate_internal(FuUsbHidDescriptorHdr *st, GError **error) |
704 | 0 | { |
705 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
706 | 0 | if (fu_usb_hid_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_HID) { |
707 | 0 | g_set_error_literal(error, |
708 | 0 | FWUPD_ERROR, |
709 | 0 | FWUPD_ERROR_INVALID_DATA, |
710 | 0 | "constant FuUsbHidDescriptorHdr.descriptor_type was not valid"); |
711 | 0 | return FALSE; |
712 | 0 | } |
713 | 0 | return TRUE; |
714 | 0 | } |
715 | | static gboolean |
716 | | fu_usb_hid_descriptor_hdr_parse_internal(FuUsbHidDescriptorHdr *st, GError **error) |
717 | 0 | { |
718 | 0 | if (!fu_usb_hid_descriptor_hdr_validate_internal(st, error)) |
719 | 0 | return FALSE; |
720 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
721 | 0 | g_autofree gchar *str = fu_usb_hid_descriptor_hdr_to_string(st); |
722 | 0 | g_debug("%s", str); |
723 | 0 | } |
724 | 0 | return TRUE; |
725 | 0 | } |
726 | | /** |
727 | | * fu_usb_hid_descriptor_hdr_parse_stream: (skip): |
728 | | **/ |
729 | | FuUsbHidDescriptorHdr * |
730 | | fu_usb_hid_descriptor_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
731 | 0 | { |
732 | 0 | g_autoptr(GByteArray) st = NULL; |
733 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error); |
734 | 0 | if (st == NULL) { |
735 | 0 | g_prefix_error(error, "FuUsbHidDescriptorHdr failed read of 0x%x: ", (guint) 9); |
736 | 0 | return NULL; |
737 | 0 | } |
738 | 0 | if (st->len != 9) { |
739 | 0 | g_set_error(error, |
740 | 0 | FWUPD_ERROR, |
741 | 0 | FWUPD_ERROR_INVALID_DATA, |
742 | 0 | "FuUsbHidDescriptorHdr requested 0x%x and got 0x%x", |
743 | 0 | (guint) 9, |
744 | 0 | (guint) st->len); |
745 | 0 | return NULL; |
746 | 0 | } |
747 | 0 | if (!fu_usb_hid_descriptor_hdr_parse_internal(st, error)) |
748 | 0 | return NULL; |
749 | 0 | return g_steal_pointer(&st); |
750 | 0 | } |
751 | | /* getters */ |
752 | | /** |
753 | | * fu_usb_dfu_descriptor_hdr_get_length: (skip): |
754 | | **/ |
755 | | guint8 |
756 | | fu_usb_dfu_descriptor_hdr_get_length(const FuUsbDfuDescriptorHdr *st) |
757 | 0 | { |
758 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
759 | 0 | return st->data[0]; |
760 | 0 | } |
761 | | /** |
762 | | * fu_usb_dfu_descriptor_hdr_get_descriptor_type: (skip): |
763 | | **/ |
764 | | static FuUsbDescriptorKind |
765 | | fu_usb_dfu_descriptor_hdr_get_descriptor_type(const FuUsbDfuDescriptorHdr *st) |
766 | 0 | { |
767 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
768 | 0 | return st->data[1]; |
769 | 0 | } |
770 | | /** |
771 | | * fu_usb_dfu_descriptor_hdr_get_attributes: (skip): |
772 | | **/ |
773 | | guint8 |
774 | | fu_usb_dfu_descriptor_hdr_get_attributes(const FuUsbDfuDescriptorHdr *st) |
775 | 0 | { |
776 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
777 | 0 | return st->data[2]; |
778 | 0 | } |
779 | | /** |
780 | | * fu_usb_dfu_descriptor_hdr_get_detach_timeout: (skip): |
781 | | **/ |
782 | | guint16 |
783 | | fu_usb_dfu_descriptor_hdr_get_detach_timeout(const FuUsbDfuDescriptorHdr *st) |
784 | 0 | { |
785 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
786 | 0 | return fu_memread_uint16(st->data + 3, G_LITTLE_ENDIAN); |
787 | 0 | } |
788 | | /** |
789 | | * fu_usb_dfu_descriptor_hdr_get_transfer_size: (skip): |
790 | | **/ |
791 | | guint16 |
792 | | fu_usb_dfu_descriptor_hdr_get_transfer_size(const FuUsbDfuDescriptorHdr *st) |
793 | 0 | { |
794 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
795 | 0 | return fu_memread_uint16(st->data + 5, G_LITTLE_ENDIAN); |
796 | 0 | } |
797 | | /** |
798 | | * fu_usb_dfu_descriptor_hdr_get_dfu_version: (skip): |
799 | | **/ |
800 | | guint16 |
801 | | fu_usb_dfu_descriptor_hdr_get_dfu_version(const FuUsbDfuDescriptorHdr *st) |
802 | 0 | { |
803 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
804 | 0 | return fu_memread_uint16(st->data + 7, G_LITTLE_ENDIAN); |
805 | 0 | } |
806 | | |
807 | | /* setters */ |
808 | | /** |
809 | | * fu_usb_dfu_descriptor_hdr_to_string: (skip): |
810 | | **/ |
811 | | static gchar * |
812 | | fu_usb_dfu_descriptor_hdr_to_string(const FuUsbDfuDescriptorHdr *st) |
813 | 0 | { |
814 | 0 | g_autoptr(GString) str = g_string_new("FuUsbDfuDescriptorHdr:\n"); |
815 | 0 | g_return_val_if_fail(st != NULL, NULL); |
816 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
817 | 0 | (guint) fu_usb_dfu_descriptor_hdr_get_length(st)); |
818 | 0 | g_string_append_printf(str, " attributes: 0x%x\n", |
819 | 0 | (guint) fu_usb_dfu_descriptor_hdr_get_attributes(st)); |
820 | 0 | g_string_append_printf(str, " detach_timeout: 0x%x\n", |
821 | 0 | (guint) fu_usb_dfu_descriptor_hdr_get_detach_timeout(st)); |
822 | 0 | g_string_append_printf(str, " transfer_size: 0x%x\n", |
823 | 0 | (guint) fu_usb_dfu_descriptor_hdr_get_transfer_size(st)); |
824 | 0 | g_string_append_printf(str, " dfu_version: 0x%x\n", |
825 | 0 | (guint) fu_usb_dfu_descriptor_hdr_get_dfu_version(st)); |
826 | 0 | if (str->len > 0) |
827 | 0 | g_string_set_size(str, str->len - 1); |
828 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
829 | 0 | } |
830 | | static gboolean |
831 | | fu_usb_dfu_descriptor_hdr_validate_internal(FuUsbDfuDescriptorHdr *st, GError **error) |
832 | 0 | { |
833 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
834 | 0 | if (fu_usb_dfu_descriptor_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_HID) { |
835 | 0 | g_set_error_literal(error, |
836 | 0 | FWUPD_ERROR, |
837 | 0 | FWUPD_ERROR_INVALID_DATA, |
838 | 0 | "constant FuUsbDfuDescriptorHdr.descriptor_type was not valid"); |
839 | 0 | return FALSE; |
840 | 0 | } |
841 | 0 | return TRUE; |
842 | 0 | } |
843 | | static gboolean |
844 | | fu_usb_dfu_descriptor_hdr_parse_internal(FuUsbDfuDescriptorHdr *st, GError **error) |
845 | 0 | { |
846 | 0 | if (!fu_usb_dfu_descriptor_hdr_validate_internal(st, error)) |
847 | 0 | return FALSE; |
848 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
849 | 0 | g_autofree gchar *str = fu_usb_dfu_descriptor_hdr_to_string(st); |
850 | 0 | g_debug("%s", str); |
851 | 0 | } |
852 | 0 | return TRUE; |
853 | 0 | } |
854 | | |
855 | | /** |
856 | | * fu_usb_dfu_descriptor_hdr_parse: (skip): |
857 | | **/ |
858 | | static FuUsbDfuDescriptorHdr * |
859 | | fu_usb_dfu_descriptor_hdr_parse(const guint8 *buf, gsize bufsz, gsize offset, GError **error) |
860 | 0 | { |
861 | 0 | g_autoptr(GByteArray) st = g_byte_array_new(); |
862 | 0 | g_return_val_if_fail(buf != NULL, NULL); |
863 | 0 | g_return_val_if_fail(error == NULL || *error == NULL, NULL); |
864 | 0 | if (!fu_memchk_read(bufsz, offset, 9, error)) { |
865 | 0 | g_prefix_error(error, "invalid struct FuUsbDfuDescriptorHdr: "); |
866 | 0 | return NULL; |
867 | 0 | } |
868 | 0 | g_byte_array_append(st, buf + offset, 9); |
869 | 0 | if (!fu_usb_dfu_descriptor_hdr_parse_internal(st, error)) |
870 | 0 | return NULL; |
871 | 0 | return g_steal_pointer(&st); |
872 | 0 | } |
873 | | /** |
874 | | * fu_usb_dfu_descriptor_hdr_parse_bytes: (skip): |
875 | | **/ |
876 | | FuUsbDfuDescriptorHdr * |
877 | | fu_usb_dfu_descriptor_hdr_parse_bytes(GBytes *blob, gsize offset, GError **error) |
878 | 0 | { |
879 | 0 | gsize bufsz = 0; |
880 | 0 | const guint8 *buf = g_bytes_get_data(blob, &bufsz); |
881 | 0 | return fu_usb_dfu_descriptor_hdr_parse(buf, bufsz, offset, error); |
882 | 0 | } |
883 | | /* getters */ |
884 | | /** |
885 | | * fu_usb_interface_hdr_get_length: (skip): |
886 | | **/ |
887 | | guint8 |
888 | | fu_usb_interface_hdr_get_length(const FuUsbInterfaceHdr *st) |
889 | 0 | { |
890 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
891 | 0 | return st->data[0]; |
892 | 0 | } |
893 | | /** |
894 | | * fu_usb_interface_hdr_get_descriptor_type: (skip): |
895 | | **/ |
896 | | static FuUsbDescriptorKind |
897 | | fu_usb_interface_hdr_get_descriptor_type(const FuUsbInterfaceHdr *st) |
898 | 0 | { |
899 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
900 | 0 | return st->data[1]; |
901 | 0 | } |
902 | | /** |
903 | | * fu_usb_interface_hdr_get_interface_number: (skip): |
904 | | **/ |
905 | | guint8 |
906 | | fu_usb_interface_hdr_get_interface_number(const FuUsbInterfaceHdr *st) |
907 | 0 | { |
908 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
909 | 0 | return st->data[2]; |
910 | 0 | } |
911 | | /** |
912 | | * fu_usb_interface_hdr_get_alternate_setting: (skip): |
913 | | **/ |
914 | | guint8 |
915 | | fu_usb_interface_hdr_get_alternate_setting(const FuUsbInterfaceHdr *st) |
916 | 0 | { |
917 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
918 | 0 | return st->data[3]; |
919 | 0 | } |
920 | | /** |
921 | | * fu_usb_interface_hdr_get_num_endpoints: (skip): |
922 | | **/ |
923 | | guint8 |
924 | | fu_usb_interface_hdr_get_num_endpoints(const FuUsbInterfaceHdr *st) |
925 | 0 | { |
926 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
927 | 0 | return st->data[4]; |
928 | 0 | } |
929 | | /** |
930 | | * fu_usb_interface_hdr_get_interface_class: (skip): |
931 | | **/ |
932 | | FuUsbClass |
933 | | fu_usb_interface_hdr_get_interface_class(const FuUsbInterfaceHdr *st) |
934 | 0 | { |
935 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
936 | 0 | return st->data[5]; |
937 | 0 | } |
938 | | /** |
939 | | * fu_usb_interface_hdr_get_interface_sub_class: (skip): |
940 | | **/ |
941 | | guint8 |
942 | | fu_usb_interface_hdr_get_interface_sub_class(const FuUsbInterfaceHdr *st) |
943 | 0 | { |
944 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
945 | 0 | return st->data[6]; |
946 | 0 | } |
947 | | /** |
948 | | * fu_usb_interface_hdr_get_interface_protocol: (skip): |
949 | | **/ |
950 | | guint8 |
951 | | fu_usb_interface_hdr_get_interface_protocol(const FuUsbInterfaceHdr *st) |
952 | 0 | { |
953 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
954 | 0 | return st->data[7]; |
955 | 0 | } |
956 | | /** |
957 | | * fu_usb_interface_hdr_get_interface: (skip): |
958 | | **/ |
959 | | guint8 |
960 | | fu_usb_interface_hdr_get_interface(const FuUsbInterfaceHdr *st) |
961 | 0 | { |
962 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
963 | 0 | return st->data[8]; |
964 | 0 | } |
965 | | |
966 | | /* setters */ |
967 | | /** |
968 | | * fu_usb_interface_hdr_to_string: (skip): |
969 | | **/ |
970 | | static gchar * |
971 | | fu_usb_interface_hdr_to_string(const FuUsbInterfaceHdr *st) |
972 | 0 | { |
973 | 0 | g_autoptr(GString) str = g_string_new("FuUsbInterfaceHdr:\n"); |
974 | 0 | g_return_val_if_fail(st != NULL, NULL); |
975 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
976 | 0 | (guint) fu_usb_interface_hdr_get_length(st)); |
977 | 0 | g_string_append_printf(str, " interface_number: 0x%x\n", |
978 | 0 | (guint) fu_usb_interface_hdr_get_interface_number(st)); |
979 | 0 | g_string_append_printf(str, " alternate_setting: 0x%x\n", |
980 | 0 | (guint) fu_usb_interface_hdr_get_alternate_setting(st)); |
981 | 0 | g_string_append_printf(str, " num_endpoints: 0x%x\n", |
982 | 0 | (guint) fu_usb_interface_hdr_get_num_endpoints(st)); |
983 | 0 | { |
984 | 0 | const gchar *tmp = fu_usb_class_to_string(fu_usb_interface_hdr_get_interface_class(st)); |
985 | 0 | if (tmp != NULL) { |
986 | 0 | g_string_append_printf(str, " interface_class: 0x%x [%s]\n", (guint) fu_usb_interface_hdr_get_interface_class(st), tmp); |
987 | 0 | } else { |
988 | 0 | g_string_append_printf(str, " interface_class: 0x%x\n", (guint) fu_usb_interface_hdr_get_interface_class(st)); |
989 | 0 | } |
990 | 0 | } |
991 | 0 | g_string_append_printf(str, " interface_sub_class: 0x%x\n", |
992 | 0 | (guint) fu_usb_interface_hdr_get_interface_sub_class(st)); |
993 | 0 | g_string_append_printf(str, " interface_protocol: 0x%x\n", |
994 | 0 | (guint) fu_usb_interface_hdr_get_interface_protocol(st)); |
995 | 0 | g_string_append_printf(str, " interface: 0x%x\n", |
996 | 0 | (guint) fu_usb_interface_hdr_get_interface(st)); |
997 | 0 | if (str->len > 0) |
998 | 0 | g_string_set_size(str, str->len - 1); |
999 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
1000 | 0 | } |
1001 | | static gboolean |
1002 | | fu_usb_interface_hdr_validate_internal(FuUsbInterfaceHdr *st, GError **error) |
1003 | 0 | { |
1004 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
1005 | 0 | if (fu_usb_interface_hdr_get_descriptor_type(st) != FU_USB_DESCRIPTOR_KIND_INTERFACE) { |
1006 | 0 | g_set_error_literal(error, |
1007 | 0 | FWUPD_ERROR, |
1008 | 0 | FWUPD_ERROR_INVALID_DATA, |
1009 | 0 | "constant FuUsbInterfaceHdr.descriptor_type was not valid"); |
1010 | 0 | return FALSE; |
1011 | 0 | } |
1012 | 0 | return TRUE; |
1013 | 0 | } |
1014 | | static gboolean |
1015 | | fu_usb_interface_hdr_parse_internal(FuUsbInterfaceHdr *st, GError **error) |
1016 | 0 | { |
1017 | 0 | if (!fu_usb_interface_hdr_validate_internal(st, error)) |
1018 | 0 | return FALSE; |
1019 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
1020 | 0 | g_autofree gchar *str = fu_usb_interface_hdr_to_string(st); |
1021 | 0 | g_debug("%s", str); |
1022 | 0 | } |
1023 | 0 | return TRUE; |
1024 | 0 | } |
1025 | | /** |
1026 | | * fu_usb_interface_hdr_parse_stream: (skip): |
1027 | | **/ |
1028 | | FuUsbInterfaceHdr * |
1029 | | fu_usb_interface_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
1030 | 0 | { |
1031 | 0 | g_autoptr(GByteArray) st = NULL; |
1032 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 9, NULL, error); |
1033 | 0 | if (st == NULL) { |
1034 | 0 | g_prefix_error(error, "FuUsbInterfaceHdr failed read of 0x%x: ", (guint) 9); |
1035 | 0 | return NULL; |
1036 | 0 | } |
1037 | 0 | if (st->len != 9) { |
1038 | 0 | g_set_error(error, |
1039 | 0 | FWUPD_ERROR, |
1040 | 0 | FWUPD_ERROR_INVALID_DATA, |
1041 | 0 | "FuUsbInterfaceHdr requested 0x%x and got 0x%x", |
1042 | 0 | (guint) 9, |
1043 | 0 | (guint) st->len); |
1044 | 0 | return NULL; |
1045 | 0 | } |
1046 | 0 | if (!fu_usb_interface_hdr_parse_internal(st, error)) |
1047 | 0 | return NULL; |
1048 | 0 | return g_steal_pointer(&st); |
1049 | 0 | } |
1050 | | /* getters */ |
1051 | | /** |
1052 | | * fu_usb_endpoint_hdr_get_length: (skip): |
1053 | | **/ |
1054 | | guint8 |
1055 | | fu_usb_endpoint_hdr_get_length(const FuUsbEndpointHdr *st) |
1056 | 0 | { |
1057 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1058 | 0 | return st->data[0]; |
1059 | 0 | } |
1060 | | /** |
1061 | | * fu_usb_endpoint_hdr_get_descriptor_type: (skip): |
1062 | | **/ |
1063 | | FuUsbDescriptorKind |
1064 | | fu_usb_endpoint_hdr_get_descriptor_type(const FuUsbEndpointHdr *st) |
1065 | 0 | { |
1066 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1067 | 0 | return st->data[1]; |
1068 | 0 | } |
1069 | | /** |
1070 | | * fu_usb_endpoint_hdr_get_endpoint_address: (skip): |
1071 | | **/ |
1072 | | guint8 |
1073 | | fu_usb_endpoint_hdr_get_endpoint_address(const FuUsbEndpointHdr *st) |
1074 | 0 | { |
1075 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1076 | 0 | return st->data[2]; |
1077 | 0 | } |
1078 | | /** |
1079 | | * fu_usb_endpoint_hdr_get_attributes: (skip): |
1080 | | **/ |
1081 | | guint8 |
1082 | | fu_usb_endpoint_hdr_get_attributes(const FuUsbEndpointHdr *st) |
1083 | 0 | { |
1084 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1085 | 0 | return st->data[3]; |
1086 | 0 | } |
1087 | | /** |
1088 | | * fu_usb_endpoint_hdr_get_max_packet_size: (skip): |
1089 | | **/ |
1090 | | guint16 |
1091 | | fu_usb_endpoint_hdr_get_max_packet_size(const FuUsbEndpointHdr *st) |
1092 | 0 | { |
1093 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1094 | 0 | return fu_memread_uint16(st->data + 4, G_LITTLE_ENDIAN); |
1095 | 0 | } |
1096 | | /** |
1097 | | * fu_usb_endpoint_hdr_get_interval: (skip): |
1098 | | **/ |
1099 | | guint8 |
1100 | | fu_usb_endpoint_hdr_get_interval(const FuUsbEndpointHdr *st) |
1101 | 0 | { |
1102 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1103 | 0 | return st->data[6]; |
1104 | 0 | } |
1105 | | |
1106 | | /* setters */ |
1107 | | /** |
1108 | | * fu_usb_endpoint_hdr_to_string: (skip): |
1109 | | **/ |
1110 | | static gchar * |
1111 | | fu_usb_endpoint_hdr_to_string(const FuUsbEndpointHdr *st) |
1112 | 0 | { |
1113 | 0 | g_autoptr(GString) str = g_string_new("FuUsbEndpointHdr:\n"); |
1114 | 0 | g_return_val_if_fail(st != NULL, NULL); |
1115 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
1116 | 0 | (guint) fu_usb_endpoint_hdr_get_length(st)); |
1117 | 0 | { |
1118 | 0 | const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_endpoint_hdr_get_descriptor_type(st)); |
1119 | 0 | if (tmp != NULL) { |
1120 | 0 | g_string_append_printf(str, " descriptor_type: 0x%x [%s]\n", (guint) fu_usb_endpoint_hdr_get_descriptor_type(st), tmp); |
1121 | 0 | } else { |
1122 | 0 | g_string_append_printf(str, " descriptor_type: 0x%x\n", (guint) fu_usb_endpoint_hdr_get_descriptor_type(st)); |
1123 | 0 | } |
1124 | 0 | } |
1125 | 0 | g_string_append_printf(str, " endpoint_address: 0x%x\n", |
1126 | 0 | (guint) fu_usb_endpoint_hdr_get_endpoint_address(st)); |
1127 | 0 | g_string_append_printf(str, " attributes: 0x%x\n", |
1128 | 0 | (guint) fu_usb_endpoint_hdr_get_attributes(st)); |
1129 | 0 | g_string_append_printf(str, " max_packet_size: 0x%x\n", |
1130 | 0 | (guint) fu_usb_endpoint_hdr_get_max_packet_size(st)); |
1131 | 0 | g_string_append_printf(str, " interval: 0x%x\n", |
1132 | 0 | (guint) fu_usb_endpoint_hdr_get_interval(st)); |
1133 | 0 | if (str->len > 0) |
1134 | 0 | g_string_set_size(str, str->len - 1); |
1135 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
1136 | 0 | } |
1137 | | static gboolean |
1138 | | fu_usb_endpoint_hdr_validate_internal(FuUsbEndpointHdr *st, GError **error) |
1139 | 0 | { |
1140 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
1141 | 0 | return TRUE; |
1142 | 0 | } |
1143 | | static gboolean |
1144 | | fu_usb_endpoint_hdr_parse_internal(FuUsbEndpointHdr *st, GError **error) |
1145 | 0 | { |
1146 | 0 | if (!fu_usb_endpoint_hdr_validate_internal(st, error)) |
1147 | 0 | return FALSE; |
1148 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
1149 | 0 | g_autofree gchar *str = fu_usb_endpoint_hdr_to_string(st); |
1150 | 0 | g_debug("%s", str); |
1151 | 0 | } |
1152 | 0 | return TRUE; |
1153 | 0 | } |
1154 | | /** |
1155 | | * fu_usb_endpoint_hdr_parse_stream: (skip): |
1156 | | **/ |
1157 | | FuUsbEndpointHdr * |
1158 | | fu_usb_endpoint_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
1159 | 0 | { |
1160 | 0 | g_autoptr(GByteArray) st = NULL; |
1161 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 7, NULL, error); |
1162 | 0 | if (st == NULL) { |
1163 | 0 | g_prefix_error(error, "FuUsbEndpointHdr failed read of 0x%x: ", (guint) 7); |
1164 | 0 | return NULL; |
1165 | 0 | } |
1166 | 0 | if (st->len != 7) { |
1167 | 0 | g_set_error(error, |
1168 | 0 | FWUPD_ERROR, |
1169 | 0 | FWUPD_ERROR_INVALID_DATA, |
1170 | 0 | "FuUsbEndpointHdr requested 0x%x and got 0x%x", |
1171 | 0 | (guint) 7, |
1172 | 0 | (guint) st->len); |
1173 | 0 | return NULL; |
1174 | 0 | } |
1175 | 0 | if (!fu_usb_endpoint_hdr_parse_internal(st, error)) |
1176 | 0 | return NULL; |
1177 | 0 | return g_steal_pointer(&st); |
1178 | 0 | } |
1179 | | /* getters */ |
1180 | | /** |
1181 | | * fu_usb_bos_hdr_get_length: (skip): |
1182 | | **/ |
1183 | | guint8 |
1184 | | fu_usb_bos_hdr_get_length(const FuUsbBosHdr *st) |
1185 | 0 | { |
1186 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1187 | 0 | return st->data[0]; |
1188 | 0 | } |
1189 | | /** |
1190 | | * fu_usb_bos_hdr_get_descriptor_type: (skip): |
1191 | | **/ |
1192 | | FuUsbDescriptorKind |
1193 | | fu_usb_bos_hdr_get_descriptor_type(const FuUsbBosHdr *st) |
1194 | 0 | { |
1195 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1196 | 0 | return st->data[1]; |
1197 | 0 | } |
1198 | | /** |
1199 | | * fu_usb_bos_hdr_get_dev_capability_type: (skip): |
1200 | | **/ |
1201 | | guint8 |
1202 | | fu_usb_bos_hdr_get_dev_capability_type(const FuUsbBosHdr *st) |
1203 | 0 | { |
1204 | 0 | g_return_val_if_fail(st != NULL, 0x0); |
1205 | 0 | return st->data[2]; |
1206 | 0 | } |
1207 | | |
1208 | | /* setters */ |
1209 | | /** |
1210 | | * fu_usb_bos_hdr_set_length: (skip): |
1211 | | **/ |
1212 | | void |
1213 | | fu_usb_bos_hdr_set_length(FuUsbBosHdr *st, guint8 value) |
1214 | 0 | { |
1215 | 0 | g_return_if_fail(st != NULL); |
1216 | 0 | st->data[0] = value; |
1217 | 0 | } |
1218 | | /** |
1219 | | * fu_usb_bos_hdr_set_descriptor_type: (skip): |
1220 | | **/ |
1221 | | void |
1222 | | fu_usb_bos_hdr_set_descriptor_type(FuUsbBosHdr *st, FuUsbDescriptorKind value) |
1223 | 0 | { |
1224 | 0 | g_return_if_fail(st != NULL); |
1225 | 0 | st->data[1] = value; |
1226 | 0 | } |
1227 | | /** |
1228 | | * fu_usb_bos_hdr_set_dev_capability_type: (skip): |
1229 | | **/ |
1230 | | void |
1231 | | fu_usb_bos_hdr_set_dev_capability_type(FuUsbBosHdr *st, guint8 value) |
1232 | 0 | { |
1233 | 0 | g_return_if_fail(st != NULL); |
1234 | 0 | st->data[2] = value; |
1235 | 0 | } |
1236 | | /** |
1237 | | * fu_usb_bos_hdr_new: (skip): |
1238 | | **/ |
1239 | | FuUsbBosHdr * |
1240 | | fu_usb_bos_hdr_new(void) |
1241 | 0 | { |
1242 | 0 | FuUsbBosHdr *st = g_byte_array_sized_new(3); |
1243 | 0 | fu_byte_array_set_size(st, 3, 0x0); |
1244 | 0 | fu_usb_bos_hdr_set_length(st, 3); |
1245 | 0 | fu_usb_bos_hdr_set_descriptor_type(st, FU_USB_DESCRIPTOR_KIND_BOS); |
1246 | 0 | return st; |
1247 | 0 | } |
1248 | | /** |
1249 | | * fu_usb_bos_hdr_to_string: (skip): |
1250 | | **/ |
1251 | | static gchar * |
1252 | | fu_usb_bos_hdr_to_string(const FuUsbBosHdr *st) |
1253 | 0 | { |
1254 | 0 | g_autoptr(GString) str = g_string_new("FuUsbBosHdr:\n"); |
1255 | 0 | g_return_val_if_fail(st != NULL, NULL); |
1256 | 0 | g_string_append_printf(str, " length: 0x%x\n", |
1257 | 0 | (guint) fu_usb_bos_hdr_get_length(st)); |
1258 | 0 | { |
1259 | 0 | const gchar *tmp = fu_usb_descriptor_kind_to_string(fu_usb_bos_hdr_get_descriptor_type(st)); |
1260 | 0 | if (tmp != NULL) { |
1261 | 0 | g_string_append_printf(str, " descriptor_type: 0x%x [%s]\n", (guint) fu_usb_bos_hdr_get_descriptor_type(st), tmp); |
1262 | 0 | } else { |
1263 | 0 | g_string_append_printf(str, " descriptor_type: 0x%x\n", (guint) fu_usb_bos_hdr_get_descriptor_type(st)); |
1264 | 0 | } |
1265 | 0 | } |
1266 | 0 | g_string_append_printf(str, " dev_capability_type: 0x%x\n", |
1267 | 0 | (guint) fu_usb_bos_hdr_get_dev_capability_type(st)); |
1268 | 0 | if (str->len > 0) |
1269 | 0 | g_string_set_size(str, str->len - 1); |
1270 | 0 | return g_string_free(g_steal_pointer(&str), FALSE); |
1271 | 0 | } |
1272 | | static gboolean |
1273 | | fu_usb_bos_hdr_validate_internal(FuUsbBosHdr *st, GError **error) |
1274 | 0 | { |
1275 | 0 | g_return_val_if_fail(st != NULL, FALSE); |
1276 | 0 | return TRUE; |
1277 | 0 | } |
1278 | | static gboolean |
1279 | | fu_usb_bos_hdr_parse_internal(FuUsbBosHdr *st, GError **error) |
1280 | 0 | { |
1281 | 0 | if (!fu_usb_bos_hdr_validate_internal(st, error)) |
1282 | 0 | return FALSE; |
1283 | 0 | if (g_getenv("FWUPD_VERBOSE") != NULL) { |
1284 | 0 | g_autofree gchar *str = fu_usb_bos_hdr_to_string(st); |
1285 | 0 | g_debug("%s", str); |
1286 | 0 | } |
1287 | 0 | return TRUE; |
1288 | 0 | } |
1289 | | /** |
1290 | | * fu_usb_bos_hdr_parse_stream: (skip): |
1291 | | **/ |
1292 | | FuUsbBosHdr * |
1293 | | fu_usb_bos_hdr_parse_stream(GInputStream *stream, gsize offset, GError **error) |
1294 | 0 | { |
1295 | 0 | g_autoptr(GByteArray) st = NULL; |
1296 | 0 | st = fu_input_stream_read_byte_array(stream, offset, 3, NULL, error); |
1297 | 0 | if (st == NULL) { |
1298 | 0 | g_prefix_error(error, "FuUsbBosHdr failed read of 0x%x: ", (guint) 3); |
1299 | 0 | return NULL; |
1300 | 0 | } |
1301 | 0 | if (st->len != 3) { |
1302 | 0 | g_set_error(error, |
1303 | 0 | FWUPD_ERROR, |
1304 | 0 | FWUPD_ERROR_INVALID_DATA, |
1305 | 0 | "FuUsbBosHdr requested 0x%x and got 0x%x", |
1306 | 0 | (guint) 3, |
1307 | 0 | (guint) st->len); |
1308 | 0 | return NULL; |
1309 | 0 | } |
1310 | 0 | if (!fu_usb_bos_hdr_parse_internal(st, error)) |
1311 | 0 | return NULL; |
1312 | 0 | return g_steal_pointer(&st); |
1313 | 0 | } |