m/p/gpt: switch to msguid
Replaces the old UUID mangling code with the newly-introduced msguid.
Change-Id: I667e41c28959b4b95265c1ffdcf7f5bfcad4083d
Reviewed-on: https://review.monogon.dev/c/monogon/+/1850
Tested-by: Jenkins CI
Reviewed-by: Serge Bazanski <serge@monogon.tech>
diff --git a/metropolis/pkg/gpt/BUILD.bazel b/metropolis/pkg/gpt/BUILD.bazel
index 25b6b7b..bc844b7 100644
--- a/metropolis/pkg/gpt/BUILD.bazel
+++ b/metropolis/pkg/gpt/BUILD.bazel
@@ -9,7 +9,10 @@
],
importpath = "source.monogon.dev/metropolis/pkg/gpt",
visibility = ["//visibility:public"],
- deps = ["@com_github_google_uuid//:uuid"],
+ deps = [
+ "//metropolis/pkg/msguid",
+ "@com_github_google_uuid//:uuid",
+ ],
)
go_test(
diff --git a/metropolis/pkg/gpt/gpt.go b/metropolis/pkg/gpt/gpt.go
index 5660237..81c50f7 100644
--- a/metropolis/pkg/gpt/gpt.go
+++ b/metropolis/pkg/gpt/gpt.go
@@ -16,6 +16,8 @@
"unicode/utf16"
"github.com/google/uuid"
+
+ "source.monogon.dev/metropolis/pkg/msguid"
)
var gptSignature = [8]byte{'E', 'F', 'I', ' ', 'P', 'A', 'R', 'T'}
@@ -179,7 +181,7 @@
// WithAlignment allows aligning the partition start block to a non-default
// value. By default, these are aligned to 1MiB.
-// Only use this flag if you are certain you need it, it can cause quite severe
+// Only use this flag if you are certain you need it, it can cause quite severe
// performance degradation under certain conditions.
func WithAlignment(alignmenet int64) AddOption {
return func(options *addOptions) {
@@ -439,8 +441,8 @@
p.ID = uuid.New()
}
rawP := partition{
- Type: mangleUUID(p.Type),
- ID: mangleUUID(p.ID),
+ Type: msguid.From(p.Type),
+ ID: msguid.From(p.ID),
FirstBlock: p.FirstBlock,
LastBlock: p.LastBlock,
Attributes: uint64(p.Attributes),
@@ -455,7 +457,7 @@
Signature: gptSignature,
Revision: gptRevision,
HeaderSize: uint32(binary.Size(&header{})),
- ID: mangleUUID(gpt.ID),
+ ID: msguid.From(gpt.ID),
PartitionEntryCount: uint32(slotCount),
PartitionEntrySize: uint32(partSize),
@@ -669,7 +671,7 @@
return nil, errors.New("GPT partition entry table checksum mismatch")
}
var g Table
- g.ID = unmangleUUID(hdr.ID)
+ g.ID = msguid.To(hdr.ID)
g.BlockSize = blockSize
g.BlockCount = blockCount
for i := uint32(0); i < hdr.PartitionEntryCount; i++ {
@@ -685,8 +687,8 @@
continue
}
g.Partitions = append(g.Partitions, &Partition{
- ID: unmangleUUID(part.ID),
- Type: unmangleUUID(part.Type),
+ ID: msguid.To(part.ID),
+ Type: msguid.To(part.Type),
Name: strings.TrimRight(string(utf16.Decode(part.Name[:])), "\x00"),
FirstBlock: part.FirstBlock,
LastBlock: part.LastBlock,
@@ -705,23 +707,3 @@
g.Partitions = g.Partitions[:maxValidPartition+1]
return &g, nil
}
-
-var mixedEndianTranspose = []int{3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15}
-
-// mangleUUID encodes a normal UUID into a "mixed-endian" UUID. This just means
-// shuffling the bytes around in a specific way. Thank Microsoft for this
-// idiosyncrasy.
-func mangleUUID(u uuid.UUID) (o [16]byte) {
- for dest, from := range mixedEndianTranspose {
- o[dest] = u[from]
- }
- return
-}
-
-// unmangleUUID does the reverse transformation of mangleUUID
-func unmangleUUID(i [16]byte) (o uuid.UUID) {
- for from, dest := range mixedEndianTranspose {
- o[dest] = i[from]
- }
- return
-}
diff --git a/metropolis/pkg/gpt/gpt_test.go b/metropolis/pkg/gpt/gpt_test.go
index df2970b..924d4f7 100644
--- a/metropolis/pkg/gpt/gpt_test.go
+++ b/metropolis/pkg/gpt/gpt_test.go
@@ -10,19 +10,6 @@
"github.com/google/uuid"
)
-func TestUUIDTranspose(t *testing.T) {
- testUUID := uuid.MustParse("00112233-4455-6677-c899-aabbccddeeff")
- mixedEndianUUID := mangleUUID(testUUID)
- expectedMixedEndianUUID := [16]byte{0x33, 0x22, 0x11, 0x00, 0x55, 0x44, 0x77, 0x66, 0xc8, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
- if mixedEndianUUID != expectedMixedEndianUUID {
- t.Errorf("mangleUUID(%s) = %x, expected %x", testUUID, mixedEndianUUID, expectedMixedEndianUUID)
- }
- roundTrippedUUID := unmangleUUID(mixedEndianUUID)
- if testUUID != roundTrippedUUID {
- t.Errorf("unmangleUUID(mangleUUID(%s)) = %s, expected input", testUUID, roundTrippedUUID)
- }
-}
-
func TestFreeSpaces(t *testing.T) {
cases := []struct {
name string