www.pudn.com > UDiskFileSystem.rar > MX_FFS_LAYER1.cpp


/*************************************************************************/ 
/*                                                                       */ 
/*        Copyright (c) 1993-2001 Macronix, Inc.           				 */ 
/*                                                                    	 */ 
/* PROPRIETARY RIGHTS of Macronix are involved in the      			     */ 
/* subject matter of this material.  All manufacturing, reproduction,    */ 
/* use, and sales rights pertaining to this subject matter are governed  */ 
/* by the license agreement.  The recipient of this software implicitly  */ 
/* accepts the terms of the license.                                     */ 
/*                                                                       */ 
/*************************************************************************/ 
 
/*************************************************************************/ 
/* FILE NAME                                                     	 	 */ 
/*      MX_FFS_LAYER1.c        			 						 		 */ 
/* DESCRIPTION                                                           */ 
/*	This file definition only internal used in File System							 */ 
/* AUTHOR                                                                */ 
/*      MXIC ISG , Inc.   System Application Department			 		 */ 
/*                                                                       */ 
/*                                                                       */ 
/* HISTORY                                                               */ 
/*      2006/03/16      Test version 0.00 for BCB6						*/ 
/*************************************************************************/ 
 
#ifndef  MX_FFS_LAYER1_C 
#define  MX_FFS_LAYER1_C 
//--------------------------------------------------------------------------- 
#include  
#pragma hdrstop 
 
#include "MX_FFS_LAYER1.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
 
 
extern HANDLE hFile; 
 
//--------------------------------------------------------------------------- 
 
void UFI_ReadFile(HANDLE SourceFile, u_32 LBA_START, u_16 LBA_Length, u_8 *Buffer) 
{ 
	DWORD BytesRead; 
	SetFilePointer (SourceFile, (LBA_START * 0x200), NULL, FILE_BEGIN); 
	ReadFile (SourceFile, Buffer, (LBA_Length * 0x200), &BytesRead, NULL); 
 
	/* 
	//cbw (32 bytes * 2) 
	(10)55, 53, 42, 43, ... ,"LBA_START"(4), ..., "LBA_Length"(2), ...  [32 Bytes] 
	//data (512 bytes * 2) 
	    for(i=0; i= 0xFFF8) 
        { 
	        *ReturnCounter = i; 
	        return SystemError; 
        } 
    } 
	*ReturnCounter = i; 
	return SystemPass; 
} 
 
//--------------------------------------------------------------------------- 
u_8 FAT16_GetFile_FATLinkTable(u_32 FAT_Address, u_16 BytesPerSector, 
                                u_32 FileFstCluster, u_32 *FATLinkTable, 
                                u_16 LinkClusterCnt, u_16 *ReturnCounter) 
{ 
	u_32 FileCluster, FAT_IndexAddr, FAT_PageIndex; 
	u_16 TEMP; 
	u_8 i, FAT_Buffer[0x200]; 
 
	//FATLinkTable[0] = FileFstCluster; 
	FileCluster = FileFstCluster; 
	*ReturnCounter = 0; 
 
	FAT_IndexAddr = (FAT_Address * 0x200) + (FileCluster * 2); 
	FAT_PageIndex = (FAT_IndexAddr/BytesPerSector); 
	UFI_ReadFile(hFile, FAT_PageIndex, 0x01, FAT_Buffer); 
 
	for(i=0; i= 0xFFF8) 
        { 
	        *ReturnCounter = i; 
	        return SystemError; 
        } 
    } 
	*ReturnCounter = i; 
	return SystemPass; 
} 
//--------------------------------------------------------------------------- 
 
u_8 FAT16_FAT_Format(u_32 FAT_Address,  u_16 BytesPerSector, 
                      u_32 SectorsPerFAT, u_16 RootDir1stCluster) 
{ 
	u_32 SectorCounter; 
	u_16 i; 
	u_8 FAT_Buffer[0x200]; 
 
	UFI_ReadFile(hFile, FAT_Address, BytesPerSector/0x200, FAT_Buffer); 
	for(i=(RootDir1stCluster+1)*0x02; i<0x200; i++) 
	    FAT_Buffer[i] = 0x00; 
	UFI_WriteFile(hFile, FAT_Address, BytesPerSector/0x200, FAT_Buffer); 
 
	for(SectorCounter=1; SectorCounter= 0x0FFFFFF8)//It means EOF 
	    { 
		    *ReturnCounter = i; 
		    return SystemError; 
	    } 
    } 
	*ReturnCounter = i; 
	return SystemPass; 
} 
//--------------------------------------------------------------------------- 
//Fill FAT Link Table  
//But its first element is the File first Cluster. 
// 
u_8 FAT32_GetFile_FristFATLinkTable(u_32 FAT_Address, u_16 BytesPerSector, 
                                u_32 FileFstCluster, u_32 *FATLinkTable, 
                                u_16 LinkClusterCnt, u_16 *ReturnCounter) 
{ 
	u_32 FileCluster, FAT_IndexAddr, FAT_PageIndex; 
	u_16 TEMP; 
	u_8 i, FAT_Buffer[0x200]; 
 
	FATLinkTable[0] = FileFstCluster; 
	FileCluster = FileFstCluster; 
	*ReturnCounter = 0; 
 
	FAT_IndexAddr = (FAT_Address * BytesPerSector) + (FileCluster * 4); 
	FAT_PageIndex = (FAT_IndexAddr/BytesPerSector); 
	UFI_ReadFile(hFile, FAT_PageIndex, 0x01, FAT_Buffer); 
 
	for(i=1; i= 0x0FFFFFF8)//It means EOF  
        { 
	        *ReturnCounter = i; 
	        return SystemError; 
        } 
    } 
	*ReturnCounter = i; 
	return SystemPass; 
} 
//--------------------------------------------------------------------------- 
 
u_8 FAT32_FAT_Format(u_32 FAT_Address,  u_16 BytesPerSector, 
                      u_32 SectorsPerFAT, u_16 RootDir1stCluster) 
{ 
	u_32 SectorCounter; 
	u_16 i; 
	u_8 FAT_Buffer[0x200]; 
 
	UFI_ReadFile(hFile, FAT_Address, BytesPerSector/0x200, FAT_Buffer); 
	for(i=(RootDir1stCluster+1)*0x04; i<0x200; i++) 
	    FAT_Buffer[i] = 0x00; 
	UFI_WriteFile(hFile, FAT_Address, BytesPerSector/0x200, FAT_Buffer); 
 
	for(SectorCounter=1; SectorCounter