Skip to content

Commit f899ec8

Browse files
committed
main, pci: Try booting off all block devices on PCI bus
This requires a refactoring of the PCI device detection to take a closure to invoke over all the devices that match the vendor/device pair. Fixes: #16 Signed-off-by: Rob Bradford <robert.bradford@intel.com>
1 parent ddecc33 commit f899ec8

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/main.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,19 @@ pub extern "C" fn _start() -> ! {
172172

173173
pci::print_bus();
174174

175-
let mut pci_transport;
176-
let mut mmio_transport;
177-
178-
let mut device = if let Some(pci_device) =
179-
pci::search_bus(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_BLOCK_DEVICE_ID)
180-
{
181-
pci_transport = pci::VirtioPciTransport::new(pci_device);
182-
block::VirtioBlockDevice::new(&mut pci_transport)
183-
} else {
184-
mmio_transport = mmio::VirtioMMIOTransport::new(0xd000_0000u64);
185-
block::VirtioBlockDevice::new(&mut mmio_transport)
186-
};
187-
175+
pci::with_devices(
176+
VIRTIO_PCI_VENDOR_ID,
177+
VIRTIO_PCI_BLOCK_DEVICE_ID,
178+
|pci_device| {
179+
let mut pci_transport = pci::VirtioPciTransport::new(pci_device);
180+
block::VirtioBlockDevice::new(&mut pci_transport);
181+
let mut device = block::VirtioBlockDevice::new(&mut pci_transport);
182+
boot_from_device(&mut device)
183+
},
184+
);
185+
186+
let mut mmio_transport = mmio::VirtioMMIOTransport::new(0xd000_0000u64);
187+
let mut device = block::VirtioBlockDevice::new(&mut mmio_transport);
188188
boot_from_device(&mut device);
189189

190190
#[allow(clippy::empty_loop)]

src/pci.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,19 @@ pub fn print_bus() {
9090
}
9191

9292
#[cfg(not(test))]
93-
pub fn search_bus(target_vendor_id: u16, target_device_id: u16) -> Option<PciDevice> {
93+
pub fn with_devices<F>(target_vendor_id: u16, target_device_id: u16, per_device: F)
94+
where
95+
F: Fn(PciDevice) -> bool,
96+
{
9497
for device in 0..MAX_DEVICES {
9598
let (vendor_id, device_id) = get_device_details(0, device, 0);
96-
if vendor_id == target_vendor_id && device_id == target_device_id {
97-
return Some(PciDevice::new(0, device, 0));
99+
if vendor_id == target_vendor_id
100+
&& device_id == target_device_id
101+
&& per_device(PciDevice::new(0, device, 0))
102+
{
103+
break;
98104
}
99105
}
100-
None
101106
}
102107

103108
#[cfg(not(test))]

0 commit comments

Comments
 (0)