source: grub-pc/trunk/fuentes/grub-core/lib/cmos_datetime.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 4.0 KB
Line 
1/* kern/cmos_datetime.c - CMOS datetime function.
2 *
3 *  GRUB  --  GRand Unified Bootloader
4 *  Copyright (C) 2008,2009  Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <grub/datetime.h>
21#include <grub/cmos.h>
22#include <grub/dl.h>
23
24GRUB_MOD_LICENSE ("GPLv3+");
25
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
30
31grub_err_t
32grub_get_datetime_cmos (struct grub_datetime *datetime)
33{
34  int is_bcd, is_12hour;
35  grub_uint8_t value, flag;
36  grub_err_t err;
37
38  err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
39  if (err)
40    return err;
41
42  is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
43
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);
49
50  datetime->year = value;
51  datetime->year += (value < 80) ? 2000 : 1900;
52
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);
58
59  datetime->month = value;
60
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);
66
67  datetime->day = value;
68
69  is_12hour = ! (flag & GRUB_CMOS_STATUS_B_24HOUR);
70
71  err = grub_cmos_read (GRUB_CMOS_INDEX_HOUR, &value);
72  if (err)
73    return err;
74  if (is_12hour)
75    {
76      is_12hour = (value & 0x80);
77
78      value &= 0x7F;
79      value--;
80    }
81
82  if (is_bcd)
83    value = grub_bcd_to_num (value);
84
85  if (is_12hour)
86    value += 12;
87
88  datetime->hour = value;
89
90  err = grub_cmos_read (GRUB_CMOS_INDEX_MINUTE, &value);
91  if (err)
92    return err;
93
94  if (is_bcd)
95    value = grub_bcd_to_num (value);
96
97  datetime->minute = value;
98
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);
104
105  datetime->second = value;
106
107  return 0;
108}
109
110grub_err_t
111grub_set_datetime_cmos (struct grub_datetime *datetime)
112{
113  int is_bcd, is_12hour;
114  grub_uint8_t value, flag;
115  grub_err_t err;
116
117  err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
118  if (err)
119    return err;
120
121  is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
122
123  value = ((datetime->year >= 2000) ? datetime->year - 2000 :
124           datetime->year - 1900);
125
126  if (is_bcd)
127    value = grub_num_to_bcd (value);
128
129  err = grub_cmos_write (GRUB_CMOS_INDEX_YEAR, value);
130  if (err)
131    return err;
132
133  value = datetime->month;
134
135  if (is_bcd)
136    value = grub_num_to_bcd (value);
137
138  err = grub_cmos_write (GRUB_CMOS_INDEX_MONTH, value);
139  if (err)
140    return err;
141
142  value = datetime->day;
143
144  if (is_bcd)
145    value = grub_num_to_bcd (value);
146
147  err = grub_cmos_write (GRUB_CMOS_INDEX_DAY_OF_MONTH, value);
148  if (err)
149    return err;
150
151  value = datetime->hour;
152
153  is_12hour = (! (flag & GRUB_CMOS_STATUS_B_24HOUR));
154
155  if (is_12hour)
156    {
157      value++;
158
159      if (value > 12)
160        value -= 12;
161      else
162        is_12hour = 0;
163    }
164
165  if (is_bcd)
166    value = grub_num_to_bcd (value);
167
168  if (is_12hour)
169    value |= 0x80;
170
171  err = grub_cmos_write (GRUB_CMOS_INDEX_HOUR, value);
172  if (err)
173    return err;
174
175  value = datetime->minute;
176
177  if (is_bcd)
178    value = grub_num_to_bcd (value);
179
180  err = grub_cmos_write (GRUB_CMOS_INDEX_MINUTE, value);
181  if (err)
182    return err;
183
184  value = datetime->second;
185
186  if (is_bcd)
187    value = grub_num_to_bcd (value);
188
189  err = grub_cmos_write (GRUB_CMOS_INDEX_SECOND, value);
190  if (err)
191    return err;
192
193  return 0;
194}
Note: See TracBrowser for help on using the repository browser.