pub fn load_cmdline<M: GuestMemory>(
    guest_mem: &M,
    guest_addr: GuestAddress,
    cmdline: &CStr,
) -> Result<()> {
    let len = cmdline.to_bytes().len();
    if len == 0 {
        return Ok(());
    }

    let end = guest_addr
        .checked_add(len as u64 + 1)
        .ok_or(Error::CommandLineOverflow)?; // Extra for null termination.
    if end > guest_mem.last_addr() {
        return Err(Error::CommandLineOverflow);
    }

    guest_mem
        .write_slice(cmdline.to_bytes_with_nul(), guest_addr)
        .map_err(|_| Error::CommandLineCopy)?;

    Ok(())
}
/// Writes the command line string to the given guest memory slice.
///
/// # Arguments
///
/// * `guest_mem` - [`GuestMemory`] that will be partially overwritten by the command line.
/// * `guest_addr` - The address in `guest_mem` at which to load the command line.
/// * `cmdline` - The kernel command line.
///
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
///
/// # Examples
///
/// ```rust
/// # extern crate vm_memory;
/// # use linux_loader::loader::*;
/// # use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap};
/// # use std::ffi::CStr;
/// let mem_size: usize = 0x1000000;
/// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
/// let cl = CStr::from_bytes_with_nul(b"foo=bar\0").unwrap();
/// let mut buf = vec![0u8;8];
/// let result = load_cmdline(&gm, GuestAddress(0x1000), &cl).unwrap();
/// gm.read_slice(buf.as_mut_slice(), GuestAddress(0x1000)).unwrap();
/// assert_eq!(buf.as_slice(), "foo=bar\0".as_bytes());
///
Writes the command line string to the given guest memory slice.
Writes the command line string to the given guest memory slice .
pub fn load_cmdline<M: GuestMemory>(
    guest_mem: &M,
    guest_addr: GuestAddress,
    cmdline: &CStr,
) -> Result<()> {
    let len = cmdline.to_bytes().len();
    if len == 0 {
        return Ok(());
    }
    let end = guest_addr
        .checked_add(len as u64 + 1)
        .ok_or(Error::CommandLineOverflow)?; // Extra for null termination.
    if end > guest_mem.last_addr() {
        return Err(Error::CommandLineOverflow);
    }
    guest_mem
        .write_slice(cmdline.to_bytes_with_nul(), guest_addr)
        .map_err(|_| Error::CommandLineCopy)?;
    Ok(())
}
pub fn load_cmdline < M : GuestMemory > ( guest_mem : & M , guest_addr : GuestAddress , cmdline : & CStr , ) -> Result < ( ) > { let len = cmdline . to_bytes ( ) . len ( ) ; if len == 0 { return Ok ( ( ) ) ; } let end = guest_addr . checked_add ( len as u64 + 1 ) . ok_or ( Error :: CommandLineOverflow ) ? ; // Extra for null termination. if end > guest_mem . last_addr ( ) { return Err ( Error :: CommandLineOverflow ) ; } guest_mem . write_slice ( cmdline . to_bytes_with_nul ( ) , guest_addr ) . map_err ( | _ | Error :: CommandLineCopy ) ? ; Ok ( ( ) ) }
Writes the command line string to the given guest memory slice.
Writes the command line string to the given guest memory slice .
guest_mem
&M
guest_addr
GuestAddress
cmdline
&CStr
guest_mem
&M
guest_addr
GuestAddress
cmdline
&CStr
examples
None
extern crate vm_memory;
None
use linux_loader::loader::*;
None
use vm_memory::{bytes, guestaddress, guestmemorymmap};
None
use std::ffi::cstr;
let mem_size: usize = 0x1000000;
let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
let cl = CStr::from_bytes_with_nul(b"foo=bar\0").unwrap();
let mut buf = vec![0u8;8];
let result = load_cmdline(&gm, GuestAddress(0x1000), &cl).unwrap();
gm.read_slice(buf.as_mut_slice(), GuestAddress(0x1000)).unwrap();
assert_eq!(buf.as_slice(), "foo=bar\0".as_bytes()).
let mem_size : usize = 0x1000000 ; let gm = GuestMemoryMmap :: from_ranges ( & [ ( GuestAddress ( 0x0 ) mem_size ) ] ) . unwrap () ; let cl = CStr :: from_bytes_with_nul ( b " foo = bar \ 0 " ) . unwrap () ; let mut buf = vec! [ 0u8 ; 8 ] ; let result = load_cmdline ( &gm GuestAddress ( 0x1000 ) &cl ) . unwrap () ; gm . read_slice ( buf . as_mut_slice () GuestAddress ( 0x1000 )) . unwrap () ; assert_eq! ( buf . as_slice () " foo = bar \ 0 " . as_bytes () ) .