I need to read all 4 hard drive partitions and display them on the screen.
Everything is fine with the output, but there is a problem with reading the partitions themselves. I don’t understand what to change - the head, cylinder or sector. I tried changing all these values and something is always read, which is why I don't really understand whether I read everything correctly.
The program is written in C using Borland C++ (TurboC). I work in Windows 98.
What information needs to be displayed about the section:
Offset Length Contents
+0 1 Boot Flag- boot flag: 0=not active, 80h=active.
+1 1 Begin Hd - start of section: head (side) number.
+2 2 Begin Sec/Cyl – start of section: sector/cylinder.
+4 1 System Code – file system code.
+5 1 Ending Hd – end of section: head number.
+6 2 Ending Sec/Cyl – end of section: sector/cylinder of the last sector.
+8 4 Starting Sector – logical number of the starting sector of the partition (address in
LBA system).
+0Ch 4 Num Sectors – partition size (number of sectors)
Program code:
#include <dos.h>
#include <stdio.h>
#include <conio.h>
//#define NUM_PARTITIONS 4
unsigned char buf[512];
int read_mbr() {
union REGS in, out;
struct SREGS sr;
int i = 0;
int j = 0;
for (; i < 64; i += 16, j++) {
sr.es = FP_SEG(buf + i);
in.x.bx = FP_OFF(buf + i);
in.h.ah = 0x02;
in.h.al = 1;
in.h.dl = 0x80 + j;
in.h.dh = 0;
in.h.cl = 1;
in.h.ch = 0;
int86x(0x13, &in, &out, &sr);
if (out.x.cflag || out.h.ah)
return 1;
printf("%d", j);
}
return 0;
}
int get_count_partitions() {
int numPartitions = 0;
int i = 0x1BE;
for (; i <= 0x1EE; i += 16) {
numPartitions++;
}
return numPartitions;
}
void extract_partition_info(int partition_number) {
unsigned char beginHd = buf[1];
unsigned short beginSecCyl = *((unsigned short*)(buf + 2));
// Print the extracted partition information
printf("Partition %d:\n", partition_number);
printf("Begin Hd: %u\n", beginHd);
printf("Begin Sec/Cyl: %u\n", beginSecCyl);
}
void print_partition_info(unsigned char* partition_entry, int partition_number) {
unsigned char bootFlag = partition_entry[0];
unsigned char beginHd = partition_entry[1];
unsigned short beginSecCyl = *((unsigned short*)(partition_entry + 2));
unsigned char systemCode = partition_entry[4];
unsigned char endingHd = partition_entry[5];
unsigned short endingSecCyl = *((unsigned short*)(partition_entry + 6));
unsigned int startingSector = *((unsigned int*)(partition_entry + 8));
unsigned int numSectors = *((unsigned int*)(partition_entry + 0x0C));
printf("Partition %d:\n", partition_number);
printf("Boot Flag: 0x%02X\n", bootFlag);
printf("Begin Hd: %u\n", beginHd);
printf("Begin Sec/Cyl: %u\n", beginSecCyl);
printf("System Code: 0x%02X\n", systemCode);
printf("Ending Hd: %u\n", endingHd);
printf("Ending Sec/Cyl: %u\n", endingSecCyl);
printf("Starting Sector (LBA): %u\n", startingSector);
printf("Number of Sectors: %u\n", numSectors);
printf("Root Boot Sector Address (LBA): %u\n", startingSector);
}
int main() {
int i = 0;
int j = 0;
clrscr();
if (read_mbr())
printf("Error reading sector\n");
else
printf("Sector read successfully\n");
printf("Number of sections: %d\n", get_count_partitions());
extract_partition_info(1);
getch();
clrscr();
for(;j < 512; j++)
printf("%d",(int)buf[j]);
getch();
for (; i < 64; i += 16){
clrscr();
print_partition_info(&buf[i], (i/16 + 1));
getch();
}
return 0;
}