cmd_date.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * RTC, Date & Time support: get and set date & time
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <rtc.h>
  29. #include <i2c.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. const char *weekdays[] = {
  32. "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  33. };
  34. #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
  35. int mk_date (char *, struct rtc_time *);
  36. int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  37. {
  38. struct rtc_time tm;
  39. int rcode = 0;
  40. int old_bus;
  41. /* switch to correct I2C bus */
  42. old_bus = I2C_GET_BUS();
  43. I2C_SET_BUS(CFG_RTC_BUS_NUM);
  44. switch (argc) {
  45. case 2: /* set date & time */
  46. if (strcmp(argv[1],"reset") == 0) {
  47. puts ("Reset RTC...\n");
  48. rtc_reset ();
  49. } else {
  50. /* initialize tm with current time */
  51. rtc_get (&tm);
  52. /* insert new date & time */
  53. if (mk_date (argv[1], &tm) != 0) {
  54. puts ("## Bad date format\n");
  55. break;
  56. }
  57. /* and write to RTC */
  58. rtc_set (&tm);
  59. }
  60. /* FALL TROUGH */
  61. case 1: /* get date & time */
  62. rtc_get (&tm);
  63. printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
  64. tm.tm_year, tm.tm_mon, tm.tm_mday,
  65. (tm.tm_wday<0 || tm.tm_wday>6) ?
  66. "unknown " : RELOC(weekdays[tm.tm_wday]),
  67. tm.tm_hour, tm.tm_min, tm.tm_sec);
  68. break;
  69. default:
  70. printf ("Usage:\n%s\n", cmdtp->usage);
  71. rcode = 1;
  72. }
  73. /* switch back to original I2C bus */
  74. I2C_SET_BUS(old_bus);
  75. return rcode;
  76. }
  77. /*
  78. * simple conversion of two-digit string with error checking
  79. */
  80. static int cnvrt2 (char *str, int *valp)
  81. {
  82. int val;
  83. if ((*str < '0') || (*str > '9'))
  84. return (-1);
  85. val = *str - '0';
  86. ++str;
  87. if ((*str < '0') || (*str > '9'))
  88. return (-1);
  89. *valp = 10 * val + (*str - '0');
  90. return (0);
  91. }
  92. /*
  93. * Convert date string: MMDDhhmm[[CC]YY][.ss]
  94. *
  95. * Some basic checking for valid values is done, but this will not catch
  96. * all possible error conditions.
  97. */
  98. int mk_date (char *datestr, struct rtc_time *tmp)
  99. {
  100. int len, val;
  101. char *ptr;
  102. ptr = strchr (datestr,'.');
  103. len = strlen (datestr);
  104. /* Set seconds */
  105. if (ptr) {
  106. int sec;
  107. *ptr++ = '\0';
  108. if ((len - (ptr - datestr)) != 2)
  109. return (-1);
  110. len = strlen (datestr);
  111. if (cnvrt2 (ptr, &sec))
  112. return (-1);
  113. tmp->tm_sec = sec;
  114. } else {
  115. tmp->tm_sec = 0;
  116. }
  117. if (len == 12) { /* MMDDhhmmCCYY */
  118. int year, century;
  119. if (cnvrt2 (datestr+ 8, &century) ||
  120. cnvrt2 (datestr+10, &year) ) {
  121. return (-1);
  122. }
  123. tmp->tm_year = 100 * century + year;
  124. } else if (len == 10) { /* MMDDhhmmYY */
  125. int year, century;
  126. century = tmp->tm_year / 100;
  127. if (cnvrt2 (datestr+ 8, &year))
  128. return (-1);
  129. tmp->tm_year = 100 * century + year;
  130. }
  131. switch (len) {
  132. case 8: /* MMDDhhmm */
  133. /* fall thru */
  134. case 10: /* MMDDhhmmYY */
  135. /* fall thru */
  136. case 12: /* MMDDhhmmCCYY */
  137. if (cnvrt2 (datestr+0, &val) ||
  138. val > 12) {
  139. break;
  140. }
  141. tmp->tm_mon = val;
  142. if (cnvrt2 (datestr+2, &val) ||
  143. val > ((tmp->tm_mon==2) ? 29 : 31)) {
  144. break;
  145. }
  146. tmp->tm_mday = val;
  147. if (cnvrt2 (datestr+4, &val) ||
  148. val > 23) {
  149. break;
  150. }
  151. tmp->tm_hour = val;
  152. if (cnvrt2 (datestr+6, &val) ||
  153. val > 59) {
  154. break;
  155. }
  156. tmp->tm_min = val;
  157. /* calculate day of week */
  158. GregorianDay (tmp);
  159. return (0);
  160. default:
  161. break;
  162. }
  163. return (-1);
  164. }
  165. /***************************************************/
  166. U_BOOT_CMD(
  167. date, 2, 1, do_date,
  168. "date - get/set/reset date & time\n",
  169. "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
  170. " - without arguments: print date & time\n"
  171. " - with numeric argument: set the system date & time\n"
  172. " - with 'reset' argument: reset the RTC\n"
  173. );