2022-06-10 10:05:39 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2022-06-10 10:05:39 +03:00
|
|
|
#include <AK/Hex.h>
|
|
|
|
#include <AK/StringView.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2022-06-10 10:05:39 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
|
|
|
|
2022-08-05 13:30:49 +03:00
|
|
|
static constexpr StringView format_row = "{:10s}\t{:10s}\t{:10s}\t{:10s}"sv;
|
2022-06-10 10:05:39 +03:00
|
|
|
|
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
|
|
|
TRY(Core::System::unveil("/sys/devices/storage", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("List Storage (Block) devices.");
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
|
|
|
Core::DirIterator di("/sys/devices/storage/", Core::DirIterator::SkipParentAndBaseDir);
|
|
|
|
if (di.has_error()) {
|
2023-03-01 18:55:15 +03:00
|
|
|
auto error = di.error();
|
|
|
|
warnln("Failed to open /sys/devices/storage - {}", error);
|
|
|
|
return error;
|
2022-06-10 10:05:39 +03:00
|
|
|
}
|
|
|
|
|
2022-08-05 13:30:49 +03:00
|
|
|
outln(format_row, "LUN"sv, "Command set"sv, "Block Size"sv, "Last LBA"sv);
|
2022-06-10 10:05:39 +03:00
|
|
|
|
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
|
|
|
|
|
|
|
while (di.has_next()) {
|
|
|
|
auto dir = di.next_path();
|
2023-12-16 17:19:34 +03:00
|
|
|
auto command_set_filename = ByteString::formatted("/sys/devices/storage/{}/command_set", dir);
|
2023-02-09 05:02:46 +03:00
|
|
|
auto command_set_file = Core::File::open(command_set_filename, Core::File::OpenMode::Read);
|
2022-09-14 17:49:14 +03:00
|
|
|
if (command_set_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", command_set_filename, command_set_file.error());
|
2022-06-10 10:05:39 +03:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:19:34 +03:00
|
|
|
auto last_lba_filename = ByteString::formatted("/sys/devices/storage/{}/last_lba", dir);
|
2023-02-09 05:02:46 +03:00
|
|
|
auto last_lba_file = Core::File::open(last_lba_filename, Core::File::OpenMode::Read);
|
2022-09-14 17:49:14 +03:00
|
|
|
if (last_lba_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", last_lba_filename, last_lba_file.error());
|
2022-06-10 10:05:39 +03:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:19:34 +03:00
|
|
|
auto sector_size_filename = ByteString::formatted("/sys/devices/storage/{}/sector_size", dir);
|
2023-02-09 05:02:46 +03:00
|
|
|
auto sector_size_file = Core::File::open(sector_size_filename, Core::File::OpenMode::Read);
|
2022-09-14 17:49:14 +03:00
|
|
|
if (sector_size_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", sector_size_filename, sector_size_file.error());
|
2022-06-10 10:05:39 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-11 19:49:00 +03:00
|
|
|
auto maybe_command_set = command_set_file.value()->read_until_eof();
|
2022-09-14 17:49:14 +03:00
|
|
|
if (maybe_command_set.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", command_set_filename, maybe_command_set.error());
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString command_set = StringView(maybe_command_set.value().bytes());
|
2022-09-14 17:49:14 +03:00
|
|
|
|
2022-12-11 19:49:00 +03:00
|
|
|
auto maybe_last_lba = last_lba_file.value()->read_until_eof();
|
2022-09-14 17:49:14 +03:00
|
|
|
if (maybe_last_lba.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", last_lba_filename, maybe_last_lba.error());
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString last_lba = StringView(maybe_last_lba.value().bytes());
|
2022-09-14 17:49:14 +03:00
|
|
|
|
2022-12-11 19:49:00 +03:00
|
|
|
auto maybe_sector_size = sector_size_file.value()->read_until_eof();
|
2022-09-14 17:49:14 +03:00
|
|
|
if (maybe_sector_size.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", sector_size_filename, maybe_sector_size.error());
|
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString sector_size = StringView(maybe_sector_size.value().bytes());
|
2022-06-10 10:05:39 +03:00
|
|
|
|
2022-08-05 13:30:49 +03:00
|
|
|
outln(format_row, dir, command_set, sector_size, last_lba);
|
2022-06-10 10:05:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|