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:
2021-02-09 22:47:35 +01:00
parent 9440fae968
commit 2ac0da6574
16 changed files with 467 additions and 106 deletions

92
kernel/fs/blockdev.c Normal file
View 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");
}
}

71
kernel/fs/blockdev.h Normal file
View File

@@ -0,0 +1,71 @@
//
// Created by rick on 06-02-21.
//
#ifndef NEW_KERNEL_BLOCKDEV_H
#define NEW_KERNEL_BLOCKDEV_H
#include <types.h>
#define BLOCK_DEV_ACCESS_OK 0
#define BLOCK_DEV_ACCESS_ERR 1
#define BLOCK_DEV_DIRECTION_READ 0
#define BLOCK_DEV_DIRECTION_WRITE 1
#define BLOCK_DEV_DRIVER_CHECK_OK 0
#define BLOCK_DEV_DRIVER_CHECK_NO_MATCH 1
#define BLOCK_DEV_REGISTER_DRIVER_OK 0
#define BLOCK_DEV_REGISTER_DRIVER_FULL 1
#define BLOCK_DEV_REGISTER_OK 0
#define BLOCK_DEV_REGISTER_FULL 1
typedef struct block_device block_device;
typedef u8 (*block_device_driver_check_device)(const block_device *device, u8 *first_sector);
typedef u8 (*block_device_driver_free)(const block_device *device);
typedef u8 (*block_device_access)(const block_device *device, u8 direction, u32 lba, u8 sectors, void *target);
typedef struct {
char name[16];
struct {
u8 root_only: 1;
} flags;
block_device_driver_check_device check_device;
block_device_driver_free free_device;
} block_dev_driver;
typedef struct block_device {
struct {
u8 present: 1;
u8 scanned: 1;
u8 unreadable: 1;
u8 root_device: 1;
u8 driver_installed: 1;
} flags;
char identifier[16];
u32 num_lba;
u16 block_size;
block_device_access access;
block_dev_driver *driver;
void *device_info; // pointer to driver defined structure
// todo device info
} block_device;
u8 block_dev_register_driver(block_dev_driver *driver);
u8 block_dev_register(block_device *device);
void block_dev_free(block_device *device);
void block_dev_scan_repeat();
void block_dev_print_info();
#endif //NEW_KERNEL_BLOCKDEV_H

53
kernel/fs/fat.c Normal file
View File

@@ -0,0 +1,53 @@
//
// Created by rick on 07-02-21.
//
#include "fat.h"
#include <types.h>
typedef struct {
struct {
u8 jump[3];
char oem_identifier[8];
u16 bytes_per_sector;
u8 sectors_per_cluster;
u16 reserved_sectors;
u8 fats;
u16 directories;
u16 total_sectors;
u8 media_descriptor;
u16 sectors_per_fat;
u16 sectors_per_track;
u16 heads_sides;
u32 hidden_sectors;
u32 large_sector_count;
} bpb;
union {
struct {
u8 drive_number;
u8 flags;
u8 signature;
u32 serial;
u8 label[11];
u8 system_id[8];
} ebr_12_16;
struct {
u32 sectors_per_fat;
u16 flags;
u16 fat_version;
u32 root_directory;
u16 fs_info;
u16 backup_boot;
u8 reserved[12];
u8 drive_number;
u8 flags_nt;
u8 signature;
u32 serial;
u8 label[11];
u8 system_id[8];
} ebr_32;
};
} fat_bpb;
// steal validation code from here https://github.com/torvalds/linux/blob/fcadab740480e0e0e9fa9bd272acd409884d431a/fs/fat/inode.c#L1456

8
kernel/fs/fat.h Normal file
View File

@@ -0,0 +1,8 @@
//
// Created by rick on 07-02-21.
//
#ifndef NEW_KERNEL_FAT_H
#define NEW_KERNEL_FAT_H
#endif //NEW_KERNEL_FAT_H

109
kernel/fs/mbr.c Normal file
View File

@@ -0,0 +1,109 @@
//
// Created by rick on 06-02-21.
//
#include "mbr.h"
#include <types.h>
#include <drivers/ide.h>
#include <mem/mem.h>
#include <libc/kprintf.h>
#include <fs/blockdev.h>
typedef struct {
u8 bootable;
u8 starting_head;
u8 starting_sector: 6;
u16 starting_cylinder: 10;
u8 system_id;
u8 ending_head;
u8 ending_sector: 6;
u16 ending_cylinder: 10;
u32 start_lba;
u32 num_lbas;
} __attribute((packed)) mbr_partition_table_entry;
typedef struct {
u32 unique_id;
u16 reserved;
mbr_partition_table_entry entries[4];
u8 signature[2];
} __attribute((packed)) mbr_table;
typedef struct {
const block_device *device;
u32 start_lba;
} mbr_block_driver_info;
u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 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 u32 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);
}
u8 mbr_check_device(const block_device *device, u8* first_sector) {
mbr_table *table = (mbr_table *) &first_sector[512 - 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;
}
}
for (int i = 0; i < 4; ++i) {
if (table->entries[i].system_id == 0) continue;
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 = {
.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);
}
return BLOCK_DEV_DRIVER_CHECK_OK;
}
block_dev_driver mbr_driver = {
.name = "mbr",
.flags.root_only = 1,
.check_device = mbr_check_device,
.free_device = NULL, // todo
};
void mbr_register_block_driver() {
block_dev_register_driver(&mbr_driver);
}
void mbr_read_from_ide(u8 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);
}

14
kernel/fs/mbr.h Normal file
View File

@@ -0,0 +1,14 @@
//
// Created by rick on 06-02-21.
//
#ifndef NEW_KERNEL_MBR_H
#define NEW_KERNEL_MBR_H
#include <types.h>
void mbr_register_block_driver();
void mbr_read_from_ide(u8 ide_drive);
#endif //NEW_KERNEL_MBR_H