#include "bootFunctions.h" #include "Common/MEMORY.h" #include "EEMEM.h" #include "keys.h" uint8_t CheckFirmware(){ boot_rww_enable_safe(); uint32_t crcRead = flash_read_dword(STATIC_DATA_APP.AppCRC,0); uint32_t crcCalc = CRC32(NULL,locationInApp<<1); if (crcRead != crcCalc) return IDIFMW_REASON_NoFmwDetected; return 0; } uint8_t CheckEEPROMFlag(){ eeprom_busy_wait(); uint8_t flag = eeprom_read_byte(&(EEBLOCK.BOOTFLAG)); if (flag != 0xFF) return IDIFMW_REASON_EnterFromApplication; return 0; } inline uint8_t CheckADDR(){ if (RS_Address == IDIBUS_DEVELOPER_ADDR_3) return IDIFMW_REASON_EnterForced; return 0; } inline void DisableAES(){ AES_DISABLED = 1; } #ifdef _LONG_ADDR_SPACE_ uint32_t baseAddr; #else uint16_t baseAddr; #endif void BootloaderDataHandler(){ wdt_reset(); boot_spm_busy_wait(); if (BootloaderInfo.BlockNumber == -1) { if (AES_DISABLED == 0) { AES_PrepareKeys(key,IV); AES_Decrypt(BootloaderData,FMW_INFO_SIZE); } _memcopy(BootloaderData,&FmwInfo,FMW_INFO_SIZE); uint32_t calculatedCRC = CRC32(&FmwInfo,FMW_INFO_SIZE-CRC32_SIZE); uint8_t Check = 1; if (calculatedCRC != FmwInfo.InfoHash) { Check = 0; } if (FmwInfo.FmwSizeInPages >= PAGE_COUNT) { Check = 0; } for (uint8_t i = 0; i < IDISN_FIXP_MODULE_TYPE_Length; i++) { if (flash_read_byte(STATIC_DATA_BL.ModuleType,i) != FmwInfo.ModuleType[i]) Check = 0; } for (uint8_t i = 0; i < IDISN_FIXP_HW_REV_Length; i++) { if (flash_read_byte(STATIC_DATA_BL.HW_revision,i) != FmwInfo.HwRevision[i]) Check = 0; } if (Check == 0) { BootloaderInfo.Status = IDIFMW_STATUS_IncompatibleFmw; return; } for (uint16_t page = 0; page < PAGE_COUNT; page++){ boot_spm_busy_wait(); #ifdef _LONG_ADDR_SPACE_ boot_page_erase(((uint32_t)page)<>8), "M" (RAMEND&0xFF), "M" (RAMEND>>8)); while(1); } void FindBootloaderEnterReason(){ BootloaderInfo.Reason = 0; BootloaderInfo.Reason |= CheckFirmware(); BootloaderInfo.Reason |= CheckEEPROMFlag(); BootloaderInfo.Reason |= CheckADDR(); } void GetBootloaderInfo(volatile uint8_t *dst){ for (uint8_t i = IDISN_FIXP_MODULE_TYPE_Pos; i < IDISN_FIXP_MAC_Pos; i++){ *dst++ = flash_read_byte(STATIC_DATA_BL,i); } *dst++ = flash_read_byte(STATIC_DATA_BL,IDISN_FIXP_SW_REV_Pos); *dst++ = flash_read_byte(STATIC_DATA_BL,IDISN_FIXP_SW_REV_Pos+1); *dst++ = BootloaderInfo.Reason; *dst = AES_DISABLED; } uint16_t GetNextBlockSize(){ if (BootloaderInfo.Status < IDIFMW_STATUS_Busy){ if (BootloaderInfo.BlockNumber != -1) { return PAGE_SIZE; } else { return FMW_INFO_SIZE; } } return 0; } void GetBootloaderStatus(volatile uint8_t *dst){ *dst++ = BootloaderInfo.Status; uint16_t BlockSize = GetNextBlockSize(); *dst++ = (uint8_t)(BlockSize>>8); *dst++ = (uint8_t)BlockSize; *dst++ = (uint8_t)(BootloaderInfo.BlockNumber>>8); *dst = (uint8_t)BootloaderInfo.BlockNumber; }