iPXE
Functions
rtc_time.c File Reference

RTC-based time source. More...

#include <stdint.h>
#include <time.h>
#include <rtc.h>
#include <ipxe/time.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static unsigned int rtc_readb (int address)
 Read RTC register. More...
 
static int rtc_is_busy (void)
 Check if RTC update is in progress. More...
 
static unsigned int rtc_readb_bcd (int address)
 Read RTC BCD register. More...
 
static time_t rtc_read_time (void)
 Read RTC time. More...
 
static time_t rtc_now (void)
 Get current time in seconds. More...
 
 PROVIDE_TIME (rtc, time_now, rtc_now)
 

Detailed Description

RTC-based time source.

Definition in file rtc_time.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ rtc_readb()

static unsigned int rtc_readb ( int  address)
static

Read RTC register.

Parameters
addressRegister address
Return values
dataData

Definition at line 43 of file rtc_time.c.

43  {
45  return inb ( CMOS_DATA );
46 }
uint64_t address
Base address.
Definition: ena.h:24
#define CMOS_DATA
CMOS/RTC data register.
Definition: rtc.h:33
uint8_t inb(volatile uint8_t *io_addr)
Read byte from I/O-mapped device.
#define outb(data, io_addr)
Definition: io.h:309
#define CMOS_ADDRESS
CMOS/RTC address (and NMI) register.
Definition: rtc.h:27

References address, CMOS_ADDRESS, CMOS_DATA, inb(), and outb.

Referenced by rtc_is_busy(), rtc_read_time(), and rtc_readb_bcd().

◆ rtc_is_busy()

static int rtc_is_busy ( void  )
static

Check if RTC update is in progress.

Return values
is_busyRTC update is in progress

Definition at line 53 of file rtc_time.c.

53  {
55 }
#define RTC_STATUS_A
RTC status register A.
Definition: rtc.h:57
#define RTC_STATUS_A_UPDATE_IN_PROGRESS
RTC update in progress bit.
Definition: rtc.h:60
static unsigned int rtc_readb(int address)
Read RTC register.
Definition: rtc_time.c:43

References rtc_readb(), RTC_STATUS_A, and RTC_STATUS_A_UPDATE_IN_PROGRESS.

Referenced by rtc_read_time().

◆ rtc_readb_bcd()

static unsigned int rtc_readb_bcd ( int  address)
static

Read RTC BCD register.

Parameters
addressRegister address
Return values
valueValue

Definition at line 63 of file rtc_time.c.

63  {
64  unsigned int bcd;
65 
66  bcd = rtc_readb ( address );
67  return ( bcd - ( 6 * ( bcd >> 4 ) ) );
68 }
uint64_t address
Base address.
Definition: ena.h:24
static unsigned int rtc_readb(int address)
Read RTC register.
Definition: rtc_time.c:43

References address, and rtc_readb().

Referenced by rtc_read_time().

◆ rtc_read_time()

static time_t rtc_read_time ( void  )
static

Read RTC time.

Return values
timeTime, in seconds

Definition at line 75 of file rtc_time.c.

