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

📄 fat.c

📁 基于s3c2410、2440的从SD卡引导Linux内核程序
💻 C
📖 第 1 页 / 共 2 页
字号:
		for(i=0;i<SectorsPerCluster;i++)	//读遍整个簇
		{
			ReadS(ClusterToSector(kk) + i,(unsigned int *)BUFFER,512);	//读取活动目录所在簇
			for(j=0;j<(BytesPerSector/32);j++)			//分析所有目录结构,每个目录结构32字节
			{
				dirfile = (DirFile *) (BUFFER + (j * 32));
				switch (dirfile->deName[0])
				{
					case SLOT_DELETED:	//文件已被删除
						break;			//跳过分析
					case SLOT_EMPTY:	//目录项为空
						Uart_Printf(0,"############################################\n");
						return ;		//不再继续分析
					default:
						if(((dirfile->deAttributes) & ATTR_LONG_FILENAME) == ATTR_LONG_FILENAME)	//长文件名不作分析
							break;

						for(k=0;k<8;k++)
							Uart_SendByte(0,dirfile->deName[k]);
						if(((dirfile->deAttributes) & ATTR_DIRECTORY) != ATTR_DIRECTORY)	//是否为子目录
						{
							//不是子目录,可能是卷标 或 文件
							if(((dirfile->deAttributes) & ATTR_VOLUME) == ATTR_VOLUME)	//是否为卷标
							{
								Uart_Printf(0,"    <Volume>");
							}
							else
							{
								Uart_SendByte(0,'.');
								for(k=0;k<3;k++)
									Uart_SendByte(0,dirfile->deExtension[k]);
							}
						}
						else
						{
							Uart_Printf(0,"    <DIR>");
						}
						Uart_SendByte(0,0x0d);   //回车换行
						Uart_SendByte(0,0x0a);

						break;
				}
			}
		}
		kk = FatNextCluster(kk);	//查处下一个簇号
	}
}
#endif
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------
//Find_DirFile(文件名,后缀名) ---- 找文件
//Find_DirFile(文件夹名,(unsigned char *)"") ---- 找文件夹
//找到返回"1",否则返回"0"
//-----------------------------------------------------------------------
int Find_DirFile(unsigned char * DFName,unsigned char * DFExt)
{
	unsigned char tmpName[8] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20};	//先全部预存入“空格”
	unsigned char tmpExt[3] = {0x20,0x20,0x20};								//先全部预存入“空格”
	unsigned char i,j,m;
	unsigned int k;
	DirFile	*dirfile;

	i = 0;	
	while(*DFName)	//转存对象名
	{
		tmpName[i] = (*DFName >= 'a' && *DFName <= 'z') ? *DFName-32 : *DFName;	//自动匹配为大写
		i++;
		DFName++;

	}

	if(*DFExt == Null_Ext)	//判断要的是文件还是目录
	{
		Flags.isDir = 1;	//是子目录
		Flags.isFile = 0;
	}
	else
	{
		i = 0;
		while(*DFExt)	//转存对象名
		{
			tmpExt[i] = (*DFExt >= 'a' && *DFExt <= 'z') ? *DFExt-32 : *DFExt;	//自动匹配为大写
			i++;
			DFExt++;
		}
		Flags.isDir = 0;
		Flags.isFile = 1;	//是文件
	}

	k = DirCluster;	//当前目录所在簇号

	while(1)
	{
		for(i=0;i<SectorsPerCluster;i++)	//读遍整个簇
		{
			ReadS(ClusterToSector(k) + i,(unsigned int *)BUFFER,512);	//读取活动目录所在簇
			for(j=0;j<(BytesPerSector/32);j++)	//分析所以目录结构,每个目录结构32字节
			{
				dirfile = (DirFile *) (BUFFER + (j * 32));
				switch (dirfile->deName[0])
				{
					case SLOT_DELETED:	//文件已被删除
						break;			//跳过分析
					case SLOT_EMPTY:	//目录项为空
						#if debug
						Uart_Printf(0,"Can not Find the Dir or File !\n");
						#endif
						return 0;		//不再继续分析
					default:
						if(((dirfile->deAttributes) & ATTR_LONG_FILENAME) == ATTR_LONG_FILENAME)	//长文件名不作分析
							break;

						if(((dirfile->deAttributes) & ATTR_VOLUME) == ATTR_VOLUME)	//卷标不作分析
							break;

						if(Flags.isDir)		//查找对象是子目录
						{
							if(((dirfile->deAttributes) & ATTR_DIRECTORY) != ATTR_DIRECTORY)	//查找目录时,没有子目录标志就退出
								goto BREAK;//回跳
						}
						if(Flags.isFile)	//查找对象是文件
						{
							if(((dirfile->deAttributes) & ATTR_DIRECTORY) == ATTR_DIRECTORY)	//查找文件时,有子目录标志就退出
								goto BREAK;//回跳

							for(m=0;m<3;m++)	//比较后缀名
							{
								//自动匹配为大写,并进行比较
								if(dirfile->deExtension[m] != tmpExt[m])
									goto BREAK;//回跳
							}
						}
						
						for(m=0;m<8;m++)	//比较文件名
						{
							if(dirfile->deName[m] != tmpName[m])
								goto BREAK;
						}
						//保存文件、子目录的起始簇号
						if(Flags.isFat16)
							DirFileCluster = dirfile->deStartCluster;
						if(Flags.isFat32)
							DirFileCluster = ((DWORD)(dirfile->deHighClust) << 16) + (dirfile->deStartCluster);
						//保存文件、子目录的大小
						DirFile_Size = dirfile->deFileSize;
						
						#if debug
						if(Flags.isFile)
							Uart_Printf(0,"Found the File : ");
						else
							Uart_Printf(0,"Found the Dir : ");

						for(m=0;m<8;m++)
							Uart_SendByte(0,dirfile->deName[m]);

						if(Flags.isFile)
						{
							Uart_SendByte(0,'.');
							for(m=0;m<3;m++)
								Uart_SendByte(0,dirfile->deExtension[m]);
						}
						Uart_SendByte(0,0x0d);   //回车换行
						Uart_SendByte(0,0x0a);

						Uart_Printf(0,"DirFileCluster = %d\n",DirFileCluster);
						Uart_Printf(0,"DirFile_Size = %d\n",DirFile_Size);
						Uart_Printf(0,"############################################\n");
						#endif
						
						return 1;

				BREAK:	
						#if debug
						Uart_Printf(0,"Cheak next---->\n");
						#endif
						break;	//回跳,继续查找
				}
			}
		}
		k = FatNextCluster(k);	//查处下一个簇号
	}
}


