⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tftpupgrade.c

📁 wi-fi手机原代码
💻 C
📖 第 1 页 / 共 4 页
字号:
		g_bTFTPXferInProcess = FALSE;
		buf_offset = 0;
		TFTPLOG("TFTP after dxXfer ipAddr:%s	tftpPort:%d config file:%s\n",ipAddr,tftpPort,g_TFTPLocationParams.pConfigFileName);
		/* the read function will be pended when there's no data in the socket, and 
		     read function will return 0 after socket has been closed */
		while ((num = read(DataFd, buf, TFTP_PACKET_BYTES)) > 0)
    		{
			hwFeedWDT();
			if (TRUE == g_bTFTPNeedCancel)
			{
				g_bTFTPNeedCancel = FALSE;
				TFTPLOG("TFTP process exit by user \n");
				free(buf64K);
				close(DataFd);

				return OK;
			}
			if (0 == buf_offset)
			{
				TFTP_Send_MMIMsg(TFTP_MMI_DOWNLOADING_CONFIG);
			}
			if( (buf_offset + num) >= TFTP_CONFIGFILE_MAX)
			{
				TFTPLOG("TFTP configuration file too big to receive on handset \n");
				free(buf64K);
				close(DataFd);

				return ERROR;
			}
			else
			{
				memcpy((buf64K + buf_offset), buf, num);		/* copy all the data to the temp buffer */
				buf_offset += num;
			}
    		}
		close(DataFd);
		TFTPLOG("TFTP the all data bytes:%d\n",buf_offset);
		
   		iErrNo = dxGetErrNo();
		if (iErrNo != DX_ERR_NONE)
    		{
			/* time out usally because of net has disconnected , or the 
			     current port is blocked by gateway */
			if (TFTP_ERR_PORT_BLOCK == iErrNo)
			{
				tftpPort = getNextTFTPPort(tftpPort);
				if (0 == tftpPort)
				{
					TFTPLOG("TFTP all port(69,21,2400) are be block at the current gateway\n");
					free(buf64K);
					TFTP_Send_MMIMsg(TFTP_MMI_FAIL_PORT_BLOCKED);
					break;
				}
				TFTP_Send_MMIMsg(TFTP_MMI_PORT_CHANGED);
				TFTPLOG("TFTP Current port maybe was blocked, I will try the next port \n");
				continue;
			}
			/* not found because of file not fould or access failed */
			if (TFTP_ERR_NOT_FOUND == iErrNo)
			{
				setCurTFTPPort(tftpPort);
				TFTPLOG("TFTP File not found\n");
			}
			/* timeout because phone can't get the seconad or next packet, error because other error */
			else if (TFTP_ERR_TRANSFER == iErrNo) 
			{
				TFTPLOG("TFTP Transfer file error\n");

			}
			else if (HTTP_ERR_PORT_BLOCK == iErrNo)
			{
				TFTPLOG("HTTP port block\n");
			}
			else if (HTTP_ERR_NOT_FOUND == iErrNo)
			{
				TFTPLOG("HTTP file not found\n");
			}
			else if (HTTP_ERR_TRANSFER == iErrNo)
			{
				TFTPLOG("HTTP Transfer file error\n");
			}
			else
			{
				TFTPLOG("Fail unknow reason\n");
			}
			free(buf64K);
			break;
    		}

		if (0 == g_TFTPLocationParams.cProto)
		{
			setCurTFTPPort(tftpPort);
		}
		if (ERROR == TCheckSum(buf64K,buf_offset))
		{
			TFTPLOG("TFTP File checksum error\n");
			if (loopIdx > 0)
			{
				break;
			}
			else
			{
				free(buf64K);

				return ERROR;
			}
		}
		printf("<TFTP> encryption key: %s\n",g_TFTPLocationParams.pEncreptionKey);
		
		plainbuf = (unsigned char *)malloc(buf_offset+1);	/* allocate file length memory */
		if (NULL == plainbuf)
		{
			TFTPLOG("TFTP malloc plainbuf error\n");
			if (loopIdx > 0)
			{
				break;
			}
			else
			{
				free(buf64K);

				return ERROR;
			}
		}
		plain_len = Ext_AES_cbc_decrypt(buf64K,buf_offset,plainbuf,
											g_TFTPLocationParams.pEncreptionKey);

		if (TftpTxtIsValid(plainbuf,plain_len) == FALSE)
		{
			TFTPLOG("TFTP decryption error %x %x %x %x \n",*plainbuf,*(plainbuf+1),*(plainbuf+2),*(plainbuf+3));
			free(plainbuf);
			if (loopIdx > 0)
			{
				free(buf64K);
				break;
			}
			else
			{
				if (OK == TftpDefaultValueReset())
				{
				#if 0
					if (OK == TGetCurLoctParams())
						continue;
				#endif
				}
				//TFTPLOG("TFTP Default reset fail!!!\n");
				return ERROR;
			}
		}
		plainbuf[plain_len] = '\0';
		TFTP_Send_MMIMsg(TFTP_MMI_PARSING_CONFIG);
		if (ERROR == TftpTxtParse(plainbuf,g_TFTPBinLoct,&iBinDestLen))
		{
			TFTPLOG("TFTP Parse error %x %x %x %x \n",*plainbuf,*(plainbuf+1),*(plainbuf+2),*(plainbuf+3));
			free(buf64K);
			free(plainbuf);
			break;
		}
		free(plainbuf);
		if (TRUE == g_bTFTPNeedCancel)
		{
			g_bTFTPNeedCancel = FALSE;
			TFTPLOG("TFTP process exit by user \n");
			free(buf64K);

			return OK;
		}

		TFTPLOG("TFTP restore current config location params which will be shown at user-agent\n");
		if (ERROR ==TRestoreConfigLoct())
		{
			TFTPLOG("TRestoreConfigLoct TFTPConfigSet fail\n");
			free(buf64K);
			break;
		}
		TFTPLOG("TFTP before active config params\n");
		if (ERROR == TActiveConfigParams((unsigned char *)g_TFTPBinLoct,iBinDestLen))
		{
			TFTPLOG("TFTP There's no parameters changed.\n");
			free(buf64K);
			break;
		}
		bCfgChanged = TRUE;
		if (TRUE == g_bTFTPNeedCancel)
		{
			g_bTFTPNeedCancel = FALSE;
			TFTPLOG("TFTP process exit by user \n");
			free(buf64K);

			return OK;
		}
		TFTPLOG("TFTP atfer active config params\n");
		if (TRUE == TConfigLoctChanged())
		{
			loopIdx ++;
			if (loopIdx >= 8)
			{
				TFTPLOG("TFTP upgrade task enter a loop,because it has downloaded 8 config files \n");
				free(buf64K);
				taskDelay(500);
				TGetCurLoctParams();
				return ERROR;
			}
			TFTP_Send_MMIMsg(TFTP_MMI_NEW_FILE_CONFIG);
			if (protocol_config_get((void *) stg, LOCT_Provisioning_ConfigServerFQDN))
			{
				TFTPLOG("protocol_config_get error for LOCT_TFTP_ServerFQDN \n");
				free(buf64K);
				TGetCurLoctParams();
				break;
			}
			TGetSvrPort(stg, stgb);
			if (strcmp(g_TFTPLocationParams.pConfigSrvFQDN,stgb) != 0)
			{
				if (ERROR == TFTPResolv(stgb,ipAddr))
				{
					TFTPLOG("TFTPResolv error : %s  in download new config file\n",stgb);
					free(buf64K);
					TGetCurLoctParams();
					break;
				}
				if (TRUE == g_bTFTPNeedCancel)
				{
					g_bTFTPNeedCancel = FALSE;
					TFTPLOG("TFTP process exit by user \n");
					free(buf64K);
					TGetCurLoctParams();
					return OK;
				}
			}
			TGetCurLoctParams();
			continue;
		}
		TGetCurLoctParams();
		free(buf64K);
		break;
	}
	/* For check whether  firmware's version has changed, if yes, we must download 
	     the new firmware, if no, we also  should check whether downloaded config fils 
	     has changed the phone's parameter, if yes, we should reboot the phone, if no, 
	     we should return do nothing */
	if (TRUE == TFirmwareIsChanged())
	{
		if (ERROR == TFTPFirmwareUpgradeProc())
		{
			TFTPLOG("TFTP TFTPFirmwareUpgradeProc error\n");
			if (TRUE == bCfgChanged)
				reboot(0);

			return ERROR;
		}
		if (TRUE == bCfgChanged)
			reboot(0);

		return OK;
	}
	if (TRUE == bCfgChanged)
		reboot(0);

	return  OK;

}

