diff --git a/dev/ahci.c b/dev/ahci.c index 738b610..5a0aa97 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -103,9 +103,16 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar) sdev->controller = dev; sdev->abar = abar; - char *name = kzalloc(32, ALLOC_KERN); - snprintf(name, "ahci%d", 32, sata_device_count); - block_register(name, &ahci_block_ops, sdev); + char *name = kzalloc(16, ALLOC_KERN); + name[15] = 0; + + 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); 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); if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, false)) return -1; - if(sector_count <= 128) + if (sector_count <= 128) break; sector_count -= 128ull; 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); if (!ahci_access_sectors(sdev, offset, n, (paddr_t)buf & ~hhdm_start, true)) return -1; - if(sector_count <= 128) + if (sector_count <= 128) break; sector_count -= 128ull; offset += 128; @@ -266,7 +273,6 @@ void ahci_init() } abar = (hbamem_t *)((uintptr_t)dev->bar5 | hhdm_start); - if (!(abar->ghc & (1 << 31))) { printf(LOG_WARN "AHCI controller not enabled\n"); return; diff --git a/fs/block.c b/fs/block.c index 2558e8b..ad180fc 100644 --- a/fs/block.c +++ b/fs/block.c @@ -2,13 +2,14 @@ #include #include #include +#include #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++; diff --git a/fs/gpt.c b/fs/gpt.c new file mode 100644 index 0000000..83109c3 --- /dev/null +++ b/fs/gpt.c @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +#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; +} diff --git a/include/dev/ahci.h b/include/dev/ahci.h index c4b1b65..b163a1e 100644 --- a/include/dev/ahci.h +++ b/include/dev/ahci.h @@ -334,6 +334,8 @@ struct sata_device { struct pci_device *controller; uint8_t port; + size_t sector_size; + size_t sector_count; hbamem_t *abar; }; diff --git a/include/kernel/block.h b/include/kernel/block.h index e49eab9..8efa09b 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -3,6 +3,7 @@ #define _BLOCK_H_ #include +#include struct block_device; @@ -16,9 +17,16 @@ struct block_device { struct block_device_ops ops; void *data; 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_write(struct block_device *bdev, void *buf, size_t offset, size_t size); diff --git a/include/kernel/gpt.h b/include/kernel/gpt.h new file mode 100644 index 0000000..4ac3159 --- /dev/null +++ b/include/kernel/gpt.h @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _GPT_H_ +#define _GPT_H_ + +#include + +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_ */ +