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

📄 main.c

📁 老外的一个开源项目
💻 C
📖 第 1 页 / 共 5 页
字号:
			break;
		case BOOT_DEVICE_HWUART:
			pUARTRegs = (volatile UART_REG_T *) OALPAtoVA(PXA255_BASE_REG_PA_HWUART, FALSE);
			break;
		case BOOT_DEVICE_BTUART:
			pUARTRegs = (volatile UART_REG_T *) OALPAtoVA(PXA255_BASE_REG_PA_BTUART, FALSE);
			break;
		default:
			return FALSE;
	}

	uCtrl = (pUARTRegs->MCR & ~UART_MCR_RTS);
	pUARTRegs->MCR = (uCtrl | UART_MCR_RTS);

	for(ct = 0; ct < *pcbFrame; ct++)
    {
		if (!bWaitInfinite)
		{
			tStart = OEMEthGetSecs();
		}

		while(!(UART_LSR_DR & (uStatus = pUARTRegs->LSR)))
        {
            if(!bWaitInfinite && (OEMEthGetSecs() - tStart > TIMEOUT_RECV))
            {
                *pcbFrame = 0;
				pUARTRegs->MCR = uCtrl;
				return FALSE;
            }            
        }

        // check and clear comm errors
        if(UART_LSR_ERRORS & uStatus)
        {
            // clear the queue
            pUARTRegs->IIR_FCR = (UART_FCR_TRFIFOE | UART_FCR_RESETRF | UART_FCR_RESETTF);
            
            *pcbFrame = 0;
			pUARTRegs->MCR = uCtrl;

            KITLOutputDebugString("Comm errors have occurred; status = 0x%x\r\n", uStatus);
            return FALSE;
        }

        *(pbFrame + ct) = (UINT8)pUARTRegs->THR_RBR_DLL;
    }
	pUARTRegs->MCR = uCtrl;
   
    return TRUE;
}


//------------------------------------------------------------------------------
//
//  Function:  OEMSerialSendRaw
//
//
BOOL OEMSerialSendRaw(LPBYTE pbFrame, USHORT cbFrame)
{
	volatile UART_REG_T	*pUARTRegs;
    UINT ct;
	
	switch (g_EthDevice)
	{
		case BOOT_DEVICE_STUART:
			pUARTRegs = (volatile UART_REG_T *) OALPAtoVA(PXA255_BASE_REG_PA_STUART, FALSE);
			break;
		case BOOT_DEVICE_HWUART:
			pUARTRegs = (volatile UART_REG_T *) OALPAtoVA(PXA255_BASE_REG_PA_HWUART, FALSE);
			break;
		case BOOT_DEVICE_BTUART:
			pUARTRegs = (volatile UART_REG_T *) OALPAtoVA(PXA255_BASE_REG_PA_BTUART, FALSE);
			break;
		default:
			return FALSE;
	}

	// block until send is complete; no timeout
    for(ct = 0; ct < cbFrame; ct++)
    {            
        // check that send transmitter holding register is empty
        while(!(pUARTRegs->LSR) & UART_LSR_TEMT)
            (VOID)NULL;

        // write character to port
        pUARTRegs->THR_RBR_DLL = (UINT32)*(pbFrame+ct);
    }
    
    return TRUE;
}


//------------------------------------------------------------------------------
//
//  Function:  OEMShowProgress
//
//  Updates a download progress indicator.
//
void OEMShowProgress(DWORD dwPacketNum)
{
    static DWORD PrevPercentComplete = -1;
    DWORD PercentComplete;

    // g_dwBytesRead is updated during a data packet read.
    // g_dwTotalBytes is determined from the size of the download image
    // (which is obtained from the .bin file header for example).

    PercentComplete = (g_dwBytesRead * 100) / g_dwTotalBytes;

    // Update the display only when the percent complete changes.
    if (PercentComplete != PrevPercentComplete)
    {
        EdbgOutputDebugString("%d%%",PercentComplete);
		if ( PercentComplete >= 10 )
		{
			EdbgOutputDebugString("\b",PercentComplete);
		}
		EdbgOutputDebugString("\b\b",PercentComplete);
        PrevPercentComplete = PercentComplete;
    }

    return;
}

