limine 0.6.3

Rust interface for the Limine boot protocol
Documentation
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
// Copyright © 2026, Julian Scheffers
// SPDX-License-Identifier: MIT OR Apache-2.0

use core::{
    cell::UnsafeCell,
    ffi::{CStr, c_char},
    ops::Deref,
    ptr::null_mut,
};

use crate::{
    COMMON_MAGIC,
    entrypoint::EntryPoint,
    file::File,
    framebuffer::{Framebuffer, FramebufferRev1},
    memmap,
    module::InternalModule,
    paging::PagingMode,
};

/// An abstract request to the bootloader.
#[repr(C)]
pub struct Request<Resp, Req = ()> {
    magic: [u64; 2],
    id: [u64; 2],
    revision: u64,
    response: UnsafeCell<*mut Response<Resp>>,
    request: Req,
}
unsafe impl<Resp, Req> Send for Request<Resp, Req> {}
unsafe impl<Resp, Req> Sync for Request<Resp, Req> {}

impl<Resp, Req> Request<Resp, Req> {
    /// Base construction for all requests.
    /// The rationale for unsafe here is that mismatching the ID and request type is undefined behaviour, even though this is a private function.
    pub const unsafe fn new_raw(id: [u64; 2], revision: u64, request: Req) -> Self {
        Self {
            magic: COMMON_MAGIC,
            id,
            revision,
            response: UnsafeCell::new(null_mut()),
            request,
        }
    }

    /// Some requests have multiple revisions.
    /// For such requests, this number identifies what version the executable needs.
    /// The bootloader may respond with a lower revision if it doesn't support the requested revision.
    pub fn revision(&self) -> u64 {
        self.revision
    }

    /// Get the response to this request.
    pub fn response(&self) -> Option<&'static Response<Resp>> {
        unsafe { core::mem::transmute(self.response.get().read_volatile()) }
    }
}

impl<Resp, Req> Deref for Request<Resp, Req> {
    type Target = Req;

    fn deref(&self) -> &Self::Target {
        &self.request
    }
}

/// An abstract response from the bootloader.
#[repr(C)]
pub struct Response<T> {
    revision: u64,
    data: T,
}
unsafe impl<Resp> Send for Response<Resp> {}
unsafe impl<Resp> Sync for Response<Resp> {}

impl<T> Response<T> {
    pub const fn revision(&self) -> u64 {
        self.revision
    }
}

impl<T> Deref for Response<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

/* ==== REQUEST AND RESPONSE DEFINITIONS ==== */

/// Get bootloader information.
pub type BootloaderInfoRequest = Request<BootloaderInfoRespData>;

/// Name and version of the bootloader.
/// Response to [`BootloaderInfoRequest`].
pub type BootloaderInfoResponse = Response<BootloaderInfoRespData>;

#[repr(C)]
pub struct BootloaderInfoRespData {
    name: *const c_char,
    version: *const c_char,
}

impl BootloaderInfoRespData {
    pub fn name(&self) -> &str {
        unsafe { CStr::from_ptr(self.name).to_str().unwrap() }
    }

    pub fn version(&self) -> &str {
        unsafe { CStr::from_ptr(self.version).to_str().unwrap() }
    }
}

impl BootloaderInfoRequest {
    pub const fn new() -> Self {
        unsafe { Request::new_raw([0xf55038d8e2a1202f, 0x279426fcf5f59740], 0, ()) }
    }
}

/// Get the executable's command line argument string.
pub type ExecutableCmdlineRequest = Request<ExecutableCmdlineRespData>;

/// The executable's command-line argument string.
/// Response to [`ExecutableCmdlineRequest`].
pub type ExecutableCmdlineResponse = Response<ExecutableCmdlineRespData>;

#[repr(C)]
pub struct ExecutableCmdlineRespData {
    cmdline: *const c_char,
}

impl ExecutableCmdlineRespData {
    pub fn cmdline(&self) -> &str {
        unsafe { CStr::from_ptr(self.cmdline).to_str().unwrap() }
    }
}

impl ExecutableCmdlineRequest {
    pub const fn new() -> Self {
        unsafe { Request::new_raw([0x4b161536e598651e, 0xb390ad4a2f1f303a], 0, ()) }
    }
}

/// Get the firmware type.
pub type FirmwareTypeRequest = Request<FirmwareTypeRespData>;

/// The firmware type.
/// Response to [`FirmwareTypeRequest`].
pub type FirmwareTypeResponse = Response<FirmwareTypeRespData>;

#[repr(C)]
pub struct FirmwareTypeRespData {
    pub firmware_type: u64,
}

