summaryrefslogtreecommitdiff
path: root/util/iseltime.c
blob: 1b16008ed990fb23eb7465816427c7483334172e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * iseltime.c
 *
 * Author:  Andy Cress  arcress at users.sourceforge.net
 *
 * 05/11/09 Andy Cress v1.0 - created
 * 07/23/10 Andy Cress v1.1 - always show System time also
 * 08/20/10 Andy Cress v1.2 - show/set RTC time also if Linux
 * 03/16/17 Andy Cress 3.03 - show/set UTC offset also
 */
/*M*
Copyright (c) 2013, Andy Cress
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

  a.. Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer. 
  b.. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *M*/
#ifdef WIN32
#include <windows.h>
#include <stdio.h>
#include "getopt.h"
#elif defined(EFI)
 // EFI: defined (EFI32) || defined (EFI64) || defined(EFIX64)
 #ifndef NULL
    #define NULL    0
 #endif
 #include <types.h>
 #include <libdbg.h>
 #include <unistd.h>
 #include <errno.h>
#elif defined(DOS)
 #include <dos.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "getopt.h"
#else
/* Linux, Solaris, BSD */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(HPUX)
/* getopt is defined in stdio.h */
#elif defined(MACOS)
/* getopt is defined in unistd.h */
#include <unistd.h>
#else
#include <getopt.h>
#endif
#endif
#include <time.h>
#include <string.h>
#include "ipmicmd.h"
 
#define GET_SELTIME            0x48
#define SET_SELTIME            0x49
#define GET_SELUTC             0x5C
#define SET_SELUTC             0x5D
/*
 * Global variables 
 */
static char * progver   = "3.05";
static char * progname  = "iseltime";
static char   fdebug    = 0;
static char   fset      = 0;
static uchar ipmi_maj = 0;
static uchar ipmi_min = 0;

static int get_sel_utc(uchar *rdata, int rlen)
{
	uchar idata[4];
	uchar ccode;
	int ret;
	ret = ipmi_cmdraw(GET_SELUTC, NETFN_STOR, BMC_SA, PUBLIC_BUS, BMC_LUN,
			  idata, 0, rdata, &rlen, &ccode, fdebug);
	if (ret == 0 && ccode != 0) ret = ccode;
	return(ret);
}  /*end get_sel_utc()*/

static int set_sel_utc(short offset)
{
	uchar idata[4];
	uchar rdata[16];
	int rlen = 8;
	int ret;
	uchar ccode;

	idata[0] = (uchar)(offset & 0x00ff);
	idata[1] = (uchar)((offset >> 8) & 0x00ff);
	ret = ipmi_cmdraw(SET_SELUTC, NETFN_STOR, BMC_SA, PUBLIC_BUS, BMC_LUN,
			  idata, 2, rdata, &rlen, &ccode, fdebug);
	if (ret == 0 && ccode != 0) ret = ccode;
	return(ret);
}  /*end set_sel_utc()*/

static int get_sel_time(uchar *rdata, int rlen)
{
	uchar idata[4];
	uchar ccode;
	int ret;
	ret = ipmi_cmdraw(GET_SELTIME, NETFN_STOR, BMC_SA, PUBLIC_BUS, BMC_LUN,
			  idata, 0, rdata, &rlen, &ccode, fdebug);
	if (ret == 0 && ccode != 0) ret = ccode;
	return(ret);
}  /*end get_sel_time()*/

static int set_sel_time(time_t newtime)
{
	uchar idata[4];
	uchar rdata[16];
	int rlen = 8;
	int ret;
	uchar ccode;

	idata[0] = (uchar)(newtime & 0x00ff);
	idata[1] = (uchar)((newtime >> 8) & 0x00ff);
	idata[2] = (uchar)((newtime >> 16) & 0x00ff);
	idata[3] = (uchar)((newtime >> 24) & 0x00ff);
	ret = ipmi_cmdraw(SET_SELTIME, NETFN_STOR, BMC_SA, PUBLIC_BUS, BMC_LUN,
			  idata, 4, rdata, &rlen, &ccode, fdebug);
	if (ret == 0 && ccode != 0) ret = ccode;
	return(ret);
}  /*end set_sel_time()*/

time_t utc2local(time_t t)
{
   struct tm * tm_tmp;
   int gt_year,gt_yday,gt_hour,lt_year,lt_yday,lt_hour;
   int delta_hour;
   time_t lt;

   //modify UTC time to local time expressed in number of seconds from 1/1/70 0:0:0 1970 GMT
   // check for dst?
   tm_tmp=gmtime(&t);
   gt_year=tm_tmp->tm_year;
   gt_yday=tm_tmp->tm_yday;
   gt_hour=tm_tmp->tm_hour;
   tm_tmp=localtime(&t);
   lt_year=tm_tmp->tm_year;
   lt_yday=tm_tmp->tm_yday;
   lt_hour=tm_tmp->tm_hour;
   delta_hour=lt_hour - gt_hour;
   if ( (lt_year > gt_year) || ((lt_year == gt_year) && (lt_yday > gt_yday)) )
           delta_hour += 24;
   if ( (lt_year < gt_year) || ((lt_year == gt_year) && (lt_yday < gt_yday)) )
           delta_hour -= 24;
   if (fdebug) printf("utc2local: delta_hour = %d\n",delta_hour);
   lt = t + (delta_hour * 60 * 60);
   return(lt);
}

