Skip to content

Commit d1e29ce

Browse files
committed
Added header decoder
1 parent f45dc5c commit d1e29ce

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

cli/ota/decode.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package ota
19+
20+
import (
21+
"os"
22+
23+
"github.com/arduino/arduino-cli/cli/errorcodes"
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/command/ota"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
type decodeHeaderFlags struct {
30+
file string
31+
}
32+
33+
func initDecodeHeaderCommand() *cobra.Command {
34+
flags := &decodeHeaderFlags{}
35+
uploadCommand := &cobra.Command{
36+
Use: "decode",
37+
Short: "OTA firmware header decoder",
38+
Long: "decode OTA firmware header of the given binary file",
39+
Run: func(cmd *cobra.Command, args []string) {
40+
if err := runDecodeHeaderCommand(flags); err != nil {
41+
feedback.Errorf("Error during firmware decoding: %v", err)
42+
os.Exit(errorcodes.ErrGeneric)
43+
}
44+
},
45+
}
46+
uploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.ota)")
47+
uploadCommand.MarkFlagRequired("file")
48+
return uploadCommand
49+
}
50+
51+
func runDecodeHeaderCommand(flags *decodeHeaderFlags) error {
52+
params := &ota.ReadHeaderParams{
53+
File: flags.file,
54+
}
55+
err := ota.ReadHeader(params)
56+
if err != nil {
57+
return err
58+
}
59+
return nil
60+
}

cli/ota/encode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func initEncodeBinaryCommand() *cobra.Command {
4747
}
4848
uploadCommand.Flags().StringVarP(&flags.FQBN, "fqbn", "b", "", "Device fqbn")
4949
uploadCommand.Flags().StringVarP(&flags.file, "file", "", "", "Binary file (.bin) to be encoded")
50-
uploadCommand.MarkFlagRequired("device-id")
50+
uploadCommand.MarkFlagRequired("fqbn")
5151
uploadCommand.MarkFlagRequired("file")
5252
return uploadCommand
5353
}

cli/ota/ota.go

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewCommand() *cobra.Command {
3131
otaCommand.AddCommand(initUploadCommand())
3232
otaCommand.AddCommand(initMassUploadCommand())
3333
otaCommand.AddCommand(initEncodeBinaryCommand())
34+
otaCommand.AddCommand(initDecodeHeaderCommand())
3435

3536
return otaCommand
3637
}

command/ota/readheader.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// This file is part of arduino-cloud-cli.
2+
//
3+
// Copyright (C) 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published
7+
// by the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package ota
19+
20+
import (
21+
"fmt"
22+
"os"
23+
24+
"github.com/arduino/arduino-cli/cli/feedback"
25+
"github.com/arduino/arduino-cloud-cli/internal/ota"
26+
"gopkg.in/yaml.v3"
27+
)
28+
29+
type ReadHeaderParams struct {
30+
File string
31+
}
32+
33+
// Encode command is used to encode a firmware OTA
34+
func ReadHeader(params *ReadHeaderParams) error {
35+
_, err := os.Stat(params.File)
36+
if err != nil {
37+
return fmt.Errorf("file %s does not exists: %w", params.File, err)
38+
}
39+
40+
// Verify if file has already an OTA header
41+
header, err := ota.DecodeOtaFirmwareHeader(params.File)
42+
if err != nil {
43+
return fmt.Errorf("file %s does not contains a valid OTA header: %v", params.File, err)
44+
}
45+
if header == nil {
46+
return fmt.Errorf("file %s does not contains a valid OTA header", params.File)
47+
}
48+
49+
out, _ := yaml.Marshal(header)
50+
feedback.Print(string(out))
51+
52+
return nil
53+
}

0 commit comments

Comments
 (0)