STATUS TFTPConfigUpgradeProc(BOOLEAN *pConfigChanged)
{
	char stg[128+1];
	char stgb[128+1];
	char ipAddr[INET_ADDR_LEN+1];
	int	tftpPort;
	int	DataFd;
	int	num;
	unsigned char	buf[TFTP_PACKET_BYTES+2];
	int	buf_offset;
  	unsigned char	*buf64K;
	unsigned char	*plainbuf;
	int 	iBinDestLen;
	char loopIdx=0;
	int 	plain_len;
	int 	iErrNo;

	*pConfigChanged = FALSE;
	if (ERROR == TFTPResolv(g_TFTPLocationParams.pConfigSrvFQDN,ipAddr))
	{
		TFTPLOG("TFTPResolv error : %s\n",g_TFTPLocationParams.pConfigSrvFQDN);
		return ERROR;
	}
	TFTPLOG("TFTPResolv Success! ip:%s\n",ipAddr);
	tftpPort = getCurTFTPPort();
	buf64K = (unsigned char *)malloc(TFTP_CONFIGFILE_MAX);	/* allocate 64KB for one sector of data */
	if(buf64K == NULL) 
	{
		TFTPLOG("TFTP malloc 64k error\n");
		return ERROR;
	}
	/* Following while(1) loop is for downloading the configuration file, 
	     if some error happens, program will break from this loop, then check the firmware */
	while (1)
	{
		g_bTFTPXferInProcess = TRUE;

		TFTPWriteLog(TFTP_LOG_CONFIGFILE);
		/* The following function dxXfer will spawn a task which will read the data from 
		     UDP socket then write the data to DataFd  */
		if (g_TFTPLocationParams.cProto == 1)
		{
			tftpPort = g_TFTPLocationParams.cHttpPort;
		}
		if (dxXfer(g_TFTPLocationParams.pConfigSrvFQDN, 
				ipAddr, 
				 tftpPort,
				 g_TFTPLocationParams.pConfigFileName, 
				 "GET",
                 			"binary", 
				 &DataFd, 
				 g_TFTPLocationParams.cProto) == ERROR)
    		{
        			TFTPLOG("TFTP transfer initiation failed.\n");
			free(buf64K);
			return ERROR;
    		}
		g_bTFTPXferInProcess = FALSE;
		buf_offset = 0;
		TFTPLOG("TFTP after dxXfer ipAddr:%s tftpPort:%d config file:%s\n",ipAddr,tftpPort,g_TFTPLocationParams.pConfigFileName);
		/* the read function will be pended when there's no data in the socket, and 
		     read function will return 0 after socket has been closed */
		while ((num = read(DataFd, buf, TFTP_PACKET_BYTES)) > 0)
    		{
    			Exit_Sleep(SLEEP_TFTP);
			hwFeedWDT();
			if( (buf_offset + num) >= TFTP_CONFIGFILE_MAX)
			{
				TFTPLOG("TFTP configuration file too big to receive on handset \n");
				free(buf64K);
				close(DataFd);
				return ERROR;
			}
			else
			{
				memcpy((buf64K + buf_offset), buf, num);		/* copy all the data to the temp buffer */
				buf_offset += num;
			}
    		}
		close(DataFd);
		TFTPLOG("TFTP the all data bytes:%d\n",buf_offset);
		iErrNo = dxGetErrNo();
		if (iErrNo != DX_ERR_NONE)
    		{
			/* time out usally because of net has disconnected , or the 
			     current port is blocked by gateway */
			if (TFTP_ERR_PORT_BLOCK == iErrNo)
			{
				tftpPort = getNextTFTPPort(tftpPort);
				if (0 == tftpPort)
				{
					TFTPLOG("TFTP all port(69,21,2400) are be block at the current gateway\n");
					free(buf64K);
					return ERROR;
				}
				TFTPLOG("TFTP Current port maybe was blocked, I will try the next port \n");
				continue;
			}
			/* not found because of file not fould or access failed */
			if (TFTP_ERR_NOT_FOUND == iErrNo)
			{
				setCurTFTPPort(tftpPort);
				TFTPLOG("TFTP File not found\n");
			}
			/* timeout because phone can't get the seconad or next packet, error because other error */
			else if (TFTP_ERR_TRANSFER == iErrNo)
			{
				TFTPLOG("TFTP Transfer file error\n");
			}
			else if (HTTP_ERR_PORT_BLOCK == iErrNo)
			{
				TFTPLOG("HTTP port block\n");
			}
			else if (HTTP_ERR_NOT_FOUND == iErrNo)
			{
				TFTPLOG("HTTP file not found\n");
			}
			else if (HTTP_ERR_TRANSFER == iErrNo)
			{
				TFTPLOG("HTTP Transfer file error\n");
			}
			else
			{
				TFTPLOG("Fail unknow reason\n");
			}
			free(buf64K);
			return ERROR;
    		}

		if (0 == g_TFTPLocationParams.cProto)
		{
			setCurTFTPPort(tftpPort);
		}
		
		if (ERROR == TCheckSum(buf64K,buf_offset))
		{
			TFTPLOG("TFTP File checksum error\n");
			free(buf64K);
			return ERROR;
		}
		printf("<TFTP> encryption key: %s\n",g_TFTPLocationParams.pEncreptionKey);
		
		plainbuf = (unsigned char *)malloc(buf_offset+1);	/* allocate file length memory */
		if (NULL == plainbuf)
		{
			TFTPLOG("TFTP malloc plainbuf error\n");
			free(buf64K);
			return ERROR;
		}
		plain_len = Ext_AES_cbc_decrypt(buf64K,buf_offset,plainbuf,
											g_TFTPLocationParams.pEncreptionKey);

		if (TftpTxtIsValid(plainbuf,plain_len) == FALSE)
		{
			TFTPLOG("TFTP decryption error %x %x %x %x \n",*plainbuf,*(plainbuf+1),*(plainbuf+2),*(plainbuf+3));
			free(plainbuf);
			if (loopIdx > 0)
			{
				free(buf64K);
				return ERROR;
			}
			else
			{
				if (OK == TftpDefaultValueReset())
				{
				#if 0
					if (OK == TGetCurLoctParams())
						continue;
				#endif
				}
				//TFTPLOG("TFTP Default reset fail!!!\n");
				return ERROR;
			}
		}
		plainbuf[plain_len] = '\0';
		TFTP_Send_MMIMsg(TFTP_MMI_PARSING_CONFIG);
		if (ERROR == TftpTxtParse(plainbuf,g_TFTPBinLoct,&iBinDestLen))
		{
			TFTPLOG("TFTP Parse error: %s\n",plainbuf);
			free(buf64K);
			free(plainbuf);
			return ERROR;
		}
		free(plainbuf);

		TFTPLOG("TFTP restore current config location params which will be shown at user-agent\n");
		if (ERROR ==TRestoreConfigLoct())
		{
			TFTPLOG("TRestoreConfigLoct TFTPConfigSet fail\n");
			free(buf64K);
			return ERROR;
		}
		TFTPLOG("TFTP before active config params\n");
		if (ERROR == TActiveConfigParams((unsigned char *)g_TFTPBinLoct,iBinDestLen))
		{
			TFTPLOG("TFTP There's no parameters changed.\n");
			free(buf64K);
			return ERROR;
		}
		*pConfigChanged = TRUE;
		if (TRUE == TFirmwareIsChangedNow())
		{
			TFTPLOG("TFTP firmware's version changed\n");
			TftpCleanRetryTimer();
		}
		TFTPLOG("TFTP atfer active config params\n");
		if (TRUE == TConfigLoctChanged())
		{
			loopIdx ++;
			if (loopIdx >= 8)
			{
				TFTPLOG("TFTP upgrade task enter a loop,because it has downloaded 8 config files \n");
				free(buf64K);
				taskDelay(500);
				TGetCurLoctParams();
				return ERROR;
			}
			TFTP_Send_MMIMsg(TFTP_MMI_NEW_FILE_CONFIG);
			if (protocol_config_get((void *) stg, LOCT_Provisioning_ConfigServerFQDN))
			{
				TFTPLOG("protocol_config_get error for LOCT_TFTP_ServerFQDN \n");
				free(buf64K);
				TGetCurLoctParams();
				return ERROR;
			}
			TGetSvrPort(stg, stgb);
			if (strcmp(g_TFTPLocationParams.pConfigSrvFQDN,stgb) != 0)
			{
				if (ERROR == TFTPResolv(stgb,ipAddr))
				{
					TFTPLOG("TFTPResolv error : %s  in download new config file\n",stgb);
					free(buf64K);
					TGetCurLoctParams();
					return ERROR;
				}
			}
			TGetCurLoctParams();
			continue;
		}
		TGetCurLoctParams();
		free(buf64K);
		return OK;
	}

	return  OK;

}

