//***************************************************************************** //! Load the proper image based on the information from the boot info //! and launch it. //***************************************************************************** static void bootmgr_image_loader(sBootInfo_t *psBootInfo) { _i32 fhandle; _u8 *image; // search for the active image switch (psBootInfo->ActiveImg) { case IMG_ACT_UPDATE1: image = (unsigned char *)IMG_UPDATE1; break; case IMG_ACT_UPDATE2: image = (unsigned char *)IMG_UPDATE2; break; default: image = (unsigned char *)IMG_FACTORY; break; } // do we have a new image that needs to be verified? if ((psBootInfo->ActiveImg != IMG_ACT_FACTORY) && (psBootInfo->Status == IMG_STATUS_CHECK)) { if (!bootmgr_verify(image)) { // verification failed, delete the broken file sl_FsDel(image, 0); // switch to the previous image psBootInfo->ActiveImg = psBootInfo->PrevImg; psBootInfo->PrevImg = IMG_ACT_FACTORY; } // in any case, change the status to "READY" psBootInfo->Status = IMG_STATUS_READY; // write the new boot info if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle)) { sl_FsWrite(fhandle, 0, (unsigned char *)psBootInfo, sizeof(sBootInfo_t)); // close the file sl_FsClose(fhandle, 0, 0, 0); } } // this one might modify the boot info hence it MUST be called after // bootmgr_verify! (so that the changes are not saved to flash) wait_for_safe_boot(psBootInfo); // select the active image again, since it might have changed switch (psBootInfo->ActiveImg) { case IMG_ACT_UPDATE1: image = (unsigned char *)IMG_UPDATE1; break; case IMG_ACT_UPDATE2: image = (unsigned char *)IMG_UPDATE2; break; default: image = (unsigned char *)IMG_FACTORY; break; } bootmgr_load_and_execute(image); }
//***************************************************************************** //! Load the proper image based on information from boot info and executes it. //***************************************************************************** static void bootmgr_image_loader(sBootInfo_t *psBootInfo) { _i32 fhandle; if (safe_mode_boot()) { _u32 count = 0; while ((BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * count++) < BOOTMGR_SAFE_MODE_ENTER_MS) { // toogle the led MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, ~MAP_GPIOPinRead(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN)); UtilsDelay(UTILS_DELAY_US_TO_COUNT(BOOTMGR_SAFE_MODE_ENTER_TOOGLE_MS * 1000)); } psBootInfo->ActiveImg = IMG_ACT_FACTORY; // turn the led off MAP_GPIOPinWrite(MICROPY_SYS_LED_PORT, MICROPY_SYS_LED_PORT_PIN, 0); // request a safe boot to the application PRCMRequestSafeBoot(); } // do we have a new update image that needs to be verified? else if ((psBootInfo->ActiveImg == IMG_ACT_UPDATE) && (psBootInfo->Status == IMG_STATUS_CHECK)) { if (!bootmgr_verify()) { // delete the corrupted file sl_FsDel((_u8 *)IMG_UPDATE, 0); // switch to the factory image psBootInfo->ActiveImg = IMG_ACT_FACTORY; } // in any case, set the status as "READY" psBootInfo->Status = IMG_STATUS_READY; // write the new boot info if (!sl_FsOpen((unsigned char *)IMG_BOOT_INFO, FS_MODE_OPEN_WRITE, NULL, &fhandle)) { sl_FsWrite(fhandle, 0, (unsigned char *)psBootInfo, sizeof(sBootInfo_t)); // close the file sl_FsClose(fhandle, 0, 0, 0); } } // now boot the active image if (IMG_ACT_UPDATE == psBootInfo->ActiveImg) { bootmgr_load_and_execute((unsigned char *)IMG_UPDATE); } else { bootmgr_load_and_execute((unsigned char *)IMG_FACTORY); } }