From 0eb0f4541ac0418be01994520c42ba7b118910cc Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 10:39:35 -0400 Subject: [PATCH 01/22] fs_init: change to dirent Signed-off-by: Michael Shaw --- fs/vfs/ramfs.c | 2 +- include/fs/vfs.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index 61433ad..5811f07 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -209,7 +209,7 @@ static ssize_t get_dents(inode_t *parent, dirent_t **dents) return n; } -static int init(int flags, inode_t *inode, fs_t **out) +static int init(int flags, dirent_t *to_mount, fs_t **out) { return ramfs_create(flags, out); } diff --git a/include/fs/vfs.h b/include/fs/vfs.h index e8a2062..8003a82 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -55,7 +55,7 @@ struct fs_driver { char name[FILE_NAME_MAX_LEN + 1]; - int (*init)(int flags, inode_t *inode, fs_t **out); + int (*init)(int flags, dirent_t *to_mount, fs_t **out); int (*deinit)(fs_t *fs); }; From d2c698ac0a4ee40dc8f215de9606797b36463dbe Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 10:47:58 -0400 Subject: [PATCH 02/22] vfs: add dirent `parent` pointer Signed-off-by: Michael Shaw --- fs/vfs/initcpio.c | 1 + fs/vfs/inode.c | 19 +++++++++++++------ fs/vfs/mount.c | 2 +- fs/vfs/ramfs.c | 1 + fs/vfs/vfs.c | 7 ++++--- include/fs/inode.h | 7 ++++--- include/fs/vfs.h | 1 + 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/fs/vfs/initcpio.c b/fs/vfs/initcpio.c index f6ff845..538f876 100644 --- a/fs/vfs/initcpio.c +++ b/fs/vfs/initcpio.c @@ -103,6 +103,7 @@ static ssize_t read_next(fs_t *fs, const char *file, rbtree_t *list, dirent_t ** write_unlock(&parent->inode->rwlock); if (res == 0) { new_dent->fs = parent->fs; + new_dent->parent = parent; *out = new_dent; } else { inode_dec_ref(existing_ino); diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index 878e893..b9458f2 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -11,8 +11,10 @@ static mem_cache_t *inode_slab = NULL; static mem_cache_t *dirent_slab = NULL; -int inode_populate_dirents(inode_t *inode) +int dirent_populate_children(dirent_t *dir) { + inode_t *inode = dir->inode; + write_lock(&inode->rwlock); if (inode->dirents.head != NULL) { @@ -34,6 +36,8 @@ int inode_populate_dirents(inode_t *inode) if (!insert) goto cleanup; + insert->parent = dir; + if (!list_insert(&inode->dirents, insert)) { dirent_free(insert); goto cleanup; @@ -62,9 +66,9 @@ cleanup: return -ENOMEM; } -int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out) +int dirent_lookup_locked(dirent_t *dir, const char *name, dirent_t **out) { - list_item_t *item = inode->dirents.head; + list_item_t *item = dir->inode->dirents.head; while (item) { dirent_t *dent = item->ptr; if (!strcmp(dent->name, name)) { @@ -77,8 +81,10 @@ int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out) return -ENOENT; } -int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out) +int dirent_lookup(dirent_t *dir, const char *name, dirent_t **out) { + inode_t *inode = dir->inode; + if ((inode->mode & S_IFMT) != S_IFDIR) return -ENOTDIR; @@ -86,13 +92,13 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out) if (inode->dirents.head == NULL) { read_unlock(&inode->rwlock); - int res = inode_populate_dirents(inode); + int res = dirent_populate_children(dir); if (res < 0) return res; read_lock(&inode->rwlock); } - int ret = inode_find_dirent_locked(inode, name, out); + int ret = dirent_lookup_locked(dir, name, out); read_unlock(&inode->rwlock); return ret; } @@ -203,6 +209,7 @@ dirent_t *dirent_dup(dirent_t *src) ret->flags = src->flags; ret->type = src->type; ret->fs = src->fs; + ret->parent = src->parent; if (src->inode) { inode_inc_ref(src->inode); diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c index d39f6d2..dc99c22 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -96,7 +96,7 @@ static int mount_traverse(const char *path, struct mount **out_mount, dirent_t * bool is_last = (*p == '\0'); dirent_t *found = NULL; - int res = inode_find_dirent(cur_dent->inode, name, &found); + int res = dirent_lookup(cur_dent, name, &found); if (res < 0) { dirent_dec_ref(cur_dent); return res; diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index 5811f07..b96ebf3 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -46,6 +46,7 @@ int ramfs_create(u64 flags, fs_t **out) root->type = S_IFDIR; root->fs = fs; root->inode = root_inode; + root->parent = NULL; *out = fs; return 0; diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 8a2faad..d01a853 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -53,7 +53,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) if (c == '/' || c == '\0') { dirent_t *dirent; - res = inode_find_dirent(dent->inode, build, &dirent); + res = dirent_lookup(dent, build, &dirent); kfree(build); if (res < 0) return -ENOENT; @@ -174,7 +174,7 @@ int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out) if ((ino->mode & S_IFMT) != S_IFDIR) return -ENOTDIR; - int res = inode_populate_dirents(ino); + int res = dirent_populate_children(dir); if (res < 0) return res; @@ -244,7 +244,7 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t } dirent_t *test = NULL; - int res = inode_find_dirent_locked(ino, name, &test); + int res = dirent_lookup_locked(parent, name, &test); if (res == 0) { dirent_dec_ref(test); write_unlock(&ino->rwlock); @@ -273,6 +273,7 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t } new_dirent->fs = fs; + new_dirent->parent = parent; *out = new_dirent; write_unlock(&ino->rwlock); return 0; diff --git a/include/fs/inode.h b/include/fs/inode.h index 7117437..abf220d 100644 --- a/include/fs/inode.h +++ b/include/fs/inode.h @@ -40,6 +40,7 @@ struct _inode { struct _dirent { fs_t *fs; inode_t *inode; + dirent_t *parent; u64 ino_num; @@ -61,9 +62,9 @@ void dirent_free(dirent_t *dirent); void dirent_inc_ref(dirent_t *dent); void dirent_dec_ref(dirent_t *dent); -int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out); -int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out); -int inode_populate_dirents(inode_t *inode); +int dirent_lookup(dirent_t *dir, const char *name, dirent_t **out); +int dirent_lookup_locked(dirent_t *dir, const char *name, dirent_t **out); +int dirent_populate_children(dirent_t *dir); inode_t *inode_create(void); void inode_free(inode_t *inode); void inode_inc_ref(inode_t *inode); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 8003a82..60e9b24 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -70,6 +70,7 @@ struct _fs { u64 flags; size_t block_size; size_t n_blocks; + void *priv; dirent_t *root_dirent; From 07ea385f961f4ad1ff7fd2f83c731b03c498fad9 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 11:00:39 -0400 Subject: [PATCH 03/22] fs_init: change to_mount to block_device Signed-off-by: Michael Shaw --- fs/fat32/fat32.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++ fs/fat32/inode.c | 2 ++ fs/fat32/inode.h | 7 +++++++ fs/vfs/initcpio.c | 1 + fs/vfs/inode.c | 7 +++++++ fs/vfs/ramfs.c | 2 +- fs/vfs/vfs.c | 1 + include/fs/vfs.h | 3 ++- 8 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 fs/fat32/fat32.c create mode 100644 fs/fat32/inode.c create mode 100644 fs/fat32/inode.h diff --git a/fs/fat32/fat32.c b/fs/fat32/fat32.c new file mode 100644 index 0000000..c86d235 --- /dev/null +++ b/fs/fat32/fat32.c @@ -0,0 +1,52 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include + +#include + +#include + +#include "inode.h" + +static struct fs_driver fat32_driver = {0}; + +static int alloc_inode(fs_t *fs, u64 mode, inode_t **out) +{ + return -ENOSYS; +} + +static int free_inode(fs_t *fs, inode_t *inode) +{ + return -ENOSYS; +} + +static int open_inode(fs_t *fs, u64 id, dirent_t *dirent, inode_t **out) +{ + return -ENOSYS; +} + +static int fs_init(int flags, struct block_device *bdev, fs_t **out) +{ + fs_t *fs = kalloc(sizeof(fs_t), ALLOC_KERN); + if (!fs) + return -ENOMEM; + *out = fs; + + fs->fs_ops.alloc_inode = alloc_inode; + fs->fs_ops.free_inode = free_inode; + fs->fs_ops.open_inode = open_inode; + + return 0; +} + +static int fs_deinit(fs_t *fs) +{ + return -EINVAL; +} + +void fat32_init() +{ + strcpy(fat32_driver.name, "fat32"); + fat32_driver.init = fs_init; + fat32_driver.deinit = fs_deinit; +} +DRIVER_ENTRY(fat32_init); diff --git a/fs/fat32/inode.c b/fs/fat32/inode.c new file mode 100644 index 0000000..1a19d3a --- /dev/null +++ b/fs/fat32/inode.c @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + diff --git a/fs/fat32/inode.h b/fs/fat32/inode.h new file mode 100644 index 0000000..9ebbeab --- /dev/null +++ b/fs/fat32/inode.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _INODE_H_ +#define _INODE_H_ + + + +#endif /* _INODE_H_ */ diff --git a/fs/vfs/initcpio.c b/fs/vfs/initcpio.c index 538f876..11409fd 100644 --- a/fs/vfs/initcpio.c +++ b/fs/vfs/initcpio.c @@ -104,6 +104,7 @@ static ssize_t read_next(fs_t *fs, const char *file, rbtree_t *list, dirent_t ** if (res == 0) { new_dent->fs = parent->fs; new_dent->parent = parent; + dirent_inc_ref(parent); *out = new_dent; } else { inode_dec_ref(existing_ino); diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index b9458f2..051bd75 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -37,6 +37,7 @@ int dirent_populate_children(dirent_t *dir) goto cleanup; insert->parent = dir; + dirent_inc_ref(dir); if (!list_insert(&inode->dirents, insert)) { dirent_free(insert); @@ -211,6 +212,9 @@ dirent_t *dirent_dup(dirent_t *src) ret->fs = src->fs; ret->parent = src->parent; + if (ret->parent) + dirent_inc_ref(ret->parent); + if (src->inode) { inode_inc_ref(src->inode); ret->inode = src->inode; @@ -221,6 +225,9 @@ dirent_t *dirent_dup(dirent_t *src) void dirent_free(dirent_t *dirent) { + if (dirent->parent) + dirent_dec_ref(dirent->parent); + kfree(dirent->name); slab_free(dirent_slab, dirent); } diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index b96ebf3..b7276c9 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -210,7 +210,7 @@ static ssize_t get_dents(inode_t *parent, dirent_t **dents) return n; } -static int init(int flags, dirent_t *to_mount, fs_t **out) +static int init(int flags, struct block_device *to_mount, fs_t **out) { return ramfs_create(flags, out); } diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index d01a853..e58c024 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -274,6 +274,7 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t new_dirent->fs = fs; new_dirent->parent = parent; + dirent_inc_ref(parent); *out = new_dirent; write_unlock(&ino->rwlock); return 0; diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 60e9b24..3dd06ae 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -55,7 +56,7 @@ struct fs_driver { char name[FILE_NAME_MAX_LEN + 1]; - int (*init)(int flags, dirent_t *to_mount, fs_t **out); + int (*init)(int flags, struct block_device *to_mount, fs_t **out); int (*deinit)(fs_t *fs); }; From a53141809ef849757f5e3822c94529ecfb01f568 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 11:02:43 -0400 Subject: [PATCH 04/22] fat32: add fat32 skeleton driver Signed-off-by: Michael Shaw --- fs/Makefile | 1 + fs/fat32/Makefile | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 fs/fat32/Makefile diff --git a/fs/Makefile b/fs/Makefile index 4336d21..12b6915 100644 --- a/fs/Makefile +++ b/fs/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += vfs/ +obj-y += fat32/ diff --git a/fs/fat32/Makefile b/fs/fat32/Makefile new file mode 100644 index 0000000..99c912b --- /dev/null +++ b/fs/fat32/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-y += fat32.o +obj-y += inode.o From c4ec0bdf21185a9985b71d1048b57b6dc66dedc3 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 11:05:30 -0400 Subject: [PATCH 05/22] tty: move tty/ from drivers/ to kernel/ Signed-off-by: Michael Shaw --- amd64/amd64_init.c | 2 +- amd64/drivers/serial.c | 2 +- drivers/Makefile | 1 - drivers/tty/Makefile | 4 -- drivers/tty/tty.c | 49 ------------------------ drivers/tty/vt100.c | 87 ------------------------------------------ fs/vfs/devfs.c | 2 +- include/drivers/tty.h | 36 ----------------- include/fs/devfs.h | 2 +- init/main.c | 2 +- kernel/Makefile | 1 + kernel/device.c | 2 +- lib/printf.c | 2 +- 13 files changed, 8 insertions(+), 184 deletions(-) delete mode 100644 drivers/tty/Makefile delete mode 100644 drivers/tty/tty.c delete mode 100644 drivers/tty/vt100.c delete mode 100644 include/drivers/tty.h diff --git a/amd64/amd64_init.c b/amd64/amd64_init.c index fd8a3e6..c709233 100644 --- a/amd64/amd64_init.c +++ b/amd64/amd64_init.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include -#include +#include #include #include #include "arch/reg.h" diff --git a/amd64/drivers/serial.c b/amd64/drivers/serial.c index 38dd216..9c761a3 100644 --- a/amd64/drivers/serial.c +++ b/amd64/drivers/serial.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include diff --git a/drivers/Makefile b/drivers/Makefile index 92e45d1..e76e034 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -1,5 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += acpi/ obj-y += pci/ -obj-y += tty/ #obj-y += ahci.o diff --git a/drivers/tty/Makefile b/drivers/tty/Makefile deleted file mode 100644 index 7b44b69..0000000 --- a/drivers/tty/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-only -obj-y += tty.o -obj-y += vt100.o - diff --git a/drivers/tty/tty.c b/drivers/tty/tty.c deleted file mode 100644 index 1c3d21e..0000000 --- a/drivers/tty/tty.c +++ /dev/null @@ -1,49 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include -#include -#include -#include -#include - -static mem_cache_t *tty_slab; - -static struct tty_serial_ops arch_serial_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write}; - -struct tty *tty_create(const char *name, void *priv) -{ - struct tty *tty = slab_alloc(tty_slab); - - dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name); - - tty->dev = tty_device; - tty->priv = priv; - tty->serial_ops = arch_serial_ops; - - return tty; -} - -void tty_putchar(struct tty *tty, char c) -{ - tty->serial_ops.serial_putchar(tty, c); -} - -char tty_getchar(struct tty *tty) -{ - return tty->serial_ops.serial_getchar(tty); -} - -void tty_write(struct tty *tty, const char *s) -{ - char c; - while ((c = *s++)) - tty_putchar(tty, c); -} - -void tty_layer_init() -{ - tty_slab = slab_create("tty_layer", sizeof(struct tty), 256 * KB, 8); - - if (!tty_slab) { - panic("tty_slab alloc failed\n"); - } -} diff --git a/drivers/tty/vt100.c b/drivers/tty/vt100.c deleted file mode 100644 index 8598a41..0000000 --- a/drivers/tty/vt100.c +++ /dev/null @@ -1,87 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include -#include -#include - -void tty_send_dsr(struct tty *tty) -{ - tty_write(tty, "\033[6n"); -} - -void tty_set_origin_mode(struct tty *tty) -{ - tty_write(tty, "\033[?6h"); -} - -void tty_clear_screen(struct tty *tty) -{ - tty_write(tty, "\033[2J"); -} - -void tty_set_cursor_pos(struct tty *tty, int r, int c) -{ - if (r > 999) - r = 999; - if (c > 999) - c = 999; - - char str[12] = { 0 }; - snprintf(str, 11, "\033[%d;%dH", r, c); - tty_write(tty, str); -} - -void tty_get_cursor_pos(struct tty *tty, int *row, int *col) -{ - tty_send_dsr(tty); - - char c; - int n = 0; - - char ro[128] = { 0 }; - char co[128] = { 0 }; - - bool sem = false; - bool begin = false; - - while ((c = tty_getchar(tty))) { - if (c == '\033') - continue; - - if (c == ';') { - sem = true; - n = 0; - } - - if (!begin) { - if (c == '[') - begin = true; - continue; - } - - if (c >= '0' && c <= '9') { - if (!sem) - ro[n] = c; - else - co[n] = c; - - n++; - } - - if (c == 'R') - break; - } - - *row = atoi(ro); - *col = atoi(co); -} - -void tty_get_size(struct tty *tty, int *rows, int *cols) -{ - tty_set_cursor_pos(tty, 999, 999); - tty_get_cursor_pos(tty, rows, cols); -} - -void tty_setlf(struct tty *tty) -{ - tty_write(tty, "\033[20l"); -} diff --git a/fs/vfs/devfs.c b/fs/vfs/devfs.c index 50f61d5..961b297 100644 --- a/fs/vfs/devfs.c +++ b/fs/vfs/devfs.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include -#include +#include #include #include diff --git a/include/drivers/tty.h b/include/drivers/tty.h deleted file mode 100644 index 33f74c4..0000000 --- a/include/drivers/tty.h +++ /dev/null @@ -1,36 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _TTY_H_ -#define _TTY_H_ - -#include - -struct tty_serial_ops { - void (*serial_putchar)(struct tty *tty, char c); - char (*serial_getchar)(struct tty *tty); -}; - -struct tty { - dev_t dev; - void *priv; - struct tty_serial_ops serial_ops; -}; - -void tty_layer_init(); - -struct tty *tty_create(const char *name, void *priv); -void tty_putchar(struct tty *tty, char c); -char tty_getchar(struct tty *tty); -void tty_write(struct tty *tty, const char *s); - -void arch_serial_write(struct tty *tty, char c); -char arch_serial_read(struct tty *tty); - -void tty_send_dsr(struct tty *tty); -void tty_set_origin_mode(struct tty *tty); -void tty_clear_screen(struct tty *tty); -void tty_set_cursor_pos(struct tty *tty, int x, int y); -void tty_get_cursor_pos(struct tty *tty, int *row, int *col); -void tty_get_size(struct tty *tty, int *rows, int *cols); -void tty_setlf(struct tty *tty); - -#endif /* _TTY_H_ */ diff --git a/include/fs/devfs.h b/include/fs/devfs.h index 7b18283..1e60201 100644 --- a/include/fs/devfs.h +++ b/include/fs/devfs.h @@ -4,7 +4,7 @@ #include -#include +#include #include diff --git a/init/main.c b/init/main.c index 80c855a..030188d 100644 --- a/init/main.c +++ b/init/main.c @@ -15,7 +15,7 @@ #include -#include +#include #include #include #include diff --git a/kernel/Makefile b/kernel/Makefile index af3538b..5dafe9b 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += device.o obj-y += block.o +obj-y += tty/ diff --git a/kernel/device.c b/kernel/device.c index b3fc145..868e41a 100644 --- a/kernel/device.c +++ b/kernel/device.c @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include +#include #include #include #include diff --git a/lib/printf.c b/lib/printf.c index e4c5950..f7150e8 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -39,7 +39,7 @@ #include #include -#include +#include // define this globally (e.g. gcc -DPRINTF_INCLUDE_CONFIG_H ...) to include the // printf_config.h header file From fa34746870e98d0d8e255b52582528f41ef31f26 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 14:56:15 -0400 Subject: [PATCH 06/22] errno: add EROFS Signed-off-by: Michael Shaw --- include/lib/errno.h | 1 + lib/errno.c | 1 + 2 files changed, 2 insertions(+) diff --git a/include/lib/errno.h b/include/lib/errno.h index 6517e2f..dfe07e2 100644 --- a/include/lib/errno.h +++ b/include/lib/errno.h @@ -18,6 +18,7 @@ #define ENOSYS 13 #define ESYSCALLBLK 14 #define ENAMETOOLONG 15 +#define EROFS 16 #ifndef __ASM__ diff --git a/lib/errno.c b/lib/errno.c index eee5421..2542ead 100644 --- a/lib/errno.c +++ b/lib/errno.c @@ -18,6 +18,7 @@ static const char *errno_str[] = { [EBUSY] = "Device or resource busy", [ENOSYS] = "Function not implemented", [ENAMETOOLONG] = "Name too long", + [EROFS] = "Read-only file system", }; const char *strerror(int errnum) From f46281b3056defeae6f1a8ba9f5d81a4269f3561 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 14:56:58 -0400 Subject: [PATCH 07/22] kernel: add tty Signed-off-by: Michael Shaw --- include/kernel/tty.h | 36 ++++++++++++++++++ kernel/tty/Makefile | 4 ++ kernel/tty/tty.c | 49 +++++++++++++++++++++++++ kernel/tty/vt100.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 include/kernel/tty.h create mode 100644 kernel/tty/Makefile create mode 100644 kernel/tty/tty.c create mode 100644 kernel/tty/vt100.c diff --git a/include/kernel/tty.h b/include/kernel/tty.h new file mode 100644 index 0000000..33f74c4 --- /dev/null +++ b/include/kernel/tty.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TTY_H_ +#define _TTY_H_ + +#include + +struct tty_serial_ops { + void (*serial_putchar)(struct tty *tty, char c); + char (*serial_getchar)(struct tty *tty); +}; + +struct tty { + dev_t dev; + void *priv; + struct tty_serial_ops serial_ops; +}; + +void tty_layer_init(); + +struct tty *tty_create(const char *name, void *priv); +void tty_putchar(struct tty *tty, char c); +char tty_getchar(struct tty *tty); +void tty_write(struct tty *tty, const char *s); + +void arch_serial_write(struct tty *tty, char c); +char arch_serial_read(struct tty *tty); + +void tty_send_dsr(struct tty *tty); +void tty_set_origin_mode(struct tty *tty); +void tty_clear_screen(struct tty *tty); +void tty_set_cursor_pos(struct tty *tty, int x, int y); +void tty_get_cursor_pos(struct tty *tty, int *row, int *col); +void tty_get_size(struct tty *tty, int *rows, int *cols); +void tty_setlf(struct tty *tty); + +#endif /* _TTY_H_ */ diff --git a/kernel/tty/Makefile b/kernel/tty/Makefile new file mode 100644 index 0000000..7b44b69 --- /dev/null +++ b/kernel/tty/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-y += tty.o +obj-y += vt100.o + diff --git a/kernel/tty/tty.c b/kernel/tty/tty.c new file mode 100644 index 0000000..fd3c840 --- /dev/null +++ b/kernel/tty/tty.c @@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include + +static mem_cache_t *tty_slab; + +static struct tty_serial_ops arch_serial_ops = {.serial_getchar = arch_serial_read, .serial_putchar = arch_serial_write}; + +struct tty *tty_create(const char *name, void *priv) +{ + struct tty *tty = slab_alloc(tty_slab); + + dev_t tty_device = device_create(tty, DEV_CLASS_TTY, name); + + tty->dev = tty_device; + tty->priv = priv; + tty->serial_ops = arch_serial_ops; + + return tty; +} + +void tty_putchar(struct tty *tty, char c) +{ + tty->serial_ops.serial_putchar(tty, c); +} + +char tty_getchar(struct tty *tty) +{ + return tty->serial_ops.serial_getchar(tty); +} + +void tty_write(struct tty *tty, const char *s) +{ + char c; + while ((c = *s++)) + tty_putchar(tty, c); +} + +void tty_layer_init() +{ + tty_slab = slab_create("tty_layer", sizeof(struct tty), 256 * KB, 8); + + if (!tty_slab) { + panic("tty_slab alloc failed\n"); + } +} diff --git a/kernel/tty/vt100.c b/kernel/tty/vt100.c new file mode 100644 index 0000000..d09399e --- /dev/null +++ b/kernel/tty/vt100.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +void tty_send_dsr(struct tty *tty) +{ + tty_write(tty, "\033[6n"); +} + +void tty_set_origin_mode(struct tty *tty) +{ + tty_write(tty, "\033[?6h"); +} + +void tty_clear_screen(struct tty *tty) +{ + tty_write(tty, "\033[2J"); +} + +void tty_set_cursor_pos(struct tty *tty, int r, int c) +{ + if (r > 999) + r = 999; + if (c > 999) + c = 999; + + char str[12] = { 0 }; + snprintf(str, 11, "\033[%d;%dH", r, c); + tty_write(tty, str); +} + +void tty_get_cursor_pos(struct tty *tty, int *row, int *col) +{ + tty_send_dsr(tty); + + char c; + int n = 0; + + char ro[128] = { 0 }; + char co[128] = { 0 }; + + bool sem = false; + bool begin = false; + + while ((c = tty_getchar(tty))) { + if (c == '\033') + continue; + + if (c == ';') { + sem = true; + n = 0; + } + + if (!begin) { + if (c == '[') + begin = true; + continue; + } + + if (c >= '0' && c <= '9') { + if (!sem) + ro[n] = c; + else + co[n] = c; + + n++; + } + + if (c == 'R') + break; + } + + *row = atoi(ro); + *col = atoi(co); +} + +void tty_get_size(struct tty *tty, int *rows, int *cols) +{ + tty_set_cursor_pos(tty, 999, 999); + tty_get_cursor_pos(tty, rows, cols); +} + +void tty_setlf(struct tty *tty) +{ + tty_write(tty, "\033[20l"); +} From 334bff74bc22d0443cf24d24f451753ca8d6db7b Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 15:58:04 -0400 Subject: [PATCH 08/22] block: clarify parameter name Signed-off-by: Michael Shaw --- fs/vfs/devfs.c | 8 ++++---- include/kernel/block.h | 4 ++-- kernel/block.c | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/fs/vfs/devfs.c b/fs/vfs/devfs.c index 961b297..6b1aad2 100644 --- a/fs/vfs/devfs.c +++ b/fs/vfs/devfs.c @@ -26,14 +26,14 @@ static ssize_t i_write_chr(inode_t *inode, char *buf, size_t size, size_t offset return size; } -static ssize_t i_read_blk(inode_t *inode, char *buf, size_t size, size_t offset) +static ssize_t i_read_blk(inode_t *inode, char *buf, size_t n_blocks, size_t offset) { - return block_read(inode->data, buf, offset, size); + return block_read(inode->data, buf, offset, n_blocks); } -static ssize_t i_write_blk(inode_t *inode, char *buf, size_t size, size_t offset) +static ssize_t i_write_blk(inode_t *inode, char *buf, size_t n_blocks, size_t offset) { - return block_write(inode->data, buf, offset, size); + return block_write(inode->data, buf, offset, n_blocks); } static int devfs_insert_device(const char *name, u64 mode, dirent_t **out) diff --git a/include/kernel/block.h b/include/kernel/block.h index 5589433..3240fe8 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -18,7 +18,7 @@ struct block_device { struct block_ops ops; }; -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); +ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); +ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); #endif /* _BLOCK_H_ */ diff --git a/kernel/block.c b/kernel/block.c index 401b8de..85e05ec 100644 --- a/kernel/block.c +++ b/kernel/block.c @@ -2,12 +2,12 @@ #include #include -ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n) +ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks) { - return dev->ops.read(dev, buf, offset, n); + return dev->ops.read(dev, buf, offset, n_blocks); } -ssize_t block_write(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) { - return dev->ops.write(dev, buf, offset, n); + return dev->ops.write(dev, buf, offset, n_blocks); } From 7aed4b6579b93b69efbcef8e45f948954bae2012 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 16:01:53 -0400 Subject: [PATCH 09/22] fat32: add more stubs Signed-off-by: Michael Shaw --- fs/fat32/fat32.c | 25 ++++++++++-------- fs/fat32/inode.c | 44 +++++++++++++++++++++++++++++++ fs/fat32/inode.h | 10 ++++--- include/fs/fat32.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 include/fs/fat32.h diff --git a/fs/fat32/fat32.c b/fs/fat32/fat32.c index c86d235..4e9d71a 100644 --- a/fs/fat32/fat32.c +++ b/fs/fat32/fat32.c @@ -1,7 +1,10 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include +#include +#include #include +#include #include @@ -26,21 +29,16 @@ static int open_inode(fs_t *fs, u64 id, dirent_t *dirent, inode_t **out) static int fs_init(int flags, struct block_device *bdev, fs_t **out) { - fs_t *fs = kalloc(sizeof(fs_t), ALLOC_KERN); - if (!fs) - return -ENOMEM; - *out = fs; - - fs->fs_ops.alloc_inode = alloc_inode; - fs->fs_ops.free_inode = free_inode; - fs->fs_ops.open_inode = open_inode; - - return 0; + return fat32_create(bdev, flags, out); } static int fs_deinit(fs_t *fs) { - return -EINVAL; + return -ENOSYS; +} + +int fat32_create(struct block_device *bdev, u64 flags, fs_t **out) +{ } void fat32_init() @@ -48,5 +46,10 @@ void fat32_init() strcpy(fat32_driver.name, "fat32"); fat32_driver.init = fs_init; fat32_driver.deinit = fs_deinit; + + void fat32_inode_ops_init(void); + fat32_inode_ops_init(); + + vfs_register(&fat32_driver); } DRIVER_ENTRY(fat32_init); diff --git a/fs/fat32/inode.c b/fs/fat32/inode.c index 1a19d3a..7240848 100644 --- a/fs/fat32/inode.c +++ b/fs/fat32/inode.c @@ -1,2 +1,46 @@ /* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include + +#include + +#include "inode.h" + +struct inode_ops fat32_inode_ops = {0}; + +static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset) +{ + return -ENOSYS; +} + +static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset) +{ + return -ENOSYS; +} + +static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out) +{ + return -ENOSYS; +} + +static int unlink(inode_t *parent_dir, const char *name) +{ + return -ENOSYS; +} + +static ssize_t get_dents(inode_t *parent, dirent_t **dents) +{ + return -ENOSYS; +} + +void fat32_inode_ops_init(void) +{ + fat32_inode_ops.read = read; + fat32_inode_ops.write = write; + fat32_inode_ops.link = link; + fat32_inode_ops.unlink = unlink; + fat32_inode_ops.get_dents = get_dents; +} diff --git a/fs/fat32/inode.h b/fs/fat32/inode.h index 9ebbeab..ddb3ca6 100644 --- a/fs/fat32/inode.h +++ b/fs/fat32/inode.h @@ -1,7 +1,11 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _INODE_H_ -#define _INODE_H_ +#ifndef _F32_INODE_H_ +#define _F32_INODE_H_ +#include +#include +#include +#include -#endif /* _INODE_H_ */ +#endif /* _F32_INODE_H_ */ diff --git a/include/fs/fat32.h b/include/fs/fat32.h new file mode 100644 index 0000000..e4c2ead --- /dev/null +++ b/include/fs/fat32.h @@ -0,0 +1,65 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _FAT32_H_ +#define _FAT32_H_ + +#include +#include + +#define FS_TYPE_FAT32 0x46415433 /* "FAT3" */ + +struct fat_bpb { + char padding[3]; + char oem[8]; + u16 bytes_per_sec; + u8 secs_per_clr; + u16 n_reserved_secs; + u8 n_fats; + u16 n_root_dirents; + u16 n_sectors; + u8 mdt; + u16 ignore; + u16 secs_per_trk; + u16 n_nheads; + u32 n_hidden; + u32 n_sectors_long; +} PACKED; + +struct f32_ebr { + struct fat_bpb bpb; + u32 secs_per_fat; + u16 flags; + u16 fat_version; + u32 root_clr_nio; + u16 fsinfo_sec_no; + u16 backup_boot_sec_no; + char reserved[12]; + u8 drive_no; + u8 reserved1; + u8 signature; + u32 serial_no; + char vol_label[11]; + char sys_id[8]; + char boot_code[420]; + char boot_signature[2]; +} PACKED; + +struct f32_fsinfo { + u32 signature; /* 0x41615252 */ + char reserved[480]; + u32 signature2; /* 0x61417272 */ + u32 last_free_clr_count; + u32 clr_search_start; + char reserved2[12]; + u32 trail_signature; /* 0xAA550000 */ +} PACKED; + +struct f32_sb { + struct f32_ebr *ebr; + struct f32_fsinfo *info; + struct block_device *bdev; +}; + +int fat32_create(struct block_device *bdev, u64 flags, fs_t **out); +void fat32_init(void); + +#endif /* _FAT32_H_ */ From fc0720b118b3d0681338b4bb749fab7ae2e6f5a8 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 13 Jul 2026 00:02:22 -0400 Subject: [PATCH 10/22] ahci: add block operations Signed-off-by: Michael Shaw --- drivers/Makefile | 2 +- drivers/ahci.c | 24 +++++++++++++++++++++--- include/fs/vfs.h | 3 ++- include/kernel/block.h | 17 +++++++++++++---- kernel/block.c | 23 +++++++++++++++++++++++ 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/drivers/Makefile b/drivers/Makefile index e76e034..c701d63 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += acpi/ obj-y += pci/ -#obj-y += ahci.o +obj-y += ahci.o diff --git a/drivers/ahci.c b/drivers/ahci.c index ed3f395..8d16645 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -5,10 +5,13 @@ #include #include +#include #include #include +#include + static uint8_t sata_device_count = 0; static struct sata_device sata_devices[32] = {0}; @@ -212,8 +215,18 @@ int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pa return true; } -void ahci_init_device(struct pci_device *device) +static ssize_t ahci_block_read(struct block_device *dev, char *buf, size_t off, size_t n_blocks) { + +} + +static int ahci_init_device(struct pci_device *device, size_t ahci_dev_no) +{ + struct block_device *bdev = kzalloc(sizeof(struct block_device), ALLOC_KERN); + if (!bdev) + return -ENOMEM; + + return 0; } void ahci_init() @@ -224,11 +237,16 @@ void ahci_init() size_t n = pci_find_device(0x0001, 0x0006, &dev_list); list_item_t *item = dev_list.head; + size_t i = 0; + while (item) { - ahci_init_device(item->ptr); + int res = ahci_init_device(item->ptr, i++); + if (res < 0) + kerr("ahci_init_device: %s\n", strerror(-res)); + item = item->next; } - krdy("AHCI ready\n"); + krdy("AHCI ready\n"); } DRIVER_ENTRY_PRIORITY(ahci_init, 3); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 3dd06ae..7914341 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -54,6 +53,8 @@ #define FS_RDONLY 0x00000001 +struct block_device; + struct fs_driver { char name[FILE_NAME_MAX_LEN + 1]; int (*init)(int flags, struct block_device *to_mount, fs_t **out); diff --git a/include/kernel/block.h b/include/kernel/block.h index 3240fe8..f1b35a1 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -2,6 +2,7 @@ #ifndef _BLOCK_H_ #define _BLOCK_H_ +#include #include struct block_device; @@ -11,11 +12,19 @@ struct block_ops { }; struct block_device { - char *name; - dev_t dev; - size_t size; - size_t block_size; + char name[256]; + dev_t dev; + size_t size; + size_t block_size; struct block_ops ops; + void *priv; + size_t lba_start; +}; + +struct disk { + struct block_device *dev; + char model[FILE_NAME_MAX_LEN + 1]; + list_t part_table; }; ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); diff --git a/kernel/block.c b/kernel/block.c index 85e05ec..856bb6e 100644 --- a/kernel/block.c +++ b/kernel/block.c @@ -2,6 +2,29 @@ #include #include +#include + +#include + +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); + if (!ret) + return -ENOMEM; + + ret->lba_start = lba_start; + + strncpy(ret->name, name, sizeof(ret->name) - 1); + *out = ret; + + memcpy(ops, &ret->ops, sizeof(struct block_ops)); + + dirent_t *dirent; + int res = devfs_insert_device_blk(ret->name, ret, &dirent); + + return 0; +} + ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks) { return dev->ops.read(dev, buf, offset, n_blocks); From 16b2e9de0a9b5d46e58c83ab9ab9aeef8802b380 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 13 Jul 2026 15:02:00 -0400 Subject: [PATCH 11/22] pci: change pci_find_device Signed-off-by: Michael Shaw --- drivers/pci/pci.c | 2 +- include/drivers/pci.h | 2 +- include/lib/endian.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 include/lib/endian.h diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 9fbcf9d..c6a18b7 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -52,7 +52,7 @@ void pci_config_write32(struct pci_host *host, u8 bus, u8 dev, u8 func, u16 reg, *addr = val; } -size_t pci_find_device(u16 minor, u16 major, list_t *out) +size_t pci_find_device(u16 major, u16 minor, list_t *out) { size_t n = 0; diff --git a/include/drivers/pci.h b/include/drivers/pci.h index 86c287e..951578f 100644 --- a/include/drivers/pci.h +++ b/include/drivers/pci.h @@ -36,6 +36,6 @@ struct pci_device { void pci_register_host(struct pci_host *host); u32 pci_config_read(struct pci_host *host, u32 bus, u32 slot, u32 func, u32 offset); void pci_config_write(struct pci_host *host, u32 bus, u32 slot, u32 func, u32 offset, u32 val); -size_t pci_find_device(u16 minor, u16 major, list_t *out); +size_t pci_find_device(u16 major, u16 minor, list_t *out); #endif /* _PCI_H_ */ diff --git a/include/lib/endian.h b/include/lib/endian.h new file mode 100644 index 0000000..2f28fc1 --- /dev/null +++ b/include/lib/endian.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ENDIAN_H_ +#define _ENDIAN_H_ + +#include + +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define htobe16(x) ((uint16_t)(x)) +#define htobe32(x) ((uint32_t)(x)) +#define htobe64(x) ((uint64_t)(x)) + +#define htole16(x) __builtin_bswap16(x) +#define htole32(x) __builtin_bswap32(x) +#define htole64(x) __builtin_bswap64(x) +#else +#define htobe16(x) __builtin_bswap16(x) +#define htobe32(x) __builtin_bswap32(x) +#define htobe64(x) __builtin_bswap64(x) + +#define htole16(x) ((u16)(x)) +#define htole32(x) ((u32)(x)) +#define htole64(x) ((u64)(x)) +#endif +#define betoh16(x) htobe16(x) +#define betoh32(x) htobe32(x) +#define betoh64(x) htobe64(x) + +#define letoh16(x) htole16(x) +#define letoh32(x) htole32(x) +#define letoh64(x) htole64(x) + +#endif /* _ENDIAN_H_ */ From 5c7d398d718eba410839a76eaf17556d9eb74b09 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 13 Jul 2026 15:02:09 -0400 Subject: [PATCH 12/22] mem: fix edge cases in map_mmio_dev Signed-off-by: Michael Shaw --- mem/page.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mem/page.c b/mem/page.c index 47c9c27..64d9063 100644 --- a/mem/page.c +++ b/mem/page.c @@ -111,10 +111,10 @@ static void map_hhdm_zones(pgd_t *pgd_table) } hhdm_end = top_paddr; - device_base = hhdm_end; + device_base = ALIGN_UP(hhdm_end + hhdm_start, PAGE_SIZE); for (paddr_t p = 0; p < top_paddr; p += 0x200000) { - vaddr_t v =(vaddr_t) TO_VIRT(p); + vaddr_t v = (vaddr_t)TO_VIRT(p); pgd_t *pgd_entry = &pgd_table[pgd_index(v)]; if (!(pgd_entry->pgd & PAGE_PRESENT)) { @@ -137,16 +137,20 @@ static void map_hhdm_zones(pgd_t *pgd_table) void *map_mmio_dev(paddr_t base, size_t len) { + len = ALIGN_UP(len, PAGE_SIZE); vaddr_t addr = device_base; u64 flags = PAGE_RW | PAGE_CACHE_DISABLE | PAGE_WRITE_THRU; + size_t offset = base & (PAGE_SIZE - 1); + base -= offset; + page_map(kpgd, base, addr, len, flags, &kmap_tree); tlb_flush(); device_base += npow2(len); - return (void *)addr; + return (void *)addr + offset; } void page_init() From bd02ddc64064faee9b8996eda7545cd8d0d5df65 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 14:36:37 -0400 Subject: [PATCH 13/22] testprogram: add gitignore'd testprogram Signed-off-by: Michael Shaw --- .gitignore | 1 + init/Makefile | 1 + init/testprogram.c | 8 ++++++++ 3 files changed, 10 insertions(+) create mode 100644 init/testprogram.c diff --git a/.gitignore b/.gitignore index 73462d0..4c8af78 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ edk2-ovmf/ .config *.hdd *.cpio +testprogram.c initramfs/ diff --git a/init/Makefile b/init/Makefile index b68b8af..f8c7364 100644 --- a/init/Makefile +++ b/init/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += main.o obj-y += requests.o +obj-y += testprogram.o diff --git a/init/testprogram.c b/init/testprogram.c new file mode 100644 index 0000000..834a68c --- /dev/null +++ b/init/testprogram.c @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include + +void z_test_program_entry() +{ + +} +DRIVER_ENTRY(z_test_program_entry); From 105bdb4464ee213fb1b65fa603a9fcf395bb12a0 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 19:03:10 -0400 Subject: [PATCH 14/22] ahci: add ahci driver Signed-off-by: Michael Shaw --- .gitignore | 1 - drivers/ahci.c | 362 +++++++++++++++++++++++++++++----------- include/drivers/ahci.h | 350 ++++++++++++++++++++++---------------- include/kernel/block.h | 25 +-- include/kernel/sg.h | 28 ++++ include/lib/errno.h | 3 +- init/Makefile | 1 - init/testprogram.c | 8 - kernel/Makefile | 1 + lib/errno.c | 1 + scripts/Makefile.limine | 2 + 11 files changed, 522 insertions(+), 260 deletions(-) create mode 100644 include/kernel/sg.h delete mode 100644 init/testprogram.c diff --git a/.gitignore b/.gitignore index 4c8af78..73462d0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,4 @@ edk2-ovmf/ .config *.hdd *.cpio -testprogram.c initramfs/ diff --git a/drivers/ahci.c b/drivers/ahci.c index 8d16645..a0330ca 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -2,18 +2,26 @@ #include #include #include +#include +#include +#include +#include #include #include -#include #include #include #include +#include + +#define SPIN_MAX 2000 + static uint8_t sata_device_count = 0; -static struct sata_device sata_devices[32] = {0}; +static uint8_t cd_device_count = 0; +static struct sata_device sata_devices[512] = {0}; static paddr_t ahci_pbase = 0; static vaddr_t ahci_vbase = 0; @@ -59,70 +67,29 @@ static void start_cmd(hbaport_t *port) port->cmd |= HBA_PORT_CMD_ST; } -static void ahci_rebase(hbaport_t *port, int n) +static void ahci_rebase(paddr_t ahci_pbase, vaddr_t ahci_vbase, hbaport_t *port, int n) { halt_cmd(port); - port->clb = (ahci_pbase) + (n << 10); - memset((void *)(port->clb | hhdm_start), 0, 1024); + port->clb = ahci_pbase + (n << 10); + memset((void *)(TO_VIRT(port->clb)), 0, 1024); port->fb = ahci_pbase + (32 << 10) + (n << 8); - memset((void *)(port->fb | hhdm_start), 0, 256); + memset((void *)(TO_VIRT(port->clb)), 0, 256); - struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start); + struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)TO_VIRT(port->clb); for (int i = 0; i < 32; i++) { cmdheader[i].prdt_len = 8; - cmdheader[i].cmd_table_base = (ahci_pbase) + (n << 13) + (40 << 10) + (i << 8); + cmdheader[i].cmd_table_base = ahci_pbase + (n << 13) + (40 << 10) + (i << 8); - void *ctba = (void *)(TO_VIRT((paddr_t)cmdheader[i].cmd_table_base)); + void *ctba = (void *)TO_VIRT((paddr_t)cmdheader[i].cmd_table_base); memset(ctba, 0, 256); } start_cmd(port); } -static void ahci_probe_ports(struct ahci_host *host) -{ - struct pci_device *dev = host->device; - hbamem_t *abar = host->abar; - uint32_t pi = abar->pi; - for (int i = 0; i < 32; i++) { - if (pi & 1) { - uint32_t type = ahci_port_type(&abar->ports[i]); - if (type == AHCI_DEV_SATA) { - struct sata_device *sdev = &sata_devices[sata_device_count]; - sdev->port = i; - sdev->host = host; - - char *name = kzalloc(16, ALLOC_KERN); - name[15] = 0; - - snprintf(name, 15, "ahci%d", 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; - - /* bdev = block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size); - block_gpt_init(bdev); */ - - ahci_rebase(&abar->ports[i], i); - sata_device_count++; - kprintf(LOG_INFO "SATA drive found at port %d\n", i); - } else if (type == AHCI_DEV_SATAPI) { - kprintf(LOG_INFO "SATAPI drive found at port %d\n", i); - } else if (type == AHCI_DEV_SEMB) { - kprintf(LOG_INFO "SEMB drive found at port %d\n", i); - } else if (type == AHCI_DEV_PM) { - kprintf(LOG_INFO "PM drive found at port %d\n", i); - } - } - pi >>= 1; - } -} - -static int cmdslot(hbaport_t *port) +static int ahci_slot_alloc(hbaport_t *port) { uint32_t slots = (port->sact | port->ci); for (int i = 0; i < 32; i++) { @@ -135,89 +102,285 @@ static int cmdslot(hbaport_t *port) return -1; } -int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write) +static void +ahci_build_fis_h2d(struct hba_cmd_header *ch, struct hba_cmd_tbl *ct, paddr_t lba, uint16_t count, uint8_t cmd, bool write) { - hbaport_t *port = &dev->host->abar->ports[dev->port]; + ch->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t); + ch->write = write; - /* clear pending interrupt */ - port->is = (uint32_t)-1; - - int spin = 0; - int slot = cmdslot(port); - if (slot == -1) - return false; - - struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start); - cmdheader += slot; - cmdheader->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t); - cmdheader->write = 0; - cmdheader->prdt_len = (uint16_t)((count - 1) >> 4) + 1; - - struct hba_cmd_tbl *cmdtbl = (struct hba_cmd_tbl *)(cmdheader->cmd_table_base | hhdm_start); - memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry)); - - struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis); + struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&ct->cfis); cmdfis->fis_type = FIS_REG_H2D; cmdfis->cmd_mode = 1; - cmdfis->command = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX; + cmdfis->command = cmd; cmdfis->lba0 = (uint8_t)lba; cmdfis->lba1 = (uint8_t)(lba >> 8); cmdfis->lba2 = (uint8_t)(lba >> 16); - cmdfis->device = 1 << 6; /* LBA mode */ + cmdfis->device = 1 << 6; cmdfis->lba3 = (uint8_t)(lba >> 24); cmdfis->lba4 = (uint8_t)(lba >> 32); cmdfis->lba5 = (uint8_t)(lba >> 40); cmdfis->count_low = count & 0xff; cmdfis->count_high = (count >> 8) & 0xff; +} - int i; - for (i = 0; i < cmdheader->prdt_len; i++) { - cmdtbl->prdt_entry[i].data_base = buf; - cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1; - cmdtbl->prdt_entry[i].interrupt = 1; - buf += 8 * 1024; - count -= 16; +static int ahci_setup_prdt(struct hba_cmd_tbl *ct, struct sg_list *sg) +{ + if (sg->count > 8) + return -EINVAL; + + for (size_t i = 0; i < sg->count; i++) { + if (sg->entries[i].len > 4 * MB) + return -EINVAL; + + ct->prdt_entry[i].data_base = sg->entries[i].paddr; + ct->prdt_entry[i].byte_count = sg->entries[i].len - 1; + ct->prdt_entry[i].interrupt = (i == sg->count - 1) ? 1 : 0; } - /* wait until port is free */ - while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000) { + return sg->count; +} + +static int ahci_issue_wait(hbaport_t *port, int slot) +{ + int spin = 0; + + while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < SPIN_MAX) spin++; - } - if (spin == 1000000) { + if (spin >= SPIN_MAX) { kprintf(LOG_ERROR "AHCI: Port is hung\n"); - return false; + return -EIO; } - /* issue command */ port->ci = 1 << slot; - /* wait for completion */ while (1) { if (port->is & HBA_PORT_IS_TFES) { kprintf(LOG_ERROR "AHCI: Read disk error\n"); - return false; + port->is = (uint32_t)-1; + return -EIO; } if ((port->ci & (1 << slot)) == 0) break; - /* ELSE IF: reschedule */ } - /* check again */ if (port->is & HBA_PORT_IS_TFES) { kprintf(LOG_ERROR "AHCI: Read disk error\n"); - return false; + port->is = (uint32_t)-1; + return -EIO; } - return true; + return 0; } -static ssize_t ahci_block_read(struct block_device *dev, char *buf, size_t off, size_t n_blocks) +static int ahci_rw_sg(struct sata_device *dev, paddr_t lba, uint16_t count, struct sg_list *sg, bool write) { + hbaport_t *port = &dev->host->abar->ports[dev->port]; + port->is = (uint32_t)-1; + + int slot = ahci_slot_alloc(port); + if (slot < 0) { + kprintf(LOG_ERROR "AHCI: No free command slot\n"); + return -EAGAIN; + } + + struct hba_cmd_header *ch = (struct hba_cmd_header *)(port->clb | hhdm_start) + slot; + struct hba_cmd_tbl *ct = (struct hba_cmd_tbl *)(ch->cmd_table_base | hhdm_start); + + memset(ct, 0, sizeof(struct hba_cmd_tbl) + (sg->count - 1) * sizeof(struct hba_prdt_entry)); + + uint8_t cmd = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX; + ahci_build_fis_h2d(ch, ct, lba, count, cmd, write); + + int prdt_entries = ahci_setup_prdt(ct, sg); + if (prdt_entries < 0) + return prdt_entries; + + ch->prdt_len = prdt_entries; + + return ahci_issue_wait(port, slot); +} + +static int ahci_rw(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write) +{ + SG_STATIC(sg, 1); + sg_append(&sg, buf, count * dev->sector_size); + return ahci_rw_sg(dev, lba, count, &sg, write); +} + +static int ahci_identify(hbaport_t *port, struct ahci_identify *ident, bool packet) +{ + void *buf = kalloc(4096, ALLOC_KERN); + if (!buf) + return -ENOMEM; + + memset(ident, 0, sizeof(*ident)); + + port->is = (uint32_t)-1; + + int slot = ahci_slot_alloc(port); + if (slot < 0) { + kprintf(LOG_ERROR "AHCI: No free command slot for identify\n"); + kfree(buf); + return -EAGAIN; + } + + struct hba_cmd_header *ch = (struct hba_cmd_header *)(port->clb | hhdm_start) + slot; + struct hba_cmd_tbl *ct = (struct hba_cmd_tbl *)(ch->cmd_table_base | hhdm_start); + + memset(ct, 0, sizeof(struct hba_cmd_tbl)); + memset(buf, 0, 4096); + + uint8_t cmd = packet ? ATA_CMD_IDENTIFY_PACKET : ATA_CMD_IDENT; + ahci_build_fis_h2d(ch, ct, 0, 0, cmd, 0); + + SG_STATIC(sg, 1); + sg_append(&sg, TO_PHYS(buf), 512); + ch->prdt_len = ahci_setup_prdt(ct, &sg); + + int ret = ahci_issue_wait(port, slot); + if (ret < 0) { + kfree(buf); + return ret; + } + + memcpy(ident, buf, sizeof(struct ahci_identify)); + + uint16_t *p = (uint16_t *)ident->serial_number; + for (int i = 0; i < 10; i++) + p[i] = betoh16(p[i]); + + p = (uint16_t *)ident->firmware_revision; + for (int i = 0; i < 4; i++) + p[i] = betoh16(p[i]); + + p = (uint16_t *)ident->model_number; + for (int i = 0; i < 20; i++) + p[i] = betoh16(p[i]); + + ident->total_lba28_sectors = letoh32(ident->total_lba28_sectors); + ident->total_lba48_sectors = letoh64(ident->total_lba48_sectors); + ident->capabilities = letoh32(ident->capabilities); + ident->current_capacity_sectors = letoh32(ident->current_capacity_sectors); + ident->physical_logical_sectors = letoh64(ident->physical_logical_sectors); + + kfree(buf); + return 0; +} + +static ssize_t ahci_block_read(struct block_device *bdev, struct sg_list *sg, size_t off, size_t n) +{ + return ahci_rw_sg(bdev->priv, off, n, sg, false); +} + +static ssize_t ahci_block_write(struct block_device *bdev, struct sg_list *sg, size_t off, size_t n) +{ + return ahci_rw_sg(bdev->priv, off, n, sg, true); +} + +struct sata_device *init_sata_port(struct ahci_host *host, u32 port, int type) +{ + static struct block_ops ahci_block_ops = { + .read = ahci_block_read, + .write = ahci_block_write, + }; + + struct sata_device *sdev = &sata_devices[sata_device_count]; + sdev->port = port; + sdev->host = host; + + bool packet = (type == AHCI_DEV_SATAPI); + + hbamem_t *abar = host->abar; + + char *name = sdev->name; + name[sizeof(sdev->name) - 1] = 0; + + if (packet) + snprintf(name, sizeof(sdev->name) - 1, "cd%d", cd_device_count); + else + snprintf(name, sizeof(sdev->name) - 1, "ahci%d", sata_device_count); + + vaddr_t ahci_vbase = (vaddr_t)kalloc(512 * KB, ALLOC_KERN); + + ahci_rebase(TO_PHYS(ahci_vbase), ahci_vbase, &abar->ports[port], port); + + struct ahci_identify ident; + int ret = ahci_identify(&abar->ports[port], &ident, packet); + if (ret < 0) { + kprintf(LOG_ERROR "AHCI: identify failed on port %d\n", port); + return NULL; + } + + sdev->sector_count = ident.total_lba48_sectors; + if (sdev->sector_count == 0) + sdev->sector_count = ident.total_lba28_sectors; + + uint16_t w106 = ident.physical_logical_sectors & 0xFFFF; + sdev->sector_size = 512; + if (w106 & (1 << 13)) { + size_t s = (ident.reserved5[7] | ((uint32_t)ident.reserved5[8] << 16)) * 2; + if (s >= 512) + sdev->sector_size = s; + } else if (w106 & (1 << 12)) { + sdev->sector_size = 1024; + } + + memcpy(sdev->model, ident.model_number, 40); + sdev->model[40] = '\0'; + + uint16_t rate = letoh16(ident.reserved6[91]); + if (rate == 0x0001) + sdev->media_type = 2; + else if (rate >= 5400) + sdev->media_type = 1; + else + sdev->media_type = 0; + + if (packet) + cd_device_count++; + else + sata_device_count++; + const char *type_str = packet ? "ATAPI" : (sdev->media_type == 2 ? "SSD" : "HDD"); + kprintf(LOG_INFO "/dev/%s: %s at port %d: %zd sectors, %zdb, model: %s\n", sdev->name, type_str, port, sdev->sector_count, + sdev->sector_size, sdev->model); + + struct block_device *bdev = kzalloc(sizeof(struct block_device), ALLOC_KERN); + if (!bdev) + return sdev; + + bdev->priv = sdev; + bdev->size = sdev->sector_count; + bdev->block_size = sdev->sector_size; + bdev->ops = ahci_block_ops; + strncpy(bdev->name, name, sizeof(bdev->name) - 1); + + devfs_insert_device_blk(bdev->name, bdev, NULL); + + return sdev; +} + +static void ahci_probe_ports(struct ahci_host *host) +{ + hbamem_t *abar = host->abar; + uint32_t pi = abar->pi; + for (int i = 0; i < 32; i++) { + if (pi & 1) { + uint32_t type = ahci_port_type(&abar->ports[i]); + if (type == AHCI_DEV_SATA || type == AHCI_DEV_SATAPI) { + init_sata_port(host, i, type); + } else if (type == AHCI_DEV_SEMB) { + kprintf(LOG_INFO "SEMB drive found at port %d\n", i); + } else if (type == AHCI_DEV_PM) { + kprintf(LOG_INFO "PM drive found at port %d\n", i); + } + } + pi >>= 1; + } } static int ahci_init_device(struct pci_device *device, size_t ahci_dev_no) @@ -226,6 +389,17 @@ static int ahci_init_device(struct pci_device *device, size_t ahci_dev_no) if (!bdev) return -ENOMEM; + struct ahci_host *host = kzalloc(sizeof(struct ahci_host), ALLOC_KERN); + if (!host) { + kfree(bdev); + return -ENOMEM; + } + + host->abar = map_mmio_dev(device->bar5, PAGE_SIZE); + host->device = device; + + ahci_probe_ports(host); + return 0; } @@ -234,7 +408,7 @@ void ahci_init() list_t dev_list; list_create(&dev_list); - size_t n = pci_find_device(0x0001, 0x0006, &dev_list); + pci_find_device(1, 6, &dev_list); list_item_t *item = dev_list.head; size_t i = 0; diff --git a/include/drivers/ahci.h b/include/drivers/ahci.h index 2e03150..fb7f749 100644 --- a/include/drivers/ahci.h +++ b/include/drivers/ahci.h @@ -3,6 +3,7 @@ #define _AHCI_H_ #include +#include /* Frame information structure (FIS) */ #define FIS_REG_H2D 0x27 @@ -20,6 +21,7 @@ #define ATA_CMD_READ_DMA_EX 0x25 #define ATA_CMD_WRITE_DMA_EX 0x35 #define ATA_CMD_IDENT 0xEC +#define ATA_CMD_IDENTIFY_PACKET 0xA1 #define SATA_SIG_ATA 0x00000101 #define SATA_SIG_ATAPI 0xEB140101 @@ -46,203 +48,203 @@ struct fis_reg_h2d { /* 0x00 */ - uint8_t fis_type; + u8 fis_type; - uint8_t pmult : 4; - uint8_t reserved : 3; - uint8_t cmd_mode : 1; /* high: command, low: control */ + u8 pmult : 4; + u8 reserved : 3; + u8 cmd_mode : 1; /* high: command, low: control */ - uint8_t command; - uint8_t feature_low; + u8 command; + u8 feature_low; /* 0x04 */ - uint8_t lba0; - uint8_t lba1; - uint8_t lba2; - uint8_t device; + u8 lba0; + u8 lba1; + u8 lba2; + u8 device; /* 0x08 */ - uint8_t lba3; - uint8_t lba4; - uint8_t lba5; - uint8_t feature_high; + u8 lba3; + u8 lba4; + u8 lba5; + u8 feature_high; /* 0x0C */ - uint8_t count_low; - uint8_t count_high; - uint8_t icc; - uint8_t control; + u8 count_low; + u8 count_high; + u8 icc; + u8 control; /* 0x10 */ - uint32_t reserved2; + u32 reserved2; } PACKED; struct fis_reg_d2h { /* 0x00 */ - uint8_t fis_type; + u8 fis_type; - uint8_t pmult : 4; - uint8_t reserved : 2; - uint8_t intr : 1; - uint8_t reserved2 : 1; + u8 pmult : 4; + u8 reserved : 2; + u8 intr : 1; + u8 reserved2 : 1; - uint8_t status; - uint8_t error; + u8 status; + u8 error; /* 0x04 */ - uint8_t lba0; - uint8_t lba1; - uint8_t lba2; - uint8_t device; + u8 lba0; + u8 lba1; + u8 lba2; + u8 device; /* 0x08 */ - uint8_t lba3; - uint8_t lba4; - uint8_t lba5; - uint8_t feature_high; + u8 lba3; + u8 lba4; + u8 lba5; + u8 feature_high; /* 0x0C */ - uint8_t count_low; - uint8_t count_high; - uint8_t icc; - uint8_t control; + u8 count_low; + u8 count_high; + u8 icc; + u8 control; /* 0x10 */ - uint32_t reserved3; + u32 reserved3; } PACKED; struct fis_data { /* 0x00 */ - uint8_t fis_type; + u8 fis_type; - uint8_t pmult : 4; - uint8_t reserved : 4; + u8 pmult : 4; + u8 reserved : 4; - uint8_t reserved2[2]; + u8 reserved2[2]; /* 0x04 */ - uint32_t data[1]; /* Variable length */ + u32 data[1]; /* Variable length */ } PACKED; struct fis_dma_setup { /* 0x00 */ - uint8_t fis_type; + u8 fis_type; - uint8_t pmult : 4; - uint8_t reserved : 1; - uint8_t data_dir : 1; - uint8_t intr : 1; - uint8_t auto_act : 1; + u8 pmult : 4; + u8 reserved : 1; + u8 data_dir : 1; + u8 intr : 1; + u8 auto_act : 1; - uint16_t reserved2; + u16 reserved2; /* 0x04 */ - uint64_t dma_buffer_id; + u64 dma_buffer_id; /* 0x0C */ - uint32_t reserved3; + u32 reserved3; /* 0x10 */ - uint32_t dma_buffer_offset; + u32 dma_buffer_offset; /* 0x14 */ - uint32_t transfer_count; + u32 transfer_count; /* 0x18 */ - uint32_t reserved4; + u32 reserved4; } PACKED; struct fis_pio_setup { /* 0x00 */ - uint8_t fis_type; + u8 fis_type; - uint8_t pmult : 4; - uint8_t reserved : 1; - uint8_t data_dir : 1; - uint8_t intr : 1; - uint8_t reserved2 : 1; + u8 pmult : 4; + u8 reserved : 1; + u8 data_dir : 1; + u8 intr : 1; + u8 reserved2 : 1; - uint8_t status; - uint8_t error; + u8 status; + u8 error; /* 0x04 */ - uint8_t lba0; - uint8_t lba1; - uint8_t lba2; - uint8_t device; + u8 lba0; + u8 lba1; + u8 lba2; + u8 device; /* 0x08 */ - uint8_t lba3; - uint8_t lba4; - uint8_t lba5; - uint8_t reserved3; + u8 lba3; + u8 lba4; + u8 lba5; + u8 reserved3; /* 0x0C */ - uint8_t count_low; - uint8_t count_high; - uint8_t reserved4; - uint8_t e_status; + u8 count_low; + u8 count_high; + u8 reserved4; + u8 e_status; /* 0x10 */ - uint16_t transfer_count; - uint16_t reserved5; + u16 transfer_count; + u16 reserved5; } PACKED; struct fis_set_device_bits { /* 0x00 */ - uint8_t fis_type; + u8 fis_type; - uint8_t pmult : 4; - uint8_t reserved : 2; - uint8_t interrupt : 1; - uint8_t n : 1; + u8 pmult : 4; + u8 reserved : 2; + u8 interrupt : 1; + u8 n : 1; - uint8_t statusl : 3; - uint8_t reserved2 : 1; - uint8_t statush : 3; - uint8_t reserved3 : 1; + u8 statusl : 3; + u8 reserved2 : 1; + u8 statush : 3; + u8 reserved3 : 1; /* 0x04 */ - uint8_t errorl : 3; + u8 errorl : 3; } PACKED; struct hba_cmd_header { - uint8_t cmd_fis_len : 5; - uint8_t atapi : 1; - uint8_t write : 1; - uint8_t prefetch : 1; + u8 cmd_fis_len : 5; + u8 atapi : 1; + u8 write : 1; + u8 prefetch : 1; - uint8_t reset : 1; - uint8_t bist : 1; - uint8_t clear_busy : 1; - uint8_t reserved : 1; - uint8_t pmp : 4; + u8 reset : 1; + u8 bist : 1; + u8 clear_busy : 1; + u8 reserved : 1; + u8 pmp : 4; - uint16_t prdt_len; + u16 prdt_len; - volatile uint32_t prdb_count; + volatile u32 prdb_count; paddr_t cmd_table_base; - uint32_t reserved2[4]; + u32 reserved2[4]; } PACKED; struct hba_prdt_entry { paddr_t data_base; - uint32_t reserved; + u32 reserved; - uint32_t byte_count : 22; - uint32_t reserved2 : 9; - uint32_t interrupt : 1; + u32 byte_count : 22; + u32 reserved2 : 9; + u32 interrupt : 1; } PACKED; struct hba_cmd_tbl { - uint8_t cfis[64]; + u8 cfis[64]; - uint8_t acmd[16]; + u8 acmd[16]; - uint8_t reserved[48]; + u8 reserved[48]; struct hba_prdt_entry prdt_entry[1]; } PACKED; @@ -254,53 +256,53 @@ typedef volatile struct { paddr_t fb; /* 0x10 */ - uint32_t is; - uint32_t ie; - uint32_t cmd; - uint32_t reserved; + u32 is; + u32 ie; + u32 cmd; + u32 reserved; /* 0x20 */ - uint32_t tfd; - uint32_t sig; - uint32_t ssts; - uint32_t sctl; + u32 tfd; + u32 sig; + u32 ssts; + u32 sctl; /* 0x30 */ - uint32_t serr; - uint32_t sact; - uint32_t ci; - uint32_t sntf; + u32 serr; + u32 sact; + u32 ci; + u32 sntf; /* 0x40 */ - uint32_t fbs; + u32 fbs; - uint32_t reserved1[11]; - uint32_t vendor[4]; + u32 reserved1[11]; + u32 vendor[4]; } PACKED hbaport_t; typedef volatile struct { /* 0x00 */ - uint32_t cap; - uint32_t ghc; - uint32_t is; - uint32_t pi; - uint32_t vs; + u32 cap; + u32 ghc; + u32 is; + u32 pi; + u32 vs; /* 0x14 */ - uint32_t ccc_ctl; - uint32_t ccc_ports; - uint32_t em_loc; - uint32_t em_ctl; - uint32_t cap2; + u32 ccc_ctl; + u32 ccc_ports; + u32 em_loc; + u32 em_ctl; + u32 cap2; /* 0x28 */ - uint32_t bohc; + u32 bohc; /* 0x2C */ - uint8_t reserved[0x74]; + u8 reserved[0x74]; /* 0xA0 */ - uint8_t vendor[0x60]; + u8 vendor[0x60]; /* 0x100 */ hbaport_t ports[32]; @@ -309,38 +311,98 @@ typedef volatile struct { typedef volatile struct { /* 0x00 */ struct fis_dma_setup dfis; - uint32_t reserved; + u32 reserved; /* 0x20 */ struct fis_pio_setup pfis; - uint32_t reserved1[3]; + u32 reserved1[3]; /* 0x40 */ struct fis_reg_d2h rfis; - uint32_t reserved2; + u32 reserved2; struct fis_set_device_bits sdbfis; /* 0x60 */ - uint8_t ufis[64]; + u8 ufis[64]; /* 0xA0 */ - uint8_t reserved3[0x60]; + u8 reserved3[0x60]; } PACKED hbafis_t; struct ahci_host { - struct pci_device *device; + struct pci_device *device; hbamem_t *abar; }; +struct ahci_identify { + u16 config_flags; + u16 cylinders_obsolete; + u16 specific_config; + u16 heads_obsolete; + u16 reserved1[2]; + u16 sectors_obsolete; + u16 reserved_for_compact[3]; + u8 serial_number[20]; + u16 buffer_type_obsolete; + u16 buffer_size_obsolete; + u16 ecc_bytes_obsolete; + u8 firmware_revision[8]; + u8 model_number[40]; + u16 max_multiple_sectors; + u16 trusted_computing; + u32 capabilities; + u16 obsolescent_word51; + u16 obsolescent_word52; + u16 field_validity; + u16 current_cylinders; + u16 current_heads; + u16 current_sectors; + u32 current_capacity_sectors; + u16 multiple_sector_setting; + u32 total_lba28_sectors; + u16 double_word_dma_obsolete; + u16 multiword_dma_settings; + u16 advanced_pio_modes; + u16 min_mwdma_cycle_time; + u16 rec_mwdma_cycle_time; + u16 min_pio_cycle_no_flow; + u16 min_pio_cycle_flow; + u16 reserved2[2]; + u16 reserved_packet_dev[4]; + u16 queue_depth; + u16 serial_ata_capabilities; + u16 reserved_sata; + u16 serial_ata_features; + u16 serial_ata_features_en; + u16 major_version; + u16 minor_version; + u16 cmd_set_supported1; + u16 cmd_set_supported2; + u16 cmd_set_supported3; + u16 cmd_set_enabled1; + u16 cmd_set_enabled2; + u16 cmd_set_enabled3; + u16 ultra_dma_modes; + u16 reserved3[11]; + u64 total_lba48_sectors; + u16 streaming_transfer_time; + u16 reserved4; + u64 physical_logical_sectors; + u16 reserved5[15]; + u16 data_structure_checksum; + u16 reserved6[130]; +} PACKED; + struct sata_device { - struct ahci_host *host; + char name[16]; + struct ahci_host *host; uint8_t port; size_t sector_size; size_t sector_count; + char model[41]; + int media_type; }; -int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write); - #endif /* _AHCI_H_ */ diff --git a/include/kernel/block.h b/include/kernel/block.h index f1b35a1..2f6e599 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -2,13 +2,21 @@ #ifndef _BLOCK_H_ #define _BLOCK_H_ -#include #include +#include + +#include struct block_device; struct block_ops { - ssize_t (*read)(struct block_device *dev, char *buf, size_t offset, size_t n); - ssize_t (*write)(struct block_device *dev, char *buf, size_t offset, size_t n); + ssize_t (*read)(struct block_device *dev, struct sg_list *sg, size_t offset, size_t n); + ssize_t (*write)(struct block_device *dev, struct sg_list *sg, size_t offset, size_t n); +}; + +struct disk { + u8 is_disk; + char model[FILE_NAME_MAX_LEN + 1]; + list_t part_table; }; struct block_device { @@ -17,14 +25,9 @@ struct block_device { size_t size; size_t block_size; struct block_ops ops; - void *priv; - size_t lba_start; -}; - -struct disk { - struct block_device *dev; - char model[FILE_NAME_MAX_LEN + 1]; - list_t part_table; + void *priv; + size_t lba_start; + struct disk disk; }; ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); diff --git a/include/kernel/sg.h b/include/kernel/sg.h new file mode 100644 index 0000000..b9bfa7d --- /dev/null +++ b/include/kernel/sg.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _SG_H_ +#define _SG_H_ + +#include + +struct sg_entry { + paddr_t paddr; + size_t len; +}; + +struct sg_list { + struct sg_entry *entries; + size_t count; + size_t max_count; +}; + +#define SG_STATIC(name, n) \ + struct sg_entry name##_buf[n]; \ + struct sg_list name = {.entries = name##_buf, .count = 0, .max_count = (n)} + +#define sg_for_each(entry, sg) for (struct sg_entry *entry = (sg)->entries; entry < (sg)->entries + (sg)->count; entry++) + +void sg_init(struct sg_list *sg, struct sg_entry *buf, size_t n); +int sg_append(struct sg_list *sg, paddr_t paddr, size_t len); +void sg_reset(struct sg_list *sg); + +#endif /* _SG_H_ */ diff --git a/include/lib/errno.h b/include/lib/errno.h index dfe07e2..82343df 100644 --- a/include/lib/errno.h +++ b/include/lib/errno.h @@ -14,7 +14,8 @@ #define ENOEXEC 9 #define EFAULT 10 #define EAGAIN 11 -#define EBUSY 12 +#define EIO 12 +#define EBUSY 13 #define ENOSYS 13 #define ESYSCALLBLK 14 #define ENAMETOOLONG 15 diff --git a/init/Makefile b/init/Makefile index f8c7364..b68b8af 100644 --- a/init/Makefile +++ b/init/Makefile @@ -1,4 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += main.o obj-y += requests.o -obj-y += testprogram.o diff --git a/init/testprogram.c b/init/testprogram.c deleted file mode 100644 index 834a68c..0000000 --- a/init/testprogram.c +++ /dev/null @@ -1,8 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include - -void z_test_program_entry() -{ - -} -DRIVER_ENTRY(z_test_program_entry); diff --git a/kernel/Makefile b/kernel/Makefile index 5dafe9b..8482876 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += device.o obj-y += block.o +obj-y += sg.o obj-y += tty/ diff --git a/lib/errno.c b/lib/errno.c index 2542ead..c20cb34 100644 --- a/lib/errno.c +++ b/lib/errno.c @@ -15,6 +15,7 @@ static const char *errno_str[] = { [ENOEXEC] = "Exec format error", [EFAULT] = "Bad address", [EAGAIN] = "Try again", + [EIO] = "Input/output error", [EBUSY] = "Device or resource busy", [ENOSYS] = "Function not implemented", [ENAMETOOLONG] = "Name too long", diff --git a/scripts/Makefile.limine b/scripts/Makefile.limine index 309ef77..0cf6163 100644 --- a/scripts/Makefile.limine +++ b/scripts/Makefile.limine @@ -47,6 +47,7 @@ run: edk2-ovmf $(INITRAMFS) $(IMG) -accel kvm \ -nographic \ -hda $(IMG) \ + -hdb test_drive.hdd \ $(QEMUFLAGS) .PHONY += debug @@ -57,6 +58,7 @@ debug: edk2-ovmf $(IMG) -drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \ -nographic \ -hda $(IMG) \ + -hdb test_drive.hdd \ $(QEMUFLAGS) edk2-ovmf: From 1620f28e71a49a270c537f0af95cf657a2e971c8 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 19:03:34 -0400 Subject: [PATCH 15/22] sg: add sg_list Signed-off-by: Michael Shaw --- kernel/sg.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 kernel/sg.c diff --git a/kernel/sg.c b/kernel/sg.c new file mode 100644 index 0000000..44c56db --- /dev/null +++ b/kernel/sg.c @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include + +void sg_init(struct sg_list *sg, struct sg_entry *buf, size_t n) +{ + sg->entries = buf; + sg->count = 0; + sg->max_count = n; +} + +int sg_append(struct sg_list *sg, paddr_t paddr, size_t len) +{ + if (sg->count >= sg->max_count) + return -ENOSPC; + + sg->entries[sg->count].paddr = paddr; + sg->entries[sg->count].len = len; + sg->count++; + return 0; +} + +void sg_reset(struct sg_list *sg) +{ + sg->count = 0; +} From 339687cdac2d389cb50c0e2275a0122d6c850943 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 19:10:34 -0400 Subject: [PATCH 16/22] block: move into own directory Signed-off-by: Michael Shaw --- include/kernel/block.h | 9 ++++++--- kernel/Makefile | 2 +- kernel/block/Makefile | 2 ++ kernel/{ => block}/block.c | 34 +++++++++++++++++++++++++++++----- 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 kernel/block/Makefile rename kernel/{ => block}/block.c (50%) diff --git a/include/kernel/block.h b/include/kernel/block.h index 2f6e599..f209e68 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -20,13 +20,16 @@ struct disk { }; struct block_device { - char name[256]; dev_t dev; + char name[256]; + size_t size; size_t block_size; - struct block_ops ops; - void *priv; size_t lba_start; + + struct block_device *part_of; /* if it is a partition */ + + struct block_ops ops; struct disk disk; }; diff --git a/kernel/Makefile b/kernel/Makefile index 8482876..fbea642 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += device.o -obj-y += block.o obj-y += sg.o +obj-y += block/ obj-y += tty/ diff --git a/kernel/block/Makefile b/kernel/block/Makefile new file mode 100644 index 0000000..4fb037c --- /dev/null +++ b/kernel/block/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-y += block.o diff --git a/kernel/block.c b/kernel/block/block.c similarity index 50% rename from kernel/block.c rename to kernel/block/block.c index 856bb6e..13f2ff8 100644 --- a/kernel/block.c +++ b/kernel/block/block.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include +#include #include @@ -12,25 +13,48 @@ int block_device_register(char *name, size_t lba_start, struct block_ops *ops, s if (!ret) return -ENOMEM; - ret->lba_start = lba_start; + ret->lba_start = lba_start; strncpy(ret->name, name, sizeof(ret->name) - 1); *out = ret; memcpy(ops, &ret->ops, sizeof(struct block_ops)); - dirent_t *dirent; - int res = devfs_insert_device_blk(ret->name, ret, &dirent); + dirent_t *dirent; + int res = devfs_insert_device_blk(ret->name, ret, &dirent); return 0; } +int block_check_partitions(struct block_device *bdev) +{ + if (!bdev || !bdev->disk.is_disk) + return -EINVAL; + + return 0; +} + ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks) { - return dev->ops.read(dev, buf, offset, n_blocks); + if (dev->lba_start) { + offset += dev->lba_start; + dev = dev->part_of; + } + + SG_STATIC(sg, 1); + sg_append(&sg, TO_PHYS(buf), dev->block_size * n_blocks); + + return dev->ops.read(dev, &sg, offset, n_blocks); } ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n_blocks) { - return dev->ops.write(dev, buf, offset, n_blocks); + if (dev->lba_start) { + offset += dev->lba_start; + dev = dev->part_of; + } + + SG_STATIC(sg, 1); + sg_append(&sg, TO_PHYS(buf), dev->block_size * n_blocks); + return dev->ops.write(dev, &sg, offset, n_blocks); } From 228bd9de9c493d3cf98795814a249f512bc4b18e Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 19:44:16 -0400 Subject: [PATCH 17/22] ahci: add partition check for drives Signed-off-by: Michael Shaw --- drivers/ahci.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/ahci.c b/drivers/ahci.c index a0330ca..bead535 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -282,7 +282,7 @@ static ssize_t ahci_block_write(struct block_device *bdev, struct sg_list *sg, s return ahci_rw_sg(bdev->priv, off, n, sg, true); } -struct sata_device *init_sata_port(struct ahci_host *host, u32 port, int type) +static struct sata_device *init_sata_port(struct ahci_host *host, u32 port, int type) { static struct block_ops ahci_block_ops = { .read = ahci_block_read, @@ -357,8 +357,12 @@ struct sata_device *init_sata_port(struct ahci_host *host, u32 port, int type) bdev->size = sdev->sector_count; bdev->block_size = sdev->sector_size; bdev->ops = ahci_block_ops; + bdev->disk.is_disk = true; strncpy(bdev->name, name, sizeof(bdev->name) - 1); + if (!packet) + block_check_partitions(bdev); + devfs_insert_device_blk(bdev->name, bdev, NULL); return sdev; From 4096f03d32d7732cf039d147a1346b770351dbcf Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 23:34:31 -0400 Subject: [PATCH 18/22] buddy: fix page fault Signed-off-by: Michael Shaw --- mem/buddy.c | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/mem/buddy.c b/mem/buddy.c index c9c6e9c..ca1d2ac 100644 --- a/mem/buddy.c +++ b/mem/buddy.c @@ -88,6 +88,7 @@ static void alloc_split(struct buddy_zone *bzone, void *alloc, size_t cur_order, for (size_t i = new_step_size; i < cur_full_size; i += new_step_size) { struct buddy_page_header *h = (void *)(((uintptr_t)alloc) + i); h->order = new_order; + h->zone = bzone; h->prev = prev; if (prev) @@ -200,19 +201,10 @@ static inline void init_zones() } } -static struct pm_entry *get_pm_entry(struct buddy_page_header *h) -{ - size_t offset = (vaddr_t)h - h->zone->usable_base; - size_t page_no = offset / PAGE_SIZE; - return h->zone->page_map + page_no; -} - static struct buddy_page_header *get_buddy(struct buddy_page_header *h) { - struct pm_entry *entry = get_pm_entry(h); size_t offset = (vaddr_t)h - h->zone->usable_base; - size_t order = entry->alloc_order; - size_t buddy_offset = offset ^ (PAGE_SIZE << order); + size_t buddy_offset = offset ^ (PAGE_SIZE << h->order); if (buddy_offset >= h->zone->usable_len) return NULL; @@ -220,22 +212,24 @@ static struct buddy_page_header *get_buddy(struct buddy_page_header *h) return (void *)(h->zone->usable_base + buddy_offset); } -static bool allocated(struct buddy_page_header *h) -{ - if (!h) - return false; - - struct pm_entry *entry = get_pm_entry(h); - - return !!entry->alloc_order; -} - void buddy_zone_free(struct buddy_zone *bzone, void *ptr) { struct buddy_page_header *h = ptr; + size_t offset = (vaddr_t)h - bzone->usable_base; + struct pm_entry *entry = bzone->page_map + offset / PAGE_SIZE; + h->order = entry->alloc_order; + h->zone = bzone; + struct buddy_page_header *buddy = get_buddy(h); - while (buddy && !allocated(buddy)) { + entry->alloc_order = 0; + + while (buddy) { + size_t b_off = (vaddr_t)buddy - bzone->usable_base; + struct pm_entry *b_entry = bzone->page_map + b_off / PAGE_SIZE; + if (b_entry->alloc_order) + break; + if (buddy->next) buddy->next->prev = buddy->prev; if (buddy->prev) From 70d171fed874b100695b3c786abf37c0c56743e2 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 23:34:49 -0400 Subject: [PATCH 19/22] amd64: add extra information on fault Signed-off-by: Michael Shaw --- amd64/arch/reg.S | 27 ++++++++++++--------------- amd64/exception.c | 6 +++++- amd64/trap.S | 9 +++++++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/amd64/arch/reg.S b/amd64/arch/reg.S index ebd2e91..0667793 100644 --- a/amd64/arch/reg.S +++ b/amd64/arch/reg.S @@ -21,25 +21,22 @@ __idt: .text -.global cr3_write -cr3_write: - mov cr3, rdi +.macro genrw reg +.global \reg\()_write +\reg\()_write: + mov \reg, rdi ret -.global cr3_read -cr3_read: - mov rax, cr3 +.global \reg\()_read +\reg\()_read: + mov rax, \reg ret +.endm -.global cr4_read -cr4_read: - mov rax, cr4 - ret - -.global cr4_write -cr4_write: - mov cr4, rdi - ret +genrw cr1 +genrw cr2 +genrw cr3 +genrw cr4 .global lgdt lgdt: diff --git a/amd64/exception.c b/amd64/exception.c index 27d624e..d0da2ce 100644 --- a/amd64/exception.c +++ b/amd64/exception.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include +#include "arch/reg.h" static const char *exception_names[] = { "Divide-by-zero Error", @@ -38,5 +39,8 @@ static const char *exception_names[] = { void exception_handler(u64 no, u64 err) { - panic("(Exception) %s(%02xh)\n", exception_names[no], no); + if (no == 0x0e) + kerr("(Page fault): 0x%16x\n", cr2_read()); + if (!kernel_init_ready) + panic("(Exception) %s(%02xh)\n", exception_names[no], no); } diff --git a/amd64/trap.S b/amd64/trap.S index b310604..72914b0 100644 --- a/amd64/trap.S +++ b/amd64/trap.S @@ -9,6 +9,15 @@ iretq .endm +.macro exception_err nr + .global _exception_\nr + _exception_\nr: + mov rdi, \nr + popq rsi + call exception_handler + iretq +.endm + .macro irq nr .global _irq_\nr _irq_\nr: From 5692aa8120d82a02d8ad13103fee3e016a01b5f9 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 23:35:13 -0400 Subject: [PATCH 20/22] main: add global kernel ready bool Signed-off-by: Michael Shaw --- include/kernel/common.h | 2 ++ init/main.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/include/kernel/common.h b/include/kernel/common.h index d0df75d..d6fd405 100644 --- a/include/kernel/common.h +++ b/include/kernel/common.h @@ -34,4 +34,6 @@ void yield(); void _panic(); +extern bool kernel_init_ready; + #endif /* _COMMON_H_ */ diff --git a/init/main.c b/init/main.c index 030188d..a9c39b1 100644 --- a/init/main.c +++ b/init/main.c @@ -39,6 +39,8 @@ typedef void (*initcall_t)(void); extern initcall_t __initcall_start, __initcall_end; struct tty *tty_printf = NULL; +bool kernel_init_ready = false; + static void do_initcalls() { for (initcall_t *fn = &__initcall_start; fn < &__initcall_end; fn++) @@ -79,6 +81,7 @@ static void kmain() do_initcalls(); /* do nothing, for now :) */ + kernel_init_ready = true; yield(); } From 0d87d7bffefa36c31812de2c10ec4c520c1452c3 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Tue, 14 Jul 2026 23:35:57 -0400 Subject: [PATCH 21/22] block: add private data field Signed-off-by: Michael Shaw --- include/kernel/block.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/kernel/block.h b/include/kernel/block.h index f209e68..07a9ecc 100644 --- a/include/kernel/block.h +++ b/include/kernel/block.h @@ -28,6 +28,7 @@ struct block_device { size_t lba_start; struct block_device *part_of; /* if it is a partition */ + void *priv; struct block_ops ops; struct disk disk; @@ -36,4 +37,6 @@ struct block_device { ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); ssize_t block_write(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); +int block_check_partitions(struct block_device *bdev); + #endif /* _BLOCK_H_ */ From 25c0b538d2b7c2ef6bd9cded050f243491df5a7a Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Wed, 15 Jul 2026 00:07:28 -0400 Subject: [PATCH 22/22] block: add gpt partitioning Signed-off-by: Michael Shaw --- kernel/block/Makefile | 1 + kernel/block/block.c | 33 ++++++++++++-- kernel/block/gpt.c | 96 ++++++++++++++++++++++++++++++++++++++++ kernel/block/gpt.h | 40 +++++++++++++++++ kernel/block/partition.h | 10 +++++ 5 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 kernel/block/gpt.c create mode 100644 kernel/block/gpt.h create mode 100644 kernel/block/partition.h diff --git a/kernel/block/Makefile b/kernel/block/Makefile index 4fb037c..1858b8f 100644 --- a/kernel/block/Makefile +++ b/kernel/block/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y += block.o +obj-y += gpt.o diff --git a/kernel/block/block.c b/kernel/block/block.c index 13f2ff8..2f2aa07 100644 --- a/kernel/block/block.c +++ b/kernel/block/block.c @@ -7,6 +7,9 @@ #include +#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; } diff --git a/kernel/block/gpt.c b/kernel/block/gpt.c new file mode 100644 index 0000000..2eaf393 --- /dev/null +++ b/kernel/block/gpt.c @@ -0,0 +1,96 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +#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; +} diff --git a/kernel/block/gpt.h b/kernel/block/gpt.h new file mode 100644 index 0000000..c29c9af --- /dev/null +++ b/kernel/block/gpt.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _GPT_H_ +#define _GPT_H_ + +#include +#include + +#include + + +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_ */ diff --git a/kernel/block/partition.h b/kernel/block/partition.h new file mode 100644 index 0000000..c05fb1c --- /dev/null +++ b/kernel/block/partition.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _PARTITION_H_ +#define _PARTITION_H_ + +#include +#include + +bool block_has_gpt_header(struct block_device *bdev); + +#endif /* _PARTITION_H_ */