From 9a0489c51d0f7be74cf0264551bb05e2f8fd9edf Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:51:25 +0300 Subject: [PATCH] move MBR and CHS to its own files, so I can link them with other prorgrams --- Makefile | 4 ++-- include/chs.h | 8 ++++++++ include/mbr.h | 23 +++++++++++++++++++++++ src/chs.c | 8 ++++++++ src/readmbr.c | 36 +++++++----------------------------- 5 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 include/chs.h create mode 100644 include/mbr.h create mode 100644 src/chs.c diff --git a/Makefile b/Makefile index 0a460c0..94aa139 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ INC_DIR = include OBJ_DIR = obj BIN_DIR = bin -CLFAGS = -g -Wall -I$(INC_DIR)/ +CFLAGS = -g -Wall -I$(INC_DIR)/ ASFLAGS = LDFLAGS = -g @@ -128,7 +128,7 @@ 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_OBJ = obj/readmbr.o obj/chs.o readmbr: $(READMBR_OBJ) $(CC) $(LDFLAGS) -o $(BIN_DIR)/readmbr $(READMBR_OBJ) diff --git a/include/chs.h b/include/chs.h new file mode 100644 index 0000000..bc34ca8 --- /dev/null +++ b/include/chs.h @@ -0,0 +1,8 @@ +#ifndef __CHS_H +#define __CHS_H + +#include + +void decode_chs(const uint8_t* chs, uint16_t* cylinder, uint16_t* head, uint16_t* sector); + +#endif \ No newline at end of file diff --git a/include/mbr.h b/include/mbr.h new file mode 100644 index 0000000..037a05e --- /dev/null +++ b/include/mbr.h @@ -0,0 +1,23 @@ +#ifndef __MBR_H +#define __MBR_H + +#include + +typedef struct { + uint8_t attributes; + uint8_t chs_first[3]; + uint8_t type; + uint8_t chs_last[3]; + 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; + +#endif \ No newline at end of file diff --git a/src/chs.c b/src/chs.c new file mode 100644 index 0000000..c432139 --- /dev/null +++ b/src/chs.c @@ -0,0 +1,8 @@ +#include "chs.h" + +void decode_chs(const uint8_t* chs, uint16_t* cylinder, uint16_t* head, uint16_t* sector) +{ + *head = chs[0]; + *sector = chs[1] & 0x3F; + *cylinder = (((uint16_t)chs[1]) & 0xC0) << 2 | (uint16_t)chs[2]; +} diff --git a/src/readmbr.c b/src/readmbr.c index 468fb39..1733c0a 100644 --- a/src/readmbr.c +++ b/src/readmbr.c @@ -3,30 +3,8 @@ #include #include #include - -typedef struct { - uint8_t attributes; - uint8_t chs_first[3]; - uint8_t type; - uint8_t chs_last[3]; - 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; - -void decode_chs(const uint8_t* chs, uint16_t* cylinder, uint16_t* head, uint16_t* sector) -{ - *head = chs[0]; - *sector = chs[1] & 0x3F; - *cylinder = (((uint16_t)chs[1]) & 0xC0) << 2 | (uint16_t)chs[2]; -} +#include "mbr.h" +#include "chs.h" int main(int argc, char** argv) { @@ -54,16 +32,16 @@ int main(int argc, char** argv) close(fd); printf("== Master Boot Record ==\n"); - printf("Unique ID:\t%u\n", mbr.unique_id); - printf("Reserved:\t%u\n", (unsigned int)mbr.reserved); + printf("Unique ID:\t%x\n", mbr.unique_id); + printf("Reserved:\t%x\n", (unsigned int)mbr.reserved); for (unsigned i = 0; i < 4; i++) { const mbr_part_t* part = &mbr.part_table[i]; uint16_t cylinder, head, sector; printf("Partition:\t%u\n", i); - printf("\tAttributes:\t%u (%s)\n", part->attributes, part->attributes & 0x80 ? "active" : ""); - printf("\tType:%u\n", part->type); + printf("\tAttributes:\t%x (%s)\n", part->attributes, part->attributes & 0x80 ? "active" : ""); + printf("\tType:\t%x\n", part->type); decode_chs(part->chs_first, &cylinder, &head, §or); printf("\tCHS First:\tC %u\tH %u\tS %u\n", cylinder, head, sector); @@ -76,4 +54,4 @@ int main(int argc, char** argv) } return 0; -} \ No newline at end of file +}