]> cvs.zerfleddert.de Git - hmcfgusb/blobdiff - flash-ota.c
check hmcfgusb firmware before trying to switch speed
[hmcfgusb] / flash-ota.c
index 1d3a090080e2de0fd2fc8e15b639fe983be928a5..fd6cd6e030ef8a3b17d8fe5d2262acf4e48f1f60 100644 (file)
@@ -53,6 +53,7 @@ struct recv_data {
        enum message_type message_type;
        uint16_t status;
        int speed;
+       uint16_t hmcfgusb_version;
 };
 
 static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
@@ -82,6 +83,9 @@ static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
                case 'G':
                        rdata->speed = buf[1];
                        break;
+               case 'H':
+                       rdata->hmcfgusb_version = (buf[11] << 8) | buf[12];
+                       break;
                default:
                        break;
        }
@@ -121,7 +125,7 @@ int send_hm_message(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t *
        memcpy(&out[0x0f], msg, msg[0] + 1);
 
        memset(rdata, 0, sizeof(struct recv_data));
-       hmcfgusb_send(dev, out, sizeof(out), 2);
+       hmcfgusb_send(dev, out, sizeof(out), 1);
 
        while (1) {
                if (rdata->message_type == MESSAGE_TYPE_R) {
@@ -129,7 +133,7 @@ int send_hm_message(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t *
                            ((rdata->status & 0xff) == 0x02)) {
                                break;
                        } else {
-                               fprintf(stderr, "\n\nInvalid status: %04x\n\n", rdata->status);
+                               fprintf(stderr, "\nInvalid status: %04x\n", rdata->status);
                                return 0;
                        }
                }
@@ -147,6 +151,35 @@ int send_hm_message(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t *
        return 1;
 }
 
+static int switch_speed(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t speed)
+{
+       uint8_t out[0x40];
+       int pfd;
+
+       printf("Entering %uk-mode\n", speed);
+
+       memset(out, 0, sizeof(out));
+       out[0] = 'G';
+       out[1] = speed;
+
+       hmcfgusb_send(dev, out, sizeof(out), 1);
+
+       while (1) {
+               errno = 0;
+               pfd = hmcfgusb_poll(dev, 1);
+               if ((pfd < 0) && errno) {
+                       if (errno != ETIMEDOUT) {
+                               perror("\n\nhmcfgusb_poll");
+                               exit(EXIT_FAILURE);
+                       }
+               }
+               if (rdata->speed == speed)
+                       break;
+       }
+
+       return 1;
+}
+
 int main(int argc, char **argv)
 {
        const char twiddlie[] = { '-', '\\', '|', '/' };
@@ -162,6 +195,7 @@ int main(int argc, char **argv)
        int pfd;
        int debug = 0;
        int cnt;
+       int switchcnt = 0;
        int msgnum = 0;
        int switched = 0;
 
@@ -199,11 +233,8 @@ int main(int argc, char **argv)
 
        printf("\nHM-CFG-USB opened\n\n");
 
-       printf("Entering 10k-mode\n");
-
        memset(out, 0, sizeof(out));
-       out[0] = 'G';
-       out[1] = 10;
+       out[0] = 'K';
        hmcfgusb_send(dev, out, sizeof(out), 1);
 
        while (1) {
@@ -215,10 +246,22 @@ int main(int argc, char **argv)
                                exit(EXIT_FAILURE);
                        }
                }
-               if (rdata.speed == 10)
+               if (rdata.hmcfgusb_version)
                        break;
        }
 
+       if (rdata.hmcfgusb_version < 0x3c7) {
+               fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.hmcfgusb_version);
+               exit(EXIT_FAILURE);
+       }
+
+       printf("HM-CFG-USB firmware version: %u\n", rdata.hmcfgusb_version);
+
+       if (!switch_speed(dev, &rdata, 10)) {
+               fprintf(stderr, "Can't switch speed!\n");
+               exit(EXIT_FAILURE);
+       }
+
        printf("Waiting for device with serial %s\n", argv[2]);
 
        while (1) {
@@ -256,8 +299,9 @@ int main(int argc, char **argv)
        out[2] = (hmid >> 8) & 0xff;
        out[3] = hmid & 0xff;
 
-       hmcfgusb_send(dev, out, sizeof(out), 2);
+       hmcfgusb_send(dev, out, sizeof(out), 1);
 
+       switchcnt = 3;
        do {
                printf("Initiating remote switch to 100k\n");
 
@@ -276,25 +320,9 @@ int main(int argc, char **argv)
                        exit(EXIT_FAILURE);
                }
 
-               printf("Entering 100k-mode\n");
-
-               memset(out, 0, sizeof(out));
-               out[0] = 'G';
-               out[1] = 100;
-
-               hmcfgusb_send(dev, out, sizeof(out), 2);
-
-               while (1) {
-                       errno = 0;
-                       pfd = hmcfgusb_poll(dev, 1);
-                       if ((pfd < 0) && errno) {
-                               if (errno != ETIMEDOUT) {
-                                       perror("\n\nhmcfgusb_poll");
-                                       exit(EXIT_FAILURE);
-                               }
-                       }
-                       if (rdata.speed == 100)
-                               break;
+               if (!switch_speed(dev, &rdata, 100)) {
+                       fprintf(stderr, "Can't switch speed!\n");
+                       exit(EXIT_FAILURE);
                }
 
                printf("Has the device switched?\n");
@@ -321,30 +349,21 @@ int main(int argc, char **argv)
                } while (cnt--);
 
                if (!switched) {
-                       printf("Entering 10k-mode\n");
+                       printf("No!\n");
 
-                       memset(out, 0, sizeof(out));
-                       out[0] = 'G';
-                       out[1] = 10;
-                       hmcfgusb_send(dev, out, sizeof(out), 1);
-
-                       while (1) {
-                               errno = 0;
-                               pfd = hmcfgusb_poll(dev, 1);
-                               if ((pfd < 0) && errno) {
-                                       if (errno != ETIMEDOUT) {
-                                               perror("\n\nhmcfgusb_poll");
-                                               exit(EXIT_FAILURE);
-                                       }
-                               }
-                               if (rdata.speed == 10)
-                                       break;
+                       if (!switch_speed(dev, &rdata, 10)) {
+                               fprintf(stderr, "Can't switch speed!\n");
+                               exit(EXIT_FAILURE);
                        }
                }
-       } while (!switched);
+       } while ((!switched) && (switchcnt--));
 
