diff --git a/Makefile b/Makefile index 82d6e7c..540eafa 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ define DEFAULT_VAR = endef # User controllable CFLAGS. -CFLAGS ?= -g -O2 -pipe -Wall -Wextra +CFLAGS ?= -g -O0 -pipe -Wall -Wextra # User controllable linker flags. We set none by default. LDFLAGS ?= diff --git a/dev/ahci.c b/dev/ahci.c index 491485d..b3d6ca0 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -145,7 +145,6 @@ static int cmdslot(hbaport_t *port) bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write) { hbaport_t *port = &dev->abar->ports[dev->port]; - uint16_t ocount = count; /* clear pending interrupt */ port->is = (uint32_t)-1; @@ -218,10 +217,6 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p return false; } - /* disk read success */ -#ifdef KDEBUG - kprintf(LOG_DEBUG "Read %d sectors from disk\n", ocount); -#endif return true; } diff --git a/fs/block.c b/fs/block.c index 833170d..861d02f 100644 --- a/fs/block.c +++ b/fs/block.c @@ -40,7 +40,17 @@ inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_ return bdev->ops.read(bdev, buf, offset, size); } +inline int block_readp(struct block_part *part, void *buf, size_t offset, size_t size) +{ + return part->bdev->ops.read(part->bdev, buf, part->lba_start + offset, size); +} + inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size) { return bdev->ops.write(bdev, buf, offset, size); } + +inline int block_writep(struct block_part *part, void *buf, size_t offset, size_t size) +{ + return part->bdev->ops.write(part->bdev, buf, part->lba_start + offset, size); +} diff --git a/fs/fat32.c b/fs/fat32.c index e7d48c7..a9a4d5e 100644 --- a/fs/fat32.c +++ b/fs/fat32.c @@ -1,3 +1,57 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include +#include #include + +struct fat32_fs *fat_init_part(struct block_part *part) +{ + struct fat32_fs *ret = kmalloc(sizeof(struct fat32_fs), ALLOC_KERN); + if (!ret) + return NULL; + + ret->part = part; + ret->dev = part->bdev; + + char *buf = kmalloc(2048, ALLOC_KERN); + if (!buf) + goto free_ret; + + if (block_readp(part, &ret->bpb, 0, 1)) { + kprintf(LOG_ERROR "Failed to read block\n"); + goto free_buf; + } + + if (ret->bpb.signature != FAT32_SIGNATURE) { + kprintf(LOG_ERROR "Invalid FAT32 signature: %x\n", ret->bpb.signature); + goto free_buf; + } + + if (ret->bpb.bytes_per_sector != 512) { + kprintf(LOG_ERROR "Invalid FAT32 bytes per sector: %d\n", ret->bpb.bytes_per_sector); + goto free_buf; + } + + if (block_readp(part, buf, ret->bpb.fsinfo_sector, 1)) { + kprintf(LOG_ERROR "Failed to read fsinfo block\n"); + goto free_buf; + } + + struct fat32_fsinfo *fsinfo = (struct fat32_fsinfo *)(buf); + ret->fsinfo = *fsinfo; + + if (block_readp(part, buf + 512, ret->bpb.root_cluster, 1)) { + goto free_buf; + } + + memcpy(&ret->root, buf + 512, sizeof(struct fat32_dirent)); + + kfree(buf); + return ret; + + /* error conditions */ +free_buf: + kfree(buf); +free_ret: + kfree(ret); + return NULL; +} diff --git a/fs/gpt.c b/fs/gpt.c index 4de7a8c..c895336 100644 --- a/fs/gpt.c +++ b/fs/gpt.c @@ -3,6 +3,8 @@ #include #include +#include + #define GPT_HEADER_SIGNATURE 0x5452415020494645ull /* "EFI PART" */ void block_gpt_init(struct block_device *dev) @@ -32,26 +34,36 @@ void block_gpt_init(struct block_device *dev) goto free_entries; } - if (block_read(dev, entries, header->lba_first, 1)) { + if (block_read(dev, entries, header->lba_table, 1)) { kprintf(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; + dev->partitions = kmalloc(sizeof(struct block_part) * header->entry_count, ALLOC_KERN); + + size_t used_count = 0; + for(size_t i = 0; i < header->entry_count; i++) { + if (entries[i].type_guid[1] == 0) + continue; + + used_count++; + + struct block_part *part = &dev->partitions[i]; + part->bdev = dev; + part->lba_start = entries[i].lba_first; + part->num_blocks = entries[i].lba_last - entries[i].lba_first + 1; + + fat_init_part(part); + } + dev->partition_count = used_count; + #ifdef KDEBUG kprintf(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/fs/fat32.h b/include/fs/fat32.h index b1a914f..ef79849 100644 --- a/include/fs/fat32.h +++ b/include/fs/fat32.h @@ -5,4 +5,75 @@ #include #include +#define FAT32_SIGNATURE 0x29ull + +struct fat32_bpb { + uint8_t jmp[3]; + uint64_t oem_id; + uint16_t bytes_per_sector; + uint8_t sectors_per_cluster; + uint16_t reserved_sectors; + uint8_t fat_count; + uint16_t root_entries; + uint16_t total_sectors; + uint8_t media_type; + uint16_t fat_sectors; + uint16_t sectors_per_track; + uint16_t heads; + uint32_t hidden_sectors; + uint32_t total_sectors_large; + + /* FAT32 */ + uint32_t fat_sectors_large; + uint16_t flags; + uint16_t version; + uint32_t root_cluster; + uint16_t fsinfo_sector; + uint16_t backup_sector; + uint8_t reserved[12]; + uint8_t drive_number; + uint8_t reserved2; + uint8_t signature; + uint32_t volume_id; + uint8_t volume_label[11]; + uint8_t system_id[8]; + uint8_t boot_code[420]; + uint16_t boot_signature; +} PACKED; + +struct fat32_fsinfo { + uint32_t signature1; + uint8_t reserved1[480]; + uint32_t signature2; + uint32_t free_cluster_count; + uint32_t next_free_cluster; + uint8_t reserved2[12]; + uint32_t signature3; +} PACKED; + +struct fat32_dirent { + uint8_t name[8]; + uint8_t ext[3]; + uint8_t attr; + uint8_t reserved; + uint8_t ctime_ms; + uint16_t ctime_time; + uint16_t ctime_date; + uint16_t atime_date; + uint16_t cluster_high; + uint16_t mtime_time; + uint16_t mtime_date; + uint16_t cluster_low; + uint32_t size; +} PACKED; + +struct fat32_fs { + struct block_device *dev; + struct block_part *part; + + struct fat32_bpb bpb; + struct fat32_fsinfo fsinfo; + struct fat32_dirent root; +}; + #endif /* _FAT32_H_ */ diff --git a/include/kernel/block.h b/include/kernel/block.h index 8efa09b..90c879d 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -12,6 +12,13 @@ struct block_device_ops { int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size); }; +struct block_part { + char *name; + struct block_device *bdev; + size_t lba_start; + size_t num_blocks; +}; + struct block_device { char *name; struct block_device_ops ops; @@ -21,14 +28,15 @@ struct block_device { 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_part *partitions; + size_t partition_count; }; 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_readp(struct block_part *part, 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_writep(struct block_part *part, void *buf, size_t offset, size_t size); extern struct block_device *block_devices[]; extern size_t block_devices_count; diff --git a/include/kernel/gpt.h b/include/kernel/gpt.h index 4ac3159..ba846cd 100644 --- a/include/kernel/gpt.h +++ b/include/kernel/gpt.h @@ -38,12 +38,12 @@ struct gpt_header { /* GPT LBA 2 */ struct gpt_entry { - uint8_t guid[16]; - uint8_t guid_parent[16]; + uint64_t type_guid[2]; + uint64_t unique_guid[2]; uint64_t lba_first; uint64_t lba_last; - uint8_t attr[8]; - char name[72]; + uint64_t flags; + uint8_t name[72]; } PACKED; void block_gpt_init(struct block_device *dev); diff --git a/include/kernel/slab.h b/include/kernel/slab.h index 240b895..c8f91c6 100644 --- a/include/kernel/slab.h +++ b/include/kernel/slab.h @@ -11,7 +11,7 @@ #define ALLOC_KERN 0x01 #define ALLOC_DMA 0x02 -struct slab { +typedef struct _slab { size_t size; /* size of each object */ size_t tsize; /* total size of this slab */ size_t num; /* number of objects in this slab */ @@ -19,20 +19,20 @@ struct slab { uint64_t flags; size_t align; uintptr_t *nextfree; - struct slab *next; - struct slab *prev; -}; + struct _slab *next; + struct _slab *prev; +} slab_t; struct kmap_entry { uintptr_t *ptr; size_t size; - struct slab *slab; + slab_t *slab; struct kmap_entry *next; }; -struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags); -void *slab_alloc(struct slab *slab); -void slab_free(struct slab *slab, void *ptr); +slab_t *slab_create(size_t size, size_t cache_size, uint64_t flags); +void *slab_alloc(slab_t *slab); +void slab_free(slab_t *slab, void *ptr); void kmalloc_init(); void *kmalloc(size_t size, uint64_t flags); diff --git a/lib/rbtree.c b/lib/rbtree.c index 3c02117..351adc9 100644 --- a/lib/rbtree.c +++ b/lib/rbtree.c @@ -4,7 +4,7 @@ #include #include -static struct slab *rbt_slab; +static slab_t *rbt_slab; void rbt_slab_init() { diff --git a/lib/stdio.c b/lib/stdio.c index 2753816..679b092 100644 --- a/lib/stdio.c +++ b/lib/stdio.c @@ -134,7 +134,7 @@ int snprintf(char *str, const char *restrict fmt, size_t size, ...) int kprintf(const char *restrict fmt, ...) { - char buf[1024] = { 0 }; + char buf[2048] = { 0 }; va_list args; va_start(args, fmt); int ret = vsnprintf(buf, fmt, 1024, args); diff --git a/mem/buddy.c b/mem/buddy.c index 1a18429..bc3ad2d 100644 --- a/mem/buddy.c +++ b/mem/buddy.c @@ -27,7 +27,7 @@ struct page kdefault_attrs; paddr_t kcr3 = 0; static void *(*alloc_page)(void); -static struct slab *kmap_slab; +static slab_t *kmap_slab; static void buddy_bitmap_set(char *bitmap, size_t depth, size_t n, int val) { diff --git a/mem/slab.c b/mem/slab.c index 4395f24..85be433 100644 --- a/mem/slab.c +++ b/mem/slab.c @@ -7,13 +7,13 @@ #define SLAB_ALIGN 7 static size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 }; -static struct slab *slab_cache[ARRAY_SIZE(slab_sizes)]; +static slab_t *slab_cache[ARRAY_SIZE(slab_sizes)]; static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000 }; -static struct slab *slab_cache_dma[ARRAY_SIZE(slab_sizes)]; +static slab_t *slab_cache_dma[ARRAY_SIZE(slab_sizes)]; static struct rbtree kmalloc_tree = { NULL }; -static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t *o_end) +static inline void slab_range(slab_t *slab, uintptr_t *o_start, uintptr_t *o_end) { uintptr_t sslab = (uintptr_t)slab; sslab = (sslab | slab->align) + 1; @@ -22,12 +22,12 @@ static inline void slab_range(struct slab *slab, uintptr_t *o_start, uintptr_t * *o_end = sslab + (slab->size * slab->num); } -struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags) +slab_t *slab_create(size_t size, size_t cache_size, uint64_t flags) { if (size < sizeof(uintptr_t)) return NULL; - struct slab *ret = buddy_alloc(cache_size); + slab_t *ret = buddy_alloc(cache_size); if (ret == NULL) return NULL; @@ -36,7 +36,7 @@ struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags) /* slab area start position */ uintptr_t aret = (uintptr_t)ret; - uintptr_t start = (uintptr_t)ret + sizeof(struct slab); + uintptr_t start = (uintptr_t)ret + sizeof(slab_t); if (flags & SLAB_PAGE_ALIGN) { ret->align = 0xFFF; @@ -72,7 +72,7 @@ struct slab *slab_create(size_t size, size_t cache_size, uint64_t flags) return ret; } -void *slab_alloc(struct slab *slab) +void *slab_alloc(slab_t *slab) { if (slab == NULL) return NULL; @@ -94,7 +94,7 @@ void *slab_alloc(struct slab *slab) return ret; } -void slab_free(struct slab *slab, void *ptr) +void slab_free(slab_t *slab, void *ptr) { if (slab == NULL) return; @@ -118,8 +118,8 @@ void slab_free(struct slab *slab, void *ptr) /* delete the slab if it's empty and not a root slab */ if (slab->free == slab->num && slab->prev != NULL) { - struct slab *next = slab->next; - struct slab *prev = slab->prev; + slab_t *next = slab->next; + slab_t *prev = slab->prev; prev->next = next; if (next != NULL) @@ -143,9 +143,9 @@ void kmalloc_init() slab_cache_dma[i] = slab_create(dma_sizes[i], 8 * MB, SLAB_DMA_64K); } -static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, struct slab **cache, bool fb_disable) +static void *kmalloc_table(size_t size, size_t *sizes, size_t nsizes, slab_t **cache, bool fb_disable) { - struct slab *slab = NULL; + slab_t *slab = NULL; void *ret = NULL; if (size > sizes[nsizes - 1] && !fb_disable) { ret = buddy_alloc(npow2(size)); @@ -210,7 +210,7 @@ void kfree(void *ptr) return; } - struct slab *slab = (struct slab *)n->value; + slab_t *slab = (slab_t *)n->value; if (slab != NULL) slab_free(slab, ptr); else