datetime.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/ieee1275/ieee1275.h>
  21. #include <grub/misc.h>
  22. #include <grub/dl.h>
  23. #if defined (__powerpc__) || defined (__sparc__)
  24. #include <grub/cmos.h>
  25. #endif
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static char *rtc = 0;
  28. static int no_ieee1275_rtc = 0;
  29. /* Helper for find_rtc. */
  30. static int
  31. find_rtc_iter (struct grub_ieee1275_devalias *alias)
  32. {
  33. if (grub_strcmp (alias->type, "rtc") == 0)
  34. {
  35. grub_dprintf ("datetime", "Found RTC %s\n", alias->path);
  36. rtc = grub_strdup (alias->path);
  37. return 1;
  38. }
  39. return 0;
  40. }
  41. static void
  42. find_rtc (void)
  43. {
  44. grub_ieee1275_devices_iterate (find_rtc_iter);
  45. if (!rtc)
  46. no_ieee1275_rtc = 1;
  47. }
  48. grub_err_t
  49. grub_get_datetime (struct grub_datetime *datetime)
  50. {
  51. struct get_time_args
  52. {
  53. struct grub_ieee1275_common_hdr common;
  54. grub_ieee1275_cell_t method;
  55. grub_ieee1275_cell_t device;
  56. grub_ieee1275_cell_t catch_result;
  57. grub_ieee1275_cell_t year;
  58. grub_ieee1275_cell_t month;
  59. grub_ieee1275_cell_t day;
  60. grub_ieee1275_cell_t hour;
  61. grub_ieee1275_cell_t minute;
  62. grub_ieee1275_cell_t second;
  63. }
  64. args;
  65. int status;
  66. grub_ieee1275_ihandle_t ihandle;
  67. if (no_ieee1275_rtc)
  68. return grub_get_datetime_cmos (datetime);
  69. if (!rtc)
  70. find_rtc ();
  71. if (!rtc)
  72. return grub_get_datetime_cmos (datetime);
  73. status = grub_ieee1275_open (rtc, &ihandle);
  74. if (status == -1)
  75. return grub_error (GRUB_ERR_IO, "couldn't open RTC");
  76. INIT_IEEE1275_COMMON (&args.common, "call-method", 2, 7);
  77. args.device = (grub_ieee1275_cell_t) ihandle;
  78. args.method = (grub_ieee1275_cell_t) "get-time";
  79. status = IEEE1275_CALL_ENTRY_FN (&args);
  80. grub_ieee1275_close (ihandle);
  81. if (status == -1 || args.catch_result)
  82. return grub_error (GRUB_ERR_IO, "get-time failed");
  83. datetime->year = args.year;
  84. datetime->month = args.month;
  85. datetime->day = args.day;
  86. datetime->hour = args.hour;
  87. datetime->minute = args.minute;
  88. datetime->second = args.second;
  89. return GRUB_ERR_NONE;
  90. }
  91. grub_err_t
  92. grub_set_datetime (struct grub_datetime *datetime)
  93. {
  94. struct set_time_args
  95. {
  96. struct grub_ieee1275_common_hdr common;
  97. grub_ieee1275_cell_t method;
  98. grub_ieee1275_cell_t device;
  99. grub_ieee1275_cell_t year;
  100. grub_ieee1275_cell_t month;
  101. grub_ieee1275_cell_t day;
  102. grub_ieee1275_cell_t hour;
  103. grub_ieee1275_cell_t minute;
  104. grub_ieee1275_cell_t second;
  105. grub_ieee1275_cell_t catch_result;
  106. }
  107. args;
  108. int status;
  109. grub_ieee1275_ihandle_t ihandle;
  110. if (no_ieee1275_rtc)
  111. return grub_set_datetime_cmos (datetime);
  112. if (!rtc)
  113. find_rtc ();
  114. if (!rtc)
  115. return grub_set_datetime_cmos (datetime);
  116. status = grub_ieee1275_open (rtc, &ihandle);
  117. if (status == -1)
  118. return grub_error (GRUB_ERR_IO, "couldn't open RTC");
  119. INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 1);
  120. args.device = (grub_ieee1275_cell_t) ihandle;
  121. args.method = (grub_ieee1275_cell_t) "set-time";
  122. args.year = datetime->year;
  123. args.month = datetime->month;
  124. args.day = datetime->day;
  125. args.hour = datetime->hour;
  126. args.minute = datetime->minute;
  127. args.second = datetime->second;
  128. status = IEEE1275_CALL_ENTRY_FN (&args);
  129. grub_ieee1275_close (ihandle);
  130. if (status == -1 || args.catch_result)
  131. return grub_error (GRUB_ERR_IO, "set-time failed");
  132. return GRUB_ERR_NONE;
  133. }