Changeset e5fbe06 in mainline for uspace/drv/time/cmos-rtc/cmos-rtc.c


Ignore:
Timestamp:
2012-04-18T00:17:52Z (12 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f4ebaf3c
Parents:
f004318
Message:

rtc: Check the rtc epoch in rtc_tm_sanity_check()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/time/cmos-rtc/cmos-rtc.c

    rf004318 re5fbe06  
    9090    ipc_callid_t callid, ipc_call_t *call);
    9191static int rtc_dev_remove(ddf_dev_t *dev);
    92 static int rtc_tm_sanity_check(struct tm *t);
     92static int rtc_tm_sanity_check(struct tm *t, int epoch);
    9393static void rtc_register_write(rtc_t *rtc, int reg, int data);
    9494static bool is_leap_year(int year);
     
    287287        bool bcd_mode;
    288288        bool pm_mode = false;
     289        int  epoch = 1900;
    289290        rtc_t *rtc = RTC_FROM_FNODE(fun);
    290291
     
    348349
    349350        if (t->tm_year < 100) {
    350                 /* tm_year is the number of years since 1900, it is not
    351                  * possible it is < 100.
     351                /* tm_year is the number of years since 1900 but the
     352                 * RTC epoch is 2000.
    352353                 */
     354                epoch = 2000;
    353355                t->tm_year += 100;
    354356        }
     
    356358        fibril_mutex_unlock(&rtc->mutex);
    357359
    358         return rtc_tm_sanity_check(t);
     360        return rtc_tm_sanity_check(t, epoch);
    359361}
    360362
     
    373375        int  reg_b;
    374376        int  reg_a;
     377        int  epoch;
    375378        rtc_t *rtc = RTC_FROM_FNODE(fun);
    376379
    377         rc = rtc_tm_sanity_check(t);
    378         if (rc != EOK)
     380        fibril_mutex_lock(&rtc->mutex);
     381
     382        /* Detect the RTC epoch */
     383        if (rtc_register_read(rtc, RTC_YEAR) < 100)
     384                epoch = 2000;
     385        else
     386                epoch = 1900;
     387
     388        rc = rtc_tm_sanity_check(t, epoch);
     389        if (rc != EOK) {
     390                fibril_mutex_unlock(&rtc->mutex);
    379391                return rc;
     392        }
    380393
    381394        t->tm_mon++; /* counts from 1, not from 0 */
    382 
    383         fibril_mutex_lock(&rtc->mutex);
    384395
    385396        reg_b = rtc_register_read(rtc, RTC_STATUS_B);
     
    435446 *
    436447 * @param t     The tm structure to check
     448 * @param epoch The RTC epoch year
    437449 *
    438450 * @return      EOK on success or EINVAL
    439451 */
    440452static int
    441 rtc_tm_sanity_check(struct tm *t)
     453rtc_tm_sanity_check(struct tm *t, int epoch)
    442454{
    443455        int ndays;
     
    453465        else if (t->tm_mon < 0 || t->tm_mon > 11)
    454466                return EINVAL;
     467        else if (epoch == 2000 && t->tm_year < 100)
     468                return EINVAL;
    455469        else if (t->tm_year < 0)
    456470                return EINVAL;
Note: See TracChangeset for help on using the changeset viewer.