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

📄 nandfat.c

📁 NAND FLASH FAT文件系统建立范例
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * FileName:	   fat.c
 *Copyright(c) 2004 VIA Technologies, Inc. All Rights Reserved.
 *
 * Author:  	   penson  Version: 1.00  Date: 2004-10-25
 * Description:   this file define  functions about sd card
 * Version: 	   
 * Function List:  
 *  			   1.
 * History: 	   
 *     <penson>   <10-25>    <v1.00 >   <desc>
 */


//#define _DEBUG_ON_


//#include <stdio.h>
//#include <stdlib.h>

//#include "driversys.h"
#include "drivermmiif.h"
#include "Driverosif.h"

//#include "driverprim.h"
//#include "type_zac.h"
#include "NANDFAT.h"

//#include "OSModule_MemoryManagement.h"
//#include"BadBlockManagement.h"  

const UINT8 BPBTaBle[512] =
/*{
	0xEB, 0x3C, 0x90, 'V' ,  'I', 'A', 'T', 'E', 'C', 'T', 'H', 0x00, 0x02, 0x02, 0x01, 0x00, 
	0x02, 0x00, 0x02, 0x00, 0x00, 0xF8, 0xFF, 0x00, 0x3F, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x29, 0xCA, 0x61, 0x58, 0x30, 0x4C, 0x4F, 0x56, 0x45, 0x42, 
	0x45, 0x4E, 0x46, 0x45, 0x4E, 0x20, 0x46, 0x41, 0x54, 0x31, 0x36, 0x20, 0x20, 0x20, 0x33, 0xC9, 
	0x8E, 0xD1, 0xBC, 0xF0, 0x7B, 0x8E, 0xD9, 0xB8, 0x00, 0x20, 0x8E, 0xC0, 0xFC, 0xBD, 0x00, 0x7C, 
	0x38, 0x4E, 0x24, 0x7D, 0x24, 0x8B, 0xC1, 0x99, 0xE8, 0x3C, 0x01, 0x72, 0x1C, 0x83, 0xEB, 0x3A, 
	0x66, 0xA1, 0x1C, 0x7C, 0x26, 0x66, 0x3B, 0x07, 0x26, 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x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x74, 0x6F, 0x20, 0x72, 
	0x65, 0x73, 0x74, 0x61, 0x72, 0x74, 0x0D, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAC, 0xBF, 0xCC, 0x55, 0xAA
};
*/
{
	0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x02,
	0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0xF8, 0xFB, 0x00, 0x3F, 0x00, 0xFF, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x01, 0x00, 0x00, 0x01, 0x29, 0xBE, 0x48, 0x2D,
	0xF8, 0x4E, 0x4F, 0x20, 0x4E, 0x41, 0x4D, 0x45, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41,
	0x54, 0x31, 0x36, 0x20, 0x20, 0x20, 0x33, 0xC9, 0x8E, 0xD1, 0xBC, 0xF0, 0x7B, 0x8E,
	0xD9, 0xB8, 0x00, 0x20, 0x8E, 0xC0, 0xFC, 0xBD, 0x00, 0x7C, 0x38, 0x4E, 0x24, 0x7D,
	0x24, 0x8B, 0xC1, 0x99, 0xE8, 0x3C, 0x01, 0x72, 0x1C, 0x83, 0xEB, 0x3A, 0x66, 0xA1,
	0x1C, 0x7C, 0x26, 0x66, 0x3B, 0x07, 0x26, 0x8A, 0x57, 0xFC, 0x75, 0x06, 0x80, 0xCA,
	0x02, 0x88, 0x56, 0x02, 0x80, 0xC3, 0x10, 0x73, 0xEB, 0x33, 0xC9, 0x8A, 0x46, 0x10,
	0x98, 0xF7, 0x66, 0x16, 0x03, 0x46, 0x1C, 0x13, 0x56, 0x1E, 0x03, 0x46, 0x0E, 0x13,
	0xD1, 0x8B, 0x76, 0x11, 0x60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0x32, 0xE4, 0xF7, 0xE2, 0x03, 0x46, 0xFC, 0x13, 0x56, 0xFE, 0xEB,
	0x4A, 0x52, 0x50, 0x06, 0x53, 0x6A, 0x01, 0x6A, 0x10, 0x91, 0x8B, 0x46, 0x18, 0x96,
	0x92, 0x33, 0xD2, 0xF7, 0xF6, 0x91, 0xF7, 0xF6, 0x42, 0x87, 0xCA, 0xF7, 0x76, 0x1A,
	0x8A, 0xF2, 0x8A, 0xE8, 0xC0, 0xCC, 0x02, 0x0A, 0xCC, 0xB8, 0x01, 0x02, 0x80, 0x7E,
	0x02, 0x0E, 0x75, 0x04, 0xB4, 0x42, 0x8B, 0xF4, 0x8A, 0x56, 0x24, 0xCD, 0x13, 0x61,
	0x61, 0x72, 0x0B, 0x40, 0x75, 0x01, 0x42, 0x03, 0x5E, 0x0B, 0x49, 0x75, 0x06, 0xF8,
	0xC3, 0x41, 0xBB, 0x00, 0x00, 0x60, 0x66, 0x6A, 0x00, 0xEB, 0xB0, 0x4E, 0x54, 0x4C,
	0x44, 0x52, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0D, 0x0A, 0x4E, 0x54, 0x4C, 0x44,
	0x52, 0x20, 0x69, 0x73, 0x20, 0x6D, 0x69, 0x73, 0x73, 0x69, 0x6E, 0x67, 0xFF, 0x0D,
	0x0A, 0x44, 0x69, 0x73, 0x6B, 0x20, 0x65, 0x72, 0x72, 0x6F, 0x72, 0xFF, 0x0D, 0x0A,
	0x50, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6E, 0x79, 0x20, 0x6B, 0x65, 0x79, 0x20,
	0x74, 0x6F, 0x20, 0x72, 0x65, 0x73, 0x74, 0x61, 0x72, 0x74, 0x0D, 0x0A, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0xAC, 0xBF, 0xCC, 0x55, 0xAA
};

