Example #1
0
OSStatus MicoFlashDisableSecurity( mico_partition_t partition, uint32_t off_set, uint32_t size )
{
  OSStatus err = kNoErr;
  uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + off_set;
  uint32_t end_addr = mico_partitions[ partition ].partition_start_addr + off_set + size - 1;

  require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr );
  require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr );
  require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr );
  require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr );
  require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length, exit, err = kParamErr );

  if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false )
  {
    err =  MicoFlashInitialize( partition );
    require_noerr_quiet( err, exit );
  }

  mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex );
  err = platform_flash_disable_protect( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], start_addr, end_addr);
  mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex );
  
exit:
  return err;
}
Example #2
0
OSStatus MICOReadConfiguration(mico_Context_t *inContext)
{
  uint32_t configInFlash;
  OSStatus err = kNoErr;
  configInFlash = PARA_START_ADDRESS;
  err = MicoFlashInitialize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);
  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){
#ifdef MFG_MODE_AUTO
    err = MICORestoreMFG(inContext);
#else
    err = MICORestoreDefault(inContext);
#endif
    
    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;
}
Example #3
0
void lua_spiffs_mount() {
    spiffs_config cfg;    
    cfg.phys_size = LUA_FLASH_SIZE;
    cfg.phys_addr = LUA_START_ADDRESS; // start spiffs at start of spi flash
    cfg.phys_erase_block = 65536/2; // according to datasheet
    cfg.log_block_size = 65536; // let us not complicate things
    cfg.log_page_size = LOG_PAGE_SIZE; // as we said */
    
    cfg.hal_read_f = lspiffs_read;
    cfg.hal_write_f = lspiffs_write;
    cfg.hal_erase_f = lspiffs_erase;
    
    MicoFlashInitialize(MICO_FLASH_FOR_LUA);
    
    if(SPIFFS_mounted(&fs)) return;
    
    int res = SPIFFS_mount(&fs,
      &cfg,
      spiffs_work_buf,
      spiffs_fds,
      sizeof(spiffs_fds),
      spiffs_cache_buf,
      sizeof(spiffs_cache_buf),
      0);
}
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, &paraStartAddress, (void *)inContext, sizeof(flash_content_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);

exit:
  return err;
}
Example #5
0
OSStatus MicoFlashErase(mico_partition_t partition, uint32_t off_set, uint32_t size)
{
  OSStatus err = kNoErr;
  uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + off_set;
  uint32_t end_addr = mico_partitions[ partition ].partition_start_addr + off_set + size - 1;

  if (size == 0)
    goto exit;
  require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr );
  require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr );  

  require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr );
#ifndef BOOTLOADER
  require_action_quiet( ( mico_partitions[ partition ].partition_options & PAR_OPT_WRITE_MASK ) == PAR_OPT_WRITE_EN, exit, err = kPermissionErr );
#endif

  require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr );
  require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length, exit, err = kParamErr );

  if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false )
  {
    err =  MicoFlashInitialize( partition );
    require_noerr_quiet( err, exit );
  }

  mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex );
  err = platform_flash_erase( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], start_addr, end_addr );
  mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex );

exit:
  return err;
}
Example #6
0
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, &paraStartAddress, (void *)inContext, sizeof(flash_content_t));
  require_noerr(err, exit);
  err = MicoFlashFinalize(MICO_FLASH_FOR_PARA);
  require_noerr(err, exit);

exit:
  return err;
}
Example #7
0
OSStatus MicoFlashRead( mico_partition_t partition, volatile uint32_t* off_set, uint8_t* outBuffer ,uint32_t inBufferLength)
{
  OSStatus err = kNoErr;
  uint32_t start_addr = mico_partitions[ partition ].partition_start_addr + *off_set;
  uint32_t  end_addr = mico_partitions[ partition ].partition_start_addr + *off_set + inBufferLength - 1;

  if (inBufferLength == 0)
    goto exit;
  require_action_quiet( partition > MICO_PARTITION_ERROR, exit, err = kParamErr );
  require_action_quiet( partition < MICO_PARTITION_MAX, exit, err = kParamErr );
  
  require_action_quiet( mico_partitions[ partition ].partition_owner != MICO_FLASH_NONE, exit, err = kNotFoundErr );
#ifndef BOOTLOADER
  require_action_quiet( ( mico_partitions[ partition ].partition_options & PAR_OPT_READ_MASK ) == PAR_OPT_READ_EN, exit, err = kPermissionErr );
#endif

  require_action_quiet( start_addr >= mico_partitions[ partition ].partition_start_addr, exit, err = kParamErr );
  require_action_quiet( end_addr < mico_partitions[ partition ].partition_start_addr + mico_partitions[ partition ].partition_length , exit, err = kParamErr );

  if( platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].initialized == false )
  {
    err =  MicoFlashInitialize( partition );
    require_noerr_quiet( err, exit );
  }

  mico_rtos_lock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex );
  err = platform_flash_read( &platform_flash_peripherals[ mico_partitions[ partition ].partition_owner ], &start_addr, outBuffer, inBufferLength );
  *off_set = start_addr - mico_partitions[ partition ].partition_start_addr;
  mico_rtos_unlock_mutex( &platform_flash_drivers[ mico_partitions[ partition ].partition_owner ].flash_mutex );