impl FirmwareTypeRequest {
    pub const fn new() -> Self {
        unsafe { Request::new_raw([0x8c2f75d90bef28a8, 0x7045a4688eac00c3], 0, ()) }
    }
}

/// Request a specific stack size.
/// Presence of the response indicates compliance to the request.
pub type StackSizeRequest = Request<StackSizeRespData, u64>;

pub struct StackSizeRespData;

impl StackSizeRequest {
    pub const fn new(size: u64) -> Self {
        unsafe { Request::new_raw([0x224ef0460a8e8926, 0xe1cb0fc25f46ea3d], 0, size) }
    }
}

/// Get information about the Higher-Half Direct Map.
pub type HhdmRequest = Request<HhdmRespData>;

/// Information about the Higher-Half Direct Map.
/// Reponse to [`HhdmRequest`].
pub type HhdmRepsonse = Response<HhdmRespData>;

#[repr(C)]
pub struct HhdmRespData {
    pub offset: u64,
}

impl HhdmRequest {
    pub const fn new() -> Self {
        unsafe { Request::new_raw([0x48dcf1cb8ad2b852, 0x63984e959a98244b], 0, ()) }
    }
}

/// Request a framebuffer be created.
pub type FramebufferRequest = Request<FramebufferRespData>;

/// Available framebuffer(s).
/// Response to [`FramebufferRequest`].
pub type FramebufferResponse = Response<FramebufferRespData>;

#[repr(C)]
pub struct FramebufferRespData {
    framebuffer_count: u64,
    framebuffers: *const (),
}

impl Response<FramebufferRespData> {
    /// Revision 0 framebuffers (with one fixed mode).
    /// You can also try [`Self::framebuffers_rev1`] to get framebuffers with multiple modes.
    pub fn framebuffers(&self) -> &[&Framebuffer] {
        unsafe {
            &*core::ptr::slice_from_raw_parts(
                self.framebuffers as _,
                self.framebuffer_count as usize,
            )
        }
    }

    /// Revision 1 framebuffers (possibly with one or more alternate modes).
    /// If this returns [`None`], use the result from [`Self::framebuffers`] instead.
    pub fn framebuffers_rev1(&self) -> Option<&[&FramebufferRev1]> {
        if self.revision < 1 {
            return None;
        }
        Some(unsafe {
            &*core::ptr::slice_from_raw_parts(
                self.framebuffers as _,
                self.framebuffer_count as usize,
            )
        })
    }
}

impl FramebufferRequest {
    pub const fn new() -> Self {
        unsafe { Request::new_raw([0x9d5827dcd881dd75, 0xa3148604f6fab11b], 0, ()) }
    }
}

/// Set paging mode request.
pub type PagingModeRequest = Request<PagingModeRespData, PagingModeReqData>;

/// Paging mode response.
/// Reponse to [`PagingModeRequest`].
pub type PagingModeResponse = Response<PagingModeRespData>;

#[repr(C)]
pub struct PagingModeReqData {
    /// Preferred mode.
    pub mode: PagingMode,
    /// Maximum mode; the bootloader will not load the OS if unsatisfied.
    pub max_mode: PagingMode,
    /// Minimum mode; the bootloader will not load the OS if unsatisfied.
    pub min_mode: PagingMode,
}

#[repr(C)]
pub struct PagingModeRespData {
    /// Currently active paging mode.
    pub mode: PagingMode,
}

impl PagingModeRequest {
    /// Request that accepts any mode but prefers the maximum available paging mode.
    pub const PREFER_MAXIMUM: PagingModeRequest =
        Self::new(PagingMode::MAX, PagingMode::MAX, PagingMode::MIN);

    pub const fn new(mode: PagingMode, max_mode: PagingMode, min_mode: PagingMode) -> Self {
        if !((min_mode as u64) <= (mode as u64) && (mode as u64) <= (max_mode as u64)) {
            // Paging mode request preconditions violated: min_mode <= mode <= max_mode.
            loop {}
        }
        unsafe {
            Self::new_raw(
                [0x95c1a0edab0944cb, 0xa4e5cb3842f7488a],
                0,
                PagingModeReqData {
                    mode,
                    max_mode,
                    min_mode,
                },
            )
        }
    }

    /// Request that accepts one exact paging mode.
    pub const fn new_exact(mode: PagingMode) -> Self {
        Self::new(mode, mode, mode)
    }
}

/// Intialize Multi-Processing request.
pub type MpRequest = Request<MpRespData, u64>;

/// Multi-Processing response.
/// Reponse to [`MpRequest`].
pub type MpResponse = Response<MpRespData>;

// Unlike other requests, the response format is arch-specific, so it's not defined here.
pub use crate::mp::MpRespData;

