main.c

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

C
856
字号

		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 1),0x3301);
		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x04 * 1),MyAddr.wMAC[0]);
		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x06 * 1),MyAddr.wMAC[1]);
		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x08 * 1),MyAddr.wMAC[2]);

		MyAddr.wMAC[0] = READ_REGISTER_USHORT(PF_ETHER_BASE + (0x04 * 1));
		MyAddr.wMAC[1] = READ_REGISTER_USHORT(PF_ETHER_BASE + (0x06 * 1));
		MyAddr.wMAC[2] = READ_REGISTER_USHORT(PF_ETHER_BASE + (0x08 * 1));

        EdbgOutputDebugString("SMC Ethernet Address: %B:%B:%B:%B:%B:%B\r\n",
                               MyAddr.wMAC[0] & 0x00FF, MyAddr.wMAC[0] >> 8,
                               MyAddr.wMAC[1] & 0x00FF, MyAddr.wMAC[1] >> 8,
                               MyAddr.wMAC[2] & 0x00FF, MyAddr.wMAC[2] >> 8);
	}
#endif

    // Make sure MAC address has been programmed
    if (!MyAddr.wMAC[0] && !MyAddr.wMAC[1] && !MyAddr.wMAC[2]) {
        EdbgOutputDebugString("Invalid Ethernet address read from SMC board\n");
        goto FatalError;
    }

    // Read any stored IP info in EEPROMs
    EdbgOutputDebugString("Calling ReadEEPROMData...\r\n");
    if (!ReadEEPROMData(&MyAddr.dwIP, &dwSubnetMask)) {
        EdbgOutputDebugString("Failed to read data from EEPROM. Returning to monitor....\n");
#if (SH_PLATFORM != PLATFORM_ASPEN)
		pDriverGlobals->eth.etherFlags|=EDBG_FL_CLEANBOOT;
		pDriverGlobals->eth.EbootMagicNum = ~EBOOT_MAGIC_NUM;
		ParallelDownloadImage();
		goto DoLaunch2;
#endif (SH_PLATFORM != PLATFORM_ASPEN)
		return 2;
	}


    EdbgOutputDebugString("Read IP config from EEPROM:\r\n");
    EdbgOutputDebugString(" IP Address: %s,", inet_ntoa(MyAddr.dwIP));
    EdbgOutputDebugString(" netmask: %s\r\n", inet_ntoa(dwSubnetMask));

    // Save our address in driver globals
    memcpy(&pDriverGlobals->eth.PlatformAddr,&MyAddr,sizeof(MyAddr));
    pDriverGlobals->eth.SubnetMask = dwSubnetMask;

