-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathflash.go
247 lines (222 loc) · 8.21 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
arduino-fwuploader
Copyright (c) 2021 Arduino LLC. All right reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package firmware
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/arduino/arduino-fwuploader/cli/arguments"
"github.com/arduino/arduino-fwuploader/cli/common"
"github.com/arduino/arduino-fwuploader/cli/feedback"
"github.com/arduino/arduino-fwuploader/cli/globals"
"github.com/arduino/arduino-fwuploader/flasher"
"github.com/arduino/arduino-fwuploader/indexes/download"
"github.com/arduino/arduino-fwuploader/indexes/firmwareindex"
"github.com/arduino/arduino-fwuploader/plugin"
"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 int
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 [email protected]\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().IntVar(&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) {
// at the end cleanup the fwuploader temp dir
defer globals.FwUploaderPath.RemoveAll()
if retries < 1 {
feedback.Fatal("Number of retries should be at least 1", feedback.ErrBadArgument)
}
common.CheckFlags(commonFlags.Fqbn, commonFlags.Address)
packageIndex, firmwareIndex := common.InitIndexes()
board := common.GetBoard(firmwareIndex, commonFlags.Fqbn)
uploadToolDir := common.DownloadRequiredToolsForBoard(packageIndex, board)
// Get module name if not specified
moduleName := board.Module
moduleVersion := ""
if module != "" {
split := strings.SplitN(module, "@", 2)
moduleName = split[0]
if len(split) == 2 {
moduleVersion = split[1]
}
}
// Normalize module name
moduleName = strings.ToUpper(moduleName)
var firmwareFilePath *paths.Path
// If a local firmware file has been specified
if fwFile != "" {
firmwareFilePath = paths.New(fwFile)
if !firmwareFilePath.Exist() {
feedback.Fatal(fmt.Sprintf("firmware file not found in %s", firmwareFilePath), feedback.ErrGeneric)
}
} else {
// Download the firmware
var firmware *firmwareindex.IndexFirmware
if moduleVersion == "" {
firmware = board.LatestFirmware()
} else {
firmware = board.GetFirmware(moduleVersion)
}
if firmware == nil {
feedback.Fatal(fmt.Sprintf("Error getting firmware for board: %s", commonFlags.Fqbn), feedback.ErrGeneric)
}
logrus.Debugf("module name: %s, firmware version: %s", firmware.Module, firmware.Version.String())
if fwPath, err := download.DownloadFirmware(firmware); err != nil {
feedback.Fatal(fmt.Sprintf("Error downloading firmware from %s: %s", firmware.URL, err), feedback.ErrGeneric)
} else {
firmwareFilePath = fwPath
}
logrus.Debugf("firmware file downloaded in %s", firmwareFilePath.String())
}
loaderSketch := ""
var uploader *plugin.FwUploader
if !board.IsPlugin() {
loaderSketchPath, err := download.DownloadSketch(board.LoaderSketch)
if err != nil {
feedback.Fatal(fmt.Sprintf("Error downloading loader sketch from %s: %s", board.LoaderSketch.URL, err), feedback.ErrGeneric)
}
logrus.Debugf("loader sketch downloaded in %s", loaderSketchPath.String())
loaderSketch = strings.ReplaceAll(loaderSketchPath.String(), loaderSketchPath.Ext(), "")
} else {
var err error
uploader, err = plugin.NewFWUploaderPlugin(uploadToolDir)
if err != nil {
feedback.Fatal(fmt.Sprintf("Could not open uploader plugin: %s", err), feedback.ErrGeneric)
}
}
retry := 0
for {
retry++
logrus.Infof("Uploading firmware (try %d of %d)", retry, retries)
var res *flasher.FlashResult
var err error
if !board.IsPlugin() {
res, err = updateFirmware(board, loaderSketch, moduleName, uploadToolDir, firmwareFilePath)
} else {
res, err = updateFirmwareWithPlugin(uploader, firmwareFilePath)
}
if err == nil {
feedback.PrintResult(res)
logrus.Info("Operation completed: success! :-)")
break
}
logrus.Error(err)
if retry == retries {
logrus.Fatal("Operation failed. :-(")
}
logrus.Info("Waiting 1 second before retrying...")
time.Sleep(time.Second)
}
}
func updateFirmwareWithPlugin(uploader *plugin.FwUploader, fwPath *paths.Path) (*flasher.FlashResult, error) {
var stdout, stderr io.Writer
if feedback.GetFormat() == feedback.Text {
stdout = os.Stdout
stderr = os.Stderr
}
res, err := uploader.FlashFirmware(commonFlags.Address, commonFlags.Fqbn, globals.LogLevel, globals.Verbose, fwPath, stdout, stderr)
if err != nil {
return nil, fmt.Errorf("couldn't update firmware: %s", err)
}
return &flasher.FlashResult{
Programmer: &flasher.ExecOutput{
Stdout: string(res.Stdout),
Stderr: string(res.Stderr),
},
}, nil
}
func updateFirmware(board *firmwareindex.IndexBoard, loaderSketch, moduleName string, uploadToolDir, firmwareFile *paths.Path) (*flasher.FlashResult, error) {
programmerOut, programmerErr, err := common.FlashSketch(board, loaderSketch, uploadToolDir, commonFlags.Address)
if err != nil {
return nil, 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.Fatal(fmt.Sprintf("Error during firmware flashing: %s", err), feedback.ErrGeneric)
}
if err != nil {
return nil, fmt.Errorf("error during firmware flashing: %s", 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 nil, err
}
// Print the results
return &flasher.FlashResult{
Programmer: (&flasher.ExecOutput{
Stdout: programmerOut.String(),
Stderr: programmerErr.String(),
}),
Flasher: (&flasher.ExecOutput{
Stdout: flasherOut.String(),
Stderr: flasherErr.String(),
}),
}, nil
}
// callback used to print the progress
func printProgress(progress int) {
fmt.Printf("Flashing progress: %d%%\r", progress)
}