lspci.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <AK/ByteBuffer.h>
  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. int main(int argc, char** argv)
  34. {
  35. UNUSED_PARAM(argc);
  36. UNUSED_PARAM(argv);
  37. if (pledge("stdio rpath", nullptr) < 0) {
  38. perror("pledge");
  39. return 1;
  40. }
  41. if (unveil("/res/pci.ids", "r") < 0) {
  42. perror("unveil");
  43. return 1;
  44. }
  45. if (unveil("/proc/pci", "r") < 0) {
  46. perror("unveil");
  47. return 1;
  48. }
  49. unveil(nullptr, nullptr);
  50. auto db = PCIDB::Database::open();
  51. if (!db)
  52. warnln("Couldn't open PCI ID database");
  53. auto proc_pci = Core::File::construct("/proc/pci");
  54. if (!proc_pci->open(Core::IODevice::ReadOnly)) {
  55. fprintf(stderr, "Error: %s\n", proc_pci->error_string());
  56. return 1;
  57. }
  58. if (pledge("stdio", nullptr) < 0) {
  59. perror("pledge");
  60. return 1;
  61. }
  62. auto file_contents = proc_pci->read_all();
  63. auto json = JsonValue::from_string(file_contents);
  64. ASSERT(json.has_value());
  65. json.value().as_array().for_each([db](auto& value) {
  66. auto dev = value.as_object();
  67. auto seg = dev.get("seg").to_u32();
  68. auto bus = dev.get("bus").to_u32();
  69. auto slot = dev.get("slot").to_u32();
  70. auto function = dev.get("function").to_u32();
  71. auto vendor_id = dev.get("vendor_id").to_u32();
  72. auto device_id = dev.get("device_id").to_u32();
  73. auto revision_id = dev.get("revision_id").to_u32();
  74. auto class_id = dev.get("class").to_u32();
  75. String vendor_name;
  76. String device_name;
  77. String class_name;
  78. if (db) {
  79. vendor_name = db->get_vendor(vendor_id);
  80. device_name = db->get_device(vendor_id, device_id);
  81. class_name = db->get_class(class_id);
  82. }
  83. if (vendor_name.is_empty())
  84. vendor_name = String::format("%02x", vendor_id);
  85. if (device_name.is_empty())
  86. device_name = String::format("%02x", device_id);
  87. if (class_name.is_empty())
  88. class_name = String::format("%04x", class_id);
  89. outln("{:04x}:{:02x}:{:02x}.{} {}: {} {} (rev {:02x})", seg, bus, slot, function, class_name, vendor_name, device_name, revision_id);
  90. });
  91. return 0;
  92. }