Skip to content

Commit f45dc5c

Browse files
committed
Added header verification before file upload
1 parent d60dbd1 commit f45dc5c

File tree

8 files changed

+45
-11
lines changed

8 files changed

+45
-11
lines changed

cli/cli.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package cli
1919

2020
import (
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"os"
2424
"strings"
2525

@@ -83,7 +83,7 @@ func parseFormatString(arg string) (feedback.OutputFormat, bool) {
8383
}
8484

8585
func preRun(flags *cliFlags) error {
86-
logrus.SetOutput(ioutil.Discard)
86+
logrus.SetOutput(io.Discard)
8787
// enable log only if verbose flag is passed
8888
if flags.verbose {
8989
logrus.SetLevel(logrus.InfoLevel)

cli/ota/encode.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
package ota
1919

2020
import (
21+
"fmt"
2122
"os"
2223

2324
"github.com/arduino/arduino-cli/cli/errorcodes"
2425
"github.com/arduino/arduino-cli/cli/feedback"
2526
"github.com/arduino/arduino-cloud-cli/command/ota"
26-
"github.com/sirupsen/logrus"
2727
"github.com/spf13/cobra"
2828
)
2929

@@ -53,8 +53,6 @@ func initEncodeBinaryCommand() *cobra.Command {
5353
}
5454

5555
func runEncodeCommand(flags *encodeBinaryFlags) error {
56-
logrus.Infof("Encoding binary %s", flags.file)
57-
5856
params := &ota.EncodeParams{
5957
FQBN: flags.FQBN,
6058
File: flags.file,
@@ -64,6 +62,7 @@ func runEncodeCommand(flags *encodeBinaryFlags) error {
6462
return err
6563
}
6664

67-
logrus.Info("Encode successfully performed. OTA file: ", *otafile)
65+
feedback.Print(fmt.Sprintf("Encode successfully performed. File: %s", *otafile))
66+
6867
return nil
6968
}

command/ota/encode.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package ota
2020
import (
2121
"fmt"
2222
"os"
23+
24+
"github.com/arduino/arduino-cloud-cli/internal/ota"
2325
)
2426

2527
type EncodeParams struct {
@@ -29,8 +31,19 @@ type EncodeParams struct {
2931

3032
// Encode command is used to encode a firmware OTA
3133
func Encode(params *EncodeParams) (*string, error) {
34+
_, err := os.Stat(params.File)
35+
if err != nil {
36+
return nil, fmt.Errorf("file %s does not exists: %w", params.File, err)
37+
}
38+
39+
// Verify if file has already an OTA header
40+
header, _ := ota.DecodeOtaFirmwareHeader(params.File)
41+
if header != nil {
42+
return nil, fmt.Errorf("file %s contains a valid OTA header. Skip header encoding", params.File)
43+
}
44+
3245
otaFile := fmt.Sprintf("%s.ota", params.File)
33-
_, err := os.Stat(otaFile)
46+
_, err = os.Stat(otaFile)
3447
if err == nil {
3548
// file already exists, we need to delete it
3649
if err = os.Remove(otaFile); err != nil {

command/ota/massupload.go

+11
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import (
2424
"os"
2525
"path/filepath"
2626

27+
"github.com/arduino/arduino-cli/cli/feedback"
2728
"github.com/arduino/arduino-cloud-cli/config"
2829
"github.com/arduino/arduino-cloud-cli/internal/iot"
30+
"github.com/arduino/arduino-cloud-cli/internal/ota"
2931
iotclient "github.com/arduino/iot-client-go"
3032
)
3133

@@ -64,6 +66,15 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
6466
return nil, fmt.Errorf("file %s does not exists: %w", params.File, err)
6567
}
6668

69+
if !params.DoNotApplyHeader {
70+
//Verify if file has already an OTA header
71+
header, _ := ota.DecodeOtaFirmwareHeader(params.File)
72+
if header != nil {
73+
feedback.Print("File contains a valid OTA header. Skip header generation.")
74+
params.DoNotApplyHeader = true
75+
}
76+
}
77+
6778
// Generate .ota file
6879
var otaFile string
6980
if params.DoNotApplyHeader {

command/ota/upload.go

+12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
"os"
2424
"path/filepath"
2525

26+
"github.com/arduino/arduino-cli/cli/feedback"
27+
2628
"github.com/arduino/arduino-cloud-cli/config"
2729
"github.com/arduino/arduino-cloud-cli/internal/iot"
30+
"github.com/arduino/arduino-cloud-cli/internal/ota"
2831
)
2932

3033
const (
@@ -61,6 +64,15 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
6164
return err
6265
}
6366

67+
if !params.DoNotApplyHeader {
68+
//Verify if file has already an OTA header
69+
header, _ := ota.DecodeOtaFirmwareHeader(params.File)
70+
if header != nil {
71+
feedback.Print("File contains a valid OTA header. Skip header generation.")
72+
params.DoNotApplyHeader = true
73+
}
74+
}
75+
6476
var otaFile string
6577
if params.DoNotApplyHeader {
6678
otaFile = params.File

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/arduino/arduino-cloud-cli
22

3-
go 1.17
3+
go 1.19
44

55
require (
66
github.com/antihax/optional v1.0.0

internal/ota/boadpids.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ var (
4444
"0266": "arduino:mbed_giga:giga",
4545
}
4646

47-
ArduinoVendorID = "2341"
48-
47+
ArduinoVendorID = "2341"
48+
4949
ArduinoFqbnToPID = map[string]string{
5050
"arduino:samd:nano_33_iot": "8057",
5151
"arduino:samd:mkr1000": "804E",

internal/ota/decoder.go

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func extractXID(buff []byte) string {
7878
// DecodeOtaFirmwareHeader decodes the OTA firmware header from a binary file.
7979
// File is composed by an header and a payload (optionally lzss compressed).
8080
// Method is also checking CRC32 of the file, verifying that file is not corrupted.
81-
// See: https://arduino.atlassian.net/wiki/spaces/RFC/pages/1616871540/OTA+header+structure
8281
func DecodeOtaFirmwareHeader(binaryFilePath string) (*OtaFirmwareHeader, error) {
8382
// Check if file exists
8483
if _, err := os.Stat(binaryFilePath); err != nil {

0 commit comments

Comments
 (0)