diff --git a/app/dfu/usbd_dfu_flash.cpp b/app/dfu/usbd_dfu_flash.cpp index 09c6e79..6a78d69 100644 --- a/app/dfu/usbd_dfu_flash.cpp +++ b/app/dfu/usbd_dfu_flash.cpp @@ -24,7 +24,7 @@ //#include "option_bits.h" #include "mbed.h" #include "target.h" -#include "QSPIFBlockDevice.h" +#include "BlockDevice.h" #include "FlashSimBlockDevice.h" #include "flash_map_backend/secondary_bd.h" #include "bootutil/bootutil.h" @@ -42,7 +42,6 @@ /* Private macro ------------------------------------------------------------- */ /* Private variables --------------------------------------------------------- */ /* Private function prototypes ----------------------------------------------- */ - char BOOTLOADER_DESC_STR[48]; @@ -54,8 +53,8 @@ uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len); uint16_t Flash_If_DeInit(void); uint16_t Flash_If_GetStatus(uint32_t Add, uint8_t Cmd, uint8_t * buffer); -FlashIAP flash; -QSPIFBlockDevice qspi_flash(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); +extern FlashIAP flash; +mbed::BlockDevice* qspi_flash = mbed::BlockDevice::get_default_instance(); mbed::BlockDevice* dfu_secondary_bd = get_secondary_bd(); const uint32_t QSPIFLASH_BASE_ADDRESS = 0x90000000; @@ -80,8 +79,10 @@ bool Flash_If_Init_requested = false; void init_Memories() { flash.init(); - qspi_flash.init(); - dfu_secondary_bd->init(); + qspi_flash->init(); + if (dfu_secondary_bd != nullptr) { + dfu_secondary_bd->init(); + } snprintf(BOOTLOADER_DESC_STR, sizeof(BOOTLOADER_DESC_STR), "@MCUBoot version %d /0x00000000/0*4Kg", BOOTLOADER_VERSION); } @@ -108,8 +109,10 @@ uint16_t Flash_If_Init(void) uint16_t Flash_If_DeInit(void) { flash.deinit(); - dfu_secondary_bd->deinit(); - boot_set_pending(false); + if (dfu_secondary_bd != nullptr) { + dfu_secondary_bd->deinit(); + boot_set_pending(false); + } return 0; } @@ -128,12 +131,14 @@ static bool isFileBlockFlash(uint32_t Add) { */ uint16_t Flash_If_Erase(uint32_t Add) { - if (isFileBlockFlash(Add)) { + if (isFileBlockFlash(Add) && dfu_secondary_bd == nullptr) { + return -1; + } else if (isFileBlockFlash(Add) && dfu_secondary_bd != nullptr) { Add -= FILEBLOCK_BASE_ADDRESS; return dfu_secondary_bd->erase(Add, dfu_secondary_bd->get_erase_size(Add)); } else if (isExternalFlash(Add)) { Add -= QSPIFLASH_BASE_ADDRESS; - return qspi_flash.erase(Add, qspi_flash.get_erase_size(Add)); + return qspi_flash->erase(Add, qspi_flash->get_erase_size(Add)); } else { return flash.erase(Add, flash.get_sector_size(Add)); } @@ -160,7 +165,9 @@ void delayed_write(struct writeInfo* info) { */ uint16_t Flash_If_Write(uint8_t * src, uint8_t * dest, uint32_t Len) { - if (isFileBlockFlash((uint32_t)dest)) { + if (isFileBlockFlash((uint32_t)dest) && dfu_secondary_bd == nullptr) { + return -1; + } else if (isFileBlockFlash((uint32_t)dest) && dfu_secondary_bd != nullptr) { dest -= FILEBLOCK_BASE_ADDRESS; if (Len < dfu_secondary_bd->get_erase_size(0)) { uint8_t* srcCopy = (uint8_t*)malloc(dfu_secondary_bd->get_erase_size(0)); @@ -171,10 +178,10 @@ uint16_t Flash_If_Write(uint8_t * src, uint8_t * dest, uint32_t Len) return dfu_secondary_bd->program(src, (uint32_t)dest, Len); } else if (isExternalFlash((uint32_t)dest)) { dest -= QSPIFLASH_BASE_ADDRESS; - if (Len < qspi_flash.get_erase_size(0)) { - Len = qspi_flash.get_erase_size(0); + if (Len < qspi_flash->get_erase_size(0)) { + Len = qspi_flash->get_erase_size(0); } - return qspi_flash.program(src, (uint32_t)dest, Len); + return qspi_flash->program(src, (uint32_t)dest, Len); } else { uint8_t* srcCopy = (uint8_t*)malloc(Len); memcpy(srcCopy, src, Len); @@ -199,12 +206,14 @@ uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len) uint32_t i = 0; uint8_t *psrc = src; - if (isFileBlockFlash((uint32_t)src)) { + if (isFileBlockFlash((uint32_t)src) && dfu_secondary_bd == nullptr) { + Len = 0; + } else if (isFileBlockFlash((uint32_t)src) && dfu_secondary_bd != nullptr) { src -= FILEBLOCK_BASE_ADDRESS; dfu_secondary_bd->read(dest, (uint32_t)src, Len); } else if (isExternalFlash((uint32_t)src)) { src -= QSPIFLASH_BASE_ADDRESS; - qspi_flash.read(dest, (uint32_t)src, Len); + qspi_flash->read(dest, (uint32_t)src, Len); } else { for (i = 0; i < Len; i++) { diff --git a/app/ota/ota.cpp b/app/ota/ota.cpp index 126d8ea..556f419 100644 --- a/app/ota/ota.cpp +++ b/app/ota/ota.cpp @@ -16,371 +16,165 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#if MCUBOOT_APPLICATION_HOOKS - -#include "ota.h" -#include "rtc.h" -#include "bootutil/bootutil_log.h" - -#include "SlicingBlockDevice.h" +#include "mbed.h" +#include "BlockDevice.h" #include "FlashIAPBlockDevice.h" -#include "QSPIFBlockDevice.h" #include "MBRBlockDevice.h" -#include "FileBlockDevice.h" -#include "FATFileSystem.h" - -#if MCUBOOT_APPLICATION_SDCARD #include "SDMMCBlockDevice.h" -#endif -#if MCUBOOT_APPLICATION_LITTLEFS +#include "FATFileSystem.h" #include "LittleFileSystem.h" -#endif - -static bool BlockTableLoaded = false; -static BlockTableData block_info[2]; -static void loadOTAData(void) { - RTCInit(); - /* - * Magic 0x07AA is set by Arduino_Portenta_OTA - * Magic 0xDF59 is set by the loader if RESET_REASON_PIN_RESET - */ - - int magic = RTCGetBKPRegister(RTC_BKP_DR0); - if (magic == 0x07AA) { - // DR1 contains the backing storage type - // DR2 the offset in case of raw device / MBR - // DR3 the update size - block_info[SECONDARY_BLOCK_DEVICE].storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1); - block_info[SECONDARY_BLOCK_DEVICE].raw_type = RTCGetBKPRegister(RTC_BKP_DR1) & 0x00000007; - block_info[SECONDARY_BLOCK_DEVICE].raw_flag = RTCGetBKPRegister(RTC_BKP_DR1) & RAW_FLAG; - block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = RTCGetBKPRegister(RTC_BKP_DR1) & MBR_FLAG; - block_info[SECONDARY_BLOCK_DEVICE].data_offset = RTCGetBKPRegister(RTC_BKP_DR2); - block_info[SECONDARY_BLOCK_DEVICE].update_size = RTCGetBKPRegister(RTC_BKP_DR3); - - // DR4 contains the backing storage type - // DR5 the offset in case of raw device / MBR - // DR6 the scratch size - block_info[SCRATCH_BLOCK_DEVICE].storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR4); - block_info[SCRATCH_BLOCK_DEVICE].raw_type = RTCGetBKPRegister(RTC_BKP_DR4) & 0x00000007; - block_info[SCRATCH_BLOCK_DEVICE].raw_flag = RTCGetBKPRegister(RTC_BKP_DR4) & RAW_FLAG; - block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = RTCGetBKPRegister(RTC_BKP_DR4) & MBR_FLAG; - block_info[SCRATCH_BLOCK_DEVICE].data_offset = RTCGetBKPRegister(RTC_BKP_DR5); - block_info[SCRATCH_BLOCK_DEVICE].update_size = RTCGetBKPRegister(RTC_BKP_DR6); - BOOT_LOG_INF("Custom OTA data"); - - /* Print loaded Data */ - BOOT_LOG_INF("Secondary [%d] [%d]", block_info[SECONDARY_BLOCK_DEVICE].storage_type, block_info[SECONDARY_BLOCK_DEVICE].raw_type); - BOOT_LOG_INF("Scratch [%d] [%d]", block_info[SCRATCH_BLOCK_DEVICE].storage_type, block_info[SCRATCH_BLOCK_DEVICE].raw_type); - } else { -#if ALL_IN_SD - block_info[SECONDARY_BLOCK_DEVICE].storage_type = SD_FATFS; - block_info[SECONDARY_BLOCK_DEVICE].data_offset = 2; - block_info[SECONDARY_BLOCK_DEVICE].update_size = MCUBOOT_SLOT_SIZE; - block_info[SECONDARY_BLOCK_DEVICE].raw_type = SDCARD_FLAG; - block_info[SECONDARY_BLOCK_DEVICE].raw_flag = 0; - block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = 0; - - block_info[SCRATCH_BLOCK_DEVICE].storage_type = SD_FATFS; - block_info[SCRATCH_BLOCK_DEVICE].data_offset = 2; - block_info[SCRATCH_BLOCK_DEVICE].update_size = MCUBOOT_SCRATCH_SIZE; - block_info[SCRATCH_BLOCK_DEVICE].raw_type = SDCARD_FLAG; - block_info[SCRATCH_BLOCK_DEVICE].raw_flag = 0; - block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = 0; -#elif MIX_SD_QSPI - block_info[SECONDARY_BLOCK_DEVICE].storage_type = SD_FATFS; - block_info[SECONDARY_BLOCK_DEVICE].data_offset = 2; - block_info[SECONDARY_BLOCK_DEVICE].update_size = MCUBOOT_SLOT_SIZE; - block_info[SECONDARY_BLOCK_DEVICE].raw_type = SDCARD_FLAG; - block_info[SECONDARY_BLOCK_DEVICE].raw_flag = 0; - block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = 0; - - block_info[SCRATCH_BLOCK_DEVICE].storage_type = QSPI_FLASH_FATFS_MBR; - block_info[SCRATCH_BLOCK_DEVICE].data_offset = 2; - block_info[SCRATCH_BLOCK_DEVICE].update_size = MCUBOOT_SCRATCH_SIZE; - block_info[SCRATCH_BLOCK_DEVICE].raw_type = QSPI_FLASH_FLAG; - block_info[SCRATCH_BLOCK_DEVICE].raw_flag = 0; - block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = 1; -#else - block_info[SECONDARY_BLOCK_DEVICE].storage_type = QSPI_FLASH_FATFS_MBR; - block_info[SECONDARY_BLOCK_DEVICE].data_offset = 2; - block_info[SECONDARY_BLOCK_DEVICE].update_size = MCUBOOT_SLOT_SIZE; - block_info[SECONDARY_BLOCK_DEVICE].raw_type = QSPI_FLASH_FLAG; - block_info[SECONDARY_BLOCK_DEVICE].raw_flag = 0; - block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = 1; +#include "ota.h" +#include "bootutil/bootutil_log.h" - block_info[SCRATCH_BLOCK_DEVICE].storage_type = QSPI_FLASH_FATFS_MBR; - block_info[SCRATCH_BLOCK_DEVICE].data_offset = 2; - block_info[SCRATCH_BLOCK_DEVICE].update_size = MCUBOOT_SCRATCH_SIZE; - block_info[SCRATCH_BLOCK_DEVICE].raw_type = QSPI_FLASH_FLAG; - block_info[SCRATCH_BLOCK_DEVICE].raw_flag = 0; - block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = 1; -#endif - BOOT_LOG_INF("Default OTA data"); - } - return; +BlockDevice* bd = NULL; +mbed::FileSystem* fs = NULL; + +extern FlashIAP flash; + +const uint32_t M7_FLASH_BASE = 0x8040000; +const uint32_t M4_FLASH_BASE = 0x8100000; + +uint32_t getOTABinaryBase(uint32_t firstWord) { + + BOOT_LOG_DBG("First OTA binary word: %lx", firstWord); + if ((firstWord & 0xFF000000) == 0x20000000 + || (firstWord & 0xFF000000) == 0x24000000 + || (firstWord & 0xFF000000) == 0x30000000 + || (firstWord & 0xFF000000) == 0x38000000) { + BOOT_LOG_DBG("Flashing on M7"); + return M7_FLASH_BASE; + } + if ((firstWord & 0xFF000000) == 0x10000000) { + BOOT_LOG_DBG("Flashing on M4"); + return M4_FLASH_BASE; + } + return 0xFFFFFFFF; } +static Timeout restart_timer; -static void initBlockTable(void) { - int err; - - loadOTAData(); - - if(block_info[SECONDARY_BLOCK_DEVICE].raw_type == block_info[SCRATCH_BLOCK_DEVICE].raw_type) { - /* Declare raw block devices */ - if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { - //block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new FlashIAPBlockDevice flashIAP_bd(data_offset, update_size); - //block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; - BOOT_LOG_ERR("U on IAP"); - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { -#if MCUBOOT_APPLICATION_SDCARD - block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new SDMMCBlockDevice(); - block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; -#else - BOOT_LOG_ERR("SDMMC"); -#endif - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { - block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new QSPIFBlockDevice(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); - block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; - } else { - BOOT_LOG_ERR("Config"); - } - - /* Setup sliced block devices */ - if(block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { - if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { - //block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new FlashIAPBlockDevice flashIAP_bd(data_offset, update_size); - //block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; - BOOT_LOG_ERR("U on IAP"); - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { -#if MCUBOOT_APPLICATION_SDCARD - block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); - block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; -#else - BOOT_LOG_ERR("SDMMC"); -#endif - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { - block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); - block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; - } else { - BOOT_LOG_ERR("Config"); - } - } else - - /* Setup MBR device */ - if(block_info[SECONDARY_BLOCK_DEVICE].mbr_flag) { - /* If using the same underlying block device configuration must be the same */ - if(block_info[SECONDARY_BLOCK_DEVICE].storage_type != block_info[SCRATCH_BLOCK_DEVICE].storage_type) { - BOOT_LOG_ERR("BD U!S"); - } - - /* If using the same underlying block device mbr partition must be the same */ - if(block_info[SECONDARY_BLOCK_DEVICE].data_offset != block_info[SCRATCH_BLOCK_DEVICE].data_offset) { - BOOT_LOG_ERR("MBR U!S"); - } - - block_info[SECONDARY_BLOCK_DEVICE].log_bd = new MBRBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset); - block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; - - /* Initialize block device */ - err = block_info[SECONDARY_BLOCK_DEVICE].log_bd->init(); - if (err) { - BOOT_LOG_ERR("Init"); - } - } else { - block_info[SECONDARY_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; - block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; - } - - /* Setup FS */ - if(!block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { - if((block_info[SECONDARY_BLOCK_DEVICE].storage_type & LITTLEFS_FLAG)) { -#if MCUBOOT_APPLICATION_LITTLEFS - block_info[SECONDARY_BLOCK_DEVICE].log_fs = new LittleFileSystem("fs"); -#else - BOOT_LOG_ERR("LFS"); -#endif - } else { - block_info[SECONDARY_BLOCK_DEVICE].log_fs = new FATFileSystem("fs"); - } - - block_info[SCRATCH_BLOCK_DEVICE].log_fs = block_info[SECONDARY_BLOCK_DEVICE].log_fs; - - err = block_info[SECONDARY_BLOCK_DEVICE].log_fs->mount(block_info[SECONDARY_BLOCK_DEVICE].log_bd); - if (err) { - BOOT_LOG_ERR("Mount"); - } - - /* Setup FileBlockDevice */ - block_info[SECONDARY_BLOCK_DEVICE].file_bd = new FileBlockDevice("/fs/update.bin", "rb+", block_info[SECONDARY_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); - block_info[SCRATCH_BLOCK_DEVICE].file_bd = new FileBlockDevice("/fs/scratch.bin", "rb+", block_info[SCRATCH_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); - } +size_t getFilesize(const char* filename) { + struct stat st; + if(stat(filename, &st) != 0) { + return 0; + } + return st.st_size; +} +int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_size) { + int err; + flash.init(); + BOOT_LOG_DBG("Configuration: "); + if (storage_type & INTERNAL_FLASH_FLAG) { + BOOT_LOG_DBG(" INTERNAL_FLASH "); + if (storage_type & (FATFS_FLAG | LITTLEFS_FLAG)) { + // have a filesystem, use offset as partition start + bd = new FlashIAPBlockDevice(0x8000000 + data_offset, 2 * 1024 * 1024 - data_offset); } else { - /* Declare raw block devices */ - if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { - //block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new FlashIAPBlockDevice flashIAP_bd(data_offset, update_size); - BOOT_LOG_ERR("U on IAP"); - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { -#if MCUBOOT_APPLICATION_SDCARD - block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new SDMMCBlockDevice(); -#else - BOOT_LOG_ERR("SDMMC"); -#endif - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { - block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new QSPIFBlockDevice(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); - } else { - BOOT_LOG_ERR("U config"); - } - - if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { - BOOT_LOG_ERR("S on IAP"); - } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { -#if MCUBOOT_APPLICATION_SDCARD - block_info[SCRATCH_BLOCK_DEVICE].raw_bd = new SDMMCBlockDevice(); -#else - BOOT_LOG_ERR("SDMMC"); -#endif - } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { - block_info[SCRATCH_BLOCK_DEVICE].raw_bd = new QSPIFBlockDevice(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); - } else { - BOOT_LOG_ERR("S config"); - } - - /* Setup Raw sliced devices */ - if(block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { - if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { - BOOT_LOG_ERR("U on IAP"); - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { -#if MCUBOOT_APPLICATION_SDCARD - block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); -#else - BOOT_LOG_ERR("SDMMC"); -#endif - } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { - block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); - } else { - BOOT_LOG_ERR("U config"); - } - } else - - /* Setup MBR devices */ - if(block_info[SECONDARY_BLOCK_DEVICE].mbr_flag) { - /* Setup MBR devices and FS if scratch and secondary are using different underlying block devices */ - block_info[SECONDARY_BLOCK_DEVICE].log_bd = new MBRBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset); - - /* Initialize block device */ - int err = block_info[SECONDARY_BLOCK_DEVICE].log_bd->init(); - if (err) { - BOOT_LOG_ERR("Init U MBR"); - } - } else { - block_info[SECONDARY_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; - } - - /* Setup Raw sliced devices */ - if(block_info[SCRATCH_BLOCK_DEVICE].raw_flag) { - if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { - BOOT_LOG_ERR("S on IAP"); - } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { + // raw device, no offset + bd = new FlashIAPBlockDevice(0x8000000, 2 * 1024 * 1024); + } + } + if (storage_type & QSPI_FLASH_FLAG) { + BOOT_LOG_DBG(" QSPI_FLASH "); + bd = mbed::BlockDevice::get_default_instance(); + } + if (storage_type & SDCARD_FLAG) { #if MCUBOOT_APPLICATION_SDCARD - block_info[SCRATCH_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SCRATCH_BLOCK_DEVICE].raw_bd, block_info[SCRATCH_BLOCK_DEVICE].data_offset, block_info[SCRATCH_BLOCK_DEVICE].update_size); + BOOT_LOG_DBG(" SD_FLASH "); + bd = new SDMMCBlockDevice(); #else - BOOT_LOG_ERR("SDMMC"); + BOOT_LOG_ERR(" SD NOT SUPPORTED"); #endif - } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { - block_info[SCRATCH_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SCRATCH_BLOCK_DEVICE].raw_bd, block_info[SCRATCH_BLOCK_DEVICE].data_offset, block_info[SCRATCH_BLOCK_DEVICE].update_size); - } else { - BOOT_LOG_ERR("S config"); - } - } else - - /* Setup MBR devices */ - if(block_info[SCRATCH_BLOCK_DEVICE].mbr_flag) { - /* Setup MBR devices and FS if scratch and secondary are using different underlying block devices */ - block_info[SCRATCH_BLOCK_DEVICE].log_bd = new MBRBlockDevice(block_info[SCRATCH_BLOCK_DEVICE].raw_bd, block_info[SCRATCH_BLOCK_DEVICE].data_offset); - - /* Initialize block device */ - err = block_info[SCRATCH_BLOCK_DEVICE].log_bd->init(); - if (err) { - BOOT_LOG_ERR("Init S MBR"); - } - } else { - block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SCRATCH_BLOCK_DEVICE].raw_bd; - } - - /* Setup FS */ - if(!block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { - if((block_info[SECONDARY_BLOCK_DEVICE].storage_type & LITTLEFS_FLAG)) { -#if MCUBOOT_APPLICATION_LITTLEFS - block_info[SECONDARY_BLOCK_DEVICE].log_fs = new LittleFileSystem("sec"); -#else - BOOT_LOG_ERR("LFS"); -#endif - } else { - block_info[SECONDARY_BLOCK_DEVICE].log_fs = new FATFileSystem("sec"); - } - - err = block_info[SECONDARY_BLOCK_DEVICE].log_fs->mount(block_info[SECONDARY_BLOCK_DEVICE].log_bd); - if (err) { - BOOT_LOG_ERR("Mount U on LOG"); - } - - /* Setup FileBlockDevice */ - block_info[SECONDARY_BLOCK_DEVICE].file_bd = new FileBlockDevice("/sec/update.bin", "rb+", block_info[SECONDARY_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); - } - - /* Setup FS */ - if(!block_info[SCRATCH_BLOCK_DEVICE].raw_flag) { - if((block_info[SCRATCH_BLOCK_DEVICE].storage_type & LITTLEFS_FLAG)) { -#if MCUBOOT_APPLICATION_LITTLEFS - block_info[SCRATCH_BLOCK_DEVICE].log_fs = new LittleFileSystem("scr"); -#else - BOOT_LOG_ERR("LFS"); -#endif - } else { - block_info[SCRATCH_BLOCK_DEVICE].log_fs = new FATFileSystem("scr"); - } - - err = block_info[SCRATCH_BLOCK_DEVICE].log_fs->mount(block_info[SCRATCH_BLOCK_DEVICE].log_bd); - if (err) { - BOOT_LOG_ERR("Mount S on LOG"); - } - - /* Setup FileBlockDevice */ - block_info[SCRATCH_BLOCK_DEVICE].file_bd = new FileBlockDevice("/scr/scratch.bin", "rb+", block_info[SCRATCH_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); - } + } + if (storage_type & MBR_FLAG) { + BOOT_LOG_DBG(" MBR "); + BlockDevice* physical_block_device = bd; + bd = new mbed::MBRBlockDevice(physical_block_device, data_offset); + } + if (storage_type & LITTLEFS_FLAG) { + BOOT_LOG_DBG(" LITTLEFS "); + fs = new LittleFileSystem("fs"); + } + if (storage_type & FATFS_FLAG) { + BOOT_LOG_DBG(" FATFS "); + fs = new FATFileSystem("fs"); + } + if (fs != NULL) { + err = fs->mount(bd); + if (err) { + BOOT_LOG_DBG("Mount failed"); + return MOUNT_FAILED; } -} - -mbed::BlockDevice* get_secondary_bd(void) { - - if(!BlockTableLoaded) { - initBlockTable(); - BlockTableLoaded = true; + FILE* update = fopen("/fs/UPDATE.BIN", "rb"); + if (update == NULL) { + BOOT_LOG_DBG("No OTA file"); + return NO_OTA_FILE; } - - if(block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { - return block_info[SECONDARY_BLOCK_DEVICE].log_bd; - } else { - return block_info[SECONDARY_BLOCK_DEVICE].file_bd; + uint32_t temp[4]; + fread((uint8_t*)temp, 1, 4, update); + fseek(update, 0, SEEK_SET); + uint32_t base = getOTABinaryBase(temp[0]); + if (base == 0xFFFFFFFF) { + BOOT_LOG_DBG("Couldn't decide if M7 or M4"); + return WRONG_OTA_BINARY; } -} - -mbed::BlockDevice* get_scratch_bd(void) { - - if(!BlockTableLoaded) { - initBlockTable(); - BlockTableLoaded = true; + // Ignore update_size and use file size instead + update_size = getFilesize("/fs/UPDATE.BIN"); + uint32_t sector_size = flash.get_sector_size(base); + if (sector_size > 4096 * 4) { + sector_size = 4096 * 4; } - - if(block_info[SCRATCH_BLOCK_DEVICE].raw_flag) { - return block_info[SCRATCH_BLOCK_DEVICE].log_bd;; - } else { - return block_info[SCRATCH_BLOCK_DEVICE].file_bd; + uint8_t* buf = (uint8_t*)malloc(sector_size); + BOOT_LOG_DBG("Sector size: %d", sector_size); + for (uint32_t i = 0; i < update_size; i+= sector_size) { + fread(buf, 1, sector_size, update); + // erase? + if (((uint32_t)base + i) % flash.get_sector_size(base) == 0) { + BOOT_LOG_DBG("Erasing: %x %x", (uint32_t)base + i, flash.get_sector_size(base)); + flash.erase((uint32_t)base + i, flash.get_sector_size(base)); + wait_us(10000); + } + flash.program(buf, (uint32_t)base + i, sector_size); + wait_us(1000); + } + } else if (bd != NULL) { + // read first chuck of the file to understand if we need to flash the M4 or the M7 + err = bd->init(); + if (err != 0) { + BOOT_LOG_DBG("Init failed"); + return INIT_FAILED; + } + int sz = bd->get_program_size(); + if (sz < 16) { + sz = 16; } + uint32_t temp[sz]; + bd->read(temp, (uint32_t)data_offset, sz); + uint32_t base = getOTABinaryBase(temp[0]); + if (base == 0xFFFFFFFF) { + BOOT_LOG_DBG("Couldn't decide if M7 or M4"); + return WRONG_OTA_BINARY; + } + uint32_t sector_size = flash.get_sector_size(base); + if (sector_size > 4096 * 4) { + sector_size = 4096 * 4; + } + uint8_t* buf = (uint8_t*)malloc(sector_size); + for (uint32_t i = 0; i < update_size; i+= sector_size) { + bd->read(buf, (uint32_t)data_offset + i, sector_size); + // erase? + if (((uint32_t)base + i) % flash.get_sector_size(base) == 0) { + flash.erase((uint32_t)base + i, flash.get_sector_size(base)); + wait_us(10000); + } + flash.program(buf, (uint32_t)base + i, sector_size); + wait_us(1000); + } + } + flash.deinit(); + restart_timer.attach(NVIC_SystemReset, 0.2f); + return 0; } - -#endif diff --git a/app/ota/ota.h b/app/ota/ota.h index b889e44..79f1b4d 100644 --- a/app/ota/ota.h +++ b/app/ota/ota.h @@ -72,8 +72,6 @@ struct BlockTableData { #define NO_OTA_FILE (-3) #define INIT_FAILED (-4) -#define FILEBD_READ_SIZE 0x1 -#define FILEBD_WRITE_SIZE 0x1 -#define FILEBD_ERASE_SIZE 0x1000 +int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_size); #endif //__OTA_H diff --git a/app/target.cpp b/app/target.cpp index 802d699..61e18da 100644 --- a/app/target.cpp +++ b/app/target.cpp @@ -104,24 +104,22 @@ static bool valid_application() { } -static bool empty_keys() { +int target_empty_keys() { unsigned int i; - extern const unsigned char enc_priv_key[]; - extern const unsigned int enc_priv_key_len; - extern const unsigned char ecdsa_pub_key[]; - extern unsigned int ecdsa_pub_key_len; - - for(i = 0; i < enc_priv_key_len; i++) { - if(enc_priv_key[i] != 0xFF) - return false; + uint8_t* encript_key = (uint8_t*)(0x08000300); + uint8_t* signing_key = (uint8_t*)(0x08000400); + + for(i = 0; i < 256; i++) { + if(encript_key[i] != 0xFF) + return 0; } - for(i = 0; i < ecdsa_pub_key_len; i++) { - if(ecdsa_pub_key[i] != 0xFF) - return false; + for(i = 0; i < 256; i++) { + if(signing_key[i] != 0xFF) + return 0; } - return true; + return 1; } int target_debug_init(void) { @@ -238,19 +236,46 @@ int target_init(void) { HAL_Delay(10); - if (magic != 0xDF59 && magic != 0x07AA) { - RTCSetBKPRegister(RTC_BKP_DR0, 0); - HAL_FLASH_Lock(); - if(valid_application() && empty_keys()) { - BOOT_LOG_INF("MCUboot not configured, but valid image found."); - BOOT_LOG_INF("Booting firmware image at 0x%x\n", APP_DEFAULT_ADD); + if (magic == 0xDF59) { + /* Boot stopped by double reset */ + return 1; + } + + if (target_empty_keys()) { + BOOT_LOG_INF("Secure keys not configured"); + if ( magic == 0x07AA ) { + /* Try unsecure OTA */ + // DR1 contains the backing storage type, DR2 the offset in case of raw device / MBR + storageType storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1); + uint32_t offset = RTCGetBKPRegister(RTC_BKP_DR2); + uint32_t update_size = RTCGetBKPRegister(RTC_BKP_DR3); + BOOT_LOG_INF("Start OTA 0x%X 0x%X 0x%X", storage_type, offset, update_size); + int ota_result = tryOTA(storage_type, offset, update_size); + if (ota_result == 0) { + // clean reboot with success flag + BOOT_LOG_INF("Sketch updated"); + RTCSetBKPRegister(RTC_BKP_DR0, 0); + HAL_FLASH_Lock(); + // wait for external reboot (watchdog) + while (1) {} + } else { + RTCSetBKPRegister(RTC_BKP_DR0, ota_result); + } + } + + if (valid_application()) { + /* Boot Sketch */ + BOOT_LOG_INF("Booting sketch at 0x%x\n", APP_DEFAULT_ADD); mbed_start_application(APP_DEFAULT_ADD); + } else { + BOOT_LOG_INF("No sketch found"); + return 1; } - swap_ticker.attach(&swap_feedback, 250ms); - return 0; } else { - return 1; + /* MCUboot secure boot */ + swap_ticker.attach(&swap_feedback, 250ms); + return 0; } } diff --git a/app/target.h b/app/target.h index 8bd0716..60bffea 100644 --- a/app/target.h +++ b/app/target.h @@ -81,5 +81,6 @@ int target_debug_init(void); int target_loop(void); int target_debug(void); int target_led_off(void); +int target_empty_keys(void); #endif /* __TARGET_INIT_H */ diff --git a/default_bd.cpp b/default_bd.cpp index 95cef28..a756d7d 100644 --- a/default_bd.cpp +++ b/default_bd.cpp @@ -1,11 +1,408 @@ /* - * default_bd.cpp - * - * Created on: Jul 30, 2020 - * Author: gdbeckstein - */ + Copyright (c) 2022 Arduino SA. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#if MCUBOOT_APPLICATION_HOOKS + +#include "ota.h" +#include "rtc.h" +#include "target.h" +#include "bootutil/bootutil_log.h" + +#include "SlicingBlockDevice.h" +#include "FlashIAPBlockDevice.h" +#include "QSPIFBlockDevice.h" +#include "MBRBlockDevice.h" +#include "FileBlockDevice.h" +#include "FATFileSystem.h" + +#if MCUBOOT_APPLICATION_SDCARD +#include "SDMMCBlockDevice.h" +#endif + +#if MCUBOOT_APPLICATION_LITTLEFS +#include "LittleFileSystem.h" +#endif + +#define FILEBD_READ_SIZE 0x1 +#define FILEBD_WRITE_SIZE 0x1 +#define FILEBD_ERASE_SIZE 0x1000 + +static bool BlockTableLoaded = false; +static BlockTableData block_info[2]; + +static void loadOTAData(void) { + RTCInit(); + /* + * Magic 0x07AA is set by Arduino_Portenta_OTA + * Magic 0xDF59 is set by the loader if RESET_REASON_PIN_RESET + */ + + int magic = RTCGetBKPRegister(RTC_BKP_DR0); + if (magic == 0x07AA) { + // DR1 contains the backing storage type + // DR2 the offset in case of raw device / MBR + // DR3 the update size + block_info[SECONDARY_BLOCK_DEVICE].storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR1); + block_info[SECONDARY_BLOCK_DEVICE].raw_type = RTCGetBKPRegister(RTC_BKP_DR1) & 0x00000007; + block_info[SECONDARY_BLOCK_DEVICE].raw_flag = RTCGetBKPRegister(RTC_BKP_DR1) & RAW_FLAG; + block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = RTCGetBKPRegister(RTC_BKP_DR1) & MBR_FLAG; + block_info[SECONDARY_BLOCK_DEVICE].data_offset = RTCGetBKPRegister(RTC_BKP_DR2); + block_info[SECONDARY_BLOCK_DEVICE].update_size = RTCGetBKPRegister(RTC_BKP_DR3); + + // DR4 contains the backing storage type + // DR5 the offset in case of raw device / MBR + // DR6 the scratch size + block_info[SCRATCH_BLOCK_DEVICE].storage_type = (storageType)RTCGetBKPRegister(RTC_BKP_DR4); + block_info[SCRATCH_BLOCK_DEVICE].raw_type = RTCGetBKPRegister(RTC_BKP_DR4) & 0x00000007; + block_info[SCRATCH_BLOCK_DEVICE].raw_flag = RTCGetBKPRegister(RTC_BKP_DR4) & RAW_FLAG; + block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = RTCGetBKPRegister(RTC_BKP_DR4) & MBR_FLAG; + block_info[SCRATCH_BLOCK_DEVICE].data_offset = RTCGetBKPRegister(RTC_BKP_DR5); + block_info[SCRATCH_BLOCK_DEVICE].update_size = RTCGetBKPRegister(RTC_BKP_DR6); + BOOT_LOG_INF("Custom OTA data"); + + /* Print loaded Data */ + BOOT_LOG_INF("Secondary [%d] [%d]", block_info[SECONDARY_BLOCK_DEVICE].storage_type, block_info[SECONDARY_BLOCK_DEVICE].raw_type); + BOOT_LOG_INF("Scratch [%d] [%d]", block_info[SCRATCH_BLOCK_DEVICE].storage_type, block_info[SCRATCH_BLOCK_DEVICE].raw_type); + } else { +#if ALL_IN_SD + block_info[SECONDARY_BLOCK_DEVICE].storage_type = SD_FATFS; + block_info[SECONDARY_BLOCK_DEVICE].data_offset = 2; + block_info[SECONDARY_BLOCK_DEVICE].update_size = MCUBOOT_SLOT_SIZE; + block_info[SECONDARY_BLOCK_DEVICE].raw_type = SDCARD_FLAG; + block_info[SECONDARY_BLOCK_DEVICE].raw_flag = 0; + block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = 0; + + block_info[SCRATCH_BLOCK_DEVICE].storage_type = SD_FATFS; + block_info[SCRATCH_BLOCK_DEVICE].data_offset = 2; + block_info[SCRATCH_BLOCK_DEVICE].update_size = MCUBOOT_SCRATCH_SIZE; + block_info[SCRATCH_BLOCK_DEVICE].raw_type = SDCARD_FLAG; + block_info[SCRATCH_BLOCK_DEVICE].raw_flag = 0; + block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = 0; +#elif MIX_SD_QSPI + block_info[SECONDARY_BLOCK_DEVICE].storage_type = SD_FATFS; + block_info[SECONDARY_BLOCK_DEVICE].data_offset = 2; + block_info[SECONDARY_BLOCK_DEVICE].update_size = MCUBOOT_SLOT_SIZE; + block_info[SECONDARY_BLOCK_DEVICE].raw_type = SDCARD_FLAG; + block_info[SECONDARY_BLOCK_DEVICE].raw_flag = 0; + block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = 0; + + block_info[SCRATCH_BLOCK_DEVICE].storage_type = QSPI_FLASH_FATFS_MBR; + block_info[SCRATCH_BLOCK_DEVICE].data_offset = 2; + block_info[SCRATCH_BLOCK_DEVICE].update_size = MCUBOOT_SCRATCH_SIZE; + block_info[SCRATCH_BLOCK_DEVICE].raw_type = QSPI_FLASH_FLAG; + block_info[SCRATCH_BLOCK_DEVICE].raw_flag = 0; + block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = 1; +#else + block_info[SECONDARY_BLOCK_DEVICE].storage_type = QSPI_FLASH_FATFS_MBR; + block_info[SECONDARY_BLOCK_DEVICE].data_offset = 2; + block_info[SECONDARY_BLOCK_DEVICE].update_size = MCUBOOT_SLOT_SIZE; + block_info[SECONDARY_BLOCK_DEVICE].raw_type = QSPI_FLASH_FLAG; + block_info[SECONDARY_BLOCK_DEVICE].raw_flag = 0; + block_info[SECONDARY_BLOCK_DEVICE].mbr_flag = 1; + + block_info[SCRATCH_BLOCK_DEVICE].storage_type = QSPI_FLASH_FATFS_MBR; + block_info[SCRATCH_BLOCK_DEVICE].data_offset = 2; + block_info[SCRATCH_BLOCK_DEVICE].update_size = MCUBOOT_SCRATCH_SIZE; + block_info[SCRATCH_BLOCK_DEVICE].raw_type = QSPI_FLASH_FLAG; + block_info[SCRATCH_BLOCK_DEVICE].raw_flag = 0; + block_info[SCRATCH_BLOCK_DEVICE].mbr_flag = 1; +#endif + BOOT_LOG_INF("Default OTA data"); + } + return; +} + + +static void initBlockTable(void) { + int err; + + loadOTAData(); + + if(block_info[SECONDARY_BLOCK_DEVICE].raw_type == block_info[SCRATCH_BLOCK_DEVICE].raw_type) { + /* Declare raw block devices */ + if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { + //block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new FlashIAPBlockDevice flashIAP_bd(data_offset, update_size); + //block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; + BOOT_LOG_ERR("U on IAP"); + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD + block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new SDMMCBlockDevice(); + block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; +#else + BOOT_LOG_ERR("SDMMC"); +#endif + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { + block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new QSPIFBlockDevice(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); + block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; + } else { + BOOT_LOG_ERR("Config"); + } + + /* Setup sliced block devices */ + if(block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { + if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { + //block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new FlashIAPBlockDevice flashIAP_bd(data_offset, update_size); + //block_info[SCRATCH_BLOCK_DEVICE].raw_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; + BOOT_LOG_ERR("U on IAP"); + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD + block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); + block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; +#else + BOOT_LOG_ERR("SDMMC"); +#endif + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { + block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); + block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; + } else { + BOOT_LOG_ERR("Config"); + } + } else + + /* Setup MBR device */ + if(block_info[SECONDARY_BLOCK_DEVICE].mbr_flag) { + /* If using the same underlying block device configuration must be the same */ + if(block_info[SECONDARY_BLOCK_DEVICE].storage_type != block_info[SCRATCH_BLOCK_DEVICE].storage_type) { + BOOT_LOG_ERR("BD U!S"); + } + + /* If using the same underlying block device mbr partition must be the same */ + if(block_info[SECONDARY_BLOCK_DEVICE].data_offset != block_info[SCRATCH_BLOCK_DEVICE].data_offset) { + BOOT_LOG_ERR("MBR U!S"); + } + + block_info[SECONDARY_BLOCK_DEVICE].log_bd = new MBRBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset); + block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; + + /* Initialize block device */ + err = block_info[SECONDARY_BLOCK_DEVICE].log_bd->init(); + if (err) { + BOOT_LOG_ERR("Init"); + } + } else { + block_info[SECONDARY_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; + block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].log_bd; + } + + /* Setup FS */ + if(!block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { + if((block_info[SECONDARY_BLOCK_DEVICE].storage_type & LITTLEFS_FLAG)) { +#if MCUBOOT_APPLICATION_LITTLEFS + block_info[SECONDARY_BLOCK_DEVICE].log_fs = new LittleFileSystem("fs"); +#else + BOOT_LOG_ERR("LFS"); +#endif + } else { + block_info[SECONDARY_BLOCK_DEVICE].log_fs = new FATFileSystem("fs"); + } + + block_info[SCRATCH_BLOCK_DEVICE].log_fs = block_info[SECONDARY_BLOCK_DEVICE].log_fs; + + err = block_info[SECONDARY_BLOCK_DEVICE].log_fs->mount(block_info[SECONDARY_BLOCK_DEVICE].log_bd); + if (err) { + BOOT_LOG_ERR("Mount"); + } + + /* Setup FileBlockDevice */ + block_info[SECONDARY_BLOCK_DEVICE].file_bd = new FileBlockDevice("/fs/update.bin", "rb+", block_info[SECONDARY_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); + block_info[SCRATCH_BLOCK_DEVICE].file_bd = new FileBlockDevice("/fs/scratch.bin", "rb+", block_info[SCRATCH_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); + } + + } else { + /* Declare raw block devices */ + if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { + //block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new FlashIAPBlockDevice flashIAP_bd(data_offset, update_size); + BOOT_LOG_ERR("U on IAP"); + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD + block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new SDMMCBlockDevice(); +#else + BOOT_LOG_ERR("SDMMC"); +#endif + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { + block_info[SECONDARY_BLOCK_DEVICE].raw_bd = new QSPIFBlockDevice(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); + } else { + BOOT_LOG_ERR("U config"); + } + + if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { + BOOT_LOG_ERR("S on IAP"); + } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD + block_info[SCRATCH_BLOCK_DEVICE].raw_bd = new SDMMCBlockDevice(); +#else + BOOT_LOG_ERR("SDMMC"); +#endif + } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { + block_info[SCRATCH_BLOCK_DEVICE].raw_bd = new QSPIFBlockDevice(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); + } else { + BOOT_LOG_ERR("S config"); + } + + /* Setup Raw sliced devices */ + if(block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { + if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { + BOOT_LOG_ERR("U on IAP"); + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD + block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); +#else + BOOT_LOG_ERR("SDMMC"); +#endif + } else if (block_info[SECONDARY_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { + block_info[SECONDARY_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset, block_info[SECONDARY_BLOCK_DEVICE].update_size); + } else { + BOOT_LOG_ERR("U config"); + } + } else + + /* Setup MBR devices */ + if(block_info[SECONDARY_BLOCK_DEVICE].mbr_flag) { + /* Setup MBR devices and FS if scratch and secondary are using different underlying block devices */ + block_info[SECONDARY_BLOCK_DEVICE].log_bd = new MBRBlockDevice(block_info[SECONDARY_BLOCK_DEVICE].raw_bd, block_info[SECONDARY_BLOCK_DEVICE].data_offset); + + /* Initialize block device */ + int err = block_info[SECONDARY_BLOCK_DEVICE].log_bd->init(); + if (err) { + BOOT_LOG_ERR("Init U MBR"); + } + } else { + block_info[SECONDARY_BLOCK_DEVICE].log_bd = block_info[SECONDARY_BLOCK_DEVICE].raw_bd; + } + + /* Setup Raw sliced devices */ + if(block_info[SCRATCH_BLOCK_DEVICE].raw_flag) { + if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & INTERNAL_FLASH_FLAG) { + BOOT_LOG_ERR("S on IAP"); + } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD + block_info[SCRATCH_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SCRATCH_BLOCK_DEVICE].raw_bd, block_info[SCRATCH_BLOCK_DEVICE].data_offset, block_info[SCRATCH_BLOCK_DEVICE].update_size); +#else + BOOT_LOG_ERR("SDMMC"); +#endif + } else if (block_info[SCRATCH_BLOCK_DEVICE].raw_type & QSPI_FLASH_FLAG) { + block_info[SCRATCH_BLOCK_DEVICE].log_bd = new SlicingBlockDevice(block_info[SCRATCH_BLOCK_DEVICE].raw_bd, block_info[SCRATCH_BLOCK_DEVICE].data_offset, block_info[SCRATCH_BLOCK_DEVICE].update_size); + } else { + BOOT_LOG_ERR("S config"); + } + } else + + /* Setup MBR devices */ + if(block_info[SCRATCH_BLOCK_DEVICE].mbr_flag) { + /* Setup MBR devices and FS if scratch and secondary are using different underlying block devices */ + block_info[SCRATCH_BLOCK_DEVICE].log_bd = new MBRBlockDevice(block_info[SCRATCH_BLOCK_DEVICE].raw_bd, block_info[SCRATCH_BLOCK_DEVICE].data_offset); + + /* Initialize block device */ + err = block_info[SCRATCH_BLOCK_DEVICE].log_bd->init(); + if (err) { + BOOT_LOG_ERR("Init S MBR"); + } + } else { + block_info[SCRATCH_BLOCK_DEVICE].log_bd = block_info[SCRATCH_BLOCK_DEVICE].raw_bd; + } + + /* Setup FS */ + if(!block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { + if((block_info[SECONDARY_BLOCK_DEVICE].storage_type & LITTLEFS_FLAG)) { +#if MCUBOOT_APPLICATION_LITTLEFS + block_info[SECONDARY_BLOCK_DEVICE].log_fs = new LittleFileSystem("sec"); +#else + BOOT_LOG_ERR("LFS"); +#endif + } else { + block_info[SECONDARY_BLOCK_DEVICE].log_fs = new FATFileSystem("sec"); + } + + err = block_info[SECONDARY_BLOCK_DEVICE].log_fs->mount(block_info[SECONDARY_BLOCK_DEVICE].log_bd); + if (err) { + BOOT_LOG_ERR("Mount U on LOG"); + } + + /* Setup FileBlockDevice */ + block_info[SECONDARY_BLOCK_DEVICE].file_bd = new FileBlockDevice("/sec/update.bin", "rb+", block_info[SECONDARY_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); + } + + /* Setup FS */ + if(!block_info[SCRATCH_BLOCK_DEVICE].raw_flag) { + if((block_info[SCRATCH_BLOCK_DEVICE].storage_type & LITTLEFS_FLAG)) { +#if MCUBOOT_APPLICATION_LITTLEFS + block_info[SCRATCH_BLOCK_DEVICE].log_fs = new LittleFileSystem("scr"); +#else + BOOT_LOG_ERR("LFS"); +#endif + } else { + block_info[SCRATCH_BLOCK_DEVICE].log_fs = new FATFileSystem("scr"); + } + + err = block_info[SCRATCH_BLOCK_DEVICE].log_fs->mount(block_info[SCRATCH_BLOCK_DEVICE].log_bd); + if (err) { + BOOT_LOG_ERR("Mount S on LOG"); + } + + /* Setup FileBlockDevice */ + block_info[SCRATCH_BLOCK_DEVICE].file_bd = new FileBlockDevice("/scr/scratch.bin", "rb+", block_info[SCRATCH_BLOCK_DEVICE].update_size, FILEBD_READ_SIZE, FILEBD_WRITE_SIZE, FILEBD_ERASE_SIZE); + } + } +} + +mbed::BlockDevice* get_secondary_bd(void) { + + if(!target_empty_keys()) { + if(!BlockTableLoaded) { + initBlockTable(); + BlockTableLoaded = true; + } + + if(block_info[SECONDARY_BLOCK_DEVICE].raw_flag) { + return block_info[SECONDARY_BLOCK_DEVICE].log_bd; + } else { + return block_info[SECONDARY_BLOCK_DEVICE].file_bd; + } + } + return nullptr; +} + +mbed::BlockDevice* get_scratch_bd(void) { + + if(!target_empty_keys()) { + if(!BlockTableLoaded) { + initBlockTable(); + BlockTableLoaded = true; + } + + if(block_info[SCRATCH_BLOCK_DEVICE].raw_flag) { + return block_info[SCRATCH_BLOCK_DEVICE].log_bd;; + } else { + return block_info[SCRATCH_BLOCK_DEVICE].file_bd; + } + } + return nullptr; +} + +mbed::BlockDevice* BlockDevice::get_default_instance() +{ + static QSPIFBlockDevice default_bd(PD_11, PD_12, PF_7, PD_13, PF_10, PG_6, QSPIF_POLARITY_MODE_1, 40000000); + return &default_bd; +} + +FlashIAP flash; -#if ! MCUBOOT_APPLICATION_HOOKS +#else // MCUBOOT_APPLICATION_HOOKS #include "BlockDevice.h" #include "SlicingBlockDevice.h" diff --git a/generate_lib.sh b/generate_lib.sh index 7aede7d..1ba7f73 100755 --- a/generate_lib.sh +++ b/generate_lib.sh @@ -2,7 +2,7 @@ mbed compile -c -m PORTENTA_H7_M7 -t GCC_ARM --app=mbed_app_bootutil.json echo echo Generating library -find ./BUILD/PORTENTA_H7_M7/GCC_ARM/ \( -name "FileBlockDevice.o" -o -name "rtc.o" -o -name "ota.o" -o -name "bootutil_extra.o" -o -name "flash_map_backend.o" -o -name "bootutil_public.o" \) | xargs arm-none-eabi-ar -csr libbootutil.a +find ./BUILD/PORTENTA_H7_M7/GCC_ARM/ \( -name "FileBlockDevice.o" -o -name "BSP.o" -o -name "SDMMCBlockDevice.o" -o -name "target.o" -o -name "rtc.o" -o -name "default_bd.o" -o -name "bootutil_extra.o" -o -name "flash_map_backend.o" -o -name "bootutil_public.o" \) | xargs arm-none-eabi-ar -csr libbootutil.a echo -n "Library: " find ./ -name "libbootutil.a" echo diff --git a/mbed_app_bootutil.json b/mbed_app_bootutil.json index 65d9fe2..44bbed8 100644 --- a/mbed_app_bootutil.json +++ b/mbed_app_bootutil.json @@ -71,7 +71,7 @@ "mcuboot.bootstrap": true, "mcuboot.application-hooks": true, "mcuboot.application-littlefs": null, - "mcuboot.application-sdcard": null, + "mcuboot.application-sdcard": true, "mcuboot.application-dfu": null, "mcuboot.signature-algorithm": "SIGNATURE_TYPE_EC256", "mcuboot.encrypt-ec256": true,