Skip to content

Instantly share code, notes, and snippets.

@jou4
Created November 4, 2012 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jou4/4013906 to your computer and use it in GitHub Desktop.
Save jou4/4013906 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char data[446];
} mbr_bootstrap_loader_t;
typedef struct {
char header;
char cy_h_and_sector;
char cy_t;
} chs_t;
typedef unsigned int uint32;
typedef struct {
char flag;
chs_t first_sector;
char type;
chs_t last_sector;
uint32 first_sector_lba;
uint32 sector_num;
} mbr_partition_entry_t;
typedef unsigned short mbr_boot_signiture_t;
int dump(char *buf, int size)
{
int i, line, flag;
if(size < 0)
return -1;
for(i=0, line=0, flag=1; i < size; i++){
if(flag){
printf("%04d: ", ++line);
flag = 0;
}
printf("%02X", buf[i] & 0xFF);
if((i & 0xf) == 15){
printf("\n");
flag = 1;
}else{
if((i & 0xf) == 7) printf(" ");
printf(" ");
}
}
printf("\n");
return 0;
}
int main(void)
{
FILE *fp;
int size;
if((fp = fopen("/dev/sda", "r")) == NULL){
printf("file open failed.\n");
exit(EXIT_FAILURE);
}
mbr_bootstrap_loader_t mbr_bootstrap_loader;
mbr_partition_entry_t mbr_partition_entry[4];
mbr_boot_signiture_t mbr_boot_signiture;
size = fread(&mbr_bootstrap_loader, sizeof(mbr_bootstrap_loader_t), 1, fp);
size = fread(mbr_partition_entry, sizeof(mbr_partition_entry_t), 4, fp);
size = fread(&mbr_boot_signiture, sizeof(mbr_boot_signiture_t), 1, fp);
fclose(fp);
printf("\n");
printf("Boot strap loader\n");
dump(mbr_bootstrap_loader.data, sizeof(mbr_bootstrap_loader_t));
printf("\n");
int i;
mbr_partition_entry_t entry;
for(i=0; i<4; i++){
entry = mbr_partition_entry[i];
printf("Partition table entry [%u]\n", i);
printf(" Flag: %02X\n", entry.flag & 0xFF);
printf(" First sector(CHS): Cylinder=%u,Head=%u,Sector=%u\n",
((entry.first_sector.cy_h_and_sector & 0xC0) << 2) | (entry.first_sector.cy_t & 0xFF),
entry.first_sector.header & 0xFF,
entry.first_sector.cy_h_and_sector & 0x3F);
printf(" Type: %02X\n", entry.type & 0xFF);
printf(" Last sector(CHS): Cylinder=%u,Head=%u,Sector=%u\n",
((entry.last_sector.cy_h_and_sector & 0xC0) << 2) | (entry.last_sector.cy_t & 0xFF),
entry.last_sector.header & 0xFF,
entry.last_sector.cy_h_and_sector & 0x3F);
printf(" First sector(LBA): %u\n", entry.first_sector_lba);
printf(" Sector num: %u\n", entry.sector_num);
printf("\n");
}
printf("Boot signiture: %02X\n", mbr_boot_signiture);
printf("\n");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment