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

📄 blcommon.c

📁 Windows CE 6.0 BSP for VOIP sample phone. Intel PXA270 platform.
💻 C
📖 第 1 页 / 共 4 页
字号:
        }

        // Look for ROMHDR to compute ROM offset.  NOTE: romimage guarantees that the record containing
        // the TOC signature and pointer will always come before the record that contains the ROMHDR contents.
        //
        if (dwRecLen == sizeof(ROMHDR) && (*(LPDWORD) OEMMapMemAddr(pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionStart + ROM_SIGNATURE_OFFSET) == ROM_SIGNATURE))
        {
            DWORD dwTempOffset = (dwRecAddr - *(LPDWORD)OEMMapMemAddr(pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionStart + ROM_SIGNATURE_OFFSET + sizeof(ULONG)));
            ROMHDR *pROMHdr = (ROMHDR *)lpDest;

            // Check to make sure this record really contains the ROMHDR.
            //
            if ((pROMHdr->physfirst == (pCurDownloadFile->dwRegionStart - dwTempOffset)) &&
                (pROMHdr->physlast  == (pCurDownloadFile->dwRegionStart - dwTempOffset + pCurDownloadFile->dwRegionLength)) &&
                (DWORD)(HIWORD(pROMHdr->dllfirst << 16) <= pROMHdr->dlllast) &&
                (DWORD)(LOWORD(pROMHdr->dllfirst << 16) <= pROMHdr->dlllast))
            {
                g_dwROMOffset = dwTempOffset;
                KITLOutputDebugString("rom_offset=0x%x.\r\n", g_dwROMOffset); 
            }
        }

        // verify partial checksum
        OEMShowProgress (dwRecNum++);

        if (fIsFlash)
        {
            OEMContinueEraseFlash ();
        }
    }  // while( records remaining )
    

    //------------------------------------------------------------------------
    //  Determine the image entry point
    //------------------------------------------------------------------------

    // Does this .bin file contain a TOC?
    if (*(LPDWORD) OEMMapMemAddr(pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionStart + ROM_SIGNATURE_OFFSET) == ROM_SIGNATURE)
    {
        // Contain the kernel?
        if (IsKernelRegion(pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionLength))
        {
            *pdwImageStart  = pCurDownloadFile->dwRegionStart;
            *pdwImageLength = pCurDownloadFile->dwRegionLength;
            *pdwLaunchAddr  = dwRecLen;
        }
    }
    // No TOC - not made by romimage.  
    else if (g_DownloadManifest.dwNumRegions == 1)
    {
        *pdwImageStart  = pCurDownloadFile->dwRegionStart;
        *pdwImageLength = pCurDownloadFile->dwRegionLength;
        *pdwLaunchAddr  = dwRecLen;
    }
    else
    {
        // If we're downloading more than one .bin file, it's probably 
        // chain.bin which doesn't have a TOC (and which isn't
        // going to be downloaded on its own) and we should ignore it.
    }


    if (fIsFlash)
    {
        // finish the flash erase
        if (!OEMFinishEraseFlash())
        {
            HALT (BLERR_FLASH_ERASE);
            return (FALSE);
        }

    }

    KITLOutputDebugString("ImageStart = 0x%x, ImageLength = 0x%x, LaunchAddr = 0x%x\r\n",
        *pdwImageStart, *pdwImageLength, *pdwLaunchAddr);

    return TRUE;
}