exit:
  return err;
}
Example #8
0
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
}
Example #9
0
void init_platform_bootloader( void )
{
  MicoGpioInitialize( MICO_SYS_LED, OUTPUT_PUSH_PULL );
  MicoGpioOutputHigh( MICO_SYS_LED );
  MicoGpioInitialize( MICO_RF_LED, OUTPUT_OPEN_DRAIN_NO_PULL );
  MicoGpioOutputHigh( MICO_RF_LED );
  
  MicoGpioInitialize(BOOT_SEL, INPUT_PULL_UP);
  MicoGpioInitialize(MFG_SEL, INPUT_PULL_UP);
  
#if defined ( USE_MICO_SPI_FLASH )
  MicoFlashInitialize( MICO_SPI_FLASH );
#endif
}
Example #10
0
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;
}
OSStatus MicoFlashFinalize( mico_flash_t flash )
{
  OSStatus err = kNoErr;
  if( platform_flash_drivers[flash].initialized == false )
  {
    err = MicoFlashInitialize( flash );
    require_noerr( err, exit );
  }

  mico_rtos_lock_mutex( &platform_flash_drivers[flash].flash_mutex );
  err = platform_flash_deinit( &platform_flash_drivers[flash] );
  mico_rtos_unlock_mutex( &platform_flash_drivers[flash].flash_mutex );
  
exit:
  return err;
}
OSStatus MicoFlashRead(mico_flash_t flash, volatile uint32_t* FlashAddress, uint8_t* Data ,uint32_t DataLength)
{
  OSStatus err = kNoErr;
  
  if( platform_flash_drivers[flash].initialized == false )
  {
    err = MicoFlashInitialize( flash );
    require_noerr( err, exit );
  }
  mico_rtos_lock_mutex( &platform_flash_drivers[flash].flash_mutex );
  err = platform_flash_read( &platform_flash_drivers[flash], FlashAddress, Data, DataLength );
  mico_rtos_unlock_mutex( &platform_flash_drivers[flash].flash_mutex );
  
exit:
  return err;
}
Example #13
0
void init_platform( void )
{
  MicoGpioInitialize( MICO_SYS_LED, OUTPUT_PUSH_PULL );
  MicoGpioOutputHigh( MICO_SYS_LED );
  MicoGpioInitialize( MICO_RF_LED, OUTPUT_OPEN_DRAIN_NO_PULL );
  MicoGpioOutputHigh( MICO_RF_LED );
//  
  //  Initialise EasyLink buttons
  MicoGpioInitialize( EasyLink_BUTTON, INPUT_PULL_UP );
  mico_init_timer(&_button_EL_timer, RestoreDefault_TimeOut, _button_EL_Timeout_handler, NULL);
  MicoGpioEnableIRQ( EasyLink_BUTTON, IRQ_TRIGGER_FALLING_EDGE, _button_EL_irq_handler, NULL );

#if defined ( USE_MICO_SPI_FLASH )
  MicoFlashInitialize( MICO_SPI_FLASH );
#endif
}
OSStatus MicoFlashErase( mico_flash_t flash, uint32_t StartAddress, uint32_t EndAddress )
{
  OSStatus err = kNoErr;

  if( platform_flash_drivers[flash].initialized == false )
  {
    err =  MicoFlashInitialize( flash );
    require_noerr( err, exit );
  }
  mico_rtos_lock_mutex( &platform_flash_drivers[flash].flash_mutex );
  err = platform_flash_erase( &platform_flash_drivers[flash], StartAddress, EndAddress );
  mico_rtos_unlock_mutex( &platform_flash_drivers[flash].flash_mutex );

exit:
  
  return err;
}
Example #15
0
void init_platform( void )
{
  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);
  
  //  Initialise EasyLink buttons
  MicoGpioInitialize( (mico_gpio_t)EasyLink_BUTTON, INPUT_PULL_UP );
  mico_init_timer(&_button_EL_timer, RestoreDefault_TimeOut, _button_EL_Timeout_handler, NULL);
  MicoGpioEnableIRQ( (mico_gpio_t)EasyLink_BUTTON, IRQ_TRIGGER_BOTH_EDGES, _button_EL_irq_handler, NULL );

  MicoFlashInitialize( MICO_SPI_FLASH );
}
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);
}
Example #17
0
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, &paraStartAddress, (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;
}
Example #19
0
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;
}
Example #20
0
int SocketReadHTTPHeader( int inSock, HTTPHeader_t *inHeader )
{
  int        err =0;
  char *          buf;
  char *          dst;
  char *          lim;
  char *          end;
  size_t          len;
  ssize_t         n;
  const char *    value;
  size_t          valueSize;
  
  buf = inHeader->buf;
  dst = buf + inHeader->len;
  lim = buf + sizeof( inHeader->buf );
  for( ;; )
  {
    if(findHeader( inHeader,  &end ))
      break ;
    n = read( inSock, dst, (size_t)( lim - dst ) );
    if(      n  > 0 ) len = (size_t) n;
    else  { err = kConnectionErr; goto exit; }
    dst += len;
    inHeader->len += len;
  }
  
  inHeader->len = (size_t)( end - buf );
  err = HTTPHeaderParse( inHeader );
  require_noerr( err, exit );
  inHeader->extraDataLen = (size_t)( dst - end );
  if(inHeader->extraDataPtr) {
    free((uint8_t *)inHeader->extraDataPtr);
    inHeader->extraDataPtr = 0;
  }
  
  if(inHeader->otaDataPtr) {
    free((uint8_t *)inHeader->otaDataPtr);
    inHeader->otaDataPtr = 0;
  }
  
  /* For MXCHIP OTA function, store extra data to OTA data temporary */
  err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL );

  if(err == kNoErr && strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){
#ifdef MICO_FLASH_FOR_UPDATE  
    http_utils_log("Receive OTA data!");        
    err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE );
    require_noerr(err, exit);
    err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &flashStorageAddress, (uint8_t *)end, inHeader->extraDataLen);
    require_noerr(err, exit);
