]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/compat/freebsd32/freebsd32_ioctl.c
MFC r219987:
[FreeBSD/stable/8.git] / sys / compat / freebsd32 / freebsd32_ioctl.c
1 /*-
2  * Copyright (c) 2008 David E. O'Brien
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_compat.h"
34
35 #include <sys/param.h>
36 #include <sys/cdio.h>
37 #include <sys/fcntl.h>
38 #include <sys/filio.h>
39 #include <sys/file.h>
40 #include <sys/ioccom.h>
41 #include <sys/mdioctl.h>
42 #include <sys/proc.h>
43 #include <sys/syscall.h>
44 #include <sys/syscallsubr.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
48 #include <sys/systm.h>
49
50 #include <compat/freebsd32/freebsd32.h>
51 #include <compat/freebsd32/freebsd32_ioctl.h>
52 #include <compat/freebsd32/freebsd32_proto.h>
53
54 /* Cannot get exact size in 64-bit due to alignment issue of entire struct. */
55 CTASSERT((sizeof(struct md_ioctl32)+4) == 436);
56 CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8);
57 CTASSERT(sizeof(struct ioc_toc_header32) == 4);
58
59
60 static int
61 freebsd32_ioctl_md(struct thread *td, struct freebsd32_ioctl_args *uap,
62     struct file *fp)
63 {
64         struct md_ioctl mdv;
65         struct md_ioctl32 md32;
66         u_long com = 0;
67         int error;
68
69         if (uap->com & IOC_IN) {
70                 if ((error = copyin(uap->data, &md32, sizeof(md32)))) {
71                         return (error);
72                 }
73                 CP(md32, mdv, md_version);
74                 CP(md32, mdv, md_unit);
75                 CP(md32, mdv, md_type);
76                 PTRIN_CP(md32, mdv, md_file);
77                 CP(md32, mdv, md_mediasize);
78                 CP(md32, mdv, md_sectorsize);
79                 CP(md32, mdv, md_options);
80                 CP(md32, mdv, md_base);
81                 CP(md32, mdv, md_fwheads);
82                 CP(md32, mdv, md_fwsectors);
83         } else if (uap->com & IOC_OUT) {
84                 /*
85                  * Zero the buffer so the user always
86                  * gets back something deterministic.
87                  */
88                 bzero(&mdv, sizeof mdv);
89         }
90
91         switch (uap->com) {
92         case MDIOCATTACH_32:
93                 com = MDIOCATTACH;
94                 break;
95         case MDIOCDETACH_32:
96                 com = MDIOCDETACH;
97                 break;
98         case MDIOCQUERY_32:
99                 com = MDIOCQUERY;
100                 break;
101         case MDIOCLIST_32:
102                 com = MDIOCLIST;
103                 break;
104         default:
105                 panic("%s: unknown MDIOC %#x", __func__, uap->com);
106         }
107         error = fo_ioctl(fp, com, (caddr_t)&mdv, td->td_ucred, td);
108         if (error == 0 && (com & IOC_OUT)) {
109                 CP(mdv, md32, md_version);
110                 CP(mdv, md32, md_unit);
111                 CP(mdv, md32, md_type);
112                 PTROUT_CP(mdv, md32, md_file);
113                 CP(mdv, md32, md_mediasize);
114                 CP(mdv, md32, md_sectorsize);
115                 CP(mdv, md32, md_options);
116                 CP(mdv, md32, md_base);
117                 CP(mdv, md32, md_fwheads);
118                 CP(mdv, md32, md_fwsectors);
119                 error = copyout(&md32, uap->data, sizeof(md32));
120         }
121         return error;
122 }
123
124
125 static int
126 freebsd32_ioctl_ioc_toc_header(struct thread *td,
127     struct freebsd32_ioctl_args *uap, struct file *fp)
128 {
129         struct ioc_toc_header toch;
130         struct ioc_toc_header32 toch32;
131         int error;
132
133         if ((error = copyin(uap->data, &toch32, sizeof(toch32))))
134                 return (error);
135         CP(toch32, toch, len);
136         CP(toch32, toch, starting_track);
137         CP(toch32, toch, ending_track);
138         error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&toch,
139             td->td_ucred, td);
140         return (error);
141 }
142
143
144 static int
145 freebsd32_ioctl_ioc_read_toc(struct thread *td,
146     struct freebsd32_ioctl_args *uap, struct file *fp)
147 {
148         struct ioc_read_toc_entry toce;
149         struct ioc_read_toc_entry32 toce32;
150         int error;
151
152         if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
153                 return (error);
154         CP(toce32, toce, address_format);
155         CP(toce32, toce, starting_track);
156         CP(toce32, toce, data_len);
157         PTRIN_CP(toce32, toce, data);
158
159         if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
160             td->td_ucred, td))) {
161                 CP(toce, toce32, address_format);
162                 CP(toce, toce32, starting_track);
163                 CP(toce, toce32, data_len);
164                 PTROUT_CP(toce, toce32, data);
165                 error = copyout(&toce32, uap->data, sizeof(toce32));
166         }
167         return error;
168 }
169
170 static int
171 freebsd32_ioctl_fiodgname(struct thread *td,
172     struct freebsd32_ioctl_args *uap, struct file *fp)
173 {
174         struct fiodgname_arg fgn;
175         struct fiodgname_arg32 fgn32;
176         int error;
177
178         if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
179                 return (error);
180         CP(fgn32, fgn, len);
181         PTRIN_CP(fgn32, fgn, buf);
182         error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
183         return (error);
184 }
185
186 int
187 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
188 {
189         struct ioctl_args ap /*{
190                 int     fd;
191                 u_long  com;
192                 caddr_t data;
193         }*/ ;
194         struct file *fp;
195         int error;
196
197         if ((error = fget(td, uap->fd, &fp)) != 0)
198                 return (error);
199         if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
200                 fdrop(fp, td);
201                 return (EBADF);
202         }
203
204         switch (uap->com) {
205         case MDIOCATTACH_32:    /* FALLTHROUGH */
206         case MDIOCDETACH_32:    /* FALLTHROUGH */
207         case MDIOCQUERY_32:     /* FALLTHROUGH */
208         case MDIOCLIST_32:
209                 error = freebsd32_ioctl_md(td, uap, fp);
210                 break;
211
212         case CDIOREADTOCENTRYS_32:
213                 error = freebsd32_ioctl_ioc_read_toc(td, uap, fp);
214                 break;
215
216         case CDIOREADTOCHEADER_32:
217                 error = freebsd32_ioctl_ioc_toc_header(td, uap, fp);
218                 break;
219
220         case FIODGNAME_32:
221                 error = freebsd32_ioctl_fiodgname(td, uap, fp);
222                 break;
223
224         default:
225                 fdrop(fp, td);
226                 ap.fd = uap->fd;
227                 ap.com = uap->com;
228                 PTRIN_CP(*uap, ap, data);
229                 return ioctl(td, &ap);
230         }
231
232         fdrop(fp, td);
233         return error;
234 }