static BOOL DownloadNB0 (LPDWORD pdwImageStart, LPDWORD pdwImageLength, LPDWORD pdwLaunchAddr)
{
    RegionInfo *pCurDownloadFile;
    BOOL        fIsFlash = FALSE;
    LPBYTE      lpDest = NULL;

    g_bBINDownload = FALSE;


    // Provide the download manifest to the OEM.  This gives the OEM the
    // opportunity to provide start addresses for the .nb0 files (which 
    // don't contain placement information like .bin files do).
    if (!g_fOEMNotified && g_pOEMMultiBINNotify)
    {
        g_pOEMMultiBINNotify((PDownloadManifest)&g_DownloadManifest);
        g_fOEMNotified = TRUE;
    }

    // Locate the current download manifest entry (current download file).
    //
    pCurDownloadFile = &g_DownloadManifest.Region[g_DownloadManifest.dwNumRegions - g_downloadFilesRemaining];

    // give the OEM a chance to verify memory
    if (g_pOEMVerifyMemory && !g_pOEMVerifyMemory (pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionLength))
    {
        KITLOutputDebugString ("!OEMVERIFYMEMORY: Invalid image\r\n");
        HALT (BLERR_OEMVERIFY);
        return (FALSE);
    }

    // check for flash image. Start erasing if it is.
    if ((fIsFlash = OEMIsFlashAddr (pCurDownloadFile->dwRegionStart)) 
        && !OEMStartEraseFlash (pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionLength))
    {
        KITLOutputDebugString ("Invalid flash address/length\r\n");
        HALT (BLERR_FLASHADDR);
        return (FALSE);
    }


    //------------------------------------------------------------------------
    //  Download the file
    //
    //  If we're downloading an UNSIGNED .nb0 file, we've already read the 
    //  start of the file in GetImageType().
    //  Copy what we've read so far to the destination, then finish downloading.
    //------------------------------------------------------------------------
    lpDest = OEMMapMemAddr (pCurDownloadFile->dwRegionStart, pCurDownloadFile->dwRegionStart);
    memcpy(lpDest, g_hdr, BL_HDRSIG_SIZE);
    lpDest += BL_HDRSIG_SIZE;

    if (!OEMReadData ((pCurDownloadFile->dwRegionLength - BL_HDRSIG_SIZE), lpDest))
    {
        KITLOutputDebugString ("ERROR: failed when reading raw binary file.\r\n");
        HALT (BLERR_CORRUPTED_DATA);
        return (FALSE);
    }


    //------------------------------------------------------------------------
    //  Determine the image entry point
    //------------------------------------------------------------------------

    *pdwImageStart  = pCurDownloadFile->dwRegionStart;
    *pdwLaunchAddr  = pCurDownloadFile->dwRegionStart;
    *pdwImageLength = pCurDownloadFile->dwRegionLength;


    if (fIsFlash)
    {
        // finish the flash erase
        if (!OEMFinishEraseFlash())
        {
            HALT (BLERR_FLASH_ERASE);
            return (FALSE);
        }
    }

    KITLOutputDebugString("ImageStart = 0x%x, ImageLength = 0x%x, LaunchAddr = 0x%x\r\n",
        *pdwImageStart, *pdwImageLength, *pdwLaunchAddr);

    return TRUE;
}


static BOOL WriteImageToFlash()
{
    BOOL  bFlash;
    DWORD i;
    
    KITLOutputDebugString("\r\nCompleted file(s):\r\n");
    KITLOutputDebugString("-------------------------------------------------------------------------------\r\n");

    for (i = 0; i < g_DownloadManifest.dwNumRegions; i++)
    {
        RegionInfo *pRegion = &g_DownloadManifest.Region[i];

        bFlash = OEMIsFlashAddr( pRegion->dwRegionStart );
        
        KITLOutputDebugString("[%d]: Address=0x%x  Length=0x%x  Name=\"%s\" Target=%s\r\n",
            i, 
            pRegion->dwRegionStart, 
            pRegion->dwRegionLength, 
            pRegion->szFileName,
            (bFlash ? "FLASH" : "RAM"));

        if (bFlash) 
        {
            if (!OEMWriteFlash (pRegion->dwRegionStart, pRegion->dwRegionLength))
            {
                HALT (BLERR_FLASH_WRITE);
                return FALSE;
            }
        }
    }

    return TRUE;    
}


#ifdef SECURE_BOOTLOADER
VOID PrintPacketData( PACKETDATA packetData ) 
{
#ifdef DEBUG
    DWORD i;
    
    KITLOutputDebugString("packetData.pbData        = 0x%x\r\n", packetData.pbData);
    
    KITLOutputDebugString("pbData = [");
    for (i = 0; i < packetData.dwDataLength; i++) {
        KITLOutputDebugString("%x ", packetData.pbData[i]);   
        
        if (20 < i && i < packetData.dwDataLength) {
            KITLOutputDebugString("...");
            break;
        }
    }
    KITLOutputDebugString("]\r\n\r\n");
    
    KITLOutputDebugString("packetData.dwDataLength  = 0x%x\r\n", packetData.dwDataLength);
    KITLOutputDebugString("packetData.pbSig         = 0x%x\r\n", packetData.pbSig       );
    KITLOutputDebugString("packetData.dwSigLength   = 0x%x\r\n", packetData.dwSigLength );
    KITLOutputDebugString("packetData.dwRecAddress  = 0x%x\r\n", packetData.dwRecAddress);
    KITLOutputDebugString("packetData.dwRecLength   = 0x%x\r\n", packetData.dwRecLength );
    KITLOutputDebugString("packetData.dwRecCheck    = 0x%x\r\n", packetData.dwRecCheck  );
    KITLOutputDebugString("packetData.dwSeqNum      = 0x%x\r\n", packetData.dwSequenceNumber);
    KITLOutputDebugString("packetData.bFlags        = 0x%x\r\n", packetData.bFlags      );
    KITLOutputDebugString("\r\n");
    
    KITLOutputDebugString("Signature = [");
    for (i = 0; i < sizeof(g_rgpbSignature); i++) {
        KITLOutputDebugString("%x ", g_rgpbSignature[i]);   
    }
    KITLOutputDebugString("]\r\n\r\n");
#endif
}
#endif



