hdd_fs.c
资源名称:8202s.rar [点击查看]
上传用户:poi891205
上传日期:2013-07-15
资源大小:9745k
文件大小:14k
源码类别:
DVD
开发平台:
C/C++
- /*=================================================================
- hdd_fs.c: Some FileSystem Info Function for HDD
- 2002-04-28 10:00AM Created by Verdure
- 2002-06-12 08:42AM Modified by cyue
- Copyright(c)2000-2002 by Worldplus Technology (ShenZhen) Co., Ltd.
- ALL RIGHTS RESERVED
- =================================================================*/
- //#include <string.h>
- #include <math.h>
- #include "config.h"
- #include "set.h"
- #include "global.h"
- #include "hdd_if.h"
- #include "hdd_info.h"
- #include "stdlib.h"
- #ifdef USE_HDD // cyue: Skip code if not use hdd!!
- #define DBG_HDD_FS
- #ifndef DBG_HDD_FS
- #undef printf
- #define printf(f, a...) {}
- #endif
- #undef print_block
- #define print_block(x,y) {}
- UINT8 FS_Emu_stricmp(UINT8* s1, UINT8* s2)
- {
- UINT16 i;
- #if 1
- for(i=0;(i<11);i++,s1++,s2++)
- { if((!*s1)&&(!*s2))
- return 0;
- if(((*s1)&~0x20)!=((*s2)&~0x20))
- return 1;
- }
- //printf("Match: %s -- %s n",s1-11,s2-11);
- return 0;
- #else
- //while(i<11)
- while(string1[i]!=' '||string2[i]!=' ')
- {
- if(string1[i]==string2[i])
- i += 1;
- else if((string1[i]>=0x41&&string1[i]<=0x5A)||(string1[i]>=0x61&&string1[i]<=0x7A))
- if(abs(string1[i]-string2[i])==0x20)
- i += 1;
- else return 1;
- else return 1;
- }
- return 0;
- #endif
- }
- UINT8 HDD_Mount(void)
- {
- //HDD_Reset(); //cyue: need reset too? 2002-07-03 11:10AM
- HD_EXIST = HDD_DEV_Existence();
- if (!HD_EXIST)
- {
- HDD_MakeInfo(&HDD_Info,Info_buff);
- printf("nStart_LBA:%dn",Start_LBA);
- FS_ReadInfoSec(Start_LBA,1);
- FS_check_BootSec(Info_buff);
- return 1;
- }
- return 0;
- // return 1;
- }
- void HDD_Backup_FAT(void)
- {
- UINT32 i=BPB.FATSize32;
- UINT16 SecCnt,j;
- SecCnt=sizeof(Info_buff)>>BPB.BytesPerSecPwr;
- if(BPB.NumFATs==2)
- {
- if(i%SecCnt)
- {
- FS_ReadInfoSec(BPB.FAT1_Start,i%SecCnt);
- HDD_WriteSec(Info_buff,BPB.FAT2_Start,i%SecCnt);
- for(j=(i%SecCnt),i-=(i%SecCnt);i>0;i-=SecCnt,j+=SecCnt)
- {
- FS_ReadInfoSec(BPB.FAT1_Start+j,SecCnt);
- HDD_WriteSec(Info_buff,BPB.FAT2_Start+j,SecCnt);
- }
- }
- else for(j=0;i>0;i-=SecCnt,j+=SecCnt)
- {
- FS_ReadInfoSec(BPB.FAT1_Start+j,SecCnt);
- HDD_WriteSec(Info_buff,BPB.FAT2_Start+j,SecCnt);
- }
- }
- }
- PLAY_BLOCK *HDD_BuildPlayBlock(FILE_INFO *pfdb,PLAY_BLOCK *pblock)
- {
- // Generate Cluster blocks of this file
- UINT32 st,next,lba_count=LBA_COUNT(pfdb->fsize),sec_count=BPB.SecPerClus;
- int i;
- for(i=0,st=pfdb->start_clus,next=0;
- i<MAX_FILE_LINK_ITEM;
- i++)
- { pblock->block[i].lba=0;// End Mark first
- pblock->block[i].len=0;
- if(st>=0x0FFFFFFF)//End?
- { break;
- }
- pblock->block[i].lba=FS_Clus2LBA(st);
- do
- { if(lba_count>=sec_count)
- { pblock->block[i].len+=sec_count;
- lba_count-=sec_count;
- }
- else
- { pblock->block[i].len+=lba_count;
- }
- //printf("n$$ next clus:0x%lx ",next);
- next=FS_Get_NextClus(st);
- st++;
- // cyue test multiblock!! 2002-06-17 03:26PM
- //if(pblock->block[i].len>0x4000)
- // st=0;
- }
- while(next==st);
- st=next;
- }
- pblock->curr_blk=0; pblock->curr_lba=pblock->block[0].lba;
- #ifdef DBG_HDD_FS
- for(i=0;i<MAX_FILE_LINK_ITEM;i++)
- { if(!(pblock->block[i].lba)) break;
- printf("### Blk:%d lba:0x%lx len:0x%lxn",
- i,pblock->block[i].lba,pblock->block[i].len);
- }
- #endif
- return pblock;
- }
- FILE_INFO *GetFileInfo(FAT32_FDB *fdb,FILE_INFO *pfinfo)
- {
- UINT16 i;
- printf(__FUNCTION__);
- #if 0
- // copy Filename (8.3 format)
- { UINT8 *d,*s;
- for(i=0,d=pfinfo->name,s=(UINT8*)fdb
- ;i<11; i++,d++,s++)
- *d=*s;
- *d=0;
- }
- #endif
- Data_Start_Clus=pfinfo->start_clus=(UINT32)((fdb->start_hi)<<16|(fdb->start_lo));
- if(pfinfo->start_clus==0) //cyue: '..' to root!!
- {
- printf(" It's Root !! ");
- Data_Start_Clus=pfinfo->start_clus=BPB.RootClus;
- }
- pfinfo->start_lba=FS_Clus2LBA(pfinfo->start_clus);//((pfinfo->start_clus-2)<<BPB.SecPerClusPwr)+BPB.Data_Area_Start;
- pfinfo->fsize=fdb->fsize;
- pfinfo->attrib=fdb->attrib;
- i=BPB.BytesPerSecPwr;
- #ifdef DBG_HDD_FS
- printf("### start_lba: 0x%lxn### size: 0x%lx start_clus 0x%lxnn",
- pfinfo->start_lba,
- pfinfo->fsize,
- pfinfo->start_clus);
- #endif
- return pfinfo;
- }
- /*
- ========================================================
- = FILE_INFO* SearchFile(UINT32 dir_clus,UINT8 *name)
- = Search file(or subdir) in dir_clus cluster
- = ARGUMENT(S):
- = UINT32 dir_clus: the start cluster of dir to search file
- = UINT8 *fname: filename to play, 11 CHAR in 8.3
- = format
- = RETURN VALUE:
- = FILE_INFO: info of this file.
- = GLOBAL USING:
- = Info_buff: buffer to read a sector (512 bytes)
- = BPB: HD infos
- = SIDE EFFECT:
- = Info_buff: Contains Last FDB Sector that have this file
- = Data_Start_Clus: first clustor of file
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- FILE_INFO* SearchFile(UINT32 dir_clus,UINT8 *name)
- {
- UINT32 lba;
- UINT32 i,offset;
- static FILE_INFO finfo;
- for(;dir_clus<0x0FFFFFFF;dir_clus=FS_Get_NextClus(dir_clus))
- {
- lba=FS_Clus2LBA(dir_clus);
- for(i=BPB.SecPerClus;i;i--,lba++)
- {
- printf("### DirLBA:0x%lxn",lba);
- FS_ReadInfoSec(lba,1);
- for(offset=0;offset<BPB.BytesPerSec;offset+=32)
- {
- switch(Info_buff[offset])
- {
- case 0: // free FDB, End!!
- printf("nNo Such File Here!! %sn",name);
- BPB.curr_fdb_lba=lba; // store file info location
- BPB.curr_fdb_offset=offset; // store file info location
- return NULL;
- case 0xE5: //deleted FDB
- continue;
- #if 0
- case '.'://Parrent or this dir
- if(Info_buff[offset+1]=='.')
- { printf("n Parrent Dir(..)");
- }
- else
- { printf("n This Dir(.)");
- }
- continue;
- #endif
- default:
- if(FS_Emu_stricmp(&Info_buff[offset],name)==0)
- { BPB.curr_fdb_lba=lba; // store file info location
- BPB.curr_fdb_offset=offset; // store file info location
- GetFileInfo((FAT32_FDB*)(&Info_buff[offset]),&finfo);
- finfo.fdb_lba=lba;
- finfo.fdb_offset=offset;
- return &finfo;
- }
- }
- }
- }
- }
- return NULL;
- }
- /*
- ========================================================
- = FILE_INFO* HDD_SearchFile(UINT8* name_buff)
- = Search file in path/path/.../fname
- = ARGUMENT(S):
- = UINT8 *fname: path/../filename to search,
- = CHAR in 8.3 format
- = RETURN VALUE:
- = FILE_INFO: info of files
- = GLOBAL USING:
- = BPB: HD infos
- = SIDE EFFECT:
- = Info_buff: Contains Last FDB Sector that have this file
- = Data_Start_Clus: first clustor of file
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- FILE_INFO* HDD_SearchFile(UINT8 *s)
- {
- FILE_INFO* ret=NULL;
- UINT32 clus,i;
- UINT8 part_name[12],*d;
- //printf(__FUNCTION__":n");
- if(*s=='/') //FROM ROOT
- {
- clus=BPB.RootClus;
- s++;
- }
- else //FROM CURRENT
- {
- clus=BPB.CurrDirClus;
- }
- do
- {
- // clear part_name buff;
- for(i=0,d=part_name;i<11;i++,d++) *d=' ';
- *d=0;
- d=part_name;
- // clear leading '/'
- if(*s=='/')s++;
- if(*s=='.') // self or parrent?
- {
- *(d++)='.';
- if(*(s+1)=='.')// parrent?
- {
- *(d++)='.'; s++;
- }
- *d=0; s++;
- }
- else
- { //copy part_name
- while((*s!='/')&&(*s!=0))
- {
- if(*s=='.')
- {
- d=&part_name[8];
- }
- else
- {
- *d++=*s;
- }
- s++;
- }
- *d=0;
- }
- // Get Part-File Info
- ret=SearchFile(clus,part_name);
- if(ret==NULL) return NULL;
- if(ret->attrib&FAT_ATTR_D) // Is this a subdir file?
- {
- if(*s==0) // End Search string => just get the dir info
- {
- printf(" CD to new dir (%s)!!",part_name);
- return ret;
- }
- }
- else // A real file finded
- { //printf(" Final Name: %sn",ret->name);
- return ret;
- }
- clus=ret->start_clus;
- }while(1);
- }
- FILE_INFO* HDD_cd(UINT8 *s)
- {
- FILE_INFO* ret;
- ret=HDD_SearchFile(s);
- if(ret==NULL) return NULL;
- printf("n*** BPB.CurrDirClus: 0x%lx ",BPB.CurrDirClus);
- if(ret->attrib&FAT_ATTR_D) // Is this a subdir file?
- {
- BPB.CurrDirClus=ret->start_clus;
- return ret;
- }
- return NULL; // No such dir, but such file!!
- }
- extern UINT32 Last_FAT_Lba;
- void HDD_dir()
- {
- UINT32 dir_clus,lba;
- UINT32 i,j;
- UINT8 name[12];
- printf("nDIR:n");
- Last_FAT_Lba=0x0FFFFFFF;
- for(dir_clus=BPB.CurrDirClus;
- dir_clus<0x0FFFFFFF;
- dir_clus=FS_Get_NextClus(dir_clus))
- {
- lba=FS_Clus2LBA(dir_clus);
- for(i=BPB.SecPerClus;i;i--,lba++)
- {
- FS_ReadInfoSec(lba,1);
- for(j=0;j<BPB.BytesPerSec;j+=32)
- {
- UINT8 *s;
- s=&Info_buff[j];
- if(*s==0)
- {
- printf("n");
- return; // No more file in dir
- }
- if(*s==0xE5) continue; // deleted file
- memcpy(name,s,11);
- name[11]=0;
- printf("t[ %s ]",name);
- }
- }
- }
- }
- #if 0
- void HDD_LinkFileClus(void)
- {
- UINT32 cluster=Data_Start_Clus;
- UINT32 i;
- read_le32(Info_buff+BPB.BytesPerSec)=FS_Clus2LBA(cluster);//((cluster-2)<<BPB.SecPerClusPwr)+BPB.Data_Area_Start;
- for(i=4;i<4096;i+=4)
- {
- cluster=FS_Get_NextClus(cluster);
- /* save the first LBA of every cluster */
- read_le32(Info_buff+BPB.BytesPerSec+i)=FS_Clus2LBA(cluster);//((cluster-2)<<BPB.SecPerClusPwr)+BPB.Data_Area_Start;
- }
- //print_block(Info_buff+BPB.BytesPerSec,512*8);
- }
- #endif
- /*
- ========================================================
- = UINT32 HDD_FindFreeClus(UINT32 first_clus, UINT32 count)
- = Find Free Cluster(s) (by count number)
- = ARGUMENT(S):
- = UINT32 first_clus: find free clusters after this
- = UINT32 count: count of free clusters needs
- =
- = RETURN VALUE:
- = 0xFFFFFFFF: No such more free clusters
- = otherwise: start clus of the linked clusters
- = GLOBAL USING:
- = BPB: HD infos
- = SIDE EFFECT:
- = Info_buff: Contains Last FDB Sector that have this file
- = Data_Start_Clus: first clustor of file
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- UINT32 HDD_FindFreeClus(UINT32 first_clus, UINT32 count)
- {
- UINT32 fat_lba=BPB.FAT1_Start+(first_clus>>(BPB.BytesPerSecPwr-2));
- //UINT32 cluster;
- UINT32 offset;//clus_ret,
- while(fat_lba<BPB.FAT2_Start) //at the end of FAT1
- {
- FS_ReadInfoSec(fat_lba,1);
- for(offset=0;offset<BPB.BytesPerSec;offset+=4)
- {
- if(read_le32(Info_buff+offset)==0)
- {
- Data_Next_Clus=(((fat_lba-BPB.FAT1_Start)<<7)+(offset>>2));
- read_le32(Info_buff+offset)=0xfffffff;
- HDD_WriteSec(Info_buff,fat_lba, 1);
- printf("%lx,%lx,%lxn",fat_lba,offset,Data_Next_Clus);
- return 1;
- }
- }
- fat_lba ++;
- }
- if(fat_lba==BPB.FAT2_Start)
- printf("The HD is Fulln");
- return 0;
- }
- /* Fill the empty FDB */
- UINT8 HDD_Set_FDB(UINT8* name_buff)
- {
- UINT32 lba;
- UINT16 offset=0;
- UINT8 file_flag=0;
- UINT8 i;
- if(1) lba=BPB.Data_Area_Start; //under the root directory
- else {};
- while(1)
- {
- FS_ReadInfoSec(lba,1);
- File_FDB_Lba=lba;
- do
- {
- file_flag=Info_buff[offset];
- if((file_flag==0xE5)||(file_flag==0x00))
- {
- //set name
- for(i=0;i<11;i++)
- Info_buff[offset+i]=name_buff[i];
- Info_buff[offset+11]=0x21;
- //set the access cluster
- read_le16(Info_buff+offset+20)=(Data_Next_Clus>>16)&0xfff;
- read_le16(Info_buff+offset+26)=Data_Next_Clus&0xffff;
- HDD_WriteSec(Info_buff, lba, 1);
- //print_block(Info_buff,BPB.BytesPerSec);
- return 1;
- }
- offset += 32;
- } while(offset<BPB.BytesPerSec);
- lba += 1;
- }
- }
- FILE_INFO *HDD_CreatFile(UINT8* name_buff)
- {
- UINT32 clus_lba=BPB.FAT1_Start;
- UINT32 data_lba;
- //UINT16 offset=0;
- //UINT8 file_flag=0;
- //UINT8 i;
- /* Creat a file in the first layer of HD */
- if(HDD_FindFreeClus(0,clus_lba)!=0)
- HDD_Set_FDB(name_buff);
- else
- {
- printf("Can't Creat File,HD is Fulln");
- return 0;
- }
- Data_Curr_Clus=Data_Next_Clus;
- //while(1)
- {
- data_lba=FS_Clus2LBA(Data_Curr_Clus);//BPB.Data_Area_Start+((Data_Curr_Clus-2)<<BPB.SecPerClusPwr);
- //HDD_WriteSec(buff,data_lba,seccnt);
- //if("It is over") return 1;
- clus_lba=(Data_Curr_Clus>>7)+BPB.FAT1_Start;
- if(HDD_FindFreeClus(0,clus_lba)!=0)
- {
- FS_ReadInfoSec(clus_lba,1);
- read_le32(Info_buff+ (Data_Curr_Clus>>(7-2)) )=Data_Next_Clus;
- HDD_WriteSec(Info_buff,clus_lba,1);
- }
- else
- {
- printf("Can't Creat File,HD is Fulln");
- return 0;
- }
- printf("%lx,%lxn",Data_Curr_Clus,Data_Next_Clus);
- //print_block(Info_buff,BPB.BytesPerSec);
- Data_Curr_Clus=Data_Next_Clus; return 0;
- }
- }
- /* Fill the FAT Chain with zero */
- void HDD_ResetFAT(void)
- {
- Data_Curr_Clus=Data_Start_Clus;
- FS_ReadInfoSec((Data_Curr_Clus>>7)+BPB.FAT1_Start,1);
- while(Data_Curr_Clus!=0x0fffffff)
- {
- FS_Get_NextClus(Data_Curr_Clus);
- read_le32(Info_buff+((Data_Curr_Clus&0x7F)<<2))=0;
- HDD_WriteSec(Info_buff,(Data_Curr_Clus>>7)+BPB.FAT1_Start,1);
- if((Data_Next_Clus>>7)!=(Data_Curr_Clus>>7))
- FS_ReadInfoSec((Data_Next_Clus>>7)+BPB.FAT1_Start,1);
- Data_Curr_Clus=Data_Next_Clus;
- }
- read_le32(Info_buff+((Data_Curr_Clus&0x7F)<<2))=0;
- HDD_WriteSec(Info_buff,(Data_Curr_Clus>>7)+BPB.FAT1_Start,1);
- }
- UINT32 HDD_DelFile(UINT8* name_buff)
- {
- FILE_INFO *pfinfo;
- if(NULL==(pfinfo=HDD_SearchFile(name_buff))) return -1; //No File ERR
- Info_buff[pfinfo->fdb_offset]=0xE5;// Mark file deleted!!
- HDD_WriteSec(Info_buff,pfinfo->fdb_lba,1);
- HDD_ResetFAT();//clear FAT chain
- printf("File Deletedn");
- return 0; //No ERR
- }
- #endif //USE_HDD