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
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.
24
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28
* RTC-based entropy source
39
#include <ipxe/entropy.h>
41
/** Maximum time to wait for an RTC interrupt, in milliseconds */
42
#define RTC_MAX_WAIT_MS 100
44
/** RTC interrupt handler */
45
extern void rtc_isr ( void );
47
/** Previous RTC interrupt handler */
48
static struct segoff rtc_old_handler;
50
/** Flag set by RTC interrupt handler */
51
extern volatile uint8_t __text16 ( rtc_flag );
52
#define rtc_flag __use_text16 ( rtc_flag )
55
* Hook RTC interrupt handler
58
static void rtc_hook_isr ( void ) {
60
/* RTC interrupt handler */
61
__asm__ __volatile__ (
62
TEXT16_CODE ( "\nrtc_isr:\n\t"
63
/* Preserve registers */
65
/* Set "interrupt triggered" flag */
66
"movb $0x01, %%cs:rtc_flag\n\t"
67
/* Read RTC status register C to
68
* acknowledge interrupt
74
"movb $0x20, %%al\n\t"
75
"outb %%al, $0xa0\n\t"
76
"outb %%al, $0x20\n\t"
77
/* Restore registers and return */
83
: "i" ( CMOS_ADDRESS ), "i" ( CMOS_DATA ),
84
"i" ( RTC_STATUS_C ) );
86
hook_bios_interrupt ( RTC_INT, ( intptr_t ) rtc_isr, &rtc_old_handler );
90
* Unhook RTC interrupt handler
93
static void rtc_unhook_isr ( void ) {
96
rc = unhook_bios_interrupt ( RTC_INT, ( intptr_t ) rtc_isr,
98
assert ( rc == 0 ); /* Should always be able to unhook */
102
* Enable RTC interrupts
105
static void rtc_enable_int ( void ) {
108
/* Clear any stale pending interrupts via status register C */
109
outb ( ( RTC_STATUS_C | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
112
/* Set Periodic Interrupt Enable bit in status register B */
113
outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
114
status_b = inb ( CMOS_DATA );
115
outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
116
outb ( ( status_b | RTC_STATUS_B_PIE ), CMOS_DATA );
118
/* Re-enable NMI and reset to default address */
119
outb ( CMOS_DEFAULT_ADDRESS, CMOS_ADDRESS );
120
inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
124
* Disable RTC interrupts
127
static void rtc_disable_int ( void ) {
130
/* Clear Periodic Interrupt Enable bit in status register B */
131
outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
132
status_b = inb ( CMOS_DATA );
133
outb ( ( RTC_STATUS_B | CMOS_DISABLE_NMI ), CMOS_ADDRESS );
134
outb ( ( status_b & ~RTC_STATUS_B_PIE ), CMOS_DATA );
136
/* Re-enable NMI and reset to default address */
137
outb ( CMOS_DEFAULT_ADDRESS, CMOS_ADDRESS );
138
inb ( CMOS_DATA ); /* Discard; may be needed on some platforms */
142
* Check that entropy gathering is functional
144
* @ret rc Return status code
146
static int rtc_entropy_check ( void ) {
149
/* Check that RTC interrupts are working */
151
for ( i = 0 ; i < RTC_MAX_WAIT_MS ; i++ ) {
153
/* Allow interrupts to occur */
154
__asm__ __volatile__ ( "sti\n\t"
159
/* Check for RTC interrupt flag */
167
DBGC ( &rtc_flag, "RTC timed out waiting for interrupt\n" );
172
* Enable entropy gathering
174
* @ret rc Return status code
176
static int rtc_entropy_enable ( void ) {
179
/* Hook ISR and enable RTC interrupts */
181
enable_irq ( RTC_IRQ );
184
/* Check that RTC interrupts are working */
185
if ( ( rc = rtc_entropy_check() ) != 0 )
192
disable_irq ( RTC_IRQ );
198
* Disable entropy gathering
201
static void rtc_entropy_disable ( void ) {
203
/* Disable RTC interrupts and unhook ISR */
205
disable_irq ( RTC_IRQ );
210
* Measure a single RTC tick
212
* @ret delta Length of RTC tick (in TSC units)
214
uint8_t rtc_sample ( void ) {
219
__asm__ __volatile__ (
220
REAL_CODE ( /* Enable interrupts */
222
/* Wait for RTC interrupt */
223
"movb %b2, %%cs:rtc_flag\n\t"
225
"xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
228
/* Read "before" TSC */
230
/* Store "before" TSC on stack */
232
/* Wait for another RTC interrupt */
234
"movb %b2, %%cs:rtc_flag\n\t"
236
"xchgb %b2, %%cs:rtc_flag\n\t" /* Serialize */
239
/* Read "after" TSC */
241
/* Retrieve "before" TSC on stack */
243
/* Disable interrupts */
246
: "=a" ( after ), "=d" ( before ), "=Q" ( temp )
249
return ( after - before );
252
PROVIDE_ENTROPY_INLINE ( rtc, min_entropy_per_sample );
253
PROVIDE_ENTROPY ( rtc, entropy_enable, rtc_entropy_enable );
254
PROVIDE_ENTROPY ( rtc, entropy_disable, rtc_entropy_disable );
255
PROVIDE_ENTROPY_INLINE ( rtc, get_noise );