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

📄 main.c

📁 WINDOWS CE BSP用于SBC2440开发板
💻 C
📖 第 1 页 / 共 3 页
字号:
    //
#ifndef SIMULATOR
    if ( g_bDownloadImage && g_bWaitForConnect )
    {
        EdbgOutputDebugString ("EbootWaitForHostConenct\r\n");
        pCfgData = EbootWaitForHostConnect (&g_pBootCfg->EdbgAddr, &EshellHostAddr);
        if (!pCfgData) {
            EdbgOutputDebugString ("EbootWaitForHostConenct failed, spin forever\r\n");
            SPIN_FOREVER;
        }
        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;
        }

        memcpy(&pDriverGlobals->eth.DownloadHostAddr,&EshellHostAddr,sizeof(EDBG_ADDR));
        pDriverGlobals->eth.etherFlags = pCfgData->Flags;
        pDriverGlobals->eth.KitlTransport = pCfgData->KitlTransport;
        EdbgOutputDebugString ("KitlTransport: 0x%x\r\n", pCfgData->KitlTransport);
        pDriverGlobals->eth.EbootMagicNum = EBOOT_MAGIC_NUM;
    }
    else if ( !g_bDownloadImage && g_bWaitForConnect)
    {
        EdbgOutputDebugString ("Eboot setup Kitl from media boot\r\n");
        pDriverGlobals->eth.KitlTransport = KTS_ETHER;
        pDriverGlobals->eth.EbootMagicNum = EBOOT_MAGIC_NUM;
    }
#endif

    // Update address info, in driver globals, and in EEPROM if necessary
    memcpy (&pDriverGlobals->eth.TargetAddr, &g_pBootCfg->EdbgAddr, sizeof(g_pBootCfg->EdbgAddr));
    pDriverGlobals->eth.SubnetMask = g_pBootCfg->SubnetMask;

    // If the user requested an image be stored on media, do so now.  For multiple RAM BIN files, we need to map
    // its RAM address to a flash address - the image base address offset in RAM is maintained in flash.
    //

    // Remember kernel launch address or recall stored address if this download didn't provide one
    // (i.e., we didn't download the kernel region).
    if (g_bDownloadImage && (g_pBootCfg->ConfigFlags & TARGET_TYPE_NAND))
    {
		if (dwImageStart && dwImageLength)
		{
			g_pTOC->id[g_dwTocEntry].dwLoadAddress = dwImageStart;
			g_pTOC->id[g_dwTocEntry].dwTtlSectors = FILE_TO_SECTOR_SIZE(dwImageLength);
		}

        switch ( g_ImageType ) {
            case IMAGE_TYPE_LOADER:
                EdbgOutputDebugString("OEMLaunch: IMAGE_TYPE_LOADER\r\n");
                if ( !WriteRamImageToBootMedia(g_dwTocEntry) ) {
                    RETAILMSG(1, (TEXT("OEMLaunch ERROR: Failed to write image to boot media.\r\n")));
                    SPIN_FOREVER;
                }
                break;

            case IMAGE_TYPE_RAMIMAGE:
                EdbgOutputDebugString("OEMLaunch: IMAGE_TYPE_RAMIMAGE\r\n");
                if ( !WriteRamImageToBootMedia(g_dwTocEntry) ) {
                    RETAILMSG(1, (TEXT("OEMLaunch ERROR: Failed to write image to boot media.\r\n")));
                    SPIN_FOREVER;
                }
                break;

            case (IMAGE_TYPE_RAMIMAGE|IMAGE_TYPE_BINFS|IMAGE_TYPE_MXIP):
            case (IMAGE_TYPE_RAMIMAGE|IMAGE_TYPE_BINFS):
                EdbgOutputDebugString("OEMLaunch: (%s|IMAGE_TYPE_BINFS)\r\n",
                    (g_ImageType & IMAGE_TYPE_MXIP) ? "IMAGE_TYPE_MXIP" : "IMAGE_TYPE_RAMIMAGE");
                if ( !WriteRegionsToBootMedia(dwImageStart, dwImageLength, dwLaunchAddr) ) {
                    EdbgOutputDebugString("WARNING: OEMLaunch: Failed to store BinFS regions to boot media.\r\n");
                    SPIN_FOREVER;
                }
                break;

            default:
                EdbgOutputDebugString("OEMLaunch ERROR: unknown image type: 0x%x \r\n",
                    g_pTOC->id[g_dwTocEntry].dwImageType);
                SPIN_FOREVER;
        }
    }

	// Remember kernel launch address or recall stored address if this download didn't provide one (i.e., we didn't download the kernel region).
	//
	if (dwLaunchAddr && (g_pTOC->id[g_dwTocEntry].dwJumpAddress != dwLaunchAddr))
	{
		g_pTOC->id[g_dwTocEntry].dwJumpAddress = dwLaunchAddr;
		if ( !TOC_Write() ) {
            EdbgOutputDebugString("*** OEMLaunch ERROR: TOC_Write failed! Next boot may not load from disk *** \r\n");
		}
        TOC_Print();
	}
	else
	{
		dwLaunchAddr= g_pTOC->id[g_dwTocEntry].dwJumpAddress;
		EdbgOutputDebugString("INFO: using TOC[%d] dwJumpAddress: 0x%x\r\n", g_dwTocEntry, dwLaunchAddr);
	}

    EdbgOutputDebugString("\r\nJumping to image at virtual address 0x%Xh\r\n", dwLaunchAddr);

    //  Our Launch function takes Physical address, so we need to convert it
    //  to physical address

    dwLaunchAddr = ToPhysicalAddr(dwLaunchAddr);
    EdbgOutputDebugString("\r\n::: Physical Launch Address: 0x%Xh\r\n",dwLaunchAddr);

    Launch(dwLaunchAddr);

    // never returned
    SPIN_FOREVER;
}