75  {
76  unsigned int status_b;
77  int is_binary;
78  int is_24hour;
79  unsigned int ( * read_component ) ( int address );
80  struct tm tm;
81  int is_pm;
82  unsigned int hour;
83  time_t time;
84 
85  /* Wait for any in-progress update to complete */
86  while ( rtc_is_busy() ) {}
87 
88  /* Determine RTC mode */
89  status_b = rtc_readb ( RTC_STATUS_B );
90  is_binary = ( status_b & RTC_STATUS_B_BINARY );
91  is_24hour = ( status_b & RTC_STATUS_B_24_HOUR );
92  read_component = ( is_binary ? rtc_readb : rtc_readb_bcd );
93 
94  /* Read time values */
95  tm.tm_sec = read_component ( RTC_SEC );
96  tm.tm_min = read_component ( RTC_MIN );
97  hour = read_component ( RTC_HOUR );
98  if ( ! is_24hour ) {
99  is_pm = ( hour >= 80 );
100  hour = ( ( ( ( hour & 0x7f ) % 80 ) % 12 ) +
101  ( is_pm ? 12 : 0 ) );
102  }
103  tm.tm_hour = hour;
104  tm.tm_mday = read_component ( RTC_MDAY );
105  tm.tm_mon = ( read_component ( RTC_MON ) - 1 );
106  tm.tm_year = ( read_component ( RTC_YEAR ) +
107  100 /* Assume we are in the 21st century, since
108  * this code was written in 2012 */ );
109 
110  DBGC ( RTC_STATUS_A, "RTCTIME is %04d-%02d-%02d %02d:%02d:%02d "
111  "(%s,%d-hour)\n", ( tm.tm_year + 1900 ), ( tm.tm_mon + 1 ),
113  ( is_binary ? "binary" : "BCD" ), ( is_24hour ? 24 : 12 ) );
114 
115  /* Convert to seconds since the Epoch */
116  time = mktime ( &tm );
117 
118  return time;
119 }
int tm_min
Minutes [0,59].
Definition: time.h:19
#define RTC_STATUS_B_BINARY
RTC binary mode bit.
Definition: rtc.h:69
#define RTC_STATUS_A
RTC status register A.
Definition: rtc.h:57
uint64_t address
Base address.
Definition: ena.h:24
#define RTC_STATUS_B_24_HOUR
RTC 24 hour format bit.
Definition: rtc.h:66
int tm_mday
Day of month [1,31].
Definition: time.h:23
#define DBGC(...)
Definition: compiler.h:505
#define RTC_MDAY
RTC day of month.
Definition: rtc.h:48
#define RTC_HOUR
RTC hours.
Definition: rtc.h:42
int tm_year
Years since 1900.
Definition: time.h:27
static int rtc_is_busy(void)
Check if RTC update is in progress.
Definition: rtc_time.c:53
#define RTC_STATUS_B
RTC status register B.
Definition: rtc.h:63
static unsigned int rtc_readb_bcd(int address)
Read RTC BCD register.
Definition: rtc_time.c:63
int tm_mon
Month of year [0,11].
Definition: time.h:25
static unsigned int rtc_readb(int address)
Read RTC register.
Definition: rtc_time.c:43
#define RTC_YEAR
RTC year.
Definition: rtc.h:54
time_t mktime(struct tm *tm)
Calculate seconds since the Epoch.
Definition: time.c:117
#define RTC_SEC
RTC seconds.
Definition: rtc.h:36
Broken-down time.
Definition: time.h:15
int tm_sec
Seconds [0,60].
Definition: time.h:17
int tm_hour
Hour [0,23].
Definition: time.h:21
int64_t time_t
Seconds since the Epoch.
Definition: time.h:18
uint64_t time
Current time.
Definition: ntlm.h:20
#define RTC_MON
RTC month.
Definition: rtc.h:51
#define RTC_MIN
RTC minutes.
Definition: rtc.h:39

References address, DBGC, mktime(), RTC_HOUR, rtc_is_busy(), RTC_MDAY, RTC_MIN, RTC_MON, rtc_readb(), rtc_readb_bcd(), RTC_SEC, RTC_STATUS_A, RTC_STATUS_B, RTC_STATUS_B_24_HOUR, RTC_STATUS_B_BINARY, RTC_YEAR, time, tm::tm_hour, tm::tm_mday, tm::tm_min, tm::tm_mon, tm::tm_sec, and tm::tm_year.

Referenced by rtc_now().

◆ rtc_now()

static time_t rtc_now ( void  )
static

Get current time in seconds.

Return values
timeTime, in seconds

Definition at line 126 of file rtc_time.c.

126  {
127  time_t time = 0;
128  time_t last_time;
129 
130  /* Read time until we get two matching values in a row, in
131  * case we end up reading a corrupted value in the middle of
132  * an update.
133  */
134  do {
135  last_time = time;
136  time = rtc_read_time();
137  } while ( time != last_time );
138 
139  return time;
140 }
static time_t rtc_read_time(void)
Read RTC time.
Definition: rtc_time.c:75
int64_t time_t
Seconds since the Epoch.
Definition: time.h:18
uint64_t time
Current time.
Definition: ntlm.h:20

References rtc_read_time(), and time.

◆ PROVIDE_TIME()

PROVIDE_TIME ( rtc  ,
time_now  ,
rtc_now   
)