Sfoglia il codice sorgente

improved error message for failed web fw update

Roel Schiphorst 3 anni fa
parent
commit
04f07d197b

+ 12 - 17
RemoteIDModule/check_firmware.cpp

@@ -40,7 +40,7 @@ bool CheckFirmware::check_OTA_partition(const esp_partition_t *part, const uint8
         spi_flash_munmap(handle);
         return false;
     }
-    Serial.printf("app descriptor at 0x%x size=%u id=%u\n", unsigned(ad)-unsigned(ptr), ad->image_size, ad->board_id);
+    Serial.printf("app descriptor at 0x%x size=%u id=%u (own id %u)\n", unsigned(ad)-unsigned(ptr), ad->image_size, ad->board_id,BOARD_ID);
     const uint32_t img_len = uint32_t(uintptr_t(ad) - uintptr_t(ptr));
     if (ad->image_size != img_len) {
         Serial.printf("app_descriptor bad size %u\n", ad->image_size);
@@ -72,28 +72,23 @@ bool CheckFirmware::check_OTA_partition(const esp_partition_t *part, const uint8
     return false;
 }
 
-bool CheckFirmware::check_OTA_next(const uint8_t *lead_bytes, uint32_t lead_length)
+bool CheckFirmware::check_OTA_next(const esp_partition_t *part, const uint8_t *lead_bytes, uint32_t lead_length)
 {
-    const auto *running_part = esp_ota_get_running_partition();
-    if (running_part == nullptr) {
-        Serial.printf("No running OTA partition\n");
-        return false;
-    }
-    const auto *part = esp_ota_get_next_update_partition(running_part);
-    if (part == nullptr) {
-        Serial.printf("No next OTA partition\n");
-        return false;
-    }
-    uint32_t board_id=0;
+    Serial.printf("Running partition %s\n", esp_ota_get_running_partition()->label);
+
+    uint32_t board_id = 0;
     bool sig_ok = check_OTA_partition(part, lead_bytes, lead_length, board_id);
+
+    if (g.lock_level == -1) {
+        // only if lock_level is -1 then accept any firmware
+        return true;
+    }
+
     // if app descriptor has a board ID and the ID is wrong then reject
     if (board_id != 0 && board_id != BOARD_ID) {
         return false;
     }
-    if (g.lock_level == 0) {
-        // if unlocked then accept any firmware
-        return true;
-    }
+
     return sig_ok;
 }
 

+ 1 - 1
RemoteIDModule/check_firmware.h

@@ -17,7 +17,7 @@ public:
     } app_descriptor_t;
 
     // check the firmware on the partition which will be updated by OTA
-    static bool check_OTA_next(const uint8_t *lead_bytes, uint32_t lead_length);
+    static bool check_OTA_next(const esp_partition_t *part, const uint8_t *lead_bytes, uint32_t lead_length);
     static bool check_OTA_running(void);
 
 private:

+ 17 - 10
RemoteIDModule/web/index.html

@@ -121,16 +121,18 @@
                 var xhr = new window.XMLHttpRequest();
                 xhr.upload.addEventListener('progress', function(evt) {
                     if (evt.lengthComputable) {
-                        var per = evt.loaded / evt.total;
-                        $('#progress').html('progress: ' + Math.round(per*100) + '%');
-                        if (evt.loaded == evt.total) {
-                            setTimeout(function(){ //popup after 1 second
-                                $('#progress').html('progress: done');
-                                alert("Firmware upgrade completed!\n\nPress OK to reboot the RemoteID device.");
-                                window.location.reload(1);
-                            }, 1000);
-                        }
-                    }
+                       var per = evt.loaded / evt.total;
+                       $('#progress').html('progress: ' + Math.round(per*100) + '%');
+                       if (evt.loaded == evt.total) {
+                           setTimeout(function(){ //popup after 2 second
+							   if (xhr.status === 200) {
+                                   $('#progress').html('progress: done');
+                                   alert("Firmware upgrade completed!\n\nPress OK to reboot the RemoteID device.");
+                                   window.location.reload(true);
+							   }
+                           }, 2000);
+                       }
+                   }
                 }, false);
                 return xhr;
             },
@@ -138,6 +140,11 @@
                 console.log('success!');
             },
             error: function (a, b, c) {
+				 setTimeout(function(){ //popup after 1 second
+                     $('#progress').html('progress: error');
+                     alert("Firmware upgrade FAILED!\n\nPlease check that:\na) you downloaded the OTA firmware (ending on _OTA.bin)\nb) you downloaded the correct file for your board\nc) if the LOCK_LEVEL has been set to 2 in your board, make sure you uploaded firmware that is signed with the correct key.");
+                     window.location.reload(true);
+                 }, 100);
             }
         });
     });

+ 25 - 5
RemoteIDModule/webinterface.cpp

@@ -98,14 +98,25 @@ void WebInterface::init(void)
 
     /*handling uploading firmware file */
     server.on("/update", HTTP_POST, []() {
-        server.sendHeader("Connection", "close");
-        server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
-        ESP.restart();
+        if (Update.hasError()) {
+			server.sendHeader("Connection", "close");
+		    server.send(500, "text/plain","FAIL");
+		    Serial.printf("Update Failed: Update function has errors\n");
+		    delay(5000);
+		} else {
+			server.sendHeader("Connection", "close");
+			server.send(200, "text/plain","OK");
+			Serial.printf("Update Success: \nRebooting...\n");
+			delay(1000);
+			ESP.restart();
+		}
     }, [this]() {
         HTTPUpload& upload = server.upload();
+        static const esp_partition_t* partition_new_firmware = esp_ota_get_next_update_partition(NULL); //get OTA partion to which we will write new firmware file;
         if (upload.status == UPLOAD_FILE_START) {
             Serial.printf("Update: %s\n", upload.filename.c_str());
             lead_len = 0;
+
             if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
                 Update.printError(Serial);
             }
@@ -129,12 +140,21 @@ void WebInterface::init(void)
                 uint8_t ff = 0xff;
                 Update.write(&ff, 1);
             }
-            if (!CheckFirmware::check_OTA_next(lead_bytes, lead_len)) {
-                Serial.printf("failed firmware check\n");
+            if (!CheckFirmware::check_OTA_next(partition_new_firmware, lead_bytes, lead_len)) {
+                Serial.printf("Update Failed: firmware checks have errors\n");
+                server.sendHeader("Connection", "close");
+                server.send(500, "text/plain","FAIL");
+                delay(5000);
             } else if (Update.end(true)) {
                 Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
+                server.sendHeader("Connection", "close");
+                server.send(200, "text/plain","OK");
             } else {
                 Update.printError(Serial);
+                Serial.printf("Update Failed: Update.end function has errors\n");
+                server.sendHeader("Connection", "close");
+                server.send(500, "text/plain","FAIL");
+                delay(5000);
             }
         }
     });