diff --git a/Makefile b/Makefile index b827810..0a460c0 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,17 @@ CC = gcc AS = as LD = ld OBJCOPY = objcopy -CLFAGS = -g -Wall -ASFLAGS = -LDFLAGS = -g QEMU = qemu-system-x86_64 SRC_DIR = src +INC_DIR = include OBJ_DIR = obj BIN_DIR = bin +CLFAGS = -g -Wall -I$(INC_DIR)/ +ASFLAGS = +LDFLAGS = -g + UID := $(shell id -u) GID := $(shell id -g) @@ -97,6 +99,9 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.s $(AS) $(ASFLAGS) -o $@ $< $(OBJCOPY) --remove-section .note.gnu.property $@ +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CFLAGS) -c -o $@ $< + MBR_TEST_OBJ = obj/mbr_test.o obj/serial.o obj/itoa.o obj/util.o obj/test_disk_target.o mbr_test: $(MBR_TEST_OBJ) $(DISK) @@ -123,6 +128,11 @@ mbr_test_qemu: mbr_test -drive format=raw,media=disk,file=$(BIN_DIR)/mbr_test.bin \ -drive format=raw,media=disk,file=$(BIN_DIR)/disk.img +READMBR_OBJ = obj/readmbr.o + +readmbr: $(READMBR_OBJ) + $(CC) $(LDFLAGS) -o $(BIN_DIR)/readmbr $(READMBR_OBJ) + clean: rm -f bin/* rm -f obj/* diff --git a/src/readmbr.c b/src/readmbr.c new file mode 100644 index 0000000..d11e2ab --- /dev/null +++ b/src/readmbr.c @@ -0,0 +1,53 @@ +// program to read MBR and its partition table +#include +#include +#include + +typedef struct { + uint8_t cylinder; + uint8_t head; + uint8_t sector; +} __attribute__((packed)) chs_t; + +typedef struct { + uint8_t attributes; + chs_t chs_first; + uint8_t type; + chs_t chs_last; + uint32_t lba_start; + uint32_t num_sectors; +} __attribute__((packed)) mbr_part_t; + +typedef struct { + char bootstrap[440]; + uint32_t unique_id; + uint16_t reserved; + mbr_part_t part_table[4]; + uint16_t signature; +} __attribute__((packed)) mbr_t; + +int main(int argc, char** argv) +{ + mbr_t mbr; + + // if (argc < 2) + // { + // fputs("provide filename to disk image as first argument\n", stderr); + // return 1; + // } + + // int fd = open(argv[1], O_RDONLY); + // if (fd < 0) + // { + // perror("failed to open disk image\n"); + // return 1; + // } + + + // close(fd); + + printf("%lu\n", sizeof(mbr_t)); + printf("%lu\n", sizeof(mbr_part_t)); + + return 0; +} \ No newline at end of file