block: add gpt partitioning

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-15 00:07:28 -04:00
commit 25c0b538d2
5 changed files with 177 additions and 3 deletions

View file

@ -7,6 +7,9 @@
#include <fs/devfs.h>
#include "gpt.h"
#include "partition.h"
int block_device_register(char *name, size_t lba_start, struct block_ops *ops, struct block_device **out)
{
struct block_device *ret = kzalloc(sizeof(struct block_device), ALLOC_KERN);
@ -31,12 +34,36 @@ int block_check_partitions(struct block_device *bdev)
if (!bdev || !bdev->disk.is_disk)
return -EINVAL;
return 0;
int ret = 0;
if (block_has_gpt_header(bdev)) {
struct gpt_partition *descs;
int n_partitions = gpt_parse_partitions(bdev, &descs);
for (int i = 0; i < n_partitions; i++) {
char name[256] = {0};
snprintf(name, 255, "%sp%d", bdev->name, i);
struct block_device *blkdev;
ret = block_device_register(name, descs[i].lba_start, &bdev->ops, &blkdev);
if (ret < 0)
break;
blkdev->part_of = bdev;
blkdev->size = descs[i].lba_end - descs[i].lba_start;
kprintf("registered blocked device /dev/%s\n", blkdev->name);
}
if (n_partitions)
kfree(descs);
}
return ret;
}
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks)
{
if (dev->lba_start) {
while (dev->part_of) {
offset += dev->lba_start;
dev = dev->part_of;
}
@ -49,7 +76,7 @@ ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_
ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n_blocks)
{
if (dev->lba_start) {
while (dev->part_of) {
offset += dev->lba_start;
dev = dev->part_of;
}