feat: implemented errno, strtol. Started ustar. Reformatted headers and
code. Added some self-tests. Started prepwork for vfs.
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
#define MAX_BLOCK_DEVS 64
|
||||
|
||||
int last_block_dev = 0;
|
||||
block_device block_devices[MAX_BLOCK_DEVS] = {0};
|
||||
block_device_t block_devices[MAX_BLOCK_DEVS] = {0};
|
||||
|
||||
extern struct block_dev_driver __start_block_dev_driver[];
|
||||
extern struct block_dev_driver __stop_block_dev_driver[];
|
||||
@@ -25,12 +25,12 @@ extern struct block_dev_driver __stop_block_dev_driver[];
|
||||
semaphore_t *block_semaphore;
|
||||
bool blockdev_task_running = false;
|
||||
|
||||
uint8_t block_dev_register(block_device *device) {
|
||||
uint8_t block_dev_register(block_device_t *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));
|
||||
memcpy((uint8_t *) &block_devices[last_block_dev++], (const uint8_t *) device, sizeof(block_device_t));
|
||||
|
||||
if (blockdev_task_running) {
|
||||
semaphore_signal(block_semaphore);
|
||||
@@ -38,7 +38,7 @@ uint8_t block_dev_register(block_device *device) {
|
||||
return BLOCK_DEV_REGISTER_OK;
|
||||
}
|
||||
|
||||
void block_dev_free(block_device *device) {
|
||||
void block_dev_free(block_device_t *device) {
|
||||
//todo
|
||||
k_panics("block dev free not supported");
|
||||
}
|
||||
@@ -51,6 +51,43 @@ 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) {
|
||||
@@ -62,9 +99,14 @@ void block_dev_scan() {
|
||||
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;
|
||||
// only partitioning drivers are automatically assigned
|
||||
if (!DRIVER(i)->flags.partitioning) {
|
||||
continue;
|
||||
}
|
||||
// only root devices have partitions
|
||||
if (!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);
|
||||
|
||||
@@ -107,7 +107,7 @@ void print_chars(char *chars, int amount) {
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t used fat_check_device(const block_device *device, uint8_t *first_sector) {
|
||||
uint8_t used fat_check_device(const block_device_t *device, uint8_t *first_sector) {
|
||||
fat_bpb bpb;
|
||||
memcpy((uint8_t *) &bpb, first_sector, sizeof(fat_bpb));
|
||||
if (bpb.bpb.sectors_per_fat == 0 || bpb.bpb.sectors_per_cluster == 0) {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <myke/fs/mbr.h>
|
||||
#include <myke/drivers/ide.h>
|
||||
#include <myke/drivers/pci/ide.h>
|
||||
#include <myke/fs/blockdev.h>
|
||||
#include <myke/attributes.h>
|
||||
|
||||
@@ -33,12 +33,12 @@ typedef struct {
|
||||
} packed mbr_table;
|
||||
|
||||
typedef struct {
|
||||
const block_device *device;
|
||||
const block_device_t *device;
|
||||
uint32_t start_lba;
|
||||
} mbr_block_driver_info;
|
||||
|
||||
uint8_t
|
||||
mbr_block_dev_access(const block_device *device, uint8_t direction, uint32_t lba, uint8_t sectors, void *target) {
|
||||
mbr_block_dev_access(const block_device_t *device, uint8_t direction, uint32_t lba, uint8_t sectors, void *target) {
|
||||
if (!device->flags.present || lba > device->num_lba) {
|
||||
return BLOCK_DEV_ACCESS_ERR;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ mbr_block_dev_access(const block_device *device, uint8_t direction, uint32_t lba
|
||||
return info->device->access(info->device, direction, actual_lba, sectors, target);
|
||||
}
|
||||
|
||||
uint8_t used mbr_check_device(const block_device *device, uint8_t *first_sector) {
|
||||
uint8_t used mbr_check_device(const block_device_t *device, uint8_t *first_sector) {
|
||||
mbr_table table;
|
||||
memcpy((uint8_t *) &table, first_sector + (device->block_size - sizeof(mbr_table)), sizeof(mbr_table));
|
||||
if (table.signature[0] != 0x55 && table.signature[1] != 0xAA) { // AA 55 but in little endian
|
||||
@@ -69,14 +69,16 @@ uint8_t used mbr_check_device(const block_device *device, uint8_t *first_sector)
|
||||
}
|
||||
}
|
||||
|
||||
int num_parts = 0;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (table.entries[i].system_id == 0) continue;
|
||||
num_parts += 1;
|
||||
|
||||
mbr_block_driver_info *info = malloc(sizeof(mbr_block_driver_info));
|
||||
info->device = device;
|
||||
info->start_lba = table.entries[i].start_lba;
|
||||
|
||||
block_device logical_device = {
|
||||
block_device_t logical_device = {
|
||||
.flags.present = 1,
|
||||
|
||||
.num_lba = table.entries[i].num_lbas,
|
||||
@@ -88,7 +90,10 @@ uint8_t used mbr_check_device(const block_device *device, uint8_t *first_sector)
|
||||
sprintf(logical_device.identifier, "%sp%d", device->identifier, i);
|
||||
|
||||
block_dev_register(&logical_device);
|
||||
}
|
||||
|
||||
if (num_parts == 0) {
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
|
||||
return BLOCK_DEV_DRIVER_CHECK_OK;
|
||||
@@ -104,7 +109,7 @@ void mbr_read_from_ide(uint8_t ide_drive) {
|
||||
|
||||
BLOCK_DEV_DRIVER(200) = {
|
||||
.name = "mbr",
|
||||
.flags.root_only = 1,
|
||||
.flags.partitioning = 1,
|
||||
.check_device = mbr_check_device,
|
||||
.free_device = NULL, // todo
|
||||
};
|
||||
|
||||
96
kernel/fs/ustar.c
Normal file
96
kernel/fs/ustar.c
Normal file
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// Created by rick on 11-03-21.
|
||||
//
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <myke/fs/blockdev.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
char filename[100];
|
||||
uint64_t mode;
|
||||
uint64_t uid;
|
||||
uint64_t gid;
|
||||
char filesize[12];
|
||||
char lastmod[12];
|
||||
uint64_t checksum;
|
||||
char type;
|
||||
char linked[100];
|
||||
char magic[6];
|
||||
char version[2];
|
||||
char username[32];
|
||||
char groupname[32];
|
||||
uint64_t device_major;
|
||||
uint64_t device_minor;
|
||||
char prefix[155];
|
||||
} packed ustar_sector;
|
||||
|
||||
typedef struct {
|
||||
ustar_sector sector;
|
||||
uint32_t lba;
|
||||
} ustar_inode;
|
||||
|
||||
typedef struct {
|
||||
ustar_inode *first_inode;
|
||||
const block_device_t *device;
|
||||
} ustar_fs;
|
||||
|
||||
const char *ustar_magic = "ustar";
|
||||
const char *ustar_version = "00";
|
||||
|
||||
#define USTAR_TYPE_FILE '0'
|
||||
#define USTAR_TYPE_LINK '1'
|
||||
#define USTAR_TYPE_SYMBOL '2'
|
||||
#define USTAR_TYPE_CHAR '3'
|
||||
#define USTAR_TYPE_BLOCK '4'
|
||||
#define USTAR_TYPE_DIR '5'
|
||||
#define USTAR_TYPE_FIFO '6'
|
||||
|
||||
#define SECTOR_SIZE 512
|
||||
|
||||
long ustar_oct_to_long(char *oct, uint32_t size) {
|
||||
char value[size + 1];
|
||||
memcpy(value, oct, size);
|
||||
value[size] = 0;
|
||||
return strtol(value, NULL, 8);
|
||||
}
|
||||
|
||||
bool ustar_sector_valid(ustar_sector *sector) {
|
||||
return strncmp(ustar_magic, sector->magic, 6) == 0;
|
||||
}
|
||||
|
||||
ustar_sector *ustar_next(ustar_sector *current) {
|
||||
if (ustar_sector_valid(current)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
long filesize = ustar_oct_to_long(current->filesize, 12);
|
||||
|
||||
long offset = (((filesize + SECTOR_SIZE - 1) / SECTOR_SIZE) + 1) * SECTOR_SIZE;
|
||||
|
||||
ustar_sector *next = ((void *) current) + offset;
|
||||
return ustar_sector_valid(next) ? next : NULL;
|
||||
}
|
||||
|
||||
uint8_t ustar_check_device(const block_device_t *device, uint8_t *first_sector) {
|
||||
ustar_sector *sector = (ustar_sector *) first_sector;
|
||||
if (!ustar_sector_valid(sector)) {
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
|
||||
ustar_fs *fs = malloc(sizeof(ustar_fs));
|
||||
fs->first_inode = malloc(sizeof(ustar_inode));
|
||||
memcpy(&fs->first_inode->sector, first_sector, sizeof(ustar_sector));
|
||||
fs->first_inode->lba = 0;
|
||||
fs->device = device;
|
||||
|
||||
return BLOCK_DEV_DRIVER_CHECK_OK;
|
||||
}
|
||||
|
||||
BLOCK_DEV_DRIVER(300) = {
|
||||
.name = "ustar",
|
||||
.check_device = ustar_check_device,
|
||||
.free_device = NULL,
|
||||
};
|
||||
Reference in New Issue
Block a user