main.c

来自「WinCE 3.0 BSP, 包含Inter SA1110, Intel_815」· C语言 代码 · 共 856 行 · 第 1/3 页

C
856
字号
                            else {
                                // TFTP routine returns non zero as long as a valid link is established.  In this case,
                                // stop sending BOOTMEs.
                                if (EbootTFtpReceiver( &MyAddr, FrameBuffer, wDestPort, wSrcPort, pwUDPData, wUDPDataLen )) {
                                    BootmeCnt = MAX_BOOTME_CNT;
                                }
                            }
							break;
                        } // switch (dwIPState)
                    }
                    break;
                default:
                    break;
            } // switch (wFrameType)
        }
    }

//    if (dwLaunchAddr) {
        EdbgOutputDebugString( "Network Transfer Complete\r\n" );
        if (fWriteToFlash) {
            unsigned i;
            unsigned retval;
            
            // First erase the Flash
            retval = OEMFlashWriteBegin(dwPhysStart, dwPhysLen);
            if(retval != 0) {
                EdbgOutputDebugString("Internal error. OEMFlashWriteBegin returns wrong value for a Flash image.");
                return 4;
            }
            
            EdbgOutputDebugString("\r\nWriting image to Flash: (Start=0x%x, Length=0x%x)...", dwPhysStart, dwPhysLen);
            for(i = 0; i < dwPhysLen; i += 4) {
                
#if 0
                if( (i >= 0x92000) && (i < 0x93050) ) {
                    
                    EdbgOutputDebugString("Calling OFW(0x%x, 0x%x)\r\n", (dwPhysStart + i), *(volatile unsigned *)(FLASH_CACHE + i));
                    
                }
#endif 0
                OEMFlashWrite( (dwPhysStart + i), * (volatile unsigned *)(FLASH_CACHE + i) );
                
                // Print a dot after every 160 k.
                if( !(i % 40000) )
                    EdbgOutputDebugString(".");
            }
            
#if (SH_PLATFORM != PLATFORM_ASPEN)
            if (FlashWrite( dwPhysStart, dwPhysLen, &pszErrorMsg )) {
                EdbgOutputDebugString( "FlashWrite() %s\r\n", pszErrorMsg );
                goto FatalError;
            }
#endif (SH_PLATFORM != PLATFORM_ASPEN)
            EdbgOutputDebugString( "done\r\n" );
        }
		else {
        	EdbgOutputDebugString("Download successful! Jumping to image at %Xh...\r\n",dwLaunchAddr);
		}
//    }

DoLaunch:
    // Update address info, in driver globals, and in EEPROM if necessary
    memcpy(&pDriverGlobals->eth.PlatformAddr,&MyAddr,sizeof(MyAddr));
    pDriverGlobals->eth.SubnetMask = dwSubnetMask;
	UpdateEEPROMData(MyAddr.dwIP, dwSubnetMask);
    
    // Set address of host running Eshell
    memcpy(&pDriverGlobals->eth.DownloadHostAddr,&EshellHostAddr,sizeof(EDBG_ADDR));

    pDriverGlobals->eth.DHCPLeaseTime = DHCPLeaseTime;
    pDriverGlobals->eth.EdbgFlags = EdbgFlags;

