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

📄 blcommon.c

📁 ebd9307开发板wince bsp源码,包括cs8900,lcd,nand,serial,touch,usb,gpio,wd等驱动
💻 C
字号:
//**********************************************************************
//                                                                      
// Filename: blcommon.c
//                                                                      
// Description: Bootloader common main module.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Use of this source code is subject to the terms of the Cirrus end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to 
// use this source code. For a copy of the EULA, please see the 
// EULA.RTF on your install media.
//
// Copyright(c) Cirrus Logic Corporation 2005, All Rights Reserved                       
//                                                                      
//**********************************************************************


//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the EULA.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:  
    blcommon.c
    
Abstract:  
    Bootloader common main module. This file contains the C BootloaderMain
    function for the boot loader.    NOTE: The firmware "entry" point (the real
    entry point is _EntryPoint in init assembler file.

    The Windows CE boot loader is the code that is executed on a Windows CE
    development system at power-on reset and loads the Windows CE
    operating system. The boot loader also provides code that monitors
    the behavior of a Windows CE platform between the time the boot loader
    starts running and the time the full operating system debugger is 
    available. Windows CE OEMs are supplied with sample boot loader code 
    that runs on a particular development platform and CPU.
    
Functions:


Notes: 

--*/
#include <windows.h>
#include <halether.h>
#include "blcommon.h"

typedef VOID (*PFN_LAUNCH)(VOID);

#define SPIN_FOREVER        while (1)
#define PTOC_SIG_OFFSET     64          // pTOC signature at offset 64 of the image
#define PTOC_OFFSET         68          // pTOC address is at offset 68
#define PTOC_SIG            0x43454345  // signature for pTOC structure ("CECE")


ROMHDR * volatile const pTOC = (ROMHDR *)-1;     // Gets replaced by RomLoader with real address

static BOOL KernelRelocate (ROMHDR *const pTOC);
static BOOL DownloadImage (LPDWORD pdwImageStart, LPDWORD pdwImageLength, LPDWORD pdwLaunchAddr);
extern BOOL bEthLaunch;

#define CURRENT_VERSION_MAJOR       1
#define CURRENT_VERSION_MINOR       0

const unsigned char NKSignon[] = {
    "\nMicrosoft Windows CE Ethernet Bootloader Common Library Version %d.%d Built "
        __DATE__ " " __TIME__ "\r\n"
    "Copyright (c) 2000-2001  Microsoft Corporation\r\n"
};

PFN_OEMVERIFYMEMORY g_pOEMVerifyMemory;

static ROMHDR romhdr;
void BootloaderMain (void)
{
    ROMHDR *pRomHdr = NULL;  // pTOC for NK image. MUST COPY IT OR CLEANBOOT may erase it
    DWORD dwAction, dwpToc;   
    DWORD dwImageStart = 0, dwImageLength = 0, dwLaunchAddr = 0;

    // relocate globals to RAM
    if (!KernelRelocate (pTOC)) {
        // spin forever
        SPIN_FOREVER;
    }

    // (1) Init debug support. We can use OEMWriteDebugString afterward.
    if (!OEMDebugInit ()) {
        // spin forever
        SPIN_FOREVER;
    }

    // output banner
    EdbgOutputDebugString (NKSignon, CURRENT_VERSION_MAJOR, CURRENT_VERSION_MINOR);

    // (3) initialize platform (clock, drivers, transports, etc)
    if (!OEMPlatformInit ()) {
        // spin forever
        SPIN_FOREVER;
    }

    // system ready, preparing for download
    EdbgOutputDebugString ("System ready!\r\nPreparing for download...\r\n");

    // (4) call OEM specific pre-download function
    switch (dwAction = OEMPreDownload ()) {
    case BL_DOWNLOAD:
        // (5) download image
        if (!DownloadImage (&dwImageStart, &dwImageLength, &dwLaunchAddr)) {
            SPIN_FOREVER;
        }
        // Check for pTOC signature ("CECE") here, after image in place
        if (*(LPDWORD) OEMMapMemAddr (dwImageStart, dwImageStart + PTOC_SIG_OFFSET) == PTOC_SIG) 
        {
            EdbgOutputDebugString("Found pTOC signature.\n");
            dwpToc = *(LPDWORD) OEMMapMemAddr (dwImageStart, dwImageStart + PTOC_OFFSET);
            //
            // need to map the content again since the pointer is going to be in a fixup address
            ///
            dwpToc = (DWORD) OEMMapMemAddr (dwImageStart, dwpToc);


            //
            // NOTE: MUST COPY or a CLEAN_BOOT flag will erase it
            //
            memcpy (pRomHdr = &romhdr, (LPVOID) dwpToc, sizeof(ROMHDR));

            EdbgOutputDebugString("ROMHDR at Address %Xh\r\n", dwImageStart + PTOC_SIG_OFFSET + sizeof (DWORD)); // right after signature
            EdbgOutputDebugString("RomHdr.ulRAMStart=%Xh RomHdr.physfirst=%Xh.\r\n", romhdr.ulRAMStart, romhdr.physfirst);

        } else 
        {
            EdbgOutputDebugString("! Did not Find Windows CE pTOC signature.!\n");
            //
            // In my image the rom header is not used anyway.
            //
            EdbgOutputDebugString("INFO: Jumping to image at 0x%X...\r\n", dwLaunchAddr);
            bEthLaunch =FALSE;

        }


        // fall through
    case BL_JUMP:
        // (5) final call to launch the image. never returned
        OEMLaunch (dwImageStart, dwImageLength, dwLaunchAddr, pRomHdr);
        // should never return
        // fall through
    default:
        // ERROR! spin forever
        SPIN_FOREVER;
    }
}


//
// KernelRelocate: move global variables to RAM
//
static BOOL KernelRelocate (ROMHDR *const pTOC)
{
    ULONG loop;
    COPYentry *cptr;
    if (pTOC == (ROMHDR *const) -1) {
        return FALSE; // spin forever!
    }
    // This is where the data sections become valid... don't read globals until after this
    for (loop = 0; loop < pTOC->ulCopyEntries; loop++) {
        cptr = (COPYentry *)(pTOC->ulCopyOffset + loop*sizeof(COPYentry));
        if (cptr->ulCopyLen)
            memcpy((LPVOID)cptr->ulDest,(LPVOID)cptr->ulSource,cptr->ulCopyLen);
        if (cptr->ulCopyLen != cptr->ulDestLen)
            memset((LPVOID)(cptr->ulDest+cptr->ulCopyLen),0,cptr->ulDestLen-cptr->ulCopyLen);
    }
    return TRUE;
}

static BOOL VerifyChecksum (DWORD cbRecord, LPBYTE pbRecord, DWORD dwChksum)
{
    // Check the CRC
    DWORD dwCRC = 0;
    DWORD i;
    for (i = 0; i < cbRecord; i++)
        dwCRC += *pbRecord ++;
    return dwCRC == dwChksum;
}

#define BINHDRSIZE      7
static BOOL DownloadImage (LPDWORD pdwImageStart, LPDWORD pdwImageLength, LPDWORD pdwLaunchAddr)
{
    BYTE hdr[BINHDRSIZE];
    DWORD dwRecLen, dwRecChk, dwRecAddr;
    BOOL fIsFlash;
    LPBYTE lpDest;
    int nPkgNum = 0;

    // read the 7 byte "magic number"
    if (!OEMReadData (BINHDRSIZE, hdr)
        || memcmp (hdr, "B000FF\x0A", BINHDRSIZE)) {
        EdbgOutputDebugString ("\r\nThis is not a .BIN file %x %x %x %x %x %x %x\r\n",
            hdr[0], hdr[1], hdr[2], hdr[3], hdr[4], hdr[5], hdr[6]);
        return FALSE;
    }

    // read image start/length
    if (!OEMReadData (sizeof (DWORD), (LPBYTE) pdwImageStart)
        || !OEMReadData (sizeof (DWORD), (LPBYTE) pdwImageLength)) {
        EdbgOutputDebugString ("Unable to read image start/length\r\n");
        return FALSE;
    }

    // give the OEM a chance to verify memory
    if (g_pOEMVerifyMemory && !g_pOEMVerifyMemory (*pdwImageStart, *pdwImageLength)) {
        EdbgOutputDebugString ("!OEMVERIFYMEMORY: Invalid image\r\n");
        return FALSE;
    }

    // check for flash image. Start erasing if it is.
    if ((fIsFlash = OEMIsFlashAddr (*pdwImageStart)) 
        && !OEMStartEraseFlash (*pdwImageStart, *pdwImageLength)) {
        EdbgOutputDebugString ("Invalid Flash Address/Length\r\n");
        return FALSE;
    }

    // read records (start with address, length, and checksum)
    while (OEMReadData (sizeof (DWORD), (LPBYTE) &dwRecAddr)
        && OEMReadData (sizeof (DWORD), (LPBYTE) &dwRecLen)
        && OEMReadData (sizeof (DWORD), (LPBYTE) &dwRecChk)) {

        // check for last record
        if (!dwRecAddr && !dwRecChk) {

            // update launch address
            *pdwLaunchAddr = dwRecLen;

            // write to flash if it's flash image
            if (fIsFlash) {
                // finish the flash erase
                if (!OEMFinishEraseFlash ()) {
                    return FALSE;
                }
                if (!OEMWriteFlash (*pdwImageStart, *pdwImageLength)) {
                    return FALSE;
                }
            }
            return TRUE;
        }

        // map the record address (FLASH data is cached, for example)
        lpDest = OEMMapMemAddr (*pdwImageStart, dwRecAddr);

        // read data block
        if (!OEMReadData (dwRecLen, lpDest)) {
            EdbgOutputDebugString ("****** Data record %d corrupted, ABORT!!! ******\r\n", nPkgNum);
            return FALSE;
        }

        if (!VerifyChecksum (dwRecLen, lpDest, dwRecChk)) {
            EdbgOutputDebugString ("****** Checksum failure on record %d, ABORT!!! ******\r\n", nPkgNum);
            return FALSE;
        }
        // verify partial checksum
        OEMShowProgress (nPkgNum ++);
		EdbgOutputDebugString(".");
        if (fIsFlash) {
            OEMContinueEraseFlash ();
        }
    }

    return FALSE;
}

⌨️ 快捷键说明

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