GRUB.cpp
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "fcntl.h"
#include "iostream"
#include "iomanip"
using namespace std;
struct partition
{
unsigned char boot_flag; /* 0 = Not active, 0x80 = Active */
unsigned char chs_begin[3];
unsigned char sys_type; /* For example : 82 --> Linux swap, 83 --> Linux native partition, ... */
unsigned char chs_end[3];
unsigned char start_sector[4];
unsigned char nr_sector[4];
};
void string_in_hex(void *in_string, int in_string_size)
{
for (int i = 0; i < in_string_size; i++)
{
printf("%02x", ((char *)in_string)[i]& 0xFF);
}
}
void dump_partition(struct partition *part, int partition_number, string dev)
{
cout<<endl<<"\t";
cout<<dev;
printf("%d\t", partition_number + 1);
if(part->boot_flag == 0x80){
printf("%02x *\t\t", part->boot_flag);
}
else if(part->boot_flag == 0x00){
printf("%02x\t\t", part->boot_flag);
}
printf("%02x\t", part->sys_type);
if (part->sys_type == 0x07){
cout<<"HPFS/NTFS/exFAT\t\t";
}
else if (part->sys_type == 0x05){
cout<<"Extended \t\t";
}
else if (part->sys_type == 0x83){
cout<<"Linux EXT4/EXT3\t\t";
}
else if (part->sys_type == 0x82){
cout<<"Linux swap \t\t";
}
else{
cout<<"OTHER \t\t";
}
string_in_hex(part->chs_begin, 3);
printf("\t\t");
string_in_hex(part->chs_end, 3);
printf("\t\t");
string_in_hex(part->start_sector, 4);
printf("\t");
string_in_hex(part->nr_sector, 4);
}
int main(int argc, char *argv[])
{
cout.setf(ios::left, ios::adjustfield);
char buf[512];
struct partition *sp;
if(argc != 2){
cout<<"Improper number of arguments\n";
exit(1);
}
int fd = open(argv[1], 'r');
if (fd == -1)
{
cout<<"cannot open file "<<argv[1]<<" !!!!!!!!!!!!!\n";
exit(1);
}
read(fd, buf, sizeof(buf));
close(fd);
cout<<"\n\t***************************************************************************\n";
cout<<"\t\t\t\t\t\t\tPRIMARY PARTITIONS OF "<<argv[1]<<endl;
cout<<"\t***************************************************************************\n\n";
cout<<"\t"<<setw(16)<<"Partiton"<<setw(16)<<"Boot Flag"<<setw(8)<<"Id"<<setw(24)<<"System"<<setw(16)<<"chs_begin"<<setw(16)<<"chs_end"<<setw(16)<<"start_sector"<<setw(16)<<"nr_sector\n\n";
for (int i = 0 ; i < 4 ; i++)
{
sp = (struct partition *)(buf + 446 + (16 * i));
dump_partition(sp, i, argv[1]);
}
cout<<"\n\n";
cout<<"\t-----------------------------------------------------------------\n";
cout<<"\t* : partition is bootable\n";
cout<<"\t------------------------------------------------------------------\n";
return 0;
}
Tags:
PROGRAMMING
how to use this program
ReplyDeletehow to run this program?
ReplyDeletekasa run karayacha bey??
ReplyDeletesawata kar program ...
Deletehow to run this program?
ReplyDeletewhile executing this code, just give "/etc/grub2.cfg" this path as secong argument after "./a.out"...
ReplyDeleteso it will display partition info
Thanks for the code. Runs fine.
ReplyDelete