/* With thanks to http://www.datarescue.com/laboratory/partition.htm */ #include #include #include #include #include #include #include #include #include "m.h" struct partition { unsigned char bootable; /* 0 or 0x80 */ unsigned char begin_head; unsigned char begin_chs[2]; unsigned char sys_type; unsigned char end_chs[2]; unsigned char start_sect[4]; /* starting sector counting from 0 */ unsigned char nr_sects[4]; /* nr of sectors in partition */ }; int fs_idx(int n) { int i; for (i=0; i386_sys_types[i].name!=NULL; i++) { if (i386_sys_types[i].type==n) { return i; } } return -1; } void show_partition_layout(struct partition p) { int cyl=0; int sect=0; printf(" Tag : 0x%02x\n", p.sys_type); cyl=(p.begin_chs[0]<<2) + (p.begin_chs[1]>>6); sect=p.begin_chs[1] >> 1; printf(" Start CHS : %d / %d / %d\n", cyl, p.begin_head, sect); printf(" Size : 0x%02x%02x%02x%02x\n", p.nr_sects[0], p.nr_sects[1], p.nr_sects[2], p.nr_sects[3]); } void read_partitions(char *dev) { int fd; struct partition p; int i; char *c; /* Open the device Read-Only (I don't really know what I'm doing!) */ fd=open(dev, O_RDONLY); if (fd<0) { printf("Cannot open %s for reading\n", dev); exit(1); } /* Skip bootblock */ if (lseek(fd, 0x1BE, 0)==-1) { printf("Error skipping bootblock: %s (%d)\n", strerror(errno), errno); exit(1); } for (i=1; i<=4; i++) { if (read(fd, &p, 16)==-1) { printf("Error %d reading partition: %s\n", errno, strerror(errno)); close(fd); free(c); exit(1); } printf("%s%d : %s", dev, i, i386_sys_types[fs_idx(p.sys_type)].name); if (p.bootable==0x80) printf(" *\n"); else printf("\n"); /* Temporary hack to see what we've got ... */ show_partition_layout(p); } close(fd); } int main() { /* Let's be crude ... just look at hda and hdb for now! */ read_partitions("/dev/hda"); read_partitions("/dev/hdb"); /* If we get this far, we didn't *detect* any problems! */ return 0; }