/*
*THIS part is created according tro SD CARD file allocation system spec
*/


FATSYS_STRUCT gNandFATSYS ;


UINT32 gNandFFBuffAddr;
UINT8 gNandFileName[14] ;


UINT8 gNFLASHBuf[512] ;


extern UINT8 HD_NFlashWrite(UINT32 src_addr, UINT32 dst_addr)  ;
extern UINT8 HD_NFlashRead(UINT32 src_addr, UINT32 dst_addr) ; //cmd17



/*----------------------------------------------------------------------*/
/*
 * Function:	   NandValidFileChar()
 * DeSCRiption:   Function to see if the character is a valid FILENAME character .please to refer microsoft datasheet
 * Calls:   	   
 * Called By:      
 * Table Accessed: 
 * Table Updated:  
 * Input:    UINT8 file_char	  
 * Output:   none      
 * Return:   true and false 	 
 * Others:  	   
 */
UINT8 NandValidFileChar(UINT8 file_char)
{
	if (file_char < 0x20)
	{
		return (SW_PARA_ERR);
	}
	else if ((file_char == 0x22) ||
		(file_char == 0x2A) ||
		(file_char == 0x2B) ||
		(file_char == 0x2C) ||
		(file_char == 0x2E) ||
		(file_char == 0x2F) ||
		((file_char >= 0x3A) && (file_char <= 0x3F)) ||
		((file_char >= 0x5B) && (file_char <= 0x5D)) ||
		(file_char == 0x7C) ||
		(file_char == 0xE5))
	{
		return (SW_PARA_ERR);
	}
	else
	{
		return (SW_PARA_OK);
	}
}



/*
 * Function:		clust_to_addr()
 * DeSCRiption:    Convert a cluster number into a read address 
 * Calls:   	   
 * Called By:      
 * Table Accessed: 
 * Table Updated:  
 * Input:     UINT32 clust_no     
 * Output:   none      
 * Return:  UINT32 clust_addr      
 * Others:  	   
 */
