Skip to content

Commit 6bec690

Browse files
committed
add embedded version of edk2 firmware files
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.
1 parent b8ffcd4 commit 6bec690

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM debian:bookworm
2+
3+
RUN apt-get update && apt-get install -y qemu-efi-aarch64 ovmf

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ cover:
1414

1515
install:
1616
go install github.com/gokrazy/tools/cmd/...
17+
18+
third_party/edk2-2022.11-6/QEMU_EFI.fd: Dockerfile
19+
docker build --rm -t gokrazy-edk2 .
20+
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

internal/gok/vmrun.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/gokrazy/internal/config"
1515
"github.com/gokrazy/internal/instanceflag"
1616
"github.com/gokrazy/tools/internal/packer"
17+
edk "github.com/gokrazy/tools/third_party/edk2-2022.11-6"
1718
"github.com/spf13/cobra"
1819
)
1920

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

122123
func (r *vmRunConfig) runQEMU(ctx context.Context, fullDiskImage string) error {
124+
tmp, err := os.MkdirTemp("", "gokrazy-vm")
125+
if err != nil {
126+
return err
127+
}
128+
defer os.RemoveAll(tmp)
129+
amd64EFI := filepath.Join(tmp, "amd64-OVMF_CODE.fd")
130+
if err := os.WriteFile(amd64EFI, edk.Amd64EFI, 0644); err != nil {
131+
return err
132+
}
133+
arm64EFI := filepath.Join(tmp, "arm64-QEMU_EFI.fd")
134+
if err := os.WriteFile(arm64EFI, edk.Arm64EFI, 0644); err != nil {
135+
return err
136+
}
137+
123138
qemuBin := "qemu-system-x86_64"
124139
switch r.arch {
125140
case "amd64":
@@ -145,22 +160,11 @@ func (r *vmRunConfig) runQEMU(ctx context.Context, fullDiskImage string) error {
145160
case "arm64":
146161
qemu.Args = append(qemu.Args,
147162
"-machine", "virt,highmem=off",
148-
"-cpu", "cortex-a72")
149-
// TODO: set -bios to an embedded copy of qemu-efi-aarch64/QEMU_EFI.fd
163+
"-cpu", "cortex-a72",
164+
"-bios", arm64EFI)
150165

151166
case "amd64":
152-
for _, location := range []string{
153-
// Debian, Fedora
154-
"/usr/share/OVMF/OVMF_CODE.fd",
155-
// Arch Linux
156-
"/usr/share/edk2-ovmf/x64/OVMF_CODE.fd",
157-
} {
158-
if _, err := os.Stat(location); err == nil {
159-
fmt.Printf("starting in UEFI mode, OVMF found at %s\n", location)
160-
qemu.Args = append(qemu.Args, "-bios", location)
161-
break
162-
}
163-
}
167+
qemu.Args = append(qemu.Args, "-bios", amd64EFI)
164168
}
165169

166170
if r.arch == runtime.GOARCH {
1.88 MB
Binary file not shown.
2 MB
Binary file not shown.

third_party/edk2-2022.11-6/edk.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Package edk provides a bundled copy of the systemd-boot UEFI app.
2+
package edk
3+
4+
import _ "embed"
5+
6+
//go:embed QEMU_EFI.fd
7+
var Arm64EFI []byte
8+
9+
//go:embed OVMF_CODE.fd
10+
var Amd64EFI []byte

0 commit comments

Comments
 (0)