block: add gpt partitioning
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
0d87d7bffe
commit
25c0b538d2
5 changed files with 177 additions and 3 deletions
|
|
@ -1,2 +1,3 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += block.o
|
||||
obj-y += gpt.o
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
96
kernel/block/gpt.c
Normal file
96
kernel/block/gpt.c
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/mem.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/stdio.h>
|
||||
|
||||
#include "gpt.h"
|
||||
#include "partition.h"
|
||||
|
||||
bool block_has_gpt_header(struct block_device *bdev)
|
||||
{
|
||||
bool ret = false;
|
||||
char *lba1 = kzalloc(bdev->block_size, ALLOC_KERN);
|
||||
if (!lba1)
|
||||
return false;
|
||||
|
||||
int res = block_read(bdev, lba1, 1, 1);
|
||||
|
||||
if (res < 0)
|
||||
goto out;
|
||||
|
||||
if (!memcmp("EFI PART", lba1, 8))
|
||||
ret = true;
|
||||
|
||||
out:
|
||||
kfree(lba1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int gpt_parse_partitions(struct block_device *bdev, struct gpt_partition **out)
|
||||
{
|
||||
if (!block_has_gpt_header(bdev))
|
||||
return -EINVAL;
|
||||
struct gpt_header *header = kzalloc(bdev->block_size, ALLOC_KERN);
|
||||
int ret = 0;
|
||||
|
||||
if (!header)
|
||||
return -ENOMEM;
|
||||
|
||||
int res = block_read(bdev, (void *)header, 1, 1);
|
||||
|
||||
if (res < 0) {
|
||||
ret = res;
|
||||
goto out;
|
||||
}
|
||||
|
||||
size_t n_blocks_pentry = ALIGN_UP(header->n_pentries * header->pentry_size, bdev->block_size) / bdev->block_size;
|
||||
struct gpt_partition *ptables = kzalloc(n_blocks_pentry * bdev->block_size, ALLOC_KERN);
|
||||
if (!ptables) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
res = block_read(bdev, (void *)ptables, header->pentry_lba, n_blocks_pentry);
|
||||
|
||||
if (res < 0) {
|
||||
ret = res;
|
||||
goto out_ptables;
|
||||
}
|
||||
|
||||
if (header->pentry_size < sizeof(struct gpt_partition))
|
||||
goto out_ptables;
|
||||
|
||||
if (header->n_pentries > 128) {
|
||||
kwarn("bdev /dev/%s: too many GPT entries (%u), limiting to 128\n", bdev->name, header->n_pentries);
|
||||
header->n_pentries = 128;
|
||||
}
|
||||
|
||||
static const char null_guid[16] = {0};
|
||||
|
||||
(*out) = kalloc(header->n_pentries * sizeof(struct gpt_partition), ALLOC_KERN);
|
||||
if (!(*out)) {
|
||||
ret = -ENOMEM;
|
||||
goto out_ptables;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
for (size_t i = 0; i < header->n_pentries; i++) {
|
||||
void *start = ptables;
|
||||
start += i * header->pentry_size;
|
||||
|
||||
struct gpt_partition *part = start;
|
||||
|
||||
if (!memcmp(part->ptypeguid, null_guid, 16))
|
||||
continue;
|
||||
|
||||
struct gpt_partition *desc = (*out) + ret;
|
||||
memcpy(desc, part, sizeof(struct gpt_partition));
|
||||
ret += 1;
|
||||
}
|
||||
|
||||
out_ptables:
|
||||
kfree(ptables);
|
||||
out:
|
||||
kfree(header);
|
||||
return ret;
|
||||
}
|
||||
40
kernel/block/gpt.h
Normal file
40
kernel/block/gpt.h
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _GPT_H_
|
||||
#define _GPT_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/block.h>
|
||||
|
||||
#include <lib/list.h>
|
||||
|
||||
|
||||
struct gpt_header {
|
||||
char signature[8];
|
||||
u32 revision;
|
||||
u32 header_size;
|
||||
u32 header_crc32;
|
||||
u32 reserved;
|
||||
u64 this_lba;
|
||||
u64 alt_lba;
|
||||
u64 first_lba;
|
||||
u64 last_lba;
|
||||
char diskguid[16];
|
||||
u64 pentry_lba;
|
||||
u32 n_pentries;
|
||||
u32 pentry_size;
|
||||
u32 pentry_crc32;
|
||||
} PACKED;
|
||||
|
||||
struct gpt_partition {
|
||||
char ptypeguid[16];
|
||||
char partguid[16];
|
||||
u64 lba_start;
|
||||
u64 lba_end;
|
||||
u64 attributes;
|
||||
char partname[72];
|
||||
} PACKED;
|
||||
|
||||
int gpt_parse_partitions(struct block_device *bdev, struct gpt_partition **out);
|
||||
bool block_has_gpt_header(struct block_device *bdev);
|
||||
|
||||
#endif /* _GPT_H_ */
|
||||
10
kernel/block/partition.h
Normal file
10
kernel/block/partition.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _PARTITION_H_
|
||||
#define _PARTITION_H_
|
||||
|
||||
#include <kernel/types.h>
|
||||
#include <kernel/block.h>
|
||||
|
||||
bool block_has_gpt_header(struct block_device *bdev);
|
||||
|
||||
#endif /* _PARTITION_H_ */
|
||||
Loading…
Reference in a new issue