freebsd32_ioctl.c revision 190529
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: head/sys/compat/freebsd32/freebsd32_ioctl.c 190529 2009-03-29 20:09:51Z ed $");
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. */
55CTASSERT((sizeof(struct md_ioctl32)+4) == 436);
56CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8);
57CTASSERT(sizeof(struct ioc_toc_header32) == 4);
58
59
60static int
61freebsd32_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->data == NULL)
70		panic("%s: where is my ioctl data??", __func__);
71	if (uap->com & IOC_IN) {
72		if ((error = copyin(uap->data, &md32, sizeof(md32)))) {
73			fdrop(fp, td);
74			return (error);
75		}
76		CP(md32, mdv, md_version);
77		CP(md32, mdv, md_unit);
78		CP(md32, mdv, md_type);
79		PTRIN_CP(md32, mdv, md_file);
80		CP(md32, mdv, md_mediasize);
81		CP(md32, mdv, md_sectorsize);
82		CP(md32, mdv, md_options);
83		CP(md32, mdv, md_base);
84		CP(md32, mdv, md_fwheads);
85		CP(md32, mdv, md_fwsectors);
86	} else if (uap->com & IOC_OUT) {
87		/*
88		 * Zero the buffer so the user always
89		 * gets back something deterministic.
90		 */
91		bzero(&mdv, sizeof mdv);
92	}
93
94	switch (uap->com) {
95	case MDIOCATTACH_32:
96		com = MDIOCATTACH;
97		break;
98	case MDIOCDETACH_32:
99		com = MDIOCDETACH;
100		break;
101	case MDIOCQUERY_32:
102		com = MDIOCQUERY;
103		break;
104	case MDIOCLIST_32:
105		com = MDIOCLIST;
106		break;
107	default:
108		panic("%s: unknown MDIOC %#x", __func__, uap->com);
109	}
110	error = fo_ioctl(fp, com, (caddr_t)&mdv, td->td_ucred, td);
111	if (error == 0 && (com & IOC_OUT)) {
112		CP(mdv, md32, md_version);
113		CP(mdv, md32, md_unit);
114		CP(mdv, md32, md_type);
115		PTROUT_CP(mdv, md32, md_file);
116		CP(mdv, md32, md_mediasize);
117		CP(mdv, md32, md_sectorsize);
118		CP(mdv, md32, md_options);
119		CP(mdv, md32, md_base);
120		CP(mdv, md32, md_fwheads);
121		CP(mdv, md32, md_fwsectors);
122		error = copyout(&md32, uap->data, sizeof(md32));
123	}
124	fdrop(fp, td);
125	return error;
126}
127
128
129static int
130freebsd32_ioctl_ioc_toc_header(struct thread *td,
131    struct freebsd32_ioctl_args *uap, struct file *fp)
132{
133	struct ioc_toc_header toch;
134	struct ioc_toc_header32 toch32;
135	int error;
136
137	if (uap->data == NULL)
138		panic("%s: where is my ioctl data??", __func__);
139
140	if ((error = copyin(uap->data, &toch32, sizeof(toch32))))
141		return (error);
142	CP(toch32, toch, len);
143	CP(toch32, toch, starting_track);
144	CP(toch32, toch, ending_track);
145	error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&toch,
146	    td->td_ucred, td);
147	fdrop(fp, td);
148	return (error);
149}
150
151
152static int
153freebsd32_ioctl_ioc_read_toc(struct thread *td,
154    struct freebsd32_ioctl_args *uap, struct file *fp)
155{
156	struct ioc_read_toc_entry toce;
157	struct ioc_read_toc_entry32 toce32;
158	int error;
159
160	if (uap->data == NULL)
161		panic("%s: where is my ioctl data??", __func__);
162
163	if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
164		return (error);
165	CP(toce32, toce, address_format);
166	CP(toce32, toce, starting_track);
167	CP(toce32, toce, data_len);
168	PTRIN_CP(toce32, toce, data);
169
170	if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
171	    td->td_ucred, td))) {
172		CP(toce, toce32, address_format);
173		CP(toce, toce32, starting_track);
174		CP(toce, toce32, data_len);
175		PTROUT_CP(toce, toce32, data);
176		error = copyout(&toce32, uap->data, sizeof(toce32));
177	}
178	fdrop(fp, td);
179	return error;
180}
181
182static int
183freebsd32_ioctl_fiodgname(struct thread *td,
184    struct freebsd32_ioctl_args *uap, struct file *fp)
185{
186	struct fiodgname_arg fgn;
187	struct fiodgname_arg32 fgn32;
188	int error;
189
190	if ((error = copyin(uap->data, &fgn32, sizeof fgn32)) != 0)
191		return (error);
192	CP(fgn32, fgn, len);
193	PTRIN_CP(fgn32, fgn, buf);
194	error = fo_ioctl(fp, FIODGNAME, (caddr_t)&fgn, td->td_ucred, td);
195	fdrop(fp, td);
196	return (error);
197}
198
199int
200freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
201{
202	struct ioctl_args ap /*{
203		int	fd;
204		u_long	com;
205		caddr_t	data;
206	}*/ ;
207	struct file *fp;
208	int error;
209
210	if ((error = fget(td, uap->fd, &fp)) != 0)
211		return (error);
212	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
213		fdrop(fp, td);
214		return (EBADF);
215	}
216
217	switch (uap->com) {
218	case MDIOCATTACH_32:	/* FALLTHROUGH */
219	case MDIOCDETACH_32:	/* FALLTHROUGH */
220	case MDIOCQUERY_32:	/* FALLTHROUGH */
221	case MDIOCLIST_32:
222		return freebsd32_ioctl_md(td, uap, fp);
223
224	case CDIOREADTOCENTRYS_32:
225		return freebsd32_ioctl_ioc_read_toc(td, uap, fp);
226
227	case CDIOREADTOCHEADER_32:
228		return freebsd32_ioctl_ioc_toc_header(td, uap, fp);
229
230	case FIODGNAME_32:
231		return freebsd32_ioctl_fiodgname(td, uap, fp);
232
233	default:
234		fdrop(fp, td);
235		ap.fd = uap->fd;
236		ap.com = uap->com;
237		PTRIN_CP(*uap, ap, data);
238		return ioctl(td, &ap);
239	}
240}
241