From 517460d3a9dc88ac4f5fe25df0f3ba6f58cc40d5 Mon Sep 17 00:00:00 2001 From: Liav A Date: Fri, 23 Jul 2021 20:16:24 +0300 Subject: [PATCH] Kernel: Ensure we don't get in an endless loop while querying the CMOS When we try to query the time from the RTC CMOS, we try to check if the CMOS is updated. If it is updated for a long period of time (as a result of hardware malfunction), break the loop and return Unix epoch time. --- Kernel/RTC.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/Kernel/RTC.cpp b/Kernel/RTC.cpp index 1d616615c7..ce0ad50d0e 100644 --- a/Kernel/RTC.cpp +++ b/Kernel/RTC.cpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace RTC { @@ -34,8 +35,27 @@ static u8 bcd_to_binary(u8 bcd) void read_registers(unsigned& year, unsigned& month, unsigned& day, unsigned& hour, unsigned& minute, unsigned& second) { - while (update_in_progress()) - ; + // Note: Let's wait 0.01 seconds until we stop trying to query the RTC CMOS + size_t time_passed_in_milliseconds = 0; + bool update_in_progress_ended_successfully = false; + while (time_passed_in_milliseconds < 100) { + if (!update_in_progress()) { + update_in_progress_ended_successfully = true; + break; + } + IO::delay(1000); + time_passed_in_milliseconds++; + } + + if (!update_in_progress_ended_successfully) { + year = 1970; + month = 1; + day = 1; + hour = 0; + minute = 0; + second = 0; + return; + } u8 status_b = CMOS::read(0x0b);