DoLaunch2:
	// if we are downloading, find pTOC
	if (dwPhysLen > 0) {
		// Check for pTOC signature ("CECE") here, after image in place
		if (*(DWORD*)(dwPhysStart - dwOffset + 64) == 0x43454345)
		{
			EdbgOutputDebugString("Found pTOC signature.\n");
		}
		else
		{
			EdbgOutputDebugString("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
			EdbgOutputDebugString("! ERROR: Did not find pTOC signature.  ABORTING. !\n");
			EdbgOutputDebugString("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");

            return 3;
			// If no signature, we're going to fail anyway, so loop forever
			//while (1);
   		}

		// Copy RomHdr from image
		lpdwToc= (LPDWORD)(*(DWORD*)(dwPhysStart - dwOffset + 68));
		memcpy((LPVOID)&RomHdr, (LPVOID)lpdwToc, sizeof(ROMHDR));

		EdbgOutputDebugString("ROMHDR at Address %Xh\r\n",lpdwToc);
		EdbgOutputDebugString("RomHdr.ulRAMStart=%Xh RomHdr.physfirst=%Xh.\r\n",RomHdr.ulRAMStart,RomHdr.physfirst);
    	if(VerifyCheckSum()) {
	    	goto FatalError;
	    }
    }

    if (dwLaunchAddr == 0) {
        if (dwLastLaunchAddrValid == LAST_LAUNCH_ADDR_VALID) {
            EdbgOutputDebugString("Using previous launch address\n");
            dwLaunchAddr = dwLastLaunchAddr;
        }
        else {
		if (RomHdr.physfirst != -1)
			dwLaunchAddr= RomHdr.physfirst;
		else
		    dwLaunchAddr = 0x80140000UL;   // Default launch address
        }
    }
    
	if(!fWriteToFlash) {
    	EdbgOutputDebugString("\r\nJumping to existing image at %Xh...\r\n",dwLaunchAddr);
	}
	else {
    	EdbgOutputDebugString("\r\nPlease toggle switch S1-1 and reset Aspen to boot from Flash...\r\n",dwLaunchAddr);
		while(1);
	}

#if (SH_PLATFORM != PLATFORM_ASPEN)
	if (RomHdr.physfirst != -1) {
	    if (pDriverGlobals->eth.etherFlags & EDBG_FL_CLEANBOOT) {
            
            //
            // Check to see if the bootloader's image overlaps the real image's
            // ram section. If so there might be a problem doing the cleanboot.
            // Of course that means warm boots are never valid for this image/processor.
            //
            //      RomHdr contains the real image's parameters
            //      pTOC contains the bootloader's parameters
            //
            if ((RomHdr.ulRAMStart < pTOC->physlast) && (RomHdr.ulRAMEnd > pTOC->physfirst)) {
                //
                // We have an overlap
                //
                EdbgOutputDebugString("WARNING! : Bootloader detected a possible overlap between the bootloader code\r\n");
                EdbgOutputDebugString("and the image's RAM section. Ignoring the CLEANBOOT request.\r\n");
                EdbgOutputDebugString("        Bootloader image => 0x%X - 0x%X\r\n",pTOC->physfirst,pTOC->physlast);
                EdbgOutputDebugString("        NK RAM           => 0x%X - 0x%X\r\n",RomHdr.ulRAMStart,RomHdr.ulRAMEnd);

            } else {
                EdbgOutputDebugString( "CLEANBOOT Clearing RAM from %Xh-%Xh.\r\n",RomHdr.ulRAMStart,RomHdr.ulRAMEnd);
                memset((LPVOID)RomHdr.ulRAMStart,0,RomHdr.ulRAMEnd-RomHdr.ulRAMStart);
            }
		}
	}
#endif (SH_PLATFORM != PLATFORM_ASPEN)

	dwLaunchAddr|=0xa0000000;
    EdbgOutputDebugString("\r\nJumping to image at %Xh\r\n",dwLaunchAddr);
    Launch(dwLaunchAddr);
    EdbgOutputDebugString("First instructions=(%Xh,%Xh,%Xh)...\r\n",*(LPDWORD)dwLaunchAddr,*(LPDWORD)(dwLaunchAddr+4),*(LPDWORD)(dwLaunchAddr+8));
    Launch(dwLaunchAddr);
	return 0;

FatalError:
    // Some catastrophic error occurred
    EdbgOutputDebugString("! Fatal error in Ethernet Bootloader, Returning to monitor...\n");
#if (SH_PLATFORM != PLATFORM_ASPEN)
    WRITE_REGISTER_ULONG(LED_ALPHA,0xDEADDEAD);
#endif (SH_PLATFORM != PLATFORM_ASPEN)

	return 3;
}


