Example #1
0
UINT_T FinalizeMRD(void)
{
	UINT_T Retval = NoError;
	
	// no valid MRD, fatal error
	if (pMRD_valid == NULL)
	{
		serial_outstr("No valid MRD\n");
		zimi_force_minisys(NOVALIDMRD);
		FatalError(NOVALIDMRD);
	}

	// both of the MRDs are OK, no problem
	if (pMRD_invalid == NULL)
	{
		serial_outstr("Primary/Backup MRD are both OK\n");

		// reload valid MRD
		Retval = ReadFlash(pMRD_valid->FlashEntryAddr, pMRD_valid->LoadAddr, pMRD_valid->ImageSize, BOOT_FLASH);
		if (Retval != NoError)
		{
			zimi_force_minisys(Retval);
			FatalError(Retval);
		}
		
		return NoError;
	}

	serial_outstr("Start to write valid MRD to corrupted MRD\n");
	
	// one MRD is OK, the other MRD has problem
	// we don't know which one is valid, so, we need to reload valid MRD
	Retval = ReadFlash(pMRD_valid->FlashEntryAddr, pMRD_valid->LoadAddr, pMRD_valid->ImageSize, BOOT_FLASH);
	if (Retval != NoError)
	{
		zimi_force_minisys(Retval);
		FatalError(Retval);
	}

	// and then erase invalid MRD
	Retval = EraseFlash(pMRD_invalid->FlashEntryAddr, pMRD_invalid->ImageSize, BOOT_FLASH);
	if (Retval != NoError)
	{
		zimi_force_minisys(Retval);
		FatalError(Retval);
	}

	// and then write valid MRD to flash
	Retval = WriteFlash(pMRD_invalid->FlashEntryAddr, pMRD_valid->LoadAddr, pMRD_invalid->ImageSize, BOOT_FLASH);
	if (Retval != NoError)
	{
		zimi_force_minisys(Retval);
		FatalError(Retval);
	}

	serial_outstr("Write valid MRD to corrupted MRD Done\n");

	return NoError;
}
Example #2
0
pIMAGE_INFO_3_4_0 LoadSilentImages(pTIM pTIM_h)
{
	pIMAGE_INFO_3_4_0 pImageInfo = NULL;
	pIMAGE_INFO_3_4_0 pNextImageToTransferTo = NULL;
	UINT_T Retval = NoError;
	UINT_T Image_Index = 0;
	UINT_T Silent_Images[] = { LWGDSPID, LWGSKYLARKID, RELIABLEID, LTGOSLOADERID, 0x0 };

	while (Silent_Images[Image_Index] != 0x0)
	{
		pImageInfo = FindImageInTIM(pTIM_h, Silent_Images[Image_Index]);
		if (pImageInfo != NULL)
		{
		#if MRD_CHECK
			if ((pImageInfo->ImageID != RELIABLEID) && (pImageInfo->ImageID  != LTGOSLOADERID))
			{
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
		        if (Retval != NoError)
				{
					FatalError(Retval);
				}
			}
			else
			{
				PlatformSetMRDAddr(pImageInfo->FlashEntryAddr, pImageInfo->ImageID);
				
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
				if (Retval != NoError)
				{
					FatalError(Retval);
				}

				CheckMRD(pImageInfo);
			}
		#else
			Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
	        if (Retval != NoError)
			{
				FatalError(Retval);
			}
		#endif
		}

		Image_Index++;
	}

	serial_outstr("Silent Reset Boot\n");
	pNextImageToTransferTo = FindImageInTIM(pTIM_h, OSLOADERID);

	#if MRD_CHECK
	FinalizeMRD();
	#endif

	// Return the OSLO image.
	return pNextImageToTransferTo;
}
Example #3
0
ERROR_CODE GetCodes(void)
{
    WriteFlash ( 0x0000, 0x90 );                /* send the auto select command to the flash */
    ReadFlash ( 0x0000, &uwManufactureCode );  /* now we can read the codes */
    uwManufactureCode &= 0x00FF;
    ReadFlash ( 0x0002, &uwDeviceCode );
    uwDeviceCode &= 0xFFFF;
    uwRCR_Default = 0xBFCF;                    /* hardwired to facilitate the test , it may be GetRCR( &RCR_Default ); */
    ResetFlash();                               /* we need to issue another command to get the part out of auto select mode so issue a reset which  just puts the device back in read mode */

    return NO_ERR;
}
Example #4
0
u8
CartTypeDetect (void)
{
  u8 type = 0xff;
  u16 Manuf, Device;

  WriteFlash (_CART_START, INTEL28F_RIC);       // Read Identifier codes from flash.
  // Works for intel 28F640J3A & Sharp LH28F320BJE.
  Manuf = ReadFlash (_CART_START);
  Device = ReadFlash (_CART_START + _MEM_INC);

  switch (Manuf)
    {
    case 0:                    // Hudson Cart
      type = 0xdc;
      break;
    case 0x2e:                 // Standard ROM
      type = (u8) Manuf;
      break;
    case 0x89:                 // Intel chips
      switch (Device)
        {
        case 0x16:             // i28F320J3A
        case 0x17:             // i28F640J3A
        case 0x18:             // i28F128J3A
          type = (u8) Device;
          break;
        default:
          // Check to see if this is a Visoly "Turbo" cart
          Device = ReadFlash (_CART_START + _MEM_INC + _MEM_INC);
          switch (Device)
            {
            case 0x16:         // 2 x i28F320J3A
            case 0x17:         // 2 x i28F640J3A
            case 0x18:         // 2 x i28F128J3A
              type = Device + 0x80;
              break;
            }
        }
      break;
    case 0xb0:                 // Sharp chips
      switch (Device)
        {
        case 0xe2:
          type = (u8) Device;
          break;
        }
      break;
    }
  WriteFlash (_CART_START, INTEL28F_READARRAY); // Set flash to normal read mode
  return type;
}
Example #5
0
ERROR_CODE PollToggleBit(unsigned long ulAddr)
{
	ERROR_CODE ErrorCode = NO_ERR;	// flag to indicate error
	unsigned short sVal1;
	unsigned short sVal2;

	// read flash 1 time
	ReadFlash( ulAddr, &sVal1 );

	while( ErrorCode == NO_ERR )
	{
		// read the value 2 times
		ReadFlash( ulAddr, &sVal1 );
		ReadFlash( ulAddr, &sVal2 );

		// XOR to see if any bits are different
		sVal1 ^= sVal2;

		// see if we are toggling
		if( !(sVal1 & 0x40) )
			break;

		// check error bit
		if( !(sVal2 & 0x20) )
			continue;
		else
		{
			// read the value 2 times
			ReadFlash( ulAddr, &sVal1 );
			ReadFlash( ulAddr, &sVal2 );

			// XOR to see if any bits are different
			sVal1 ^= sVal2;

			// see if we are toggling
			if( !(sVal1 & 0x40) )
				break;
			else
			{
				ErrorCode = POLL_TIMEOUT;
				ResetFlash(ulAddr);
			}
		}
	}

	// we can return
	return ErrorCode;
}
Example #6
0
ERROR_CODE GetRCR ( u16* uwRCR )
{
    WriteFlash( 0x0000, 0x90 );                /* send the auto select command to the flash */
    ReadFlash ( 0x000A, uwRCR );               /* now we can read the RCR */
    ResetFlash();

    return NO_ERR;
}
/**
 * main:
 **/
