aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/main.cpp
diff options
context:
space:
mode:
authorLukas Oesch <lukas.oesch@ost.ch>2026-06-10 10:40:46 +0200
committerLukas Oesch <lukas.oesch@ost.ch>2026-06-10 10:40:46 +0200
commit33abd5cf264cb9e34121082105b0bc17b3cf7a36 (patch)
tree36b15d53fea04f4f9d9af817100f7ad013bd9b5c /kernel/src/main.cpp
parentd01caf1c4aef3c89c68b9d1cc9fe56445f0860b5 (diff)
parent7e27130c342b7299a1d2188a7192a7f17b5ac2ad (diff)
downloadkernel-33abd5cf264cb9e34121082105b0bc17b3cf7a36.tar.xz
kernel-33abd5cf264cb9e34121082105b0bc17b3cf7a36.zip
Merge branch 'develop-BA-FS26' into 'develop'HEADdevelop
Merge of BA-FS26 branch into develop See merge request teachos/kernel!49
Diffstat (limited to 'kernel/src/main.cpp')
-rw-r--r--kernel/src/main.cpp142
1 files changed, 137 insertions, 5 deletions
diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp
index 01bcbb0..8dc1349 100644
--- a/kernel/src/main.cpp
+++ b/kernel/src/main.cpp
@@ -1,20 +1,152 @@
-#include "kapi/cio.hpp"
-#include "kapi/memory.hpp"
-#include "kapi/system.hpp"
+#include "kernel/filesystem/type_registry.hpp"
+#include <kernel/devices/storage/management.hpp>
+#include <kernel/filesystem/open_file_table.hpp>
+#include <kernel/filesystem/vfs.hpp>
+#include <kernel/memory.hpp>
-#include "kernel/memory.hpp"
+#include <kapi/boot_modules.hpp>
+#include <kapi/cio.hpp>
+#include <kapi/cpu.hpp>
+#include <kapi/devices.hpp>
+#include <kapi/filesystem.hpp>
+#include <kapi/interrupts.hpp>
+#include <kapi/memory.hpp>
+#include <kapi/system.hpp>
+#include <kstd/format>
#include <kstd/print>
+#include <kstd/units>
+#include <kstd/vector>
+
+#include <cstddef>
+#include <string_view>
+
+using namespace kstd::units_literals;
+
+auto run_demo() -> void
+{
+ // 1) open a file
+ kstd::println("attempting to open /entrance/tickets.txt");
+ auto fd_1 = kapi::filesystem::open("/entrance/tickets.txt");
+ if (fd_1 == -1)
+ {
+ kapi::system::panic("demo failed");
+ }
+ else
+ {
+ kstd::println("--> successfully opened /entrance/tickets.txt with file descriptor {}", fd_1);
+ }
+
+ // 2) read from the file
+ kstd::vector<std::byte> buffer_1{10};
+ auto bytes_read = kapi::filesystem::read(fd_1, buffer_1.data(), buffer_1.size());
+ auto buffer_as_str = std::string_view{reinterpret_cast<char *>(buffer_1.data()), static_cast<size_t>(bytes_read)};
+ kstd::println("--> read {} bytes from /entrance/tickets.txt: {}", bytes_read, buffer_as_str);
+ kstd::println("");
+
+ // 3) show that /entrance/information/info_1.txt is not accessible before mounting
+ kstd::println("attempting to open /entrance/information/info_1.txt before mounting");
+ auto fd_before_mount = kapi::filesystem::open("/entrance/information/info_1.txt");
+ if (fd_before_mount == -1)
+ {
+ kstd::println("--> as expected the file could not be opened before mounting");
+ }
+
+ // 4) mount a new filesystem on top of /entrance
+ kstd::println("mount /dev/ram16 to /entrance");
+ if (kapi::filesystem::mount("/dev/ram16", "/entrance") == 0)
+ {
+ kstd::println("--> successfully mounted /dev/ram16 to /entrance");
+ }
+ else
+ {
+ kapi::system::panic("demo failed");
+ }
+ kstd::println("");
+
+ // 5) open a file from the new filesystem
+ kstd::println("attempting to open /entrance/information/info_1.txt");
+ auto fd_2 = kapi::filesystem::open("/entrance/information/info_1.txt");
+ if (fd_2 != -1)
+ {
+ kstd::println("--> successfully opened /entrance/information/info_1.txt with file descriptor {}", fd_2);
+ }
+ else
+ {
+ kapi::system::panic("demo failed");
+ }
+
+ // 6) read from the new file
+ kstd::vector<std::byte> buffer_2{10};
+ bytes_read = kapi::filesystem::read(fd_2, buffer_2.data(), buffer_2.size());
+ buffer_as_str = std::string_view{reinterpret_cast<char *>(buffer_2.data()), static_cast<size_t>(bytes_read)};
+ kstd::println("--> read {} bytes from /entrance/information/info_1.txt: {} ", bytes_read, buffer_as_str);
+
+ // 7) open device as file
+ kstd::println("attempting to open /dev/ram32 as a file");
+ auto fd_3 = kapi::filesystem::open("/dev/ram32");
+ if (fd_3 != -1)
+ {
+ kstd::println("--> successfully opened /dev/ram32 as a file with file descriptor {}", fd_3);
+ }
+ else
+ {
+ kapi::system::panic("demo failed");
+ }
+
+ // 8) read from the device file
+ kstd::vector<std::byte> buffer_3{2};
+ bytes_read = kapi::filesystem::read(fd_3, buffer_3.data(), buffer_3.size());
+ kstd::println("--> read {} bytes from /dev/ram32: {::#04x} ", bytes_read, buffer_3);
+
+ // 9) write to the device file
+ auto const default_buffer_value = std::byte{0xAA};
+ kstd::vector<std::byte> write_buffer{default_buffer_value, default_buffer_value};
+ auto bytes_written = kapi::filesystem::write(fd_3, write_buffer.data(), write_buffer.size());
+ kstd::println("--> written {} bytes to /dev/ram32: {::#04x}", bytes_written, write_buffer);
+
+ // 10) do memory dump to show that the write to the device file had an effect
+}
auto main() -> int
{
kapi::cio::init();
kstd::println("[OS] IO subsystem initialized.");
+ kapi::cpu::init();
+
kapi::memory::init();
kernel::memory::init_heap(kapi::memory::heap_base);
- kstd::println("[OS] Memory subsystem initialized.");
kapi::system::memory_initialized();
+ kapi::memory::init_mmio(kapi::memory::mmio_base, 1_GiB / kapi::memory::page::size);
+ kstd::println("[OS] Memory subsystem initialized.");
+
+ kapi::devices::init();
+ kstd::println("[OS] System root bus initialized.");
+
+ kapi::devices::init_platform_devices();
+ kstd::println("[OS] Platform devices initialized.");
+
+ kapi::interrupts::enable();
+ kstd::println("[OS] Interrupts enabled.");
+
+ kapi::boot_modules::init();
+ kstd::println("[OS] Boot module registry initialized.");
+
+ kernel::devices::storage::management::init();
+ kstd::println("[OS] Storage management initialized.");
+
+ kernel::filesystem::open_file_table::init();
+ kstd::println("[OS] Global open file table initialized.");
+
+ kernel::filesystem::type_registry::init();
+ kstd::println("[OS] Builtin filesystems registered.");
+
+ kernel::filesystem::vfs::init();
+ kstd::println("[OS] Virtual filesystem initialized.");
+
+ // TODO BA-FS26 remove demo code?
+ // run_demo();
kapi::system::panic("Returning from kernel main!");
}