int CD(unsigned char * DFName)
{
	unsigned int i;
	i=Find_DirFile(DFName,(unsigned char *)"");
	if(i)
	{
		if(DirFileCluster==0)
			DirCluster=RootDirCluster;
		else
			DirCluster=DirFileCluster;
	}
	return i;	//成功返回"1"
}

void DIR(void)
{
	List_DirFile();
}

int LoadFile(unsigned int * DesAddr,unsigned char * DFName,unsigned char * DFExt)
{
	unsigned int i,k,Tmp_Cluster,Tmp_Sector,temp;
	unsigned int F_T_C,F_T_S,F_T_B;	//文件大小 = 簇 + 扇区 + 字节
	i=Find_DirFile(DFName,DFExt);
	if(i)
	{
		Tmp_Cluster=DirFileCluster;
		F_T_C=DirFile_Size/(SectorsPerCluster*BytesPerSector);
		F_T_S=(DirFile_Size%(SectorsPerCluster*BytesPerSector))/BytesPerSector;
		F_T_B=DirFile_Size%BytesPerSector;
#if 1//debug
		Uart_Printf(0,"############################################\n");
		Uart_Printf(0,"Load File %s.%s To 0x%x\n",DFName,DFExt,DesAddr);
		//Uart_Printf(0,"F_T_C = %d\n",F_T_C);
		//Uart_Printf(0,"F_T_S = %d\n",F_T_S);
		//Uart_Printf(0,"F_T_B = %d\n",F_T_B);
		Uart_Printf(0,"It takes %d Clusters %d Sectors and %d Bytes\n",F_T_C,F_T_S,F_T_B);
		Uart_Printf(0,"############################################\n");
#endif
		for(k=0;k<F_T_C;k++)
		{
			ReadS(ClusterToSector(Tmp_Cluster),DesAddr,SectorsPerCluster*BytesPerSector);
			temp=FatNextCluster(Tmp_Cluster);
			Tmp_Cluster=((temp==0)?Tmp_Cluster:temp);
			DesAddr=DesAddr+SectorsPerCluster*BytesPerSector/4;
		}
		Tmp_Sector=ClusterToSector(Tmp_Cluster);
		for(k=0;k<F_T_S;k++)
		{
			ReadS(Tmp_Sector+k,DesAddr,BytesPerSector);
			DesAddr=DesAddr+BytesPerSector;
		}
		ReadS(Tmp_Sector+F_T_S,DesAddr,BytesPerSector);
	}
	return i;	//成功返回"1"
}

char *ReadCmd(unsigned char * DFName,unsigned char * DFExt)
{
	unsigned int k;
	char* p;

	if(Find_DirFile(DFName,DFExt))
	{
		Uart_Printf(0,"Found the CmdLine.ini !\n");
		p=(char*)malloc(DirFile_Size);
		ReadS(ClusterToSector(DirFileCluster),(unsigned int *)BUFFER,BytesPerSector);
		for(k=0;k<DirFile_Size;k++)
		{
			Uart_Printf(0,"%s", BUFFER[k]);
			p[k]=BUFFER[k];
		}
		
		return p;
	}
	Uart_Printf(0,"Can not find the CmdLine.ini !\n");
	return NULL;
}

⌨️ 快捷键说明

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