|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; |
| 15 | +use std::cmp::min; |
| 16 | +use std::io::Write; |
16 | 17 |
|
17 | | -use crate::firmware::ovmf::x86_64::GUID_SIZE; |
18 | | -use crate::{bitflags, consts}; |
| 18 | +use snafu::ResultExt; |
| 19 | +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; |
19 | 20 |
|
20 | | -use crate::firmware::ovmf::x86_64::parse_data; |
| 21 | +use crate::firmware::ovmf::x86_64::{GUID_SIZE, parse_data}; |
| 22 | +use crate::firmware::uefi::{ |
| 23 | + HOB_HANDOFF_TABLE_VERSION, HobGenericHeader, HobHandoffInfoTable, HobResourceDesc, |
| 24 | + HobResourceType, HobType, ResourceAttr, |
| 25 | +}; |
21 | 26 | use crate::firmware::{Result, error}; |
| 27 | +use crate::mem::{MemRegionEntry, MemRegionType}; |
| 28 | +use crate::{bitflags, consts}; |
22 | 29 |
|
23 | 30 | pub const GUID_TDX_METADATA_OFFSET: [u8; GUID_SIZE] = [ |
24 | 31 | 0x35, 0x65, 0x7a, 0xe4, 0x4a, 0x98, 0x98, 0x47, 0x86, 0x5e, 0x46, 0x85, 0xa7, 0xbf, 0x8e, 0xc2, |
@@ -57,9 +64,9 @@ bitflags! { |
57 | 64 | #[derive(Debug, Clone, Default, KnownLayout, Immutable, FromBytes, IntoBytes)] |
58 | 65 | pub struct TdvfSectionEntry { |
59 | 66 | pub data_offset: u32, |
60 | | - pub raw_data_size: u32, |
61 | | - pub memory_address: u64, |
62 | | - pub memory_data_size: u64, |
| 67 | + pub data_size: u32, |
| 68 | + pub address: u64, |
| 69 | + pub size: u64, |
63 | 70 | pub r#type: TdvfSectionType, |
64 | 71 | pub attributes: TdvfSectionAttr, |
65 | 72 | } |
@@ -98,3 +105,136 @@ pub fn parse_entries(data: &[u8]) -> Result<&[TdvfSectionEntry]> { |
98 | 105 | }; |
99 | 106 | Ok(entries) |
100 | 107 | } |
| 108 | + |
| 109 | +fn create_hob_mem_resources( |
| 110 | + entries: &[(u64, MemRegionEntry)], |
| 111 | + accepted: &[(u64, u64)], |
| 112 | + mut op: impl FnMut(HobResourceDesc) -> Result<()>, |
| 113 | +) -> Result<()> { |
| 114 | + let tmpl = HobResourceDesc { |
| 115 | + hdr: HobGenericHeader { |
| 116 | + r#type: HobType::RESOURCE_DESCRIPTOR, |
| 117 | + length: size_of::<HobResourceDesc>() as u16, |
| 118 | + reserved: 0, |
| 119 | + }, |
| 120 | + owner: [0; 16], |
| 121 | + attr: ResourceAttr::PRESENT | ResourceAttr::INIT | ResourceAttr::TESTED, |
| 122 | + ..Default::default() |
| 123 | + }; |
| 124 | + let mut iter_e = entries.iter(); |
| 125 | + let mut iter_a = accepted.iter(); |
| 126 | + let mut section = iter_a.next().copied(); |
| 127 | + let mut entry = iter_e.next().copied(); |
| 128 | + loop { |
| 129 | + match (&mut entry, &mut section) { |
| 130 | + (None, None) => break, |
| 131 | + (None, Some((start, size))) => { |
| 132 | + log::error!( |
| 133 | + "Section [{start:x}, {:x}) is not covered by system memory", |
| 134 | + *start + *size |
| 135 | + ); |
| 136 | + return error::UncoveredTdvfSection.fail(); |
| 137 | + } |
| 138 | + (Some((_, e)), _) if e.type_ != MemRegionType::Ram => { |
| 139 | + entry = iter_e.next().copied(); |
| 140 | + continue; |
| 141 | + } |
| 142 | + (Some((s, e)), None) => { |
| 143 | + op(HobResourceDesc { |
| 144 | + r#type: HobResourceType::MEMORY_UNACCEPTED, |
| 145 | + address: *s, |
| 146 | + len: e.size, |
| 147 | + ..tmpl.clone() |
| 148 | + })?; |
| 149 | + entry = iter_e.next().copied(); |
| 150 | + } |
| 151 | + (Some((s, e)), Some((start, size))) => { |
| 152 | + if let Some(len) = min(*s, *start + *size).checked_sub(*start) |
| 153 | + && len > 0 |
| 154 | + { |
| 155 | + *size = len; |
| 156 | + entry = None; |
| 157 | + // Jump to branch (Some, None) |
| 158 | + continue; |
| 159 | + } |
| 160 | + if let Some(len) = min(*s + e.size, *start).checked_sub(*s) |
| 161 | + && len > 0 |
| 162 | + { |
| 163 | + op(HobResourceDesc { |
| 164 | + r#type: HobResourceType::MEMORY_UNACCEPTED, |
| 165 | + address: *s, |
| 166 | + len, |
| 167 | + ..tmpl.clone() |
| 168 | + })?; |
| 169 | + *s += len; |
| 170 | + e.size -= len; |
| 171 | + } |
| 172 | + if let Some(len) = min(*s + e.size, *start + *size).checked_sub(*start) |
| 173 | + && len > 0 |
| 174 | + { |
| 175 | + op(HobResourceDesc { |
| 176 | + r#type: HobResourceType::SYSTEM_MEMORY, |
| 177 | + address: *start, |
| 178 | + len, |
| 179 | + ..tmpl.clone() |
| 180 | + })?; |
| 181 | + *start += len; |
| 182 | + *size -= len; |
| 183 | + *s += len; |
| 184 | + e.size -= len |
| 185 | + }; |
| 186 | + if *size == 0 { |
| 187 | + section = iter_a.next().copied(); |
| 188 | + }; |
| 189 | + if e.size == 0 { |
| 190 | + entry = iter_e.next().copied(); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + Ok(()) |
| 196 | +} |
| 197 | + |
| 198 | +pub fn create_hob( |
| 199 | + buffer: &mut [u8], |
| 200 | + hob_phys: u64, |
| 201 | + entries: &mut [(u64, MemRegionEntry)], |
| 202 | + accepted: &mut [(u64, u64)], |
| 203 | +) -> Result<()> { |
| 204 | + entries.sort_by_key(|(s, _)| *s); |
| 205 | + accepted.sort(); |
| 206 | + |
| 207 | + let Ok((table, mut dst)) = HobHandoffInfoTable::mut_from_prefix(buffer) else { |
| 208 | + return error::InvalidLayout.fail(); |
| 209 | + }; |
| 210 | + |
| 211 | + let mut desc_size = 0; |
| 212 | + create_hob_mem_resources(entries, accepted, |d| { |
| 213 | + desc_size += size_of_val(&d); |
| 214 | + dst.write_all(d.as_bytes()).context(error::WriteHob) |
| 215 | + })?; |
| 216 | + |
| 217 | + let end = HobGenericHeader { |
| 218 | + r#type: HobType::END_OF_HOB_LIST, |
| 219 | + length: size_of::<HobGenericHeader>() as u16, |
| 220 | + reserved: 0, |
| 221 | + }; |
| 222 | + dst.write_all(end.as_bytes()).context(error::WriteHob)?; |
| 223 | + |
| 224 | + *table = HobHandoffInfoTable { |
| 225 | + hdr: HobGenericHeader { |
| 226 | + r#type: HobType::HANDOFF, |
| 227 | + length: size_of::<HobHandoffInfoTable>() as u16, |
| 228 | + reserved: 0, |
| 229 | + }, |
| 230 | + version: HOB_HANDOFF_TABLE_VERSION, |
| 231 | + end_of_hob_list: hob_phys + (size_of_val(table) + desc_size + size_of_val(&end)) as u64, |
| 232 | + ..Default::default() |
| 233 | + }; |
| 234 | + |
| 235 | + Ok(()) |
| 236 | +} |
| 237 | + |
| 238 | +#[cfg(test)] |
| 239 | +#[path = "tdx_test.rs"] |
| 240 | +mod tests; |
0 commit comments