• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of ioctl_mtd strace test.
3  *
4  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 
33 #ifdef HAVE_STRUCT_MTD_WRITE_REQ
34 
35 # include <errno.h>
36 # include <inttypes.h>
37 # include <stdio.h>
38 # include <string.h>
39 # include <sys/ioctl.h>
40 # include <linux/ioctl.h>
41 # include <linux/version.h>
42 # include <mtd/mtd-abi.h>
43 
44 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
45 
46 # define TEST_NULL_ARG(cmd) \
47 	do { \
48 		ioctl(-1, cmd, 0); \
49 		if (_IOC_DIR(cmd) == _IOC_WRITE) \
50 			printf("ioctl(-1, MIXER_WRITE(%u) or %s, NULL)" \
51 			       " = -1 EBADF (%m)\n", \
52 			       (unsigned int) _IOC_NR(cmd), #cmd); \
53 		else if (_IOC_DIR(cmd) == _IOC_READ) \
54 			printf("ioctl(-1, MIXER_READ(%u) or %s, NULL)" \
55 			       " = -1 EBADF (%m)\n", \
56 			       (unsigned int) _IOC_NR(cmd), #cmd); \
57 		else \
58 			printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd); \
59 	} while (0)
60 
61 # define TEST_erase_info_user(cmd, eiu)						\
62 	do {									\
63 		ioctl(-1, cmd, eiu);						\
64 		printf("ioctl(-1, MIXER_%s(%u) or %s, {start=%#x, length=%#x})"	\
65 		       " = -1 EBADF (%m)\n",					\
66 		       (_IOC_DIR(cmd) == _IOC_READ) ? "READ" : "WRITE",		\
67 		       (unsigned int) _IOC_NR(cmd), #cmd,			\
68 		       eiu->start, eiu->length);				\
69 	} while (0)
70 
71 int
main(void)72 main(void)
73 {
74 	TEST_NULL_ARG(ECCGETLAYOUT);
75 	TEST_NULL_ARG(ECCGETSTATS);
76 	TEST_NULL_ARG(MEMERASE);
77 	TEST_NULL_ARG(MEMERASE64);
78 	TEST_NULL_ARG(MEMGETBADBLOCK);
79 	TEST_NULL_ARG(MEMGETINFO);
80 	TEST_NULL_ARG(MEMGETOOBSEL);
81 	TEST_NULL_ARG(MEMGETREGIONCOUNT);
82 	TEST_NULL_ARG(MEMISLOCKED);
83 	TEST_NULL_ARG(MEMLOCK);
84 	TEST_NULL_ARG(MEMREADOOB);
85 	TEST_NULL_ARG(MEMREADOOB64);
86 	TEST_NULL_ARG(MEMSETBADBLOCK);
87 	TEST_NULL_ARG(MEMUNLOCK);
88 	TEST_NULL_ARG(MEMWRITE);
89 	TEST_NULL_ARG(MEMWRITEOOB);
90 	TEST_NULL_ARG(MEMWRITEOOB64);
91 	TEST_NULL_ARG(OTPGETREGIONCOUNT);
92 	TEST_NULL_ARG(OTPGETREGIONINFO);
93 	TEST_NULL_ARG(OTPLOCK);
94 	TEST_NULL_ARG(OTPSELECT);
95 
96 	ioctl(-1, MTDFILEMODE, MTD_FILE_MODE_NORMAL);
97 	printf("ioctl(-1, MTDFILEMODE, MTD_FILE_MODE_NORMAL) = -1 EBADF (%m)\n");
98 
99 	TAIL_ALLOC_OBJECT_CONST_PTR(int, opt);
100 	*opt = MTD_OTP_OFF;
101 	ioctl(-1, OTPSELECT, opt);
102 	printf("ioctl(-1, MIXER_READ(%u) or OTPSELECT, [MTD_OTP_OFF])"
103 	       " = -1 EBADF (%m)\n", (unsigned int) _IOC_NR(OTPSELECT));
104 
105 	TAIL_ALLOC_OBJECT_CONST_PTR(uint64_t, v64);
106 	fill_memory(v64, sizeof(*v64));
107 
108 	ioctl(-1, MEMGETBADBLOCK, v64);
109 	printf("ioctl(-1, MIXER_WRITE(%u) or MEMGETBADBLOCK, [%" PRIu64 "])"
110 	       " = -1 EBADF (%m)\n",
111 	       (unsigned int) _IOC_NR(MEMGETBADBLOCK), *v64);
112 
113 	ioctl(-1, MEMSETBADBLOCK, v64);
114 	printf("ioctl(-1, MIXER_WRITE(%u) or MEMSETBADBLOCK, [%" PRIu64 "])"
115 	       " = -1 EBADF (%m)\n",
116 	       (unsigned int) _IOC_NR(MEMSETBADBLOCK), *v64);
117 
118 	TAIL_ALLOC_OBJECT_CONST_PTR(struct region_info_user, riu);
119 	fill_memory(riu, sizeof(*riu));
120 	ioctl(-1, MEMGETREGIONINFO, riu);
121 	printf("ioctl(-1, %s, {regionindex=%#x}) = -1 EBADF (%m)\n",
122 	       "MEMGETREGIONINFO"
123 # ifdef __i386__
124 	       " or MTRRIOC_GET_PAGE_ENTRY"
125 # endif
126 	       , riu->regionindex);
127 
128 	TAIL_ALLOC_OBJECT_CONST_PTR(struct erase_info_user, eiu);
129 	fill_memory(eiu, sizeof(*eiu));
130 
131 	TEST_erase_info_user(MEMERASE, eiu);
132 	TEST_erase_info_user(MEMLOCK, eiu);
133 	TEST_erase_info_user(MEMUNLOCK, eiu);
134 	TEST_erase_info_user(MEMISLOCKED, eiu);
135 
136 	TAIL_ALLOC_OBJECT_CONST_PTR(struct erase_info_user64, eiu64);
137 	fill_memory(eiu64, sizeof(*eiu64));
138 	ioctl(-1, MEMERASE64, eiu64);
139 	printf("ioctl(-1, MIXER_WRITE(%u) or %s, {start=%#llx, length=%#llx})"
140 	       " = -1 EBADF (%m)\n",
141 	       (unsigned int) _IOC_NR(MEMERASE64), "MEMERASE64",
142 	       (unsigned long long) eiu64->start,
143 	       (unsigned long long) eiu64->length);
144 
145 	TAIL_ALLOC_OBJECT_CONST_PTR(struct mtd_oob_buf, oob);
146 	fill_memory(oob, sizeof(*oob));
147 
148 	ioctl(-1, MEMWRITEOOB, oob);
149 	printf("ioctl(-1, MEMWRITEOOB, {start=%#x, length=%#x, ptr=%p})"
150 	       " = -1 EBADF (%m)\n", oob->start, oob->length, oob->ptr);
151 
152 	ioctl(-1, MEMREADOOB, oob);
153 	printf("ioctl(-1, MEMREADOOB, {start=%#x, length=%#x, ptr=%p})"
154 	       " = -1 EBADF (%m)\n", oob->start, oob->length, oob->ptr);
155 
156 	TAIL_ALLOC_OBJECT_CONST_PTR(struct mtd_oob_buf64, oob64);
157 	fill_memory(oob64, sizeof(*oob64));
158 
159 	ioctl(-1, MEMWRITEOOB64, oob64);
160 	printf("ioctl(-1, MEMWRITEOOB64"
161 	       ", {start=%#llx, length=%#x, usr_ptr=%#llx}) = -1 EBADF (%m)\n",
162 	       (unsigned long long) oob64->start, oob64->length,
163 	       (unsigned long long) oob64->usr_ptr);
164 
165 	ioctl(-1, MEMREADOOB64, oob64);
166 	printf("ioctl(-1, MEMREADOOB64"
167 	       ", {start=%#llx, length=%#x, usr_ptr=%#llx}) = -1 EBADF (%m)\n",
168 	       (unsigned long long) oob64->start, oob64->length,
169 	       (unsigned long long) oob64->usr_ptr);
170 
171 
172 	TAIL_ALLOC_OBJECT_CONST_PTR(struct otp_info, oi);
173 	fill_memory(oi, sizeof(*oi));
174 	ioctl(-1, OTPLOCK, oi);
175 	printf("ioctl(-1, MIXER_READ(%u) or OTPLOCK"
176 	       ", {start=%#x, length=%#x, locked=%u}) = -1 EBADF (%m)\n",
177 	       (unsigned int) _IOC_NR(OTPLOCK),  oi->start, oi->length, oi->locked);
178 
179 	TAIL_ALLOC_OBJECT_CONST_PTR(struct mtd_write_req, wr);
180 	fill_memory(wr, sizeof(*wr));
181 	wr->mode = MTD_OPS_PLACE_OOB;
182 	ioctl(-1, MEMWRITE, wr);
183 	printf("ioctl(-1, MEMWRITE, {start=%#llx, len=%#llx, ooblen=%#llx"
184 	       ", usr_data=%#llx, usr_oob=%#llx, mode=MTD_OPS_PLACE_OOB})"
185 	       " = -1 EBADF (%m)\n",
186 	       (unsigned long long) wr->start,
187 	       (unsigned long long) wr->len,
188 	       (unsigned long long) wr->ooblen,
189 	       (unsigned long long) wr->usr_data,
190 	       (unsigned long long) wr->usr_oob);
191 
192 	ioctl(-1, _IOC(_IOC_READ|_IOC_WRITE, 0x4d, 0xfe, 0xff), lmagic);
193 	printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
194 	       "_IOC(_IOC_READ|_IOC_WRITE, 0x4d, 0xfe, 0xff)", lmagic);
195 
196 	puts("+++ exited with 0 +++");
197 	return 0;
198 }
199 
200 #else
201 
202 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_MTD_WRITE_REQ")
203 
204 #endif
205