/*** DEBUG ***/
#if 0 /* debug for S1 */
	{
		USHORT	bank;

		bank = READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2));

		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2), 0x3300);
		EdbgOutputDebugString("BANK0\r\n");
		EdbgOutputDebugString("TCR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x00 * 2)));
		EdbgOutputDebugString("EPHSR : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x02 * 2)));
		EdbgOutputDebugString("RCR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x04 * 2)));
		EdbgOutputDebugString("ECR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x06 * 2)));
		EdbgOutputDebugString("MIR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x08 * 2)));
		EdbgOutputDebugString("MCR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0A * 2)));
		EdbgOutputDebugString("BSR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2)));

		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2), 0x3301);
		EdbgOutputDebugString("BANK1\r\n");
		EdbgOutputDebugString("CR    : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x00 * 2)));
		EdbgOutputDebugString("BAR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x02 * 2)));
		EdbgOutputDebugString("IAR0  : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x04 * 2)));
		EdbgOutputDebugString("IAR1  : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x06 * 2)));
		EdbgOutputDebugString("IAR2  : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x08 * 2)));
		EdbgOutputDebugString("GP    : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0A * 2)));
		EdbgOutputDebugString("CTR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0C * 2)));
		EdbgOutputDebugString("BSR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2)));

		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2), 0x3302);
		EdbgOutputDebugString("BANK2\r\n");
		EdbgOutputDebugString("MMUCR : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x00 * 2)));
		EdbgOutputDebugString("PNR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x02 * 2)));
		EdbgOutputDebugString("FIFO  : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x04 * 2)));
		EdbgOutputDebugString("PTR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x06 * 2)));
		EdbgOutputDebugString("DATAH : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x08 * 2)));
		EdbgOutputDebugString("DATAL : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0A * 2)));
		EdbgOutputDebugString("IST   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0C * 2)));
		EdbgOutputDebugString("BSR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2)));

		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2), 0x3303);
		EdbgOutputDebugString("BANK3\r\n");
		EdbgOutputDebugString("MT0   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x00 * 2)));
		EdbgOutputDebugString("MT2   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x02 * 2)));
		EdbgOutputDebugString("MT4   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x04 * 2)));
		EdbgOutputDebugString("MT6   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x06 * 2)));
		EdbgOutputDebugString("MGMT  : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x08 * 2)));
		EdbgOutputDebugString("REV   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0A * 2)));
		EdbgOutputDebugString("ERCV  : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0C * 2)));
		EdbgOutputDebugString("BSR   : %X\r\n", READ_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2)));
		WRITE_REGISTER_USHORT(PF_ETHER_BASE + (0x0E * 2), bank);
	}
#endif
/*** DEBUG ***/


	/* Aspen does not have these switches. */
#if (SH_PLATFORM != PLATFORM_ASPEN)
    // If DIP switch S2-1 is set, jump straight to the programmed image.
    wDIPs = READ_REGISTER_ULONG(0xA3E00000) & 0xFF;
    EdbgOutputDebugString("Read DIP switch S2 : 0x%X\r\n",wDIPs);
    // Polarity is reversed, so this is actually testing the "Off" position
    if (wDIPs & 1) {
        EdbgOutputDebugString("Switch S2-1 off, waiting for commands\r\n(turn on and reboot to bypass eboot)\r\n");
    }
    else {
        EdbgOutputDebugString("Switch S2-1 on, jumping directly to flash image...\r\n");
		pDriverGlobals->eth.EbootMagicNum = ~EBOOT_MAGIC_NUM;	// not to use EDBG
        goto DoLaunch;
	}
#endif (SH_PLATFORM != PLATFORM_ASPEN)

    // Set up TFTP server.  Note that internally, we store port numbers in network byte order.
    EbootInitTFtp( htons(EDBG_DOWNLOAD_PORT), htons(EDBG_DOWNLOAD_PORT));
    EbootInitTFtpd();

    if (EbootTFtpdServerRegister( EDBG_DOWNLOAD_FILENAME, EthDown ))
        EdbgOutputDebugString( "Server Registration Failed\r\n" );

    // Initialize DHCP, and send out first request
    if (EbootInitDHCP( &MyAddr ))
        EdbgOutputDebugString( "Error On InitDHCP() Call\r\n" );

    EdbgOutputDebugString ( "Wait for DHCP, enter new IP address, or CR to use existing IP: ");

    dwStartTime = OEMEthGetSecs();

    // Loop until we have an IP address and eshell tells us which services are
    // configured for Ethernet
    while (!fGotJumpimg) {
        USHORT wLen, wDestPort, wSrcPort, wUDPDataLen, *pwUDPData;

        // Blink LEDs
#if (SH_PLATFORM != PLATFORM_ASPEN)
        WRITE_REGISTER_UCHAR(LED_DISCRETE, (wDiscreteLedCount++) >> 8);
#endif (SH_PLATFORM != PLATFORM_ASPEN)
        // Once we get our IP address, periodically send BOOTME commands to eshell.
        switch (dwIPState) {
        case IPSTATE_NONE:
            // We haven't gotten an IP address yet.  Call into DHCP to see if we
            // need to retransmit request, and poll serial port for user input.
            EbootDHCPRetransmit(&MyAddr,NULL,NULL);
            if (EbootReadSerialIP(&MyAddr, &dwSubnetMask)) {
                dwIPState = IPSTATE_GOTIP;
                EdbgFlags |= EDBG_FLAGS_STATIC_IP;
                DHCPLeaseTime = DEFAULT_DHCP_LEASE;
            }
            break;

        case IPSTATE_GOTIP:
            // Send a gratuitous ARP (an ARP of our new IP address) to verify that no other
            // station is using our IP address.
            if (!EbootGratuitousARP(&MyAddr, FrameBuffer)) {
                dwIPState = IPSTATE_ARP;
                dwNextBootme = OEMEthGetSecs(); // use dwNextBootme for ARP timeout 
            } else {
                EdbgOutputDebugString("EbootGratuitousARP failed\r\n");
            }
            break;

        case IPSTATE_ARP:
            if ((OEMEthGetSecs() - dwNextBootme) >= ARP_RESPONSE_WAIT) {
                EdbgOutputDebugString("No ARP response in %d seconds, assuming ownership of %s\r\n",
                                      ARP_RESPONSE_WAIT, inet_ntoa(MyAddr.dwIP));
                dwIPState = IPSTATE_ARPED;
                dwNextBootme = 0;
            }
            break;

        case IPSTATE_ARPED:
            if ((BootmeCnt < MAX_BOOTME_CNT) &&
                ((BootmeCnt == 0) || (OEMEthGetSecs() >= dwNextBootme))) {
                WRITE_REGISTER_ULONG(LED_ALPHA,MyAddr.dwIP);
                // Send broadcast BOOTME packet to Eshell.  Pass in NULL for device
                // name - it will be constructed from our platform name and the last 
                // word of our MAC address (e.g. Odo23).
                EbootSendBootme(&MyAddr, EBOOT_VERSION_MAJOR, EBOOT_VERSION_MINOR,PLATFORM_STRING,NULL,EDBG_CPUID,0);
                BootmeCnt++;
                dwNextBootme = OEMEthGetSecs() + BOOTME_INTERVAL;
            }
            break;

        case IPSTATE_RETRY:
            EbootInitDHCP(&MyAddr);
            dwIPState = IPSTATE_NONE;
            break;
        }
            
        // Get a frame and pass it to the appropriate handler routine
        wLen = sizeof(FrameBuffer);
        if (SMCGetFrame(FrameBuffer, &wLen)) {
            // Ethernet frame type is in 7th word of buffer
            USHORT wFrameType = *(USHORT *)(FrameBuffer + 6*sizeof(USHORT));

		//	EdbgOutputDebugString("[FrameType:0x%H]\r\n", ntohs(wFrameType));
            switch (ntohs(wFrameType)) {
                case 0x0806:
                    if (EbootProcessARP(&MyAddr,FrameBuffer) == PROCESS_ARP_RESPONSE) {
                        switch (dwIPState) {
                        // Someone has responded to our gratuitous ARP request so they have our IP address.
                        // We need to retry the DHCP request. 
                        case IPSTATE_ARP:
                        case IPSTATE_GOTIP:
                            DHCPRetry++;
                            if (DHCPRetry > MAX_DHCP_RETRY) {
                                EdbgOutputDebugString( "Some other station has IP Address: %s !!! Aborting.\r\n", inet_ntoa(MyAddr.dwIP));
                                goto FatalError;
                            }

                            EdbgOutputDebugString( "Some other station has IP Address: %s !!! Retrying.\r\n", inet_ntoa(MyAddr.dwIP));
                            dwIPState = IPSTATE_RETRY;
                            break;
                        }
                    }
                    break;
                case 0x0800:
                    if (!EbootCheckUDP(&MyAddr, FrameBuffer, &wDestPort, &wSrcPort, &pwUDPData, &wUDPDataLen)) {
#if 0	/* debug for S1 */
						{
							EdbgOutputDebugString("[IP info]\r\n");
							EdbgOutputDebugString("SrcIP : %B.%B.%B.%B (%d.%d.%d.%d)\r\n",
								FrameBuffer[26], FrameBuffer[27], FrameBuffer[28], FrameBuffer[29],
								FrameBuffer[26], FrameBuffer[27], FrameBuffer[28], FrameBuffer[29]);
							EdbgOutputDebugString("DstIP : %B.%B.%B.%B (%d.%d.%d.%d)\r\n",
								FrameBuffer[30], FrameBuffer[31], FrameBuffer[32], FrameBuffer[33],
								FrameBuffer[30], FrameBuffer[31], FrameBuffer[32], FrameBuffer[33]);
							if(FrameBuffer[23] == 0x11) {
								EdbgOutputDebugString("[UDP info]\r\n");
								EdbgOutputDebugString("%B%B ",    FrameBuffer[34], FrameBuffer[35]);
								EdbgOutputDebugString("%B%B\r\n", FrameBuffer[36], FrameBuffer[37]);
								EdbgOutputDebugString("%B%B ",    FrameBuffer[38], FrameBuffer[39]);
								EdbgOutputDebugString("%B%B\r\n", FrameBuffer[40], FrameBuffer[41]);
								EdbgOutputDebugString("%B%B ",    FrameBuffer[42], FrameBuffer[43]);
								EdbgOutputDebugString("%B%B\r\n", FrameBuffer[44], FrameBuffer[45]);
							}
						}
#endif
                        switch (dwIPState) {
                        case IPSTATE_NONE:
                            if ((wDestPort == DHCP_CLIENT_PORT) && (wSrcPort == DHCP_SERVER_PORT)) {
                                EbootProcessDHCP(
                                    &MyAddr,
                                    &dwSubnetMask,
                                    (BYTE *)pwUDPData,
                                    wUDPDataLen,
                                    &DHCPLeaseTime,
                                    &fGotIP);
                                if (fGotIP) {
                                    dwIPState = IPSTATE_GOTIP;
                                }
                            }
                            break;

                        case IPSTATE_ARPED:
                            pCfgData = NULL;
                            if (EbootProcessEDBG(&MyAddr,&EshellHostAddr,FrameBuffer,pwUDPData, wUDPDataLen, &fGotJumpimg, &pCfgData)) {
                                if (pCfgData) {

                                    if (pCfgData->Flags & EDBG_FL_DBGMSG) {
                                        EdbgOutputDebugString("Enabling debug messages over Ethernet, IP: %s, port:%u\n",
                                                              inet_ntoa(pCfgData->DbgMsgIPAddr),ntohs(pCfgData->DbgMsgPort));
                                        memcpy(&pDriverGlobals->eth.DbgHostAddr.wMAC,&EshellHostAddr.wMAC,6);
                                        pDriverGlobals->eth.DbgHostAddr.dwIP  = pCfgData->DbgMsgIPAddr;
                                        pDriverGlobals->eth.DbgHostAddr.wPort = pCfgData->DbgMsgPort;
                                    }
                                    if (pCfgData->Flags & EDBG_FL_PPSH) {
                                        EdbgOutputDebugString("Enabling CESH over Ethernet,           IP: %s, port:%u\n",
                                                              inet_ntoa(pCfgData->PpshIPAddr),ntohs(pCfgData->PpshPort));
                                        memcpy(&pDriverGlobals->eth.PpshHostAddr.wMAC,&EshellHostAddr.wMAC,6);
                                        pDriverGlobals->eth.PpshHostAddr.dwIP  = pCfgData->PpshIPAddr;
                                        pDriverGlobals->eth.PpshHostAddr.wPort = pCfgData->PpshPort;
                                    }
                                    if (pCfgData->Flags & EDBG_FL_KDBG) {
                                        EdbgOutputDebugString("Enabling KDBG over Ethernet,           IP: %s, port:%u\n",
                                                              inet_ntoa(pCfgData->KdbgIPAddr),ntohs(pCfgData->KdbgPort));
                                        memcpy(&pDriverGlobals->eth.KdbgHostAddr.wMAC,&EshellHostAddr.wMAC,6);
                                        pDriverGlobals->eth.KdbgHostAddr.dwIP  = pCfgData->KdbgIPAddr;
                                        pDriverGlobals->eth.KdbgHostAddr.wPort = pCfgData->KdbgPort;
                                    }
                                    pDriverGlobals->eth.etherFlags = pCfgData->Flags;
                                }
                            }

⌨️ 快捷键说明

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