cmd_date.c 4.1 KB

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