Skip to contentSkip to conversation

possible off-by-one error on month counting in rtc rv8803 driver

DISCUSSION Oliver Graute 2 participants 2 messages Apr 25, 2022

lore
Share & tools
lore
#0 lore raw

Hello,

I stumbled across the following possible off-by-one error in counting the month in RTC driver rv8803.

I'am using struct rtc_time to define a EOL date for my U-Boots. So after this date U-Boot stops booting by reading the RTC before with rtc_get(). I'am using the same EOL code for two different imx boards with different RTCs and therefore different RTC drivers. On both boards I have set the same EOL date.

So the EOL Date is 25.09.2022 23:59:59

The board with rv3029 stopps booting on 26.09.2022 0:00:00 The board with rv8803 stopps booting on 26.08.2022 0:00:00

U-Boot Code:
(drivers/rtc/rv3029.c)

v3029_rtc_set()
...
regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
...

rv3029_rtc_get()
...
tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
...

(drivers/rtc/rv8803.c)
rv8803_rtc_set()
...
buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon)
...

rv8803_rtc_get()
...
tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
...

I assume that the error is here and increase and decrease by one is also required here like in the Linux driver code for RTC 8803.

Linux Code:

rv3029_set_time()
...
regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
...

rv3029_read_time()
...
tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
...

rv8803_set_time()
...
date[RV8803_MONTH] = bin2bcd(tm->tm_mon + 1);
...

rv8803_get_time()
...
tm->tm_mon = bcd2bin(date[RV8803_MONTH] & 0x1f) - 1;
...

Can someone verify and comment on this. Then I would prepare a patch later on.

Best Regards,

Oliver

#1 lore raw

Hi,

Am 2022-04-25 14:55, schrieb Oliver Graute:

Quoted 2 lines

I stumbled across the following possible off-by-one error in counting the month in RTC driver rv8803.

..

Quoted 6 lines

(drivers/rtc/rv8803.c)
rv8803_rtc_set()
...
buf[RTC_MON_REG_ADDR] = bin2bcd(tm->tm_mon)
...

Quoted 7 lines

rv8803_rtc_get()
...
tm->tm_mon = bcd2bin(buf[RTC_MON_REG_ADDR] & 0x1F);
...

I assume that the error is here and increase and decrease by one is also required here like in the Linux driver code for RTC 8803.

Indeed. tm_mon has a range from 0 .. 11, but the RTC expects 1..12. Nice catch.

Quoted 2 lines

Can someone verify and comment on this. Then I would prepare a patch later on.

Yes please.

-michael