Utilize libflate crate to compress and decompress payload

This commit is contained in:
azongchang
2024-07-20 12:12:46 +00:00
committed by Tate, Hongliang Tian
parent 930b0d208d
commit 6752baf166
15 changed files with 321 additions and 37 deletions

View File

@ -14,6 +14,8 @@ path = "src/main.rs"
[dependencies]
cfg-if = "1.0.0"
core2 = { version = "0.4.0", default-features = false, features = ["nightly"] }
libflate = { version = "2.1.0", default-features = false }
linux-boot-params = { path = "../boot-params", version = "0.1.0" }
uart_16550 = "0.3.0"
xmas-elf = "0.8.0"

View File

@ -0,0 +1,50 @@
// SPDX-License-Identifier: MPL-2.0
//! This module is used to decompress payload.
extern crate alloc;
pub use alloc::vec::Vec;
use core::convert::TryFrom;
use core2::io::Read;
use libflate::{gzip, zlib};
enum MagicNumber {
Elf,
Gzip,
Zlib,
}
impl TryFrom<&[u8]> for MagicNumber {
type Error = &'static str;
fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
match *slice {
[0x7F, 0x45, 0x4C, 0x46, ..] => Ok(Self::Elf),
[0x1F, 0x8B, ..] => Ok(Self::Gzip),
[0x78, 0x9C, ..] => Ok(Self::Zlib),
_ => Err("Unsupported payload type"),
}
}
}
/// Checking the encoding format and matching decoding methods to decode payload.
pub fn decode_payload(payload: &[u8]) -> Vec<u8> {
let mut kernel = Vec::new();
let magic = MagicNumber::try_from(payload).unwrap();
match magic {
MagicNumber::Elf => {
kernel = payload.to_vec();
}
MagicNumber::Gzip => {
let mut decoder = gzip::Decoder::new(payload).unwrap();
decoder.read_to_end(&mut kernel).unwrap();
}
MagicNumber::Zlib => {
let mut decoder = zlib::Decoder::new(payload).unwrap();
decoder.read_to_end(&mut kernel).unwrap();
}
}
kernel
}

View File

@ -8,6 +8,7 @@ use uefi::{
};
use super::{
decoder::decode_payload,
paging::{Ia32eFlags, PageNumber, PageTableCreator},
relocation::apply_rela_dyn_relocations,
};
@ -55,9 +56,11 @@ fn efi_phase_boot(
uefi_services::println!("[EFI stub] Relocations applied.");
uefi_services::println!("[EFI stub] Loading payload.");
let payload = unsafe { crate::get_payload(&*boot_params_ptr) };
crate::loader::load_elf(payload);
let kernel = decode_payload(payload);
uefi_services::println!("[EFI stub] Loading payload.");
crate::loader::load_elf(&kernel);
uefi_services::println!("[EFI stub] Exiting EFI boot services.");
let memory_type = {

View File

@ -1,5 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
mod decoder;
mod efi;
mod paging;
mod relocation;