+       if (!switched) {
+               fprintf(stderr, "Too many errors, giving up!\n");
+               exit(EXIT_FAILURE);
+       }
 
-       printf("Initiating firmware upload!\n");
+       printf("Yes!\n");
 
        printf("Flashing %d blocks", fw->fw_blocks);
        if (debug) {
@@ -388,7 +407,7 @@ int main(int argc, char **argv)
 
                        memset(out, 0, sizeof(out));
 
-                       out[MSGID] = msgid++;
+                       out[MSGID] = msgid;
                        if (ack)
                                out[CTL] = 0x20;
                        out[TYPE] = 0xCA;
@@ -419,28 +438,16 @@ int main(int argc, char **argv)
                                fflush(stdout);
                        }
                } while((pos - &(fw->fw[block][2])) < len);
+               msgid++;
        }
 
        firmware_free(fw);
 
-       printf("\nEntering 10k-mode\n");
-
-       memset(out, 0, sizeof(out));
-       out[0] = 'G';
-       out[1] = 10;
-       hmcfgusb_send(dev, out, sizeof(out), 1);
+       printf("\n");
 
-       while (1) {
-               errno = 0;
-               pfd = hmcfgusb_poll(dev, 1);
-               if ((pfd < 0) && errno) {
-                       if (errno != ETIMEDOUT) {
-                               perror("\n\nhmcfgusb_poll");
-                               exit(EXIT_FAILURE);
-                       }
-               }
-               if (rdata.speed == 10)
-                       break;
+       if (!switch_speed(dev, &rdata, 10)) {
+               fprintf(stderr, "Can't switch speed!\n");
+               exit(EXIT_FAILURE);
        }
 
        printf("Waiting for device to reboot\n");
Impressum, Datenschutz