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

@ -10,6 +10,7 @@ use std::{
use linux_bzimage_builder::{legacy32_rust_target_json, make_bzimage, BzImageType};
use crate::{
arch::Arch,
bundle::{
bin::{AsterBin, AsterBinType, AsterBzImageMeta, AsterElfMeta},
file::BundleFile,
@ -58,6 +59,7 @@ pub fn make_install_bzimage(
AsterBin::new(
&install_path,
aster_elf.arch(),
AsterBinType::BzImage(AsterBzImageMeta {
support_legacy32_boot: linux_x86_legacy_boot,
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();
}
// 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.
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open(&result_elf_path)
.unwrap();
if elf.arch() == Arch::X86_64 {
// 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.
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open(&result_elf_path)
.unwrap();
let bytes: [u8; 2] = [0x03, 0x00];
let bytes: [u8; 2] = [0x03, 0x00];
file.seek(SeekFrom::Start(18)).unwrap();
file.write_all(&bytes).unwrap();
file.flush().unwrap();
file.seek(SeekFrom::Start(18)).unwrap();
file.write_all(&bytes).unwrap();
file.flush().unwrap();
}
AsterBin::new(
&result_elf_path,
elf.arch(),
AsterBinType::Elf(AsterElfMeta {
has_linux_header: false,
has_pvh_header: false,