neotron_common_bios/
types.rs1use crate::make_ffi_enum;
28
29pub type OsStartFn = extern "C" fn(&crate::Api) -> !;
42
43#[repr(u8)]
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub enum Error {
50 InvalidDevice = 1,
52 Unimplemented,
54 DeviceError,
57 UnsupportedConfiguration,
60 NoMediaFound,
62 BlockOutOfBounds,
64}
65
66#[derive(Debug, Copy, Clone)]
68pub struct EnumConversionFail();
69
70#[repr(C)]
72#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
73pub struct Timeout(u32);
74
75#[repr(C)]
78#[derive(Debug, Clone)]
79pub struct Time {
80 pub secs: u32,
82 pub nsecs: u32,
84}
85
86#[repr(C)]
90#[derive(Debug, Clone)]
91pub struct Ticks(pub u64);
92
93make_ffi_enum!("The kinds of memory we know about",
94 MemoryKind, FfiMemoryKind, {
95 #[doc = "Read-write memory."]
96 #[doc = ""]
97 #[doc = "The OS is free to use Ram regions for code or data."]
98 Ram,
99 #[doc = "Read-only memory"]
100 #[doc = ""]
101 #[doc = "The OS is free to look inside Rom regions for ROM filing systems."]
102 Rom,
103 #[doc = "Used stack."]
104 #[doc = ""]
105 #[doc = "This is for information - the OS should not read or write here."]
106 StackUsed,
107 #[doc = "Free stack"]
108 #[doc = ""]
109 #[doc = "This is for information - the OS should not read or write here."]
110 StackFree,
111 #[doc = "Reserved memory region"]
112 #[doc = ""]
113 #[doc = "This is for information - the OS should not read or write here."]
114 Reserved
115});
116
117#[repr(C)]
119#[derive(Debug, Clone)]
120pub struct MemoryRegion {
121 pub start: *mut u8,
123 pub length: usize,
125 pub kind: FfiMemoryKind,
127}
128
129make_ffi_enum!("The kinds of power control we can do.",
130 PowerMode, FfiPowerMode, {
131 #[doc = "Turn the system power off"]
132 Off,
133 #[doc = "Reboot the main processor"]
134 Reset,
135 #[doc = "Reboot the main processor, but tell it to enter a bootloader mode"]
136 #[doc = "for programming."]
137 #[doc = ""]
138 #[doc = "Precisely what this will do will depend upon the BIOS. Some BIOSes"]
139 #[doc = "will not have a bootloader mode and this will do a regular reboot."]
140 Bootloader
141});
142
143impl Timeout {
152 pub fn new_ms(milliseconds: u32) -> Timeout {
154 Timeout(milliseconds)
155 }
156
157 pub fn new_secs(seconds: u16) -> Timeout {
159 let milliseconds = u32::from(seconds) * 1000;
160 Self::new_ms(milliseconds)
161 }
162
163 pub fn get_ms(self) -> u32 {
165 self.0
166 }
167}
168
169impl core::fmt::Display for Time {
172 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::result::Result<(), core::fmt::Error> {
173 let timestamp: chrono::DateTime<chrono::Utc> = self.into();
174 write!(f, "{}", timestamp)
175 }
176}
177
178impl From<&Time> for chrono::DateTime<chrono::Utc> {
179 fn from(time: &Time) -> Self {
180 use chrono::prelude::*;
181 let our_epoch = Utc
182 .with_ymd_and_hms(2000, 1, 1, 0, 0, 0)
183 .unwrap()
184 .timestamp();
185 chrono::Utc
186 .timestamp_opt(i64::from(time.secs) + our_epoch, time.nsecs)
187 .unwrap()
188 }
189}
190
191impl core::fmt::Display for MemoryKind {
194 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
195 write!(
196 f,
197 "{}",
198 match self {
199 MemoryKind::Rom => "ROM",
200 MemoryKind::Ram => "RAM",
201 MemoryKind::StackUsed => "StackUsed",
202 MemoryKind::StackFree => "StackFree",
203 MemoryKind::Reserved => "Reserved",
204 }
205 )
206 }
207}
208
209impl core::fmt::Display for MemoryRegion {
212 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
213 write!(
214 f,
215 "{} KiB {} @ {:p}..{:p}",
216 self.length / 1024,
217 self.kind.make_safe().unwrap_or(MemoryKind::Reserved),
218 self.start,
219 unsafe { self.start.add(self.length) },
220 )
221 }
222}
223
224