Skip to content

Commit 7cb4ee8

Browse files
author
Alexandra Iordache
committed
cosmetic changes & doctests
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent bd01b6d commit 7cb4ee8

File tree

5 files changed

+134
-43
lines changed

5 files changed

+134
-43
lines changed

coverage_config_aarch64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 80.3,
2+
"coverage_score": 80.4,
33
"exclude_path": "",
44
"crate_features": ""
55
}

coverage_config_x86_64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 78.5,
2+
"coverage_score": 78.7,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/cmdline/mod.rs

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt;
1212
use std::result;
1313

1414
/// The error type for command line building operations.
15-
#[derive(PartialEq, Debug)]
15+
#[derive(Debug, PartialEq)]
1616
pub enum Error {
1717
/// Operation would have resulted in a non-printable ASCII character.
1818
InvalidAscii,
@@ -30,16 +30,17 @@ impl fmt::Display for Error {
3030
f,
3131
"{}",
3232
match *self {
33-
Error::InvalidAscii => "string contains non-printable ASCII character",
34-
Error::HasSpace => "string contains a space",
35-
Error::HasEquals => "string contains an equals sign",
36-
Error::TooLarge => "inserting string would make command line too long",
33+
Error::InvalidAscii => "String contains a non-printable ASCII character.",
34+
Error::HasSpace => "String contains a space.",
35+
Error::HasEquals => "String contains an equals sign.",
36+
Error::TooLarge => "Inserting string would make command line too long.",
3737
}
3838
)
3939
}
4040
}
4141

42-
/// Specialized Result type for command line operations.
42+
/// Specialized [`Result`] type for command line operations.
43+
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
4344
pub type Result<T> = result::Result<T, Error>;
4445

4546
fn valid_char(c: char) -> bool {
@@ -69,17 +70,37 @@ fn valid_element(s: &str) -> Result<()> {
6970
}
7071
}
7172

72-
/// A builder for a kernel command line string that validates the string as its being built. A
73-
/// `CString` can be constructed from this directly using `CString::new`.
74-
#[derive(Clone)]
73+
/// A builder for a kernel command line string that validates the string as it's being built.
74+
/// A `CString` can be constructed from this directly using `CString::new`.
75+
///
76+
/// # Examples
77+
///
78+
/// ```rust
79+
/// # use linux_loader::cmdline::*;
80+
/// # use std::ffi::CString;
81+
/// let cl = Cmdline::new(100);
82+
/// let cl_cstring = CString::new(cl).unwrap();
83+
/// assert_eq!(cl_cstring.to_str().unwrap(), "");
84+
/// ```
7585
pub struct Cmdline {
7686
line: String,
7787
capacity: usize,
7888
}
7989

8090
impl Cmdline {
81-
/// Constructs an empty Cmdline with the given capacity, which includes the nul terminator.
82-
/// Capacity must be greater than 0.
91+
/// Constructs an empty [`Cmdline`] with the given capacity, including the nul terminator.
92+
///
93+
/// # Arguments
94+
///
95+
/// * `capacity` - Command line capacity. Must be greater than 0.
96+
///
97+
/// # Examples
98+
///
99+
/// ```rust
100+
/// # use linux_loader::cmdline::*;
101+
/// let cl = Cmdline::new(100);
102+
/// ```
103+
/// [`Cmdline`]: struct.Cmdline.html
83104
pub fn new(capacity: usize) -> Cmdline {
84105
assert_ne!(capacity, 0);
85106
Cmdline {
@@ -109,7 +130,23 @@ impl Cmdline {
109130
assert!(self.line.len() < self.capacity);
110131
}
111132

112-
/// Validates and inserts a key value pair into this command line
133+
/// Validates and inserts a key-value pair into this command line.
134+
///
135+
/// # Arguments
136+
///
137+
/// * `key` - Key to be inserted in the command line string.
138+
/// * `val` - Value corresponding to `key`.
139+
///
140+
/// # Examples
141+
///
142+
/// ```rust
143+
/// # use linux_loader::cmdline::*;
144+
/// # use std::ffi::CString;
145+
/// let mut cl = Cmdline::new(100);
146+
/// cl.insert("foo", "bar");
147+
/// let cl_cstring = CString::new(cl).unwrap();
148+
/// assert_eq!(cl_cstring.to_str().unwrap(), "foo=bar");
149+
/// ```
113150
pub fn insert<T: AsRef<str>>(&mut self, key: T, val: T) -> Result<()> {
114151
let k = key.as_ref();
115152
let v = val.as_ref();
@@ -127,7 +164,22 @@ impl Cmdline {
127164
Ok(())
128165
}
129166

130-
/// Validates and inserts a string to the end of the current command line
167+
/// Validates and inserts a string to the end of the current command line.
168+
///
169+
/// # Arguments
170+
///
171+
/// * `slug` - String to be appended to the command line.
172+
///
173+
/// # Examples
174+
///
175+
/// ```rust
176+
/// # use linux_loader::cmdline::*;
177+
/// # use std::ffi::CString;
178+
/// let mut cl = Cmdline::new(100);
179+
/// cl.insert_str("foobar");
180+
/// let cl_cstring = CString::new(cl).unwrap();
181+
/// assert_eq!(cl_cstring.to_str().unwrap(), "foobar");
182+
/// ```
131183
pub fn insert_str<T: AsRef<str>>(&mut self, slug: T) -> Result<()> {
132184
let s = slug.as_ref();
133185
valid_str(s)?;
@@ -141,7 +193,16 @@ impl Cmdline {
141193
Ok(())
142194
}
143195

144-
/// Returns the cmdline in progress without nul termination
196+
/// Returns the string representation of the command line without the nul terminator.
197+
///
198+
/// # Examples
199+
///
200+
/// ```rust
201+
/// # use linux_loader::cmdline::*;
202+
/// let mut cl = Cmdline::new(10);
203+
/// cl.insert_str("foobar");
204+
/// assert_eq!(cl.as_str(), "foobar");
205+
/// ```
145206
pub fn as_str(&self) -> &str {
146207
self.line.as_str()
147208
}
@@ -159,7 +220,7 @@ mod tests {
159220
use std::ffi::CString;
160221

161222
#[test]
162-
fn insert_hello_world() {
223+
fn test_insert_hello_world() {
163224
let mut cl = Cmdline::new(100);
164225
assert_eq!(cl.as_str(), "");
165226
assert!(cl.insert("hello", "world").is_ok());
@@ -170,15 +231,15 @@ mod tests {
170231
}
171232

172233
#[test]
173-
fn insert_multi() {
234+
fn test_insert_multi() {
174235
let mut cl = Cmdline::new(100);
175236
assert!(cl.insert("hello", "world").is_ok());
176237
assert!(cl.insert("foo", "bar").is_ok());
177238
assert_eq!(cl.as_str(), "hello=world foo=bar");
178239
}
179240

180241
#[test]
181-
fn insert_space() {
242+
fn test_insert_space() {
182243
let mut cl = Cmdline::new(100);
183244
assert_eq!(cl.insert("a ", "b"), Err(Error::HasSpace));
184245
assert_eq!(cl.insert("a", "b "), Err(Error::HasSpace));
@@ -188,7 +249,7 @@ mod tests {
188249
}
189250

190251
#[test]
191-
fn insert_equals() {
252+
fn test_insert_equals() {
192253
let mut cl = Cmdline::new(100);
193254
assert_eq!(cl.insert("a=", "b"), Err(Error::HasEquals));
194255
assert_eq!(cl.insert("a", "b="), Err(Error::HasEquals));
@@ -199,15 +260,15 @@ mod tests {
199260
}
200261

201262
#[test]
202-
fn insert_emoji() {
263+
fn test_insert_emoji() {
203264
let mut cl = Cmdline::new(100);
204265
assert_eq!(cl.insert("heart", "💖"), Err(Error::InvalidAscii));
205266
assert_eq!(cl.insert("💖", "love"), Err(Error::InvalidAscii));
206267
assert_eq!(cl.as_str(), "");
207268
}
208269

209270
#[test]
210-
fn insert_string() {
271+
fn test_insert_string() {
211272
let mut cl = Cmdline::new(13);
212273
assert_eq!(cl.as_str(), "");
213274
assert!(cl.insert_str("noapic").is_ok());
@@ -217,7 +278,7 @@ mod tests {
217278
}
218279

219280
#[test]
220-
fn insert_too_large() {
281+
fn test_insert_too_large() {
221282
let mut cl = Cmdline::new(4);
222283
assert_eq!(cl.insert("hello", "world"), Err(Error::TooLarge));
223284
assert_eq!(cl.insert("a", "world"), Err(Error::TooLarge));

src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
//!
1414
//! This crate offers support for loading raw ELF (vmlinux), compressed
1515
//! big zImage (bzImage) and PE (Image) kernel images.
16+
//! ELF support includes the Linux and PVH boot protocols.
1617
//! Support for any other kernel image format can be added by implementing
17-
//! the KernelLoader.
18+
//! the [`KernelLoader`] and [`BootConfigurator`].
1819
//!
1920
//! # Platform support
2021
//!
21-
//! - x86_64
22-
//! - ARM64
22+
//! - `x86_64`
23+
//! - `ARM64`
2324
//!
25+
//! [`BootConfigurator`]: trait.BootConfigurator.html
26+
//! [`KernelLoader`]: trait.KernelLoader.html
2427
2528
pub mod cmdline;
2629
pub mod configurator;

src/loader/mod.rs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
//
1010
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
1111

12-
//! Traits and Structs for loading kernels into guest memory.
13-
//! - [KernelLoader](trait.KernelLoader.html): load kernel image into guest memory
14-
//! - [KernelLoaderResult](struct.KernelLoaderResult.html): the structure which loader
15-
//! returns to VMM to assist zero page construction and boot environment setup
16-
//! - [Elf](struct.Elf.html): elf image loader
17-
//! - [BzImage](struct.BzImage.html): bzImage loader
12+
//! Traits and structs for loading kernels into guest memory.
13+
//! - [KernelLoader](trait.KernelLoader.html): load kernel image into guest memory.
14+
//! - [KernelLoaderResult](struct.KernelLoaderResult.html): structure passed to the VMM to assist
15+
//! zero page construction and boot environment setup.
16+
//! - [Elf](struct.Elf.html): elf image loader.
17+
//! - [BzImage](struct.BzImage.html): bzImage loader.
1818
1919
extern crate vm_memory;
2020

@@ -71,7 +71,8 @@ pub enum Error {
7171
MemoryOverflow,
7272
}
7373

74-
/// A specialized `Result` type for the kernel loader.
74+
/// A specialized [`Result`] type for the kernel loader.
75+
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
7576
pub type Result<T> = std::result::Result<T, Error>;
7677

7778
impl StdError for Error {
@@ -126,13 +127,13 @@ impl From<pe::Error> for Error {
126127
/// the VMM.
127128
#[derive(Clone, Copy, Debug, Default, PartialEq)]
128129
pub struct KernelLoaderResult {
129-
/// Address in the guest memory where the kernel image starts to be loaded
130+
/// Address in the guest memory where the kernel image starts to be loaded.
130131
pub kernel_load: GuestAddress,
131-
/// Offset in guest memory corresponding to the end of kernel image, in case that
132-
/// device tree blob and initrd will be loaded adjacent to kernel image.
132+
/// Offset in guest memory corresponding to the end of kernel image, in case the device tree
133+
/// blob and initrd will be loaded adjacent to kernel image.
133134
pub kernel_end: GuestUsize,
134-
/// This field is only for bzImage following https://www.kernel.org/doc/Documentation/x86/boot.txt
135-
/// VMM should make use of it to fill zero page for bzImage direct boot.
135+
/// Configuration for the VMM to use to fill zero page for bzImage direct boot.
136+
/// See https://www.kernel.org/doc/Documentation/x86/boot.txt.
136137
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
137138
pub setup_header: Option<bootparam::setup_header>,
138139
/// This field optionally holds the address of a PVH entry point, indicating that
@@ -141,10 +142,18 @@ pub struct KernelLoaderResult {
141142
pub pvh_entry_addr: Option<GuestAddress>,
142143
}
143144

144-
/// A kernel image loading support must implement the KernelLoader trait.
145-
/// The only method to be implemented is the load one, returning a KernelLoaderResult structure.
145+
/// Trait that specifies kernel image loading support.
146146
pub trait KernelLoader {
147147
/// How to load a specific kernel image format into the guest memory.
148+
///
149+
/// # Arguments
150+
///
151+
/// * `guest_mem`: [`GuestMemory`] to load the kernel in.
152+
/// * `kernel_start`: Address in guest memory where the kernel is loaded.
153+
/// * `kernel_image`: Kernel image to be loaded.
154+
/// * `highmem_start_address`: Address where high memory starts.
155+
///
156+
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
148157
fn load<F, M: GuestMemory>(
149158
guest_mem: &M,
150159
kernel_start: Option<GuestAddress>,
@@ -164,9 +173,27 @@ unsafe impl ByteValued for bootparam::boot_params {}
164173
///
165174
/// # Arguments
166175
///
167-
/// * `guest_mem` - A u8 slice that will be partially overwritten by the command line.
176+
/// * `guest_mem` - [`GuestMemory`] that will be partially overwritten by the command line.
168177
/// * `guest_addr` - The address in `guest_mem` at which to load the command line.
169178
/// * `cmdline` - The kernel command line.
179+
///
180+
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
181+
///
182+
/// # Examples
183+
///
184+
/// ```rust
185+
/// # extern crate vm_memory;
186+
/// # use linux_loader::loader::*;
187+
/// # use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
188+
/// # use std::ffi::CStr;
189+
/// let mem_size: usize = 0x1000000;
190+
/// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
191+
/// let cl = CStr::from_bytes_with_nul(b"foo=bar\0").unwrap();
192+
/// let mut buf = vec![0u8;8];
193+
/// let result = load_cmdline(&gm, GuestAddress(0x1000), &cl).unwrap();
194+
/// gm.read_slice(buf.as_mut_slice(), GuestAddress(0x1000)).unwrap();
195+
/// assert_eq!(buf.as_slice(), "foo=bar\0".as_bytes());
196+
///
170197
pub fn load_cmdline<M: GuestMemory>(
171198
guest_mem: &M,
172199
guest_addr: GuestAddress,
@@ -203,7 +230,7 @@ mod tests {
203230
}
204231

205232
#[test]
206-
fn cmdline_overflow() {
233+
fn test_cmdline_overflow() {
207234
let gm = create_guest_mem();
208235
let cmdline_address = GuestAddress(MEM_SIZE - 5);
209236
assert_eq!(
@@ -217,7 +244,7 @@ mod tests {
217244
}
218245

219246
#[test]
220-
fn cmdline_write_end() {
247+
fn test_cmdline_write_end() {
221248
let gm = create_guest_mem();
222249
let mut cmdline_address = GuestAddress(45);
223250
assert_eq!(

0 commit comments

Comments
 (0)