2k_mass.c

来自「HID-Ukey底层源码实现(st72651芯片) windows上层驱动」· C语言 代码 · 共 1,672 行 · 第 1/3 页

C
1,672
字号
		DTC_Wait_Stop();
		Enable_DTC_Int(); 
		DTC_Process(SMC_DTC_Format_Read_0);
	}
	else {
		Copy_malPaddr();	// cluster address
		Buffer_Param[0x25]= 0x00;

		if (Buffer_Param[0x3f] != 0x40)
			Buffer_Param[0x23]= Buffer_Param[0x19];

		DTC_Process(SMC_DTC_Format_Cmd_Read256);
		DTC_Wait_Stop();

		Enable_DTC_Int();
		DTC_Process(SMC_DTC_Format_Read0_256);
	}
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ROUTINE NAME : SMC_Read_Buffer
INPUT/OUTPUT : malPaddr = page address of SMC
               iBuffer = 0, read the page to buffer_0
               iBuffer !=0, read the page to buffer_1
DESCRIPTION  : Read one page of data
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
unsigned char SMC_Read_Buffer(unsigned char iBuffer)
{
	Buffer_Param[0x20] = Read1A;
	DTC_SMC_Send_Cmd();
	
	if (SMC_Cluster_Size != 64) {
		Buffer_Param[0x25] = 0;		// CA0-7
		Buffer_Param[0x24] = ((unsigned char *)(&(malPaddr)))[3];	// PA0-7
		Buffer_Param[0x23] = ((unsigned char *)(&(malPaddr)))[2];	// PA8-15
		Buffer_Param[0x22] = ((unsigned char *)(&(malPaddr)))[1];	// PA16-23
	}
	
	else {
		Copy_malPaddr();
		Buffer_Param[0x25] = 0x00;
		Buffer_Param[0x24] = 0x00;
	}

	SMC_Send_Addr();

	if (SMC_Cluster_Size == 64){
		Buffer_Param[0x20] = 0x30; //2nd read cmd for 2K flash
		DTC_SMC_Send_Cmd();
	}

	No_DTC_Int();
	DTC_Process((iBuffer == 0) ? SMC_DTC_Play_Read0 : SMC_DTC_Play_Read1);
	DTC_Wait_Stop();

	// Check ECC here
	return 0;
}

