Align cap_ptr in PCI

This commit is contained in:
Yuke Peng 2025-07-25 15:55:47 +08:00 committed by Tate, Hongliang Tian
parent 48376efa52
commit d60e2e2a6b
2 changed files with 9 additions and 5 deletions

View File

@ -92,16 +92,15 @@ impl Capability {
return Vec::new();
}
let mut capabilities = Vec::new();
let mut cap_ptr =
let mut cap_ptr = PciDeviceLocation::align_ptr(
dev.location()
.read8(PciDeviceCommonCfgOffset::CapabilitiesPointer as u16) as u16
& PciDeviceLocation::BIT32_ALIGN_MASK;
.read8(PciDeviceCommonCfgOffset::CapabilitiesPointer as u16) as u16,
);
let mut cap_ptr_vec = Vec::new();
// read all cap_ptr so that it is easy for us to get the length.
while cap_ptr > 0 {
cap_ptr_vec.push(cap_ptr);
cap_ptr =
dev.location().read8(cap_ptr + 1) as u16 & PciDeviceLocation::BIT32_ALIGN_MASK;
cap_ptr = PciDeviceLocation::align_ptr(dev.location().read8(cap_ptr + 1) as u16);
}
cap_ptr_vec.sort();
// Push here so that we can calculate the length of the last capability.

View File

@ -77,6 +77,11 @@ impl PciDeviceLocation {
impl PciDeviceLocation {
const BIT32_ALIGN_MASK: u16 = 0xFFFC;
/// Aligns the given pointer to a 32-bit boundary.
pub const fn align_ptr(ptr: u16) -> u16 {
ptr & Self::BIT32_ALIGN_MASK
}
/// Reads a 8-bit value from the PCI configuration space at the specified offset.
pub fn read8(&self, offset: u16) -> u8 {
let val = self.read32(offset & Self::BIT32_ALIGN_MASK);