cmd_date.c 4.6 KB

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