| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Use of this source code is governed by a MIT-style | |
| // license that can be found in the LICENSE file or at | |
| // https://opensource.org/licenses/MIT | |
| #![cfg(any(test, fuzz))] | |
| extern crate std; | |
| use crate::structures::AcpiSdtHeader; | |
| use crate::{AcpiParserInterface, PhysMemReader}; | |
| use zx_status::Status; | |
| pub struct NullPhysMemReader; | |
| impl PhysMemReader for NullPhysMemReader { | |
| fn phys_to_slice(&self, _phys: usize, _length: usize) -> Result<&[u8], Status> { | |
| Err(Status::OUT_OF_RANGE) | |
| } | |
| } | |
| pub struct EmptyPhysMemReader { | |
| empty_data: std::vec::Vec<u8>, | |
| } | |
| impl EmptyPhysMemReader { | |
| pub fn new() -> Self { | |
| Self { empty_data: std::vec![0; 16 * 1024] } | |
| } | |
| } | |
| impl PhysMemReader for EmptyPhysMemReader { | |
| fn phys_to_slice(&self, _phys: usize, length: usize) -> Result<&[u8], Status> { | |
| if length >= 16 * 1024 { | |
| return Err(Status::OUT_OF_RANGE); | |
| } | |
| Ok(&self.empty_data[..length]) | |
| } | |
| } | |
| #[derive(Clone)] | |
| pub struct FakeRegion<'a> { | |
| pub phys_addr: usize, | |
| pub data: &'a [u8], | |
| } | |
| pub struct FakePhysMemReader<'a> { | |
| rsdp: usize, | |
| regions: std::vec::Vec<FakeRegion<'a>>, | |
| } | |
| impl<'a> FakePhysMemReader<'a> { | |
| pub fn new(rsdp: usize, regions: &[FakeRegion<'a>]) -> Self { | |
| Self { rsdp, regions: regions.to_vec() } | |
| } | |
| pub fn rsdp(&self) -> usize { | |
| self.rsdp | |
| } | |
| pub fn regions(&self) -> &[FakeRegion<'a>] { | |
| &self.regions | |
| } | |
| } | |
| impl<'a> PhysMemReader for FakePhysMemReader<'a> { | |
| fn phys_to_slice(&self, phys: usize, length: usize) -> Result<&[u8], Status> { | |
| for region in &self.regions { | |
| if region.phys_addr == phys && length <= region.data.len() { | |
| return Ok(®ion.data[..length]); | |
| } | |
| } | |
| Err(Status::NOT_FOUND) | |
| } | |
| } | |
| pub struct FakeAcpiParser<'a> { | |
| tables: std::vec::Vec<&'a AcpiSdtHeader>, | |
| } | |
| impl<'a> FakeAcpiParser<'a> { | |
| pub fn new(tables: &[&'a [u8]]) -> Self { | |
| let mut parsed_tables = std::vec::Vec::new(); | |
| for table in tables { | |
| assert!(table.len() >= core::mem::size_of::<AcpiSdtHeader>()); | |
| // SAFETY: We asserted that `table` has at least `size_of::<AcpiSdtHeader>()` bytes. | |
| let r = zerocopy::Ref::<_, AcpiSdtHeader>::from_bytes( | |
| &table[..core::mem::size_of::<AcpiSdtHeader>()], | |
| ) | |
| .unwrap(); | |
| let header = zerocopy::Ref::into_ref(r); | |
| parsed_tables.push(header); | |
| } | |
| Self { tables: parsed_tables } | |
| } | |
| pub fn new_from_headers(tables: &[&'a AcpiSdtHeader]) -> Self { | |
| Self { tables: tables.to_vec() } | |
| } | |
| } | |
| impl<'a> AcpiParserInterface for FakeAcpiParser<'a> { | |
| fn num_tables(&self) -> usize { | |
| self.tables.len() | |
| } | |
| fn get_table_at_index(&self, index: usize) -> Option<&AcpiSdtHeader> { | |
| self.tables.get(index).copied() | |
| } | |
| } | |
| // QEMU Test Data | |
| const QEMU_RSDP: &[u8] = &[ | |
| 0x52, 0x53, 0x44, 0x20, 0x50, 0x54, 0x52, 0x20, 0xaf, 0x42, 0x4f, 0x43, 0x48, 0x53, 0x20, 0x00, | |
| 0x04, 0x22, 0xfe, 0x7f, | |
| ]; | |
| const QEMU_RSDT: &[u8] = &[ | |
| 0x52, 0x53, 0x44, 0x54, 0x34, 0x00, 0x00, 0x00, 0x01, 0x8b, 0x42, 0x4f, 0x43, 0x48, 0x53, 0x20, | |
| 0x42, 0x58, 0x50, 0x43, 0x52, 0x53, 0x44, 0x54, 0x01, 0x00, 0x00, 0x00, 0x42, 0x58, 0x50, 0x43, | |
| 0x01, 0x00, 0x00, 0x00, 0x0c, 0x20, 0xfe, 0x7f, 0x00, 0x21, 0xfe, 0x7f, 0x90, 0x21, 0xfe, 0x7f, | |
| 0xc8, 0x21, 0xfe, 0x7f, | |
| ]; | |
| const QEMU_HPET: &[u8] = &[ | |
| 0x48, 0x50, 0x45, 0x54, 0x38, 0x00, 0x00, 0x00, 0x01, 0x03, 0x42, 0x4f, 0x43, 0x48, 0x53, 0x20, | |
| 0x42, 0x58, 0x50, 0x43, 0x48, 0x50, 0x45, 0x54, 0x01, 0x00, 0x00, 0x00, 0x42, 0x58, 0x50, 0x43, | |
| 0x01, 0x00, 0x00, 0x00, 0x01, 0xa2, 0x86, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0xfe, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| ]; | |
| pub fn qemu_phys_mem_reader() -> FakePhysMemReader<'static> { | |
| FakePhysMemReader::new( | |
| 0xf5860, | |
| &[ | |
| FakeRegion { phys_addr: 0xf5860, data: QEMU_RSDP }, | |
| FakeRegion { phys_addr: 0x7ffe2204, data: QEMU_RSDT }, | |
| FakeRegion { phys_addr: 0x7ffe200c, data: QEMU_HPET }, | |
| ], | |
| ) | |
| } | |
| // Fuchsia Hypervisor Test Data | |
| const FUCHSIA_HYPERVISOR_RSDP: &[u8] = &[ | |
| 0x52, 0x53, 0x44, 0x20, 0x50, 0x54, 0x52, 0x20, 0x0d, 0x5a, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x14, 0x00, 0x0e, 0x00, | |
| ]; | |
| const FUCHSIA_HYPERVISOR_RSDT: &[u8] = &[ | |
| 0x52, 0x53, 0x44, 0x54, 0x30, 0x00, 0x00, 0x00, 0x01, 0x42, 0x5a, 0x58, 0x00, 0x00, 0x00, 0x00, | |
| 0x5a, 0x58, 0x20, 0x41, 0x43, 0x50, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
| 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x0e, 0x00, 0xf2, 0x02, 0x0e, 0x00, 0x4a, 0x03, 0x0e, 0x00, | |
| ]; | |
| pub fn fuchsia_hypervisor_phys_mem_reader() -> FakePhysMemReader<'static> { | |
| FakePhysMemReader::new( | |
| 0xe0000, | |
| &[ | |
| FakeRegion { phys_addr: 0xe0000, data: FUCHSIA_HYPERVISOR_RSDP }, | |
| FakeRegion { phys_addr: 0xe0014, data: FUCHSIA_HYPERVISOR_RSDT }, | |
| ], | |
| ) | |
| } | |
| // Intel NUC Test Data | |
| const INTEL_NUC_RSDP: &[u8] = &[ | |
| 0x52, 0x53, 0x44, 0x20, 0x50, 0x54, 0x52, 0x20, 0x8a, 0x49, 0x4e, 0x54, 0x45, 0x4c, 0x00, 0x02, | |
| 0x28, 0x90, 0xa2, 0x7f, 0x24, 0x00, 0x00, 0x00, 0xc0, 0x90, 0xa2, 0x7f, 0x00, 0x00, 0x00, 0x00, | |
| 0x6b, 0x00, 0x00, 0x00, | |
| ]; | |
| const INTEL_NUC_XSDT: &[u8] = &[ | |
| 0x58, 0x53, 0x44, 0x54, 0x04, 0x01, 0x00, 0x00, 0x01, 0xc8, 0x49, 0x4e, 0x54, 0x45, 0x4c, 0x20, | |
| 0x4e, 0x55, 0x43, 0x37, 0x69, 0x35, 0x44, 0x4e, 0x1f, 0x00, 0x00, 0x00, 0x41, 0x4d, 0x49, 0x20, | |
| 0x13, 0x00, 0x01, 0x00, 0x30, 0x31, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x48, 0x32, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0xd0, 0x32, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x18, 0x33, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0xb8, 0x33, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x33, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x58, 0x37, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x38, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x48, 0x6a, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x80, 0x6a, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0xa8, 0x6a, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x38, 0xbd, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0xe0, 0xce, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x08, 0xd6, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x50, 0xd6, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xee, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x98, 0xee, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xef, 0xa5, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x80, 0xf2, 0xa5, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x88, 0x22, 0xa6, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0xa0, 0x27, 0xa6, 0x7f, 0x00, 0x00, 0x00, 0x00, 0xd8, 0x27, 0xa6, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x30, 0x28, 0xa6, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3b, 0xa6, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x08, 0x3c, 0xa6, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x3c, 0xa6, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, 0x78, 0x3c, 0xa6, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3d, 0xa6, 0x7f, | |
| 0x00, 0x00, 0x00, 0x00, | |
| ]; | |
| const INTEL_NUC_DBG2: &[u8] = &[ | |
| 0x44, 0x42, 0x47, 0x32, 0x54, 0x00, 0x00, 0x00, 0x00, 0x98, 0x49, 0x4E, 0x54, 0x45, 0x4C, 0x20, | |
| 0x4E, 0x55, 0x43, 0x37, 0x69, 0x35, 0x44, 0x4E, 0x1F, 0x00, 0x00, 0x00, 0x4D, 0x53, 0x46, 0x54, | |
| 0x5F, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00, 0x01, | |
| 0x02, 0x00, 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, | |
| 0x22, 0x00, 0x01, 0x08, 0x00, 0x00, 0xF8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, | |
| 0x00, 0x00, 0x2E, 0x00, | |
| ]; | |
| const INTEL_NUC_HPET: &[u8] = &[ | |
| 0x48, 0x50, 0x45, 0x54, 0x38, 0x00, 0x00, 0x00, 0x01, 0xBE, 0x49, 0x4E, 0x54, 0x45, 0x4C, 0x20, | |
| 0x4E, 0x55, 0x43, 0x37, 0x69, 0x35, 0x44, 0x4E, 0x1F, 0x00, 0x00, 0x00, 0x4D, 0x53, 0x46, 0x54, | |
| 0x5F, 0x00, 0x00, 0x00, 0x01, 0xA2, 0x86, 0x80, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0xD0, 0xFE, | |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, | |
| ]; | |
| pub fn intel_nuc7i5dn_phys_mem_reader() -> FakePhysMemReader<'static> { | |
| FakePhysMemReader::new( | |
| 0x7fa29000, | |
| &[ | |
| FakeRegion { phys_addr: 0x7fa29000, data: INTEL_NUC_RSDP }, | |
| FakeRegion { phys_addr: 0x7fa290c0, data: INTEL_NUC_XSDT }, | |
| FakeRegion { phys_addr: 0x7fa62830, data: INTEL_NUC_DBG2 }, | |
| FakeRegion { phys_addr: 0x7fa63d18, data: INTEL_NUC_HPET }, | |
| ], | |
| ) | |
| } | |
| // Parser helpers | |
| use crate::test_data::*; | |
| pub fn pixelbook_eve_acpi_parser() -> FakeAcpiParser<'static> { | |
| FakeAcpiParser::new(&[K_EVE_MADT_TABLE_DATA, K_EVE_HPET_TABLE_DATA]) | |
| } | |
| pub fn pixelbook_atlas_acpi_parser() -> FakeAcpiParser<'static> { | |
| FakeAcpiParser::new(&[K_ATLAS_DBG2_TEST_DATA]) | |
| } | |
| pub fn z840_acpi_parser() -> FakeAcpiParser<'static> { | |
| FakeAcpiParser::new(&[K_Z840_MADT_TABLE_DATA, K_Z840_SRAT_TABLE_DATA]) | |
| } | |
| pub fn sys2970wx_acpi_parser() -> FakeAcpiParser<'static> { | |
| FakeAcpiParser::new(&[K2970WX_MADT_TABLE_DATA, K2970WX_SRAT_TABLE_DATA]) | |
| } |