blob: 1043c81e72624a46327c51ade4cc791e548fa8d2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#include "kapi/boot_modules.hpp"
#include "kapi/cio.hpp"
#include "kapi/memory.hpp"
#include "kapi/system.hpp"
#include "kernel/devices/storage/storage_management.hpp"
#include "kernel/filesystem/device_inode.hpp"
#include "kernel/filesystem/file_descriptor_table.hpp"
#include "kernel/filesystem/open_file_description.hpp"
#include "kernel/filesystem/vfs.hpp"
#include "kernel/memory.hpp"
#include <kstd/format>
#include <kstd/memory>
#include <kstd/os/error.hpp>
#include <kstd/print>
#include <kstd/string>
#include <kstd/vector>
#include <algorithm>
#include <cstddef>
#include <cstdint>
auto test_device_names() -> void
{
auto storage_mgmt = devices::storage::storage_management::get();
std::ranges::for_each(storage_mgmt.all_controllers(), [](auto const & controller) {
std::ranges::for_each(controller->all_devices(),
[](auto const & device) { kstd::println("{}", device->name().view()); });
});
}
auto test_file_description_manually() -> 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);
auto dev_node = kstd::make_shared<filesystem::device_inode>(device);
auto ofd = kstd::make_shared<filesystem::open_file_description>(dev_node);
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");
}
kstd::vector<std::byte> buffer{2};
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
auto const value1 = std::byte{0xAA};
auto const value2 = std::byte{0xBB};
kstd::vector<std::byte> write_buffer{value1, value2};
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
auto ofd1 = kstd::make_shared<filesystem::open_file_description>(dev_node);
fd_index = fd_table.add_file(ofd1);
auto fd1 = fd_table.get_file(fd_index);
if (!fd1)
{
kstd::os::panic("test code failed");
}
kstd::vector<std::byte> buffer1{4};
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 test_device_with_vfs() -> void
{
// TODO BA-FS26
auto vfs = filesystem::vfs::get();
vfs.open("/");
}
auto run_test_code() -> void
{
test_device_names();
test_file_description_manually();
test_device_with_vfs();
}
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.");
kapi::system::memory_initialized();
kapi::boot_modules::init();
kstd::println("[OS] Boot module registry initialized.");
devices::storage::storage_management::init();
kstd::println("[OS] Storage management initialized.");
filesystem::file_descriptor_table::init();
kstd::println("[OS] Global file descriptor table initialized.");
filesystem::vfs::init();
kstd::println("[OS] Virtual filesystem initialized.");
run_test_code();
kapi::system::panic("Returning from kernel main!");
}
|