#else
    http_utils_log("OTA flash memory is not existed!");
    err = kUnsupportedErr;
#endif
    goto exit;
  }

  /* For chunked extra data without content length */
  if(inHeader->chunkedData == true){
    inHeader->chunkedDataBufferLen = (inHeader->extraDataLen > 256)? inHeader->extraDataLen:256;
    inHeader->chunkedDataBufferPtr = calloc(inHeader->chunkedDataBufferLen, sizeof(uint8_t)); //Make extra data buffer larger than chunk length
    require_action(inHeader->chunkedDataBufferPtr, exit, err = kNoMemoryErr);
    memcpy((uint8_t *)inHeader->chunkedDataBufferPtr, end, inHeader->extraDataLen);
    inHeader->extraDataPtr = inHeader->chunkedDataBufferPtr;
    return kNoErr;
  }

  /* Extra data with content length */
  if (inHeader->contentLength != 0){ //Content length >0, create a memory buffer (Content length) and store extra data
    size_t copyDataLen = (inHeader->contentLength >= inHeader->extraDataLen)? inHeader->contentLength:inHeader->extraDataLen;
    inHeader->extraDataPtr = calloc(copyDataLen , sizeof(uint8_t));
    require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr);
    memcpy((uint8_t *)inHeader->extraDataPtr, end, copyDataLen);
    err = kNoErr;
  } /* Extra data without content length, data is ended by conntection close */
  else if(inHeader->extraDataLen != 0){ //Content length =0, but extra data length >0, create a memory buffer (1500)and store extra data
    inHeader->dataEndedbyClose = true;
    inHeader->extraDataPtr = calloc(1500, sizeof(uint8_t));
    require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr);
    memcpy((uint8_t *)inHeader->extraDataPtr, end, inHeader->extraDataLen);
    err = kNoErr;
  }
  else
    return kNoErr;
  
exit:
  return err;
}
Example #21
0
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 );
}
Example #22
0
/**
  * @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;
}
Example #23
0
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, &paraStartAddress, (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, &paraStartAddress, 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, &paraStartAddress, 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;
}
Example #24
0
/**
  * @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;
	}
  }
}
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;
}