Skip to main content

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    /// format: [[<bus>]:][slot][.[<func>]]    Show only devices in selected slots
25    pub filter: Option<filter::Filter>,
26
27    #[argh(switch, short = 'v')]
28    /// print verbose device configuration
29    pub verbose: bool,
30
31    #[argh(switch, short = 'q')]
32    /// don't print errors found trying to parse the database
33    pub quiet: bool,
34
35    #[argh(switch, short = 'x')]
36    /// dump raw configuration space
37    pub print_config: bool,
38
39    #[argh(switch, short = 'n')]
40    /// print numeric IDs.
41    pub print_numeric: bool,
42
43    #[argh(switch, short = 'N')]
44    /// only print numeric IDs.
45    pub only_print_numeric: bool,
46
47    #[argh(subcommand)]
48    pub command: Option<SubCommand>,
49}
50
51#[derive(Copy, Clone, FromArgs, PartialEq, Debug)]
52#[argh(subcommand)]
53pub enum SubCommand {
54    Buses(BusesCommand),
55    Read(ReadBarCommand),
56}
57
58/// List PCI buses found in the system.
59#[derive(Copy, Clone, FromArgs, PartialEq, Default, Debug)]
60#[argh(subcommand, name = "buses")]
61pub struct BusesCommand {}
62
63/// Read from an MMIO BAR of a specified device.
64/// For example, to read from BAR 2 of device at address 00:01.0:
65///   lspci read 00:01.0 2
66#[derive(Copy, Clone, FromArgs, PartialEq, Default, Debug)]
67#[argh(subcommand, name = "read")]
68pub struct ReadBarCommand {
69    /// device address in BDF format BB:DD.F.
70    #[argh(positional)]
71    pub device: filter::Filter,
72    /// BAR id to read from.
73    #[argh(positional)]
74    pub bar_id: u8,
75    /// offset into the BAR to read [default = 0x0].
76    #[argh(option, short = 'o', default = "0", from_str_fn(u64_from_maybe_hex_str))]
77    pub offset: u64,
78    /// how much to read [default = 0x80].
79    #[argh(option, short = 's', default = "128", from_str_fn(u64_from_maybe_hex_str))]
80    pub size: u64,
81    /// print verbose read information
82    #[argh(switch, short = 'v')]
83    pub verbose: bool,
84}