@@ -8,7 +8,7 @@ use amd_efs::flash::Location;
8
8
use amd_efs:: {
9
9
AddressMode , ComboDirectoryEntryFilter , EfhBulldozerSpiMode ,
10
10
EfhEspiConfiguration , EfhNaplesSpiMode , EfhRomeSpiMode ,
11
- ProcessorGeneration ,
11
+ ProcessorGeneration , PspSoftFuseChain ,
12
12
} ;
13
13
use amd_efs:: {
14
14
BhdDirectoryEntry , BhdDirectoryEntryRegionType , BhdDirectoryEntryType ,
@@ -30,6 +30,8 @@ pub enum Error {
30
30
Io ( std:: io:: Error ) ,
31
31
#[ error( "image too big" ) ]
32
32
ImageTooBig ,
33
+ #[ error( "psp entry source {0} unknown" ) ]
34
+ PspEntrySourceUnknown ( PspDirectoryEntryType ) ,
33
35
}
34
36
35
37
impl From < amd_efs:: Error > for Error {
@@ -106,11 +108,125 @@ impl TryFromSerdeDirectoryEntryWithContext<SerdePspDirectoryEntry>
106
108
}
107
109
}
108
110
111
+ #[ derive( Clone , serde:: Serialize , schemars:: JsonSchema ) ]
112
+ #[ serde( rename = "SerdePspEntrySourceValue" ) ]
113
+ #[ serde( deny_unknown_fields) ]
114
+ #[ non_exhaustive]
115
+ pub enum SerdePspEntrySourceValue {
116
+ PspSoftFuseChain ( PspSoftFuseChain ) ,
117
+ #[ serde( deserialize_with = "deserialize_raw" ) ]
118
+ Unknown ( u64 ) ,
119
+ }
120
+
121
+ impl < ' de > serde:: de:: Deserialize < ' de > for SerdePspEntrySourceValue {
122
+ fn deserialize < D > ( deserializer : D ) -> std:: result:: Result < Self , D :: Error >
123
+ where
124
+ D : serde:: de:: Deserializer < ' de > ,
125
+ {
126
+ struct PspVisitor ;
127
+
128
+ impl < ' de > serde:: de:: Visitor < ' de > for PspVisitor {
129
+ type Value = SerdePspEntrySourceValue ;
130
+
131
+ fn expecting (
132
+ & self ,
133
+ formatter : & mut std:: fmt:: Formatter ,
134
+ ) -> std:: fmt:: Result {
135
+ formatter
136
+ . write_str ( "a u64 or a SerdePspEntrySourceValue variant" )
137
+ }
138
+
139
+ fn visit_u64 < E > (
140
+ self ,
141
+ value : u64 ,
142
+ ) -> std:: result:: Result < Self :: Value , E >
143
+ where
144
+ E : serde:: de:: Error ,
145
+ {
146
+ Ok ( SerdePspEntrySourceValue :: Unknown ( value) )
147
+ }
148
+
149
+ fn visit_i64 < E > (
150
+ self ,
151
+ value : i64 ,
152
+ ) -> std:: result:: Result < Self :: Value , E >
153
+ where
154
+ E : serde:: de:: Error ,
155
+ {
156
+ if value >= 0 {
157
+ Ok ( SerdePspEntrySourceValue :: Unknown ( value as u64 ) )
158
+ } else {
159
+ Err ( E :: invalid_value (
160
+ serde:: de:: Unexpected :: Signed ( value) ,
161
+ & "a positive integer or SerdePspEntrySourceValue variant" ,
162
+ ) )
163
+ }
164
+ }
165
+
166
+ fn visit_map < A > (
167
+ self ,
168
+ mut map : A ,
169
+ ) -> std:: result:: Result < Self :: Value , A :: Error >
170
+ where
171
+ A : serde:: de:: MapAccess < ' de > ,
172
+ {
173
+ if let Some ( key) = map. next_key :: < String > ( ) ? {
174
+ match key. as_str ( ) {
175
+ "PspSoftFuseChain" => {
176
+ Ok ( SerdePspEntrySourceValue :: PspSoftFuseChain (
177
+ map. next_value :: < PspSoftFuseChain > ( ) ?,
178
+ ) )
179
+ }
180
+ _ => Err ( serde:: de:: Error :: custom (
181
+ "expected SerdePspEntrySourceValue variant" ,
182
+ ) ) ,
183
+ }
184
+ } else {
185
+ Err ( serde:: de:: Error :: custom (
186
+ "expected SerdePspEntrySourceValue variant" ,
187
+ ) )
188
+ }
189
+ }
190
+ }
191
+
192
+ deserializer. deserialize_any ( PspVisitor )
193
+ }
194
+ }
195
+
196
+ impl SerdePspEntrySourceValue {
197
+ pub fn from_u64 ( value : u64 , typ : PspDirectoryEntryType ) -> Self {
198
+ match typ {
199
+ PspDirectoryEntryType :: PspSoftFuseChain => {
200
+ Self :: PspSoftFuseChain ( PspSoftFuseChain :: from ( value) )
201
+ }
202
+ _ => SerdePspEntrySourceValue :: Unknown ( value) ,
203
+ }
204
+ }
205
+
206
+ pub fn to_u64 (
207
+ & self ,
208
+ typ_or_err : std:: result:: Result < PspDirectoryEntryType , amd_efs:: Error > ,
209
+ ) -> Result < u64 > {
210
+ if let SerdePspEntrySourceValue :: Unknown ( x) = self {
211
+ Ok ( * x)
212
+ } else {
213
+ let typ = typ_or_err. unwrap ( ) ;
214
+ match typ {
215
+ PspDirectoryEntryType :: PspSoftFuseChain => match self {
216
+ Self :: PspSoftFuseChain ( x) => Ok ( u64:: from ( * x) ) ,
217
+ _ => Err ( Error :: PspEntrySourceUnknown ( typ) ) ,
218
+ } ,
219
+ _ => Err ( Error :: PspEntrySourceUnknown ( typ) ) ,
220
+ }
221
+ }
222
+ }
223
+ }
224
+
109
225
#[ derive( Clone , serde:: Serialize , serde:: Deserialize , schemars:: JsonSchema ) ]
110
226
#[ serde( rename = "PspEntrySource" ) ]
111
227
#[ serde( deny_unknown_fields) ]
112
228
pub enum SerdePspEntrySource {
113
- Value ( u64 ) ,
229
+ Value ( SerdePspEntrySourceValue ) ,
114
230
BlobFile ( PathBuf ) ,
115
231
SecondLevelDirectory ( SerdePspDirectory ) ,
116
232
}
0 commit comments