Don't try to use printf
with binary data. If your binary data starts with a NUL
(ASCII 0), then printf will assume you've got an empty string. You can use write()
to write out arbitrary data (it takes a buffer and length), e.g:
#include <unistd.h>
write(STDOUT_FILENO, mbr, 512)
...but even this won't necessarily display anything useful, because your terminal may try to interpret control characters in the output. You're best bet would then be to pipe the output to something like xxd
or od
, both of which will produce a hexdump of their input data.
For example, the first 512 bytes of my local drive are all NUL
. Using write()
in your code (and removing that lseek
) results in 512 NUL
bytes on output. Try passing something other than disk to your code, e.g.:
myexe /etc/passwd
The structure of a standard DOS MBR is documented here, suggesting that you might start with data structures like this:
struct _partition {
uint8_t status;
uint8_t chs_start[3];
uint8_t part_type;
uint8_t chs_end[3];
uint32_t lba_start;
uint32_t sectors;
};
And populate it something like this:
fd = open(target, O_RDONLY);
lseek(fd, 446, SEEK_SET);
for (i=0; i<4; i++) {
struct _partition p;
bytes_read = read(fd, &p, sizeof(struct _partition));
// assume this prints information to stdout or something.
print_part(i, &p);
}