cmos_datetime.c 4.03 KB
Newer Older
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 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

20
#include <grub/datetime.h>
21
#include <grub/cmos.h>
22 23 24
#include <grub/dl.h>

GRUB_MOD_LICENSE ("GPLv3+");
25

26 27 28 29 30
#if !defined (__powerpc__) && !defined (__sparc__)
#define grub_get_datetime_cmos grub_get_datetime
#define grub_set_datetime_cmos grub_set_datetime
#endif

31
grub_err_t
32
grub_get_datetime_cmos (struct grub_datetime *datetime)
33 34 35
{
  int is_bcd, is_12hour;
  grub_uint8_t value, flag;
36
  grub_err_t err;
37

38 39 40
  err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
  if (err)
    return err;
41 42 43

  is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);

44 45 46
  err = grub_cmos_read (GRUB_CMOS_INDEX_YEAR, &value);
  if (err)
    return err;
47 48 49 50 51 52
  if (is_bcd)
    value = grub_bcd_to_num (value);

  datetime->year = value;
  datetime->year += (value < 80) ? 2000 : 1900;

53 54 55
  err = grub_cmos_read (GRUB_CMOS_INDEX_MONTH, &value);
  if (err)
    return err;
56 57 58 59 60
  if (is_bcd)
    value = grub_bcd_to_num (value);

  datetime->month = value;

61 62 63
  err = grub_cmos_read (GRUB_CMOS_INDEX_DAY_OF_MONTH, &value);
  if (err)
    return err;
64 65 66 67 68 69 70
  if (is_bcd)
    value = grub_bcd_to_num (value);

  datetime->day = value;

  is_12hour = ! (flag & GRUB_CMOS_STATUS_B_24HOUR);

71 72 73
  err = grub_cmos_read (GRUB_CMOS_INDEX_HOUR, &value);
  if (err)
    return err;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  if (is_12hour)
    {
      is_12hour = (value & 0x80);

      value &= 0x7F;
      value--;
    }

  if (is_bcd)
    value = grub_bcd_to_num (value);

  if (is_12hour)
    value += 12;

  datetime->hour = value;

90 91 92 93
  err = grub_cmos_read (GRUB_CMOS_INDEX_MINUTE, &value);
  if (err)
    return err;

94 95 96 97 98
  if (is_bcd)
    value = grub_bcd_to_num (value);

  datetime->minute = value;

99 100 101
  err = grub_cmos_read (GRUB_CMOS_INDEX_SECOND, &value);
  if (err)
    return err;
102 103 104 105 106 107 108 109 110
  if (is_bcd)
    value = grub_bcd_to_num (value);

  datetime->second = value;

  return 0;
}

grub_err_t
111
grub_set_datetime_cmos (struct grub_datetime *datetime)
112 113 114
{
  int is_bcd, is_12hour;
  grub_uint8_t value, flag;
115
  grub_err_t err;
116

117 118 119
  err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
  if (err)
    return err;
120 121 122 123 124 125 126 127 128

  is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);

  value = ((datetime->year >= 2000) ? datetime->year - 2000 :
           datetime->year - 1900);

  if (is_bcd)
    value = grub_num_to_bcd (value);

129 130 131
  err = grub_cmos_write (GRUB_CMOS_INDEX_YEAR, value);
  if (err)
    return err;
132 133 134 135 136 137

  value = datetime->month;

  if (is_bcd)
    value = grub_num_to_bcd (value);

138 139 140
  err = grub_cmos_write (GRUB_CMOS_INDEX_MONTH, value);
  if (err)
    return err;
141 142 143 144 145 146

  value = datetime->day;

  if (is_bcd)
    value = grub_num_to_bcd (value);

147 148 149
  err = grub_cmos_write (GRUB_CMOS_INDEX_DAY_OF_MONTH, value);
  if (err)
    return err;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  value = datetime->hour;

  is_12hour = (! (flag & GRUB_CMOS_STATUS_B_24HOUR));

  if (is_12hour)
    {
      value++;

      if (value > 12)
        value -= 12;
      else
        is_12hour = 0;
    }

  if (is_bcd)
    value = grub_num_to_bcd (value);

  if (is_12hour)
    value |= 0x80;

171 172 173
  err = grub_cmos_write (GRUB_CMOS_INDEX_HOUR, value);
  if (err)
    return err;
174 175 176 177 178 179

  value = datetime->minute;

  if (is_bcd)
    value = grub_num_to_bcd (value);

180 181 182
  err = grub_cmos_write (GRUB_CMOS_INDEX_MINUTE, value);
  if (err)
    return err;
183 184 185 186 187 188

  value = datetime->second;

  if (is_bcd)
    value = grub_num_to_bcd (value);

189 190 191
  err = grub_cmos_write (GRUB_CMOS_INDEX_SECOND, value);
  if (err)
    return err;
192 193 194

  return 0;
}