1 /*	$NetBSD: netbsd32_rndpseudo_50.c,v 1.6 2021/01/19 03:20:13 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Michael Graff <explorer@flame.org> and Thor Lancelot Simon.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: netbsd32_rndpseudo_50.c,v 1.6 2021/01/19 03:20:13 simonb Exp $");
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #include "opt_compat_netbsd32.h"
38 #endif
39 
40 #include <sys/param.h>
41 #include <sys/file.h>
42 
43 #include <compat/netbsd32/netbsd32.h>
44 #include <compat/sys/rnd.h>
45 
46 /*
47  * Convert from rndsource_t to rndsource50_t, for the results from
48  * RNDGETNUM50 and RNDGETNAME50.
49  */
50 
51 /*
52  * Convert from rndsource_t to rndsource50_32_t, for the results from
53  * RNDGETNUM50_32 and RNDGETNAME50_32.
54  */
55 static void
rndsource_to_rndsource50_32(rndsource_t * r,rndsource50_32_t * r50_32)56 rndsource_to_rndsource50_32(rndsource_t *r, rndsource50_32_t *r50_32)
57 {
58 	memset(r50_32, 0, sizeof(*r50_32));
59 	strlcpy(r50_32->name, r->name, sizeof(r50_32->name));
60 	r50_32->total = r->total;
61 	r50_32->type = r->type;
62 	r50_32->flags = r->flags;
63 }
64 
65 /*
66  * COMPAT32_50 handling for rnd_ioctl.  This is called from rnd_ioctl.
67  *
68  * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32).
69  */
70 int
compat32_50_rnd_ioctl(struct file * fp,u_long cmd,void * addr)71 compat32_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr)
72 {
73 	int ret = 0;
74 
75 	switch (cmd) {
76 	case RNDGETSRCNUM50_32:
77 	{
78 		rndstat_t rstbuf = {.start = 0};
79 		rndstat50_32_t *rst50_32 = (rndstat50_32_t *)addr;
80 		size_t count;
81 
82 		if (rst50_32->count > RND_MAXSTATCOUNT50)
83 			return EINVAL;
84 
85 		rstbuf.start = rst50_32->start;
86 		rstbuf.count = rst50_32->count;
87 
88 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf);
89 		if (ret != 0)
90 			return ret;
91 
92 		for (count = 0; count < rst50_32->count; count++) {
93 			rndsource_to_rndsource50_32(&rstbuf.source[count],
94 			    &rst50_32->source[count]);
95 		}
96 		rst50_32->count = rstbuf.count;
97 
98 		break;
99 	}
100 
101 	case RNDGETSRCNAME50_32:
102 	{
103 		rndstat_name_t rstnmbuf = {.name[0] = 0};
104 		rndstat_name50_32_t *rstnm50_32;
105 		rstnm50_32 = (rndstat_name50_32_t *)addr;
106 
107 		strlcpy(rstnmbuf.name, rstnm50_32->name, sizeof(rstnmbuf.name));
108 
109 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf);
110 		if (ret != 0)
111 			return ret;
112 
113 		rndsource_to_rndsource50_32(&rstnmbuf.source,
114 		    &rstnm50_32->source);
115 
116 		break;
117 	}
118 
119 	default:
120 		return ENOTTY;
121 	}
122 
123 	return ret;
124 }
125