UINT32 NandClusterToAddr(UINT16 clust_no)
{
	UINT32 clust_addr;

	if (clust_no < 2)
	{
		return SW_PARA_ERR;
	}

	gNandFATSYS.FirstSectorofCluster = ((clust_no - 2) * (UINT32)
		gNandFATSYS.Bpb50.bpbSecPerClust) +
		(UINT32)
		gNandFATSYS.FirstDataSector;
	clust_addr = (UINT32)
		gNandFATSYS.FirstSectorofCluster * (UINT32)
		gNandFATSYS.Bpb50.bpbBytesPerSec +
		gNandFATSYS.PartitionAddr;

	return (clust_addr);
}
/*
 * Function:	NandInitializeFat ()
 * DeSCRiption:  initial SD card  and reads the card's partition table and 
 *  				   boot sector, storing the card information needed to 
 *  				   handle the file system
 * Calls:   	   
 * Called By:      
 * Table Accessed: 
 * Table Updated:  
 * Input:     none
 * Output:   none
 * Return:   true and flase 	 
 * Others:  	   
 */
#define  FAT_DEBUG

UINT8 NandInitializeFat(void)
{
	UINT32 i;
	UINT8 error = HW_ERR_NONE ;

	gNandFATSYS.Bpb50.bpbBytesPerSec = 512 ;// Initialize sector size to 512 (all SD cards have a 512 sector size)

	// *(unsigned int *) (0x4003002c) = 1 ;
	error = HD_NFlashRead(0,(UINT32) gNFLASHBuf) ;
	if (error != HW_ERR_NONE)
	{
#ifdef _DEBUG_ON_
#endif
		return error;
	}
	// *(unsigned int *)(0x4003002c) = 0 ;

	//get the relative sector from partition table
	//gPartRecord.prStartLBA = ((UINT32) gNFLASHBuf[0x1c6] | (UINT32) gNFLASHBuf[0x1c7] << 8) ;
	gNandFATSYS.PartRecord.prStartLBA = ((UINT32) gNFLASHBuf[0x1c6] |
		(UINT32) gNFLASHBuf[0x1c7] <<
		8) ;
	if ((((gNFLASHBuf[0] == 0xEB) && (gNFLASHBuf[2] == 0x90)) || (gNFLASHBuf[0] == 0xE9)) &&
		((gNFLASHBuf[510] == 0x55) && (gNFLASHBuf[511] == 0xAA)))
	{
		//gPartRecord.prStartLBA = 0 ;
		gNandFATSYS.PartRecord.prStartLBA = 0;
	}

	gNandFATSYS.PartitionAddr = gNandFATSYS.PartRecord.prStartLBA * gNandFATSYS.Bpb50.bpbBytesPerSec;  //GET THE PARTITION BOOT SECTOR ADDRESS
	if (gNandFATSYS.PartRecord.prStartLBA)
	{
		error = HD_NFlashRead(gNandFATSYS.PartitionAddr,(UINT32) gNFLASHBuf) ;
		if (error != HW_ERR_NONE)  //READ THE  FDC DESCRIPTOR
			//AND THE RESULT STORE IN  gNFLASHBuf
		{
#ifdef _DEBUG_ON_
#endif
			return error;
		}
	}

	gNandFATSYS.BootSector.bsJump[0] = gNFLASHBuf[0] ; /* jump inst E9xxxx or EBxx90 */
	gNandFATSYS.BootSector.bsJump[1] = gNFLASHBuf[1] ;
	gNandFATSYS.BootSector.bsJump[2] = gNFLASHBuf[2] ; 
	gNandFATSYS.Bpb50.bpbBytesPerSec = ((UINT16) gNFLASHBuf[0xC] << 8) |
		(UINT16) gNFLASHBuf[0xB]; /* bytes per sector  (in sd card max=512bytes,so get the last two bytes)*/ 
	gNandFATSYS.Bpb50.bpbSecPerClust = gNFLASHBuf[0xD] ;   /* sectors per cluster */
	gNandFATSYS.Bpb50.bpbResSectors = ((UINT16) gNFLASHBuf[0xF] << 8) |
		(UINT16) gNFLASHBuf[0xE];	/* number of reserved sectors */
	gNandFATSYS.Bpb50.bpbFATs = (UINT8) gNFLASHBuf[0x10] ;  /* number of FATs */
	gNandFATSYS.Bpb50.bpbRootDirEnts = ((UINT16) gNFLASHBuf[0x12] << 8) |
		(UINT16) gNFLASHBuf[0x11]; /* number of root directory entries */	
	gNandFATSYS.Bpb50.bpbFATsecs = ((UINT16) gNFLASHBuf[0x17] << 8) |
		(UINT16) gNFLASHBuf[0x16];   /* number of sectors per FAT */
	gNandFATSYS.Bpb50.bpbSectors = ((UINT16) gNFLASHBuf[0x14] << 8) |
		(UINT16) gNFLASHBuf[0x13];  //* total number of sectors 16bits */

	//gFatTable.Total_FAT_Cluster_Number = (gBpb50.bpbFATsecs * gBpb50.bpbBytesPerSec / 2)  ; 

	if (gNandFATSYS.Bpb50.bpbSectors == 0)
	{
		gNandFATSYS.Bpb50.bpbHugeSectors = ((UINT32) gNFLASHBuf[0x23] << 24)  //* total number of sectors 32bits */
		|
			((UINT32) gNFLASHBuf[0x22] << 16) |
			((UINT32) gNFLASHBuf[0x21] << 8) |
			((UINT32) gNFLASHBuf[0x20]);
	}

	gNandFATSYS.ExtBoot.exVolumeID[3] = gNFLASHBuf[0x2A] ; /* volume ID number */
	gNandFATSYS.ExtBoot.exVolumeID[2] = gNFLASHBuf[0x29] ;
	gNandFATSYS.ExtBoot.exVolumeID[1] = gNFLASHBuf[0x28] ;
	gNandFATSYS.ExtBoot.exVolumeID[0] = gNFLASHBuf[0x27] ;

	for (i = 0; i < 11; i++)
		gNandFATSYS.ExtBoot.exVolumeLabel[i] = gNFLASHBuf[(0x2b + i)]; /* volume label */

	//get the the first fat address
	gNandFATSYS.FAT1_addr = gNandFATSYS.PartitionAddr +
		(UINT32)
		gNandFATSYS.Bpb50.bpbResSectors * (UINT32)
		gNandFATSYS.Bpb50.bpbBytesPerSec; 
	//gFatTable.FAT_Table_1 = (UINT32 *) gNandFATSYS.FAT1_addr ;
	//get the the second fat address
	gNandFATSYS.FAT2_addr = gNandFATSYS.FAT1_addr +
		(UINT32)
		gNandFATSYS.Bpb50.bpbFATsecs * (UINT32)
		gNandFATSYS.Bpb50.bpbBytesPerSec ;
	//gFatTable.FAT_Table_2 = (UINT32 *) gNandFATSYS.FAT2_addr ;
	//get the root directory address

	gNandFATSYS.RootAddr = (UINT32)
		gNandFATSYS.Bpb50.bpbFATs * (UINT32)
		gNandFATSYS.Bpb50.bpbFATsecs +
		gNandFATSYS.Bpb50.bpbResSectors;
	gNandFATSYS.RootAddr *= (UINT32) gNandFATSYS.Bpb50.bpbBytesPerSec; 
	gNandFATSYS.RootAddr += gNandFATSYS.PartitionAddr;

	//determine the count of sectors occupied by the root directory:
	gNandFATSYS.RootDirSectors = ((gNandFATSYS.Bpb50.bpbRootDirEnts * 32) +
		gNandFATSYS.Bpb50.bpbBytesPerSec -
		1) /
		gNandFATSYS.Bpb50.bpbBytesPerSec;

	//The start of the data region
	gNandFATSYS.FirstDataSector = (gNandFATSYS.Bpb50.bpbFATs * gNandFATSYS.Bpb50.bpbFATsecs) +
		gNandFATSYS.Bpb50.bpbResSectors +
		gNandFATSYS.RootDirSectors ; 

	//get the data  cluster  total
	if (gNandFATSYS.Bpb50.bpbSectors != 0)
	{
		gNandFATSYS.DataClusTot = gNandFATSYS.Bpb50.bpbSectors -
			gNandFATSYS.FirstDataSector;
	}
	else
	{
		gNandFATSYS.DataClusTot = gNandFATSYS.Bpb50.bpbHugeSectors -
			gNandFATSYS.FirstDataSector;
	}
	gNandFATSYS.DataClusTot /= gNandFATSYS.Bpb50.bpbSecPerClust;

	//get the file allocation system infomation
	if (gNandFATSYS.DataClusTot < 4085)				// FAT12
	{
		gNandFATSYS.FAT_type = PART_TYPE_FAT12 ;
	}
	else if (gNandFATSYS.DataClusTot < 65525)		// FAT16
	{
		gNandFATSYS.FAT_type = PART_TYPE_FAT16 ;
	}
	else
	{
		gNandFATSYS.FAT_type = PART_TYPE_UNKNOWN	;
		return SW_PARA_ERR;
	}

	gNandFATSYS.DIR_adrr = gNandFATSYS.RootAddr;		// Set current directory to root address

	return SW_PARA_OK;
}

