Skip to main content

hermit_entry/boot_info/
loader.rs

1use super::{
2    BootInfo, HardwareInfo, LoadInfo, PlatformInfo, RawBootInfo, RawHardwareInfo, RawLoadInfo,
3    RawPlatformInfo, TlsInfo,
4};
5
6impl From<HardwareInfo> for RawHardwareInfo {
7    fn from(hardware_info: HardwareInfo) -> Self {
8        Self {
9            phys_addr_start: hardware_info.phys_addr_range.start,
10            phys_addr_end: hardware_info.phys_addr_range.end,
11            serial_port_base: hardware_info.serial_port_base,
12            device_tree: hardware_info.device_tree,
13        }
14    }
15}
16
17impl From<LoadInfo> for RawLoadInfo {
18    fn from(load_info: LoadInfo) -> Self {
19        Self {
20            kernel_image_addr_start: load_info.kernel_image_addr_range.start,
21            kernel_image_addr_end: load_info.kernel_image_addr_range.end,
22            tls_info: load_info.tls_info.unwrap_or(TlsInfo {
23                start: 0,
24                filesz: 0,
25                memsz: 0,
26                align: 0,
27            }),
28        }
29    }
30}
31
32impl From<PlatformInfo> for RawPlatformInfo {
33    fn from(platform_info: PlatformInfo) -> Self {
34        match platform_info {
35            #[cfg(target_arch = "x86_64")]
36            PlatformInfo::Multiboot {
37                command_line,
38                multiboot_info_addr,
39            } => Self::Multiboot {
40                command_line_data: command_line
41                    .map(|s| s.as_ptr())
42                    .unwrap_or(core::ptr::null()),
43                command_line_len: command_line.map(|s| s.len() as u64).unwrap_or(0),
44                multiboot_info_addr,
45            },
46            #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
47            PlatformInfo::LinuxBoot => Self::LinuxBoot,
48            PlatformInfo::Uhyve {
49                has_pci,
50                num_cpus,
51                cpu_freq,
52                boot_time,
53            } => Self::Uhyve {
54                has_pci,
55                num_cpus,
56                cpu_freq,
57                boot_time: boot_time.unix_timestamp_nanos().to_ne_bytes().into(),
58            },
59            PlatformInfo::LinuxBootParams {
60                command_line,
61                boot_params_addr,
62            } => Self::LinuxBootParams {
63                command_line_data: command_line
64                    .map(|s| s.as_ptr())
65                    .unwrap_or(core::ptr::null()),
66                command_line_len: command_line.map(|s| s.len() as u64).unwrap_or(0),
67                boot_params_addr,
68            },
69            PlatformInfo::Fdt => Self::Fdt,
70        }
71    }
72}
73
74impl From<BootInfo> for RawBootInfo {
75    fn from(boot_info: BootInfo) -> Self {
76        RawBootInfo {
77            hardware_info: boot_info.hardware_info.into(),
78            load_info: boot_info.load_info.into(),
79            platform_info: boot_info.platform_info.into(),
80        }
81    }
82}