打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
硬盘的分区格式化的具体流程(DPT与DBR的操作)
//-----------------------------------》》》》》》》》存储设备信息结构体
typedef struct SFS_DiskInfo_S
{
SFS_Dst dev; //存储设备
typedef enum {
       DST_HD = 0, //硬盘
       DST_SD = 1, //SD卡
       DST_USB = 2, //USB
       DST_NULL = 3 //无
                      }SFS_Dst;
        
        int diskcnt; //硬盘个数
        GST_DISKINFO sdinfo; //SD卡信息
        typedef struct _GST_DISKINFO
        {
   char devname[40];                   /*  HardDisk name ( /dev/hda )  */
   int partitionNum;                               /* the number of partitions */
   int partitionID[GDE_MAXPARTITION]; /* the id of partition ( /dev/hda2  2 is id ) */
   int disktype[GDE_MAXPARTITION]; /* FAT32 FAT16 NTFS  */
   long long partitionSize[GDE_MAXPARTITION];  /* Partition Size ( K bytes) */
   long long size;                         /* the size of disk ( bytes )*/
   unsigned int  cylinder;                     /* the cylinder of the disk*/
        }GST_DISKINFO;      

GST_DISKINFO *pdiskinfo; //硬盘信息
}SFS_DiskInfo_T, *pSFS_DiskInfo_T;



----------------------------------》》》》》》》》》》》》
//分区信息
typedef struct{
unsigned char boot_ind;         /* 0x80 - active */
unsigned char head;             /* starting head */
unsigned char sector;           /* starting sector */
unsigned char cyl;              /* starting cylinder */
unsigned char sys_ind;          /* What partition type */
unsigned char end_head;         /* end head */
unsigned char end_sector;       /* end sector */
unsigned char end_cyl;          /* end cylinder */
unsigned char start4[4];        /* starting sector counting from 0 */
unsigned char size4[4];         /* nr of sectors in partition */
}Partition;

//分区表对应扇区信息
typedef struct{
Partition *part_table; /* points into sectorbuffer */
Partition *ext_pointer; /* points into sectorbuffer */
uint offset; /* disk sector number */
uint first; /*开始扇区号*/
uint last; /*结束扇区号*/
char *sectorbuffer; /* disk sector contents */
char changed; /* boolean */
}PartSecInfo;



//------------------------------》》》》》》》》》》》FAT32文件系统引导扇区
typedef struct _boot_sector
{
uint8 boot_jump[3]; /* Boot strap short or near jump */ // 跳转指令 3
uint8 system_id[8]; /* Name - can be used to special case //厂商标志OS版本号 8
partition manager volumes */
uint8 sector_size[2]; /* bytes per logical sector */ //扇区字节数 2
uint8 cluster_size; /* sectors/cluster */ //每簇扇区数 1
uint16 reserved; /* reserved sectors */ //保留山区数 2 (一般为32)
uint8 fats; /* number of FATs */ //FAT 数 (一般为2)
uint8 dir_entries[2]; /* root directory entries */ //根目录数 2(FAT12和FAT16中使用该字段

,FAT32中必须为0)
uint8 sectors[2]; /* number of sectors */ //小扇区数 2 (FAT12和FAT16中使用该字段,

FAT32中必须为0)
uint8 media; /* media code (unused) */ //媒体描述符 1 主要用于FAT16 0XF8表示硬盘 

0XF0表示高密度3.5寸软盘
uint16 fat_length; /* sectors/FAT */ //每FAT扇区数 2 (FAT12和FAT16中使用该字段,FAT32

中必须为0)
uint16 secs_track; /* sectors per track */ //每道扇区数 2
uint16 heads; /* number of heads */ //磁头数 2
uint32 hidden; /* hidden sectors (unused) */ //隐藏扇区数 4 该分区引导扇区之前的扇

区数
uint32 total_sect; /* number of sectors (if sectors == 0) */ // 总扇区数 4
struct 
{
uint32 fat32_length; /* sectors/FAT */ //每FAT扇区数 4 (只被FAT32使用)
uint16 flags; /* bit 8: fat mirroring, low 4: active fat */ //扩展标志 2 

(只被FAT32使用)
uint8 version[2]; /* major, minor filesystem version */ //文件系统版本 2 (只

被FAT32使用)
uint32 root_cluster; /* first cluster in root directory */ //根目录第一簇的簇号 

