1 /* kern/cmos_datetime.c - CMOS datetime function.
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/datetime.h>
21 #include <grub/cmos.h>
24 GRUB_MOD_LICENSE ("GPLv3+");
26 #if !defined (__powerpc__) && !defined (__sparc__)
27 #define grub_get_datetime_cmos grub_get_datetime
28 #define grub_set_datetime_cmos grub_set_datetime
32 grub_get_datetime_cmos (struct grub_datetime
*datetime
)
34 int is_bcd
, is_12hour
;
35 grub_uint8_t value
, flag
;
38 err
= grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B
, &flag
);
42 is_bcd
= ! (flag
& GRUB_CMOS_STATUS_B_BINARY
);
44 err
= grub_cmos_read (GRUB_CMOS_INDEX_YEAR
, &value
);
48 value
= grub_bcd_to_num (value
);
50 datetime
->year
= value
;
51 datetime
->year
+= (value
< 80) ? 2000 : 1900;
53 err
= grub_cmos_read (GRUB_CMOS_INDEX_MONTH
, &value
);
57 value
= grub_bcd_to_num (value
);
59 datetime
->month
= value
;
61 err
= grub_cmos_read (GRUB_CMOS_INDEX_DAY_OF_MONTH
, &value
);
65 value
= grub_bcd_to_num (value
);
67 datetime
->day
= value
;
69 is_12hour
= ! (flag
& GRUB_CMOS_STATUS_B_24HOUR
);
71 err
= grub_cmos_read (GRUB_CMOS_INDEX_HOUR
, &value
);
76 is_12hour
= (value
& 0x80);
83 value
= grub_bcd_to_num (value
);
88 datetime
->hour
= value
;
90 err
= grub_cmos_read (GRUB_CMOS_INDEX_MINUTE
, &value
);
95 value
= grub_bcd_to_num (value
);
97 datetime
->minute
= value
;
99 err
= grub_cmos_read (GRUB_CMOS_INDEX_SECOND
, &value
);
103 value
= grub_bcd_to_num (value
);
105 datetime
->second
= value
;
111 grub_set_datetime_cmos (struct grub_datetime
*datetime
)
113 int is_bcd
, is_12hour
;
114 grub_uint8_t value
, flag
;
117 err
= grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B
, &flag
);
121 is_bcd
= ! (flag
& GRUB_CMOS_STATUS_B_BINARY
);
123 value
= ((datetime
->year
>= 2000) ? datetime
->year
- 2000 :
124 datetime
->year
- 1900);
127 value
= grub_num_to_bcd (value
);
129 err
= grub_cmos_write (GRUB_CMOS_INDEX_YEAR
, value
);
133 value
= datetime
->month
;
136 value
= grub_num_to_bcd (value
);
138 err
= grub_cmos_write (GRUB_CMOS_INDEX_MONTH
, value
);
142 value
= datetime
->day
;
145 value
= grub_num_to_bcd (value
);
147 err
= grub_cmos_write (GRUB_CMOS_INDEX_DAY_OF_MONTH
, value
);
151 value
= datetime
->hour
;
153 is_12hour
= (! (flag
& GRUB_CMOS_STATUS_B_24HOUR
));
166 value
= grub_num_to_bcd (value
);
171 err
= grub_cmos_write (GRUB_CMOS_INDEX_HOUR
, value
);
175 value
= datetime
->minute
;
178 value
= grub_num_to_bcd (value
);
180 err
= grub_cmos_write (GRUB_CMOS_INDEX_MINUTE
, value
);
184 value
= datetime
->second
;
187 value
= grub_num_to_bcd (value
);
189 err
= grub_cmos_write (GRUB_CMOS_INDEX_SECOND
, value
);