2 * blockdev implementation for busybox
4 * Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com>
6 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //applet:IF_BLOCKDEV(APPLET(blockdev, _BB_DIR_SBIN, _BB_SUID_DROP))
11 //kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o
13 //config:config BLOCKDEV
14 //config: bool "blockdev"
17 //config: Performs some ioctls with block devices.
19 //usage:#define blockdev_trivial_usage
20 //usage: "OPTION BLOCKDEV"
21 //usage:#define blockdev_full_usage "\n\n"
23 //usage: "\n --setro Set ro"
24 //usage: "\n --setrw Set rw"
25 //usage: "\n --getro Get ro"
26 //usage: "\n --getss Get sector size"
27 //usage: "\n --getbsz Get block size"
28 //usage: "\n --setbsz BYTES Set block size"
29 //usage: "\n --getsize Get device size in 512-byte sectors"
30 //usage: "\n --getsize64 Get device size in bytes"
31 //usage: "\n --flushbufs Flush buffers"
32 //usage: "\n --rereadpt Reread partition table"
42 /* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */
46 FL_USRARG
= 4, /* argument is provided by user */
51 uint32_t ioc
; /* ioctl code */
52 const char name
[sizeof("flushbufs")]; /* "--setfoo" wothout "--" */
54 int8_t argval
; /* default argument value */
57 static const struct bdc bdcommands
[] = {
61 .flags
= ARG_INT
+ FL_NORESULT
,
66 .flags
= ARG_INT
+ FL_NORESULT
,
86 .flags
= ARG_INT
+ FL_NORESULT
+ FL_USRARG
,
101 .flags
= ARG_NONE
+ FL_NORESULT
,
106 .flags
= ARG_NONE
+ FL_NORESULT
,
111 static const struct bdc
*find_cmd(const char *s
)
113 const struct bdc
*bdcmd
= bdcommands
;
114 if (s
[0] == '-' && s
[1] == '-') {
117 if (strcmp(s
, bdcmd
->name
) == 0)
120 } while (bdcmd
!= bdcommands
+ ARRAY_SIZE(bdcommands
));
125 int blockdev_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
126 int blockdev_main(int argc
, char **argv
)
128 const struct bdc
*bdcmd
;
135 } ioctl_val_on_stack
;
137 if ((unsigned)(argc
- 3) > 1) /* must have 2 or 3 args */
140 bdcmd
= find_cmd(*++argv
);
142 u64
= (int)bdcmd
->argval
;
143 if (bdcmd
->flags
& FL_USRARG
)
144 u64
= xatoi_positive(*++argv
);
146 if (!*++argv
|| argv
[1])
148 fd
= xopen(*argv
, O_RDONLY
);
150 ioctl_val_on_stack
.u64
= u64
;
152 /* Store data properly wrt data size.
153 * (1) It's no-op for little-endian.
154 * (2) it's no-op for 0 and -1. Only --setro uses arg != 0 and != -1,
155 * and it is ARG_INT. --setbsz USER_VAL is also ARG_INT.
156 * Thus, we don't need to handle ARG_ULONG.
158 switch (bdcmd
->flags
& ARG_MASK
) {
160 ioctl_val_on_stack
.i
= (int)u64
;
164 ioctl_val_on_stack
.lu
= (unsigned long)u64
;
170 if (ioctl(fd
, bdcmd
->ioc
, &ioctl_val_on_stack
.u64
) == -1)
171 bb_simple_perror_msg_and_die(*argv
);
173 /* Fetch it into register(s) */
174 u64
= ioctl_val_on_stack
.u64
;
176 /* Zero- or one-extend the value if needed, then print */
177 switch (bdcmd
->flags
& (ARG_MASK
+FL_NORESULT
)) {
179 /* Smaller code when we use long long
180 * (gcc tail-merges printf call)
182 printf("%lld\n", (long long)(int)u64
);
185 u64
= (unsigned long)u64
;
188 printf("%llu\n", (unsigned long long)u64
);
192 if (ENABLE_FEATURE_CLEAN_UP
)