fs: add detection for ext2 filesystems

Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
Michael Shaw 2023-06-10 20:24:17 -04:00
commit 8584c56f5e
5 changed files with 183 additions and 15 deletions

View file

@ -24,8 +24,6 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
bdev->lba_start = 0;
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
block_gpt_init(bdev);
block_devices[block_devices_count] = bdev;
block_devices_count++;

44
fs/ext2.c Normal file
View file

@ -0,0 +1,44 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/slab.h>
#include <fs/ext2.h>
static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb_struct)
{
char *buf = kmalloc(1024, ALLOC_DMA);
if (!buf)
return -1;
int ret = block_read(bdev, buf, 2, 1);
if (ret < 0) {
kfree(buf);
return ret;
}
struct ext2_superblock *sb = (struct ext2_superblock *)buf;
if (sb->magic != EXT2_SUPER_MAGIC) {
kfree(buf);
return -1;
}
memcpy(sb_struct, buf, sizeof(*sb_struct));
kfree(buf);
return ret;
}
struct ext2fs *ext2_init_fs(struct block_device *bdev)
{
struct ext2fs *ret = kmalloc(sizeof(*ret), ALLOC_KERN);
if (!ret)
return NULL;
ret->bdev = bdev;
if (ext2_read_super(bdev, ret->sb) < 0) {
kfree(ret);
return NULL;
}
return ret;
}

View file

@ -3,12 +3,15 @@
#include <kernel/slab.h>
#include <kernel/block.h>
#include <fs/ext2.h>
#define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */
void block_gpt_init(struct block_device *dev)
{
size_t block_size = dev->block_size;
/* Verify that this is a GPT partition table */
struct gpt_header *header = kmalloc(block_size, ALLOC_DMA);
if (!header)
return;
@ -37,30 +40,38 @@ void block_gpt_init(struct block_device *dev)
goto free_entries;
}
/* Parse the GPT entries and create block devices for each partition
* that is found.
*
* After block device is registered, the system attempts to register a
* filesystem on the device if it is detected
*/
size_t used_count = 0;
for (size_t i = 0; i < header->entry_count; i++) {
if (entries[i].type_guid[1] == 0)
struct gpt_entry *entry = &entries[i];
if (entry->type_guid[1] == 0)
continue;
used_count++;
struct block_device *bdev = kmalloc(sizeof(struct block_device), ALLOC_KERN);
memcpy(bdev, dev, sizeof(struct block_device));
/* Name and register the partition */
char *buf = kzalloc(64, ALLOC_KERN);
snprintf(buf, "%sp%d", 63, dev->name, used_count - 1);
if (!bdev)
continue;
bdev->name = kmalloc(64, ALLOC_KERN);
if (!bdev->name) {
kfree(bdev);
struct block_device *bdev = block_register(buf, &dev->ops, dev->data, dev->block_count, dev->block_size);
if (!bdev) {
kfree(buf);
continue;
}
snprintf(bdev->name, "%sp%d", 63, dev->name, used_count - 1);
bdev->name[63] = '\0';
bdev->lba_start = entries[i].lba_first;
bdev->block_count = entries[i].lba_last - entries[i].lba_first + 1;
struct ext2fs *fs = ext2_init_fs(bdev);
if (fs) {
kprintf(LOG_INFO "Found ext2 filesystem on device %s\n", buf);
}
}
dev->partition_count = used_count;