:: commit 52125cd07f988502c32f55a582d61d31edd91aa9

streaksu <assemblyislaw@gmail.com> — 2020-01-21 05:42

parents: 59ea2f0b59

Add MBR parsing

diff --git a/drivers/disk.h b/drivers/disk.h
index 4f9191e2..635c2714 100644
--- a/drivers/disk.h
+++ b/drivers/disk.h
@@ -4,7 +4,7 @@
 #include <stddef.h>
 #include <stdint.h>
 
-int read_sector(int, void *, uint64_t, uint64_t);
-int read(int, void *, uint64_t, uint64_t);
+int read_sector(int drive, void *buffer, uint64_t lba, uint64_t count);
+int read(int drive, void *buffer, uint64_t loc, uint64_t count);
 
 #endif
diff --git a/lib/mbr.c b/lib/mbr.c
new file mode 100644
index 00000000..4249ebef
--- /dev/null
+++ b/lib/mbr.c
@@ -0,0 +1,29 @@
+#include <lib/mbr.h>
+#include <drivers/disk.h>
+#include <stddef.h>
+
+struct mbr_entry {
+	uint8_t status;
+	uint8_t chs_first_sect[3];
+	uint8_t type;
+	uint8_t chs_last_sect[3];
+	uint32_t first_sect;
+	uint32_t sect_count;
+} __attribute__((packed));
+
+int mbr_get_part(struct mbr_part *part, int drive, int partition) {
+    // Variables.
+    struct mbr_entry entry;
+    const size_t entry_address = 0x1be + sizeof(struct mbr_entry) * partition;
+    
+    // Read the entry of the MBR.
+    int ret;
+    if ((ret = read(drive, &entry, entry_address, sizeof(struct mbr_entry)))) {
+        return ret;
+    }
+
+    // Assign the final fields and return.
+    part->first_sect = entry.first_sect;
+    part->sect_count = entry.sect_count;
+    return 0;
+}
diff --git a/lib/mbr.h b/lib/mbr.h
new file mode 100644
index 00000000..72ac8b19
--- /dev/null
+++ b/lib/mbr.h
@@ -0,0 +1,13 @@
+#ifndef __MBR_H__
+#define __MBR_H__
+
+#include <stdint.h>
+
+struct mbr_part {
+	uint64_t first_sect;
+	uint64_t sect_count;
+};
+
+int mbr_get_part(struct mbr_part *part, int drive, int partition);
+
+#endif
tab: 248 wrap: offon