//------------------------------------------------------------------------------
//
//  Function:  OEMLaunch
//
//  Launch downloaded/stored image.
//
void OEMLaunch(DWORD dwImageStart, DWORD dwImageLength, DWORD dwLaunchAddr, const ROMHDR *pRomHdr)
{
    EDBG_OS_CONFIG_DATA *pCfgData;
    EDBG_ADDR EshellHostAddr;
    UINT32 PhysAddress;
    ROMHDR *pToc;
	UINT32 j;


	// If a launch address wasn't specified - use the last known good address.
    //
    if (!dwLaunchAddr)
    {
		switch (g_EbootCFG.imageLoadDevice) {
			case IMG_LOAD_FLASH:
				PhysAddress = (UINT32)OALPAtoVA(BOOT_FLASH_PA_BASE, FALSE) + IMAGEFLASH_NK_OFFSET;
				if (*(UINT32 *)(PhysAddress + ROM_SIGNATURE_OFFSET) != ROM_SIGNATURE)
				{
					EdbgOutputDebugString("ERROR: Image Signature in Flash Memory not found.\r\n");
					EdbgOutputDebugString("Press any key to reset.\r\n");
					SpinForDebugKey();
					OEMReset();
				}
				else
				{
					pToc = (ROMHDR *)(*(UINT32 *)(PhysAddress + ROM_TOC_OFFSET_OFFSET) + PhysAddress);
					// compute length in bytes
					j = pToc->physlast - pToc->physfirst;

					EdbgOutputDebugString("Copying OS Image from FLASH(0x%X) to RAM(0x%X) (%d bytes)...\r\n",
						PhysAddress, IMAGE_NK_CA_START, j);
					memcpy((void *)IMAGE_NK_CA_START,(void *)PhysAddress,j);
					// image should be in ram at LaunchAddr now
					// Check for pTOC signature ("CECE") here, after image in place
					if (*(UINT32 *)((pToc->physfirst)+ ROM_SIGNATURE_OFFSET) == ROM_SIGNATURE)
					{
						EdbgOutputDebugString("Jumping to image at physical 0x%x...\r\n\r\n\r\n",(UINT32)OALCAtoUA(pToc->physfirst));
						OALStall(1000000);

						g_pBSPArgs->bootFlags |= BOOTFLAGS_BOOTEDFLASH;
						// away we go...
						Launch((UINT32)OALCAtoUA(pToc->physfirst));
						EdbgOutputDebugString("\r\n\r\nERROR: OS Image failed to launch at 0x%X\r\n\r\n",pToc->physfirst);
					}
					else
						EdbgOutputDebugString("\r\n\r\nERROR: OS Image not found at 0x%X\r\n\r\n",pToc->physfirst);
				}
				break;
			case IMG_LOAD_DOWNLOAD:
				break;
		}
    }

    // Translate the image launch address (virtual address) to a physical address.
    //
    PhysAddress = (UINT32) OALVAtoPA((VOID *)dwLaunchAddr);
    EdbgOutputDebugString("Download successful!  Jumping to image at physical 0x%x...\r\n", PhysAddress);

    // Wait for PB connection...
    //
    if (g_DownloadImage)
    {
		if (g_EthDevice == BOOT_DEVICE_STUART || g_EthDevice == BOOT_DEVICE_HWUART || g_EthDevice == BOOT_DEVICE_BTUART)
		{
			// serial download
			// wait for jump packet indefinitely
//			pBootArgs->KitlTransport = (WORD)SerialWaitForJump();
			SerialWaitForJump();
		}
		else
		{ 
			if (!(pCfgData = EbootWaitForHostConnect(&g_DeviceAddr, &EshellHostAddr)))
			{
				EdbgOutputDebugString("ERROR: EbootWaitForHostConnect failed!\r\n");
				EdbgOutputDebugString("Press any key to reset.\r\n");
				SpinForDebugKey();
				OEMReset();
			}

			// If the user selected "passive" KITL (i.e., don't connect to the target at boot time), set the
			// flag in the args structure so the OS image can honor it when it boots.
			//
			if (pCfgData && (pCfgData->KitlTransport & KTS_PASSIVE_MODE))
			{
				g_pBSPArgs->kitl.flags |= OAL_KITL_FLAGS_PASSIVE;
			}
		}
    }


    // Jump to the image we just downloaded.
    //
    EdbgOutputDebugString("\r\n\r\n");
    Launch(PhysAddress);

    // Should never get here...
    //
    OALSpinForever();

}


