|
发表于 2003-7-15 05:37:32
|
显示全部楼层
本来认为可以直接open("/dev/hda5"...打开第一个逻辑盘,读第一个扇区,但是试了一下不成功,不知道为什么,我前面用dd if=/dev/hda5 of=hda5fs count=1试了一下可以的,但是后来再试又不行了,难道我记错了?奇怪。
- #include <stdio.h>
- #define __USE_FILE_OFFSET64
- #include <unistd.h>
- #include <fcntl.h>
- struct {
- unsigned int index;
- char *name;
- } typeinfo[] = {
- 0x0, "none",
- 0x1, "FAT12",
- 0x2, "XENIX root",
- 0x3, "XENIX usr",
- 0x4, "FAT16 <=32M",
- 0x5, "EXTEND",
- 0x6, "FAT16 >=32M",
- 0x7, "OS/2 HPFS",
- 0x8, "AIX",
- 0x9, "AIX bootable",
- 0xa, "OS/2 boot manager",
- 0xb, "FAT32",
- 0xc, "FAT32 (LBA)",
- 0xe, "FAT16 (LBA)",
- 0xf, "EXTEND (LBA)",
- 0x40, "Venix 80286",
- 0x51, "Novell",
- 0x52, "Microport",
- 0x63, "GNU HURD",
- 0x64, "Novell Netware 286",
- 0x65, "Novell Netware 386",
- 0x75, "PIC/IX",
- 0x80, "Old MINIX",
- 0x81, "Linux/MINUX",
- 0x82, "Linux SWAP",
- 0x83, "Linux Native",
- 0x85, "Linux EXTEND",
- 0x93, "Amoeba",
- 0x94, "Amoeba BBT",
- 0xa5, "BSD/386",
- 0xa6, "OpenBSD",
- 0xa7, "NEXTSTEP",
- 0xb7, "BSDI fs",
- 0xb8, "BSDI swap",
- 0xc7, "Syrinx",
- 0xdb, "CP/M",
- 0xe1, "DOS access",
- 0xe3, "DOS R/O",
- 0xf2, "DOS secondary",
- 0xff, "BBT"
- };
- void getPartitions(char *);
- void getLogic(char *, long long int);
- void printPType(unsigned int);
- int readSector(unsigned char *, char *, long long int);
- int
- main(int argc, char *argv[])
- {
- int i;
- char hd[10];
- if(argc == 1){
- fprintf(stderr, "usage: %s hdx\n", argv[0]);
- exit(-1);
- }
- for(i = 1; i < argc; ++i){
- if(strlen(argv[i]) != 3
- || argv[i][0] != 'h' || argv[i][1] != 'd'
- || argv[i][2] < 'a' || argv[i][2] > 'd'){
- fprintf(stderr, "%s is error name\n", argv[i]);
- continue;
- }
- strcpy(hd, "/dev/");
- strcat(hd, argv[i]);
-
- getPartitions(hd);
- printf("\n");
- }
-
- exit(0);
- }
- /* 读分区表并打印4个主分区 */
- void
- getPartitions(char *hd)
- {
- int i;
- unsigned char *type, buf[512];
- long long int off;
- printf("%s:\n----------\n", hd);
- if(readSector(buf, hd, 0) < 0)
- return;
- for(i = 0, type = buf + 446 + 4; i < 4; ++i, type += 16){
- printf("%s%d: ", hd, i + 1);
- printPType(*type);
- printf("\n");
- if(*type == 0x5 || *type == 0xf){
- off = *((unsigned int *)(type + 4));
- getLogic(hd, off);
- }
- }
- }
- /* 读逻辑盘的第一个扇区,并打印类型 */
- void
- getLogic(char *hd, long long int off)
- {
- int i;
- char hdx[15];
- unsigned char type, buf[512];
- long long int coff = 0;
- for(i = 5; ; ++i){
- sprintf(hdx, "%s%d", hd, i);
- if(readSector(buf, hd, off + coff) < 0)
- return;
- type = buf[446 + 4];
- printf(" %s: ", hdx);
- printPType(type);
- printf("\n");
- if(buf[446 + 4 + 16] == 0)
- return;
- else
- coff = *((unsigned int *)(buf + 446 + 16 + 8));
- }
- }
- /* 打印分区类型 */
- void
- printPType(unsigned int type)
- {
- int i;
- for(i = 0; i < sizeof(typeinfo) / sizeof(typeinfo[0]); ++i){
- if(typeinfo[i].index == type){
- printf("%s", typeinfo[i].name);
- return;
- }
- }
- printf("unknown");
- }
- /* 读hd盘的第off扇区 */
- int
- readSector(unsigned char *buf, char *hd, long long int off)
- {
- int fd;
- char errstr[1024];
- if((fd = open(hd, O_RDONLY)) < 0){
- snprintf(errstr, 1023, "%s cannot open", hd);
- perror(errstr);
- return(-1);
- }
- off *= 512;
- if(lseek(fd, off, SEEK_SET) < 0){
- snprintf(errstr, 1023, "cannot seek %s to %lld", hd, off);
- perror(errstr);
- close(fd);
- return(-1);
- }
- if(read(fd, buf, 512) != 512){
- snprintf(errstr, 1023, "cannot read first sector for %s", hd);
- perror(errstr);
- close(fd);
- return(-1);
- }
- close(fd);
- return(0);
- }
复制代码 |
|