ladybird/Userland/Utilities/lspci.cpp
Liav A 25ea7461a0 Kernel/PCI: Simplify the entire subsystem
A couple of things were changed:
1. Semantic changes - PCI segments are now called PCI domains, to better
match what they are really. It's also the name that Linux gave, and it
seems that Wikipedia also uses this name.
We also remove PCI::ChangeableAddress, because it was used in the past
but now it's no longer being used.
2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as
they made a bunch of unnecessary complexity. Instead, Windowed access is
removed entirely (this was tested, but never was benchmarked), so we are
left with IO access and memory access options. The memory access option
is essentially mapping the PCI bus (from the chosen PCI domain), to
virtual memory as-is. This means that unless needed, at any time, there
is only one PCI bus being mapped, and this is changed if access to
another PCI bus in the same PCI domain is needed. For now, we don't
support mapping of different PCI buses from different PCI domains at the
same time, because basically it's still a non-issue for most machines
out there.
2. OOM-safety is increased, especially when constructing the Access
object. It means that we pre-allocating any needed resources, and we try
to find PCI domains (if requested to initialize memory access) after we
attempt to construct the Access object, so it's possible to fail at this
point "gracefully".
3. All PCI API functions are now separated into a different header file,
which means only "clients" of the PCI subsystem API will need to include
that header file.
4. Functional changes - we only allow now to enumerate the bus after
a hardware scan. This means that the old method "enumerate_hardware"
is removed, so, when initializing an Access object, the initializing
function must call rescan on it to force it to find devices. This makes
it possible to fail rescan, and also to defer it after construction from
both OOM-safety terms and hotplug capabilities.
2021-09-07 13:47:37 +02:00

105 lines
3.1 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteBuffer.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibPCIDB/Database.h>
#include <stdio.h>
#include <unistd.h>
static bool flag_show_numerical = false;
static const char* format_numerical = "{:04x}:{:02x}:{:02x}.{} {}: {}:{} (rev {:02x})";
static const char* format_textual = "{:04x}:{:02x}:{:02x}.{} {}: {} {} (rev {:02x})";
int main(int argc, char** argv)
{
if (pledge("stdio rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
if (unveil("/res/pci.ids", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil("/proc/pci", "r") < 0) {
perror("unveil");
return 1;
}
unveil(nullptr, nullptr);
Core::ArgsParser args_parser;
args_parser.set_general_help("List PCI devices.");
args_parser.add_option(flag_show_numerical, "Show numerical IDs", "numerical", 'n');
args_parser.parse(argc, argv);
const char* format = flag_show_numerical ? format_numerical : format_textual;
RefPtr<PCIDB::Database> db;
if (!flag_show_numerical) {
db = PCIDB::Database::open();
if (!db) {
warnln("Couldn't open PCI ID database");
flag_show_numerical = true;
}
}
auto proc_pci = Core::File::construct("/proc/pci");
if (!proc_pci->open(Core::OpenMode::ReadOnly)) {
warnln("Failed to open {}: {}", proc_pci->name(), proc_pci->error_string());
return 1;
}
if (pledge("stdio", nullptr) < 0) {
perror("pledge");
return 1;
}
auto file_contents = proc_pci->read_all();
auto json = JsonValue::from_string(file_contents);
VERIFY(json.has_value());
json.value().as_array().for_each([db, format](auto& value) {
auto& dev = value.as_object();
auto domain = dev.get("domain").to_u32();
auto bus = dev.get("bus").to_u32();
auto device = dev.get("device").to_u32();
auto function = dev.get("function").to_u32();
auto vendor_id = dev.get("vendor_id").to_u32();
auto device_id = dev.get("device_id").to_u32();
auto revision_id = dev.get("revision_id").to_u32();
auto class_id = dev.get("class").to_u32();
auto subclass_id = dev.get("subclass").to_u32();
String vendor_name;
String device_name;
String class_name;
if (db) {
vendor_name = db->get_vendor(vendor_id);
device_name = db->get_device(vendor_id, device_id);
class_name = db->get_class(class_id);
}
if (vendor_name.is_empty())
vendor_name = String::formatted("{:04x}", vendor_id);
if (device_name.is_empty())
device_name = String::formatted("{:04x}", device_id);
if (class_name.is_empty())
class_name = String::formatted("{:02x}{:02x}", class_id, subclass_id);
outln(format, domain, bus, device, function, class_name, vendor_name, device_name, revision_id);
});
return 0;
}