📄 fat.c
字号:
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Extension[2] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[0] = (S8)'.';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[1] = (S8)'.';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[2] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[3] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[4] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[5] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[6] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileName[7] = (S8)' ';
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Attribute = ARCHIVE_DIR;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[0] = 0x08;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[1] = 0x2A;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[2] = 0x2E;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[3] = 0x5A;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[4] = 0x2D;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[5] = 0x35;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[6] = 0x39;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Reserved[7] = 0x35;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FSClusHI = 0;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Time = 0x5A2E;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->Date = 0x352D;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FSClusLO = 0;
((_DIR *)(sbuf2 + (1 & 0x0F)* DIREntrySize))->FileSize = 0;
}
DMA_write_sector(p++, 0x06);
}
SendChar(0xC6);
return;
}
else
{
FileData.wCurFDBNo++;
if(!(FileData.wCurFDBNo & 0x0F))
flag = 0;
}
}
}
}
#endif
#if( 1 )
void fat_format (void)
{
#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
#ifndef MEM_RESERVED_SIZE
#define MEM_RESERVED_SIZE 0
#endif
Byte j;
Byte nb_hidden_sector;
Byte nb_sector;
Byte nb_head;
#define k nb_head
Uint16 nb_sector_fat;
Uint16 i;
xdata Uint32 nb_total_sectors;
bit parity_bit;
xdata s_format *tab;
#define reserved_cluster_nb nb_sector
#define nb_tot_cluster nb_total_sectors
tab = Hard_format();
nb_hidden_sector = FORMAT_NB_HIDDEN_SECTOR;
nb_sector = FORMAT_NB_SECTOR;
nb_head = FORMAT_NB_HEAD;
fat_cluster_size = FORMAT_NB_SECTOR_PER_CLUSTER;
nb_total_sectors = (Uint32)FORMAT_NB_CYLINDER * nb_head * nb_sector;
/* FAT type caculation */
fat_is_fat16 = (((nb_total_sectors - (Uint32)(nb_hidden_sector)) / fat_cluster_size) > MAX_CLUSTERS12);
#if FAT_PARTITIONNED == TRUE
/* -- MASTER BOOT RECORD -- */
Hard_write_open(MBR_ADDRESS);
for (i = 0; ((Byte*)&i)[0] != 0x02; i++) /* Boot Code */
{
fat_buf_sector[i] = 0x00;
}
/* First Partition entry */
fat_buf_sector[446] = 0x80; /* Default Boot Partition */
fat_buf_sector[447] = nb_hidden_sector / nb_sector; /* start head */
fat_buf_sector[448] = (nb_hidden_sector % nb_sector) + 1; /* Start Sector */
fat_buf_sector[449] = 0x00; /* Start Cylinder */
if (fat_is_fat16)
{
if (nb_total_sectors > 0xFFFF) /* Total Sectors */
{
fat_buf_sector[450] = FAT16_SUP32M; /* FAT16 > 32 Mbytes */
}
else
{
fat_buf_sector[450] = FAT16_INF32M; /* FAT16 < 32 Mbytes */
}
}
else
{
fat_buf_sector[450] = FAT12; /* FAT12 */
}
fat_buf_sector[451] = FORMAT_NB_HEAD - 1; /* Endhead-Zero-based(0) head number */
fat_buf_sector[452] = (Byte)((((FORMAT_NB_CYLINDER - 1) / 0x04) & 0xC0) + nb_sector);
/* EndSector-Zero-based(1) sector number */
fat_buf_sector[453] = (Byte)((FORMAT_NB_CYLINDER - 1) & 0xFF); /* EndCylinder */
fat_buf_sector[454] = nb_hidden_sector; /* Start sector */
fat_buf_sector[455] = 0x00;
fat_buf_sector[456] = 0x00;
fat_buf_sector[457] = 0x00;
nb_total_sectors -= (Uint32)(nb_hidden_sector);
fat_buf_sector[458] = ((Byte*)&nb_total_sectors)[3];
fat_buf_sector[459] = ((Byte*)&nb_total_sectors)[2];
fat_buf_sector[460] = ((Byte*)&nb_total_sectors)[1];
fat_buf_sector[461] = ((Byte*)&nb_total_sectors)[0];
fat_buf_sector[510] = 0x55; /* Signature Word */
fat_buf_sector[511] = 0xAA;
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
fat_buf_sector[i] = 0x00;
/* -- HIDDEN SECTORS -- */
for (j = nb_hidden_sector - 1; j != 0; j--)
{
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
}
#else /* fat not partitionned */
nb_total_sectors -= (Uint32)(nb_hidden_sector);
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
fat_buf_sector[i] = 0x00;
Hard_write_open(MBR_ADDRESS);
#endif /* FAT_PARTITIONNED == TRUE */
/* -- PARTITION BOOT RECORD -- */
for (j = 0; j < 22; j++)
fat_buf_sector[j] = PBR_record_part1[j];
fat_buf_sector[13] = fat_cluster_size; /* Number of sector per cluster */
if (nb_total_sectors <= 0xFFFF) /* Total Sectors */
{
fat_buf_sector[19] = ((Byte*)&nb_total_sectors)[3];
fat_buf_sector[20] = ((Byte*)&nb_total_sectors)[2];
}
/* Number of sector in each FAT */
if (fat_is_fat16)
nb_sector_fat = (((nb_total_sectors - 1 - 32) * 2) + (512 * fat_cluster_size)) /
((512 * fat_cluster_size) + 4);
else
nb_sector_fat = (((nb_total_sectors - 1 - 32) * 3) + (1024 * fat_cluster_size)) /
((1024 * fat_cluster_size) + 6);
fat_buf_sector[22] = ((Byte*)&nb_sector_fat)[1]; /* number of sector for each fat */
fat_buf_sector[23] = ((Byte*)&nb_sector_fat)[0];
fat_buf_sector[24] = nb_sector; /* number of sectors on a track */
fat_buf_sector[25] = 0x00; /* number of sectors on a track */
fat_buf_sector[26] = nb_head; /* number of heads */
fat_buf_sector[27] = 0x00; /* number of heads */
fat_buf_sector[28] = nb_hidden_sector; /* number of hidden sectors */
fat_buf_sector[29] = 0x00; /* number of hidden sectors */
if (nb_total_sectors > 0xFFFF)
{ /* number of sectors > 65535 */
fat_buf_sector[32] = ((Byte*)&nb_total_sectors)[3];
fat_buf_sector[33] = ((Byte*)&nb_total_sectors)[2];
fat_buf_sector[34] = ((Byte*)&nb_total_sectors)[1];
fat_buf_sector[35] = ((Byte*)&nb_total_sectors)[0];
}
for (j = 0, nb_head = 36; j < 26; j++,nb_head++)
{
fat_buf_sector[nb_head] = PBR_record_part2[j];
}
if (fat_is_fat16)
{
fat_buf_sector[58] = '6';
}
else
{
fat_buf_sector[58] = '2';
}
fat_buf_sector[510] = 0x55;
fat_buf_sector[511] = 0xAA;
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
/* -- FATS -- */
nb_tot_cluster = ((nb_total_sectors - (1 + (2 * nb_sector_fat) + 32))
/ fat_cluster_size) + 2 - (MEM_RESERVED_SIZE / FORMAT_NB_SECTOR_PER_CLUSTER);
for (k = 0; k < 2; k++)
{
if (reserved_disk_space == FALSE)
{
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
fat_buf_sector[i] = 0x00;
}
}
else
{
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
fat_buf_sector[i] = 0xFF;
}
}
fat_buf_sector[0] = 0xF8;
fat_buf_sector[1] = 0xFF;
fat_buf_sector[2] = 0xFF;
if (fat_is_fat16)
{ /* FAT16 */
fat_buf_sector[3] = 0xFF;
}
if (fat_is_fat16)
{
reserved_cluster_nb = nb_sector_fat - (nb_tot_cluster / 256) - 1;
nb_sector_fat = nb_tot_cluster / 256;
}
else
{
reserved_cluster_nb = nb_sector_fat - (nb_tot_cluster * 3 / 1024) - 1;
nb_sector_fat = nb_tot_cluster * 3 / 1024;
}
for (; nb_sector_fat != 0; nb_sector_fat--)
{
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
if (reserved_disk_space == FALSE)
{
fat_buf_sector[0] = 0x00;
fat_buf_sector[1] = 0x00;
fat_buf_sector[2] = 0x00;
fat_buf_sector[3] = 0x00;
}
else
{
fat_buf_sector[0] = 0xFF;
}
}
if (fat_is_fat16)
{
i = (nb_tot_cluster % 256) * 2;
}
else
{
parity_bit = nb_tot_cluster & 0x01;
i = (nb_tot_cluster * 3 / 2) & 0x1FF;
if (parity_bit == 0)
{
fat_buf_sector[i] = 0x00;
}
else
{
fat_buf_sector[i] &= 0x0F;
}
i++;
}
for (; ((Byte*)&i)[0] != 0x02; i++)
fat_buf_sector[i] = 0x00;
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
fat_buf_sector[i] = 0x00;
for ( ; reserved_cluster_nb != 0; reserved_cluster_nb--)
{
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
}
}
/* -- ROOT DIRECTORY ENTRIES -- */
for (j = NB_ROOT_ENTRY / 16; j != 0 ; j--)
{
for (i = 0; ((Byte*)&i)[0] != 0x02; i++)
{
Hard_write_byte(fat_buf_sector[i]);
}
}
Hard_write_close();
#undef k
#undef reserved_cluster_nb
#undef nb_tot_cluster
}
#endif
//***********************************************
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -