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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#![no_std]
#![feature(const_nonnull_new)]
#[cfg(feature = "requests-section")]
pub use limine_proc::*;
use core::cell::UnsafeCell;
use core::ffi::{c_char, CStr};
use core::fmt::Debug;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
#[derive(Debug)]
#[repr(transparent)]
pub struct LimineEntryPoint(*const ());
#[repr(transparent)]
pub struct NonNullPtr<T> {
ptr: NonNull<T>,
_phantom: PhantomData<T>,
}
impl<T> NonNullPtr<T> {
pub fn as_ptr(&self) -> *mut T {
self.ptr.as_ptr()
}
}
impl<T> Deref for NonNullPtr<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { self.ptr.as_ref() }
}
}
impl<T> DerefMut for NonNullPtr<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.ptr.as_mut() }
}
}
impl<T: Debug> Debug for NonNullPtr<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let value: &T = &*self;
f.debug_tuple("NonNullPtr")
.field(&format_args!("{:#x?}", value))
.finish()
}
}
#[repr(transparent)]
pub struct LiminePtr<T> {
ptr: Option<NonNull<T>>,
_phantom: PhantomData<T>,
}
impl<T> LiminePtr<T> {
const DEFAULT: LiminePtr<T> = Self {
ptr: None,
_phantom: PhantomData,
};
#[inline]
pub fn as_ptr(&self) -> Option<*mut T> {
Some(self.ptr?.as_ptr())
}
#[inline]
pub fn get<'a>(&self) -> Option<&'a T> {
self.ptr.map(|e| unsafe { e.as_ref() })
}
#[inline]
pub fn get_mut<'a>(&mut self) -> Option<&'a mut T> {
self.ptr.as_mut().map(|e| unsafe { e.as_mut() })
}
}
impl LiminePtr<c_char> {
pub fn to_str(&self) -> Option<&CStr> {
unsafe { Some(CStr::from_ptr(self.as_ptr()?)) }
}
}
impl LiminePtr<LimineEntryPoint> {
#[inline]
pub const fn new(entry_point: fn() -> !) -> Self {
Self {
ptr: NonNull::new(entry_point as *mut _),
_phantom: PhantomData,
}
}
}
impl<T: Debug> Debug for LiminePtr<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("LiminePtr")
.field(&format_args!("{:#x?}", self.get()))
.finish()
}
}
unsafe impl<T: Sync> Sync for LiminePtr<T> {}
type ArrayPtr<T> = NonNullPtr<NonNullPtr<T>>;
impl<T> ArrayPtr<T> {
fn into_slice<'a>(&'a self, len: usize) -> &'a [NonNullPtr<T>] {
unsafe { core::slice::from_raw_parts(self.as_ptr(), len) }
}
fn into_slice_mut<'a>(&'a mut self, len: usize) -> &'a mut [NonNullPtr<T>] {
unsafe { core::slice::from_raw_parts_mut(self.as_ptr(), len) }
}
}
macro_rules! make_struct {
(
$(#[$meta:meta])*
struct $name:ident: [$id1:expr, $id2:expr] => $response_ty:ty {
$($(#[$field_meta:meta])* $field_name:ident : $field_ty:ty = $field_default:expr),*
};
) => {
$(#[$meta])*
#[repr(C)]
#[derive(Debug)]
pub struct $name {
id: [u64; 4],
revision: u64,
response: UnsafeCell<LiminePtr<$response_ty>>,
$(pub $field_name: $field_ty),*
}
impl $name {
pub const ID: [u64; 4] = [0xc7b1dd30df4c8b88, 0x0a82e883a194f07b, $id1, $id2];
pub const fn new(revision: u64) -> Self {
Self {
id: Self::ID,
revision,
response: UnsafeCell::new(LiminePtr::DEFAULT),
$($field_name: $field_default),*
}
}
pub fn get_response(&self) -> LiminePtr<$response_ty> {
unsafe { core::ptr::read_volatile(self.response.get()) }
}
$($(#[$field_meta])* pub const fn $field_name(mut self, value: $field_ty) -> Self {
self.$field_name = value;
self
})*
}
unsafe impl Sync for $name {}
};
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct LimineUuid {
pub a: u32,
pub b: u16,
pub c: u16,
pub d: [u8; 8],
}
#[cfg(feature = "into-uuid")]
impl From<LimineUuid> for uuid::Uuid {
fn from(lim: LimineUuid) -> Self {
Self::from_fields(lim.a, lim.b, lim.c, &lim.d)
}
}
#[cfg(feature = "into-uuid")]
impl From<uuid::Uuid> for LimineUuid {
fn from(uuid: uuid::Uuid) -> Self {
let (a, b, c, d) = uuid.as_fields();
Self {
a,
b,
c,
d: d.clone(),
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct LimineFile {
pub revision: u64,
pub base: LiminePtr<u8>,
pub length: u64,
pub path: LiminePtr<c_char>,
pub cmdline: LiminePtr<c_char>,
pub media_type: u64,
pub unused: u32,
pub tftp_ip: u32,
pub tftp_port: u32,
pub partition_index: u32,
pub mbr_disk_id: u32,
pub gpt_disk_uuid: LimineUuid,
pub gpt_part_uuid: LimineUuid,
pub part_uuid: LimineUuid,
}
#[repr(C)]
#[derive(Debug)]
pub struct LimineBootInfoResponse {
pub revision: u64,
pub name: LiminePtr<c_char>,
pub version: LiminePtr<c_char>,
}
make_struct!(
struct LimineBootInfoRequest: [0xf55038d8e2a1202f, 0x279426fcf5f59740] => LimineBootInfoResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineStackSizeResponse {
pub revision: u64,
}
make_struct!(
struct LimineStackSizeRequest: [0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d] => LimineStackSizeResponse {
stack_size: u64 = 0
};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineHhdmResponse {
pub revision: u64,
pub offset: u64,
}
make_struct!(
struct LimineHhdmRequest: [0x48dcf1cb8ad2b852, 0x63984e959a98244b] => LimineHhdmResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineFramebuffer {
pub address: LiminePtr<u8>,
pub width: u64,
pub height: u64,
pub pitch: u64,
pub bpp: u16,
pub memory_model: u8,
pub red_mask_size: u8,
pub red_mask_shift: u8,
pub green_mask_size: u8,
pub green_mask_shift: u8,
pub blue_mask_size: u8,
pub blue_mask_shift: u8,
pub reserved: [u8; 7],
pub edid_size: u64,
pub edid: LiminePtr<u8>,
}
impl LimineFramebuffer {
pub fn size(&self) -> usize {
self.pitch as usize * self.height as usize * (self.bpp as usize / 8)
}
}
#[repr(C)]
#[derive(Debug)]
pub struct LimineFramebufferResponse {
pub revision: u64,
pub framebuffer_count: u64,
pub framebuffers: ArrayPtr<LimineFramebuffer>,
}
impl LimineFramebufferResponse {
pub fn framebuffers<'a>(&'a self) -> &'a [NonNullPtr<LimineFramebuffer>] {
self.framebuffers
.into_slice(self.framebuffer_count as usize)
}
}
make_struct!(
struct LimineFramebufferRequest: [0x9d5827dcd881dd75, 0xa3148604f6fab11b] => LimineFramebufferResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineTerminal {
pub cols: u64,
pub rows: u64,
pub framebuffer: LiminePtr<LimineFramebuffer>,
}
type LimineTerminalWrite =
Option<unsafe extern "C" fn(terminal: *const LimineTerminal, string: *const u8, length: u64)>;
#[repr(C)]
#[derive(Debug)]
pub struct LimineTerminalResponse {
pub revision: u64,
pub terminal_count: u64,
pub terminals: ArrayPtr<LimineTerminal>,
write_fn: LimineTerminalWrite,
}
impl LimineTerminalResponse {
pub fn terminals<'a>(&'a self) -> &'a [NonNullPtr<LimineTerminal>] {
self.terminals.into_slice(self.terminal_count as usize)
}
pub fn write(&self) -> Option<impl Fn(&LimineTerminal, &str)> {
let term_func = self.write_fn?;
Some(move |terminal: &LimineTerminal, txt: &str| unsafe {
term_func(terminal as *const _, txt.as_ptr(), txt.len() as u64);
})
}
}
make_struct!(
struct LimineTerminalRequest: [0xc8ac59310c2b0844, 0xa68d0c7265d38878] => LimineTerminalResponse {
callback: LiminePtr<()> = LiminePtr::DEFAULT
};
);
#[repr(C)]
#[derive(Debug)]
pub struct Limine5LevelPagingResponse {
pub revision: u64,
}
make_struct!(
struct Limine5LevelPagingRequest: [0x94469551da9b3192, 0xebe5e86db7382888] => Limine5LevelPagingResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineSmpInfo {
pub processor_id: u32,
pub lapic_id: u32,
pub reserved: u64,
pub goto_address: extern "C" fn(info: *const LimineSmpInfo) -> !,
pub extra_argument: u64,
}
#[repr(C)]
#[derive(Debug)]
pub struct LimineSmpResponse {
pub revision: u64,
pub flags: u32,
pub bsp_lapic_id: u32,
pub cpu_count: u64,
pub cpus: ArrayPtr<LimineSmpInfo>,
}
impl LimineSmpResponse {
pub fn cpus<'a>(&'a mut self) -> &'a mut [NonNullPtr<LimineSmpInfo>] {
self.cpus.into_slice_mut(self.cpu_count as usize)
}
}
make_struct!(
struct LimineSmpRequest: [0x95a67b819a1b857e, 0xa0b61b723b6a73e0] => LimineSmpResponse {
flags: u32 = 0
};
);
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LimineMemoryMapEntryType {
Usable = 0,
Reserved = 1,
AcpiReclaimable = 2,
AcpiNvs = 3,
BadMemory = 4,
BootloaderReclaimable = 5,
KernelAndModules = 6,
Framebuffer = 7,
}
#[repr(C)]
#[derive(Debug)]
pub struct LimineMemmapEntry {
pub base: u64,
pub len: u64,
pub typ: LimineMemoryMapEntryType,
}
#[repr(C)]
#[derive(Debug)]
pub struct LimineMemmapResponse {
pub revision: u64,
pub entry_count: u64,
pub entries: ArrayPtr<LimineMemmapEntry>,
}
impl LimineMemmapResponse {
pub fn memmap<'a>(&'a self) -> &'a [NonNullPtr<LimineMemmapEntry>] {
self.entries.into_slice(self.entry_count as usize)
}
pub fn memmap_mut<'a>(&'a mut self) -> &'a mut [NonNullPtr<LimineMemmapEntry>] {
self.entries.into_slice_mut(self.entry_count as usize)
}
}
make_struct!(
struct LimineMemmapRequest: [0x67cf3d9d378a806f, 0xe304acdfc50c3c62] => LimineMemmapResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineEntryPointResponse {
pub revision: u64,
}
make_struct!(
struct LimineEntryPointRequest: [0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a] => LimineEntryPointResponse {
entry: LiminePtr<LimineEntryPoint> = LiminePtr::DEFAULT
};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineKernelFileResponse {
pub revision: u64,
pub kernel_file: LiminePtr<LimineFile>,
}
make_struct!(
struct LimineKernelFileRequest: [0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69] => LimineKernelFileResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineModuleResponse {
pub revision: u64,
pub module_count: u64,
pub modules: ArrayPtr<LimineFile>,
}
impl LimineModuleResponse {
pub fn modules<'a>(&'a self) -> &'a [NonNullPtr<LimineFile>] {
self.modules.into_slice(self.module_count as usize)
}
}
make_struct!(
struct LimineModuleRequest: [0x3e7e279702be32af, 0xca1c4f3bd1280cee] => LimineModuleResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineRsdpResponse {
pub revision: u64,
pub address: LiminePtr<u8>,
}
make_struct!(
struct LimineRsdpRequest: [0xc5e77b6b397e7b43, 0x27637845accdcf3c] => LimineRsdpResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineSmbiosResponse {
pub revision: u64,
pub entry_32: LiminePtr<u8>,
pub entry_64: LiminePtr<u8>,
}
make_struct!(
struct LimineSmbiosRequest: [0x9e9046f11e095391, 0xaa4a520fefbde5ee] => LimineSmbiosResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineEfiSystemTableResponse {
pub revision: u64,
pub address: LiminePtr<u8>,
}
make_struct!(
struct LimineEfiSystemTableRequest: [0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc] => LimineEfiSystemTableResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineBootTimeResponse {
pub revision: u64,
pub boot_time: i64,
}
make_struct!(
struct LimineBootTimeRequest: [0x502746e184c088aa, 0xfbc5ec83e6327893] => LimineBootTimeResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineKernelAddressResponse {
pub revision: u64,
pub physical_base: u64,
pub virtual_base: u64,
}
make_struct!(
struct LimineKernelAddressRequest: [0x71ba76863cc55f63, 0xb2644a48c516a487] => LimineKernelAddressResponse {};
);
#[repr(C)]
#[derive(Debug)]
pub struct LimineDtbResponse {
pub revision: u64,
pub dtb_ptr: LiminePtr<u8>,
}
make_struct!(
struct LimineDtbRequest: [0xb40ddb48fb54bac7, 0x545081493f81ffb7] => LimineDtbResponse {};
);