2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * Linux entropy source
30 #include <linux_api.h>
31 #include <ipxe/entropy.h>
33 /** Entropy source filename */
34 static const char entropy_filename[] = "/dev/random";
36 /** Entropy source file handle */
37 static int entropy_fd;
40 * Enable entropy gathering
42 * @ret rc Return status code
44 static int linux_entropy_enable ( void ) {
46 /* Open entropy source */
47 entropy_fd = linux_open ( entropy_filename, O_RDONLY );
48 if ( entropy_fd < 0 ) {
49 DBGC ( &entropy_fd, "ENTROPY could not open %s: %s\n",
50 entropy_filename, linux_strerror ( linux_errno ) );
58 * Disable entropy gathering
61 static void linux_entropy_disable ( void ) {
63 /* Close entropy source */
64 linux_close ( entropy_fd );
70 * @ret noise Noise sample
71 * @ret rc Return status code
73 static int linux_get_noise ( noise_sample_t *noise ) {
77 /* Read a single byte from entropy source */
78 len = linux_read ( entropy_fd, &byte, sizeof ( byte ) );
80 DBGC ( &entropy_fd, "ENTROPY could not read from %s: %s\n",
81 entropy_filename, linux_strerror ( linux_errno ) );
85 DBGC ( &entropy_fd, "ENTROPY EOF on reading from %s: %s\n",
86 entropy_filename, linux_strerror ( linux_errno ) );
94 PROVIDE_ENTROPY_INLINE ( linux, min_entropy_per_sample );
95 PROVIDE_ENTROPY ( linux, entropy_enable, linux_entropy_enable );
96 PROVIDE_ENTROPY ( linux, entropy_disable, linux_entropy_disable );
97 PROVIDE_ENTROPY ( linux, get_noise, linux_get_noise );