lspci/
lib.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5pub mod bridge;
6pub mod capability;
7pub mod config;
8pub mod db;
9pub mod device;
10pub mod filter;
11pub mod util;
12
13fn u64_from_maybe_hex_str(s: &str) -> Result<u64, String> {
14    let (s, radix) = if let Some(s) = s.strip_prefix("0x") { (s, 16) } else { (s, 10) };
15
16    u64::from_str_radix(&s, radix).map_err(|e| e.to_string())
17}
18
19use argh::FromArgs;
20#[derive(FromArgs, Default)]
21/// Display PCI information
22pub struct Args {
23    #[argh(positional)]
24    /// [[<bus>]:][slot][.[<func>]]    Show only devices in selected slots
25    pub filter: Option<filter::Filter>,
26
27    #[argh(option, default = "String::from(\"/dev/sys/platform/pt/\")")]
28    /// path to the parent directory of the fuchsia.hardware.pci service
29    pub service: String,
30
31    #[argh(switch, short = 'v')]
32    /// print verbose device configuration
33    pub verbose: bool,
34
35    #[argh(switch, short = 'q')]
36    /// don't print errors found trying to parse the database
37    pub quiet: bool,
38
39    #[argh(switch, short = 'x')]
40    /// dump raw configuration space
41    pub print_config: bool,
42
43    #[argh(switch, short = 'n')]
44    /// print numeric IDs.
45    pub print_numeric: bool,
46
47    #[argh(switch, short = 'N')]
48    /// only print numeric IDs.
49    pub only_print_numeric: bool,
50
51    #[argh(subcommand)]
52    pub command: Option<SubCommand>,
53}
54
55#[derive(Copy, Clone, FromArgs, PartialEq, Debug)]
56#[argh(subcommand)]
57pub enum SubCommand {
58    Buses(BusesCommand),
59    Read(ReadBarCommand),
60}
61
62/// List PCI buses found in the system.
63#[derive(Copy, Clone, FromArgs, PartialEq, Default, Debug)]
64#[argh(subcommand, name = "buses")]
65pub struct BusesCommand {}
66
67/// Read from an MMIO BAR of a specified device.
68/// For example, to read from BAR 2 of device at address 00:01.0:
69///   lspci read 00:01.0 2
70#[derive(Copy, Clone, FromArgs, PartialEq, Default, Debug)]
71#[argh(subcommand, name = "read")]
72pub struct ReadBarCommand {
73    /// device address in BDF format BB:DD.F.
74    #[argh(positional)]
75    pub device: filter::Filter,
76    /// BAR id to read from.
77    #[argh(positional)]
78    pub bar_id: u8,
79    /// offset into the BAR to read [default = 0x0].
80    #[argh(option, short = 'o', default = "0", from_str_fn(u64_from_maybe_hex_str))]
81    pub offset: u64,
82    /// how much to read [default = 0x80].
83    #[argh(option, short = 's', default = "128", from_str_fn(u64_from_maybe_hex_str))]
84    pub size: u64,
85    /// print verbose read information
86    #[argh(switch, short = 'v')]
87    pub verbose: bool,
88}