cmd_date.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. static const char * const weekdays[] = {
  32. "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
  33. };
  34. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  35. #define RELOC(a) ((typeof(a))((unsigned long)(a) + gd->reloc_off))
  36. #else
  37. #define RELOC(a) a
  38. #endif
  39. int mk_date (const char *, struct rtc_time *);
  40. static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const 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. rcode = CMD_RET_USAGE;
  85. }
  86. /* switch back to original I2C bus */
  87. I2C_SET_BUS(old_bus);
  88. return rcode;
  89. }
  90. /*
  91. * simple conversion of two-digit string with error checking
  92. */
  93. static int cnvrt2 (const char *str, int *valp)
  94. {
  95. int val;
  96. if ((*str < '0') || (*str > '9'))
  97. return (-1);
  98. val = *str - '0';
  99. ++str;
  100. if ((*str < '0') || (*str > '9'))
  101. return (-1);
  102. *valp = 10 * val + (*str - '0');
  103. return (0);
  104. }
  105. /*
  106. * Convert date string: MMDDhhmm[[CC]YY][.ss]
  107. *
  108. * Some basic checking for valid values is done, but this will not catch
  109. * all possible error conditions.
  110. */
  111. int mk_date (const char *datestr, struct rtc_time *tmp)
  112. {
  113. int len, val;
  114. char *ptr;
  115. ptr = strchr (datestr,'.');
  116. len = strlen (datestr);
  117. /* Set seconds */
  118. if (ptr) {
  119. int sec;
  120. *ptr++ = '\0';
  121. if ((len - (ptr - datestr)) != 2)
  122. return (-1);
  123. len = strlen (datestr);
  124. if (cnvrt2 (ptr, &sec))
  125. return (-1);
  126. tmp->tm_sec = sec;
  127. } else {
  128. tmp->tm_sec = 0;
  129. }
  130. if (len == 12) { /* MMDDhhmmCCYY */
  131. int year, century;
  132. if (cnvrt2 (datestr+ 8, &century) ||
  133. cnvrt2 (datestr+10, &year) ) {
  134. return (-1);
  135. }
  136. tmp->tm_year = 100 * century + year;
  137. } else if (len == 10) { /* MMDDhhmmYY */
  138. int year, century;
  139. century = tmp->tm_year / 100;
  140. if (cnvrt2 (datestr+ 8, &year))
  141. return (-1);
  142. tmp->tm_year = 100 * century + year;
  143. }
  144. switch (len) {
  145. case 8: /* MMDDhhmm */
  146. /* fall thru */
  147. case 10: /* MMDDhhmmYY */
  148. /* fall thru */
  149. case 12: /* MMDDhhmmCCYY */
  150. if (cnvrt2 (datestr+0, &val) ||
  151. val > 12) {
  152. break;
  153. }
  154. tmp->tm_mon = val;
  155. if (cnvrt2 (datestr+2, &val) ||
  156. val > ((tmp->tm_mon==2) ? 29 : 31)) {
  157. break;
  158. }
  159. tmp->tm_mday = val;
  160. if (cnvrt2 (datestr+4, &val) ||
  161. val > 23) {
  162. break;
  163. }
  164. tmp->tm_hour = val;
  165. if (cnvrt2 (datestr+6, &val) ||
  166. val > 59) {
  167. break;
  168. }
  169. tmp->tm_min = val;
  170. /* calculate day of week */
  171. GregorianDay (tmp);
  172. return (0);
  173. default:
  174. break;
  175. }
  176. return (-1);
  177. }
  178. /***************************************************/
  179. U_BOOT_CMD(
  180. date, 2, 1, do_date,
  181. "get/set/reset date & time",
  182. "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
  183. " - without arguments: print date & time\n"
  184. " - with numeric argument: set the system date & time\n"
  185. " - with 'reset' argument: reset the RTC"
  186. );