Skip to content

Commit 447537a

Browse files
deadprogramaykevl
authored andcommitted
flash: use win32 wmi to try to find UF2 and COM ports
Signed-off-by: Ron Evans <[email protected]>
1 parent 74e32ac commit 447537a

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

main.go

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"errors"
56
"flag"
67
"fmt"
@@ -209,7 +210,18 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
209210
flashCmd = strings.Replace(flashCmd, "{port}", port, -1)
210211

211212
// Execute the command.
212-
cmd := exec.Command("/bin/sh", "-c", flashCmd)
213+
var cmd *exec.Cmd
214+
switch runtime.GOOS {
215+
case "windows":
216+
command := strings.Split(flashCmd, " ")
217+
if len(command) < 2 {
218+
return errors.New("invalid flash command")
219+
}
220+
cmd = exec.Command(command[0], command[1:]...)
221+
default:
222+
cmd = exec.Command("/bin/sh", "-c", flashCmd)
223+
}
224+
213225
cmd.Stdout = os.Stdout
214226
cmd.Stderr = os.Stderr
215227
cmd.Dir = goenv.Get("TINYGOROOT")
@@ -438,9 +450,18 @@ func touchSerialPortAt1200bps(port string) error {
438450

439451
func flashUF2UsingMSD(volume, tmppath string) error {
440452
// find standard UF2 info path
441-
infoPath := "/media/*/" + volume + "/INFO_UF2.TXT"
442-
if runtime.GOOS == "darwin" {
453+
var infoPath string
454+
switch runtime.GOOS {
455+
case "linux":
456+
infoPath = "/media/*/" + volume + "/INFO_UF2.TXT"
457+
case "darwin":
443458
infoPath = "/Volumes/" + volume + "/INFO_UF2.TXT"
459+
case "windows":
460+
path, err := windowsFindUSBDrive()
461+
if err != nil {
462+
return err
463+
}
464+
infoPath = path + "/INFO_UF2.TXT"
444465
}
445466

446467
d, err := filepath.Glob(infoPath)
@@ -456,9 +477,18 @@ func flashUF2UsingMSD(volume, tmppath string) error {
456477

457478
func flashHexUsingMSD(volume, tmppath string) error {
458479
// find expected volume path
459-
destPath := "/media/*/" + volume
460-
if runtime.GOOS == "darwin" {
480+
var destPath string
481+
switch runtime.GOOS {
482+
case "linux":
483+
destPath = "/media/*/" + volume
484+
case "darwin":
461485
destPath = "/Volumes/" + volume
486+
case "windows":
487+
path, err := windowsFindUSBDrive()
488+
if err != nil {
489+
return err
490+
}
491+
destPath = path + "/"
462492
}
463493

464494
d, err := filepath.Glob(destPath)
@@ -472,6 +502,29 @@ func flashHexUsingMSD(volume, tmppath string) error {
472502
return moveFile(tmppath, d[0]+"/flash.hex")
473503
}
474504

505+
func windowsFindUSBDrive() (string, error) {
506+
cmd := exec.Command("wmic", "PATH", "Win32_LogicalDisk",
507+
"get", "DeviceID,", "VolumeName,",
508+
"FileSystem,", "DriveType")
509+
510+
var out bytes.Buffer
511+
cmd.Stdout = &out
512+
err := cmd.Run()
513+
if err != nil {
514+
return "", err
515+
}
516+
517+
for _, line := range strings.Split(out.String(), "\n") {
518+
words := strings.Fields(line)
519+
if len(words) >= 3 {
520+
if words[1] == "2" && words[2] == "FAT" {
521+
return words[0], nil
522+
}
523+
}
524+
}
525+
return "", errors.New("unable to locate a USB device to be flashed")
526+
}
527+
475528
// parseSize converts a human-readable size (with k/m/g suffix) into a plain
476529
// number.
477530
func parseSize(s string) (int64, error) {
@@ -505,6 +558,30 @@ func getDefaultPort() (port string, err error) {
505558
portPath = "/dev/cu.usb*"
506559
case "linux":
507560
portPath = "/dev/ttyACM*"
561+
case "windows":
562+
cmd := exec.Command("wmic", "PATH", "Win32_SerialPort",
563+
"get", "DeviceID")
564+
565+
var out bytes.Buffer
566+
cmd.Stdout = &out
567+
err := cmd.Run()
568+
if err != nil {
569+
return "", err
570+
}
571+
572+
if out.String() == "No Instance(s) Available." {
573+
return "", errors.New("unable to locate a USB device to be flashed")
574+
}
575+
576+
for _, line := range strings.Split(out.String(), "\n") {
577+
words := strings.Fields(line)
578+
if len(words) == 1 {
579+
if strings.Contains(words[0], "COM") {
580+
return words[0], nil
581+
}
582+
}
583+
}
584+
return "", errors.New("unable to locate a USB device to be flashed")
508585
default:
509586
return "", errors.New("unable to search for a default USB device to be flashed on this OS")
510587
}

0 commit comments

Comments
 (0)