aboutsummaryrefslogtreecommitdiff
path: root/kernel/devices/src/storage/RAMDisk/RAMDiskDevice.cpp
blob: 1bc475de2b2ad237a2787e79a8c91b777fde41d3 (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
#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
{
  ram_disk_device::ram_disk_device()  // TODO BA-FS26 remove when kstd::vector is available
      : block_device(0, 0)
  {}

  ram_disk_device::ram_disk_device(kapi::boot_modules::boot_module const & module, size_t major, size_t minor)
      : block_device(major, minor)
      , 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.");
    }

    // TODO BA-FS26 add bounds checking based on module size?
    // TODO BA-FS26 fill with 0 after the end of the module, if the block extends beyond it?

    auto const offset = block_index * block_size;
    auto const source = static_cast<std::byte *>(m_boot_module.start_address) + offset;

    kstd::libc::memcpy(buffer, source, block_size);
  }

  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.");
    }

    // TODO BA-FS26 add bounds checking based on module size?
    // TODO BA-FS26 ignore writes beyond the end of the module?

    auto const offset = block_index * block_size;
    auto const destination = static_cast<std::byte *>(m_boot_module.start_address) + offset;

    kstd::libc::memcpy(destination, buffer, block_size);
  }
}  // namespace devices::storage::ram_disk