#define TIMESTR_SZ  30
void show_time(time_t etime, int doutc)
{
	char buf[TIMESTR_SZ];
	int bufsz = TIMESTR_SZ;
	time_t t;

	strcpy(buf,"00/00/00 00:00:00");
	if (doutc) t = utc2local(etime);
	else t = etime;
	strftime(buf,bufsz, "%x %H:%M:%S", gmtime(&t)); /*or "%x %T"*/
	printf("%s\n",buf);
	return;
}

#ifdef METACOMMAND
int i_iseltime(int argc, char **argv)
#else
#ifdef WIN32
int __cdecl
#else
int
#endif
main(int argc, char **argv)
#endif
{
   int rv = 0;
   uchar devrec[20];
   uchar timebuf[4];
   time_t ltime1, ltime2, ltime3;
   short offset;
   int c;

#if defined (EFI)
        InitializeLib(_LIBC_EFIImageHandle, _LIBC_EFISystemTable);
#else
        printf("%s ver %s\n", progname,progver);
#endif

   while ( (c = getopt( argc, argv,"sT:V:J:EYF:P:N:R:U:x?")) != EOF ) 
      switch(c) {
          case 's': fset  = 1;   break;  /* read only */
          case 'x': fdebug = 1;     break;  /* debug messages */
          case 'N':    /* nodename */
          case 'U':    /* remote username */
          case 'P':    /* remote password */
          case 'R':    /* remote password */
          case 'E':    /* get password from IPMI_PASSWORD environment var */
          case 'F':    /* force driver type */
          case 'T':    /* auth type */
          case 'J':    /* cipher suite */ 
          case 'V':    /* priv level */
          case 'Y':    /* prompt for remote password */
                parse_lan_options(c,optarg,fdebug);
                break;
	  default:
                printf("Usage: %s [-sx -NUPRETVF]\n", progname);
                printf(" where -s      Set SEL time (usually once a day)\n");
                printf("       -x      show eXtra debug messages\n");
		print_lan_opt_usage(0);
                exit(1);
      }

   rv = ipmi_getdeviceid(devrec,16,fdebug);
   if (rv != 0) {
        show_outcome(progname,rv); 
	ipmi_close_();
	exit(rv);
   } else {
      ipmi_maj = devrec[4] & 0x0f;
      ipmi_min = devrec[4] >> 4;
#ifndef EFI
      printf("-- BMC version %x.%x, IPMI version %d.%d \n",
             devrec[2],  devrec[3], ipmi_maj, ipmi_min);
#endif
   }

   offset = 0;
   rv = get_sel_utc((uchar *)&offset,2);
   if (fdebug) printf("get_sel_utc: ret=%x offset=%d\n",rv,offset);
   if (rv == 0) {
      /* TODO: get system UTC offset */
      rv = set_sel_utc(offset);
      printf("set_sel_utc(%d): ret=%x\n",offset,rv);
   }
   rv = get_sel_time(&timebuf[0],4);
   if (rv != 0) {
	  printf("get_sel_time error: ret = %x\n",rv);
	  ipmi_close_();
	  exit(1);
   }
   time(&ltime2);
   printf("Current System time: "); show_time(ltime2,1);
   ltime1 = timebuf[0] + (timebuf[1] << 8) + (timebuf[2] << 16) + 
		(timebuf[3] << 24);
   printf("Current SEL time:    "); show_time(ltime1,0);
   
   // if (fdebug) ltime3 = utc2local(ltime1); 

   if (fset == 1) {
      /* get current time */
      time(&ltime2);
      rv = set_sel_time(ltime2);
      printf("Setting SEL time to System Time: ret = %x\n",rv); 
      if (rv != 0) printf("set_sel_time error: ret = %x\n",rv);
      else {  /*successful*/
	rv = get_sel_time(timebuf,8);
	if (rv != 0) printf("get_sel_time error: ret = %x\n",rv);
	else {
	   ltime3 = timebuf[0] + (timebuf[1] << 8) + (timebuf[2] << 16) + 
			(timebuf[3] << 24);
	   printf("New SEL time:        "); show_time(ltime3,0);
	}
      }
   } 
#ifdef LINUX
   if (is_remote() == 0) {
      c = system("echo \"Current RTC time:    `hwclock`\"");
      if (fset == 1) {
         c = system("hwclock --systohc");
	 printf("Copying System Time to RTC: ret = %d\n",c); 
      }
   }
#endif
   ipmi_close_();
   show_outcome(progname,rv); 
   exit (rv);
}  /* end main()*/

/* end iseltime.c */