forked from arduino/ArduinoCore-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (48 loc) · 1.2 KB
/
main.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
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide a filename")
return
}
filename := os.Args[1]
debug := 0
if len(os.Args) == 3 {
if os.Args[2] == "debug" {
debug = 1
}
}
// Read the file content
content, err := os.ReadFile(filename)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
// Get the length of the file content
length := len(content)
// Create a new filename for the copy
newFilename := filename + ".dfu"
// Create the new content with the length in front
len_str := fmt.Sprintf("%d", length)
newContent := append([]byte(len_str), 0, byte(debug))
// make newContent 16 bytes
tmp := make([]byte, 16-len(newContent))
newContent = append(newContent, tmp...)
newContent = append(newContent, content...)
// Write the new content to the new file
err = os.WriteFile(newFilename, []byte(newContent), 0644)
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
return
}
// Copy in .bin
err = os.WriteFile(newFilename+".bin", []byte(newContent), 0644)
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
return
}
fmt.Printf("File copied and saved as %s\n", newFilename)
}