-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserial.go
More file actions
58 lines (51 loc) · 1.32 KB
/
Copy pathserial.go
File metadata and controls
58 lines (51 loc) · 1.32 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
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
)
var (
sFilename = flag.String("filename", "32u2.eep", "Name fo the eeprom image we insert the serial number in")
bCreate = flag.Bool("create", true, "Create the file if it does not exist")
)
func main() {
flag.Parse()
var image []byte
var err error
if image, err = ioutil.ReadFile(*sFilename); err != nil {
if err != os.ErrNotExist && !*bCreate {
fmt.Printf("FATAL %v\n", err)
return
}
image = bytes.Repeat([]byte{0xFF}, 1024)
}
if len(image) != 1024 {
fmt.Printf("Expected 1024 byte eprom image, not %v. Exiting\n", len(image))
return
}
if flag.NArg() > 1 {
fmt.Printf("Usage: %s [--create] [--filename filename] [Serialnumber]\n")
flag.PrintDefaults()
}
if flag.NArg() == 1 {
if len(flag.Arg(0)) > 19 {
fmt.Printf("FATAL: Serial numbers need to be smaller than 20 characters\n")
return
}
copy(image[256:256+32], bytes.Repeat([]byte{0xFF}, 20))
for i, ch := range flag.Arg(0) {
image[256+i] = byte(ch) + 0x80
}
if err := ioutil.WriteFile(*sFilename, image, os.FileMode(0666)); err != nil {
fmt.Printf("Error writing file: %v\n", err)
return
}
}
var buf bytes.Buffer
for p := 0x100; image[p] != 0xFF && p < 0x11A; p++ {
buf.WriteByte(image[p] & 0x7F)
}
fmt.Printf("Serial number: %v\n", buf.String())
}