Program to update the D7A modem's firmware.

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
15:24434827c575
Parent:
13:c3324b26d473
Child:
16:5a6168dbe7bb
--- a/main.cpp	Fri Feb 03 11:36:50 2017 +0000
+++ b/main.cpp	Fri Mar 10 16:01:27 2017 +0000
@@ -82,11 +82,43 @@
     }
 }
 
+void print_check_rev(void)
+{
+    PRINT("Please, check that you are at the right version is rhe revision tree\r\n");
+}
+
+void print_check_hardware(void)
+{
+    PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
+}
+
+int32_t check_slack(cup_param_t* cup, cup_cfg_t* cup_cfg)
+{
+    PRINT("key: %d data: %d code: %d src: %d\r\n", cup_cfg->key, cup->data_size, cup->code_size, cup_cfg->src_offset);
+    PRINT("Checking CUP Slack...              ");
+        
+    int32_t cup_slack = cup_cfg->key - cup->data_size - (cup->code_size > cup_cfg->src_offset)? cup->code_size - cup_cfg->src_offset : 0;
+    
+    if (cup_slack < 0)
+    {
+        PRINT("Failed. (%d bytes short)\r\n", -cup_slack);
+    }
+    else
+    {
+        PRINT("OK. (%d bytes)\r\n", cup_slack);
+    }
+    
+    return cup_slack;
+}
+
 int main()
 {
+    bool bootloader;
     d7a_revision_t rev;
     cup_cfg_t cup_cfg;
+    cup_param_t* cup = (cup_param_t*)&cup_modem;
     uint32_t cup_offset;
+    int32_t cup_slack;
     
     // Start & initialize
     DBG_OPEN(DEBUG_LED);
@@ -107,99 +139,130 @@
         // Check modem revision
         D7A_READ(&rev, D7A_FID_FIRMWARE_VERSION, 0, sizeof(d7a_revision_t), NULL);
         
-        uint8_t hw_errors = 0;
-        uint8_t fw_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_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);
-        fw_errors += check_parameter("Firmware id...           ", rev.fw_version.id, CUP_FW_ID);
-        
-        if (version_old != version_new)
+        if (check_parameter("Manufacturer ID...       ", rev.manufacturer_id, cup->mfg_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);
-        }    
-        else
-        {
-            PRINT("\r\nYour modem is up to date! (v%d.%d.%d)\r\n",
-                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
+            print_check_rev();
             break;
         }
         
-        // Read CUP config with root permissions
-        D7A_READ(&cup_cfg, CUP_CFG_FID, 0, sizeof(cup_cfg_t), root_key);
-        
-        PRINT("Checking CUP Slack...              ");
-        cup_offset = (CUP_CODE_SIZE > cup_cfg.src_offset)? CUP_CODE_SIZE - cup_cfg.src_offset : 0;
-        int32_t cup_slack = cup_cfg.key - cup_offset - CUP_DATA_SIZE;
-        if (cup_slack < 0)
+        if (check_parameter("Hardware version...      ", rev.hw_version, cup->hw_id))
         {
-            PRINT("Failed. (%d bytes short)\r\n", -cup_slack);
-            fw_errors++;
-        }
-        else
-        {
-            PRINT("OK. (%d bytes)\r\n", cup_slack);
-        }
-                
-        if (hw_errors)
-        {
-            PRINT("Please, check that you chose the right Hardware in bin.h\r\n");
+            print_check_hardware();
             break;
         }
         
-        if (fw_errors)
+        if (rev.device_id == BOOTLOADER_DEV_ID)
         {
-            PRINT("Please, check that you chose the right Revision in mbed revision tree.\r\n"
-                  "You are trying to upgrade from v%d.%d.%d to v%d.%d.%d.\r\n"
-                  "You can only upgrade to another patch %d.%d.x --> %d.%d.y\r\n"
-                  "or to the next minor %d.%d.x --> %d.%d.0\r\n",
-                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
-                    CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH,
-                    rev.fw_version.major, rev.fw_version.minor,
-                    rev.fw_version.major, rev.fw_version.minor,
-                    rev.fw_version.major, rev.fw_version.minor,
-                    rev.fw_version.major, rev.fw_version.minor+1);
+            // Update bootloader
+            
+            // Read CUP config with root permissions
+            D7A_READ(&cup_cfg, cup->cfg_fid, 0, sizeof(cup_cfg_t), root_key);
+            
+            cup_slack = check_slack(cup, &cup_cfg);
+            
+            if (cup_slack < 0)
+            {
+                PRINT("/!\\ Not enough space for bootloader /!\\\r\n");
+                break;
+            }
+            
+            cup_offset = cup_cfg.key + cup_cfg.src_offset - cup->data_size;
+            
+            cup_start_update(cup_offset);
         }
-                
-        if (hw_errors || fw_errors)
-        {
-            PRINT("\r\nSelect the right parameters and recompile.\r\n");
-            break;
-        }
-    
-        if (version_old > version_new)
+        else
         {
-            PRINT("/!\\ Your modem is at a more recent version (v%d.%d.%d)/!\\\r\n"
-                  "/!\\ Are you sure you want to downgrade to v%d.%d.%d ? /!\\\r\n",
-                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
-                    CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
-            PRINT("PRESS USER BUTTON TO CONFIRM...\r\n");
-            button_user.wait();
-            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_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
-#ifdef DEBUG_BUTTON
-            PRINT("PRESS USER BUTTON TO START DOWNGRADE...\r\n");
-            button_user.wait();
-#endif
+            uint32_t version_old = (rev.fw_version.major << 24) | (rev.fw_version.minor << 16) | rev.fw_version.patch;
+            uint32_t version_new = (cup->fw_major << 24) | (cup->fw_minor << 16) | cup->fw_patch;
+
+            // Update modem
+            if (check_parameter("Device ID...             ", rev.device_id, cup->dev_id))
+            {
+                print_check_rev();
+                break;
+            }
+            
+            if (check_parameter("Firmware id...           ", rev.fw_version.id, cup->fw_id))
+            {
+                print_check_rev();
+                break;
+            }
+            
+            if (version_old != version_new)
+            {
+                if (check_parameter("Firmware version major...", rev.fw_version.major, cup->target_fw_major))
+                {
+                    print_check_rev();
+                    break;
+                }
+                
+                if (check_parameter("Firmware version minor...", rev.fw_version.minor, cup->target_fw_minor))
+                {
+                    print_check_rev();
+                    break;
+                }
+            }
+            else
+            {
+                PRINT("\r\nYour modem is up to date! (v%d.%d.%d)\r\n",
+                        rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
+                break;
+            }
+            
+            // Read CUP config with root permissions
+            D7A_READ(&cup_cfg, cup->cfg_fid, 0, sizeof(cup_cfg_t), root_key);
+            
+            cup_slack = check_slack(cup, &cup_cfg);
+            
+            if (cup_slack < 0)
+            {
+                PRINT("/!\\ Not enough space for binary, checking for bootloader /!\\\r\n");
+                
+                cup_slack = check_slack((cup_param_t*)&cup_bootloader, &cup_cfg);
+                
+                if (cup_slack < 0)
+                {
+                    PRINT("/!\\ Not enough space for bootloader /!\\\r\n");
+                    break;
+                }
+                
+                bootloader = true;
+            }
+            else
+            {
+                bootloader = false;
+            }
+            
+            if (version_old > version_new)
+            {
+                PRINT("/!\\ Your modem is at a more recent version (v%d.%d.%d)/!\\\r\n"
+                      "/!\\ Are you sure you want to downgrade to v%d.%d.%d ? /!\\\r\n",
+                        rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
+                        cup->fw_major, cup->fw_minor, cup->fw_patch);
+                PRINT("PRESS USER BUTTON TO CONFIRM...\r\n");
+                button_user.wait();
+                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->fw_major, cup->fw_minor, cup->fw_patch);
+    #ifdef DEBUG_BUTTON
+                PRINT("PRESS USER BUTTON TO START DOWNGRADE...\r\n");
+                button_user.wait();
+    #endif
+            }
+            else if (version_old != version_new)
+            {
+                PRINT("\r\nUpgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
+                        rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
+                        cup->fw_major, cup->fw_minor, cup->fw_patch);
+    #ifdef DEBUG_BUTTON
+                PRINT("PRESS USER BUTTON TO START UPGRADE...\r\n");
+                button_user.wait();
+    #endif
+            }
+        
+            cup_offset = cup_cfg.key - cup->data_size;
+            cup_start_update(cup_offset, bootloader);
         }
-        else if (version_old != version_new)
-        {
-            PRINT("\r\nUpgrading firmware: v%d.%d.%d --> v%d.%d.%d\r\n", 
-                    rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch,
-                    CUP_FW_MAJOR, CUP_FW_MINOR, CUP_FW_PATCH);
-#ifdef DEBUG_BUTTON
-            PRINT("PRESS USER BUTTON TO START UPGRADE...\r\n");
-            button_user.wait();
-#endif
-        }
-    
-        cup_start_update(cup_offset);
     
     } while (0);