Add PCI device enumeration and configuration utilities

This commit introduces a new PCI library and associated tools for inspecting and modifying PCI configuration space via Linux sysfs. The following changes were made:

- Implemented `pci.c` and `pci.h` for PCI device management, including enumeration, attribute reading, and configuration space access.
- Added `setpci` command-line utility for reading and writing PCI configuration registers, with support for filtering devices by vendor, device, and slot.
- Created manual pages for `setpci` to document its usage and options.
- Developed test scripts for both `lspci` and `setpci` to ensure functionality and correctness of the new features.

These additions enhance the ability to interact with PCI devices, providing a robust framework for device management and configuration.
This commit is contained in:
2026-03-22 19:04:16 -05:00
parent e2451c3952
commit 0db2ec12c5
10 changed files with 1562 additions and 3 deletions
+48
View File
@@ -0,0 +1,48 @@
#!/bin/sh
_test_name="lspci"
. "$(dirname "$0")/harness.sh"
setup_tmpdir
LSPCI="$VX lspci"
echo "Running lspci tests..."
dev_path="/sys/bus/pci/devices/$(ls /sys/bus/pci/devices | head -n 1)"
slot="$(basename "$dev_path")"
short_slot="$(printf '%s' "$slot" | sed 's/^0000://')"
vendor="$(sed 's/^0x//' "$dev_path/vendor")"
device="$(sed 's/^0x//' "$dev_path/device")"
class="$(sed 's/^0x//' "$dev_path/class" | cut -c1-4)"
out="$($LSPCI | sed -n '1p')"
if [ -n "$out" ]; then
pass "basic: lists at least one device"
else
fail "basic: lists at least one device" "got empty output"
fi
out="$($LSPCI -D -s "$slot")"
case "$out" in
"$slot "*) pass "filter: -D preserves domain-qualified slot" ;;
*) fail "filter: -D preserves domain-qualified slot" "got: $out" ;;
esac
out="$($LSPCI -n -s "$short_slot")"
expected="$short_slot $class: $vendor:$device"
case "$out" in
"$expected"*) pass "numeric: -n shows class and ids" ;;
*) fail "numeric: -n shows class and ids" "expected prefix: $expected, got: $out" ;;
esac
out="$($LSPCI -d "$vendor:$device" | sed -n '1p')"
case "$out" in
*"$vendor"*|*"$device"*) pass "filter: -d finds matching device" ;;
"") fail "filter: -d finds matching device" "got empty output" ;;
*) pass "filter: -d finds matching device" ;;
esac
hex_out="$($LSPCI -x -s "$short_slot")"
printf '%s\n' "$hex_out" > hex.out
check_grep " 00:" hex.out "hex: emits config dump"
summary
+33
View File
@@ -0,0 +1,33 @@
#!/bin/sh
_test_name="setpci"
. "$(dirname "$0")/harness.sh"
setup_tmpdir
SETPCI="$VX setpci"
echo "Running setpci tests..."
dev_path="/sys/bus/pci/devices/$(ls /sys/bus/pci/devices | head -n 1)"
slot="$(basename "$dev_path")"
short_slot="$(printf '%s' "$slot" | sed 's/^0000://')"
vendor="$(sed 's/^0x//' "$dev_path/vendor")"
device="$(sed 's/^0x//' "$dev_path/device")"
command_reg="$(od -An -tx2 -N2 -j4 "$dev_path/config" | tr -d ' \n')"
$SETPCI --dumpregs > dumpregs.out
check_grep "VENDOR_ID" dumpregs.out "dumpregs: lists standard names"
out="$($SETPCI -s "$short_slot" VENDOR_ID)"
check_eq "$out" "$vendor" "read: VENDOR_ID matches sysfs"
out="$($SETPCI -s "$short_slot" DEVICE_ID)"
check_eq "$out" "$device" "read: DEVICE_ID matches sysfs"
out="$($SETPCI -s "$short_slot" 04.w)"
check_eq "$out" "$command_reg" "read: numeric register syntax works"
check_exit "dry-run: masked write succeeds" 0 $SETPCI -D -s "$short_slot" COMMAND="$command_reg:ffff"
after="$($SETPCI -s "$short_slot" COMMAND)"
check_eq "$after" "$command_reg" "dry-run: command register unchanged"
summary