:: commit f31ed9c495e47be6caee268fef52004ae1d00295

mintsuki <mintsuki@protonmail.com> — 2022-03-08 13:26

parents: 33b52eb339

time: Work around potential issue with day wraparound

diff --git a/common/lib/time.c b/common/lib/time.c
index 19e59b52..7678a07d 100644
--- a/common/lib/time.c
+++ b/common/lib/time.c
@@ -28,8 +28,10 @@ static uint64_t get_unix_epoch(uint8_t seconds, uint8_t minutes, uint8_t  hours,
 
 #if bios == 1
 uint64_t time(void) {
-    struct rm_regs r = {0};
+    struct rm_regs r;
 
+again:
+    r = (struct rm_regs){0};
     r.eax = 0x0400;
     rm_int(0x1a, &r, &r);
 
@@ -38,6 +40,7 @@ uint64_t time(void) {
     uint16_t year   = bcd_to_int( r.ecx & 0x00ff) +
     /* century */     bcd_to_int((r.ecx & 0xff00) >> 8) * 100;
 
+    r = (struct rm_regs){0};
     r.eax = 0x0200;
     rm_int(0x1a, &r, &r);
 
@@ -45,6 +48,14 @@ uint64_t time(void) {
     uint8_t minute  = bcd_to_int( r.ecx & 0x00ff);
     uint8_t hour    = bcd_to_int((r.ecx & 0xff00) >> 8);
 
+    // Check RTC day wraparound
+    r = (struct rm_regs){0};
+    r.eax = 0x0400;
+    rm_int(0x1a, &r, &r);
+    if (bcd_to_int(r.edx & 0x00ff) != day) {
+        goto again;
+    }
+
     return get_unix_epoch(second, minute, hour, day, month, year);
 }
 #endif
tab: 248 wrap: offon