2 * Copyright (c) 2012 Mike Frysinger <vapier@gentoo.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/ioctl.h>
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)
36 # include <mtd/mtd-abi.h>
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
39 # include "ubi-user.h"
41 # include <mtd/ubi-user.h>
44 static const struct xlat mtd_mode_options[] = {
45 XLAT(MTD_OPS_PLACE_OOB),
46 XLAT(MTD_OPS_AUTO_OOB),
51 static const struct xlat mtd_type_options[] = {
59 XLAT(MTD_MLCNANDFLASH),
63 static const struct xlat mtd_flags_options[] = {
65 XLAT(MTD_BIT_WRITEABLE),
67 XLAT(MTD_POWERUP_LOCK),
71 static const struct xlat mtd_otp_options[] = {
73 XLAT(MTD_OTP_FACTORY),
78 static const struct xlat mtd_nandecc_options[] = {
79 XLAT(MTD_NANDECC_OFF),
80 XLAT(MTD_NANDECC_PLACE),
81 XLAT(MTD_NANDECC_AUTOPLACE),
82 XLAT(MTD_NANDECC_PLACEONLY),
83 XLAT(MTD_NANDECC_AUTOPL_USR),
87 int mtd_ioctl(struct tcb *tcp, long code, long arg)
89 struct mtd_info_user minfo;
90 struct erase_info_user einfo;
91 struct erase_info_user64 einfo64;
92 struct mtd_oob_buf mbuf;
93 struct mtd_oob_buf64 mbuf64;
94 struct region_info_user rinfo;
95 struct otp_info oinfo;
96 struct mtd_ecc_stats estat;
97 struct mtd_write_req mreq;
98 struct nand_oobinfo ninfo;
99 struct nand_ecclayout_user nlay;
108 if (!verbose(tcp) || umove(tcp, arg, &minfo) < 0)
112 printxval(mtd_type_options, minfo.type, "MTD_???");
114 printflags(mtd_flags_options, minfo.flags, "MTD_???");
115 tprintf(", size=%#" PRIx32 ", erasesize=%#" PRIx32,
116 minfo.size, minfo.erasesize);
117 tprintf(", writesize=%#" PRIx32 ", oobsize=%#" PRIx32,
118 minfo.writesize, minfo.oobsize);
119 tprintf(", padding=%#" PRIx64 "}",
120 (uint64_t) minfo.padding);
127 if (!verbose(tcp) || umove(tcp, arg, &einfo) < 0)
130 tprintf(", {start=%#" PRIx32 ", length=%#" PRIx32 "}",
131 einfo.start, einfo.length);
135 if (!verbose(tcp) || umove(tcp, arg, &einfo64) < 0)
138 tprintf(", {start=%#" PRIx64 ", length=%#" PRIx64 "}",
139 (uint64_t) einfo64.start, (uint64_t) einfo64.length);
144 if (!verbose(tcp) || umove(tcp, arg, &mbuf) < 0)
147 tprintf(", {start=%#" PRIx32 ", length=%#" PRIx32 ", ptr=...}",
148 mbuf.start, mbuf.length);
153 if (!verbose(tcp) || umove(tcp, arg, &mbuf64) < 0)
156 tprintf(", {start=%#" PRIx64 ", length=%#" PRIx64 ", ptr=...}",
157 (uint64_t) mbuf64.start, (uint64_t) mbuf64.length);
160 case MEMGETREGIONINFO:
161 if (!verbose(tcp) || umove(tcp, arg, &rinfo) < 0)
164 tprintf(", {offset=%#" PRIx32 ", erasesize=%#" PRIx32,
165 rinfo.offset, rinfo.erasesize);
166 tprintf(", numblocks=%#" PRIx32 ", regionindex=%#" PRIx32 "}",
167 rinfo.numblocks, rinfo.regionindex);
171 if (!verbose(tcp) || umove(tcp, arg, &ninfo) < 0)
174 tprints(", {useecc=");
175 printxval(mtd_nandecc_options, ninfo.useecc, "MTD_NANDECC_???");
176 tprintf(", eccbytes=%#" PRIx32, ninfo.eccbytes);
178 tprints(", oobfree={");
179 for (i = 0; i < ARRAY_SIZE(ninfo.oobfree); ++i) {
183 for (j = 0; j < ARRAY_SIZE(ninfo.oobfree[0]); ++j) {
186 tprintf("%#" PRIx32, ninfo.oobfree[i][j]);
190 tprints("}}, eccpos={");
191 for (i = 0; i < ARRAY_SIZE(ninfo.eccpos); ++i) {
194 tprintf("%#" PRIx32, ninfo.eccpos[i]);
200 case OTPGETREGIONINFO:
202 if (!verbose(tcp) || umove(tcp, arg, &oinfo) < 0)
205 tprintf(", {start=%#" PRIx32 ", length=%#" PRIx32 ", locked=%" PRIu32 "}",
206 oinfo.start, oinfo.length, oinfo.locked);
210 if (!verbose(tcp) || umove(tcp, arg, &nlay) < 0)
213 tprintf(", {eccbytes=%#" PRIx32 ", eccpos={", nlay.eccbytes);
214 for (i = 0; i < ARRAY_SIZE(nlay.eccpos); ++i) {
217 tprintf("%#" PRIx32, nlay.eccpos[i]);
219 tprintf("}, oobavail=%#" PRIx32 ", oobfree={", nlay.oobavail);
220 for (i = 0; i < ARRAY_SIZE(nlay.oobfree); ++i) {
223 tprintf("{offset=%#" PRIx32 ", length=%#" PRIx32 "}",
224 nlay.oobfree[i].offset, nlay.oobfree[i].length);
230 if (!verbose(tcp) || umove(tcp, arg, &estat) < 0)
233 tprintf(", {corrected=%#" PRIx32 ", failed=%#" PRIx32,
234 estat.corrected, estat.failed);
235 tprintf(", badblocks=%#" PRIx32 ", bbtblocks=%#" PRIx32 "}",
236 estat.badblocks, estat.bbtblocks);
240 if (!verbose(tcp) || umove(tcp, arg, &mreq) < 0)
243 tprintf(", {start=%#" PRIx64 ", len=%#" PRIx64,
244 (uint64_t) mreq.start, (uint64_t) mreq.len);
245 tprintf(", ooblen=%#" PRIx64 ", usr_data=%#" PRIx64,
246 (uint64_t) mreq.ooblen, (uint64_t) mreq.usr_data);
247 tprintf(", usr_oob=%#" PRIx64 ", mode=",
248 (uint64_t) mreq.usr_oob);
249 printxval(mtd_mode_options, mreq.mode, "MTD_OPS_???");
250 tprints(", padding=...}");
254 if (!verbose(tcp) || umove(tcp, arg, &i) < 0)
258 printxval(mtd_otp_options, i, "MTD_OTP_???");
268 print_loff_t(tcp, arg);
271 case OTPGETREGIONCOUNT:
272 if (!verbose(tcp) || umove(tcp, arg, &i) < 0)
275 tprintf(", [%d]", i);
279 /* XXX: process return value as enum mtd_file_modes */
281 case MEMGETREGIONCOUNT:
282 /* These ones take simple args, so let default printer handle it */
289 static const struct xlat ubi_volume_types[] = {
290 XLAT(UBI_DYNAMIC_VOLUME),
291 XLAT(UBI_STATIC_VOLUME),
295 static const struct xlat ubi_volume_props[] = {
296 XLAT(UBI_VOL_PROP_DIRECT_WRITE),
300 int ubi_ioctl(struct tcb *tcp, long code, long arg)
302 struct ubi_mkvol_req mkvol;
303 struct ubi_rsvol_req rsvol;
304 struct ubi_rnvol_req rnvol;
305 struct ubi_attach_req attach;
306 struct ubi_map_req map;
307 struct ubi_set_vol_prop_req prop;
308 /* 4*(n-1) + 3 for quotes and NUL */
309 char vol_name[(UBI_MAX_VOLUME_NAME + 1) * 4];
317 if (!verbose(tcp) || umove(tcp, arg, &mkvol) < 0)
320 tprintf(", {vol_id=%" PRIi32 ", alignment=%" PRIi32
321 ", bytes=%" PRIi64 ", vol_type=", mkvol.vol_id,
322 mkvol.alignment, (int64_t)mkvol.bytes);
323 printxval(ubi_volume_types, mkvol.vol_type, "UBI_???_VOLUME");
324 ret = string_quote(mkvol.name, vol_name, -1,
325 CLAMP(mkvol.name_len, 0, UBI_MAX_VOLUME_NAME));
326 tprintf(", name_len=%" PRIi16 ", name=%s%s",
327 mkvol.name_len, vol_name, ret ? "..." : "");
332 if (!verbose(tcp) || umove(tcp, arg, &rsvol) < 0)
335 tprintf(", {vol_id=%" PRIi32 ", bytes=%" PRIi64 "}",
336 rsvol.vol_id, (int64_t)rsvol.bytes);
342 if (!verbose(tcp) || umove(tcp, arg, &rnvol) < 0)
345 tprintf(", {count=%" PRIi32 ", ents=[", rnvol.count);
346 for (c = 0; c < CLAMP(rnvol.count, 0, UBI_MAX_RNVOL); ++c) {
349 ret = string_quote(rnvol.ents[c].name, vol_name, -1,
350 CLAMP(rnvol.ents[c].name_len, 0, UBI_MAX_VOLUME_NAME));
351 tprintf("{vol_id=%" PRIi32 ", name_len=%" PRIi16
352 ", name=%s%s}", rnvol.ents[c].vol_id,
353 rnvol.ents[c].name_len, vol_name, ret ? "..." : "");
362 if (!verbose(tcp) || umove(tcp, arg, &bytes) < 0)
365 tprintf(", %" PRIi64, (int64_t)bytes);
370 if (!verbose(tcp) || umove(tcp, arg, &attach) < 0)
373 tprintf(", {ubi_num=%" PRIi32 ", mtd_num=%" PRIi32
374 ", vid_hdr_offset=%" PRIi32
375 ", max_beb_per1024=%" PRIi16 "}",
376 attach.ubi_num, attach.mtd_num,
377 attach.vid_hdr_offset, attach.max_beb_per1024);
381 if (!verbose(tcp) || umove(tcp, arg, &map) < 0)
384 tprintf(", {lnum=%" PRIi32 ", dtype=%" PRIi8 "}",
385 map.lnum, map.dtype);
388 case UBI_IOCSETVOLPROP:
389 if (!verbose(tcp) || umove(tcp, arg, &prop) < 0)
392 tprints(", {property=");
393 printxval(ubi_volume_props, prop.property, "UBI_VOL_PROP_???");
394 tprintf(", value=%#" PRIx64 "}", (uint64_t)prop.value);
403 /* These ones take simple args, so let default printer handle it */