fs: add detection for ext2 filesystems
Signed-off-by: Michael Shaw <spencershawiscool@gmail.com>
This commit is contained in:
parent
30bf724282
commit
8584c56f5e
5 changed files with 183 additions and 15 deletions
|
|
@ -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++;
|
||||
|
|
|
|||
|
|
@ -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
44
fs/ext2.c
Normal 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;
|
||||
}
|
||||
35
fs/gpt.c
35
fs/gpt.c
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
112
include/fs/ext2.h
Normal file
112
include/fs/ext2.h
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _EXT2_H_
|
||||
#define _EXT2_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/block.h>
|
||||
#include <kernel/gpt.h>
|
||||
|
||||
#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_ */
|
||||
Loading…
Reference in a new issue