1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/*
* blockdev implementation for busybox
*
* Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com>
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
//config:config BLOCKDEV
//config: bool "blockdev (2.3 kb)"
//config: default y
//config: help
//config: Performs some ioctls with block devices.
//applet:IF_BLOCKDEV(APPLET_NOEXEC(blockdev, blockdev, BB_DIR_SBIN, BB_SUID_DROP, blockdev))
//kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o
//usage:#define blockdev_trivial_usage
//usage: "OPTION BLOCKDEV"
//usage:#define blockdev_full_usage "\n\n"
//usage: " --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 --getsz Get device size in 512-byte sectors"
/*//usage: "\n --getsize Get device size in sectors (deprecated)"*/
//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>
/* 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"
;
static const uint32_t bdcmd_ioctl[] = {
BLKROSET, //setro
BLKROSET, //setrw
BLKROGET, //getro
BLKSSZGET, //getss
BLKBSZGET, //getbsz
BLKBSZSET, //setbsz
BLKGETSIZE64, //getsz
BLKGETSIZE, //getsize
BLKGETSIZE64, //getsize64
BLKFLSBUF, //flushbufs
BLKRRPART, //rereadpt
};
enum {
ARG_NONE = 0,
ARG_INT = 1,
ARG_ULONG = 2,
/* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */
ARG_U64 = 3,
ARG_MASK = 3,
FL_USRARG = 4, /* argument is provided by user */
FL_NORESULT = 8,
FL_SCALE512 = 16,
};
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
};
static unsigned find_cmd(const char *s)
{
if (s[0] == '-' && s[1] == '-') {
int n = index_in_strings(bdcmd_names, s + 2);
if (n >= 0)
return n;
}
bb_show_usage();
}
int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int blockdev_main(int argc UNUSED_PARAM, char **argv)
{
unsigned bdcmd;
unsigned flags;
int fd;
uint64_t u64;
union {
int i;
unsigned long lu;
uint64_t u64;
} ioctl_val_on_stack;
argv++;
if (!argv[0] || !argv[1]) /* must have at least 2 args */
bb_show_usage();
bdcmd = find_cmd(*argv);
/* 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) */
u64 = xatoi_positive(*++argv);
}
argv++;
if (!argv[0] || argv[1])
bb_show_usage();
fd = xopen(argv[0], O_RDONLY);
ioctl_val_on_stack.u64 = u64;
flags = bdcmd_flags[bdcmd];
#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.
*/
switch (flags & ARG_MASK) {
case ARG_INT:
ioctl_val_on_stack.i = (int)u64;
break;
# if 0 /* unused */
case ARG_ULONG:
ioctl_val_on_stack.lu = (unsigned long)u64;
break;
# endif
}
#endif
if (ioctl(fd, bdcmd_ioctl[bdcmd], &ioctl_val_on_stack.u64) == -1)
bb_simple_perror_msg_and_die(*argv);
/* Fetch it into register(s) */
u64 = ioctl_val_on_stack.u64;
if (flags & FL_SCALE512)
u64 >>= 9;
/* Zero- or one-extend the value if needed, then print */
switch (flags & (ARG_MASK+FL_NORESULT)) {
case ARG_INT:
/* Smaller code when we use long long
* (gcc tail-merges printf call)
*/
printf("%lld\n", (long long)(int)u64);
break;
case ARG_ULONG:
u64 = (unsigned long)u64;
/* FALLTHROUGH */
case ARG_U64:
printf("%llu\n", (unsigned long long)u64);
break;
}
if (ENABLE_FEATURE_CLEAN_UP)
close(fd);
return EXIT_SUCCESS;
}
|