Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Added Ramdisk!
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinAlavik committed Feb 22, 2024
1 parent 068cce1 commit b2425ca
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 118 deletions.
5 changes: 1 addition & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
branch = v6.x-branch-binary
[submodule "src/nighterm"]
path = src/nighterm
url = https://github.com/schkwve/nighterm-extended.git
[submodule "src/corelib/stb"]
path = src/corelib/stb
url = https://github.com/nothings/stb
url = https://github.com/schkwve/nighterm-extended.git
4 changes: 0 additions & 4 deletions limine.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ TIMEOUT=0
PROTOCOL=limine

KERNEL_PATH=boot:///Paradox
MODULE_PATH=boot:///kfont.psf
MODULE_PATH=boot:///bsod.kif
MODULE_PATH=boot:///ramdisk.tar

:Paradox (KASLR off)
Expand All @@ -14,6 +12,4 @@ TIMEOUT=0
KASLR=no

KERNEL_PATH=boot:///Paradox
MODULE_PATH=boot:///kfont.psf
MODULE_PATH=boot:///bsod.kif
MODULE_PATH=boot:///ramdisk.tar
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion ramdisk/etc/motd
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Hello, Paradox!
Welcome to Paradox!
To edit this message edit /etc/motd
7 changes: 7 additions & 0 deletions src/corelib/stdlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __STDLIB_H__
#define __STDLIB_H__

#include <system/memory/heap.h>
#include <system/memory/pmm.h>

#endif // __STDLIB_H__
56 changes: 56 additions & 0 deletions src/filesystem/ramdisk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "ramdisk.h"
#include <kernel/boot.h>
#include <printf.h>

#define RD_MODULE_NAME mod_request.response->modules[0]

ramdisk_t *init_rd(void) {
ramdisk_t *rd = (ramdisk_t *)malloc(sizeof(ramdisk_t));
if (rd == NULL) {
dprintf("[Ramdisk] Failed to allocate memory for ramdisk_t");
return NULL;
}

struct limine_file *temp_file;
temp_file = RD_MODULE_NAME;

struct Tar *tar = (struct Tar *)malloc(sizeof(struct Tar));
if (tar == NULL) {
dprintf("[Ramdisk] Failed to allocate memory for Tar");
free(rd);
return NULL;
}

extractTarData((char *)(temp_file->address), (char *)(temp_file->size), tar);

rd->content = tar; // Assign dynamically allocated tar to rd->content

if (rd->content == NULL) {
free(rd); // Free rd if content is NULL
return NULL;
}

rd->location = (uint64_t)rd;
rd->size = temp_file->size;
rd->files = rd->content->fileCount;
rd->actual_size = sizeof(ramdisk_t);

dprintf("[Ramdisk] Ramdisk located at 0x%016llX is now initialized!\n",
rd->location);

return rd;
}

struct File *rd_get_file(ramdisk_t *rd, const char *filename) {
if (rd == NULL || rd->content == NULL || filename == NULL) {
return NULL;
}

for (unsigned int i = 0; i < rd->content->fileCount; ++i) {
if (strcmp(rd->content->files[i].name, filename) == 0) {
return &(rd->content->files[i]);
}
}

return NULL;
}
21 changes: 21 additions & 0 deletions src/filesystem/ramdisk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __RAMDISK_H__
#define __RAMDISK_H__

#include <filesystem/tar.h>

#include <stdint.h>
#include <stdlib.h>
#include <strings.h>

typedef struct {
struct Tar *content;
uint64_t location;
uint32_t size;
uint16_t files;
uint32_t actual_size;
} ramdisk_t;

ramdisk_t *init_rd(void);
struct File *rd_get_file(ramdisk_t *rd, const char *filename);

#endif // __RAMDISK_H__
189 changes: 95 additions & 94 deletions src/kernel/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,116 @@
#include <nighterm/nighterm.h>
#include <printf.h>
#include <system/cpu/cpu.h>
#include <system/drivers/keyboard.h>
#include <system/idt/idt.h>
#include <system/logger/sys_log.h>
#include <system/utilities/utilities.h>
#include <system/pit/pit.h>
#include <system/pic/pic.h>
#include <system/memory/pmm.h>
#include <system/pic/pic.h>
#include <system/pit/pit.h>
#include <system/utilities/irqs.h>
#include <system/drivers/keyboard.h>
#include <system/utilities/utilities.h>

volatile struct limine_module_request mod_request = {
.id = LIMINE_MODULE_REQUEST, .revision = 0};

