Partition mappings

This commit is contained in:
Kevin Lange 2014-03-16 18:39:03 -07:00
parent ddfcd235e9
commit 0a08fa257e
6 changed files with 134 additions and 58 deletions

View File

@ -52,9 +52,11 @@ DD = dd conv=notrunc
# There are a few modules that are kinda required for a working system
# such as all of the dependencies needed to mount the root partition.
# We can also include things like the debug shell...
BOOT_MODULES := zero random
BOOT_MODULES := zero random serial
BOOT_MODULES += procfs tmpfs ata
BOOT_MODULES += serial ext2 debug_shell
#BOOT_MODULES += dospart
BOOT_MODULES += ext2
BOOT_MODULES += debug_shell
BOOT_MODULES += ps2mouse ps2kbd
# This is kinda silly. We're going to form an -initrd argument..

View File

@ -1,44 +0,0 @@
#include <system.h>
#include <logging.h>
#include <ata.h>
#define SECTORSIZE 512
#define DISK_PORT 0x1F0
mbr_t mbr;
int read_partition_map(fs_node_t * device) {
read_fs(device, 0, SECTORSIZE, (uint8_t *)&mbr);
if (mbr.signature[0] == 0x55 && mbr.signature[1] == 0xAA) {
debug_print(INFO, "Partition table found.");
for (int i = 0; i < 4; ++i) {
if (mbr.partitions[i].status & 0x80) {
debug_print(NOTICE, "Partition #%d: @%d+%d", i+1, mbr.partitions[i].lba_first_sector, mbr.partitions[i].sector_count);
} else {
debug_print(NOTICE, "Partition #%d: inactive", i+1);
}
}
return 0;
} else {
debug_print(ERROR, "Did not find partition table.");
debug_print(ERROR, "Signature was 0x%x 0x%x instead of 0x55 0xAA", mbr.signature[0], mbr.signature[1]);
debug_print(ERROR, "Parsing anyone yields:");
for (int i = 0; i < 4; ++i) {
if (mbr.partitions[i].status & 0x80) {
debug_print(NOTICE, "Partition #%d: @%d+%d", i+1, mbr.partitions[i].lba_first_sector, mbr.partitions[i].sector_count);
} else {
debug_print(NOTICE, "Partition #%d: inactive", i+1);
}
}
}
return 1;
}

View File

@ -138,7 +138,6 @@ typedef struct {
partition_t partitions[4];
uint8_t signature[2];
} __attribute__((packed)) mbr_t;
mbr_t mbr;
#endif

View File

@ -140,9 +140,9 @@ int kmain(struct multiboot *mboot, uint32_t mboot_mag, uintptr_t esp) {
uint32_t module_start = mod->mod_start;
uint32_t module_end = mod->mod_end;
size_t module_size = module_end - module_end;
debug_print(NOTICE, "Loading a module: 0x%x:0x%x", module_start);
module_defs * mod_info = (module_defs *)module_load_direct((void *)(module_start), module_size);
debug_print(NOTICE, "Loaded: %s", mod_info->name);
debug_print(NOTICE, "Loading a module: 0x%x:0x%x", module_start, module_end);
module_data_t * mod_info = (module_data_t *)module_load_direct((void *)(module_start), module_size);
debug_print(NOTICE, "Loaded: %s", mod_info->mod_info->name);
}
late_stage_args();

View File

