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