Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
7:5b8648784381
Parent:
5:ac38f09fd179
Child:
8:914b7aff5554
--- a/main.cpp	Thu Nov 03 11:26:12 2016 +0000
+++ b/main.cpp	Thu Nov 10 15:45:16 2016 +0000
@@ -17,7 +17,9 @@
 
 // This is the default root key
 // if you have changed this key, please specify it here
-uint8_t root_key[D7A_ROOT_KEY_SIZE] = DEFAULT_ROOT_KEY;
+// For now root key size is always D7A_ROOT_KEY_SIZE
+uint8_t root_key[D7A_ROOT_KEY_SIZE] = CUP_DEFAULT_KEY;
+uint8_t root_key_size = D7A_ROOT_KEY_SIZE; // XXX: Do not change this
 
 // callbacks structure
 const d7a_callbacks_t callbacks = {
@@ -45,9 +47,25 @@
     button_user.release();
 }
 
+uint8_t check_parameter(const char* str, uint32_t param1, uint32_t param2)
+{
+    PRINT("Checking %s ", str);
+    if (param1 != param2)
+    {
+        PRINT("Failed. (0x%08X != 0x%08X)\r\n", param1, param2);
+        return 1;
+    }
+    else
+    {
+        PRINT("OK. (0x%08X)\r\n", param1);
+        return 0;
+    }
+}
+
 int main()
 {
     d7a_revision_t rev;
+    cup_cfg_t cup_cfg;
     
     // Start & initialize
     DBG_OPEN();
@@ -63,52 +81,56 @@
     // Check modem revision
     D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
     
-    PRINT("-----------------------------------\r\n");
-    PRINT("--------- D7A Modem infos ---------\r\n");
-    PRINT("Manufacturer ID:  0x%08X\r\n",       rev.manufacturer_id);
-    PRINT("Device ID:        0x%08X\r\n",       rev.device_id);
-    PRINT("Hardware version: 0x%08X\r\n",       rev.hw_version);
-    PRINT("Firmware version: v%d.%d.%d\r\n",    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
-    PRINT("Firmware hash:    0x%08x\r\n",       rev.fw_version.hash);
-    PRINT("File system CRC:  0x%08x\r\n",       rev.fs_crc);
-    PRINT("-----------------------------------\r\n");
-    
-    // Check package infos
-    CUP_Archive cup_pkg;
-    
-    PRINT("-------- CUP Archive infos --------\r\n");
-    PRINT("Hardware version: 0x%08X\r\n",       cup_pkg.hw_version);
-    PRINT("Firmware version: v%d.%d.%d\r\n",    cup_pkg.fw_major, cup_pkg.fw_minor, cup_pkg.fw_patch);
-    PRINT("Size:             %d\r\n",           cup_pkg.data_size);
-    PRINT("Nb archives:      %d\r\n",           cup_pkg.nb_archives);
-    PRINT("Signature:        0x%08X\r\n",       cup_pkg.signature);
-    PRINT("-----------------------------------\r\n");
-    
     // Read CUP config with root permissions
-    cup_cfg_t cup_cfg;
     D7A_READ(&cup_cfg, CUP_CFG_FID, 0, sizeof(cup_cfg_t), root_key);
     
-    PRINT("----- CUP Configuration infos -----\r\n");
-    PRINT("Src offset:       0x%08X\r\n",       cup_cfg.src_offset);
-    PRINT("Signature:        0x%08X\r\n",       cup_cfg.signature);
-    PRINT("File max size:    %d\r\n",           cup_cfg.key);
-    PRINT("Dbg config:       0x%08X\r\n",       cup_cfg.dbg_cfg);
-    PRINT("-----------------------------------\r\n");
+    uint8_t hw_errors = 0;
+    uint8_t fw_errors = 0;
+    uint8_t errors = 0;
     
     uint32_t version_old = (rev.fw_version.major << 24) | (rev.fw_version.minor << 16) | rev.fw_version.patch;
-    uint32_t version_new = (cup_pkg.fw_major << 24) | (cup_pkg.fw_minor << 16) | cup_pkg.fw_patch;
+    uint32_t version_new = (CUP_FW_MAJOR << 24) | (CUP_FW_MINOR << 16) | CUP_FW_PATCH;
+    
+    hw_errors += check_parameter("Manufacturer ID...       ", rev.manufacturer_id, CUP_MFG_ID);
+    hw_errors += check_parameter("Device ID...             ", rev.device_id, CUP_DEV_ID);
+    hw_errors += check_parameter("Hardware version...      ", rev.hw_version, CUP_HW_ID);
     
-    if (rev.hw_version != cup_pkg.hw_version)
+    if (version_old != version_new)
     {
-        PRINT("Please select the right Hardware in bin.h\r\n");
+        fw_errors += check_parameter("Firmware id...           ", rev.fw_version.id, CUP_FW_ID);
+        fw_errors += check_parameter("Firmware version major...", rev.fw_version.major, CUP_TARGET_FW_MAJOR);
+        fw_errors += check_parameter("Firmware version minor...", rev.fw_version.minor, CUP_TARGET_FW_MINOR);
+    
+        PRINT("Checking Available CUP space...    ");
+        if (CUP_DATA_SIZE > cup_cfg.key)
+        {
+            PRINT("Failed. (0x%08X < 0x%08X)\r\n", cup_cfg.key, CUP_DATA_SIZE);
+            fw_errors++;
+        }
+        else
+        {
+            PRINT("OK. (0x%08X)\r\n", cup_cfg.key);
+        }
     }
-    else if((cup_pkg.data_size % cup_pkg.word_size) != 0)
+    
+    PRINT("\r\n");
+            
+    if (hw_errors)
     {
-        PRINT("Size %d should be multiple of %d.\r\n", cup_pkg.data_size, cup_pkg.word_size);
+        PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
     }
-    else if (cup_pkg.data_size > cup_cfg.key)
+    
+    if (fw_errors)
     {
-        PRINT("CUP File too big for upgrade. Available %d needs %d.\r\n", cup_cfg.key, cup_pkg.data_size);
+        PRINT("Please, check that you chose the right Revision in mbed revision tree.\r\n");
+        PRINT("You can only upgrade to another patch 4.5.x --> 4.5.y\r\nor to the next minor 4.5.x --> 4.6.0\r\n");
+    }
+    
+    errors = hw_errors + fw_errors;
+    
+    if (errors)
+    {
+        PRINT("\r\nSelect the right parameters and recompile.\r\n");
     }
     else if (version_old == version_new)
     {
@@ -121,21 +143,21 @@
             PRINT("/!\\ Your modem is at a more recent version /!\\\r\n/!\\ Are you sure you want to downgrade?    /!\\\r\n");
             PRINT("PRESS USER BUTTON TO CONFIRM...\r\n");
             button_user.wait();
-            PRINT("Downgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
+            PRINT("\r\nDowngrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
                     rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
-                    cup_pkg.fw_major, cup_pkg.fw_minor, cup_pkg.fw_patch);
+                    CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
             PRINT("PRESS USER BUTTON TO START DOWNGRADE...\r\n");
         }
         else
         {
             PRINT("Upgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
                     rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
-                    cup_pkg.fw_major, cup_pkg.fw_minor, cup_pkg.fw_patch);
+                    CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
             PRINT("PRESS USER BUTTON TO START UPGRADE...\r\n");
         }
         
         button_user.wait();
-        cup_pkg.start_update(cup_cfg.src_offset, cup_cfg.key);
+        cup_start_update(cup_cfg.src_offset, cup_cfg.key);
     }
     
     // Set main task to lowest priority