xref: /freebsd/sys/compat/freebsd32/freebsd32_ioctl.c (revision 54ebdd631db8c0bba2baab0155f603a8b5cf014a)
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/file.h>
39 #include <sys/ioccom.h>
40 #include <sys/mdioctl.h>
41 #include <sys/proc.h>
42 #include <sys/syscall.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysctl.h>
45 #include <sys/sysent.h>
46 #include <sys/sysproto.h>
47 #include <sys/systm.h>
48 
49 #include <compat/freebsd32/freebsd32.h>
50 #include <compat/freebsd32/freebsd32_ioctl.h>
51 #include <compat/freebsd32/freebsd32_proto.h>
52 
53 /* Cannot get exact size in 64-bit due to alignment issue of entire struct. */
54 CTASSERT((sizeof(struct md_ioctl32)+4) == 436);
55 CTASSERT(sizeof(struct ioc_read_toc_entry32) == 8);
56 CTASSERT(sizeof(struct ioc_toc_header32) == 4);
57 
58 
59 static int
60 freebsd32_ioctl_md(struct thread *td, struct freebsd32_ioctl_args *uap,
61     struct file *fp)
62 {
63 	struct md_ioctl mdv;
64 	struct md_ioctl32 md32;
65 	u_long com = 0;
66 	int error;
67 
68 	if (uap->data == NULL)
69 		panic("%s: where is my ioctl data??", __func__);
70 	if (uap->com & IOC_IN) {
71 		if ((error = copyin(uap->data, &md32, sizeof(md32)))) {
72 			fdrop(fp, td);
73 			return (error);
74 		}
75 		CP(md32, mdv, md_version);
76 		CP(md32, mdv, md_unit);
77 		CP(md32, mdv, md_type);
78 		PTRIN_CP(md32, mdv, md_file);
79 		CP(md32, mdv, md_mediasize);
80 		CP(md32, mdv, md_sectorsize);
81 		CP(md32, mdv, md_options);
82 		CP(md32, mdv, md_base);
83 		CP(md32, mdv, md_fwheads);
84 		CP(md32, mdv, md_fwsectors);
85 	} else if (uap->com & IOC_OUT) {
86 		/*
87 		 * Zero the buffer so the user always
88 		 * gets back something deterministic.
89 		 */
90 		bzero(&mdv, sizeof mdv);
91 	}
92 
93 	switch (uap->com) {
94 	case MDIOCATTACH_32:
95 		com = MDIOCATTACH;
96 		break;
97 	case MDIOCDETACH_32:
98 		com = MDIOCDETACH;
99 		break;
100 	case MDIOCQUERY_32:
101 		com = MDIOCQUERY;
102 		break;
103 	case MDIOCLIST_32:
104 		com = MDIOCLIST;
105 		break;
106 	default:
107 		panic("%s: unknown MDIOC %#x", __func__, uap->com);
108 	}
109 	error = fo_ioctl(fp, com, (caddr_t)&mdv, td->td_ucred, td);
110 	if (error == 0 && (com & IOC_OUT)) {
111 		CP(mdv, md32, md_version);
112 		CP(mdv, md32, md_unit);
113 		CP(mdv, md32, md_type);
114 		PTROUT_CP(mdv, md32, md_file);
115 		CP(mdv, md32, md_mediasize);
116 		CP(mdv, md32, md_sectorsize);
117 		CP(mdv, md32, md_options);
118 		CP(mdv, md32, md_base);
119 		CP(mdv, md32, md_fwheads);
120 		CP(mdv, md32, md_fwsectors);
121 		error = copyout(&md32, uap->data, sizeof(md32));
122 	}
123 	fdrop(fp, td);
124 	return error;
125 }
126 
127 
128 static int
129 freebsd32_ioctl_ioc_toc_header(struct thread *td,
130     struct freebsd32_ioctl_args *uap, struct file *fp)
131 {
132 	struct ioc_toc_header toch;
133 	struct ioc_toc_header32 toch32;
134 	int error;
135 
136 	if (uap->data == NULL)
137 		panic("%s: where is my ioctl data??", __func__);
138 
139 	if ((error = copyin(uap->data, &toch32, sizeof(toch32))))
140 		return (error);
141 	CP(toch32, toch, len);
142 	CP(toch32, toch, starting_track);
143 	CP(toch32, toch, ending_track);
144 	error = fo_ioctl(fp, CDIOREADTOCHEADER, (caddr_t)&toch,
145 	    td->td_ucred, td);
146 	fdrop(fp, td);
147 	return (error);
148 }
149 
150 
151 static int
152 freebsd32_ioctl_ioc_read_toc(struct thread *td,
153     struct freebsd32_ioctl_args *uap, struct file *fp)
154 {
155 	struct ioc_read_toc_entry toce;
156 	struct ioc_read_toc_entry32 toce32;
157 	int error;
158 
159 	if (uap->data == NULL)
160 		panic("%s: where is my ioctl data??", __func__);
161 
162 	if ((error = copyin(uap->data, &toce32, sizeof(toce32))))
163 		return (error);
164 	CP(toce32, toce, address_format);
165 	CP(toce32, toce, starting_track);
166 	CP(toce32, toce, data_len);
167 	PTRIN_CP(toce32, toce, data);
168 
169 	if ((error = fo_ioctl(fp, CDIOREADTOCENTRYS, (caddr_t)&toce,
170 	    td->td_ucred, td))) {
171 		CP(toce, toce32, address_format);
172 		CP(toce, toce32, starting_track);
173 		CP(toce, toce32, data_len);
174 		PTROUT_CP(toce, toce32, data);
175 		error = copyout(&toce32, uap->data, sizeof(toce32));
176 	}
177 	fdrop(fp, td);
178 	return error;
179 }
180 
181 
182 int
183 freebsd32_ioctl(struct thread *td, struct freebsd32_ioctl_args *uap)
184 {
185 	struct ioctl_args ap /*{
186 		int	fd;
187 		u_long	com;
188 		caddr_t	data;
189 	}*/ ;
190 	struct file *fp;
191 	int error;
192 
193 	if ((error = fget(td, uap->fd, &fp)) != 0)
194 		return (error);
195 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
196 		fdrop(fp, td);
197 		return (EBADF);
198 	}
199 
200 	switch (uap->com) {
201 	case MDIOCATTACH_32:	/* FALLTHROUGH */
202 	case MDIOCDETACH_32:	/* FALLTHROUGH */
203 	case MDIOCQUERY_32:	/* FALLTHROUGH */
204 	case MDIOCLIST_32:
205 		return freebsd32_ioctl_md(td, uap, fp);
206 
207 	case CDIOREADTOCENTRYS_32:
208 		return freebsd32_ioctl_ioc_read_toc(td, uap, fp);
209 
210 	case CDIOREADTOCHEADER_32:
211 		return freebsd32_ioctl_ioc_toc_header(td, uap, fp);
212 
213 	default:
214 		fdrop(fp, td);
215 		ap.fd = uap->fd;
216 		ap.com = uap->com;
217 		PTRIN_CP(*uap, ap, data);
218 		return ioctl(td, &ap);
219 	}
220 }
221