主要参考:http://www.linuxforum.net/forum/showflat.php?Cat=&Board=linuxK&Number=288776&fpart=all
http://www.linuxforum.net/forum/showflat.php?Cat=&Board=linuxK&Number=69203&page=&view=&sb=&o=&vc=1
我暂时改成了我需要的(这还不是我的最终需求):
1 /* by hugang at 6/8 9:40 */ 2 /* mod by jevan at 31/8 11:56 */ 3 /* read some disk a sector */ 4 #include <stdio.h> 5 #include <errno.h> 6 #include <unistd.h> 7 #include <sys/types.h> 8 #include <sys/stat.h> 9 #include <fcntl.h> 10 11 #include <linux/fs.h> 12 13 int 14 get_disk_sector (int fd) 15 { 16 int sectorsize; 17 18 ioctl (fd, BLKSSZGET, §orsize); 19 20 return sectorsize; 21 } 22 23 /** 24 * read_disk_sector: 25 * @dev: raw disk FILE * 26 * @sector: 27 * return is the disk sectorsize 28 * */ 29 extern int errno; 30 int 31 read_disk_sector (int fd, unsigned long sector, char **p) 32 { 33 int sectorsize; 34 FILE *fp; 35 36 /* get disk sector size */ 37 sectorsize = get_disk_sector (fd); 38 if (sectorsize == 0) 39 { 40 fprintf (stderr, "get disk sector size failed\n"); 41 return (-1); 42 } 43 44 /* seek it */ 45 lseek (fd, 0, SEEK_SET); 46 if (lseek (fd, (sectorsize * sector), SEEK_CUR) == -1) 47 { 48 fprintf (stderr, "seek to %d failed\n", sectorsize * sector); 49 return (-1); 50 } 51 /* read it */ 52 *p = (char *) malloc (sectorsize); 53 if (*p == NULL) 54 { 55 fprintf (stderr, "malloc memory failed\n"); 56 return (-1); 57 } 58 59 return read (fd, *p, sectorsize); 60 } 61 62 /* dump data for display */ 63 void 64 dump_disk_sector (char *p, int size) 65 { 66 int i; 67 for (i = 0; i < size; i++) 68 { 69 if (i % 16 == 0 && i != 0) 70 printf ("\n"); 71 printf ("%2x ",/* (unsigned int) p & 0xff*/(unsigned char)*p++); 72 } 73 74 printf ("\n"); 75 return; 76 } 77 78 int 79 main (int argc,char* argv[]) 80 { 81 char *d; 82 int size; 83 int fd; 84 char *dev = "/dev/sda"; 85 86 /* open it */ 87 fd = open (dev, O_RDONLY); 88 if (fd == -1) 89 { 90 fprintf (stderr, "open %s failed %s\n", dev, strerror (errno)); 91 return (-1); 92 } 93 94 size = read_disk_sector (fd, atoi(argv[1]), &d); 95 96 close (fd); 97 98 if (size <= 0) 99 return (0); 100 101 dump_disk_sector (d, size); 102 103 free (d); 104 105 return (0); 106 }