-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapping.go
66 lines (56 loc) · 1.43 KB
/
mapping.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 amdfw
import (
"bytes"
"fmt"
)
const DefaultFlashMapping = uint32(0xFF000000)
func GetFlashMapping(firmwareBytes []byte, fet *FirmwareEntryTable) (uint32, error) {
type mappingMagic struct {
addr *uint32
magic []string
}
for _, s := range []mappingMagic{{
addr: fet.PSPDirBase,
magic: []string{PSPCOOCKIE, DUALPSPCOOCKIE},
}, {
addr: fet.NewPSPDirBase,
magic: []string{PSPCOOCKIE, DUALPSPCOOCKIE},
}, {
addr: fet.BHDDirBase,
magic: []string{BHDCOOCKIE},
}, {
addr: fet.BHDDirBase,
magic: []string{BHDCOOCKIE},
},
} {
for _, m := range s.magic {
if s.addr != nil && *s.addr != 0 {
mapping, err := testMapping(firmwareBytes, *s.addr, m)
if err == nil {
return mapping, nil
}
}
}
}
return 0, fmt.Errorf("No valid mapping found!")
}
func testMapping(firmwareBytes []byte, address uint32, expected string) (uint32, error) {
for _, mapping := range []uint32{
DefaultFlashMapping + 0x000000, //16M
DefaultFlashMapping + 0x800000, // 8M
DefaultFlashMapping + 0xB00000, // 4M
DefaultFlashMapping + 0xD00000, // 2M
DefaultFlashMapping + 0xE00000, // 1M
DefaultFlashMapping + 0xE80000, // 512K
} {
expectedBytes := []byte(expected)
testAddr := address - mapping
if int(testAddr) > len(firmwareBytes) {
continue
}
if bytes.Equal(firmwareBytes[testAddr:testAddr+4], expectedBytes) {
return mapping, nil
}
}
return 0, fmt.Errorf("No Default Mapping fits")
}