Set EM_386 only in x86

This commit is contained in:
YanWQ-monad
2024-07-19 04:52:48 +08:00
committed by Tate, Hongliang Tian
parent e7a75947ea
commit eb5356c492
3 changed files with 37 additions and 17 deletions

View File

@ -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,

View File

@ -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,6 +109,7 @@ 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();
} }
if elf.arch() == Arch::X86_64 {
// Because QEMU denies a x86_64 multiboot ELF file (GRUB2 accept it, btw), // 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, // modify `em_machine` to pretend to be an x86 (32-bit) ELF image,
// //
@ -123,9 +126,11 @@ pub fn make_elf_for_qemu(install_dir: impl AsRef<Path>, elf: &AsterBin, strip: b
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,

View File

@ -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,