OSStatus MICORestoreMFG(mico_Context_t *inContext) { OSStatus err = kNoErr; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; /*wlan configration is not need to change to a default state, use easylink to do that*/ sprintf(inContext->flashContentInRam.micoSystemConfig.name, DEFAULT_NAME); inContext->flashContentInRam.micoSystemConfig.configured = mfgConfigured; /*Application's default configuration*/ appRestoreDefault_callback(inContext); err = MicoFlashInitialize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, (void *)inContext, sizeof(flash_content_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); exit: return err; }
OSStatus MICORestoreDefault(mico_Context_t *inContext) { OSStatus err = kNoErr; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; /*wlan configration is not need to change to a default state, use easylink to do that*/ sprintf(inContext->flashContentInRam.micoSystemConfig.name, DEFAULT_NAME); inContext->flashContentInRam.micoSystemConfig.configured = unConfigured; inContext->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO; inContext->flashContentInRam.micoSystemConfig.rfPowerSaveEnable = false; inContext->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable = false; inContext->flashContentInRam.micoSystemConfig.bonjourEnable = true; inContext->flashContentInRam.micoSystemConfig.configServerEnable = true; inContext->flashContentInRam.micoSystemConfig.seed = seedNum; /*Application's default configuration*/ appRestoreDefault_callback(inContext); err = MicoFlashInitialize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, (void *)inContext, sizeof(flash_content_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); exit: return err; }
static OSStatus onReceivedData(struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData, size_t inLen, void * inUserContext ) { OSStatus err = kUnknownErr; const char * value; size_t valueSize; configContext_t *context = (configContext_t *)inUserContext; err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL ); if(err == kNoErr && strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){ config_log("OTA data %d, %d to: %x", inPos, inLen, context->flashStorageAddress); #ifdef MICO_FLASH_FOR_UPDATE if(inPos == 0){ context->flashStorageAddress = UPDATE_START_ADDRESS; mico_rtos_lock_mutex(&Context->flashContentInRam_mutex); //We are write the Flash content, no other write is possiable context->isFlashLocked = true; err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, flashErrExit); err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS); require_noerr(err, flashErrExit); err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &context->flashStorageAddress, (uint8_t *)inData, inLen); require_noerr(err, flashErrExit); }else{ err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &context->flashStorageAddress, (uint8_t *)inData, inLen); require_noerr(err, flashErrExit); } #else config_log("OTA storage is not exist"); return kUnsupportedErr; #endif } else if(inHeader->chunkedData == true){ config_log("ChunkedData: %d, %d:", inPos, inLen); for(uint32_t i = 0; i<inLen; i++) printf("%c", inData[i]); printf("\r\n"); } else{ return kUnsupportedErr; } if(err!=kNoErr) config_log("onReceivedData"); return err; #ifdef MICO_FLASH_FOR_UPDATE flashErrExit: MicoFlashFinalize(MICO_FLASH_FOR_UPDATE); mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex); return err; #endif }
int application_start( void ) { #if MCU_POWERSAVE_ENABLED MicoMcuPowerSaveConfig(true); #endif power_log( "Power measure program: RTOS initialized and erase flash" ); MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); MicoFlashErase( MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS ); MicoFlashFinalize( MICO_FLASH_FOR_UPDATE ); mico_rtos_delete_thread( NULL ); return 0; }
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); }
OSStatus HMUpdatePairList(pair_list_in_flash_t *pPairList) { OSStatus err = kNoErr; uint32_t exParaStartAddress, exParaEndAddress; exParaStartAddress = EX_PARA_START_ADDRESS; exParaEndAddress = EX_PARA_END_ADDRESS; err = MicoFlashInitialize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_EX_PARA, exParaStartAddress, exParaEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_EX_PARA, &exParaStartAddress, (uint8_t *)pPairList, sizeof(pair_list_in_flash_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); exit: return err; }
OSStatus MICOUpdateConfiguration(mico_Context_t *inContext) { OSStatus err = kNoErr; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; inContext->flashContentInRam.micoSystemConfig.seed = ++seedNum; err = MicoFlashInitialize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, (uint8_t *)&inContext->flashContentInRam, sizeof(flash_content_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); exit: return err; }
OSStatus HMClearPairList(void) { OSStatus err = kNoErr; uint32_t exParaStartAddress, exParaEndAddress; exParaStartAddress = EX_PARA_START_ADDRESS; exParaEndAddress = EX_PARA_END_ADDRESS; pair_list_in_flash_t *pairList = NULL; pairList = calloc(1, sizeof(pair_list_in_flash_t)); require_action(pairList, exit, err = kNoMemoryErr); err = MicoFlashInitialize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_EX_PARA, exParaStartAddress, exParaEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_EX_PARA, &exParaStartAddress, (uint8_t *)pairList, sizeof(pair_list_in_flash_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); exit: if(pairList) free(pairList); return err; }
/** * @brief Display the Main Menu on HyperTerminal * @param None * @retval None */ void Main_Menu(void) { char cmdbuf [CMD_STRING_SIZE] = {0}, cmdname[15] = {0}; /* command input buffer */ int i, j; /* index for command buffer */ int targetFlash; char startAddressStr[10], endAddressStr[10]; int32_t startAddress, endAddress; bool inputFlashArea = false; while (1) { /* loop forever */ printf ("\n\rWiFiMCU> "); getline (&cmdbuf[0], sizeof (cmdbuf)); /* input command line */ for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for (; cmdbuf[i] != 0; i++) { /* convert to upper characters */ cmdbuf[i] = toupper(cmdbuf[i]); } for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for(j=0; cmdbuf[i] != ' '&&cmdbuf[i] != 0; i++,j++) { /* find command name */ cmdname[j] = cmdbuf[i]; } cmdname[j] = '\0'; /***************** Command "0" or "BOOTUPDATE": Update the application *************************/ if(strcmp(cmdname, "BOOTUPDATE") == 0 || strcmp(cmdname, "0") == 0) { if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead Bootloader only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_BOOT); SerialUpload(MICO_FLASH_FOR_BOOT, BOOT_START_ADDRESS, "BootLoaderImage.bin", BOOT_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_BOOT); continue; } printf ("\n\rUpdating Bootloader...\n\r"); SerialDownload(MICO_FLASH_FOR_BOOT, BOOT_START_ADDRESS, BOOT_FLASH_SIZE); } /***************** Command "1" or "FWUPDATE": Update the MICO application *************************/ else if(strcmp(cmdname, "FWUPDATE") == 0 || strcmp(cmdname, "1") == 0) { if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead MICO application only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_APPLICATION); SerialUpload(MICO_FLASH_FOR_APPLICATION, APPLICATION_START_ADDRESS, "ApplicationImage.bin", APPLICATION_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_APPLICATION); continue; } printf ("\n\rUpdating MICO application...\n\r"); SerialDownload(MICO_FLASH_FOR_APPLICATION, APPLICATION_START_ADDRESS, APPLICATION_FLASH_SIZE); } /***************** Command "2" or "DRIVERUPDATE": Update the RF driver *************************/ else if(strcmp(cmdname, "DRIVERUPDATE") == 0 || strcmp(cmdname, "2") == 0) { #ifdef MICO_FLASH_FOR_DRIVER if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead RF driver only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_DRIVER); SerialUpload(MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, "DriverImage.bin", DRIVER_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_DRIVER); continue; } printf ("\n\rUpdating RF driver...\n\r"); SerialDownload(MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, DRIVER_FLASH_SIZE); #else printf ("\n\rNo independ flash memory for RF driver, exiting...\n\r"); #endif } /***************** Command "3" or "PARAUPDATE": Update the application *************************/ else if(strcmp(cmdname, "PARAUPDATE") == 0 || strcmp(cmdname, "3") == 0) { if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing MICO settings only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_PARA); MicoFlashErase(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_END_ADDRESS); MicoFlashFinalize(MICO_FLASH_FOR_PARA); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead MICO settings only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_PARA); SerialUpload(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, "DriverImage.bin", PARA_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_PARA); continue; } printf ("\n\rUpdating MICO settings...\n\r"); SerialDownload(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_FLASH_SIZE); } /***************** Command "4" or "FLASHUPDATE": : Update the Flash *************************/ else if(strcmp(cmdname, "FLASHUPDATE") == 0 || strcmp(cmdname, "4") == 0) { if (findCommandPara(cmdbuf, "i", NULL, 0) != -1){ targetFlash = MICO_INTERNAL_FLASH; }else if(findCommandPara(cmdbuf, "s", NULL, 200) != -1){ targetFlash = MICO_SPI_FLASH; }else{ printf ("\n\rUnkown target type! Exiting...\n\r"); continue; } inputFlashArea = false; if (findCommandPara(cmdbuf, "start", startAddressStr, 10) != -1){ if(Str2Int((uint8_t *)startAddressStr, &startAddress)==0){ //Found Flash start address printf ("\n\rIllegal start address.\n\r"); continue; }else{ if (findCommandPara(cmdbuf, "end", endAddressStr, 10) != -1){ //Found Flash end address if(Str2Int((uint8_t *)endAddressStr, &endAddress)==0){ printf ("\n\rIllegal end address.\n\r"); continue; }else{ inputFlashArea = true; } }else{ printf ("\n\rFlash end address not found.\n\r"); continue; } } } if(endAddress<startAddress && inputFlashArea == true) { printf ("\n\rIllegal flash address.\n\r"); continue; } if(inputFlashArea != true){ if(targetFlash == MICO_INTERNAL_FLASH){ startAddress = platform_flash_peripherals[MICO_INTERNAL_FLASH].flash_start_addr ; endAddress = platform_flash_peripherals[MICO_INTERNAL_FLASH].flash_start_addr + platform_flash_peripherals[MICO_INTERNAL_FLASH].flash_length - 1; }else{ #ifdef USE_MICO_SPI_FLASH startAddress = platform_flash_peripherals[MICO_SPI_FLASH].flash_start_addr ; endAddress = platform_flash_peripherals[MICO_SPI_FLASH].flash_start_addr + platform_flash_peripherals[MICO_SPI_FLASH].flash_length - 1; #else printf ("\n\rSPI Flash not exist\n\r"); continue; #endif } } if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing flash content From 0x%x to 0x%x\n\r", startAddress, endAddress); MicoFlashInitialize((mico_flash_t)targetFlash); MicoFlashErase((mico_flash_t)targetFlash, startAddress, endAddress); MicoFlashFinalize((mico_flash_t)targetFlash); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead flash content From 0x%x to 0x%x\n\r", startAddress, endAddress); MicoFlashInitialize((mico_flash_t)targetFlash); SerialUpload((mico_flash_t)targetFlash, startAddress, "FlashImage.bin", endAddress-startAddress+1); MicoFlashFinalize((mico_flash_t)targetFlash); continue; } printf ("\n\rUpdating flash content From 0x%x to 0x%x\n\r", startAddress, endAddress); SerialDownload((mico_flash_t)targetFlash, startAddress, endAddress-startAddress+1); } /***************** Command: Reboot *************************/ else if(strcmp(cmdname, "MEMORYMAP") == 0 || strcmp(cmdname, "5") == 0) { #if defined MICO_FLASH_FOR_UPDATE && defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_UPDATE], UPDATE_START_ADDRESS, UPDATE_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_DRIVER], DRIVER_START_ADDRESS, DRIVER_END_ADDRESS); #endif #if !defined MICO_FLASH_FOR_UPDATE && defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_DRIVER], DRIVER_START_ADDRESS, DRIVER_END_ADDRESS); #endif #if defined MICO_FLASH_FOR_UPDATE && !defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS); #endif #if !defined MICO_FLASH_FOR_UPDATE && !defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS); #endif } /***************** Command: Excute the application *************************/ else if(strcmp(cmdname, "BOOT") == 0 || strcmp(cmdname, "6") == 0) { printf ("\n\rBooting.......\n\r"); startApplication(); } /***************** Command: Reboot *************************/ else if(strcmp(cmdname, "REBOOT") == 0 || strcmp(cmdname, "7") == 0) { printf ("\n\rReBooting.......\n\r"); MicoSystemReboot(); break; } else if(strcmp(cmdname, "HELP") == 0 || strcmp(cmdname, "?") == 0) { printf ( menu, MODEL, Bootloader_REVISION ); /* display command menu */ break; } else if(strcmp(cmdname, "") == 0 ) { break; } else{ printf (ERROR_STR, "UNKNOWN COMMAND"); break; } } }
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; }
/** * @brief Receive a file using the ymodem protocol. * @param buf: Address of the first byte. * @retval The size of the file. */ int32_t Ymodem_Receive (uint8_t *buf, mico_flash_t flash, uint32_t flashdestination, int32_t maxRecvSize) { uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr, *buf_ptr; int32_t i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0; uint32_t ramsource; MicoFlashInitialize(flash); for (session_done = 0, errors = 0, session_begin = 0; ;) { for (packets_received = 0, file_done = 0, buf_ptr = buf; ;) { switch (Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT)) { case 0: errors = 0; switch (packet_length) { /* Abort by sender */ case - 1: Send_Byte(ACK); MicoFlashFinalize(flash); return 0; /* End of transmission */ case 0: Send_Byte(ACK); file_done = 1; break; /* Normal packet */ default: if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff)) { Send_Byte(NAK); } else { if (packets_received == 0) { /* Filename packet */ if (packet_data[PACKET_HEADER] != 0) { /* Filename packet has valid data */ for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);) { FileName[i++] = *file_ptr++; } FileName[i++] = '\0'; for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);) { file_size[i++] = *file_ptr++; } file_size[i++] = '\0'; Str2Int(file_size, &size); /* Test the size of the image to be sent */ /* Image size is greater than Flash size */ if (size > (maxRecvSize + 1)) { /* End session */ Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return -1; } /* erase user application area */ MicoFlashErase(flash, flashdestination, flashdestination + maxRecvSize - 1); Send_Byte(ACK); Send_Byte(CRC16); } /* Filename packet is empty, end session */ else { Send_Byte(ACK); file_done = 1; session_done = 1; break; } } /* Data packet */ else { memcpy(buf_ptr, packet_data + PACKET_HEADER, packet_length); ramsource = (uint32_t)buf; /* Write received data in Flash */ if (MicoFlashWrite(flash, &flashdestination, (uint8_t*) ramsource, (uint32_t) packet_length) == 0) { Send_Byte(ACK); } else /* An error occurred while writing to Flash memory */ { /* End session */ Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return -2; } } packets_received ++; session_begin = 1; } } break; case 1: Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return -3; default: if (session_begin > 0) { errors ++; } if (errors > MAX_ERRORS) { Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return 0; } Send_Byte(CRC16); break; } if (file_done != 0) { break; } } if (session_done != 0) { break; } } MicoFlashFinalize(flash); return (int32_t)size; }
OSStatus SocketReadHTTPBody( int inSock, HTTPHeader_t *inHeader ) { OSStatus err = kParamErr; ssize_t readResult; int selectResult; fd_set readSet; const char * value; size_t valueSize; size_t lastChunkLen, chunckheaderLen; char *nextPackagePtr; #ifdef MICO_FLASH_FOR_UPDATE bool writeToFlash = false; #endif require( inHeader, exit ); err = kNotReadableErr; FD_ZERO( &readSet ); FD_SET( inSock, &readSet ); /* Chunked data, return after receive one chunk */ if( inHeader->chunkedData == true ){ /* Move next chunk to chunked data buffer header point */ lastChunkLen = inHeader->extraDataPtr - inHeader->chunkedDataBufferPtr + inHeader->contentLength; if(inHeader->contentLength) lastChunkLen+=2; //Last chunck data has a CRLF tail memmove( inHeader->chunkedDataBufferPtr, inHeader->chunkedDataBufferPtr + lastChunkLen, inHeader->chunkedDataBufferLen - lastChunkLen ); inHeader->extraDataLen -= lastChunkLen; while ( findChunkedDataLength( inHeader->chunkedDataBufferPtr, inHeader->extraDataLen, &inHeader->extraDataPtr ,"%llu", &inHeader->contentLength ) == false){ require_action(inHeader->extraDataLen < inHeader->chunkedDataBufferLen, exit, err=kMalformedErr ); selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); require( selectResult >= 1, exit ); readResult = read( inSock, inHeader->extraDataPtr, (size_t)( inHeader->chunkedDataBufferLen - inHeader->extraDataLen ) ); if( readResult > 0 ) inHeader->extraDataLen += readResult; else { err = kConnectionErr; goto exit; } } chunckheaderLen = inHeader->extraDataPtr - inHeader->chunkedDataBufferPtr; if(inHeader->contentLength == 0){ //This is the last chunk while( findCRLF( inHeader->extraDataPtr, inHeader->extraDataLen - chunckheaderLen, &nextPackagePtr ) == false){ //find CRLF selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); require( selectResult >= 1, exit ); readResult = read( inSock, (uint8_t *)( inHeader->extraDataPtr + inHeader->extraDataLen - chunckheaderLen ), 256 - inHeader->extraDataLen ); //Assume chunk trailer length is less than 256 (256 is the min chunk buffer, maybe dangerous if( readResult > 0 ) inHeader->extraDataLen += readResult; else { err = kConnectionErr; goto exit; } } err = kNoErr; goto exit; } else{ /* Extend chunked data buffer */ if( inHeader->chunkedDataBufferLen < inHeader->contentLength + chunckheaderLen + 2){ inHeader->chunkedDataBufferLen = inHeader->contentLength + chunckheaderLen + 256; inHeader->chunkedDataBufferPtr = realloc(inHeader->chunkedDataBufferPtr, inHeader->chunkedDataBufferLen); require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr); } /* Read chunked data */ while ( inHeader->extraDataLen < inHeader->contentLength + chunckheaderLen + 2 ){ selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); require( selectResult >= 1, exit ); readResult = read( inSock, (uint8_t *)( inHeader->extraDataPtr + inHeader->extraDataLen - chunckheaderLen), ( inHeader->contentLength - (inHeader->extraDataLen - chunckheaderLen) + 2 )); if( readResult > 0 ) inHeader->extraDataLen += readResult; else { err = kConnectionErr; goto exit; } } if( *(inHeader->extraDataPtr + inHeader->contentLength) != '\r' || *(inHeader->extraDataPtr + inHeader->contentLength +1 ) != '\n'){ err = kMalformedErr; goto exit; } } } /* We has extra data but total length is not clear, store them to 1500 bytes buffer return when connection is disconnected by remote server */ if( inHeader->dataEndedbyClose == true){ if(inHeader->contentLength == 0) { //First read body, return using data received by SocketReadHTTPHeader inHeader->contentLength = inHeader->extraDataLen; }else{ selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); require( selectResult >= 1, exit ); readResult = read( inSock, (uint8_t*)( inHeader->extraDataPtr ), 1500 ); if( readResult > 0 ) inHeader->contentLength = readResult; else { err = kConnectionErr; goto exit; } } err = kNoErr; goto exit; } /* We has extra data and we has a predefined buffer to store the total extra data return when all data has received*/ while ( inHeader->extraDataLen < inHeader->contentLength ) { selectResult = select( inSock + 1, &readSet, NULL, NULL, NULL ); require( selectResult >= 1, exit ); err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL ); require_noerr(err, exit); if( strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0 ){ #ifdef MICO_FLASH_FOR_UPDATE writeToFlash = true; inHeader->otaDataPtr = calloc(OTA_Data_Length_per_read, sizeof(uint8_t)); require_action(inHeader->otaDataPtr, exit, err = kNoMemoryErr); if((inHeader->contentLength - inHeader->extraDataLen)<OTA_Data_Length_per_read){ readResult = read( inSock, (uint8_t*)( inHeader->otaDataPtr ), ( inHeader->contentLength - inHeader->extraDataLen ) ); }else{ readResult = read( inSock, (uint8_t*)( inHeader->otaDataPtr ), OTA_Data_Length_per_read); } if( readResult > 0 ) inHeader->extraDataLen += readResult; else { err = kConnectionErr; goto exit; } err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &flashStorageAddress, (uint8_t *)inHeader->otaDataPtr, readResult); require_noerr(err, exit); free(inHeader->otaDataPtr); inHeader->otaDataPtr = 0; #else http_utils_log("OTA flash memory is not existed, !"); err = kUnsupportedErr; #endif }else{ readResult = read( inSock, (uint8_t*)( inHeader->extraDataPtr + inHeader->extraDataLen ), ( inHeader->contentLength - inHeader->extraDataLen ) ); if( readResult > 0 ) inHeader->extraDataLen += readResult; else { err = kConnectionErr; goto exit; } } } err = kNoErr; exit: if(err != kNoErr) inHeader->len = 0; if(inHeader->otaDataPtr) { free(inHeader->otaDataPtr); inHeader->otaDataPtr = 0; } #ifdef MICO_FLASH_FOR_UPDATE if(writeToFlash == true) MicoFlashFinalize(MICO_FLASH_FOR_UPDATE); #endif return err; }
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 ); }