blob: e53ade995a9fa2b49d22e6d652a1a27b7ebce9a9 [file] [log] [blame]
Sergey Naumov289aff82010-09-06 13:35:58 +02001/*
2 * blockdev implementation for busybox
3 *
4 * Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com>
5 *
Denys Vlasenkof38b9122010-09-06 17:41:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Sergey Naumov289aff82010-09-06 13:35:58 +02007 */
Sergey Naumov289aff82010-09-06 13:35:58 +02008//config:config BLOCKDEV
Denys Vlasenko4eed2c62017-07-18 22:01:24 +02009//config: bool "blockdev (2.4 kb)"
Sergey Naumov289aff82010-09-06 13:35:58 +020010//config: default y
11//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020012//config: Performs some ioctls with block devices.
Sergey Naumov289aff82010-09-06 13:35:58 +020013
Denys Vlasenko9f598492017-08-05 01:29:12 +020014//applet:IF_BLOCKDEV(APPLET_NOEXEC(blockdev, blockdev, BB_DIR_SBIN, BB_SUID_DROP, blockdev))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010015
16//kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o
17
Sergey Naumov289aff82010-09-06 13:35:58 +020018//usage:#define blockdev_trivial_usage
Denys Vlasenkob4e6b412010-09-06 14:15:46 +020019//usage: "OPTION BLOCKDEV"
Sergey Naumov289aff82010-09-06 13:35:58 +020020//usage:#define blockdev_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020021//usage: " --setro Set ro"
Sergey Naumov289aff82010-09-06 13:35:58 +020022//usage: "\n --setrw Set rw"
23//usage: "\n --getro Get ro"
24//usage: "\n --getss Get sector size"
25//usage: "\n --getbsz Get block size"
26//usage: "\n --setbsz BYTES Set block size"
Dan Fandrichf303bdd2011-02-20 04:15:43 +010027//usage: "\n --getsz Get device size in 512-byte sectors"
28/*//usage: "\n --getsize Get device size in sectors (deprecated)"*/
Sergey Naumov289aff82010-09-06 13:35:58 +020029//usage: "\n --getsize64 Get device size in bytes"
30//usage: "\n --flushbufs Flush buffers"
31//usage: "\n --rereadpt Reread partition table"
32
33
34#include "libbb.h"
35#include <linux/fs.h>
36
37enum {
38 ARG_NONE = 0,
39 ARG_INT = 1,
40 ARG_ULONG = 2,
Denys Vlasenko571c5ab2010-09-06 17:37:52 +020041 /* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */
42 ARG_U64 = 3,
Sergey Naumov289aff82010-09-06 13:35:58 +020043 ARG_MASK = 3,
44
45 FL_USRARG = 4, /* argument is provided by user */
46 FL_NORESULT = 8,
Dan Fandrichf303bdd2011-02-20 04:15:43 +010047 FL_SCALE512 = 16,
Sergey Naumov289aff82010-09-06 13:35:58 +020048};
49
50struct bdc {
51 uint32_t ioc; /* ioctl code */
52 const char name[sizeof("flushbufs")]; /* "--setfoo" wothout "--" */
53 uint8_t flags;
54 int8_t argval; /* default argument value */
55};
56
57static const struct bdc bdcommands[] = {
58 {
59 .ioc = BLKROSET,
60 .name = "setro",
61 .flags = ARG_INT + FL_NORESULT,
62 .argval = 1,
63 },{
64 .ioc = BLKROSET,
65 .name = "setrw",
66 .flags = ARG_INT + FL_NORESULT,
67 .argval = 0,
68 },{
69 .ioc = BLKROGET,
70 .name = "getro",
71 .flags = ARG_INT,
72 .argval = -1,
73 },{
74 .ioc = BLKSSZGET,
75 .name = "getss",
76 .flags = ARG_INT,
77 .argval = -1,
78 },{
79 .ioc = BLKBSZGET,
80 .name = "getbsz",
81 .flags = ARG_INT,
82 .argval = -1,
83 },{
84 .ioc = BLKBSZSET,
85 .name = "setbsz",
86 .flags = ARG_INT + FL_NORESULT + FL_USRARG,
87 .argval = 0,
88 },{
Dan Fandrichf303bdd2011-02-20 04:15:43 +010089 .ioc = BLKGETSIZE64,
90 .name = "getsz",
91 .flags = ARG_U64 + FL_SCALE512,
92 .argval = -1,
93 },{
Sergey Naumov289aff82010-09-06 13:35:58 +020094 .ioc = BLKGETSIZE,
95 .name = "getsize",
96 .flags = ARG_ULONG,
97 .argval = -1,
98 },{
99 .ioc = BLKGETSIZE64,
100 .name = "getsize64",
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200101 .flags = ARG_U64,
Sergey Naumov289aff82010-09-06 13:35:58 +0200102 .argval = -1,
103 },{
104 .ioc = BLKFLSBUF,
105 .name = "flushbufs",
106 .flags = ARG_NONE + FL_NORESULT,
107 .argval = 0,
108 },{
109 .ioc = BLKRRPART,
110 .name = "rereadpt",
111 .flags = ARG_NONE + FL_NORESULT,
112 .argval = 0,
113 }
114};
115
116static const struct bdc *find_cmd(const char *s)
117{
Denys Vlasenko264bdad2010-09-06 15:34:15 +0200118 const struct bdc *bdcmd = bdcommands;
119 if (s[0] == '-' && s[1] == '-') {
120 s += 2;
121 do {
122 if (strcmp(s, bdcmd->name) == 0)
123 return bdcmd;
124 bdcmd++;
125 } while (bdcmd != bdcommands + ARRAY_SIZE(bdcommands));
126 }
Sergey Naumov289aff82010-09-06 13:35:58 +0200127 bb_show_usage();
128}
129
130int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Dan Fandrichf303bdd2011-02-20 04:15:43 +0100131int blockdev_main(int argc UNUSED_PARAM, char **argv)
Sergey Naumov289aff82010-09-06 13:35:58 +0200132{
133 const struct bdc *bdcmd;
Sergey Naumov289aff82010-09-06 13:35:58 +0200134 int fd;
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200135 uint64_t u64;
136 union {
137 int i;
138 unsigned long lu;
139 uint64_t u64;
140 } ioctl_val_on_stack;
Sergey Naumov289aff82010-09-06 13:35:58 +0200141
Dan Fandrichf303bdd2011-02-20 04:15:43 +0100142 argv++;
143 if (!argv[0] || !argv[1]) /* must have at least 2 args */
Sergey Naumov289aff82010-09-06 13:35:58 +0200144 bb_show_usage();
145
Dan Fandrichf303bdd2011-02-20 04:15:43 +0100146 bdcmd = find_cmd(*argv);
Sergey Naumov289aff82010-09-06 13:35:58 +0200147
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200148 u64 = (int)bdcmd->argval;
Sergey Naumov289aff82010-09-06 13:35:58 +0200149 if (bdcmd->flags & FL_USRARG)
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200150 u64 = xatoi_positive(*++argv);
Sergey Naumov289aff82010-09-06 13:35:58 +0200151
Dan Fandrichf303bdd2011-02-20 04:15:43 +0100152 argv++;
153 if (!argv[0] || argv[1])
Sergey Naumov289aff82010-09-06 13:35:58 +0200154 bb_show_usage();
Dan Fandrichf303bdd2011-02-20 04:15:43 +0100155 fd = xopen(argv[0], O_RDONLY);
Sergey Naumov289aff82010-09-06 13:35:58 +0200156
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200157 ioctl_val_on_stack.u64 = u64;
158#if BB_BIG_ENDIAN
159 /* Store data properly wrt data size.
160 * (1) It's no-op for little-endian.
161 * (2) it's no-op for 0 and -1. Only --setro uses arg != 0 and != -1,
162 * and it is ARG_INT. --setbsz USER_VAL is also ARG_INT.
163 * Thus, we don't need to handle ARG_ULONG.
164 */
Sergey Naumov289aff82010-09-06 13:35:58 +0200165 switch (bdcmd->flags & ARG_MASK) {
166 case ARG_INT:
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200167 ioctl_val_on_stack.i = (int)u64;
Sergey Naumov289aff82010-09-06 13:35:58 +0200168 break;
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200169# if 0 /* unused */
Sergey Naumov289aff82010-09-06 13:35:58 +0200170 case ARG_ULONG:
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200171 ioctl_val_on_stack.lu = (unsigned long)u64;
Sergey Naumov289aff82010-09-06 13:35:58 +0200172 break;
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200173# endif
Sergey Naumov289aff82010-09-06 13:35:58 +0200174 }
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200175#endif
Sergey Naumov289aff82010-09-06 13:35:58 +0200176
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200177 if (ioctl(fd, bdcmd->ioc, &ioctl_val_on_stack.u64) == -1)
Sergey Naumov289aff82010-09-06 13:35:58 +0200178 bb_simple_perror_msg_and_die(*argv);
179
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200180 /* Fetch it into register(s) */
181 u64 = ioctl_val_on_stack.u64;
182
Dan Fandrichf303bdd2011-02-20 04:15:43 +0100183 if (bdcmd->flags & FL_SCALE512)
184 u64 >>= 9;
185
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200186 /* Zero- or one-extend the value if needed, then print */
Sergey Naumov289aff82010-09-06 13:35:58 +0200187 switch (bdcmd->flags & (ARG_MASK+FL_NORESULT)) {
188 case ARG_INT:
189 /* Smaller code when we use long long
190 * (gcc tail-merges printf call)
191 */
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200192 printf("%lld\n", (long long)(int)u64);
Sergey Naumov289aff82010-09-06 13:35:58 +0200193 break;
194 case ARG_ULONG:
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200195 u64 = (unsigned long)u64;
Sergey Naumov289aff82010-09-06 13:35:58 +0200196 /* FALLTHROUGH */
Denys Vlasenko571c5ab2010-09-06 17:37:52 +0200197 case ARG_U64:
198 printf("%llu\n", (unsigned long long)u64);
Sergey Naumov289aff82010-09-06 13:35:58 +0200199 break;
200 }
201
202 if (ENABLE_FEATURE_CLEAN_UP)
203 close(fd);
204 return EXIT_SUCCESS;
205}