📄 fat.c
字号:
/*_____ V A R I A B L E __________________________________________________*/
bdata bit dir_is_root; /* TRUE: point the root directory */
//bdata bit fat_open_mode; // READ or WRITE
data Byte fat_cluster_size; /* cluster size (sector count) */
data Byte fat_cluster_mask; /* mask for end of cluster test */
//xdata char ext[3]; /* file extension (limited to 3 characters) */
xdata Uint32 fat_nb_sector;
xdata Uint32 fat_hidden_sector;
xdata Byte fat_sector_per_track;
xdata Byte fat_end_head;
xdata Uint16 fat_end_cylinder;
xdata Uint32 fat_ptr_fats; /* address of the first byte of FAT */
xdata Uint32 fat_ptr_rdir; /* address of the first byte of root dir */
xdata Uint32 fat_ptr_data; /* address of the first byte of data */
xdata Uint16 nb_reserved_sector;
/*F**************************************************************************
* NAME: fat_format
PURPOSE:
* Create single FAT12 or FAT16 partition and format the selected memory
*/
void fat_format ()
{
#define WAV_CLUST_SIZE (Uint16)((WAV_SECTOR_SIZE) / fat_cluster_size)
#define FORMAT_NB_CYLINDER (*tab).nb_cylinder
#define FORMAT_NB_HEAD (*tab).nb_head
#define FORMAT_NB_SECTOR (*tab).nb_sector
#define FORMAT_NB_HIDDEN_SECTOR (*tab).nb_hidden_sector
#define FORMAT_NB_SECTOR_PER_CLUSTER (*tab).nb_sector_per_cluster
Byte i, j;
Uint32 nb_total_sectors;
Uint32 temp;
Uint16 nb_sector_fat;
s_format code *tab;
tab = Hard_format();
fat_cluster_size = FORMAT_NB_SECTOR_PER_CLUSTER;
nb_total_sectors = (Uint32)FORMAT_NB_CYLINDER * ((Uint16)FORMAT_NB_HEAD * FORMAT_NB_SECTOR);
// FAT type caculation
fat_is_fat16 = ((nb_total_sectors / fat_cluster_size) > MAX_CLUSTERS12);
// -- MASTER BOOT RECORD --
Hard_write_open(0x00);
for (i = 446/2; i != 0; i--) // Boot Code
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
// First Partition entry
i=0;
gl_buffer[i++]=0x80; // Default Boot Partition
gl_buffer[i++]=(Byte)(FORMAT_NB_HIDDEN_SECTOR / FORMAT_NB_SECTOR); // Start head
gl_buffer[i++]=(Byte)((FORMAT_NB_HIDDEN_SECTOR % FORMAT_NB_SECTOR) + 1); // Start Sector
gl_buffer[i++]=0x00; // Start Cylinder
if (fat_is_fat16)
{
// if (nb_total_sectors > 0xFFFF) // Total Sectors
if((((Byte *)&nb_total_sectors)[0]!=0)||(((Byte *)&nb_total_sectors)[1]!=0))
{
gl_buffer[i++]=FAT16_SUP32M; // FAT16 > 32 Mbytes
}
else
{
gl_buffer[i++]=FAT16_INF32M; // FAT16 < 32 Mbytes
}
}
else
{
gl_buffer[i++]=FAT12; // FAT12
}
gl_buffer[i++]=(Byte)(FORMAT_NB_HEAD - 1); // Endhead-Zero-based(0)head number
gl_buffer[i++]=(Byte)((((FORMAT_NB_CYLINDER - 1) / 0x04) & 0xC0) + FORMAT_NB_SECTOR);
// EndSector-Zero-based(1) sector number
gl_buffer[i++]=(Byte)((FORMAT_NB_CYLINDER - 1) & 0xFF); // EndCylinder
gl_buffer[i++]=(Byte)(FORMAT_NB_HIDDEN_SECTOR ); // Start sector
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
temp = nb_total_sectors - FORMAT_NB_HIDDEN_SECTOR;
gl_buffer[i++] = ((Byte *)&temp)[3];
gl_buffer[i++] = ((Byte *)&temp)[2];
gl_buffer[i++] = ((Byte *)&temp)[1];
gl_buffer[i++] = ((Byte *)&temp)[0];
for(j=0;j<48;j++) gl_buffer[i++]=0x00;
gl_buffer[i++]=0x55; // Signature Word
gl_buffer[i++]=0xAA;
j=0;
for (; i != 0; i--) // Boot Code
{
Hard_write_byte(gl_buffer[j++]);
}
// -- HIDDEN SECTORS --
for (j = FORMAT_NB_HIDDEN_SECTOR - 1; j != 0; j--)
{
for (i = (SECTOR_SIZE / 4); i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
}
// -- PARTITION BOOT RECORD --
i=0;
gl_buffer[i++]=0xEB; // JMP inst to PBR boot code
gl_buffer[i++]=0x3C;
gl_buffer[i++]=0x90;
gl_buffer[i++]='O'; // OEM name
gl_buffer[i++]='E';
gl_buffer[i++]='M';
gl_buffer[i++]=' ';
gl_buffer[i++]='N';
gl_buffer[i++]='A';
gl_buffer[i++]='M';
gl_buffer[i++]='E';
gl_buffer[i++]=(Byte)SECTOR_SIZE; // number of bytes per sector
gl_buffer[i++]=(Byte)(SECTOR_SIZE >> 8);
gl_buffer[i++]=fat_cluster_size; // Number of sector per cluster
gl_buffer[i++]=(Byte)nb_reserved_sector; // number of reserved sector
gl_buffer[i++]=(Byte)(nb_reserved_sector >> 8);
gl_buffer[i++]=NB_FATS; // Number of FAT
gl_buffer[i++]=(Byte)NB_ROOT_ENTRY; // number of root directory entries
gl_buffer[i++]=(Byte)(NB_ROOT_ENTRY >> 8);
// if (nb_total_sectors > 0xFFFF) // Total Sectors
if((((Byte *)&nb_total_sectors)[0]!=0)||(((Byte *)&nb_total_sectors)[1]!=0))
{
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
}
else
{
gl_buffer[i++] = ((Byte *)&temp)[3];
gl_buffer[i++] = ((Byte *)&temp)[2];
}
gl_buffer[i++]=HARD_DISK; // Media Byte
// Number of sector in each FAT
if (fat_is_fat16)
{
nb_sector_fat = ((temp / fat_cluster_size) * 2 / 512) + 1;
}
else
{
nb_sector_fat = ((temp / fat_cluster_size) * 3 / 1024) + 1;
}
gl_buffer[i++]=(Byte)(nb_sector_fat);
gl_buffer[i++]=(Byte)(nb_sector_fat >> 8);
gl_buffer[i++]=(Byte)(FORMAT_NB_SECTOR); // Number of sectors on a track
gl_buffer[i++]=(Byte)(FORMAT_NB_SECTOR >> 8);
gl_buffer[i++]=(Byte)(FORMAT_NB_HEAD); // Number of heads
gl_buffer[i++]=(Byte)(FORMAT_NB_HEAD >> 8);
gl_buffer[i++]=(Byte)(FORMAT_NB_HIDDEN_SECTOR); // Number of hidden sectors
gl_buffer[i++]=(Byte)(FORMAT_NB_HIDDEN_SECTOR >> 8);
gl_buffer[i++]=(Byte)(FORMAT_NB_HIDDEN_SECTOR >> 16);
gl_buffer[i++]=(Byte)(FORMAT_NB_HIDDEN_SECTOR >> 24);
// if (nb_total_sectors > 0xFFFF)
if((((Byte *)&nb_total_sectors)[0]!=0)||(((Byte *)&nb_total_sectors)[1]!=0))
{ // number of sectors > 65535
gl_buffer[i++] = ((Byte *)&temp)[3];
gl_buffer[i++] = ((Byte *)&temp)[2];
gl_buffer[i++] = ((Byte *)&temp)[1];
gl_buffer[i++] = ((Byte *)&temp)[0];
}
else
{ // number of sectors < 65535
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
}
gl_buffer[i++]=FAT_DRIVE_NUMBER; // Driver number
gl_buffer[i++]=0x00; // not used
gl_buffer[i++]=FAT_EXT_SIGN; // extended boot signature
gl_buffer[i++]=0x00; // volume ID
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
gl_buffer[i++]=0x00;
gl_buffer[i++]='N'; // Volume Label
gl_buffer[i++]='O';
gl_buffer[i++]=' ';
gl_buffer[i++]='N';
gl_buffer[i++]='A';
gl_buffer[i++]='M';
gl_buffer[i++]='E';
gl_buffer[i++]=' ';
gl_buffer[i++]=' ';
gl_buffer[i++]=' ';
gl_buffer[i++]=' ';
gl_buffer[i++]='F'; // File System Type in ASCII
gl_buffer[i++]='A';
gl_buffer[i++]='T';
gl_buffer[i++]='1';
if (fat_is_fat16)
{
gl_buffer[i++]='6';
}
else
{
gl_buffer[i++]='2';
}
gl_buffer[i++]=' ';
gl_buffer[i++]=' ';
gl_buffer[i++]=' ';
j=0;
for (; i != 0; i--) // Boot Code
{
Hard_write_byte(gl_buffer[j++]);
}
for (i = 448/2; i != 0; i--) // Boot Code
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
Hard_write_byte(0x55); // Signature word
Hard_write_byte(0xAA);
for (j = (nb_reserved_sector-1); j != 0 ; j--)
{
for (i = 128; i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
}
//------------------
// -- FATS --
// -- FAT 1 --
Hard_write_byte(0xF8); // reserved clusters 0 & 1
Hard_write_byte(0xFF);
Hard_write_byte(0xFF);
if (fat_is_fat16)
{ // FAT16
Hard_write_byte(0xFF);
}
else
{ // FAT12
Hard_write_byte(0x00);
}
// free clusters in first FAT sector
for (i = (SECTOR_SIZE - 4) / 2; i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
// free clusters in other FAT sectors
for (j = nb_sector_fat - 1; j != 0; j--)
{
for (i = SECTOR_SIZE / 4; i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
}
// -- FAT 2 --
Hard_write_byte(0xF8); // reserved clusters 0 & 1
Hard_write_byte(0xFF);
Hard_write_byte(0xFF);
if (fat_is_fat16)
{ // FAT16
Hard_write_byte(0xFF);
}
else
{ // FAT12
Hard_write_byte(0x00);
}
// free clusters in first FAT sector
for (i = (SECTOR_SIZE - 4) / 2; i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
// free clusters in other FAT sectors
for (j = nb_sector_fat - 1; j != 0; j--)
{
for (i = SECTOR_SIZE / 4; i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
}
// -- ROOT DIRECTORY ENTRIES --
for (j = NB_ROOT_ENTRY / 4; j != 0 ; j--)
{
for (i = DIR_SIZE; i != 0; i--)
{
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
Hard_write_byte(0x00);
}
}
Hard_write_close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -