feat: setup structure for block devices
added ide block device added mbr block device driver started fat block device driver reordered boot start timer before pci/ide/etc. enable interrupts earlier fixed exception from ide controller cleanup code more printfs
This commit is contained in:
92
kernel/fs/blockdev.c
Normal file
92
kernel/fs/blockdev.c
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by rick on 06-02-21.
|
||||
//
|
||||
|
||||
#include <libc/libc.h>
|
||||
#include <libk.h>
|
||||
#include <mem/mem.h>
|
||||
#include <libc/kprintf.h>
|
||||
#include "blockdev.h"
|
||||
|
||||
#define MAX_BLOCK_DEVS 64
|
||||
#define MAX_BLOCK_DRIVERS 64
|
||||
|
||||
int last_block_dev = 0;
|
||||
block_device block_devices[MAX_BLOCK_DEVS];
|
||||
int last_block_driver = 0;
|
||||
block_dev_driver block_dev_drivers[MAX_BLOCK_DRIVERS];
|
||||
|
||||
u8 block_dev_register_driver(block_dev_driver *driver) {
|
||||
if (last_block_dev >= MAX_BLOCK_DEVS - 1) {
|
||||
return BLOCK_DEV_REGISTER_DRIVER_FULL;
|
||||
}
|
||||
|
||||
memcpy((u8 *) &block_dev_drivers[last_block_driver++], (const u8 *) driver, sizeof(block_dev_drivers));
|
||||
return BLOCK_DEV_REGISTER_DRIVER_OK;
|
||||
}
|
||||
|
||||
u8 block_dev_register(block_device *device) {
|
||||
if (last_block_dev >= MAX_BLOCK_DEVS - 1) {
|
||||
return BLOCK_DEV_REGISTER_FULL;
|
||||
}
|
||||
|
||||
memcpy((u8 *) &block_devices[last_block_dev++], (const u8 *) device, sizeof(block_device));
|
||||
return BLOCK_DEV_REGISTER_OK;
|
||||
}
|
||||
|
||||
void block_dev_free(block_device *device) {
|
||||
//todo
|
||||
k_panics("block dev free not supported");
|
||||
}
|
||||
|
||||
int block_dev_num_not_scanned() {
|
||||
int not_scanned = 0;
|
||||
for (int i = 0; i < last_block_dev; ++i) {
|
||||
if (block_devices[i].flags.present && !block_devices[i].flags.scanned) not_scanned++;
|
||||
}
|
||||
return not_scanned;
|
||||
}
|
||||
|
||||
void block_dev_scan() {
|
||||
int c_last_block_dev = last_block_dev;
|
||||
for (int i = 0; i < c_last_block_dev; ++i) {
|
||||
if (block_devices[i].flags.scanned || !block_devices[i].flags.present) continue;
|
||||
u8 *lba0 = malloc(block_devices[i].block_size*4);
|
||||
u8 read_result = block_devices[i].access(&block_devices[i], BLOCK_DEV_DIRECTION_READ, 0, 1, lba0);
|
||||
if (read_result != BLOCK_DEV_ACCESS_OK) {
|
||||
block_devices[i].flags.unreadable = 1;
|
||||
goto _block_dev_scan_free;
|
||||
}
|
||||
for (int j = 0; j < last_block_driver; ++j) {
|
||||
// validate if driver is appropriate at all
|
||||
// don't use a root device driver (i.e. mbr) for a non-root device
|
||||
if (block_dev_drivers[j].flags.root_only && !block_devices[i].flags.root_device) continue;
|
||||
|
||||
// let the driver test the disk
|
||||
u8 driver_result = block_dev_drivers[j].check_device(&block_devices[i], lba0);
|
||||
if (driver_result == BLOCK_DEV_DRIVER_CHECK_OK) {
|
||||
block_devices[i].driver = &block_dev_drivers[j];
|
||||
block_devices[i].flags.driver_installed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
_block_dev_scan_free:
|
||||
free(lba0);
|
||||
block_devices[i].flags.scanned = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void block_dev_scan_repeat() {
|
||||
while (block_dev_num_not_scanned() > 0) {
|
||||
block_dev_scan();
|
||||
}
|
||||
}
|
||||
|
||||
void block_dev_print_info() {
|
||||
printf("Block devices:\n");
|
||||
for (int i = 0; i < last_block_dev; ++i) {
|
||||
printf("-%s driver: %s\n",
|
||||
block_devices[i].identifier,
|
||||
block_devices[i].flags.driver_installed ? block_devices[i].driver->name : "n/a");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user