xref: /netbsd-src/sys/compat/common/rndpseudo_50.c (revision 6bf9fb1500e41eae5c10150bbbd1e377aac0c43c)
1*6bf9fb15Sriastradh /*	$NetBSD: rndpseudo_50.c,v 1.7 2020/04/30 03:30:10 riastradh Exp $	*/
2381a8142Sapb 
3381a8142Sapb /*-
4381a8142Sapb  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
5381a8142Sapb  * All rights reserved.
6381a8142Sapb  *
7381a8142Sapb  * This code is derived from software contributed to The NetBSD Foundation
8381a8142Sapb  * by Michael Graff <explorer@flame.org> and Thor Lancelot Simon.
9381a8142Sapb  *
10381a8142Sapb  * Redistribution and use in source and binary forms, with or without
11381a8142Sapb  * modification, are permitted provided that the following conditions
12381a8142Sapb  * are met:
13381a8142Sapb  * 1. Redistributions of source code must retain the above copyright
14381a8142Sapb  *    notice, this list of conditions and the following disclaimer.
15381a8142Sapb  * 2. Redistributions in binary form must reproduce the above copyright
16381a8142Sapb  *    notice, this list of conditions and the following disclaimer in the
17381a8142Sapb  *    documentation and/or other materials provided with the distribution.
18381a8142Sapb  *
19381a8142Sapb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20381a8142Sapb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21381a8142Sapb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22381a8142Sapb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23381a8142Sapb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24381a8142Sapb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25381a8142Sapb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26381a8142Sapb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27381a8142Sapb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28381a8142Sapb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29381a8142Sapb  * POSSIBILITY OF SUCH DAMAGE.
30381a8142Sapb  */
31381a8142Sapb 
32381a8142Sapb #include <sys/cdefs.h>
33*6bf9fb15Sriastradh __KERNEL_RCSID(0, "$NetBSD: rndpseudo_50.c,v 1.7 2020/04/30 03:30:10 riastradh Exp $");
34381a8142Sapb 
35381a8142Sapb #if defined(_KERNEL_OPT)
36381a8142Sapb #include "opt_compat_netbsd.h"
37381a8142Sapb #endif
38381a8142Sapb 
39381a8142Sapb #include <sys/param.h>
40381a8142Sapb #include <sys/file.h>
41d91f98a8Spgoyette #include <sys/module_hook.h>
42d91f98a8Spgoyette #include <sys/compat_stub.h>
43d91f98a8Spgoyette 
44381a8142Sapb #include <compat/sys/rnd.h>
45d91f98a8Spgoyette #include <compat/common/compat_mod.h>
46381a8142Sapb 
47381a8142Sapb /*
48381a8142Sapb  * Convert from rndsource_t to rndsource50_t, for the results from
49381a8142Sapb  * RNDGETNUM50 and RNDGETNAME50.
50381a8142Sapb  */
51381a8142Sapb static void
rndsource_to_rndsource50(rndsource_t * r,rndsource50_t * r50)52381a8142Sapb rndsource_to_rndsource50(rndsource_t *r, rndsource50_t *r50)
53381a8142Sapb {
54381a8142Sapb 	memset(r50, 0, sizeof(*r50));
55381a8142Sapb 	strlcpy(r50->name, r->name, sizeof(r50->name));
56381a8142Sapb 	r50->total = r->total;
57381a8142Sapb 	r50->type = r->type;
58381a8142Sapb 	r50->flags = r->flags;
59381a8142Sapb }
60381a8142Sapb 
61381a8142Sapb /*
62381a8142Sapb  * COMPAT_50 handling for rnd_ioctl.  This is called from rnd_ioctl.
63381a8142Sapb  *
64381a8142Sapb  * It also handles the case of (COMPAT_50 && COMPAT_NETBSD32).
65381a8142Sapb  */
66381a8142Sapb int
compat_50_rnd_ioctl(struct file * fp,u_long cmd,void * addr)67381a8142Sapb compat_50_rnd_ioctl(struct file *fp, u_long cmd, void *addr)
68381a8142Sapb {
69381a8142Sapb 	int ret = 0;
70381a8142Sapb 
71381a8142Sapb 	switch (cmd) {
72381a8142Sapb 
73381a8142Sapb 	case RNDGETSRCNUM50:
74381a8142Sapb 	{
75381a8142Sapb 		rndstat_t rstbuf = {.start = 0};
76381a8142Sapb 		rndstat50_t *rst50 = (rndstat50_t *)addr;
77535ba13aSchristos 		size_t count;
78381a8142Sapb 
79381a8142Sapb 		if (rst50->count > RND_MAXSTATCOUNT50)
80381a8142Sapb 			return EINVAL;
81381a8142Sapb 
82381a8142Sapb 		rstbuf.start = rst50->start;
83381a8142Sapb 		rstbuf.count = rst50->count;
84381a8142Sapb 
85381a8142Sapb 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNUM, &rstbuf);
86381a8142Sapb 		if (ret != 0)
87381a8142Sapb 			return ret;
88381a8142Sapb 
89381a8142Sapb 		for (count = 0; count < rst50->count; count++) {
90381a8142Sapb 			rndsource_to_rndsource50(&rstbuf.source[count],
91381a8142Sapb 			    &rst50->source[count]);
92381a8142Sapb 		}
93381a8142Sapb 		rst50->count = rstbuf.count;
94381a8142Sapb 
95381a8142Sapb 		break;
96381a8142Sapb 	}
97381a8142Sapb 
98381a8142Sapb 	case RNDGETSRCNAME50:
99381a8142Sapb 	{
100381a8142Sapb 		rndstat_name_t rstnmbuf = {.name[0] = 0};
101381a8142Sapb 		rndstat_name50_t *rstnm50;
102381a8142Sapb 		rstnm50 = (rndstat_name50_t *)addr;
103381a8142Sapb 
104381a8142Sapb 		strlcpy(rstnmbuf.name, rstnm50->name, sizeof(rstnmbuf.name));
105381a8142Sapb 
106381a8142Sapb 		ret = (fp->f_ops->fo_ioctl)(fp, RNDGETSRCNAME, &rstnmbuf);
107381a8142Sapb 		if (ret != 0)
108381a8142Sapb 			return ret;
109381a8142Sapb 
110381a8142Sapb 		rndsource_to_rndsource50(&rstnmbuf.source, &rstnm50->source);
111381a8142Sapb 
112381a8142Sapb 		break;
113381a8142Sapb 	}
114381a8142Sapb 
115381a8142Sapb 	default:
116381a8142Sapb 		return ENOTTY;
117381a8142Sapb 	}
118381a8142Sapb 
119381a8142Sapb 	return ret;
120381a8142Sapb }
121d91f98a8Spgoyette 
122d91f98a8Spgoyette void
rndpseudo_50_init(void)123d91f98a8Spgoyette rndpseudo_50_init(void)
124d91f98a8Spgoyette {
125d91f98a8Spgoyette 
1268a031a1dSpgoyette 	MODULE_HOOK_SET(rnd_ioctl_50_hook, compat_50_rnd_ioctl);
127d91f98a8Spgoyette }
128d91f98a8Spgoyette 
129d91f98a8Spgoyette void
rndpseudo_50_fini(void)130d91f98a8Spgoyette rndpseudo_50_fini(void)
131d91f98a8Spgoyette {
132d91f98a8Spgoyette 
1338c2f80f1Spgoyette 	MODULE_HOOK_UNSET(rnd_ioctl_50_hook);
134d91f98a8Spgoyette }
135