/
flash.go
212 lines (191 loc) · 7.05 KB
/
flash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
arduino-fwuploader
Copyright (c) 2021 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package firmware
import (
"bytes"
"fmt"
"os"
"strings"
"time"
"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-fwuploader/cli/arguments"
"github.com/arduino/arduino-fwuploader/cli/common"
"github.com/arduino/arduino-fwuploader/flasher"
"github.com/arduino/arduino-fwuploader/indexes/download"
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
commonFlags arguments.Flags // contains fqbn and address
module string
retries uint8
fwFile string
)
// NewFlashCommand creates a new `flash` command
func NewFlashCommand() *cobra.Command {
command := &cobra.Command{
Use: "flash",
Short: "Flashes firmwares to board.",
Long: "Flashes specified module firmware to board at specified address. Module name and version can be omitted to install latest version.",
Example: "" +
" " + os.Args[0] + " firmware flash --fqbn arduino:samd:mkr1000 --address COM10 --module WINC1500@19.5.2\n" +
" " + os.Args[0] + " firmware flash -b arduino:samd:mkr1000 -a COM10 -m WINC15000\n" +
" " + os.Args[0] + " firmware flash -b arduino:samd:mkr1000 -a COM10\n" +
" " + os.Args[0] + " firmware flash -b arduino:samd:mkr1000 -a COM10 -i firmware.bin\n",
Args: cobra.NoArgs,
Run: runFlash,
}
commonFlags.AddToCommand(command)
command.Flags().StringVarP(&module, "module", "m", "", "Firmware module ID, e.g.: WINC1500, NINA")
command.Flags().Uint8Var(&retries, "retries", 9, "Number of retries in case of upload failure (default 9)")
command.Flags().StringVarP(&fwFile, "input-file", "i", "", "Path of the firmware to upload")
return command
}
func runFlash(cmd *cobra.Command, args []string) {
packageIndex, firmwareIndex := common.InitIndexes()
common.CheckFlags(commonFlags.Fqbn, commonFlags.Address)
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
uploadToolDir := common.GetUploadToolDir(packageIndex, board)
// Get module name if not specified
moduleName := ""
moduleVersion := ""
if module == "" {
moduleName = board.Module
} else {
moduleSplit := strings.Split(module, "@")
if len(moduleSplit) == 2 {
moduleName = moduleSplit[0]
moduleVersion = moduleSplit[1]
} else {
moduleName = module
}
}
// Normalize module name
moduleName = strings.ToUpper(moduleName)
var firmwareFilePath *paths.Path
var err error
// If a local firmware file has been specified
if fwFile != "" {
firmwareFilePath = paths.New(fwFile)
if !firmwareFilePath.Exist() {
feedback.Errorf("firmware file not found in %s", firmwareFilePath)
os.Exit(errorcodes.ErrGeneric)
}
} else {
// Download the firmware
var firmware *firmwareindex.IndexFirmware
if moduleVersion == "" {
firmware = board.LatestFirmware
} else {
firmware = board.GetFirmware(moduleVersion)
}
logrus.Debugf("module name: %s, firmware version: %s", firmware.Module, firmware.Version.String())
if firmware == nil {
feedback.Errorf("Error getting firmware for board: %s", commonFlags.Fqbn)
os.Exit(errorcodes.ErrGeneric)
}
firmwareFilePath, err = download.DownloadFirmware(firmware)
if err != nil {
feedback.Errorf("Error downloading firmware from %s: %s", firmware.URL, err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Debugf("firmware file downloaded in %s", firmwareFilePath.String())
}
loaderSketchPath, err := download.DownloadSketch(board.LoaderSketch)
if err != nil {
feedback.Errorf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Debugf("loader sketch downloaded in %s", loaderSketchPath.String())
loaderSketch := strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "")
for retry := 1; retry <= int(retries); retry++ {
err = updateFirmware(board, loaderSketch, moduleName, uploadToolDir, firmwareFilePath)
if err == nil {
logrus.Info("Operation completed: success! :-)")
break
}
feedback.Error(err)
if retry == int(retries) {
logrus.Fatal("Operation failed. :-(")
}
logrus.Info("Waiting 1 second before retrying...")
time.Sleep(time.Second)
logrus.Infof("Retrying upload (%d of %d)", retry, retries)
}
}
func updateFirmware(board *firmwareindex.IndexBoard, loaderSketch, moduleName string, uploadToolDir, firmwareFile *paths.Path) error {
programmerOut, programmerErr, err := common.FlashSketch(board, loaderSketch, uploadToolDir, commonFlags.Address)
if err != nil {
return err
}
// Wait a bit after flashing the loader sketch for the board to become
// available again.
logrus.Debug("sleeping for 3 sec")
time.Sleep(3 * time.Second)
// Get flasher depending on which module to use
var f flasher.Flasher
// This matches the baudrate used in the FirmwareUpdater.ino sketch
// https://github.com/arduino-libraries/WiFiNINA/blob/master/examples/Tools/FirmwareUpdater/FirmwareUpdater.ino
const baudRate = 1000000
switch moduleName {
case "NINA":
// we use address and not bootloaderPort because the board should not be in bootloader mode
f, err = flasher.NewNinaFlasher(commonFlags.Address, baudRate, 30)
case "WINC1500":
f, err = flasher.NewWincFlasher(commonFlags.Address, baudRate, 30)
default:
err = fmt.Errorf("unknown module: %s", moduleName)
feedback.Errorf("Error during firmware flashing: %s", err)
os.Exit(errorcodes.ErrGeneric)
}
if err != nil {
feedback.Errorf("Error during firmware flashing: %s", err)
return err
}
defer f.Close()
// now flash the actual firmware
flasherOut := new(bytes.Buffer)
flasherErr := new(bytes.Buffer)
if feedback.GetFormat() == feedback.JSON {
err = f.FlashFirmware(firmwareFile, flasherOut)
} else {
f.SetProgressCallback(printProgress)
err = f.FlashFirmware(firmwareFile, os.Stdout)
}
if err != nil {
flasherErr.Write([]byte(fmt.Sprintf("Error during firmware flashing: %s", err)))
return err
}
// Print the results
feedback.PrintResult(&flasher.FlashResult{
Programmer: (&flasher.ExecOutput{
Stdout: programmerOut.String(),
Stderr: programmerErr.String(),
}),
Flasher: (&flasher.ExecOutput{
Stdout: flasherOut.String(),
Stderr: flasherErr.String(),
}),
})
return nil
}
// callback used to print the progress
func printProgress(progress int) {
fmt.Printf("Flashing progress: %d%%\r", progress)
}