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

📄 ethdown.c

📁 Xcale270Bsp包,wince平台
💻 C
📖 第 1 页 / 共 3 页
字号:
/*++
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.
Copyright (c) 1995, 1996, 1997, 1998  Microsoft Corporation
*/
/* 
** INTEL CONFIDENTIAL
** Copyright 2000-2003 Intel Corporation All Rights Reserved.
**
** The source code contained or described herein and all documents
** related to the source code (Material) are owned by Intel Corporation
** or its suppliers or licensors.  Title to the Material remains with
** Intel Corporation or its suppliers and licensors. The Material contains
** trade secrets and proprietary and confidential information of Intel
** or its suppliers and licensors. The Material is protected by worldwide
** copyright and trade secret laws and treaty provisions. No part of the
** Material may be used, copied, reproduced, modified, published, uploaded,
** posted, transmitted, distributed, or disclosed in any way without Intel抯
** prior express written permission.

** No license under any patent, copyright, trade secret or other intellectual
** property right is granted to or conferred upon you by disclosure or
** delivery of the Materials, either expressly, by implication, inducement,
** estoppel or otherwise. Any license under such intellectual property rights
** must be express and approved by Intel in writing.
*/
/*
Module Name:  
    ethdown.c

Abstract:  
    This contains an example of a registered TFTP server 
    process.  It is the EthDown() routine that handles the download
    of .BIN files to RAM.  

Functions:


Notes: 

--*/
#include <windows.h>
#include <halether.h>
#include <pehdr.h>
#include <romldr.h>

#include "loader.h"
#include "ethdown.h"
#include "bvd1bd.h"



extern void DumpDwords(PDWORD pdw, int len);
extern void OEMWriteDebugByte(unsigned char c);

// Flash type constants
#define J3	1
#define K3	2
#define K18 3
#define L18 4
#define L30 5

// File type constants
#define BIN_FILE_TYPE	1
#define NB0_FILE_TYPE	2
#define BOOTLOADER		4
#define NKBIN			8
#define FLASHTARGET		16
#define SDRAMEXE		32

#ifdef MMXIP_MEMMAP
    #define RAM_IMAGE_START 0x96CB8000
#else
    #define RAM_IMAGE_START 0x800B8000
#endif //MMXIP_MEMMAP
#define FLASH_START		BOOT_FLASH_BASE_U_VIRTUAL   

DWORD dwPhysStart;      // image physical starting address
DWORD dwPhysLen;        // image physical length
DWORD dwOffset;
DWORD dwLaunchAddr;
DWORD v_FlashBlock;
BOOL bFirst=TRUE;
LPDWORD pdwRecStart=0, pdwRecNext=0;    // saved record address, length and checksum pointers
DWORD v_PacketNum=0;
DWORD v_FlashBlock;
DWORD fileType=0;
DWORD dwEBOOT_OFFSET;
int offset = 0;
extern int flashType;

DWORD VerifyCheckSum(void)
{
    DWORD dwRecordLen;                  // Total length of the record being processed
    DWORD dwRecordAddr;                 // starting address of the record
    DWORD dwPerfectCRC;                 // Theoretical CRC over the record being processed
    DWORD dwPartialCRC;                 // Running CRC over the record being processed
    DWORD dwCurDataWord;                // Usually, this is the data word at the current position within the
    BYTE *pbCRC;                        // Pointer used to run through memory to calculate CRC
    DWORD i;
    if (!pdwRecNext)
        return 0;
    EdbgOutputDebugString( "Verify checksums...\r\n");
    *pdwRecNext++=0;
    *pdwRecNext++=0;
    *pdwRecNext++=0;
    pdwRecNext=pdwRecStart;
    do 
    {
        dwRecordAddr=dwCurDataWord=*pdwRecNext++;
        dwRecordLen=*pdwRecNext++;
        dwPerfectCRC=*pdwRecNext++;
        if (dwRecordAddr && dwPerfectCRC)
        {
            dwCurDataWord-= dwOffset;
            pbCRC = (BYTE *)dwCurDataWord;

            // Check the CRC
            dwPartialCRC = 0;
            for ( i = 0; i < dwRecordLen; i++ )
                dwPartialCRC += *pbCRC++;
            if (dwPartialCRC != dwPerfectCRC)
            {
                EdbgOutputDebugString( "Checksum Error Addr %Xh Len %Xh Expected %Xh Calculated %Xh\r\n",
                                       dwRecordAddr, dwRecordLen, dwPerfectCRC, dwPartialCRC);
                return 1;
            }
        }
    } while (dwRecordAddr && dwPerfectCRC);
    EdbgOutputDebugString( "Checksums verified correct.\r\n");
    return 0;
}

