hdd_info.c
资源名称:8202s.rar [点击查看]
上传用户:poi891205
上传日期:2013-07-15
资源大小:9745k
文件大小:11k
源码类别:
DVD
开发平台:
C/C++
- /*=================================================================
- hdd_info.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 "set.h"
- #include "config.h"
- #include "global.h"
- #include "hdd_if.h"
- #include "hdd_info.h"
- #ifdef USE_HDD // cyue: Skip code if not use hdd!!
- #define DBG_HDD_INFO
- #ifndef DBG_HDD_INFO
- #undef printf
- #undef print_block
- #define printf(f, a...) {}
- #define print_block(x,y) {}
- #endif
- // Global for FAT FS
- //#define MAX_INFO_BUFF (512*2)
- //UINT8 Info_buff[MAX_INFO_BUFF];
- UINT8 HD_EXIST;
- HDD_INFO HDD_Info;
- BPB_INFO BPB;
- //FAT32_FDB* FDB;
- UINT32 Data_Start_Clus;
- UINT32 File_FDB_Lba;
- UINT32 Start_LBA;
- UINT32 Data_Curr_Clus;
- UINT32 Data_Next_Clus;
- int FS_Get_BPBPara(UINT8*);
- /*
- ========================================================
- = PARTITION_INFO* HDD_AddPE(HDD_INFO* HDD_Info,UINT32 MBR,PARTITION_ENTRY* pe)
- =
- = Add partition entry to HDD_Info;
- =
- = ARGUMENT(S):
- = HDD_INFO* HDD_Info : HDD infos
- = UINT32 MBR : MBR LBA of current partition
- = PARTITION_ENTRY* pe
- = RETURN VALUE:
- = NONE
- = GLOBAL USING:
- = NONE
- = SIDE EFFECT:
- = NONE
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- PARTITION_INFO* HDD_AddPE(HDD_INFO* HDD_Info,UINT32 MBR,PARTITION_ENTRY* pe)
- {
- PARTITION_INFO *pi=0;
- #ifdef PI_USE_MATRIX
- int j;
- for(j=0;j<MAX_PART_INFO;j++)
- if(!HDD_Info->part[j].type) break;
- pi=&HDD_Info->part[j];
- #else
- PARTITION_INFO *hddpi;
- pi=(PARTITION_INFO*)malloc(sizeof(PARTITION_INFO));
- #endif
- pi->type=pe->type;
- pi->next=NULL;
- pi->startlba=MBR+(pe->secfrommbr_hi<<16|pe->secfrommbr_lo);
- Start_LBA=pi->startlba;
- pi->seccnt=pe->seccnt_hi<<16|pe->seccnt_lo;
- pi->act=pe->act;
- #ifndef PI_USE_MATRIX
- if(HDD_Info->partition_list==NULL)
- HDD_Info->partition_list=pi;
- else
- { for(hddpi=HDD_Info->partition_list;hddpi->next!=NULL;hddpi=hddpi->next);
- hddpi->next=pi;
- }
- #endif
- return pi;
- }
- /*
- ========================================================
- = int HDD_MakeInfo(HDD_INFO *HDD_Info,UINT8* buff)
- = Make HDD partition infos to terminal
- =
- = ARGUMENT(S):
- = HDD_INFO* HDD_Info : HDD infos
- = UINT8* buff : buff to put sector infos
- = RETURN VALUE:
- = NONE
- = GLOBAL USING:
- = NONE
- = SIDE EFFECT:
- = NONE
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- int HDD_MakeInfo(HDD_INFO *HDD_Info,UINT8* buff)
- {
- PARTITION_ENTRY *pe;
- UINT32 i;
- //UINT16 c=0,h=0,s=0;
- UINT32 MBR=0,secfrommbr,seccnt;
- #ifdef PI_USE_MATRIX
- for(i=0;i<MAX_PART_INFO;i++)
- HDD_Info->part[i].type=0;
- #else
- HDD_Info->partition_list=NULL;
- #endif
- HDD_ReadSec(buff,MBR,1);
- for(i=0;i<4;i++)//Parse Primary partitions
- {
- pe=(PARTITION_ENTRY*)(buff+ 0x1be +i*16);
- secfrommbr=pe->secfrommbr_hi<<16|pe->secfrommbr_lo;
- seccnt=pe->seccnt_hi<<16|pe->seccnt_lo;
- if (!pe->type)
- continue;
- if ((pe->type==0x0f)||(pe->type==0x05))
- {
- // Extended Partition!!
- UINT32 MBREXT=MBR+secfrommbr;
- do
- {
- HDD_ReadSec(buff,MBREXT,1);
- printf("n Parse EXT %lu... ",MBREXT);
- print_sector(buff+512);
- if (*(UINT16*)(buff+512+510)!=0xaa55)
- {
- printf("n Fail EXT Partition!!");
- break;
- }
- pe=(PARTITION_ENTRY*)(buff+512+ 0x1be);
- secfrommbr=pe->secfrommbr_hi<<16|pe->secfrommbr_lo;
- seccnt=pe->seccnt_hi<<16|pe->seccnt_lo;
- printf("n--secfrommbr=%lx--n",secfrommbr);
- if (pe->type)
- {
- HDD_AddPE(HDD_Info,MBREXT,pe);
- }
- else break;
- pe=(PARTITION_ENTRY*)(buff+512+ 0x1be +16);
- secfrommbr=pe->secfrommbr_hi<<16|pe->secfrommbr_lo;
- seccnt=pe->seccnt_hi<<16|pe->seccnt_lo;
- printf("n--secfrommbr=%lx--n",secfrommbr);
- if (MBREXT+secfrommbr >= HDD_Info->seccnt)
- //if(!pe->type)
- {
- printf("nLast MBR!!");
- MBREXT=HDD_Info->seccnt - seccnt;
- //break;
- }
- else
- {
- MBREXT+=secfrommbr;
- }
- }while((pe->type==0x0f)||(pe->type==0x05));
- }
- else
- {
- HDD_AddPE(HDD_Info,MBR,pe);
- printf("primary=%d",i);
- }
- }
- return 0;
- }
- /*
- ========================================================
- = void HDD_Dump_Info(HDD_INFO* HDD_Info)
- = Dump HDD partition infos to terminal
- =
- = ARGUMENT(S):
- = HDD_INFO* HDD_Info : HDD infos
- = RETURN VALUE:
- = NONE
- = GLOBAL USING:
- = NONE
- = SIDE EFFECT:
- = NONE
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- void HDD_Dump_Info(HDD_INFO* HDD_Info)
- { PARTITION_INFO *pi;
- int i;
- printf("nnHDD: C=%lu H=%lu S=%lu",HDD_Info->cyl,HDD_Info->head,HDD_Info->sec);
- printf("nHDD total Sectors = %lu, %lu MB ",HDD_Info->seccnt,HDD_Info->seccnt/2000);
- printf("nPAR A TYPE START LBA COUNT");
- #ifdef PI_USE_MATRIX
- for(i=0;i<MAX_PART_INFO;i++)
- { pi= &HDD_Info->part[i];
- if(pi->type)
- printf("n%2d %02x %02x %10lu %10lun",i,pi->act,pi->type,pi->startlba,pi->seccnt);
- }
- #else
- for(i=0,pi=HDD_Info->partition_list;pi!=NULL;i++,pi=pi->next)
- { printf("n%2d %02x %02x %10lu %10lu",i,pi->act,pi->type,pi->startlba,pi->seccnt);
- }
- #endif
- }
- /*
- ========================================================
- = static int FS_check_SecPerClus(unsigned SecPerClus)
- =
- =
- = ARGUMENT(S):
- = unsigned SecPerClus :
- = RETURN VALUE:
- = int
- = GLOBAL USING:
- = NONE
- = SIDE EFFECT:
- = NONE
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- static int FS_check_SecPerClus(unsigned SecPerClus)
- {
- unsigned char log;
- for(log = 0; log < 8; log++)
- {
- if(SecPerClus & 1)
- {
- SecPerClus >>= 1;
- return (SecPerClus != 0) ? -1 : log;
- }
- SecPerClus >>= 1;
- }
- return -1;
- }
- /*
- ========================================================
- = int FS_check_BootSec(UINT8* buf)
- = Check & Get BPB info form the buffer of BPB sectors
- = ARGUMENT(S):
- = UINT8 *buf : BPB sector buffer
- = RETURN VALUE:
- = int : 0 for no error
- = GLOBAL USING:
- = BPB
- = SIDE EFFECT:
- = BPB: selected BPB infos
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- int FS_check_BootSec(UINT8* buf)
- {
- UINT16 temp;
- //check Jump Code + NOP
- if(buf[0] == 0xE9)
- /* OK */;
- else if(buf[0] == 0xEB && buf[2] == 0x90)
- /* OK */;
- else
- {
- printf("Missing JMP/NOPn");
- return 1;
- }
- //check Sectors Per Cluster
- temp = buf[13];
- if(FS_check_SecPerClus(temp) < 0)
- {
- printf("Sectors per cluster (%u) is not a power of 2n",temp);
- return 1;
- }
- //check the Number of FATs
- temp = buf[16];
- if(temp != 1 && temp != 2)
- {
- printf("Invalid number of FATs (%u)n",temp);
- return 1;
- }
- //check Sectors Per Track
- temp = read_le16(buf + 24);
- if(temp == 0 || temp > 63)
- {
- printf("Invalid number of sectors (%u)n",temp);
- return 1;
- }
- //check Number of Heads
- temp = read_le16(buf + 26);
- if(temp == 0 || temp > 255)
- {
- printf("Invalid number of heads (%u)n",temp);
- return 1;
- }
- return FS_Get_BPBPara(buf);
- }
- /*
- ========================================================
- = int FS_Get_BPBPara(UINT8* buf)
- = Get BPB info form the buffer of BPB sectors
- = ARGUMENT(S):
- = UINT8 *buf : BPB sector buffer
- = RETURN VALUE:
- = int : 0 for no error
- = GLOBAL USING:
- = BPB
- = SIDE EFFECT:
- = BPB: selected BPB infos
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- int FS_Get_BPBPara(UINT8* buf)
- {
- BPB.BytesPerSec=(buf[12]<<8)|buf[11];
- switch(BPB.BytesPerSec)
- { case 512: BPB.BytesPerSecPwr=9; break;
- case 2048: BPB.BytesPerSecPwr=11; break;
- case 256: BPB.BytesPerSecPwr=8; break;
- }
- printf("n## BytesPerSec:%d ",BPB.BytesPerSec);
- BPB.SecPerClus=buf[13];
- switch(BPB.SecPerClus)
- { case 128: BPB.SecPerClusPwr=7; break; //64K allocation unit
- case 64: BPB.SecPerClusPwr=6; break; //32K allocation unit
- case 32: BPB.SecPerClusPwr=5; break; //16K allocation unit
- case 16: BPB.SecPerClusPwr=4; break; // 8K allocation unit
- case 8: BPB.SecPerClusPwr=3; break; // 4K allocation unit
- case 4: BPB.SecPerClusPwr=2; break; // 2K allocation unit
- }
- printf("n## SecPerClus:%d ",BPB.SecPerClus);
- BPB.ReserSecCnt=read_le16(buf + 14);
- BPB.NumFATs=buf[16];
- // BPB.MediaDescriptor=buf[21];
- // BPB.SecPerTrack=read_le16(buf + 24);
- // BPB.NumHeads=read_le16(buf + 26);
- // BPB.HiddenSecCnt=read_le32(buf + 28);
- // BPB.TolSec32=read_le32(buf + 32);
- BPB.FATSize32=read_le32(buf + 36);
- printf("n## FATSize32:%d ",BPB.FATSize32);
- BPB.CurrDirClus=BPB.RootClus=read_le32(buf + 44);
- BPB.FAT1_Start=Start_LBA+BPB.ReserSecCnt;
- printf("n## BPB.FAT1_Start:%d ",BPB.FAT1_Start);
- if (BPB.NumFATs==1)
- BPB.FAT2_Start=0;
- if (BPB.NumFATs==2)
- BPB.FAT2_Start=BPB.FAT1_Start+BPB.FATSize32;
- BPB.Data_Area_Start=((BPB.RootClus-2)*BPB.SecPerClus)
- +BPB.FAT1_Start+BPB.FATSize32*BPB.NumFATs;
- printf("n## BPB.Data_Area_Start:%d ",BPB.Data_Area_Start);
- return 0;
- }
- UINT32 Last_FAT_Lba=0x0FFFFFFF; // cyue: Try to make a cache 2002-06-11 11:14PM
- UINT8 *FS_ReadInfoSec(UINT32 lba,int seccnt)
- {
- if(Last_FAT_Lba!=lba)
- { HDD_ReadSec(Info_buff,lba,seccnt);
- }
- return Info_buff;
- }
- /*
- ========================================================
- = UINT32 FS_Get_NextClus(UINT32 currClus)
- = Get Next Cluster of current cluster by FAT
- = ARGUMENT(S):
- = UINT32 *currClus: current cluster
- = RETURN VALUE:
- = UINT32: next cluster number, 0x0FFFFFFF for no cluster
- = GLOBAL USING:
- = UINT32 Last_FAT_Lba: cache mark for last lba // carefully to use it!!
- = SIDE EFFECT:
- = Info_buff: Contains Last FAT Sector that have this file
- = Data_Next_Clus: first clustor of file
- = 2002-06-12 08:38AM Modified by cyue
- = play continue LBAs currently
- ========================================================
- */
- UINT32 FS_Get_NextClus(UINT32 currClus)
- {
- UINT32 tmpLba;
- UINT32 tmpOffset;
- tmpOffset=currClus&0x7F;//%128;
- tmpLba=(currClus>>7)+BPB.FAT1_Start;
- FS_ReadInfoSec(tmpLba,1);
- Data_Next_Clus=read_le32(Info_buff+(tmpOffset<<2))&0x0fffffff;
- //printf("Data_Next_Clus=%lxn",Data_Next_Clus);
- return Data_Next_Clus;
- }
- #endif //USE_HDD