-
Notifications
You must be signed in to change notification settings - Fork 1
/
psp.rs
203 lines (185 loc) · 6.71 KB
/
psp.rs
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
use alloc::boxed::Box;
use alloc::string::String;
use core::mem;
use plain::Plain;
use super::{
ComboDirectoryEntry,
ComboDirectoryHeader,
DirectoryHeader
};
#[derive(Clone, Copy, Debug)]
#[repr(packed)]
pub struct PspDirectoryEntry {
/// 0x00: type of entry
pub kind: u8,
/// 0x01: used to filter entries by model
pub sub_program: u8,
/// 0x02: specifies which ROM contains the entry
pub rom_id: u8,
pub rsvd_03: u8,
/// 0x04: size of the entry
pub size: u32,
/// 0x08: location or value of the entry
pub value: u64,
}
impl PspDirectoryEntry {
pub fn data(&self, data: &[u8]) -> Result<Box<[u8]>, String> {
if self.size == 0xFFFFFFFF {
return Ok(vec![
(self.value >> (0 * 8)) as u8,
(self.value >> (1 * 8)) as u8,
(self.value >> (2 * 8)) as u8,
(self.value >> (3 * 8)) as u8,
(self.value >> (4 * 8)) as u8,
(self.value >> (5 * 8)) as u8,
(self.value >> (6 * 8)) as u8,
(self.value >> (7 * 8)) as u8,
].into_boxed_slice());
}
let start = (self.value & 0x1FFFFFF) as usize;
let end = start + self.size as usize;
if end <= data.len() {
Ok(data[start..end].to_vec().into_boxed_slice())
} else {
Err(format!("PSP directory entry invalid: {:08X}:{:08X}", start, end))
}
}
pub fn description(&self) -> &'static str {
match self.kind {
0x00 => "AMD Public Key",
0x01 => "PSP Boot Loader",
0x02 => "PSP Secure OS",
0x03 => "PSP Recovery Boot Loader",
0x04 => "PSP Non-volatile Data",
0x08 => "SMU Firmware",
0x09 => "AMD Secure Debug Key",
0x0A => "OEM Public Key",
0x0B => "PSP Soft Fuse Chain",
0x0C => "PSP Trustlet",
0x0D => "PSP Trustlet Public Key",
0x12 => "SMU Firmware",
0x13 => "PSP Early Secure Unlock Debug",
0x20 => "IP Discovery",
0x21 => "Wrapped iKEK",
0x22 => "PSP Token Unlock",
0x24 => "Security Policy",
0x25 => "MP2 Firmware",
0x26 => "MP2 Firmware Part 2",
0x27 => "User Mode Unit Test",
0x28 => "System Driver",
0x29 => "KVM Image",
0x2A => "MP5 Firmware",
0x2B => "Embedded Firmware Signature",
0x2C => "TEE Write-once NVRAM",
0x2D => "External Chipset PSP Boot Loader",
0x2E => "External Chipset MP0 Firmware",
0x2F => "External Chipset MP1 Firmware",
0x30 => "PSP AGESA Binary 0",
0x31 => "PSP AGESA Binary 1",
0x32 => "PSP AGESA Binary 2",
0x33 => "PSP AGESA Binary 3",
0x34 => "PSP AGESA Binary 4",
0x35 => "PSP AGESA Binary 5",
0x36 => "PSP AGESA Binary 6",
0x37 => "PSP AGESA Binary 7",
0x38 => "SEV Data",
0x39 => "SEV Code",
0x3A => "Processor Serial Number Allow List",
0x3B => "SERDES Microcode",
0x3C => "VBIOS Pre-load",
0x3D => "WLAN Umac",
0x3E => "WLAN Imac",
0x3F => "WLAN Bluetooth",
0x40 => "PSP Level 2 Directory",
0x41 => "External Chipset MP0 Boot Loader",
0x42 => "DXIO PHY SRAM Firmware",
0x43 => "DXIO PHY SRAM Firmware Public Key",
0x44 => "USB PHY Firmware",
0x45 => "Security Policy for tOS",
0x46 => "External Chipset PSP Boot Loader",
0x47 => "DRTM TA",
0x48 => "Recovery L2A PSP Directory",
0x49 => "Recovery L2 BIOS Directory",
0x4A => "Recovery L2B PSP Directory",
0x4C => "External Chipset Security Policy",
0x4D => "External Chipset Secure Debug Unlock",
0x4E => "PMU Public Key",
0x4F => "UMC Firmware",
0x50 => "PSP Boot Loader Public Keys Table",
0x51 => "PSP tOS Public Keys Table",
0x52 => "OEM PSP Boot Loader Application",
0x53 => "OEM PSP Boot Loader Application Public Key",
0x54 => "PSP RPMC NVRAM",
0x55 => "PSP Boot Loader Anti-rollback",
0x56 => "PSP Secure OS Anti-rollback",
0x57 => "CVIP Configuration Table",
0x58 => "DMCU-ERAM",
0x59 => "DMCU-ISR",
0x5A => "MSMU Binary 0",
0x5B => "MSMU Binary 1",
0x73 => "PSP Boot Loader AB",
0x80 => "OEM Sys-TA",
0x81 => "OEM Sys-TA Signing Key",
_ => "Unknown",
}
}
}
unsafe impl Plain for PspDirectoryEntry {}
pub struct PspDirectory<'a> {
header: &'a DirectoryHeader,
entries: &'a [PspDirectoryEntry]
}
impl<'a> PspDirectory<'a> {
pub fn new(data: &'a [u8]) -> Result<Self, String> {
if &data[..4] == b"$PSP" || &data[..4] == b"$PL2" {
let header: &DirectoryHeader = plain::from_bytes(&data).map_err(|err| {
format!("PSP directory header invalid: {:?}", err)
})?;
return Ok(Self {
header: header,
entries: plain::slice_from_bytes_len(
&data[mem::size_of::<DirectoryHeader>()..],
header.entries as usize
).map_err(|err| {
format!("PSP directory entries invalid: {:?}", err)
})?
});
}
Err(format!("PSP directory header not found"))
}
pub fn header(&self) -> &'a DirectoryHeader {
self.header
}
pub fn entries(&self) -> &'a [PspDirectoryEntry] {
self.entries
}
}
pub struct PspComboDirectory<'a> {
header: &'a ComboDirectoryHeader,
entries: &'a [ComboDirectoryEntry]
}
impl<'a> PspComboDirectory<'a> {
pub fn new(data: &'a [u8]) -> Result<Self, String> {
if &data[..4] == b"2PSP" {
let header: &ComboDirectoryHeader = plain::from_bytes(&data).map_err(|err| {
format!("PSP combo header invalid: {:?}", err)
})?;
return Ok(Self {
header: header,
entries: plain::slice_from_bytes_len(
&data[mem::size_of::<ComboDirectoryHeader>()..],
header.entries as usize
).map_err(|err| {
format!("PSP combo entries invalid: {:?}", err)
})?
});
}
Err(format!("PSP combo header not found"))
}
pub fn header(&self) -> &'a ComboDirectoryHeader {
self.header
}
pub fn entries(&self) -> &'a [ComboDirectoryEntry] {
self.entries
}
}