aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/acpi.cpp
blob: e7c49218bf206a5407484e29fb2e5b9432fc46b3 (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
#include "kapi/acpi.hpp"

#include "kapi/system.hpp"

#include "kernel/acpi/manager.hpp"

#include <kstd/memory>

#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <string_view>

namespace kapi::acpi
{

  namespace
  {
    auto constinit manager = std::optional<kernel::acpi::manager>{};
  }  // namespace

  auto init(root_system_description_pointer const & sdp) -> bool
  {
    auto static constinit initialized = std::atomic_flag{};
    if (initialized.test_and_set())
    {
      system::panic("[OS:ACPI] The ACPI manager has already been initialized!");
    }

    manager.emplace(sdp);
    return manager->load_tables();
  }

  auto validate_checksum(std::span<std::byte const> data) -> bool
  {
    auto sum = std::ranges::fold_left(data, std::uint8_t{}, [](auto acc, auto byte) {
      return static_cast<std::uint8_t>(acc + static_cast<std::uint8_t>(byte));
    });
    return sum == 0;
  }

  auto get_table(std::string_view signature) -> kstd::observer_ptr<system_description_table_header const>
  {
    return manager->get_table(signature);
  }

};  // namespace kapi::acpi