Skip to content

Commit

Permalink
Support i386/x86 platforms (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
george-hopkins authored Aug 18, 2024
1 parent d0d0a0a commit de5f76c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
18 changes: 12 additions & 6 deletions internal/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ func (pack *Pack) logic(programName string) error {
}

// kernelGoarch returns the GOARCH value that corresponds to the provided
// vmlinuz header. It returns one of "arm64", "arm", "amd64", or the empty
// vmlinuz header. It returns one of "arm", "arm64", "386", "amd64" or the empty
// string if not detected.
func kernelGoarch(hdr []byte) string {
// Some constants from the file(1) command's magic.
Expand All @@ -1873,17 +1873,23 @@ func kernelGoarch(hdr []byte) string {
arm64Magic = 0x644d5241
arm64MagicOffset = 0x38
// x86: https://github.com/file/file/blob/65be1904/magic/Magdir/linux#L137-L152
x86Magic = 0xaa55
x86MagicOffset = 0x1fe
x86Magic = 0xaa55
x86MagicOffset = 0x1fe
x86XloadflagsOffset = 0x236
)
if len(hdr) >= arm64MagicOffset+4 && binary.LittleEndian.Uint32(hdr[arm64MagicOffset:]) == arm64Magic {
return "arm64"
}
if len(hdr) >= arm64MagicOffset+4 && binary.LittleEndian.Uint32(hdr[arm32MagicOffset:]) == arm32Magic {
if len(hdr) >= arm32MagicOffset+4 && binary.LittleEndian.Uint32(hdr[arm32MagicOffset:]) == arm32Magic {
return "arm"
}
if len(hdr) >= arm64MagicOffset+2 && binary.LittleEndian.Uint16(hdr[x86MagicOffset:]) == x86Magic {
return "amd64" // we'll assume 386 is unsupported
if len(hdr) >= x86XloadflagsOffset+2 && binary.LittleEndian.Uint16(hdr[x86MagicOffset:]) == x86Magic {
// XLF0 in arch/x86/boot/header.S
if hdr[x86XloadflagsOffset]&1 != 0 {
return "amd64"
} else {
return "386"
}
}
return ""
}
Expand Down
2 changes: 1 addition & 1 deletion internal/packer/packer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func TestKernelGoarch(t *testing.T) {
for _, arch := range []string{"arm", "arm64", "amd64"} {
for _, arch := range []string{"386", "arm", "arm64", "amd64"} {
t.Run(arch, func(t *testing.T) {
k, err := os.ReadFile("testdata/kernel." + arch)
if err != nil {
Expand Down
Binary file added internal/packer/testdata/kernel.386
Binary file not shown.
11 changes: 9 additions & 2 deletions packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func (p *Pack) writeGPT(w io.Writer, devsize uint64, primary bool) error {
partitionTypeLinuxFilesystemData = "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
partitionTypeLinuxRootPartitionAMD64 = "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709"
partitionTypeLinuxRootPartitionARM64 = "B921B045-1DF0-41C3-AF44-4C6F280D3FAE"
partitionTypeLinuxRootPartitionX86 = "44479540-f297-41b2-9af7-d131d5f0458a"
)

type partitionEntry struct {
Expand All @@ -284,10 +285,16 @@ func (p *Pack) writeGPT(w io.Writer, devsize uint64, primary bool) error {
partition3First := partition2Last + 1
partition3Last := partition3First + uint64(permSize(p.FirstPartitionOffsetSectors, devsize)) - 1

rootType := mustParseGUID(partitionTypeLinuxRootPartitionARM64)
if os.Getenv("GOARCH") == "amd64" {
var rootType [16]byte
switch os.Getenv("GOARCH") {
case "386":
rootType = mustParseGUID(partitionTypeLinuxRootPartitionX86)
case "amd64":
rootType = mustParseGUID(partitionTypeLinuxRootPartitionAMD64)
default:
rootType = mustParseGUID(partitionTypeLinuxRootPartitionARM64)
}

partitionEntries := []partitionEntry{
{
TypeGUID: mustParseGUID(partitionTypeEFISystemPartition),
Expand Down

0 comments on commit de5f76c

Please sign in to comment.