-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
225 lines (197 loc) · 6.08 KB
/
types.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package ccboot
import (
"encoding/hex"
"errors"
"fmt"
"strings"
)
var ErrParse = errors.New("Unable to parse given string")
// CC_SYNC contains the bootloader sync words
//var CC_SYNC = []byte{0x00, 0x03, 0x00, 0x00, 0x55, 0x55}
var CC_SYNC = []byte{0x55, 0x55}
const (
CC_ACK byte = 0xCC
CC_NACK byte = 0x33
)
type CommandType byte
// CommandType constants
const (
COMMAND_PING = CommandType(0x20)
COMMAND_DOWNLOAD = CommandType(0x21)
COMMAND_GET_STATUS = CommandType(0x23)
COMMAND_SEND_DATA = CommandType(0x24)
COMMAND_RESET = CommandType(0x25)
COMMAND_SECTOR_ERASE = CommandType(0x26)
COMMAND_CRC32 = CommandType(0x27)
COMMAND_GET_CHIP_ID = CommandType(0x28)
COMMAND_MEMORY_READ = CommandType(0x2A)
COMMAND_MEMORY_WRITE = CommandType(0x2B)
COMMAND_BANK_ERASE = CommandType(0x2C)
COMMAND_SET_CCFG = CommandType(0x2D)
)
var cmd2String = map[CommandType]string{
COMMAND_PING: "COMMAND_PING",
COMMAND_DOWNLOAD: "COMMAND_DOWNLOAD",
COMMAND_GET_STATUS: "COMMAND_GET_STATUS",
COMMAND_SEND_DATA: "COMMAND_SEND_DATA",
COMMAND_RESET: "COMMAND_RESET",
COMMAND_SECTOR_ERASE: "COMMAND_SECTOR_ERASE",
COMMAND_CRC32: "COMMAND_CRC32",
COMMAND_GET_CHIP_ID: "COMMAND_GET_CHIP_ID",
COMMAND_MEMORY_READ: "COMMAND_MEMORY_READ",
COMMAND_MEMORY_WRITE: "COMMAND_MEMORY_WRITE",
COMMAND_BANK_ERASE: "COMMAND_BANK_ERASE",
COMMAND_SET_CCFG: "COMMAND_SET_CCFG",
}
func (c CommandType) String() string {
if str, ok := cmd2String[c]; ok {
return str
}
return fmt.Sprintf("0x%X", byte(c))
}
// Command represents the command type and paramerters
type Command struct {
Type CommandType
Parameters []byte
}
func (c *Command) Marshal() []byte {
size := 1 + len(c.Parameters)
buf := make([]byte, size)
buf[0] = byte(c.Type)
copy(buf[1:], c.Parameters)
return buf
}
func (c *Command) Unmarshal(data []byte) error {
if len(data) < 1 {
return ErrBadPacket
}
c.Type = CommandType(data[0])
c.Parameters = data[1:]
return nil
}
func (c Command) String() string {
switch c.Type {
case COMMAND_PING:
fallthrough
case COMMAND_GET_STATUS:
fallthrough
case COMMAND_BANK_ERASE:
fallthrough
case COMMAND_RESET:
fallthrough
case COMMAND_GET_CHIP_ID:
return c.Type.String()
case COMMAND_SECTOR_ERASE:
// address
return fmt.Sprintf("%v (addr=0x%s)", c.Type, hex.EncodeToString(c.Parameters[0:4]))
case COMMAND_CRC32:
//address, size, and read count
return fmt.Sprintf("%v (addr=0x%s, size=%d, read_count=%d)", c.Type, hex.EncodeToString(c.Parameters[0:4]), decodeUint32(c.Parameters[4:8]), decodeUint32(c.Parameters[8:]))
case COMMAND_DOWNLOAD:
return fmt.Sprintf("%v (addr=0x%s, size=%d)", c.Type, hex.EncodeToString(c.Parameters[0:4]), decodeUint32(c.Parameters[4:8]))
case COMMAND_MEMORY_READ:
return fmt.Sprintf("%v (addr=0x%s, type=%v, count=%d)", c.Type, hex.EncodeToString(c.Parameters[0:4]), ReadWriteType(c.Parameters[4]), uint8(c.Parameters[5]))
default:
return fmt.Sprintf("%v [%d]=(%s)", c.Type, len(c.Parameters), hex.EncodeToString(c.Parameters))
}
}
const (
SendDataMaxSize = 255 - 3
)
// Status represents the status received by the GetStatus command
type Status byte
// These constants are returned from COMMAND_GET_STATUS
const (
COMMAND_RET_SUCCESS = Status(0x40)
COMMAND_RET_UNKNOW_CMD = Status(0x41)
COMMAND_RET_INVALID_CMD = Status(0x42)
COMMAND_RET_INVALID_ADR = Status(0x43)
COMMAND_RET_FLASH_FAIL = Status(0x44)
)
var cmdret2String = map[Status]string{
COMMAND_RET_SUCCESS: "SUCCESS",
COMMAND_RET_UNKNOW_CMD: "UNKNOWN_CMD",
COMMAND_RET_INVALID_CMD: "INVALID_CMD",
COMMAND_RET_INVALID_ADR: "INVALID_ADR",
COMMAND_RET_FLASH_FAIL: "FLASH_FAIL",
}
func (s Status) String() string {
if str, ok := cmdret2String[s]; ok {
return str
}
return fmt.Sprintf("0x%X", byte(s))
}
type ReadWriteType byte
const (
ReadWriteType8Bit = ReadWriteType(0)
ReadWriteType32Bit = ReadWriteType(1)
)
var readWriteType2String = map[ReadWriteType]string{
ReadWriteType8Bit: "8BIT",
ReadWriteType32Bit: "32BIT",
}
func (rt ReadWriteType) String() string {
if str, ok := readWriteType2String[rt]; ok {
return str
}
return fmt.Sprintf("0x%X", byte(rt))
}
const (
ReadMaxCount8Bit = uint8(253)
ReadMaxCount32Bit = uint8(63)
)
const (
WriteMaxCount8Bit = uint8(247)
WriteMaxCount32Bit = uint8(244) // 32 bit aligned writes - divisible by 4
)
type CCFG_FieldID uint32
const (
ID_SECTOR_PROT = CCFG_FieldID(0)
ID_IMAGE_VALID = CCFG_FieldID(1)
ID_TEST_TAP_LCK = CCFG_FieldID(2)
ID_PRCM_TAP_LCK = CCFG_FieldID(3)
ID_CPU_DAP_LCK = CCFG_FieldID(4)
ID_WUC_TAP_LCK = CCFG_FieldID(5)
ID_PBIST1_TAP_LCK = CCFG_FieldID(6)
ID_PBIST2_TAP_LCK = CCFG_FieldID(7)
ID_BANK_ERASE_DIS = CCFG_FieldID(8)
ID_CHIP_ERASE_DIS = CCFG_FieldID(9)
ID_TI_FA_ENABLE = CCFG_FieldID(10)
ID_BL_BACKDOOR_EN = CCFG_FieldID(11)
ID_BL_BACKDOOR_PIN = CCFG_FieldID(12)
ID_BL_BACKDOOR_LEVEL = CCFG_FieldID(13)
ID_BL_ENABLE = CCFG_FieldID(14)
)
var ccfgFieldID2String = map[CCFG_FieldID]string{
ID_SECTOR_PROT: "ID_SECTOR_PROT",
ID_IMAGE_VALID: "ID_IMAGE_VALID",
ID_TEST_TAP_LCK: "ID_TEST_TAP_LCK",
ID_PRCM_TAP_LCK: "ID_PRCM_TAP_LCK",
ID_CPU_DAP_LCK: "ID_CPU_DAP_LCK",
ID_WUC_TAP_LCK: "ID_WUC_TAP_LCK",
ID_PBIST1_TAP_LCK: "ID_PBIST1_TAP_LCK",
ID_PBIST2_TAP_LCK: "ID_PBIST2_TAP_LCK",
ID_BANK_ERASE_DIS: "ID_BANK_ERASE_DIS",
ID_CHIP_ERASE_DIS: "ID_CHIP_ERASE_DIS",
ID_TI_FA_ENABLE: "ID_TI_FA_ENABLE",
ID_BL_BACKDOOR_EN: "ID_BL_BACKDOOR_EN",
ID_BL_BACKDOOR_PIN: "ID_BL_BACKDOOR_PIN",
ID_BL_BACKDOOR_LEVEL: "ID_BL_BACKDOOR_LEVEL",
ID_BL_ENABLE: "ID_BL_ENABLE",
}
func (c CCFG_FieldID) String() string {
if str, ok := ccfgFieldID2String[c]; ok {
return str
}
return fmt.Sprintf("0x%X", byte(c))
}
// ParseCCFGFieldID parses a string that names a CCFG Field ID and
// returns the proper CCFG_FieldID value
func ParseCCFGFieldID(fieldname string) (CCFG_FieldID, error) {
for value, name := range ccfgFieldID2String {
if strings.Compare(name, fieldname) == 0 {
return value, nil
}
}
return CCFG_FieldID(255), ErrParse
}