volatile struct limine_framebuffer_request framebuffer_request = {
.id = LIMINE_FRAMEBUFFER_REQUEST, .revision = 1};

volatile struct limine_hhdm_request hhdm_request = {
.id = LIMINE_HHDM_REQUEST,
.revision = 0};
volatile struct limine_hhdm_request hhdm_request = {.id = LIMINE_HHDM_REQUEST,
.revision = 0};

struct limine_framebuffer *framebuffer;
uint64_t hhdm_offset;
int_frame_t *cur_frame;
ramdisk_t *rd;

void init_boot(int debug_info) {
int nstatus;

hhdm_offset = hhdm_request.response->offset;
framebuffer = framebuffer_request.response->framebuffers[0];

void init_boot(int debug_info)
{
int nstatus;

hhdm_offset = hhdm_request.response->offset;
framebuffer = framebuffer_request.response->framebuffers[0];

dprintf("[System] Initialized kmsg stream.\n");
init_idt();
dprintf("[System] Initialized IDT (And IRQ)\n");
init_pmm();
dprintf("[System] Initialized PMM\n");
pit_init();
dprintf("[System] Initialized PIT\n");
init_keyboard();
dprintf("[System] Initialized Keyboard\n");
register_irqs();
dprintf("[System] Registered IRQs\n");
dprintf("\n");
dprintf("[System] Loaded modules, file count: %d\n", mod_request.response->module_count);

if (debug_info)
{
for (int files = 0; files < (int)mod_request.response->module_count; files++)
{
struct limine_file *curFile = mod_request.response->modules[files];
int size = curFile->size;
char *path = curFile->path;
dprintf("[Modules] Loaded %s (%d)!\n", path, size);
}

dprintf("[Screen] Width: %d, Height: %d\n", framebuffer->width, framebuffer->height);
dprintf("\n");
}
dprintf("[System] Starting display...\n");
if (mod_request.response->modules[0]->size == 10741)
{
dprintf("[System] Found font!\n");
dprintf("[System] Initializing Nighterm with font!\n");
nstatus = nighterm_initialize((char *)mod_request.response->modules[0]->address, framebuffer->address, framebuffer->width, framebuffer->height, framebuffer->pitch, framebuffer->bpp, NULL);
}
else
{
dprintf("[Font Error] Failed to get font, no kfont.psf? (Got size: %d)", mod_request.response->modules[0]->size);
}

if (nstatus)
{
dprintf("[System] Nightem failed to initialize, got code: %s\n", get_nighterm_return_string(nstatus));
hcf();
}
else
{
dprintf("[System] Initialized Nighterm with code: %s\n", get_nighterm_return_string(nstatus));
}

int kstatus = main(); // Launch the kernel

if (kstatus == KERNEL_QUIT_SUCCESS)
{
dprintf("[Kernel Success] Kernel quit successfully, shutting down in 10 seconds!\n");
pit_sleep(10000);
shutdown();
}
else if (kstatus == KERNEL_QUIT_ERROR)
{
dprintf("[Kernel Error] A kernel error occured, check kmesg for rason! Rebooting...\n");
reboot();
}
else if (kstatus == KERNEL_QUIT_PANIC)
{
panic("[Kernel Panic] Kernel quit with a critical error code, please see kmsg for extra info.\n", *cur_frame);
dprintf("[Kernel Panic] Kernel quit with a critical error code, the kernel process returned with a 2 that means something went really wrong, and we are shutting down your computer in 10 seconds!\n");
pit_sleep(10000);
shutdown();
}
else if (kstatus == KERNEL_QUIT_HANG)
{
hlt();
}
else
{
dprintf("[Kernel Warning] Kernel returned %d\n", kstatus);
}

// If we reach this point, something unexpected happened
dprintf("[Error] Unexpected behavior occurred, shutting down...\n");
dprintf("[System] Initialized kmsg stream.\n");
init_idt();
dprintf("[System] Initialized IDT (And IRQ)\n");
init_pmm();
dprintf("[System] Initialized PMM\n");
pit_init();
dprintf("[System] Initialized PIT\n");
init_keyboard();
dprintf("[System] Initialized Keyboard\n");
register_irqs();
dprintf("[System] Registered IRQs\n");
rd = init_rd();
dprintf("\n");
dprintf("[System] Loaded ramdisk, file count: %d\n", rd->files);
dprintf("[System] Loaded modules, file count: %d\n",
mod_request.response->module_count);

dprintf("[Screen] Width: %d, Height: %d\n", framebuffer->width,
framebuffer->height);
dprintf("\n");
dprintf("[System] Starting display...\n");

struct File *font_file = rd_get_file(rd, "ramdisk/etc/fonts/nighterm.psf");
if (font_file == NULL) {
dprintf("[System] Failed to load font! Didnt find: "
"/etc/fonts/nighterm.psf\n");
return KERNEL_QUIT_ERROR;
}

if (font_file != NULL) {
dprintf("[System] Found font!\n");
dprintf("[System] Initializing Nighterm with font!\n");

nstatus = nighterm_initialize(font_file->content, framebuffer->address,
framebuffer->width, framebuffer->height,
framebuffer->pitch, framebuffer->bpp, NULL);
} else {
dprintf("[System] Found no font!\n");
dprintf("[System] Initializing Nighterm with built in font!\n");

nstatus = nighterm_initialize(NULL, framebuffer->address,
framebuffer->width, framebuffer->height,
framebuffer->pitch, framebuffer->bpp, NULL);
}

if (nstatus) {
dprintf("[System] Nightem failed to initialize, got code: %s\n",
get_nighterm_return_string(nstatus));
hcf();
} else {
dprintf("[System] Initialized Nighterm with code: %s\n",
get_nighterm_return_string(nstatus));
}

int kstatus = main(); // Launch the kernel

if (kstatus == KERNEL_QUIT_SUCCESS) {
dprintf("[Kernel Success] Kernel quit successfully, shutting down in 10 "
"seconds!\n");
pit_sleep(10000);
shutdown();
} else if (kstatus == KERNEL_QUIT_ERROR) {
dprintf("[Kernel Error] A kernel error occured, check kmesg for rason! "
"Rebooting...\n");
reboot();
} else if (kstatus == KERNEL_QUIT_PANIC) {
panic("[Kernel Panic] Kernel quit with a critical error code, please see "
"kmsg for extra info.\n",
*cur_frame);
dprintf("[Kernel Panic] Kernel quit with a critical error code, the kernel "
"process returned with a 2 that means something went really wrong, "
"and we are shutting down your computer in 10 seconds!\n");
pit_sleep(10000);
shutdown();
} else if (kstatus == KERNEL_QUIT_HANG) {
hlt();
} else {
dprintf("[Kernel Warning] Kernel returned %d\n", kstatus);
}

// If we reach this point, something unexpected happened
dprintf("[Error] Unexpected behavior occurred, shutting down...\n");
shutdown();
}
4 changes: 3 additions & 1 deletion src/kernel/boot.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#ifndef __BOOT_H__
#define __BOOT_H__

#include <system/idt/idt.h>
#include <filesystem/ramdisk.h>
#include <limine.h>
#include <system/idt/idt.h>

extern volatile struct limine_module_request mod_request;
extern volatile struct limine_framebuffer_request framebuffer_request;
extern volatile struct limine_hhdm_request hhdm_request;
extern struct limine_framebuffer *framebuffer;
extern uint64_t hhdm_offset;
extern int_frame_t *cur_frame;
extern ramdisk_t *rd;

void init_boot(int debug_info);

Expand Down
27 changes: 15 additions & 12 deletions src/kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ return codes)
*/

