cmos_datetime.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include <grub/datetime.h>
  20. #include <grub/cmos.h>
  21. GRUB_EXPORT(grub_get_datetime);
  22. GRUB_EXPORT(grub_set_datetime);
  23. grub_err_t
  24. grub_get_datetime (struct grub_datetime *datetime)
  25. {
  26. int is_bcd, is_12hour;
  27. grub_uint8_t value, flag;
  28. flag = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B);
  29. is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
  30. value = grub_cmos_read (GRUB_CMOS_INDEX_YEAR);
  31. if (is_bcd)
  32. value = grub_bcd_to_num (value);
  33. datetime->year = value;
  34. datetime->year += (value < 80) ? 2000 : 1900;
  35. value = grub_cmos_read (GRUB_CMOS_INDEX_MONTH);
  36. if (is_bcd)
  37. value = grub_bcd_to_num (value);
  38. datetime->month = value;
  39. value = grub_cmos_read (GRUB_CMOS_INDEX_DAY_OF_MONTH);
  40. if (is_bcd)
  41. value = grub_bcd_to_num (value);
  42. datetime->day = value;
  43. is_12hour = ! (flag & GRUB_CMOS_STATUS_B_24HOUR);
  44. value = grub_cmos_read (GRUB_CMOS_INDEX_HOUR);
  45. if (is_12hour)
  46. {
  47. is_12hour = (value & 0x80);
  48. value &= 0x7F;
  49. value--;
  50. }
  51. if (is_bcd)
  52. value = grub_bcd_to_num (value);
  53. if (is_12hour)
  54. value += 12;
  55. datetime->hour = value;
  56. value = grub_cmos_read (GRUB_CMOS_INDEX_MINUTE);
  57. if (is_bcd)
  58. value = grub_bcd_to_num (value);
  59. datetime->minute = value;
  60. value = grub_cmos_read (GRUB_CMOS_INDEX_SECOND);
  61. if (is_bcd)
  62. value = grub_bcd_to_num (value);
  63. datetime->second = value;
  64. return 0;
  65. }
  66. grub_err_t
  67. grub_set_datetime (struct grub_datetime *datetime)
  68. {
  69. int is_bcd, is_12hour;
  70. grub_uint8_t value, flag;
  71. flag = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B);
  72. is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
  73. value = ((datetime->year >= 2000) ? datetime->year - 2000 :
  74. datetime->year - 1900);
  75. if (is_bcd)
  76. value = grub_num_to_bcd (value);
  77. grub_cmos_write (GRUB_CMOS_INDEX_YEAR, value);
  78. value = datetime->month;
  79. if (is_bcd)
  80. value = grub_num_to_bcd (value);
  81. grub_cmos_write (GRUB_CMOS_INDEX_MONTH, value);
  82. value = datetime->day;
  83. if (is_bcd)
  84. value = grub_num_to_bcd (value);
  85. grub_cmos_write (GRUB_CMOS_INDEX_DAY_OF_MONTH, value);
  86. value = datetime->hour;
  87. is_12hour = (! (flag & GRUB_CMOS_STATUS_B_24HOUR));
  88. if (is_12hour)
  89. {
  90. value++;
  91. if (value > 12)
  92. value -= 12;
  93. else
  94. is_12hour = 0;
  95. }
  96. if (is_bcd)
  97. value = grub_num_to_bcd (value);
  98. if (is_12hour)
  99. value |= 0x80;
  100. grub_cmos_write (GRUB_CMOS_INDEX_HOUR, value);
  101. value = datetime->minute;
  102. if (is_bcd)
  103. value = grub_num_to_bcd (value);
  104. grub_cmos_write (GRUB_CMOS_INDEX_MINUTE, value);
  105. value = datetime->second;
  106. if (is_bcd)
  107. value = grub_num_to_bcd (value);
  108. grub_cmos_write (GRUB_CMOS_INDEX_SECOND, value);
  109. return 0;
  110. }