?? ethdown.c
字號:
//
// 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:
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 "xsc1bd.h"
extern void DumpDwords(PDWORD pdw, int len);
extern void OEMWriteDebugByte(unsigned char c);
#define BIN_FILE_TYPE 1
#define NB0_FILE_TYPE 2
#define BOOTLOADER 4
#define NKBIN 8
#define FLASHTARGET 16
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;
#if defined ( ORGEBOOT_FLAG )
#define RAM_IMAGE_START 0x8008D000
#else
#define RAM_IMAGE_START 0x800C0000
#endif
#define FLASH_START BOOT_FLASH_BASE_U_VIRTUAL
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;
#if defined ( PLAT_LUBBOCK )
//volatile BLR_REGS *BLR = (BLR_REGS *)FPGA_REGS_BASE_U_VIRTUAL;
static int prev_percentComplete = -1;
int percentComplete;
int x,y,z;
#endif
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.
#if defined ( RTECH_FLAG )
fileType |= BOOTLOADER;
#endif
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;
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);
}
EdbgOutputDebugString("\r\nImage start: 0x%X length: 0x%X cache location: 0x%X\r\n", dwPhysStart, dwPhysLen, dwPhysStart + offset );
// Go ahead and apply the offset to the image start address.
dwPhysStart += offset;
bNumBytesInDataTail = 0;
BINFileParseState = BIN_PHYSADDR;
pdwRecStart=pdwRecNext=(LPDWORD)(dwPhysStart+dwPhysLen-dwOffset);
}
// Check to see if this is the last packet and it's empty
else if (*cwLength == 0)
return 0;
v_PacketNum++;
// Pull the rest of the 32-bit data word that was started in the last packet
if (bNumBytesInDataTail) {
dwCurDataWord = 0;
for( i = 0; i < 4UL-bNumBytesInDataTail; i++ )
dwCurDataWord |= pbData[i] << ((i+bNumBytesInDataTail) * 8);
dwCurDataWord |= dwDataTailFromLastPacket;
// DWORD align the buffer
*cwLength -= 4 - bNumBytesInDataTail;
pdwBuffer = (DWORD UNALIGNED *) (pbData + (4-bNumBytesInDataTail)); //memmove( pbData, pbData + (4-bNumBytesInDataTail), *cwLength );
iBufferPos = -4;
}
else
dwCurDataWord = *pdwBuffer++;
#if defined ( PLAT_LUBBOCK )
percentComplete = (((v_PacketNum+1) * 512 * 100) / dwPhysLen);
// Update the display only when the percent complete changes
if (percentComplete != prev_percentComplete)
{
// Display percentage in decimal on the hex display
x = percentComplete / 100;
y = (percentComplete % 100) / 10;
z = ((percentComplete % 100) % 10);
//BLR->hex_led = ((x << 8) | (y << 4) | (z));
prev_percentComplete = percentComplete;
}
#endif
// I'll keep looping until I get almost to the end of the packet buffer
while( iBufferPos + 4 <= *cwLength )
{
switch( BINFileParseState )
{
case BIN_PHYSADDR:
dwRecordAddr= dwCurDataWord;
if (dwRecordAddr != 0)
dwRecordAddr += offset;
if (offset && dwRecordAddr)
EdbgOutputDebugString("\r\nAddress fixup was 0x%X now 0x%X\r\n", dwCurDataWord, dwRecordAddr );
#ifdef ARM
dwCurDataWord-= dwOffset;
#endif
pdwDest = (DWORD UNALIGNED *)dwRecordAddr;
pbCRC = (BYTE *)dwRecordAddr;
BINFileParseState = BIN_PHYSLEN;
break;
case BIN_PHYSLEN:
dwRecordLen = dwCurDataWord;
BINFileParseState = BIN_CHECKSUM;
break;
case BIN_CHECKSUM:
if (pdwDest == 0 && dwCurDataWord == 0)
{
dwLaunchAddr = dwRecordLen;
#if defined ( PLAT_LUBBOCK )
// Display 100% on the display
//BLR->hex_led = 0x100;
#endif
}
dwPerfectCRC = dwCurDataWord;
dwRecordPos = 0;
BINFileParseState = BIN_DATA;
*pdwRecNext++=dwRecordAddr;
*pdwRecNext++=dwRecordLen;
*pdwRecNext++=dwPerfectCRC;
break;
case BIN_DATA:
// If we aren't at the end of the record yet read another DWORD. Note that the number of
// bytes in a record is guaranteed to always be DWORD aligned, so we can assume that there
// won't be a partial DWORD at the end of a record.
*pdwDest++ = dwCurDataWord;
dwRecordPos += 4;
if (dwRecordPos < dwRecordLen)
BINFileParseState = BIN_DATA;
// If we have reached the end of the record
else {
BINFileParseState = BIN_PHYSADDR;
}
break;
} // switch(BINFileParseState)
dwCurDataWord = *pdwBuffer++;
iBufferPos += 4;
} // while not at end of buffer
// Store the fragmented DWORD until we get the next packet
bNumBytesInDataTail = (BYTE)(*cwLength - iBufferPos);
dwDataTailFromLastPacket = dwCurDataWord;
// Mask off the garbage that was picked up from the end of the packet buffer
switch( bNumBytesInDataTail ) {
case 1:
dwDataTailFromLastPacket &= 0x000000FFUL;
break;
case 2:
dwDataTailFromLastPacket &= 0x0000FFFFUL;
break;
case 3:
dwDataTailFromLastPacket &= 0x00FFFFFFUL;
break;
}
break;
case TFTPD_DESTROY:
EdbgOutputDebugString( "EthDown::TFTPD_DESTROY::%s with %u bytes\r\n", pszFileName, *cwLength );
for( i = 0; i < *cwLength; i++ ) {
if (i > 0 && i % 8 == 0)
EdbgOutputDebugString( " " );
if (i > 0 && i % 16 == 0)
EdbgOutputDebugString( "\r\n" );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -