1 /* This file contains the device dependent part of the drivers for the
2 * following special files:
3 * /dev/random - random number generator
6 #include <minix/drivers.h>
7 #include <minix/chardriver.h>
8 #include <minix/type.h>
13 #define NR_DEVS 1 /* number of minor devices */
14 # define RANDOM_DEV 0 /* minor device for /dev/random */
16 #define KRANDOM_PERIOD 1 /* ticks between krandom calls */
18 static struct device m_geom
[NR_DEVS
]; /* base and size of each device */
19 static dev_t m_device
; /* current device */
21 extern int errno
; /* error number for PM calls */
23 static struct device
*r_prepare(dev_t device
);
24 static ssize_t
r_read(devminor_t minor
, u64_t position
, endpoint_t endpt
,
25 cp_grant_id_t grant
, size_t size
, int flags
, cdev_id_t id
);
26 static ssize_t
r_write(devminor_t minor
, u64_t position
, endpoint_t endpt
,
27 cp_grant_id_t grant
, size_t size
, int flags
, cdev_id_t id
);
28 static int r_open(devminor_t minor
, int access
, endpoint_t user_endpt
);
29 static void r_random(clock_t stamp
);
30 static void r_updatebin(int source
, struct k_randomness_bin
*rb
);
31 static int r_select(devminor_t
, unsigned int, endpoint_t
);
33 /* Entry points to this driver. */
34 static struct chardriver r_dtab
= {
35 .cdr_open
= r_open
, /* open device */
36 .cdr_read
= r_read
, /* read from device */
37 .cdr_write
= r_write
, /* write to device (seeding it) */
38 .cdr_select
= r_select
, /* select hook */
39 .cdr_alarm
= r_random
/* get randomness from kernel (alarm) */
42 /* select requestor */
43 static endpoint_t random_select
= NONE
;
45 /* Buffer for the /dev/random number generator. */
46 #define RANDOM_BUF_SIZE 1024
47 static char random_buf
[RANDOM_BUF_SIZE
];
49 /* SEF functions and variables. */
50 static void sef_local_startup(void);
51 static int sef_cb_init_fresh(int type
, sef_init_info_t
*info
);
53 /*===========================================================================*
55 *===========================================================================*/
58 /* SEF local startup. */
61 /* Call the generic receive loop. */
62 chardriver_task(&r_dtab
);
67 /*===========================================================================*
69 *===========================================================================*/
70 static void sef_local_startup()
72 /* Register init callbacks. */
73 sef_setcb_init_fresh(sef_cb_init_fresh
);
74 sef_setcb_init_restart(sef_cb_init_fresh
);
76 /* Let SEF perform startup. */
80 /*===========================================================================*
82 *===========================================================================*/
83 static int sef_cb_init_fresh(int UNUSED(type
), sef_init_info_t
*UNUSED(info
))
85 /* Initialize the random driver. */
86 static struct k_randomness krandom
;
90 r_random(0); /* also set periodic timer */
92 /* Retrieve first randomness buffer with parameters. */
93 if (OK
!= (s
=sys_getrandomness(&krandom
))) {
94 printf("RANDOM: sys_getrandomness failed: %d\n", s
);
98 /* Do sanity check on parameters. */
99 if(krandom
.random_sources
!= RANDOM_SOURCES
||
100 krandom
.random_elements
!= RANDOM_ELEMENTS
) {
101 printf("random: parameters (%d, %d) don't match kernel's (%d, %d)\n",
102 RANDOM_SOURCES
, RANDOM_ELEMENTS
,
103 krandom
.random_sources
, krandom
.random_elements
);
107 /* Feed initial batch. */
108 for(i
= 0; i
< RANDOM_SOURCES
; i
++)
109 r_updatebin(i
, &krandom
.bin
[i
]);
111 /* Announce we are up! */
112 chardriver_announce();
117 /*===========================================================================*
119 *===========================================================================*/
120 static ssize_t
r_read(devminor_t minor
, u64_t
UNUSED(position
),
121 endpoint_t endpt
, cp_grant_id_t grant
, size_t size
, int UNUSED(flags
),
122 cdev_id_t
UNUSED(id
))
124 /* Read from one of the driver's minor devices. */
125 size_t offset
, chunk
;
128 if (minor
!= RANDOM_DEV
) return(EIO
);
130 if (!random_isseeded()) return(EAGAIN
);
132 for (offset
= 0; offset
< size
; offset
+= chunk
) {
133 chunk
= MIN(size
- offset
, RANDOM_BUF_SIZE
);
134 random_getbytes(random_buf
, chunk
);
135 r
= sys_safecopyto(endpt
, grant
, offset
, (vir_bytes
)random_buf
, chunk
);
137 printf("random: sys_safecopyto failed for proc %d, grant %d\n",
146 /*===========================================================================*
148 *===========================================================================*/
149 static ssize_t
r_write(devminor_t minor
, u64_t
UNUSED(position
),
150 endpoint_t endpt
, cp_grant_id_t grant
, size_t size
, int UNUSED(flags
),
151 cdev_id_t
UNUSED(id
))
153 /* Write to one of the driver's minor devices. */
154 size_t offset
, chunk
;
157 if (minor
!= RANDOM_DEV
) return(EIO
);
159 for (offset
= 0; offset
< size
; offset
+= chunk
) {
160 chunk
= MIN(size
- offset
, RANDOM_BUF_SIZE
);
161 r
= sys_safecopyfrom(endpt
, grant
, offset
, (vir_bytes
)random_buf
,
164 printf("random: sys_safecopyfrom failed for proc %d,"
165 " grant %d\n", endpt
, grant
);
168 random_putbytes(random_buf
, chunk
);
174 /*===========================================================================*
176 *===========================================================================*/
177 static int r_open(devminor_t minor
, int access
, endpoint_t
UNUSED(user_endpt
))
179 /* Check device number on open.
182 if (minor
< 0 || minor
>= NR_DEVS
) return(ENXIO
);
187 #define UPDATE(binnumber, bp, startitem, elems) { \
189 int n = elems, item = startitem;\
191 assert(binnumber >= 0 && binnumber < RANDOM_SOURCES); \
192 assert(item >= 0 && item < RANDOM_ELEMENTS); \
195 assert(high >= item); \
196 assert(high >= 0 && high < RANDOM_ELEMENTS); \
197 r = &bp->r_buf[item]; \
198 random_update(binnumber, r, n); \
202 /*===========================================================================*
204 *===========================================================================*/
205 static void r_updatebin(int source
, struct k_randomness_bin
*rb
)
207 int r_next
, r_size
, r_high
;
212 assert(r_next
>= 0 && r_next
< RANDOM_ELEMENTS
);
213 assert(r_size
>= 0 && r_size
<= RANDOM_ELEMENTS
);
215 r_high
= r_next
+r_size
;
217 if (r_high
<= RANDOM_ELEMENTS
) {
218 UPDATE(source
, rb
, r_next
, r_size
);
220 assert(r_next
< RANDOM_ELEMENTS
);
221 UPDATE(source
, rb
, r_next
, RANDOM_ELEMENTS
-r_next
);
222 UPDATE(source
, rb
, 0, r_high
-RANDOM_ELEMENTS
);
228 /*===========================================================================*
230 *===========================================================================*/
231 static void r_random(clock_t UNUSED(stamp
))
233 /* Fetch random information from the kernel to update /dev/random. */
236 static struct k_randomness_bin krandom_bin
;
239 int nextperiod
= random_isseeded() ? KRANDOM_PERIOD
*500 : KRANDOM_PERIOD
;
241 bin
= (bin
+1) % RANDOM_SOURCES
;
243 if(sys_getrandom_bin(&krandom_bin
, bin
) == OK
)
244 r_updatebin(bin
, &krandom_bin
);
246 /* Add our own timing source. */
249 random_update(RND_TIMING
, &r
, 1);
251 /* Schedule new alarm for next m_random call. */
252 if (OK
!= (s
=sys_setalarm(nextperiod
, 0)))
253 printf("RANDOM: sys_setalarm failed: %d\n", s
);
256 /*===========================================================================*
258 *===========================================================================*/
259 static int r_select(devminor_t minor
, unsigned int ops
, endpoint_t ep
)
261 /* random device is always writable; it's infinitely readable
262 * once seeded, and doesn't block when it's not, so all operations
263 * are instantly possible. we ignore CDEV_OP_ERR.
266 if (minor
!= RANDOM_DEV
) return(EIO
);
267 return ops
& (CDEV_OP_RD
| CDEV_OP_WR
);