forked from trhodeos/makemask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (62 loc) · 1.39 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
package main
import (
"flag"
"fmt"
"os"
)
func makeMask(filename string, bootdata []byte) error {
fmt.Printf("Masking %s.\n", filename)
f, err := os.OpenFile(filename, os.O_RDWR, 0755)
if err != nil {
return fmt.Errorf("could not open ROM: %v", err)
}
defer f.Close()
st, err := f.Stat()
if err != nil {
return err
}
const minSize = checksumStart + checksumLength
if st.Size() < minSize {
fmt.Fprintf(os.Stderr, "Padding to %d bytes\n", minSize)
if err := f.Truncate(minSize); err != nil {
return err
}
}
_, err = f.WriteAt(bootdata, 0x40)
if err != nil {
return fmt.Errorf("could not write: %v", err)
}
crcBytes, err := calculateN64Crc(f, 6102)
if err != nil {
return fmt.Errorf("could not calculate CRC: %v", err)
}
fmt.Printf("Generate checksum: 0x%X\n", crcBytes)
_, err = f.WriteAt(crcBytes[:], 0x10)
if err != nil {
return fmt.Errorf("could not write: %v", err)
}
return nil
}
func mainE() error {
flag.Parse()
if len(flag.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Usage: makemask [rom file, rom file, ...]")
os.Exit(64)
}
bootdata, err := Asset("data/boot.6102")
if err != nil {
return fmt.Errorf("could not load boot data: %v", err)
}
for _, arg := range flag.Args() {
if err := makeMask(arg, bootdata); err != nil {
return err
}
}
return nil
}
func main() {
if err := mainE(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
}