From 9f683b843678048d6008bf26f3e6cdef085258db Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 5 Apr 2023 21:25:16 +0000 Subject: [PATCH] lspci: add -x. I caught someone using pciutils lspci, asked why, and -x (including -xxx) and -v (specifically -vvv) were the reasons. I took a quick look, and -x in particular seemed easy (modulo the TODO I've left in the code). I notice that half the pci devices on my machine are missing from the toybox lspci output versus the pciutils lspci --- it looks like scan_uevent() is returning 2 for the bridges? --- toys/other/lsusb.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/toys/other/lsusb.c b/toys/other/lsusb.c index 3ca7703a..c4f64529 100644 --- a/toys/other/lsusb.c +++ b/toys/other/lsusb.c @@ -4,7 +4,7 @@ * Copyright 2013 Isaac Dunham USE_LSUSB(NEWTOY(lsusb, "i:", TOYFLAG_USR|TOYFLAG_BIN)) -USE_LSPCI(NEWTOY(lspci, "emkn@i:", TOYFLAG_USR|TOYFLAG_BIN)) +USE_LSPCI(NEWTOY(lspci, "emkn@x@i:", TOYFLAG_USR|TOYFLAG_BIN)) config LSPCI bool "lspci" @@ -14,11 +14,12 @@ config LSPCI List PCI devices. - -e Extended (6 digit) class - -i ID database (default /etc/pci.ids[.gz]) - -k Show kernel driver - -m Machine readable - -n Numeric output (-nn for both) + -e Extended (6 digit) class + -i ID database (default /etc/pci.ids[.gz]) + -k Show kernel driver + -m Machine readable + -n Numeric output (-nn for both) + -x Hex dump of config space (64 bytes; -xxx for 256, -xxxx for 4096) config LSUSB bool "lsusb" @@ -36,7 +37,7 @@ config LSUSB GLOBALS( char *i; - long n; + long x, n; void *ids, *class; int count; @@ -229,6 +230,24 @@ static int list_pci(struct dirtree *new) if (FLAG(k)) printf(FLAG(m) ? " \"%s\"" : " %s", driver); xputc('\n'); + if (TT.x) { + FILE *fp; + int b, col = 0, max = (TT.x >= 4) ? 4096 : ((TT.x >= 3) ? 256 : 64); + + // TODO: where does the "0000:" come from? + snprintf(toybuf, sizeof(toybuf), "/sys/bus/pci/devices/0000:%s/config", + new->name+5); + fp = xfopen(toybuf, "r"); + while ((b = fgetc(fp)) != EOF) { + if ((col % 16) == 0) printf("%02x: ", col & 0xf0); + printf("%02x ", (b & 0xff)); + if ((++col % 16) == 0) xputc('\n'); + if (col == max) break; + } + xputc('\n'); + fclose(fp); + } + return 0; } -- 2.39.2