forked from arduino/ArduinoCore-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (84 loc) · 2.42 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
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
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"os"
)
func main() {
var output = flag.String("output", "", "Output to a specific file (default: add -zsk.bin suffix)")
var debug = flag.Bool("debug", false, "Enable debugging mode")
var linked = flag.Bool("prelinked", false, "Provided file has already been linked to Zephyr")
var force = flag.Bool("force", false, "Ignore safety checks and overwrite the header")
var add_header = flag.Bool("add_header", false, "Add space for the header to the file")
flag.Parse()
if flag.NArg() != 1 {
fmt.Printf("Usage: %s [flags] <filename>\n", os.Args[0])
flag.PrintDefaults()
return
}
filename := flag.Arg(0)
// Read the file content
content, err := os.ReadFile(filename)
if err != nil {
fmt.Printf("Error reading file: %v\n", err)
return
}
var ELF_HEADER = []byte { 0x7f, 0x45, 0x4c, 0x46 }
var elf_header_found = bytes.Compare(ELF_HEADER, content[0:4]) == 0
if *add_header || (!*force && !elf_header_found) {
fmt.Printf("File does not have an ELF header, adding empty space\n")
var newContent = make([]byte, len(content)+16)
copy(newContent[16:], content)
content = newContent
}
// Create and fill custom header
var header struct {
ver uint8 // @ 0x07
len uint32 // @ 0x08
magic uint16 // @ 0x0c
flags uint8 // @ 0x0e
}
header.ver = 1
header.magic = 0x2341 // Arduino USB VID
header.len = uint32(len(content))
header.flags = 0
if *debug {
header.flags |= 0x01
}
if *linked {
header.flags |= 0x02
}
var bytes = make([]byte, 9)
_, err = binary.Encode(bytes, binary.LittleEndian, header)
if err != nil {
fmt.Printf("Error encoding header: %v\n", err)
return
}
// Bytes 7 to 15 are free to use in current ELF specification. We will
// use them to store the debug and linked flags.
// Check if the target area is empty
if !*force {
for i := 7; i < 16; i++ {
if content[i] != 0 {
fmt.Printf("Target ELF header area is not empty. Use --force to overwrite\n")
return
}
}
}
// Change the header bytes in the content
copy(content[7:16], bytes)
// Create a new filename for the copy
newFilename := *output
if newFilename == "" {
newFilename = filename + "-zsk.bin"
}
// Write the new content to the new file
err = os.WriteFile(newFilename, content, 0644)
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
return
}
fmt.Printf("File %s saved as %s\n", filename, newFilename)
}