Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / 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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Linux entropy source
25  *
26  */
27
28 #include <stdint.h>
29 #include <errno.h>
30 #include <linux_api.h>
31 #include <ipxe/entropy.h>
32
33 /** Entropy source filename */
34 static const char entropy_filename[] = "/dev/random";
35
36 /** Entropy source file handle */
37 static int entropy_fd;
38
39 /**
40  * Enable entropy gathering
41  *
42  * @ret rc              Return status code
43  */
44 static int linux_entropy_enable ( void ) {
45
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 ) );
51                 return entropy_fd;
52         }
53
54         return 0;
55 }
56
57 /**
58  * Disable entropy gathering
59  *
60  */
61 static void linux_entropy_disable ( void ) {
62
63         /* Close entropy source */
64         linux_close ( entropy_fd );
65 }
66
67 /**
68  * Get noise sample
69  *
70  * @ret noise           Noise sample
71  * @ret rc              Return status code
72  */
73 static int linux_get_noise ( noise_sample_t *noise ) {
74         uint8_t byte;
75         ssize_t len;
76
77         /* Read a single byte from entropy source */
78         len = linux_read ( entropy_fd, &byte, sizeof ( byte ) );
79         if ( len < 0 ) {
80                 DBGC ( &entropy_fd, "ENTROPY could not read from %s: %s\n",
81                        entropy_filename, linux_strerror ( linux_errno ) );
82                 return len;
83         }
84         if ( len == 0 ) {
85                 DBGC ( &entropy_fd, "ENTROPY EOF on reading from %s: %s\n",
86                        entropy_filename, linux_strerror ( linux_errno ) );
87                 return -EPIPE;
88         }
89         *noise = byte;
90
91         return 0;
92 }
93
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 );