1
1
package main
2
2
3
3
import (
4
+ "bytes"
4
5
"errors"
5
6
"flag"
6
7
"fmt"
@@ -209,7 +210,18 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
209
210
flashCmd = strings .Replace (flashCmd , "{port}" , port , - 1 )
210
211
211
212
// 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
+
213
225
cmd .Stdout = os .Stdout
214
226
cmd .Stderr = os .Stderr
215
227
cmd .Dir = goenv .Get ("TINYGOROOT" )
@@ -438,9 +450,18 @@ func touchSerialPortAt1200bps(port string) error {
438
450
439
451
func flashUF2UsingMSD (volume , tmppath string ) error {
440
452
// 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" :
443
458
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"
444
465
}
445
466
446
467
d , err := filepath .Glob (infoPath )
@@ -456,9 +477,18 @@ func flashUF2UsingMSD(volume, tmppath string) error {
456
477
457
478
func flashHexUsingMSD (volume , tmppath string ) error {
458
479
// 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" :
461
485
destPath = "/Volumes/" + volume
486
+ case "windows" :
487
+ path , err := windowsFindUSBDrive ()
488
+ if err != nil {
489
+ return err
490
+ }
491
+ destPath = path + "/"
462
492
}
463
493
464
494
d , err := filepath .Glob (destPath )
@@ -472,6 +502,29 @@ func flashHexUsingMSD(volume, tmppath string) error {
472
502
return moveFile (tmppath , d [0 ]+ "/flash.hex" )
473
503
}
474
504
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
+
475
528
// parseSize converts a human-readable size (with k/m/g suffix) into a plain
476
529
// number.
477
530
func parseSize (s string ) (int64 , error ) {
@@ -505,6 +558,30 @@ func getDefaultPort() (port string, err error) {
505
558
portPath = "/dev/cu.usb*"
506
559
case "linux" :
507
560
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" )
508
585
default :
509
586
return "" , errors .New ("unable to search for a default USB device to be flashed on this OS" )
510
587
}
0 commit comments