-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash.go
More file actions
101 lines (82 loc) · 2.65 KB
/
flash.go
File metadata and controls
101 lines (82 loc) · 2.65 KB
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
// This file is part of remoteocd.
//
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of remoteocd.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package main
import (
"context"
"fmt"
"path"
"strings"
"github.com/arduino/go-paths-helper"
"github.com/arduino/remoteocd/board"
"github.com/arduino/remoteocd/feedback"
)
const binaryDir = "/tmp/remoteocd/"
const binaryName = "sketch.elf-zsk.bin"
func flash(ctx context.Context, cmder board.Boarder, binary *paths.Path, files []*paths.Path) error {
err := cmder.MkDirAll(ctx, binaryDir)
if err != nil {
return err
}
feedback.Logf("Pushing binary %q", binary)
remoteBinary, err := pushBinary(ctx, cmder, binary)
if err != nil {
return err
}
feedback.Logf("Pushing config files: %v", files)
remoteFiles, err := pushFiles(ctx, cmder, files)
if err != nil {
return err
}
args := makeOpenOCDCmd(remoteBinary, remoteFiles...)
feedback.Logf("Running command: %s", strings.Join(args, " "))
err = cmder.Run(ctx, args...)
if err != nil {
return fmt.Errorf("error running OpenOCD: %w", err)
}
return nil
}
func pushBinary(ctx context.Context, cmder board.Boarder, binary *paths.Path) (string, error) {
destination := path.Join(binaryDir, binaryName)
if err := cmder.CopyTo(ctx, binary.String(), destination); err != nil {
return "", err
}
return destination, nil
}
func pushFiles(ctx context.Context, cmder board.Boarder, files []*paths.Path) ([]string, error) {
remoteFiles := make([]string, 0, len(files))
for _, file := range files {
destination := path.Join(binaryDir, file.Base())
if err := cmder.CopyTo(ctx, file.String(), destination); err != nil {
return nil, err
}
remoteFiles = append(remoteFiles, destination)
}
return remoteFiles, nil
}
const openOCDPath = "/opt/openocd"
const openOCDBin = openOCDPath + "/bin/openocd"
func makeOpenOCDCmd(binary string, files ...string) []string {
args := []string{
openOCDBin, "-d2",
"-s", openOCDPath,
"-s", openOCDPath + "/share/openocd/scripts",
"-f", "openocd_gpiod.cfg",
"-c", "set filename " + binary,
}
for _, file := range files {
args = append(args, "-f", file)
}
return args
}