mkdosfs 把分区信息写进磁盘.(三)

mkdosfs 把分区信息写进磁盘.(三)

以扇区大小为单位,分几步走:

一,保留扇区

1:清空保留扇区,(即FATS表之前的所有扇区清零,包括启动扇区,信息扇区,启动扇区备份,除以上其它的保留扇区)  reserved sector.

2:启动扇区,boot sector. (0--------511)

3:如果是FAT32,写信息扇区,info sector .  (512------1023)

4.如果是FAT32,且有设置有保留启动扇区备份,写备份启动扇区,backup boot sector. (backup_boot*512-------backup_boot*512+512)

二,FATS表

5.跟着保留扇区的是第一个FAT表,把第一个FAT表清零.first fat sector .    (backup_boot*512+512) ----- ((backup_boot*512+512) + (blank_fat_length*512))

6.跟着第一个FAT表后的是第二个FAT表,把第二个FAT表清零. second fat sector .    ((backup_boot*512+512) + (blank_fat_length*512)) -------(((backup_boot*512+512) + (blank_fat_length*512))+(blank_fat_length*512))

三.根目录项

7.跟着第二个FAT表的是根目录项,root directory   (((backup_boot*512+512) + (blank_fat_length*512))+(blank_fat_length*512)) ------((((backup_boot*512+512) + (blank_fat_length*512))+(blank_fat_length*512))+(size_root_dir*512))

static void write_tables(void)
{
    int x;
    int fat_length;

    fat_length = (size_fat == 32) ?
 CF_LE_L(bs.fat32.fat32_length) : CF_LE_W(bs.fat_length);

    seekto(0, "start of device");
    /* clear all reserved sectors */
    for (x = 0; x < reserved_sectors; ++x)
 writebuf(blank_sector, sector_size, "reserved sector");
    /* seek back to sector 0 and write the boot sector */
    seekto(0, "boot sector");
    writebuf((char *)&bs, sizeof(struct msdos_boot_sector), "boot sector");
    printf("sizeof(struct msdos_boot_sector) = %d\n",sizeof(struct msdos_boot_sector));   //sizeof(struct msdos_boot_sector) = 512
    /* on FAT32, write the info sector and backup boot sector */
    if (size_fat == 32) {
 seekto(CF_LE_W(bs.fat32.info_sector) * sector_size, "info sector");
    printf("bs.fat32.info_sector = %d,sector_size = %d\n",bs.fat32.info_sector,sector_size);  //bs.fat32.info_sector = 1,sector_size = 512
 writebuf(info_sector, 512, "info sector");
    printf("backup_boot  = %d\n",backup_boot);    //backup_boot  = 6
 if (backup_boot != 0) {
     seekto(backup_boot * sector_size, "backup boot sector");
     writebuf((char *)&bs, sizeof(struct msdos_boot_sector),
       "backup boot sector");
 }
    }
    /* seek to start of FATS and write them all */
    seekto(reserved_sectors * sector_size, "first FAT");
    printf("reserved_sectors = %d,sector_size = %d\n",reserved_sectors,sector_size);    //reserved_sectors = 32,sector_size = 512
    for (x = 1; x <= nr_fats; x++) {
 int y;
 int blank_fat_length = fat_length - alloced_fat_length;
 writebuf(fat, alloced_fat_length * sector_size, "FAT");
    printf("alloced_fat_length = %d , sector_size = %d\n",alloced_fat_length , sector_size);  //alloced_fat_length = 1 , sector_size = 512
    printf("blank_fat_length  = %d\n",blank_fat_length);    //blank_fat_length  = 16351
 for (y = 0; y < blank_fat_length; y++)
     writebuf(blank_sector, sector_size, "FAT");
    }
    /* Write the root directory directly after the last FAT. This is the root
     * dir area on FAT12/16, and the first cluster on FAT32. */
    printf("size_root_dir = %d\n",size_root_dir );     //size_root_dir = 4096
    writebuf((char *)root_dir, size_root_dir, "root directory");

    if (blank_sector)
 free(blank_sector);
    if (info_sector)
 free(info_sector);
    free(root_dir);  /* Free up the root directory space from setup_tables */
    free(fat);   /* Free up the fat table space reserved during setup_tables */
}

你可能感兴趣的:(磁盘管理)