void
main(void)
{
	uint16_t runcode_start = 0xffff;

	/* stack overflow / underflow */
	if (STKPTRbits.STKFUL || STKPTRbits.STKUNF)
		CHugFatalError(CH_ERROR_OVERFLOW_STACK);

	/* the watchdog saved us from our doom */
	if (!RCONbits.NOT_TO)
		CHugFatalError(CH_ERROR_WATCHDOG);

	/*
	 * Boot into the flashed program if all of these are true:
	 *  1. we didn't do soft-reset
	 *  2. the flashed program exists
	 *  3. the flash success is 0x01
	 */
	ReadFlash(CH_EEPROM_ADDR_RUNCODE, 2,
		  (unsigned char *) &runcode_start);
	ReadFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, 1,
		  (unsigned char *) &flash_success);
	if (RCONbits.NOT_RI &&
	    runcode_start != 0xffff &&
	    flash_success == 0x01)
		CHugBootFlash();

	InitializeSystem();

	while(1) {

		/* clear watchdog */
		ClrWdt();

		/* check bus status and service USB interrupts */
		USBDeviceTasks();

		ProcessIO();
	}
}
Example #8
0
ERROR_CODE GetCodes(int *pnManCode, int *pnDevCode, unsigned long ulAddr)
{
    unsigned long ulFlashStartAddr;		//flash start address

	// get flash start address from absolute address
	// The ulAddr should ideally be pointing to the flash start
	// address. However we just verify it here again.
	ulFlashStartAddr = GetFlashStartAddress(ulAddr);

	// send the auto select command to the flash
	WriteFlash( ulFlashStartAddr + 0x0aaa, 0xaa );
	WriteFlash( ulFlashStartAddr + 0x0554, 0x55 );
	WriteFlash( ulFlashStartAddr + 0x0aaa, 0x90 );

	// now we can read the codes
	ReadFlash( ulFlashStartAddr + 0x0000,(unsigned short *)pnManCode );
	*pnManCode &= 0x00FF;

	ReadFlash( ulFlashStartAddr + 0x0002, (unsigned short *)pnDevCode );
	*pnDevCode &= 0x00FF;

	if( *pnDevCode == 0x5B )
	{
	    gNumSectors = 19;
	  	pFlashDesc = "S29AL008D(512 x 16)";
	}
	else
	{
	    gNumSectors = 11;
		pFlashDesc = "S29AL004D(256 x 16)";
	}

	// we need to issue another command to get the part out
	// of auto select mode so issue a reset which just puts
	// the device back in read mode
	ResetFlash(ulAddr);

	// ok
	return NO_ERR;
}
Example #9
0
/*------------------------------------------------------------
 * Function Name  : main
 * Description    : 程序入口
 * Input          : None
 * Output         : None
 * Return         : None
 *------------------------------------------------------------*/
int main( void )
{			
	/* 全局初始化 */
	All_Init();
		
	while ( 1 )
	{
		KeyValueInit();
		
		switch ( GetPage() )
		{
			case SYS_INIT:								//初始化系统
				ReadFlash();
				break;
			
			case MAIN_PAGE:								//主界面
				LoadMainPage();
				break;
				
			case TEST_PARAMETER_PAGE:					//试验参数
				LoadTestParameter();
				break;
				
			case TEST_REPORT_PAGE:						//试验报告
				LoadTestReport();
				break;

			case FORCE_CALIBRATION:						//校准界面
				LoadForceCalibration();
				break;
			
			case SYSTEM_SET:							//系统设置
				LoadSystemSet();
				break;
			
			case LINK_PAGE:								//联机
				LinkHandlerCore();
				break;
	
			case POS_ADJUST_PAGE:						//位置调整
				LoadPosAdjustPage(MAIN_PAGE_ADJ);
				break;
			
 			default:
				SetPage(MAIN_PAGE);
 				break;
 		}							
	}
}
Example #10
0
ERROR_CODE GetCodes(int *pnManCode, int *pnDevCode, unsigned long ulAddr)
{
 	// send the auto select command to the flash
	WriteFlash( ulAddr + 0x0AAA, 0xaa );
	WriteFlash( ulAddr + 0x0554, 0x55 );
	WriteFlash( ulAddr + 0x0AAA, 0x90 );

 	// now we can read the codes
	ReadFlash( ulAddr + 0x0400,(unsigned short *)pnManCode );
	*pnManCode &= 0x00FF;

	ReadFlash( ulAddr + 0x0402, (unsigned short *)pnDevCode );
	*pnDevCode &= 0xFFFF;

	// if it is the M29W320DB
	if( *pnDevCode == 0x22CB )
	{
	    gNumSectors = 67;
	  	pFlashDesc =	"STMicro. M29W320DB";
	}
	else if( *pnDevCode == 0x2257 )
	{
	    gNumSectors = 71;
		pFlashDesc =	"STMicro. M29W320EB";
	}
	else
		return PROCESS_COMMAND_ERR;

	// we need to issue another command to get the part out
	// of auto select mode so issue a reset which just puts
	// the device back in read mode
	ResetFlash(ulAddr);

	// ok
	return NO_ERR;
}
Example #11
0
void DumpFlash(tDevice dev, tDumpMode mode)
{
	int totalBlocks= WII_FLASH_SIZE/FLASH_BLOCKSIZE;	// Number of pages in NAND
	int totalSteps= totalBlocks/WII_MAX_BUFFER;
	FILE *pFile;
	int blocks_read=0;
	int steps= 0;
	
	// To test
	//totalSteps= 2;
	
	buffer = (u8*) memalign(0x40, WII_MAX_BUFFER*getDumpBlockSize(mode));
	if(buffer==NULL) {
		printf("ERROR: Unable to allocate enough memory for buffer. Unable to continue. Exiting...\n");
		pressAnyButton();
		exit(0);
	}

	initialise_fat(dev);
	pFile = fopen(getDeviceFile(dev, mode), "wb+");
	if (pFile==NULL) {
		printf("ERROR: fopen(\"%s\") failed. No FAT filesystem on the %s?\n", getDeviceFile(dev, mode), getDeviceName(dev));
		pressAnyButton();
		exit(0);
	} else {
		fclose(pFile);
		
		CleanScreen();
		for(steps=0; steps<totalSteps; steps++) {
			// Block set to read in this step starts on...
			blocks_read= steps*WII_MAX_BUFFER;
			if((steps % WII_SCREEN_LINES)==0) {CleanScreen(); printf("\nDumping NAND. Pass %d of %d.", (steps/WII_SCREEN_LINES)+1, WII_SCREEN_STEPS);}
			ReadFlash(blocks_read, mode);
			AppendToFile(dev, mode);
		}
	}
	printf("\n\nEnd of Flash Dump.");
	
	pressAnyButton();
	if(buffer!=NULL) free(buffer);	
}
Example #12
0
u32 adi_pdd_Read(	ADI_DEV_PDD_HANDLE 	PDDHandle,
 			  		ADI_DEV_BUFFER_TYPE BufferType,
 			  		ADI_DEV_BUFFER		*pBuffer)
{

	ADI_DEV_1D_BUFFER *pBuff1D;
	unsigned short    *pusValue;
	unsigned long	  *pulAbsoluteAddr;
	u32		Result;

	// cast our buffer to a 1D buffer
	pBuff1D = (ADI_DEV_1D_BUFFER*)pBuffer;

	// cast our data buffer
	pusValue = (unsigned short *)pBuff1D->Data;

	// cast our offset
	pulAbsoluteAddr = (unsigned long *)pBuff1D->pAdditionalInfo;

	Result = ReadFlash( *pulAbsoluteAddr, pusValue );

	return(Result);
}
Example #13
0
pIMAGE_INFO_3_4_0 LoadBackupImages(pTIM pTIM_h)
{
	pIMAGE_INFO_3_4_0 pImageInfo = NULL;
	pIMAGE_INFO_3_4_0 pNextImageToTransferTo = NULL;
	UINT_T Retval = NoError;
	UINT_T Image_Index = 0, decompressLength;
	UINT_T Backup_Images[] = { SBOOT_ID, LTGRELIABLEID,LTGRESERVED1ID,0x0 };

	
	#if ZIMI_PB05
	extern int isInMiniSys;
	isInMiniSys = 1;  //set flag
	LED_BAT_R_ON();
	LED_LTE_R_ON();
	LED_WIFI_R_ON();
	#endif
	
#if LWG_LTG_SUPPORT
	VUINT_T LWGLTGFlag = PlatformCheckLTGLWGFlag();
	pWTP_RESERVED_AREA_HEADER pWRAH = NULL;
	pLTG_LWG_Select pLL = NULL;
	UINT_T choice = 0;

	serial_outstr("LWG/LTG switch flag\n");
	serial_outnum(LWGLTGFlag);
	serial_outstr("\n");

	if ((LWGLTGFlag != PLATFORM_5MODE_LTG_VER) &&
		(LWGLTGFlag != PLATFORM_5MODE_LWG_VER) &&
		(LWGLTGFlag != PLATFORM_3MODE_LTG_VER) &&
		(LWGLTGFlag != PLATFORM_3MODE_LWG_VER))
	{
		pWRAH = FindPackageInReserved(&Retval, pTIM_h, LTGLWGSELECTID);
		if ((pWRAH == NULL) || (Retval != NoError))
		{
			PlatformSetLTGLWGFlag(PLATFORM_3MODE_LTG_VER);
			LWGLTGFlag = PLATFORM_3MODE_LTG_VER;
			serial_outstr("LTG/LWG select package not found, default to 3Mode LTG\n");
		}
		else
		{
			pLL = (pLTG_LWG_Select) pWRAH;
			choice	= pLL->Choice;
			switch (choice)
			{
				case 0: // 5Mdoe LTG
					PlatformSetLTGLWGFlag(PLATFORM_5MODE_LTG_VER);
					LWGLTGFlag = PLATFORM_5MODE_LTG_VER;
					serial_outstr("Select to 5Mode LTG\n");
					break;
					
				case 1: // 5Mode LWG
					PlatformSetLTGLWGFlag(PLATFORM_5MODE_LWG_VER);
					LWGLTGFlag = PLATFORM_5MODE_LWG_VER;
					serial_outstr("Select to 5Mode LWG\n");
					break;
					
				case 2: // 3Mode LTG
					PlatformSetLTGLWGFlag(PLATFORM_3MODE_LTG_VER);
					LWGLTGFlag = PLATFORM_3MODE_LTG_VER;
					serial_outstr("Select to 3Mode LTG\n");
					break;
					
				case 3: // 3Mode LWG
					PlatformSetLTGLWGFlag(PLATFORM_3MODE_LWG_VER);
					LWGLTGFlag = PLATFORM_3MODE_LWG_VER;
					serial_outstr("Select to 3Mode LWG\n");
					break;
					
				default:
					PlatformSetLTGLWGFlag(PLATFORM_3MODE_LTG_VER);
					LWGLTGFlag = PLATFORM_3MODE_LTG_VER;
					serial_outstr("Wrong package setting found, default to 3Mode LTG\n");
					break;
			}
		}
	}
#endif

#if DECOMPRESS_SUPPORT
	OBM_MPUCache_Init();
	MPUFlag = 1;
#endif

	while (Backup_Images[Image_Index] != 0x0)
	{
		pImageInfo = FindImageInTIM(pTIM_h, Backup_Images[Image_Index]);

if ((pImageInfo->ImageID != RELIABLEID) && 
					(pImageInfo->ImageID != LTGRELIABLEID) &&
					(pImageInfo->ImageID != LWGRESERVED1ID) &&
					(pImageInfo->ImageID != LTGRESERVED1ID))
{
		if (pImageInfo != NULL)
		{
			CacheInvalidateMemory(DECOMPRESS_TEMP_ADDR, DECOMPRESS_LENGTH);
			Retval = ReadFlash(pImageInfo->FlashEntryAddr, DECOMPRESS_TEMP_ADDR, pImageInfo->ImageSize, BOOT_FLASH);
			if (Retval != NoError)
			{
				FatalError(Retval);
			}

			decompressLength = DECOMPRESS_LENGTH;

			#if QPRESS
			Retval = quickLZ_decompress(DECOMPRESS_TEMP_ADDR, (UINT8_T *)pImageInfo->LoadAddr);
			#else
			Retval = LZMA_Decompress((UINT_T *)pImageInfo->LoadAddr, &decompressLength, (UINT_T *)DECOMPRESS_TEMP_ADDR, pImageInfo->ImageSize);
			#endif

			if (Retval != NoError)
			{
				FatalError(Retval);
			}
		}
}
else
{
	#if MRD_CHECK
				PlatformSetMRDAddr(pImageInfo->FlashEntryAddr, pImageInfo->ImageID);
				
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
				if (Retval != NoError)
				{
					FatalError(Retval);
				}

				CheckMRD(pImageInfo);
			
	#else
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
				if (Retval != NoError)
				{
					FatalError(Retval);
				}
	#endif
}
		Image_Index++;
	}

	serial_outstr("Backup Boot\n");
	pNextImageToTransferTo = FindImageInTIM(pTIM_h, SBOOT_ID);


#if MRD_CHECK
#if SBOOT
	if ((pMRD_valid == NULL) && (sboot_flag != 0xaa))
		FatalError(NOVALIDMRD);
#else
	if (pMRD_valid == NULL)
		FatalError(NOVALIDMRD);
#endif

	FinalizeMRD();
#endif

	

#if DECOMPRESS_SUPPORT
	OBM_Flush();
	OBM_MPUCache_Disable();
#endif

#if SPI_CODE
	Giga_Disable4BytesMode();
#endif

	// Return the OSLO image.
	return pNextImageToTransferTo;
}
/**
 * ProcessIO:
 **/
