Browse Source

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.
Liav A 4 years ago
parent
commit
517460d3a9
1 changed files with 22 additions and 2 deletions
  1. 22 2
      Kernel/RTC.cpp

+ 22 - 2
Kernel/RTC.cpp

@@ -6,6 +6,7 @@
 
 #include <AK/Time.h>
 #include <Kernel/CMOS.h>
+#include <Kernel/IO.h>
 #include <Kernel/RTC.h>
 
 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);