From 97afdf08922956627073d929bcb2529306333313 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Sat, 25 Apr 2026 23:29:27 +0200 Subject: add demo code --- kernel/src/main.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'kernel') diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 60b6e6e..9f4ec1e 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -187,6 +188,64 @@ auto run_test_code() -> void kstd::println("---------------------------------"); } +auto run_demo() -> void +{ + // 1) open a file + auto fd_1 = kapi::filesystem::open("/entrance/tickets.txt"); + if (fd_1 == -1) + { + kstd::os::panic("demo failed"); + } + + // 2) read from the file + kstd::vector 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(buffer_1.data()), static_cast(bytes_read)}; + kstd::println("Read {} bytes from /entrance/tickets.txt: {}", bytes_read, buffer_as_str); + + // 3) show that /entrance/information/info_1.txt is not accessible before mounting + auto fd_before_mount = kapi::filesystem::open("/entrance/information/info_1.txt"); + if (fd_before_mount == -1) + { + kstd::println("/entrance/information/info_1.txt is not accessible before mounting, as expected."); + } + + // 4) mount a new filesystem on top of /entrance + kapi::filesystem::mount("/dev/ram16", "/entrance"); + + // 5) open a file from the new filesystem + auto fd_2 = kapi::filesystem::open("/entrance/information/info_1.txt"); + if (fd_2 == -1) + { + kstd::os::panic("demo failed"); + } + + // 6) read from the new file + kstd::vector buffer_2{10}; + bytes_read = kapi::filesystem::read(fd_2, buffer_2.data(), buffer_2.size()); + buffer_as_str = std::string_view{reinterpret_cast(buffer_2.data()), static_cast(bytes_read)}; + kstd::println("Read {} bytes from /entrance/information/info_1.txt: {}", bytes_read, buffer_as_str); + + // 7) open device as file + auto fd_3 = kapi::filesystem::open("/dev/ram48"); + if (fd_3 == -1) + { + kstd::os::panic("demo failed"); + } + + // 8) read from the device file + kstd::vector buffer_3{2}; + bytes_read = kapi::filesystem::read(fd_3, buffer_3.data(), buffer_3.size()); + kstd::println("Read {} bytes from /dev/ram48: {::#04x}", bytes_read, buffer_3); + + // 9) write to the device file + kstd::vector write_buffer{std::byte{0xAA}, std::byte{0xAA}}; + auto bytes_written = kapi::filesystem::write(fd_3, write_buffer.data(), write_buffer.size()); + kstd::println("Written {} bytes to /dev/ram48: {::#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(); @@ -221,6 +280,10 @@ auto main() -> int kernel::filesystem::vfs::init(); kstd::println("[OS] Virtual filesystem initialized."); + // TODO BA-FS26 remove demo and test code again? + // run_demo(); + + // TODO BA-FS26 remove demo and test code again? run_test_code(); kstd::println("[TEST] All tests completed."); -- cgit v1.2.3