Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Mike Frysinger <vapier@gentoo.org> |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * 3. The name of the author may not be used to endorse or promote products |
| 13 | * derived from this software without specific prior written permission. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "defs.h" |
| 28 | |
| 29 | #include <sys/ioctl.h> |
| 30 | |
| 31 | /* The mtd api changes quickly, so we have to keep a local copy */ |
| 32 | #include <linux/version.h> |
| 33 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) |
| 34 | # include "mtd-abi.h" |
| 35 | #else |
| 36 | # include <mtd/mtd-abi.h> |
| 37 | #endif |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 38 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0) |
| 39 | # include "ubi-user.h" |
| 40 | #else |
| 41 | # include <mtd/ubi-user.h> |
| 42 | #endif |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 43 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 44 | #include "xlat/mtd_mode_options.h" |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 45 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 46 | #include "xlat/mtd_type_options.h" |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 47 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 48 | #include "xlat/mtd_flags_options.h" |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 49 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 50 | #include "xlat/mtd_otp_options.h" |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 51 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 52 | #include "xlat/mtd_nandecc_options.h" |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 53 | |
| 54 | int mtd_ioctl(struct tcb *tcp, long code, long arg) |
| 55 | { |
| 56 | struct mtd_info_user minfo; |
| 57 | struct erase_info_user einfo; |
| 58 | struct erase_info_user64 einfo64; |
| 59 | struct mtd_oob_buf mbuf; |
| 60 | struct mtd_oob_buf64 mbuf64; |
| 61 | struct region_info_user rinfo; |
| 62 | struct otp_info oinfo; |
| 63 | struct mtd_ecc_stats estat; |
| 64 | struct mtd_write_req mreq; |
| 65 | struct nand_oobinfo ninfo; |
| 66 | struct nand_ecclayout_user nlay; |
| 67 | int i, j; |
| 68 | |
| 69 | if (entering(tcp)) |
| 70 | return 0; |
| 71 | |
| 72 | switch (code) { |
| 73 | |
| 74 | case MEMGETINFO: |
| 75 | if (!verbose(tcp) || umove(tcp, arg, &minfo) < 0) |
| 76 | return 0; |
| 77 | |
| 78 | tprints(", {type="); |
| 79 | printxval(mtd_type_options, minfo.type, "MTD_???"); |
| 80 | tprints(", flags="); |
| 81 | printflags(mtd_flags_options, minfo.flags, "MTD_???"); |
| 82 | tprintf(", size=%#" PRIx32 ", erasesize=%#" PRIx32, |
| 83 | minfo.size, minfo.erasesize); |
| 84 | tprintf(", writesize=%#" PRIx32 ", oobsize=%#" PRIx32, |
| 85 | minfo.writesize, minfo.oobsize); |
| 86 | tprintf(", padding=%#" PRIx64 "}", |
| 87 | (uint64_t) minfo.padding); |
| 88 | return 1; |
| 89 | |
| 90 | case MEMERASE: |
| 91 | case MEMLOCK: |
| 92 | case MEMUNLOCK: |
| 93 | case MEMISLOCKED: |
| 94 | if (!verbose(tcp) || umove(tcp, arg, &einfo) < 0) |
| 95 | return 0; |
| 96 | |
| 97 | tprintf(", {start=%#" PRIx32 ", length=%#" PRIx32 "}", |
| 98 | einfo.start, einfo.length); |
| 99 | return 1; |
| 100 | |
| 101 | case MEMERASE64: |
| 102 | if (!verbose(tcp) || umove(tcp, arg, &einfo64) < 0) |
| 103 | return 0; |
| 104 | |
| 105 | tprintf(", {start=%#" PRIx64 ", length=%#" PRIx64 "}", |
| 106 | (uint64_t) einfo64.start, (uint64_t) einfo64.length); |
| 107 | return 1; |
| 108 | |
| 109 | case MEMWRITEOOB: |
| 110 | case MEMREADOOB: |
| 111 | if (!verbose(tcp) || umove(tcp, arg, &mbuf) < 0) |
| 112 | return 0; |
| 113 | |
| 114 | tprintf(", {start=%#" PRIx32 ", length=%#" PRIx32 ", ptr=...}", |
| 115 | mbuf.start, mbuf.length); |
| 116 | return 1; |
| 117 | |
| 118 | case MEMWRITEOOB64: |
| 119 | case MEMREADOOB64: |
| 120 | if (!verbose(tcp) || umove(tcp, arg, &mbuf64) < 0) |
| 121 | return 0; |
| 122 | |
| 123 | tprintf(", {start=%#" PRIx64 ", length=%#" PRIx64 ", ptr=...}", |
| 124 | (uint64_t) mbuf64.start, (uint64_t) mbuf64.length); |
| 125 | return 1; |
| 126 | |
| 127 | case MEMGETREGIONINFO: |
| 128 | if (!verbose(tcp) || umove(tcp, arg, &rinfo) < 0) |
| 129 | return 0; |
| 130 | |
| 131 | tprintf(", {offset=%#" PRIx32 ", erasesize=%#" PRIx32, |
| 132 | rinfo.offset, rinfo.erasesize); |
| 133 | tprintf(", numblocks=%#" PRIx32 ", regionindex=%#" PRIx32 "}", |
| 134 | rinfo.numblocks, rinfo.regionindex); |
| 135 | return 1; |
| 136 | |
| 137 | case MEMGETOOBSEL: |
| 138 | if (!verbose(tcp) || umove(tcp, arg, &ninfo) < 0) |
| 139 | return 0; |
| 140 | |
| 141 | tprints(", {useecc="); |
| 142 | printxval(mtd_nandecc_options, ninfo.useecc, "MTD_NANDECC_???"); |
| 143 | tprintf(", eccbytes=%#" PRIx32, ninfo.eccbytes); |
| 144 | |
| 145 | tprints(", oobfree={"); |
| 146 | for (i = 0; i < ARRAY_SIZE(ninfo.oobfree); ++i) { |
| 147 | if (i) |
| 148 | tprints("}, "); |
| 149 | tprints("{"); |
| 150 | for (j = 0; j < ARRAY_SIZE(ninfo.oobfree[0]); ++j) { |
| 151 | if (j) |
| 152 | tprints(", "); |
| 153 | tprintf("%#" PRIx32, ninfo.oobfree[i][j]); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | tprints("}}, eccpos={"); |
| 158 | for (i = 0; i < ARRAY_SIZE(ninfo.eccpos); ++i) { |
| 159 | if (i) |
| 160 | tprints(", "); |
| 161 | tprintf("%#" PRIx32, ninfo.eccpos[i]); |
| 162 | } |
| 163 | |
| 164 | tprints("}"); |
| 165 | return 1; |
| 166 | |
| 167 | case OTPGETREGIONINFO: |
| 168 | case OTPLOCK: |
| 169 | if (!verbose(tcp) || umove(tcp, arg, &oinfo) < 0) |
| 170 | return 0; |
| 171 | |
| 172 | tprintf(", {start=%#" PRIx32 ", length=%#" PRIx32 ", locked=%" PRIu32 "}", |
| 173 | oinfo.start, oinfo.length, oinfo.locked); |
| 174 | return 1; |
| 175 | |
| 176 | case ECCGETLAYOUT: |
| 177 | if (!verbose(tcp) || umove(tcp, arg, &nlay) < 0) |
| 178 | return 0; |
| 179 | |
| 180 | tprintf(", {eccbytes=%#" PRIx32 ", eccpos={", nlay.eccbytes); |
| 181 | for (i = 0; i < ARRAY_SIZE(nlay.eccpos); ++i) { |
| 182 | if (i) |
| 183 | tprints(", "); |
| 184 | tprintf("%#" PRIx32, nlay.eccpos[i]); |
| 185 | } |
| 186 | tprintf("}, oobavail=%#" PRIx32 ", oobfree={", nlay.oobavail); |
| 187 | for (i = 0; i < ARRAY_SIZE(nlay.oobfree); ++i) { |
| 188 | if (i) |
| 189 | tprints(", "); |
| 190 | tprintf("{offset=%#" PRIx32 ", length=%#" PRIx32 "}", |
| 191 | nlay.oobfree[i].offset, nlay.oobfree[i].length); |
| 192 | } |
| 193 | tprints("}"); |
| 194 | return 1; |
| 195 | |
| 196 | case ECCGETSTATS: |
| 197 | if (!verbose(tcp) || umove(tcp, arg, &estat) < 0) |
| 198 | return 0; |
| 199 | |
| 200 | tprintf(", {corrected=%#" PRIx32 ", failed=%#" PRIx32, |
| 201 | estat.corrected, estat.failed); |
| 202 | tprintf(", badblocks=%#" PRIx32 ", bbtblocks=%#" PRIx32 "}", |
| 203 | estat.badblocks, estat.bbtblocks); |
| 204 | return 1; |
| 205 | |
| 206 | case MEMWRITE: |
| 207 | if (!verbose(tcp) || umove(tcp, arg, &mreq) < 0) |
| 208 | return 0; |
| 209 | |
| 210 | tprintf(", {start=%#" PRIx64 ", len=%#" PRIx64, |
| 211 | (uint64_t) mreq.start, (uint64_t) mreq.len); |
| 212 | tprintf(", ooblen=%#" PRIx64 ", usr_data=%#" PRIx64, |
| 213 | (uint64_t) mreq.ooblen, (uint64_t) mreq.usr_data); |
| 214 | tprintf(", usr_oob=%#" PRIx64 ", mode=", |
| 215 | (uint64_t) mreq.usr_oob); |
| 216 | printxval(mtd_mode_options, mreq.mode, "MTD_OPS_???"); |
| 217 | tprints(", padding=...}"); |
| 218 | return 1; |
| 219 | |
| 220 | case OTPSELECT: |
| 221 | if (!verbose(tcp) || umove(tcp, arg, &i) < 0) |
| 222 | return 0; |
| 223 | |
| 224 | tprints(", ["); |
| 225 | printxval(mtd_otp_options, i, "MTD_OTP_???"); |
| 226 | tprints("]"); |
| 227 | return 1; |
| 228 | |
| 229 | case MEMGETBADBLOCK: |
| 230 | case MEMSETBADBLOCK: |
| 231 | if (!verbose(tcp)) |
| 232 | return 0; |
| 233 | |
| 234 | tprints(", "); |
| 235 | print_loff_t(tcp, arg); |
| 236 | return 1; |
| 237 | |
| 238 | case OTPGETREGIONCOUNT: |
| 239 | if (!verbose(tcp) || umove(tcp, arg, &i) < 0) |
| 240 | return 0; |
| 241 | |
Denys Vlasenko | 318a273 | 2012-05-16 12:29:09 +0200 | [diff] [blame] | 242 | tprintf(", [%d]", i); |
Mike Frysinger | 0cbed35 | 2012-04-04 22:22:01 -0400 | [diff] [blame] | 243 | return 1; |
| 244 | |
| 245 | case MTDFILEMODE: |
| 246 | /* XXX: process return value as enum mtd_file_modes */ |
| 247 | |
| 248 | case MEMGETREGIONCOUNT: |
| 249 | /* These ones take simple args, so let default printer handle it */ |
| 250 | |
| 251 | default: |
| 252 | return 0; |
| 253 | } |
| 254 | } |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 255 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 256 | #include "xlat/ubi_volume_types.h" |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 257 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame^] | 258 | #include "xlat/ubi_volume_props.h" |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 259 | |
| 260 | int ubi_ioctl(struct tcb *tcp, long code, long arg) |
| 261 | { |
| 262 | struct ubi_mkvol_req mkvol; |
| 263 | struct ubi_rsvol_req rsvol; |
| 264 | struct ubi_rnvol_req rnvol; |
| 265 | struct ubi_attach_req attach; |
| 266 | struct ubi_map_req map; |
| 267 | struct ubi_set_vol_prop_req prop; |
| 268 | /* 4*(n-1) + 3 for quotes and NUL */ |
| 269 | char vol_name[(UBI_MAX_VOLUME_NAME + 1) * 4]; |
Mike Frysinger | 2f99788 | 2013-05-05 01:21:54 -0400 | [diff] [blame] | 270 | int ret; |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 271 | |
| 272 | if (entering(tcp)) |
| 273 | return 0; |
| 274 | |
| 275 | switch (code) { |
| 276 | case UBI_IOCMKVOL: |
| 277 | if (!verbose(tcp) || umove(tcp, arg, &mkvol) < 0) |
| 278 | return 0; |
| 279 | |
| 280 | tprintf(", {vol_id=%" PRIi32 ", alignment=%" PRIi32 |
| 281 | ", bytes=%" PRIi64 ", vol_type=", mkvol.vol_id, |
| 282 | mkvol.alignment, (int64_t)mkvol.bytes); |
| 283 | printxval(ubi_volume_types, mkvol.vol_type, "UBI_???_VOLUME"); |
Mike Frysinger | 2f99788 | 2013-05-05 01:21:54 -0400 | [diff] [blame] | 284 | ret = string_quote(mkvol.name, vol_name, -1, |
| 285 | CLAMP(mkvol.name_len, 0, UBI_MAX_VOLUME_NAME)); |
| 286 | tprintf(", name_len=%" PRIi16 ", name=%s%s", |
| 287 | mkvol.name_len, vol_name, ret ? "..." : ""); |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 288 | tprints("}"); |
| 289 | return 1; |
| 290 | |
| 291 | case UBI_IOCRSVOL: |
| 292 | if (!verbose(tcp) || umove(tcp, arg, &rsvol) < 0) |
| 293 | return 0; |
| 294 | |
| 295 | tprintf(", {vol_id=%" PRIi32 ", bytes=%" PRIi64 "}", |
| 296 | rsvol.vol_id, (int64_t)rsvol.bytes); |
| 297 | return 1; |
| 298 | |
| 299 | case UBI_IOCRNVOL: { |
| 300 | __s32 c; |
| 301 | |
| 302 | if (!verbose(tcp) || umove(tcp, arg, &rnvol) < 0) |
| 303 | return 0; |
| 304 | |
| 305 | tprintf(", {count=%" PRIi32 ", ents=[", rnvol.count); |
| 306 | for (c = 0; c < CLAMP(rnvol.count, 0, UBI_MAX_RNVOL); ++c) { |
| 307 | if (c) |
| 308 | tprints(", "); |
Mike Frysinger | 2f99788 | 2013-05-05 01:21:54 -0400 | [diff] [blame] | 309 | ret = string_quote(rnvol.ents[c].name, vol_name, -1, |
| 310 | CLAMP(rnvol.ents[c].name_len, 0, UBI_MAX_VOLUME_NAME)); |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 311 | tprintf("{vol_id=%" PRIi32 ", name_len=%" PRIi16 |
Mike Frysinger | 2f99788 | 2013-05-05 01:21:54 -0400 | [diff] [blame] | 312 | ", name=%s%s}", rnvol.ents[c].vol_id, |
| 313 | rnvol.ents[c].name_len, vol_name, ret ? "..." : ""); |
Mike Frysinger | d648f29 | 2013-05-01 23:35:30 -0400 | [diff] [blame] | 314 | } |
| 315 | tprints("]}"); |
| 316 | return 1; |
| 317 | } |
| 318 | |
| 319 | case UBI_IOCVOLUP: { |
| 320 | __s64 bytes; |
| 321 | |
| 322 | if (!verbose(tcp) || umove(tcp, arg, &bytes) < 0) |
| 323 | return 0; |
| 324 | |
| 325 | tprintf(", %" PRIi64, (int64_t)bytes); |
| 326 | return 1; |
| 327 | } |
| 328 | |
| 329 | case UBI_IOCATT: |
| 330 | if (!verbose(tcp) || umove(tcp, arg, &attach) < 0) |
| 331 | return 0; |
| 332 | |
| 333 | tprintf(", {ubi_num=%" PRIi32 ", mtd_num=%" PRIi32 |
| 334 | ", vid_hdr_offset=%" PRIi32 |
| 335 | ", max_beb_per1024=%" PRIi16 "}", |
| 336 | attach.ubi_num, attach.mtd_num, |
| 337 | attach.vid_hdr_offset, attach.max_beb_per1024); |
| 338 | return 1; |
| 339 | |
| 340 | case UBI_IOCEBMAP: |
| 341 | if (!verbose(tcp) || umove(tcp, arg, &map) < 0) |
| 342 | return 0; |
| 343 | |
| 344 | tprintf(", {lnum=%" PRIi32 ", dtype=%" PRIi8 "}", |
| 345 | map.lnum, map.dtype); |
| 346 | return 1; |
| 347 | |
| 348 | case UBI_IOCSETVOLPROP: |
| 349 | if (!verbose(tcp) || umove(tcp, arg, &prop) < 0) |
| 350 | return 0; |
| 351 | |
| 352 | tprints(", {property="); |
| 353 | printxval(ubi_volume_props, prop.property, "UBI_VOL_PROP_???"); |
| 354 | tprintf(", value=%#" PRIx64 "}", (uint64_t)prop.value); |
| 355 | return 1; |
| 356 | |
| 357 | case UBI_IOCRMVOL: |
| 358 | case UBI_IOCDET: |
| 359 | case UBI_IOCEBER: |
| 360 | case UBI_IOCEBCH: |
| 361 | case UBI_IOCEBUNMAP: |
| 362 | case UBI_IOCEBISMAP: |
| 363 | /* These ones take simple args, so let default printer handle it */ |
| 364 | |
| 365 | default: |
| 366 | return 0; |
| 367 | } |
| 368 | } |