block: replace block_part with independent block devices
this also removes the fat32 support for now, as the virtual filesystem needs more work
This commit is contained in:
parent
db5990cc97
commit
21e2540243
6 changed files with 25 additions and 190 deletions
15
fs/block.c
15
fs/block.c
|
|
@ -21,6 +21,7 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
|
||||||
bdev->data = data;
|
bdev->data = data;
|
||||||
bdev->block_count = block_count;
|
bdev->block_count = block_count;
|
||||||
bdev->block_size = block_size;
|
bdev->block_size = block_size;
|
||||||
|
bdev->lba_start = 0;
|
||||||
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
|
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
|
||||||
|
|
||||||
block_gpt_init(bdev);
|
block_gpt_init(bdev);
|
||||||
|
|
@ -37,20 +38,10 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
|
||||||
|
|
||||||
inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size)
|
inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size)
|
||||||
{
|
{
|
||||||
return bdev->ops.read(bdev, buf, offset, size);
|
return bdev->ops.read(bdev, buf, offset + bdev->lba_start, size);
|
||||||
}
|
|
||||||
|
|
||||||
inline int block_readp(struct block_part *part, void *buf, size_t offset, size_t size)
|
|
||||||
{
|
|
||||||
return part->bdev->ops.read(part->bdev, buf, part->lba_start + offset, size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size)
|
inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size)
|
||||||
{
|
{
|
||||||
return bdev->ops.write(bdev, buf, offset, size);
|
return bdev->ops.write(bdev, buf, offset + bdev->lba_start, size);
|
||||||
}
|
|
||||||
|
|
||||||
inline int block_writep(struct block_part *part, void *buf, size_t offset, size_t size)
|
|
||||||
{
|
|
||||||
return part->bdev->ops.write(part->bdev, buf, part->lba_start + offset, size);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
68
fs/fat32.c
68
fs/fat32.c
|
|
@ -1,68 +0,0 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
#include <kernel/block.h>
|
|
||||||
#include <kernel/slab.h>
|
|
||||||
#include <fs/fat32.h>
|
|
||||||
|
|
||||||
struct fat32_fs *fat_init_part(struct block_part *part)
|
|
||||||
{
|
|
||||||
struct fat32_fs *ret = kmalloc(sizeof(struct fat32_fs), ALLOC_KERN);
|
|
||||||
if (!ret)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
ret->part = part;
|
|
||||||
ret->dev = part->bdev;
|
|
||||||
|
|
||||||
char *buf = kmalloc(2048, ALLOC_KERN);
|
|
||||||
if (!buf)
|
|
||||||
goto free_ret;
|
|
||||||
|
|
||||||
if (block_readp(part, &ret->bpb, 0, 1)) {
|
|
||||||
kprintf(LOG_ERROR "Failed to read block\n");
|
|
||||||
goto free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret->bpb.signature != FAT32_SIGNATURE) {
|
|
||||||
kprintf(LOG_ERROR "Invalid FAT32 signature: %x\n", ret->bpb.signature);
|
|
||||||
goto free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret->bpb.bytes_per_sector != 512) {
|
|
||||||
kprintf(LOG_ERROR "Invalid FAT32 bytes per sector: %d\n", ret->bpb.bytes_per_sector);
|
|
||||||
goto free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (block_readp(part, buf, ret->bpb.fsinfo_sector, 1)) {
|
|
||||||
kprintf(LOG_ERROR "Failed to read fsinfo block\n");
|
|
||||||
goto free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct fat32_fsinfo *fsinfo = (struct fat32_fsinfo *)(buf);
|
|
||||||
ret->fsinfo = *fsinfo;
|
|
||||||
|
|
||||||
if (block_readp(part, buf + 512, ret->bpb.root_cluster, 1)) {
|
|
||||||
goto free_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&ret->root, buf + 512, sizeof(struct fat32_dirent));
|
|
||||||
|
|
||||||
kfree(buf);
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
/* error conditions */
|
|
||||||
free_buf:
|
|
||||||
kfree(buf);
|
|
||||||
free_ret:
|
|
||||||
kfree(ret);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fat_print_root(struct fat32_fs *fs)
|
|
||||||
{
|
|
||||||
for(int i = 0; i < fs->bpb.root_entries; i++) {
|
|
||||||
struct fat32_dirent *entry = &fs->root;
|
|
||||||
if (entry->name[0] == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
kprintf(LOG_WARN "%s\n", entry->name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
34
fs/gpt.c
34
fs/gpt.c
|
|
@ -1,9 +1,6 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
#include <kernel/block.h>
|
|
||||||
#include <kernel/gpt.h>
|
#include <kernel/gpt.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
|
#include <kernel/block.h>
|
||||||
#include <fs/fat32.h>
|
|
||||||
|
|
||||||
#define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */
|
#define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */
|
||||||
|
|
||||||
|
|
@ -11,11 +8,11 @@ void block_gpt_init(struct block_device *dev)
|
||||||
{
|
{
|
||||||
size_t block_size = dev->block_size;
|
size_t block_size = dev->block_size;
|
||||||
|
|
||||||
struct gpt_header *header = kmalloc(block_size, ALLOC_KERN);
|
struct gpt_header *header = kmalloc(block_size, ALLOC_DMA);
|
||||||
if (!header)
|
if (!header)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
struct gpt_entry *entries = kmalloc(block_size, ALLOC_KERN);
|
struct gpt_entry *entries = kmalloc(block_size, ALLOC_DMA);
|
||||||
if (!entries)
|
if (!entries)
|
||||||
goto free_header;
|
goto free_header;
|
||||||
|
|
||||||
|
|
@ -39,8 +36,6 @@ void block_gpt_init(struct block_device *dev)
|
||||||
goto free_entries;
|
goto free_entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
dev->partitions = kmalloc(sizeof(struct block_part) * header->entry_count, ALLOC_KERN);
|
|
||||||
|
|
||||||
size_t used_count = 0;
|
size_t used_count = 0;
|
||||||
for (size_t i = 0; i < header->entry_count; i++) {
|
for (size_t i = 0; i < header->entry_count; i++) {
|
||||||
if (entries[i].type_guid[1] == 0)
|
if (entries[i].type_guid[1] == 0)
|
||||||
|
|
@ -48,17 +43,26 @@ void block_gpt_init(struct block_device *dev)
|
||||||
|
|
||||||
used_count++;
|
used_count++;
|
||||||
|
|
||||||
struct block_part *part = &dev->partitions[i];
|
struct block_device *bdev = kmalloc(sizeof(struct block_device), ALLOC_KERN);
|
||||||
part->bdev = dev;
|
memcpy(bdev, dev, sizeof(struct block_device));
|
||||||
part->lba_start = entries[i].lba_first;
|
|
||||||
part->num_blocks = entries[i].lba_last - entries[i].lba_first + 1;
|
|
||||||
|
|
||||||
struct fat32_fs *fs = fat_init_part(part);
|
if (!bdev)
|
||||||
fat_print_root(fs);
|
continue;
|
||||||
|
|
||||||
|
bdev->name = kmalloc(64, ALLOC_KERN);
|
||||||
|
if (!bdev->name) {
|
||||||
|
kfree(bdev);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
dev->partition_count = used_count;
|
dev->partition_count = used_count;
|
||||||
|
|
||||||
|
|
||||||
#ifdef KDEBUG
|
#ifdef KDEBUG
|
||||||
kprintf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name);
|
kprintf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,82 +0,0 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
||||||
#ifndef _FAT32_H_
|
|
||||||
#define _FAT32_H_
|
|
||||||
|
|
||||||
#include <kernel/common.h>
|
|
||||||
#include <kernel/block.h>
|
|
||||||
|
|
||||||
#define FAT32_SIGNATURE 0x29ull
|
|
||||||
|
|
||||||
struct fat32_bpb {
|
|
||||||
uint8_t jmp[3];
|
|
||||||
uint64_t oem_id;
|
|
||||||
uint16_t bytes_per_sector;
|
|
||||||
uint8_t sectors_per_cluster;
|
|
||||||
uint16_t reserved_sectors;
|
|
||||||
uint8_t fat_count;
|
|
||||||
uint16_t root_entries;
|
|
||||||
uint16_t total_sectors;
|
|
||||||
uint8_t media_type;
|
|
||||||
uint16_t fat_sectors;
|
|
||||||
uint16_t sectors_per_track;
|
|
||||||
uint16_t heads;
|
|
||||||
uint32_t hidden_sectors;
|
|
||||||
uint32_t total_sectors_large;
|
|
||||||
|
|
||||||
/* FAT32 */
|
|
||||||
uint32_t fat_sectors_large;
|
|
||||||
uint16_t flags;
|
|
||||||
uint16_t version;
|
|
||||||
uint32_t root_cluster;
|
|
||||||
uint16_t fsinfo_sector;
|
|
||||||
uint16_t backup_sector;
|
|
||||||
uint8_t reserved[12];
|
|
||||||
uint8_t drive_number;
|
|
||||||
uint8_t reserved2;
|
|
||||||
uint8_t signature;
|
|
||||||
uint32_t volume_id;
|
|
||||||
uint8_t volume_label[11];
|
|
||||||
uint8_t system_id[8];
|
|
||||||
uint8_t boot_code[420];
|
|
||||||
uint16_t boot_signature;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct fat32_fsinfo {
|
|
||||||
uint32_t signature1;
|
|
||||||
uint8_t reserved1[480];
|
|
||||||
uint32_t signature2;
|
|
||||||
uint32_t free_cluster_count;
|
|
||||||
uint32_t next_free_cluster;
|
|
||||||
uint8_t reserved2[12];
|
|
||||||
uint32_t signature3;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct fat32_dirent {
|
|
||||||
uint8_t name[8];
|
|
||||||
uint8_t ext[3];
|
|
||||||
uint8_t attr;
|
|
||||||
uint8_t reserved;
|
|
||||||
uint8_t ctime_ms;
|
|
||||||
uint16_t ctime_time;
|
|
||||||
uint16_t ctime_date;
|
|
||||||
uint16_t atime_date;
|
|
||||||
uint16_t cluster_high;
|
|
||||||
uint16_t mtime_time;
|
|
||||||
uint16_t mtime_date;
|
|
||||||
uint16_t cluster_low;
|
|
||||||
uint32_t size;
|
|
||||||
} PACKED;
|
|
||||||
|
|
||||||
struct fat32_fs {
|
|
||||||
struct block_device *dev;
|
|
||||||
struct block_part *part;
|
|
||||||
|
|
||||||
struct fat32_bpb bpb;
|
|
||||||
struct fat32_fsinfo fsinfo;
|
|
||||||
struct fat32_dirent root;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct fat32_fs *fat_init_part(struct block_part *part);
|
|
||||||
void fat_print_root(struct fat32_fs *fs);
|
|
||||||
|
|
||||||
#endif /* _FAT32_H_ */
|
|
||||||
|
|
@ -12,13 +12,6 @@ struct block_device_ops {
|
||||||
int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct block_part {
|
|
||||||
char *name;
|
|
||||||
struct block_device *bdev;
|
|
||||||
size_t lba_start;
|
|
||||||
size_t num_blocks;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct block_device {
|
struct block_device {
|
||||||
char *name;
|
char *name;
|
||||||
struct block_device_ops ops;
|
struct block_device_ops ops;
|
||||||
|
|
@ -27,16 +20,14 @@ struct block_device {
|
||||||
|
|
||||||
size_t block_count;
|
size_t block_count;
|
||||||
size_t block_size;
|
size_t block_size;
|
||||||
|
size_t lba_start;
|
||||||
|
|
||||||
struct block_part *partitions;
|
|
||||||
size_t partition_count;
|
size_t partition_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data, size_t block_count, size_t block_size);
|
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data, size_t block_count, size_t block_size);
|
||||||
int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
int block_readp(struct block_part *part, void *buf, size_t offset, size_t size);
|
|
||||||
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size);
|
||||||
int block_writep(struct block_part *part, void *buf, size_t offset, size_t size);
|
|
||||||
|
|
||||||
extern struct block_device *block_devices[];
|
extern struct block_device *block_devices[];
|
||||||
extern size_t block_devices_count;
|
extern size_t block_devices_count;
|
||||||
|
|
|
||||||
|
|
@ -157,7 +157,6 @@ void kmalloc_init()
|
||||||
for (size_t i = 0; i < ARRAY_SIZE(dma_sizes); i++)
|
for (size_t i = 0; i < ARRAY_SIZE(dma_sizes); i++)
|
||||||
slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K);
|
slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, slab_t **cache, bool fb_disable)
|
static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, slab_t **cache, bool fb_disable)
|
||||||
{
|
{
|
||||||
slab_t *slab = NULL;
|
slab_t *slab = NULL;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue