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

📄 blcommon.c

📁 wince 下的bsp测试wince_bspSMDK2440_L35T32.rar
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// 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 LICENSE.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>

#ifdef SIMULATOR
extern void CleanExit(DWORD dwExitCode);
#define SPIN_FOREVER        CleanExit(42)
#else
#define SPIN_FOREVER        while (1)
#endif

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

static BOOL KernelRelocate (ROMHDR *const pTOC);
static BOOL DownloadImage (LPDWORD pdwImageStart, LPDWORD pdwImageLength, LPDWORD pdwLaunchAddr);
static BOOL IsKernelRegion(DWORD dwRegionStart, DWORD dwRegionLength);

#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"
};

DWORD g_dwROMOffset;

PFN_OEMVERIFYMEMORY    g_pOEMVerifyMemory;
PFN_OEMREPORTERROR     g_pOEMReportError;
PFN_OEMCHECKSIGNATURE  g_pOEMCheckSignature;
PFN_OEMMULTIBINNOTIFY    g_pOEMMultiBINNotify;


static void HALT (DWORD dwReason)
{
    if (g_pOEMReportError) {
        g_pOEMReportError (dwReason, 0);
    }
    SPIN_FOREVER;
}


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;
    BOOL bDownloaded = FALSE;
#ifndef SIMULATOR
    // relocate globals to RAM
    if (!KernelRelocate (pTOC)) {
        // spin forever
        HALT (BLERR_KERNELRELOCATE);
    }
#endif
	EdbgOutputDebugString ("1!\n");

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

	EdbgOutputDebugString ("2!\n");
    // output banner
    EdbgOutputDebugString (NKSignon, CURRENT_VERSION_MAJOR, CURRENT_VERSION_MINOR);

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

    // 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)) {
            // error already reported in DownloadImage
            SPIN_FOREVER;
        }
        bDownloaded = TRUE;

		if (dwImageStart) {
			// Check for pTOC signature ("CECE") here, after image in place
			if (*(LPDWORD) OEMMapMemAddr (dwImageStart, dwImageStart + ROM_SIGNATURE_OFFSET) == ROM_SIGNATURE) {
				EdbgOutputDebugString("Found pTOC signature.\n");
			} else {
				EdbgOutputDebugString ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n");
				EdbgOutputDebugString ("! ERROR: Did not find pTOC signature.  ABORTING. !\r\n");
				EdbgOutputDebugString ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n");

				// If no signature, we're going to fail anyway, so loop forever
				HALT (BLERR_SIGNATURE);
			}

			dwpToc = *(LPDWORD) OEMMapMemAddr (dwImageStart, dwImageStart + ROM_SIGNATURE_OFFSET + sizeof(ULONG));
			// 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 + ROM_SIGNATURE_OFFSET + sizeof (DWORD)); // right after signature
			EdbgOutputDebugString ("RomHdr.ulRAMStart=%Xh RomHdr.physfirst=%Xh.\r\n", romhdr.ulRAMStart, romhdr.physfirst);
		}

        // fall through
    case BL_JUMP:
        // Before jumping to the image, optionally check the image signature.
        if (g_pOEMCheckSignature && dwImageStart)
        {
            if (!g_pOEMCheckSignature(dwImageStart, g_dwROMOffset, dwLaunchAddr, bDownloaded))
                HALT(BLERR_WHQL_SIGNATURE);
        }
        // (5) final call to launch the image. never returned
        OEMLaunch (dwImageStart, dwImageLength, dwLaunchAddr, pRomHdr);
        // should never return
        // fall through
    default:
        // ERROR! spin forever
        HALT (BLERR_INVALIDCMD);
    }
}


//
// 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 ++;

    if (dwCRC != dwChksum)
        EdbgOutputDebugString ("ERROR: Checksum failure (expected=0x%x  computed=0x%x)\r\n", dwChksum, dwCRC);

    return dwCRC == dwChksum;
}

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

	*pdwImageStart = *pdwImageLength = *pdwLaunchAddr = 0;

	do
	{
		// read the 7 byte "magic number"
		if (!OEMReadData (BL_HDRSIG_SIZE, hdr)) {
			EdbgOutputDebugString ("\r\nUnable to read image signature.\r\n");
			HALT (BLERR_MAGIC);
			return FALSE;
		}

		// check for multi-bin information packet.

⌨️ 快捷键说明

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