From 22647958aee9828ecdd16f09e3a1bdf6b606578f Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Mon, 9 Nov 2020 15:38:10 +0100 Subject: installer: updater: introduce ARM64 MSI packages Windows 10 ARM64 refuses to install ARM MSI. Signed-off-by: Simon Rozman --- updater/mksyscall.go | 8 +++++++ updater/syscall_windows.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ updater/versions.go | 15 +++---------- updater/versions_arm.go | 34 ++++++++++++++++++++++++++++ updater/versions_default.go | 28 +++++++++++++++++++++++ updater/zsyscall_windows.go | 52 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 12 deletions(-) create mode 100644 updater/mksyscall.go create mode 100644 updater/syscall_windows.go create mode 100644 updater/versions_arm.go create mode 100644 updater/versions_default.go create mode 100644 updater/zsyscall_windows.go (limited to 'updater') diff --git a/updater/mksyscall.go b/updater/mksyscall.go new file mode 100644 index 00000000..8287e501 --- /dev/null +++ b/updater/mksyscall.go @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2020 WireGuard LLC. All Rights Reserved. + */ + +package updater + +//go:generate go run golang.org/x/sys/windows/mkwinsyscall -output zsyscall_windows.go syscall_windows.go diff --git a/updater/syscall_windows.go b/updater/syscall_windows.go new file mode 100644 index 00000000..0410e708 --- /dev/null +++ b/updater/syscall_windows.go @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2020 WireGuard LLC. All Rights Reserved. + */ + +package updater + +import "golang.org/x/sys/windows" + +const ( + // TODO: Push those constants upstream. golang.zx2c4.com/wireguard/tun/wintun/memmod is declaring them too. It will share the benefit. + IMAGE_FILE_MACHINE_UNKNOWN = 0 + IMAGE_FILE_MACHINE_TARGET_HOST = 0x0001 // Useful for indicating we want to interact with the host and not a WoW guest. + IMAGE_FILE_MACHINE_I386 = 0x014c // Intel 386. + IMAGE_FILE_MACHINE_R3000 = 0x0162 // MIPS little-endian, 0x160 big-endian + IMAGE_FILE_MACHINE_R4000 = 0x0166 // MIPS little-endian + IMAGE_FILE_MACHINE_R10000 = 0x0168 // MIPS little-endian + IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x0169 // MIPS little-endian WCE v2 + IMAGE_FILE_MACHINE_ALPHA = 0x0184 // Alpha_AXP + IMAGE_FILE_MACHINE_SH3 = 0x01a2 // SH3 little-endian + IMAGE_FILE_MACHINE_SH3DSP = 0x01a3 + IMAGE_FILE_MACHINE_SH3E = 0x01a4 // SH3E little-endian + IMAGE_FILE_MACHINE_SH4 = 0x01a6 // SH4 little-endian + IMAGE_FILE_MACHINE_SH5 = 0x01a8 // SH5 + IMAGE_FILE_MACHINE_ARM = 0x01c0 // ARM Little-Endian + IMAGE_FILE_MACHINE_THUMB = 0x01c2 // ARM Thumb/Thumb-2 Little-Endian + IMAGE_FILE_MACHINE_ARMNT = 0x01c4 // ARM Thumb-2 Little-Endian + IMAGE_FILE_MACHINE_AM33 = 0x01d3 + IMAGE_FILE_MACHINE_POWERPC = 0x01F0 // IBM PowerPC Little-Endian + IMAGE_FILE_MACHINE_POWERPCFP = 0x01f1 + IMAGE_FILE_MACHINE_IA64 = 0x0200 // Intel 64 + IMAGE_FILE_MACHINE_MIPS16 = 0x0266 // MIPS + IMAGE_FILE_MACHINE_ALPHA64 = 0x0284 // ALPHA64 + IMAGE_FILE_MACHINE_MIPSFPU = 0x0366 // MIPS + IMAGE_FILE_MACHINE_MIPSFPU16 = 0x0466 // MIPS + IMAGE_FILE_MACHINE_AXP64 = IMAGE_FILE_MACHINE_ALPHA64 + IMAGE_FILE_MACHINE_TRICORE = 0x0520 // Infineon + IMAGE_FILE_MACHINE_CEF = 0x0CEF + IMAGE_FILE_MACHINE_EBC = 0x0EBC // EFI Byte Code + IMAGE_FILE_MACHINE_AMD64 = 0x8664 // AMD64 (K8) + IMAGE_FILE_MACHINE_M32R = 0x9041 // M32R little-endian + IMAGE_FILE_MACHINE_ARM64 = 0xAA64 // ARM64 Little-Endian + IMAGE_FILE_MACHINE_CEE = 0xC0EE +) + +//sys isWow64Process2Internal(process windows.Handle, processMachine *uint16, nativeMachine *uint16) (err error) = kernel32.IsWow64Process2 + +func isWow64Process2(process windows.Handle) (processMachine, nativeMachine uint16, err error) { + err = procIsWow64Process2.Find() + if err != nil { + return + } + err = isWow64Process2Internal(process, &processMachine, &nativeMachine) + return +} diff --git a/updater/versions.go b/updater/versions.go index c0009ad8..08830d32 100644 --- a/updater/versions.go +++ b/updater/versions.go @@ -8,7 +8,6 @@ package updater import ( "errors" "fmt" - "runtime" "strconv" "strings" @@ -55,17 +54,9 @@ func versionNewerThanUs(candidate string) (bool, error) { } func findCandidate(candidates fileList) (*UpdateFound, error) { - var arch string - if runtime.GOARCH == "amd64" { - arch = "amd64" - } else if runtime.GOARCH == "386" { - arch = "x86" - } else if runtime.GOARCH == "arm" { - arch = "arm" - } else if runtime.GOARCH == "arm64" { - arch = "arm64" - } else { - return nil, errors.New("Invalid GOARCH") + arch, err := findArch() + if err != nil { + return nil, err } prefix := fmt.Sprintf(msiArchPrefix, arch) suffix := msiSuffix diff --git a/updater/versions_arm.go b/updater/versions_arm.go new file mode 100644 index 00000000..2ffb4b74 --- /dev/null +++ b/updater/versions_arm.go @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2020 WireGuard LLC. All Rights Reserved. + */ + +package updater + +import ( + "errors" + + "golang.org/x/sys/windows" +) + +func findArch() (arch string, err error) { + process := windows.CurrentProcess() + _, nativeMachine, err2 := isWow64Process2(process) + if err2 != nil { + var isWow64 bool + if windows.IsWow64Process(process, &isWow64) != nil || !isWow64 { + nativeMachine = IMAGE_FILE_MACHINE_ARMNT + } else { + nativeMachine = IMAGE_FILE_MACHINE_ARM64 + } + } + switch nativeMachine { + case IMAGE_FILE_MACHINE_ARM64: + arch = "arm64" + case IMAGE_FILE_MACHINE_ARMNT: + arch = "arm" + default: + err = errors.New("Invalid GOARCH") + } + return +} diff --git a/updater/versions_default.go b/updater/versions_default.go new file mode 100644 index 00000000..268e97ce --- /dev/null +++ b/updater/versions_default.go @@ -0,0 +1,28 @@ +// +build !arm + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019-2020 WireGuard LLC. All Rights Reserved. + */ + +package updater + +import ( + "errors" + "runtime" +) + +func findArch() (arch string, err error) { + if runtime.GOARCH == "amd64" { + arch = "amd64" + } else if runtime.GOARCH == "386" { + arch = "x86" + } else if runtime.GOARCH == "arm" { + arch = "arm" + } else if runtime.GOARCH == "arm64" { + arch = "arm64" + } else { + err = errors.New("Invalid GOARCH") + } + return +} diff --git a/updater/zsyscall_windows.go b/updater/zsyscall_windows.go new file mode 100644 index 00000000..a2d32cc9 --- /dev/null +++ b/updater/zsyscall_windows.go @@ -0,0 +1,52 @@ +// Code generated by 'go generate'; DO NOT EDIT. + +package updater + +import ( + "syscall" + "unsafe" + + "golang.org/x/sys/windows" +) + +var _ unsafe.Pointer + +// Do the interface allocations only once for common +// Errno values. +const ( + errnoERROR_IO_PENDING = 997 +) + +var ( + errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING) + errERROR_EINVAL error = syscall.EINVAL +) + +// errnoErr returns common boxed Errno values, to prevent +// allocations at runtime. +func errnoErr(e syscall.Errno) error { + switch e { + case 0: + return errERROR_EINVAL + case errnoERROR_IO_PENDING: + return errERROR_IO_PENDING + } + // TODO: add more here, after collecting data on the common + // error values see on Windows. (perhaps when running + // all.bat?) + return e +} + +var ( + modkernel32 = windows.NewLazySystemDLL("kernel32.dll") + + procIsWow64Process2 = modkernel32.NewProc("IsWow64Process2") +) + +func isWow64Process2Internal(process windows.Handle, processMachine *uint16, nativeMachine *uint16) (err error) { + r1, _, e1 := syscall.Syscall(procIsWow64Process2.Addr(), 3, uintptr(process), uintptr(unsafe.Pointer(processMachine)), uintptr(unsafe.Pointer(nativeMachine))) + if r1 == 0 { + err = errnoErr(e1) + } + return +} -- cgit v1.2.3-59-g8ed1b