static void
ProcessIO(void)
{
	uint16_t address;
	uint16_t erase_length;
	uint8_t length;
	uint8_t checksum;
	uint8_t cmd;
	uint8_t rc = CH_ERROR_NONE;

	/* reset the LED state */
	led_counter--;
	if (led_counter == 0) {
		CHugSetLEDsInternal(led_color);
		led_color *= 2;
		if (led_color > CH_STATUS_LED_BLUE)
			led_color = CH_STATUS_LED_GREEN;
		led_counter = BOOTLOADER_FLASH_INTERVAL;
	}

	/* User Application USB tasks */
	if ((USBDeviceState < CONFIGURED_STATE) ||
	    (USBSuspendControl == 1))
		return;

	/* no data was received */
	if(HIDRxHandleBusy(USBOutHandle)) {
		if (idle_counter++ == 0xff &&
		    idle_command != 0x00)
			CHugDeviceIdle();
		return;
	}

	/* got data, reset idle counter */
	idle_counter = 0;

	/* clear for debugging */
	memset (TxBuffer, 0xff, sizeof (TxBuffer));

	cmd = RxBuffer[CH_BUFFER_INPUT_CMD];
	switch(cmd) {
	case CH_CMD_GET_HARDWARE_VERSION:
		TxBuffer[CH_BUFFER_OUTPUT_DATA] = PORTB & 0x0f;
		break;
	case CH_CMD_RESET:
		/* only reset when USB stack is not busy */
		idle_command = CH_CMD_RESET;
		break;
	case CH_CMD_GET_FIRMWARE_VERSION:
		memcpy (&TxBuffer[CH_BUFFER_OUTPUT_DATA],
			&FirmwareVersion,
			2 * 3);
		break;
	case CH_CMD_ERASE_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		memcpy (&address,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0],
			2);
		/* allow to erase any address but not the bootloader */
		if (address < CH_EEPROM_ADDR_RUNCODE) {
			rc = CH_ERROR_INVALID_ADDRESS;
			break;
		}
		memcpy (&erase_length,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+2],
			2);
		EraseFlash(address, address + erase_length);
		break;
	case CH_CMD_READ_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		/* allow to read any address */
		memcpy (&address,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0],
			2);
		length = RxBuffer[CH_BUFFER_INPUT_DATA+2];
		if (length > 60) {
			rc = CH_ERROR_INVALID_LENGTH;
			break;
		}
		ReadFlash(address, length,
			  (unsigned char *) &TxBuffer[CH_BUFFER_OUTPUT_DATA+1]);
		checksum = CHugCalculateChecksum (&TxBuffer[CH_BUFFER_OUTPUT_DATA+1],
						  length);
		TxBuffer[CH_BUFFER_OUTPUT_DATA+0] = checksum;
		break;
	case CH_CMD_WRITE_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		/* write to flash that's not the bootloader */
		memcpy (&address,
			(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+0],
			2);
		if (address < CH_EEPROM_ADDR_RUNCODE) {
			rc = CH_ERROR_INVALID_ADDRESS;
			break;
		}
		length = RxBuffer[CH_BUFFER_INPUT_DATA+2];
		if (length > CH_FLASH_TRANSFER_BLOCK_SIZE) {
			rc = CH_ERROR_INVALID_LENGTH;
			break;
		}
		checksum = CHugCalculateChecksum(&RxBuffer[CH_BUFFER_INPUT_DATA+4],
						 length);
		if (checksum != RxBuffer[CH_BUFFER_INPUT_DATA+3]) {
			rc = CH_ERROR_INVALID_CHECKSUM;
			break;
		}

		/* copy low 32 bytes into flash buffer, and only write
		 * in 64 byte chunks as this is a limitation of the
		 * hardware */
		if ((address & CH_FLASH_TRANSFER_BLOCK_SIZE) == 0) {
			memset (FlashBuffer,
				0xff,
				CH_FLASH_WRITE_BLOCK_SIZE);
			memcpy (FlashBuffer,
				(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4],
				length);
		} else {
			memcpy (FlashBuffer + CH_FLASH_TRANSFER_BLOCK_SIZE,
				(const void *) &RxBuffer[CH_BUFFER_INPUT_DATA+4],
				length);
			WriteBytesFlash(address - CH_FLASH_TRANSFER_BLOCK_SIZE,
					CH_FLASH_WRITE_BLOCK_SIZE,
					(unsigned char *) FlashBuffer);
		}
		break;
	case CH_CMD_BOOT_FLASH:
		/* are we lost or stolen */
		if (flash_success == 0xff) {
			rc = CH_ERROR_DEVICE_DEACTIVATED;
			break;
		}
		/* only boot when USB stack is not busy */
		idle_command = CH_CMD_BOOT_FLASH;
		break;
	case CH_CMD_SET_FLASH_SUCCESS:
		if (RxBuffer[CH_BUFFER_INPUT_DATA] != 0x00) {
			rc = CH_ERROR_INVALID_VALUE;
			break;
		}
		flash_success = RxBuffer[CH_BUFFER_INPUT_DATA];
		EraseFlash(CH_EEPROM_ADDR_FLASH_SUCCESS,
			   CH_EEPROM_ADDR_FLASH_SUCCESS + 1);
		WriteBytesFlash(CH_EEPROM_ADDR_FLASH_SUCCESS, 1,
				(unsigned char *) &RxBuffer[CH_BUFFER_INPUT_DATA]);
		break;
	case CH_CMD_SELF_TEST:
		rc = CHugSelfTest();
		break;
	default:
		rc = CH_ERROR_UNKNOWN_CMD_FOR_BOOTLOADER;
		break;
	}

	/* always send return code */
	if(!HIDTxHandleBusy(USBInHandle)) {
		TxBuffer[CH_BUFFER_OUTPUT_RETVAL] = rc;
		TxBuffer[CH_BUFFER_OUTPUT_CMD] = cmd;
		USBInHandle = HIDTxPacket(HID_EP,
					  (BYTE*)&TxBuffer[0],
					  CH_USB_HID_EP_SIZE);
	}

	/* re-arm the OUT endpoint for the next packet */
	USBOutHandle = HIDRxPacket(HID_EP,
				   (BYTE*)&RxBuffer,
				   CH_USB_HID_EP_SIZE);
}
Example #15
0
T DYNACALL ReadMem_area0(u32 addr)
{
	addr &= 0x01FFFFFF;//to get rid of non needed bits
	const u32 base=(addr>>16);
	//map 0x0000 to 0x01FF to Default handler
	//mirror 0x0200 to 0x03FF , from 0x0000 to 0x03FFF
	//map 0x0000 to 0x001F
	if (base<=0x001F)//	:MPX	System/Boot ROM
	{
		return ReadBios(addr,sz);
	}
	//map 0x0020 to 0x0021
	else if ((base>= 0x0020) && (base<= 0x0021)) // :Flash Memory
	{
		return ReadFlash(addr&0x1FFFF,sz);
	}
	//map 0x005F to 0x005F
	else if (likely(base==0x005F))
	{
		if ( /*&& (addr>= 0x00400000)*/ (addr<= 0x005F67FF)) // :Unassigned
		{
			EMUERROR2("Read from area0_32 not implemented [Unassigned], addr=%x",addr);
		}
		else if ((addr>= 0x005F7000) && (addr<= 0x005F70FF)) // GD-ROM
		{
			//EMUERROR3("Read from area0_32 not implemented [GD-ROM], addr=%x,size=%d",addr,sz);
	#if DC_PLATFORM == DC_PLATFORM_NAOMI
			return (T)ReadMem_naomi(addr,sz);
	#else
			return (T)ReadMem_gdrom(addr,sz);
	#endif
		}
		else if (likely((addr>= 0x005F6800) && (addr<=0x005F7CFF))) //	/*:PVR i/f Control Reg.*/ -> ALL SB registers now
		{
			//EMUERROR2("Read from area0_32 not implemented [PVR i/f Control Reg], addr=%x",addr);
			return (T)sb_ReadMem(addr,sz);
		}
		else if (likely((addr>= 0x005F8000) && (addr<=0x005F9FFF))) //	:TA / PVR Core Reg.
		{
			//EMUERROR2("Read from area0_32 not implemented [TA / PVR Core Reg], addr=%x",addr);
			if (sz != 4)
				// House of the Dead 2
				return 0;
			return (T)pvr_ReadReg(addr);
		}
	}
	//map 0x0060 to 0x0060
	else if ((base ==0x0060) /*&& (addr>= 0x00600000)*/ && (addr<= 0x006007FF)) //	:MODEM
	{
		return (T)libExtDevice_ReadMem_A0_006(addr,sz);
		//EMUERROR2("Read from area0_32 not implemented [MODEM], addr=%x",addr);
	}
	//map 0x0060 to 0x006F
	else if ((base >=0x0060) && (base <=0x006F) && (addr>= 0x00600800) && (addr<= 0x006FFFFF)) //	:G2 (Reserved)
	{
		EMUERROR2("Read from area0_32 not implemented [G2 (Reserved)], addr=%x",addr);
	}
	//map 0x0070 to 0x0070
	else if ((base ==0x0070) /*&& (addr>= 0x00700000)*/ && (addr<=0x00707FFF)) //	:AICA- Sound Cntr. Reg.
	{
		//EMUERROR2("Read from area0_32 not implemented [AICA- Sound Cntr. Reg], addr=%x",addr);
		return (T) ReadMem_aica_reg(addr,sz);//libAICA_ReadReg(addr,sz);
	}
	//map 0x0071 to 0x0071
	else if ((base ==0x0071) /*&& (addr>= 0x00710000)*/ && (addr<= 0x0071000B)) //	:AICA- RTC Cntr. Reg.
	{
		//EMUERROR2("Read from area0_32 not implemented [AICA- RTC Cntr. Reg], addr=%x",addr);
		return (T)ReadMem_aica_rtc(addr,sz);
	}
	//map 0x0080 to 0x00FF
	else if ((base >=0x0080) && (base <=0x00FF) /*&& (addr>= 0x00800000) && (addr<=0x00FFFFFF)*/) //	:AICA- Wave Memory
	{
		//EMUERROR2("Read from area0_32 not implemented [AICA- Wave Memory], addr=%x",addr);
		//return (T)libAICA_ReadMem_aica_ram(addr,sz);
		ReadMemArrRet(aica_ram.data,addr&ARAM_MASK,sz);
	}
	//map 0x0100 to 0x01FF
	else if ((base >=0x0100) && (base <=0x01FF) /*&& (addr>= 0x01000000) && (addr<= 0x01FFFFFF)*/) //	:Ext. Device
	{
	//	EMUERROR2("Read from area0_32 not implemented [Ext. Device], addr=%x",addr);
		return (T)libExtDevice_ReadMem_A0_010(addr,sz);
	}
	return 0;
}
Example #16
0
TInt RAddressedContainer::Add(TLinAddr aAddress, TAny* aObject)
	{
	__NK_ASSERT_DEBUG(aObject);
	__ASSERT_CRITICAL;
	__NK_ASSERT_DEBUG(CheckWriteLock());

#ifdef _DEBUG
	if(K::CheckForSimulatedAllocFail())
		{
		__KTRACE_OPT(KMMU,Kern::Printf("RAddressedContainer::Add returns simulated OOM %d",KErrNoMemory));
		return KErrNoMemory;
		}
#endif

	// find list insertion point...
	TUint i = FindIndex(aAddress);

	if(iCount<iMaxCount)
		{
		// insert new entry...
		ReadLock();

		// make room by shuffling entries up in the array KMaxEntriesInOneGo at a time...
		TEntry* entry = iList+i;
		TEntry* prev = iList+iCount;
		++iCount; // must do this before releasing read lock for the first time
		for(;;)
			{
			TEntry* next = prev-KMaxEntriesInOneGo;
			if(next<=entry)
				{
				// move the final remaining entries...
				wordmove(entry+1,entry,(TUintPtr)prev-(TUintPtr)entry);
				break;
				}
			wordmove(next+1,next,(TUintPtr)prev-(TUintPtr)next);
			prev = next;
			// flash the read lock to give readers a chance to look at the list...
			ReadFlash();
			// Note, readers may see a duplicate entry in the list at 'prev' but this
			// is OK as the Find functions will still work.
			}

		// copy in new entry...
		entry->iAddress = aAddress;
		entry->iObject = aObject;

		ReadUnlock();
		}
	else
		{
		// list memory needs expanding...
		TUint newMaxCount = CalculateGrow();

		// allocate new memory...
		TEntry* newList = (TEntry*)Kern::Alloc(sizeof(TEntry)*newMaxCount);
		if(!newList)
			return KErrNoMemory;
		#ifdef _DEBUG
			if(iList)
				K::Allocator->DebugFunction(RAllocator::ECopyDebugInfo, iList, newList);
		#endif
		iMaxCount = newMaxCount;

		// copy list to new memory, and insert new entry...
		wordmove(newList,iList,sizeof(TEntry)*i);
		TEntry* entry = newList+i;
		entry->iAddress = aAddress;
		entry->iObject = aObject;
		wordmove(entry+1,iList+i,sizeof(TEntry)*(iCount-i));

		// start using new list...
		TEntry* oldList = iList;
		ReadLock();
		iList = newList;
		++iCount;
		ReadUnlock();

		// free memory used for old list...
		Kern::Free(oldList);
		}

	return KErrNone;
	}
