mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-24 01:43:22 +00:00
Set EM_386 only in x86
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
e7a75947ea
commit
eb5356c492
@ -6,10 +6,12 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::file::BundleFile;
|
use super::file::BundleFile;
|
||||||
|
use crate::arch::Arch;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct AsterBin {
|
pub struct AsterBin {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
|
arch: Arch,
|
||||||
typ: AsterBinType,
|
typ: AsterBinType,
|
||||||
version: String,
|
version: String,
|
||||||
sha256sum: String,
|
sha256sum: String,
|
||||||
@ -48,9 +50,16 @@ impl BundleFile for AsterBin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AsterBin {
|
impl AsterBin {
|
||||||
pub fn new(path: impl AsRef<Path>, typ: AsterBinType, version: String, stripped: bool) -> Self {
|
pub fn new(
|
||||||
|
path: impl AsRef<Path>,
|
||||||
|
arch: Arch,
|
||||||
|
typ: AsterBinType,
|
||||||
|
version: String,
|
||||||
|
stripped: bool,
|
||||||
|
) -> Self {
|
||||||
let created = Self {
|
let created = Self {
|
||||||
path: path.as_ref().to_path_buf(),
|
path: path.as_ref().to_path_buf(),
|
||||||
|
arch,
|
||||||
typ,
|
typ,
|
||||||
version,
|
version,
|
||||||
sha256sum: String::new(),
|
sha256sum: String::new(),
|
||||||
@ -62,6 +71,10 @@ impl AsterBin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn arch(&self) -> Arch {
|
||||||
|
self.arch
|
||||||
|
}
|
||||||
|
|
||||||
pub fn version(&self) -> &String {
|
pub fn version(&self) -> &String {
|
||||||
&self.version
|
&self.version
|
||||||
}
|
}
|
||||||
@ -78,6 +91,7 @@ impl AsterBin {
|
|||||||
fs::remove_file(&self.path).unwrap();
|
fs::remove_file(&self.path).unwrap();
|
||||||
Self {
|
Self {
|
||||||
path: PathBuf::from(file_name),
|
path: PathBuf::from(file_name),
|
||||||
|
arch: self.arch,
|
||||||
typ: self.typ,
|
typ: self.typ,
|
||||||
version: self.version,
|
version: self.version,
|
||||||
sha256sum: self.sha256sum,
|
sha256sum: self.sha256sum,
|
||||||
|
@ -10,6 +10,7 @@ use std::{
|
|||||||
use linux_bzimage_builder::{legacy32_rust_target_json, make_bzimage, BzImageType};
|
use linux_bzimage_builder::{legacy32_rust_target_json, make_bzimage, BzImageType};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
arch::Arch,
|
||||||
bundle::{
|
bundle::{
|
||||||
bin::{AsterBin, AsterBinType, AsterBzImageMeta, AsterElfMeta},
|
bin::{AsterBin, AsterBinType, AsterBzImageMeta, AsterElfMeta},
|
||||||
file::BundleFile,
|
file::BundleFile,
|
||||||
@ -58,6 +59,7 @@ pub fn make_install_bzimage(
|
|||||||
|
|
||||||
AsterBin::new(
|
AsterBin::new(
|
||||||
&install_path,
|
&install_path,
|
||||||
|
aster_elf.arch(),
|
||||||
AsterBinType::BzImage(AsterBzImageMeta {
|
AsterBinType::BzImage(AsterBzImageMeta {
|
||||||
support_legacy32_boot: linux_x86_legacy_boot,
|
support_legacy32_boot: linux_x86_legacy_boot,
|
||||||
support_efi_boot: false,
|
support_efi_boot: false,
|
||||||
@ -107,25 +109,28 @@ pub fn make_elf_for_qemu(install_dir: impl AsRef<Path>, elf: &AsterBin, strip: b
|
|||||||
std::fs::copy(elf.path(), &result_elf_path).unwrap();
|
std::fs::copy(elf.path(), &result_elf_path).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Because QEMU denies a x86_64 multiboot ELF file (GRUB2 accept it, btw),
|
if elf.arch() == Arch::X86_64 {
|
||||||
// modify `em_machine` to pretend to be an x86 (32-bit) ELF image,
|
// Because QEMU denies a x86_64 multiboot ELF file (GRUB2 accept it, btw),
|
||||||
//
|
// modify `em_machine` to pretend to be an x86 (32-bit) ELF image,
|
||||||
// https://github.com/qemu/qemu/blob/950c4e6c94b15cd0d8b63891dddd7a8dbf458e6a/hw/i386/multiboot.c#L197
|
//
|
||||||
// Set EM_386 (0x0003) to em_machine.
|
// https://github.com/qemu/qemu/blob/950c4e6c94b15cd0d8b63891dddd7a8dbf458e6a/hw/i386/multiboot.c#L197
|
||||||
let mut file = OpenOptions::new()
|
// Set EM_386 (0x0003) to em_machine.
|
||||||
.read(true)
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.read(true)
|
||||||
.open(&result_elf_path)
|
.write(true)
|
||||||
.unwrap();
|
.open(&result_elf_path)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let bytes: [u8; 2] = [0x03, 0x00];
|
let bytes: [u8; 2] = [0x03, 0x00];
|
||||||
|
|
||||||
file.seek(SeekFrom::Start(18)).unwrap();
|
file.seek(SeekFrom::Start(18)).unwrap();
|
||||||
file.write_all(&bytes).unwrap();
|
file.write_all(&bytes).unwrap();
|
||||||
file.flush().unwrap();
|
file.flush().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
AsterBin::new(
|
AsterBin::new(
|
||||||
&result_elf_path,
|
&result_elf_path,
|
||||||
|
elf.arch(),
|
||||||
AsterBinType::Elf(AsterElfMeta {
|
AsterBinType::Elf(AsterElfMeta {
|
||||||
has_linux_header: false,
|
has_linux_header: false,
|
||||||
has_pvh_header: false,
|
has_pvh_header: false,
|
||||||
|
@ -150,7 +150,7 @@ pub fn do_build(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let aster_elf = build_kernel_elf(
|
let aster_elf = build_kernel_elf(
|
||||||
&config.target_arch,
|
config.target_arch,
|
||||||
&build.profile,
|
&build.profile,
|
||||||
&build.features[..],
|
&build.features[..],
|
||||||
build.no_default_features,
|
build.no_default_features,
|
||||||
@ -187,7 +187,7 @@ pub fn do_build(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn build_kernel_elf(
|
fn build_kernel_elf(
|
||||||
arch: &Arch,
|
arch: Arch,
|
||||||
profile: &str,
|
profile: &str,
|
||||||
features: &[String],
|
features: &[String],
|
||||||
no_default_features: bool,
|
no_default_features: bool,
|
||||||
@ -255,6 +255,7 @@ fn build_kernel_elf(
|
|||||||
|
|
||||||
AsterBin::new(
|
AsterBin::new(
|
||||||
aster_bin_path,
|
aster_bin_path,
|
||||||
|
arch,
|
||||||
AsterBinType::Elf(AsterElfMeta {
|
AsterBinType::Elf(AsterElfMeta {
|
||||||
has_linux_header: false,
|
has_linux_header: false,
|
||||||
has_pvh_header: false,
|
has_pvh_header: false,
|
||||||
|
Reference in New Issue
Block a user