Make systemd boot the EFI binary

This commit is contained in:
Julian Stecklina 2022-11-21 15:36:39 +01:00
parent cd39fd3a6b
commit b10ee4d0d6
5 changed files with 95 additions and 6 deletions

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# UEFI Helpers
How to boot a VM: https://rust-osdev.github.io/uefi-rs/HEAD/tutorial/vm.html

View File

@ -22,12 +22,90 @@
cargo = rust; cargo = rust;
rustc = rust; rustc = rust;
}; };
qemuUefi = pkgs.writeShellScriptBin "qemu-uefi" ''
exec ${pkgs.qemu}/bin/qemu-system-x86_64 \
-machine q35,accel=kvm:tcg -bios ${pkgs.OVMF.fd}/FV/OVMF.fd \
-m 4096 -serial stdio "$@"
'';
uefi-run = naersk'.buildPackage {
src = pkgs.fetchFromGitHub {
owner = "Richard-W";
repo = "uefi-run";
rev = "8ba33c934525458a784a6620705bcf46c3ca91d2";
sha256 = "fwzWdOinW/ECVI/65pPB1shxPdl2nZThAqlg8wlWg/g=";
};
nativeBuildInputs = [ pkgs.makeWrapper ];
postInstall = ''
wrapProgram "$out/bin/uefi-run" \
--add-flags '--bios-path ${pkgs.OVMF.fd}/FV/OVMF.fd --qemu-path ${pkgs.qemu}/bin/qemu-system-x86_64'
'';
};
systemd-boot-run = pkgs.writeShellScriptBin "systemd-boot-run" ''
${uefi-run}/bin/uefi-run lib/systemd/boot/efi/systemd-bootx64.efi
'';
add-sections = pkgs.writeShellScriptBin "add-sections" ''
set -eu
IN=$1
OSREL=$2
CMDLINE=$3
OUT=$4
stub_line=$(objdump -h "$1" | tail -2 | head -1)
stub_size=0x$(echo "$stub_line" | awk '{print $3}')
stub_offs=0x$(echo "$stub_line" | awk '{print $4}')
osrel_offs=$((stub_size + stub_offs))
cmdline_offs=$((osrel_offs + $(stat -c%s "$OSREL")))
objcopy \
--add-section .osrel="$OSREL" --change-section-vma .osrel=$(printf 0x%x $osrel_offs) \
--add-section .cmdline="$CMDLINE" \
--change-section-vma .cmdline=$(printf 0x%x $cmdline_offs) \
"$IN" "$OUT"
'';
lanzaboote = naersk'.buildPackage {
src = ./rust;
cargoBuildOptions = old: old ++ [
"--target x86_64-unknown-uefi"
];
};
osrel = pkgs.writeText "lanzaboote-osrel" ''
NAME=Lanzaboote
VERSION="0"
'';
cmdline = pkgs.writeText "lanzaboote-cmdline" " ";
lanzaboote-uki = pkgs.runCommand "lanzboote-uki" {
nativeBuildInputs = [
pkgs.binutils-unwrapped
add-sections
];
} ''
mkdir -p $out/bin
add-sections ${lanzaboote}/bin/lanzaboote.efi ${osrel} ${cmdline} $out/bin/lanzaboote.efi
'';
in in
{ rec {
packages.x86_64-linux.default = naersk'.buildPackage { packages.x86_64-linux = {
src = ./rust; inherit qemuUefi uefi-run lanzaboote lanzaboote-uki;
cargoBuildOptions = old: old ++ [ default = lanzaboote-uki;
"--target x86_64-unknown-uefi" };
devShells.x86_64-linux.default = pkgs.mkShell {
nativeBuildInputs = [
qemuUefi
uefi-run
rust
pkgs.pev
add-sections
]; ];
}; };
}; };

1
rust/Cargo.lock generated
View File

@ -24,6 +24,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "lanzaboote" name = "lanzaboote"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"log",
"uefi", "uefi",
"uefi-services", "uefi-services",
] ]

View File

@ -5,5 +5,7 @@ edition = "2021"
publish = false publish = false
[dependencies] [dependencies]
uefi = { version = "0.18.0", features = ["alloc"] } uefi = { version = "0.18.0", features = [ "alloc", "logger" ] }
uefi-services = "0.15.0" uefi-services = "0.15.0"
log = "0.4.17"

View File

@ -2,11 +2,14 @@
#![no_std] #![no_std]
#![feature(abi_efiapi)] #![feature(abi_efiapi)]
use log::info;
use uefi::prelude::*; use uefi::prelude::*;
#[entry] #[entry]
fn main(_handle: Handle, mut system_table: SystemTable<Boot>) -> Status { fn main(_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
uefi_services::init(&mut system_table).unwrap(); uefi_services::init(&mut system_table).unwrap();
info!("Hello World!");
Status::SUCCESS Status::SUCCESS
} }