4 (只被FAT32使用,一般为2)
uint16 info_sector; /* filesystem info sector */ //文件系统信息扇区号 2
uint16 backup_boot; /* backup boot sector */ //备份引导扇区扇区号 2 (一般为6)
uint16 reserved2[6]; /* Unused */ //保留字段 12
msdos_volume_info vi; 
uint8 boot_code[BOOTCODE_FAT32_SIZE];
} __attribute__ ((packed)) fat32;
uint16 boot_sign;
}__attribute__((packed)) msdos_boot_sector;



-------------------------------》》》》》》》》》》》》》分区类型标志:
#define WIN95_FAT32 0x0b
#define Win95_FAT32_LBA 0x0c
#define EXTENDED 0x05
#define WIN98_EXTENDED 0x0f
#define LINUX_EXTENDED   0x85




----------------------------》》》》》》》》》》分区格式化的实际操作
(存储设备)硬盘分区即写磁盘的DPT(disk partition table)表


(存储设备)硬盘格式化即将分区好的扩展分区写入DBR(Dos boot record分区引导记录),分成多少个逻辑分区就写

入多少个DBR,每个DBR都会指向下一个DBR(最大226)


DBR:4各部分(1)BIOS参数记录快BPB(BIOS Parameter Block),(2)磁盘标志记录表,(3)分区引导记录代码(引导

程序),(4)结束标志

格式化时:主要是BPB数据的初始化(文件系统:FAT,NTFS)





-------------------》》》》》》》》》(硬盘)存储设备分区流程
1.umount 所有分区

2.分区数为经验值:小于300G4个分区,大于则8个分区
   SD卡只一个分区

3.初始化分区表DPT
   创建一个扩展分区,创建一个逻辑分区

   即初始化分区扇区对应信息结构体:PartSecInfo

   struct hd_geometry geometry;
   ioctl(fd, BLKGETSIZE, &total_number_of_sectors):sector总数

   ioctl(fd, HDIO_GETGEO, &geometry):磁头,柱面,扇区数


   初始化以下两个结构体:

   //分区信息
typedef struct{
unsigned char boot_ind;         /* 0x80 - active */
unsigned char head;             /* starting head */
unsigned char sector;           /* starting sector */
unsigned char cyl;              /* starting cylinder */
unsigned char sys_ind;          /* What partition type */
unsigned char end_head;         /* end head */
unsigned char end_sector;       /* end sector */
unsigned char end_cyl;          /* end cylinder */
unsigned char start4[4];        /* starting sector counting from 0 */
unsigned char size4[4];         /* nr of sectors in partition */
}Partition;

//分区表对应扇区信息
typedef struct{
Partition *part_table; /* points into sectorbuffer */
Partition *ext_pointer; /* points into sectorbuffer */
uint offset; /* disk sector number */
uint first; /*开始扇区号*/
uint last; /*结束扇区号*/
char *sectorbuffer; /* disk sector contents */
char changed; /* boolean */
}PartSecInfo;



4.将分区表写入硬盘

5.system("mdev -s"); 添加热插拔机制



-------------------》》》》》》》》》》》》》硬盘格式化流程

磁盘的分区格式化,就是写入DPT表分区分扩展分区,再写入与MBR一样数据作用的DBR表即分成逻辑分区,也就是

格式化。


1.检测该分区是否格式化过
    
    struct mntent *mnt;
    FILE *f = setmntent (MOUNTED, "r")
    mnt = getmntent (f)
    strcmp (devicename, mnt->mnt_fsname):查看是否存在此分区
    endmntent (f);

    mntent结构体中的数据即是/etc/fstab中的数据。


2.fd = open 分区名
   fstat (dev, &statbuf):获取一些相关的信息
   (statbuf.st_rdev & 0xff3f) == 0x0300   || /* hda, hdb */
   (statbuf.st_rdev & 0xff0f) == 0x0800 || /* sd */
   (statbuf.st_rdev & 0xff3f) == 0x0d00   || /* xd */
   (statbuf.st_rdev & 0xff3f) == 0x1600 ) /* hdc, hdd */

3.建立设备参数,即初始化文件系统FAT32,NTFS
   struct hd_geometry geometry;
   ioctl (dev, HDIO_GETGEO, &geometry):获得磁盘物理参数
   (磁头数,磁道上的扇区数,sector数)


