cmd_date.c 4.6 KB

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