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

📄 main.c.bak

📁 这个是嵌入式arm系列的一个bootloader程序。对需要编写bootloader的很有参考价值
💻 BAK
📖 第 1 页 / 共 4 页
字号:

void WINAPIV NKDbgPrintfW(LPCWSTR lpszFmt, ...)  
{

}

int StoreEBootCFG(EBOOTCFG *EBootCFG)
{
	volatile DWORD * pdwFlash;
	DWORD *pCFG;
	int i,j,k;
		
	EdbgOutputDebugString("StoreEBootCFG.\r\n");
	j = sizeof(EBOOTCFG)/4;
	pdwFlash	= (volatile DWORD *)(FLASH_CFG_START); 

	// Erase the EBootCFG block
	FlashErase((DWORD)pdwFlash, 128*1024);  

	pCFG = (DWORD *)EBootCFG;

	// Program the contents of EBOOTCFG one DWORD at a time.
	for(i = 0; i < j; i++)
	{

		*pdwFlash = 0x00400040;
		*pdwFlash = *(DWORD *)(pCFG+i);
		k = 0;
		while((k & 0x00800080) != 0x00800080)
		{
			k = (*pdwFlash);
		}
		if (k & 0x00000008)
			EdbgOutputDebugString("Voltage Range Error ... \r\n");

		if (k & 0x00000002)
			EdbgOutputDebugString("Device Protect Error ... \r\n");

		if (k & 0x00000010)
			EdbgOutputDebugString("Programming Error ... \r\n");
		
		pdwFlash++;

	}
	// Put flash back into read mode.
	*pdwFlash = 0x00FF00FF;
	return 0;
}

int LoadEBootCFG(EBOOTCFG *EBootCFG)
{
	volatile DWORD * pdwFlash;
	DWORD *pCFG;
	DWORD i,j;
	
	j = sizeof(EBOOTCFG)/4;
	pdwFlash	= (volatile DWORD *)(FLASH_CFG_START);
	pCFG = (DWORD *)EBootCFG;

	// Read the contents of Flash into EBOOTCFG one DWORD at a time.
	*pdwFlash = 0x00FF00FF;
	for(i = 0; i < j; i++)
	{
		*(DWORD *)(pCFG+i) = *(pdwFlash+i);
	}

	// Is the CFG data valid?  Check for the magic number that was written the last time
	// the CFG block was updated.  If Eboot has never been run, there will be no configuration
	// information, so the magic number will likely not be found.  In this case, setup the 
	// factory defaults and store them into Flash.
	if (EBootCFG->ConfigMagicNumber != EbootCFGMagicNumber)
	{
		ResetFactoryDefaultCFG(EBootCFG);
	}

	return 0;
}

void ResetFactoryDefaultCFG(EBOOTCFG *pEbootCFG)
{
	EdbgOutputDebugString("\r\nResetting factory default configuration ... \r\n");
	pEbootCFG->autoDownloadImage = TRUE;
	pEbootCFG->IP = inet_addr("10.1.15.240"); 
	pEbootCFG->subnetMask = inet_addr("255.255.254.0"); 
	pEbootCFG->numBootMe = 25;
	pEbootCFG->delay = 5;
	pEbootCFG->DHCPEnable = FALSE;
	//======= Daric 2002/08/23 ========//
	pEbootCFG->dwLaunchAddr = 0x98381000; //change by houjg 2007.05.10
	//pEbootCFG->dwLaunchAddr = 0x800c1000;
	//================================//
	pEbootCFG->bootDeviceOrder = 0;
	pEbootCFG->ConfigMagicNumber = EbootCFGMagicNumber;
}

