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

📄 mini.c

📁 内含纯DOS,WIN32控制台和WIN32 GUI三个版本.它们生成的备份文件可以在任何版本中使用. WIN32 GUI版本暂使用备份功能(其他模块也都已全部写好,全是完整的,现在仅开启备份功能),如
💻 C
📖 第 1 页 / 共 5 页
字号:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <direct.h>
#include <io.h>
/*#define getch();*/

extern char nextDOSletter;
extern short nextLetterNumber;
extern char miniTOtag[];

int readWriteDrive(unsigned int cmd, 
				   char *devicePar, 
				   unsigned long startSectorPar, 
				   DWORD sectorToDo,
				   char *buf)
{
	DWORD deviceGeneric,open;
	
	__int64 startSector=0;
	__int64 startbPos;
	DWORD sectorHigh;
	
	HANDLE hDev=0;
	
	
	DWORD bDone;
	/*CString device;*/
	char device[32];
	char startSectorPar2[64];
	
	/*device.Format("%d",devicePar);*/
	memset(device,0,32);
	memset(startSectorPar2,0,64);
	
	sprintf(startSectorPar2,"%lu",startSectorPar);
	
	startSector=_atoi64(startSectorPar2);
	startbPos=startSector*512;
	sectorHigh=startbPos>>32;
	/*
	if(device.IsEmpty()==TRUE)
	{printf("请输入设备名。"); return 0;} 
	if(sectorToDo==0)
	{printf("读取的扇区数不能为0。"); return 0;}
	*/
	
	/*device=CString("\\\\.\\PHYSICALDRIVE")+CString(device);*/
	strcpy(device,"\\\\.\\PHYSICALDRIVE\0");
	strcpy(device+strlen(device),devicePar);
	
	/*printf("%s\n",device);*/
	
	if(cmd==0x42)
	{
		deviceGeneric=GENERIC_READ;
		
		
		
	}
	
	else if(cmd==0x43)
	{
		deviceGeneric=GENERIC_WRITE;
		
		
		
	}
	
	
	
	hDev=CreateFile(device,deviceGeneric,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
	if(hDev==INVALID_HANDLE_VALUE)
	{/*printf("打开硬盘 %s 失败。",device);*/ if(hDev!=INVALID_HANDLE_VALUE) CloseHandle(hDev);
	return -1;}
	//	SetFilePointer(readHandle,startbPos,(LONG *)&sectorHigh,FILE_BEGIN);
	
	
	
	
	SetFilePointer(hDev,startbPos,(LONG *)&sectorHigh,FILE_BEGIN);
	
	if(cmd==0x42) 
	{
		if(!ReadFile(hDev,buf,sectorToDo*512,&bDone,0))
		{
			printf("Failed to read %s\n",device);
			return -1;
		}
		
	}
	
	else if(cmd==0x43) 
	{
		if(!WriteFile(hDev,buf,sectorToDo*512,&bDone,0))
		{
			printf("Failed to write %s\n",device);
			return -1;
			
		}
	}
	if(hDev!=INVALID_HANDLE_VALUE)	CloseHandle(hDev);
	return 0;
	
}



short absread(short drive, unsigned long sectorToDo, unsigned long startSector, char *buf)
{
	
	
	short cmd=0x42;
	char deviceStr[32];
	memset(deviceStr,0,32);
	sprintf(deviceStr,"%d",drive);
	return readWriteDrive(cmd, 
		deviceStr, 
		startSector, 
		sectorToDo,
		buf);
	
}

short abswrite(short drive, unsigned long sectorToDo, unsigned long startSector, char *buf)
{
	
	
	short cmd=0x43;
	char deviceStr[32];
	memset(deviceStr,0,32);
	sprintf(deviceStr,"%d",drive);
	
	return readWriteDrive(cmd, 
		deviceStr, 
		startSector, 
		sectorToDo,
		buf);
	
}

short extInt13(unsigned short cmd,
			   short drive,
			   unsigned long startSector,
			   unsigned long sectorToDo,
			   char *buf)
{
	char deviceStr[32];
	memset(deviceStr,0,32);
	sprintf(deviceStr,"%d",drive-128);
	return readWriteDrive(cmd, 
		deviceStr, 
		startSector, 
		sectorToDo,
		buf);
	
}



int compareByte(char *buf1, char *buf2, unsigned long byte, char pause, unsigned long *diff, unsigned long *offsetAllRecord)
{
	unsigned long i,j;
	
	for(i=0,j=0;i<byte;i++)
	{
		
		if(buf1[i]==buf2[i]) continue;
		else
		{
			printf("%09lX  %02X %02X\n",i+(*offsetAllRecord),buf1[i],buf2[i]);
			j++;
			printf("Press other key to continue...");
			getch();
			printf("\n");
		}
		if(pause)
		{
			if(j%24==0)
			{
				printf("Press other key to continue...");
				getch();
				
				printf("\n");
				
			}
		}
	}
	if(j) *diff+=j;
	*offsetAllRecord+=i;
	
	return 0;
}




void help()
{
	printf(" miniTOw MBR/BOOT/FAT Tool 1.0 Copyright(c) GOTOmsdos tdaim@sina.com\n");
	printf(" miniTOw [/?|/H]\n");
	printf("         [/D[.]] [/P[...]] [/V] [Common]\n");
	printf("         /S|/R|/E|/C|/L [n|n:n|?:] [/MBR|/BOOT/FAT] [/A] [/Fxx] [/Y] [Common]\n");
	printf("         /T n:n|?: [Common]\n");
	printf(" Common:\n"); 
	printf(" /FLOPPY  : Support Floppy\n");
	printf(" /NoHP    : Do not get HDD Parameter\n");
	printf(" /NTFSPRO : Support NTFSPRO DOS letter\n");
	printf(" /IFS     : ...     IFS     ...\n");
	printf(" /DOS     : Take ?: as DOS letter\n");
	printf("\n");
	printf(" n        : No.drive\n");
	printf(" n:n      : drive:partition, e.g. 1:2\n");
	printf(" ?:       : WIN letter, e.g. D:\n");
	printf("\n");
	printf(" /?|/H    : Print help\n");
	printf(" /D[.]    : Print Drive info. n=drive; A=Floppy\n");
	printf(" /P[...]  : Print Partition info. A=Floppy; :=DOS letters; F=FAT N=NTFS\n"); 
	printf("            FN=FAT&NTFS; L=Linux; NoH=No Hidden\n");
	printf(" /V       : WIN Vol prior to DOS label\n");   
	printf(" /S       : Save\n");
	printf(" /R       : Restore\n");
	printf(" /E       : rEplace BOOT1/FAT1 with BOOT2/FAT2, FAT32 only\n");
	printf(" /C       : Compare with file (/MBR|/BOOT/FAT UNneeded)\n");
	printf(" /L       : Look info in drive/file\n");
	printf(" /MBR     : Master Boot Record\n");
	printf(" /BOOT    : DOS Boot Record. i.e. DBR\n");
	printf(" /FAT     : File Allocation Table\n");
	printf(" /A       : All partitions/drives, allowed with n\n");
	printf(" /Fxx     : File, xx = name\n");
	printf(" /Y       : Yes to write drive\n");
	printf(" /T       : Turn to print ?:|n:n from n:n|?:\n");
}


void getVolumeSerialNumber(Pinfo **part)
{
	char buf[512];
	unsigned short fat1216=0;
	unsigned long fat32=0;
	char fatTRUE=0;
	char fat32TRUE=0;
	short i,j;
	char fat[]={0x04,0x06,0x0B,0x0C,0x01,0x0E,
		0x14,0x16,0x1B,0x1C,0x11,0x1E,0x00};
	for(i=0;part[i];i++)
	{
		fatTRUE=0;
		
		
		
		if(part[i]->hardDrive<0)
		{
			if (absread(part[i]->hardDrive+127, 1, 0, buf) != 0)
			{
				/* printf("Failed to read %c:\n",part[i]->letter);  */
				continue;
			}
		}
		else
		{
			if(extInt13(0x42,part[i]->hardDrive+127,part[i]->start,1,buf)!=0) continue;
		}
		
		if(part[i]->ID==0x07 || part[i]->ID==0x17)
			part[i]->volumeSerialNumber=*((unsigned long *)(buf+0x48));
		
		
		for(j=0;fat[j];j++)
		{
			if(part[i]->ID==fat[j])
			{
				fatTRUE=1;
				break;
			}
			
		}
		
		if(!fatTRUE) continue;
		
		fat1216=*(unsigned short *)(buf+0x16);
		fat32=*(unsigned long *)(buf+0x24);
		
		if((strnicmp(buf+0x36,"FAT16",5)==0 || strnicmp(buf+0x36,"FAT12",5)==0) && fat1216) fat32TRUE=0;
		else if(strnicmp(buf+0x52,"FAT32",5)==0 && fat32) fat32TRUE=1;
		else  /*return -1;*/continue;
		
		part[i]->volumeSerialNumber=*(unsigned long *)(buf+(fat32TRUE ? 0x43 : 0x27));
		
	}	
}
short putInWINletterVol(Pinfo *part[])
{
	char WINvol[12];
	unsigned long volSNviaWIN=0;
	int i,j;
	char letter='C';
	
	
	getVolumeSerialNumber(part);
	
	for(;letter<='Z';letter++)
	{
		char str[4];
		memset(WINvol,0,12);
		
		str[0]=letter;
		strcpy(str+1,":\\\0");
		
		
		volSNviaWIN=0;
		
		
		if(GetVolumeInformation(str,WINvol,12,&volSNviaWIN,NULL,NULL,NULL,12))
		/*printf("error\n");
		printf("%X\n",volSNviaWIN);*/
		{
			
			
			
			for(j=0;part[j];j++)
			{
				
				
				
				if(part[j]->volumeSerialNumber==volSNviaWIN) 
				{
					/*	printf("index=%d %c: %c:\n",j,part[j]->letter,letter);*/
					part[j]->WINletter=letter;
					memset(part[j]->label,0,12);
					strcpy(part[j]->label,WINvol);
					break;
				}
			}
			
			
		}
		
		
		
	}
	
	
	return 0;
}

void WINputInDiskFreeSpace(Pinfo *part[])
{
	/*bytePerSect=*(unsigned short *)(buf+0x0B); */
	
	unsigned long sectPerCluster=0;
	
	unsigned long freeCluster=0;
	int i;
	char WINletterStr[4];
	for(i=0;part[i];i++)
	{
		
		strcpy(WINletterStr,&part[i]->WINletter);
		strcpy(WINletterStr+1,":\\\0");
		/*printf("%s\n",WINletterStr);*/
		if(GetDiskFreeSpace(WINletterStr,&sectPerCluster,NULL,&freeCluster,NULL))
			
			part[i]->freeSpaceKB=sectPerCluster*freeCluster/2UL;
		
		
	}
	
	
}

void takeFS(unsigned char ID, char *FS, char *hidden)
{
	memset(FS,0,16);
	switch(ID)
	{
	case 0x0C : strcpy(FS,"FAT32(LBA)"); break;
	case 0x1C : strcpy(FS,"FAT32(LBA)"); *hidden=1; break;
		
	case 0x0B : strcpy(FS,"FAT32"); break;
	case 0x1B : strcpy(FS,"FAT32"); *hidden=1; break;
		
	case 0x07 : strcpy(FS,"NTFS/HPFS"); break;
	case 0x17 : strcpy(FS,"NTFS/HPFS"); *hidden=1; break;
		
	case 0x06 : strcpy(FS,"FAT16(32M+)"); break;
	case 0x16 : strcpy(FS,"FAT16(32M+)"); *hidden=1; break;
		
	case 0x04 : strcpy(FS,"FAT16(32M-)"); break;
	case 0x14 : strcpy(FS,"FAT16(32M-)"); *hidden=1; break;
		
	case 0x01 : strcpy(FS,"FAT12"); break;
	case 0x11 : strcpy(FS,"FAT12"); *hidden=1; break;
		
	case 0x0E : strcpy(FS,"FATs(LBA)"); break;
	case 0x1E : strcpy(FS,"FATs(LBA)"); *hidden=1; break;
		
	case 0x83 : strcpy(FS,"Linux Native"); break;
		
	case 0x82 : strcpy(FS,"Linux Swap"); break;
		
	case 0x26 : *hidden=1; break;
		
	default : strcpy(FS,"");
	}
}


short getDrive(short drive,
			   char *supportExtInt13,
			   unsigned long *maxCylinder,
			   unsigned long *maxHead,
			   unsigned long *maxSectorPerTrack,
			   unsigned long *totalSector,
			   char ignoreHDDparameter)
{
	short result;
	char buffer[512];
	char device[32];
	memset(device,0,32);
	strcpy(device,"\\\\.\\PHISICALDRIVE\0");
	sprintf(device+strlen(device),"%d",0);
    *supportExtInt13=/*checkExtInt13(drive+127);*/1;
	/*  printf("passed\n"); */
	if(*supportExtInt13)
	{
		/*if(ignoreHDDparameter) return 0;*/
		return extInt13(0x42,
			drive+127,
			0,
			1,
			buffer);
			/*
			if(CreateFile(device,0,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0)!=INVALID_HANDLE_VALUE)
			
		return 0;*/
		
		
	}
	
	else return -1;
}

void getDriveTable(Dinfo *driveTable[], char useFloppy, char ignoreHDDparameter)
{
	short i,j,k;
	short floppy;
	short hard;
	
	for(i=0,j=0,k=1,floppy=-127,hard=0;j<MAX_DRIVE_NUM-2;j++,floppy++)
	{
		
		driveTable[i]=(Dinfo *)malloc(sizeof(Dinfo));
		memset(driveTable[i],0,sizeof(Dinfo));
		
		if(useFloppy || floppy>-126) floppy=0;
		if(getDrive((floppy ? floppy : ++hard),
			&(driveTable[i]->supportExtInt13),
			&(driveTable[i]->maxCylinder),
			&(driveTable[i]->maxHead),
			&(driveTable[i]->maxSectorPerTrack),
			&(driveTable[i]->totalSector),
			ignoreHDDparameter)==-1)
		{
			if(driveTable[i]) free(driveTable[i]);
			driveTable[i]=NULL;
			
			if(!floppy) break;
			else continue;
		}
		
		else
		{
			if(floppy)
			{
				driveTable[i]->floppy=1;
				driveTable[i]->number=floppy;
			}
			else driveTable[i]->number=/*k++*/hard;
			i++;
		}
		/*if(!floppy) hard++;*/
		
	}
	
}

short putInDrivePartNum(Dinfo *driveTable[], Pinfo *partitionTable[])
{
	int i,j;
	for(i=0,j=0;driveTable[i];i++)
		if(driveTable[i]->number>0)
		{
			
			
			for(;partitionTable[j] && driveTable[i]->number==partitionTable[j]->hardDrive;j++)
				
				driveTable[i]->part++;
				/*if(driveTable[i]->number==partitionTable[j]->hardDrive)
				{
				part++;
				if(partitionTable[j+1] && driveTable[i]->number!=partitionTable[j+1]->hardDrive)
				{
				driveTable[i]->part=part;
				j++;
				break;
				}
		}*/
			
			
			
			
		}	
		return 0;
		
}

short getDriveItem(char drive,
				   Dinfo *driveTable[],
				   short *driveIndex)
{
	short i=0;
	*driveIndex=INVALID_INDEX;
	
	
	if(driveTable[0]==NULL)
	{
		printf("No drive\n");
		return -1;
	}
	
	
	while(1)
	{
		if(driveTable[i]==NULL)
		{
			printf("No drive %d\n",drive);
			return -1;
		}
		if(driveTable[i]->number==drive) break;
		i++;
	}
			 *driveIndex=i;
			 
			 return 0;
}

short printDrive(Dinfo *driveTable[], /*Pinfo *partitionTable[], */short index)
{
	short i=0,j,k;
	if(index==INVALID_INDEX) return -1;
	else if(index==PRINT_DRIVE_ALL || index==PRINT_DRIVE_FLOPPY) i=0;
	else /*if(index!=PRINT_DRIVE_FLOPPY) */i=index;

	
	CComboBox *pm_print=(CComboBox *)AfxGetApp()->m_pMainWnd->GetDlgItem(IDC_COMBO4);
	
	if(driveTable[0]==NULL)
	{
		printf("No drive\n");
		return -1;
	}
	
	if(driveTable[0] && driveTable[i]==NULL)
	{
		printf("No drive %d\n",driveTable[i]->number);
		return -1;
	}
/*
	for(j=0,k=printD->GetCount();k && j<k; j++)
	printD->DeleteItem(j);*/

	

	k=0;
	pm_print->InsertString(k++," DRIVE  PARTITIONS CYLINDER   HEAD     SECTOR   ALL SECTOR CAPACITY");
	for(;driveTable[i]/*!=NULL*/;i++)
	{
		unsigned long GB=0;
		unsigned long MB=0;
		/*short part=0;*/
		char print[256];
		memset(print,0,256);
		
		if(index==PRINT_DRIVE_FLOPPY)
		{
			if(driveTable[i]->number!=-127 && driveTable[i]->number!=-126)
				break;
		}
		

⌨️ 快捷键说明

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