// Read IP address and netmask from serial EEPROM on the SMC board (stored from the
// last time we got this info from user or DHCP).
BOOL
ReadEEPROMData(DWORD *pdwIP, DWORD *pdwSubnetMask)
{
    UINT16 wIPH, wIPL, wSMH, wSML;

    if (!SMCReadEEPROM(0x23,&wIPL) ||
        !SMCReadEEPROM(0x24,&wIPH) ||
        !SMCReadEEPROM(0x25,&wSML) ||
        !SMCReadEEPROM(0x26,&wSMH))
        return FALSE;

    *pdwIP         = ((ULONG)wIPH << 16) | wIPL;
    *pdwSubnetMask = ((ULONG)wSMH << 16) | wSML;

	return TRUE;
}

// Write new address info to the serial EEPROM on the SMC board, if necessary.
// Only update data if it is different!
BOOL
UpdateEEPROMData(DWORD dwIP, DWORD dwSubnetMask)
{
    DWORD dwPrevIP, dwPrevSubnetMask;
    if (!ReadEEPROMData(&dwPrevIP, &dwPrevSubnetMask))
        return FALSE;
	if (dwPrevIP != dwIP) {
		EdbgOutputDebugString( "Writing EEPROM 0x23 with new IP\r\n" );
		if (!SMCWriteEEPROM( 0x23, (WORD)(dwIP) ))
            return FALSE;
		EdbgOutputDebugString( "Writing EEPROM 0x24 with new IP\r\n" );
		if (!SMCWriteEEPROM( 0x24, (WORD)(dwIP >> 16) ))
            return FALSE;
	}

	if (dwPrevSubnetMask != dwSubnetMask) {
		EdbgOutputDebugString( "Writing EEPROM 0x25 with new netmask\r\n" );
		if (!SMCWriteEEPROM( 0x25, (WORD)(dwSubnetMask) ))
            return FALSE;
		EdbgOutputDebugString( "Writing EEPROM 0x26 with new netmask\r\n" );
		if (!SMCWriteEEPROM( 0x26, (WORD)(dwSubnetMask >> 16) ))
            return FALSE;        
	}
    return TRUE;
}


/* OEMEthGetFrame
 *
 *   Check to see if a frame has been received, and if so copy to buffer. An optimization
 *   which may be performed in the Ethernet driver is to filter out all received broadcast
 *   packets except for ARPs.  This is done in the SMC9000 driver.
 *
 * Return Value:
 *    Return TRUE if frame has been received, FALSE if not.
 */
BOOL
OEMEthGetFrame(
    BYTE *pData,       // OUT - Receives frame data
    UINT16 *pwLength)  // IN  - Length of Rx buffer
                       // OUT - Number of bytes received
{
    return SMCGetFrame(pData, pwLength);
}

/* OEMEthSendFrame
 *
 *   Send Ethernet frame.  
 *
 *  Return Value:
 *   TRUE if frame successfully sent, FALSE otherwise.
 */
BOOL
OEMEthSendFrame(
    BYTE *pData,     // IN - Data buffer
    DWORD dwLength)  // IN - Length of buffer
{
    int retries = 0;

    // Let's be persistant here
    while (retries++ < 4) {
        if (!SMCSendFrame(pData, dwLength))
            return TRUE;
        else
            EdbgOutputDebugString("!OEMEthSendFrame failure, retry %u\n",retries);
    }
    return FALSE;
}

/* OEMEthGetSecs
 *
 *  Return a count of seconds from some arbitrary time (the absolute value is not important,
 *  so long as it increments appropriately).
 */
DWORD OEMEthGetSecs( void ) 
{
    SYSTEMTIME st;
    OEMGetRealTime( &st );
    return ((60UL * (60UL * (24UL * (31UL * st.wMonth + st.wDay) + st.wHour) + st.wMinute)) + st.wSecond);
}

VOID SC_WriteDebugLED(WORD wIndex, DWORD dwPattern) 
{
}

#if (SH_PLATFORM != PLATFORM_ASPEN)
// make NKDbgPrintfW do nothing 
void WINAPIV NKDbgPrintfW(LPWSTR lpszFmt, ...) 
{
}
#endif (SH_PLATFORM != PLATFORM_ASPEN)

⌨️ 快捷键说明

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