/
psp_header.go
80 lines (65 loc) · 2.2 KB
/
psp_header.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
// Copyright 2019 the LinuxBoot Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package manifest
import (
"encoding/binary"
"fmt"
"io"
)
// PSPBootloaderCookie is a special identifier of a PSP binary
const PSPBootloaderCookie = 0x31535024 // "$PS1"
// FirmwareVersion represents PSP firmware version
type FirmwareVersion [4]byte
// String converts FirmwareVersion into a string
func (v FirmwareVersion) String() string {
return fmt.Sprintf("%x.%x.%x.%x", v[3], v[2], v[1], v[0])
}
// PSPHeader represents a header of each firmware binary
// See: https://doc.coreboot.org/soc/amd/psp_integration.html
type PSPHeader struct {
Reserved1 [16]byte
Cookie uint32
Reserved2 [76]byte
Version FirmwareVersion
Reserved3 [156]byte
}
// Reserved1Offset returns the offset in bytes of field Reserved1
func (h *PSPHeader) Reserved1Offset() uint64 {
return 0
}
func (h *PSPHeader) Reserved1Length() uint64 {
return uint64(binary.Size(h.Reserved1))
}
// CookieOffset returns the offset in bytes of field Cookie
func (h *PSPHeader) CookieOffset() uint64 {
return h.Reserved1Offset() + h.Reserved1Length()
}
// CookieLength returns the size in bytes of field Cookie
func (h *PSPHeader) CookieLength() uint64 {
return uint64(binary.Size(h.Cookie))
}
// Reserved2Offset returns the offset in bytes of field Reserved2
func (h *PSPHeader) Reserved2Offset() uint64 {
return h.CookieOffset() + h.CookieLength()
}
// Reserved2Length returns the size in bytes of field Reserved2
func (h *PSPHeader) Reserved2Length() uint64 {
return uint64(binary.Size(h.Reserved2))
}
// VersionOffset returns the offset in bytes of field Version
func (h *PSPHeader) VersionOffset() uint64 {
return h.Reserved2Offset() + h.Reserved2Length()
}
// VersionLength returns the size in bytes of field Version
func (h *PSPHeader) VersionLength() uint64 {
return uint64(binary.Size(h.Version))
}
// ParsePSPHeader parses the PSP header that is supposed to be the beginning of each PSP binary
func ParsePSPHeader(r io.Reader) (*PSPHeader, error) {
var result PSPHeader
if err := binary.Read(r, binary.LittleEndian, &result); err != nil {
return nil, err
}
return &result, nil
}