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 <linux/ioctl.h>
30 
31 /* The UBI api changes, so we have to keep a local copy */
32 #include <linux/version.h>
33 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
34 # include "ubi-user.h"
35 #else
36 # include <mtd/ubi-user.h>
37 #endif
38 
39 #include "xlat/ubi_volume_types.h"
40 #include "xlat/ubi_volume_props.h"
41 
42 int
ubi_ioctl(struct tcb * const tcp,const unsigned int code,const kernel_ulong_t arg)43 ubi_ioctl(struct tcb *const tcp, const unsigned int code,
44 	  const kernel_ulong_t arg)
45 {
46 	if (!verbose(tcp))
47 		return RVAL_DECODED;
48 
49 	switch (code) {
50 	case UBI_IOCMKVOL:
51 		if (entering(tcp)) {
52 			struct ubi_mkvol_req mkvol;
53 
54 			tprints(", ");
55 			if (umove_or_printaddr(tcp, arg, &mkvol))
56 				break;
57 
58 			tprintf("{vol_id=%" PRIi32 ", alignment=%" PRIi32
59 				", bytes=%" PRIi64 ", vol_type=", mkvol.vol_id,
60 				mkvol.alignment, (int64_t)mkvol.bytes);
61 			printxval(ubi_volume_types,
62 				    (uint8_t) mkvol.vol_type, "UBI_???_VOLUME");
63 			tprintf(", name_len=%" PRIi16 ", name=", mkvol.name_len);
64 			if (print_quoted_string(mkvol.name,
65 					CLAMP(mkvol.name_len, 0, UBI_MAX_VOLUME_NAME),
66 					QUOTE_0_TERMINATED) > 0) {
67 				tprints("...");
68 			}
69 			tprints("}");
70 			return 1;
71 		}
72 		if (!syserror(tcp)) {
73 			tprints(" => ");
74 			printnum_int(tcp, arg, "%d");
75 		}
76 		break;
77 
78 	case UBI_IOCRSVOL: {
79 		struct ubi_rsvol_req rsvol;
80 
81 		tprints(", ");
82 		if (umove_or_printaddr(tcp, arg, &rsvol))
83 			break;
84 
85 		tprintf("{vol_id=%" PRIi32 ", bytes=%" PRIi64 "}",
86 			rsvol.vol_id, (int64_t)rsvol.bytes);
87 		break;
88 	}
89 
90 	case UBI_IOCRNVOL: {
91 		struct ubi_rnvol_req rnvol;
92 		int c;
93 
94 		tprints(", ");
95 		if (umove_or_printaddr(tcp, arg, &rnvol))
96 			break;
97 
98 		tprintf("{count=%" PRIi32 ", ents=[", rnvol.count);
99 		for (c = 0; c < CLAMP(rnvol.count, 0, UBI_MAX_RNVOL); ++c) {
100 			if (c)
101 				tprints(", ");
102 			tprintf("{vol_id=%" PRIi32 ", name_len=%" PRIi16
103 				", name=", rnvol.ents[c].vol_id,
104 				rnvol.ents[c].name_len);
105 			if (print_quoted_string(rnvol.ents[c].name,
106 					CLAMP(rnvol.ents[c].name_len, 0, UBI_MAX_VOLUME_NAME),
107 					QUOTE_0_TERMINATED) > 0) {
108 				tprints("...");
109 			}
110 			tprints("}");
111 		}
112 		tprints("]}");
113 		break;
114 	}
115 
116 	case UBI_IOCEBCH: {
117 		struct ubi_leb_change_req leb;
118 
119 		tprints(", ");
120 		if (umove_or_printaddr(tcp, arg, &leb))
121 			break;
122 
123 		tprintf("{lnum=%d, bytes=%d}", leb.lnum, leb.bytes);
124 		break;
125 	}
126 
127 	case UBI_IOCATT:
128 		if (entering(tcp)) {
129 			struct ubi_attach_req attach;
130 
131 			tprints(", ");
132 			if (umove_or_printaddr(tcp, arg, &attach))
133 				break;
134 
135 			tprintf("{ubi_num=%" PRIi32 ", mtd_num=%" PRIi32
136 				", vid_hdr_offset=%" PRIi32
137 				", max_beb_per1024=%" PRIi16 "}",
138 				attach.ubi_num, attach.mtd_num,
139 				attach.vid_hdr_offset, attach.max_beb_per1024);
140 			return 1;
141 		}
142 		if (!syserror(tcp)) {
143 			tprints(" => ");
144 			printnum_int(tcp, arg, "%d");
145 		}
146 		break;
147 
148 	case UBI_IOCEBMAP: {
149 		struct ubi_map_req map;
150 
151 		tprints(", ");
152 		if (umove_or_printaddr(tcp, arg, &map))
153 			break;
154 
155 		tprintf("{lnum=%" PRIi32 ", dtype=%" PRIi8 "}",
156 			map.lnum, map.dtype);
157 		break;
158 	}
159 
160 	case UBI_IOCSETVOLPROP: {
161 		struct ubi_set_vol_prop_req prop;
162 
163 		tprints(", ");
164 		if (umove_or_printaddr(tcp, arg, &prop))
165 			break;
166 
167 		tprints("{property=");
168 		printxval(ubi_volume_props, prop.property, "UBI_VOL_PROP_???");
169 		tprintf(", value=%#" PRIx64 "}", (uint64_t)prop.value);
170 		break;
171 	}
172 
173 
174 	case UBI_IOCVOLUP:
175 		tprints(", ");
176 		printnum_int64(tcp, arg, "%" PRIi64);
177 		break;
178 
179 	case UBI_IOCDET:
180 	case UBI_IOCEBER:
181 	case UBI_IOCEBISMAP:
182 	case UBI_IOCEBUNMAP:
183 	case UBI_IOCRMVOL:
184 		tprints(", ");
185 		printnum_int(tcp, arg, "%d");
186 		break;
187 
188 #ifdef UBI_IOCVOLCRBLK
189 	case UBI_IOCVOLCRBLK:
190 #endif
191 #ifdef UBI_IOCVOLRMBLK
192 	case UBI_IOCVOLRMBLK:
193 #endif
194 		/* no arguments */
195 		break;
196 
197 	default:
198 		return RVAL_DECODED;
199 	}
200 
201 	return RVAL_DECODED | 1;
202 }
203