Example #17
0
TAny* RAddressedContainer::Remove(TLinAddr aAddress)
	{
	__ASSERT_CRITICAL;
	__NK_ASSERT_DEBUG(CheckWriteLock());

	// search for it...
	TUint i = FindIndex(aAddress);
	TEntry* entry = iList+i-1;

	if(!i || entry->iAddress!=aAddress)
		{
		// not found...
		return 0;
		}
	--i; // make 'i' the index of entry to remove

	// get object...
	TAny* result = entry->iObject;
	__NK_ASSERT_DEBUG(result);

	TUint newMaxCount = CalculateShrink(iCount-1);

	if(newMaxCount>=iMaxCount)
		{
remove_without_resize:
		// remove old entry...
		ReadLock();

		// shuffling entries down in the array KMaxEntriesInOneGo at a time...
		TEntry* prev = iList+i+1;
		TEntry* end = iList+iCount;
		for(;;)
			{
			TEntry* next = prev+KMaxEntriesInOneGo;
			if(next>=end)
				{
				// move the final remaining entries...
				wordmove(prev-1,prev,(TUintPtr)end-(TUintPtr)prev);
				break;
				}
			wordmove(prev-1,prev,(TUintPtr)next-(TUintPtr)prev);
			prev = next;
			// flash the read lock to give readers a chance to look at the list...
			ReadFlash();
			// Note, readers may see a duplicate entry at the end of the list but this
			// is OK as the Find functions will still work.
			}

		--iCount; // must do this after moving all the entries
		ReadUnlock();		
		}
	else
		{
		// list memory needs shrinking...

		// allocate new memory...
		TEntry* newList = 0;
		if(newMaxCount)
			{
			newList = (TEntry*)Kern::Alloc(sizeof(TEntry)*newMaxCount);
			if(!newList)
				goto remove_without_resize; // have no memory to shrink array
			#ifdef _DEBUG
				if(iList)
					K::Allocator->DebugFunction(RAllocator::ECopyDebugInfo, iList, newList);
			#endif
			}
		iMaxCount = newMaxCount;

		// copy list to new memory, deleting removed entry...
		wordmove(newList,iList,sizeof(TEntry)*i);
		wordmove(newList+i,iList+i+1,sizeof(TEntry)*(iCount-i-1));

		// start using new list...
		TEntry* oldList = iList;
		ReadLock();
		iList = newList;
		--iCount;
		ReadUnlock();

		// free memory used for old list...
		Kern::Free(oldList);
		}

	return result;
	}