static VOID TPreHandleFileName(char * pFileName)
{
// fomart sring
}

STATUS TFTPFirmwareUpgradeProc()
{
	char FirmwareFileName[160];
	char ipAddr[INET_ADDR_LEN+1];
	STATUS st;

	TFTPLOG("DX firmware upgrade start\n");

	strcpy(FirmwareFileName,g_TFTPLocationParams.pFirmwareFilePath);
	strcat(FirmwareFileName,g_TFTPLocationParams.pFirmwareFileName);
	TPreHandleFileName(FirmwareFileName);
	if (ERROR == TFTPResolv(g_TFTPLocationParams.pFirmwareSrvFQDN,ipAddr))
	{
		TFTPLOG("DX Resolv error :%s\n",g_TFTPLocationParams.pFirmwareSrvFQDN);
		//TFTP_Send_MMIMsg(TFTP_MMI_FAIL_DNS);
		return ERROR;
	}
	if (TRUE == g_bTFTPNeedCancel)
	{
		g_bTFTPNeedCancel = FALSE;
		TFTPLOG("Provisioning process exit by user \n");
		return OK;
	}
	st = TftpCodeGet(g_TFTPLocationParams.pFirmwareSrvFQDN,ipAddr,FirmwareFileName,1);
	return st;
}

