+ printf("culfw-device firmware version: %u.%02u\n",
+ (rdata.version >> 8) & 0xff,
+ rdata.version & 0xff);
+
+ if (rdata.version < 0x0139) {
+ fprintf(stderr, "\nThis version does _not_ support firmware upgrade mode!\n");
+ exit(EXIT_FAILURE);
+ } else if (rdata.version < 0x0140) {
+ printf("\n*** This version probably not supports firmware upgrade mode! ***\n\n");
+ }
+ } else {
+ hmcfgusb_set_debug(debug);
+
+ dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata);
+ if (!dev.hmcfgusb) {
+ fprintf(stderr, "Can't initialize HM-CFG-USB\n");
+ exit(EXIT_FAILURE);
+ }
+ dev.type = DEVICE_TYPE_HMCFGUSB;
+
+ printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n");
+
+ if (!dev.hmcfgusb->bootloader) {
+ printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n");
+ hmcfgusb_enter_bootloader(dev.hmcfgusb);
+ printf("Waiting for device to reappear...\n");
+
+ do {
+ if (dev.hmcfgusb) {
+ hmcfgusb_close(dev.hmcfgusb);
+ }
+ sleep(1);
+ } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev.hmcfgusb->bootloader));
+ }
+
+ if (dev.hmcfgusb->bootloader) {
+ printf("HM-CFG-USB in bootloader mode, rebooting\n");
+ hmcfgusb_leave_bootloader(dev.hmcfgusb);
+
+ do {
+ if (dev.hmcfgusb) {
+ hmcfgusb_close(dev.hmcfgusb);
+ }
+ sleep(1);
+ } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (dev.hmcfgusb->bootloader));
+ }
+
+ printf("\n\nHM-CFG-USB opened\n\n");
+
+ memset(out, 0, sizeof(out));
+ out[0] = 'K';
+ hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1);
+
+ while (1) {
+ errno = 0;
+ pfd = hmcfgusb_poll(dev.hmcfgusb, 1);
+ if ((pfd < 0) && errno) {
+ if (errno != ETIMEDOUT) {
+ perror("\n\nhmcfgusb_poll");
+ exit(EXIT_FAILURE);
+ }