当前位置: 技术问答>linux和unix
用程序实现格式化磁盘怎么实现?
来源: 互联网 发布时间:2014-11-24
本文导语: 如何编程实现对软盘的格式化?听说在用中断13H时,需要向磁头写一个数据表才能实现格式化。如果有人知道,请把详细方法告诉我,谢谢! | /*---------------------+ | FAT 12 or 16 | +--...
如何编程实现对软盘的格式化?听说在用中断13H时,需要向磁头写一个数据表才能实现格式化。如果有人知道,请把详细方法告诉我,谢谢!
|
/*---------------------+
| FAT 12 or 16 |
+---------------------*/
else
{
bpb->Comm.nBootSect = 1; /** FAT 玡合跋计 **/
bpb->FAT1x.nFdbRoot = 512; /** ヘ魁兜计 **/
bpb->FAT1x.firstHD = 0x80; /** 材场祑盒腹 **/
bpb->FAT1x.signature = 0x29; /** 疭紉絏 **/
bpb->FAT1x.serNoLow = random(0xffff) + 1;
bpb->FAT1x.serNoHigh = random(0xffff) + 1;
cpymem(bpb->FAT1x.label, "NO NAME ", 11); /** Label **/
cpymem(bpb->FAT1x.fsType, "FAT16 ", 8); /** 郎╰参 **/
if ( ptr->old_id == 1 )
{
bpb->FAT1x.fsType[4] = '2';
bpb->Comm.sectPerClust = 8; /** FAT12 **/
}
else
{
/*----------------------------------+
| – Cluster 合跋计 |
+----------------------------------*/
bpb->Comm.sectPerClust = ( Size FAT1x.bootStrap; /** 币笆祘Α絏熬簿竚 **/
} /* end if */
codePTR = (byte FAR *)BOOT_HEAD;
while ( codePTR != (byte FAR *)BOOT_END ) /** 恶币笆祘Α絏 **/
*bootStrap++ = *codePTR++;
/*--------------------------------------+
| Modify SI context at BootSect.asm |
| and fill JUMP instruction to buffer |
+--------------------------------------*/
bpb->Comm.NOP = 0x90; /** NOP **/
bpb->Comm.jumpCode = 0xeb;
if ( ptr->old_id == 0xb || ptr->old_id == 0xc )
{ /** JMP 58h 单 BPB **/
bpb->Comm.jumpOffset = 512 - sizeof(bpb->FAT32.bootStrap) - 4;
bpb->FAT32.bootStrap[ (word)BOOT_DATA - (word)BOOT_HEAD + 1 ] += 0x5a;
}
else
{ /** JMP 3Ch 单 BPB **/
bpb->Comm.jumpOffset = 512 - sizeof(bpb->FAT1x.bootStrap) - 4;
bpb->FAT1x.bootStrap[ (word)BOOT_DATA - (word)BOOT_HEAD + 1 ] += 0x3e;
}
} /* end CreateBPB */
/*============================= 秈 ︽ Α て ==============================*/
sword Format (PARTN *ptr, void *bootSect)
{
dword Sys_Sector, NowSect, Start, Total;
byte Temp[512];
FS_SECT *fsSect;
byte isFAT32;
DOSBPB *bpb;
sword ratio, i;
bpb = (DOSBPB *)bootSect;
isFAT32 = ( (ptr->old_id == 0xb) || (ptr->old_id == 0xc) );
NowSect = GetStart(ptr); /** 眔币﹍合跋 **/
Total = get_nsector(ptr); /** だ澄合跋羆计 **/
if ( isFAT32 ) /** 璸衡郎╰参ノぶ合跋 **/
Sys_Sector = bpb->FAT32.sectPerFAT * bpb->Comm.nFAT +
bpb->Comm.nBootSect + bpb->Comm.sectPerClust;
else
Sys_Sector = bpb->FAT1x.sectPerFAT * bpb->Comm.nFAT +
bpb->Comm.nBootSect + (bpb->FAT1x.nFdbRoot >> 4);
#if ( DISPLAY == CHINESE )
show_msg("タ 秈 ︽ Α て Ч Θ ");
#else
show_msg("It's formatting, completed ");
#endif
/*-----------------------------*/
/* ╰ 参 合 跋 恶 */
/*-----------------------------*/
for ( Start = 0 ; Start title = 0x41615252L;
fsSect->signature = 0x61417272L;
fsSect->next_free_clus = 0x00000002L;
fsSect->magicNum = 0xaa55;
/*----------------------------------------------------+
| FAT32 郎 ╰ 参 合 跋 戈 癟 |
+----------------------------------------------------*/
fsSect->free_clust_cnt = (Total - Sys_Sector) /
(dword)bpb->Comm.sectPerClust;
if ( lba_WriteWDK(NowSect + 1, 1, fsSect) ||
lba_WriteWDK(NowSect + bpb->FAT32.bakBootSect + 1, 1, fsSect) )
{
return( -1 );
}
memset(Temp, 0, 512); /** ミ FAT ㄏノぇ既丁 **/
*(dword *)&Temp[4] = 0x0fffffffL;
*(dword *)&Temp[8] = 0x0fffffffL;
Temp[3] = 0x0f; /** 砞﹚ FAT 繷 **/
}
else
{
if ( ptr->old_id != 1 ) /** 砞﹚ FAT 繷 **/
Temp[3] = 0xff;
}
*(word *)&Temp[1] = 0xffff; /** 砞﹚ FAT 繷 **/
Temp[0] = bpb->Comm.m_descriptor; /** 砞称磞瓃じ **/
/*-----------------------------+
| ミ ㄢ FAT |
+-----------------------------*/
NowSect += bpb->Comm.nBootSect;
for ( i = 0 ; i Comm.nFAT ; i++ )
{
if ( lba_WriteWDK(NowSect, 1, Temp) )
return( -1 );
/*-------------- 璸 衡 材 FAT ぇ 熬 簿 秖 ---------------*/
NowSect += ((isFAT32) ? bpb->FAT32.sectPerFAT : bpb->FAT1x.sectPerFAT);
}
return( 0 );
} /* end Format */
| FAT 12 or 16 |
+---------------------*/
else
{
bpb->Comm.nBootSect = 1; /** FAT 玡合跋计 **/
bpb->FAT1x.nFdbRoot = 512; /** ヘ魁兜计 **/
bpb->FAT1x.firstHD = 0x80; /** 材场祑盒腹 **/
bpb->FAT1x.signature = 0x29; /** 疭紉絏 **/
bpb->FAT1x.serNoLow = random(0xffff) + 1;
bpb->FAT1x.serNoHigh = random(0xffff) + 1;
cpymem(bpb->FAT1x.label, "NO NAME ", 11); /** Label **/
cpymem(bpb->FAT1x.fsType, "FAT16 ", 8); /** 郎╰参 **/
if ( ptr->old_id == 1 )
{
bpb->FAT1x.fsType[4] = '2';
bpb->Comm.sectPerClust = 8; /** FAT12 **/
}
else
{
/*----------------------------------+
| – Cluster 合跋计 |
+----------------------------------*/
bpb->Comm.sectPerClust = ( Size FAT1x.bootStrap; /** 币笆祘Α絏熬簿竚 **/
} /* end if */
codePTR = (byte FAR *)BOOT_HEAD;
while ( codePTR != (byte FAR *)BOOT_END ) /** 恶币笆祘Α絏 **/
*bootStrap++ = *codePTR++;
/*--------------------------------------+
| Modify SI context at BootSect.asm |
| and fill JUMP instruction to buffer |
+--------------------------------------*/
bpb->Comm.NOP = 0x90; /** NOP **/
bpb->Comm.jumpCode = 0xeb;
if ( ptr->old_id == 0xb || ptr->old_id == 0xc )
{ /** JMP 58h 单 BPB **/
bpb->Comm.jumpOffset = 512 - sizeof(bpb->FAT32.bootStrap) - 4;
bpb->FAT32.bootStrap[ (word)BOOT_DATA - (word)BOOT_HEAD + 1 ] += 0x5a;
}
else
{ /** JMP 3Ch 单 BPB **/
bpb->Comm.jumpOffset = 512 - sizeof(bpb->FAT1x.bootStrap) - 4;
bpb->FAT1x.bootStrap[ (word)BOOT_DATA - (word)BOOT_HEAD + 1 ] += 0x3e;
}
} /* end CreateBPB */
/*============================= 秈 ︽ Α て ==============================*/
sword Format (PARTN *ptr, void *bootSect)
{
dword Sys_Sector, NowSect, Start, Total;
byte Temp[512];
FS_SECT *fsSect;
byte isFAT32;
DOSBPB *bpb;
sword ratio, i;
bpb = (DOSBPB *)bootSect;
isFAT32 = ( (ptr->old_id == 0xb) || (ptr->old_id == 0xc) );
NowSect = GetStart(ptr); /** 眔币﹍合跋 **/
Total = get_nsector(ptr); /** だ澄合跋羆计 **/
if ( isFAT32 ) /** 璸衡郎╰参ノぶ合跋 **/
Sys_Sector = bpb->FAT32.sectPerFAT * bpb->Comm.nFAT +
bpb->Comm.nBootSect + bpb->Comm.sectPerClust;
else
Sys_Sector = bpb->FAT1x.sectPerFAT * bpb->Comm.nFAT +
bpb->Comm.nBootSect + (bpb->FAT1x.nFdbRoot >> 4);
#if ( DISPLAY == CHINESE )
show_msg("タ 秈 ︽ Α て Ч Θ ");
#else
show_msg("It's formatting, completed ");
#endif
/*-----------------------------*/
/* ╰ 参 合 跋 恶 */
/*-----------------------------*/
for ( Start = 0 ; Start title = 0x41615252L;
fsSect->signature = 0x61417272L;
fsSect->next_free_clus = 0x00000002L;
fsSect->magicNum = 0xaa55;
/*----------------------------------------------------+
| FAT32 郎 ╰ 参 合 跋 戈 癟 |
+----------------------------------------------------*/
fsSect->free_clust_cnt = (Total - Sys_Sector) /
(dword)bpb->Comm.sectPerClust;
if ( lba_WriteWDK(NowSect + 1, 1, fsSect) ||
lba_WriteWDK(NowSect + bpb->FAT32.bakBootSect + 1, 1, fsSect) )
{
return( -1 );
}
memset(Temp, 0, 512); /** ミ FAT ㄏノぇ既丁 **/
*(dword *)&Temp[4] = 0x0fffffffL;
*(dword *)&Temp[8] = 0x0fffffffL;
Temp[3] = 0x0f; /** 砞﹚ FAT 繷 **/
}
else
{
if ( ptr->old_id != 1 ) /** 砞﹚ FAT 繷 **/
Temp[3] = 0xff;
}
*(word *)&Temp[1] = 0xffff; /** 砞﹚ FAT 繷 **/
Temp[0] = bpb->Comm.m_descriptor; /** 砞称磞瓃じ **/
/*-----------------------------+
| ミ ㄢ FAT |
+-----------------------------*/
NowSect += bpb->Comm.nBootSect;
for ( i = 0 ; i Comm.nFAT ; i++ )
{
if ( lba_WriteWDK(NowSect, 1, Temp) )
return( -1 );
/*-------------- 璸 衡 材 FAT ぇ 熬 簿 秖 ---------------*/
NowSect += ((isFAT32) ? bpb->FAT32.sectPerFAT : bpb->FAT1x.sectPerFAT);
}
return( 0 );
} /* end Format */