impl MpRequest {
    pub const fn new(flags: u64) -> Self {
        unsafe { Self::new_raw([0x95a67b819a1b857e, 0xa0b61b723b6a73e0], 0, flags) }
    }
}

/// Get memory-map request.
pub type MemmapRequest = Request<MemmapRespData>;

/// Memory-map response.
/// Reponse to [`MemmapRequest`].
pub type MemmapResponse = Response<MemmapRespData>;

#[repr(C)]
pub struct MemmapRespData {
    entry_count: u64,
    entries: *const (),
}

impl MemmapRespData {
    pub fn entries(&self) -> &[&memmap::Entry] {
        unsafe { &*core::ptr::slice_from_raw_parts(self.entries as _, self.entry_count as usize) }
    }
}

impl MemmapRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x67cf3d9d378a806f, 0xe304acdfc50c3c62], 0, ()) }
    }
}

/// Alternate kernel entrypoint request.
/// Overrides the executable file's entrypoint address.
/// Presence of the response indicates compliance to the request.
pub type EntryPointRequest = Request<EntrypointRespData, EntryPoint>;

pub struct EntrypointRespData;

impl EntryPointRequest {
    pub const fn new(entry: EntryPoint) -> Self {
        unsafe { Self::new_raw([0x13d86c035a1cd3e1, 0x2b0caa89d8f3026a], 0, entry) }
    }
}

/// Get executable file information request.
pub type ExecutableFileRequest = Request<ExecutableFileRespData>;

/// Executable file information response.
/// Reponse to [`ExecutableFileRequest`].
pub type ExecutableFileResponse = Response<ExecutableFileRespData>;

#[repr(C)]
pub struct ExecutableFileRespData {
    executable_file: *const File,
}

impl ExecutableFileRespData {
    pub fn executable_file(&self) -> &File {
        unsafe { &*self.executable_file }
    }
}

impl ExecutableFileRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0xad97e90e83f1ed67, 0x31eb5d1c5ff23b69], 0, ()) }
    }
}

/// Get modules request.
pub type ModulesRequest = Request<ModulesRespData, ModulesReqData>;

/// Loaded modules response.
/// Response to [`ModulesRequest`].
pub type ModulesResponse = Response<ModulesRespData>;

#[repr(C)]
pub struct ModulesReqData {
    internal_module_count: u64,
    internal_modules: *const *const InternalModule,
}

#[repr(C)]
pub struct ModulesRespData {
    module_count: u64,
    modules: *const (),
}

impl ModulesRespData {
    pub fn modules(&self) -> &[&File] {
        unsafe { &*core::ptr::slice_from_raw_parts(self.modules as _, self.module_count as usize) }
    }
}

impl ModulesRequest {
    /// Revision 0 loaded modules (without ability to request internal modules).
    /// If internal modules are desired, use [`Self::new_rev1`].
    pub const fn new() -> Self {
        unsafe {
            Self::new_raw(
                [0x3e7e279702be32af, 0xca1c4f3bd1280cee],
                0,
                ModulesReqData {
                    internal_module_count: 0,
                    internal_modules: core::ptr::null(),
                },
            )
        }
    }

    /// Revision 1 loaded modules (with ability to request internal modules).
    pub const fn new_rev1(internal_modules: &'static [&'static InternalModule]) -> Self {
        unsafe {
            Self::new_raw(
                [0x3e7e279702be32af, 0xca1c4f3bd1280cee],
                1,
                ModulesReqData {
                    internal_module_count: internal_modules.len() as u64,
                    internal_modules: internal_modules.as_ptr() as _,
                },
            )
        }
    }
}

/// Get RSDP address request.
/// WARNING: For base revision 3 only, this is a physical address, whereas other revisions use a virtual address.
pub type RsdpRequest = Request<RsdpRespData>;

/// RSDP address response.
/// WARNING: For base revision 3 only, this is a physical address, whereas other revisions use a virtual address.
/// Response to [`RsdpRequest`].
pub type RsdpResponse = Response<RsdpRespData>;

#[repr(C)]
pub struct RsdpRespData {
    pub address: *mut (),
}

impl RsdpRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0xc5e77b6b397e7b43, 0x27637845accdcf3c], 0, ()) }
    }
}

/// Get SMBIOS entrypoint request.
/// If SMBIOS is not available, no response will be provided.
/// WARNING: For base revisions 3 and 4 only, this is a physical address, whereas other revisions use a virtual address.
pub type SmbiosRequest = Request<SmbiosRespData>;

/// SMBIOS entrypoint response.
/// WARNING: For base revisions 3 and 4 only, this is a physical address, whereas other revisions use a virtual address.
/// Response to [`SmbiosRequest`].
pub type SmbiosResponse = Response<SmbiosRespData>;

