Skip to main content

ostd/arch/x86/boot/linux_boot/
mod.rs

1// SPDX-License-Identifier: MPL-2.0
2
3//! The Linux 64-bit Boot Protocol supporting module.
4//!
5
6use linux_boot_params::{BootParams, E820Type, LINUX_BOOT_HEADER_MAGIC};
7
8#[cfg(feature = "cvm_guest")]
9use crate::arch::init_cvm_guest;
10use crate::{
11    arch::if_tdx_enabled,
12    boot::{
13        BootloaderAcpiArg, BootloaderFramebufferArg,
14        memory_region::{MemoryRegion, MemoryRegionArray, MemoryRegionType},
15    },
16    mm::kspace::paddr_to_vaddr,
17};
18
19fn parse_bootloader_name(boot_params: &BootParams) -> &str {
20    // The bootloaders have assigned IDs in Linux, see
21    // https://www.kernel.org/doc/Documentation/x86/boot.txt
22    // for details.
23    match boot_params.hdr.type_of_loader {
24        0x0 => "LILO", // (0x00 reserved for pre-2.00 bootloader)
25        0x1 => "Loadlin",
26        0x2 => "bootsect-loader", // (0x20, all other values reserved)
27        0x3 => "Syslinux",
28        0x4 => "Etherboot/gPXE/iPXE",
29        0x5 => "ELILO",
30        0x7 => "GRUB",
31        0x8 => "U-Boot",
32        0x9 => "Xen",
33        0xA => "Gujin",
34        0xB => "Qemu",
35        0xC => "Arcturus Networks uCbootloader",
36        0xD => "kexec-tools",
37        0xE => "Extended loader",
38        0xF => "Special", // (0xFF = undefined)
39        0x10 => "Reserved",
40        0x11 => "Minimal Linux Bootloader <http://sebastian-plotz.blogspot.de>",
41        0x12 => "OVMF UEFI virtualization stack",
42        _ => "Unknown Linux Loader",
43    }
44}
45
46fn parse_kernel_commandline(boot_params: &BootParams) -> Option<&str> {
47    if boot_params.ext_cmd_line_ptr != 0 {
48        // TODO: We can support the above 4GiB command line after setting up
49        // linear mappings. By far, we cannot log the error because the serial is
50        // not up. Proceed as if there was no command line.
51        return None;
52    }
53
54    if boot_params.hdr.cmd_line_ptr == 0 || boot_params.hdr.cmdline_size == 0 {
55        return None;
56    }
57
58    let cmdline_ptr = paddr_to_vaddr(boot_params.hdr.cmd_line_ptr as usize);
59    let cmdline_len = boot_params.hdr.cmdline_size as usize;
60    // SAFETY: The command line is safe to read because of the contract with the loader.
61    let cmdline = unsafe { core::slice::from_raw_parts(cmdline_ptr as *const u8, cmdline_len) };
62
63    // Now, unfortunately, there are silent errors because the serial is not up.
64    core::ffi::CStr::from_bytes_until_nul(cmdline)
65        .ok()?
66        .to_str()
67        .ok()
68}
69
70fn parse_initramfs(boot_params: &BootParams) -> Option<&[u8]> {
71    if boot_params.ext_ramdisk_image != 0 || boot_params.ext_ramdisk_size != 0 {
72        // See the explanation in `parse_kernel_commandline`.
73        return None;
74    }
75
76    if boot_params.hdr.ramdisk_image == 0 || boot_params.hdr.ramdisk_size == 0 {
77        return None;
78    }
79
80    let initramfs_ptr = paddr_to_vaddr(boot_params.hdr.ramdisk_image as usize);
81    let initramfs_len = boot_params.hdr.ramdisk_size as usize;
82    // SAFETY: The initramfs is safe to read because of the contract with the loader.
83    let initramfs =
84        unsafe { core::slice::from_raw_parts(initramfs_ptr as *const u8, initramfs_len) };
85
86    Some(initramfs)
87}
88
89fn parse_acpi_arg(boot_params: &BootParams) -> BootloaderAcpiArg {
90    let rsdp = boot_params.acpi_rsdp_addr;
91
92    if rsdp == 0 {
93        if is_efi_boot(boot_params) {
94            BootloaderAcpiArg::NotProvided
95        } else {
96            BootloaderAcpiArg::ScanBios
97        }
98    } else {
99        BootloaderAcpiArg::Rsdp(rsdp.try_into().expect("RSDP address overflowed!"))
100    }
101}
102
103fn is_efi_boot(boot_params: &BootParams) -> bool {
104    const EFI32_LOADER_SIGNATURE: u32 = u32::from_le_bytes(*b"EL32");
105    const EFI64_LOADER_SIGNATURE: u32 = u32::from_le_bytes(*b"EL64");
106
107    let efi_info = boot_params.efi_info;
108    matches!(
109        efi_info.efi_loader_signature,
110        EFI32_LOADER_SIGNATURE | EFI64_LOADER_SIGNATURE
111    )
112}
113
114fn parse_framebuffer_info(boot_params: &BootParams) -> Option<BootloaderFramebufferArg> {
115    let screen_info = boot_params.screen_info;
116
117    let address = screen_info.lfb_base as usize | ((screen_info.ext_lfb_base as usize) << 32);
118    if address == 0 {
119        return None;
120    }
121
122    Some(BootloaderFramebufferArg {
123        address,
124        width: screen_info.lfb_width as usize,
125        height: screen_info.lfb_height as usize,
126        bpp: screen_info.lfb_depth as usize,
127    })
128}
129
130impl From<E820Type> for MemoryRegionType {
131    fn from(value: E820Type) -> Self {
132        match value {
133            E820Type::Ram => Self::Usable,
134            E820Type::Reserved => Self::Reserved,
135            E820Type::Acpi => Self::Reclaimable,
136            E820Type::Nvs => Self::NonVolatileSleep,
137            E820Type::Unusable => Self::BadMemory,
138            // All other memory regions are reserved.
139            // FIXME: Using Rust enum in this way can be unsound if the bootloader passes an
140            // unknown memory type to the kernel (e.g., due to a newer protocol version).
141            _ => Self::Reserved,
142        }
143    }
144}
145
146fn parse_memory_regions(boot_params: &BootParams) -> MemoryRegionArray {
147    let mut regions = MemoryRegionArray::new();
148
149    // Add regions from E820.
150    let num_entries = boot_params.e820_entries as usize;
151    for e820_entry in &boot_params.e820_table[0..num_entries] {
152        regions
153            .push(MemoryRegion::new(
154                e820_entry.addr.try_into().unwrap(),
155                e820_entry.size.try_into().unwrap(),
156                e820_entry.typ.into(),
157            ))
158            .unwrap();
159    }
160
161    // Add the framebuffer region.
162    if let Some(fb) = parse_framebuffer_info(boot_params) {
163        regions.push(MemoryRegion::framebuffer(&fb)).unwrap();
164    }
165
166    // Add the kernel region.
167    regions.push(MemoryRegion::kernel()).unwrap();
168
169    // Add the initramfs region.
170    if let Some(initramfs) = parse_initramfs(boot_params) {
171        regions.push(MemoryRegion::module(initramfs)).unwrap();
172    }
173
174    // Add the AP boot code region that will be copied into by the BSP.
175    regions
176        .push(super::smp::reclaimable_memory_region())
177        .unwrap();
178
179    // Add the region of the kernel cmdline since some bootloaders do not provide it.
180    if let Some(kcmdline) = parse_kernel_commandline(boot_params) {
181        regions
182            .push(MemoryRegion::module(kcmdline.as_bytes()))
183            .unwrap();
184    }
185
186    // FIXME: Early versions of TDVF did not correctly report the location of AP's page tables as
187    // EfiACPIMemoryNVS. We need to manually reserve this memory region to prevent them from being
188    // corrupted. TDVF has now been upstreamed to OVMF, and this issue has been fixed in OVMF
189    // stable-202411 or later. See the commit for details:
190    // <https://github.com/tianocore/edk2/commit/383f729ac096b8deb279933fce86e83a5f7f5ec7>.
191    if_tdx_enabled!({
192        // The definition of these constants can be found in:
193        // <https://github.com/tianocore/edk2/blob/a7ab45ace25c4b987994158687d04de07ed20a96/OvmfPkg/IntelTdx/IntelTdxX64.fdf#L64-L71>
194        // <https://github.com/tianocore/edk2/blob/a7ab45ace25c4b987994158687d04de07ed20a96/OvmfPkg/Include/Fdf/OvmfPkgDefines.fdf.inc#L106>
195        regions
196            .push(MemoryRegion::new(
197                // PcdOvmfSecPageTablesBase = $(MEMFD_BASE_ADDRESS) + 0x000000 = 0x800000
198                0x800000,
199                // PcdOvmfSecPageTablesSize = 0x006000
200                0x006000,
201                // EfiACPIMemoryNVS
202                MemoryRegionType::NonVolatileSleep,
203            ))
204            .unwrap();
205    });
206
207    regions.into_non_overlapping()
208}
209
210/// The entry point of the Rust code portion of Asterinas (with Linux boot parameters).
211///
212/// # Safety
213///
214/// - This function must be called only once at a proper timing in the BSP's boot assembly code.
215/// - The caller must follow C calling conventions and put the right arguments in registers.
216/// - If this function is called, entry points of other boot protocols must never be called.
217// SAFETY: The name does not collide with other symbols.
218#[unsafe(no_mangle)]
219unsafe extern "sysv64" fn __linux_boot(params_ptr: *const BootParams) -> ! {
220    let params = unsafe { &*params_ptr };
221    assert_eq!({ params.hdr.header }, LINUX_BOOT_HEADER_MAGIC);
222
223    use crate::boot::{EARLY_INFO, EarlyBootInfo, start_kernel};
224
225    #[cfg(feature = "cvm_guest")]
226    init_cvm_guest();
227
228    EARLY_INFO.call_once(|| EarlyBootInfo {
229        bootloader_name: parse_bootloader_name(params),
230        kernel_cmdline: parse_kernel_commandline(params).unwrap_or(""),
231        initramfs: parse_initramfs(params),
232        acpi_arg: parse_acpi_arg(params),
233        framebuffer_arg: parse_framebuffer_info(params),
234        memory_regions: parse_memory_regions(params),
235    });
236
237    // SAFETY: The safety is guaranteed by the safety preconditions and the fact that we call it
238    // once after setting up necessary resources.
239    unsafe { start_kernel() };
240}