Serenity Operating System
1/*
2 * Copyright (c) 2021, David Isaksson <davidisaksson93@gmail.com>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibCore/ArgsParser.h>
8#include <LibCore/System.h>
9#include <LibMain/Main.h>
10#include <fcntl.h>
11#include <sys/ioctl.h>
12
13static ErrorOr<void> fetch_ioctl(int fd, int request)
14{
15 u64 value;
16 TRY(Core::System::ioctl(fd, request, &value));
17 outln("{}", value);
18 return {};
19}
20
21ErrorOr<int> serenity_main(Main::Arguments arguments)
22{
23 TRY(Core::System::unveil("/dev", "r"));
24 TRY(Core::System::unveil(nullptr, nullptr));
25 TRY(Core::System::pledge("stdio rpath"));
26
27 StringView device;
28
29 bool flag_get_disk_size = false;
30 bool flag_get_block_size = false;
31
32 Core::ArgsParser args_parser;
33 args_parser.set_general_help("Call block device ioctls");
34 args_parser.add_option(flag_get_disk_size, "Get size in bytes", "size", 's');
35 args_parser.add_option(flag_get_block_size, "Get block size in bytes", "block-size", 'b');
36 args_parser.add_positional_argument(device, "Device to query", "device");
37 args_parser.parse(arguments);
38
39 int fd = TRY(Core::System::open(device, O_RDONLY));
40
41 if (flag_get_disk_size) {
42 TRY(fetch_ioctl(fd, STORAGE_DEVICE_GET_SIZE));
43 }
44 if (flag_get_block_size) {
45 TRY(fetch_ioctl(fd, STORAGE_DEVICE_GET_BLOCK_SIZE));
46 }
47
48 return 0;
49}