Example #18
0
//////////////////////////////////////////////////////////////////////
// This function mainly loads ALL the images available in the TIM 
// (except TIM, DUALTIM, and OBM) and validates them. 
// It essentially returns the next image that we will transfer the 
// control to. 
//
// Inputs: Current TIM pointer, BootMode(SINGLE or DUAL)
// Outputs: Returns the next image that we will transfer the control to.
//////////////////////////////////////////////////////////////////////
pIMAGE_INFO_3_4_0 LoadAllImages( pTIM pTIM_h)
{
	UINT_T Retval = NoError;
    UINT_T ImageID = 0;									// Initialize it as an invalid image
	UINT_T ImageIndex, ImageSize, decompressLength, start, end, start_read, end_read;
	pIMAGE_INFO_3_4_0 pImageInfo = NULL;
	pIMAGE_INFO_3_4_0 pNextImageToTransferTo = NULL;	// Used for storing the next image we want to transfer control to
														// after loading all the available images.


	flash_test();
	
	#if ZIMI_PB05
	extern PowerUPType_t powerup;
	extern int isInNormalSys;
	isInNormalSys = 1;   //set Normal sys flag
	if(powerup != PowerUP_USB)
	{
		#if ZIMI_LAST_LED_MODE
		if(powerup == PowerUP_ONKEY)
		{
			zimi_blink_led();
		}
		else if(powerup == PowerUP_Reset)
		{
			LED_ALL_ON();
		}
		#else
		LED_WIFI_G_ON();
		LED_BAT_G_ON();
		LED_LTE_G_ON();
		#endif
	}
	else  //xyl USB mode, we light BAT led only according to VBAT
	{
		#if 0
		UINT16_T vbat_mv;


		GetBatInstantVolt(&vbat_mv,USB_NotConnect);
		UINT8_T m_temp = IIC_read(0x07) & 0xff;

		if(3500<vbat_mv && vbat_mv<4050)
		{
			 m_temp |= 0x20;
		 	IIC_write(0x07,m_temp);
		 	Delay_us(300*1000);
			//GetBatInstantVolt(&vbat_mv,USB_NotConnect);
			ReadBatVolt(&vbat_mv);
		 	m_temp &= ~0x20;
		 	IIC_write(0x07,m_temp);
		}
		else
		{
			//GetBatInstantVolt(&vbat_mv,USB_NotConnect);
			ReadBatVolt(&vbat_mv);
		}

		
		serial_outstr("zimi# last vbat\n");
		serial_outnum(vbat_mv);
		serial_outstr("\n");

		*(VUINT_T *)ZIMI_BATVOL_INIT_FLAG_ADDR = vbat_mv;

		if(vbat_mv<3600)
		{
			serial_outstr("zimi# bat led low\n");
			LED_BAT_R_ON();
		}
		else if(vbat_mv<3750)
		{
			serial_outstr("zimi# bat led middle\n");
			LED_BAT_R_ON();
			LED_BAT_G_ON();
		}
		else
		{
			serial_outstr("zimi# bat led high\n");
			LED_BAT_G_ON();
		}
		#endif
	}
	#endif


#if LWG_LTG_SUPPORT
	VUINT_T LWGLTGFlag = PlatformCheckLTGLWGFlag();
	pWTP_RESERVED_AREA_HEADER pWRAH = NULL;
	pLTG_LWG_Select pLL = NULL;
	UINT_T choice = 0;
#endif

#if DECOMPRESS_SUPPORT
	OBM_MPUCache_Init();
	MPUFlag = 1;
#endif

	// Depending on the version of the TIM, determine the size of each image in bytes. 
	// We will use this size to iterate through the TIM binary from image to image. 
	if (pTIM_h->pConsTIM->VersionBind.Version >= TIM_3_4_00)
		ImageSize = sizeof(IMAGE_INFO_3_4_0);
	else
		ImageSize = sizeof(IMAGE_INFO_3_2_0);

	// This is the very initial TIM image! Assumes that the TIM image is located at the top of the 
	// TIM file. Otherwise, we would skip images. 
	pImageInfo = pTIM_h->pImg;

#if LWG_LTG_SUPPORT
	serial_outstr("LWG/LTG switch flag\n");
	serial_outnum(LWGLTGFlag);
	serial_outstr("\n");

	if ((LWGLTGFlag != PLATFORM_5MODE_LTG_VER) &&
		(LWGLTGFlag != PLATFORM_5MODE_LWG_VER) &&
		(LWGLTGFlag != PLATFORM_3MODE_LTG_VER) &&
		(LWGLTGFlag != PLATFORM_3MODE_LWG_VER))
	{
		pWRAH = FindPackageInReserved(&Retval, pTIM_h, LTGLWGSELECTID);
		if ((pWRAH == NULL) || (Retval != NoError))
		{
			PlatformSetLTGLWGFlag(PLATFORM_3MODE_LTG_VER);
			LWGLTGFlag = PLATFORM_3MODE_LTG_VER;
			serial_outstr("LTG/LWG select package not found, default to 3Mode LTG\n");
		}
		else
		{
			pLL = (pLTG_LWG_Select) pWRAH;
			choice	= pLL->Choice;
			switch (choice)
			{
				case 0: // 5Mdoe LTG
					PlatformSetLTGLWGFlag(PLATFORM_5MODE_LTG_VER);
					LWGLTGFlag = PLATFORM_5MODE_LTG_VER;
					serial_outstr("Select to 5Mode LTG\n");
					break;
					
				case 1: // 5Mode LWG
					PlatformSetLTGLWGFlag(PLATFORM_5MODE_LWG_VER);
					LWGLTGFlag = PLATFORM_5MODE_LWG_VER;
					serial_outstr("Select to 5Mode LWG\n");
					break;
					
				case 2: // 3Mode LTG
					PlatformSetLTGLWGFlag(PLATFORM_3MODE_LTG_VER);
					LWGLTGFlag = PLATFORM_3MODE_LTG_VER;
					serial_outstr("Select to 3Mode LTG\n");
					break;
					
				case 3: // 3Mode LWG
					PlatformSetLTGLWGFlag(PLATFORM_3MODE_LWG_VER);
					LWGLTGFlag = PLATFORM_3MODE_LWG_VER;
					serial_outstr("Select to 3Mode LWG\n");
					break;
					
				default:
					PlatformSetLTGLWGFlag(PLATFORM_3MODE_LTG_VER);
					LWGLTGFlag = PLATFORM_3MODE_LTG_VER;
					serial_outstr("Wrong package setting found, default to 3Mode LTG\n");
					break;
			}
		}
	}
#endif

	// Read in the number of images from the TIM and iterate through each of these images. 
	// We load them and we validate them. 
	for( ImageIndex = 0; ImageIndex < pTIM_h->pConsTIM->NumImages; ImageIndex++ )
    {
		// We skip the TIM, DUALTIM, and OBM images. We load/validate all the others. 
    	if( (pImageInfo->ImageID != TIMIDENTIFIER) && 
            (pImageInfo->ImageID != OBMIDENTIFIER) &&  
            (pImageInfo->ImageID != OBM2IDENTIFIER) &&
			(pImageInfo->LoadAddr != 0xFFFFFFFF)
          )	
		{
		#if I2C
			//battery_process_step3();
		#endif

		#if BACKUP_IMAGE
		if (pImageInfo->ImageID == SBOOT_ID)
		{
			serial_outstr("skip to load backup CP\n");
			pImageInfo = (IMAGE_INFO_3_4_0*)((unsigned char*)pImageInfo + ImageSize);
			continue;
		}
		#endif

		#if SBOOT
		if ((sboot_flag == 0x55) && (pImageInfo->ImageID == SBOOT_ID))
		{
			serial_outstr("skip to load sboot\n");
			pImageInfo = (IMAGE_INFO_3_4_0*)((unsigned char*)pImageInfo + ImageSize);
			continue;
		}
		#else
		if (pImageInfo->ImageID == SBOOT_ID)
		{
			serial_outstr("skip to load sboot\n");
			pImageInfo = (IMAGE_INFO_3_4_0*)((unsigned char*)pImageInfo + ImageSize);
			continue;
		}
		#endif

		#if LWG_LTG_SUPPORT
		if ((LWGLTGFlag == PLATFORM_5MODE_LWG_VER) || 
			(LWGLTGFlag == PLATFORM_3MODE_LWG_VER))
		{
			if ( (pImageInfo->ImageID == LTGOSLOADERID) || 
	            (pImageInfo->ImageID == LTGRELIABLEID) ||  
	            (pImageInfo->ImageID == LTGDSPID) ||
				(pImageInfo->ImageID == LTGSKYLARKID) ||
				(pImageInfo->ImageID == LTGRESERVED1ID) ||
				(pImageInfo->ImageID == LTGRESERVED2ID) ||
				(pImageInfo->ImageID == LTGRESERVED3ID)
	          )
			{
				serial_outstr("Skip to load LTG image flash address\n");
				serial_outnum(pImageInfo->FlashEntryAddr);
				serial_outstr("\n");
				pImageInfo = (IMAGE_INFO_3_4_0*)((unsigned char*)pImageInfo + ImageSize);
				continue;
			}
		}
		else if ((LWGLTGFlag == PLATFORM_5MODE_LTG_VER) || 
				(LWGLTGFlag == PLATFORM_3MODE_LTG_VER))
		{
			if ( (pImageInfo->ImageID == OSLOADERID) || 
	            (pImageInfo->ImageID == RELIABLEID) ||  
	            (pImageInfo->ImageID == LWGDSPID) ||
				(pImageInfo->ImageID == LWGSKYLARKID) ||
				(pImageInfo->ImageID == LWGRESERVED1ID) ||
				(pImageInfo->ImageID == LWGRESERVED2ID) ||
				(pImageInfo->ImageID == LWGRESERVED3ID)
	          )
	        {
				serial_outstr("Skip to load LWG image flash address\n");
				serial_outnum(pImageInfo->FlashEntryAddr);
				serial_outstr("\n");
				pImageInfo = (IMAGE_INFO_3_4_0*)((unsigned char*)pImageInfo + ImageSize);
				continue;
			}
		}
		#endif
		
			// Store a pointer to the OSLO image because we will transfer control to it!		    
			// If this image is not found in the TIM, then we return NULL and 
			// we will fail in BootLoaderMain.
		#if LWG_LTG_SUPPORT
			if (((LWGLTGFlag == PLATFORM_5MODE_LTG_VER) || 
				(LWGLTGFlag == PLATFORM_3MODE_LTG_VER)) && 
				(pImageInfo->ImageID == LTGOSLOADERID))
			{
				#if USE_SERIAL_DEBUG
				serial_outstr("LTG uboot\n");
				#endif
			    pNextImageToTransferTo = pImageInfo;
			}

			if (((LWGLTGFlag == PLATFORM_5MODE_LWG_VER) || 
				(LWGLTGFlag == PLATFORM_3MODE_LWG_VER)) && 
				(pImageInfo->ImageID == OSLOADERID))
			{
				#if USE_SERIAL_DEBUG
				serial_outstr("LWG uboot\n");
				#endif
		    	pNextImageToTransferTo = pImageInfo;
			}
		#elif SBOOT
			if ((pImageInfo->ImageID == OSLOADERID) && (sboot_flag == 0x55))
			{
				#if USE_SERIAL_DEBUG
				serial_outstr("uboot\n");
				#endif
			    pNextImageToTransferTo = pImageInfo;
			}

			if ((sboot_flag == 0xaa) && (pImageInfo->ImageID == SBOOT_ID))
			{
				#if USE_SERIAL_DEBUG
				serial_outstr("Sboot\n");
				#endif
			    pNextImageToTransferTo = pImageInfo;
			}
		#else
			if (pImageInfo->ImageID == OSLOADERID)
			{
				#if USE_SERIAL_DEBUG
				serial_outstr("uboot\n");
				#endif
			    pNextImageToTransferTo = pImageInfo;
			}
		#endif

	        // We read images from the flash and load them into the LOAD ADDRESS specified in the TIM for each image. 
	        // For now, we do NOT care if there is a conflict when an image needs to load where we are currently running from.
			// We assume that this will not happen. 
	        #if MMC_CODE
			if((pTIM_h->pConsTIM->VersionBind.Version) >= TIM_3_2_00)
            {
				SetPartition(pImageInfo->PartitionNumber, BOOT_FLASH);
			}
			#endif

		#if DECOMPRESS_SUPPORT
			if ((pImageInfo->ImageID != RELIABLEID) && 
					(pImageInfo->ImageID != LTGRELIABLEID) &&
					(pImageInfo->ImageID != LWGRESERVED1ID) &&
					(pImageInfo->ImageID != LTGRESERVED1ID))
			{
			#if SBOOT
				if (pImageInfo->ImageID != SBOOT_ID)
			#endif
				{
					CacheInvalidateMemory(DECOMPRESS_TEMP_ADDR, DECOMPRESS_LENGTH);
					start_read= GetOSCR0();
					start_read = GetOSCR0();
			        Retval = ReadFlash(pImageInfo->FlashEntryAddr, DECOMPRESS_TEMP_ADDR, pImageInfo->ImageSize, BOOT_FLASH);
					end_read= GetOSCR0();
					serial_outstr("read time\n");
					if (end_read > start_read)
						serial_outnum(OSCR0IntervalInMilli(start_read, end_read));
					else
						serial_outnum(OSCR0IntervalInMilli(end_read, start_read));
					serial_outstr("\n");
					// If ReadFlash function fails, then we fail and return NULL. 
			        if (Retval != NoError)
					{
						zimi_force_minisys(Retval);
						FatalError(Retval);
					}

					decompressLength = DECOMPRESS_LENGTH;

					#if ZIMI_PB05
					#if ZIMI_LED_MODE
					UINT8_T onkey_long_flag =0;
					onkey_long_flag = UsticaBaseRead(USTICA_STATUS);
					onkey_long_flag = UsticaBaseRead(USTICA_STATUS);//xyl:re-read for bug,f**k marvell
					if ((onkey_long_flag & USTICA_ONKEY_STATUS) == USTICA_ONKEY_STATUS)  //when onkey pressed > 3s, we need to tell CP
					{
						serial_outstr("onkey long press detected\n");
						*(VUINT_T *)ZIMI_ONKEY_LONG_PRESS_FLAG_ADDR= 0x4C4F4E47;//LONG
						LED_WIFI_G_ON();
					}
					#endif
					#endif
					
					start = GetOSCR0();
					start = GetOSCR0();
					#if QPRESS
					Retval = quickLZ_decompress(DECOMPRESS_TEMP_ADDR, (UINT8_T *)pImageInfo->LoadAddr);
					#else
					Retval = LZMA_Decompress((UINT_T *)pImageInfo->LoadAddr, &decompressLength, (UINT_T *)DECOMPRESS_TEMP_ADDR, pImageInfo->ImageSize);
					#endif
					end = GetOSCR0();

					if (pImageInfo->ImageID == OSLOADERID)
						nextImageSize = decompressLength;
					
					serial_outstr("Retval\n");
					serial_outnum(Retval);
					serial_outstr("\n");
					serial_outstr("time\n");
					if (end > start)
						serial_outnum(OSCR0IntervalInMilli(start, end));
					else
						serial_outnum(OSCR0IntervalInMilli(end, start));
					serial_outstr("\n");

					//CacheInvalidateMemory(DECOMPRESS_TEMP_ADDR, DECOMPRESS_LENGTH);
					
					if (Retval != NoError)
					{
						zimi_force_minisys(Retval);
						FatalError(Retval);
					}
				}
			#if SBOOT
				else
				{
					Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
					if (Retval != NoError)
					{
						zimi_force_minisys(Retval);
						FatalError(Retval);
					}
				}
			#endif
			}
			else
			{
			#if MRD_CHECK
				PlatformSetMRDAddr(pImageInfo->FlashEntryAddr, pImageInfo->ImageID);
				
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
				if (Retval != NoError)
				{
					zimi_force_minisys(Retval);
					FatalError(Retval);
				}

				CheckMRD(pImageInfo);
			
			#else
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
				if (Retval != NoError)
				{
					zimi_force_minisys(Retval);
					FatalError(Retval);
				}
			#endif
			}
		#else
			#if MRD_CHECK
				if ((pImageInfo->ImageID != RELIABLEID) && 
					(pImageInfo->ImageID != LTGRELIABLEID) &&
					(pImageInfo->ImageID != LWGRESERVED1ID) &&
					(pImageInfo->ImageID != LTGRESERVED1ID))
				{
					//start = GetOSCR0();
					Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
					//end = GetOSCR0();
					//serial_outstr("read singal image\n");
					//if (end > start)
					//	serial_outnum(OSCR0IntervalInMilli(start, end));
					//else
					//	serial_outnum(OSCR0IntervalInMilli(end, start));
					//serial_outstr("\n");
					if (Retval != NoError)
					{
						zimi_force_minisys(Retval);
						FatalError(Retval);
					}
				}
				else 
				{
					PlatformSetMRDAddr(pImageInfo->FlashEntryAddr, pImageInfo->ImageID);
									
					Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
					if (Retval != NoError)
					{
						zimi_force_minisys(Retval);
						FatalError(Retval);
					}

					CheckMRD(pImageInfo);
				}
			#else
				Retval = ReadFlash(pImageInfo->FlashEntryAddr, pImageInfo->LoadAddr, pImageInfo->ImageSize, BOOT_FLASH);
				if (Retval != NoError)
				{
					zimi_force_minisys(Retval);
					FatalError(Retval);
				}
			#endif

		#endif
			
		}

		// Get a pointer to the next image we will load. 
		// For the last iteration (e.g. after the last image), we won't find an image.
		// However, this is OK because pImageInfo is not used again.   
		pImageInfo = (IMAGE_INFO_3_4_0*)((unsigned char*)pImageInfo + ImageSize);
    }

#if MRD_CHECK
#if SBOOT
	if ((pMRD_valid == NULL) && (sboot_flag != 0xaa))
	{
		zimi_force_minisys(NOVALIDMRD);
		FatalError(NOVALIDMRD);
	}
#else
	if (pMRD_valid == NULL)
	{
		zimi_force_minisys(NOVALIDMRD);
		FatalError(NOVALIDMRD);
	}
#endif

	FinalizeMRD();
#endif

#if DECOMPRESS_SUPPORT
	OBM_Flush();
	OBM_MPUCache_Disable();
#endif

#if SPI_CODE
	Giga_Disable4BytesMode();
#endif

   	return pNextImageToTransferTo;
}
ERROR_CODE GetCodes(int *pnManCode, int *pnDevCode, unsigned long ulAddr)
{

	unsigned long ulFlashStartAddr;		//flash start address

	// get flash start address from absolute address
	// The ulAddr should ideally be pointing to the flash start
	// address. However we just verify it here again.
	ulFlashStartAddr = GetFlashStartAddress(ulAddr);

 	// send the auto select command to the flash
//	WriteFlash( ulFlashStartAddr + 0x0AAA, 0xaa );
//	WriteFlash( ulFlashStartAddr + 0x0554, 0x55 );
//	WriteFlash( ulFlashStartAddr + 0x0AAA, 0x90 );

	WriteFlash( ulFlashStartAddr + 2*0x0555, 0xaa );
	WriteFlash( ulFlashStartAddr + 2*0x02aa, 0x55 );
	WriteFlash( ulFlashStartAddr + 2*0x0555, 0x90 );

 	// now we can read the codes
//	ReadFlash( ulFlashStartAddr + 0x0402, (unsigned short *)pnManCode );
	ReadFlash( ulFlashStartAddr + 2*0x0000, (unsigned short *)pnManCode );
	*pnManCode &= 0x00FF;

//	ReadFlash( ulFlashStartAddr + 0x0400, (unsigned short *)pnDevCode );
	ReadFlash( ulFlashStartAddr + 2*0x0001, (unsigned short *)pnDevCode );
	*pnDevCode &= 0xFFFF;

//	ReadFlash( 0x1ffffffc, (unsigned short *)pnDevCode );
//	*pnDevCode &= 0xFFFF;

	// we need to issue another command to get the part out
	// of auto select mode so issue a reset which just puts
	// the device back in read mode
	ResetFlash(ulAddr);
	ResetFlash(ulAddr);

	return NO_ERR;

	WriteFlash( ulFlashStartAddr + 2*0x0055, 0x98 );
	unsigned long int a=0, b=0, c=0, d=0;
//	ReadFlash( ulFlashStartAddr + 2*0x10, (unsigned short *) &a );
//	ReadFlash( ulFlashStartAddr + 2*0x11, (unsigned short *) &b );
//	ReadFlash( ulFlashStartAddr + 2*0x12, (unsigned short *) &c );
//	ReadFlash( ulFlashStartAddr + 2*0x13, (unsigned short *) &d );
//	ReadFlash( ulFlashStartAddr + 2*0x40, (unsigned short *) &a );
//	ReadFlash( ulFlashStartAddr + 2*0x41, (unsigned short *) &b );
//	ReadFlash( ulFlashStartAddr + 2*0x42, (unsigned short *) &c );
//	ReadFlash( ulFlashStartAddr + 2*0x43, (unsigned short *) &d );
//	ReadFlash( ulFlashStartAddr + 2*0x27, (unsigned short *) &a );
//	ReadFlash( ulFlashStartAddr + 2*0x28, (unsigned short *) &b );
//	ReadFlash( ulFlashStartAddr + 2*0x29, (unsigned short *) &c );
//	ReadFlash( ulFlashStartAddr + 2*0x4e, (unsigned short *) &d );
//	ReadFlash( ulFlashStartAddr + 2*0x61, (unsigned short *) &a );
//	ReadFlash( ulFlashStartAddr + 2*0x62, (unsigned short *) &b );
//	ReadFlash( ulFlashStartAddr + 2*0x63, (unsigned short *) &c );
//	ReadFlash( ulFlashStartAddr + 2*0x64, (unsigned short *) &d );
//	ReadFlash( ulFlashStartAddr + 2*0x2d, (unsigned short *) &a );
//	ReadFlash( ulFlashStartAddr + 2*0x2e, (unsigned short *) &b );
//	ReadFlash( ulFlashStartAddr + 2*0x2f, (unsigned short *) &c );
//	ReadFlash( ulFlashStartAddr + 2*0x30, (unsigned short *) &d );
	ReadFlash( ulFlashStartAddr + 2*0x31, (unsigned short *) &a );
//	ReadFlash( ulFlashStartAddr + 2*0x32, (unsigned short *) &b );
//	ReadFlash( ulFlashStartAddr + 2*0x33, (unsigned short *) &c );
//	ReadFlash( ulFlashStartAddr + 2*0x34, (unsigned short *) &d );
	a &= 0xFFFF; b &= 0xFFFF; c &= 0xFFFF; d &= 0xFFFF;
//	a &= 0xFF; b &= 0xFF; c &= 0xFF; d &= 0xFF;
//	*pnDevCode = a<<24 + b<<26 + c<<8 + d;
//	*pnDevCode = a*16777216 + b*65536 + c*256 + d;

//	*pnDevCode = a*65536 + b;
//	*pnDevCode = c*65536 + d;
//	*pnDevCode = d;
	*pnDevCode = a;
//	*pnDevCode &= 0xFFFF;

/*	while(1) {
		WriteFlash( ulFlashStartAddr + 2*0x0055, 0x98 );
		ReadFlash( ulFlashStartAddr + 2*0x34, (unsigned short *) &a );
		a &= 0xFFFF;
		ResetFlash(ulAddr);
		WriteFlash( ulFlashStartAddr + 2*0x34, a );
		ResetFlash(ulAddr);
	}
*/

	ResetFlash(ulAddr);
	ResetFlash(ulAddr);
	
	// ok
	return NO_ERR;
}
Example #20
0
void main(void)  
{
    // Initialize Pull Up/Down ressistors
    //Port_0_Data_SHADE = 0x80;                     // Enable distance input pull-up P0.7
    //PRT0DR = Port_0_Data_SHADE;
    Port_2_Data_SHADE = 0x04;                     // Enable pull-down resistor on LED P2.2
    PRT2DR = Port_2_Data_SHADE;
    Port_3_Data_SHADE = 0x20;                     // Enable pull-up on button bit P3.5 
    PRT3DR = Port_3_Data_SHADE;
    Port_4_Data_SHADE = 0x44;						// Enable pull-down LED P4.2, P4.6
    PRT4DR = Port_4_Data_SHADE;

    Timer8_WritePeriod(50);				// 12MHz/15/16/50 = 1KHz => 1ms main timer interrupt
    Timer8_WriteCompareValue(0);
    Timer8_EnableInt();
    Timer8_Start();
	PRS8_WritePolynomial(0x78);			// load the PRS polynomial
	PRS8_WriteSeed(0xFF);				// load the PRS seed
	PRS8_Start();						// start the PRS8
	RED_Start();
	GREEN_Start();
	BLUE_Start();
    PWM8_WritePeriod(100);				// set period to eight clocks
    PWM8_WritePulseWidth(0); 			// set pulse width to generate a % duty cycle 
    PWM8_EnableInt(); 					// ensure interrupt is enabled  
    PWM8_Start();						// start PWM 
    //DAC_CR &= ~0x80;	                // turn off SplitMUX bit 7 (P0[7] on right, others on left)    
    PGA_Start(PGA_HIGHPOWER);           // Start PGA
    ADCINC_Start(ADCINC_HIGHPOWER);     // Start ADC
    ADCINC_GetSamples(1);               // initiate the first sample
	
    M8C_EnableGInt;						// Global interrupt enable
  
	ReadFlash();						// read on/off times and LED dutycyle from FLASH
    if(!(RamFlashBlock.Dummy == 0x55))
    {
        SetFlashDefaults();             // clear flash first time
    } 

	LedPowerTog = 1;						// flag change				
	LedPower = RamFlashBlock.PowerState;
    RedDutyMax = RamFlashBlock.RedDuty;
    BlueDutyMax = RamFlashBlock.BlueDuty;
    GreenDutyMax = RamFlashBlock.GreenDuty;
	the_state = RamFlashBlock.the_state;		
	ledChangeRate = RamFlashBlock.ledChangeRate;
	
    Events.press = 0;
    Events.hold = 0;
    Events.release = 0;  	
    MenuFsm(&Events, &the_state);       // initlaize the state machine 
	
    USB_Start(0, USB_3V_OPERATION);     // Start USB
    //while (!USB_bGetConfiguration());   // Wait to be enumerated
    USB_INT_REG |= USB_INT_SOF_MASK;
    USB_EnableOutEP(1);                 // Post a buffer to wait for a command
			    
    while(1)                            // cycle the puck here
    {       
        MeasureTemperature();               // sample input temperature sensor voltage
        ThermalProtection();                 // decrease LED power if temperature rises above limit
		ButtonStates();                    // button driver
        CommunicateUSB();                       // USB driver
		LedStates();						// LED Cadence state machine
		DelayedSaveFlash();					// Save power state and RGB dutycycle 10 seconds after last button event
	}
}