Skip to content

Commit

Permalink
main, pci: Try booting off all block devices on PCI bus
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rbradford committed Sep 23, 2019
1 parent ddecc33 commit f899ec8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
26 changes: 13 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,19 @@ pub extern "C" fn _start() -> ! {

pci::print_bus();

let mut pci_transport;
let mut mmio_transport;

let mut device = if let Some(pci_device) =
pci::search_bus(VIRTIO_PCI_VENDOR_ID, VIRTIO_PCI_BLOCK_DEVICE_ID)
{
pci_transport = pci::VirtioPciTransport::new(pci_device);
block::VirtioBlockDevice::new(&mut pci_transport)
} else {
mmio_transport = mmio::VirtioMMIOTransport::new(0xd000_0000u64);
block::VirtioBlockDevice::new(&mut mmio_transport)
};

pci::with_devices(
VIRTIO_PCI_VENDOR_ID,
VIRTIO_PCI_BLOCK_DEVICE_ID,
|pci_device| {
let mut pci_transport = pci::VirtioPciTransport::new(pci_device);
block::VirtioBlockDevice::new(&mut pci_transport);
let mut device = block::VirtioBlockDevice::new(&mut pci_transport);
boot_from_device(&mut device)
},
);

let mut mmio_transport = mmio::VirtioMMIOTransport::new(0xd000_0000u64);
let mut device = block::VirtioBlockDevice::new(&mut mmio_transport);
boot_from_device(&mut device);

#[allow(clippy::empty_loop)]
Expand Down
13 changes: 9 additions & 4 deletions src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,19 @@ pub fn print_bus() {
}

#[cfg(not(test))]
pub fn search_bus(target_vendor_id: u16, target_device_id: u16) -> Option<PciDevice> {
pub fn with_devices<F>(target_vendor_id: u16, target_device_id: u16, per_device: F)
where
F: Fn(PciDevice) -> bool,
{
for device in 0..MAX_DEVICES {
let (vendor_id, device_id) = get_device_details(0, device, 0);
if vendor_id == target_vendor_id && device_id == target_device_id {
return Some(PciDevice::new(0, device, 0));
if vendor_id == target_vendor_id
&& device_id == target_device_id
&& per_device(PciDevice::new(0, device, 0))
{
break;
}
}
None
}

#[cfg(not(test))]
Expand Down

0 comments on commit f899ec8

Please sign in to comment.