void SetIP(EBOOTCFG *pEbootCFG)
{
	char szDottedD[16];	// The string used to collect the dotted decimal IP address
	WORD cwNumChars = 0;
	UINT16 InChar = 0;

	EdbgOutputDebugString ( "\r\nEnter new IP address: ");

	while(!((InChar == 0x0d) || (InChar == 0x0a)))
	{
		InChar = OEMReadDebugByte();
		if (InChar != OEM_DEBUG_COM_ERROR && InChar != OEM_DEBUG_READ_NODATA) 
		{
			// If it's a number or a period, add it to the string
			if (InChar == '.' || (InChar >= '0' && InChar <= '9')) 
			{
				if (cwNumChars < 16) 
				{
					szDottedD[cwNumChars++] = (char)InChar;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
			// If it's a backspace, back up
			else if (InChar == 8) 
			{
				if (cwNumChars > 0) 
				{
					cwNumChars--;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
		}
	}

	// If it's a carriage return with an empty string, don't change anything.
	if (cwNumChars) 
	{
		szDottedD[cwNumChars] = '\0';
		pEbootCFG->IP = inet_addr( szDottedD );
	}
	ClearPromiscuousIP();                
}

void SetMask(EBOOTCFG *pEbootCFG)
{
	char szDottedD[16];	// The string used to collect the dotted masks
	WORD cwNumChars = 0;
	UINT16 InChar = 0;

	EdbgOutputDebugString ( "\r\nEnter new subnet mask: ");

	while(!((InChar == 0x0d) || (InChar == 0x0a)))
	{
		InChar = OEMReadDebugByte();
		if (InChar != OEM_DEBUG_COM_ERROR && InChar != OEM_DEBUG_READ_NODATA) 
		{
			// If it's a number or a period, add it to the string
			if (InChar == '.' || (InChar >= '0' && InChar <= '9')) 
			{
				if (cwNumChars < 16) 
				{
					szDottedD[cwNumChars++] = (char)InChar;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
			// If it's a backspace, back up
			else if (InChar == 8) 
			{
				if (cwNumChars > 0) 
				{
					cwNumChars--;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
		}
	}

	// If it's a carriage return with an empty string, don't change anything.
	if (cwNumChars) 
	{
		szDottedD[cwNumChars] = '\0';
		pEbootCFG->subnetMask = inet_addr( szDottedD );
	}
}
void SetBootMe(EBOOTCFG *pEbootCFG)
{
	char szCount[16];
	WORD cwNumChars = 0;
	UINT16 InChar = 0;

	EdbgOutputDebugString ( "\r\nUse 0 for continuous boot me packets. \r\n");
	EdbgOutputDebugString ( "Enter maximum number of boot me packets to send [0-255]: ");

	while(!((InChar == 0x0d) || (InChar == 0x0a)))
	{
		InChar = OEMReadDebugByte();
		if (InChar != OEM_DEBUG_COM_ERROR && InChar != OEM_DEBUG_READ_NODATA) 
		{
			// If it's a number or a period, add it to the string
			if ((InChar >= '0' && InChar <= '9')) 
			{
				if (cwNumChars < 16) 
				{
					szCount[cwNumChars++] = (char)InChar;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
			// If it's a backspace, back up
			else if (InChar == 8) 
			{
				if (cwNumChars > 0) 
				{
					cwNumChars--;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
		}
	}

	// If it's a carriage return with an empty string, don't change anything.
	if (cwNumChars) 
	{
		szCount[cwNumChars] = '\0';
		pEbootCFG->numBootMe = atoi(szCount);
		if (pEbootCFG->numBootMe > 255)
		{
			pEbootCFG->numBootMe = 255;
		} 
		else if (pEbootCFG->numBootMe < 0)
		{
			pEbootCFG->numBootMe = 1;
		}
	}
}

void SetDelay(EBOOTCFG *pEbootCFG)
{
	char szCount[16];
	WORD cwNumChars = 0;
	UINT16 InChar = 0;

	EdbgOutputDebugString ( "\r\nEnter maximum number of seconds to delay [1-255]: ");

	while(!((InChar == 0x0d) || (InChar == 0x0a)))
	{
		InChar = OEMReadDebugByte();
		if (InChar != OEM_DEBUG_COM_ERROR && InChar != OEM_DEBUG_READ_NODATA) 
		{
			// If it's a number or a period, add it to the string
			if ((InChar >= '0' && InChar <= '9')) 
			{
				if (cwNumChars < 16) 
				{
					szCount[cwNumChars++] = (char)InChar;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
			// If it's a backspace, back up
			else if (InChar == 8) 
			{
				if (cwNumChars > 0) 
				{
					cwNumChars--;
					OEMWriteDebugByte((BYTE)InChar);
				}
			}
		}
	}

	// If it's a carriage return with an empty string, don't change anything.
	if (cwNumChars) 
	{
		szCount[cwNumChars] = '\0';
		pEbootCFG->delay = atoi(szCount);
		if (pEbootCFG->delay > 255)
		{
			pEbootCFG->delay = 255;
		} 
		else if (pEbootCFG->delay < 1)
		{
			pEbootCFG->delay = 1;
		}
	}
}
#if defined ( PLAT_LUBBOCK )
void SetSMSCMACAddress()
{
	char szDottedD[24];	// The string used to collect the dotted masks
	WORD cwNumChars = 0;
	UINT16 InChar = 0;
	USHORT MacAddr[3];
	USHORT MacAddrTemp[3];

#if defined ( PLAT_LUBBOCK )
    //volatile BLR_REGS *BLR = (BLR_REGS *)FPGA_REGS_BASE_U_VIRTUAL;
#endif
	
    EdbgOutputDebugString("Checking for SMC 91C96 Ethernet controller...\r\n");

    // Enable the oscillator on the Neponset board.  Lubbock's oscillator is always running.
    #if defined ( PLAT_SANDGATE )
        #if defined ( USING_SA1111 )
			pBDRegister->ncr[0] |= ncrBits_enet_osc_en_mask;
       #endif
    #endif

    // Enable 16 bit access to the SMC device for the Lubbock platform, only.
    #if defined ( PLAT_LUBBOCK )
		//BLR->misc_wr &= ~ENET_nEN16;
		*((volatile PBYTE) SMC_ETHERNET_ATTRIB_BASE_U_VIRTUAL+0x20008) = 0;  // Enable 16 bit mode
		*((volatile PBYTE) SMC_ETHERNET_ATTRIB_BASE_U_VIRTUAL+0x20000) = 1;  // Enable Ethernet controller
    #endif
    // Enable 8 bit access to the SMC device for the Sandgate/Neponset platform, only.
    #if defined ( PLAT_SANDGATE )
        #if defined ( USING_SA1111 )
            *((volatile PBYTE) SMC_ETHERNET_ATTRIB_BASE_U_VIRTUAL+0x20008) =0x20;  // Enable 8 bit mode
			*((volatile PBYTE) SMC_ETHERNET_ATTRIB_BASE_U_VIRTUAL+0x20000) = 1;    // Enable Ethernet controller
        #endif
    #endif

	if (SMCInit( (BYTE*)(SMC_ETHERNET_IO_BASE_U_VIRTUAL), 4, MacAddr))
	{
	    EdbgOutputDebugString("SMC 91C96 Ethernet controller initialized.\r\n");
		EdbgOutputDebugString ( "\r\nEnter new MAC address (nnn.nnn.nnn.nnn.nnn.nnn): ");

		while(!((InChar == 0x0d) || (InChar == 0x0a)))
		{
			InChar = OEMReadDebugByte();
			if (InChar != OEM_DEBUG_COM_ERROR && InChar != OEM_DEBUG_READ_NODATA) 
			{
				// If it's a number or a period, add it to the string
				if (InChar == '.' || (InChar >= '0' && InChar <= '9')) 
				{
					if (cwNumChars < 23) 
					{
						szDottedD[cwNumChars++] = (char)InChar;
						OEMWriteDebugByte((BYTE)InChar);
					}
				}
				// If it's a backspace, back up
				else if (InChar == 8) 
				{
					if (cwNumChars > 0) 
					{
						cwNumChars--;
						OEMWriteDebugByte((BYTE)InChar);
					}
				}
			}
		}

		// If it's a carriage return with an empty string, don't change anything.
		if (cwNumChars) 
		{
			szDottedD[cwNumChars] = '\0';
			CvtMAC(MacAddr, szDottedD );
			EdbgOutputDebugString ( "\r\n");
			SMCSetMACAddress(MacAddr);
			SMCGetMACAddress(MacAddrTemp);
			if ( (MacAddr[0] != MacAddrTemp[0]) &&
				 (MacAddr[1] != MacAddrTemp[1]) &&
				 (MacAddr[2] != MacAddrTemp[2]) )
			{
				EdbgOutputDebugString ( "\r\nMAC address programming failure!\r\n");
			    EdbgOutputDebugString("Requested address: %B:%B:%B:%B:%B:%B\r\n",
                          MacAddr[0] & 0x00FF, MacAddr[0] >> 8,
                          MacAddr[1] & 0x00FF, MacAddr[1] >> 8,
                          MacAddr[2] & 0x00FF, MacAddr[2] >> 8);
			    EdbgOutputDebugString("SMSC controller returned: %B:%B:%B:%B:%B:%B\r\n",
                          MacAddr[0] & 0x00FF, MacAddr[0] >> 8,
                          MacAddr[1] & 0x00FF, MacAddr[1] >> 8,
                          MacAddr[2] & 0x00FF, MacAddr[2] >> 8);
			} else
			{
				EdbgOutputDebugString ( "\r\nMAC address programming complete.\r\n");
			}
		}
	} else 
	{
		EdbgOutputDebugString("SMC 91C96 Ethernet controller failed to initialize.\n");
		EdbgOutputDebugString("Unable to program MAC address.\n");
	}
}

// This routine will convert a dotted decimal MAC address into a binary version
void CvtMAC(USHORT MacAddr[3], char *pszDottedD ) 
{
    DWORD cBytes;
    char *pszLastNum;
    int atoi (const char *s);
	int i=0;    
	BYTE *p = (BYTE *)MacAddr;

    // Replace the dots with NULL terminators
    pszLastNum = pszDottedD;
    for( cBytes = 0; cBytes < 6; cBytes++ ) {
        while(*pszDottedD != '.' && *pszDottedD != '\0')
            pszDottedD++;
        if (pszDottedD == '\0' && cBytes != 5)
		{
			// zero out the rest of MAC address
			while(i++ < 6)
			{
				*p++ = 0;
			}
			break;
		}
        *pszDottedD = '\0';
        *p++ = (atoi(pszLastNum) & 0xFF);
		i++;
        pszLastNum = ++pszDottedD;
    }
}
#endif

⌨️ 快捷键说明

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