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
#![no_std]
#![feature(const_fn)]
#![warn(clippy::all)]
#[cfg(not(target_arch = "x86_64"))]
compile_error!("This crate only supports the x86_64 architecture");
#[cfg(not(target_pointer_width = "64"))]
compile_error!("This crate only supports 64-bit architectures");
#[macro_use]
extern crate bitflags;
pub mod header;
pub use header::*;
pub mod framebuffer;
pub mod rsdp;
pub mod epoch;
pub mod firmware;
use framebuffer::FramebufferTag;
use rsdp::RSDPTag;
use epoch::EpochTag;
use firmware::FirmwareTag;
pub mod memory;
pub mod module;
use memory::MemoryMapTag;
use module::ModuleTag;
pub(crate) fn string_from_u8(data: &[u8]) -> Option<&str> {
use core::{slice, str};
if data[0] == 0 {
None
} else {
let mut strlen = 0;
while strlen < data.len() && data[strlen] != 0 {
strlen += 1;
}
unsafe {
Some(str::from_utf8_unchecked(slice::from_raw_parts(
(&data[0]) as *const u8,
strlen,
)))
}
}
}
pub unsafe fn load(address: usize) -> StivaleStructure {
let inner = &*(address as *const StivaleStructureInner);
StivaleStructure { inner }
}
pub struct StivaleStructure {
inner: *const StivaleStructureInner,
}
#[repr(packed)]
struct StivaleStructureInner {
bootloader_brand: [u8; 64],
bootloader_version: [u8; 64],
tags: u64,
}
impl StivaleStructure {
fn inner(&self) -> &StivaleStructureInner {
unsafe { &*self.inner }
}
fn get_tag(&self, identifier: u64) -> Option<u64> {
let mut next: *const EmptyStivaleTag = self.inner().tags as *const EmptyStivaleTag;
while !next.is_null() {
let tag = unsafe { &*next };
if tag.identifier == identifier { return Some(next as u64); }
next = tag.next as *const EmptyStivaleTag;
}
None
}
pub fn bootloader_brand(&self) -> Option<&str> {
string_from_u8(&self.inner().bootloader_brand)
}
pub fn bootloader_version(&self) -> Option<&str> {
string_from_u8(&self.inner().bootloader_version)
}
pub fn framebuffer(&self) -> Option<&FramebufferTag> {
self.get_tag(0x506461d2950408fa).map(|tag| unsafe { &*(tag as *const FramebufferTag) })
}
pub fn rsdp(&self) -> Option<&RSDPTag> {
self.get_tag(0x9e1786930a375e78).map(|tag| unsafe { &*(tag as *const RSDPTag) })
}
pub fn epoch(&self) -> Option<&EpochTag> {
self.get_tag(0x566a7bed888e1407).map(|tag| unsafe { &*(tag as *const EpochTag) })
}
pub fn firmware(&self) -> Option<&FirmwareTag> {
self.get_tag(0x359d837855e3858c).map(|tag| unsafe { &*(tag as *const FirmwareTag) })
}
pub fn memory_map(&self) -> Option<&MemoryMapTag> {
self.get_tag(0x2187f79e8612de07).map(|tag| unsafe { &*(tag as *const MemoryMapTag) })
}
pub fn module(&self) -> Option<&ModuleTag> {
self.get_tag(0x4b6fe466aade04ce).map(|tag| unsafe { &*(tag as *const ModuleTag) })
}
}
struct EmptyStivaleTag {
identifier: u64,
next: u64,
}