Skip to content

Commit f4c6507

Browse files
Alexandra Iordachealexandruag
authored andcommitted
docs: high level usage examples
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent 1af92d2 commit f4c6507

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,70 @@
2222
//! - `x86_64`
2323
//! - `ARM64`
2424
//!
25+
//! # Example - load an ELF kernel and configure boot params with the PVH protocol
26+
//!
27+
//! This example shows how to prepare a VM for booting with an ELF kernel, following the PVH
28+
//! boot protocol.
29+
//!
30+
//! ```rust,ignore
31+
//! # extern crate linux_loader;
32+
//! # extern crate vm_memory;
33+
//! # use std::io::Cursor;
34+
//! # use linux_loader::configurator::{BootConfigurator, BootParams};
35+
//! # use linux_loader::configurator::pvh::PvhBootConfigurator;
36+
//! # use linux_loader::loader::elf::start_info::{hvm_memmap_table_entry, hvm_start_info};
37+
//! # use linux_loader::loader::elf::Elf;
38+
//! # use linux_loader::loader::KernelLoader;
39+
//! # use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
40+
//! # const E820_RAM: u32 = 1;
41+
//! # const MEM_SIZE: usize = 0x100_0000;
42+
//! # const XEN_HVM_START_MAGIC_VALUE: u32 = 0x336ec578;
43+
//! # fn create_guest_memory() -> GuestMemoryMmap {
44+
//! # GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), MEM_SIZE)]).unwrap()
45+
//! # }
46+
//! # fn create_elf_pvh_image() -> Vec<u8> {
47+
//! # include_bytes!(concat!(
48+
//! # env!("CARGO_MANIFEST_DIR"), "/src/loader/x86_64/elf/test_elfnote.bin"
49+
//! # )).to_vec()
50+
//! # }
51+
//! fn build_boot_params() -> (hvm_start_info, Vec<hvm_memmap_table_entry>) {
52+
//! let mut start_info = hvm_start_info::default();
53+
//! let memmap_entry = hvm_memmap_table_entry {
54+
//! addr: 0x7000,
55+
//! size: 0,
56+
//! type_: E820_RAM,
57+
//! reserved: 0,
58+
//! };
59+
//! start_info.magic = XEN_HVM_START_MAGIC_VALUE;
60+
//! start_info.version = 1;
61+
//! start_info.nr_modules = 0;
62+
//! start_info.memmap_entries = 0;
63+
//! (start_info, vec![memmap_entry])
64+
//! }
65+
//!
66+
//! fn main() {
67+
//! let guest_mem = create_guest_memory();
68+
//! let elf_pvh_image = create_elf_pvh_image();
69+
//! // Load the kernel image.
70+
//! let loader_result = Elf::load(
71+
//! &guest_mem, None, &mut Cursor::new(&elf_pvh_image), None
72+
//! ).unwrap();
73+
//!
74+
//! // Build boot parameters.
75+
//! let (mut start_info, memmap_entries) = build_boot_params();
76+
//! // Address in guest memory where the `start_info` struct will be written.
77+
//! let start_info_addr = GuestAddress(0x6000);
78+
//! // Address in guest memory where the memory map will be written.
79+
//! let memmap_addr = GuestAddress(0x7000);
80+
//! start_info.memmap_paddr = memmap_addr.raw_value();
81+
//!
82+
//! // Write boot parameters in guest memory.
83+
//! let mut boot_params = BootParams::new::<hvm_start_info>(&start_info, start_info_addr);
84+
//! boot_params.set_sections::<hvm_memmap_table_entry>(&memmap_entries, memmap_addr);
85+
//! PvhBootConfigurator::write_bootparams::<GuestMemoryMmap>(boot_params, &guest_mem).unwrap();
86+
//! }
87+
//! ```
88+
//!
2589
//! [`BootConfigurator`]: trait.BootConfigurator.html
2690
//! [`KernelLoader`]: trait.KernelLoader.html
2791

0 commit comments

Comments
 (0)