Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/cmd/date.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * (C) Copyright 2001
4
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5
 */
6
7
/*
8
 * RTC, Date & Time support: get and set date & time
9
 */
10
#include <command.h>
11
#include <dm.h>
12
#include <rtc.h>
13
#include <i2c.h>
14
#include <asm/global_data.h>
15
16
DECLARE_GLOBAL_DATA_PTR;
17
18
static const char * const weekdays[] = {
19
  "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur",
20
};
21
22
int mk_date (const char *, struct rtc_time *);
23
24
static struct rtc_time default_tm = { 0, 0, 0, 1, 1, 2000, 6, 0, 0 };
25
26
static int do_date(struct cmd_tbl *cmdtp, int flag, int argc,
27
       char *const argv[])
28
0
{
29
0
  struct rtc_time tm;
30
0
  int rcode = 0;
31
0
  int old_bus __maybe_unused;
32
33
  /* switch to correct I2C bus */
34
0
  struct udevice *dev;
35
36
0
  rcode = uclass_get_device_by_seq(UCLASS_RTC, 0, &dev);
37
0
  if (rcode) {
38
0
    rcode = uclass_get_device(UCLASS_RTC, 0, &dev);
39
0
    if (rcode) {
40
0
      printf("Cannot find RTC: err=%d\n", rcode);
41
0
      return CMD_RET_FAILURE;
42
0
    }
43
0
  }
44
45
0
  switch (argc) {
46
0
  case 2:     /* set date & time */
47
0
    if (strcmp(argv[1],"reset") == 0) {
48
0
      puts ("Reset RTC...\n");
49
0
      rcode = dm_rtc_reset(dev);
50
0
      if (!rcode)
51
0
        rcode = dm_rtc_set(dev, &default_tm);
52
0
      if (rcode)
53
0
        puts("## Failed to set date after RTC reset\n");
54
0
    } else {
55
      /* initialize tm with current time */
56
0
      rcode = dm_rtc_get(dev, &tm);
57
0
      if (!rcode) {
58
        /* insert new date & time */
59
0
        if (mk_date(argv[1], &tm) != 0) {
60
0
          puts ("## Bad date format\n");
61
0
          break;
62
0
        }
63
        /* and write to RTC */
64
0
        rcode = dm_rtc_set(dev, &tm);
65
0
        if (rcode) {
66
0
          printf("## Set date failed: err=%d\n",
67
0
                 rcode);
68
0
        }
69
0
      } else {
70
0
        puts("## Get date failed\n");
71
0
      }
72
0
    }
73
0
    fallthrough;
74
0
  case 1:     /* get date & time */
75
0
    rcode = dm_rtc_get(dev, &tm);
76
0
    if (rcode) {
77
0
      puts("## Get date failed\n");
78
0
      break;
79
0
    }
80
81
0
    printf ("Date: %4d-%02d-%02d (%sday)    Time: %2d:%02d:%02d\n",
82
0
      tm.tm_year, tm.tm_mon, tm.tm_mday,
83
0
      (tm.tm_wday<0 || tm.tm_wday>6) ?
84
0
        "unknown " : weekdays[tm.tm_wday],
85
0
      tm.tm_hour, tm.tm_min, tm.tm_sec);
86
87
0
    break;
88
0
  default:
89
0
    rcode = CMD_RET_USAGE;
90
0
  }
91
92
0
  return rcode ? CMD_RET_FAILURE : 0;
93
0
}
94
95
/*
96
 * simple conversion of two-digit string with error checking
97
 */
98
static int cnvrt2 (const char *str, int *valp)
99
0
{
100
0
  int val;
101
102
0
  if ((*str < '0') || (*str > '9'))
103
0
    return (-1);
104
105
0
  val = *str - '0';
106
107
0
  ++str;
108
109
0
  if ((*str < '0') || (*str > '9'))
110
0
    return (-1);
111
112
0
  *valp = 10 * val + (*str - '0');
113
114
0
  return (0);
115
0
}
116
117
/*
118
 * Convert date string: MMDDhhmm[[CC]YY][.ss]
119
 *
120
 * Some basic checking for valid values is done, but this will not catch
121
 * all possible error conditions.
122
 */
123
int mk_date (const char *datestr, struct rtc_time *tmp)
124
0
{
125
0
  int len, val;
126
0
  char *ptr;
127
128
0
  ptr = strchr(datestr, '.');
129
0
  len = strlen(datestr);
130
131
  /* Set seconds */
132
0
  if (ptr) {
133
0
    int sec;
134
135
0
    ptr++;
136
0
    if ((len - (ptr - datestr)) != 2)
137
0
      return (-1);
138
139
0
    len -= 3;
140
141
0
    if (cnvrt2 (ptr, &sec))
142
0
      return (-1);
143
144
0
    tmp->tm_sec = sec;
145
0
  } else {
146
0
    tmp->tm_sec = 0;
147
0
  }
148
149
0
  if (len == 12) {   /* MMDDhhmmCCYY */
150
0
    int year, century;
151
152
0
    if (cnvrt2 (datestr+ 8, &century) ||
153
0
        cnvrt2 (datestr+10, &year) ) {
154
0
      return (-1);
155
0
    }
156
0
    tmp->tm_year = 100 * century + year;
157
0
  } else if (len == 10) {   /* MMDDhhmmYY */
158
0
    int year, century;
159
160
0
    century = tmp->tm_year / 100;
161
0
    if (cnvrt2 (datestr+ 8, &year))
162
0
      return (-1);
163
0
    tmp->tm_year = 100 * century + year;
164
0
  }
165
166
0
  switch (len) {
167
0
  case 8:     /* MMDDhhmm */
168
    /* fall thru */
169
0
  case 10:    /* MMDDhhmmYY */
170
    /* fall thru */
171
0
  case 12:    /* MMDDhhmmCCYY */
172
0
    if (cnvrt2 (datestr+0, &val) ||
173
0
        val > 12) {
174
0
      break;
175
0
    }
176
0
    tmp->tm_mon  = val;
177
0
    if (cnvrt2 (datestr+2, &val) ||
178
0
        val > ((tmp->tm_mon==2) ? 29 : 31)) {
179
0
      break;
180
0
    }
181
0
    tmp->tm_mday = val;
182
183
0
    if (cnvrt2 (datestr+4, &val) ||
184
0
        val > 23) {
185
0
      break;
186
0
    }
187
0
    tmp->tm_hour = val;
188
189
0
    if (cnvrt2 (datestr+6, &val) ||
190
0
        val > 59) {
191
0
      break;
192
0
    }
193
0
    tmp->tm_min  = val;
194
195
    /* calculate day of week */
196
0
    rtc_calc_weekday(tmp);
197
198
0
    return (0);
199
0
  default:
200
0
    break;
201
0
  }
202
203
0
  return (-1);
204
0
}
205
206
/***************************************************/
207
208
U_BOOT_CMD(
209
  date, 2,  1,  do_date,
210
  "get/set/reset date & time",
211
  "[MMDDhhmm[[CC]YY][.ss]]\ndate reset\n"
212
  "  - without arguments: print date & time\n"
213
  "  - with numeric argument: set the system date & time\n"
214
  "  - with 'reset' argument: reset the RTC"
215
);