gitignore: add .cpio
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
49abcba0aa
commit
f0a9ff299b
27 changed files with 253 additions and 245 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -10,3 +10,4 @@ boot.iso
|
||||||
edk2-ovmf/
|
edk2-ovmf/
|
||||||
.config
|
.config
|
||||||
*.hdd
|
*.hdd
|
||||||
|
*.cpio
|
||||||
|
|
|
||||||
|
|
@ -32,5 +32,4 @@ void arch_init()
|
||||||
{
|
{
|
||||||
gdt_init();
|
gdt_init();
|
||||||
idt_init();
|
idt_init();
|
||||||
acpi_init();
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,6 @@
|
||||||
|
|
||||||
#include <lib/stdio.h>
|
#include <lib/stdio.h>
|
||||||
|
|
||||||
static int num_serial_ports = 0;
|
|
||||||
|
|
||||||
void init_serial_port(int port)
|
void init_serial_port(int port)
|
||||||
{
|
{
|
||||||
outb(port | 1, 0x00);
|
outb(port | 1, 0x00);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include "kernel/page.h"
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/mem.h>
|
#include <kernel/mem.h>
|
||||||
#include <kernel/requests.h>
|
#include <kernel/requests.h>
|
||||||
|
|
@ -81,3 +80,4 @@ void acpi_init()
|
||||||
|
|
||||||
acpi_load_pci();
|
acpi_load_pci();
|
||||||
}
|
}
|
||||||
|
DRIVER_ENTRY_PRIORITY(acpi_init, 1);
|
||||||
|
|
|
||||||
|
|
@ -229,3 +229,4 @@ void ahci_init()
|
||||||
item = item->next;
|
item = item->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DRIVER_ENTRY_PRIORITY(ahci_init, 3);
|
||||||
|
|
|
||||||
|
|
@ -151,4 +151,4 @@ void pci_init()
|
||||||
|
|
||||||
krdy("PCI ready\n");
|
krdy("PCI ready\n");
|
||||||
}
|
}
|
||||||
DRIVER_ENTRY_PRIORITY(pci_init, 1);
|
DRIVER_ENTRY_PRIORITY(pci_init, 2);
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
# SPDX-License-Identifier: GPL-2.0-only
|
# SPDX-License-Identifier: GPL-2.0-only
|
||||||
obj-y += vfs.o
|
obj-y += vfs.o
|
||||||
obj-y += inode.o
|
obj-y += inode.o
|
||||||
|
obj-y += ramfs.o
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,18 @@ static mem_cache_t *dirent_slab = NULL;
|
||||||
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
|
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
|
||||||
{
|
{
|
||||||
list_item_t *p = inode->dentries.head;
|
list_item_t *p = inode->dentries.head;
|
||||||
|
dirent_t *ret = NULL;
|
||||||
|
|
||||||
while (p) {
|
while (p) {
|
||||||
dirent_t *dirent = (void *)p->ptr;
|
dirent_t *dirent = (void *)p->ptr;
|
||||||
if (!strncmp(name, dirent->name, dirent->name_len))
|
if (!strncmp(name, dirent->name, dirent->name_len)) {
|
||||||
return dirent;
|
ret = dirent;
|
||||||
|
break;
|
||||||
|
}
|
||||||
p = p->next;
|
p = p->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
dirent_t *dirent_create(const char *name)
|
dirent_t *dirent_create(const char *name)
|
||||||
|
|
@ -66,6 +69,30 @@ void inode_free(inode_t *inode)
|
||||||
slab_free(inode_slab, 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()
|
void inode_init()
|
||||||
{
|
{
|
||||||
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
||||||
|
|
|
||||||
161
fs/vfs/ramfs.c
161
fs/vfs/ramfs.c
|
|
@ -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);
|
|
||||||
64
fs/vfs/vfs.c
64
fs/vfs/vfs.c
|
|
@ -1,5 +1,4 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include "lib/rbtree.h"
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
|
|
||||||
|
|
@ -13,65 +12,30 @@ static fs_t *root_fs = NULL;
|
||||||
|
|
||||||
static list_t fs_type_list = {0};
|
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) {
|
return ino->i_ops.write(ino, buf, size, offset);
|
||||||
if (root_fs) {
|
}
|
||||||
return -EBUSY;
|
|
||||||
} else {
|
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;
|
root_fs = fs_to_mount;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
name[i] = c;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return -ENOENT;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vfs_register(struct fs_type *fs)
|
void vfs_register(struct fs_type *fs)
|
||||||
{
|
{
|
||||||
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
||||||
|
|
||||||
memcpy(ins, fs, sizeof(*fs));
|
memcpy(ins, fs, sizeof(*fs));
|
||||||
list_insert(&fs_type_list, ins);
|
list_insert(&fs_type_list, ins);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
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_ */
|
#endif /* _AHCI_H_ */
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,6 @@ struct pci_device {
|
||||||
};
|
};
|
||||||
|
|
||||||
void pci_register_host(struct pci_host *host);
|
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);
|
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);
|
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 minor, u16 major, list_t *out);
|
||||||
|
|
|
||||||
25
include/fs/cpio.h
Normal file
25
include/fs/cpio.h
Normal 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_ */
|
||||||
|
|
@ -4,5 +4,7 @@
|
||||||
|
|
||||||
#include <fs/vfs.h>
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
dirent_t *ramfs_mount(int flags, struct block_device *bdev);
|
||||||
|
int ramfs_umount(fs_t *fs);
|
||||||
|
|
||||||
#endif /* _RAMFS_H_ */
|
#endif /* _RAMFS_H_ */
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,8 @@
|
||||||
#define INODE_SOCKET 0x6
|
#define INODE_SOCKET 0x6
|
||||||
#define INODE_SYMLINK 0x7
|
#define INODE_SYMLINK 0x7
|
||||||
|
|
||||||
|
#define I_NO_FREE 0x1
|
||||||
|
|
||||||
typedef struct _inode inode_t;
|
typedef struct _inode inode_t;
|
||||||
typedef struct _dirent dirent_t;
|
typedef struct _dirent dirent_t;
|
||||||
typedef struct _fs fs_t;
|
typedef struct _fs fs_t;
|
||||||
|
|
@ -36,8 +38,7 @@ typedef struct _fs fs_t;
|
||||||
struct fs_type {
|
struct fs_type {
|
||||||
char name[256];
|
char name[256];
|
||||||
dirent_t *(*fs_mount)(int flags, struct block_device *bdev);
|
dirent_t *(*fs_mount)(int flags, struct block_device *bdev);
|
||||||
void (*fs_umount)(fs_t *fs);
|
int (*fs_umount)(fs_t *fs);
|
||||||
bool (*fs_detect)(struct block_device *bdev);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs_ops {
|
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 (*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);
|
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||||
/* for directory files */
|
/* 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 (*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, inode_t *link_inode, const char *name, dirent_t **out);
|
||||||
int (*link)(inode_t *inode, const char *name, dirent_t **out);
|
|
||||||
int (*unlink)(inode_t *inode, const char *name);
|
int (*unlink)(inode_t *inode, const char *name);
|
||||||
int (*readdir)(inode_t *parent);
|
int (*readdir)(inode_t *parent);
|
||||||
};
|
};
|
||||||
|
|
@ -65,13 +66,19 @@ struct _fs {
|
||||||
|
|
||||||
inode_t *root_inode;
|
inode_t *root_inode;
|
||||||
|
|
||||||
|
bool mounted;
|
||||||
|
dirent_t *mount_point;
|
||||||
|
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
struct fs_ops fs_ops;
|
struct fs_ops fs_ops;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _dirent {
|
struct _dirent {
|
||||||
inode_t *inode;
|
inode_t *inode;
|
||||||
|
union {
|
||||||
u64 ino_num;
|
u64 ino_num;
|
||||||
|
fs_t *fs_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
size_t rec_len;
|
size_t rec_len;
|
||||||
size_t name_len;
|
size_t name_len;
|
||||||
|
|
@ -88,6 +95,8 @@ struct _inode {
|
||||||
u64 ino_num;
|
u64 ino_num;
|
||||||
u64 file_size;
|
u64 file_size;
|
||||||
|
|
||||||
|
u64 flags;
|
||||||
|
|
||||||
list_t dentries;
|
list_t dentries;
|
||||||
|
|
||||||
void *data;
|
void *data;
|
||||||
|
|
@ -99,11 +108,27 @@ struct _inode {
|
||||||
struct inode_ops i_ops;
|
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();
|
void vfs_init();
|
||||||
inode_t *inode_create();
|
void vfs_register(struct fs_type *fs);
|
||||||
void inode_free(inode_t *inode);
|
|
||||||
dirent_t *inode_find_dirent(inode_t *inode, const char *name);
|
|
||||||
dirent_t *dirent_create(const char *name);
|
dirent_t *dirent_create(const char *name);
|
||||||
void dirent_free(dirent_t *dirent);
|
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_ */
|
#endif /* _VFS_H_ */
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ struct mem_zone {
|
||||||
|
|
||||||
size_t mem_get_zones(struct mem_zone **ret);
|
size_t mem_get_zones(struct mem_zone **ret);
|
||||||
void mem_init();
|
void mem_init();
|
||||||
|
void *align(void *p, size_t align);
|
||||||
|
|
||||||
extern vaddr_t hhdm_start;
|
extern vaddr_t hhdm_start;
|
||||||
extern vaddr_t hhdm_end;
|
extern vaddr_t hhdm_end;
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,10 @@
|
||||||
#include <kernel/spinlock.h>
|
#include <kernel/spinlock.h>
|
||||||
|
|
||||||
typedef struct _list_item {
|
typedef struct _list_item {
|
||||||
|
union {
|
||||||
void *ptr;
|
void *ptr;
|
||||||
|
u64 val;
|
||||||
|
};
|
||||||
struct _list_head *list;
|
struct _list_head *list;
|
||||||
struct _list_item *next;
|
struct _list_item *next;
|
||||||
struct _list_item *prev;
|
struct _list_item *prev;
|
||||||
|
|
@ -17,7 +20,6 @@ typedef struct _list_head {
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
} list_t;
|
} list_t;
|
||||||
|
|
||||||
|
|
||||||
void list_create(list_t *list);
|
void list_create(list_t *list);
|
||||||
list_item_t *list_insert(list_t *list, void *item);
|
list_item_t *list_insert(list_t *list, void *item);
|
||||||
void list_remove(list_item_t *item);
|
void list_remove(list_item_t *item);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ size_t npow2(size_t n);
|
||||||
void bitmap_set(char *bitmap, size_t n, uint64_t b);
|
void bitmap_set(char *bitmap, size_t n, uint64_t b);
|
||||||
void bitmap_clear(char *bitmap, size_t n);
|
void bitmap_clear(char *bitmap, size_t n);
|
||||||
uint8_t bitmap_get(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) ({ \
|
#define MIN(a, b) ({ \
|
||||||
typeof(a) _a = (a); \
|
typeof(a) _a = (a); \
|
||||||
|
|
|
||||||
|
|
@ -15,24 +15,27 @@ typedef struct _rbnode_t {
|
||||||
int color;
|
int color;
|
||||||
|
|
||||||
u64 key;
|
u64 key;
|
||||||
|
union {
|
||||||
u64 value;
|
u64 value;
|
||||||
|
void *ptr;
|
||||||
|
};
|
||||||
} rbnode_t;
|
} rbnode_t;
|
||||||
|
|
||||||
/* we use this rbtree_tstructure because the root node will potentially be
|
/* we use this rbtree_tstructure because the root node will potentially be
|
||||||
* changed when we insert or delete a node
|
* changed when we insert or delete a node
|
||||||
*/
|
*/
|
||||||
typedef struct _rbtree_t{
|
typedef struct _rbtree_t {
|
||||||
rbnode_t *root;
|
rbnode_t *root;
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
size_t num_nodes;
|
size_t num_nodes;
|
||||||
} rbtree_t;
|
} rbtree_t;
|
||||||
|
|
||||||
rbnode_t *rbt_insert(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_delete(rbtree_t *tree, rbnode_t *del);
|
||||||
void rbt_destroy(rbtree_t*tree);
|
void rbt_destroy(rbtree_t *tree);
|
||||||
rbnode_t *rbt_search(rbtree_t*tree, u64 key);
|
rbnode_t *rbt_search(rbtree_t *tree, u64 key);
|
||||||
rbnode_t *rbt_successor(rbnode_t *node);
|
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);
|
rbnode_t *rbt_minimum(rbnode_t *node);
|
||||||
|
|
||||||
void rbt_init();
|
void rbt_init();
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ bool strempty(const char *s);
|
||||||
char **strsplit(const char *str, char delim, int *num);
|
char **strsplit(const char *str, char delim, int *num);
|
||||||
size_t strlen(const char *c);
|
size_t strlen(const char *c);
|
||||||
int atoi(const char *a);
|
int atoi(const char *a);
|
||||||
|
char *dirname(const char *path);
|
||||||
|
char *basename(const char *path);
|
||||||
|
|
||||||
#endif /* __ASM__ */
|
#endif /* __ASM__ */
|
||||||
#endif /* _STRING_H_ */
|
#endif /* _STRING_H_ */
|
||||||
|
|
|
||||||
11
init/main.c
11
init/main.c
|
|
@ -1,4 +1,10 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* 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/common.h>
|
||||||
#include <kernel/buddy.h>
|
#include <kernel/buddy.h>
|
||||||
#include <kernel/mem.h>
|
#include <kernel/mem.h>
|
||||||
|
|
@ -42,6 +48,7 @@ static void kmain()
|
||||||
list_init();
|
list_init();
|
||||||
kalloc_init();
|
kalloc_init();
|
||||||
vfs_init();
|
vfs_init();
|
||||||
|
arch_init();
|
||||||
device_layer_init();
|
device_layer_init();
|
||||||
tty_layer_init();
|
tty_layer_init();
|
||||||
|
|
||||||
|
|
@ -51,19 +58,17 @@ static void kmain()
|
||||||
int rows;
|
int rows;
|
||||||
int cols;
|
int cols;
|
||||||
|
|
||||||
|
tty_clear_screen(tty_printf);
|
||||||
tty_get_size(tty_printf, &rows, &cols);
|
tty_get_size(tty_printf, &rows, &cols);
|
||||||
tty_setlf(tty_printf);
|
tty_setlf(tty_printf);
|
||||||
tty_set_cursor_pos(tty_printf, 0, 0);
|
tty_set_cursor_pos(tty_printf, 0, 0);
|
||||||
|
|
||||||
/* start the kernel */
|
/* start the kernel */
|
||||||
arch_init();
|
|
||||||
|
|
||||||
page_init();
|
page_init();
|
||||||
|
|
||||||
do_initcalls();
|
do_initcalls();
|
||||||
|
|
||||||
krdy("Welcome to vbos!\n" COL_RESET);
|
|
||||||
|
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include "kernel/requests.h"
|
|
||||||
#include "limine/limine.h"
|
|
||||||
#include <kernel/common.h>
|
#include <kernel/common.h>
|
||||||
|
#include <kernel/requests.h>
|
||||||
|
|
||||||
#define REQUEST __attribute__((used, section(".limine_requests"))) volatile
|
#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_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_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_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_rsdp_request rsdp_req = {.id = LIMINE_RSDP_REQUEST_ID, .revision = 0};
|
||||||
|
|
||||||
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
|
REQUEST u64 limine_base_revision[] = LIMINE_BASE_REVISION(6);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/* SPDX-License-Identifier: GPL-2.0-only */
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||||
#include "kernel/spinlock.h"
|
#include <kernel/spinlock.h>
|
||||||
#include <kernel/types.h>
|
#include <kernel/types.h>
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
#include <lib/list.h>
|
#include <lib/list.h>
|
||||||
|
|
|
||||||
31
lib/math.c
31
lib/math.c
|
|
@ -24,7 +24,7 @@ size_t log2(size_t n)
|
||||||
|
|
||||||
inline void bitmap_set(char *bitmap, size_t n, uint64_t b)
|
inline void bitmap_set(char *bitmap, size_t n, uint64_t b)
|
||||||
{
|
{
|
||||||
if(b)
|
if (b)
|
||||||
bitmap[n >> 3] |= (1 << (n & 7));
|
bitmap[n >> 3] |= (1 << (n & 7));
|
||||||
else
|
else
|
||||||
bitmap[n >> 3] &= (~(1 << (n & 7)));
|
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));
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
72
lib/string.c
72
lib/string.c
|
|
@ -171,3 +171,75 @@ bool strempty(const char *s)
|
||||||
|
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,4 @@ timeout: 1
|
||||||
/vbos
|
/vbos
|
||||||
protocol: limine
|
protocol: limine
|
||||||
kernel_path: boot():/kernel.elf
|
kernel_path: boot():/kernel.elf
|
||||||
|
module_path: boot():/initramfs.cpio
|
||||||
|
|
|
||||||
13
mem/page.c
13
mem/page.c
|
|
@ -40,6 +40,19 @@ static void *pt_alloc()
|
||||||
return ret;
|
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)
|
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);
|
struct map_entry *insert = slab_alloc(map_entry_cache);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue