tpm2_key_protector: Enable build for powerpc_ieee1275
[grub.git] / grub-core / lib / cmos_datetime.c
blob86cd911804281ea24feaa21556e68b6756dcfec0
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>
22 #include <grub/dl.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
29 #endif
31 grub_err_t
32 grub_get_datetime_cmos (struct grub_datetime *datetime)
34 int is_bcd, is_12hour;
35 grub_uint8_t value, flag;
36 grub_err_t err;
38 err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
39 if (err)
40 return err;
42 is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
44 err = grub_cmos_read (GRUB_CMOS_INDEX_YEAR, &value);
45 if (err)
46 return err;
47 if (is_bcd)
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);
54 if (err)
55 return err;
56 if (is_bcd)
57 value = grub_bcd_to_num (value);
59 datetime->month = value;
61 err = grub_cmos_read (GRUB_CMOS_INDEX_DAY_OF_MONTH, &value);
62 if (err)
63 return err;
64 if (is_bcd)
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);
72 if (err)
73 return err;
74 if (is_12hour)
76 is_12hour = (value & 0x80);
78 value &= 0x7F;
79 value--;
82 if (is_bcd)
83 value = grub_bcd_to_num (value);
85 if (is_12hour)
86 value += 12;
88 datetime->hour = value;
90 err = grub_cmos_read (GRUB_CMOS_INDEX_MINUTE, &value);
91 if (err)
92 return err;
94 if (is_bcd)
95 value = grub_bcd_to_num (value);
97 datetime->minute = value;
99 err = grub_cmos_read (GRUB_CMOS_INDEX_SECOND, &value);
100 if (err)
101 return err;
102 if (is_bcd)
103 value = grub_bcd_to_num (value);
105 datetime->second = value;
107 return 0;
110 grub_err_t
111 grub_set_datetime_cmos (struct grub_datetime *datetime)
113 int is_bcd, is_12hour;
114 grub_uint8_t value, flag;
115 grub_err_t err;
117 err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
118 if (err)
119 return err;
121 is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
123 value = ((datetime->year >= 2000) ? datetime->year - 2000 :
124 datetime->year - 1900);
126 if (is_bcd)
127 value = grub_num_to_bcd (value);
129 err = grub_cmos_write (GRUB_CMOS_INDEX_YEAR, value);
130 if (err)
131 return err;
133 value = datetime->month;
135 if (is_bcd)
136 value = grub_num_to_bcd (value);
138 err = grub_cmos_write (GRUB_CMOS_INDEX_MONTH, value);
139 if (err)
140 return err;
142 value = datetime->day;
144 if (is_bcd)
145 value = grub_num_to_bcd (value);
147 err = grub_cmos_write (GRUB_CMOS_INDEX_DAY_OF_MONTH, value);
148 if (err)
149 return err;
151 value = datetime->hour;
153 is_12hour = (! (flag & GRUB_CMOS_STATUS_B_24HOUR));
155 if (is_12hour)
157 value++;
159 if (value > 12)
160 value -= 12;
161 else
162 is_12hour = 0;
165 if (is_bcd)
166 value = grub_num_to_bcd (value);
168 if (is_12hour)
169 value |= 0x80;
171 err = grub_cmos_write (GRUB_CMOS_INDEX_HOUR, value);
172 if (err)
173 return err;
175 value = datetime->minute;
177 if (is_bcd)
178 value = grub_num_to_bcd (value);
180 err = grub_cmos_write (GRUB_CMOS_INDEX_MINUTE, value);
181 if (err)
182 return err;
184 value = datetime->second;
186 if (is_bcd)
187 value = grub_num_to_bcd (value);
189 err = grub_cmos_write (GRUB_CMOS_INDEX_SECOND, value);
190 if (err)
191 return err;
193 return 0;