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
|
#include "devices/storage/RAMDisk/RAMDiskDevice.hpp"
#include "kapi/boot_module/boot_module.hpp"
#include "kapi/system.hpp"
#include "devices/BlockDevice.hpp"
#include <kstd/cstring>
#include <cstddef>
namespace devices::storage::ram_disk
{
namespace
{
constexpr size_t RAM_DISK_BLOCK_SIZE = 512uz; // TODO BA-FS26 really correct / good??
} // namespace
ram_disk_device::ram_disk_device() // TODO BA-FS26 remove when kstd::vector is available
: block_device(0, 0, RAM_DISK_BLOCK_SIZE)
{}
ram_disk_device::ram_disk_device(kapi::boot_modules::boot_module const & module, size_t major, size_t minor)
: block_device(major, minor, RAM_DISK_BLOCK_SIZE)
, m_boot_module(module)
{}
auto ram_disk_device::read_block(size_t block_index, void * buffer) const -> void
{
if (buffer == nullptr)
{
kapi::system::panic("[RAM DISK DEVICE] read_block called with null buffer.");
}
auto const info = calculate_transfer(block_index);
if (info.to_transfer > 0)
{
auto const src = static_cast<std::byte const *>(m_boot_module.start_address) + info.offset;
kstd::libc::memcpy(buffer, src, info.to_transfer);
}
if (info.remainder > 0)
{
kstd::libc::memset(static_cast<std::byte *>(buffer) + info.to_transfer, 0, info.remainder);
}
}
auto ram_disk_device::write_block(size_t block_index, void const * buffer) -> void
{
if (buffer == nullptr)
{
kapi::system::panic("[RAM DISK DEVICE] write_block called with null buffer.");
}
auto const info = calculate_transfer(block_index);
if (info.to_transfer > 0)
{
auto const dest = static_cast<std::byte *>(m_boot_module.start_address) + info.offset;
kstd::libc::memcpy(dest, buffer, info.to_transfer);
}
}
auto ram_disk_device::size() const -> size_t
{
return m_boot_module.size;
}
} // namespace devices::storage::ram_disk
|