118 lines
3.6 KiB
C
118 lines
3.6 KiB
C
//
|
|
// Created by rick on 06-02-21.
|
|
//
|
|
|
|
#include <libc/libc.h>
|
|
#include <libk/libk.h>
|
|
#include <libc/kprintf.h>
|
|
#include <mem/malloc.h>
|
|
#include <tasks/task.h>
|
|
#include <tasks/locking.h>
|
|
#include <attributes.h>
|
|
#include <libc/sort.h>
|
|
#include "blockdev.h"
|
|
|
|
#define MAX_BLOCK_DEVS 64
|
|
|
|
int last_block_dev = 0;
|
|
block_device block_devices[MAX_BLOCK_DEVS] = {0};
|
|
|
|
extern struct block_dev_driver __start_block_dev_driver[];
|
|
extern struct block_dev_driver __stop_block_dev_driver[];
|
|
#define NUM_DRIVERS ((size_t)(__stop_block_dev_driver - __start_block_dev_driver))
|
|
#define DRIVER(i) ((__start_block_dev_driver) + (i))
|
|
|
|
semaphore_t *block_semaphore;
|
|
bool blockdev_task_running = false;
|
|
|
|
uint8_t block_dev_register(block_device *device) {
|
|
if (last_block_dev >= MAX_BLOCK_DEVS - 1) {
|
|
return BLOCK_DEV_REGISTER_FULL;
|
|
}
|
|
|
|
memcpy((uint8_t *) &block_devices[last_block_dev++], (const uint8_t *) device, sizeof(block_device));
|
|
|
|
if (blockdev_task_running) {
|
|
semaphore_signal(block_semaphore);
|
|
}
|
|
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;
|
|
uint8_t *lba0 = malloc(block_devices[i].block_size);
|
|
uint8_t 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 (size_t j = 0; j < NUM_DRIVERS; ++j) {
|
|
// validate if driver is appropriate at all
|
|
// don't use a root device driver (i.e. mbr) for a non-root device
|
|
if (DRIVER(j)->flags.root_only && !block_devices[i].flags.root_device) continue;
|
|
|
|
// let the driver test the disk
|
|
uint8_t driver_result = DRIVER(j)->check_device(&block_devices[i], lba0);
|
|
if (driver_result == BLOCK_DEV_DRIVER_CHECK_OK) {
|
|
block_devices[i].driver = DRIVER(j);
|
|
block_devices[i].flags.driver_installed = 1;
|
|
break;
|
|
}
|
|
}
|
|
_block_dev_scan_free:
|
|
free(lba0);
|
|
block_devices[i].flags.scanned = 1;
|
|
}
|
|
}
|
|
|
|
int block_dev_driver_comp(const void *a, const void *b) {
|
|
int rank_a = ((struct block_dev_driver *) a)->rank;
|
|
int rank_b = ((struct block_dev_driver *) b)->rank;
|
|
if (rank_a > rank_b) {
|
|
return 1;
|
|
} else if (rank_a < rank_b) {
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void block_dev_pre_init() {
|
|
qsort(DRIVER(0), NUM_DRIVERS, sizeof(struct block_dev_driver), block_dev_driver_comp);
|
|
}
|
|
|
|
void noreturn block_dev_task(void *data) {
|
|
while (true) {
|
|
semaphore_wait(block_semaphore);
|
|
block_dev_scan();
|
|
}
|
|
}
|
|
|
|
void block_dev_start_task() {
|
|
block_semaphore = semaphore_create(1);
|
|
blockdev_task_running = true;
|
|
task_spawn(block_dev_task, NULL);
|
|
}
|
|
|
|
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");
|
|
}
|
|
} |