void Init_Write_Buffer(void)
{
	Buffer_Param[0x20] = Read1A;
	DTC_SMC_Send_Cmd();

	Buffer_Param[0x20] = Sequential_Data_Input;
	DTC_SMC_Send_Cmd();

	Copy_malPaddr();	// page address
	SMC_Send_Addr();

	asm {					// Clear Spare area with 0xFF
		LD		X, #5
		LD		A, #0xFF
FF_Spare:
		LD		(Buffer_Param[0x27], X), A
		DEC		X
		JRPL	FF_Spare
	}
//	Spare_Area.Reserved_Field = 0xFFFFFFFFL;
//	Spare_Area.Data_Status_Byte = 0xFF;
//	Spare_Area.Block_Status_Byte = 0xFF;
	Spare_Area.Block_Address_Field_1 =
	Spare_Area.Block_Address_Field_2 = DTC_SMC_Addr_Parity(SMC_Laddr);
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
ROUTINE NAME : SMC_Write_Buffer
INPUT/OUTPUT : malPaddr = page address of SMC
               SMC_Laddr = the logical address of the page
               iBuffer = 0, write the page from buffer_0
               iBuffer !=0, write the page from buffer_1
DESCRIPTION  : Write one page of data
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
unsigned char SMC_Write_Buffer(unsigned char iBuffer)
{
	Init_Write_Buffer();

	DTC_Process((iBuffer == 0) ? SMC_DTC_Play_Write0 : SMC_DTC_Play_Write1);
	DTC_Wait_Stop();

	Buffer_Param[0x20] = Page_Pgm;
	DTC_SMC_Send_Cmd();

	return SMC_Read_Status();
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
// Write a full big page of 2K buffer in pBuf. Using buffer1
unsigned char SMC_Write2K_Buffer1(unsigned char *pBuf)
{
	asm {
		LD		pBuf:1, A
		LD		pBuf, X
	}

	Init_Write_Buffer();

	asm {
		LD		Y, #4
CopyBuf:PUSH	Y
		CLR		X					// Copy one page
Copy256:LD		A, ([pBuf.w],X)
		LD		(Buffer_1, X), A
		INC		X
		JRNE	Copy256
		INC		pBuf

Copy512:LD		A, ([pBuf.w],X)
		LD		(Buffer_1:256, X), A
		INC		X
		JRNE	Copy512
		INC		pBuf

		LD		A, #SMC_DTC_Play_Write1
		CALL	DTC_Process
		CALL	DTC_Wait_Stop

		POP		Y
		DEC		Y					// Copy 4 pages
		JRNE	CopyBuf
	}

/*	if (*pBuf == 0)		// Compile bug needs this virtual line
		asm	NOP;
	asm {			// Write the 1st page
		CLR		X
Page11:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1, X), A
		INC		X
		JRNE	Page11
		INC		pBuf
Page12:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1:256, X), A
		INC		X
		JRNE	Page12
		INC		pBuf
	}
	DTC_Process(SMC_DTC_Play_Write1);
	DTC_Wait_Stop();

	asm {			// Write the 2nd page
		CLR		X
Page21:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1, X), A
		INC		X
		JRNE	Page21
		INC		pBuf
Page22:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1:256, X), A
		INC		X
		JRNE	Page22
		INC		pBuf
	}
	DTC_Process(SMC_DTC_Play_Write1);
	DTC_Wait_Stop();

	asm {			// Write the 3rd page
		CLR		X
Page31:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1, X), A
		INC		X
		JRNE	Page31
		INC		pBuf
Page32:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1:256, X), A
		INC		X
		JRNE	Page32
		INC		pBuf
	}
	DTC_Process(SMC_DTC_Play_Write1);
	DTC_Wait_Stop();

	asm {			// Write the 3rd page
		CLR		X
Page41:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1, X), A
		INC		X
		JRNE	Page41
		INC		pBuf
Page42:	LD		A, ([pBuf.w], X)
		LD		(Buffer_1:256, X), A
		INC		X
		JRNE	Page42
	}
	DTC_Process(SMC_DTC_Play_Write1);
	DTC_Wait_Stop();
*/
	Buffer_Param[0x20] = Page_Pgm;
	DTC_SMC_Send_Cmd();

	return SMC_Read_Status();
}

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#pragma CODE_SEG PAGE_CODE
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
// page number of the parameter page in the parameter block 
static unsigned char Param_Page;
#pragma DATA_SEG DEFAULT_RAM
extern unsigned char Current_Mode;
extern unsigned char Flash_State;
extern unsigned long Private_Zone_Size;
extern unsigned char Slot_Description[34];
extern unsigned char PassWord[34];
extern unsigned char Serial_Number[66];

typedef struct {
	unsigned char ST_Signature[11];
	unsigned long ST_PVT_Size;
	unsigned char ST_Mode;
	unsigned char ST_Slot_Description[34];
	unsigned char ST_PassWord[34];
	unsigned char ST_Serial_Number[66];
} ST_PARTITION_PAGE;
#define ST_Page	((ST_PARTITION_PAGE *)Buffer_0)

void Init_Buffer0()
{
	signed char iInit;

	ST_Page->ST_Signature[0] = 'S';
	ST_Page->ST_Signature[1] = 'T';
	ST_Page->ST_Signature[2] = ' ';
	ST_Page->ST_Signature[3] = 'S';
	ST_Page->ST_Signature[4] = 'e';
	ST_Page->ST_Signature[5] = 'c';
	ST_Page->ST_Signature[6] = 'u';
	ST_Page->ST_Signature[7] = 'r';
	ST_Page->ST_Signature[8] = 'i';
	ST_Page->ST_Signature[9] = 't';
	ST_Page->ST_Signature[10] = 'y';

	ST_Page->ST_PVT_Size = Private_Zone_Size;
	ST_Page->ST_Mode = Current_Mode;

	for (iInit = sizeof(Slot_Description); iInit >= 0; iInit--)
		ST_Page->ST_Slot_Description[iInit] = Slot_Description[iInit];
	for(iInit = sizeof(PassWord); iInit >= 0; iInit--)
		ST_Page->ST_PassWord[iInit] = PassWord[iInit];
	for(iInit = sizeof(Serial_Number); iInit >= 0; iInit--)
		ST_Page->ST_Serial_Number[iInit] = Serial_Number[iInit];
}

void SMC_Write_Param()
{
	if (SMC_Cluster_Size != 64) {
		Buffer_Param[0x20] = Read1A;
		DTC_SMC_Send_Cmd();
	}

	Buffer_Param[0x20] = Sequential_Data_Input;
	DTC_SMC_Send_Cmd();

	if (SMC_Cluster_Size != 64) {
		Buffer_Param[0x25] = 0;		// CA0-7
		Buffer_Param[0x24] = ((unsigned char *)(&(malPaddr)))[3];	// PA0-7
		Buffer_Param[0x23] = ((unsigned char *)(&(malPaddr)))[2];	// PA8-15
		Buffer_Param[0x22] = ((unsigned char *)(&(malPaddr)))[1];	// PA16-23
	}
	else {
		Copy_malPaddr();
		Buffer_Param[0x25] = 0;		// CA0-7
		Buffer_Param[0x24] = 0;
	}

	SMC_Send_Addr();

	Spare_Area.Reserved_Field = 0xFFFFFFFFL;
	Spare_Area.Data_Status_Byte = 0x00;
	Spare_Area.Block_Status_Byte = 0x00;
	Spare_Area.Block_Address_Field_1 =
	Spare_Area.Block_Address_Field_2 = 0;

	DTC_Process(SMC_DTC_Play_Write0 );	// Write from buffer0
	DTC_Wait_Stop();

	Buffer_Param[0x20] = Page_Pgm;
	DTC_SMC_Send_Cmd();

	SMC_Read_Status();
}

void SMC_Save_Parameters()
{
	NAND_Select(0);

	if (Param_Page == 31) {
		if(SMC_Cluster_Size != 64)
			malPaddr = 0;
		else
#ifdef Factory_bad_void			// PL: not necessary
			malPaddr = 0x4000;
#else
			malPaddr = 0x0;
#endif
		DTC_SMC_Erase();		// Erase block 0/1

		Init_Buffer0();

		if(SMC_Cluster_Size != 64)
			malPaddr = 0;
		else
#ifdef Factory_bad_void			// PL: not necessary
			malPaddr = 0x4000;
#else
			malPaddr = 0;
#endif
		SMC_Write_Param();		// Write to page 0 of block 0/1

		///////////////
		if (SMC_Cluster_Size != 64)
			malPaddr = 0x20;
		else
#ifdef Factory_bad_void			// PL: not necessary
			malPaddr = 0x8000;
#else
			malPaddr = 0x4000;
#endif

		DTC_SMC_Erase();		// Erase block 1

		if (SMC_Cluster_Size != 64)
			malPaddr = 0x20;
		else
#ifdef Factory_bad_void			// PL: not necessary
			malPaddr = 0x8000;
#else
			malPaddr = 0x4000;
#endif

		SMC_Write_Param();		// Write to page 0 of block 1

		Param_Page = 0;

		return;
	}

	Init_Buffer0();

	Param_Page++;
	if (SMC_Cluster_Size != 64)
		malPaddr = Param_Page;
	else
#ifdef Factory_bad_void	// PL: not necessary
		malPaddr = 0x4000 + (Param_Page << 8);
#else
		malPaddr = (Param_Page << 8);
#endif

	SMC_Write_Param();			// Write to page N of block 0

	if (SMC_Cluster_Size != 64)
		malPaddr = 0x20 + Param_Page;
	else
#ifdef Factory_bad_void	// PL: not necessary
		malPaddr = 0x8000 + (Param_Page << 8);
#else
		malPaddr = 0x4000 + (Param_Page << 8);
#endif
	SMC_Write_Param();			// Write to page N of block 1
}

extern unsigned char Startup_VAR;
void SMC_Restore_Parameters()
{
	signed char iInit;

	NAND_Select(0);

	for (Param_Page = 31; Param_Page != 0xFF; Param_Page--) {
		if (SMC_Cluster_Size != 64)
			malPaddr = Param_Page;
		else  
#ifdef Factory_bad_void	// PL: not necessary
			malPaddr = 0x4000 + (Param_Page << 8);
#else
			malPaddr = (Param_Page << 8);
#endif
		SMC_Read_Buffer(0);

		if (Buffer_Param[0x29] != 0xFF ||
			Buffer_Param[0x2A] != 0xFF ||					// Valid reserved area
			Buffer_Param[0x2B] != 0x00 ||					// Valid Data Status Byte
			Buffer_Param[0x2C] != 0x00)						// Valid Block Status Byte
			continue;

		if (Buffer_Param[0x38] != Buffer_Param[0x34] ||
			Buffer_Param[0x39] != Buffer_Param[0x35] ||
			Buffer_Param[0x3A] != Buffer_Param[0x36] ||		// Compare ECC1
			Buffer_Param[0x3B] != Buffer_Param[0x2F] ||
			Buffer_Param[0x3C] != Buffer_Param[0x30] ||
			Buffer_Param[0x3D] != Buffer_Param[0x31]) {		// Compare ECC2

			if (SMC_Cluster_Size != 64)
				malPaddr += 0x20;
			else
#ifdef Factory_bad_void	// PL: not necessary
				malPaddr += 0x8000;
#else
				malPaddr += 0x4000;
#endif
			SMC_Read_Buffer(0);
		}

		if(	ST_Page->ST_Signature[0] != 'S' ||
			ST_Page->ST_Signature[1] != 'T' ||
			ST_Page->ST_Signature[2] != ' ' ||
			ST_Page->ST_Signature[3] != 'S' ||
			ST_Page->ST_Signature[4] != 'e' ||
			ST_Page->ST_Signature[5] != 'c' ||
			ST_Page->ST_Signature[6] != 'u' ||
			ST_Page->ST_Signature[7] != 'r' ||
			ST_Page->ST_Signature[8] != 'i' ||
			ST_Page->ST_Signature[9] != 't' ||
			ST_Page->ST_Signature[10] != 'y' ) {

			Private_Zone_Size = 0;
			Slot_Description[0] = 0;
			PassWord[0] = 0;
			Serial_Number[0] = 0;
			Current_Mode = 0;
		}
		else{
			Flash_State = 1;
			Private_Zone_Size = ST_Page->ST_PVT_Size;
			for(iInit = sizeof(Slot_Description); iInit >= 0; iInit--)
				Slot_Description[iInit] = ST_Page->ST_Slot_Description[iInit];
			for(iInit = sizeof(PassWord); iInit >= 0; iInit--)
				PassWord[iInit] = ST_Page->ST_PassWord[iInit];
			for(iInit = sizeof(Serial_Number); iInit >= 0; iInit--)
				Serial_Number[iInit] = ST_Page->ST_Serial_Number[iInit];

			if(PassWord[0] == 0)
				Current_Mode = 0;
			else
				Current_Mode = 1;
		}
		Startup_VAR = 1;
		return;
	}

	Private_Zone_Size = 0;
	Slot_Description[0] = 0;
	PassWord[0] = 0;
	Serial_Number[0] = 0;
	Current_Mode = 0;
	Startup_VAR = 1;
}

void SMC_Init_Param()
{
	signed char iInit;

	for (iInit = sizeof(Slot_Description); iInit >= 0; iInit--) {
		Slot_Description[iInit] = 0;
		PassWord[iInit] = 0;
	}

	if(Serial_Number[0] == 0)
		for (iInit = sizeof(Serial_Number); iInit >= 0; iInit--)
			Serial_Number[iInit] = 0;

	NAND_Select(0);

	if(SMC_Cluster_Size != 64)
		malPaddr = 0;		// Erase block 0
	else
#ifdef Factory_bad_void		// PL: not necessary
		malPaddr = 0x4000;	// Erase block 1 for 2K Flash
#else
		malPaddr = 0;
#endif

	DTC_SMC_Erase();
	if (SMC_Cluster_Size != 64)
		malPaddr = 0x20;		// Erase block 1
	else 
#ifdef Factory_bad_void		// PL: not necessary
		malPaddr = 0x8000;
#else
		malPaddr = 0x4000;
#endif

	DTC_SMC_Erase();		// Erase block 2 for 2K Flash

	Flash_State = 1;
	Init_Buffer0();

	if(SMC_Cluster_Size != 64)
		malPaddr = 0;
	else
#ifdef Factory_bad_void		// PL: not necessary
		malPaddr = 0x4000;
#else
		malPaddr = 0;
#endif

	SMC_Write_Param();			// Write to page 0 of block 0/block1

	if (SMC_Cluster_Size != 64)
		malPaddr = 0x20;
	else
#ifdef Factory_bad_void		// PL: not necessary
		malPaddr = 0x8000;
#else
		malPaddr = 0x4000;
#endif
	SMC_Write_Param();			// Write to page 0 of block 1/block2

	Param_Page = 0;
}

unsigned char DTC_SMC_Chk_Spare(unsigned char flag){

	if (DTC_Code_Number != CODE_DTC_CODE0) {
		DTC_Load_Code(SMC_DTC_Code0);
		DTC_Code_Number = CODE_DTC_CODE0;
	}

	Copy_malPaddr();
	if (flag == format)
		Buffer_Param[0x24] = 0x02;
	else
		Buffer_Param[0x24] = 0x08;
	Buffer_Param[0x25] = 0x0;
	Buffer_Param[0x3f] = malNpage;

	No_DTC_Int();
	DTC_Process(0x3f);

	return DTC_Wait_Stop();
}

/****** (c) 1999   STMicroelectronics *************** END OF FILE *************/

⌨️ 快捷键说明

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