UINT16 EthDown( char *pszFileName, TFtpdCallBackOps Operation, BYTE *pbData, UINT16 *cwLength, char **ppszErrorMsg )
{

    volatile DWORD *pdwFlashCache;
    static BINFileParseStates BINFileParseState;
    // Because the signature of the BIN file is 7 bytes long and the length of a TFTP data packet
    //  is typically 512 bytes long, it is very probable that the last 4 byte DWORD will be broken
    //  across two TFTP packets.  I use these two variables to store the broken DWORD.
    static DWORD dwDataTailFromLastPacket;  // The beginning of the broken DWORD that has been
                                            //  retained from the last packet.
    static BYTE bNumBytesInDataTail;        // The number of bytes that have been carried over from
                                            //  the last packet.  Note that these are numbered starting
                                            //  from low byte to high byte because this is little endian.
                                            // DANGER - This may not be true on all processors.
    DWORD UNALIGNED *pdwBuffer;         // Pointer to current data position within the current TFTP packet
    int iBufferPos;             // Position of processing within the pbData packet buffer
    DWORD dwCurDataWord;        // Usually, this is the data word at the current position within the
                                //  TFTP packet.  However, in order to compensate for the 7 byte preamble
                                //  misalignment, this word may be "doctored" at the beginning of parsing
                                //  a packet in order to include carry over byte(s) from the last packet.
    static DWORD UNALIGNED *pdwDest;        // Pointer to the address that the data is to be written too.
    static DWORD dwRecordLen;   // Total length of the record being processed
    static DWORD dwRecordPos;   // Position of processing within the record being processed
    static DWORD dwRecordAddr;  // starting address of the record
    static DWORD dwPerfectCRC;  // Theoretical CRC over the record being processed
    static BYTE *pbCRC;         // Pointer used to run through memory to calculate CRC
    DWORD i;

    volatile BLR_REGS *BLR = (BLR_REGS *)FPGA_REGS_BASE_U_VIRTUAL;
    static int prev_percentComplete = -1;
    int percentComplete;
    int x,y,z;

    switch ( Operation )
    {
        
        case TFTPD_OPEN:
            EdbgOutputDebugString( "EthDown::TFTPD_OPEN::%s\r\n", pszFileName );
            dwPhysLen = 0;
            break;

        case TFTPD_READ:
            *cwLength = 0;
            EdbgOutputDebugString( "EthDown::TFTPD_READ::%s returning %u bytes\r\n", pszFileName, *cwLength );
            break;

        case TFTPD_WRITE:
            // EdbgOutputDebugString( "EthDown::TFTPD_WRITE::%s with %u bytes\r\n", pszFileName, *cwLength );
            // If it's the first packet, check the file type to determine 
            // if this is a .BIN file or something else.  We assume that if 
            // it's not in .BIN format, then it's the NB0 format.
            if (v_PacketNum == 0)
            {
                if (memcmp( pbData, "B000FF\x0A", 7) == 0)
                {
                    EdbgOutputDebugString( "Downloading .BIN file\r\n");
                    fileType |= BIN_FILE_TYPE;
                }
                else
                {
                    EdbgOutputDebugString( "Downloading .NB0 file\r\n");
                    fileType |= (NB0_FILE_TYPE | FLASHTARGET);
                }
            }

            if (fileType & NB0_FILE_TYPE)
            {
                pdwFlashCache = (volatile DWORD *)RAM_IMAGE_START;

                // Copy data packet into SDRAM and increment index by 512 bytes (128 DWORDS)
                memcpy((LPVOID)(pdwFlashCache+(v_PacketNum*128)), (LPVOID)pbData, 512); 
                if (v_PacketNum % 100 == 0)
                {
                    EdbgOutputDebugString( "X" );
                }
                v_PacketNum++;

                // Write this image in place of the boot loader, so there is no offset.
                dwEBOOT_OFFSET = 0x00000000;
                break;
            }

            iBufferPos = 0;
            pdwBuffer = (DWORD *)pbData;

            // If this is the beginning of the file
            if (dwPhysLen == 0)
            {
                // I need to DWORD align the rest of the data so that the DWORD accesses won't cause
                //  processor exceptions
                *cwLength -= 7;
                pdwBuffer = (DWORD UNALIGNED *) (pbData + 7); // memmove( pbData, pbData + 7, *cwLength );

                dwPhysStart = *pdwBuffer++;
                dwOffset=0;
                dwPhysLen = *pdwBuffer++;
                iBufferPos = 8;

                // Normal NK.BIN RAM images will be located at RAM_IMAGE_START.
                // NK.BIN FLASH images and EBOOT.BIN FLASH images must be cached into RAM before flashing.  
                // We'll cache the data at RAM_IMAGE_START.
                // We'll calculate an offset to be added to dwPhysStart in order to cache each image at the same location.
                offset = RAM_IMAGE_START - dwPhysStart; 

#ifndef MMXIP_MEMMAP
                if (offset > 0) // downloading eboot.bin
                {
                    EdbgOutputDebugString("\r\nDownloading bootloader image.\r\n");
                    // Write this image in place of the bootloader.  
                    // Therefore, there is no offset.
                    dwEBOOT_OFFSET = 0x00000000;
                    fileType |= (BOOTLOADER | FLASHTARGET);
                }
                else if (offset < 0) // downloading nk.bin (IMGFLASH=1)
                {
                    EdbgOutputDebugString("\r\nDownloading operating system image for flash target.\r\n");
                    // Write this image above the boot loader and configuration block.
                    dwEBOOT_OFFSET = 0x00080000;
                    fileType |= (NKBIN | FLASHTARGET);
                }
                else // downloading nk.bin (IMGFLASH=<not set>)
                {
                    EdbgOutputDebugString("\r\nDownloading operating system image for SDRAM target.\r\n");
                    fileType |= (NKBIN | SDRAMEXE);

                    // This image may still be placed into flash - it depends on 
                    // pEbootCFG->bWriteBINToFlash.  If writing to flash, we'll need
                    // to configure this offset.
                    dwEBOOT_OFFSET = 0x00080000;

⌨️ 快捷键说明

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