//readmbr.c //written by, Soni #include #include #include "mbr.h" static void near * ptrMBR; void showPartition(unsigned char (*)[]); void main(int argc, char * argv[]){ if( argc < TWO ){ printf("argument required....\n%s < r | w | s >\n",argv[ZERO]); printf("r, HD read and floppy write,\n"); printf("w, floppy read and HD write.\n"); printf("s, Just shows the partition table.\n"); printf("Written by soni.malik@gmail.com\n"); return; } if( *argv[ONE] == ARGUMENT_ONE || *argv[ONE] == ARGUMENT_TWO || *argv[ONE] == ARGUMENT_THREE) ; else{ printf("Nothing happend! allowed arguments are, r, w or s.\n"); return; } if( ( ptrMBR = malloc(SIZE) ) == NULL ){ printf("Unable to allocate memory in the near heap.\n"); printf("Check the value of directive SIZE, if it is more than 0x0200,\n"); printf("then decrease it by 0x0100 and then recompile.\n"); return; } if(*argv[ONE] == ARGUMENT_THREE || *argv[ONE] == ARGUMENT_ONE) //reads MBR from the HD. //C drive is default. Change DRIVE_HD if you want other drive. readSector(ptrMBR, DRIVE_HD + HEAD, CYLINDER + SECTOR_ONE, SECTORS); if(*argv[ONE] == ARGUMENT_TWO) //reads MBR from the FD. //A drive is default. Change DRIVE_FD for other drive. readSector(ptrMBR, DRIVE_FD + HEAD, TRACK + SECTOR_ONE, SECTORS); //check if what is read, is MBR. if( ((unsigned char *)ptrMBR)[SYMBOLAINDEX] == SYMBOLA && ((unsigned char *)ptrMBR)[SYMBOLBINDEX] == SYMBOLB ) printf("read MBR....\n"); else{ printf("unable to read MBR, returning\n"); return; } if( *argv[ONE] == ARGUMENT_THREE ){ showPartition((unsigned char (*)[])((char *)ptrMBR + 0x01be) ); return; } if( *argv[ONE] == ARGUMENT_ONE ) //writes MBR to the FD. //A drive is default. Change DRIVE_FD for other drive. writeSector(ptrMBR, DRIVE_FD + HEAD, TRACK + SECTOR_ONE, SECTORS); if( *argv[ONE] == ARGUMENT_TWO ) //writes MBR to the HD. //C drive is default. Change DRIVE_HD if you want other drive. writeSector(ptrMBR, DRIVE_HD + HEAD, CYLINDER + SECTOR_ONE, SECTORS); //I'm assuming that the MBR is written, //to make sure that it should happen, put a floppy in the drive before, //the execution of this program. printf("written MBR....\n"); printf(":)\n"); } void showPartition(unsigned char (*tables)[16]){ int count = 0; unsigned int Scyl = 0; unsigned int Ssec = 0; unsigned int Ecyl = 0; unsigned int Esec = 0; unsigned long Rsec = 0; printf("Partition table.\n"); printf("Boot Shead Scyl Ssec sys Ehead Ecyl Esec Rsec size(Sec)\n"); for(count=0; count<4; count++){ Scyl = ((*((unsigned int *)(tables[count] + 2)) & 0x00c0) << 2) + tables[count][3]; Ssec = tables[count][2] & 0x003f; Ecyl = ((*((unsigned int *)(tables[count] + 6)) & 0x00c0) << 2) + tables[count][7]; Esec = tables[count][6] & 0x003f; Rsec = *((unsigned long *)(tables[count] + 8)); printf("%.2x %.2d %.4d %.2d %.2x %2d %.4d %.2d %.8ld %.8ld\n",tables[count][0], tables[count][1], Scyl, Ssec, tables[count][4], tables[count][5], Ecyl, Esec, Rsec, *((unsigned long *)(tables[count] +12)) ); } }