static VOID ComputeChecksum()
{
#ifdef DEBUG
    RegionInfo *pRegion;
    DWORD       dwRegionLength;
    DWORD       dwChecksum;
    BYTE       *pbCache;
    DWORD       i;
    
    for (i = 0; i < g_DownloadManifest.dwNumRegions; i++)
    {
        pRegion         = &g_DownloadManifest.Region[i];
        pbCache         = (LPBYTE) OEMMapMemAddr( pRegion->dwRegionStart, pRegion->dwRegionStart );
        dwRegionLength  = pRegion->dwRegionLength;
        dwChecksum      = 0;

        KITLOutputDebugString("Computing checksum: image start = 0x%x, len = 0x%x\r\n", 
            pbCache, dwRegionLength);

        while(dwRegionLength--) {
            dwChecksum += *pbCache++;
        }

        KITLOutputDebugString("Checksum = 0x%x (0x%x bytes)\r\n", dwChecksum, pRegion->dwRegionLength);
    }
#endif
}


/*
    @func   BOOLEAN | IsKernelRegion | Determines if the expanded BIN file provided contains the kernel image.
    @rdesc  TRUE if the region contains the kernel image, FALSE if it doesn't.
    @comm   <l Download Image> 
    @xref   
    @notes  dwCurrentBase is the base address where the BIN records are currently stored (this can be a RAM, a RAM
            file cache, or flash).  dwImageStoreBase is the images base storage address (this is the base address of potentially
            multiple BIN regions and can be in RAM or flash) and is used to translate addresses to the file cache area.
            dwROMOffset is the difference between the address where the BIN records are stored versus where they're fixed-up
            to run from (for example, an image may be stored in flash, but fixed-up to run in RAM).
*/
static BOOL IsKernelRegion(DWORD dwRegionStart, DWORD dwRegionLength)
{
	DWORD dwCacheAddress = 0;
	ROMHDR *pROMHeader;
	DWORD dwNumModules = 0;
	TOCentry *plTOC;

    if (dwRegionStart == 0 || dwRegionLength == 0)
        return(FALSE);

    if (*(LPDWORD) OEMMapMemAddr (dwRegionStart, dwRegionStart + ROM_SIGNATURE_OFFSET) != ROM_SIGNATURE)
        return (FALSE);

    // A pointer to the ROMHDR structure lives just past the ROM_SIGNATURE (which is a longword value).  Note that
    // this pointer is remapped since it might be a flash address (image destined for flash), but is actually cached
    // in RAM.
    //
    dwCacheAddress = *(LPDWORD) OEMMapMemAddr (dwRegionStart, dwRegionStart + ROM_SIGNATURE_OFFSET + sizeof(ULONG));
    pROMHeader     = (ROMHDR *) OEMMapMemAddr (dwRegionStart, dwCacheAddress + g_dwROMOffset);

    // Make sure sure are some modules in the table of contents.
    //
    if ((dwNumModules = pROMHeader->nummods) == 0)
        return (FALSE);

	// Locate the table of contents and search for the kernel executable and the TOC immediately follows the ROMHDR.
	//
    plTOC = (TOCentry *)(pROMHeader + 1);

	while(dwNumModules--) {
		LPBYTE pFileName = OEMMapMemAddr(dwRegionStart, (DWORD)plTOC->lpszFileName + g_dwROMOffset);
		if (!strcmp(pFileName, "nk.exe")) {
			return TRUE;
		}
		++plTOC;
	}
	return FALSE;
}



char Nib2HexChar (BYTE Nibble)
{
    if (Nibble < 0x0a) {
		return Nibble+'0';
    } else if (Nibble < 0x10) {
		return Nibble-0x0a+'A';
    } else {
		return '?';
    }
}


VOID DumpMem (PBYTE Ptr, ULONG Len)
{
#ifdef DEBUG
	ULONG i,j;
	char OutString[256];
	int Index;

    KITLOutputDebugString("0x%x - 0x%x\r\n", Ptr, Ptr + Len);
	for (j=0; j < Len; j+= 16) {
		Index = 0;
		// Print out a leader
        KITLOutputDebugString("0x%x ", j);
        Index = 0;        
		
		for (i=0; i < 16; i++) {
			if (i+j < Len) {
				OutString[Index++] = Nib2HexChar ((BYTE)(Ptr[i+j]>>4));
				OutString[Index++] = Nib2HexChar ((BYTE)(Ptr[i+j] & 0x0f));
			} else {
				OutString[Index++] = ' ';
				OutString[Index++] = ' ';
			}
			if (i == 7) {
				OutString[Index++] = '-';
			} else {
				OutString[Index++] = ' ';
			}
		}
		for (i=0; i < 2; i++)
			OutString[Index++] = ' ';

		for (i=0; i < 16; i++) {
			if ((i+j) >=  Len) {
				OutString[Index++] = ' ';
			} else if ((Ptr[i+j] < ' ') || (Ptr[i+j] >= 0x7f)) {
				OutString[Index++] = '.';
			} else {
				OutString[Index++] = (CHAR)Ptr[i+j];
			}
		}
		OutString[Index] = '\0';
		KITLOutputDebugString("%s\r\n", OutString);
	}
#endif DEBUG	
}

⌨️ 快捷键说明

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