/*
    @func   BOOL | OEMReadData | Generically read download data (abstracts actual transport read call).
    @rdesc  TRUE = Success, FALSE = Failure.
    @comm
    @xref
*/
BOOL OEMReadData (DWORD cbData, LPBYTE pbData)
{
	if ( g_bUSBDownload == FALSE )
	{
		return EbootEtherReadData(cbData, pbData);
	}
	else
	{
		return UbootReadData(cbData, pbData);
	}
}


/*
    @func   void | OEMShowProgress | Displays download progress for the user.
    @rdesc  N/A.
    @comm
    @xref
*/
void OEMShowProgress (DWORD dwPacketNum)
{
}


#define BACKSPACE   8
#define IPADDR_MAX  15
// Read IP from command line
static BOOL ReadIPLine (char *pbuf, DWORD dwTimeout)
{
    DWORD dwCurrSec = OEMEthGetSecs ();
    char ch;
    int nLen = 0;
    while (OEMEthGetSecs () - dwCurrSec < dwTimeout) {
        ch = (CHAR)OEMReadDebugByte();
        switch (ch) {
        case OEM_DEBUG_COM_ERROR:
        case OEM_DEBUG_READ_NODATA:
            // no data or error, keep reading
            break;

        case BACKSPACE:
            nLen --;
            OEMWriteDebugByte (ch);
            break;

        case '\r':
        case '\n':
            OEMWriteDebugByte ('\n');
            pbuf[nLen] = 0;
            return TRUE;

        default:
            if ((ch == '.' || (ch >= '0' && ch <= '9')) && (nLen < IPADDR_MAX)) {
                pbuf[nLen ++] = ch;
                OEMWriteDebugByte (ch);
            }
        }
    }

    return FALSE;   // timeout
}


// get users IP / SubnetMask
// return TRUE is changes made to either, else FALSE.
static BOOL GetUserIPAddr (EDBG_ADDR *pMyAddr, DWORD *pdwSubnetMask)
{
    char  szbuf[IPADDR_MAX+1];
    uchar changes = 0;

    memset(szbuf, 0, sizeof(szbuf));

    EdbgOutputDebugString ("\r\nEnter IP address, or CR for default (%s): ", inet_ntoa(pMyAddr->dwIP));
    ReadIPLine (szbuf, INFINITE);
    if (szbuf[0]) {
        pMyAddr->dwIP = inet_addr(szbuf);
        changes++;
        memset(szbuf, 0, sizeof(szbuf));
    }

    EdbgOutputDebugString ("\r\nEnter Subnet Masks, or CR for default (%s): ", inet_ntoa(*pdwSubnetMask));
    ReadIPLine (szbuf, INFINITE);
    if (szbuf[0]) {
        *pdwSubnetMask = inet_addr (szbuf);
        changes++;
    }

    EdbgOutputDebugString ( "\r\nUsing IP Address %s, subnet mask %s\r\n",
                inet_ntoa (pMyAddr->dwIP), inet_ntoa (*pdwSubnetMask));

    return (changes ? TRUE : FALSE);
}

