mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:07:35 +00:00
Userland: Use LibPCIDB in lspci to print device names
This commit is contained in:
parent
37a2c03655
commit
c7040cee62
1 changed files with 28 additions and 8 deletions
|
@ -2,6 +2,7 @@
|
||||||
#include <AK/JsonArray.h>
|
#include <AK/JsonArray.h>
|
||||||
#include <AK/JsonObject.h>
|
#include <AK/JsonObject.h>
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
|
#include <LibPCIDB/Database.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
|
@ -9,15 +10,19 @@ int main(int argc, char** argv)
|
||||||
UNUSED_PARAM(argc);
|
UNUSED_PARAM(argc);
|
||||||
UNUSED_PARAM(argv);
|
UNUSED_PARAM(argv);
|
||||||
|
|
||||||
CFile file("/proc/pci");
|
auto db = PCIDB::Database::open();
|
||||||
if (!file.open(CIODevice::ReadOnly)) {
|
if (!db)
|
||||||
fprintf(stderr, "Error: %s\n", file.error_string());
|
fprintf(stderr, "Couldn't open PCI ID database\n");
|
||||||
|
|
||||||
|
CFile proc_pci("/proc/pci");
|
||||||
|
if (!proc_pci.open(CIODevice::ReadOnly)) {
|
||||||
|
fprintf(stderr, "Error: %s\n", proc_pci.error_string());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto file_contents = file.read_all();
|
auto file_contents = proc_pci.read_all();
|
||||||
auto json = JsonValue::from_string(file_contents).as_array();
|
auto json = JsonValue::from_string(file_contents).as_array();
|
||||||
json.for_each([](auto& value) {
|
json.for_each([db](auto& value) {
|
||||||
auto dev = value.as_object();
|
auto dev = value.as_object();
|
||||||
|
|
||||||
auto bus = dev.get("bus").to_u32();
|
auto bus = dev.get("bus").to_u32();
|
||||||
|
@ -28,10 +33,25 @@ int main(int argc, char** argv)
|
||||||
auto revision_id = dev.get("revision_id").to_u32();
|
auto revision_id = dev.get("revision_id").to_u32();
|
||||||
auto class_id = dev.get("class").to_u32();
|
auto class_id = dev.get("class").to_u32();
|
||||||
|
|
||||||
printf("%02x:%02x.%d %04x: %04x:%04x (rev %02x)\n",
|
String vendor_name = String::format("%02x", vendor_id);
|
||||||
|
auto vendor = db->get_vendor(vendor_id);
|
||||||
|
if (vendor != "")
|
||||||
|
vendor_name = vendor;
|
||||||
|
|
||||||
|
String device_name = String::format("%02x", device_id);
|
||||||
|
auto device = db->get_device(vendor_id, device_id);
|
||||||
|
if (device != "")
|
||||||
|
device_name = device;
|
||||||
|
|
||||||
|
String class_name = String::format("%04x", class_id);
|
||||||
|
auto class_ptr = db->get_class(class_id);
|
||||||
|
if (class_ptr != "")
|
||||||
|
class_name = class_ptr;
|
||||||
|
|
||||||
|
printf("%02x:%02x.%d %s: %s %s (rev %02x)\n",
|
||||||
bus, slot, function,
|
bus, slot, function,
|
||||||
class_id, vendor_id, device_id, revision_id
|
class_name.characters(), vendor_name.characters(),
|
||||||
);
|
device_name.characters(), revision_id);
|
||||||
});
|
});
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue