aboutsummaryrefslogtreecommitdiff
path: root/libs/multiboot2/include/multiboot2/information.hpp
blob: a2ded56b3749f4787dfb295c962e695b926bbe69 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef MULTIBOOT2_INFORMATION_HPP
#define MULTIBOOT2_INFORMATION_HPP

#include "information/data.hpp"      // IWYU pragma: export
#include "information/iterator.hpp"  // IWYU pragma: export
#include "information/tag.hpp"       // IWYU pragma: export

#include <kstd/units>

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

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

namespace multiboot2
{

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

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

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

    /**
     * @brief The command line string
     */
    [[nodiscard]] auto string() const noexcept -> range_type
    {
      return {data(), size()};
    }
  };

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

    [[nodiscard]] auto name(elf::section_header<Format> const & section) const noexcept -> std::string_view
    {
      if (!this->string_table_index)
      {
        std::abort();
      }

      auto string_table = this->begin()[this->string_table_index];
      auto name_offset = section.name_offset;
      auto name_array = std::bit_cast<char const *>(string_table.virtual_load_address);
      return name_array + name_offset;
    }
  };

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

    /**
     * @brief The name of the bootloader
     */
    [[nodiscard]] auto string() const noexcept -> std::string_view
    {
      return {data(), size()};
    }
  };

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

    /**
     * @brief The available memory regions
     */
    [[nodiscard]] auto regions() const noexcept -> range_type
    {
      return {data(), size()};
    }
  };

  /**
   * @copydoc multiboot2::data::module
   */
  struct module : vla_tag<data::module, char, std::basic_string_view>
  {
    using vla_tag::vla_tag;

    /**
     * @brief The module command line or name.
     */
    [[nodiscard]] auto string() const noexcept -> std::string_view
    {
      return {data(), vla_tag::size()};
    }

    [[nodiscard]] constexpr auto size() const noexcept -> kstd::units::bytes
    {
      return kstd::units::bytes{end_address - start_address};
    }
  };
  
  struct acpi_rsdp : vla_tag<data::acpi_rsdp, std::byte, std::span>
  {
    using vla_tag::vla_tag;

    [[nodiscard]] auto pointer() const noexcept -> range_type
    {
      return {data(), size()};
    }
  };

  struct acpi_xsdp : vla_tag<data::acpi_xsdp, std::byte, std::span>
  {
    using vla_tag::vla_tag;

    [[nodiscard]] auto pointer() const noexcept -> range_type
    {
      return {data(), size()};
    }
  };

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

    [[nodiscard]] auto size() const noexcept -> kstd::units::bytes
    {
      return kstd::units::bytes{m_size};
    }

    // Range access

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

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

    // Tag access

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

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

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

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

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

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

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

    template<elf::format Format>
    [[nodiscard]] auto maybe_elf_symbols() const noexcept -> std::optional<elf_symbols<Format>>
    {
      return get<multiboot2::elf_symbols<Format>>().and_then(
          [](auto x) -> std::optional<multiboot2::elf_symbols<Format>> {
            if (x.entry_size_in_B == elf::section_header_size<Format>)
            {
              return std::optional{x};
            }
            else
            {
              return std::nullopt;
            }
          });
    }

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

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

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

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

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

    [[nodiscard]] auto modules() const noexcept
    {
      auto filter_modules = [](auto const & tag) {
        return tag.information_id() == module::id;
      };
      auto transform_module = [](auto const & tag) {
        return module{&tag};
      };
      return std::ranges::subrange(begin(), end()) | std::views::filter(filter_modules) |
             std::views::transform(transform_module);
    }

    [[nodiscard]] auto maybe_acpi_rsdp() const noexcept -> std::optional<acpi_rsdp>
    {
      return get<multiboot2::acpi_rsdp>();
    }

    [[nodiscard]] auto acpi_rsdp() const noexcept -> acpi_rsdp
    {
      return maybe_acpi_rsdp().value();
    }

    [[nodiscard]] auto maybe_acpi_xsdp() const noexcept -> std::optional<acpi_xsdp>
    {
      return get<multiboot2::acpi_xsdp>();
    }

    [[nodiscard]] auto acpi_xsdp() const noexcept -> acpi_xsdp
    {
      return maybe_acpi_xsdp().value();
    }

  private:
    template<typename Tag>
    [[nodiscard]] 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;
    }

    uint32_t m_size{};
    uint32_t : 32;
    tag_header m_tags{};
  };

}  // namespace multiboot2

#endif