feat: moved file system code around
This commit is contained in:
145
kernel/vfs/blockdev.c
Normal file
145
kernel/vfs/blockdev.c
Normal file
@@ -0,0 +1,145 @@
|
||||
//
|
||||
// Created by rick on 06-02-21.
|
||||
//
|
||||
|
||||
#include <attributes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <myke/vfs/blockdev.h>
|
||||
#include <myke/libk/libk.h>
|
||||
#include <myke/tasks/locking.h>
|
||||
#include <myke/tasks/task.h>
|
||||
|
||||
#define MAX_BLOCK_DEVS 64
|
||||
|
||||
int last_block_dev = 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[];
|
||||
#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_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_t));
|
||||
|
||||
if (blockdev_task_running) {
|
||||
semaphore_signal(block_semaphore);
|
||||
}
|
||||
return BLOCK_DEV_REGISTER_OK;
|
||||
}
|
||||
|
||||
void block_dev_free(block_device_t *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;
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void att_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, "blockdev");
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
201
kernel/vfs/lfs/fat.c
Normal file
201
kernel/vfs/lfs/fat.c
Normal file
@@ -0,0 +1,201 @@
|
||||
//
|
||||
// Created by rick on 07-02-21.
|
||||
//
|
||||
|
||||
#include <attributes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <myke/vfs/blockdev.h>
|
||||
|
||||
#define FAT_TYPE_12 1
|
||||
#define FAT_TYPE_16 2
|
||||
#define FAT_TYPE_32 3
|
||||
#define FAT_TYPE_EXFAT 4
|
||||
|
||||
typedef struct {
|
||||
struct {
|
||||
uint8_t jump[3];
|
||||
char oem_identifier[8];
|
||||
uint16_t bytes_per_sector;
|
||||
uint8_t sectors_per_cluster;
|
||||
uint16_t reserved_sectors;
|
||||
uint8_t fats;
|
||||
uint16_t directories;
|
||||
uint16_t total_sectors;
|
||||
uint8_t media_descriptor;
|
||||
uint16_t sectors_per_fat;
|
||||
uint16_t sectors_per_track;
|
||||
uint16_t heads_sides;
|
||||
uint32_t hidden_sectors;
|
||||
uint32_t large_total_sectors;
|
||||
} att_packed bpb;
|
||||
union {
|
||||
struct {
|
||||
uint8_t drive_number;
|
||||
uint8_t flags;
|
||||
uint8_t signature;
|
||||
uint32_t serial;
|
||||
uint8_t label[11];
|
||||
uint8_t system_id[8];
|
||||
} att_packed ebr_12_16;
|
||||
struct {
|
||||
uint32_t sectors_per_fat;
|
||||
uint16_t flags;
|
||||
uint16_t fat_version;
|
||||
uint32_t root_directory;
|
||||
uint16_t fs_info;
|
||||
uint16_t backup_boot;
|
||||
uint8_t reserved[12];
|
||||
uint8_t drive_number;
|
||||
uint8_t flags_nt;
|
||||
uint8_t signature;
|
||||
uint32_t serial;
|
||||
uint8_t label[11];
|
||||
uint8_t system_id[8];
|
||||
} att_packed ebr_32;
|
||||
};
|
||||
} att_packed fat_bpb;
|
||||
|
||||
typedef struct {
|
||||
uint8_t hours: 5;
|
||||
uint8_t minutes: 6;
|
||||
uint8_t seconds: 5;
|
||||
} att_packed time_83;
|
||||
|
||||
typedef struct {
|
||||
uint8_t hours: 5;
|
||||
uint8_t minutes: 6;
|
||||
uint8_t seconds: 5;
|
||||
} att_packed date_83;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
uint8_t name[11];
|
||||
uint8_t attributes;
|
||||
uint8_t reserved;
|
||||
uint8_t created_seconds;
|
||||
time_83 created_time;
|
||||
date_83 created_date;
|
||||
date_83 access_date;
|
||||
uint16_t high_first_cluster;
|
||||
time_83 last_mod_time;
|
||||
date_83 last_mod_date;
|
||||
uint16_t low_first_cluster;
|
||||
uint32_t file_size;
|
||||
} att_packed name_83;
|
||||
struct {
|
||||
uint8_t order;
|
||||
uint16_t text_1[5];
|
||||
uint8_t attribute;
|
||||
uint8_t long_entry_type;
|
||||
uint8_t checksum;
|
||||
uint16_t text_2[6];
|
||||
uint16_t reserved;
|
||||
uint16_t text_3[2];
|
||||
} att_packed long_name;
|
||||
};
|
||||
} att_packed fat_directory_entry;
|
||||
|
||||
void print_chars(char *chars, int amount) {
|
||||
for (int i = 0; i < amount; ++i) {
|
||||
if (chars[i] == 0) break;
|
||||
printf("%c", chars[i]);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t att_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) {
|
||||
printf("Definitely not FAT\n");
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
|
||||
printf("OEM: ");
|
||||
print_chars(bpb.bpb.oem_identifier, 11);
|
||||
printf("\n");
|
||||
|
||||
uint32_t total_sectors = bpb.bpb.total_sectors == 0 ? bpb.bpb.large_total_sectors : bpb.bpb.total_sectors;
|
||||
uint32_t fat_size = bpb.bpb.sectors_per_fat == 0 ? bpb.ebr_32.sectors_per_fat : bpb.bpb.sectors_per_fat;
|
||||
uint32_t root_dir_sectors =
|
||||
((bpb.bpb.directories * 32) + (bpb.bpb.bytes_per_sector - 1)) / bpb.bpb.bytes_per_sector;
|
||||
uint32_t first_data_sector = bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors;
|
||||
uint32_t first_fat_sector = bpb.bpb.reserved_sectors;
|
||||
uint32_t data_sectors = total_sectors - (bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors);
|
||||
uint32_t total_clusters = data_sectors / bpb.bpb.sectors_per_cluster;
|
||||
|
||||
uint8_t fat_type;
|
||||
if (total_clusters < 4085) {
|
||||
fat_type = FAT_TYPE_12;
|
||||
} else if (total_clusters < 65525) {
|
||||
fat_type = FAT_TYPE_16;
|
||||
} else if (total_clusters < 268435445) {
|
||||
fat_type = FAT_TYPE_32;
|
||||
} else {
|
||||
fat_type = FAT_TYPE_EXFAT;
|
||||
}
|
||||
|
||||
// dump dir
|
||||
// fat12/16 only
|
||||
uint32_t first_root_dir_sector = first_data_sector - root_dir_sectors;
|
||||
// todo fat32
|
||||
fat_directory_entry *entries = malloc(root_dir_sectors * bpb.bpb.bytes_per_sector);
|
||||
device->access(device, BLOCK_DEV_DIRECTION_READ, first_root_dir_sector, root_dir_sectors, entries);
|
||||
// todo read table
|
||||
free(entries);
|
||||
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
uint32_t sector;
|
||||
uint32_t value;
|
||||
} table_result;
|
||||
|
||||
table_result
|
||||
get_fat_table_value(uint8_t fat_type, const uint8_t *fat_table, uint32_t active_cluster, uint32_t first_fat_cluster,
|
||||
uint32_t sector_size) {
|
||||
uint32_t fat_offset;
|
||||
table_result result = {
|
||||
.value = 0,
|
||||
.sector = 0,
|
||||
};
|
||||
switch (fat_type) {
|
||||
case FAT_TYPE_12:
|
||||
fat_offset = active_cluster * (active_cluster / 2);
|
||||
result.sector = first_fat_cluster + (fat_offset / sector_size);
|
||||
result.value = *(uint16_t *) &fat_table[fat_offset % sector_size];
|
||||
if (active_cluster & 0x1) {
|
||||
result.value = result.value >> 4;
|
||||
} else {
|
||||
result.value = result.value & 0x0FFF;
|
||||
}
|
||||
return result;
|
||||
case FAT_TYPE_16:
|
||||
fat_offset = active_cluster * 2;
|
||||
result.sector = first_fat_cluster + (fat_offset / sector_size);
|
||||
result.value = *(uint16_t *) &fat_table[fat_offset % sector_size];
|
||||
return result;
|
||||
case FAT_TYPE_32:
|
||||
case FAT_TYPE_EXFAT:
|
||||
fat_offset = active_cluster * 4;
|
||||
result.sector = first_fat_cluster + (fat_offset / sector_size);
|
||||
result.value = *(uint32_t *) &fat_table[fat_offset % sector_size] & 0x0FFFFFFF;
|
||||
return result;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
BLOCK_DEV_DRIVER(900) = {
|
||||
.name = "fat",
|
||||
.check_device = fat_check_device,
|
||||
.free_device = NULL, // todo
|
||||
};
|
||||
|
||||
// steal validation code from here https://github.com/torvalds/linux/blob/fcadab740480e0e0e9fa9bd272acd409884d431a/fs/fat/inode.c#L1456
|
||||
97
kernel/vfs/lfs/ustar.c
Normal file
97
kernel/vfs/lfs/ustar.c
Normal file
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// Created by rick on 11-03-21.
|
||||
//
|
||||
#include <attributes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <myke/vfs/blockdev.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];
|
||||
} att_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,
|
||||
};
|
||||
115
kernel/vfs/part/mbr.c
Normal file
115
kernel/vfs/part/mbr.c
Normal file
@@ -0,0 +1,115 @@
|
||||
//
|
||||
// Created by rick on 06-02-21.
|
||||
//
|
||||
|
||||
#include <attributes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <myke/drivers/pci/ide.h>
|
||||
#include <myke/vfs/blockdev.h>
|
||||
#include <myke/vfs/part/mbr.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t bootable;
|
||||
uint8_t starting_head;
|
||||
uint8_t starting_sector: 6;
|
||||
uint16_t starting_cylinder: 10;
|
||||
uint8_t system_id;
|
||||
uint8_t ending_head;
|
||||
uint8_t ending_sector: 6;
|
||||
uint16_t ending_cylinder: 10;
|
||||
uint32_t start_lba;
|
||||
uint32_t num_lbas;
|
||||
} att_packed mbr_partition_table_entry;
|
||||
|
||||
typedef struct {
|
||||
uint32_t unique_id;
|
||||
uint16_t reserved;
|
||||
mbr_partition_table_entry entries[4];
|
||||
uint8_t signature[2];
|
||||
} att_packed mbr_table;
|
||||
|
||||
typedef struct {
|
||||
const block_device_t *device;
|
||||
uint32_t start_lba;
|
||||
} mbr_block_driver_info;
|
||||
|
||||
uint8_t
|
||||
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;
|
||||
}
|
||||
const mbr_block_driver_info *info = device->device_info;
|
||||
const uint32_t actual_lba = info->start_lba + lba;
|
||||
if (actual_lba > info->device->num_lba) {
|
||||
return BLOCK_DEV_ACCESS_ERR;
|
||||
}
|
||||
|
||||
// delegate to backing driver
|
||||
return info->device->access(info->device, direction, actual_lba, sectors, target);
|
||||
}
|
||||
|
||||
uint8_t att_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
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
|
||||
if (table.reserved != 0x0000 && table.reserved != 0x5A5A) {
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (table.entries[i].num_lbas > device->num_lba || table.entries[i].start_lba > device->num_lba) {
|
||||
return BLOCK_DEV_DRIVER_CHECK_NO_MATCH;
|
||||
}
|
||||
}
|
||||
|
||||
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_t logical_device = {
|
||||
.flags.present = 1,
|
||||
|
||||
.num_lba = table.entries[i].num_lbas,
|
||||
.block_size = device->block_size,
|
||||
.access = mbr_block_dev_access,
|
||||
.device_info = info,
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void mbr_read_from_ide(uint8_t ide_drive) {
|
||||
char *mbr_data = malloc(0x200);
|
||||
ide_access(0, ide_drive, 0, 1, mbr_data);
|
||||
mbr_partition_table_entry *entry = (mbr_partition_table_entry *) (mbr_data + 0x1BE);
|
||||
printf("Start at %d, count: %d\n", entry->start_lba, entry->num_lbas);
|
||||
free(mbr_data);
|
||||
}
|
||||
|
||||
BLOCK_DEV_DRIVER(200) = {
|
||||
.name = "mbr",
|
||||
.flags.partitioning = 1,
|
||||
.check_device = mbr_check_device,
|
||||
.free_device = NULL, // todo
|
||||
};
|
||||
Reference in New Issue
Block a user