Serenity Operating System
at portability 108 lines 3.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/JsonArray.h> 28#include <AK/JsonObject.h> 29#include <AK/String.h> 30#include <LibCore/File.h> 31#include <LibPCIDB/Database.h> 32#include <stdio.h> 33 34int main(int argc, char** argv) 35{ 36 UNUSED_PARAM(argc); 37 UNUSED_PARAM(argv); 38 39 if (pledge("stdio rpath", nullptr) < 0) { 40 perror("pledge"); 41 return 1; 42 } 43 44 if (unveil("/res/pci.ids", "r") < 0) { 45 perror("unveil"); 46 return 1; 47 } 48 49 if (unveil("/proc/pci", "r") < 0) { 50 perror("unveil"); 51 return 1; 52 } 53 54 unveil(nullptr, nullptr); 55 56 auto db = PCIDB::Database::open(); 57 if (!db) 58 fprintf(stderr, "Couldn't open PCI ID database\n"); 59 60 auto proc_pci = Core::File::construct("/proc/pci"); 61 if (!proc_pci->open(Core::IODevice::ReadOnly)) { 62 fprintf(stderr, "Error: %s\n", proc_pci->error_string()); 63 return 1; 64 } 65 66 if (pledge("stdio", nullptr) < 0) { 67 perror("pledge"); 68 return 1; 69 } 70 71 auto file_contents = proc_pci->read_all(); 72 auto json = JsonValue::from_string(file_contents).as_array(); 73 json.for_each([db](auto& value) { 74 auto dev = value.as_object(); 75 auto seg = dev.get("seg").to_u32(); 76 auto bus = dev.get("bus").to_u32(); 77 auto slot = dev.get("slot").to_u32(); 78 auto function = dev.get("function").to_u32(); 79 auto vendor_id = dev.get("vendor_id").to_u32(); 80 auto device_id = dev.get("device_id").to_u32(); 81 auto revision_id = dev.get("revision_id").to_u32(); 82 auto class_id = dev.get("class").to_u32(); 83 84 String vendor_name; 85 String device_name; 86 String class_name; 87 88 if (db) { 89 vendor_name = db->get_vendor(vendor_id); 90 device_name = db->get_device(vendor_id, device_id); 91 class_name = db->get_class(class_id); 92 } 93 94 if (vendor_name.is_empty()) 95 vendor_name = String::format("%02x", vendor_id); 96 if (device_name.is_empty()) 97 device_name = String::format("%02x", device_id); 98 if (class_name.is_empty()) 99 class_name = String::format("%04x", class_id); 100 101 printf("%04x:%02x:%02x.%d %s: %s %s (rev %02x)\n", 102 seg, bus, slot, function, 103 class_name.characters(), vendor_name.characters(), 104 device_name.characters(), revision_id); 105 }); 106 107 return 0; 108}