diff options
| author | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-15 15:35:04 +0100 |
|---|---|---|
| committer | Lukas Oesch <lukasoesch20@gmail.com> | 2026-03-17 16:43:03 +0100 |
| commit | e87963115bcdc0f0534bc2194bf3f7e3d3f3e2b6 (patch) | |
| tree | a3e059cffeba1b9cf7ae4404b1452106f5108f8a | |
| parent | 76219d593867f6e0c86c9f65ec90c3da18877e2a (diff) | |
| download | teachos-e87963115bcdc0f0534bc2194bf3f7e3d3f3e2b6.tar.xz teachos-e87963115bcdc0f0534bc2194bf3f7e3d3f3e2b6.zip | |
move test code into separate function
| -rw-r--r-- | kernel/src/main.cpp | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 07fb81b..011821a 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -4,21 +4,92 @@ #include "kapi/system.hpp" #include "devices/storage/storage_management.hpp" +#include "filesystem/device_file.hpp" #include "filesystem/file_descriptor_table.hpp" +#include "filesystem/open_file_description.hpp" #include "filesystem/vfs.hpp" #include "kernel/memory.hpp" +#include <kstd/os/error.hpp> #include <kstd/print> +#include <array> +#include <cstddef> +#include <cstdint> + +auto run_test_code() -> void +{ + // setup + auto fd_table = filesystem::file_descriptor_table::get(); + auto storage_mgmt = devices::storage::storage_management::get(); + auto device = storage_mgmt.device_by_major_minor(1, 0); + + filesystem::device_file dev_file(device); + filesystem::open_file_description ofd(&dev_file); + auto fd_index = fd_table.add_file(ofd); + + // use: read two bytes and write two again + auto fd = fd_table.get_file(fd_index); + if (!fd) + { + kstd::os::panic("test code failed"); + } + + std::array<std::byte, 2> buffer{}; + auto number_of_read_bytes = fd->read(buffer.data(), buffer.size()); + + for (size_t i = 0; i < number_of_read_bytes; ++i) + { + kstd::print("{:02x} ", static_cast<uint8_t>(buffer[i])); + + if ((i + 1) % 16 == 0) + { + kstd::println(""); + } + } + kstd::println("---"); + + // write half of the file new + std::array<std::byte, 2> write_buffer{std::byte{0xBB}, std::byte{0xAA}}; + auto written_bytes = fd->write(write_buffer.data(), write_buffer.size()); + + kstd::println("written bytes: {}", written_bytes); + + fd_table.remove_file(fd_index); + + // use: read four bytes again -> two old bytes two new bytes + filesystem::open_file_description ofd1(&dev_file); + fd_index = fd_table.add_file(ofd1); + auto fd1 = fd_table.get_file(fd_index); + + if (!fd1) + { + kstd::os::panic("test code failed"); + } + + std::array<std::byte, 4> buffer1{}; + number_of_read_bytes = fd1->read(buffer1.data(), buffer1.size()); + + for (size_t i = 0; i < number_of_read_bytes; ++i) + { + kstd::print("{:02x} ", static_cast<uint8_t>(buffer1[i])); + + if ((i + 1) % 16 == 0) + { + kstd::println(""); + } + } + kstd::println("---"); +} + auto main() -> int { kapi::cio::init(); kstd::println("[OS] IO subsystem initialized."); kapi::memory::init(); - kernel::memory::init_heap(kapi::memory::heap_base); - kstd::println("[OS] Memory subsystem initialized."); + kernel::memory::init_heap(kapi::memory::heap_base); kstd::println("[OS] Memory subsystem initialized."); kapi::system::memory_initialized(); kapi::boot_modules::init(); @@ -33,5 +104,7 @@ auto main() -> int filesystem::vfs::init(); kstd::println("[OS] Virtual filesystem initialized."); + run_test_code(); + kapi::system::panic("Returning from kernel main!"); } |
