From 8584c56f5e8e6b20568171fa8cb613bede776eb9 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 10 Jun 2023 20:24:17 -0400 Subject: [PATCH] fs: add detection for ext2 filesystems Signed-off-by: Michael Shaw --- dev/ahci.c | 5 ++- fs/block.c | 2 - fs/ext2.c | 44 ++++++++++++++++++ fs/gpt.c | 35 ++++++++++----- include/fs/ext2.h | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 15 deletions(-) create mode 100644 fs/ext2.c create mode 100644 include/fs/ext2.h diff --git a/dev/ahci.c b/dev/ahci.c index b3d6ca0..cc85b91 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -112,7 +112,10 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar) sdev->sector_count = sector_count; sdev->sector_size = 512; - block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size); + + struct block_device *bdev; + bdev = block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size); + block_gpt_init(bdev); ahci_rebase(&abar->ports[i], i); sata_device_count++; diff --git a/fs/block.c b/fs/block.c index d8f4e3f..70641d0 100644 --- a/fs/block.c +++ b/fs/block.c @@ -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++; diff --git a/fs/ext2.c b/fs/ext2.c new file mode 100644 index 0000000..92f87e3 --- /dev/null +++ b/fs/ext2.c @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include + +#include + +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; +} diff --git a/fs/gpt.c b/fs/gpt.c index ec742ac..8887c2b 100644 --- a/fs/gpt.c +++ b/fs/gpt.c @@ -3,12 +3,15 @@ #include #include +#include + #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; diff --git a/include/fs/ext2.h b/include/fs/ext2.h new file mode 100644 index 0000000..aaae649 --- /dev/null +++ b/include/fs/ext2.h @@ -0,0 +1,112 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _EXT2_H_ +#define _EXT2_H_ + +#include +#include +#include + +#define EXT2_SUPER_MAGIC 0xEF53 + +struct ext2_superblock { + uint32_t inodes_count; + uint32_t blocks_count; + uint32_t u_blocks_count; + uint32_t free_blocks_count; + uint32_t free_inodes_count; + uint32_t first_data_block; + + uint32_t log_block_size; + uint32_t log_frag_size; + + uint32_t blocks_per_group; + uint32_t frags_per_group; + uint32_t inodes_per_group; + + uint32_t mtime; + uint32_t wtime; + + uint16_t mnt_count; + uint16_t max_mnt_count; + uint16_t magic; + uint16_t state; + uint16_t errors; + uint16_t minor_rev_level; + + uint32_t lastcheck; + uint32_t checkinterval; + uint32_t creator_os; + uint32_t rev_level; + + uint16_t def_resuid; + uint16_t def_resgid; + + /* extended superblock fields + * if rev_level >= 1 + */ + uint32_t first_ino; + uint16_t inode_size; + uint16_t block_group_nr; + uint32_t feature_compat; + uint32_t feature_incompat; + uint32_t feature_ro_compat; + uint8_t uuid[16]; + char volume_name[16]; + char last_mounted[64]; + uint32_t algo_bitmap; + + /* performance hints */ + uint8_t prealloc_blocks; + uint8_t prealloc_dir_blocks; + uint16_t padding1; + + /* journaling support */ + uint8_t journal_uuid[16]; + uint32_t journal_inum; + uint32_t journal_dev; + uint32_t last_orphan; +} PACKED; + +struct ext2_inode { + uint16_t mode; + uint16_t uid; + uint32_t size; + + uint32_t atime; + uint32_t ctime; + uint32_t mtime; + uint32_t dtime; + + uint16_t gid; + uint16_t links_count; + uint32_t blocks; + uint32_t flags; + uint32_t osd1; + uint32_t block[12]; + + uint32_t block_indirect; + uint32_t block_dindirect; + uint32_t block_tindirect; + + uint32_t generation; + uint32_t file_acl; + uint32_t dir_acl; + uint32_t faddr; + + uint8_t frag_number; + uint8_t frag_size; + uint16_t padding1; + uint16_t user_high; + uint16_t group_high; + uint32_t padding2; +} PACKED; + + +struct ext2fs { + struct ext2_superblock *sb; + struct block_device *bdev; +}; + +struct ext2fs *ext2_init_fs(struct block_device *bdev); + +#endif /* _EXT2_H_ */