fdtdump.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedString.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibCore/MappedFile.h>
  9. #include <LibCore/System.h>
  10. #include <LibDeviceTree/FlattenedDeviceTree.h>
  11. #include <LibDeviceTree/Validation.h>
  12. #include <LibMain/Main.h>
  13. ErrorOr<int> serenity_main(Main::Arguments arguments)
  14. {
  15. TRY(Core::System::pledge("stdio rpath"));
  16. DeprecatedString filename;
  17. Core::ArgsParser args;
  18. args.add_positional_argument(filename, "File to process", "file", Core::ArgsParser::Required::Yes);
  19. args.parse(arguments);
  20. // FIXME: Figure out how to do this sanely from stdin
  21. auto file = TRY(Core::MappedFile::map(filename));
  22. if (file->size() < sizeof(DeviceTree::FlattenedDeviceTreeHeader)) {
  23. warnln("Not enough data in {} to contain a device tree header!", filename);
  24. return 1;
  25. }
  26. auto const* fdt_header = reinterpret_cast<DeviceTree::FlattenedDeviceTreeHeader const*>(file->data());
  27. auto bytes = file->bytes();
  28. TRY(DeviceTree::dump(*fdt_header, bytes));
  29. auto compatible = TRY(DeviceTree::slow_get_property<StringView>("/compatible"sv, *fdt_header, bytes));
  30. auto compatible_strings = compatible.split_view('\0');
  31. dbgln("compatible with: {}", compatible_strings);
  32. auto bootargs = TRY(DeviceTree::slow_get_property<StringView>("/chosen/bootargs"sv, *fdt_header, bytes));
  33. dbgln("bootargs: {}", bootargs);
  34. auto cpu_compatible = TRY(DeviceTree::slow_get_property<StringView>("/cpus/cpu@0/compatible"sv, *fdt_header, bytes));
  35. dbgln("cpu0 compatible: {}", cpu_compatible);
  36. return 0;
  37. }