aboutsummaryrefslogtreecommitdiff
path: root/libs/multiboot2/include/multiboot2/information.hpp
blob: d2b4c98647161b09b2616d0f4efd3504ae262e59 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef JOS_MULTIBOOT2_INFORMATION_HPP
#define JOS_MULTIBOOT2_INFORMATION_HPP

#include "impl/data.hpp"
#include "impl/iterator.hpp"
#include "impl/tag.hpp"

#include <elf/format.hpp>
#include <elf/section_header.hpp>

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

namespace multiboot2
{

  /**
   * @copydoc multiboot2::impl::basic_memory_data
   */
  struct basic_memory : impl::tag<impl::basic_memory_data>
  {
    using tag::tag;
  };

  /**
   * @copydoc multiboot2::impl::bios_boot_device_data
   */
  struct bios_boot_device : impl::tag<impl::bios_boot_device_data>
  {
    using tag::tag;
  };

  /**
   * @copydoc multiboot2::impl::command_line_data
   */
  struct command_line : impl::vla_tag<impl::command_line_data, char, std::basic_string_view>
  {
    using vla_tag::vla_tag;

    /**
     * @brief The command line string
     */
    auto string() const noexcept -> range_type
    {
      return m_vla;
    }
  };

  /**
   * @copydoc multiboot2::impl::elf_symbols_data
   */
  template<elf::format Format>
  struct elf_symbols : impl::vla_tag<impl::elf_symbols_data, elf::section_header<Format> const, std::span>
  {
    using base = impl::vla_tag<impl::elf_symbols_data, elf::section_header<Format> const, std::span>;
    using base::base;

    using iterator = base::range_type::iterator;

    auto data() const noexcept -> base::range_type
    {
      return this->m_vla;
    }
  };

  /**
   * @copydoc multiboot2::impl::loader_name_data
   */
  struct loader_name : impl::vla_tag<impl::loader_name_data, char, std::basic_string_view>
  {
    using vla_tag::vla_tag;

    /**
     * @brief The name of the bootloader
     */
    auto string() const noexcept -> std::string_view
    {
      return m_vla;
    }
  };

  /**
   * @copydoc multiboot2::impl::memory_map_data
   */
  struct memory_map : impl::vla_tag<impl::memory_map_data, impl::memory_map_data::region, std::span>
  {
    using vla_tag::vla_tag;

    using iterator = range_type::iterator;

    auto begin() const noexcept -> iterator
    {
      return regions().begin();
    }

    auto end() const noexcept -> iterator
    {
      return regions().end();
    }

    /**
     * @brief The available memory regions
     */
    auto regions() const noexcept -> range_type
    {
      return m_vla;
    }
  };

  struct information_view
  {
    using iterator = impl::information_iterator;
    using value_type = impl::information_iterator::value_type;
    using pointer = impl::information_iterator::pointer;
    using reference = impl::information_iterator::reference;

    auto size_bytes() const noexcept -> std::size_t
    {
      return m_size;
    }

    // Range access

    auto begin() const noexcept -> iterator
    {
      return iterator{&m_tags};
    }

    auto end() const noexcept -> iterator
    {
      return iterator{};
    }

    // Tag access

    template<typename Tag>
    auto has() const noexcept -> bool
    {
      return get<Tag>().has_value();
    }

    auto maybe_basic_memory() const noexcept -> std::optional<basic_memory>
    {
      return get<multiboot2::basic_memory>();
    }

    auto basic_memory() const -> basic_memory
    {
      return maybe_basic_memory().value();
    }

    auto maybe_bios_boot_device() const noexcept -> std::optional<bios_boot_device>
    {
      return get<multiboot2::bios_boot_device>();
    }

    auto bios_boot_device() const -> bios_boot_device
    {
      return maybe_bios_boot_device().value();
    }

    auto maybe_command_line() const noexcept -> std::optional<command_line>
    {
      return get<multiboot2::command_line>();
    }

    auto command_line() const -> command_line
    {
      return maybe_command_line().value();
    }

    template<elf::format Format>
    auto maybe_elf_symbols() const noexcept -> std::optional<elf_symbols<Format>>
    {
      return get<multiboot2::elf_symbols<Format>>();
    }

    template<elf::format Format>
    auto elf_symbols() const -> elf_symbols<Format>
    {
      return maybe_elf_symbols<Format>().value();
    }

    auto maybe_loader_name() const noexcept -> std::optional<loader_name>
    {
      return get<multiboot2::loader_name>();
    }

    auto loader_name() const -> loader_name
    {
      return maybe_loader_name().value();
    }

    auto maybe_memory_map() const noexcept -> std::optional<memory_map>
    {
      return get<multiboot2::memory_map>();
    }

    auto memory_map() const -> memory_map
    {
      return maybe_memory_map().value();
    }

  private:
    template<typename Tag>
    constexpr auto get() const noexcept -> std::optional<Tag>
    {
      if (auto found = std::ranges::find_if(*this, [](auto tag) { return tag.information_id() == Tag::id; });
          found != end())
      {
        return Tag{&*found};
      }
      return std::nullopt;
    }

    [[maybe_unused]] uint32_t const m_size{};
    uint32_t : 32;
    impl::tag_header const m_tags{};
  };

}  // namespace multiboot2

#endif