//------------------------------------------------------------------------------
//
//  Function:  OEMIsFlashAddr
//
// Checks the address provided and determines if it's a flash address or not.
//
BOOL OEMIsFlashAddr(DWORD dwAddr)
{
    DWORD dwVirtFlashStart = (DWORD) OALPAtoVA(BOOT_FLASH_PA_BASE, FALSE);

    if ((dwAddr >= dwVirtFlashStart) && (dwAddr <= (dwVirtFlashStart + g_PCIFlashInfo.MemLen.Reg[0])))
    {
        return(TRUE);
    }

    return(FALSE);
}


//------------------------------------------------------------------------------
//
//  Function:  OEMMapMemAddr
//
// Maps a flash address to a RAM address (used when temporarily caching a
// flash-destined download image to RAM).
//
LPBYTE OEMMapMemAddr (DWORD dwImageStart, DWORD dwAddr)
{

    if (OEMIsFlashAddr(dwAddr))
    {
		// The image being downloaded is a flash image - temporarily
		// cache the image in RAM until it's completely downloaded.
		//
        dwAddr -= dwImageStart;
        dwAddr += EBOOT_FLASH_BUFFER_START;
    }

    return((LPBYTE) (dwAddr));
}

//------------------------------------------------------------------------------
//
//  Function:  OEMStartEraseFlash
//
// Begins the flash erase procedure.
//
BOOL OEMStartEraseFlash (DWORD dwStartAddr, DWORD dwLength)
{
    return(TRUE);
}


//------------------------------------------------------------------------------
//
//  Function:  OEMContinueEraseFlash
//
// Continues the flash erase procedure (erases are incrementally done during
// download to speed up the overall flash operation from the user's perspective).
//
void OEMContinueEraseFlash(void)
{
}


//------------------------------------------------------------------------------
//
//  Function:  OEMFinishEraseFlash
//
// Finished the flash erase procedure.
//
BOOL OEMFinishEraseFlash(void)
{
    return(TRUE);
}


