2010-09-06 17:05:58 +05:30
|
|
|
/*
|
|
|
|
* blockdev implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com>
|
|
|
|
*
|
2010-09-06 21:11:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2010-09-06 17:05:58 +05:30
|
|
|
*/
|
|
|
|
//config:config BLOCKDEV
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "blockdev (2.3 kb)"
|
2010-09-06 17:05:58 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Performs some ioctls with block devices.
|
2010-09-06 17:05:58 +05:30
|
|
|
|
2017-08-05 04:59:12 +05:30
|
|
|
//applet:IF_BLOCKDEV(APPLET_NOEXEC(blockdev, blockdev, BB_DIR_SBIN, BB_SUID_DROP, blockdev))
|
2016-11-23 16:16:32 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o
|
|
|
|
|
2010-09-06 17:05:58 +05:30
|
|
|
//usage:#define blockdev_trivial_usage
|
2010-09-06 17:45:46 +05:30
|
|
|
//usage: "OPTION BLOCKDEV"
|
2010-09-06 17:05:58 +05:30
|
|
|
//usage:#define blockdev_full_usage "\n\n"
|
2011-06-05 07:28:28 +05:30
|
|
|
//usage: " --setro Set ro"
|
2010-09-06 17:05:58 +05:30
|
|
|
//usage: "\n --setrw Set rw"
|
|
|
|
//usage: "\n --getro Get ro"
|
|
|
|
//usage: "\n --getss Get sector size"
|
|
|
|
//usage: "\n --getbsz Get block size"
|
|
|
|
//usage: "\n --setbsz BYTES Set block size"
|
2011-02-20 08:45:43 +05:30
|
|
|
//usage: "\n --getsz Get device size in 512-byte sectors"
|
|
|
|
/*//usage: "\n --getsize Get device size in sectors (deprecated)"*/
|
2010-09-06 17:05:58 +05:30
|
|
|
//usage: "\n --getsize64 Get device size in bytes"
|
|
|
|
//usage: "\n --flushbufs Flush buffers"
|
|
|
|
//usage: "\n --rereadpt Reread partition table"
|
2019-06-08 12:27:11 +05:30
|
|
|
// util-linux 2.31 also has:
|
|
|
|
// --getdiscardzeroes BLKDISCARDZEROES Get discard zeroes support status
|
|
|
|
// --getpbsz BLKPBSZGET Get physical block (sector) size
|
|
|
|
// --getiomin BLKIOMIN Get minimum I/O size
|
|
|
|
// --getioopt BLKIOOPT Get optimal I/O size
|
|
|
|
// --getalignoff BLKALIGNOFF Get alignment offset in bytes
|
|
|
|
// --getmaxsect BLKSECTGET Get max sectors per request
|
|
|
|
// --setra SECTORS BLKRASET Set readahead
|
|
|
|
// --getra BLKRAGET Get readahead
|
|
|
|
// --setfra SECTORS BLKFRASET Set filesystem readahead
|
|
|
|
// --getfra BLKFRAGET Get filesystem readahead
|
2010-09-06 17:05:58 +05:30
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
2019-03-31 00:37:21 +05:30
|
|
|
/* Takes less space is separate arrays than one array of struct */
|
|
|
|
static const char bdcmd_names[] ALIGN1 =
|
|
|
|
"setro" "\0"
|
|
|
|
#define CMD_SETRO 0
|
|
|
|
"setrw" "\0"
|
|
|
|
"getro" "\0"
|
|
|
|
"getss" "\0"
|
|
|
|
"getbsz" "\0"
|
|
|
|
"setbsz" "\0"
|
|
|
|
#define CMD_SETBSZ 5
|
|
|
|
"getsz" "\0"
|
|
|
|
"getsize" "\0"
|
|
|
|
"getsize64" "\0"
|
|
|
|
"flushbufs" "\0"
|
|
|
|
"rereadpt" "\0"
|
|
|
|
;
|
2020-11-30 19:28:02 +05:30
|
|
|
static const uint32_t bdcmd_ioctl[] ALIGN4 = {
|
2019-03-31 00:37:21 +05:30
|
|
|
BLKROSET, //setro
|
|
|
|
BLKROSET, //setrw
|
|
|
|
BLKROGET, //getro
|
|
|
|
BLKSSZGET, //getss
|
|
|
|
BLKBSZGET, //getbsz
|
|
|
|
BLKBSZSET, //setbsz
|
|
|
|
BLKGETSIZE64, //getsz
|
|
|
|
BLKGETSIZE, //getsize
|
|
|
|
BLKGETSIZE64, //getsize64
|
|
|
|
BLKFLSBUF, //flushbufs
|
|
|
|
BLKRRPART, //rereadpt
|
|
|
|
};
|
2010-09-06 17:05:58 +05:30
|
|
|
enum {
|
|
|
|
ARG_NONE = 0,
|
|
|
|
ARG_INT = 1,
|
|
|
|
ARG_ULONG = 2,
|
2010-09-06 21:07:52 +05:30
|
|
|
/* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */
|
|
|
|
ARG_U64 = 3,
|
2010-09-06 17:05:58 +05:30
|
|
|
ARG_MASK = 3,
|
|
|
|
|
|
|
|
FL_USRARG = 4, /* argument is provided by user */
|
|
|
|
FL_NORESULT = 8,
|
2011-02-20 08:45:43 +05:30
|
|
|
FL_SCALE512 = 16,
|
2010-09-06 17:05:58 +05:30
|
|
|
};
|
2019-03-31 00:37:21 +05:30
|
|
|
static const uint8_t bdcmd_flags[] ALIGN1 = {
|
|
|
|
ARG_INT + FL_NORESULT, //setro
|
|
|
|
ARG_INT + FL_NORESULT, //setrw
|
|
|
|
ARG_INT, //getro
|
|
|
|
ARG_INT, //getss
|
|
|
|
ARG_INT, //getbsz
|
|
|
|
ARG_INT + FL_NORESULT + FL_USRARG, //setbsz
|
|
|
|
ARG_U64 + FL_SCALE512, //getsz
|
|
|
|
ARG_ULONG, //getsize
|
|
|
|
ARG_U64, //getsize64
|
|
|
|
ARG_NONE + FL_NORESULT, //flushbufs
|
|
|
|
ARG_NONE + FL_NORESULT, //rereadpt
|
2010-09-06 17:05:58 +05:30
|
|
|
};
|
|
|
|
|
2019-03-31 00:37:21 +05:30
|
|
|
static unsigned find_cmd(const char *s)
|
2010-09-06 17:05:58 +05:30
|
|
|
{
|
2010-09-06 19:04:15 +05:30
|
|
|
if (s[0] == '-' && s[1] == '-') {
|
2019-03-31 00:37:21 +05:30
|
|
|
int n = index_in_strings(bdcmd_names, s + 2);
|
|
|
|
if (n >= 0)
|
|
|
|
return n;
|
2010-09-06 19:04:15 +05:30
|
|
|
}
|
2010-09-06 17:05:58 +05:30
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2011-02-20 08:45:43 +05:30
|
|
|
int blockdev_main(int argc UNUSED_PARAM, char **argv)
|
2010-09-06 17:05:58 +05:30
|
|
|
{
|
2019-03-31 00:37:21 +05:30
|
|
|
unsigned bdcmd;
|
|
|
|
unsigned flags;
|
2010-09-06 17:05:58 +05:30
|
|
|
int fd;
|
2010-09-06 21:07:52 +05:30
|
|
|
uint64_t u64;
|
|
|
|
union {
|
|
|
|
int i;
|
|
|
|
unsigned long lu;
|
|
|
|
uint64_t u64;
|
|
|
|
} ioctl_val_on_stack;
|
2010-09-06 17:05:58 +05:30
|
|
|
|
2011-02-20 08:45:43 +05:30
|
|
|
argv++;
|
|
|
|
if (!argv[0] || !argv[1]) /* must have at least 2 args */
|
2010-09-06 17:05:58 +05:30
|
|
|
bb_show_usage();
|
|
|
|
|
2011-02-20 08:45:43 +05:30
|
|
|
bdcmd = find_cmd(*argv);
|
2019-03-31 00:37:21 +05:30
|
|
|
/* setrw translates to BLKROSET(0), most other ioctls don't care... */
|
|
|
|
/* ...setro will do BLKROSET(1) */
|
|
|
|
u64 = (bdcmd == CMD_SETRO);
|
|
|
|
if (bdcmd == CMD_SETBSZ) {
|
|
|
|
/* ...setbsz is BLKBSZSET(bytes) */
|
2010-09-06 21:07:52 +05:30
|
|
|
u64 = xatoi_positive(*++argv);
|
2019-03-31 00:37:21 +05:30
|
|
|
}
|
2010-09-06 17:05:58 +05:30
|
|
|
|
2011-02-20 08:45:43 +05:30
|
|
|
argv++;
|
|
|
|
if (!argv[0] || argv[1])
|
2010-09-06 17:05:58 +05:30
|
|
|
bb_show_usage();
|
2011-02-20 08:45:43 +05:30
|
|
|
fd = xopen(argv[0], O_RDONLY);
|
2010-09-06 17:05:58 +05:30
|
|
|
|
2010-09-06 21:07:52 +05:30
|
|
|
ioctl_val_on_stack.u64 = u64;
|
2019-03-31 00:37:21 +05:30
|
|
|
flags = bdcmd_flags[bdcmd];
|
2010-09-06 21:07:52 +05:30
|
|
|
#if BB_BIG_ENDIAN
|
|
|
|
/* Store data properly wrt data size.
|
|
|
|
* (1) It's no-op for little-endian.
|
|
|
|
* (2) it's no-op for 0 and -1. Only --setro uses arg != 0 and != -1,
|
|
|
|
* and it is ARG_INT. --setbsz USER_VAL is also ARG_INT.
|
|
|
|
* Thus, we don't need to handle ARG_ULONG.
|
|
|
|
*/
|
2019-03-31 00:37:21 +05:30
|
|
|
switch (flags & ARG_MASK) {
|
2010-09-06 17:05:58 +05:30
|
|
|
case ARG_INT:
|
2010-09-06 21:07:52 +05:30
|
|
|
ioctl_val_on_stack.i = (int)u64;
|
2010-09-06 17:05:58 +05:30
|
|
|
break;
|
2010-09-06 21:07:52 +05:30
|
|
|
# if 0 /* unused */
|
2010-09-06 17:05:58 +05:30
|
|
|
case ARG_ULONG:
|
2010-09-06 21:07:52 +05:30
|
|
|
ioctl_val_on_stack.lu = (unsigned long)u64;
|
2010-09-06 17:05:58 +05:30
|
|
|
break;
|
2010-09-06 21:07:52 +05:30
|
|
|
# endif
|
2010-09-06 17:05:58 +05:30
|
|
|
}
|
2010-09-06 21:07:52 +05:30
|
|
|
#endif
|
2010-09-06 17:05:58 +05:30
|
|
|
|
2019-03-31 00:37:21 +05:30
|
|
|
if (ioctl(fd, bdcmd_ioctl[bdcmd], &ioctl_val_on_stack.u64) == -1)
|
2010-09-06 17:05:58 +05:30
|
|
|
bb_simple_perror_msg_and_die(*argv);
|
|
|
|
|
2010-09-06 21:07:52 +05:30
|
|
|
/* Fetch it into register(s) */
|
|
|
|
u64 = ioctl_val_on_stack.u64;
|
|
|
|
|
2019-03-31 00:37:21 +05:30
|
|
|
if (flags & FL_SCALE512)
|
2011-02-20 08:45:43 +05:30
|
|
|
u64 >>= 9;
|
|
|
|
|
2010-09-06 21:07:52 +05:30
|
|
|
/* Zero- or one-extend the value if needed, then print */
|
2019-03-31 00:37:21 +05:30
|
|
|
switch (flags & (ARG_MASK+FL_NORESULT)) {
|
2010-09-06 17:05:58 +05:30
|
|
|
case ARG_INT:
|
|
|
|
/* Smaller code when we use long long
|
|
|
|
* (gcc tail-merges printf call)
|
|
|
|
*/
|
2010-09-06 21:07:52 +05:30
|
|
|
printf("%lld\n", (long long)(int)u64);
|
2010-09-06 17:05:58 +05:30
|
|
|
break;
|
|
|
|
case ARG_ULONG:
|
2010-09-06 21:07:52 +05:30
|
|
|
u64 = (unsigned long)u64;
|
2010-09-06 17:05:58 +05:30
|
|
|
/* FALLTHROUGH */
|
2010-09-06 21:07:52 +05:30
|
|
|
case ARG_U64:
|
|
|
|
printf("%llu\n", (unsigned long long)u64);
|
2010-09-06 17:05:58 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|