扫描磁盘分区表

2019-12-12  本文已影响0人  HAPPYers

Linux 中所有的设备都在/dev/目录下,硬盘命名规则是[x]d[y][n],其中只有字母 d 是固定的,其他带中括号的字符都是多选值,下面从左到右介绍各个字符。

__attribute__是gcc特有的关键字,用于告诉 gcc在编译时需要做些“特殊处理”, packed就是“特殊处理”, 意为压缩的。__attribute__ ((packed))即不允许编译器为对齐而在此结构中填充空隙,从而保证结构partition_table_entry的大小是 16 宇节,这与分区表项的大小是吻合的。
在vs中,可以使用类似的代码

#pragma pack(push)
#pragma pack(__packed__)
typedef struct{
      int a;
      char b;
      long l;
}test1;
typedef struct{
      char a1;
      char a2;
      test1 t1;
      double b1;
} test2;
#pragma pack(pop)

扇区的结构

/*------------------ partition structure define ------------------------------- */
//partition_table struct (16byte)
struct partition_table_entry {
   uint8_t  bootable;        // 是否可引导   
   uint8_t  start_head;      // 起始磁头号
   uint8_t  start_sec;       // 起始扇区号
   uint8_t  start_cylinder;      // 起始柱面号
   uint8_t  fs_type;         // 分区类型
   uint8_t  end_head;        // 结束磁头号
   uint8_t  end_sector;      // 结束扇区号
   uint8_t  end_cylinder;        // 结束柱面号

   uint32_t start_lba;       // 本分区起始扇区的lba地址
   uint32_t sec_cnt;         // 本分区的扇区数目
} __attribute__ ((packed));  // pack struct to ensure 16byte size

//boot_sector (store mbr/ebr)
struct boot_sector {
   uint8_t  other[446];      // boot code
   struct   partition_table_entry partition_table[4];       // four partition_tables, 64bytes
   uint16_t signature;       // boot sector ends with 0x55,0xaa
} __attribute__ ((packed));
/*----------------------------------------------------------------------------- */

此处在global.h中定义

#define UNUSED __attribute__((unused))

表示某个变量或函数不会用到,避免编译器产生警告信息

上一篇下一篇

猜你喜欢

热点阅读