+ printf("Firmware block %d with length %u read.\n", fw_blocks, len);
+ } while(r > 0);
+
+ if (fw_blocks == 0) {
+ fprintf(stderr, "Firmware file not valid!\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("Firmware with %d blocks successfully read.\n", fw_blocks);
+
+ hmcfgusb_set_debug(debug);
+
+ memset(&rdata, 0, sizeof(rdata));
+
+ dev = hmcfgusb_init(parse_hmcfgusb, &rdata);
+ if (!dev) {
+ fprintf(stderr, "Can't initialize HM-CFG-USB\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (!dev->bootloader) {
+ fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, entering bootloader.\n");
+ hmcfgusb_enter_bootloader(dev);
+ fprintf(stderr, "\nWaiting for device to reappear...\n");
+
+ do {
+ sleep(1);
+ } while ((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL);
+
+ if (!dev->bootloader) {
+ fprintf(stderr, "Can't enter bootloader, giving up!\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ printf("\nHM-CFG-USB opened.\n\n");
+
+
+ printf("Flasing %d blocks", fw_blocks);
+ if (debug) {
+ printf("\n");
+ } else {
+ printf(": %c", twiddlie[0]);
+ fflush(stdout);
+ }
+
+ for (block = 0; block < fw_blocks; block++) {
+ len = fw[block][2] << 8;
+ len |= fw[block][3];
+
+ len += 4; /* block nr., length */
+
+ if (debug)
+ hexdump(fw[block], len, "F> ");