]> granicus.if.org Git - strace/blob - tests/ioctl_mtd.c
3345b8c6a8713d1dc6304c74b7c3c841b641edf9
[strace] / tests / ioctl_mtd.c
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  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12
13 #ifdef HAVE_STRUCT_MTD_WRITE_REQ
14
15 # include <errno.h>
16 # include <inttypes.h>
17 # include <stdio.h>
18 # include <string.h>
19 # include <sys/ioctl.h>
20 # include <linux/ioctl.h>
21 # include <linux/version.h>
22 # include <mtd/mtd-abi.h>
23
24 static const unsigned long lmagic = (unsigned long) 0xdeadbeefbadc0dedULL;
25
26 # define TEST_NULL_ARG(cmd) \
27         do { \
28                 ioctl(-1, cmd, 0); \
29                 if (_IOC_DIR(cmd) == _IOC_WRITE) \
30                         printf("ioctl(-1, MIXER_WRITE(%u) or %s, NULL)" \
31                                " = -1 EBADF (%m)\n", \
32                                (unsigned int) _IOC_NR(cmd), #cmd); \
33                 else if (_IOC_DIR(cmd) == _IOC_READ) \
34                         printf("ioctl(-1, MIXER_READ(%u) or %s, NULL)" \
35                                " = -1 EBADF (%m)\n", \
36                                (unsigned int) _IOC_NR(cmd), #cmd); \
37                 else \
38                         printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd); \
39         } while (0)
40
41 # define TEST_erase_info_user(cmd, eiu)                                         \
42         do {                                                                    \
43                 ioctl(-1, cmd, eiu);                                            \
44                 printf("ioctl(-1, MIXER_%s(%u) or %s, {start=%#x, length=%#x})" \
45                        " = -1 EBADF (%m)\n",                                    \
46                        (_IOC_DIR(cmd) == _IOC_READ) ? "READ" : "WRITE",         \
47                        (unsigned int) _IOC_NR(cmd), #cmd,                       \
48                        eiu->start, eiu->length);                                \
49         } while (0)
50
51 int
52 main(void)
53 {
54         TEST_NULL_ARG(ECCGETLAYOUT);
55         TEST_NULL_ARG(ECCGETSTATS);
56         TEST_NULL_ARG(MEMERASE);
57         TEST_NULL_ARG(MEMERASE64);
58         TEST_NULL_ARG(MEMGETBADBLOCK);
59         TEST_NULL_ARG(MEMGETINFO);
60         TEST_NULL_ARG(MEMGETOOBSEL);
61         TEST_NULL_ARG(MEMGETREGIONCOUNT);
62         TEST_NULL_ARG(MEMISLOCKED);
63         TEST_NULL_ARG(MEMLOCK);
64         TEST_NULL_ARG(MEMREADOOB);
65         TEST_NULL_ARG(MEMREADOOB64);
66         TEST_NULL_ARG(MEMSETBADBLOCK);
67         TEST_NULL_ARG(MEMUNLOCK);
68         TEST_NULL_ARG(MEMWRITE);
69         TEST_NULL_ARG(MEMWRITEOOB);
70         TEST_NULL_ARG(MEMWRITEOOB64);
71         TEST_NULL_ARG(OTPGETREGIONCOUNT);
72         TEST_NULL_ARG(OTPGETREGIONINFO);
73         TEST_NULL_ARG(OTPLOCK);
74         TEST_NULL_ARG(OTPSELECT);
75
76         ioctl(-1, MTDFILEMODE, MTD_FILE_MODE_NORMAL);
77         printf("ioctl(-1, MTDFILEMODE, MTD_FILE_MODE_NORMAL) = -1 EBADF (%m)\n");
78
79         TAIL_ALLOC_OBJECT_CONST_PTR(int, opt);
80         *opt = MTD_OTP_OFF;
81         ioctl(-1, OTPSELECT, opt);
82         printf("ioctl(-1, MIXER_READ(%u) or OTPSELECT, [MTD_OTP_OFF])"
83                " = -1 EBADF (%m)\n", (unsigned int) _IOC_NR(OTPSELECT));
84
85         TAIL_ALLOC_OBJECT_CONST_PTR(uint64_t, v64);
86         fill_memory(v64, sizeof(*v64));
87
88         ioctl(-1, MEMGETBADBLOCK, v64);
89         printf("ioctl(-1, MIXER_WRITE(%u) or MEMGETBADBLOCK, [%" PRIu64 "])"
90                " = -1 EBADF (%m)\n",
91                (unsigned int) _IOC_NR(MEMGETBADBLOCK), *v64);
92
93         ioctl(-1, MEMSETBADBLOCK, v64);
94         printf("ioctl(-1, MIXER_WRITE(%u) or MEMSETBADBLOCK, [%" PRIu64 "])"
95                " = -1 EBADF (%m)\n",
96                (unsigned int) _IOC_NR(MEMSETBADBLOCK), *v64);
97
98         TAIL_ALLOC_OBJECT_CONST_PTR(struct region_info_user, riu);
99         fill_memory(riu, sizeof(*riu));
100         ioctl(-1, MEMGETREGIONINFO, riu);
101         printf("ioctl(-1, %s, {regionindex=%#x}) = -1 EBADF (%m)\n",
102                "MEMGETREGIONINFO"
103 # ifdef __i386__
104                " or MTRRIOC_GET_PAGE_ENTRY"
105 # endif
106                , riu->regionindex);
107
108         TAIL_ALLOC_OBJECT_CONST_PTR(struct erase_info_user, eiu);
109         fill_memory(eiu, sizeof(*eiu));
110
111         TEST_erase_info_user(MEMERASE, eiu);
112         TEST_erase_info_user(MEMLOCK, eiu);
113         TEST_erase_info_user(MEMUNLOCK, eiu);
114         TEST_erase_info_user(MEMISLOCKED, eiu);
115
116         TAIL_ALLOC_OBJECT_CONST_PTR(struct erase_info_user64, eiu64);
117         fill_memory(eiu64, sizeof(*eiu64));
118         ioctl(-1, MEMERASE64, eiu64);
119         printf("ioctl(-1, MIXER_WRITE(%u) or %s, {start=%#llx, length=%#llx})"
120                " = -1 EBADF (%m)\n",
121                (unsigned int) _IOC_NR(MEMERASE64), "MEMERASE64",
122                (unsigned long long) eiu64->start,
123                (unsigned long long) eiu64->length);
124
125         TAIL_ALLOC_OBJECT_CONST_PTR(struct mtd_oob_buf, oob);
126         fill_memory(oob, sizeof(*oob));
127
128         ioctl(-1, MEMWRITEOOB, oob);
129         printf("ioctl(-1, MEMWRITEOOB, {start=%#x, length=%#x, ptr=%p})"
130                " = -1 EBADF (%m)\n", oob->start, oob->length, oob->ptr);
131
132         ioctl(-1, MEMREADOOB, oob);
133         printf("ioctl(-1, MEMREADOOB, {start=%#x, length=%#x, ptr=%p})"
134                " = -1 EBADF (%m)\n", oob->start, oob->length, oob->ptr);
135
136         TAIL_ALLOC_OBJECT_CONST_PTR(struct mtd_oob_buf64, oob64);
137         fill_memory(oob64, sizeof(*oob64));
138
139         ioctl(-1, MEMWRITEOOB64, oob64);
140         printf("ioctl(-1, MEMWRITEOOB64"
141                ", {start=%#llx, length=%#x, usr_ptr=%#llx}) = -1 EBADF (%m)\n",
142                (unsigned long long) oob64->start, oob64->length,
143                (unsigned long long) oob64->usr_ptr);
144
145         ioctl(-1, MEMREADOOB64, oob64);
146         printf("ioctl(-1, MEMREADOOB64"
147                ", {start=%#llx, length=%#x, usr_ptr=%#llx}) = -1 EBADF (%m)\n",
148                (unsigned long long) oob64->start, oob64->length,
149                (unsigned long long) oob64->usr_ptr);
150
151
152         TAIL_ALLOC_OBJECT_CONST_PTR(struct otp_info, oi);
153         fill_memory(oi, sizeof(*oi));
154         ioctl(-1, OTPLOCK, oi);
155         printf("ioctl(-1, MIXER_READ(%u) or OTPLOCK"
156                ", {start=%#x, length=%#x, locked=%u}) = -1 EBADF (%m)\n",
157                (unsigned int) _IOC_NR(OTPLOCK),  oi->start, oi->length, oi->locked);
158
159         TAIL_ALLOC_OBJECT_CONST_PTR(struct mtd_write_req, wr);
160         fill_memory(wr, sizeof(*wr));
161         wr->mode = MTD_OPS_PLACE_OOB;
162         ioctl(-1, MEMWRITE, wr);
163         printf("ioctl(-1, MEMWRITE, {start=%#llx, len=%#llx, ooblen=%#llx"
164                ", usr_data=%#llx, usr_oob=%#llx, mode=MTD_OPS_PLACE_OOB})"
165                " = -1 EBADF (%m)\n",
166                (unsigned long long) wr->start,
167                (unsigned long long) wr->len,
168                (unsigned long long) wr->ooblen,
169                (unsigned long long) wr->usr_data,
170                (unsigned long long) wr->usr_oob);
171
172         ioctl(-1, _IOC(_IOC_READ|_IOC_WRITE, 0x4d, 0xfe, 0xff), lmagic);
173         printf("ioctl(-1, %s, %#lx) = -1 EBADF (%m)\n",
174                "_IOC(_IOC_READ|_IOC_WRITE, 0x4d, 0xfe, 0xff)", lmagic);
175
176         puts("+++ exited with 0 +++");
177         return 0;
178 }
179
180 #else
181
182 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_MTD_WRITE_REQ")
183
184 #endif