// Kernel includes
#include <filesystem/ramdisk.h>
#include <filesystem/tar.h>
#include <kernel/boot.h>
#include <kernel/kernel.h>
Expand All @@ -29,22 +30,24 @@ return codes)
#include <transform.h>

int main() {
if (rd == NULL) {
dprintf("[Ramdisk] Failed to initialize ramdisk\n");
return 1;
}

const char *rawData = (char *)mod_request.response->modules[2]->address;
unsigned int dataSize = mod_request.response->modules[2]->size;

struct Tar tar;
extractTarData(rawData, dataSize, &tar);
struct File *motd = rd_get_file(rd, "ramdisk/etc/motd");

for (unsigned int i = 0; i < tar.fileCount; ++i) {
printf("%s\n", tar.files[i].name);
if (!tar.files[i].isDirectory) {
printf("\t%s\n", tar.files[i].content);
}
printf(" * Is Directory: %s\n\n", tar.files[i].isDirectory ? "Yes" : "No");
if (motd == NULL) {
dprintf("[Kernel] Failed to find motd file\n");
return KERNEL_QUIT_ERROR;
}

freeTar(&tar);
printf("%s\n", motd->content);

asm("int $2");

free(rd->content);
free(rd);

return KERNEL_QUIT_HANG;
}
2 changes: 1 addition & 1 deletion src/nighterm
Loading

0 comments on commit b2425ca

Please sign in to comment.