Skip to content

Commit b610829

Browse files
committed
Added sanity check for restore_firmware flag
1 parent 5ce5279 commit b610829

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/arduino/FirmwareUploader/modules/winc"
1414
"github.com/arduino/FirmwareUploader/utils"
1515
"github.com/arduino/FirmwareUploader/utils/context"
16+
"github.com/arduino/go-paths-helper"
1617
)
1718

1819
var ctx = &context.Context{}
@@ -24,7 +25,7 @@ func init() {
2425
flag.StringVar(&ctx.FirmwareFile, "firmware", "", "firmware file to flash")
2526
flag.BoolVar(&ctx.ReadAll, "read", false, "read all firmware and output to stdout")
2627
flag.StringVar(&ctx.FWUploaderBinary, "flasher", "", "firmware upload binary (precompiled for the right target)")
27-
flag.StringVar(&ctx.BinaryToRestore, "restore_binary", "", "firmware upload binary (precompiled for the right target)")
28+
flag.StringVar(&ctx.BinaryToRestore, "restore_binary", "", "binary to restore after the firmware upload (precompiled for the right target)")
2829
flag.StringVar(&ctx.ProgrammerPath, "programmer", "", "path of programmer in use (avrdude/bossac)")
2930
flag.StringVar(&ctx.Model, "model", "", "module model (winc, nina or sara)")
3031
flag.StringVar(&ctx.Compatible, "get_available_for", "", "Ask for available firmwares matching a given board")
@@ -43,6 +44,22 @@ func main() {
4344
log.Fatal("Please specify a serial port")
4445
}
4546

47+
if ctx.BinaryToRestore != "" {
48+
// sanity check for BinaryToRestore
49+
f := paths.New(ctx.BinaryToRestore)
50+
info, err := f.Stat()
51+
if err != nil {
52+
log.Fatalf("Error opening restore_binary: %s", err)
53+
}
54+
if info.IsDir() {
55+
log.Fatalf("Error opening restore_binary: is a directory...")
56+
}
57+
if info.Size() == 0 {
58+
log.Println("WARNING: restore_binary is empty! Will not restore binary after upload.")
59+
ctx.BinaryToRestore = ""
60+
}
61+
}
62+
4663
if ctx.Model == "nina" || strings.Contains(ctx.FirmwareFile, "NINA") || strings.Contains(ctx.FWUploaderBinary, "NINA") {
4764
nina.Run(ctx)
4865
} else if ctx.Model == "winc" || strings.Contains(ctx.FirmwareFile, "WINC") || strings.Contains(ctx.FWUploaderBinary, "WINC") {

0 commit comments

Comments
 (0)