cmos_datetime.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include <grub/dl.h>
  22. GRUB_MOD_LICENSE ("GPLv3+");
  23. #if !defined (__powerpc__) && !defined (__sparc__)
  24. #define grub_get_datetime_cmos grub_get_datetime
  25. #define grub_set_datetime_cmos grub_set_datetime
  26. #endif
  27. grub_err_t
  28. grub_get_datetime_cmos (struct grub_datetime *datetime)
  29. {
  30. int is_bcd, is_12hour;
  31. grub_uint8_t value, flag;
  32. grub_err_t err;
  33. err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
  34. if (err)
  35. return err;
  36. is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
  37. err = grub_cmos_read (GRUB_CMOS_INDEX_YEAR, &value);
  38. if (err)
  39. return err;
  40. if (is_bcd)
  41. value = grub_bcd_to_num (value);
  42. datetime->year = value;
  43. datetime->year += (value < 80) ? 2000 : 1900;
  44. err = grub_cmos_read (GRUB_CMOS_INDEX_MONTH, &value);
  45. if (err)
  46. return err;
  47. if (is_bcd)
  48. value = grub_bcd_to_num (value);
  49. datetime->month = value;
  50. err = grub_cmos_read (GRUB_CMOS_INDEX_DAY_OF_MONTH, &value);
  51. if (err)
  52. return err;
  53. if (is_bcd)
  54. value = grub_bcd_to_num (value);
  55. datetime->day = value;
  56. is_12hour = ! (flag & GRUB_CMOS_STATUS_B_24HOUR);
  57. err = grub_cmos_read (GRUB_CMOS_INDEX_HOUR, &value);
  58. if (err)
  59. return err;
  60. if (is_12hour)
  61. {
  62. is_12hour = (value & 0x80);
  63. value &= 0x7F;
  64. value--;
  65. }
  66. if (is_bcd)
  67. value = grub_bcd_to_num (value);
  68. if (is_12hour)
  69. value += 12;
  70. datetime->hour = value;
  71. err = grub_cmos_read (GRUB_CMOS_INDEX_MINUTE, &value);
  72. if (err)
  73. return err;
  74. if (is_bcd)
  75. value = grub_bcd_to_num (value);
  76. datetime->minute = value;
  77. err = grub_cmos_read (GRUB_CMOS_INDEX_SECOND, &value);
  78. if (err)
  79. return err;
  80. if (is_bcd)
  81. value = grub_bcd_to_num (value);
  82. datetime->second = value;
  83. return 0;
  84. }
  85. grub_err_t
  86. grub_set_datetime_cmos (struct grub_datetime *datetime)
  87. {
  88. int is_bcd, is_12hour;
  89. grub_uint8_t value, flag;
  90. grub_err_t err;
  91. err = grub_cmos_read (GRUB_CMOS_INDEX_STATUS_B, &flag);
  92. if (err)
  93. return err;
  94. is_bcd = ! (flag & GRUB_CMOS_STATUS_B_BINARY);
  95. value = ((datetime->year >= 2000) ? datetime->year - 2000 :
  96. datetime->year - 1900);
  97. if (is_bcd)
  98. value = grub_num_to_bcd (value);
  99. err = grub_cmos_write (GRUB_CMOS_INDEX_YEAR, value);
  100. if (err)
  101. return err;
  102. value = datetime->month;
  103. if (is_bcd)
  104. value = grub_num_to_bcd (value);
  105. err = grub_cmos_write (GRUB_CMOS_INDEX_MONTH, value);
  106. if (err)
  107. return err;
  108. value = datetime->day;
  109. if (is_bcd)
  110. value = grub_num_to_bcd (value);
  111. err = grub_cmos_write (GRUB_CMOS_INDEX_DAY_OF_MONTH, value);
  112. if (err)
  113. return err;
  114. value = datetime->hour;
  115. is_12hour = (! (flag & GRUB_CMOS_STATUS_B_24HOUR));
  116. if (is_12hour)
  117. {
  118. value++;
  119. if (value > 12)
  120. value -= 12;
  121. else
  122. is_12hour = 0;
  123. }
  124. if (is_bcd)
  125. value = grub_num_to_bcd (value);
  126. if (is_12hour)
  127. value |= 0x80;
  128. err = grub_cmos_write (GRUB_CMOS_INDEX_HOUR, value);
  129. if (err)
  130. return err;
  131. value = datetime->minute;
  132. if (is_bcd)
  133. value = grub_num_to_bcd (value);
  134. err = grub_cmos_write (GRUB_CMOS_INDEX_MINUTE, value);
  135. if (err)
  136. return err;
  137. value = datetime->second;
  138. if (is_bcd)
  139. value = grub_num_to_bcd (value);
  140. err = grub_cmos_write (GRUB_CMOS_INDEX_SECOND, value);
  141. if (err)
  142. return err;
  143. return 0;
  144. }