Skip to content

Commit 456d2dd

Browse files
authored
Merge pull request #91 from mythril-hypervisor/adam-dev
Include guest bios and linux loader in mythril binary
2 parents 1b8a8e2 + bc4bc5a commit 456d2dd

File tree

10 files changed

+25
-46
lines changed

10 files changed

+25
-46
lines changed

.github/workflows/rust.yml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
options: "-u 0:0"
2222
steps:
2323
- uses: actions/checkout@v1
24+
with:
25+
submodules: true
2426
- name: Unit Tests
2527
run: |
2628
make test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/boot
22
/target
33
mythril/target
4+
mythril/src/blob/bios.bin
45
_boot.img
56
**/*.rs.bk
67
debug.log

Makefile

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mythril_src = $(shell find mythril* -type f -name '*.rs' -or -name '*.S' -or -na
77
-name 'Cargo.toml')
88
kernel = linux/arch/x86_64/boot/bzImage
99
seabios = seabios/out/bios.bin
10+
seabios_blob = mythril/src/blob/bios.bin
1011
git_hooks_src = $(wildcard .mythril_githooks/*)
1112
git_hooks = $(subst .mythril_githooks,.git/hooks,$(git_hooks_src))
1213

@@ -40,20 +41,23 @@ $(seabios):
4041
cp scripts/seabios.config seabios/.config
4142
make -C seabios
4243

44+
$(seabios_blob): $(seabios)
45+
cp $(seabios) $(seabios_blob)
46+
4347
$(kernel):
4448
cp scripts/kernel.config linux/.config
4549
make -C linux bzImage
4650

4751
.PHONY: qemu
48-
qemu: mythril $(seabios) $(kernel)
52+
qemu: mythril $(kernel)
4953
./scripts/mythril-run.sh $(mythril_binary) $(QEMU_EXTRA)
5054

5155
.PHONY: qemu-debug
52-
qemu-debug: mythril-debug $(seabios) $(kernel)
56+
qemu-debug: mythril-debug $(kernel)
5357
./scripts/mythril-run.sh $(mythril_binary) \
5458
-gdb tcp::1234 -S $(QEMU_EXTRA)
5559

56-
$(mythril_binary): $(mythril_src)
60+
$(mythril_binary): $(mythril_src) $(seabios_blob)
5761
$(CARGO) build $(CARGO_BUILD_FLAGS) $(CARGO_MANIFEST) \
5862
-Z build-std=core,alloc \
5963
--target mythril/mythril_target.json \
@@ -67,7 +71,7 @@ fmt:
6771
$(CARGO) fmt $(CARGO_MANIFEST) --all
6872

6973
.PHONY: test_core
70-
test_common:
74+
test_common: $(seabios_blob)
7175
$(CARGO) test $(CARGO_MANIFEST) --lib \
7276
--features=test \
7377

@@ -77,6 +81,7 @@ test: test_common
7781
.PHONY: clean
7882
clean:
7983
$(CARGO) clean $(CARGO_MANIFEST)
84+
rm $(seabios_blob)
8085
make -C seabios clean
8186
make -C linux clean
8287

File renamed without changes.

mythril/src/kmain.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ fn default_vm(
4949
let mut config =
5050
vm::VirtualMachineConfig::new(vec![core], mem, physical_config);
5151

52-
// FIXME: When `map_bios` may return an error, log the error.
53-
config.map_bios("seabios.bin".into()).unwrap_or(());
54-
5552
let device_map = config.virtual_devices_mut();
5653
device_map
5754
.register_device(virtdev::acpi::AcpiRuntime::new(0xb000).unwrap())
@@ -100,10 +97,7 @@ fn default_vm(
10097
// The 'linuxboot' file is an option rom that loads the linux kernel
10198
// via qemu_fw_cfg
10299
fw_cfg_builder
103-
.add_file(
104-
"genroms/linuxboot_dma.bin",
105-
info.find_module("linuxboot_dma.bin").unwrap().data(),
106-
)
100+
.add_file("genroms/linuxboot_dma.bin", linux::LINUXBOOT_DMA_ROM)
107101
.unwrap();
108102

109103
// Passing the bootorder file automatically selects the option rom

mythril/src/linux.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ use crate::virtdev::qemu_fw_cfg::{FwCfgSelector, QemuFwCfgBuilder};
44
use bitflags::bitflags;
55
use byteorder::{ByteOrder, LittleEndian};
66

7+
// This blob is taken from QEMU. See:
8+
// https://github.com/qemu/qemu/blob/887adde81d1f1f3897f1688d37ec6851b4fdad86/pc-bios/optionrom/linuxboot_dma.c
9+
pub const LINUXBOOT_DMA_ROM: &'static [u8] =
10+
include_bytes!("blob/linuxboot_dma.bin");
11+
712
bitflags! {
813
pub struct XLoadFlags: u32 {
914
const KERNEL_64 = 1 << 0;

mythril/src/vm.rs

+7-31
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use alloc::vec::Vec;
2121
use arraydeque::ArrayDeque;
2222
use spin::RwLock;
2323

24+
static BIOS_BLOB: &'static [u8] = include_bytes!("blob/bios.bin");
25+
2426
static VIRTUAL_MACHINES: RoAfterInit<VirtualMachines> =
2527
RoAfterInit::uninitialized();
2628

@@ -218,7 +220,6 @@ pub struct PhysicalDeviceConfig {
218220
pub struct VirtualMachineConfig {
219221
cpus: Vec<percore::CoreId>,
220222
images: Vec<(String, GuestPhysAddr)>,
221-
bios: Option<String>,
222223
virtual_devices: DeviceMap,
223224
physical_devices: PhysicalDeviceConfig,
224225
memory: u64, // in MB
@@ -241,7 +242,6 @@ impl VirtualMachineConfig {
241242
images: vec![],
242243
virtual_devices: DeviceMap::default(),
243244
physical_devices: physical_devices,
244-
bios: None,
245245
memory: memory,
246246
}
247247
}
@@ -259,18 +259,6 @@ impl VirtualMachineConfig {
259259
Ok(())
260260
}
261261

262-
/// Specify that the given image 'path' should be mapped as the BIOS
263-
///
264-
/// The precise meaning of `image` will vary by platform. This will be a
265-
/// value suitable to be passed to `VmServices::read_file`.
266-
///
267-
/// The BIOS image will be mapped such that the end of the image is at
268-
/// 0xffffffff and 0xfffff (i.e., it will be mapped in two places)
269-
pub fn map_bios(&mut self, bios: String) -> Result<()> {
270-
self.bios = Some(bios);
271-
Ok(())
272-
}
273-
274262
/// Access the configurations virtual `DeviceMap`
275263
pub fn virtual_devices(&self) -> &DeviceMap {
276264
&self.virtual_devices
@@ -398,25 +386,15 @@ impl VirtualMachine {
398386
Self::map_data(data, addr, space)
399387
}
400388

401-
fn map_bios(
402-
bios: &str,
403-
space: &mut GuestAddressSpace,
404-
info: &BootInfo,
405-
) -> Result<()> {
406-
let data = info
407-
.find_module(bios)
408-
.ok_or_else(|| {
409-
Error::InvalidValue(format!("No such bios '{}'", bios))
410-
})?
411-
.data();
412-
let bios_size = data.len() as u64;
389+
fn map_bios(space: &mut GuestAddressSpace) -> Result<()> {
390+
let bios_size = BIOS_BLOB.len() as u64;
413391
Self::map_data(
414-
data,
392+
BIOS_BLOB,
415393
&memory::GuestPhysAddr::new((1024 * 1024) - bios_size),
416394
space,
417395
)?;
418396
Self::map_data(
419-
data,
397+
BIOS_BLOB,
420398
&memory::GuestPhysAddr::new((4 * 1024 * 1024 * 1024) - bios_size),
421399
space,
422400
)
@@ -429,9 +407,7 @@ impl VirtualMachine {
429407
let mut guest_space = GuestAddressSpace::new()?;
430408

431409
// First map the bios
432-
if let Some(ref bios) = config.bios {
433-
Self::map_bios(&bios, &mut guest_space, info)?;
434-
}
410+
Self::map_bios(&mut guest_space)?;
435411

436412
// Now map any guest iamges
437413
for image in config.images.iter() {

scripts/grub.cfg

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ menuentry 'Mythril' {
55
echo 'Loading Mythril'
66
acpi -2
77
multiboot2 /boot/mythril.bin
8-
module2 /boot/seabios.bin seabios.bin
9-
module2 /boot/linuxboot_dma.bin linuxboot_dma.bin
108
module2 /boot/vmlinuz kernel
119
module2 /boot/initramfs initramfs
1210
}

scripts/linuxboot.bin

-1 KB
Binary file not shown.

scripts/mythril-run.sh

-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ rm -rf _isofiles
1313
mkdir -p _isofiles/boot/grub
1414

1515
cp scripts/grub.cfg _isofiles/boot/grub/
16-
cp seabios/out/bios.bin _isofiles/boot/seabios.bin
1716
cp linux/arch/x86_64/boot/bzImage _isofiles/boot/vmlinuz
18-
cp scripts/linuxboot_dma.bin _isofiles/boot/linuxboot_dma.bin
1917
cp scripts/initramfs _isofiles/boot/initramfs
2018
cp "$1" _isofiles/boot/mythril.bin
2119

0 commit comments

Comments
 (0)