-
Notifications
You must be signed in to change notification settings - Fork 8
/
firmware.go
103 lines (91 loc) · 2.12 KB
/
firmware.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
100
101
102
103
package main
import (
log "ndsemu/emu/logger"
"ndsemu/emu/spi"
"os"
)
var modFw = log.NewModule("firmware")
const (
FFCodeRead uint8 = 0x03
FFCodeRdsr uint8 = 0x05
FFCodeWren uint8 = 0x06
FFCodeWrdi uint8 = 0x04
FFCodePw uint8 = 0x0A
)
type HwFirmwareFlash struct {
f *os.File
wen bool
wbuf []byte
addr uint32
}
func NewHwFirmwareFlash() *HwFirmwareFlash {
return &HwFirmwareFlash{}
}
func (ff *HwFirmwareFlash) MapFirmwareFile(fn string) error {
f, err := os.OpenFile(fn, os.O_RDWR, 0777)
if err != nil {
return err
}
ff.f = f
return nil
}
func (ff *HwFirmwareFlash) SpiBegin() {
ff.addr = 0
ff.wbuf = nil
}
func (ff *HwFirmwareFlash) SpiTransfer(data []byte) ([]byte, spi.ReqStatus) {
cmd := data[0]
switch cmd {
case 0:
// Dummy command that is sent as part of the last byte transfer
// FIXME: we could fix this at the spibus level
return nil, spi.ReqFinish
case FFCodeRead:
if len(data) < 4 {
return nil, spi.ReqContinue
}
if len(data) == 4 {
ff.addr = uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
modFw.InfoZ("HEAD").Hex32("addr", ff.addr).End()
}
buf := make([]byte, 1024)
ff.f.ReadAt(buf, int64(ff.addr))
ff.addr += 1024
return buf, spi.ReqContinue
case FFCodeRdsr:
status := uint8(0)
if ff.wen {
status |= 2
}
modFw.InfoZ("read status").Hex8("val", status).End()
return []byte{status}, spi.ReqFinish
case FFCodeWren:
modFw.InfoZ("write enabled").End()
ff.wen = true
return nil, spi.ReqFinish
case FFCodeWrdi:
modFw.InfoZ("write disabled").End()
ff.wen = false
return nil, spi.ReqFinish
case FFCodePw:
if len(data) < 4 {
return nil, spi.ReqContinue
}
if len(data) == 4 {
ff.addr = uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
modFw.InfoZ("WRITE").Hex32("addr", ff.addr).End()
}
// Put away buffer data; will be written just once, in SpiEnd
ff.wbuf = data[4:]
return nil, spi.ReqContinue
default:
modFw.ErrorZ("unsupported command").Hex8("cmd", cmd).End()
return nil, spi.ReqFinish
}
}
func (ff *HwFirmwareFlash) SpiEnd() {
if ff.wbuf != nil {
ff.f.WriteAt(ff.wbuf, int64(ff.addr))
ff.wbuf = nil
}
}