/*
 * Function:	   NandNextCluster()
 * DeSCRiption:    Find the cluster that the current cluster is pointing to
 * Calls:   	   
 * Called By:      
 * Table Accessed: 
 * Table Updated:  
 * Input:   	 UINT32 current_cluster,  UINT8 mode  
 * Output:  	 none 
 * Return:  	 true and false  
 * Others:  		
 */
UINT16 NandNextCluster(UINT32 current_cluster)
{
	UINT16 calc_sec, calc_offset, next_clust;
	UINT32 addr_temp;
	UINT8 error = HW_ERR_NONE ;

	if (current_cluster <= 1)   // If cluster is 0 or 1, its the wrong cluster
	{
		return SW_PARA_ERR;
	}

	if (gNandFATSYS.FAT_type == PART_TYPE_FAT16)   // if FAT16
	{
		// FAT16 table address calculations
		calc_sec = current_cluster /
			(gNandFATSYS.Bpb50.bpbBytesPerSec / 2) +
			gNandFATSYS.Bpb50.bpbResSectors;
		calc_offset = 2 * (current_cluster % (gNandFATSYS.Bpb50.bpbBytesPerSec / 2));

		addr_temp = gNandFATSYS.PartitionAddr + (calc_sec * 0x200);
		if (addr_temp != gNandFFBuffAddr)
		{
			error = HD_NFlashRead(addr_temp,(UINT32) gNFLASHBuf) ;
			if (error != HW_ERR_NONE)
			{
				return error;
			}
		} 
		next_clust = (((UINT16) gNFLASHBuf[calc_offset + 1]) << 8) |
			gNFLASHBuf[calc_offset];
	}
	else	// not FAT12 or FAT16, return 0
	{
		return SW_PARA_ERR;
	}

	return (next_clust);
}

