?? drive.cpp
字號:
/*
Command Line Tools for DOS(V32) Prgram v0.1.0 write by 孫寶建
6-20-2004
(暫停)
*/
/********************************************************************
created: 2004/09/20
created: 20:9:2004 19:06
filename: d:\sun\src3\tools\drive\drive.cpp
file path: d:\sun\src3\tools\drive
file base: drive
file ext: cpp
modify: 孫寶建(sunbaojian)
version: 1.0.1
purpose:
*********************************************************************/
#include "stdafx.h"
#ifndef __commcpp_h
#include "commcpp.hpp"
#endif
#ifndef _INCLUDE_SLIB_FUNC_HPP_INCLUDE
#include "slib_func.hpp"
#endif
#ifndef _INCLUDE_SLIB_ERROR_HPP_INCLUDE
#include "slib_error.hpp"
#endif
#ifndef _INT13_H
#include "int13.hpp"
#endif
#ifndef _DRIVE_H
#include "drive.hpp"
#endif
#ifdef __DJGPP__
#ifndef __dj_include_sys_movedata_h_
#include <sys/movedata.h>
#endif
#ifndef __dj_include_go32_h_
#include <go32.h>
#endif
#endif
using namespace SLib;
SLIB_DEFINE_FILE_NAME_VARIABLE
CLogicalDisk32::CLogicalDisk32(CInt13 *pInt13,CSError *pError):m_pError(pError)
{
SVERIFY_ARGUMENTS(pInt13!=NULL);
SVERIFY_ARGUMENTS(pError!=NULL);
m_dqPartitionPos=0; //邏輯盤分區表所在的扇區,它指向m_dqStartSector
m_dqBootPosition=0;
m_dqBeginFatSector=0; //相對于本邏輯盤的起始扇區的偏移量
m_dqBeginFdtSector=0; //相對于本邏輯盤的起始扇區的偏移量
m_dqBeginDataSector=0; //相對于本邏輯盤的起始扇區的偏移量
memset(&m_Partition,0,sizeof(m_Partition));
memset(&m_Mbr,0,sizeof(m_Mbr));
memset(&m_Boot32,0,sizeof(m_Boot32));
memset(&m_Fdt,0,sizeof(m_Fdt));
m_pInt13=pInt13;
m_pFat32=new CFat32(m_pError);
/*
m_pPartition=NULL;
m_pMbr=NULL;
m_Boot32=NULL;
m_Fdt=NULL;
m_bData=FALSE;
m_bFat=FALSE;
m_bDir=FALSE;
m_bBpb=FALSE;
m_bPartition=FALSE;
m_bMbr=FALSE;
m_dqLogicalSector=0;
m_dqBeginFatSector=0;
m_dqBeginFdtSector=0;
m_dqBeginFdtSector=0;
m_dqBootPosition=0;
m_dqPartitionPos=0;
m_ulParentPartition=0;
*/
}
CLogicalDisk32::~CLogicalDisk32()
{
delete m_pFat32;
}
BOOL CLogicalDisk32::LoadPartition(DWORD64 sectorPos)
{
m_dqPartitionPos=sectorPos;
return m_pInt13->ReadSector(sectorPos,sizeof(m_Boot32)/c_uBYTES_PER_SECTOR,(BYTE*)&m_Partition) ;
}
BOOL CLogicalDisk32::LoadMbr(DWORD64 sectorPos,int MbrNumber)
{
if(LoadPartition(sectorPos))
{
memcpy(&m_Mbr,&m_Partition.m_Mbr[MbrNumber],sizeof(m_Mbr) );
return TRUE;
}
return FALSE;
}
BOOL CLogicalDisk32::LoadBoot(DWORD64 sectorPos)
{
m_dqBootPosition=sectorPos;
return m_pInt13->ReadSector(sectorPos,sizeof(m_Boot32)/c_uBYTES_PER_SECTOR,(BYTE*)&m_Boot32 ) ;
}
BOOL CLogicalDisk32::LoadFat32(DWORD64 sectorPos,unsigned int ulFat32Size)
{
m_dqBeginFatSector=sectorPos;
return m_pFat32->LoadFat32(m_pInt13,sectorPos,ulFat32Size);
}
void CLogicalDisk32::LoadFdt(BYTE *pfdtBuf)
{
memcpy(&m_Fdt,pfdtBuf,sizeof(m_Fdt));
}
string CLogicalDisk32::PartitionToString(void)
{
ostringstream ostr;
ostr.fill('0');
ostr.setf(ios_base::right,ios_base::adjustfield);
ostr.setf(ios_base::uppercase| ios_base::showbase);
ostr.setf(ios_base::hex,ios_base::basefield);
return ostr.str();
}
string CLogicalDisk32::MbrToString(void)
{
ostringstream ostr;
ostr.fill('0');
ostr.setf(ios_base::right,ios_base::adjustfield);
ostr.setf(ios_base::uppercase| ios_base::showbase);
ostr.setf(ios_base::hex,ios_base::basefield);
ostr<< "ByBootFlag: " << setw(4) << (unsigned short ) m_Mbr.ByBootFlag
<< " ByBeginHead: " << setw(4) << (unsigned short ) m_Mbr.ByBeginHead
<< "ByBeginSector: " << setw(4) << (unsigned short ) m_Mbr.ByBeginSector
<< " ByBeginCyl: " << setw(4) << (unsigned short ) m_Mbr.ByBeginCyl
<<endl
<< "ByPartitionType: " << setw(4) << (unsigned short ) m_Mbr.ByPartitionType
<< " ByEndHead: " << setw(4) << (unsigned short ) m_Mbr.ByEndHead
<< "ByEndSector: " << setw(4) << (unsigned short ) m_Mbr.ByEndSector
<< " ByEndCyl: " << setw(4) << (unsigned short ) m_Mbr.ByEndCyl
<<endl
<< "dwPreservedSector: "<< setw(10) << m_Mbr.dwPreservedSector
<< " SumSector: " <<setw(10) << m_Mbr.dwSumSector
<< endl
<< "m_dqPartitionPos: " << setw(18) << m_dqPartitionPos
<< endl;
return ostr.str();
}
string CLogicalDisk32::Boot32ToString(void)
{
CaculateData();
ostringstream ostr;
ostr.fill('0');
ostr.setf(ios_base::right,ios_base::adjustfield);
ostr.setf(ios_base::uppercase| ios_base::showbase);
ostr.setf(ios_base::hex,ios_base::basefield);
ostr<< "cOEM[8]: ";
for (int i = 0; i< 8 && (char )m_Boot32.cOEM[i] ; i++)
ostr<< (char )m_Boot32.cOEM[i];
ostr<< " \twBytePerSector: " << m_Boot32.wBytePerSector <<endl
<< "BySectorPerCluster: " << setw(4) << (unsigned short ) m_Boot32.BySectorPerCluster << " \twPreserveSector: " << m_Boot32.wPreserveSector <<endl
<< "NumberofFATs: " << setw(4) << (unsigned short )m_Boot32.NumberofFATs << " \t RootEntries: " << m_Boot32.RootEntries << endl
<< "SmallSectors: " << m_Boot32.SmallSectors << " \t MediaDescriptor: " << setw(4) << (unsigned short ) m_Boot32.MediaDescriptor <<endl
<< "wSectorPerFat: " << m_Boot32.wSectorPerFat << " \twSectorPerTrack: " << m_Boot32.wSectorPerTrack <<endl
<< "wSumHead: " << m_Boot32.wSumHead <<" \tdwHideSector: " << m_Boot32.dwHideSector <<endl
<< "LargeSectors: " << m_Boot32.LargeSectors << " \tdwSectorPerFat: " << m_Boot32.dwSectorPerFat <<endl
<< "ExtendedFlags: " << m_Boot32.ExtendedFlags << " \twFileSystemVersion: " << m_Boot32.wFileSystemVersion <<endl
<< "RootClusterNumber " << m_Boot32.RootClusterNumber << " \t FileSystemInformationSectorNumber: " << m_Boot32.FileSystemInformationSectorNumber <<endl
<< "BackupBootSector: " << m_Boot32.BackupBootSector << " \t PhysicalDriveNumber: " <<(unsigned short )m_Boot32.PhysicalDriveNumber << endl
<< "ExtendedBootSignature: " << setw(4) << (unsigned short )m_Boot32.ExtendedBootSignature << " \t VolumeSerialNumber: " << m_Boot32.VolumeSerialNumber << endl
<< "VolumeLabel[11]: ";
for (int i = 0; i< 11 && m_Boot32.VolumeLabel[i]; i++)
ostr<< (char )m_Boot32.VolumeLabel[i];
ostr<< " \t SystemID[8]: ";
for (int i = 0; i< 8 && m_Boot32.SystemID[i]; i++)
ostr<< (char)m_Boot32.SystemID[i];
ostr<< "wBootFlag: " << m_Boot32. wBootFlag << endl
// << "ByPreserve[12]: " << "ByPreserve2: "
// << "ByBootCode[420]: "
// << "cJmpCode[3]: "
<< "BeginFatSector: "<<setw(16) <<m_dqBeginFatSector <<endl
<< "BeginFdtSector: "<< setw(16) <<m_dqBeginFdtSector << endl
<< "BeginDataSector: " << setw(16) <<m_dqBeginDataSector <<endl;
return ostr.str();
}
string CLogicalDisk32::Fat32ToString(void)
{
ostringstream ostr;
ostr.fill('0');
ostr.setf(ios_base::right,ios_base::adjustfield);
ostr.setf(ios_base::uppercase| ios_base::showbase);
ostr.setf(ios_base::hex,ios_base::basefield);
return ostr.str();
}
string CLogicalDisk32::FdtToString(void)
{
ostringstream ostr;
ostr.fill('0');
ostr.setf(ios_base::right,ios_base::adjustfield);
ostr.setf(ios_base::uppercase| ios_base::showbase);
ostr.setf(ios_base::hex,ios_base::basefield);
return ostr.str();
}
void CLogicalDisk32::CaculateData(void)
{
/*m_dqBeginFatSector=m_dqBootPosition+wPreserveSector;
m_dqBeginFdtSector=m_dqBeginFatSector+dwSectorPerFat*BySumFat;
m_dqBeginFdtSector=m_dqBeginFdtSector+dwClusterPerFdt*BySectorPerCluster;
*/
// m_dqBeginFatSector=m_dqBootPosition+m_Boot32.dwHideSector+m_Boot32.wPreserveSector;//m_pMbr->dwPreservedSector;//+m_Boot32.wPreserveSector+m_dqPartitionPos;
m_dqBeginFatSector=m_dqBootPosition+m_Boot32.wPreserveSector;
m_dqBeginDataSector=m_dqBeginFatSector+m_Boot32.dwSectorPerFat*m_Boot32.NumberofFATs;
m_dqBeginFdtSector=CtoS(m_Boot32.RootClusterNumber);
}
DWORD64 CLogicalDisk32::CtoS(DWORD Clustor)
{
m_dqLogicalSector=m_dqBeginDataSector+(Clustor-2)*m_Boot32.BySectorPerCluster;//m_dqBeginFdtSector
return m_dqLogicalSector ;
}
BOOL CLogicalDisk32::GetCluster(IN DWORD Clustor,OUT DWORD &newCluster)
{
return m_pFat32->GetCluster(Clustor,newCluster);
}
BOOL CFat32::GetCluster(IN DWORD Clustor,OUT DWORD &nextCluster)
{
assert(m_pInt13!=NULL);
unsigned int ulClusterSumOff=Clustor*4;
unsigned int ulSecOff=ulClusterSumOff/c_uBYTES_PER_SECTOR;
if(ulSecOff>m_ulFat32Size)
{
m_pError->AddError(ERR_DRV_NO_FOUND_CLUSTER);
return FALSE;
}
//unsigned int ulBufOff=ulClusterSumOff%c_uBYTES_PER_SECTOR;
if( (ulSecOff >= m_ulCurSectors)
&& (ulSecOff <= m_ulCurSectors+c_uMAX_FAT_CACHE_BUF_SIZE/c_uBYTES_PER_SECTOR )
)
{
// memmove(&ultmp,&m_pbyFat32[(dqClusterSects-m_ulCurSectors)*c_uBYTES_PER_SECTOR+ulClusterSectsOff],
// sizeof(unsigned int));
// return m_pbyFat32[(dqClusterSects-m_ulCurSectors)*c_uBYTES_PER_SECTOR/4+ulClusterSectsOff/4];
}
else
{
m_ulCurSectors=ulSecOff;
if( !m_pInt13->ReadSector(m_ulCurSectors+m_dqFat32StartSector,c_uMAX_FAT_CACHE_BUF_SIZE/c_uBYTES_PER_SECTOR,m_pbyFat32))
{
m_pError->AddError(ERR_DRV_NO_FOUND_CLUSTER);
return FALSE;
}
}
nextCluster= ( (DWORD *)m_pbyFat32) [ (ulClusterSumOff-m_ulCurSectors*c_uBYTES_PER_SECTOR)/4];
return TRUE;
}
CFat32::CFat32(CSError *pError):m_pError(pError)
{
SVERIFY_ARGUMENTS(pError!=NULL);
m_pInt13 = NULL;
m_dqFat32StartSector = 0;
m_ulCurSectors = 0; //相對于fat表的偏移數,以扇區數計算
m_ulFat32Size = 0; //nsectors 以扇區數計算
m_ulBufSize = 0; //bytes計算
m_pbyFat32 = NULL;
m_pbyFat32=new BYTE [c_uMAX_FAT_CACHE_BUF_SIZE];
}
CFat32::~CFat32()
{
delete []m_pbyFat32;
}
BOOL CFat32::LoadFat32(CInt13 *pInt13,DWORD64 ulFat32StartSector,unsigned int ulFat32Size)
{
assert(pInt13!=NULL);
m_dqFat32StartSector=ulFat32StartSector;
m_ulCurSectors=0;
m_ulFat32Size=ulFat32Size;//nsectors
m_ulBufSize=c_uMAX_FAT_CACHE_BUF_SIZE;
m_pInt13=pInt13;
if(m_pInt13->ReadSector(m_ulCurSectors+m_dqFat32StartSector,c_uMAX_FAT_CACHE_BUF_SIZE/c_uBYTES_PER_SECTOR,m_pbyFat32))
{
return TRUE;
}
else
{
m_pError->AddError(ERR_FAT32_LOAD_FAT32);
}
return FALSE;
}
string CLogicalDisk32::DirToString(BYTE *pfdtBuf,unsigned int len,unsigned int ulPos)
{
unsigned int i=0;
unsigned int k=0;
UCHAR c;
unsigned int linecount=(len-sizeof(FDT)*ulPos)/sizeof(FDT);
PFDT pfdt=PFDT(pfdtBuf+ulPos);
ostringstream ostr;
ostr.fill(' ');
ostr.setf(ios_base::right,ios_base::adjustfield);
ostr.setf(ios_base::uppercase| ios_base::showbase);
ostr.setf(ios_base::hex,ios_base::basefield);
ostr<< "Name Time Date Clustor Length" << endl;
for(k=0;k<c_DRV_MAX_FILE_SUM&&k<linecount;k++)
{
for( i=0;i<8;i++)
{
c=pfdt->cFileName[i];
if(!iscntrl(c))
{
ostr<< c;
}
else
ostr<<'.';
}
ostr<< '.';
for(i=0;i<3;i++)
{
c=pfdt->cFileNameExt[i];
if(iscntrl(c)||c==0xff)
{
ostr<< '.';
}
else
ostr<<(unsigned char) c;
}
ostr<<" ";
ostr.setf(ios_base::dec,ios_base::basefield);
ostr << setw(5) << pfdt->wTime << " " <<setw(5)<< pfdt->wDate << " ";
DWORD dwCluster=pfdt->wHightCustorByte;
dwCluster=dwCluster<<16;
dwCluster=dwCluster+pfdt->wLowCustorByte;
ostr.setf(ios_base::hex,ios_base::basefield);
ostr<< setfill('0') << setw(10) << dwCluster << " ";
ostr.setf(ios_base::dec,ios_base::basefield);
ostr<< setfill(' ') << setw(10) << pfdt->dwFileLength <<endl;
pfdt++;
}
return ostr.str();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -