gitignore: add .cpio

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-05-13 04:47:59 -04:00
commit f0a9ff299b
27 changed files with 253 additions and 245 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ boot.iso
edk2-ovmf/
.config
*.hdd
*.cpio

View file

@ -32,5 +32,4 @@ void arch_init()
{
gdt_init();
idt_init();
acpi_init();
}

View file

@ -9,8 +9,6 @@
#include <lib/stdio.h>
static int num_serial_ports = 0;
void init_serial_port(int port)
{
outb(port | 1, 0x00);

View file

@ -1,5 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "kernel/page.h"
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/requests.h>
@ -81,3 +80,4 @@ void acpi_init()
acpi_load_pci();
}
DRIVER_ENTRY_PRIORITY(acpi_init, 1);

View file

@ -229,3 +229,4 @@ void ahci_init()
item = item->next;
}
}
DRIVER_ENTRY_PRIORITY(ahci_init, 3);

View file

@ -151,4 +151,4 @@ void pci_init()
krdy("PCI ready\n");
}
DRIVER_ENTRY_PRIORITY(pci_init, 1);
DRIVER_ENTRY_PRIORITY(pci_init, 2);

View file

@ -1,3 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += vfs.o
obj-y += inode.o
obj-y += ramfs.o

View file

@ -12,15 +12,18 @@ static mem_cache_t *dirent_slab = NULL;
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
{
list_item_t *p = inode->dentries.head;
dirent_t *ret = NULL;
while (p) {
dirent_t *dirent = (void *)p->ptr;
if (!strncmp(name, dirent->name, dirent->name_len))
return dirent;
if (!strncmp(name, dirent->name, dirent->name_len)) {
ret = dirent;
break;
}
p = p->next;
}
return NULL;
return ret;
}
dirent_t *dirent_create(const char *name)
@ -66,6 +69,30 @@ void inode_free(inode_t *inode)
slab_free(inode_slab, inode);
}
void inode_inc_ref(inode_t *inode)
{
write_lock(&inode->rwlock);
inode->refcount += 1;
write_unlock(&inode->rwlock);
}
void inode_dec_ref(inode_t *inode)
{
write_lock(&inode->rwlock);
inode->refcount -= 1;
write_unlock(&inode->rwlock);
if (inode->refcount == 0) {
if (inode->parent_inode) {
write_lock(&inode->parent_inode->rwlock);
inode_dec_ref(inode->parent_inode);
write_unlock(&inode->parent_inode->rwlock);
}
inode->fs->fs_ops.free_inode(inode->fs, inode);
}
}
void inode_init()
{
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);

View file

@ -1,161 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* ramfs: in-ram filesystem
*
* notes:
* inode->ino_num is a the length of the current kalloc buffer
* inode->data is a pointer to the direct data block of the file
* dirent->ino_num is a pointer to the inode
*/
#include "fs/vfs.h"
#include "lib/errno.h"
#include "lib/list.h"
#include <kernel/common.h>
#include <kernel/slab.h>
#include <lib/string.h>
#include <fs/ramfs.h>
static int alloc_inode(fs_t *fs, inode_t **out)
{
const size_t alloc_len = 0x4000;
inode_t *ret = inode_create();
if (!ret)
return -ENOMEM;
memset(ret, 0, sizeof(*ret));
ret->fs = fs;
ret->ino_num = alloc_len;
ret->data = kalloc(alloc_len, ALLOC_KERN);
*out = ret;
return 0;
}
static int free_inode(fs_t *fs, inode_t *inode)
{
inode_free(inode);
return 0;
}
static int open_inode(fs_t *fs, u64 id, inode_t **out)
{
if (!id)
return -ENOENT;
*out = (void *)id;
return 0;
}
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
{
if (offset < inode->file_size) {
size = MIN(size, inode->file_size - offset);
memcpy(buf, inode->data + offset, size);
return size;
}
return 0;
}
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
{
size_t cur_len = inode->ino_num;
if (offset + size >= cur_len) {
size_t new_size = npow2(size + offset);
void *new_buf = krealloc(inode->data, new_size, ALLOC_KERN);
if (!new_buf)
return -ENOMEM;
inode->ino_num = new_size;
inode->data = new_buf;
}
memcpy(inode->data + offset, buf, size);
return size;
}
static int creat(inode_t *inode, const char *name, u64 mode, dirent_t **out)
{
if (inode_find_dirent(inode, name))
return -EEXIST;
dirent_t *dirent = dirent_create(name);
if (!dirent)
return -ENOMEM;
inode_t *new_ino;
int ret = alloc_inode(inode->fs, &new_ino);
if (ret < 0)
goto out_1;
dirent->inode = new_ino;
inode->mode = mode;
list_insert(&inode->dentries, dirent);
*out = dirent;
return 0;
out_1:
dirent_free(dirent);
return ret;
}
static int mkdir(inode_t *inode, const char *name, dirent_t **out)
{
dirent_t *d;
int ret = creat(inode, name, INODE_DIRECTORY, &d);
if (ret >= 0)
*out = d;
return ret;
}
int link(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out)
{
dirent_t *d;
int ret = creat(inode, name, INODE_SYMLINK, &d);
if (ret >= 0)
*out = d;
return ret;
}
static int unlink(inode_t *inode, const char *name)
{
if (inode->mode != INODE_DIRECTORY)
return -ENOTDIR;
list_item_t *item = inode->dentries.head;
size_t name_len = strlen(name);
while (item) {
dirent_t *ent = item->ptr;
if (!strncmp(name, ent->name, name_len)) {
list_remove(item);
dirent_free(ent);
return 0;
}
item = item->next;
}
return -ENOENT;
}
static int readdir(inode_t *parent)
{
if (parent->mode != INODE_DIRECTORY)
return -ENOTDIR;
return 0;
}
static dirent_t *ramfs_mount(int flags, struct block_device *bdev)
{
static struct inode_ops ramfs_inode_ops = {
.read = read, .write = write, .mkdir = mkdir, .creat = creat, .link = link, .unlink = unlink, .readdir = readdir};
static struct fs_ops ramfs_ops = {.alloc_inode = alloc_inode, .free_inode = free_inode, .open_inode = open_inode};
}
void ramfs_init()
{
struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = ramfs_mount};
}
DRIVER_ENTRY_PRIORITY(ramfs_init, 2);

View file

@ -1,5 +1,4 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "lib/rbtree.h"
#include <kernel/common.h>
#include <kernel/slab.h>
@ -13,65 +12,30 @@ static fs_t *root_fs = NULL;
static list_t fs_type_list = {0};
int vfs_mount(inode_t *mount_point, fs_t *fs_to_mount)
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
{
if (!mount_point) {
if (root_fs) {
return -EBUSY;
} else {
root_fs = fs_to_mount;
}
return ino->i_ops.write(ino, buf, size, offset);
}
ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
{
return ino->i_ops.read(ino, buf, size, offset);
}
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
{
if (!mount_point || !strcmp(mount_point, "/")) {
root_fs = fs_to_mount;
} else {
}
return 0;
}
static int iter_inode_dirent(inode_t *ino, const char *name, inode_t **out)
{
fs_t *fs = ino->fs;
dirent_t *d = inode_find_dirent(ino, name);
if (d) {
if (d->inode)
*out = d->inode;
else
return fs->fs_ops.open_inode(fs, d->ino_num, out);
}
return 0;
}
int vfs_get_inode(const char *path, inode_t *ret)
{
inode_t *ino = root_fs->root_inode;
char name[512] = {0};
char c;
int i = 0;
while (i < 256 && ino) {
c = *path++;
if (c == '/' || c == '\0') {
name[i] = 0;
i = 0;
int res = iter_inode_dirent(ino, name, &ino);
if (res)
return res;
if (c == '\0')
break;
} else {
name[i] = c;
i++;
}
}
return -ENOENT;
}
void vfs_register(struct fs_type *fs)
{
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
memcpy(ins, fs, sizeof(*fs));
list_insert(&fs_type_list, ins);
}

View file

@ -342,6 +342,5 @@ struct sata_device {
};
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write);
void ahci_init();
#endif /* _AHCI_H_ */

View file

@ -34,7 +34,6 @@ struct pci_device {
};
void pci_register_host(struct pci_host *host);
void pci_init();
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);

25
include/fs/cpio.h Normal file
View file

@ -0,0 +1,25 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _CPIO_H_
#define _CPIO_H_
#define CPIO_NEWC_MAGIC "070701"
#define CPIO_TRAILER "TRAILER!!!"
struct cpio_newc_header {
char c_magic[6];
char c_ino[8];
char c_mode[8];
char c_uid[8];
char c_gid[8];
char c_nlink[8];
char c_mtime[8];
char c_filesize[8];
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8];
char c_check[8];
};
#endif /* _CPIO_H_ */

View file

@ -4,5 +4,7 @@
#include <fs/vfs.h>
dirent_t *ramfs_mount(int flags, struct block_device *bdev);
int ramfs_umount(fs_t *fs);
#endif /* _RAMFS_H_ */

View file

@ -29,6 +29,8 @@
#define INODE_SOCKET 0x6
#define INODE_SYMLINK 0x7
#define I_NO_FREE 0x1
typedef struct _inode inode_t;
typedef struct _dirent dirent_t;
typedef struct _fs fs_t;
@ -36,8 +38,7 @@ typedef struct _fs fs_t;
struct fs_type {
char name[256];
dirent_t *(*fs_mount)(int flags, struct block_device *bdev);
void (*fs_umount)(fs_t *fs);
bool (*fs_detect)(struct block_device *bdev);
int (*fs_umount)(fs_t *fs);
};
struct fs_ops {
@ -51,9 +52,9 @@ struct inode_ops {
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
/* for directory files */
int (*creat)(inode_t *inode, const char *name, u64 mode, dirent_t **out);
int (*mkdir)(inode_t *inode, const char *name, dirent_t **out);
int (*creat)(inode_t *inode, const char *name, dirent_t **out);
int (*link)(inode_t *inode, const char *name, dirent_t **out);
int (*link)(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out);
int (*unlink)(inode_t *inode, const char *name);
int (*readdir)(inode_t *parent);
};
@ -65,13 +66,19 @@ struct _fs {
inode_t *root_inode;
bool mounted;
dirent_t *mount_point;
spinlock_t lock;
struct fs_ops fs_ops;
};
struct _dirent {
inode_t *inode;
u64 ino_num;
union {
u64 ino_num;
fs_t *fs_ptr;
};
size_t rec_len;
size_t name_len;
@ -88,6 +95,8 @@ struct _inode {
u64 ino_num;
u64 file_size;
u64 flags;
list_t dentries;
void *data;
@ -99,11 +108,27 @@ struct _inode {
struct inode_ops i_ops;
};
struct ram_file {
u64 type;
u64 extra;
char *name;
size_t name_len;
void *data;
size_t data_size;
size_t file_size;
};
void vfs_init();
inode_t *inode_create();
void inode_free(inode_t *inode);
dirent_t *inode_find_dirent(inode_t *inode, const char *name);
void vfs_register(struct fs_type *fs);
dirent_t *dirent_create(const char *name);
void dirent_free(dirent_t *dirent);
dirent_t *inode_find_dirent(inode_t *inode, const char *name);
inode_t *inode_create();
void inode_free(inode_t *inode);
void inode_inc_ref(inode_t *inode);
void inode_dec_ref(inode_t *inode);
#endif /* _VFS_H_ */

View file

@ -29,6 +29,7 @@ struct mem_zone {
size_t mem_get_zones(struct mem_zone **ret);
void mem_init();
void *align(void *p, size_t align);
extern vaddr_t hhdm_start;
extern vaddr_t hhdm_end;

View file

@ -5,7 +5,10 @@
#include <kernel/spinlock.h>
typedef struct _list_item {
void *ptr;
union {
void *ptr;
u64 val;
};
struct _list_head *list;
struct _list_item *next;
struct _list_item *prev;
@ -17,7 +20,6 @@ typedef struct _list_head {
spinlock_t lock;
} list_t;
void list_create(list_t *list);
list_item_t *list_insert(list_t *list, void *item);
void list_remove(list_item_t *item);

View file

@ -10,6 +10,7 @@ size_t npow2(size_t n);
void bitmap_set(char *bitmap, size_t n, uint64_t b);
void bitmap_clear(char *bitmap, size_t n);
uint8_t bitmap_get(char *bitmap, size_t n);
u64 hex_to_int(char *ascii_hex, size_t n);
#define MIN(a, b) ({ \
typeof(a) _a = (a); \

View file

@ -15,24 +15,27 @@ typedef struct _rbnode_t {
int color;
u64 key;
u64 value;
union {
u64 value;
void *ptr;
};
} rbnode_t;
/* we use this rbtree_tstructure because the root node will potentially be
* changed when we insert or delete a node
*/
typedef struct _rbtree_t{
typedef struct _rbtree_t {
rbnode_t *root;
spinlock_t lock;
size_t num_nodes;
} rbtree_t;
rbnode_t *rbt_insert(rbtree_t*tree, u64 key);
void rbt_delete(rbtree_t*tree, rbnode_t *del);
void rbt_destroy(rbtree_t*tree);
rbnode_t *rbt_search(rbtree_t*tree, u64 key);
rbnode_t *rbt_insert(rbtree_t *tree, u64 key);
void rbt_delete(rbtree_t *tree, rbnode_t *del);
void rbt_destroy(rbtree_t *tree);
rbnode_t *rbt_search(rbtree_t *tree, u64 key);
rbnode_t *rbt_successor(rbnode_t *node);
u64 rbt_next_key(rbtree_t*tree);
u64 rbt_next_key(rbtree_t *tree);
rbnode_t *rbt_minimum(rbnode_t *node);
void rbt_init();

View file

@ -21,6 +21,8 @@ bool strempty(const char *s);
char **strsplit(const char *str, char delim, int *num);
size_t strlen(const char *c);
int atoi(const char *a);
char *dirname(const char *path);
char *basename(const char *path);
#endif /* __ASM__ */
#endif /* _STRING_H_ */

View file

@ -1,4 +1,10 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* vbos main entry point:
* VERY BAD OS: a hobby operating system
*
* Copyright 2023-2026 Michael Shaw
*/
#include <kernel/common.h>
#include <kernel/buddy.h>
#include <kernel/mem.h>
@ -42,6 +48,7 @@ static void kmain()
list_init();
kalloc_init();
vfs_init();
arch_init();
device_layer_init();
tty_layer_init();
@ -51,19 +58,17 @@ static void kmain()
int rows;
int cols;
tty_clear_screen(tty_printf);
tty_get_size(tty_printf, &rows, &cols);
tty_setlf(tty_printf);
tty_set_cursor_pos(tty_printf, 0, 0);
/* start the kernel */
arch_init();
page_init();
do_initcalls();
krdy("Welcome to vbos!\n" COL_RESET);
yield();
}

View file

@ -1,14 +1,13 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "kernel/requests.h"
#include "limine/limine.h"
#include <kernel/common.h>
#include <kernel/requests.h>
#define REQUEST __attribute__((used, section(".limine_requests"))) volatile
REQUEST struct limine_executable_address_request kern_addr_req = { .id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, .revision = 0 };
REQUEST struct limine_memmap_request memmap_req = { .id = LIMINE_MEMMAP_REQUEST_ID, .revision = 0 };
REQUEST struct limine_hhdm_request hhdm_req = { .id = LIMINE_HHDM_REQUEST_ID, .revision = 0 };
REQUEST struct limine_rsdp_request rsdp_req = { .id = LIMINE_RSDP_REQUEST_ID, .revision = 0 };
REQUEST struct limine_executable_address_request kern_addr_req = {.id = LIMINE_EXECUTABLE_ADDRESS_REQUEST_ID, .revision = 0};
REQUEST struct limine_memmap_request memmap_req = {.id = LIMINE_MEMMAP_REQUEST_ID, .revision = 0};
REQUEST struct limine_hhdm_request hhdm_req = {.id = LIMINE_HHDM_REQUEST_ID, .revision = 0};
REQUEST struct limine_rsdp_request rsdp_req = {.id = LIMINE_RSDP_REQUEST_ID, .revision = 0};
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);

View file

@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include "kernel/spinlock.h"
#include <kernel/spinlock.h>
#include <kernel/types.h>
#include <kernel/slab.h>
#include <lib/list.h>

View file

@ -24,7 +24,7 @@ size_t log2(size_t n)
inline void bitmap_set(char *bitmap, size_t n, uint64_t b)
{
if(b)
if (b)
bitmap[n >> 3] |= (1 << (n & 7));
else
bitmap[n >> 3] &= (~(1 << (n & 7)));
@ -34,3 +34,32 @@ inline uint8_t bitmap_get(char *bitmap, size_t n)
{
return bitmap[n >> 3] & (1 << (n & 7));
}
static int hexc_to_int(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'F';
else if (c >= 'a' && c <= 'f')
return c - 'a';
else
return -1;
}
u64 hex_to_int(char *ascii_hex, size_t n)
{
u64 ret = 0;
for (size_t i = 0; i < n; i++) {
char c = ascii_hex[i];
int i = hexc_to_int(c);
if (i < 0)
break;
ret <<= 4;
ret |= i;
}
return ret;
}

View file

@ -171,3 +171,75 @@ bool strempty(const char *s)
return true;
}
static char *udirname(char *path)
{
if (!path || !*path)
return ".";
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
path[--len] = '\0';
int last_slash_idx = -1;
for (int i = 0; path[i] != '\0'; i++)
if (path[i] == '/')
last_slash_idx = i;
if (last_slash_idx == -1)
return ".";
if (last_slash_idx == 0)
return "/";
path[last_slash_idx] = '\0';
return path;
}
static char *ubasename(char *path)
{
if (!path || !*path)
return ".";
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
path[--len] = '\0';
int last_slash_idx = -1;
for (int i = 0; path[i] != '\0'; i++)
if (path[i] == '/')
last_slash_idx = i;
if (last_slash_idx == -1)
return path;
if (path[last_slash_idx + 1] == '\0' && last_slash_idx == 0)
return "/";
return &path[last_slash_idx + 1];
}
char *dirname(const char *path)
{
if (!path)
return strdup(".");
char *copy = strdup(path);
char *res = udirname(copy);
char *ret = strdup(res);
kfree(copy);
return ret;
}
char *basename(const char *path)
{
if (!path)
return strdup(".");
char *copy = strdup(path);
char *res = ubasename(copy);
char *ret = strdup(res);
kfree(copy);
return ret;
}

View file

@ -5,3 +5,4 @@ timeout: 1
/vbos
protocol: limine
kernel_path: boot():/kernel.elf
module_path: boot():/initramfs.cpio

View file

@ -40,6 +40,19 @@ static void *pt_alloc()
return ret;
}
void *align(void *p, size_t align)
{
/* align must be power of 2 ! */
if (npow2(align) != align || p == NULL)
return p;
u64 i = (u64)p;
i -= 1;
i |= (align - 1);
i += 1;
return (void *)i;
}
void page_map(pgd_t *pgd_table, paddr_t paddr, vaddr_t vaddr, size_t len, u64 flags, rbtree_t *map_tree)
{
struct map_entry *insert = slab_alloc(map_entry_cache);