mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-25 02:13:24 +00:00
Implement linux x86 32bit legacy boot protocol
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
9d0e0bbc70
commit
a532340c65
98
runner/src/machine/qemu_grub_efi/linux_boot.rs
Normal file
98
runner/src/machine/qemu_grub_efi/linux_boot.rs
Normal file
@ -0,0 +1,98 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use xmas_elf::program::{ProgramHeader, SegmentData};
|
||||
|
||||
// We chose the legacy setup sections to be 7 so that the setup header
|
||||
// is page-aligned and the legacy setup section size would be 0x1000.
|
||||
const LEGACY_SETUP_SECS: usize = 7;
|
||||
const LEGACY_SETUP_SEC_SIZE: usize = 0x200 * (LEGACY_SETUP_SECS + 1);
|
||||
const SETUP32_LMA: usize = 0x100000;
|
||||
|
||||
/// We need a binary which satisfies `LMA == File_Offset`, and objcopy
|
||||
/// does not satisfy us well, so we should parse the ELF and do our own
|
||||
/// objcopy job.
|
||||
///
|
||||
/// Interstingly, the resulting binary should be the same as the memory
|
||||
/// dump of the kernel setup header when it's loaded by the bootloader.
|
||||
fn header_to_raw_binary(elf_file: &[u8]) -> Vec<u8> {
|
||||
let elf = xmas_elf::ElfFile::new(&elf_file).unwrap();
|
||||
let mut bin = Vec::<u8>::new();
|
||||
|
||||
for ph in elf.program_iter() {
|
||||
let ProgramHeader::Ph32(program) = ph else {
|
||||
panic!("Unexpected program header type");
|
||||
};
|
||||
if program.get_type().unwrap() == xmas_elf::program::Type::Load {
|
||||
let SegmentData::Undefined(header_data) = program.get_data(&elf).unwrap() else {
|
||||
panic!("Unexpected segment data type");
|
||||
};
|
||||
let dst_file_offset =
|
||||
program.virtual_addr as usize + LEGACY_SETUP_SEC_SIZE - SETUP32_LMA;
|
||||
let dst_file_length = program.file_size as usize;
|
||||
if bin.len() < dst_file_offset + dst_file_length {
|
||||
bin.resize(dst_file_offset + dst_file_length, 0);
|
||||
}
|
||||
let dest_slice = bin[dst_file_offset..dst_file_offset + dst_file_length].as_mut();
|
||||
dest_slice.copy_from_slice(header_data);
|
||||
}
|
||||
}
|
||||
|
||||
bin
|
||||
}
|
||||
|
||||
/// This function sould be used when generating the Linux x86 Boot setup header.
|
||||
/// Some fields in the Linux x86 Boot setup header should be filled after assembled.
|
||||
/// And the filled fields must have the bytes with values of 0xAB. See
|
||||
/// `framework/jinux-frame/src/arch/x86/boot/linux_boot/setup/src/header.S` for more
|
||||
/// info on this mechanism.
|
||||
fn fill_header_field(header: &mut [u8], offset: usize, value: &[u8]) {
|
||||
let size = value.len();
|
||||
assert_eq!(
|
||||
&header[offset..offset + size],
|
||||
vec![0xABu8; size].as_slice()
|
||||
);
|
||||
header[offset..offset + size].copy_from_slice(value);
|
||||
}
|
||||
|
||||
pub fn make_bzimage(path: &Path, kernel_path: &Path, header_path: &Path) -> std::io::Result<()> {
|
||||
let mut header = Vec::new();
|
||||
File::open(header_path)?.read_to_end(&mut header)?;
|
||||
let mut header = header_to_raw_binary(&header);
|
||||
// Pad the header to let the payload starts with 8-byte alignment.
|
||||
header.resize((header.len() + 7) & !7, 0x00);
|
||||
|
||||
let mut kernel = Vec::new();
|
||||
File::open(kernel_path)?.read_to_end(&mut kernel)?;
|
||||
|
||||
let header_len = header.len();
|
||||
let kernel_len = kernel.len();
|
||||
|
||||
let payload_offset = header_len - LEGACY_SETUP_SEC_SIZE + SETUP32_LMA;
|
||||
fill_header_field(
|
||||
&mut header,
|
||||
0x248, /* payload_offset */
|
||||
&(payload_offset as u32).to_le_bytes(),
|
||||
);
|
||||
|
||||
fill_header_field(
|
||||
&mut header,
|
||||
0x24C, /* payload_length */
|
||||
&(kernel_len as u32).to_le_bytes(),
|
||||
);
|
||||
|
||||
fill_header_field(
|
||||
&mut header,
|
||||
0x260, /* init_size */
|
||||
&((header_len + kernel_len) as u32).to_le_bytes(),
|
||||
);
|
||||
|
||||
let mut kernel_image = File::create(path)?;
|
||||
kernel_image.write_all(&header)?;
|
||||
kernel_image.write_all(&kernel)?;
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
mod linux_boot;
|
||||
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{Read, Write},
|
||||
fs,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
@ -85,13 +87,14 @@ pub fn create_bootdev_image(
|
||||
BootProtocol::Linux => {
|
||||
// Find the setup header in the build script output directory.
|
||||
let bs_out_dir = glob("target/x86_64-custom/debug/build/jinux-frame-*").unwrap();
|
||||
let header_bin = Path::new(bs_out_dir.into_iter().next().unwrap().unwrap().as_path())
|
||||
let header_path = Path::new(bs_out_dir.into_iter().next().unwrap().unwrap().as_path())
|
||||
.join("out")
|
||||
.join("bin")
|
||||
.join("jinux-frame-x86-boot-setup.bin");
|
||||
// Make the `zimage`-compatible kernel image and place it in the boot directory.
|
||||
.join("jinux-frame-x86-boot-setup");
|
||||
// Make the `bzImage`-compatible kernel image and place it in the boot directory.
|
||||
let target_path = iso_root.join("boot").join("jinuz");
|
||||
make_zimage(&target_path, &jinux_path.as_path(), &header_bin.as_path()).unwrap();
|
||||
linux_boot::make_bzimage(&target_path, &jinux_path.as_path(), &header_path.as_path())
|
||||
.unwrap();
|
||||
target_path
|
||||
}
|
||||
BootProtocol::Multiboot | BootProtocol::Multiboot2 => {
|
||||
@ -162,52 +165,3 @@ pub fn generate_grub_cfg(
|
||||
|
||||
buffer
|
||||
}
|
||||
|
||||
/// This function sould be used when generating the Linux x86 Boot setup header.
|
||||
/// Some fields in the Linux x86 Boot setup header should be filled after assembled.
|
||||
/// And the filled fields must have the bytes with values of 0xAB. See
|
||||
/// `framework/jinux-frame/src/arch/x86/boot/linux_boot/setup/src/header.S` for more
|
||||
/// info on this mechanism.
|
||||
fn fill_header_field(header: &mut [u8], offset: usize, value: &[u8]) {
|
||||
let size = value.len();
|
||||
assert_eq!(
|
||||
&header[offset..offset + size],
|
||||
vec![0xABu8; size].as_slice()
|
||||
);
|
||||
header[offset..offset + size].copy_from_slice(value);
|
||||
}
|
||||
|
||||
fn make_zimage(path: &Path, kernel_path: &Path, header_path: &Path) -> std::io::Result<()> {
|
||||
let mut header = Vec::new();
|
||||
File::open(header_path)?.read_to_end(&mut header)?;
|
||||
// Pad the header to let the payload starts with 8-byte alignment.
|
||||
header.resize((header.len() + 7) & !7, 0x00);
|
||||
|
||||
let mut kernel = Vec::new();
|
||||
File::open(kernel_path)?.read_to_end(&mut kernel)?;
|
||||
|
||||
let header_len = header.len();
|
||||
let kernel_len = kernel.len();
|
||||
|
||||
fill_header_field(
|
||||
&mut header,
|
||||
0x248, /* payload_offset */
|
||||
&(header_len as u32).to_le_bytes(),
|
||||
);
|
||||
fill_header_field(
|
||||
&mut header,
|
||||
0x24C, /* payload_length */
|
||||
&(kernel_len as u32).to_le_bytes(),
|
||||
);
|
||||
fill_header_field(
|
||||
&mut header,
|
||||
0x260, /* init_size */
|
||||
&((kernel_len + header_len) as u32).to_le_bytes(),
|
||||
);
|
||||
|
||||
let mut kernel_image = File::create(path)?;
|
||||
kernel_image.write_all(&header)?;
|
||||
kernel_image.write_all(&kernel)?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user