feat: Vfs and Ext2 support. Code style/attribute improvements

Added VFS and Ext2 support.
Optimized attributes of methods to improve code highlighting.
Printf attribute, malloc attribute, etc.
This commit is contained in:
2021-10-06 21:45:15 +02:00
parent 073051c99e
commit 03f0ec6f88
24 changed files with 1322 additions and 90 deletions

View File

@@ -4,7 +4,6 @@
#include <attributes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <myke/vfs/blockdev.h>
@@ -26,6 +25,25 @@ extern struct block_dev_driver __stop_block_dev_driver[];
semaphore_t *block_semaphore;
bool blockdev_task_running = false;
block_device_t *block_dev_get_by_id(const char *name) {
for (int i = 0; i < last_block_dev; ++i) {
if (block_devices[i].flags.present
&& strcmp(block_devices[i].identifier, name) == 0) {
return &block_devices[i];
}
}
return NULL;
}
block_dev_driver_t *block_dev_driver_by_name(const char *name) {
for (size_t i = 0; i < NUM_DRIVERS; ++i) {
if (strcmp(DRIVER(i)->name, name) == 0) {
return DRIVER(i);
}
}
return NULL;
}
uint8_t block_dev_register(block_device_t *device) {
if (last_block_dev >= MAX_BLOCK_DEVS - 1) {
return BLOCK_DEV_REGISTER_FULL;
@@ -39,6 +57,25 @@ uint8_t block_dev_register(block_device_t *device) {
return BLOCK_DEV_REGISTER_OK;
}
void *block_dev_mount(const char *device, const char *driver_name) {
if (device == NULL || driver_name == NULL || strlen(device) == 0 || strlen(driver_name) == 0) {
return NULL; // invalid input
}
block_device_t *dev = block_dev_get_by_id(device);
if (dev == NULL) {
return NULL;
}
block_dev_driver_t *driver = block_dev_driver_by_name(driver_name);
if (driver == NULL) {
return NULL;
}
if (driver->check_device(dev) == BLOCK_DEV_DRIVER_CHECK_OK) {
dev->driver = driver;
return dev->driver_info;
}
return NULL;
}
void block_dev_free(block_device_t *device) {
//todo
k_panics("block dev free not supported");
@@ -52,53 +89,10 @@ int block_dev_num_not_scanned() {
return not_scanned;
}
bool block_dev_mount(char *identifier, char *driver_name) {
bool result = true;
block_device_t *device = NULL;
for (int i = 0; i < last_block_dev; ++i) {
if (strncmp(block_devices[i].identifier, identifier, 16) == 0) {
device = &block_devices[i];
}
}
if (identifier == NULL) {
result = false;
goto _end;
}
struct block_dev_driver *driver = NULL;
for (size_t j = 0; j < NUM_DRIVERS; ++j) {
if (strncmp(DRIVER(j)->name, driver_name, 16) == 0) {
driver = DRIVER(j);
}
}
if (driver == NULL) {
result = false;
goto _end;
}
uint8_t *lba0 = malloc(device->block_size);
uint8_t read_result = device->access(device, BLOCK_DEV_DIRECTION_READ, 0, 1, lba0);
if (read_result != BLOCK_DEV_ACCESS_OK) {
result = false;
goto _access_fail;
}
if (driver->check_device(device, lba0) != BLOCK_DEV_DRIVER_CHECK_OK) {
result = false;
}
_access_fail:
free(lba0);
_end:
return result;
}
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) {
// only partitioning drivers are automatically assigned
if (!DRIVER(i)->flags.partitioning) {
@@ -110,15 +104,13 @@ void block_dev_scan() {
}
// let the driver test the disk
uint8_t driver_result = DRIVER(j)->check_device(&block_devices[i], lba0);
uint8_t driver_result = DRIVER(j)->check_device(&block_devices[i]);
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;
}
}