Skip to content

Commit

Permalink
add embedded version of edk2 firmware files
Browse files Browse the repository at this point in the history
Whether edk2 is available on operating systems varies,
so the best choice is to embed the amd64/arm64 EFI files.

We need these files to boot amd64 images in EFI mode,
and also for arm64 images to boot at all.
  • Loading branch information
stapelberg committed Sep 27, 2024
1 parent b8ffcd4 commit 6bec690
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM debian:bookworm

RUN apt-get update && apt-get install -y qemu-efi-aarch64 ovmf
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ cover:

install:
go install github.com/gokrazy/tools/cmd/...

third_party/edk2-2022.11-6/QEMU_EFI.fd: Dockerfile
docker build --rm -t gokrazy-edk2 .
docker run --rm -v $$(pwd)/third_party/edk2-2022.11-6:/tmp/bins gokrazy-edk2 cp /usr/share/qemu-efi-aarch64/QEMU_EFI.fd /usr/share/OVMF/OVMF_CODE.fd /tmp/bins
32 changes: 18 additions & 14 deletions internal/gok/vmrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/gokrazy/internal/config"
"github.com/gokrazy/internal/instanceflag"
"github.com/gokrazy/tools/internal/packer"
edk "github.com/gokrazy/tools/third_party/edk2-2022.11-6"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -120,6 +121,20 @@ func (r *vmRunConfig) buildFullDiskImage(ctx context.Context, dest string) error
}

func (r *vmRunConfig) runQEMU(ctx context.Context, fullDiskImage string) error {
tmp, err := os.MkdirTemp("", "gokrazy-vm")
if err != nil {
return err
}
defer os.RemoveAll(tmp)
amd64EFI := filepath.Join(tmp, "amd64-OVMF_CODE.fd")
if err := os.WriteFile(amd64EFI, edk.Amd64EFI, 0644); err != nil {
return err
}
arm64EFI := filepath.Join(tmp, "arm64-QEMU_EFI.fd")
if err := os.WriteFile(arm64EFI, edk.Arm64EFI, 0644); err != nil {
return err
}

qemuBin := "qemu-system-x86_64"
switch r.arch {
case "amd64":
Expand All @@ -145,22 +160,11 @@ func (r *vmRunConfig) runQEMU(ctx context.Context, fullDiskImage string) error {
case "arm64":
qemu.Args = append(qemu.Args,
"-machine", "virt,highmem=off",
"-cpu", "cortex-a72")
// TODO: set -bios to an embedded copy of qemu-efi-aarch64/QEMU_EFI.fd
"-cpu", "cortex-a72",
"-bios", arm64EFI)

case "amd64":
for _, location := range []string{
// Debian, Fedora
"/usr/share/OVMF/OVMF_CODE.fd",
// Arch Linux
"/usr/share/edk2-ovmf/x64/OVMF_CODE.fd",
} {
if _, err := os.Stat(location); err == nil {
fmt.Printf("starting in UEFI mode, OVMF found at %s\n", location)
qemu.Args = append(qemu.Args, "-bios", location)
break
}
}
qemu.Args = append(qemu.Args, "-bios", amd64EFI)
}

if r.arch == runtime.GOARCH {
Expand Down
Binary file added third_party/edk2-2022.11-6/OVMF_CODE.fd
Binary file not shown.
Binary file added third_party/edk2-2022.11-6/QEMU_EFI.fd
Binary file not shown.
10 changes: 10 additions & 0 deletions third_party/edk2-2022.11-6/edk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package edk provides a bundled copy of the systemd-boot UEFI app.
package edk

import _ "embed"

//go:embed QEMU_EFI.fd
var Arm64EFI []byte

//go:embed OVMF_CODE.fd
var Amd64EFI []byte

0 comments on commit 6bec690

Please sign in to comment.