From 01ec30a8a44dc7af91368af45b87f2ce67555534 Mon Sep 17 00:00:00 2001 From: Atheria Date: Sun, 26 Oct 2025 16:48:20 +0700 Subject: [PATCH 1/3] Cryptography --- README.md | 1 - cmake/source.cmake | 8 +++ crypto/CRC32.c | 35 ++++++++++++ crypto/CRC32.h | 10 ++++ crypto/RNG.c | 47 ++++++++++++++++ crypto/RNG.h | 14 +++++ crypto/SHA256.c | 124 +++++++++++++++++++++++++++++++++++++++++++ crypto/SHA256.h | 20 +++++++ docs/ROADMAP.md | 10 ---- kernel/core/Kernel.c | 5 ++ 10 files changed, 263 insertions(+), 11 deletions(-) create mode 100644 crypto/CRC32.c create mode 100644 crypto/CRC32.h create mode 100644 crypto/RNG.c create mode 100644 crypto/RNG.h create mode 100644 crypto/SHA256.c create mode 100644 crypto/SHA256.h delete mode 100644 docs/ROADMAP.md diff --git a/README.md b/README.md index 48dd778..29ad173 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ --- -- Roadmap/Features: [here!](docs/ROADMAP.md) - How it works: [here!](docs/ARCHITECTURE.md) - Development Guide: [here!](docs/DEVELOPMENT.md) diff --git a/cmake/source.cmake b/cmake/source.cmake index 858a56c..9b78146 100644 --- a/cmake/source.cmake +++ b/cmake/source.cmake @@ -122,6 +122,12 @@ set(INCLUDE_SOURCES include/Io.c ) +set(CRYPTO_SOURCES + crypto/RNG.c + crypto/CRC32.c + crypto/SHA256.c +) + set(CPP_SOURCES ports/6502/6502.cpp ) @@ -181,6 +187,7 @@ include_directories( arch/x86_64/syscall vfcompositor vfcompositor/app + crypto ) # ============================================================================ @@ -209,4 +216,5 @@ set(C_SOURCES ${ARCH_SOURCES} ${INCLUDE_SOURCES} ${VFC_SOURCES} + ${CRYPTO_SOURCES} ) \ No newline at end of file diff --git a/crypto/CRC32.c b/crypto/CRC32.c new file mode 100644 index 0000000..3d129cd --- /dev/null +++ b/crypto/CRC32.c @@ -0,0 +1,35 @@ +#include "CRC32.h" + +static uint32_t crc32_table[256]; + +void CRC32Init() { + uint32_t polynomial = 0xEDB88320; + for (uint32_t i = 0; i < 256; i++) { + uint32_t c = i; + for (size_t j = 0; j < 8; j++) { + if (c & 1) { + c = polynomial ^ (c >> 1); + } else { + c >>= 1; + } + } + crc32_table[i] = c; + } +} + +uint32_t CRC32(const void* data, size_t length) { + static int table_generated = 0; + if (!table_generated) { + CRC32Init(); + table_generated = 1; + } + + uint32_t crc = 0xFFFFFFFF; + const uint8_t* p = (const uint8_t*)data; + + for (size_t i = 0; i < length; i++) { + crc = crc32_table[(crc ^ p[i]) & 0xFF] ^ (crc >> 8); + } + + return crc ^ 0xFFFFFFFF; +} diff --git a/crypto/CRC32.h b/crypto/CRC32.h new file mode 100644 index 0000000..1490916 --- /dev/null +++ b/crypto/CRC32.h @@ -0,0 +1,10 @@ +#ifndef VOIDFRAME_CRC32_H +#define VOIDFRAME_CRC32_H + +#include +#include + +uint32_t CRC32(const void* data, size_t length); +void CRC32Init(); + +#endif //VOIDFRAME_CRC32_H diff --git a/crypto/RNG.c b/crypto/RNG.c new file mode 100644 index 0000000..8721a33 --- /dev/null +++ b/crypto/RNG.c @@ -0,0 +1,47 @@ +#include "RNG.h" +#include "Io.h" + +static uint64_t s[2]; // state + +static inline uint64_t rotl(const uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +uint64_t xoroshiro128plus(void) { + uint64_t s0 = s[0]; + uint64_t s1 = s[1]; + uint64_t result = s0 + s1; + s1 ^= s0; + s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); + s[1] = rotl(s1, 36); + return result; +} + +void rng_seed(uint64_t a, uint64_t b) { + s[0] ^= a; + s[1] ^= b; +} + +int rdrand_supported(void) { + unsigned int eax, ebx, ecx, edx; + cpuid(1, &eax, &ebx, &ecx, &edx); + return (ecx >> 30) & 1; +} + +uint16_t rdrand16(void) { + uint16_t r; + __asm__ volatile("rdrand %0" : "=r"(r)); + return r; +} + +uint32_t rdrand32(void) { + uint32_t r; + __asm__ volatile("rdrand %0" : "=r"(r)); + return r; +} + +uint64_t rdrand64(void) { + uint64_t r; + __asm__ volatile("rdrand %0" : "=r"(r)); + return r; +} diff --git a/crypto/RNG.h b/crypto/RNG.h new file mode 100644 index 0000000..045dc12 --- /dev/null +++ b/crypto/RNG.h @@ -0,0 +1,14 @@ +#ifndef VOIDFRAME_RNG_H +#define VOIDFRAME_RNG_H +#include "stdint.h" + +void rng_seed(uint64_t a, uint64_t b); +uint64_t xoroshiro128plus(); + +int rdrand_supported(void); + +uint16_t rdrand16(void); +uint32_t rdrand32(void); +uint64_t rdrand64(void); + +#endif //VOIDFRAME_RNG_H \ No newline at end of file diff --git a/crypto/SHA256.c b/crypto/SHA256.c new file mode 100644 index 0000000..7c2686a --- /dev/null +++ b/crypto/SHA256.c @@ -0,0 +1,124 @@ + +#include "SHA256.h" +#include "MemOps.h" + +#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) +#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) +#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) +#define EP0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) +#define EP1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) +#define SIG0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ ((x) >> 3)) +#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10)) + +static const uint32_t k[64] = { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 +}; + +static void SHA256Transform(SHA256_CTX *ctx, const uint8_t data[]) { + uint32_t a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; + + for (i = 0, j = 0; i < 16; ++i, j += 4) + m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); + + for (; i < 64; ++i) + m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; + + a = ctx->state[0]; + b = ctx->state[1]; + c = ctx->state[2]; + d = ctx->state[3]; + e = ctx->state[4]; + f = ctx->state[5]; + g = ctx->state[6]; + h = ctx->state[7]; + + for (i = 0; i < 64; ++i) { + t1 = h + EP1(e) + CH(e, f, g) + k[i] + m[i]; + t2 = EP0(a) + MAJ(a, b, c); + h = g; + g = f; + f = e; + e = d + t1; + d = c; + c = b; + b = a; + a = t1 + t2; + } + + ctx->state[0] += a; + ctx->state[1] += b; + ctx->state[2] += c; + ctx->state[3] += d; + ctx->state[4] += e; + ctx->state[5] += f; + ctx->state[6] += g; + ctx->state[7] += h; +} + +void SHA256Init(SHA256_CTX *ctx) { + ctx->datalen = 0; + ctx->bitlen = 0; + ctx->state[0] = 0x6a09e667; + ctx->state[1] = 0xbb67ae85; + ctx->state[2] = 0x3c6ef372; + ctx->state[3] = 0xa54ff53a; + ctx->state[4] = 0x510e527f; + ctx->state[5] = 0x9b05688c; + ctx->state[6] = 0x1f83d9ab; + ctx->state[7] = 0x5be0cd19; +} + +void SHA256Update(SHA256_CTX *ctx, const uint8_t data[], size_t len) { + for (size_t i = 0; i < len; ++i) { + ctx->data[ctx->datalen] = data[i]; + ctx->datalen++; + if (ctx->datalen == 64) { + SHA256Transform(ctx, ctx->data); + ctx->bitlen += 512; + ctx->datalen = 0; + } + } +} + +void SHA256Final(SHA256_CTX *ctx, uint8_t hash[]) { + uint32_t i = ctx->datalen; + + ctx->data[i++] = 0x80; + + if (ctx->datalen < 56) { + FastMemset(ctx->data + i, 0, 56 - i); + } else { + FastMemset(ctx->data + i, 0, 64 - i); + SHA256Transform(ctx, ctx->data); + FastMemset(ctx->data, 0, 56); + } + + ctx->bitlen += ctx->datalen * 8; + ctx->data[63] = ctx->bitlen; + ctx->data[62] = ctx->bitlen >> 8; + ctx->data[61] = ctx->bitlen >> 16; + ctx->data[60] = ctx->bitlen >> 24; + ctx->data[59] = ctx->bitlen >> 32; + ctx->data[58] = ctx->bitlen >> 40; + ctx->data[57] = ctx->bitlen >> 48; + ctx->data[56] = ctx->bitlen >> 56; + SHA256Transform(ctx, ctx->data); + + for (i = 0; i < 4; ++i) { + hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0xff; + hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0xff; + hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0xff; + hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0xff; + hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0xff; + hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0xff; + hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0xff; + hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0xff; + } +} diff --git a/crypto/SHA256.h b/crypto/SHA256.h new file mode 100644 index 0000000..2c31974 --- /dev/null +++ b/crypto/SHA256.h @@ -0,0 +1,20 @@ +#ifndef VOIDFRAME_SHA256_H +#define VOIDFRAME_SHA256_H + +#include +#include + +#define SHA256_BLOCK_SIZE 32 + +typedef struct { + uint8_t data[64]; + uint32_t datalen; + uint64_t bitlen; + uint32_t state[8]; +} SHA256_CTX; + +void sha256_init(SHA256_CTX *ctx); +void SHA256Update(SHA256_CTX *ctx, const uint8_t data[], size_t len); +void SHA256Final(SHA256_CTX *ctx, uint8_t hash[]); + +#endif //VOIDFRAME_SHA256_H diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md deleted file mode 100644 index 964e31f..0000000 --- a/docs/ROADMAP.md +++ /dev/null @@ -1,10 +0,0 @@ -# VoidFrame roadmap (v0.0.2-development4) - -### Goals for v0.0.2-development4 -- Bidirectional filesystem handling -- FS and Drive management -- AHCI integration -- Tune APIC timer -- EEVDF scheduling -- Unified process management stack -- Position independent code (KASLR support) \ No newline at end of file diff --git a/kernel/core/Kernel.c b/kernel/core/Kernel.c index b63405d..5fbf3cc 100644 --- a/kernel/core/Kernel.c +++ b/kernel/core/Kernel.c @@ -5,6 +5,7 @@ #include "ACPI.h" #include "Compositor.h" #include "Console.h" +#include "CRC32.h" #include "FileSystem.h" #include "Gdt.h" #include "ISA.h" @@ -751,6 +752,10 @@ static InitResultT PXS2(void) { PrintKernelSuccess("System: Multiboot modules loaded\n"); #endif + PrintKernel("Info: Initializing CRC32...\n"); + CRC32Init(); + PrintKernelSuccess("System: CRC32 initialized\n"); + SchedulerInit(); return INIT_SUCCESS; } From 0b4e2c439878c4d01bedb099387c316bd2e70df4 Mon Sep 17 00:00:00 2001 From: Atheria Date: Sun, 26 Oct 2025 18:04:04 +0700 Subject: [PATCH 2/3] Blockdevice --- cmake/source.cmake | 3 +++ drivers/Serial.c | 38 +++++++++++++++++++++++++++++- fs/CharDevice.c | 40 +++++++++++++++++++++++++++++++ fs/CharDevice.h | 26 ++++++++++++++++++++ fs/VFS.c | 21 +++++++++++++++++ fs/devfs/DevFS.c | 56 ++++++++++++++++++++++++++++++++++++++++++++ fs/devfs/DevFS.h | 15 ++++++++++++ kernel/core/Kernel.c | 13 ---------- 8 files changed, 198 insertions(+), 14 deletions(-) create mode 100644 fs/CharDevice.c create mode 100644 fs/CharDevice.h create mode 100644 fs/devfs/DevFS.c create mode 100644 fs/devfs/DevFS.h diff --git a/cmake/source.cmake b/cmake/source.cmake index 9b78146..3e5be65 100644 --- a/cmake/source.cmake +++ b/cmake/source.cmake @@ -76,6 +76,8 @@ set(FS_SOURCES fs/FileSystem.c fs/MBR.c fs/DriveNaming.c + fs/CharDevice.c + fs/devfs/DevFS.c ) set(DRIVER_SOURCES @@ -171,6 +173,7 @@ include_directories( fs/FAT fs/EXT fs/NTFS + fs/devfs mm mm/dynamic mm/dynamic/c diff --git a/drivers/Serial.c b/drivers/Serial.c index ecdd1f9..93cac8d 100644 --- a/drivers/Serial.c +++ b/drivers/Serial.c @@ -1,5 +1,6 @@ #include "Serial.h" #include "Io.h" +#include "../fs/CharDevice.h" // Serial port register offsets #define SERIAL_DATA_REG 0 // Data register (DLAB=0) @@ -37,8 +38,43 @@ static uint16_t serial_port = COM1; static int serial_initialized = 0; +static int SerialDevRead(struct CharDevice* dev, void* buffer, uint32_t size) { + if (!serial_initialized) return -1; + char* buf = (char*)buffer; + uint32_t i = 0; + while (i < size) { + int c = SerialReadChar(); + if (c == -1) { + break; + } + buf[i++] = (char)c; + } + return i; +} + +static int SerialDevWrite(struct CharDevice* dev, const void* buffer, uint32_t size) { + if (!serial_initialized) return -1; + const char* buf = (const char*)buffer; + for (uint32_t i = 0; i < size; i++) { + if (SerialWriteChar(buf[i]) < 0) { + return i; + } + } + return size; +} + +static CharDevice_t g_serial_device = { + .name = "Serial", + .Read = SerialDevRead, + .Write = SerialDevWrite, +}; + int SerialInit(void) { - return SerialInitPort(COM1); + int result = SerialInitPort(COM1); + if (result == 0) { + CharDeviceRegister(&g_serial_device); + } + return result; } int SerialInitPort(uint16_t port) { diff --git a/fs/CharDevice.c b/fs/CharDevice.c new file mode 100644 index 0000000..36a74f1 --- /dev/null +++ b/fs/CharDevice.c @@ -0,0 +1,40 @@ +#include "CharDevice.h" +#include "../kernel/etc/StringOps.h" + +static CharDevice_t* g_char_devices[MAX_CHAR_DEVICES]; +static int g_num_char_devices = 0; + +void CharDeviceInit(void) { + for (int i = 0; i < MAX_CHAR_DEVICES; i++) { + g_char_devices[i] = 0; + } + g_num_char_devices = 0; +} + +int CharDeviceRegister(CharDevice_t* device) { + if (g_num_char_devices >= MAX_CHAR_DEVICES) { + return -1; + } + g_char_devices[g_num_char_devices++] = device; + return 0; +} + +CharDevice_t* CharDeviceFind(const char* name) { + for (int i = 0; i < g_num_char_devices; i++) { + if (FastStrCmp(g_char_devices[i]->name, name) == 0) { + return g_char_devices[i]; + } + } + return 0; +} + +CharDevice_t* CharDeviceGet(int index) { + if (index < 0 || index >= g_num_char_devices) { + return 0; + } + return g_char_devices[index]; +} + +int CharDeviceGetCount(void) { + return g_num_char_devices; +} diff --git a/fs/CharDevice.h b/fs/CharDevice.h new file mode 100644 index 0000000..9251f8f --- /dev/null +++ b/fs/CharDevice.h @@ -0,0 +1,26 @@ +#ifndef VOIDFRAME_CHARDEVICE_H +#define VOIDFRAME_CHARDEVICE_H + +#include +#include + +#define MAX_CHAR_DEVICES 32 + +struct CharDevice; + +typedef int (*CharReadFunc)(struct CharDevice* dev, void* buffer, uint32_t size); +typedef int (*CharWriteFunc)(struct CharDevice* dev, const void* buffer, uint32_t size); + +typedef struct CharDevice { + char name[32]; + CharReadFunc Read; + CharWriteFunc Write; +} CharDevice_t; + +void CharDeviceInit(void); +int CharDeviceRegister(CharDevice_t* device); +CharDevice_t* CharDeviceFind(const char* name); +CharDevice_t* CharDeviceGet(int index); +int CharDeviceGetCount(void); + +#endif //VOIDFRAME_CHARDEVICE_H diff --git a/fs/VFS.c b/fs/VFS.c index a364135..ac9a55e 100644 --- a/fs/VFS.c +++ b/fs/VFS.c @@ -1,7 +1,9 @@ #include "VFS.h" #include "../mm/MemOps.h" #include "BlockDevice.h" +#include "CharDevice.h" #include "Console.h" +#include "devfs/DevFS.h" #include "EXT/Ext2.h" #include "FAT/FAT1x.h" #include "FileSystem.h" @@ -64,6 +66,9 @@ int VfsInit(void) { } PrintKernel( "VFS: Mount table cleared\n"); + CharDeviceInit(); + PrintKernel("VFS: Char device subsystem initialized\n"); + // Register filesystems static FileSystemDriver ntfs_driver = {"NTFS", NtfsDetect, NtfsMount}; FileSystemRegister(&ntfs_driver); @@ -74,12 +79,20 @@ int VfsInit(void) { static FileSystemDriver ext2_driver = {"EXT2", Ext2Detect, Ext2Mount}; FileSystemRegister(&ext2_driver); PrintKernel("VFS: EXT2 driver registered\n"); + static FileSystemDriver devfs_driver = {"DevFS", NULL, DevfsMount}; + FileSystemRegister(&devfs_driver); + PrintKernel("VFS: DevFS driver registered\n"); int result = VfsMount("/", NULL, NULL); // RamFS doesn't need a device or driver if (result != 0) { SerialWrite("VFS: Failed to mount root\n"); } + result = VfsMount(DevicesDir, NULL, &devfs_driver); + if (result != 0) { + SerialWrite("VFS: Failed to mount /Devices\n"); + } + PrintKernelSuccess("VFS: Virtual File System initialized\n"); return 0; } @@ -154,6 +167,8 @@ int VfsReadFile(const char* path, void* buffer, uint32_t max_size) { } else if (FastStrCmp(mount->fs_driver->name, "NTFS") == 0) { NtfsSetActive(mount->device); return NtfsReadFile(local_path, buffer, max_size); + } else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) { + return DevfsReadFile(local_path, buffer, max_size); } } else { FsNode* node = FsFind(local_path); @@ -185,6 +200,8 @@ int VfsWriteFile(const char* path, const void* buffer, uint32_t size) { } else if (FastStrCmp(mount->fs_driver->name, "NTFS") == 0) { NtfsSetActive(mount->device); return NtfsWriteFile(local_path, buffer, size); + } else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) { + return DevfsWriteFile(local_path, buffer, size); } } else { int fd = FsOpen(local_path, FS_WRITE); @@ -214,6 +231,8 @@ int VfsListDir(const char* path) { } else if (FastStrCmp(mount->fs_driver->name, "NTFS") == 0) { NtfsSetActive(mount->device); return NtfsListDir(local_path); + } else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) { + return DevfsListDir(local_path); } } else { return FsListDir(local_path); @@ -319,6 +338,8 @@ int VfsIsDir(const char* path) { } else if (FastStrCmp(mount->fs_driver->name, "NTFS") == 0) { NtfsSetActive(mount->device); return NtfsIsDir(local_path); + } else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) { + return DevfsIsDir(local_path); } } else { FsNode* node = FsFind(local_path); diff --git a/fs/devfs/DevFS.c b/fs/devfs/DevFS.c new file mode 100644 index 0000000..de12153 --- /dev/null +++ b/fs/devfs/DevFS.c @@ -0,0 +1,56 @@ +#include "DevFS.h" +#include "../CharDevice.h" +#include "../../kernel/etc/Console.h" +#include "../../kernel/etc/StringOps.h" + +int DevfsMount(struct BlockDevice* device, const char* mount_point) { + // DevFS is a virtual filesystem and doesn't need a device. + (void)device; + (void)mount_point; + return 0; +} + +int DevfsReadFile(const char* path, void* buffer, uint32_t max_size) { + // The path is the device name, e.g. "/Serial" + // We need to strip the leading '/' + const char* dev_name = path + 1; + CharDevice_t* dev = CharDeviceFind(dev_name); + if (!dev || !dev->Read) { + return -1; + } + return dev->Read(dev, buffer, max_size); +} + +int DevfsWriteFile(const char* path, const void* buffer, uint32_t size) { + const char* dev_name = path + 1; + CharDevice_t* dev = CharDeviceFind(dev_name); + if (!dev || !dev->Write) { + return -1; + } + return dev->Write(dev, buffer, size); +} + +int DevfsListDir(const char* path) { + // We only support listing the root of /Devices + if (FastStrCmp(path, "/") != 0) { + return -1; + } + + int count = CharDeviceGetCount(); + for (int i = 0; i < count; i++) { + CharDevice_t* dev = CharDeviceGet(i); + if (dev) { + PrintKernel(dev->name); + PrintKernel("\n"); + } + } + return 0; +} + +int DevfsIsDir(const char* path) { + // Only the root of DevFS is considered a directory + if (FastStrCmp(path, "/") == 0) { + return 1; + } + return 0; +} diff --git a/fs/devfs/DevFS.h b/fs/devfs/DevFS.h new file mode 100644 index 0000000..cdc8841 --- /dev/null +++ b/fs/devfs/DevFS.h @@ -0,0 +1,15 @@ +#ifndef VOIDFRAME_DEVFS_H +#define VOIDFRAME_DEVFS_H + +#include "../BlockDevice.h" + +// This is a virtual filesystem, so it doesn't have a block device. +// The mount function is just a placeholder to satisfy the FileSystemDriver struct. +int DevfsMount(struct BlockDevice* device, const char* mount_point); + +int DevfsReadFile(const char* path, void* buffer, uint32_t max_size); +int DevfsWriteFile(const char* path, const void* buffer, uint32_t size); +int DevfsListDir(const char* path); +int DevfsIsDir(const char* path); + +#endif //VOIDFRAME_DEVFS_H diff --git a/kernel/core/Kernel.c b/kernel/core/Kernel.c index 5fbf3cc..b565905 100644 --- a/kernel/core/Kernel.c +++ b/kernel/core/Kernel.c @@ -760,17 +760,6 @@ static InitResultT PXS2(void) { return INIT_SUCCESS; } -void A20Test(void) { - volatile uint32_t *low = (uint32_t*)0x000000; - volatile uint32_t *high = (uint32_t*)0x100000; - - *low = 0x12345678; - *high = 0x87654321; - - if (*low == *high) PrintKernelWarning("A20 is disabled - memory is contiguous\n"); - else PrintKernelSuccess("A20 is enabled - memory is not contiguous\n"); -} - void StackUsage(void) { uintptr_t current_sp; __asm__ __volatile__("mov %%rsp, %0" : "=r"(current_sp)); @@ -786,8 +775,6 @@ asmlinkage void KernelMain(const uint32_t magic, const uint32_t info) { PANIC("Unrecognized Multiboot2 magic."); } - A20Test(); - console.buffer = (volatile uint16_t*)VGA_BUFFER_ADDR; PrintKernelSuccess("System: VoidFrame Kernel - Version 0.0.2-development4 loaded\n"); From 1e3a6f27ee137614311f70e0421f8af4881e03e5 Mon Sep 17 00:00:00 2001 From: Atheria Date: Sun, 26 Oct 2025 18:30:00 +0700 Subject: [PATCH 3/3] Minor fix --- README.md | 9 ++++++++- crypto/RNG.c | 20 +++++++++++++++----- drivers/Serial.c | 12 ++++++++++-- fs/CharDevice.c | 1 + fs/VFS.c | 3 --- kernel/core/Kernel.c | 4 ++++ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 29ad173..f817b4f 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ cd VoidFrame mkdir build cd build cmake .. -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain/-x64.cmake \ + -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain/linux-x64.cmake \ -G Ninja \ -DVF_SCHEDULER= ccmake . # Optinal, tune as needed @@ -137,6 +137,12 @@ ninja run - [x] Create - [x] Delete - [x] List +- DEVFS + - [x] Read + - [x] Write + - [x] Create (register) + - [ ] Delete (unregister) + - [x] List - ISO9660 (RO) - [x] Read - [x] List @@ -145,6 +151,7 @@ ninja run - [x] EXT2 - [x] FAT1x - [x] VFRFS + - [ ] DEVFS - [ ] ISO9660 ### Drivers - Network diff --git a/crypto/RNG.c b/crypto/RNG.c index 8721a33..1b5adc7 100644 --- a/crypto/RNG.c +++ b/crypto/RNG.c @@ -17,15 +17,25 @@ uint64_t xoroshiro128plus(void) { return result; } -void rng_seed(uint64_t a, uint64_t b) { - s[0] ^= a; - s[1] ^= b; +static uint64_t splitmix64(uint64_t* x) { + *x += 0x9e3779b97f4a7c15ULL; + uint64_t z = *x; + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; + z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; + return z ^ (z >> 31); +} + +void rng_seed(uint64_t seed_lo, uint64_t seed_hi) { + uint64_t seed = seed_lo ^ (seed_hi + 0x9e3779b97f4a7c15ULL); + s[0] ^= splitmix64(&seed); + s[1] ^= splitmix64(&seed); + if ((s[0] | s[1]) == 0) s[0] = 0x8a5cd789635d2dffULL; // ensure non-zero state } int rdrand_supported(void) { - unsigned int eax, ebx, ecx, edx; + uint32_t eax, ebx, ecx, edx; cpuid(1, &eax, &ebx, &ecx, &edx); - return (ecx >> 30) & 1; + return (ecx & (1u << 30)) != 0; } uint16_t rdrand16(void) { diff --git a/drivers/Serial.c b/drivers/Serial.c index 93cac8d..60abf7d 100644 --- a/drivers/Serial.c +++ b/drivers/Serial.c @@ -1,6 +1,7 @@ #include "Serial.h" #include "Io.h" #include "../fs/CharDevice.h" +#include "../kernel/etc/Console.h" // Serial port register offsets #define SERIAL_DATA_REG 0 // Data register (DLAB=0) @@ -38,8 +39,13 @@ static uint16_t serial_port = COM1; static int serial_initialized = 0; +// Function prototypes for internal use +int SerialInitPort(uint16_t port); + static int SerialDevRead(struct CharDevice* dev, void* buffer, uint32_t size) { - if (!serial_initialized) return -1; + if (!serial_initialized) { + return -1; + } char* buf = (char*)buffer; uint32_t i = 0; while (i < size) { @@ -53,7 +59,9 @@ static int SerialDevRead(struct CharDevice* dev, void* buffer, uint32_t size) { } static int SerialDevWrite(struct CharDevice* dev, const void* buffer, uint32_t size) { - if (!serial_initialized) return -1; + if (!serial_initialized) { + return -1; + } const char* buf = (const char*)buffer; for (uint32_t i = 0; i < size; i++) { if (SerialWriteChar(buf[i]) < 0) { diff --git a/fs/CharDevice.c b/fs/CharDevice.c index 36a74f1..42b97fe 100644 --- a/fs/CharDevice.c +++ b/fs/CharDevice.c @@ -1,5 +1,6 @@ #include "CharDevice.h" #include "../kernel/etc/StringOps.h" +#include "../../kernel/etc/Console.h" static CharDevice_t* g_char_devices[MAX_CHAR_DEVICES]; static int g_num_char_devices = 0; diff --git a/fs/VFS.c b/fs/VFS.c index ac9a55e..f298655 100644 --- a/fs/VFS.c +++ b/fs/VFS.c @@ -66,9 +66,6 @@ int VfsInit(void) { } PrintKernel( "VFS: Mount table cleared\n"); - CharDeviceInit(); - PrintKernel("VFS: Char device subsystem initialized\n"); - // Register filesystems static FileSystemDriver ntfs_driver = {"NTFS", NtfsDetect, NtfsMount}; FileSystemRegister(&ntfs_driver); diff --git a/kernel/core/Kernel.c b/kernel/core/Kernel.c index b565905..900d6ee 100644 --- a/kernel/core/Kernel.c +++ b/kernel/core/Kernel.c @@ -3,6 +3,7 @@ #include "drivers/APIC/APIC.h" #include "drivers/storage/Ide.h" #include "ACPI.h" +#include "CharDevice.h" #include "Compositor.h" #include "Console.h" #include "CRC32.h" @@ -383,6 +384,9 @@ static void PrintBootstrapSummary(void) { // Pre-eXecutionSystem 1 void PXS1(const uint32_t info) { PICMaskAll(); + CharDeviceInit(); + PrintKernel("System: Char device subsystem initialized\n"); + int sret = SerialInit(); if (sret != 0) { PrintKernelWarning("[WARN] COM1 failed, probing other COM ports...\n");