random.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  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 University 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 UNIVERSITY 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 UNIVERSITY 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. #include <types.h>
  30. #include <kern/errno.h>
  31. #include <kern/fcntl.h>
  32. #include <lib.h>
  33. #include <uio.h>
  34. #include <vfs.h>
  35. #include <generic/random.h>
  36. #include "autoconf.h"
  37. /*
  38. * Machine-independent generic randomness device.
  39. *
  40. * Remembers something that's a random source, and provides random()
  41. * and randmax() to the rest of the kernel.
  42. *
  43. * The kernel config mechanism can be used to explicitly choose which
  44. * of the available random sources to use, if more than one is
  45. * available.
  46. */
  47. static struct random_softc *the_random = NULL;
  48. /*
  49. * VFS device functions.
  50. * open: allow reading only.
  51. */
  52. static
  53. int
  54. randopen(struct device *dev, int openflags)
  55. {
  56. (void)dev;
  57. if (openflags != O_RDONLY) {
  58. return EIO;
  59. }
  60. return 0;
  61. }
  62. /*
  63. * VFS close function.
  64. */
  65. static
  66. int
  67. randclose(struct device *dev)
  68. {
  69. (void)dev;
  70. return 0;
  71. }
  72. /*
  73. * VFS I/O function. Hand off to implementation.
  74. */
  75. static
  76. int
  77. randio(struct device *dev, struct uio *uio)
  78. {
  79. struct random_softc *rs = dev->d_data;
  80. if (uio->uio_rw != UIO_READ) {
  81. return EIO;
  82. }
  83. return rs->rs_read(rs->rs_devdata, uio);
  84. }
  85. /*
  86. * VFS ioctl function.
  87. */
  88. static
  89. int
  90. randioctl(struct device *dev, int op, userptr_t data)
  91. {
  92. /*
  93. * We don't support any ioctls.
  94. */
  95. (void)dev;
  96. (void)op;
  97. (void)data;
  98. return EIOCTL;
  99. }
  100. /*
  101. * Config function.
  102. */
  103. int
  104. config_random(struct random_softc *rs, int unit)
  105. {
  106. int result;
  107. /* We use only the first random device. */
  108. if (unit!=0) {
  109. return ENODEV;
  110. }
  111. KASSERT(the_random==NULL);
  112. the_random = rs;
  113. rs->rs_dev.d_open = randopen;
  114. rs->rs_dev.d_close = randclose;
  115. rs->rs_dev.d_io = randio;
  116. rs->rs_dev.d_ioctl = randioctl;
  117. rs->rs_dev.d_blocks = 0;
  118. rs->rs_dev.d_blocksize = 1;
  119. rs->rs_dev.d_data = rs;
  120. /* Add the VFS device structure to the VFS device list. */
  121. result = vfs_adddev("random", &rs->rs_dev, 0);
  122. if (result) {
  123. return result;
  124. }
  125. return 0;
  126. }
  127. /*
  128. * Random number functions exported to the rest of the kernel.
  129. */
  130. uint32_t
  131. random(void)
  132. {
  133. if (the_random==NULL) {
  134. panic("No random device\n");
  135. }
  136. return the_random->rs_random(the_random->rs_devdata);
  137. }
  138. uint32_t
  139. randmax(void)
  140. {
  141. if (the_random==NULL) {
  142. panic("No random device\n");
  143. }
  144. return the_random->rs_randmax(the_random->rs_devdata);
  145. }