static ULONG mystrtoul(PUCHAR pStr, UCHAR nBase)
{
    UCHAR nPos=0;
    BYTE c;
    ULONG nVal = 0;
    UCHAR nCnt=0;
    ULONG n=0;

    // fulllibc doesn't implement isctype or iswctype, which are needed by
    // strtoul, rather than including coredll code, here's our own simple strtoul.

    if (pStr == NULL)
        return(0);

    for (nPos=0 ; nPos < strlen(pStr) ; nPos++)
    {
//        c = tolower(*(pStr + strlen(pStr) - 1 - nPos));
        c = (*(pStr + strlen(pStr) - 1 - nPos));
        if (c >= '0' && c <= '9')
            c -= '0';
        else if (c >= 'a' && c <= 'f')
        {
            c -= 'a';
            c  = (0xa + c);
        }

        for (nCnt = 0, n = 1 ; nCnt < nPos ; nCnt++)
        {
            n *= nBase;
        }
        nVal += (n * c);
    }

    return(nVal);
}

// since the startup code is in OAL and it branch to main, we'll just
// implement a pseudo 'main' instead of changing the startup code
void main (void)
{
    BootloaderMain ();

    SPIN_FOREVER;
}

VOID SC_WriteDebugLED(WORD wIndex, DWORD dwPattern)
{
    OEMWriteDebugLED(wIndex,dwPattern);
}

/*
    @func   void | SetDelay | Accepts an autoboot delay value from user input.
    @rdesc  N/A.
    @comm    
    @xref   
*/
static void SetDelay()
{
    CHAR szCount[16];
    USHORT cwNumChars = 0;
    USHORT 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';
        g_pBootCfg->BootDelay = atoi(szCount);
        if (g_pBootCfg->BootDelay > 255)
        {
            g_pBootCfg->BootDelay = 255;
        } 
        else if (g_pBootCfg->BootDelay < 1)
        {
            g_pBootCfg->BootDelay = 1;
        }
    }
}



static 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++ = (BYTE)(mystrtoul(pszLastNum, 16) & 0xFF);
        i++;
        pszLastNum = ++pszDottedD;
    }
}



static void SetCS8900MACAddress()
{
    CHAR szDottedD[24];
    USHORT cwNumChars = 0;
    USHORT InChar = 0;

    memset(szDottedD, '0', 24);

    EdbgOutputDebugString ( "\r\nEnter new MAC address in hexadecimal (hh.hh.hh.hh.hh.hh): ");

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

    EdbgOutputDebugString ( "\r\n");

    // If it's a carriage return with an empty string, don't change anything.
    //
    if (cwNumChars) 
    {
        szDottedD[cwNumChars] = '\0';
        CvtMAC(g_pBootCfg->EdbgAddr.wMAC, szDottedD);

        EdbgOutputDebugString("INFO: MAC address set to: %x:%x:%x:%x:%x:%x\r\n",
                  g_pBootCfg->EdbgAddr.wMAC[0] & 0x00FF, g_pBootCfg->EdbgAddr.wMAC[0] >> 8,
                  g_pBootCfg->EdbgAddr.wMAC[1] & 0x00FF, g_pBootCfg->EdbgAddr.wMAC[1] >> 8,
                  g_pBootCfg->EdbgAddr.wMAC[2] & 0x00FF, g_pBootCfg->EdbgAddr.wMAC[2] >> 8);
    }
    else
    {
        EdbgOutputDebugString("WARNING: SetCS8900MACAddress: Invalid MAC address.\r\n");
    }
}

⌨️ 快捷键说明

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