1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
|
/*
* Copyright 1999-2021 Logitech, Inc.
* Copyright 2021 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <json-glib/json-glib.h>
#include <string.h>
#include "fu-logitech-bulkcontroller-child.h"
#include "fu-logitech-bulkcontroller-common.h"
#include "fu-logitech-bulkcontroller-device.h"
#include "fu-logitech-bulkcontroller-struct.h"
#define HASH_TIMEOUT 30000
#define UPD_INTERFACE_SUBPROTOCOL_ID 117
#define SYNC_INTERFACE_SUBPROTOCOL_ID 118
#define BULK_TRANSFER_TIMEOUT 2500
#define BULK_TRANSFER_FLUSH_TIMEOUT 5
#define HASH_VALUE_SIZE 16
#define MAX_RETRIES 5
#define MAX_SETUP_RETRIES 50
#define MAX_WAIT_COUNT 150
#define POST_INSTALL_SLEEP_DURATION 80 * 1000 /* ms */
enum { EP_OUT, EP_IN, EP_LAST };
struct _FuLogitechBulkcontrollerDevice {
FuUsbDevice parent_instance;
guint sync_ep[EP_LAST];
guint update_ep[EP_LAST];
guint sync_iface;
guint update_iface;
FuLogitechBulkcontrollerDeviceState status;
FuLogitechBulkcontrollerUpdateState update_status;
guint update_progress; /* percentage value */
gboolean is_sync_flush_events_in_progress;
GString *device_info_response_json;
gsize transfer_bufsz;
guint32 sequence_id;
};
G_DEFINE_TYPE(FuLogitechBulkcontrollerDevice, fu_logitech_bulkcontroller_device, FU_TYPE_USB_DEVICE)
static void
fu_logitech_bulkcontroller_device_to_string(FuDevice *device, guint idt, GString *str)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
fwupd_codec_string_append_hex(str, idt, "BufferSize", self->transfer_bufsz);
fwupd_codec_string_append_hex(str, idt, "SyncIface", self->sync_iface);
fwupd_codec_string_append_hex(str, idt, "UpdateIface", self->update_iface);
fwupd_codec_string_append(str,
idt,
"State",
fu_logitech_bulkcontroller_device_state_to_string(self->status));
fwupd_codec_string_append(
str,
idt,
"UpdateState",
fu_logitech_bulkcontroller_update_state_to_string(self->update_status));
if (self->device_info_response_json->len > 0) {
fwupd_codec_string_append(str,
idt,
"DeviceInfoResponse",
self->device_info_response_json->str);
}
fwupd_codec_string_append_hex(str, idt, "SequenceId", self->sequence_id);
}
static gboolean
fu_logitech_bulkcontroller_device_probe(FuDevice *device, GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
g_autoptr(GPtrArray) intfs = NULL;
intfs = fu_usb_device_get_interfaces(FU_USB_DEVICE(self), error);
if (intfs == NULL)
return FALSE;
for (guint i = 0; i < intfs->len; i++) {
FuUsbInterface *intf = g_ptr_array_index(intfs, i);
if (fu_usb_interface_get_class(intf) == FU_USB_CLASS_VENDOR_SPECIFIC &&
fu_usb_interface_get_protocol(intf) == 0x1) {
if (fu_usb_interface_get_subclass(intf) == SYNC_INTERFACE_SUBPROTOCOL_ID) {
g_autoptr(GPtrArray) endpoints =
fu_usb_interface_get_endpoints(intf);
self->sync_iface = fu_usb_interface_get_number(intf);
if (endpoints == NULL)
continue;
for (guint j = 0; j < endpoints->len; j++) {
FuUsbEndpoint *ep = g_ptr_array_index(endpoints, j);
if (j == EP_OUT)
self->sync_ep[EP_OUT] =
fu_usb_endpoint_get_address(ep);
else
self->sync_ep[EP_IN] =
fu_usb_endpoint_get_address(ep);
}
} else if (fu_usb_interface_get_subclass(intf) ==
UPD_INTERFACE_SUBPROTOCOL_ID) {
g_autoptr(GPtrArray) endpoints =
fu_usb_interface_get_endpoints(intf);
self->update_iface = fu_usb_interface_get_number(intf);
if (endpoints == NULL)
continue;
for (guint j = 0; j < endpoints->len; j++) {
FuUsbEndpoint *ep = g_ptr_array_index(endpoints, j);
if (j == EP_OUT)
self->update_ep[EP_OUT] =
fu_usb_endpoint_get_address(ep);
else
self->update_ep[EP_IN] =
fu_usb_endpoint_get_address(ep);
}
}
}
}
fu_usb_device_add_interface(FU_USB_DEVICE(self), self->update_iface);
fu_usb_device_add_interface(FU_USB_DEVICE(self), self->sync_iface);
return TRUE;
}
typedef struct {
FuLogitechBulkcontrollerCmd cmd;
guint32 sequence_id;
GByteArray *data;
guint timeout;
} FuLogitechBulkcontrollerResponse;
static FuLogitechBulkcontrollerResponse *
fu_logitech_bulkcontroller_device_response_new(void)
{
FuLogitechBulkcontrollerResponse *response = g_new0(FuLogitechBulkcontrollerResponse, 1);
response->data = g_byte_array_new();
return response;
}
static void
fu_logitech_bulkcontroller_device_response_free(FuLogitechBulkcontrollerResponse *response)
{
if (response->data != NULL)
g_byte_array_unref(response->data);
g_free(response);
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(FuLogitechBulkcontrollerResponse,
fu_logitech_bulkcontroller_device_response_free)
static gboolean
fu_logitech_bulkcontroller_device_sync_send_cmd(FuLogitechBulkcontrollerDevice *self,
FuLogitechBulkcontrollerCmd cmd,
GByteArray *buf,
guint timeout,
GError **error)
{
g_autoptr(FuStructLogitechBulkcontrollerSendSyncReq) st_req =
fu_struct_logitech_bulkcontroller_send_sync_req_new();
g_autofree gchar *str = NULL;
/* increment */
self->sequence_id++;
/* send */
fu_struct_logitech_bulkcontroller_send_sync_req_set_cmd(st_req, cmd);
fu_struct_logitech_bulkcontroller_send_sync_req_set_sequence_id(st_req, self->sequence_id);
if (buf != NULL) {
fu_struct_logitech_bulkcontroller_send_sync_req_set_payload_length(st_req,
buf->len);
g_byte_array_append(st_req->buf, buf->data, buf->len);
}
str = fu_struct_logitech_bulkcontroller_send_sync_req_to_string(st_req);
g_debug("sending: %s", str);
if (!fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
self->sync_ep[EP_OUT],
st_req->buf->data,
st_req->buf->len,
NULL, /* transferred */
timeout,
NULL,
error)) {
g_prefix_error_literal(error, "failed to send sync bulk transfer: ");
return FALSE;
}
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_sync_send_ack(FuLogitechBulkcontrollerDevice *self,
FuLogitechBulkcontrollerCmd cmd,
guint timeout,
GError **error)
{
g_autoptr(GByteArray) buf_ack = g_byte_array_new();
fu_byte_array_append_uint32(buf_ack, cmd, G_LITTLE_ENDIAN);
if (!fu_logitech_bulkcontroller_device_sync_send_cmd(self,
FU_LOGITECH_BULKCONTROLLER_CMD_ACK,
buf_ack,
timeout,
error)) {
g_prefix_error(error,
"failed to send ack for %s: ",
fu_logitech_bulkcontroller_cmd_to_string(cmd));
return FALSE;
}
return TRUE;
}
static FuLogitechBulkcontrollerResponse *
fu_logitech_bulkcontroller_device_sync_wait_any(FuLogitechBulkcontrollerDevice *self,
guint timeout,
GError **error)
{
gsize actual_length = 0;
g_autofree guint8 *buf = g_malloc0(self->transfer_bufsz);
g_autoptr(FuStructLogitechBulkcontrollerSendSyncRes) st = NULL;
g_autoptr(FuLogitechBulkcontrollerResponse) response =
fu_logitech_bulkcontroller_device_response_new();
if (!fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
self->sync_ep[EP_IN],
buf,
self->transfer_bufsz,
&actual_length,
timeout,
NULL,
error)) {
g_prefix_error_literal(error, "failed to receive: ");
return NULL;
}
fu_dump_raw(G_LOG_DOMAIN, "response", buf, MIN(actual_length, 12));
st = fu_struct_logitech_bulkcontroller_send_sync_res_parse(buf,
self->transfer_bufsz,
0x0,
error);
if (st == NULL)
return NULL;
response->cmd = fu_struct_logitech_bulkcontroller_send_sync_res_get_cmd(st);
response->sequence_id = fu_struct_logitech_bulkcontroller_send_sync_res_get_sequence_id(st);
g_byte_array_append(response->data,
buf + st->buf->len,
fu_struct_logitech_bulkcontroller_send_sync_res_get_payload_length(st));
/* no payload for UninitBuffer, skip check */
if ((response->cmd != FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER) &&
(response->data->len == 0)) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
"failed to receive packet");
return NULL;
}
return g_steal_pointer(&response);
}
static GByteArray *
fu_logitech_bulkcontroller_device_sync_wait_cmd(FuLogitechBulkcontrollerDevice *self,
FuLogitechBulkcontrollerCmd cmd,
guint32 sequence_id,
guint timeout,
GError **error)
{
g_autoptr(FuLogitechBulkcontrollerResponse) response = NULL;
response = fu_logitech_bulkcontroller_device_sync_wait_any(self, timeout, error);
if (response == NULL)
return NULL;
if (response->cmd != cmd) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"command invalid, expected %s and got %s",
fu_logitech_bulkcontroller_cmd_to_string(cmd),
fu_logitech_bulkcontroller_cmd_to_string(response->cmd));
return NULL;
}
/* verify the sequence ID */
if (response->sequence_id != sequence_id) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"sequence ID invalid, expected 0x%04x and got 0x%04x",
sequence_id,
response->sequence_id);
return NULL;
}
/* success */
return g_steal_pointer(&response->data);
}
static gboolean
fu_logitech_bulkcontroller_device_sync_wait_cmd_retry_cb(FuDevice *device,
gpointer user_data,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
FuLogitechBulkcontrollerResponse *helper = (FuLogitechBulkcontrollerResponse *)user_data;
helper->data = fu_logitech_bulkcontroller_device_sync_wait_cmd(self,
helper->cmd,
helper->sequence_id,
helper->timeout,
error);
if (helper->data == NULL)
return FALSE;
/* success */
return TRUE;
}
static GByteArray *
fu_logitech_bulkcontroller_device_sync_wait_cmd_retry(FuLogitechBulkcontrollerDevice *self,
FuLogitechBulkcontrollerCmd cmd,
guint32 sequence_id,
guint timeout,
GError **error)
{
FuLogitechBulkcontrollerResponse helper = {.cmd = cmd,
.sequence_id = sequence_id,
.timeout = timeout};
if (!fu_device_retry(FU_DEVICE(self),
fu_logitech_bulkcontroller_device_sync_wait_cmd_retry_cb,
MAX_RETRIES,
&helper,
error))
return NULL;
return helper.data;
}
static gboolean
fu_logitech_bulkcontroller_device_sync_check_ack_cmd(GByteArray *buf,
FuLogitechBulkcontrollerCmd cmd,
GError **error)
{
gchar ack_payload[6] = {0x0};
guint64 ack_cmd = 0;
/* this is weird; base 10 number as ASCII as the ack payload... */
if (!fu_memcpy_safe((guint8 *)ack_payload,
sizeof(ack_payload),
0x0,
buf->data,
buf->len,
0x0,
sizeof(ack_payload) - 1,
error)) {
g_prefix_error_literal(error, "failed to copy ack payload: ");
return FALSE;
}
fu_dump_raw(G_LOG_DOMAIN, "ack_payload", (guint8 *)ack_payload, sizeof(ack_payload));
if (!fu_strtoull((const gchar *)ack_payload,
&ack_cmd,
0,
G_MAXUINT32,
FU_INTEGER_BASE_AUTO,
error)) {
g_prefix_error_literal(error, "failed to parse ack payload cmd: ");
return FALSE;
}
g_debug("ack_cmd: %s [0x%x]",
fu_logitech_bulkcontroller_cmd_to_string(ack_cmd),
(guint)ack_cmd);
if (ack_cmd != cmd) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"command invalid, expected %s and got %s",
fu_logitech_bulkcontroller_cmd_to_string(cmd),
fu_logitech_bulkcontroller_cmd_to_string(ack_cmd));
return FALSE;
}
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_sync_wait_ack_cb(FuDevice *device,
gpointer user_data,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
FuLogitechBulkcontrollerResponse *helper = (FuLogitechBulkcontrollerResponse *)user_data;
g_autoptr(GByteArray) buf = NULL;
buf = fu_logitech_bulkcontroller_device_sync_wait_cmd(self,
FU_LOGITECH_BULKCONTROLLER_CMD_ACK,
self->sequence_id,
helper->timeout,
error);
if (buf == NULL)
return FALSE;
if (!fu_logitech_bulkcontroller_device_sync_check_ack_cmd(buf, helper->cmd, error))
return FALSE;
/* success */
return TRUE;
}
/* send command and wait for ACK */
static gboolean
fu_logitech_bulkcontroller_device_sync_wait_ack(FuLogitechBulkcontrollerDevice *self,
FuLogitechBulkcontrollerCmd cmd,
guint timeout,
GError **error)
{
FuLogitechBulkcontrollerResponse helper = {.cmd = cmd, .timeout = timeout};
return fu_device_retry_full(FU_DEVICE(self),
fu_logitech_bulkcontroller_device_sync_wait_ack_cb,
10,
200,
&helper,
error);
}
static gboolean
fu_logitech_bulkcontroller_device_sync_check_ack(FuLogitechBulkcontrollerDevice *self,
FuLogitechBulkcontrollerResponse *response,
FuLogitechBulkcontrollerCmd cmd,
GError **error)
{
/* verify the sequence ID */
if (response->sequence_id != self->sequence_id) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"sequence ID invalid, expected 0x%04x and got 0x%04x",
self->sequence_id,
response->sequence_id);
return FALSE;
}
return fu_logitech_bulkcontroller_device_sync_check_ack_cmd(response->data, cmd, error);
}
static GByteArray *
fu_logitech_bulkcontroller_device_sync_write(FuLogitechBulkcontrollerDevice *self,
GByteArray *req,
GError **error)
{
g_autoptr(GByteArray) res_ack = NULL;
g_autoptr(GByteArray) res_read = NULL;
g_autoptr(GByteArray) buf = NULL;
/* send host->device buffer-write */
if (!fu_logitech_bulkcontroller_device_sync_send_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_WRITE,
req,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to send request: ");
return NULL;
}
/* wait device->host ack */
if (!fu_logitech_bulkcontroller_device_sync_wait_ack(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_WRITE,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to wait for ack: ");
return NULL;
}
/* send host->device buffer-uninit */
if (!fu_logitech_bulkcontroller_device_sync_send_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER,
NULL,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to uninit buffer: ");
return NULL;
}
/* wait device->host buffer-read|ack */
do {
g_autoptr(FuLogitechBulkcontrollerResponse) response_tmp = NULL;
response_tmp =
fu_logitech_bulkcontroller_device_sync_wait_any(self,
BULK_TRANSFER_TIMEOUT,
error);
if (response_tmp == NULL) {
g_prefix_error_literal(error, "failed to wait for any: ");
return NULL;
}
if (response_tmp->cmd == FU_LOGITECH_BULKCONTROLLER_CMD_ACK) {
if (res_ack != NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"already received ack");
return NULL;
}
if (!fu_logitech_bulkcontroller_device_sync_check_ack(
self,
response_tmp,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER,
error)) {
g_prefix_error_literal(error, "failed to check uninit buffer: ");
return NULL;
}
res_ack = g_steal_pointer(&response_tmp->data);
} else if (response_tmp->cmd == FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_READ) {
if (res_read != NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"already received read-buffer");
return NULL;
}
res_read = g_steal_pointer(&response_tmp->data);
}
} while (res_ack == NULL || res_read == NULL);
/* send host->device ack */
if (!fu_logitech_bulkcontroller_device_sync_send_ack(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_READ,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to ack read buffer: ");
return NULL;
}
/* wait device->host uninit */
buf = fu_logitech_bulkcontroller_device_sync_wait_cmd_retry(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER,
0x0, /* why? */
BULK_TRANSFER_TIMEOUT,
error);
if (buf == NULL) {
g_prefix_error_literal(error, "failed to wait for uninit buffer: ");
return NULL;
}
/* send host->device ack */
if (!fu_logitech_bulkcontroller_device_sync_send_ack(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to ack uninit buffer: ");
return NULL;
}
/* success */
return g_steal_pointer(&res_read);
}
static gboolean
fu_logitech_bulkcontroller_device_upd_send_cmd(FuLogitechBulkcontrollerDevice *self,
guint32 cmd,
GBytes *buf,
guint timeout,
GError **error)
{
gsize actual_length = 0;
g_autofree guint8 *buf_tmp = g_malloc0(self->transfer_bufsz);
g_autoptr(FuStructLogitechBulkcontrollerUpdateRes) st_res = NULL;
g_autoptr(FuStructLogitechBulkcontrollerUpdateReq) st_pkt =
fu_struct_logitech_bulkcontroller_update_req_new();
fu_struct_logitech_bulkcontroller_update_req_set_cmd(st_pkt, cmd);
if (buf != NULL) {
fu_struct_logitech_bulkcontroller_update_req_set_payload_length(
st_pkt,
g_bytes_get_size(buf));
fu_byte_array_append_bytes(st_pkt->buf, buf);
}
fu_dump_raw(G_LOG_DOMAIN, "request", st_pkt->buf->data, MIN(st_pkt->buf->len, 12));
if (!fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
self->update_ep[EP_OUT],
st_pkt->buf->data,
st_pkt->buf->len,
NULL, /* transferred */
BULK_TRANSFER_TIMEOUT,
NULL,
error)) {
g_prefix_error_literal(error, "failed to send upd bulk transfer: ");
fwupd_error_convert(error);
return FALSE;
}
/* receiving ACK */
if (!fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
self->update_ep[EP_IN],
buf_tmp,
self->transfer_bufsz,
&actual_length,
timeout,
NULL,
error)) {
g_prefix_error_literal(error, "failed to receive: ");
return FALSE;
}
fu_dump_raw(G_LOG_DOMAIN, "response", buf_tmp, MIN(actual_length, 12));
st_res = fu_struct_logitech_bulkcontroller_update_res_parse(buf_tmp,
self->transfer_bufsz,
0x0,
error);
if (st_res == NULL)
return FALSE;
if (fu_struct_logitech_bulkcontroller_update_res_get_cmd(st_res) !=
FU_LOGITECH_BULKCONTROLLER_CMD_ACK) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"not CMD_ACK, got %s",
fu_logitech_bulkcontroller_cmd_to_string(
fu_struct_logitech_bulkcontroller_update_res_get_cmd(st_res)));
return FALSE;
}
if (fu_struct_logitech_bulkcontroller_update_res_get_cmd_req(st_res) != cmd) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"invalid upd message received, expected %s, got %s",
fu_logitech_bulkcontroller_cmd_to_string(cmd),
fu_logitech_bulkcontroller_cmd_to_string(
fu_struct_logitech_bulkcontroller_update_res_get_cmd_req(st_res)));
return FALSE;
}
return TRUE;
}
static FwupdStatus
fu_logitech_bulkcontroller_device_update_state_to_status(
FuLogitechBulkcontrollerUpdateState update_state)
{
if (update_state == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_DOWNLOADING)
return FWUPD_STATUS_DEVICE_WRITE;
if (update_state == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_STARTING)
return FWUPD_STATUS_DEVICE_VERIFY;
if (update_state == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_UPDATING)
return FWUPD_STATUS_DEVICE_WRITE;
if (update_state == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_CURRENT)
return FWUPD_STATUS_IDLE;
return FWUPD_STATUS_UNKNOWN;
}
static gboolean
fu_logitech_bulkcontroller_device_ensure_child(FuLogitechBulkcontrollerDevice *self,
JsonObject *json_device,
GError **error)
{
FuLogitechBulkcontrollerDeviceState status;
GPtrArray *children = fu_device_get_children(FU_DEVICE(self));
g_autoptr(FuDevice) child = NULL;
const gchar *name;
const gchar *required_members[] = {
"make",
"model",
"name",
"status",
"sw",
"type",
"vid",
};
/* sanity check */
for (guint i = 0; i < G_N_ELEMENTS(required_members); i++) {
if (!json_object_has_member(json_device, required_members[i])) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"no %s",
required_members[i]);
return FALSE;
}
}
if (g_strcmp0(json_object_get_string_member(json_device, "type"), "Sentinel") != 0)
return TRUE;
/* check status */
status = json_object_get_int_member(json_device, "status");
if (status != FU_LOGITECH_BULKCONTROLLER_DEVICE_STATE_ONLINE) {
g_set_error(error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"status is %s",
fu_logitech_bulkcontroller_device_state_to_string(status));
return FALSE;
}
/* child already exists */
name = json_object_get_string_member(json_device, "name");
for (guint i = 0; i < children->len; i++) {
FuDevice *child_tmp = g_ptr_array_index(children, i);
if (g_strcmp0(fu_device_get_logical_id(child_tmp), name) == 0) {
g_debug("found existing %s device, just updating version", name);
fu_device_set_version(child_tmp,
json_object_get_string_member(json_device, "sw"));
return TRUE;
}
}
/* create new child */
child = g_object_new(FU_TYPE_LOGITECH_BULKCONTROLLER_CHILD, "proxy", self, NULL);
fu_device_add_private_flag(child, FU_DEVICE_PRIVATE_FLAG_REFCOUNTED_PROXY);
fu_device_set_name(child, name);
fu_device_set_vendor(child, json_object_get_string_member(json_device, "make"));
fu_device_set_logical_id(child, name);
fu_device_set_version(child, json_object_get_string_member(json_device, "sw"));
if (json_object_has_member(json_device, "serial"))
fu_device_set_serial(child, json_object_get_string_member(json_device, "serial"));
fu_device_add_instance_strup(child,
"MODEL",
json_object_get_string_member(json_device, "model"));
if (!fu_device_build_instance_id(child, error, "LOGI", "MODEL", NULL))
return FALSE;
fu_device_add_child(FU_DEVICE(self), child);
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_json_parser(FuLogitechBulkcontrollerDevice *self,
GByteArray *decoded_pkt,
GError **error)
{
JsonArray *json_devices;
JsonNode *json_root;
JsonObject *json_device;
JsonObject *json_object;
JsonObject *json_payload;
g_autoptr(JsonParser) json_parser = json_parser_new();
/* parse JSON reply */
if (!json_parser_load_from_data(json_parser,
(const gchar *)decoded_pkt->data,
decoded_pkt->len,
error)) {
g_prefix_error_literal(error, "failed to parse json data: ");
return FALSE;
}
json_root = json_parser_get_root(json_parser);
if (json_root == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"did not get JSON root");
return FALSE;
}
json_object = json_node_get_object(json_root);
json_payload = json_object_get_object_member(json_object, "payload");
if (json_payload == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"did not get JSON payload");
return FALSE;
}
json_devices = json_object_get_array_member(json_payload, "devices");
if (json_devices == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"did not get JSON devices");
return FALSE;
}
json_device = json_array_get_object_element(json_devices, 0);
if (json_device == NULL) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"did not get JSON device");
return FALSE;
}
if (json_object_has_member(json_device, "name"))
fu_device_set_name(FU_DEVICE(self),
json_object_get_string_member(json_device, "name"));
if (json_object_has_member(json_device, "sw"))
fu_device_set_version(FU_DEVICE(self),
json_object_get_string_member(json_device, "sw"));
if (json_object_has_member(json_device, "type")) {
fu_device_add_instance_id_full(FU_DEVICE(self),
json_object_get_string_member(json_device, "type"),
FU_DEVICE_INSTANCE_FLAG_QUIRKS);
}
if (json_object_has_member(json_device, "status"))
self->status = json_object_get_int_member(json_device, "status");
if (json_object_has_member(json_device, "updateStatus"))
self->update_status = json_object_get_int_member(json_device, "updateStatus");
/* updateProgress only available while firmware upgrade is going on */
if (json_object_has_member(json_device, "updateProgress"))
self->update_progress = json_object_get_int_member(json_device, "updateProgress");
/* ensure child pheripheral devices exist */
for (guint i = 1; i < json_array_get_length(json_devices); i++) {
JsonObject *json_child = json_array_get_object_element(json_devices, i);
g_autoptr(GError) error_local = NULL;
if (!fu_logitech_bulkcontroller_device_ensure_child(self, json_child, &error_local))
g_warning("failed to add child: %s", error_local->message);
}
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_parse_info(FuLogitechBulkcontrollerDevice *self,
GByteArray *buf,
GError **error)
{
FuLogitechBulkcontrollerProtoId proto_id = kProtoId_UnknownId;
g_autofree gchar *bufstr = NULL;
g_autoptr(GByteArray) decoded_pkt = NULL;
decoded_pkt = fu_logitech_bulkcontroller_proto_manager_decode_message(buf->data,
buf->len,
&proto_id,
error);
if (decoded_pkt == NULL) {
g_prefix_error_literal(error, "failed to unpack packet for device info request: ");
return FALSE;
}
bufstr = fu_strsafe((const gchar *)decoded_pkt->data, decoded_pkt->len);
g_debug("received device response: id: %u, length %u, data: %s",
proto_id,
buf->len,
bufstr);
if (proto_id != kProtoId_GetDeviceInfoResponse && proto_id != kProtoId_KongEvent) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"incorrect response for device info request");
return FALSE;
}
if (!fu_logitech_bulkcontroller_device_json_parser(self, decoded_pkt, error))
return FALSE;
/* success */
g_string_assign(self->device_info_response_json, bufstr);
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_ensure_info_cb(FuDevice *device,
gpointer user_data,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
g_autoptr(GByteArray) buf = NULL;
gboolean send_req = *(gboolean *)user_data;
/* sending GetDeviceInfoRequest. Device reports quite a few matrix, including status,
* progress etc
* Two ways to get data from device:
* 1. Listen for the data broadcasted by device, while firmware upgrade is going on
* 2. Make explicit request to the device. Used when data is needed before/after firmware
* upgrade
*/
if (send_req) {
g_autoptr(GByteArray) device_request =
fu_logitech_bulkcontroller_proto_manager_generate_get_device_info_request(self);
buf = fu_logitech_bulkcontroller_device_sync_write(self, device_request, error);
if (buf == NULL)
return FALSE;
} else {
/* poll the out interface */
buf = fu_logitech_bulkcontroller_device_sync_wait_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_READ,
0x0, /* sequence_id */
BULK_TRANSFER_TIMEOUT,
error);
if (buf == NULL)
return FALSE;
}
return fu_logitech_bulkcontroller_device_parse_info(self, buf, error);
}
static gboolean
fu_logitech_bulkcontroller_device_ensure_info(FuLogitechBulkcontrollerDevice *self,
gboolean send_req,
GError **error)
{
return fu_device_retry(FU_DEVICE(self),
fu_logitech_bulkcontroller_device_ensure_info_cb,
MAX_SETUP_RETRIES,
&send_req,
error);
}
static gboolean
fu_logitech_bulkcontroller_device_upd_send_init_cmd_cb(FuDevice *device,
gpointer user_data,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
return fu_logitech_bulkcontroller_device_upd_send_cmd(self,
FU_LOGITECH_BULKCONTROLLER_CMD_INIT,
NULL,
BULK_TRANSFER_TIMEOUT,
error);
}
static gboolean
fu_logitech_bulkcontroller_device_write_fw(FuLogitechBulkcontrollerDevice *self,
GInputStream *stream,
FuProgress *progress,
GError **error)
{
g_autoptr(FuChunkArray) chunks = NULL;
chunks = fu_chunk_array_new_from_stream(
stream,
FU_CHUNK_ADDR_OFFSET_NONE,
FU_CHUNK_PAGESZ_NONE,
self->transfer_bufsz - FU_STRUCT_LOGITECH_BULKCONTROLLER_UPDATE_REQ_SIZE,
error);
if (chunks == NULL)
return FALSE;
fu_progress_set_id(progress, G_STRLOC);
fu_progress_set_steps(progress, fu_chunk_array_length(chunks));
for (guint i = 0; i < fu_chunk_array_length(chunks); i++) {
g_autoptr(FuChunk) chk = NULL;
g_autoptr(GBytes) chk_blob = NULL;
/* prepare chunk */
chk = fu_chunk_array_index(chunks, i, error);
if (chk == NULL)
return FALSE;
chk_blob = fu_chunk_get_bytes(chk);
if (!fu_logitech_bulkcontroller_device_upd_send_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_DATA_TRANSFER,
chk_blob,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error(error, "failed to send data packet 0x%x: ", i);
return FALSE;
}
fu_progress_step_done(progress);
}
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_verify_cb(FuDevice *device, gpointer user_data, GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
FuProgress *progress = FU_PROGRESS(user_data);
g_autoptr(GError) error_local = NULL;
g_autoptr(GByteArray) buf = NULL;
g_autoptr(GByteArray) uninit_buf = NULL;
/*
* poll the out interface. Device mandates following read flow on SYNC interface
* Device->Host FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_READ
* Host->Device FU_LOGITECH_BULKCONTROLLER_CMD_ACK
* Device->Host FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER
* Host->Device FU_LOGITECH_BULKCONTROLLER_CMD_ACK
*
* use lower timeout to quickly flush any kUpdateStateDownloading progress events,
* accumulated while host busy sending firmware image. Device queue stores upto 100 events.
* These events are not removed from the queue, unless properly acknowledged
*/
buf = fu_logitech_bulkcontroller_device_sync_wait_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_READ,
0x0, /* sequence_id */
(self->is_sync_flush_events_in_progress) ? BULK_TRANSFER_FLUSH_TIMEOUT
: BULK_TRANSFER_TIMEOUT,
error);
if (buf == NULL)
return FALSE;
/* send host->device ack */
if (!fu_logitech_bulkcontroller_device_sync_send_ack(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_BUFFER_READ,
(self->is_sync_flush_events_in_progress) ? BULK_TRANSFER_FLUSH_TIMEOUT
: BULK_TRANSFER_TIMEOUT,
error))
return FALSE;
/* wait device->host uninit */
uninit_buf = fu_logitech_bulkcontroller_device_sync_wait_cmd_retry(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER,
0x0, /* why? */
(self->is_sync_flush_events_in_progress) ? BULK_TRANSFER_FLUSH_TIMEOUT
: BULK_TRANSFER_TIMEOUT,
error);
if (uninit_buf == NULL)
return FALSE;
/* send host->device ack */
if (!fu_logitech_bulkcontroller_device_sync_send_ack(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT_BUFFER,
(self->is_sync_flush_events_in_progress) ? BULK_TRANSFER_FLUSH_TIMEOUT
: BULK_TRANSFER_TIMEOUT,
error))
return FALSE;
if (buf == NULL) {
g_autoptr(GByteArray) device_request = NULL;
g_debug("manually requesting as no pending request: %s", error_local->message);
device_request =
fu_logitech_bulkcontroller_proto_manager_generate_get_device_info_request(self);
buf = fu_logitech_bulkcontroller_device_sync_write(self, device_request, error);
if (buf == NULL)
return FALSE;
}
if (!fu_logitech_bulkcontroller_device_parse_info(self, buf, error))
return FALSE;
g_debug("firmware update status: %s, progress: %u",
fu_logitech_bulkcontroller_update_state_to_string(self->update_status),
self->update_progress);
/* events are not sorted, so stale downloading events can appear anytime */
if (self->update_status == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_DOWNLOADING) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_BUSY,
"waiting for download to finish");
self->is_sync_flush_events_in_progress = TRUE;
return FALSE;
} else {
self->is_sync_flush_events_in_progress = FALSE;
}
fu_progress_set_status(
progress,
fu_logitech_bulkcontroller_device_update_state_to_status(self->update_status));
/* existing device image version is same as newly pushed image? */
if (self->update_status == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_ERROR ||
self->update_status == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_CURRENT)
return TRUE;
/* only update the child if the percentage is bigger -- which means the progressbar
* may stall, but will never go backwards */
if (self->update_progress > fu_progress_get_percentage(progress))
fu_progress_set_percentage(progress, self->update_progress);
/* keep waiting */
g_set_error_literal(error, FWUPD_ERROR, FWUPD_ERROR_BUSY, "waiting for verify to finish");
return FALSE;
}
static gboolean
fu_logitech_bulkcontroller_device_write_firmware(FuDevice *device,
FuFirmware *firmware,
FuProgress *progress,
FwupdInstallFlags flags,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
gsize streamsz = 0;
g_autofree gchar *base64hash = NULL;
g_autofree gchar *md5_str = NULL;
g_autoptr(GByteArray) md5_buf = NULL;
g_autoptr(GByteArray) end_pkt = g_byte_array_new();
g_autoptr(GByteArray) start_pkt = g_byte_array_new();
g_autoptr(GInputStream) stream = NULL;
g_autoptr(GBytes) end_pkt_blob = NULL;
g_autoptr(GBytes) start_pkt_blob = NULL;
/* progress */
fu_progress_set_id(progress, G_STRLOC);
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 4, "checksum-stream");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 1, "init");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 51, "device-write-blocks");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 2, "end-transfer");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 2, "uninit");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_VERIFY, 40, NULL);
/* get default image */
stream = fu_firmware_get_stream(firmware, error);
if (stream == NULL)
return FALSE;
/* calculate firmware checksum, weirdly as a base64 string */
md5_str = fu_firmware_get_checksum(firmware, G_CHECKSUM_MD5, error);
md5_buf = fu_byte_array_from_string(md5_str, error);
if (md5_buf == NULL)
return FALSE;
base64hash = g_base64_encode(md5_buf->data, md5_buf->len);
fu_progress_step_done(progress);
/* sending INIT. Retry if device is not in IDLE state to receive the file */
if (!fu_device_retry(device,
fu_logitech_bulkcontroller_device_upd_send_init_cmd_cb,
MAX_RETRIES,
NULL,
error)) {
g_prefix_error_literal(
error,
"failed to write init transfer packet: please reboot the device: ");
return FALSE;
}
/* transfer sent */
if (!fu_input_stream_size(stream, &streamsz, error))
return FALSE;
fu_byte_array_append_uint64(start_pkt, streamsz, G_LITTLE_ENDIAN);
start_pkt_blob = g_bytes_new(start_pkt->data, start_pkt->len);
if (!fu_logitech_bulkcontroller_device_upd_send_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_START_TRANSFER,
start_pkt_blob,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to write start transfer packet: ");
return FALSE;
}
fu_progress_step_done(progress);
/* push each block to device */
if (!fu_logitech_bulkcontroller_device_write_fw(self,
stream,
fu_progress_get_child(progress),
error)) {
g_prefix_error_literal(error, "failed to write firmware: ");
return FALSE;
}
fu_progress_step_done(progress);
/* sending end transfer -- extend the bulk transfer timeout value, as android device takes
* some time to calculate the hash and respond */
fu_byte_array_append_uint32(end_pkt, 1, G_LITTLE_ENDIAN); /* update */
fu_byte_array_append_uint32(end_pkt, 0, G_LITTLE_ENDIAN); /* force */
fu_byte_array_append_uint32(end_pkt,
FU_LOGITECH_BULKCONTROLLER_CHECKSUM_TYPE_MD5,
G_LITTLE_ENDIAN);
g_byte_array_append(end_pkt, (const guint8 *)base64hash, strlen(base64hash));
end_pkt_blob = g_bytes_new(end_pkt->data, end_pkt->len);
if (!fu_logitech_bulkcontroller_device_upd_send_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_END_TRANSFER,
end_pkt_blob,
HASH_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to write end transfer packet: ");
return FALSE;
}
fu_progress_step_done(progress);
/* send uninit */
if (!fu_logitech_bulkcontroller_device_upd_send_cmd(self,
FU_LOGITECH_BULKCONTROLLER_CMD_UNINIT,
NULL,
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to write finish transfer packet: ");
return FALSE;
}
fu_progress_step_done(progress);
/*
* image file pushed. Device validates and uploads new image on inactive partition.
* Restart sync cb, to get the update progress
* Flush any kUpdateStateDownloading progress events, accumulated while busy sending
* firmware image. Peripheral device needs higher delay
* Normally status changes as follows:
* While image being pushed: kUpdateStateCurrent->kUpdateStateDownloading (~5minutes)
* After image push is complete: kUpdateStateDownloading->kUpdateStateReady
* Validating image: kUpdateStateReady->kUpdateStateStarting
* Uploading image: kUpdateStateStarting->kUpdateStateUpdating
* Upload finished: kUpdateStateUpdating->kUpdateStateCurrent (~5minutes)
* After upload is finished, device reboots itself
*/
if (fu_device_has_private_flag(device,
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_PHERIPHERAL_UPDATE))
self->is_sync_flush_events_in_progress = FALSE;
else
self->is_sync_flush_events_in_progress = TRUE;
if (!fu_device_retry_full(device,
fu_logitech_bulkcontroller_device_verify_cb,
5000, /* over 10 minutes */
(self->is_sync_flush_events_in_progress) ? 5 : 250, /* ms */
fu_progress_get_child(progress),
error))
return FALSE;
if (self->update_status == FU_LOGITECH_BULKCONTROLLER_UPDATE_STATE_ERROR) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"firmware upgrade failed");
return FALSE;
}
fu_progress_step_done(progress);
/* success! */
if (fu_device_has_private_flag(device,
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_PHERIPHERAL_UPDATE)) {
/* skip replug if updating child. parent waits till child reboot with new firmware
*/
g_debug("child firmware updated");
fu_device_remove_private_flag(
device,
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_PHERIPHERAL_UPDATE);
} else {
fu_device_set_remove_delay(FU_DEVICE(self),
10 * 60 * 1000); /* >1 min to finish init */
fu_device_add_flag(device, FWUPD_DEVICE_FLAG_WAIT_FOR_REPLUG);
}
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_send_time_cb(FuDevice *device, gpointer user_data, GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
FuLogitechBulkcontrollerProtoId proto_id = kProtoId_UnknownId;
g_autofree gchar *bufstr = NULL;
g_autoptr(GByteArray) decoded_pkt = NULL;
g_autoptr(GByteArray) device_request = NULL;
g_autoptr(GByteArray) buf = NULL;
/* send SetDeviceTimeRequest to sync device clock with host */
device_request =
fu_logitech_bulkcontroller_proto_manager_generate_set_device_time_request(self, error);
if (device_request == NULL)
return FALSE;
buf = fu_logitech_bulkcontroller_device_sync_write(self, device_request, error);
if (buf == NULL)
return FALSE;
decoded_pkt = fu_logitech_bulkcontroller_proto_manager_decode_message(buf->data,
buf->len,
&proto_id,
error);
if (decoded_pkt == NULL) {
g_prefix_error_literal(error, "failed to unpack packet: ");
return FALSE;
}
bufstr = fu_strsafe((const gchar *)decoded_pkt->data, decoded_pkt->len);
g_debug("received device response while processing: id: %u, length %u, data: %s",
proto_id,
buf->len,
bufstr);
if (proto_id != kProtoId_Ack) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"incorrect response");
return FALSE;
}
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_send_time(FuLogitechBulkcontrollerDevice *self, GError **error)
{
return fu_device_retry(FU_DEVICE(self),
fu_logitech_bulkcontroller_device_send_time_cb,
MAX_SETUP_RETRIES,
NULL,
error);
}
static gboolean
fu_logitech_bulkcontroller_device_transition_to_device_mode_cb(FuDevice *device,
gpointer user_data,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
FuLogitechBulkcontrollerProtoId proto_id = kProtoId_UnknownId;
g_autoptr(GByteArray) req = NULL;
g_autoptr(GByteArray) res = NULL;
g_autoptr(GByteArray) decoded_pkt = NULL;
req = fu_logitech_bulkcontroller_proto_manager_generate_transition_to_device_mode_request(
self);
res = fu_logitech_bulkcontroller_device_sync_write(self, req, error);
if (res == NULL)
return FALSE;
decoded_pkt = fu_logitech_bulkcontroller_proto_manager_decode_message(res->data,
res->len,
&proto_id,
error);
if (decoded_pkt == NULL) {
g_prefix_error_literal(error, "failed to unpack packet: ");
return FALSE;
}
g_debug("received transition mode response: id: %u, length %u", proto_id, res->len);
if (proto_id != kProtoId_TransitionToDeviceModeResponse) {
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INVALID_DATA,
"incorrect response");
return FALSE;
}
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_transition_to_device_mode(FuLogitechBulkcontrollerDevice *self,
GError **error)
{
return fu_device_retry(FU_DEVICE(self),
fu_logitech_bulkcontroller_device_transition_to_device_mode_cb,
MAX_SETUP_RETRIES,
NULL,
error);
}
static gboolean
fu_logitech_bulkcontroller_device_clear_queue_cb(FuDevice *device,
gpointer user_data,
GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
gsize actual_length = 0;
g_autofree guint8 *buf = g_malloc0(self->transfer_bufsz);
g_autoptr(GError) error_local = NULL;
if (!fu_usb_device_bulk_transfer(FU_USB_DEVICE(self),
self->sync_ep[EP_IN],
buf,
self->transfer_bufsz,
&actual_length,
250, /* ms */
NULL,
&error_local)) {
if (g_error_matches(error_local, FWUPD_ERROR, FWUPD_ERROR_TIMED_OUT)) {
g_debug("timed out successfully");
return TRUE;
}
g_propagate_error(error, g_steal_pointer(&error_local));
return FALSE;
}
/* failed */
g_set_error_literal(error,
FWUPD_ERROR,
FWUPD_ERROR_INTERNAL,
"got valid data, so keep going");
return FALSE;
}
static gboolean
fu_logitech_bulkcontroller_device_clear_queue(FuLogitechBulkcontrollerDevice *self, GError **error)
{
g_debug("clearing any bulk data");
return fu_device_retry(FU_DEVICE(self),
fu_logitech_bulkcontroller_device_clear_queue_cb,
3,
NULL,
error);
}
static gboolean
fu_logitech_bulkcontroller_device_check_buffer_size(FuLogitechBulkcontrollerDevice *self,
GError **error)
{
g_autoptr(GByteArray) buf = NULL;
g_autoptr(GError) error_local = NULL;
if (!fu_logitech_bulkcontroller_device_sync_send_cmd(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_CHECK_BUFFERSIZE,
NULL, /* data */
BULK_TRANSFER_TIMEOUT,
error)) {
g_prefix_error_literal(error, "failed to send request: ");
return FALSE;
}
buf = fu_logitech_bulkcontroller_device_sync_wait_cmd_retry(
self,
FU_LOGITECH_BULKCONTROLLER_CMD_CHECK_BUFFERSIZE,
0x0, /* always zero */
BULK_TRANSFER_TIMEOUT,
&error_local);
if (buf != NULL) {
self->transfer_bufsz = 16 * 1024;
} else {
g_debug("sticking to 8k buffersize: %s", error_local->message);
}
/* success */
return TRUE;
}
static gboolean
fu_logitech_bulkcontroller_device_setup(FuDevice *device, GError **error)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(device);
/* the hardware is unable to handle requests -- firmware issue */
if (fu_device_has_private_flag(device,
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_POST_INSTALL)) {
fu_device_sleep(device, POST_INSTALL_SLEEP_DURATION);
fu_device_remove_private_flag(device,
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_POST_INSTALL);
}
/* FuUsbDevice->setup */
if (!FU_DEVICE_CLASS(fu_logitech_bulkcontroller_device_parent_class)
->setup(device, error)) {
g_prefix_error_literal(error, "failed to FuUsbDevice->setup: ");
return FALSE;
}
/* empty the queue */
if (!fu_logitech_bulkcontroller_device_clear_queue(self, error)) {
g_prefix_error_literal(error, "failed to clear queue: ");
return FALSE;
}
/* check if the device supports a 16kb transfer buffer */
if (fu_device_has_private_flag(device,
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_CHECK_BUFFER_SIZE)) {
if (!fu_logitech_bulkcontroller_device_check_buffer_size(self, error)) {
g_prefix_error_literal(error, "failed to check buffer size: ");
return FALSE;
}
}
/* device supports modes of Device (supported), Appliance and BYOD (both unsupported) */
if (!fu_logitech_bulkcontroller_device_transition_to_device_mode(self, error)) {
g_prefix_error_literal(error, "failed to transition to device_mode: ");
return FALSE;
}
/* set device time */
if (!fu_logitech_bulkcontroller_device_send_time(self, error)) {
g_prefix_error_literal(error, "failed to set time: ");
return FALSE;
}
/* load current device data */
if (!fu_logitech_bulkcontroller_device_ensure_info(self, TRUE, error)) {
g_prefix_error_literal(error, "failed to ensure info: ");
return FALSE;
}
/* success */
return TRUE;
}
static void
fu_logitech_bulkcontroller_device_set_progress(FuDevice *device, FuProgress *progress)
{
fu_progress_set_id(progress, G_STRLOC);
fu_progress_add_step(progress, FWUPD_STATUS_DECOMPRESSING, 0, "prepare-fw");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, "detach");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_WRITE, 90, "write");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_RESTART, 0, "attach");
fu_progress_add_step(progress, FWUPD_STATUS_DEVICE_BUSY, 10, "reload");
}
static void
fu_logitech_bulkcontroller_device_init(FuLogitechBulkcontrollerDevice *self)
{
self->transfer_bufsz = 8 * 1024;
self->device_info_response_json = g_string_new(NULL);
fu_device_add_protocol(FU_DEVICE(self), "com.logitech.vc.proto");
fu_device_set_version_format(FU_DEVICE(self), FWUPD_VERSION_FORMAT_TRIPLET);
fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_UPDATABLE);
fu_device_add_flag(FU_DEVICE(self), FWUPD_DEVICE_FLAG_SIGNED_PAYLOAD);
fu_device_add_private_flag(FU_DEVICE(self), FU_DEVICE_PRIVATE_FLAG_RETRY_OPEN);
fu_usb_device_set_claim_retry_count(FU_USB_DEVICE(self), 100);
fu_device_retry_set_delay(FU_DEVICE(self), 1000);
fu_device_register_private_flag(FU_DEVICE(self),
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_CHECK_BUFFER_SIZE);
fu_device_register_private_flag(FU_DEVICE(self),
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_POST_INSTALL);
fu_device_register_private_flag(FU_DEVICE(self),
FU_LOGITECH_BULKCONTROLLER_DEVICE_FLAG_PHERIPHERAL_UPDATE);
/* these are unrecoverable */
fu_device_retry_add_recovery(FU_DEVICE(self), FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, NULL);
fu_device_retry_add_recovery(FU_DEVICE(self),
FWUPD_ERROR,
FWUPD_ERROR_PERMISSION_DENIED,
NULL);
}
static void
fu_logitech_bulkcontroller_device_finalize(GObject *object)
{
FuLogitechBulkcontrollerDevice *self = FU_LOGITECH_BULKCONTROLLER_DEVICE(object);
g_string_free(self->device_info_response_json, TRUE);
G_OBJECT_CLASS(fu_logitech_bulkcontroller_device_parent_class)->finalize(object);
}
static void
fu_logitech_bulkcontroller_device_class_init(FuLogitechBulkcontrollerDeviceClass *klass)
{
FuDeviceClass *device_class = FU_DEVICE_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = fu_logitech_bulkcontroller_device_finalize;
device_class->to_string = fu_logitech_bulkcontroller_device_to_string;
device_class->write_firmware = fu_logitech_bulkcontroller_device_write_firmware;
device_class->probe = fu_logitech_bulkcontroller_device_probe;
device_class->setup = fu_logitech_bulkcontroller_device_setup;
device_class->set_progress = fu_logitech_bulkcontroller_device_set_progress;
}
|