/*
 * Function:	   NandQuickFormat
 * DeSCRiption:   Quickformat of a card (erase cluster table and root directory)
 * Calls:   	   
 * Called By:      
 * Table Accessed: 
 * Table Updated:  
 * Input:   	   
 * Output:  	   
 * Return:  	   
 * Others:  	   
 */
UINT8 NandQuickFormat(void)
{
	UINT16 c ;
	UINT32 temp_addr;
	UINT32 temp_addr1 ;
	UINT32 temp_addr2 ;
	UINT32 temp_addr3 ;
	UINT8 error = HW_ERR_NONE;

	for (c = 0;
		c < gNandFATSYS.Bpb50.bpbBytesPerSec;
		c++)   // initialize  gNFLASHBuf to  all zero
		gNFLASHBuf[c] = 0;

	temp_addr = gNandFATSYS.FAT1_addr + 0x200;  
	temp_addr3 = gNandFATSYS.RootAddr + (0x200 * gNandFATSYS.RootDirSectors);
	while (temp_addr < temp_addr3)
	{
		error = HD_NFlashWrite((UINT32) gNFLASHBuf,temp_addr) ;
		if (error != HW_ERR_NONE)
		{
			return error;
		}

		temp_addr += 0x200;
	}	
	gNFLASHBuf[0] = 0xF8;
	gNFLASHBuf[1] = 0xFF;
	gNFLASHBuf[2] = 0xFF;
	if (gNandFATSYS.FAT_type == PART_TYPE_FAT16)
	{
		gNFLASHBuf[3] = 0xFF;
	}

	temp_addr1 = gNandFATSYS.FAT1_addr ;
	temp_addr2 = gNandFATSYS.FAT2_addr ;

⌨️ 快捷键说明

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