Mail Archives: djgpp/2000/02/07/01:43:59
> Is there a "good clean" way of going around Windows to look at the
partition
> table (or even issue Drive Cmds?)
>
> Just wondering.
Here's some code I wrote to read the partition table (a quick hack, so
forgive any minor errors, it works ok though). Note: this code also saves
the mbr into mbr.bin, and the partition table to pt.bin:
/* reads partition table on first hard disk and prints info */
#include <bios.h>
#include <stdio.h>
struct partitionEntry {
unsigned char mediaByte __attribute__ ((packed));
unsigned char startHead __attribute__ ((packed));
unsigned short startCylSec __attribute__ ((packed));
unsigned char partitionType __attribute__ ((packed));
unsigned char endHead __attribute__ ((packed));
unsigned short endCylSec __attribute__ ((packed));
unsigned int sectorsToPartition __attribute__ ((packed));
unsigned int partitionSectors __attribute__ ((packed));
};
struct partitionTable {
struct partitionEntry partitions[4] __attribute__ ((packed));
} primaryPartitions;
unsigned char MasterBootRecord[512];
int readMBR(unsigned char *MBR);
struct partitionTable extractPartitionTable(unsigned char *MBR);
int main(void)
{
int c;
if (readMBR(MasterBootRecord)) {
printf("error reading MBR.\n");
exit(0);
}
primaryPartitions = extractPartitionTable(MasterBootRecord);
printf("Partition Table/MBR reading example\n");
printf("by Johan Venter (jventer AT writeme DOT com)\n\n");
printf("number\t\ttype\t\tsize\t\tactive?\n");
printf("======\t\t====\t\t====\t\t=======\n");
for (c = 0; c < 4; c++) {
printf("%d\t\t%d\t\t%d\t\t%s\n", c + 1,
primaryPartitions.partitions[c].partitionType,
primaryPartitions.partitions[c].partitionSectors*512/1024/1024,
primaryPartitions.partitions[c].mediaByte != 0 ? "Yes" : "No");
}
return 0;
}
int readMBR(unsigned char *MBR)
{
FILE *f = fopen("mbr.bin", "wb");
if (biosdisk(2, 0x80, 0, 0, 1, 1, MBR))
return 1;
else {
fwrite(MBR, 512, 1, f);
fclose(f);
return 0;
}
}
struct partitionTable extractPartitionTable(unsigned char *MBR)
{
FILE *f = fopen("pt.bin", "wb");
struct partitionTable temp;
memcpy(&temp, MBR + 0x1be, sizeof(struct partitionTable));
fwrite(&temp, sizeof(struct partitionTable), 1, f);
fclose(f);
return temp;
}
Hope this helps. This won't work from Windows NT obviously, so I suggest you
make a Windows 95 or plain DOS bootdisk from someone elses machine.
Johan Venter
ICQ 3643877
jventer AT writeme DOT com
Binaries Servers ==-----
- Raw text -