From d53be11f067d8dcb0527fc8b260a74e0fe9fdc3d Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 09:26:34 +0200 Subject: [PATCH 01/14] Move block device initialization code from ota.cpp to default_bd.cpp --- app/ota/ota.cpp | 386 ----------------------------------------------- app/ota/ota.h | 4 - default_bd.cpp | 394 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 388 insertions(+), 396 deletions(-) diff --git a/app/ota/ota.cpp b/app/ota/ota.cpp index 126d8ea..e69de29 100644 --- a/app/ota/ota.cpp +++ b/app/ota/ota.cpp @@ -1,386 +0,0 @@ -/* - 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 "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 - -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(!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; - } -} - -mbed::BlockDevice* get_scratch_bd(void) { - - 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; - } -} - -#endif diff --git a/app/ota/ota.h b/app/ota/ota.h index b889e44..0d15938 100644 --- a/app/ota/ota.h +++ b/app/ota/ota.h @@ -72,8 +72,4 @@ 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 - #endif //__OTA_H diff --git a/default_bd.cpp b/default_bd.cpp index 95cef28..ae6014d 100644 --- a/default_bd.cpp +++ b/default_bd.cpp @@ -1,11 +1,393 @@ /* - * 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 "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(!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; + } +} + +mbed::BlockDevice* get_scratch_bd(void) { + + 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; + } +} -#if ! MCUBOOT_APPLICATION_HOOKS +#else // MCUBOOT_APPLICATION_HOOKS #include "BlockDevice.h" #include "SlicingBlockDevice.h" From cb790e182e0331e1d1e3388347ebe0cc4c4a17b3 Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 09:35:06 +0200 Subject: [PATCH 02/14] Restore unsecure OTA code --- app/ota/ota.cpp | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ app/ota/ota.h | 2 + 2 files changed, 189 insertions(+) diff --git a/app/ota/ota.cpp b/app/ota/ota.cpp index e69de29..729a941 100644 --- a/app/ota/ota.cpp +++ b/app/ota/ota.cpp @@ -0,0 +1,187 @@ +/* + 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 +*/ + +#include "mbed.h" +#include "QSPIFBlockDevice.h" +#include "FlashIAPBlockDevice.h" +#include "MBRBlockDevice.h" +#include "SDMMCBlockDevice.h" + +#include "FATFileSystem.h" +#include "LittleFileSystem.h" + +#include "ota.h" + +// Debug available +#ifndef OTA_DEBUG +#define OTA_DEBUG 0 +#endif + +#if OTA_DEBUG +#define DEBUG_PRINTF(...) printf(__VA_ARGS__) +#else +#define DEBUG_PRINTF(...) +#endif + +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) { + + DEBUG_PRINTF("First OTA binary word: %lx\n", firstWord); + if ((firstWord & 0xFF000000) == 0x20000000 + || (firstWord & 0xFF000000) == 0x24000000 + || (firstWord & 0xFF000000) == 0x30000000 + || (firstWord & 0xFF000000) == 0x38000000) { + DEBUG_PRINTF("Flashing on M7\n"); + return M7_FLASH_BASE; + } + if ((firstWord & 0xFF000000) == 0x10000000) { + DEBUG_PRINTF("Flashing on M4\n"); + return M4_FLASH_BASE; + } + return 0xFFFFFFFF; +} + +static Timeout restart_timer; + +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(); + DEBUG_PRINTF("Configuration: \n"); + if (storage_type & INTERNAL_FLASH_FLAG) { + DEBUG_PRINTF(" INTERNAL_FLASH \n"); + 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 { + // raw device, no offset + bd = new FlashIAPBlockDevice(0x8000000, 2 * 1024 * 1024); + } + } + if (storage_type & QSPI_FLASH_FLAG) { + DEBUG_PRINTF(" QSPI_FLASH \n"); + extern QSPIFBlockDevice qspi_flash; + bd = &qspi_flash; + } + if (storage_type & SDCARD_FLAG) { + DEBUG_PRINTF(" SD_FLASH \n"); + bd = new SDMMCBlockDevice(); + } + if (storage_type & MBR_FLAG) { + DEBUG_PRINTF(" MBR \n"); + BlockDevice* physical_block_device = bd; + bd = new mbed::MBRBlockDevice(physical_block_device, data_offset); + } + if (storage_type & LITTLEFS_FLAG) { + DEBUG_PRINTF(" LITTLEFS \n"); + fs = new LittleFileSystem("fs"); + } + if (storage_type & FATFS_FLAG) { + DEBUG_PRINTF(" FATFS \n"); + fs = new FATFileSystem("fs"); + } + if (fs != NULL) { + err = fs->mount(bd); + if (err) { + DEBUG_PRINTF("Mount failed\n"); + return MOUNT_FAILED; + } + FILE* update = fopen("/fs/UPDATE.BIN", "rb"); + if (update == NULL) { + DEBUG_PRINTF("No OTA file\n"); + return NO_OTA_FILE; + } + 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) { + DEBUG_PRINTF("Couldn't decide if M7 or M4\n"); + return WRONG_OTA_BINARY; + } + // 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; + } + uint8_t* buf = (uint8_t*)malloc(sector_size); + DEBUG_PRINTF("Sector size: %d\n", 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) { + DEBUG_PRINTF("Erasing: %x %x\n", (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) { + DEBUG_PRINTF("Init failed\n"); + 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) { + DEBUG_PRINTF("Couldn't decide if M7 or M4\n"); + 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; +} diff --git a/app/ota/ota.h b/app/ota/ota.h index 0d15938..79f1b4d 100644 --- a/app/ota/ota.h +++ b/app/ota/ota.h @@ -72,4 +72,6 @@ struct BlockTableData { #define NO_OTA_FILE (-3) #define INIT_FAILED (-4) +int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_size); + #endif //__OTA_H From a2a87f5486dfd37782f2bfaaf5870acef0c43e9c Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 09:39:30 +0200 Subject: [PATCH 03/14] OTA: align debug macros to the rest of the project --- app/ota/ota.cpp | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/app/ota/ota.cpp b/app/ota/ota.cpp index 729a941..4dc3ed5 100644 --- a/app/ota/ota.cpp +++ b/app/ota/ota.cpp @@ -26,17 +26,7 @@ #include "LittleFileSystem.h" #include "ota.h" - -// Debug available -#ifndef OTA_DEBUG -#define OTA_DEBUG 0 -#endif - -#if OTA_DEBUG -#define DEBUG_PRINTF(...) printf(__VA_ARGS__) -#else -#define DEBUG_PRINTF(...) -#endif +#include "bootutil/bootutil_log.h" BlockDevice* bd = NULL; mbed::FileSystem* fs = NULL; @@ -48,16 +38,16 @@ const uint32_t M4_FLASH_BASE = 0x8100000; uint32_t getOTABinaryBase(uint32_t firstWord) { - DEBUG_PRINTF("First OTA binary word: %lx\n", firstWord); + BOOT_LOG_DBG("First OTA binary word: %lx", firstWord); if ((firstWord & 0xFF000000) == 0x20000000 || (firstWord & 0xFF000000) == 0x24000000 || (firstWord & 0xFF000000) == 0x30000000 || (firstWord & 0xFF000000) == 0x38000000) { - DEBUG_PRINTF("Flashing on M7\n"); + BOOT_LOG_DBG("Flashing on M7"); return M7_FLASH_BASE; } if ((firstWord & 0xFF000000) == 0x10000000) { - DEBUG_PRINTF("Flashing on M4\n"); + BOOT_LOG_DBG("Flashing on M4"); return M4_FLASH_BASE; } return 0xFFFFFFFF; @@ -76,9 +66,9 @@ size_t getFilesize(const char* filename) { int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_size) { int err; flash.init(); - DEBUG_PRINTF("Configuration: \n"); + BOOT_LOG_DBG("Configuration: "); if (storage_type & INTERNAL_FLASH_FLAG) { - DEBUG_PRINTF(" INTERNAL_FLASH \n"); + 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); @@ -88,36 +78,36 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ } } if (storage_type & QSPI_FLASH_FLAG) { - DEBUG_PRINTF(" QSPI_FLASH \n"); + BOOT_LOG_DBG(" QSPI_FLASH "); extern QSPIFBlockDevice qspi_flash; bd = &qspi_flash; } if (storage_type & SDCARD_FLAG) { - DEBUG_PRINTF(" SD_FLASH \n"); + BOOT_LOG_DBG(" SD_FLASH "); bd = new SDMMCBlockDevice(); } if (storage_type & MBR_FLAG) { - DEBUG_PRINTF(" MBR \n"); + BOOT_LOG_DBG(" MBR "); BlockDevice* physical_block_device = bd; bd = new mbed::MBRBlockDevice(physical_block_device, data_offset); } if (storage_type & LITTLEFS_FLAG) { - DEBUG_PRINTF(" LITTLEFS \n"); + BOOT_LOG_DBG(" LITTLEFS "); fs = new LittleFileSystem("fs"); } if (storage_type & FATFS_FLAG) { - DEBUG_PRINTF(" FATFS \n"); + BOOT_LOG_DBG(" FATFS "); fs = new FATFileSystem("fs"); } if (fs != NULL) { err = fs->mount(bd); if (err) { - DEBUG_PRINTF("Mount failed\n"); + BOOT_LOG_DBG("Mount failed"); return MOUNT_FAILED; } FILE* update = fopen("/fs/UPDATE.BIN", "rb"); if (update == NULL) { - DEBUG_PRINTF("No OTA file\n"); + BOOT_LOG_DBG("No OTA file"); return NO_OTA_FILE; } uint32_t temp[4]; @@ -125,7 +115,7 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ fseek(update, 0, SEEK_SET); uint32_t base = getOTABinaryBase(temp[0]); if (base == 0xFFFFFFFF) { - DEBUG_PRINTF("Couldn't decide if M7 or M4\n"); + BOOT_LOG_DBG("Couldn't decide if M7 or M4"); return WRONG_OTA_BINARY; } // Ignore update_size and use file size instead @@ -135,12 +125,12 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ sector_size = 4096 * 4; } uint8_t* buf = (uint8_t*)malloc(sector_size); - DEBUG_PRINTF("Sector size: %d\n", 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) { - DEBUG_PRINTF("Erasing: %x %x\n", (uint32_t)base + i, flash.get_sector_size(base)); + 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); } @@ -151,7 +141,7 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ // read first chuck of the file to understand if we need to flash the M4 or the M7 err = bd->init(); if (err != 0) { - DEBUG_PRINTF("Init failed\n"); + BOOT_LOG_DBG("Init failed"); return INIT_FAILED; } int sz = bd->get_program_size(); @@ -162,7 +152,7 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ bd->read(temp, (uint32_t)data_offset, sz); uint32_t base = getOTABinaryBase(temp[0]); if (base == 0xFFFFFFFF) { - DEBUG_PRINTF("Couldn't decide if M7 or M4\n"); + BOOT_LOG_DBG("Couldn't decide if M7 or M4"); return WRONG_OTA_BINARY; } uint32_t sector_size = flash.get_sector_size(base); From c688839a5e7278036ccbe740289c2134dab0c9dc Mon Sep 17 00:00:00 2001 From: pennam Date: Mon, 4 Apr 2022 09:40:19 +0200 Subject: [PATCH 04/14] Update boot flow to allow unsecure OTA if keys are not configured --- app/target.cpp | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/app/target.cpp b/app/target.cpp index 802d699..29e0045 100644 --- a/app/target.cpp +++ b/app/target.cpp @@ -238,19 +238,45 @@ 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 (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_DBG("Try 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 + 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; } } From 678800ca4ae04e165ac1721486a7ab8e8178fb81 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 09:37:27 +0200 Subject: [PATCH 05/14] Allow unsecure OTA build without SDMMC support --- app/ota/ota.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/ota/ota.cpp b/app/ota/ota.cpp index 4dc3ed5..c238191 100644 --- a/app/ota/ota.cpp +++ b/app/ota/ota.cpp @@ -83,8 +83,12 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ bd = &qspi_flash; } if (storage_type & SDCARD_FLAG) { +#if MCUBOOT_APPLICATION_SDCARD BOOT_LOG_DBG(" SD_FLASH "); bd = new SDMMCBlockDevice(); +#else + BOOT_LOG_ERR(" SD NOT SUPPORTED"); +#endif } if (storage_type & MBR_FLAG) { BOOT_LOG_DBG(" MBR "); From dfa2c79b442876837013cfd7947c8837c2065ee7 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 09:38:57 +0200 Subject: [PATCH 06/14] Use BlockDevice::get_default_instance in dfu and ota code --- app/dfu/usbd_dfu_flash.cpp | 17 ++++++++--------- app/ota/ota.cpp | 5 ++--- default_bd.cpp | 6 ++++++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/dfu/usbd_dfu_flash.cpp b/app/dfu/usbd_dfu_flash.cpp index 09c6e79..5de7cc1 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]; @@ -55,7 +54,7 @@ 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); +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,7 +79,7 @@ bool Flash_If_Init_requested = false; void init_Memories() { flash.init(); - qspi_flash.init(); + qspi_flash->init(); dfu_secondary_bd->init(); snprintf(BOOTLOADER_DESC_STR, sizeof(BOOTLOADER_DESC_STR), "@MCUBoot version %d /0x00000000/0*4Kg", BOOTLOADER_VERSION); } @@ -133,7 +132,7 @@ uint16_t Flash_If_Erase(uint32_t Add) 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)); } @@ -171,10 +170,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); @@ -204,7 +203,7 @@ uint8_t *Flash_If_Read(uint8_t * src, uint8_t * dest, uint32_t Len) 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 c238191..556f419 100644 --- a/app/ota/ota.cpp +++ b/app/ota/ota.cpp @@ -17,7 +17,7 @@ */ #include "mbed.h" -#include "QSPIFBlockDevice.h" +#include "BlockDevice.h" #include "FlashIAPBlockDevice.h" #include "MBRBlockDevice.h" #include "SDMMCBlockDevice.h" @@ -79,8 +79,7 @@ int tryOTA(enum storageType storage_type, uint32_t data_offset, uint32_t update_ } if (storage_type & QSPI_FLASH_FLAG) { BOOT_LOG_DBG(" QSPI_FLASH "); - extern QSPIFBlockDevice qspi_flash; - bd = &qspi_flash; + bd = mbed::BlockDevice::get_default_instance(); } if (storage_type & SDCARD_FLAG) { #if MCUBOOT_APPLICATION_SDCARD diff --git a/default_bd.cpp b/default_bd.cpp index ae6014d..7e9ea80 100644 --- a/default_bd.cpp +++ b/default_bd.cpp @@ -387,6 +387,12 @@ mbed::BlockDevice* get_scratch_bd(void) { } } +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; +} + #else // MCUBOOT_APPLICATION_HOOKS #include "BlockDevice.h" From 6f0efd375f19bdde2f07a1ead800df81de5813db Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 09:47:08 +0200 Subject: [PATCH 07/14] Move FlashIAP declaration from dfu to default_bd --- app/dfu/usbd_dfu_flash.cpp | 2 +- default_bd.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/dfu/usbd_dfu_flash.cpp b/app/dfu/usbd_dfu_flash.cpp index 5de7cc1..6a88b93 100644 --- a/app/dfu/usbd_dfu_flash.cpp +++ b/app/dfu/usbd_dfu_flash.cpp @@ -53,7 +53,7 @@ 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; +extern FlashIAP flash; mbed::BlockDevice* qspi_flash = mbed::BlockDevice::get_default_instance(); mbed::BlockDevice* dfu_secondary_bd = get_secondary_bd(); diff --git a/default_bd.cpp b/default_bd.cpp index 7e9ea80..b640a3d 100644 --- a/default_bd.cpp +++ b/default_bd.cpp @@ -393,6 +393,8 @@ mbed::BlockDevice* BlockDevice::get_default_instance() return &default_bd; } +FlashIAP flash; + #else // MCUBOOT_APPLICATION_HOOKS #include "BlockDevice.h" From b2041fa10f1b2654929cae067bb5a66e1e555a70 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 09:52:12 +0200 Subject: [PATCH 08/14] Update script to generate libbootutil including default_bd object file instead of ota --- generate_lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate_lib.sh b/generate_lib.sh index 7aede7d..f1ef955 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 "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 From ecec1115751d4e72237b2c99af5974d2908493df Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 11:23:35 +0200 Subject: [PATCH 09/14] Add exported function to check if security keys are configured --- app/target.cpp | 10 +++++----- app/target.h | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/target.cpp b/app/target.cpp index 29e0045..9765b00 100644 --- a/app/target.cpp +++ b/app/target.cpp @@ -104,7 +104,7 @@ 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; @@ -113,15 +113,15 @@ static bool empty_keys() { for(i = 0; i < enc_priv_key_len; i++) { if(enc_priv_key[i] != 0xFF) - return false; + return 0; } for(i = 0; i < ecdsa_pub_key_len; i++) { if(ecdsa_pub_key[i] != 0xFF) - return false; + return 0; } - return true; + return 1; } int target_debug_init(void) { @@ -243,7 +243,7 @@ int target_init(void) { return 1; } - if (empty_keys()) { + if (target_empty_keys()) { BOOT_LOG_INF("Secure keys not configured"); if ( magic == 0x07AA ) { /* Try unsecure OTA */ 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 */ From 55226e24ffd5b58707b6032dc1cfc1249b6e8a9a Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 11:24:33 +0200 Subject: [PATCH 10/14] Check if security keys are configured before loading MCUboot block device table --- default_bd.cpp | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/default_bd.cpp b/default_bd.cpp index b640a3d..a756d7d 100644 --- a/default_bd.cpp +++ b/default_bd.cpp @@ -20,6 +20,7 @@ #include "ota.h" #include "rtc.h" +#include "target.h" #include "bootutil/bootutil_log.h" #include "SlicingBlockDevice.h" @@ -361,30 +362,36 @@ static void initBlockTable(void) { mbed::BlockDevice* get_secondary_bd(void) { - if(!BlockTableLoaded) { - initBlockTable(); - BlockTableLoaded = true; - } + 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; + 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(!BlockTableLoaded) { - initBlockTable(); - BlockTableLoaded = true; - } + 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; + 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() From ac127bbeb790e4df0d11e815d12b8d1539e89a8f Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 11:25:55 +0200 Subject: [PATCH 11/14] Check if secondary block device is used and initialized to avoid dfu crash --- app/dfu/usbd_dfu_flash.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/dfu/usbd_dfu_flash.cpp b/app/dfu/usbd_dfu_flash.cpp index 6a88b93..6a78d69 100644 --- a/app/dfu/usbd_dfu_flash.cpp +++ b/app/dfu/usbd_dfu_flash.cpp @@ -80,7 +80,9 @@ bool Flash_If_Init_requested = false; void init_Memories() { flash.init(); qspi_flash->init(); - dfu_secondary_bd->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); } @@ -107,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; } @@ -127,7 +131,9 @@ 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)) { @@ -159,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)); @@ -198,7 +206,9 @@ 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)) { From 109b94352f6954f8a434a04523f8b3d82a58dafc Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 11:26:57 +0200 Subject: [PATCH 12/14] Add BOOT_LOG_INF to check unsecure OTA flow --- app/target.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/target.cpp b/app/target.cpp index 9765b00..d2313a1 100644 --- a/app/target.cpp +++ b/app/target.cpp @@ -251,10 +251,11 @@ int target_init(void) { 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_DBG("Try OTA 0x%x, 0x%x, 0x%x", storage_type, offset, update_size); + 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) From 52f5a5e349a50f8bb382682fd87889d84c83234e Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 12:26:12 +0200 Subject: [PATCH 13/14] Use flash address to check for empty keys --- app/target.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/target.cpp b/app/target.cpp index d2313a1..61e18da 100644 --- a/app/target.cpp +++ b/app/target.cpp @@ -106,18 +106,16 @@ static bool valid_application() { 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; + uint8_t* encript_key = (uint8_t*)(0x08000300); + uint8_t* signing_key = (uint8_t*)(0x08000400); - for(i = 0; i < enc_priv_key_len; i++) { - if(enc_priv_key[i] != 0xFF) + 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) + for(i = 0; i < 256; i++) { + if(signing_key[i] != 0xFF) return 0; } From 536f85699afef2498993eac22a2423966d187526 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 5 Apr 2022 12:27:14 +0200 Subject: [PATCH 14/14] Include SDMMC object files in libbootutil --- generate_lib.sh | 2 +- mbed_app_bootutil.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/generate_lib.sh b/generate_lib.sh index f1ef955..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 "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 +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,