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