-
Notifications
You must be signed in to change notification settings - Fork 2
/
rom.go
186 lines (153 loc) · 4.81 KB
/
rom.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
package amdfw
import (
"bytes"
"fmt"
)
const (
IMCRom RomType = "IMC"
GECRom RomType = "GEC"
XHCIRom RomType = "XHCI"
PSPRom RomType = "PSP"
NewPSPRom RomType = "NEWPSP"
BHDRom RomType = "BHD"
NewBHDRom RomType = "NEWBDH"
)
type (
RomType string
Rom struct {
Type RomType
Directories []*Directory
Raw []byte
}
)
func ParseRoms(firmwareBytes []byte, table *FirmwareEntryTable, flashMapping uint32) ([]*Rom, []error) {
var roms []*Rom
var errors []error
// Parsing ROMs without length is annoying. Not going to implement soon...
//TODO IMC
//TODO GEC
//TODO XHCI
// PSP
rom, err := ParsePSPRom(firmwareBytes, table, flashMapping)
if err != nil {
errors = append(errors, fmt.Errorf("Could not parse psp rom: %v", err))
}
if rom != nil {
roms = append(roms, rom)
}
// newPSP
rom, err = ParseNewPSPRom(firmwareBytes, table, flashMapping)
if err != nil {
errors = append(errors, fmt.Errorf("Could not parse newpsp rom: %v", err))
}
if rom != nil {
roms = append(roms, rom)
}
// BHD
rom, err = ParseBHDRom(firmwareBytes, table, flashMapping)
if err != nil {
errors = append(errors, fmt.Errorf("Could not parse bhd rom: %v", err))
}
if rom != nil {
roms = append(roms, rom)
}
// newBHD
rom, err = ParseNewBHDRom(firmwareBytes, table, flashMapping)
if err != nil {
errors = append(errors, fmt.Errorf("Could not parse new bhd rom: %v", err))
}
if rom != nil {
roms = append(roms, rom)
}
return roms, errors
}
func ParsePSPRom(firmwareBytes []byte, table *FirmwareEntryTable, flashMapping uint32) (*Rom, error) {
return parseDirectoryRom(firmwareBytes, table.PSPDirBase, flashMapping, PSPRom)
}
func ParseNewPSPRom(firmwareBytes []byte, table *FirmwareEntryTable, flashMapping uint32) (*Rom, error) {
return parseDirectoryRom(firmwareBytes, table.NewPSPDirBase, flashMapping, NewPSPRom)
}
func ParseBHDRom(firmwareBytes []byte, table *FirmwareEntryTable, flashMapping uint32) (*Rom, error) {
return parseDirectoryRom(firmwareBytes, table.BHDDirBase, flashMapping, BHDRom)
}
func ParseNewBHDRom(firmwareBytes []byte, table *FirmwareEntryTable, flashMapping uint32) (*Rom, error) {
return parseDirectoryRom(firmwareBytes, table.NewBHDDirBase, flashMapping, NewBHDRom)
}
func parseDirectoryRom(firmwareBytes []byte, address *uint32, flashMapping uint32, romType RomType) (*Rom, error) {
rom := Rom{
Type: romType,
}
if address == nil {
return nil, fmt.Errorf("No %s offset available", romType)
}
directory, err := ParseDirectory(firmwareBytes, *address, flashMapping)
if err != nil {
return nil, fmt.Errorf("Could not read %s Rom: %v", romType, err)
}
rom.Directories = append(rom.Directories, directory)
others, err := recursiveDirectories(firmwareBytes, directory, flashMapping)
rom.Directories = append(rom.Directories, others...)
return &rom, err
}
func recursiveDirectories(firmwareBytes []byte, directory *Directory, flashMapping uint32) ([]*Directory, error) {
var directories []*Directory
for _, entry := range directory.Entries {
if entry.DirectoryEntry.Type == 0x40 ||
entry.DirectoryEntry.Type == 0x70 ||
bytes.Equal(directory.Header.Cookie[:], []byte(DUALPSPCOOCKIE)) {
newDirectory, err := ParseDirectory(firmwareBytes, entry.DirectoryEntry.Location, flashMapping)
if err != nil {
return directories, fmt.Errorf("Could not read Directory: %v", err)
}
directories = append(directories, newDirectory)
others, err := recursiveDirectories(firmwareBytes, newDirectory, flashMapping)
if err != nil {
return directories, fmt.Errorf("Could not read Directory: %v", err)
}
directories = append(directories, others...)
}
}
return directories, nil
}
func GetAddressFromTable(romType RomType, table *FirmwareEntryTable) (uint32, error) {
switch romType {
case PSPRom:
return *table.PSPDirBase, nil
case NewPSPRom:
return *table.NewPSPDirBase, nil
case BHDRom:
return *table.BHDDirBase, nil
case NewBHDRom:
return *table.NewBHDDirBase, nil
case GECRom:
return *table.GecRomBase, nil
case IMCRom:
return *table.ImcRomBase, nil
case XHCIRom:
return *table.XHCRomBase, nil
default:
return 0, fmt.Errorf("Cannot get Address: Unknown Type")
}
}
func (rom Rom) Write(baseImage []byte, table *FirmwareEntryTable, flashMapping uint32) error {
var err error
if rom.Raw != nil {
address, err := GetAddressFromTable(rom.Type, table)
if err != nil {
return fmt.Errorf("Cannot Write: Unknown Type")
}
address = address &^ flashMapping
if int(address)+len(rom.Raw) > len(baseImage) {
return fmt.Errorf("Cannot write Rom: Invalid address in FET")
}
copy(baseImage[address:], rom.Raw)
} else if rom.Directories != nil {
for _, directory := range rom.Directories {
err = directory.Write(baseImage, flashMapping)
if err != nil {
return fmt.Errorf("Cannot Write Rom: %v", err)
}
}
}
return nil
}