VOID TftpflashupCompressSize(UINT32 totalFileBytes)
{
	UINT32    sec;
    FLASH_DEF * pAddr;
	UINT32		flashDev;

#ifdef ONECHIP_FLASH
	flashDev = FLASH1_DEV;
#else
	flashDev = FLASH2_DEV;
#endif	/* ONECHIP_FLASH */

#ifdef ONECHIP_FLASH
	/* Write the size info of the compressed image */
    /* get the flash base */
    pAddr = flashSectorAddr( flashDev, 0 );
    /* point to magic address */
    pAddr += (FLASHUP_COMPRESSED_IMAGE_SIZE_OFFSET / FLASH_WIDTH);
    /* get the base sector number */
    sec = flashSectorNum( pAddr );

    /**************************************************
    *  perform the FLASH programming
    **************************************************/
    sysFlashWrite( pAddr, FLASH_CAST &totalFileBytes, sizeof(UINT32), 0, 0, 0 );
    TFTPLOG("<TFTP> after write compressed file bytes %d \n",totalFileBytes);
#endif	/* ONECHIP_FLASH */

}

STATUS TftFlashEraseSector(UINT32 sec)
{
	FLASH_DEF * pAddr;
	STATUS    stat;
	UINT32		flashDev;

#ifdef ONECHIP_FLASH
	flashDev = FLASH1_DEV;
#else
	flashDev = FLASH2_DEV;
#endif	/* ONECHIP_FLASH */
	/* get base address of sector */
	pAddr = flashSectorAddr( flashDev, sec );
	if( (FLASH_DEF *) ~0 == pAddr )
	{
		TFTPLOG( "flashUp: ERASE Addr ERROR: Sector #%3u, Abs Address %08X\n",
			sec, (unsigned int)pAddr );
		return ERROR;
	}
	TFTPLOG( "flashUp4: ERASE: Sector #%3u, Abs Address %08X\n",
			sec, (unsigned int)pAddr );

	   /**************************************************
		*  perform the erase
		**************************************************/
	stat = sysSectorErase( pAddr );
	hwFeedWDT();
	TFTPLOG( "flashUp4: ERASE_end: Sector #%3u, Abs Address %08X\n",
			sec, (unsigned int)pAddr );
	if(stat != OK)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -