Skip to content

Commit 98664e4

Browse files
committed
Create td-shim-interface crate for publishig
Signed-off-by: OuyangHang33 <[email protected]>
1 parent 9b4e454 commit 98664e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+239
-65
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ members = [
1919
"tests/test-td-paging",
2020
"tests/test-td-payload",
2121
"xtask",
22+
"td-shim-interface",
2223
]
2324

2425
# the profile used for debug build of `td-shim` and `td-payload`

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ else
1111
export BUILD_TYPE_FLAG=
1212
endif
1313

14-
GENERIC_LIB_CRATES = td-layout td-logger td-uefi-pi td-loader cc-measurement
14+
GENERIC_LIB_CRATES = td-layout td-logger td-shim-interface td-loader cc-measurement
1515
NIGHTLY_LIB_CRATES = td-exception td-paging tdx-tdcall
1616
SHIM_CRATES = td-shim td-payload
1717
TEST_CRATES = test-td-exception test-td-paging

sh_script/fuzzing.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ readonly script_name=${0##*/}
88

99
fuzz_folder=(
1010
"td-loader"
11-
"td-uefi-pi"
11+
"td-shim-interface/src"
1212
"td-shim"
1313
)
1414

sh_script/rudra.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ paths=(
2525
"td-paging"
2626
"td-payload"
2727
"td-shim"
28-
"td-uefi-pi"
28+
"td-shim-interface/src"
2929
"td-shim-tools"
3030
"tdx-tdcall"
3131
)

td-layout/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2018"
1212
[dependencies]
1313
scroll = { version = "0.10", default-features = false, features = ["derive"]}
1414
log = "0.4.13"
15-
td-uefi-pi = { path = "../td-uefi-pi" }
15+
td-shim-interface = { path = "../td-shim-interface" }
1616

1717
[dev-dependencies]
1818
memoffset = "0.6"

td-payload/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
2323
spin = "0.9"
2424
td-logger = { path = "../td-logger" }
2525
td-shim = { path = "../td-shim", default-features = false }
26-
td-uefi-pi = { path = "../td-uefi-pi" }
26+
td-shim-interface = { path = "../td-shim-interface" }
2727
td-exception = { path = "../td-exception" }
2828
td-paging = { path = "../td-paging" }
2929
x86 = "0.47.0"

td-payload/src/acpi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use alloc::vec::Vec;
66
use scroll::Pread;
77
use spin::Once;
88
use td_shim::TD_ACPI_TABLE_HOB_GUID;
9-
use td_uefi_pi::{
9+
use td_shim_interface::td_uefi_pi::{
1010
hob as hob_lib,
1111
pi::hob::{GuidExtension, Header, HOB_TYPE_END_OF_HOB_LIST, HOB_TYPE_GUID_EXTENSION},
1212
};

td-payload/src/bin/example/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use td_payload as _;
2626
use td_payload::println;
2727
use td_shim::e820::{E820Entry, E820Type};
2828
use td_shim::{TD_ACPI_TABLE_HOB_GUID, TD_E820_TABLE_HOB_GUID};
29-
use td_uefi_pi::hob;
30-
use td_uefi_pi::pi;
29+
use td_shim_interface::td_uefi_pi::hob;
30+
use td_shim_interface::td_uefi_pi::pi;
3131
use zerocopy::FromBytes;
3232

3333
#[macro_use]

td-payload/src/hob.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use core::mem::size_of;
66
use scroll::Pread;
77
use spin::Once;
8-
use td_uefi_pi::{
8+
use td_shim_interface::td_uefi_pi::{
99
hob::check_hob_integrity,
1010
pi::hob::{HandoffInfoTable, HOB_TYPE_HANDOFF},
1111
};

td-payload/src/mm/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use td_shim::{
1111
e820::{E820Entry, E820Type},
1212
TD_E820_TABLE_HOB_GUID,
1313
};
14-
use td_uefi_pi::{
14+
use td_shim_interface::td_uefi_pi::{
1515
hob as hob_lib,
1616
pi::hob::{GuidExtension, Header, HOB_TYPE_END_OF_HOB_LIST, HOB_TYPE_GUID_EXTENSION},
1717
};

td-shim-interface/Cargo.toml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "td-shim-interface"
3+
version = "0.1.0"
4+
license = "BSD-2-Clause-Patent"
5+
description = "TD-shim metadata data structures and related functions. UEFI Platform Initializaiton data structures and accessors"
6+
7+
edition = "2018"
8+
homepage = "https://github.com/confidential-containers/td-shim"
9+
repository = "https://github.com/confidential-containers/td-shim"
10+
readme = "README.md"
11+
keywords = ["td-shim", "TDX", "intel"]
12+
13+
[dependencies]
14+
r-efi = "3.2.0"
15+
scroll = { version = "0.10", default-features = false, features = ["derive"] }
16+
zerocopy = { version = "0.7.31", features = ["derive"] }
17+
18+
log = "0.4.13"

td-shim-interface/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim?ref=badge_shield)
2+
# TD-shim-interface - Confidential Containers Shim Firmware Interface
3+
4+
## Documents
5+
6+
* [TD-Shim specification](doc/tdshim_spec.md)
7+
8+
* Introduction [PDF](doc/td-shim-introduction.pdf) and [conference talk](https://fosdem.org/2023/schedule/event/cc_online_rust/)
9+
10+
## Introduction
11+
12+
This td-shim-interface is to support user for creating data structures and functions required for td-shim, such as TdxMetadataDescriptor and TdxMetadataSection.
13+
Td-uefi-pi is used for UEFI Platform Initializaiton data structures and accessors.
14+
15+
To import the data structure of metadata, TD HOB and related function, such as:
16+
```
17+
use td_shim_interface::{TD_ACPI_TABLE_HOB_GUID, TD_E820_TABLE_HOB_GUID, TD_PAYLOAD_INFO_HOB_GUID};
18+
use td_shim_interface::PayloadInfo;
19+
use td_shim_interface::acpi;
20+
use td_shim_interface::td_uefi_pi::{hob, pi, pi::guid}
21+
```
22+
23+
This is a Shim Firmware to support [Intel TDX](https://software.intel.com/content/www/us/en/develop/articles/intel-trust-domain-extensions.html).
24+
25+
The API specification is at [td-shim specification](doc/tdshim_spec.md).
26+
27+
The secure boot specification for td-shim is at [secure boot specification](doc/secure_boot.md)
28+
29+
The design is at [td-shim design](doc/design.md).
30+
31+
The threat model analysis is at [td-shim threat model](doc/threat_model.md).
32+
33+
34+
## License
35+
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fconfidential-containers%2Ftd-shim?ref=badge_large)
File renamed without changes.
File renamed without changes.

td-uefi-pi/fuzz/fuzz_targets/fuzzlib.rs td-shim-interface/src/fuzz/fuzz_targets/fuzzlib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use core::mem::size_of;
77
use r_efi::efi::Guid;
88
use std::vec::Vec;
9-
use td_uefi_pi::{fv, hob, pi};
9+
use td_shim_interface::td_uefi_pi::{fv, hob, pi};
1010

1111
const EFI_END_OF_HOB_LIST_OFFSET: usize = 48;
1212

td-shim-interface/src/lib.rs

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Copyright (c) 2022 Alibaba Cloud
2+
//
3+
// SPDX-License-Identifier: BSD-2-Clause-Patent
4+
5+
#![no_std]
6+
7+
use scroll::{Pread, Pwrite};
8+
9+
pub mod acpi;
10+
pub mod metadata;
11+
pub mod td_uefi_pi;
12+
13+
use td_uefi_pi::pi::{self, guid};
14+
15+
// This GUID is used for ACPI GUID Extension HOB
16+
// Please refer to:
17+
// https://github.com/confidential-containers/td-shim/blob/main/doc/tdshim_spec.md#acpi-guid-extension-hob
18+
pub const TD_ACPI_TABLE_HOB_GUID: guid::Guid = guid::Guid::from_fields(
19+
0x6a0c5870,
20+
0xd4ed,
21+
0x44f4,
22+
[0xa1, 0x35, 0xdd, 0x23, 0x8b, 0x6f, 0xc, 0x8d],
23+
);
24+
25+
// This GUID is used for TD Payload Info GUID Extension HOB
26+
// Please refer to:
27+
// https://github.com/confidential-containers/td-shim/blob/main/doc/tdshim_spec.md#td-payload-info-guid-extension-hob
28+
pub const TD_PAYLOAD_INFO_HOB_GUID: guid::Guid = guid::Guid::from_fields(
29+
0xb96fa412,
30+
0x461f,
31+
0x4be3,
32+
[0x8c, 0xd, 0xad, 0x80, 0x5a, 0x49, 0x7a, 0xc0],
33+
);
34+
35+
// This GUID is used for E820 Memory Map GUID Extension HOB
36+
// Please refer to:
37+
// https://github.com/confidential-containers/td-shim/blob/main/doc/tdshim_spec.md#e820-memory-map-guid-extension-hob
38+
pub const TD_E820_TABLE_HOB_GUID: pi::guid::Guid = pi::guid::Guid::from_fields(
39+
0x8f8072ea,
40+
0x3486,
41+
0x4b47,
42+
[0x86, 0xa7, 0x23, 0x53, 0xb8, 0x8a, 0x87, 0x73],
43+
);
44+
45+
#[repr(u32)]
46+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
47+
pub enum TdPayloadInfoHobType {
48+
/// Payload Binary is a PE/COFF or ELF executable image as payload.
49+
///
50+
/// Entrypoint can be found by parsing the image header. This type image does not follow
51+
/// Linux boot protocol. A payload HOB is used to pass data from TdShim to payload.
52+
ExecutablePayload = 0,
53+
54+
/// Payload Binary is bzImage, follow Linux boot protocol.
55+
///
56+
/// The first 512 bytes are boot_param. (zero page). The entrypoint is start address of loaded
57+
/// 64bit Linux kernel plus 0x200
58+
BzImage,
59+
60+
/// Payload Binary is VMM loaded vmLinux, follow Linux boot protocol.
61+
///
62+
/// The entrypoint is defined at HOB_PAYLOAD_INFO_TABLE.Entrypoint.
63+
RawVmLinux,
64+
65+
/// Unknown Image type
66+
UnknownImage = u32::MAX,
67+
}
68+
69+
impl From<&TdPayloadInfoHobType> for u32 {
70+
fn from(v: &TdPayloadInfoHobType) -> Self {
71+
*v as u32
72+
}
73+
}
74+
75+
impl From<u32> for TdPayloadInfoHobType {
76+
fn from(v: u32) -> Self {
77+
match v {
78+
0 => TdPayloadInfoHobType::ExecutablePayload,
79+
1 => TdPayloadInfoHobType::BzImage,
80+
2 => TdPayloadInfoHobType::RawVmLinux,
81+
_ => TdPayloadInfoHobType::UnknownImage,
82+
}
83+
}
84+
}
85+
86+
#[repr(C)]
87+
#[derive(Default, Clone, Copy, Pread, Pwrite)]
88+
pub struct PayloadInfo {
89+
pub image_type: u32,
90+
pub reserved: u32,
91+
pub entry_point: u64,
92+
}
93+
94+
/// Write three bytes from an integer value into the buffer.
95+
pub fn write_u24(data: u32, buf: &mut [u8; 3]) {
96+
assert!(data <= 0xffffff);
97+
buf[0] = (data & 0xFF) as u8;
98+
buf[1] = ((data >> 8) & 0xFF) as u8;
99+
buf[2] = ((data >> 16) & 0xFF) as u8;
100+
}
101+
102+
// To protect against speculative attacks, place the LFENCE instruction after the range
103+
// check and branch, but before any code that consumes the checked value.
104+
pub fn speculation_barrier() {
105+
unsafe { core::arch::asm!("lfence") }
106+
}
107+
108+
#[cfg(test)]
109+
mod test {
110+
use super::*;
111+
112+
#[test]
113+
fn test_tdpayload_info_hob_type() {
114+
assert_eq!(
115+
TdPayloadInfoHobType::from(0),
116+
TdPayloadInfoHobType::ExecutablePayload
117+
);
118+
assert_eq!(TdPayloadInfoHobType::from(1), TdPayloadInfoHobType::BzImage);
119+
assert_eq!(
120+
TdPayloadInfoHobType::from(2),
121+
TdPayloadInfoHobType::RawVmLinux
122+
);
123+
assert_eq!(
124+
TdPayloadInfoHobType::from(3),
125+
TdPayloadInfoHobType::UnknownImage
126+
);
127+
}
128+
129+
#[test]
130+
fn test_write_u24() {
131+
let mut buf: [u8; 3] = [0; 3];
132+
write_u24(0xffffff, &mut buf);
133+
}
134+
}

td-shim/src/metadata.rs td-shim-interface/src/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
extern crate alloc;
66

7+
use crate::td_uefi_pi::pi::guid::Guid;
78
use alloc::string::String;
89
use core::{ptr::slice_from_raw_parts, str::FromStr};
910
use scroll::{Pread, Pwrite};
10-
use td_uefi_pi::pi::guid::Guid;
1111

1212
/// TDX Metadata GUID defined in td-shim specification
1313
pub const TDX_METADATA_GUID_STR: &str = "E9EAF9F3-168E-44D5-A8EB-7F4D8738F6AE";
File renamed without changes.
File renamed without changes.

td-uefi-pi/src/lib.rs td-shim-interface/src/td_uefi_pi/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
//!
2222
//! Constants and data structures defined by [UEFI PI Spec] are hosted by [crate::pi], functions
2323
//! to access them are hosted by [crate::fv] and [crate::hob].
24-
#![no_std]
2524
2625
pub mod fv;
2726
pub mod hob;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

td-shim-tools/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ regex = "1"
4242
scroll = { version = "0.10", default-features = false, features = ["derive"]}
4343
td-layout = { path = "../td-layout" }
4444
td-shim = { path = "../td-shim", default-features = false }
45-
td-uefi-pi = { path = "../td-uefi-pi" }
45+
td-shim-interface = { path = "../td-shim-interface" }
4646
cfg-if = "1.0"
4747

4848
anyhow = { version = "1.0.68", optional = true }

td-shim-tools/src/bin/td-shim-checker/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use log::{error, LevelFilter};
1010
use std::str::FromStr;
1111
use std::vec::Vec;
1212
use std::{env, io};
13-
use td_shim::metadata::{TdxMetadataDescriptor, TdxMetadataSection};
13+
use td_shim_interface::metadata::{TdxMetadataDescriptor, TdxMetadataSection};
1414
use td_shim_tools::loader::TdShimLoader;
1515

1616
struct Config {

td-shim-tools/src/bin/td-shim-enroll/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use std::path::PathBuf;
1111
use std::str::FromStr;
1212
use std::vec::Vec;
1313
use std::{env, io, path::Path};
14+
use td_shim_interface::td_uefi_pi::pi::guid;
1415
use td_shim_tools::enroller::{create_key_file, enroll_files, FirmwareRawFile};
1516
use td_shim_tools::InputData;
16-
use td_uefi_pi::pi::guid;
1717
const TDSHIM_SB_NAME: &str = "final.sb.bin";
1818

1919
struct Config {

td-shim-tools/src/enroller.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use td_shim::secure_boot::{
1818
PUBKEY_FILE_STRUCT_VERSION_V1, PUBKEY_HASH_ALGORITHM_SHA384,
1919
};
2020
use td_shim::write_u24;
21-
use td_uefi_pi::pi::fv::{
21+
use td_shim_interface::td_uefi_pi::pi::fv::{
2222
FIRMWARE_FILE_SYSTEM3_GUID, FVH_REVISION, FVH_SIGNATURE, FV_FILETYPE_RAW,
2323
};
2424

@@ -298,7 +298,7 @@ mod test {
298298
use std::str::FromStr;
299299

300300
use super::*;
301-
use td_uefi_pi::pi::guid;
301+
use td_shim_interface::td_uefi_pi::pi::guid;
302302

303303
fn read_u24(data: &[u8]) -> u32 {
304304
let mut num = data[0] as u32;

td-shim-tools/src/linker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use td_shim::fv::{
2020
FvFfsFileHeader, FvFfsSectionHeader, FvHeader, IplFvFfsHeader, IplFvFfsSectionHeader,
2121
IplFvHeader,
2222
};
23-
use td_shim::metadata::{TdxMetadataGuid, TdxMetadataPtr};
2423
use td_shim::reset_vector::{ResetVectorHeader, ResetVectorParams};
2524
use td_shim::write_u24;
26-
use td_uefi_pi::pi::fv::{
25+
use td_shim_interface::metadata::{TdxMetadataGuid, TdxMetadataPtr};
26+
use td_shim_interface::td_uefi_pi::pi::fv::{
2727
FfsFileHeader, FVH_REVISION, FVH_SIGNATURE, FV_FILETYPE_DXE_CORE, FV_FILETYPE_SECURITY_CORE,
2828
SECTION_PE32,
2929
};

td-shim-tools/src/loader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::fs;
99
use std::io;
1010
use std::io::Read;
1111
use std::io::Seek;
12-
use td_shim::metadata::{
12+
use td_shim_interface::metadata::{
1313
self, TdxMetadataDescriptor, TdxMetadataGuid, TdxMetadataSection, TDX_METADATA_DESCRIPTOR_LEN,
1414
TDX_METADATA_GUID_LEN, TDX_METADATA_OFFSET, TDX_METADATA_SECTION_LEN,
1515
};

0 commit comments

Comments
 (0)