@ -4,10 +4,6 @@
#include <tokenize.h>
#include <fs.h>
/* XXX: This should be moved */
void ext2_disk_mount(fs_node_t *);
int read_partition_map(fs_node_t *);
void early_stage_args(void) {
char * c;
@ -54,10 +50,6 @@ void early_stage_args(void) {
}
}
if (args_present("read-mbr")) {
fs_node_t * f = kopen(args_value("root"), 0);
read_partition_map(f);
}
}
void late_stage_args(void) {

127
modules/dospart.c Normal file
View File

@ -0,0 +1,127 @@
#include <system.h>
#include <logging.h>
#include <module.h>
#include <ata.h>
#define SECTORSIZE 512
#define DISK_PORT 0x1F0
static mbr_t mbr;
struct dos_partition_entry {
fs_node_t * device;
partition_t partition;
};
static uint32_t read_part(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
struct dos_partition_entry * device = (struct dos_partition_entry *)node->device;
if (offset > device->partition.sector_count * SECTORSIZE) {
debug_print(WARNING, "Read beyond partition!");
return 0;
}
if (offset + size > device->partition.sector_count * SECTORSIZE) {
size = device->partition.sector_count * SECTORSIZE - offset;
debug_print(WARNING, "Tried to read past end of partition, clamped to %d", size);
}
return read_fs(device->device, offset + device->partition.lba_first_sector * SECTORSIZE, size, buffer);
}
static uint32_t write_part(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
struct dos_partition_entry * device = (struct dos_partition_entry *)node->device;
if (offset > device->partition.sector_count * SECTORSIZE) {
return 0;
}
if (offset + size > device->partition.sector_count * SECTORSIZE) {
size = device->partition.sector_count * SECTORSIZE - offset;
}
return write_fs(device->device, offset + device->partition.lba_first_sector * SECTORSIZE, size, buffer);
}
static void open_part(fs_node_t * node, unsigned int flags) {
return;
}
static void close_part(fs_node_t * node) {
return;
}
static fs_node_t * dospart_device_create(int i, fs_node_t * dev, partition_t * part) {
struct dos_partition_entry * device = malloc(sizeof(struct dos_partition_entry));
memcpy(&device->partition, part, sizeof(partition_t));
device->device = dev;
fs_node_t * fnode = malloc(sizeof(fs_node_t));
memset(fnode, 0x00, sizeof(fs_node_t));
fnode->inode = 0;
sprintf(fnode->name, "dospart%d", i);
fnode->device = device;
fnode->uid = 0;
fnode->gid = 0;
fnode->length = device->partition.sector_count * SECTORSIZE; /* TODO */
fnode->flags = FS_BLOCKDEVICE;
fnode->read = read_part;
fnode->write = write_part;
fnode->open = open_part;
fnode->close = close_part;
fnode->readdir = NULL;
fnode->finddir = NULL;
fnode->ioctl = NULL; /* TODO, identify, etc? */
return fnode;
}
static int read_partition_map(char * name) {
fs_node_t * device = kopen(name, 0);
if (!device) return 1;
read_fs(device, 0, SECTORSIZE, (uint8_t *)&mbr);
if (mbr.signature[0] == 0x55 && mbr.signature[1] == 0xAA) {
debug_print(INFO, "Partition table found.");
for (int i = 0; i < 4; ++i) {
if (mbr.partitions[i].status & 0x80) {
debug_print(NOTICE, "Partition #%d: @%d+%d", i+1, mbr.partitions[i].lba_first_sector, mbr.partitions[i].sector_count);
fs_node_t * node = dospart_device_create(i, device, &mbr.partitions[i]);
char tmp[64];
sprintf(tmp, "%s%d", name, i);
vfs_mount(tmp, node);
} else {
debug_print(NOTICE, "Partition #%d: inactive", i+1);
}
}
} else {
debug_print(NOTICE, "No partition table on %s", name);
}
return 0;
}
static int dospart_initialize(void) {
kprint_to_file = kopen("/dev/ttyS0",0);
debug_level = 0;
debug_print(WARNING, "Hello world!");
for (char l = 'a'; l < 'z'; ++l) {
char name[64];
sprintf(name, "/dev/hd%c", l);
if (read_partition_map(name)) {
break;
}
}
return 0;
}
static int dospart_finalize(void) {
return 0;
}
MODULE_DEF(dospart, dospart_initialize, dospart_finalize);