cmd_date.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #if (CONFIG_COMMANDS & CFG_CMD_DATE)
  32. const char *weekdays[] = {
  33. "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  34. };
  35. #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
  36. int mk_date (char *, struct rtc_time *);
  37. int do_date (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  38. {
  39. struct rtc_time tm;
  40. int rcode = 0;
  41. int old_bus;
  42. /* switch to correct I2C bus */
  43. old_bus = I2C_GET_BUS();
  44. I2C_SET_BUS(CFG_RTC_BUS_NUM);
  45. switch (argc) {
  46. case 2: /* set date & time */
  47. if (strcmp(argv[1],"reset") == 0) {
  48. puts ("Reset RTC...\n");
  49. rtc_reset ();
  50. } else {
  51. /* initialize tm with current time */
  52. rtc_get (&tm);
  53. /* insert new date & time */
  54. if (mk_date (argv[1], &tm) != 0) {
  55. puts ("## Bad date format\n");
  56. break;
  57. }
  58. /* and write to RTC */
  59. rtc_set (&tm);
  60. }
  61. /* FALL TROUGH */
  62. case 1: /* get date & time */
  63. rtc_get (&tm);
  64. printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
  65. tm.tm_year, tm.tm_mon, tm.tm_mday,
  66. (tm.tm_wday<0 || tm.tm_wday>6) ?
  67. "unknown " : RELOC(weekdays[tm.tm_wday]),
  68. tm.tm_hour, tm.tm_min, tm.tm_sec);
  69. break;
  70. default:
  71. printf ("Usage:\n%s\n", cmdtp->usage);
  72. rcode = 1;
  73. }
  74. /* switch back to original I2C bus */
  75. I2C_SET_BUS(old_bus);
  76. return rcode;
  77. }
  78. /*
  79. * simple conversion of two-digit string with error checking
  80. */
  81. static int cnvrt2 (char *str, int *valp)
  82. {
  83. int val;
  84. if ((*str < '0') || (*str > '9'))
  85. return (-1);
  86. val = *str - '0';
  87. ++str;
  88. if ((*str < '0') || (*str > '9'))
  89. return (-1);
  90. *valp = 10 * val + (*str - '0');
  91. return (0);
  92. }
  93. /*
  94. * Convert date string: MMDDhhmm[[CC]YY][.ss]
  95. *
  96. * Some basic checking for valid values is done, but this will not catch
  97. * all possible error conditions.
  98. */
  99. int mk_date (char *datestr, struct rtc_time *tmp)
  100. {
  101. int len, val;
  102. char *ptr;
  103. ptr = strchr (datestr,'.');
  104. len = strlen (datestr);
  105. /* Set seconds */
  106. if (ptr) {
  107. int sec;
  108. *ptr++ = '\0';
  109. if ((len - (ptr - datestr)) != 2)
  110. return (-1);
  111. len = strlen (datestr);
  112. if (cnvrt2 (ptr, &sec))
  113. return (-1);
  114. tmp->tm_sec = sec;
  115. } else {
  116. tmp->tm_sec = 0;
  117. }
  118. if (len == 12) { /* MMDDhhmmCCYY */
  119. int year, century;
  120. if (cnvrt2 (datestr+ 8, &century) ||
  121. cnvrt2 (datestr+10, &year) ) {
  122. return (-1);
  123. }
  124. tmp->tm_year = 100 * century + year;
  125. } else if (len == 10) { /* MMDDhhmmYY */
  126. int year, century;
  127. century = tmp->tm_year / 100;
  128. if (cnvrt2 (datestr+ 8, &year))
  129. return (-1);
  130. tmp->tm_year = 100 * century + year;
  131. }
  132. switch (len) {
  133. case 8: /* MMDDhhmm */
  134. /* fall thru */
  135. case 10: /* MMDDhhmmYY */
  136. /* fall thru */
  137. case 12: /* MMDDhhmmCCYY */
  138. if (cnvrt2 (datestr+0, &val) ||
  139. val > 12) {
  140. break;
  141. }
  142. tmp->tm_mon = val;
  143. if (cnvrt2 (datestr+2, &val) ||
  144. val > ((tmp->tm_mon==2) ? 29 : 31)) {
  145. break;
  146. }
  147. tmp->tm_mday = val;
  148. if (cnvrt2 (datestr+4, &val) ||
  149. val > 23) {
  150. break;
  151. }
  152. tmp->tm_hour = val;
  153. if (cnvrt2 (datestr+6, &val) ||
  154. val > 59) {
  155. break;
  156. }
  157. tmp->tm_min = val;
  158. /* calculate day of week */
  159. GregorianDay (tmp);
  160. return (0);
  161. default:
  162. break;
  163. }
  164. return (-1);
  165. }
  166. /***************************************************/
  167. U_BOOT_CMD(
  168. date, 2, 1, do_date,
  169. "date - get/set/reset date & time\n",
  170. "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
  171. " - without arguments: print date & time\n"
  172. " - with numeric argument: set the system date & time\n"
  173. " - with 'reset' argument: reset the RTC\n"
  174. );
  175. #endif /* CFG_CMD_DATE */