BOOL OEMWriteFlash(DWORD dwImageStart, DWORD dwImageLength)
{
    BOOL  retVal = FALSE;
    UINT8 *pDataBuffer = (UINT8 *) OEMMapMemAddr(dwImageStart, dwImageStart);
	DWORD dwNumberOfSectors, dwNumberOfBlocks;
	DWORD dwSectorBlocks, dwSectorAddress;
	DWORD timeout;
	DWORD i;

	OALMSG(1, (L"INFO: Write flash start 0x%08X  length %d.\r\n",dwImageStart,dwImageLength));
	dwNumberOfSectors = dwImageLength / g_FlashInfo.wDataBytesPerSector;
	if (dwImageLength % g_FlashInfo.wDataBytesPerSector)
		dwNumberOfSectors++;		// round up
	dwNumberOfBlocks = dwImageLength / g_FlashInfo.dwBytesPerBlock;
	if (dwImageLength % g_FlashInfo.dwBytesPerBlock)
		dwNumberOfBlocks++;			// round up
	OALMSG(1, (L"INFO: Writing %d sectors for length %d.\r\n",dwNumberOfSectors,dwImageLength));
	switch (g_ImageType) {
		case IMAGE_TYPE_BOOTLOADER:
			OALMSG(1, (L"INFO: Erasing %d blocks at 0x%08X.\r\n",dwNumberOfBlocks,IMAGEFLASH_LOADER_BLOCKOFFSET));
			EdbgOutputDebugString(" ");		// for progress indicator
			for (i=0;i<dwNumberOfBlocks;i++)
			{
				printProgress();
				if (!FMD_EraseBlock((BLOCK_ID)(i + IMAGEFLASH_LOADER_BLOCKOFFSET)))
					{
						EdbgOutputDebugString("\r\nERROR: failed to erase bootloder in Flash.\r\n");
						EdbgOutputDebugString("The boot blocks may be locked.\r\n");
						retVal = FALSE;
						break;
					}
				retVal = TRUE;
			}
			if (retVal == FALSE)
				break;
			retVal = FMD_WriteSector((g_FlashInfo.wSectorsPerBlock * IMAGEFLASH_LOADER_BLOCKOFFSET), 
										pDataBuffer, NULL, dwNumberOfSectors);
			if (retVal == FALSE)
			{
				EdbgOutputDebugString("\r\nERROR: failed to program bootloder in Flash.\r\n");
				break;
			}
			else
			{
				LockBootBlocks(TRUE);
				EdbgOutputDebugString("\r\nINFO: bootloader image stored successfully.\r\n");
				timeout = OEMEthGetSecs() + 5;
				EdbgOutputDebugString("Rebooting in  ");
				do
				{
					i = OEMEthGetSecs();
	                OEMWriteDebugByte((BYTE)0x08); // print back space
					EdbgOutputDebugString("%d",(timeout - i));
					OALStall(500000);
				}
				while ( i < timeout);
				EdbgOutputDebugString("\r\n");
				OEMReset();
			}
			break;
		case IMAGE_TYPE_FLASHIMAGE:
			OALMSG(1, (L"INFO: Erasing %d blocks at 0x%08X.\r\n",dwNumberOfBlocks,IMAGEFLASH_NK_BLOCKOFFSET));
			EdbgOutputDebugString(" ");		// for progress indicator
			for (i=0;i<dwNumberOfBlocks;i++)
			{ 
				printProgress();
				if (!FMD_EraseBlock((BLOCK_ID)(i + IMAGEFLASH_NK_BLOCKOFFSET)))
					{
						EdbgOutputDebugString("\r\nERROR: failed to erase image in Flash.\r\n");
						retVal = FALSE;
						break;
					}
				retVal = TRUE;
			}
			if (retVal == FALSE)
				break;
			dwSectorBlocks = dwNumberOfSectors / g_FlashInfo.wSectorsPerBlock;
			dwSectorAddress = g_FlashInfo.wSectorsPerBlock * IMAGEFLASH_NK_BLOCKOFFSET;
			for (i=0;i<dwSectorBlocks;i++)
			{
				printProgress();
				if (!FMD_WriteSector(dwSectorAddress, pDataBuffer, NULL, g_FlashInfo.wSectorsPerBlock))
				{
					EdbgOutputDebugString("\r\nERROR: failed to program image in Flash.\r\n");
					retVal = FALSE;
					break;
				}
				dwSectorAddress += g_FlashInfo.wSectorsPerBlock;
				pDataBuffer += g_FlashInfo.dwBytesPerBlock;
				retVal = TRUE;
			}
			if (retVal == FALSE)
				break;
			// program the odd sectors
			dwSectorBlocks = dwNumberOfSectors % g_FlashInfo.wSectorsPerBlock;
			retVal = FMD_WriteSector(dwSectorAddress, pDataBuffer, NULL, dwSectorBlocks);
			if (retVal == FALSE)
			{
				EdbgOutputDebugString("\r\nERROR: failed to program image in Flash.\r\n");
				break;
			}
			else
			{
				EdbgOutputDebugString("\r\nINFO: image stored successfully.\r\n");
				timeout = OEMEthGetSecs() + 5;
				EdbgOutputDebugString("Rebooting in  ");
				do
				{
					i = OEMEthGetSecs();
	                OEMWriteDebugByte((BYTE)0x08); // print back space
					EdbgOutputDebugString("%d",(timeout - i));
					OALStall(500000);
				}
				while ( i < timeout);
				EdbgOutputDebugString("\r\n");
				OEMReset();
			}
			break;

⌨️ 快捷键说明

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