cmd_date.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
  25. static int do_date(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  26. {
  27. struct rtc_time tm;
  28. int rcode = 0;
  29. int old_bus;
  30. /* switch to correct I2C bus */
  31. #ifdef CONFIG_SYS_I2C
  32. old_bus = i2c_get_bus_num();
  33. i2c_set_bus_num(CONFIG_SYS_RTC_BUS_NUM);
  34. #else
  35. old_bus = I2C_GET_BUS();
  36. I2C_SET_BUS(CONFIG_SYS_RTC_BUS_NUM);
  37. #endif
  38. switch (argc) {
  39. case 2: /* set date & time */
  40. if (strcmp(argv[1],"reset") == 0) {
  41. puts ("Reset RTC...\n");
  42. rtc_reset ();
  43. rcode = rtc_set(&default_tm);
  44. if (rcode)
  45. puts("## Failed to set date after RTC reset\n");
  46. } else {
  47. /* initialize tm with current time */
  48. rcode = rtc_get (&tm);
  49. if(!rcode) {
  50. /* insert new date & time */
  51. if (mk_date (argv[1], &tm) != 0) {
  52. puts ("## Bad date format\n");
  53. break;
  54. }
  55. /* and write to RTC */
  56. rcode = rtc_set (&tm);
  57. if(rcode)
  58. puts("## Set date failed\n");
  59. } else {
  60. puts("## Get date failed\n");
  61. }
  62. }
  63. /* FALL TROUGH */
  64. case 1: /* get date & time */
  65. rcode = rtc_get (&tm);
  66. if (rcode) {
  67. puts("## Get date failed\n");
  68. break;
  69. }
  70. printf ("Date: %4d-%02d-%02d (%sday) Time: %2d:%02d:%02d\n",
  71. tm.tm_year, tm.tm_mon, tm.tm_mday,
  72. (tm.tm_wday<0 || tm.tm_wday>6) ?
  73. "unknown " : RELOC(weekdays[tm.tm_wday]),
  74. tm.tm_hour, tm.tm_min, tm.tm_sec);
  75. break;
  76. default:
  77. rcode = CMD_RET_USAGE;
  78. }
  79. /* switch back to original I2C bus */
  80. #ifdef CONFIG_SYS_I2C
  81. i2c_set_bus_num(old_bus);
  82. #else
  83. I2C_SET_BUS(old_bus);
  84. #endif
  85. return rcode;
  86. }
  87. /*
  88. * simple conversion of two-digit string with error checking
  89. */
  90. static int cnvrt2 (const 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 (const 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. rtc_calc_weekday(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. "get/set/reset date & time",
  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"
  183. );