[ipoib] Simplify test for received broadcast packets
[ipxe.git] / src / interface / linux / linux_entropy.c
1 /*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * Linux entropy source
29 *
30 */
31
32 #include <stdint.h>
33 #include <errno.h>
34 #include <linux_api.h>
35 #include <ipxe/entropy.h>
36
37 /** Entropy source filename */
38 static const char entropy_filename[] = "/dev/random";
39
40 /** Entropy source file handle */
41 static int entropy_fd;
42
43 /**
44 * Enable entropy gathering
45 *
46 * @ret rc Return status code
47 */
48 static int linux_entropy_enable ( void ) {
49
50 /* Open entropy source */
51 entropy_fd = linux_open ( entropy_filename, O_RDONLY );
52 if ( entropy_fd < 0 ) {
53 DBGC ( &entropy_fd, "ENTROPY could not open %s: %s\n",
54 entropy_filename, linux_strerror ( linux_errno ) );
55 return entropy_fd;
56 }
57
58 return 0;
59 }
60
61 /**
62 * Disable entropy gathering
63 *
64 */
65 static void linux_entropy_disable ( void ) {
66
67 /* Close entropy source */
68 linux_close ( entropy_fd );
69 }
70
71 /**
72 * Get noise sample
73 *
74 * @ret noise Noise sample
75 * @ret rc Return status code
76 */
77 static int linux_get_noise ( noise_sample_t *noise ) {
78 uint8_t byte;
79 ssize_t len;
80
81 /* Read a single byte from entropy source */
82 len = linux_read ( entropy_fd, &byte, sizeof ( byte ) );
83 if ( len < 0 ) {
84 DBGC ( &entropy_fd, "ENTROPY could not read from %s: %s\n",
85 entropy_filename, linux_strerror ( linux_errno ) );
86 return len;
87 }
88 if ( len == 0 ) {
89 DBGC ( &entropy_fd, "ENTROPY EOF on reading from %s: %s\n",
90 entropy_filename, linux_strerror ( linux_errno ) );
91 return -EPIPE;
92 }
93 *noise = byte;
94
95 return 0;
96 }
97
98 PROVIDE_ENTROPY_INLINE ( linux, min_entropy_per_sample );
99 PROVIDE_ENTROPY ( linux, entropy_enable, linux_entropy_enable );
100 PROVIDE_ENTROPY ( linux, entropy_disable, linux_entropy_disable );
101 PROVIDE_ENTROPY ( linux, get_noise, linux_get_noise );