diff --git a/README.md b/README.md new file mode 100644 index 0000000..63399ed --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ + + +# UEFI Helpers + +How to boot a VM: https://rust-osdev.github.io/uefi-rs/HEAD/tutorial/vm.html diff --git a/flake.nix b/flake.nix index 651a121..7b2d161 100644 --- a/flake.nix +++ b/flake.nix @@ -22,12 +22,90 @@ cargo = 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 - { - packages.x86_64-linux.default = naersk'.buildPackage { - src = ./rust; - cargoBuildOptions = old: old ++ [ - "--target x86_64-unknown-uefi" + rec { + packages.x86_64-linux = { + inherit qemuUefi uefi-run lanzaboote lanzaboote-uki; + default = lanzaboote-uki; + }; + + devShells.x86_64-linux.default = pkgs.mkShell { + nativeBuildInputs = [ + qemuUefi + uefi-run + rust + pkgs.pev + add-sections ]; }; }; diff --git a/rust/Cargo.lock b/rust/Cargo.lock index c2058c5..617821d 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -24,6 +24,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "lanzaboote" version = "0.1.0" dependencies = [ + "log", "uefi", "uefi-services", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 8e82f61..6f9b585 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -5,5 +5,7 @@ edition = "2021" publish = false [dependencies] -uefi = { version = "0.18.0", features = ["alloc"] } +uefi = { version = "0.18.0", features = [ "alloc", "logger" ] } uefi-services = "0.15.0" +log = "0.4.17" + diff --git a/rust/src/main.rs b/rust/src/main.rs index 614326c..b20cfe2 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -2,11 +2,14 @@ #![no_std] #![feature(abi_efiapi)] +use log::info; use uefi::prelude::*; #[entry] fn main(_handle: Handle, mut system_table: SystemTable) -> Status { uefi_services::init(&mut system_table).unwrap(); + info!("Hello World!"); + Status::SUCCESS }