feat: refactor to use gcc types
This commit is contained in:
@@ -16,21 +16,21 @@ 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) {
|
||||
uint8_t 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));
|
||||
memcpy((uint8_t *) &block_dev_drivers[last_block_driver++], (const uint8_t *) driver, sizeof(block_dev_drivers));
|
||||
return BLOCK_DEV_REGISTER_DRIVER_OK;
|
||||
}
|
||||
|
||||
u8 block_dev_register(block_device *device) {
|
||||
uint8_t 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));
|
||||
memcpy((uint8_t *) &block_devices[last_block_dev++], (const uint8_t *) device, sizeof(block_device));
|
||||
return BLOCK_DEV_REGISTER_OK;
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ 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);
|
||||
u8 read_result = block_devices[i].access(&block_devices[i], BLOCK_DEV_DIRECTION_READ, 0, 1, lba0);
|
||||
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;
|
||||
@@ -63,7 +63,7 @@ void block_dev_scan() {
|
||||
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);
|
||||
uint8_t 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;
|
||||
|
||||
@@ -25,17 +25,17 @@
|
||||
|
||||
typedef struct block_device block_device;
|
||||
|
||||
typedef u8 (*block_device_driver_check_device)(const block_device *device, u8 *first_sector);
|
||||
typedef uint8_t (*block_device_driver_check_device)(const block_device *device, uint8_t *first_sector);
|
||||
|
||||
typedef u8 (*block_device_driver_free)(const block_device *device);
|
||||
typedef uint8_t (*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 uint8_t (*block_device_access)(const block_device *device, uint8_t direction, uint32_t lba, uint8_t sectors, void *target);
|
||||
|
||||
|
||||
typedef struct {
|
||||
char name[16];
|
||||
struct {
|
||||
u8 root_only: 1;
|
||||
uint8_t root_only: 1;
|
||||
} flags;
|
||||
block_device_driver_check_device check_device;
|
||||
block_device_driver_free free_device;
|
||||
@@ -43,24 +43,24 @@ typedef struct {
|
||||
|
||||
typedef struct block_device {
|
||||
struct {
|
||||
u8 present: 1;
|
||||
u8 scanned: 1;
|
||||
u8 unreadable: 1;
|
||||
u8 root_device: 1;
|
||||
u8 driver_installed: 1;
|
||||
uint8_t present: 1;
|
||||
uint8_t scanned: 1;
|
||||
uint8_t unreadable: 1;
|
||||
uint8_t root_device: 1;
|
||||
uint8_t driver_installed: 1;
|
||||
} flags;
|
||||
char identifier[16];
|
||||
u32 num_lba;
|
||||
u16 block_size;
|
||||
uint32_t num_lba;
|
||||
uint16_t 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);
|
||||
uint8_t block_dev_register_driver(block_dev_driver *driver);
|
||||
|
||||
u8 block_dev_register(block_device *device);
|
||||
uint8_t block_dev_register(block_device *device);
|
||||
|
||||
void block_dev_free(block_device *device);
|
||||
|
||||
|
||||
144
kernel/fs/fat.c
144
kernel/fs/fat.c
@@ -17,90 +17,90 @@
|
||||
|
||||
typedef struct {
|
||||
struct {
|
||||
u8 jump[3];
|
||||
uint8_t 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_total_sectors;
|
||||
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;
|
||||
} __attribute((packed)) bpb;
|
||||
union {
|
||||
struct {
|
||||
u8 drive_number;
|
||||
u8 flags;
|
||||
u8 signature;
|
||||
u32 serial;
|
||||
u8 label[11];
|
||||
u8 system_id[8];
|
||||
uint8_t drive_number;
|
||||
uint8_t flags;
|
||||
uint8_t signature;
|
||||
uint32_t serial;
|
||||
uint8_t label[11];
|
||||
uint8_t system_id[8];
|
||||
} __attribute((packed)) 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];
|
||||
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];
|
||||
} __attribute((packed)) ebr_32;
|
||||
};
|
||||
} __attribute((packed)) fat_bpb;
|
||||
|
||||
typedef struct {
|
||||
u8 hours: 5;
|
||||
u8 minutes: 6;
|
||||
u8 seconds: 5;
|
||||
uint8_t hours: 5;
|
||||
uint8_t minutes: 6;
|
||||
uint8_t seconds: 5;
|
||||
} __attribute((packed)) time_83;
|
||||
|
||||
typedef struct {
|
||||
u8 hours: 5;
|
||||
u8 minutes: 6;
|
||||
u8 seconds: 5;
|
||||
uint8_t hours: 5;
|
||||
uint8_t minutes: 6;
|
||||
uint8_t seconds: 5;
|
||||
} __attribute((packed)) date_83;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
u8 name[11];
|
||||
u8 attributes;
|
||||
u8 reserved;
|
||||
u8 created_seconds;
|
||||
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;
|
||||
u16 high_first_cluster;
|
||||
uint16_t high_first_cluster;
|
||||
time_83 last_mod_time;
|
||||
date_83 last_mod_date;
|
||||
u16 low_first_cluster;
|
||||
u32 file_size;
|
||||
uint16_t low_first_cluster;
|
||||
uint32_t file_size;
|
||||
} __attribute((packed)) name_83;
|
||||
struct {
|
||||
u8 order;
|
||||
u16 text_1[5];
|
||||
u8 attribute;
|
||||
u8 long_entry_type;
|
||||
u8 checksum;
|
||||
u16 text_2[6];
|
||||
u16 reserved;
|
||||
u16 text_3[2];
|
||||
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];
|
||||
} __attribute((packed)) long_name;
|
||||
};
|
||||
} __attribute((packed)) fat_directory_entry;
|
||||
|
||||
u8 fat_check_device(const block_device *device, u8 *first_sector);
|
||||
uint8_t fat_check_device(const block_device *device, uint8_t *first_sector);
|
||||
|
||||
block_dev_driver fat_driver = {
|
||||
.name = "fat",
|
||||
@@ -119,22 +119,22 @@ void print_chars(char *chars, int amount) {
|
||||
}
|
||||
}
|
||||
|
||||
u8 fat_check_device(const block_device *device, u8 *first_sector) {
|
||||
uint8_t fat_check_device(const block_device *device, uint8_t *first_sector) {
|
||||
fat_bpb bpb;
|
||||
memcpy((u8 *) &bpb, first_sector, sizeof(fat_bpb));
|
||||
memcpy((uint8_t *) &bpb, first_sector, sizeof(fat_bpb));
|
||||
printf("OEM: ");
|
||||
print_chars(bpb.bpb.oem_identifier, 11);
|
||||
printf("\n");
|
||||
|
||||
u32 total_sectors = bpb.bpb.total_sectors == 0 ? bpb.bpb.large_total_sectors : bpb.bpb.total_sectors;
|
||||
u32 fat_size = bpb.bpb.sectors_per_fat == 0 ? bpb.ebr_32.sectors_per_fat : bpb.bpb.sectors_per_fat;
|
||||
u32 root_dir_sectors = ((bpb.bpb.directories * 32) + (bpb.bpb.bytes_per_sector - 1)) / bpb.bpb.bytes_per_sector;
|
||||
u32 first_data_sector = bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors;
|
||||
u32 first_fat_sector = bpb.bpb.reserved_sectors;
|
||||
u32 data_sectors = total_sectors - (bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors);
|
||||
u32 total_clusters = data_sectors / bpb.bpb.sectors_per_cluster;
|
||||
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;
|
||||
|
||||
u8 fat_type;
|
||||
uint8_t fat_type;
|
||||
if (total_clusters < 4085) {
|
||||
fat_type = FAT_TYPE_12;
|
||||
} else if (total_clusters < 65525) {
|
||||
@@ -147,7 +147,7 @@ u8 fat_check_device(const block_device *device, u8 *first_sector) {
|
||||
|
||||
// dump dir
|
||||
// fat12/16 only
|
||||
u32 first_root_dir_sector = first_data_sector - root_dir_sectors;
|
||||
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);
|
||||
@@ -158,13 +158,13 @@ u8 fat_check_device(const block_device *device, u8 *first_sector) {
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
u32 sector;
|
||||
u32 value;
|
||||
uint32_t sector;
|
||||
uint32_t value;
|
||||
} table_result;
|
||||
|
||||
table_result
|
||||
get_fat_table_value(u8 fat_type, const u8 *fat_table, u32 active_cluster, u32 first_fat_cluster, u32 sector_size) {
|
||||
u32 fat_offset;
|
||||
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,
|
||||
@@ -173,7 +173,7 @@ get_fat_table_value(u8 fat_type, const u8 *fat_table, u32 active_cluster, u32 fi
|
||||
case FAT_TYPE_12:
|
||||
fat_offset = active_cluster * (active_cluster / 2);
|
||||
result.sector = first_fat_cluster + (fat_offset / sector_size);
|
||||
result.value = *(u16 *) &fat_table[fat_offset % sector_size];
|
||||
result.value = *(uint16_t *) &fat_table[fat_offset % sector_size];
|
||||
if (active_cluster & 0x1) {
|
||||
result.value = result.value >> 4;
|
||||
} else {
|
||||
@@ -183,13 +183,13 @@ get_fat_table_value(u8 fat_type, const u8 *fat_table, u32 active_cluster, u32 fi
|
||||
case FAT_TYPE_16:
|
||||
fat_offset = active_cluster * 2;
|
||||
result.sector = first_fat_cluster + (fat_offset / sector_size);
|
||||
result.value = *(u16 *) &fat_table[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 = *(u32 *) &fat_table[fat_offset % sector_size] & 0x0FFFFFFF;
|
||||
result.value = *(uint32_t *) &fat_table[fat_offset % sector_size] & 0x0FFFFFFF;
|
||||
return result;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -11,31 +11,31 @@
|
||||
#include <libc/libc.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;
|
||||
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;
|
||||
} __attribute((packed)) mbr_partition_table_entry;
|
||||
|
||||
typedef struct {
|
||||
u32 unique_id;
|
||||
u16 reserved;
|
||||
uint32_t unique_id;
|
||||
uint16_t reserved;
|
||||
mbr_partition_table_entry entries[4];
|
||||
u8 signature[2];
|
||||
uint8_t signature[2];
|
||||
} __attribute((packed)) mbr_table;
|
||||
|
||||
typedef struct {
|
||||
const block_device *device;
|
||||
u32 start_lba;
|
||||
uint32_t start_lba;
|
||||
} mbr_block_driver_info;
|
||||
|
||||
u8 mbr_check_device(const block_device *device, u8 *first_sector);
|
||||
uint8_t mbr_check_device(const block_device *device, uint8_t *first_sector);
|
||||
|
||||
block_dev_driver mbr_driver = {
|
||||
.name = "mbr",
|
||||
@@ -48,12 +48,12 @@ void mbr_register_block_driver() {
|
||||
block_dev_register_driver(&mbr_driver);
|
||||
}
|
||||
|
||||
u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 sectors, void *target) {
|
||||
uint8_t mbr_block_dev_access(const block_device *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 u32 actual_lba = info->start_lba + lba;
|
||||
const uint32_t actual_lba = info->start_lba + lba;
|
||||
if (actual_lba > info->device->num_lba) {
|
||||
return BLOCK_DEV_ACCESS_ERR;
|
||||
}
|
||||
@@ -62,9 +62,9 @@ u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 se
|
||||
return info->device->access(info->device, direction, actual_lba, sectors, target);
|
||||
}
|
||||
|
||||
u8 mbr_check_device(const block_device *device, u8 *first_sector) {
|
||||
uint8_t mbr_check_device(const block_device *device, uint8_t *first_sector) {
|
||||
mbr_table table;
|
||||
memcpy((u8 *) &table, first_sector + (device->block_size - sizeof(mbr_table)), sizeof(mbr_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;
|
||||
}
|
||||
@@ -104,7 +104,7 @@ u8 mbr_check_device(const block_device *device, u8 *first_sector) {
|
||||
return BLOCK_DEV_DRIVER_CHECK_OK;
|
||||
}
|
||||
|
||||
void mbr_read_from_ide(u8 ide_drive) {
|
||||
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);
|
||||
|
||||
@@ -9,6 +9,6 @@
|
||||
|
||||
void mbr_register_block_driver();
|
||||
|
||||
void mbr_read_from_ide(u8 ide_drive);
|
||||
void mbr_read_from_ide(uint8_t ide_drive);
|
||||
|
||||
#endif //NEW_KERNEL_MBR_H
|
||||
|
||||
Reference in New Issue
Block a user