2010-09-06 11:35:58 +00:00
|
|
|
/*
|
|
|
|
* blockdev implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com>
|
|
|
|
*
|
2010-09-06 15:37:52 +00:00
|
|
|
* Licensed under GPLv2, see file License in this tarball for details.
|
2010-09-06 11:35:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
//applet:IF_BLOCKDEV(APPLET(blockdev, _BB_DIR_SBIN, _BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o
|
|
|
|
|
|
|
|
//config:config BLOCKDEV
|
|
|
|
//config: bool "blockdev"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: Performs some ioctls with block devices.
|
|
|
|
|
|
|
|
//usage:#define blockdev_trivial_usage
|
2010-09-06 12:15:46 +00:00
|
|
|
//usage: "OPTION BLOCKDEV"
|
2010-09-06 11:35:58 +00:00
|
|
|
//usage:#define blockdev_full_usage "\n\n"
|
|
|
|
//usage: "Options:"
|
|
|
|
//usage: "\n --setro Set ro"
|
|
|
|
//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"
|
|
|
|
//usage: "\n --getsize Get device size in 512-byte sectors"
|
|
|
|
//usage: "\n --getsize64 Get device size in bytes"
|
|
|
|
//usage: "\n --flushbufs Flush buffers"
|
|
|
|
//usage: "\n --rereadpt Reread partition table"
|
|
|
|
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_NONE = 0,
|
|
|
|
ARG_INT = 1,
|
|
|
|
ARG_ULONG = 2,
|
2010-09-06 15:37:52 +00:00
|
|
|
/* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */
|
|
|
|
ARG_U64 = 3,
|
2010-09-06 11:35:58 +00:00
|
|
|
ARG_MASK = 3,
|
|
|
|
|
|
|
|
FL_USRARG = 4, /* argument is provided by user */
|
|
|
|
FL_NORESULT = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bdc {
|
|
|
|
uint32_t ioc; /* ioctl code */
|
|
|
|
const char name[sizeof("flushbufs")]; /* "--setfoo" wothout "--" */
|
|
|
|
uint8_t flags;
|
|
|
|
int8_t argval; /* default argument value */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct bdc bdcommands[] = {
|
|
|
|
{
|
|
|
|
.ioc = BLKROSET,
|
|
|
|
.name = "setro",
|
|
|
|
.flags = ARG_INT + FL_NORESULT,
|
|
|
|
.argval = 1,
|
|
|
|
},{
|
|
|
|
.ioc = BLKROSET,
|
|
|
|
.name = "setrw",
|
|
|
|
.flags = ARG_INT + FL_NORESULT,
|
|
|
|
.argval = 0,
|
|
|
|
},{
|
|
|
|
.ioc = BLKROGET,
|
|
|
|
.name = "getro",
|
|
|
|
.flags = ARG_INT,
|
|
|
|
.argval = -1,
|
|
|
|
},{
|
|
|
|
.ioc = BLKSSZGET,
|
|
|
|
.name = "getss",
|
|
|
|
.flags = ARG_INT,
|
|
|
|
.argval = -1,
|
|
|
|
},{
|
|
|
|
.ioc = BLKBSZGET,
|
|
|
|
.name = "getbsz",
|
|
|
|
.flags = ARG_INT,
|
|
|
|
.argval = -1,
|
|
|
|
},{
|
|
|
|
.ioc = BLKBSZSET,
|
|
|
|
.name = "setbsz",
|
|
|
|
.flags = ARG_INT + FL_NORESULT + FL_USRARG,
|
|
|
|
.argval = 0,
|
|
|
|
},{
|
|
|
|
.ioc = BLKGETSIZE,
|
|
|
|
.name = "getsize",
|
|
|
|
.flags = ARG_ULONG,
|
|
|
|
.argval = -1,
|
|
|
|
},{
|
|
|
|
.ioc = BLKGETSIZE64,
|
|
|
|
.name = "getsize64",
|
2010-09-06 15:37:52 +00:00
|
|
|
.flags = ARG_U64,
|
2010-09-06 11:35:58 +00:00
|
|
|
.argval = -1,
|
|
|
|
},{
|
|
|
|
.ioc = BLKFLSBUF,
|
|
|
|
.name = "flushbufs",
|
|
|
|
.flags = ARG_NONE + FL_NORESULT,
|
|
|
|
.argval = 0,
|
|
|
|
},{
|
|
|
|
.ioc = BLKRRPART,
|
|
|
|
.name = "rereadpt",
|
|
|
|
.flags = ARG_NONE + FL_NORESULT,
|
|
|
|
.argval = 0,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct bdc *find_cmd(const char *s)
|
|
|
|
{
|
2010-09-06 13:34:15 +00:00
|
|
|
const struct bdc *bdcmd = bdcommands;
|
|
|
|
if (s[0] == '-' && s[1] == '-') {
|
|
|
|
s += 2;
|
|
|
|
do {
|
|
|
|
if (strcmp(s, bdcmd->name) == 0)
|
|
|
|
return bdcmd;
|
|
|
|
bdcmd++;
|
|
|
|
} while (bdcmd != bdcommands + ARRAY_SIZE(bdcommands));
|
|
|
|
}
|
2010-09-06 11:35:58 +00:00
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int blockdev_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const struct bdc *bdcmd;
|
|
|
|
int fd;
|
2010-09-06 15:37:52 +00:00
|
|
|
uint64_t u64;
|
|
|
|
union {
|
|
|
|
int i;
|
|
|
|
unsigned long lu;
|
|
|
|
uint64_t u64;
|
|
|
|
} ioctl_val_on_stack;
|
2010-09-06 11:35:58 +00:00
|
|
|
|
|
|
|
if ((unsigned)(argc - 3) > 1) /* must have 2 or 3 args */
|
|
|
|
bb_show_usage();
|
|
|
|
|
|
|
|
bdcmd = find_cmd(*++argv);
|
|
|
|
|
2010-09-06 15:37:52 +00:00
|
|
|
u64 = (int)bdcmd->argval;
|
2010-09-06 11:35:58 +00:00
|
|
|
if (bdcmd->flags & FL_USRARG)
|
2010-09-06 15:37:52 +00:00
|
|
|
u64 = xatoi_positive(*++argv);
|
2010-09-06 11:35:58 +00:00
|
|
|
|
2010-09-06 14:31:18 +00:00
|
|
|
if (!*++argv || argv[1])
|
2010-09-06 11:35:58 +00:00
|
|
|
bb_show_usage();
|
|
|
|
fd = xopen(*argv, O_RDONLY);
|
|
|
|
|
2010-09-06 15:37:52 +00:00
|
|
|
ioctl_val_on_stack.u64 = u64;
|
|
|
|
#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.
|
|
|
|
*/
|
2010-09-06 11:35:58 +00:00
|
|
|
switch (bdcmd->flags & ARG_MASK) {
|
|
|
|
case ARG_INT:
|
2010-09-06 15:37:52 +00:00
|
|
|
ioctl_val_on_stack.i = (int)u64;
|
2010-09-06 11:35:58 +00:00
|
|
|
break;
|
2010-09-06 15:37:52 +00:00
|
|
|
# if 0 /* unused */
|
2010-09-06 11:35:58 +00:00
|
|
|
case ARG_ULONG:
|
2010-09-06 15:37:52 +00:00
|
|
|
ioctl_val_on_stack.lu = (unsigned long)u64;
|
2010-09-06 11:35:58 +00:00
|
|
|
break;
|
2010-09-06 15:37:52 +00:00
|
|
|
# endif
|
2010-09-06 11:35:58 +00:00
|
|
|
}
|
2010-09-06 15:37:52 +00:00
|
|
|
#endif
|
2010-09-06 11:35:58 +00:00
|
|
|
|
2010-09-06 15:37:52 +00:00
|
|
|
if (ioctl(fd, bdcmd->ioc, &ioctl_val_on_stack.u64) == -1)
|
2010-09-06 11:35:58 +00:00
|
|
|
bb_simple_perror_msg_and_die(*argv);
|
|
|
|
|
2010-09-06 15:37:52 +00:00
|
|
|
/* Fetch it into register(s) */
|
|
|
|
u64 = ioctl_val_on_stack.u64;
|
|
|
|
|
|
|
|
/* Zero- or one-extend the value if needed, then print */
|
2010-09-06 11:35:58 +00:00
|
|
|
switch (bdcmd->flags & (ARG_MASK+FL_NORESULT)) {
|
|
|
|
case ARG_INT:
|
|
|
|
/* Smaller code when we use long long
|
|
|
|
* (gcc tail-merges printf call)
|
|
|
|
*/
|
2010-09-06 15:37:52 +00:00
|
|
|
printf("%lld\n", (long long)(int)u64);
|
2010-09-06 11:35:58 +00:00
|
|
|
break;
|
|
|
|
case ARG_ULONG:
|
2010-09-06 15:37:52 +00:00
|
|
|
u64 = (unsigned long)u64;
|
2010-09-06 11:35:58 +00:00
|
|
|
/* FALLTHROUGH */
|
2010-09-06 15:37:52 +00:00
|
|
|
case ARG_U64:
|
|
|
|
printf("%llu\n", (unsigned long long)u64);
|
2010-09-06 11:35:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|