//格式化初始化下列结构体
//FAT32文件系统卷标信息
typedef struct _volume_info 
{
uint8 drive_number; /* BIOS drive number */ //物理驱动器号 1 (硬盘为0x80)
uint8 RESERVED; /* Unused */ //保留字段 1 (一般为0)
uint8 ext_boot_sign; /* 0x29 if fields below exist (DOS 3.3+) */ //扩展引导标签 1 一般为

0X28或0X29
uint8 volume_id[4]; /* Volume ID number */ //分区序号 4 (格式花的时候随即产生的序号)
uint8 volume_label[11];/* Volume label */ //卷标 11 (用来保存卷标号,现在被当成文件保存在根

目录区)
uint8 fs_type[8]; /* Typically FAT12 or FAT16 */ //系统ID FAT32中一般为"FAT32"
}__attribute__((packed)) msdos_volume_info;

//FAT32文件系统引导扇区
typedef struct _boot_sector
{
uint8 boot_jump[3]; /* Boot strap short or near jump */ // 跳转指令 3
uint8 system_id[8]; /* Name - can be used to special case //厂商标志OS版本号 8
partition manager volumes */
uint8 sector_size[2]; /* bytes per logical sector */ //扇区字节数 2
uint8 cluster_size; /* sectors/cluster */ //每簇扇区数 1
uint16 reserved; /* reserved sectors */ //保留山区数 2 (一般为32)
uint8 fats; /* number of FATs */ //FAT 数 (一般为2)
uint8 dir_entries[2]; /* root directory entries */ //根目录数 2(FAT12和FAT16中使用该字段

,FAT32中必须为0)
uint8 sectors[2]; /* number of sectors */ //小扇区数 2 (FAT12和FAT16中使用该字段,

FAT32中必须为0)
uint8 media; /* media code (unused) */ //媒体描述符 1 主要用于FAT16 0XF8表示硬盘 

0XF0表示高密度3.5寸软盘
uint16 fat_length; /* sectors/FAT */ //每FAT扇区数 2 (FAT12和FAT16中使用该字段,FAT32

中必须为0)
uint16 secs_track; /* sectors per track */ //每道扇区数 2
uint16 heads; /* number of heads */ //磁头数 2
uint32 hidden; /* hidden sectors (unused) */ //隐藏扇区数 4 该分区引导扇区之前的扇

区数
uint32 total_sect; /* number of sectors (if sectors == 0) */ // 总扇区数 4
struct 
{
uint32 fat32_length; /* sectors/FAT */ //每FAT扇区数 4 (只被FAT32使用)
uint16 flags; /* bit 8: fat mirroring, low 4: active fat */ //扩展标志 2 

(只被FAT32使用)
uint8 version[2]; /* major, minor filesystem version */ //文件系统版本 2 (只

被FAT32使用)
uint32 root_cluster; /* first cluster in root directory */ //根目录第一簇的簇号 

4 (只被FAT32使用,一般为2)
uint16 info_sector; /* filesystem info sector */ //文件系统信息扇区号 2
uint16 backup_boot; /* backup boot sector */ //备份引导扇区扇区号 2 (一般为6)
uint16 reserved2[6]; /* Unused */ //保留字段 12
msdos_volume_info vi; 
uint8 boot_code[BOOTCODE_FAT32_SIZE];
} __attribute__ ((packed)) fat32;
uint16 boot_sign;
}__attribute__((packed)) msdos_boot_sector;

  
  

4.写入文件系统

   设置开始扇区0
   /保留扇区5---10中的数据
   设置引导扇区/* seek back to sector 0 and write the boot sector */
   写入一张FAT32备份表/* on FAT32, write the info sector and backup boot sector */
   /* seek to start of FATS and write them all */
   写入根目录/* Write the root directory directly after the last FAT. This is the root
   * dir area on FAT12/16, and the first cluster on FAT32. */








本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
关于单片机EEPROM数据保存的若干经验总结
中国linux公社 - 基于atmel at91rm9200的armlinux的bootloader启动代码分析
硬盘数据恢复入门教程(数据存储原理篇)
基于单片机的SD卡设计
vboot完全解读
FAT32文件系统解析
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服