-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
61 lines (50 loc) · 1.22 KB
/
image.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
package amdfw
import (
"fmt"
)
type (
Image struct {
FET *FirmwareEntryTable
FlashMapping *uint32
Roms []*Rom
}
)
func ParseImage(firmwareBytes []byte) (*Image, error) {
image := Image{}
fetOffset, err := FindFirmwareEntryTable(firmwareBytes)
if err != nil {
return nil, fmt.Errorf("Could not parse Image: %v", err)
}
fet, err := ParseFirmwareEntryTable(firmwareBytes, fetOffset)
if err != nil {
return nil, fmt.Errorf("Could not parse Image: %v", err)
}
image.FET = fet
mapping, err := GetFlashMapping(firmwareBytes, fet)
if err != nil {
return nil, fmt.Errorf("Could not parse Image: %v", err)
}
image.FlashMapping = &mapping
roms, errs := ParseRoms(firmwareBytes, fet, mapping)
if len(errs) != 0 {
err = fmt.Errorf("Errors parsing images %v", errs)
} else {
err = nil
}
image.Roms = roms
return &image, err
}
func (image *Image) Write(baseImage []byte) ([]byte, error) {
var err error
if err = image.FET.Write(baseImage, image.FET.Location); err != nil {
return nil, err
}
if image.FlashMapping != nil {
for _, rom := range image.Roms {
if err = rom.Write(baseImage, image.FET, *image.FlashMapping); err != nil {
return nil, err
}
}
}
return baseImage, nil
}