block: read GPT header from block devices on init
This commit is contained in:
parent
c94d26dff1
commit
05c528f64c
6 changed files with 138 additions and 8 deletions
18
dev/ahci.c
18
dev/ahci.c
|
|
@ -103,9 +103,16 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar)
|
||||||
sdev->controller = dev;
|
sdev->controller = dev;
|
||||||
sdev->abar = abar;
|
sdev->abar = abar;
|
||||||
|
|
||||||
char *name = kzalloc(32, ALLOC_KERN);
|
char *name = kzalloc(16, ALLOC_KERN);
|
||||||
snprintf(name, "ahci%d", 32, sata_device_count);
|
name[15] = 0;
|
||||||
block_register(name, &ahci_block_ops, sdev);
|
|
||||||
|
snprintf(name, "ahci%d", 15, sata_device_count);
|
||||||
|
|
||||||
|
size_t sector_count = ((size_t)abar->ports[i].ssts) << 32 | abar->ports[i].ssts;
|
||||||
|
sdev->sector_count = sector_count;
|
||||||
|
|
||||||
|
sdev->sector_size = 512;
|
||||||
|
block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size);
|
||||||
|
|
||||||
ahci_rebase(&abar->ports[i], i);
|
ahci_rebase(&abar->ports[i], i);
|
||||||
sata_device_count++;
|
sata_device_count++;
|
||||||
|
|
@ -227,7 +234,7 @@ static int ahci_block_read(struct block_device *dev, void *buf, size_t offset, s
|
||||||
size_t n = MIN(sector_count, 128u);
|
size_t n = MIN(sector_count, 128u);
|
||||||
if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, false))
|
if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, false))
|
||||||
return -1;
|
return -1;
|
||||||
if(sector_count <= 128)
|
if (sector_count <= 128)
|
||||||
break;
|
break;
|
||||||
sector_count -= 128ull;
|
sector_count -= 128ull;
|
||||||
offset += 128;
|
offset += 128;
|
||||||
|
|
@ -246,7 +253,7 @@ static int ahci_block_write(struct block_device *dev, void *buf, size_t offset,
|
||||||
size_t n = MIN(sector_count, 128u);
|
size_t n = MIN(sector_count, 128u);
|
||||||
if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, true))
|
if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, true))
|
||||||
return -1;
|
return -1;
|
||||||
if(sector_count <= 128)
|
if (sector_count <= 128)
|
||||||
break;
|
break;
|
||||||
sector_count -= 128ull;
|
sector_count -= 128ull;
|
||||||
offset += 128;
|
offset += 128;
|
||||||
|
|
@ -266,7 +273,6 @@ void ahci_init()
|
||||||
}
|
}
|
||||||
abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start);
|
abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start);
|
||||||
|
|
||||||
|
|
||||||
if (!(abar->ghc & (1 << 31))) {
|
if (!(abar->ghc & (1 << 31))) {
|
||||||
printf(LOG_WARN "AHCI controller not enabled\n");
|
printf(LOG_WARN "AHCI controller not enabled\n");
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -2,13 +2,14 @@
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/block.h>
|
#include <kernel/block.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
|
#include <kernel/gpt.h>
|
||||||
|
|
||||||
#define MAX_BLOCK_DEVICES 24
|
#define MAX_BLOCK_DEVICES 24
|
||||||
|
|
||||||
struct block_device *block_devices[MAX_BLOCK_DEVICES];
|
struct block_device *block_devices[MAX_BLOCK_DEVICES];
|
||||||
size_t block_devices_count = 0;
|
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;
|
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->name = name;
|
||||||
bdev->data = data;
|
bdev->data = data;
|
||||||
|
bdev->block_count = block_count;
|
||||||
|
bdev->block_size = block_size;
|
||||||
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
|
memcpy(&bdev->ops, ops, sizeof(struct block_device_ops));
|
||||||
|
|
||||||
|
block_gpt_init(bdev);
|
||||||
|
|
||||||
block_devices[block_devices_count] = bdev;
|
block_devices[block_devices_count] = bdev;
|
||||||
block_devices_count++;
|
block_devices_count++;
|
||||||
|
|
||||||
|
|
|
||||||
57
fs/gpt.c
Normal file
57
fs/gpt.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -334,6 +334,8 @@ struct sata_device {
|
||||||
struct pci_device *controller;
|
struct pci_device *controller;
|
||||||
uint8_t port;
|
uint8_t port;
|
||||||
|
|
||||||
|
size_t sector_size;
|
||||||
|
size_t sector_count;
|
||||||
hbamem_t *abar;
|
hbamem_t *abar;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
#define _BLOCK_H_
|
#define _BLOCK_H_
|
||||||
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/gpt.h>
|
||||||
|
|
||||||
struct block_device;
|
struct block_device;
|
||||||
|
|
||||||
|
|
@ -16,9 +17,16 @@ struct block_device {
|
||||||
struct block_device_ops ops;
|
struct block_device_ops ops;
|
||||||
void *data;
|
void *data;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
|
||||||
|
size_t block_count;
|
||||||
|
size_t block_size;
|
||||||
|
|
||||||
|
struct gpt_header *gpt_header;
|
||||||
|
struct gpt_entry *gpt_entries;
|
||||||
|
size_t gpt_entries_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
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_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);
|
||||||
|
|
||||||
|
|
|
||||||
52
include/kernel/gpt.h
Normal file
52
include/kernel/gpt.h
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
|
#ifndef _GPT_H_
|
||||||
|
#define _GPT_H_
|
||||||
|
|
||||||
|
#include <kernel/common.h>
|
||||||
|
|
||||||
|
struct block_device;
|
||||||
|
|
||||||
|
/* GPT LBA 0 */
|
||||||
|
struct gpt_mbr_prot {
|
||||||
|
uint8_t boot[1];
|
||||||
|
uint8_t chs_start[3];
|
||||||
|
uint8_t type[1];
|
||||||
|
uint8_t chs_end[3];
|
||||||
|
uint8_t lba_start[4];
|
||||||
|
uint8_t lba_size[4];
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
/* GPT LBA 1
|
||||||
|
* checksums use CRC32
|
||||||
|
*/
|
||||||
|
struct gpt_header {
|
||||||
|
uint64_t signature;
|
||||||
|
uint8_t revision[4];
|
||||||
|
uint8_t header_size[4];
|
||||||
|
uint8_t checksum[4];
|
||||||
|
uint8_t reserved[4];
|
||||||
|
uint64_t lba_this;
|
||||||
|
uint64_t lba_alt;
|
||||||
|
uint64_t lba_first;
|
||||||
|
uint64_t lba_last;
|
||||||
|
uint8_t guid[16];
|
||||||
|
uint64_t lba_table;
|
||||||
|
uint32_t entry_count;
|
||||||
|
uint32_t entry_size;
|
||||||
|
uint32_t checksum_table;
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
/* GPT LBA 2 */
|
||||||
|
struct gpt_entry {
|
||||||
|
uint8_t guid[16];
|
||||||
|
uint8_t guid_parent[16];
|
||||||
|
uint64_t lba_first;
|
||||||
|
uint64_t lba_last;
|
||||||
|
uint8_t attr[8];
|
||||||
|
char name[72];
|
||||||
|
} PACKED;
|
||||||
|
|
||||||
|
void block_gpt_init(struct block_device *dev);
|
||||||
|
|
||||||
|
#endif /* _GPT_H_ */
|
||||||
|
|
||||||
Loading…
Reference in a new issue