Add MBR parsing

This commit is contained in:
streaksu 2020-01-21 06:42:25 +01:00
parent 59ea2f0b59
commit 52125cd07f
3 changed files with 44 additions and 2 deletions

View File

@ -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

29
lib/mbr.c Normal file
View File

@ -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;
}

13
lib/mbr.h Normal file
View File

@ -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