linux_loader/lib.rs
1// Copyright (c) 2019 Intel Corporation. All rights reserved.
2// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3//
4// Copyright 2017 The Chromium OS Authors. All rights reserved.
5// Use of this source code is governed by a BSD-style license that can be
6// found in the LICENSE-BSD-3-Clause file.
7//
8// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
9
10#![deny(missing_docs)]
11#![cfg_attr(docsrs, feature(doc_auto_cfg))]
12
13//! A Linux kernel image loading crate.
14//!
15//! This crate offers support for loading raw ELF (vmlinux), compressed
16//! big zImage (bzImage) and PE (Image) kernel images.
17//! ELF support includes the Linux and PVH boot protocols.
18//! Support for any other kernel image format can be added by implementing
19//! the [`KernelLoader`] and [`BootConfigurator`].
20//!
21//! # Platform support
22//!
23//! - `x86_64`
24//! - `ARM64`
25//! - `RISC-V64`
26//!
27//! # Example - load an ELF kernel and configure boot params with the PVH protocol
28//!
29//! This example shows how to prepare a VM for booting with an ELF kernel, following the PVH
30//! boot protocol.
31//!
32//! ```rust
33//! # extern crate linux_loader;
34//! # extern crate vm_memory;
35//! # use std::{io::{Cursor, Read}, fs::File};
36//! # use linux_loader::configurator::{BootConfigurator, BootParams};
37//! # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
38//! # use linux_loader::configurator::pvh::PvhBootConfigurator;
39//! # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
40//! # use linux_loader::loader::elf::start_info::{hvm_memmap_table_entry, hvm_start_info};
41//! # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
42//! # use linux_loader::loader::elf::Elf;
43//! # use linux_loader::loader::KernelLoader;
44//! # use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
45//! # const E820_RAM: u32 = 1;
46//! # const MEM_SIZE: usize = 0x100_0000;
47//! # const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578;
48//!
49//! # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
50//! fn build_boot_params() -> (hvm_start_info, Vec<hvm_memmap_table_entry>) {
51//! let mut start_info = hvm_start_info::default();
52//! let memmap_entry = hvm_memmap_table_entry {
53//! addr: 0x7000,
54//! size: 0,
55//! type_: E820_RAM,
56//! reserved: 0,
57//! };
58//! start_info.magic = XEN_HVM_START_MAGIC_VALUE;
59//! start_info.version = 1;
60//! start_info.nr_modules = 0;
61//! start_info.memmap_entries = 0;
62//! (start_info, vec![memmap_entry])
63//! }
64//!
65//! # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
66//! fn main() {
67//! let guest_mem = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), MEM_SIZE)]).unwrap();
68//!
69//! let mut elf_pvh_image = Vec::new();
70//! let path = concat!(
71//! env!("CARGO_MANIFEST_DIR"),
72//! "/src/loader/elf/test_elfnote.bin"
73//! );
74//! let mut file = File::open(path).unwrap();
75//! file.read_to_end(&mut elf_pvh_image).unwrap();
76//!
77//! // Load the kernel image.
78//! let loader_result =
79//! Elf::load(&guest_mem, None, &mut Cursor::new(&elf_pvh_image), None).unwrap();
80//!
81//! // Build boot parameters.
82//! let (mut start_info, memmap_entries) = build_boot_params();
83//! // Address in guest memory where the `start_info` struct will be written.
84//! let start_info_addr = GuestAddress(0x6000);
85//! // Address in guest memory where the memory map will be written.
86//! let memmap_addr = GuestAddress(0x7000);
87//! start_info.memmap_paddr = memmap_addr.raw_value();
88//!
89//! // Write boot parameters in guest memory.
90//! let mut boot_params = BootParams::new::<hvm_start_info>(&start_info, start_info_addr);
91//! boot_params.set_sections::<hvm_memmap_table_entry>(&memmap_entries, memmap_addr);
92//! PvhBootConfigurator::write_bootparams::<GuestMemoryMmap>(&boot_params, &guest_mem).unwrap();
93//! }
94//!
95//! # #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
96//! # fn main() {}
97//! ```
98//!
99//! [`BootConfigurator`]: trait.BootConfigurator.html
100//! [`KernelLoader`]: trait.KernelLoader.html
101
102pub mod cmdline;
103pub mod configurator;
104pub mod loader;
105
106#[allow(clippy::undocumented_unsafe_blocks)]
107#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
108mod loader_gen;
109#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
110pub use loader_gen::*;
111
112extern crate vm_memory;