flash: use win32 wmi to try to find UF2 and COM ports

Signed-off-by: Ron Evans <ron@hybridgroup.com>
Этот коммит содержится в:
Ron Evans 2019-12-18 00:25:33 +01:00 коммит произвёл Ayke
родитель 74e32acf33
коммит 447537aebe

87
main.go
Просмотреть файл

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
@ -209,7 +210,18 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
flashCmd = strings.Replace(flashCmd, "{port}", port, -1) flashCmd = strings.Replace(flashCmd, "{port}", port, -1)
// Execute the command. // Execute the command.
cmd := exec.Command("/bin/sh", "-c", flashCmd) var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
command := strings.Split(flashCmd, " ")
if len(command) < 2 {
return errors.New("invalid flash command")
}
cmd = exec.Command(command[0], command[1:]...)
default:
cmd = exec.Command("/bin/sh", "-c", flashCmd)
}
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Dir = goenv.Get("TINYGOROOT") cmd.Dir = goenv.Get("TINYGOROOT")
@ -438,9 +450,18 @@ func touchSerialPortAt1200bps(port string) error {
func flashUF2UsingMSD(volume, tmppath string) error { func flashUF2UsingMSD(volume, tmppath string) error {
// find standard UF2 info path // find standard UF2 info path
infoPath := "/media/*/" + volume + "/INFO_UF2.TXT" var infoPath string
if runtime.GOOS == "darwin" { switch runtime.GOOS {
case "linux":
infoPath = "/media/*/" + volume + "/INFO_UF2.TXT"
case "darwin":
infoPath = "/Volumes/" + volume + "/INFO_UF2.TXT" infoPath = "/Volumes/" + volume + "/INFO_UF2.TXT"
case "windows":
path, err := windowsFindUSBDrive()
if err != nil {
return err
}
infoPath = path + "/INFO_UF2.TXT"
} }
d, err := filepath.Glob(infoPath) d, err := filepath.Glob(infoPath)
@ -456,9 +477,18 @@ func flashUF2UsingMSD(volume, tmppath string) error {
func flashHexUsingMSD(volume, tmppath string) error { func flashHexUsingMSD(volume, tmppath string) error {
// find expected volume path // find expected volume path
destPath := "/media/*/" + volume var destPath string
if runtime.GOOS == "darwin" { switch runtime.GOOS {
case "linux":
destPath = "/media/*/" + volume
case "darwin":
destPath = "/Volumes/" + volume destPath = "/Volumes/" + volume
case "windows":
path, err := windowsFindUSBDrive()
if err != nil {
return err
}
destPath = path + "/"
} }
d, err := filepath.Glob(destPath) d, err := filepath.Glob(destPath)
@ -472,6 +502,29 @@ func flashHexUsingMSD(volume, tmppath string) error {
return moveFile(tmppath, d[0]+"/flash.hex") return moveFile(tmppath, d[0]+"/flash.hex")
} }
func windowsFindUSBDrive() (string, error) {
cmd := exec.Command("wmic", "PATH", "Win32_LogicalDisk",
"get", "DeviceID,", "VolumeName,",
"FileSystem,", "DriveType")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
for _, line := range strings.Split(out.String(), "\n") {
words := strings.Fields(line)
if len(words) >= 3 {
if words[1] == "2" && words[2] == "FAT" {
return words[0], nil
}
}
}
return "", errors.New("unable to locate a USB device to be flashed")
}
// parseSize converts a human-readable size (with k/m/g suffix) into a plain // parseSize converts a human-readable size (with k/m/g suffix) into a plain
// number. // number.
func parseSize(s string) (int64, error) { func parseSize(s string) (int64, error) {
@ -505,6 +558,30 @@ func getDefaultPort() (port string, err error) {
portPath = "/dev/cu.usb*" portPath = "/dev/cu.usb*"
case "linux": case "linux":
portPath = "/dev/ttyACM*" portPath = "/dev/ttyACM*"
case "windows":
cmd := exec.Command("wmic", "PATH", "Win32_SerialPort",
"get", "DeviceID")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return "", err
}
if out.String() == "No Instance(s) Available." {
return "", errors.New("unable to locate a USB device to be flashed")
}
for _, line := range strings.Split(out.String(), "\n") {
words := strings.Fields(line)
if len(words) == 1 {
if strings.Contains(words[0], "COM") {
return words[0], nil
}
}
}
return "", errors.New("unable to locate a USB device to be flashed")
default: default:
return "", errors.New("unable to search for a default USB device to be flashed on this OS") return "", errors.New("unable to search for a default USB device to be flashed on this OS")
} }