分区体系与文件系统——分区体系(中)

作者:落雪风之殇 和c/c++相关  

这个是 linux C实现的读取MBR的代码:

(我的系统是 64 位 fedora 18,编译环境是 gcc 4.7.2)                                     


  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>

  7. #define MBR_TABLE 512 //MBR分区表大小

  8. #define PARTITION_OFFSET 446 //分区表项的起始位置
  9. #define PARTITION_START 16             //分区大小

  10. typedef struct mbr_struct
  11. {
  12.     unsigned char active_record; //0x00表示非活动记录,0x80表示活动记录
  13.     unsigned char start[3];  //起始磁头号,扇区号,柱面号。其中磁头号1字节,扇区号2字节的低6位,柱面号2字节的高2位+3字节
  14.     unsigned char partition_type;     //分区类型
  15.     unsigned char end[3];             //结束磁头号
  16.     unsigned int start_sector;         //逻辑起始扇区号
  17.     unsigned int occupy_sector_num; //占用的扇区数
  18. }mbr_read;


  19. void print_mbr_info(mbr_read *mbr)
  20. {
  21.     printf("(第1字节)分区活动标记: %02hhX\n", mbr->active_record);
  22.     
  23.     printf("(第2、3、4字节)分区起始磁头、扇区、柱面号:");
  24.     printf("%02hhX%02hhX%02hhX\n", mbr->start[0], mbr->start[1], mbr->start[2]);
  25.     
  26.     printf("(第5字节)分区类型: %02hhX\n", mbr->partition_type);
  27.     
  28.     printf("(第6、7、8字节)分区结束磁头、扇区、柱面号:");
  29.     printf("%02hhX%02hhX%02hhX\n", mbr->end[0], mbr->end[1], mbr->end[2]);
  30.     
  31.     printf("(第9、10、11、12字节)逻辑起始扇区号:%u\n", mbr->start_sector);
  32.     
  33.     printf("(第13、14、15、16字节)所占扇区数量: %u\n", mbr->occupy_sector_num);
  34. }


  35. int main(int argc, char *argv[])
  36. {
  37.     
  38.     int fd, size, index;
  39.     char mbr_buffer[MBR_TABLE];
  40.     mbr_read *mbr;



  41.     /*打开文件和错误信息处理*/
  42.     fd = open("/dev/sda", O_RDONLY); //这个一定要注意,以只读打开。防止对mbr的误操作
  43.     if ( fd == 0 )
  44.     {
  45.         printf("open failed!\n");
  46.         exit(0);
  47.     }

  48.     size = read(fd, mbr_buffer, sizeof(mbr_buffer));

  49.     if ( size == -1 )
  50.     {
  51.         printf("权限不够,请用root权限操作!\n");
  52.         exit(0);
  53.     }
  54.     close(fd);



  55.     /*打印mbr信息,十六进制码*/
  56.     for (index = 0; index < MBR_TABLE; index++)
  57.     {
  58.         printf("%02hhX ", mbr_buffer[index]);
  59.     
  60.         if (( index + 1 ) % 32 == 0)
  61.         {
  62.             printf("\n");
  63.         }
  64.     }
  65.     printf("\n\n\n");



  66.     /*打印分区表项信息*/
  67.     printf("打印分区表项信息\n");
  68.     printf("\n分区表项 1:\n\n");
  69.     printf("当前分区数据:");
  70.     for (index = PARTITION_OFFSET + PARTITION_START * 0; index < PARTITION_OFFSET + PARTITION_START * 1; index++)
  71.     {
  72.         printf("%02hhX ", mbr_buffer[index]);
  73.     }
  74.     printf("\n\n");
  75.     print_mbr_info((mbr_read *)(mbr_buffer + PARTITION_OFFSET + PARTITION_START * 0));
  76.     



  77.     printf("\n\n分区表项 2:\n\n");
  78.     printf("当前分区数据:");
  79.     for (index = PARTITION_OFFSET + PARTITION_START * 1; index < PARTITION_OFFSET + PARTITION_START * 2; index++)
  80.     {
  81.         printf("%02hhX ", mbr_buffer[index]);
  82.     }
  83.     printf("\n\n");
  84.     print_mbr_info((mbr_read *)(mbr_buffer + PARTITION_OFFSET + PARTITION_START * 1));



  85.     
  86.     printf("\n\n分区表项 3:\n\n");
  87.     printf("当前分区数据:");
  88.     for (index = PARTITION_OFFSET + PARTITION_START * 2; index < PARTITION_OFFSET + PARTITION_START * 3; index++)
  89.     {
  90.         printf("%02hhX ", mbr_buffer[index]);
  91.     }
  92.     printf("\n\n");
  93.     print_mbr_info((mbr_read *)(mbr_buffer + PARTITION_OFFSET + PARTITION_START * 2));


  94.     

  95.     printf("\n\n分区表项 4:\n\n");
  96.     printf("当前分区数据:");
  97.     for (index =PARTITION_OFFSET + PARTITION_START * 3; index < PARTITION_OFFSET + PARTITION_START * 4; index++)
  98.     {
  99.         printf("%02hhX ", mbr_buffer[index]);
  100.     }
  101.     printf("\n\n");
  102.     print_mbr_info((mbr_read *)(mbr_buffer + PARTITION_OFFSET + PARTITION_START * 3));


  103.     return 0;

  104. }

相关资料:

分区体系与文件系统——分区体系(中)来源网络,如有侵权请告知,即处理!

编程Tags: