fn load<F, M: GuestMemory>(
        guest_mem: &M,
        kernel_start: Option<GuestAddress>,
        kernel_image: &mut F,
        highmem_start_address: Option<GuestAddress>,
    ) -> Result<KernelLoaderResult>
    where
        F: Read + Seek,
    {
        let mut kernel_size = kernel_image
            .seek(SeekFrom::End(0))
            .map_err(|_| Error::SeekBzImageEnd)? as usize;
        kernel_image
            .seek(SeekFrom::Start(0x1F1))
            .map_err(|_| Error::SeekBzImageHeader)?;

        let mut boot_header = bootparam::setup_header::default();
        boot_header
            .as_bytes()
            .read_from(0, kernel_image, mem::size_of::<bootparam::setup_header>())
            .map_err(|_| Error::ReadBzImageHeader)?;

        // If the `HdrS` magic number is not found at offset 0x202, the boot protocol version is
        // "old", the image type is assumed as zImage, not bzImage.
        if boot_header.header != 0x5372_6448 {
            Err(Error::InvalidBzImage)?;
        }

        // Follow the section related to loading the rest of the kernel in the linux boot protocol.
        if (boot_header.version < 0x0200) || ((boot_header.loadflags & 0x1) == 0x0) {
            Err(Error::InvalidBzImage)?;
        }

        let mut setup_size = boot_header.setup_sects as usize;
        if setup_size == 0 {
            setup_size = 4;
        }
        setup_size = (setup_size + 1) * 512;
        kernel_size -= setup_size;

        // Check that `code32_start`, the default address of the kernel, is not lower than high
        // memory.
        if (highmem_start_address.is_some())
            && (u64::from(boot_header.code32_start) < highmem_start_address.unwrap().raw_value())
        {
            return Err(KernelLoaderError::InvalidKernelStartAddress);
        }

        let mem_offset = match kernel_start {
            Some(start) => start,
            None => GuestAddress(u64::from(boot_header.code32_start)),
        };

        boot_header.code32_start = mem_offset.raw_value() as u32;

        let mut loader_result: KernelLoaderResult = Default::default();
        loader_result.setup_header = Some(boot_header);
        loader_result.kernel_load = mem_offset;

        // Seek the compressed `vmlinux.bin` and read it to memory.
        kernel_image
            .seek(SeekFrom::Start(setup_size as u64))
            .map_err(|_| Error::SeekBzImageCompressedKernel)?;
        guest_mem
            .read_exact_from(mem_offset, kernel_image, kernel_size)
            .map_err(|_| Error::ReadBzImageCompressedKernel)?;

        loader_result.kernel_end = mem_offset
            .raw_value()
            .checked_add(kernel_size as GuestUsize)
            .ok_or(KernelLoaderError::MemoryOverflow)?;

        Ok(loader_result)
    }
/// Loads a kernel from a bzImage to guest memory.
///
/// The kernel is loaded at `code32_start`, the default load address stored in the bzImage
/// setup header.
///
/// # Arguments
///
/// * `guest_mem`: [`GuestMemory`] to load the kernel in.
/// * `kernel_start`: Address in guest memory where the kernel is loaded.
/// * `kernel_image` - Input bzImage image.
/// * `highmem_start_address`: Address where high memory starts.
///
/// # Examples
///
/// ```rust
/// # extern crate vm_memory;
/// # use linux_loader::loader::*;
/// # use vm_memory::{Address, GuestAddress, GuestMemoryMmap};
/// # use std::io::Cursor;
/// let mem_size: usize = 0x1000000;
/// let himem_start = GuestAddress(0x0);
/// let kernel_addr = GuestAddress(0x200000);
/// let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
/// let mut kernel_image = vec![];
/// kernel_image.extend_from_slice(include_bytes!("bzimage"));
/// bzimage::BzImage::load(
///     &gm,
///     Some(kernel_addr),
///     &mut Cursor::new(&kernel_image),
///     Some(himem_start),
/// ).unwrap();
/// ```
///
/// [`GuestMemory`]: https://docs.rs/vm-memory/latest/vm_memory/guest_memory/trait.GuestMemory.html
Loads a kernel from a bzImage to guest memory.
The kernel is loaded at `code32_start`, the default load address stored in the bzImage
setup header.
Loads a kernel from a bzImage to guest memory . The kernel is loaded at ` code32_start ` the default load address stored in the bzImage setup header .
fn load<F, M: GuestMemory>(
        guest_mem: &M,
        kernel_start: Option<GuestAddress>,
        kernel_image: &mut F,
        highmem_start_address: Option<GuestAddress>,
    ) -> Result<KernelLoaderResult>
    where
        F: Read + Seek,
    {
        let mut kernel_size = kernel_image
            .seek(SeekFrom::End(0))
            .map_err(|_| Error::SeekBzImageEnd)? as usize;
        kernel_image
            .seek(SeekFrom::Start(0x1F1))
            .map_err(|_| Error::SeekBzImageHeader)?;
        let mut boot_header = bootparam::setup_header::default();
        boot_header
            .as_bytes()
            .read_from(0, kernel_image, mem::size_of::<bootparam::setup_header>())
            .map_err(|_| Error::ReadBzImageHeader)?;
        // If the `HdrS` magic number is not found at offset 0x202, the boot protocol version is
        // "old", the image type is assumed as zImage, not bzImage.
        if boot_header.header != 0x5372_6448 {
            Err(Error::InvalidBzImage)?;
        }
        // Follow the section related to loading the rest of the kernel in the linux boot protocol.
        if (boot_header.version < 0x0200) || ((boot_header.loadflags & 0x1) == 0x0) {
            Err(Error::InvalidBzImage)?;
        }
        let mut setup_size = boot_header.setup_sects as usize;
        if setup_size == 0 {
            setup_size = 4;
        }
        setup_size = (setup_size + 1) * 512;
        kernel_size -= setup_size;
        // Check that `code32_start`, the default address of the kernel, is not lower than high
        // memory.
        if (highmem_start_address.is_some())
            && (u64::from(boot_header.code32_start) < highmem_start_address.unwrap().raw_value())
        {
            return Err(KernelLoaderError::InvalidKernelStartAddress);
        }
        let mem_offset = match kernel_start {
            Some(start) => start,
            None => GuestAddress(u64::from(boot_header.code32_start)),
        };
        boot_header.code32_start = mem_offset.raw_value() as u32;
        let mut loader_result: KernelLoaderResult = Default::default();
        loader_result.setup_header = Some(boot_header);
        loader_result.kernel_load = mem_offset;
        // Seek the compressed `vmlinux.bin` and read it to memory.
        kernel_image
            .seek(SeekFrom::Start(setup_size as u64))
            .map_err(|_| Error::SeekBzImageCompressedKernel)?;
        guest_mem
            .read_exact_from(mem_offset, kernel_image, kernel_size)
            .map_err(|_| Error::ReadBzImageCompressedKernel)?;
        loader_result.kernel_end = mem_offset
            .raw_value()
            .checked_add(kernel_size as GuestUsize)
            .ok_or(KernelLoaderError::MemoryOverflow)?;
        Ok(loader_result)
    }
fn load < F , M : GuestMemory > ( guest_mem : & M , kernel_start : Option < GuestAddress > , kernel_image : & mut F , highmem_start_address : Option < GuestAddress > , ) -> Result < KernelLoaderResult > where F : Read + Seek , { let mut kernel_size = kernel_image . seek ( SeekFrom :: End ( 0 ) ) . map_err ( | _ | Error :: SeekBzImageEnd ) ? as usize ; kernel_image . seek ( SeekFrom :: Start ( 0x1F1 ) ) . map_err ( | _ | Error :: SeekBzImageHeader ) ? ; let mut boot_header = bootparam :: setup_header :: default ( ) ; boot_header . as_bytes ( ) . read_from ( 0 , kernel_image , mem :: size_of :: < bootparam :: setup_header > ( ) ) . map_err ( | _ | Error :: ReadBzImageHeader ) ? ; // If the `HdrS` magic number is not found at offset 0x202, the boot protocol version is // "old", the image type is assumed as zImage, not bzImage. if boot_header . header != 0x5372_6448 { Err ( Error :: InvalidBzImage ) ? ; } // Follow the section related to loading the rest of the kernel in the linux boot protocol. if ( boot_header . version < 0x0200 ) || ( ( boot_header . loadflags & 0x1 ) == 0x0 ) { Err ( Error :: InvalidBzImage ) ? ; } let mut setup_size = boot_header . setup_sects as usize ; if setup_size == 0 { setup_size = 4 ; } setup_size = ( setup_size + 1 ) * 512 ; kernel_size -= setup_size ; // Check that `code32_start`, the default address of the kernel, is not lower than high // memory. if ( highmem_start_address . is_some ( ) ) && ( u64 :: from ( boot_header . code32_start ) < highmem_start_address . unwrap ( ) . raw_value ( ) ) { return Err ( KernelLoaderError :: InvalidKernelStartAddress ) ; } let mem_offset = match kernel_start { Some ( start ) => start , None => GuestAddress ( u64 :: from ( boot_header . code32_start ) ) , } ; boot_header . code32_start = mem_offset . raw_value ( ) as u32 ; let mut loader_result : KernelLoaderResult = Default :: default ( ) ; loader_result . setup_header = Some ( boot_header ) ; loader_result . kernel_load = mem_offset ; // Seek the compressed `vmlinux.bin` and read it to memory. kernel_image . seek ( SeekFrom :: Start ( setup_size as u64 ) ) . map_err ( | _ | Error :: SeekBzImageCompressedKernel ) ? ; guest_mem . read_exact_from ( mem_offset , kernel_image , kernel_size ) . map_err ( | _ | Error :: ReadBzImageCompressedKernel ) ? ; loader_result . kernel_end = mem_offset . raw_value ( ) . checked_add ( kernel_size as GuestUsize ) . ok_or ( KernelLoaderError :: MemoryOverflow ) ? ; Ok ( loader_result ) }
Loads a kernel from a bzImage to guest memory.
Loads a kernel from a bzImage to guest memory .
guest_mem
&M
kernel_start
Option<GuestAddress>
kernel_image
&mut F
highmem_start_address
Option<GuestAddress>
guest_mem
&M
kernel_start
Option<GuestAddress>
kernel_image
&mut F
highmem_start_address
Option<GuestAddress>
examples
None
extern crate vm_memory;
None
use linux_loader::loader::*;
None
use vm_memory::{address, guestaddress, guestmemorymmap};
None
use std::io::cursor;
let mem_size: usize = 0x1000000;
let himem_start = GuestAddress(0x0);
let kernel_addr = GuestAddress(0x200000);
let gm = GuestMemoryMmap::from_ranges(&[(GuestAddress(0x0), mem_size)]).unwrap();
let mut kernel_image = vec![];
kernel_image.extend_from_slice(include_bytes!("bzimage"));
bzimage::BzImage::load(
&gm,
Some(kernel_addr),
&mut Cursor::new(&kernel_image),
Some(himem_start),
).unwrap();
```


let mem_size : usize = 0x1000000 ; let himem_start = GuestAddress ( 0x0 ) ; let kernel_addr = GuestAddress ( 0x200000 ) ; let gm = GuestMemoryMmap :: from_ranges ( & [ ( GuestAddress ( 0x0 ) mem_size ) ] ) . unwrap () ; let mut kernel_image = vec! [] ; kernel_image . extend_from_slice ( include_bytes! ( " bzimage " )) ; bzimage :: BzImage :: load ( &gm Some ( kernel_addr ) &mut Cursor :: new ( &kernel_image ) Some ( himem_start ) ) . unwrap () ; ` ` `