ovmf_x86_64.rs0.00%
1
// Copyright 2026 Google LLC2
//3
// Licensed under the Apache License, Version 2.0 (the "License");4
// you may not use this file except in compliance with the License.5
// You may obtain a copy of the License at6
//7
// https://www.apache.org/licenses/LICENSE-2.08
//9
// Unless required by applicable law or agreed to in writing, software10
// distributed under the License is distributed on an "AS IS" BASIS,11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12
// See the License for the specific language governing permissions and13
// limitations under the License.14
15
pub mod sev;16
pub mod tdx;17
18
use zerocopy::FromBytes;19
20
pub const GUID_SIZE: usize = 16;21
22
pub const OFFSET_R_LENGTH_GUID_TABLE: usize = 50;23
pub const GUID_TABLE_FOOTER: [u8; GUID_SIZE] = [24
0xDE, 0x82, 0xB5, 0x96, 0xB2, 0x1F, 0xF7, 0x45, 0xBA, 0xEA, 0xA3, 0x66, 0xC5, 0x5A, 0x08, 0x2D,25
];26
27
pub fn parse_data<'a>(blob: &'a [u8], target: &[u8; GUID_SIZE]) -> Option<&'a [u8]> {28
let offset_table_len = blob.len().checked_sub(OFFSET_R_LENGTH_GUID_TABLE)?;29
// `table_len` is the total length of the table, including the footer30
let (table_len, guid) = u16::read_from_prefix(&blob[offset_table_len..]).ok()?;31
if !guid.starts_with(&GUID_TABLE_FOOTER) {32
return None;33
}34
let body_len = (table_len as usize).checked_sub(size_of::<u16>() + GUID_SIZE)?;35
let offset_table_start = offset_table_len.checked_sub(body_len)?;36
// Every entry in the table has the following structure:37
// - Actual entry content38
// - size_of::<u16>() bytes for the length of the entry39
// - GUID_SIZE bytes for the GUID40
let mut offset_entry_len = offset_table_len.checked_sub(GUID_SIZE + size_of::<u16>())?;41
while offset_entry_len >= offset_table_start {42
let (len_entry, guid) = u16::read_from_prefix(&blob[offset_entry_len..]).ok()?;43
if guid.starts_with(target) {44
let len_content = (len_entry as usize).checked_sub(GUID_SIZE + size_of::<u16>())?;45
let offset_content = offset_entry_len.checked_sub(len_content)?;46
return Some(&blob[offset_content..offset_entry_len]);47
}48
offset_entry_len = offset_entry_len.checked_sub(len_entry as usize)?;49
}50
None51
}52