-
Notifications
You must be signed in to change notification settings - Fork 2
/
directory.go
284 lines (216 loc) · 6.97 KB
/
directory.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package amdfw
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
const PSPCOOCKIE = "$PSP"
const DUALPSPCOOCKIE = "2PSP"
const BHDCOOCKIE = "$BHD"
const SECONDPSPCOOCKIE = "$PL2"
const SECONDBHDCOOCKIE = "$BL2"
type (
Directory struct {
Header DirectoryHeader
Entries []Entry
Location uint32
}
DirectoryHeader struct {
Cookie [4]byte
Checksum uint32
TotalEntries uint32
Reserved uint32
}
DirectoryEntry struct {
Type uint32
Size uint32
Location uint32
Reserved uint32
Unknown *uint64
}
binaryDirectoryEntry struct {
Type uint32
Size uint32
Location uint32
Reserved uint32
}
)
func ParseDirectory(firmwareBytes []byte, address uint32, flashMapping uint32) (*Directory, error) {
directory := Directory{}
if address > flashMapping {
address -= flashMapping
}
if address > uint32(len(firmwareBytes)) {
return nil, fmt.Errorf("Firmwarebytes not long enough for reading directory header..")
}
directory.Location = address
directoryBytes := firmwareBytes[address:]
directoryReader := bytes.NewReader(directoryBytes)
directoryHeader := DirectoryHeader{}
err := binary.Read(directoryReader, binary.LittleEndian, &directoryHeader)
if err != nil {
return nil, fmt.Errorf("Could not read directory header: %v", err)
}
cookie := string(directoryHeader.Cookie[:])
isCookieKnown := false
for _, c := range []string{PSPCOOCKIE, DUALPSPCOOCKIE, SECONDPSPCOOCKIE, BHDCOOCKIE, SECONDBHDCOOCKIE} {
if isCookieKnown = c == cookie; isCookieKnown {
break
}
}
if !isCookieKnown {
return nil, fmt.Errorf("No Valid Cookie at start of directory: %v", directoryHeader.Cookie[:])
}
directory.Header = directoryHeader
if int(directory.Header.TotalEntries)*4*8 > len(firmwareBytes[address+8:]) {
return nil, fmt.Errorf("Could not read directory entries from image: Too many entries(%d)!", directory.Header.TotalEntries)
}
if cookie == DUALPSPCOOCKIE {
if _, err := directoryReader.Seek(16, io.SeekCurrent); err != nil {
return nil, fmt.Errorf("Could not read 2PSP directory header: %v", err)
}
}
directory.Entries = make([]Entry, directory.Header.TotalEntries)
for i := 0; i < int(directory.Header.TotalEntries); i++ {
directoryEntry := DirectoryEntry{}
binDirEntry := binaryDirectoryEntry{}
if err := binary.Read(directoryReader, binary.LittleEndian, &binDirEntry); err != nil {
return nil, fmt.Errorf("Coulf not read directory directoryEntry")
}
directoryEntry.Type = binDirEntry.Type
directoryEntry.Size = binDirEntry.Size
directoryEntry.Location = binDirEntry.Location
directoryEntry.Reserved = binDirEntry.Reserved
entry, _ := ParseEntry(firmwareBytes, directoryEntry, flashMapping)
if cookie == DUALPSPCOOCKIE {
entry.TypeInfo.Name = "PSP_DIRECTORY"
entry.TypeInfo.Comment = "Full PSP Directory"
} else if cookie == BHDCOOCKIE || cookie == SECONDBHDCOOCKIE {
//BHD Entries adds 2 additional bytes
unknownBytes := make([]byte, 8)
if c, err := directoryReader.Read(unknownBytes); err != nil || c != 8 {
return nil, fmt.Errorf("Could not read BHD directory entry: %v", err)
}
val := binary.LittleEndian.Uint64(unknownBytes)
entry.DirectoryEntry.Unknown = &val
}
directory.Entries[i] = *entry
}
return &directory, nil
}
func (header *DirectoryHeader) Write(baseImage []byte, address uint32) error {
buf := new(bytes.Buffer)
if int(address)+binary.Size(header) > len(baseImage) {
return fmt.Errorf("Writing DirectoryHeader failed: BaseImage to small")
}
err := binary.Write(buf, binary.LittleEndian, header)
if err != nil {
return fmt.Errorf("Writing binary failed: %v", err)
}
copy(baseImage[address:], buf.Bytes())
return nil
}
func (entry *DirectoryEntry) Write(baseImage []byte, address uint32) error {
buf := new(bytes.Buffer)
binEntry := binaryDirectoryEntry{
Type: entry.Type,
Size: entry.Size,
Location: entry.Location,
Reserved: entry.Reserved,
}
if int(address)+binary.Size(binEntry) > len(baseImage) {
return fmt.Errorf("Writing DirectoryHeader failed: BaseImage to small")
}
err := binary.Write(buf, binary.LittleEndian, binEntry)
if err != nil {
return fmt.Errorf("Writing binary failed: %v", err)
}
bytesNeeded := binary.Size(binaryDirectoryEntry{})
if entry.Unknown != nil {
binary.Write(buf, binary.LittleEndian, *entry.Unknown)
bytesNeeded += binary.Size(*entry.Unknown)
}
bytesCopied := copy(baseImage[address:], buf.Bytes())
if bytesCopied != bytesNeeded {
return fmt.Errorf("Writing binary failed: Not all Bytes copied!")
}
return nil
}
func (directory *Directory) Write(baseImage []byte, flashMapping uint32) error {
if int(directory.Location) > len(baseImage) {
return fmt.Errorf("BaseImage to small to insert Directory")
}
err := directory.Header.Write(baseImage, directory.Location)
if err != nil {
return err
}
location := directory.Location + 0x10
cookie := string(directory.Header.Cookie[:])
if cookie == DUALPSPCOOCKIE {
location += 0x10
}
for i, entry := range directory.Entries {
entryLength := uint32(16)
if entry.DirectoryEntry.Unknown != nil {
entryLength += 8
}
entryAddress := location + uint32(i)*entryLength
err := entry.DirectoryEntry.Write(baseImage, entryAddress)
if err != nil {
return err
}
entryLocation := entry.DirectoryEntry.Location
err = entry.Write(baseImage, entryLocation&^flashMapping)
if err != nil {
return err
}
}
return nil
}
func fletcher32(data []byte) uint32 {
c0 := 0xFFFF
c1 := 0xFFFF
count := len(data)
for index := 0; index < count; index += 2 {
next := binary.LittleEndian.Uint16(data[index:])
c0 += int(next)
c1 += c0
if index > 255 || index == count-2 {
c0 = (c0 & 0xFFFF) + (c0 >> 16)
c1 = (c1 & 0xFFFF) + (c1 >> 16)
}
}
return uint32((c1 << 16) | c0)
}
// Validates the Directory Checksum and return the actual value
func (directory *Directory) ValidateChecksum() (valid bool, actual uint32) {
uint32Buffer := make([]byte, 4)
buf := new(bytes.Buffer)
//Rest of the header
binary.LittleEndian.PutUint32(uint32Buffer, directory.Header.TotalEntries)
buf.Write(uint32Buffer)
binary.LittleEndian.PutUint32(uint32Buffer, directory.Header.Reserved)
buf.Write(uint32Buffer)
cookie := string(directory.Header.Cookie[:])
if cookie == DUALPSPCOOCKIE {
buf.Write(bytes.Repeat([]byte{0}, 16))
}
for _, entry := range directory.Entries {
binary.LittleEndian.PutUint32(uint32Buffer, entry.DirectoryEntry.Type)
buf.Write(uint32Buffer)
binary.LittleEndian.PutUint32(uint32Buffer, entry.DirectoryEntry.Size)
buf.Write(uint32Buffer)
binary.LittleEndian.PutUint32(uint32Buffer, entry.DirectoryEntry.Location)
buf.Write(uint32Buffer)
binary.LittleEndian.PutUint32(uint32Buffer, entry.DirectoryEntry.Reserved)
buf.Write(uint32Buffer)
if cookie == BHDCOOCKIE || cookie == SECONDBHDCOOCKIE {
uint64Buffer := make([]byte, 8)
binary.LittleEndian.PutUint64(uint64Buffer, *entry.DirectoryEntry.Unknown)
buf.Write(uint64Buffer)
}
}
sum := fletcher32(buf.Bytes())
return sum == directory.Header.Checksum, sum
}