efivarfs, osimage: fix boot entry handling

efivarfs was updated to handle partition UUIDs in the mixed-endian
format [1], enabling it to produce correct boot entries. Its interface
was changed in the process, leading to further changes in osimage.

In addition, BootEntry.Marshal will replace any backslash with a
forward slash in the EFI executable path.

[1] https://en.wikipedia.org/wiki/Universally_unique_identifier#Encoding

Change-Id: Ib8300e01fd1664d0c08bb033b1dc36addb925b20
Reviewed-on: https://review.monogon.dev/c/monogon/+/456
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/node/build/mkimage/osimage/BUILD.bazel b/metropolis/node/build/mkimage/osimage/BUILD.bazel
index d9cacae..88f1dd8 100644
--- a/metropolis/node/build/mkimage/osimage/BUILD.bazel
+++ b/metropolis/node/build/mkimage/osimage/BUILD.bazel
@@ -11,5 +11,6 @@
         "@com_github_diskfs_go_diskfs//disk:go_default_library",
         "@com_github_diskfs_go_diskfs//filesystem:go_default_library",
         "@com_github_diskfs_go_diskfs//partition/gpt:go_default_library",
+        "@com_github_google_uuid//:go_default_library",
     ],
 )
diff --git a/metropolis/node/build/mkimage/osimage/osimage.go b/metropolis/node/build/mkimage/osimage/osimage.go
index 2f000a6..5aeeccd 100644
--- a/metropolis/node/build/mkimage/osimage/osimage.go
+++ b/metropolis/node/build/mkimage/osimage/osimage.go
@@ -27,6 +27,7 @@
 	"github.com/diskfs/go-diskfs/disk"
 	"github.com/diskfs/go-diskfs/filesystem"
 	"github.com/diskfs/go-diskfs/partition/gpt"
+	"github.com/google/uuid"
 
 	"source.monogon.dev/metropolis/pkg/efivarfs"
 )
@@ -372,10 +373,14 @@
 	t, err := diskImg.GetPartitionTable()
 	p := t.GetPartitions()
 	esp := (p[0]).(*gpt.Partition)
+	guid, err := uuid.Parse(esp.GUID)
+	if err != nil {
+		return nil, fmt.Errorf("couldn't parse the GPT GUID: %w", err)
+	}
 	be := efivarfs.BootEntry{
 		Description:     "Metropolis",
 		Path:            EFIPayloadPath,
-		PartitionGUID:   esp.GUID,
+		PartitionGUID:   guid,
 		PartitionNumber: 1,
 		PartitionStart:  esp.Start,
 		PartitionSize:   esp.End - esp.Start + 1,
diff --git a/metropolis/pkg/efivarfs/BUILD.bazel b/metropolis/pkg/efivarfs/BUILD.bazel
index 3b1e0b0..c41bb88 100644
--- a/metropolis/pkg/efivarfs/BUILD.bazel
+++ b/metropolis/pkg/efivarfs/BUILD.bazel
@@ -5,10 +5,12 @@
     srcs = [
         "boot.go",
         "efivarfs.go",
+        "format.go",
     ],
     importpath = "source.monogon.dev/metropolis/pkg/efivarfs",
     visibility = ["//visibility:public"],
     deps = [
+        "@com_github_google_uuid//:go_default_library",
         "@org_golang_x_text//encoding/unicode:go_default_library",
         "@org_golang_x_text//transform:go_default_library",
     ],
diff --git a/metropolis/pkg/efivarfs/boot.go b/metropolis/pkg/efivarfs/boot.go
index 096ec7c..4178c8d 100644
--- a/metropolis/pkg/efivarfs/boot.go
+++ b/metropolis/pkg/efivarfs/boot.go
@@ -26,7 +26,9 @@
 import (
 	"fmt"
 	"math"
+	"strings"
 
+	"github.com/google/uuid"
 	"golang.org/x/text/transform"
 )
 
@@ -51,7 +53,7 @@
 type BootEntry struct {
 	Description     string // eg. "Linux Boot Manager"
 	Path            string // eg. `\EFI\systemd\systemd-bootx64.efi`
-	PartitionGUID   string
+	PartitionGUID   uuid.UUID
 	PartitionNumber uint32 // Starts with 1
 	PartitionStart  uint64 // LBA
 	PartitionSize   uint64 // LBA
@@ -63,7 +65,7 @@
 // as attributes of an EFI variable) are always set to LOAD_OPTION_ACTIVE.
 func (t *BootEntry) Marshal() ([]byte, error) {
 	if t.Description == "" ||
-		t.PartitionGUID == "00000000-0000-0000-0000-000000000000" ||
+		t.PartitionGUID.String() == "00000000-0000-0000-0000-000000000000" ||
 		t.Path == "" ||
 		t.PartitionNumber == 0 ||
 		t.PartitionStart == 0 ||
@@ -83,7 +85,9 @@
 	dp = append32(dp, t.PartitionNumber)
 	dp = append64(dp, t.PartitionStart)
 	dp = append64(dp, t.PartitionSize)
-	dp = append(dp, t.PartitionGUID[0:16]...) // Partition Signature
+	// Append the partition GUID in the EFI format.
+	dp = append(dp, MarshalEFIGUID(t.PartitionGUID)...)
+
 	dp = append(dp,
 		0x02, // Partition Format ("GUID Partition Table")
 		0x02, // Signature Type ("GUID signature")
@@ -91,7 +95,8 @@
 
 	// EFI_LOAD_OPTION.FilePathList[1]
 	enc := Encoding.NewEncoder()
-	path, _, e := transform.Bytes(enc, []byte(t.Path))
+	bsp := strings.ReplaceAll(t.Path, "/", "\\")
+	path, _, e := transform.Bytes(enc, []byte(bsp))
 	if e != nil {
 		return nil, fmt.Errorf("while encoding Path: %v", e)
 	}
diff --git a/metropolis/pkg/efivarfs/format.go b/metropolis/pkg/efivarfs/format.go
new file mode 100644
index 0000000..a71dbf9
--- /dev/null
+++ b/metropolis/pkg/efivarfs/format.go
@@ -0,0 +1,33 @@
+// Copyright 2020 The Monogon Project Authors.
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package efivarfs
+
+import (
+	"github.com/google/uuid"
+)
+
+// MarshalEFIGUID returns src represented in the EFI GUID format, as defined in
+// UEFI specification, version 2.9, appendix A:
+// https://uefi.org/sites/default/files/resources/UEFI_Spec_2_9_2021_03_18.pdf
+func MarshalEFIGUID(src uuid.UUID) []byte {
+	efi := make([]byte, 16)
+	transform := []int{3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15}
+	for dest, from := range transform {
+		efi[dest] = src[from]
+	}
+	return efi
+}