Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * blockdev implementation for busybox |
| 3 | * |
| 4 | * Copyright (C) 2010 Sergey Naumov <sknaumov@gmail.com> |
| 5 | * |
Denys Vlasenko | f38b912 | 2010-09-06 17:41:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 7 | */ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 8 | //config:config BLOCKDEV |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 9 | //config: bool "blockdev (2.3 kb)" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 10 | //config: default y |
| 11 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 12 | //config: Performs some ioctls with block devices. |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 13 | |
Denys Vlasenko | 9f59849 | 2017-08-05 01:29:12 +0200 | [diff] [blame] | 14 | //applet:IF_BLOCKDEV(APPLET_NOEXEC(blockdev, blockdev, BB_DIR_SBIN, BB_SUID_DROP, blockdev)) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 15 | |
| 16 | //kbuild:lib-$(CONFIG_BLOCKDEV) += blockdev.o |
| 17 | |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 18 | //usage:#define blockdev_trivial_usage |
Denys Vlasenko | b4e6b41 | 2010-09-06 14:15:46 +0200 | [diff] [blame] | 19 | //usage: "OPTION BLOCKDEV" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 20 | //usage:#define blockdev_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 21 | //usage: " --setro Set ro" |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 22 | //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 Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 27 | //usage: "\n --getsz Get device size in 512-byte sectors" |
| 28 | /*//usage: "\n --getsize Get device size in sectors (deprecated)"*/ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 29 | //usage: "\n --getsize64 Get device size in bytes" |
| 30 | //usage: "\n --flushbufs Flush buffers" |
| 31 | //usage: "\n --rereadpt Reread partition table" |
Denys Vlasenko | 023ae75 | 2019-06-08 08:57:11 +0200 | [diff] [blame] | 32 | // util-linux 2.31 also has: |
| 33 | // --getdiscardzeroes BLKDISCARDZEROES Get discard zeroes support status |
| 34 | // --getpbsz BLKPBSZGET Get physical block (sector) size |
| 35 | // --getiomin BLKIOMIN Get minimum I/O size |
| 36 | // --getioopt BLKIOOPT Get optimal I/O size |
| 37 | // --getalignoff BLKALIGNOFF Get alignment offset in bytes |
| 38 | // --getmaxsect BLKSECTGET Get max sectors per request |
| 39 | // --setra SECTORS BLKRASET Set readahead |
| 40 | // --getra BLKRAGET Get readahead |
| 41 | // --setfra SECTORS BLKFRASET Set filesystem readahead |
| 42 | // --getfra BLKFRAGET Get filesystem readahead |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 43 | |
| 44 | #include "libbb.h" |
| 45 | #include <linux/fs.h> |
| 46 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 47 | /* Takes less space is separate arrays than one array of struct */ |
| 48 | static const char bdcmd_names[] ALIGN1 = |
| 49 | "setro" "\0" |
| 50 | #define CMD_SETRO 0 |
| 51 | "setrw" "\0" |
| 52 | "getro" "\0" |
| 53 | "getss" "\0" |
| 54 | "getbsz" "\0" |
| 55 | "setbsz" "\0" |
| 56 | #define CMD_SETBSZ 5 |
| 57 | "getsz" "\0" |
| 58 | "getsize" "\0" |
| 59 | "getsize64" "\0" |
| 60 | "flushbufs" "\0" |
| 61 | "rereadpt" "\0" |
| 62 | ; |
| 63 | static const uint32_t bdcmd_ioctl[] = { |
| 64 | BLKROSET, //setro |
| 65 | BLKROSET, //setrw |
| 66 | BLKROGET, //getro |
| 67 | BLKSSZGET, //getss |
| 68 | BLKBSZGET, //getbsz |
| 69 | BLKBSZSET, //setbsz |
| 70 | BLKGETSIZE64, //getsz |
| 71 | BLKGETSIZE, //getsize |
| 72 | BLKGETSIZE64, //getsize64 |
| 73 | BLKFLSBUF, //flushbufs |
| 74 | BLKRRPART, //rereadpt |
| 75 | }; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 76 | enum { |
| 77 | ARG_NONE = 0, |
| 78 | ARG_INT = 1, |
| 79 | ARG_ULONG = 2, |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 80 | /* Yes, BLKGETSIZE64 takes pointer to uint64_t, not ullong! */ |
| 81 | ARG_U64 = 3, |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 82 | ARG_MASK = 3, |
| 83 | |
| 84 | FL_USRARG = 4, /* argument is provided by user */ |
| 85 | FL_NORESULT = 8, |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 86 | FL_SCALE512 = 16, |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 87 | }; |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 88 | static const uint8_t bdcmd_flags[] ALIGN1 = { |
| 89 | ARG_INT + FL_NORESULT, //setro |
| 90 | ARG_INT + FL_NORESULT, //setrw |
| 91 | ARG_INT, //getro |
| 92 | ARG_INT, //getss |
| 93 | ARG_INT, //getbsz |
| 94 | ARG_INT + FL_NORESULT + FL_USRARG, //setbsz |
| 95 | ARG_U64 + FL_SCALE512, //getsz |
| 96 | ARG_ULONG, //getsize |
| 97 | ARG_U64, //getsize64 |
| 98 | ARG_NONE + FL_NORESULT, //flushbufs |
| 99 | ARG_NONE + FL_NORESULT, //rereadpt |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 102 | static unsigned find_cmd(const char *s) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 103 | { |
Denys Vlasenko | 264bdad | 2010-09-06 15:34:15 +0200 | [diff] [blame] | 104 | if (s[0] == '-' && s[1] == '-') { |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 105 | int n = index_in_strings(bdcmd_names, s + 2); |
| 106 | if (n >= 0) |
| 107 | return n; |
Denys Vlasenko | 264bdad | 2010-09-06 15:34:15 +0200 | [diff] [blame] | 108 | } |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 109 | bb_show_usage(); |
| 110 | } |
| 111 | |
| 112 | int blockdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 113 | int blockdev_main(int argc UNUSED_PARAM, char **argv) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 114 | { |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 115 | unsigned bdcmd; |
| 116 | unsigned flags; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 117 | int fd; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 118 | uint64_t u64; |
| 119 | union { |
| 120 | int i; |
| 121 | unsigned long lu; |
| 122 | uint64_t u64; |
| 123 | } ioctl_val_on_stack; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 124 | |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 125 | argv++; |
| 126 | if (!argv[0] || !argv[1]) /* must have at least 2 args */ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 127 | bb_show_usage(); |
| 128 | |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 129 | bdcmd = find_cmd(*argv); |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 130 | /* setrw translates to BLKROSET(0), most other ioctls don't care... */ |
| 131 | /* ...setro will do BLKROSET(1) */ |
| 132 | u64 = (bdcmd == CMD_SETRO); |
| 133 | if (bdcmd == CMD_SETBSZ) { |
| 134 | /* ...setbsz is BLKBSZSET(bytes) */ |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 135 | u64 = xatoi_positive(*++argv); |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 136 | } |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 137 | |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 138 | argv++; |
| 139 | if (!argv[0] || argv[1]) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 140 | bb_show_usage(); |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 141 | fd = xopen(argv[0], O_RDONLY); |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 142 | |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 143 | ioctl_val_on_stack.u64 = u64; |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 144 | flags = bdcmd_flags[bdcmd]; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 145 | #if BB_BIG_ENDIAN |
| 146 | /* Store data properly wrt data size. |
| 147 | * (1) It's no-op for little-endian. |
| 148 | * (2) it's no-op for 0 and -1. Only --setro uses arg != 0 and != -1, |
| 149 | * and it is ARG_INT. --setbsz USER_VAL is also ARG_INT. |
| 150 | * Thus, we don't need to handle ARG_ULONG. |
| 151 | */ |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 152 | switch (flags & ARG_MASK) { |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 153 | case ARG_INT: |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 154 | ioctl_val_on_stack.i = (int)u64; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 155 | break; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 156 | # if 0 /* unused */ |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 157 | case ARG_ULONG: |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 158 | ioctl_val_on_stack.lu = (unsigned long)u64; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 159 | break; |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 160 | # endif |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 161 | } |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 162 | #endif |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 163 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 164 | if (ioctl(fd, bdcmd_ioctl[bdcmd], &ioctl_val_on_stack.u64) == -1) |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 165 | bb_simple_perror_msg_and_die(*argv); |
| 166 | |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 167 | /* Fetch it into register(s) */ |
| 168 | u64 = ioctl_val_on_stack.u64; |
| 169 | |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 170 | if (flags & FL_SCALE512) |
Dan Fandrich | f303bdd | 2011-02-20 04:15:43 +0100 | [diff] [blame] | 171 | u64 >>= 9; |
| 172 | |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 173 | /* Zero- or one-extend the value if needed, then print */ |
Denys Vlasenko | 35082fc | 2019-03-30 20:07:21 +0100 | [diff] [blame] | 174 | switch (flags & (ARG_MASK+FL_NORESULT)) { |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 175 | case ARG_INT: |
| 176 | /* Smaller code when we use long long |
| 177 | * (gcc tail-merges printf call) |
| 178 | */ |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 179 | printf("%lld\n", (long long)(int)u64); |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 180 | break; |
| 181 | case ARG_ULONG: |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 182 | u64 = (unsigned long)u64; |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 183 | /* FALLTHROUGH */ |
Denys Vlasenko | 571c5ab | 2010-09-06 17:37:52 +0200 | [diff] [blame] | 184 | case ARG_U64: |
| 185 | printf("%llu\n", (unsigned long long)u64); |
Sergey Naumov | 289aff8 | 2010-09-06 13:35:58 +0200 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | |
| 189 | if (ENABLE_FEATURE_CLEAN_UP) |
| 190 | close(fd); |
| 191 | return EXIT_SUCCESS; |
| 192 | } |