Skip to content

Instantly share code, notes, and snippets.

@thomasalvatran
Last active January 28, 2022 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasalvatran/1ac0d12be6400d1577627b58fd5997fc to your computer and use it in GitHub Desktop.
Save thomasalvatran/1ac0d12be6400d1577627b58fd5997fc to your computer and use it in GitHub Desktop.
Read Master Boot Record(MBR) from the disk (USB, Hard Drive, SSD, SD card)
//Check img whether there is partition in MBR locates at 0x1BE of 1st sector
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE * in = fopen("test.img", "rb"); //change img to your img
// FILE * in = fopen("usb.img", "rb");
unsigned int i, start_sector, length_sectors;
fseek(in, 0x1BE, SEEK_SET); // go to partition table start
for(i=0; i<4; i++) { // read all four entries
printf("Partition entry %d: First byte %02X\n", i, fgetc(in));
printf(" Partition start in CHS: %02X:%02X:%02X\n", fgetc(in), fgetc(in), fgetc(in));
printf(" Partition type 0x%02X\n", fgetc(in));
printf(" Partition end in CHS: %02X:%02X:%02X\n", fgetc(in), fgetc(in), fgetc(in));
fread(&start_sector, 4, 1, in);
fread(&length_sectors, 4, 1, in);
printf(" Relative LBA address 0x%08X, %d sectors long\n", start_sector, length_sectors);
}
fclose(in);
return 0;
}
@thomasalvatran
Copy link
Author

First FAT sector of test.img with one partition at location 0x1BE. Each entry has 16 bytes, 0x55aa is the end of sector each sector has 512 bytes
tovantran@kubuntu-vm:~/Ctest/k_r/kernel_hi11Bulk$ sudo dd if=test.img bs=512 count=1 skip=0 | xxd
[sudo] password for tovantran:
1+0 records in
1+0 records out
512 bytes (512 B) copied, 3.9441e-05 s, 13.0 MB/s
0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000060: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000080: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000090: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000b0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000c0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000f0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000100: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000110: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000120: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000130: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000140: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000150: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000160: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000170: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000180: 0000 0000 0000 0000 0000 0000 0000 0000 ................
0000190: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00001a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00001b0: 0000 0000 0000 0000 0000 0000 0000 0002 ................
00001c0: 0400 0604 e4c9 8100 0000 7fcf 1d00 0000 ................
00001d0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00001e0: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00001f0: 0000 0000 0000 0000 0000 0000 0000 55aa ..............U.

@thomasalvatran
Copy link
Author

~/Ctest/k_r/kernel_hi11Bulk$ gcc read_mbr.c -o read_mbr
~/Ctest/k_r/kernel_hi11Bulk$ sudo ./read_mbr
Partition entry 0: First byte 00
Partition start in CHS: 00:04:02
Partition type 0x06
Partition end in CHS: C9:E4:04
Relative LBA address 0x00000081, 1953663 sectors long
Partition entry 1: First byte 00
Partition start in CHS: 00:00:00
Partition type 0x00
Partition end in CHS: 00:00:00
Relative LBA address 0x00000000, 0 sectors long
Partition entry 2: First byte 00
Partition start in CHS: 00:00:00
Partition type 0x00
Partition end in CHS: 00:00:00
Relative LBA address 0x00000000, 0 sectors long
Partition entry 3: First byte 00
Partition start in CHS: 00:00:00
Partition type 0x00
Partition end in CHS: 00:00:00
Relative LBA address 0x00000000, 0 sectors long
~/Ctest/k_r/kernel_hi11Bulk$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment