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

📄 flashdet.cpp

📁 PC燒錄BIOS的源程序
💻 CPP
字号:
#include "mem.h"
#include "FlashDet.h"
#include "Memory.h"
#include "stdio.h"
#include "tools.h"
#include "ChipSet.h"
#include "intel.h"
#include <string.h>

BYTE Man1, Dev1, Man2, Dev2;
PFLASHCHIPINFO pCurCInfo = new FLASHCHIPINFO;

void FlashCmd(LONGINT RomBase , BYTE cmd )
{
	FOMemByte( RomBase + 0x5555, 0xAA );
	FOMemByte( RomBase + 0x2AAA, 0x55 );
	FOMemByte( RomBase + 0x5555, cmd);
}

void SendIdCmd(LONGINT RomBase , BYTE Method)
{
	switch (Method)
	{
	case 0:
		FlashCmd(RomBase, 0x80 );
		FlashCmd(RomBase, 0x60 );
		break;
	case 1:
		FlashCmd(RomBase, 0x90 );
		break;
	default:
	;
	}
}

void ResetRom(LONGINT RomBase , BYTE Method)
{
	if ( ( Method && 2 ) == 0 )
    {
		FOMemByte( RomBase, 0xff );
		FOMemByte( RomBase, 0xff );
		FOMemByte( RomBase, 0x00 );
		FlashCmd( 0xf0 , RomBase );
	}
}

void ToBinaryString(BYTE number, char *buff)
{
	char szTemp[10];
	while( number > 0 )
	{
		if ( ( number % 2 ) == 0 )
		{
			strcpy( szTemp, "0" );
		}
		else
		{
			strcpy( szTemp, "1" );
		}
		strcat( szTemp, buff );
		strcpy( buff, szTemp );
		strcpy( szTemp, "" );
		number = number / 2;
	}
}

BOOL ParityOdd( BYTE B )
{
	char buffer[10];
	int index, number = 0;

//	sprintf( buffer, "%08b", B );
	ToBinaryString( B, buffer );
	for ( index = 0; index < 8; index++ )
	{
		if ( buffer[index] == '1' )
		{
			number++;
		}
	}
	if ( number % 2 == 0 )
		return FALSE;
	else
		return TRUE;
/*	_asm{
			MOV AL,B
			OR  AL,AL
			JPE @@1
			MOV AL,1
			JMP @@2
		@@1:
			MOV AL,0
		@@2:
		}
*/
 }

BOOL DetectLoop(PFLASHCHIPINFO pMyFlash , BYTE MinB , BYTE MaxB , LONGINT* pRomBase, BYTE Method, BYTE Man, BYTE Dev )
{
	BYTE M, D, X, M0, D0;
	BOOL Done, Found;
//	BOOL bRet = TRUE;
	PCHIPMANUINFO pManuRoot = NULL, pManuTail = NULL, pCurManuf = NULL;

	pManuTail = new CHIPMANUINFO;
	if (pManuTail == NULL)
	{
		return FALSE;
	}
	pManuRoot = pManuTail;
	pManuTail->Manuf = 0x89;  //Intel always 0x89
	pManuTail->F_IdChip =  IntelIdChip;
	pManuTail->Next = NULL;

	Found = FALSE;
	for ( X = MinB; X <= MaxB; X++ )
	{
		if ( X > 0 ) *pRomBase = ( - ( LONGINT( 1 ) << X ) );
		Done = FALSE;

		do
		{
			ResetRom(*pRomBase , Method);
			M0 = FIMemByte(*pRomBase);
			D0 = FIMemByte(*pRomBase+1);
			_asm{CLI};
			SendIdCmd(*pRomBase , Method);
			//Get mfg. code
			M = FIMemByte(*pRomBase );
			if ( M == 0x7F ) M = FIMemByte( *pRomBase + 0x100 ); //Mfg. ID for EON
			if ( ( M == 0x7F ) && ( FIMemByte( *pRomBase + 0x3 ) == 0x1F ) ) M = 0x7F; //Mfg. ID for IMT
			//Get dev. code
			D = FIMemByte( *pRomBase + 1 );
			if ( D == 0x7F ) D = FIMemByte( *pRomBase + 0x101 ); //Dev. ID for EON
			ResetRom(*pRomBase , Method);
			_asm{STI};

			if ( ( ( M != M0 ) || ( D != D0 ) ) && ParityOdd( M ) )
			{
				if ( !Found )
				{
					Man = M;
					Dev = D;
					pCurManuf = pManuRoot;
					Found = FALSE;
					do
					{
						//FillChar( CurCInfo.Sectors, SizeOf( CurCInfo.Sectors ), 0); //v1.21 Clear sector table
						memset( (void*) pMyFlash->Sectors , 0 , 5 * 2 * sizeof( WORD ) );
						//with CurManuf^ do
							if ( ( pCurManuf->Manuf == Man ) && pCurManuf->F_IdChip( Dev, pMyFlash ) )
								Found = TRUE;
							else
								pCurManuf = pCurManuf->Next;
					} while ( !( Found || ( pCurManuf == NULL ) ) );
					if ( Found )
					{
						if ( X > 0 ) *pRomBase = - ( LONGINT( pMyFlash->Size ) << 10 );
						return TRUE;
					}
					else
					{
						Done = TRUE;
					}
				}
				else
				{
					pMyFlash->Size = pMyFlash->Size;
					*pRomBase = - ( LONGINT( pMyFlash->Size ) << 10 );
					return TRUE;
				}
			}
			else
			{
				Done = TRUE;
				if ( Found )
				{
					pMyFlash->Size = pMyFlash->Size;
					*pRomBase = - ( LONGINT( pMyFlash->Size ) << 10 );
					return TRUE;
				}
			}
		} while ( !Done );
	}
	return FALSE;
	
}

BOOL FlashDetect(PFLASHCHIPINFO pMyFlash , LONGINT* pRomBase)
{
	if (pMyFlash == NULL)
		return FALSE;
	memset((void*) pMyFlash , 0 , sizeof(FLASHCHIPINFO));

	BYTE MinB, MaxB, Tries;
	BOOL ROMFound = FALSE;
//	WORD FlashError = 0;

	Man1 = 0xFF;
	Dev1 = 0xFF;
	Man2 = 0xFF;
	Dev2 = 0xFF;
//	FillChar( pCurCInfo, SizeOf( CurCInfo ), 0 );
	if ( *pRomBase == 0 )
	{
		MinB = 15;
		MaxB = 20;
	}
	else
	{
		MinB = 0;
		MaxB = 0;
	}
	if ( DetectLoop(pMyFlash , MinB , MaxB , pRomBase , 0, Man1, Dev1 ) )
	{
		ROMFound = TRUE;
	}
	else
	{
		if (( Man1 != 0xFF ) || ( Dev2 != 0xFF ))
		{
			Man2 = Man1;
			Dev2 = Dev1;
		}
		if ( DetectLoop(pMyFlash , MinB , MaxB , pRomBase , 1, Man1, Dev1 ) )
		{
			ROMFound = TRUE;
		}
	}

/*	if ( !ROMFound )
	{
		CurManuf = NULL;
		FlashError = 1;    //unknown chip
	}
*/
	return ROMFound;
}

//End of the Implementation of FlashDet.Cpp

⌨️ 快捷键说明

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