#[repr(C)]
pub struct SmbiosRespData {
    pub entry_32: *const (),
    pub entry_64: *const (),
}

impl SmbiosRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x9e9046f11e095391, 0xaa4a520fefbde5ee], 0, ()) }
    }
}

/// Get EFI system table address request.
/// If EFI is not available, no response will be provided.
/// WARNING: For base revisions 3 and 4 only, this is a physical address, whereas other revisions use a virtual address.
pub type EfiRequest = Request<EfiRespData>;

/// EFI system table address response.
/// WARNING: For base revisions 3 and 4 only, this is a physical address, whereas other revisions use a virtual address.
/// Response to [`EfiRequest`].
pub type EfiResponse = Response<EfiRespData>;

#[repr(C)]
pub struct EfiRespData {
    pub address: *const (),
}

impl EfiRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x5ceba5163eaaf6d6, 0x0a6981610cf65fcc], 0, ()) }
    }
}

/// Get EFI memory map request.
/// WARNING: For base revisions 3 and 4 only, this is a physical address, whereas other revisions use a virtual address.
pub type EfiMemmapRequest = Request<EfiMemmapRespData>;

/// EFI memory map response.
/// Response to [`EfiMemmapRequest`].
pub type EfiMemmapResponse = Response<EfiMemmapRespData>;

#[repr(C)]
pub struct EfiMemmapRespData {
    memmap: *const (),
    memmap_size: u64,
    pub desc_size: u64,
    pub desc_version: u64,
}

impl EfiMemmapRespData {
    pub fn memmap(&self) -> &[u8] {
        unsafe {
            &*core::ptr::slice_from_raw_parts(self.memmap as *const u8, self.memmap_size as usize)
        }
    }
}

impl EfiMemmapRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x7df62a431d6872d5, 0xa4fcdfb3e57306c8], 0, ()) }
    }
}

/// Get date at boot request.
pub type DateAtBootRequest = Request<DateAtBootRespData>;

/// Date at boot response.
/// Response to [`DateAtBootRequest`].
pub type DateAtBootResponse = Response<DateAtBootRespData>;

#[repr(C)]
pub struct DateAtBootRespData {
    pub timestamp: i64,
}

impl DateAtBootRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x502746e184c088aa, 0xfbc5ec83e6327893], 0, ()) }
    }
}

/// Get executable address request.
pub type ExecutableAddressRequest = Request<ExecutableAddressRespData>;

/// Executable address response.
/// Response to [`ExecutableAddressRequest`].
pub type ExecutableAddressResponse = Response<ExecutableAddressRespData>;

#[repr(C)]
pub struct ExecutableAddressRespData {
    pub physical_base: u64,
    pub virtual_base: u64,
}

impl ExecutableAddressRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x71ba76863cc55f63, 0xb2644a48c516a487], 0, ()) }
    }
}

/// Get DTB request.
pub type DtbRequest = Request<DtbRespData>;

/// DTB response.
/// Response to [`DtbRequest`].
pub type DtbResponse = Response<DtbRespData>;

#[repr(C)]
pub struct DtbRespData {
    pub dtb_ptr: *const (),
}

impl DtbRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0xb40ddb48fb54bac7, 0x545081493f81ffb7], 0, ()) }
    }
}

/// Get BSP hartid request.
pub type BspHartidRequest = Request<BspHartidRespData>;

/// BSP hartid response.
/// Response to [`BspHartidRequest`].
pub type BspHartidResponse = Response<BspHartidRespData>;

#[repr(C)]
pub struct BspHartidRespData {
    pub bsp_hartid: u64,
}

impl BspHartidRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x1369359f025525f9, 0x2ff2a56178391bb6], 0, ()) }
    }
}

/// Get bootloader performance request.
pub type BootloaderPerformanceRequest = Request<BootloaderPerformanceRespData>;

/// Bootloader performance response.
/// Response to [`BootloaderPerformanceRequest`].
pub type BootloaderPerformanceResponse = Response<BootloaderPerformanceRespData>;

#[repr(C)]
pub struct BootloaderPerformanceRespData {
    pub reset_usec: u64,
    pub init_usec: u64,
    pub exec_usec: u64,
}

impl BootloaderPerformanceRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x6b50ad9bf36d13ad, 0xdc4c7e88fc759e17], 0, ()) }
    }
}

/// Keep x86_64 I/O MMU enabled request.
/// Currently has no effect on other platforms.
pub type KeepIommuRequest = Request<KeepIommuRespData>;

pub struct KeepIommuRespData;

impl KeepIommuRequest {
    pub const fn new() -> Self {
        unsafe { Self::new_raw([0x8ebaabe51f490179, 0x2aa86a59ffb4ab0f], 0, ()) }
    }
}