uint32_t platform_get_wifi_image_size(void) { uint32_t FlashAddress = DRIVER_START_ADDRESS + DRIVER_FLASH_SIZE - 0x4; uint32_t imageTail; MicoFlashRead(MICO_FLASH_FOR_DRIVER, &FlashAddress, (uint8_t *)&imageTail, 4); while(imageTail == 0xFFFFFFFF) { image_size-= 4; FlashAddress -=8; MicoFlashRead(MICO_FLASH_FOR_DRIVER, &FlashAddress, (uint8_t *)&imageTail, 4); } return image_size; }
u32 MX_FirmwareUpdateFinish(u32 u32TotalLen) { #if 1 mico_logic_partition_t* part; int len; OSStatus err = kNoErr; uint32_t update_data_offset = 0x0; CRC16_Context crc_context; u32 total_len = u32TotalLen; CRC16_Init( &crc_context ); mico_logic_partition_t *para_partition_info; para_partition_info = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP); memset(&(context->flashContentInRam.bootTable), 0, sizeof(boot_table_t)); context->flashContentInRam.bootTable.length = u32TotalLen; context->flashContentInRam.bootTable.start_address = para_partition_info->partition_start_addr; context->flashContentInRam.bootTable.type = 'A'; context->flashContentInRam.bootTable.upgrade_type = 'U'; while(total_len > 0){ if( SizePerRW < total_len ){ len = SizePerRW; } else { len = total_len; } err = MicoFlashRead( MICO_PARTITION_OTA_TEMP, &update_data_offset, data , len); total_len -= len; CRC16_Update( &crc_context, data, len ); } CRC16_Final( &crc_context, &context->flashContentInRam.bootTable.crc ); MICOUpdateConfiguration(context); return ZC_RET_OK; #endif }
uint32_t platform_get_wifi_image_size(void) { #define READ_LEN 2048 mico_logic_partition_t *driver_partition = MicoFlashGetInfo( MICO_PARTITION_RF_FIRMWARE ); uint32_t offset = driver_partition->partition_length; uint32_t *p; uint32_t *buf = (uint32_t *)malloc(READ_LEN); uint32_t image_size = driver_partition->partition_length; do { offset -= READ_LEN; // Next block MicoFlashRead( MICO_PARTITION_RF_FIRMWARE, &offset, (uint8_t *)buf, READ_LEN); offset -= READ_LEN; // MicoFlashRead will increase FlashAddress READ_LEN, move back. p = buf + (READ_LEN - 4)/sizeof(uint32_t); while(p >= buf) { if (*p != 0xFFFFFFFF) { goto EXIT; } p--; image_size -= 4; } } while (offset > 0); EXIT: free(buf); return image_size; }
uint32_t platform_get_wifi_image_size(void) { #define READ_LEN 2048 uint32_t FlashAddress = DRIVER_START_ADDRESS + DRIVER_FLASH_SIZE; uint32_t imageTail; uint32_t *p; uint32_t *buf = (uint32_t *)malloc(READ_LEN); image_size = DRIVER_FLASH_SIZE; do { FlashAddress -= READ_LEN; // Next block MicoFlashRead(MICO_FLASH_FOR_DRIVER, &FlashAddress, (uint8_t *)buf, READ_LEN); FlashAddress -= READ_LEN; // MicoFlashRead will increase FlashAddress READ_LEN, move back. p = buf + (READ_LEN - 4)/sizeof(uint32_t); while(p >= buf) { if (*p != 0xFFFFFFFF) { goto EXIT; } p--; image_size -= 4; } } while (FlashAddress > DRIVER_START_ADDRESS); EXIT: free(buf); return image_size; }
void app_crc (char *str,int len) { mico_logic_partition_t *partition_flash; CRC16_Context mfg_context; uint8_t *mfgbuf; uint16_t crc = 0; uint32_t flash_addr = 0x0; int flash_len,buf_len; mfgbuf = malloc (1024); partition_flash = MicoFlashGetInfo (MICO_PARTITION_APPLICATION); flash_len = partition_flash->partition_length; CRC16_Init (&mfg_context); while (flash_len > 0) { if (flash_len > 1024) { buf_len = 1024; } else { buf_len = flash_len; } flash_len -= buf_len; MicoFlashRead (MICO_PARTITION_APPLICATION, &flash_addr, (uint8_t *)mfgbuf, buf_len); CRC16_Update (&mfg_context, (uint8_t *)mfgbuf, buf_len); } CRC16_Final (&mfg_context, &crc); snprintf (str, len, "%04X", crc); }
OSStatus MICOReadConfiguration(mico_Context_t *inContext) { uint32_t configInFlash; OSStatus err = kNoErr; configInFlash = PARA_START_ADDRESS; err = MicoFlashRead(MICO_FLASH_FOR_PARA, &configInFlash, (uint8_t *)&inContext->flashContentInRam, sizeof(flash_content_t)); seedNum = inContext->flashContentInRam.micoSystemConfig.seed; if(seedNum == -1) seedNum = 0; if(inContext->flashContentInRam.appConfig.configDataVer != CONFIGURATION_VERSION){ err = MICORestoreDefault(inContext); require_noerr(err, exit); MicoSystemReboot(); } if(inContext->flashContentInRam.micoSystemConfig.dhcpEnable == DHCP_Disable){ strcpy((char *)inContext->micoStatus.localIp, inContext->flashContentInRam.micoSystemConfig.localIp); strcpy((char *)inContext->micoStatus.netMask, inContext->flashContentInRam.micoSystemConfig.netMask); strcpy((char *)inContext->micoStatus.gateWay, inContext->flashContentInRam.micoSystemConfig.gateWay); strcpy((char *)inContext->micoStatus.dnsServer, inContext->flashContentInRam.micoSystemConfig.dnsServer); } exit: return err; }
/** * @brief Prepare the data packet * @param timeout * 0: end of transmission * @retval None */ void Ymodem_PreparePacket(mico_flash_t flash, uint32_t flashdestination, uint8_t *data, uint8_t pktNo, uint32_t sizeBlk) { uint16_t i, size, packetSize; /* Make first three packet */ packetSize = sizeBlk >= PACKET_1K_SIZE ? PACKET_1K_SIZE : PACKET_SIZE; size = sizeBlk < packetSize ? sizeBlk :packetSize; if (packetSize == PACKET_1K_SIZE) { data[0] = STX; } else { data[0] = SOH; } data[1] = pktNo; data[2] = (~pktNo); MicoFlashRead(flash, &flashdestination, data + PACKET_HEADER, size); if ( size <= packetSize) { for (i = size + PACKET_HEADER; i < packetSize + PACKET_HEADER; i++) { data[i] = 0x1A; /* EOF (0x1A) or 0x00 */ } } }
/************************************************* * Function: MX_ReadDataFormFlash * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void MX_ReadDataFormFlash(u8 *pu8Data, u16 u16Len) { uint32_t para_offset = PARA_OFFSET; OSStatus err = kNoErr; err = MicoFlashRead( MICO_PARTITION_PARAMETER_2,¶_offset, pu8Data, u16Len ); require_noerr(err, exit); exit: if(err != kNoErr) update_log("Update exit with err = %d", err); }
char *mico_get_bootloader_ver(void) { static char ver[33]; uint32_t flashaddr = BOOT_END_ADDRESS + 1 - 0x20; memset(ver, 0, sizeof(ver)); MicoFlashRead(MICO_FLASH_FOR_BOOT, &flashaddr, (uint8_t *)ver , 32); return ver; }
char *mico_get_bootloader_ver(void) { static char ver[33]; const mico_logic_partition_t* bootloader_partition = &mico_partitions[ MICO_PARTITION_BOOTLOADER ]; uint32_t version_offset = bootloader_partition->partition_length - 0x20; memset(ver, 0, sizeof(ver)); MicoFlashRead( MICO_PARTITION_BOOTLOADER, &version_offset, (uint8_t *)ver , 32); return ver; }
uint32_t platform_get_wifi_image(unsigned char* buffer, uint32_t size, uint32_t offset) { uint32_t buffer_size; uint32_t FlashAddress = DRIVER_START_ADDRESS; buffer_size = MIN(size, (image_size - offset)); FlashAddress += offset; MicoFlashRead(MICO_FLASH_FOR_DRIVER, &FlashAddress, buffer, buffer_size); return buffer_size; }
OSStatus HMReadPairList(pair_list_in_flash_t *pPairList) { uint32_t configInFlash; OSStatus err = kNoErr; require(pPairList, exit); configInFlash = EX_PARA_START_ADDRESS; err = MicoFlashRead(MICO_FLASH_FOR_EX_PARA, &configInFlash, (uint8_t *)pPairList, sizeof(pair_list_in_flash_t)); //memcpy(pPairList, (void *)configInFlash, sizeof(pair_list_in_flash_t)); exit: return err; }
uint32_t platform_get_wifi_image(unsigned char* buffer, uint32_t size, uint32_t offset) { uint32_t buffer_size; uint32_t read_address = offset; mico_logic_partition_t *driver_partition = MicoFlashGetInfo( MICO_PARTITION_RF_FIRMWARE ); if( image_size == 0) image_size = driver_partition->partition_length; buffer_size = MIN(size, (image_size - offset)); MicoFlashRead( MICO_PARTITION_RF_FIRMWARE, &read_address, buffer, buffer_size); return buffer_size; }
void mico_set_bootload_ver(void) { char ver[33]; uint32_t flashaddr = BOOT_END_ADDRESS + 1 - 0x20; int i; MicoFlashInitialize(MICO_FLASH_FOR_BOOT); memset(ver, 0, sizeof(ver)); MicoFlashRead(MICO_FLASH_FOR_BOOT, &flashaddr, (uint8_t *)ver , 32); for(i=0;i<32;i++) { if (ver[i] != 0xFF) return; } snprintf(ver, 33, "%s%s", MODEL, Bootloader_REVISION ); flashaddr = BOOT_END_ADDRESS + 1 - 0x20; MicoFlashWrite(MICO_FLASH_FOR_BOOT, &flashaddr, (uint8_t *)ver , 32); MicoFlashFinalize(MICO_FLASH_FOR_BOOT); }
void mico_set_bootload_ver(void) { char ver[33]; mico_logic_partition_t *boot_partition = MicoFlashGetInfo( MICO_PARTITION_BOOTLOADER ); uint32_t flashaddr = boot_partition->partition_length - 0x20; int i; memset(ver, 0, sizeof(ver)); MicoFlashRead( MICO_PARTITION_BOOTLOADER, &flashaddr, (uint8_t *)ver , 32); for(i=0;i<32;i++) { if (ver[i] != 0xFF) return; } snprintf(ver, 33, "%s %s %d", MODEL, Bootloader_REVISION , STDIO_UART_BAUDRATE); flashaddr = boot_partition->partition_length - 0x20; MicoFlashDisableSecurity( MICO_PARTITION_BOOTLOADER, 0x0, boot_partition->partition_length ); MicoFlashWrite( MICO_PARTITION_BOOTLOADER, &flashaddr, (uint8_t *)ver , 32); }
teZcbStatus ePDM_Init(mico_Context_t* mico_context) { OSStatus err; user_zigbeePDM_log("Create PDM lock"); mico_rtos_init_mutex(&sLock); mico_logic_partition_t *zigbeePDM_partition_info; //mico_logic_partition_t *p1_info; uint8_t read_test[100]= {0}; uint8_t i = 0; uint32_t dest_offset = 0; uint8_t data_write[6]= {0x06,0x05,0x04,0x03,0x02,0x01}; mico_rtos_lock_mutex(&sLock); #if 0 //init MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashInitialize(MICO_PARTITION_ZIGBEEPDM_TEMP); require_noerr(err, exit); // Get Info MICO_PARTITION_ZIGBEEPDM_TEMP zigbeePDM_partition_info = MicoFlashGetInfo(MICO_PARTITION_ZIGBEEPDM_TEMP); user_zigbeePDM_log("ZigBee PDM Partition info:start_addr:%x ,length:%x",zigbeePDM_partition_info->partition_start_addr,zigbeePDM_partition_info->partition_length); //Erase MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashErase( MICO_PARTITION_ZIGBEEPDM_TEMP, 0x0, zigbeePDM_partition_info->partition_length); require_noerr(err, exit); mico_thread_msleep(100); //sleep //MicoFlashWrite(mico_partition_t partition, volatile uint32_t * off_set, uint8_t * inBuffer, uint32_t inBufferLength); //Write MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashWrite(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, (uint8_t *)data_write, sizeof(data_write)); require_noerr(err, exit); #endif #if 0 mico_context -> user_config_data = (void*)data_write; mico_context -> user_config_data_size = 10; err = mico_system_context_update(mico_context); require_noerr(err, exit); mico_thread_msleep(1000); #endif #if 0 //Read dest_offset = 0; err = MicoFlashRead(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, read_test, 5); require_noerr(err, exit); #endif #if 0 err = MicoFlashErase( MICO_PARTITION_PARAMETER_1, 0x0, 60); require_noerr(err, exit); mico_thread_msleep(10); err = MicoFlashWrite( MICO_PARTITION_PARAMETER_1, &dest_offset, "aaaaa", 5); require_noerr(err, exit); p1_info = MicoFlashGetInfo(MICO_PARTITION_PARAMETER_1); err = MicoFlashRead(MICO_PARTITION_PARAMETER_1, &dest_offset, read_test, 60); require_noerr(err, exit); #endif #if 0 //Output for(i = 0; i<5; i++) { printf("0x%x ",read_test[i]); } printf("\r\n"); #endif //MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &context->offset, (uint8_t *)inData, inLen); //MicoFlashRead(MICO_PARTITION_OTA_TEMP, &flashaddr, (uint8_t *)md5_recv, 16); //err = MicoFlashDisableSecurity( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition_info->partition_length ); //if (sqlite3_open_v2(pcPDMFile, &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) != SQLITE_OK) //{ // daemon_log(LOG_ERR, "Error initialising PDM database (%s)", sqlite3_errmsg(pDb)); // return E_ZCB_ERROR; //} //user_zigbeePDM_log("PDM Database opened\n"); { //const char *pcTableDef = "CREATE TABLE IF NOT EXISTS pdm (id INTEGER, size INTEGER, numblocks INTEGER, block INTEGER, blocksize INTEGER, data BLOB, PRIMARY KEY (id,block))"; //char *pcErr; //user_zigbeePDM_log("Execute SQL: '%s'\n", pcTableDef); //if (sqlite3_exec(pDb, pcTableDef, NULL, NULL, &pcErr) != SQLITE_OK) //{ // mico_log("Error creating table (%s)", pcErr); //sqlite3_free(pcErr); //mico_rtos_unlock_mutex(&sLock); //return E_ZCB_ERROR; //} } //user_zigbeePDM_log("PDM Database initialised\n"); //eSL_AddListener(E_SL_MSG_PDM_AVAILABLE_REQUEST, PDM_HandleAvailableRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_LOAD_RECORD_REQUEST, PDM_HandleLoadRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_SAVE_RECORD_REQUEST, PDM_HandleSaveRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_DELETE_ALL_RECORDS_REQUEST,PDM_HandleDeleteAllRequest, NULL); mico_rtos_unlock_mutex(&sLock); return E_ZCB_OK; exit: mico_rtos_unlock_mutex(&sLock); return err; }
OSStatus update(void) { boot_table_t updateLog; uint32_t i, j, size; uint32_t updateStartAddress; uint32_t destStartAddress_tmp; uint32_t paraStartAddress; OSStatus err = kNoErr; MicoFlashInitialize( (mico_flash_t)MICO_FLASH_FOR_UPDATE ); memset(data, 0xFF, SizePerRW); memset(newData, 0xFF, SizePerRW); memset(paraSaveInRam, 0xFF, PARA_FLASH_SIZE); updateStartAddress = UPDATE_START_ADDRESS; paraStartAddress = PARA_START_ADDRESS; err = MicoFlashRead(MICO_FLASH_FOR_PARA, ¶StartAddress, (uint8_t *)&updateLog, sizeof(boot_table_t)); require_noerr(err, exit); /*Not a correct record*/ if(updateLogCheck(&updateLog) != Log_NeedUpdate){ size = UPDATE_FLASH_SIZE/SizePerRW; for(i = 0; i <= size; i++){ if( i==size ){ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , UPDATE_FLASH_SIZE%SizePerRW); require_noerr(err, exit); } else{ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , SizePerRW); require_noerr(err, exit); } for(j=0; j<SizePerRW; j++){ if(data[j] != 0xFF){ update_log("Update data need to be erased"); err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, exit); err = MicoFlashErase( MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS ); require_noerr(err, exit); err = MicoFlashFinalize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, exit); break; } } } goto exit; } update_log("Write OTA data to destination, type:%d, from 0x%08x to 0x%08x, length 0x%x", destFlashType, destStartAddress, destEndAddress, updateLog.length); destStartAddress_tmp = destStartAddress; updateStartAddress = UPDATE_START_ADDRESS; err = MicoFlashInitialize( destFlashType ); require_noerr(err, exit); err = MicoFlashErase( destFlashType, destStartAddress, destEndAddress ); require_noerr(err, exit); size = (updateLog.length)/SizePerRW; for(i = 0; i <= size; i++){ if( i==size && (updateLog.length)%SizePerRW){ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , (updateLog.length)%SizePerRW); require_noerr(err, exit); err = MicoFlashInitialize( destFlashType ); require_noerr(err, exit); err = MicoFlashWrite(destFlashType, &destStartAddress_tmp, data, (updateLog.length)%SizePerRW); require_noerr(err, exit); destStartAddress_tmp -= (updateLog.length)%SizePerRW; err = MicoFlashRead(destFlashType, &destStartAddress_tmp, newData , (updateLog.length)%SizePerRW); require_noerr(err, exit); err = memcmp(data, newData, (updateLog.length)%SizePerRW); require_noerr_action(err, exit, err = kWriteErr); } else{ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , SizePerRW); require_noerr(err, exit); err = MicoFlashInitialize( destFlashType ); require_noerr(err, exit); err = MicoFlashWrite(destFlashType, &destStartAddress_tmp, data, SizePerRW); require_noerr(err, exit); destStartAddress_tmp -= SizePerRW; err = MicoFlashRead(destFlashType, &destStartAddress_tmp, newData , SizePerRW); require_noerr(err, exit); err = memcmp(data, newData, SizePerRW); require_noerr_action(err, exit, err = kWriteErr); } } update_log("Update start to clear data..."); paraStartAddress = PARA_START_ADDRESS; err = MicoFlashRead(MICO_FLASH_FOR_PARA, ¶StartAddress, paraSaveInRam, PARA_FLASH_SIZE); require_noerr(err, exit); memset(paraSaveInRam, 0xff, sizeof(boot_table_t)); err = MicoFlashErase(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_END_ADDRESS); require_noerr(err, exit); paraStartAddress = PARA_START_ADDRESS; err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, paraSaveInRam, PARA_FLASH_SIZE); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS); require_noerr(err, exit); update_log("Update success"); exit: if(err != kNoErr) update_log("Update exit with err = %d", err); MicoFlashFinalize(MICO_FLASH_FOR_UPDATE); MicoFlashFinalize(destFlashType); return err; }
OSStatus fogCloudDevFirmwareUpdate(app_context_t* const inContext, MVDOTARequestData_t devOTARequestData) { cloud_if_log_trace(); OSStatus err = kUnknownErr; ecs_ota_flash_params_t ota_flash_params = { MICO_PARTITION_OTA_TEMP, 0x0, }; md5_context md5; unsigned char md5_16[16] = {0}; char *pmd5_32 = NULL; char rom_file_md5[32] = {0}; uint8_t data[SizePerRW] = {0}; uint32_t updateStartAddress = 0; uint32_t readLength = 0; uint32_t i = 0, size = 0; uint32_t romStringLen = 0; // crc16 CRC16_Context contex; cloud_if_log("fogCloudDevFirmwareUpdate: start ..."); //get latest rom version, file_path, md5 cloud_if_log("fogCloudDevFirmwareUpdate: get latest rom version from server ..."); err = FogCloudGetLatestRomVersion(&easyCloudContext); require_noerr_action( err, exit_with_error, cloud_if_log("ERROR: FogCloudGetLatestRomVersion failed! err=%d", err) ); //FW version compare cloud_if_log("currnt_version=%s", inContext->appConfig->fogcloudConfig.romVersion); cloud_if_log("latestRomVersion=%s", easyCloudContext.service_status.latestRomVersion); cloud_if_log("bin_file=%s", easyCloudContext.service_status.bin_file); cloud_if_log("bin_md5=%s", easyCloudContext.service_status.bin_md5); romStringLen = strlen(easyCloudContext.service_status.latestRomVersion) > strlen(inContext->appConfig->fogcloudConfig.romVersion) ? strlen(easyCloudContext.service_status.latestRomVersion):strlen(inContext->appConfig->fogcloudConfig.romVersion); if(0 == strncmp(inContext->appConfig->fogcloudConfig.romVersion, easyCloudContext.service_status.latestRomVersion, romStringLen)) { cloud_if_log("the current firmware version[%s] is up-to-date!", inContext->appConfig->fogcloudConfig.romVersion); inContext->appStatus.fogcloudStatus.RecvRomFileSize = 0; err = kNoErr; goto exit_with_no_error; } cloud_if_log("fogCloudDevFirmwareUpdate: new firmware[%s] found on server, downloading ...", easyCloudContext.service_status.latestRomVersion); inContext->appStatus.fogcloudStatus.isOTAInProgress = true; OTAWillStart(inContext); //get rom data err = FogCloudGetRomData(&easyCloudContext, ota_flash_params); require_noerr_action( err, exit_with_error, cloud_if_log("ERROR: FogCloudGetRomData failed! err=%d", err) ); //------------------------------ OTA DATA VERIFY ----------------------------- // md5 init InitMd5(&md5); CRC16_Init( &contex ); memset(rom_file_md5, 0, 32); memset(data, 0xFF, SizePerRW); updateStartAddress = ota_flash_params.update_offset; size = (easyCloudContext.service_status.bin_file_size)/SizePerRW; // read flash, md5 update for(i = 0; i <= size; i++) { if( i == size ) { if( (easyCloudContext.service_status.bin_file_size)%SizePerRW ) { readLength = (easyCloudContext.service_status.bin_file_size)%SizePerRW; } else { break; } } else { readLength = SizePerRW; } err = MicoFlashRead(ota_flash_params.update_partion, &updateStartAddress, data, readLength); require_noerr(err, exit_with_error); Md5Update(&md5, (uint8_t *)data, readLength); CRC16_Update( &contex, data, readLength ); } // read done, calc MD5 Md5Final(&md5, md5_16); CRC16_Final( &contex, &ota_crc ); pmd5_32 = ECS_DataToHexStringLowercase(md5_16, sizeof(md5_16)); //convert hex data to hex string cloud_if_log("ota_data_in_flash_md5[%d]=%s", strlen(pmd5_32), pmd5_32); if (NULL != pmd5_32) { strncpy(rom_file_md5, pmd5_32, strlen(pmd5_32)); free(pmd5_32); pmd5_32 = NULL; } else { err = kNoMemoryErr; goto exit_with_error; } // check md5 if(0 != strncmp( easyCloudContext.service_status.bin_md5, (char*)&(rom_file_md5[0]), strlen( easyCloudContext.service_status.bin_md5))) { cloud_if_log("ERROR: ota data wrote in flash md5 checksum err!!!"); err = kChecksumErr; goto exit_with_error; } else { cloud_if_log("OTA data in flash md5 check success, crc16=%d.", ota_crc); } //---------------------------------------------------------------------------- //update rom version in flash cloud_if_log("fogCloudDevFirmwareUpdate: return rom version && file size."); mico_rtos_lock_mutex(&inContext->mico_context->flashContentInRam_mutex); memset(inContext->appConfig->fogcloudConfig.romVersion, 0, MAX_SIZE_FW_VERSION); strncpy(inContext->appConfig->fogcloudConfig.romVersion, easyCloudContext.service_status.latestRomVersion, strlen(easyCloudContext.service_status.latestRomVersion)); inContext->appStatus.fogcloudStatus.RecvRomFileSize = easyCloudContext.service_status.bin_file_size; err = mico_system_context_update(inContext->mico_context); mico_rtos_unlock_mutex(&inContext->mico_context->flashContentInRam_mutex); OTASuccess(inContext); err = kNoErr; goto exit_with_no_error; exit_with_no_error: cloud_if_log("fogCloudDevFirmwareUpdate exit with no error."); inContext->appStatus.fogcloudStatus.isOTAInProgress = false; return err; exit_with_error: cloud_if_log("fogCloudDevFirmwareUpdate exit with err=%d.", err); OTAFailed(inContext); inContext->appStatus.fogcloudStatus.isOTAInProgress = false; return err; }
//------------------------------------------------------------ static s32_t lspiffs_read(u32_t addr, u32_t size, u8_t *dst) { MicoFlashRead(MICO_PARTITION_LUA, &addr,dst,size); return SPIFFS_OK; }
void init_platform_bootloader( void ) { OSStatus err = kNoErr; MicoGpioInitialize( (mico_gpio_t)MICO_SYS_LED, OUTPUT_PUSH_PULL ); MicoGpioOutputLow( (mico_gpio_t)MICO_SYS_LED ); MicoGpioInitialize( (mico_gpio_t)MICO_RF_LED, OUTPUT_OPEN_DRAIN_NO_PULL ); MicoGpioOutputHigh( (mico_gpio_t)MICO_RF_LED ); MicoGpioInitialize((mico_gpio_t)BOOT_SEL, INPUT_PULL_UP); MicoGpioInitialize((mico_gpio_t)MFG_SEL, INPUT_PULL_UP); /* Specific operations used in EMW3165 production */ #define NEED_RF_DRIVER_COPY_BASE ((uint32_t)0x08008000) #define TEMP_RF_DRIVER_BASE ((uint32_t)0x08040000) #define TEMP_RF_DRIVER_END ((uint32_t)0x0807FFFF) const uint8_t isDriverNeedCopy = *(uint8_t *)(NEED_RF_DRIVER_COPY_BASE); const uint32_t totalLength = ( DRIVER_FLASH_SIZE < 0x40000)? DRIVER_FLASH_SIZE:0x40000; const uint8_t crcResult = *(uint8_t *)(TEMP_RF_DRIVER_END); uint8_t targetCrcResult = 0; uint32_t copyLength; uint32_t destStartAddress_tmp = DRIVER_START_ADDRESS; uint32_t sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE; uint32_t i; if ( isDriverNeedCopy != 0x0 ) return; platform_log( "Bootloader start to copy RF driver..." ); /* Copy RF driver to SPI flash */ err = MicoFlashInitialize( (mico_flash_t)MICO_FLASH_FOR_DRIVER ); require_noerr(err, exit); err = MicoFlashInitialize( (mico_flash_t)MICO_INTERNAL_FLASH ); require_noerr(err, exit); err = MicoFlashErase( MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, DRIVER_END_ADDRESS ); require_noerr(err, exit); platform_log( "Time: %d", mico_get_time_no_os() ); for(i = 0; i <= totalLength/SizePerRW; i++){ if( i == totalLength/SizePerRW ){ if(totalLength%SizePerRW) copyLength = totalLength%SizePerRW; else break; }else{ copyLength = SizePerRW; } printf("."); err = MicoFlashRead( MICO_INTERNAL_FLASH, &sourceStartAddress_tmp, data , copyLength ); require_noerr( err, exit ); err = MicoFlashWrite( MICO_FLASH_FOR_DRIVER, &destStartAddress_tmp, data, copyLength); require_noerr(err, exit); } printf("\r\n"); /* Check CRC-8 check-sum */ platform_log( "Bootloader start to verify RF driver..." ); sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE; destStartAddress_tmp = DRIVER_START_ADDRESS; for(i = 0; i <= totalLength/SizePerRW; i++){ if( i == totalLength/SizePerRW ){ if(totalLength%SizePerRW) copyLength = totalLength%SizePerRW; else break; }else{ copyLength = SizePerRW; } printf("."); err = MicoFlashRead( MICO_FLASH_FOR_DRIVER, &destStartAddress_tmp, data, copyLength ); require_noerr( err, exit ); targetCrcResult = CRC8_Table(targetCrcResult, data, copyLength); } printf("\r\n"); //require_string( crcResult == targetCrcResult, exit, "Check-sum error" ); if( crcResult != targetCrcResult ){ platform_log("Check-sum error"); while(1); } /* Clear RF driver from temperary storage */ platform_log("Bootloader start to clear RF driver temporary storage..."); err = MicoFlashInitialize( (mico_flash_t)MICO_INTERNAL_FLASH ); require_noerr(err, exit); /* Clear copy tag */ err = MicoFlashErase(MICO_INTERNAL_FLASH, NEED_RF_DRIVER_COPY_BASE, NEED_RF_DRIVER_COPY_BASE); require_noerr(err, exit); exit: MicoFlashFinalize( MICO_INTERNAL_FLASH ); MicoFlashFinalize( MICO_FLASH_FOR_DRIVER ); }