Skip to content

Commit 4443b91

Browse files
sgramsjyao1
authored andcommitted
td-shim-tools,td-shim,devtools/td-layout-config: Add a rudimentary support for TD_PARAMS
Signed-off-by: Stanislaw Grams <[email protected]>
1 parent e1aa1f9 commit 4443b91

File tree

5 files changed

+73
-10
lines changed

5 files changed

+73
-10
lines changed

devtools/td-layout-config/src/image.rs

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct ImageConfig {
2020
builtin_payload: Option<String>,
2121
#[serde(rename = "TdInfo")]
2222
td_info: Option<String>,
23+
#[serde(rename = "TdParams")]
24+
td_params: Option<String>,
2325
#[serde(rename = "Metadata")]
2426
metadata: String,
2527
#[serde(rename = "Ipl")]
@@ -79,6 +81,14 @@ pub fn parse_image(data: String) -> String {
7981
)
8082
}
8183

84+
if let Some(td_params_config) = image_config.td_params {
85+
image_layout.reserve_high(
86+
"TdParams",
87+
parse_int::parse::<u32>(&td_params_config).unwrap() as usize,
88+
"Reserved",
89+
)
90+
}
91+
8292
if let Some(payload_config) = image_config.builtin_payload {
8393
image_layout.reserve_high(
8494
"Payload",

doc/tdshim_spec.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ The TD Shim metadata can be located at (TD Shim end – 0x20) byte. It is a
177177
| 5 | Payload | Private Memory | PAGE.ADD + MR.EXTEND(o) | RTMR.EXTEND(o) | MRTD (or) RTMR[1] |
178178
| 6 | PayloadParam | Private Memory | PAGE.ADD | RTMR.EXTEND | RTMR[1] |
179179
| 7 | TD_INFO | Private Memory | N/A | N/A | N/A |
180-
| 8 ~ 0xFFFFFFFF | Reserved | N/A | N/A | N/A | N/A |
180+
| 8 | TD_PARAMS | Private Memory | N/A | N/A | N/A |
181+
| 9 ~ 0xFFFFFFFF | Reserved | N/A | N/A | N/A | N/A |
181182

182183
Rules for the TDVF_SECTION:
183184
* A TD-Shim shall include at least one BFV and the reset vector shall be inside
@@ -199,8 +200,10 @@ Rules for the TDVF_SECTION:
199200
must be zero.
200201
* A TD-Shim may have zero or one PayloadParam. PayloadParam is present only if
201202
the Payload is present.
202-
* A TDVF may have zero or one TD_INFO section. If present, it shall be included
203+
* A TD-Shim may have zero or one TD_INFO section. If present, it shall be included
203204
in BFV section. MemoryAddress and MemoryDataSize shall be zero. See Table 1.1-5.
205+
* A TD-Shim may have zero or one TD_PARAMS section. If present, it shall be included
206+
in BFV section. MemoryAddress and MemoryDataSize shall be zero.
204207

205208
**Table 1.1-5 TD_INFO definition**
206209

td-shim-interface/src/metadata.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ pub const TDX_METADATA_SECTION_TYPE_PAYLOAD: u32 = 5;
4747
pub const TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM: u32 = 6;
4848
/// Section type for td info.
4949
pub const TDX_METADATA_SECTION_TYPE_TD_INFO: u32 = 7;
50+
/// Section type for TD Params.
51+
pub const TDX_METADATA_SECTION_TYPE_TD_PARAMS: u32 = 8;
5052
/// Max Section type
51-
pub const TDX_METADATA_SECTION_TYPE_MAX: u32 = 8;
53+
pub const TDX_METADATA_SECTION_TYPE_MAX: u32 = 9;
5254

5355
pub const TDX_METADATA_SECTION_TYPE_STRS: [&str; TDX_METADATA_SECTION_TYPE_MAX as usize] = [
5456
"BFV",
@@ -59,6 +61,7 @@ pub const TDX_METADATA_SECTION_TYPE_STRS: [&str; TDX_METADATA_SECTION_TYPE_MAX a
5961
"Payload",
6062
"PayloadParam",
6163
"TdInfo",
64+
"TdParams",
6265
];
6366

6467
/// Attribute flags for BFV.
@@ -204,6 +207,9 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
204207
let mut td_info_cnt = 0;
205208
let mut td_info_start = 0;
206209
let mut td_info_end = 0;
210+
let mut td_params_cnt = 0;
211+
let mut td_params_start = 0;
212+
let mut td_params_end = 0;
207213
let check_data_memory_fields =
208214
|data_offset: u32, data_size: u32, memory_address: u64, memory_size: u64| -> bool {
209215
if data_size == 0 && data_offset != 0 {
@@ -407,6 +413,31 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
407413
}
408414
}
409415

416+
TDX_METADATA_SECTION_TYPE_TD_PARAMS => {
417+
// A TD-Shim may have zero or one TdParams. If present, it shall be included in BFV section.
418+
if td_params_cnt == i32::MAX {
419+
return Err(TdxMetadataError::InvalidSection);
420+
}
421+
td_params_cnt += 1;
422+
if td_params_cnt > 1 {
423+
return Err(TdxMetadataError::InvalidSection);
424+
}
425+
if section.attributes != 0 {
426+
return Err(TdxMetadataError::InvalidSection);
427+
}
428+
if section.raw_data_size == 0 {
429+
return Err(TdxMetadataError::InvalidSection);
430+
} else {
431+
td_params_start = section.data_offset;
432+
td_params_end = td_params_start + section.raw_data_size;
433+
}
434+
435+
// MemoryAddress and MemoryDataSize shall be zero.
436+
if section.memory_address != 0 || section.memory_data_size != 0 {
437+
return Err(TdxMetadataError::InvalidSection);
438+
}
439+
}
440+
410441
_ => {
411442
return Err(TdxMetadataError::InvalidSection);
412443
}
@@ -427,13 +458,20 @@ pub fn validate_sections(sections: &[TdxMetadataSection]) -> Result<(), TdxMetad
427458
return Err(TdxMetadataError::InvalidSection);
428459
}
429460

430-
//TdInfo. If present, it shall be included in BFV section.
461+
// TdInfo. If present, it shall be included in BFV section.
431462
if td_info_cnt != 0
432463
&& (td_info_start < bfv_start || td_info_start >= bfv_end || td_info_end > bfv_end)
433464
{
434465
return Err(TdxMetadataError::InvalidSection);
435466
}
436467

468+
// TdParams. If present, it shall be included in BFV section.
469+
if td_params_cnt != 0
470+
&& (td_params_start < bfv_start || td_params_start >= bfv_end || td_params_end > bfv_end)
471+
{
472+
return Err(TdxMetadataError::InvalidSection);
473+
}
474+
437475
Ok(())
438476
}
439477

@@ -523,8 +561,9 @@ mod tests {
523561
"PayloadParam"
524562
);
525563
assert_eq!(TdxMetadataSection::get_type_name(7).unwrap(), "TdInfo");
564+
assert_eq!(TdxMetadataSection::get_type_name(8).unwrap(), "TdParams");
526565

527-
assert!(TdxMetadataSection::get_type_name(8).is_none());
566+
assert!(TdxMetadataSection::get_type_name(9).is_none());
528567
}
529568

530569
#[test]

td-shim-tools/src/metadata.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use td_shim_interface::metadata::{
1212
TDX_METADATA_SECTION_TYPE_CFV, TDX_METADATA_SECTION_TYPE_PAYLOAD,
1313
TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM, TDX_METADATA_SECTION_TYPE_PERM_MEM,
1414
TDX_METADATA_SECTION_TYPE_TD_HOB, TDX_METADATA_SECTION_TYPE_TD_INFO,
15-
TDX_METADATA_SECTION_TYPE_TEMP_MEM, TDX_METADATA_SIGNATURE, TDX_METADATA_VERSION,
15+
TDX_METADATA_SECTION_TYPE_TD_PARAMS, TDX_METADATA_SECTION_TYPE_TEMP_MEM,
16+
TDX_METADATA_SIGNATURE, TDX_METADATA_VERSION,
1617
};
1718
use td_shim_interface::td_uefi_pi::pi::guid::Guid;
1819

@@ -76,6 +77,7 @@ where
7677
"Payload" => Ok(TDX_METADATA_SECTION_TYPE_PAYLOAD),
7778
"PayloadParam" => Ok(TDX_METADATA_SECTION_TYPE_PAYLOAD_PARAM),
7879
"TdInfo" => Ok(TDX_METADATA_SECTION_TYPE_TD_INFO),
80+
"TdParams" => Ok(TDX_METADATA_SECTION_TYPE_TD_PARAMS),
7981
_ => Err(D::Error::custom("Invalid metadata section type")),
8082
}
8183
}

td-shim/src/bin/td-shim/shim_info.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ impl<'a> BootTimeDynamic<'a> {
259259

260260
memory.push(resource)
261261
}
262-
TDX_METADATA_SECTION_TYPE_TD_INFO => {
263-
// for TD_INFO type, the MemoryDataSize is zero, should not make it
262+
TDX_METADATA_SECTION_TYPE_TD_INFO | TDX_METADATA_SECTION_TYPE_TD_PARAMS => {
263+
// for TD_INFO and TD_PARAMS type, the MemoryDataSize is zero, should not make it
264264
// into a ResourceDescription!
265265
continue;
266266
}
@@ -278,10 +278,10 @@ mod tests {
278278

279279
#[test]
280280
fn test_parse_metadata() {
281-
// Ensure the TD_INFO section is not parsed into a ResourceDescription.
281+
// Ensure the TD_INFO and TD_PARAMS section is not parsed into a ResourceDescription.
282282

283283
// init sections include all types
284-
let mut sections: [TdxMetadataSection; 7] = [TdxMetadataSection::default(); 7];
284+
let mut sections: [TdxMetadataSection; 8] = [TdxMetadataSection::default(); 8];
285285
// BFV
286286
sections[0] = TdxMetadataSection {
287287
data_offset: 0,
@@ -345,6 +345,15 @@ mod tests {
345345
attributes: 0,
346346
r#type: TDX_METADATA_SECTION_TYPE_TD_INFO,
347347
};
348+
// TdParams
349+
sections[7] = TdxMetadataSection {
350+
data_offset: 0,
351+
raw_data_size: 0x400,
352+
memory_address: 0,
353+
memory_data_size: 0,
354+
attributes: 0,
355+
r#type: TDX_METADATA_SECTION_TYPE_TD_PARAMS,
356+
};
348357

349358
let res = BootTimeDynamic::parse_metadata(&sections);
350359

0 commit comments

Comments
 (0)