IgrOS-Kernel 0.1.34.545
Simple C++20 Kernel for i386/x86_64
Loading...
Searching...
No Matches
multiboot.cpp
Go to the documentation of this file.
1
11
12
13// C++
14#include <array>
15#include <bit>
16#include <string_view>
17// IgrOS-Kernel arch
18#include <arch/cpu.hpp>
19// IgrOS-Kernel arch platform
20#include <platform/platform.hpp>
21// IgrOS-Kernel klib
22#include <klib/kPrint.hpp>
23#include <klib/kVersion.hpp>
24// IgrOS-Kernel multiboot
26
27
28// Multiboot code zone
29namespace igros::multiboot {
30
31
33 namespace {
34
35
37 constexpr auto BUILD_YEAR = []() consteval noexcept {
38 auto year {0_u32};
39 for (const auto digit : std::string_view {__DATE__}.substr(7)) {
40 year = (year * 10_u32) + static_cast<igros_dword_t>(digit - '0');
41 }
42 return year;
43 }();
44
45
49 [[nodiscard]]
50 auto vbeString(const igros_dword_t pointer) noexcept -> const char* {
51 if (0_u32 == pointer) {
52 return nullptr;
53 }
54 // Linear address: segment * 16 + offset
55 return mem::direct::string((static_cast<mem::phys_t>(pointer & 0xFFFF0000_u32) >> 12) + (pointer & 0x0000FFFF_u32));
56 }
57
58
59 } // namespace
60
61
62 // Test multiboot
63 void info_t::test(const igros_dword_t magic) const noexcept {
64 // Check multiboot magic
65 if (!multiboot::check(magic)) [[unlikely]] {
66 // Write Multiboot magic error message message
67 klib::kprintf(R"multiboot(
68BAD MULTIBOOT MAGIC!!!
69 Magic: 0x%08x
70 Address: 0x%p
71)multiboot",
72 magic,
73 static_cast<const void*>(this)
74 );
75 // Hang CPU
76 arch::cpu::halt();
77 }
78 }
79
80
81 // Print multiboot flags
82 void info_t::printFlags() const noexcept {
83 // Print header
84 klib::kprintf(R"multiboot(
85MULTIBOOT header:
86 Flags: 0x%08x
87 BIOS memory map: [ %c ]
88 Boot device: [ %c ]
89 Command line: [ %c ]
90 AOUT: [ %c ]
91 ELF: [ %c ]
92 Memory map: [ %c ]
93 Drives: [ %c ]
94 Table config: [ %c ]
95 Bootloader name: [ %c ]
96 APM: [ %c ]
97 VBE: [ %c ]
98 FB: [ %c ]
99)multiboot",
100 flags.value(),
101 info_t::hasInfoMemory() ? 'Y' : 'N',
102 info_t::hasInfoBootDevice() ? 'Y' : 'N',
103 info_t::hasInfoCommandLine() ? 'Y' : 'N',
104 info_t::hasInfoAOUT() ? 'Y' : 'N',
105 info_t::hasInfoELF() ? 'Y' : 'N',
106 info_t::hasInfoMemoryMap() ? 'Y' : 'N',
107 info_t::hasInfoDrives() ? 'Y' : 'N',
108 info_t::hasInfoConfig() ? 'Y' : 'N',
109 info_t::hasInfoBootloaderName() ? 'Y' : 'N',
110 info_t::hasInfoAPM() ? 'Y' : 'N',
111 info_t::hasInfoVBE() ? 'Y' : 'N',
113 );
114 }
115
116 // Print multiboot memory info
117 void info_t::printMemInfo() const noexcept {
118 // Check if memory info exists
119 if (info_t::hasInfoMemory()) [[likely]] {
120 klib::kprintf(R"multiboot(
121MEMORY INFO:
122 Low: %d Kb.
123 High: %d Kb.
124)multiboot",
125 memLow,
126 memHigh
127 );
128 } else {
129 klib::kprintf(R"multiboot(
130MEMORY INFO:
131 No memory info provided...
132)multiboot"
133 );
134 }
135 }
136
137 // Dump multiboot memory map
138 void info_t::printMemMap() const noexcept {
139 // Check if memory map exists
140 if (info_t::hasInfoMemoryMap()) [[likely]] {
141 klib::kprintf(R"multiboot(
142MEMORY MAP:
143 Size: %u bytes
144 Addr: 0x%08x
145)multiboot",
148 );
149 // Loop through memory map
150 for (const auto &entry : info_t::memoryMap()) {
152 R"multiboot( [%u] 0x%016llx - 0x%016llx)multiboot",
153 static_cast<igros_dword_t>(entry.type),
154 static_cast<unsigned long long>(entry.address),
155 static_cast<unsigned long long>(entry.address) + entry.length
156 );
157 }
158 klib::kprintf("\n");
159 } else {
160 klib::kprintf(R"multiboot(
161MEMORY MAP:
162 No memory map provided...
163)multiboot"
164 );
165 }
166 }
167
168
169 // Print multiboot VBE info
170 void info_t::printVBEInfo() const noexcept {
171 // Test VBE
172 if (info_t::hasInfoVBE()) [[likely]] {
173 // Get VBE config info
175 // Get current VBE mode
177 // Check if VBE info is reachable
178 if ((nullptr == config) || (nullptr == mode)) [[unlikely]] {
179 klib::kprintf(R"multiboot(
180VBE:
181 VBE info is not directly mapped...
182)multiboot"
183 );
184 return;
185 }
186 // Get OEM string
187 const auto* const oem {vbeString(config->oem)};
188 // Get vendor string
189 const auto* const vendor {vbeString(config->vendor)};
190 // Get product string
191 const auto* const product {vbeString(config->productName)};
192 // Get revision string
193 const auto* const revision {vbeString(config->productRev)};
194 // Dump VBE
195 klib::kprintf(R"multiboot(
196VBE:
197 Signature: %c%c%c%c
198 Version: %u.%u
199 OEM: "%s"
200 Vendor name: "%s"
201 Card name: "%s"
202 Card rev.: "%s"
203 Current mode: #%d (%dx%d, %dbpp, 0x%08x)
204 Video memory: %d Kb.
205)multiboot",
206 config->signature[0],
207 config->signature[1],
208 config->signature[2],
209 config->signature[3],
210 (static_cast<igros_dword_t>(config->version) >> 8) & 0x00FF_u32,
211 static_cast<igros_dword_t>(config->version) & 0x00FF_u32,
212 (nullptr != oem) ? oem : "<none>",
213 (nullptr != vendor) ? vendor : "<none>",
214 (nullptr != product) ? product : "<none>",
215 (nullptr != revision) ? revision : "<none>",
217 mode->width,
218 mode->height,
219 mode->bpp,
220 mode->physbase,
221 static_cast<igros_dword_t>(config->memory) * 64_u32
222 );
223 } else {
224 klib::kprintf(R"multiboot(
225VBE:
226 No VBE info provided...
227)multiboot"
228 );
229 }
230 }
231
232 // Print multiboot FB info
233 void info_t::printFBInfo() const noexcept {
234 // Check framebuffer
235 if (info_t::hasInfoFrameBuffer()) [[likely]] {
236 // Framebuffer type name
237 const auto* fbTypeName {""};
238 // Get framebuffer type
239 switch (fbType) {
240 // Indexed
241 case 0x00_u8:
242 fbTypeName = "Indexed";
243 break;
244 // RGB
245 case 0x01_u8:
246 fbTypeName = "RGB";
247 break;
248 // Text
249 default:
250 fbTypeName = "Text";
251 break;
252 }
253 // Dump FB
254 klib::kprintf(R"multiboot(
255FrameBuffer:
256 Current mode: (%dx%d, %dbpp, %d, %s)
257 Address: 0x%016llx
258 Size: %u bytes
259)multiboot",
260 fbWidth,
261 fbHeight,
262 fbBpp,
263 fbPitch,
264 fbTypeName,
265 static_cast<unsigned long long>(fbAddress),
266 // Pitch is a line size in bytes
268 );
269 } else {
270 klib::kprintf(R"multiboot(
271FrameBuffer:
272 No FrameBuffer info provided...
273)multiboot"
274 );
275 }
276 }
277
278
279 // Print kernel header
280 void info_t::printHeader() const noexcept {
281 // Write kernel info
282 klib::kprintf(R"info(
283Kernel info:
284 Arch: %s
285 Start addr: 0x%p
286 End addr: 0x%p
287 Size: %zu Kb.
288 Build: %s, %s
289 Version: %s
290 Author: Igor Baklykov (c) %u - %u
291 Loader: "%s"
292 Command line: "%s"
293)info",
295 static_cast<const void*>(platform::Platform::kernelStart()),
296 static_cast<const void*>(platform::Platform::kernelEnd()),
298 __DATE__,
299 __TIME__,
301 2017_u32,
302 BUILD_YEAR,
305 );
306 }
307
308
309} // namespace igros::multiboot
310
static constexpr auto text() noexcept -> const char *
Kernel version to string.
Definition kVersion.hpp:76
static auto string(const phys_t phys) noexcept -> const char *
String at physical memory (nullptr if not directly mapped up to null terminator).
Definition direct.hpp:121
static auto pointer(const phys_t phys, const phys_t size=sizeof(T)) noexcept -> T *
Pointer to physical memory (nullptr if range is not directly mapped).
Definition direct.hpp:112
static constexpr auto kernelStart() noexcept -> const igros_byte_t *
Get kernel start address.
Definition platform.hpp:295
static constexpr auto kernelEnd() noexcept -> const igros_byte_t *
Get kernel end address.
Definition platform.hpp:302
static auto kernelSize() noexcept -> igros_usize_t
Get kernel size.
Definition platform.hpp:309
static auto current() noexcept -> const Platform &
Get kernel current platform (implemented in corresponding platform file).
CPU operations.
std::uint32_t igros_dword_t
32-bit unsigned integer
Definition types.hpp:43
Kernel text print functions.
IgrOS version info.
Multiboot 1 header info.
void kprintf(const char *const format,...) noexcept
Kernel printf function (arguments are checked against format at compile time).
Definition kPrint.cpp:358
igros_quad_t phys_t
Physical address (64-bit: physical memory may lie above 4 GB).
Definition phys.hpp:36
constexpr auto BUILD_YEAR
Build year (DATE is "Mmm dd yyyy").
Definition multiboot.cpp:37
auto vbeString(const igros_dword_t pointer) noexcept -> const char *
VBE string at real mode segment:offset pointer (nullptr if not provided or not directly mapped).
Definition multiboot.cpp:50
Multiboot 1 information passed by the bootloader.
auto check(const igros_dword_t signature) noexcept -> bool
Multiboot 1 header signature check function.
Definition multiboot.hpp:41
IgrOS platform description.
auto hasInfoBootDevice() const noexcept -> bool
Multiboot contains valid boot device info.
void printVBEInfo() const noexcept
Print multiboot VBE info.
auto hasInfoConfig() const noexcept -> bool
Multiboot contains valid config table info.
igros_quad_t fbAddress
Multiboot FB address.
igros_dword_t mmapAddr
Multiboot memory map start address.
igros_dword_t vbeControlInfo
Multiboot VBE control info.
igros_dword_t memLow
Multiboot bios memory low info.
void printFBInfo() const noexcept
Print multiboot FB info.
igros_dword_t fbPitch
Multiboot FB pitch.
klib::kFlags< flags_t > flags
Multiboot present features flag.
Definition multiboot.hpp:99
igros_byte_t fbBpp
Multiboot FB bpp.
auto hasInfoAOUT() const noexcept -> bool
Multiboot contains valid A.OUT sections info.
igros_byte_t fbType
Multiboot FB type.
auto hasInfoAPM() const noexcept -> bool
Multiboot contains valid APM table info.
auto loaderName() const noexcept -> const char *
Get multiboot bootloader name.
auto hasInfoELF() const noexcept -> bool
Multiboot contains valid ELF sections info.
auto hasInfoFrameBuffer() const noexcept -> bool
Multiboot contains valid FrameBuffer info.
void printMemMap() const noexcept
Print multiboot memory map.
igros_dword_t mmapLength
Multiboot memory map length.
auto hasInfoCommandLine() const noexcept -> bool
Multiboot contains valid kernel command line info.
igros_word_t vbeModeCurrent
Multiboot VBE current mode.
void printHeader() const noexcept
Print kernel header.
auto hasInfoMemory() const noexcept -> bool
Multiboot contains valid memory info.
auto commandLine() const noexcept -> const char *
Get multiboot command line.
auto hasInfoMemoryMap() const noexcept -> bool
Multiboot contains valid memory map info.
igros_dword_t vbeModeInfo
Multiboot VBE mode info.
void test(const igros_dword_t magic) const noexcept
Test multiboot.
Definition multiboot.cpp:63
igros_dword_t fbHeight
Multiboot FB height.
auto memoryMap() const noexcept -> memoryMap_t
Get multiboot memory map (empty if not provided).
igros_dword_t fbWidth
Multiboot FB width.
void printMemInfo() const noexcept
Print multiboot memory info.
Definition multiboot.cpp:98
auto hasInfoVBE() const noexcept -> bool
Multiboot contains valid VBE info.
auto hasInfoBootloaderName() const noexcept -> bool
Multiboot contains valid bootloader name info.
void printFlags() const noexcept
Print multiboot flags.
Definition multiboot.cpp:78
auto hasInfoDrives() const noexcept -> bool
Multiboot contains valid drives info.
igros_dword_t memHigh
Multiboot bios memory high info.