@@ -20,12 +20,14 @@ package certificates
20
20
21
21
import (
22
22
"bytes"
23
+ "crypto/x509"
23
24
"fmt"
24
25
"io"
25
26
"os"
26
27
"strings"
27
28
"time"
28
29
30
+ "github.com/arduino/arduino-fwuploader/certificates"
29
31
"github.com/arduino/arduino-fwuploader/cli/arguments"
30
32
"github.com/arduino/arduino-fwuploader/cli/common"
31
33
"github.com/arduino/arduino-fwuploader/cli/feedback"
@@ -80,19 +82,86 @@ func runFlash(certificateURLs, certificatePaths []string) {
80
82
uploadToolDir := common .DownloadRequiredToolsForBoard (packageIndex , board )
81
83
82
84
var res * flasher.FlashResult
83
- var err error
85
+ var flashErr error
84
86
if ! board .IsPlugin () {
85
- res , err = flashCertificates (board , uploadToolDir , certificateURLs , certificatePaths )
87
+ res , flashErr = flashCertificates (board , uploadToolDir , certificateURLs , certificatePaths )
86
88
} else {
87
- // TODO
89
+ uploader , err := plugin .NewFWUploaderPlugin (uploadToolDir )
90
+ if err != nil {
91
+ feedback .Fatal (fmt .Sprintf ("Could not open uploader plugin: %s" , err ), feedback .ErrGeneric )
92
+ }
93
+ res , flashErr = flashCertificatesWithPlugin (uploader , certificateURLs , certificatePaths )
88
94
}
89
95
90
96
feedback .PrintResult (res )
91
- if err != nil {
97
+ if flashErr != nil {
92
98
os .Exit (int (feedback .ErrGeneric ))
93
99
}
94
100
}
95
101
102
+ func flashCertificatesWithPlugin (uploader * plugin.FwUploader , certificateURLs , certificatePaths []string ) (* flasher.FlashResult , error ) {
103
+ tmp , err := paths .MkTempDir ("" , "" )
104
+ if err != nil {
105
+ return nil , err
106
+ }
107
+ defer tmp .RemoveAll ()
108
+ certsBundle := tmp .Join ("certs.pem" )
109
+
110
+ stdoutBuffer := & bytes.Buffer {}
111
+ stderrBuffer := & bytes.Buffer {}
112
+ var stdout io.Writer = stdoutBuffer
113
+ var stderr io.Writer = stdoutBuffer
114
+ if feedback .GetFormat () == feedback .Text {
115
+ stdout = io .MultiWriter (os .Stdout , stdoutBuffer )
116
+ stderr = io .MultiWriter (os .Stderr , stderrBuffer )
117
+ }
118
+
119
+ var allCerts []* x509.Certificate
120
+ for _ , certPath := range certificatePaths {
121
+ logrus .Infof ("Converting and flashing certificate %s" , certPath )
122
+ stdout .Write ([]byte (fmt .Sprintf ("Converting and flashing certificate %s\n " , certPath )))
123
+
124
+ certs , err := certificates .LoadCertificatesFromFile (paths .New (certPath ))
125
+ if err != nil {
126
+ return nil , err
127
+ }
128
+ allCerts = append (allCerts , certs ... )
129
+ }
130
+
131
+ for _ , URL := range certificateURLs {
132
+ logrus .Infof ("Converting and flashing certificate from %s" , URL )
133
+ stdout .Write ([]byte (fmt .Sprintf ("Converting and flashing certificate from %s\n " , URL )))
134
+ rootCert , err := certificates .ScrapeRootCertificatesFromURL (URL )
135
+ if err != nil {
136
+ return nil , err
137
+ }
138
+ allCerts = append (allCerts , rootCert )
139
+ }
140
+
141
+ f , err := certsBundle .Create ()
142
+ if err != nil {
143
+ return nil , err
144
+ }
145
+ defer f .Close () // Defer close if an error occurs while writing file
146
+ for _ , cert := range allCerts {
147
+ _ , err := f .Write (certificates .EncodeCertificateAsPEM (cert ))
148
+ if err != nil {
149
+ return nil , err
150
+ }
151
+ }
152
+ if err := f .Close (); err != nil {
153
+ return nil , err
154
+ }
155
+
156
+ _ , err = uploader .FlashCertificates (commonFlags .Address , commonFlags .Fqbn , certsBundle , stdout , stderr )
157
+ return & flasher.FlashResult {
158
+ Flasher : & flasher.ExecOutput {
159
+ Stdout : stdoutBuffer .String (),
160
+ Stderr : stderrBuffer .String (),
161
+ },
162
+ }, err
163
+ }
164
+
96
165
func flashCertificates (board * firmwareindex.IndexBoard , uploadToolDir * paths.Path , certificateURLs , certificatePaths []string ) (* flasher.FlashResult , error ) {
97
166
loaderSketchPath , err := download .DownloadSketch (board .LoaderSketch )
98
167
if err != nil {
0 commit comments