2021-10-08 23:15:07 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2021-11-23 19:16:42 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-10-08 23:15:07 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2023-01-07 23:02:53 +03:00
|
|
|
static ErrorOr<void> fetch_ioctl(int fd, int request)
|
2021-10-08 23:15:07 +03:00
|
|
|
{
|
2022-01-25 21:29:02 +03:00
|
|
|
u64 value;
|
2023-01-07 23:02:53 +03:00
|
|
|
TRY(Core::System::ioctl(fd, request, &value));
|
2021-10-08 23:15:07 +03:00
|
|
|
outln("{}", value);
|
2023-01-07 23:02:53 +03:00
|
|
|
return {};
|
2021-10-08 23:15:07 +03:00
|
|
|
}
|
|
|
|
|
2021-11-23 19:16:42 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-10-08 23:15:07 +03:00
|
|
|
{
|
2021-11-23 19:16:42 +03:00
|
|
|
TRY(Core::System::unveil("/dev", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-11-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-10-08 23:15:07 +03:00
|
|
|
|
2023-01-07 23:03:00 +03:00
|
|
|
StringView device;
|
2021-10-08 23:15:07 +03:00
|
|
|
|
|
|
|
bool flag_get_disk_size = false;
|
2021-10-09 12:23:29 +03:00
|
|
|
bool flag_get_block_size = false;
|
2021-10-08 23:15:07 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Call block device ioctls");
|
|
|
|
args_parser.add_option(flag_get_disk_size, "Get size in bytes", "size", 's');
|
2021-10-09 12:23:29 +03:00
|
|
|
args_parser.add_option(flag_get_block_size, "Get block size in bytes", "block-size", 'b');
|
2021-10-08 23:15:07 +03:00
|
|
|
args_parser.add_positional_argument(device, "Device to query", "device");
|
2021-11-23 19:16:42 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-10-08 23:15:07 +03:00
|
|
|
|
2023-01-07 23:03:00 +03:00
|
|
|
int fd = TRY(Core::System::open(device, O_RDONLY));
|
2021-10-08 23:15:07 +03:00
|
|
|
|
|
|
|
if (flag_get_disk_size) {
|
2023-01-07 23:02:53 +03:00
|
|
|
TRY(fetch_ioctl(fd, STORAGE_DEVICE_GET_SIZE));
|
2021-10-08 23:15:07 +03:00
|
|
|
}
|
2021-10-09 12:23:29 +03:00
|
|
|
if (flag_get_block_size) {
|
2023-01-07 23:02:53 +03:00
|
|
|
TRY(fetch_ioctl(fd, STORAGE_DEVICE_GET_BLOCK_SIZE));
|
2021-10-09 12:23:29 +03:00
|
|
|
}
|
2021-10-08 23:15:07 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|