From 1724b74f693812b48fa7e36e69174b271f3b5310 Mon Sep 17 00:00:00 2001 From: Werner Zeh Date: Tue, 25 May 2021 14:19:50 +0200 Subject: lib/rtc: Add sanity check for time and date Add a function to check sanity of a given RTC date and time. Invalid values in terms of overrun ranges of the registers can lead to strange issues in the OS. Change-Id: I0a381d445c894eee4f82b50fe86dad22cc587605 Signed-off-by: Werner Zeh Reviewed-on: https://review.coreboot.org/c/coreboot/+/54913 Tested-by: build bot (Jenkins) Reviewed-by: Arthur Heymans Reviewed-by: Paul Menzel --- src/include/rtc.h | 1 + src/lib/rtc.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) (limited to 'src') diff --git a/src/include/rtc.h b/src/include/rtc.h index 0a380745d3..6d562f76b0 100644 --- a/src/include/rtc.h +++ b/src/include/rtc.h @@ -21,5 +21,6 @@ int rtc_get(struct rtc_time *time); int rtc_to_tm(int tim, struct rtc_time *tm); unsigned long rtc_mktime(const struct rtc_time *tm); void rtc_display(const struct rtc_time *tm); +int rtc_invalid(const struct rtc_time *tm); #endif /* _RTC_H_ */ diff --git a/src/lib/rtc.c b/src/lib/rtc.c index c5fffa0e8b..d15148024d 100644 --- a/src/lib/rtc.c +++ b/src/lib/rtc.c @@ -121,3 +121,19 @@ void rtc_display(const struct rtc_time *tm) (tm->wday < 0 || tm->wday > 6) ? "unknown " : weekdays[tm->wday], tm->hour, tm->min, tm->sec); } + +static int rtc_month_days(unsigned int month, unsigned int year) +{ + int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + return month_days[month] + (LEAP_YEAR(year) && month == 2); +} + +int rtc_invalid(const struct rtc_time *tm) +{ + if (tm->sec > 59 || tm->min > 59 || tm->hour > 23 || tm->mon == 0 || tm->mon > 12 || + tm->year < 1970 || tm->mday > rtc_month_days(tm->mon - 1, tm->year)) + return 1; + else + return 0; +} -- cgit v1.2.3