block: read GPT header from block devices on init

This commit is contained in:
Michael Shaw 2023-05-13 23:04:15 -04:00
commit 05c528f64c
6 changed files with 138 additions and 8 deletions

View file

@ -2,13 +2,14 @@
#include <kernel/common.h>
#include <kernel/block.h>
#include <kernel/slab.h>
#include <kernel/gpt.h>
#define MAX_BLOCK_DEVICES 24
struct block_device *block_devices[MAX_BLOCK_DEVICES];
size_t block_devices_count = 0;
struct block_device *block_register(char *name, struct block_device_ops *ops, void *data)
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 *bdev;
@ -18,8 +19,12 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
bdev->name = name;
bdev->data = data;
bdev->block_count = block_count;
bdev->block_size = block_size;
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
block_gpt_init(bdev);
block_devices[block_devices_count] = bdev;
block_devices_count++;

57
fs/gpt.c Normal file
View file

@ -0,0 +1,57 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/block.h>
#include <kernel/gpt.h>
#include <kernel/slab.h>
#define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */
void block_gpt_init(struct block_device *dev)
{
size_t block_size = dev->block_size;
struct gpt_header *header = kmalloc(block_size, ALLOC_KERN);
if (!header)
return;
struct gpt_entry *entries = kmalloc(block_size, ALLOC_KERN);
if (!entries)
goto free_header;
if (block_read(dev, header, 1, 1)) {
printf(LOG_ERROR "Failed to read GPT header for device %s\n", dev->name);
goto free_entries;
}
if (header->signature != GPT_HEADER_SIGNATURE) {
printf(LOG_ERROR "Invalid GPT header signature for device %s\n", dev->name);
goto free_entries;
}
if (header->entry_size != sizeof(struct gpt_entry)) {
printf(LOG_ERROR "Invalid GPT entry size for device %s\n", dev->name);
goto free_entries;
}
if (block_read(dev, entries, header->lba_first, 1)) {
printf(LOG_ERROR "Failed to read GPT entries for device %s\n", dev->name);
goto free_entries;
}
dev->gpt_header = header;
dev->gpt_entries = entries;
dev->gpt_entries_count = header->entry_count;
#ifdef KDEBUG
printf(LOG_DEBUG "GPT header for device %s loaded\n", dev->name);
#endif
return; /* Success */
free_entries:
kfree(entries);
free_header:
kfree(header);
dev->gpt_entries = NULL;
dev->gpt_header = NULL;
dev->gpt_entries_count = 0;
}