summaryrefslogtreecommitdiff
path: root/util/isol.c
blob: c73099a123535e3ca8eaf41da28641b665099302 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
/*
 * isol.c (was isolconsole.c)
 *      IPMI Serial-Over-LAN console application
 *
 * Author:  Andy Cress  arcress at users.sourceforge.net
 * Copyright (c) 2007 Intel Corporation
 * Copyright (c) 2009 Kontron America, Inc.
 *
 * 10/12/06 Andy Cress - created, just a framework for now
 * 02/08/07 Andy Cress - now working for IPMI LAN 2.0 SOL in Linux,
 *                       still stdio problems in Windows
 * 02/23/07 Andy Cress - fixed Windows stdin, still Windows output issues,
 *                       need more in xlate_vt100_win().
 * 04/06/07 Andy Cress - more vt100-to-windows translation, 
 *                       added -r for raw termio
 * 04/19/07 Andy Cress - more vt100-to-windows translation, 
 * 05/08/07 Andy Cress - more logic for 1.5 SOL.  Session opens but 
 *                       1.5 SOL data packets are not yet working.
 * 05/24/07 Andy Cress - Fix Enter key confusion in BIOS Setup (use CR+LF)
 * 06/15/07 Andy Cress - More fixes to Windows translation (fUseWinCon)
 * 08/20/07 Andy Cress - moved Windows translation to isolwin.c
 * See ChangeLog for further changes
 *
 * NOTE: There are a few other options for Serial-Over-LAN console 
 * applications:
 *   ipmitool.sf.net has v2 sol console capability for Linux (BSD)
 *   Intel dpccli, a CLI console for Linux and Windows (proprietary)
 *   Intel ISM console for Windows (proprietary)
 *   Intel System Management Software (LANDesk) console, proprietary 
 * The Intel applications support both IPMI 1.5 and 2.0 SOL 
 * protocols.
 */
/*M*
Copyright (c) 2007 Intel Corporation
Copyright (c) 2009 Kontron America, Inc.
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. 
  c.. Neither the name of Kontron nor the names of its contributors 
      may be used to endorse or promote products derived from this software 
      without specific prior written permission. 

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*/
#if defined(DOS)
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
int i_sol(int argc, char **argv)
{
   printf("IPMI SOL console is not supported under DOS.\n");
   return(1);
}
#else

/* All other OSs:  Linux, Windows, Solaris, BSD */
#ifdef WIN32
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <io.h>
#include "getopt.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h> 
#include <termios.h>
#include <unistd.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 <string.h>
#include <time.h>
#include "ipmicmd.h"
#include "ipmilanplus.h"
 
#define ACTIVATE_PAYLOAD    0x48
#define DEACTIVATE_PAYLOAD  0x49
#define PAYLOAD_TYPE_SOL    0x01
#define SOL_SERIAL_ALERT_MASK_DEFERRED 0x04
#define SOL_BMC_ASSERTS_CTS_MASK_TRUE  0x00  
#define SOL_BMC_ASSERTS_CTS_MASK_FALSE 0x02
#define IPMI_PAYLOAD_TYPE_SOL  0x01
#define AUTHTYPE_RMCP_PLUS  0x06  
#define RV_END   -2
#define CH_CR   '\r'    /*0x0D =='\r'*/
#define CH_LF   '\n'    /*0x0A =='\n' LineFeed(Down) */
#define CH_ESC  '\033'  /*0x1B == ASCII Escape */
#define CH_CTL  '\316'  /*0xCE == VT100 Control */
#define CH_DEL  '\177'  /*0x7F == Delete key */
#define CH_BS   '\010'  /*0x08 == Backspace */
#define CH_TAB  '\011'  /*0x09 == Tab */
#define CH_UP   '\013'  /*0x0B == Up Arrow */

/* Input data buffer size: screen size (80*24) = 1920 */
/* IPMI_BUF_SIZE = 1024, less RMCP+IPMI hdr */
/* IDATA_SZ=2048 was too big, used =300 thru v2.1.0 */
#define IDATA_SZ  512
#define IPKT_SZ   512
#define MAX_INTEL_DATA  203  /*sol_send >= 204 bytes gives error w Intel BMC*/
#define MAX_DELL_DATA    46  /*sol_send >= 46 bytes gives error w Dell BMC*/
#define MAX_KONTRON_DATA 74  /*sol_send >= 75 bytes gives error w Kontron BMC*/
#define MAX_OTHER_DATA   45  /*use a small sol_send limit of 45 by default */

typedef struct {
  int type;
  int len;
  char *data;
  } SOL_RSP_PKT;

extern void tty_setraw(int mode);      /*from ipmicmd.c*/
extern void tty_setnormal(int mode);   /*from ipmicmd.c*/
/* extern int  tty_getattr(int *lflag,int *oflag,int *iflag);  *from ipmicmd.c*/
extern SockType lan_get_fd(void);          /*from ipmilan.c*/
extern int  lan_send_sol( uchar *payload, int len, SOL_RSP_PKT *rsp);
extern int  lan_recv_sol( SOL_RSP_PKT *rsp );
extern int  lan_keepalive(int type);
extern void lan_get_sol_data(uchar fEnc, uchar iseed, uint32 *seed);
extern void lan_set_sol_data(uchar fEnc, uchar auth, uchar iseed, 
				int insize, int outsize);
/* lan2 routines, from ipmilanplus.c, even if not HAVE_LANPLUS: */
extern int  lan2_get_fd(void);          /*from ipmilanplus.c*/
extern int  lan2_send_sol( uchar *payload, int len, SOL_RSP_PKT *rsp);
extern int  lan2_recv_sol( SOL_RSP_PKT *rsp );
extern void lan2_set_sol_data(int in, int out, int port, void*hnd, char esc);
extern int  lan2_keepalive(int type, SOL_RSP_PKT *rsp);
extern void lan2_recv_handler( void *rs );
extern long lan2_get_latency( void );  /*from ipmilanplus.c*/
extern void lanplus_set_recvdelay( int delay );  /*lib/lanplus/lanplus.c*/
extern int  lan2_send_break( SOL_RSP_PKT *rsp );

extern char  fdbglog;    /*see ipmilanplus.c*/
extern char log_name[];  /*see ipmicmd.c*/
extern int  is_lan2intel(int vend, int prod); /*see oem_intel.c*/
void dbglog( char *pattn, ... ); /*local prototype*/

/*
 * Global variables 
 */
static char * progver   = "2.97";
static char * progname  = "isol";
static char  fdebug        = 0;
static char  fpicmg        = 0;
static char  factivate     = 0;
static char  fdeactivate   = 0;
static char  fScript       = 0;
static char  fTrace        = 0;
static char  fprivset      = 0;
static char  bDriver       = 0;
static uchar bSolVer       = 0;
static uchar bKeepAlive    = 1;   /*1=GetDeviceID, 2=empty SOL data*/
static char  sol_esc_len   = 2;   /* SOL escape seq ("~.") */
static char  sol_esc_ch    = '~'; 
static char  sol_esc_pending = 0;
static char  sol_esc_fn[4] = {'.','b','d', '?'}; /* SOL escape functions */
static char  file_scr[80] = {""};
static char  file_trc[80] = {""};
static char  dbglog_name[40] = "isoldbg.log";
static FILE *fp_scr = NULL;
static FILE *fp_trc = NULL;
static uchar ipmi_maj = 0;
static uchar ipmi_min = 0;
static int   vend_id  = 0;  /* Manufacturer IANA vendor id */
static int   prod_id  = 0;
static uchar fSolEncryption     = 1;
static uchar fSolAuthentication = 1;
static int   sol_timeout = 30;  /* default: send keepalive every 30 seconds */
static time_t keepalive_start; 
static int   max_bmc_data = MAX_OTHER_DATA;
static int   fgotrecv = 0;
static int   fsentok = 0;
static uchar payload_instance = 1;
char  fUseWinCon    = 1;
uchar fCRLF     = 0;  /* 0=use CR, 1=use legacy CR+LF, 2=use LF (like raw) */
uchar fRaw      = 0;
uchar bTextMode = 0;
int   sol_done = 0;
int   wait_time = 500;           /*number of msec to wait*/
int   retry_time = 5;            /*number of msec between retries*/

static int    sol_ver = 2;         /*use IPMI 2.0 SOL by default*/
static int    sol_retries = 1;     /*send retries for sol_input_handler*/
static int    sol_recvdelay = 0;   /*if != 0, num us to delay before recv*/
static uchar  sol15_iseed = 0x01;  /*initial seed count, usu 0 or 1 */
static uint32 sol15_wseed = 0;     /*32-bit seed value*/
static long lan2_latency = 0;      /*lan2 latency in msec */
static long lan2_slow = 100;       /*slow if latency > 100 msec */

extern FILE *fplog;  /*see ipmicmd.c*/
#ifdef WIN32
/* these routines are in isolwin.c */
extern DWORD WINAPI input_thread( LPVOID lpParam);
extern void console_open(char fdebugcmd);
extern void console_close(void);
extern void console_out(uchar *pdata, int len);
extern int  console_in(DWORD keydata, uchar *pdata, int len);
extern int  os_select(int infd, SockType sfd, fd_set *read_fds, fd_set *error_fds);
extern int  os_read(int fd, uchar *buf, int sz);
extern void Ascii2KeyCode(uchar c, uchar *buf);
#else

/*======== start Linux ===============================*/
int os_read(int fd, uchar *buf, int sz)
{
    int len = 0;
    len = read(fd, buf, sz);
    return(len);
}

int os_select(int infd, SockType sfd, fd_set *read_fds, fd_set *error_fds)
{
   int rv = 0;
   struct timeval tv;

   /* Linux handles both stdin & socket via select() */
   FD_ZERO(read_fds);
   FD_SET(infd, read_fds);
   FD_SET(sfd, read_fds);
   FD_ZERO(error_fds);
   FD_SET(sfd, error_fds);
   tv.tv_sec =  0;
   tv.tv_usec = wait_time * 1000;  /* 500000 usec, 500 msec, 0.5 sec*/
   rv = select((int)(sfd + 1), read_fds, NULL, error_fds, &tv);
   return rv;
}

void CheckTextMode(uchar *buffer, int sbuf)
{
    int c, i, j, imatch;
    uchar pattern[5] = {CH_ESC,'[','0','0','m'}; /*was [0m */
    int spattern = 5;
 
    /* if CH_ESC,"[0m", TextMode change */
    j = 0;
    imatch = 0;
    for (j = 0; j < sbuf; j++) {
        if ((sbuf - j) < spattern && imatch == 0) break;
        c = buffer[j];
        if (c == pattern[imatch]) {
            imatch++;
        } else if (pattern[imatch] == '?') {  /*wildcard char*/
            imatch++;
        } else if (pattern[imatch] == '0' && (c>='0' && c <='9')) { /*numeric*/
            imatch++;
        } else {
            if (imatch > 0) {
               if (j > 0) j--; /* try again with the first match char */
               imatch = 0;
            } 
        }
        if (imatch == spattern) { /*match found*/
	    char mystr[5];
            i = (j+1) - imatch;  /*buffer[i] is the match */
            // bTextMode = buffer[i+2] & 0x0f;  /* usu 0 or 1 */
	    mystr[0] = buffer[i+2];
	    mystr[1] = buffer[i+3];
	    mystr[2] = 0;
            bTextMode = atob(mystr);
            dbglog("TextMode changed to %d\n",bTextMode);
        }
        else if (imatch > spattern) break;
    }
    return;
}

/*======== end Linux =================================*/
#endif

void printm( char *pattn, ... )
{ /* all sol error/info messages go to log or stderr by default */
    va_list arglist;
    FILE *fp;
    if (fdbglog && fplog != NULL) fp = fplog;
    else fp = stderr;
    va_start(arglist, pattn);
    vfprintf(fp, pattn, arglist);
    va_end(arglist);
    fprintf(fp,"\r\n");
}

void dbglog( char *pattn, ... )
{
    va_list arglist;
    int fadd = 0;
    int l;
#ifdef WIN32
	FILE *fp;
	// char _pattn[80];
	if (fdebug == 0 && fdbglog == 0) return;
	if (fdbglog && fplog != NULL) fp = fplog;
	else fp = stderr;
	l = (int)strlen(pattn);
	if ((l >= 1) && (pattn[l-1] == '\n')) { /*check EOL chars*/
	   if (pattn[l-2] == '\r') fadd = 0;
	   else fadd = 0;  /* '\n' only */
	   // else _pattn[l-1] = 0;  /*truncate, add \r\n below, dangerous*/
	} else fadd = 1;  /*no line-end, so add it below*/
        va_start(arglist, pattn);
        vfprintf(fp, pattn, arglist);
        va_end(arglist);
	if (fadd) fprintf(fp,"\r\n");
#else
    char logtmp[LOG_MSG_LENGTH];
    if (fdebug == 0 && fdbglog == 0) return;
    l = strlen(pattn);
    if ((l >= 1) && (pattn[l-1] == '\n')) fadd = 0;
    else fadd = 1;
    va_start( arglist, pattn );
    vsnprintf(logtmp, LOG_MSG_LENGTH, pattn, arglist);
    va_end( arglist );
    if (fadd) strcat(logtmp,"\n");
    if (fdbglog) print_log(logtmp);
    else if (fdebug) fprintf( stderr, "%s", logtmp);
#endif
}
             
void dbg_dump(char *tag, uchar *pdata, int len, int fascii)
{
    FILE *fp = NULL;
    if (fdebug == 0 && fdbglog == 0) return;
    if (fdbglog && (fplog != NULL)) fp = fplog;
    dump_log(fp,tag,pdata,len,(char)fascii); /*uses fplog or fpdbg */
}

#ifdef OLD
static void set_latency( struct timeval *t1, struct timeval *t2, long *latency)
{
   long nsec;
   nsec = t2->tv_sec - t1->tv_sec;
   if ((ulong)(nsec) > 1) nsec = 1;
   *latency = nsec*1000 + (t2->tv_usec - t1->tv_usec)/1000;
}
#endif

static int sol_send(uchar *buf, int len, SOL_RSP_PKT *rsp)
{
    int rv = 0;
    if (bSolVer == 1) {
       rv = lan_send_sol(buf, len, rsp);
    } else {
       rv = lan2_send_sol(buf, len, rsp);
       if (rv == 0x01) rv = 0;  
       /* else bit-mapped error, 0x02 = need retry*/
    }
    return(rv);
}

static int sol_recv( SOL_RSP_PKT *rsp)
{
    int rv = 0;
    if (bSolVer == 1) {
       rv = lan_recv_sol( rsp );
    } else 
       rv = lan2_recv_sol( rsp );
    return(rv);
}

static SockType  sol_get_fd(void)
{
    SockType fd = 0;
    if (bSolVer == 1) {
       fd = lan_get_fd();
    } else 
       fd = lan2_get_fd();
    return(fd);
}

/*
 * sol_output_handler 
 * This routine is called both in isol.c and ipmilanplus.c/lan2_recv_handler
 * whenever SOL data is received from the BMC for output to the console. 
 */
void sol_output_handler(SOL_RSP_PKT *rsp)
{
    if (rsp == NULL) return;
    if (rsp->type == PAYLOAD_TYPE_SOL)
    {
#ifdef WIN32
	 fgotrecv = 1; 
         if (fdbglog) dbg_dump("sol_output(console_out)",rsp->data,rsp->len,1);
	 console_out(rsp->data,rsp->len);
#else
         uchar *pdata;
         int len;
         int i;
         uchar c;
         // int mode;

	 fgotrecv = 1; 
         pdata = rsp->data;
         len   = rsp->len;
         if (fdbglog) {
             // dbglog("sol_output: len = %d\n",len);
             dbg_dump("sol_output",pdata,len,1); /*like printlog*/
         }
         CheckTextMode(pdata,len);
         for (i = 0; i < len; i++) {
	      c = pdata[i];
	      /* do special character handling here?  
	       * CH_CR, 0xb0-0xb3, 0xdb */
              putc(c, stdout);
         }
         fflush(stdout);
#endif
	 if (fTrace) { fwrite(rsp->data,1,rsp->len,fp_trc); fflush(fp_trc); }
    } else { 
         dbglog("sol_output: rsp.type=%x, rsp.len=%d\n", rsp->type, rsp->len);
    }
}

static int sol_keepalive(int type)
{
   int rv = 0;
   time_t end; 

   time(&end); 
   if (sol_timeout == 0) return(rv);
   if ((end - keepalive_start) > sol_timeout) {
       dbglog("sol_keepalive: timeout, lan%d\n",bSolVer);  /*++++*/
       if (bSolVer == 1) {
           rv = lan_keepalive(type);   
       } else {
           SOL_RSP_PKT  rs;
           rv = lan2_keepalive(type, &rs);
           if (rv >= 0 && rs.len != 0) { /*handle any rsp data*/
               if (rs.type == IPMI_PAYLOAD_TYPE_SOL) {
	       dbglog("keepalive: output_handler(%d)\n",rs.len);
               sol_output_handler(&rs);
	       }
	   }
       }
       time(&keepalive_start);  /*time in seconds*/
       dbglog("sol_keepalive sent, rv = %d\n",rv);
       flush_log();
   }
   return(rv);
}

static void sol_keepalive_reset(void)
{
    /* not idle, so reset the keepalive timer */
    time(&keepalive_start);  /*time in seconds*/
}

static int send_break( void )
{
    SOL_RSP_PKT  rs;
    int rv;

    if (sol_ver == 1) return LAN_ERR_NOTSUPPORT;
    rv = lan2_send_break(&rs);
    dbglog("lan2_send_break rv=%d rs.len=%d\n", rv,rs.len);
    return(rv);
}

static int send_ctlaltdel( void )
{
    uchar buf[8];
    int rv, rlen;

// #define TESTALT  1
#ifdef TESTALT
    SOL_RSP_PKT  rs;
    /* This doesn't work.  Fix it later. */
    /* Problem is that keycodes must be translated for serial console */
    /* CtrlL = 0x0706, AltL = 0x0703, Del = 0x007f, MetaDel = 0x087f 
     * Del = keycode 0x53 
     * 1b 53 33 7e       = alt+del
     * 1b 4f 48          = ctl-alt-home
     * 1b 4f 46          = ctl-alt-end
     * 1b 53 33 3b 38 7e = ctl+alt+shift+del
     * 1b 53 33 3b 33 7e = ctl+alt+ins
     * 1b 53 33 3b 35 7e = ctl+alt+ins
     * 1b 53 35 3b 37 7e = ctl+alt+pgup
     * 1b 53 36 3b 37 7e = ctl+alt+pgdn
     * Need to find a sequence at serial console that does a reboot
     * when the command prompt is not up.
     */
    buf[0] = 0x1b;
    buf[1] = 0x53;
    buf[2] = 0x33;
    buf[3] = 0x3b;
    buf[4] = 0x36;
    buf[5] = 0x7e;
    rv = sol_send(buf, 6, &rs);
    rlen = rs.len;
#else
   uchar rbuf[16];
   uchar cc;

   rlen = sizeof(rbuf);
   buf[0] = 5;  /*soft reset via ACPI */
   rv = ipmi_cmdraw(CHASSIS_CTL,NETFN_CHAS,BMC_SA, PUBLIC_BUS, BMC_LUN,
                   buf,1,rbuf,&rlen,&cc,fdebug);
   if (rv == 0 && cc != 0) rv = cc;
#endif
    dbglog("send_ctlaltdel rv=%d rlen=%d\n", rv,rlen);
    return(rv);
}

static void show_esc_help( void )
{
	printf("%c?\r\n",sol_esc_ch);
        printf("Supported SOL escape sequences:\r\n");
        printf("\t%c.  - terminate connection\r\n",sol_esc_ch);
        printf("\t%cB  - send break\r\n",sol_esc_ch);
        printf("\t%cD  - send ctl-alt-delete (chassis soft reset)\r\n",sol_esc_ch);
        printf("\t%c?  - show this help message\r\n",sol_esc_ch);
        printf("\t%c%c  - send the escape character by typing it twice\r\n",
		sol_esc_ch, sol_esc_ch);
}

int sol_input_handler( uchar *input, int ilen)
{
   int rv = 0;
   uchar payload[IPKT_SZ]; 
   SOL_RSP_PKT  rs;
   int i, length, t;
   static uchar lastc = 0;
   char rvend = 0;
   char fdoinput = 1;
   uchar c;
#ifdef WIN32
   uchar c1, c2, c3;
   unsigned long dwKeyCode;
#endif

   memset(&payload, 0, sizeof(payload)); 
   length = 0;
   rs.len = 0;

   if ((fdebug > 2) && (ilen > 4)) 
      dbg_dump("sol_input dump:",input,ilen,1);
   // i = 0;
   for (i = 0; i < ilen; i++)
   {
    if (fdebug > 2) 
      dbglog("sol_input keys[%d]: %02x %02x %02x %02x, ilen=%d\n",  
	  i, input[i], input[i+1], input[i+2], input[i+3], ilen);
   /* look for console escape sequences (~) */

    if (sol_esc_pending) {
	sol_esc_pending = 0;
	/* next char in 2-char esc seq */
#ifdef WIN32
	if ( ((input[i+3] & 0x01) == 0) ||  /* h.o byte, if not keydown */
	     (i < 0)) {  /* skip h.o. bytes in dwChar for windows */
	   fdoinput = 0;  /*skip this input*/
	   sol_esc_pending = 1;  /*if here, esc was pending*/
   	   if (fdebug > 2)
      	      dbglog("sol_input: skip, %02x!=X1 for i=%d\n",input[i+3],i);
	} else
#endif
	switch(input[i]) {
	   case '.':
		dbglog("sol_input RV_END (%02x %02x)\n",sol_esc_ch,input[i]);
		rvend = 1;
		fdoinput = 0;
		break;
	   case 'b':
	   case 'B':
		rv = send_break();
		fdoinput = 0;
		break;
	   case 'd':
	   case 'D':
		rv = send_ctlaltdel();
		fdoinput = 0;
		break;
	   case '?':
		show_esc_help();
		fdoinput = 0;
		break;
	   default:
		/* includes entering "~~" to output '~' */
                dbglog("sol_input sol_esc no match (%02x %02x/%c%c)\n",
			sol_esc_ch,input[i], sol_esc_ch,input[i]);
		fdoinput = 1;
		break;
	}
    } else if (input[i] == sol_esc_ch) {
	/* start of new sol_esc seq */
        if (fdebug > 2)
           dbglog("sol_input esc_pending (%02x %02x)\n",input[i],input[i+1]);
	if ((sol_esc_len == 1) ||
	    ((ilen > (i+1)) && (input[i+1] == '.')) ) {   /*then exit now*/
           dbglog("sol_input RV_END (%02x %02x)\n",input[i],input[i+1]);
	   rvend = 1;
	}
	fdoinput = 0;
	sol_esc_pending = 1;
    }
    else fdoinput = 1;  /*send as normal input*/

    if (fdoinput) 
    {
        c = input[i];
#ifdef WIN32
        c1 = input[i+1];
        c2 = input[i+2];
        c3 = input[i+3];

        dwKeyCode = (c << 24) | (c1 << 16) | (c2 << 8) | c3;
        dbglog("sol_input[%d]: KeyCode=0x%08X, '%c'\n", 
               i, dwKeyCode, (0x20 <= c && c <= 0x7E ? c : '.') );

	/* check Windows input to see if it should be sent */
        t = console_in(dwKeyCode, &payload[length], sizeof(payload) - length);
        if (t > 0) length += t;
        lastc = c;
#else
        /*
         * Linux end-of-line handling with fCRLF: 
	 *      (0) CR, (1) CR+LF, (2) LF
         * Note that the CR+LF logic works for BIOS, but causes
         * extra returns after Linux commands. Using -l sets CR+LF.
	 * Using bTextMode detects if bg color, as in BIOS Setup
	 * or other apps that need CR+LF.
	 * 
	 * If stty has '-onlcr', then LF (fCRLF=2) is fine, but 
	 * if stty has 'onlcr', LF causes two output lines,
	 * so CR is the default (fCRLF=0), which works for 
	 * either 'onlcr' or '-onlcr'.
	 * Using -r (fRaw) has the same effect as LF (fCRLF=2).
         */
        if (c == CH_LF && !fRaw) { // input=enter/LF and not raw (-r)
            if ((fCRLF == 1) || (bTextMode > 40)) { /*-l option or bg color*/
		/*CR+LF: insert CR here, LF below*/
		payload[length++] = CH_CR; 
	    } 
	    else if (fCRLF == 2) ; /*LF: same as raw, use CH_LF*/
            else  c = CH_CR;  /* CR: switch LF to CR */
        }
        /* copy other data to payload */
        payload[length++] = c;
        lastc = c;
        if (c == CH_CR || c == CH_LF)
	    dbglog("sol_input[%d] payload: %02x %02x, bTextMode=%d\n",
			i,payload[0],payload[1],bTextMode);
#endif
	if (length >= max_bmc_data) {  /*overflow, truncate*/
	    dbglog("sol_send: ilen(%d) >= BMC max data(%d), truncated\n", 
		   ilen,max_bmc_data);
	    break;
	} else if (length >= sizeof(payload)) {  /*overflow, truncate*/
	    dbglog("sol_send: ilen(%d) >= buffer size(%d), truncated\n", 
		   ilen,sizeof(payload));
	    break;
	} 
    }  /*endif fdoinput*/
#ifdef WIN32
    i += 3;  /*increment to next dwKeyCode (3+1)*/
#endif
   }  /*end-for input[ilen] */

   if (length > 0) {
      /* Send the SOL payload */
      for (t = 0; t < sol_retries; t++) 
      {
         if (t > 0) 
	    os_usleep(0,(retry_time * 1000)); /*wait between retries 5000 us*/
         rv = sol_send(payload, length, &rs);
	 dbglog("sol_send(%d,%d) rv=%d rs.len=%d\n", t,length,rv,rs.len);
	 if (rv >= 0) {  /* rv ok */
	    if (rs.len != 0) {   /* have some rsp data, so handle it */
             if (rs.type == IPMI_PAYLOAD_TYPE_SOL) {
	       dbglog("output: handler(%d)\n",rs.len);
               sol_output_handler(&rs);
	     } else {
	       dbglog("WARNING: after sol_send, rs.type=%d, not SOL\n",rs.type);
	     }
	    } /*endif have rsp data*/
	    rv = 0;  /*recvd something, so dont retry */
         }
	 if (rv == 0) break;
	 /* else retry again if rv < 0 or rv == 0x02 */
      } /*end for loop*/
      if (rv < 0) rv = LAN_ERR_DROPPED;
   }
   if (rvend) rv = RV_END;
   return(rv);
}

static int send_activate_15sol(void)
{
   int rv = 0;
   uchar actcmd;
   uchar netfn;
   uchar ibuf[64];
   uchar rbuf[64];
   int rlen;
   uchar ilen, cc;

   lan_get_sol_data(fSolEncryption,sol15_iseed,&sol15_wseed);
   actcmd  = (uchar)(ACTIVATE_SOL1 & 0x00ff); 
   netfn   = NETFN_SOL; 
   if (fSolEncryption) ibuf[0] = 0x11;   /* use encryption, activate SOL */
   else ibuf[0] = 0x01;   /*no encryptionn, no serial alerts, activate SOL */
   memcpy(&ibuf[1],&sol15_wseed,4);  /*32-bit seed*/
   ilen = 5;
   rlen = sizeof(rbuf);
   rv = ipmi_cmdraw(actcmd,netfn,BMC_SA, PUBLIC_BUS, BMC_LUN,
                   ibuf,ilen,rbuf,&rlen,&cc,fdebug);
   if (rv == 0 && cc != 0) rv = cc;
   /* switch(cc) { case 0x80: SOL already active on another session
    *              case 0x81: SOL disabled
    *              case 0x82: SOL activation limit reached  }
    */
   if (rv == 0) {  /*success*/
      dbg_dump("sol 15 act_resp",rbuf,rlen,0);
      /* sol 15 act_resp (len=4): 0000: 00 01 64 64 */
      /* 00=auth, 01=seed_cnt, 64=in_payload_sz, 64=out_payload_sz */
      lan_set_sol_data(fSolEncryption,rbuf[0],rbuf[1], rbuf[2], rbuf[3]);
   }
   return(rv);
}

static int send_deactivate_15sol(void)
{
   int rv = -1;

   uchar actcmd;
   uchar netfn;
   uchar ibuf[64];
   uchar rbuf[64];
   int  rlen;
   uchar ilen, cc;

   /* SOL for IPMI 1.5 does not have an explicit deactivate, so
    * use an activate with data0 = 00. 
    */
   actcmd  = (uchar)(ACTIVATE_SOL1 & 0x00ff); 
   netfn   = NETFN_SOL; 
   if (fSolEncryption) ibuf[0] = 0x10;  /* deactivate, use encryption */
   else ibuf[0] = 0x00;    /*deactivate*/
   memcpy(&ibuf[1],&sol15_wseed,4);  /*32-bit seed*/
   ilen = 5;
   rlen = sizeof(rbuf);
   rv = ipmi_cmdraw(actcmd,netfn,BMC_SA, PUBLIC_BUS, BMC_LUN,
                   ibuf,ilen,rbuf,&rlen,&cc,fdebug);
   if (rv == 0 && cc != 0) rv = cc;
   dbg_dump("sol 15 deact_resp",rbuf,rlen,0); /* 80 00 32 ff */
   return(rv);
}

int send_activate_sol(uchar verSOL)
{
   int rv = 0;
   int in_payload_size, out_payload_size, udp_port;
   uchar actcmd;
   uchar netfn;
   uchar ibuf[64];
   uchar rbuf[64];
   int rlen;
   uchar ilen, cc;
   
   sol_ver = verSOL;
   /* activate SOL = 0x01, get SOL status = 0x05 */
   if (verSOL == 1) {
      rv = send_activate_15sol();
      return(rv);
   } else {   /* use IPMI 2.0 method */
      actcmd  = ACTIVATE_PAYLOAD;
      netfn   = NETFN_APP;  
      ibuf[0] = PAYLOAD_TYPE_SOL;
      ibuf[1] = payload_instance;  /* payload instance */
      ibuf[2] = SOL_SERIAL_ALERT_MASK_DEFERRED;
      if (fSolEncryption)     ibuf[2] |= 0x80;
      if (fSolAuthentication) ibuf[2] |= 0x40;
      if (vend_id == VENDOR_INTEL) 
           ibuf[2] |= SOL_BMC_ASSERTS_CTS_MASK_TRUE;
      else ibuf[2] |= SOL_BMC_ASSERTS_CTS_MASK_FALSE;
      ibuf[3] = 0;
      ibuf[4] = 0;
      ibuf[5] = 0;
      ilen = 6;
   }
   rlen = sizeof(rbuf);
   rv = ipmi_cmdraw(actcmd,netfn,BMC_SA, PUBLIC_BUS, BMC_LUN,
                   ibuf,ilen,rbuf,&rlen,&cc,fdebug);
   dbg_dump("sol act_cmd",ibuf,ilen,0);
   dbglog("send_activate v2(%x,%x) rv = %d cc = %x\n",actcmd,netfn,rv,cc);
   if (rv >= 0) {
      rv = cc;
      switch(cc) {
         case 0x00: 
	       if (rlen != 12) {
	         printm("Unexpected SOL response data received, len=%d\n",rlen);
	         rv = -1;
               }
               break;
         case 0x80: 
	       printm("SOL payload already active on another session\n");
               break;
         case 0x81: 
	       printm("SOL payload disabled\n");
               break;
         case 0x82: 
	       printm("SOL payload activation limit reached\n");
               break;
         case 0x83: 
	       printm("Cannot activate SOL payload with encryption\n");
               break;
         case 0x84: 
	       printm("Cannot activate SOL payload without encryption\n");
               break;
         default:
	       printm("Cannot activate SOL, ccode = 0x%02x\n",cc);
               break;
      }
   }
   if (rv == 0) {   /* success, use the response data */
      /* only here if response data is from IPMI 2.0 method */
      dbg_dump("sol act_resp",rbuf,rlen,0);
      in_payload_size  = rbuf[4] + (rbuf[5] << 8);
      out_payload_size = rbuf[6] + (rbuf[7] << 8);
      udp_port         = rbuf[8] + (rbuf[9] << 8);
      dbglog("activate ok, in=%d out=%d port=%d\n",
			in_payload_size, out_payload_size, udp_port);
      if (bSolVer == 2) 
         lan2_set_sol_data(in_payload_size, out_payload_size, udp_port,
			   (void *)lan2_recv_handler, sol_esc_ch);
   }
   return(rv);
}

int send_deactivate_sol(uchar verSOL)
{
   int rv = 0;
   uchar deactcmd;
   uchar netfn;
   uchar ibuf[64];
   uchar rbuf[64];
   int rlen;
   uchar ilen, cc;

   if (verSOL == 1) {
      return(send_deactivate_15sol());
   } else {   /* use IPMI 2.0 method */
      deactcmd  = DEACTIVATE_PAYLOAD;
      netfn   = NETFN_APP;
      ibuf[0] = PAYLOAD_TYPE_SOL;   /* payload type */
      ibuf[1] = payload_instance;  /* payload instance */
      ibuf[2] = 0;
      ibuf[3] = 0;
      ibuf[4] = 0;
      ibuf[5] = 0;
      ilen = 6;
   }
   rlen = sizeof(rbuf);
   rv = ipmi_cmdraw(deactcmd,netfn,BMC_SA, PUBLIC_BUS, BMC_LUN,
                   ibuf,ilen,rbuf,&rlen,&cc,fdebug);
   dbglog("sol deactivate rv = %d, cc = %x\n",rv,cc);
   if (rv == 0 && cc != 0) rv = cc;
   return(rv);
}

int sol_data_loop( void )
{
   int istdin;
   int rv = 0;
   uchar ibuf[IDATA_SZ];
   uchar cbuf[IDATA_SZ]; 
   int szibuf, szcbuf;
   SockType fd;
   int len;
   fd_set read_fds;
   fd_set error_fds;
   static uchar lastc = 0;
   int c;
#ifdef WIN32
   int c1;
   time_t ltime1 = 0;
   time_t ltime2 = 0;
   HANDLE thnd;
   istdin = 0;
#else
   long ltime1 = 0;
   long ltime2 = 0;
   istdin = fileno(stdin);
#endif

   /* 
    * OK, now loop and listen to 
    *   stdin for user input 
    *   interface fd for incoming SOL data 
    */
   /* sizeof(ibuf/cbuf)=IDATA_SZ(512), max_bmc_data=203 for sol data */
   szibuf = max_bmc_data;
   szcbuf = max_bmc_data;  
   fd = sol_get_fd();
   if (fScript) { /* open file_scr */
      fp_scr = fopen(file_scr,"r");
      if (fp_scr == NULL) {
	  printm("Cannot open %s, ignoring\n",file_scr);
          fScript = 0;
      }
   }
   if (fTrace) { /* open file_trc for writing */
      fp_trc = fopen(file_trc,"a");
      if (fp_trc == NULL) {
	  printm("Cannot open %s, ignoring\n",file_trc);
          fTrace = 0;
      }
   }

   dbglog("stdin = %d, intf->fd = %d\n",istdin,fd);
   if (sol_esc_len == 1) 
     printf("[SOL session is running, use '%c' to end session.]\n",sol_esc_ch);
   else 
     printf("[SOL session is running, use '%c.' to end, '%c?' for help.]\n",
		sol_esc_ch, sol_esc_ch);

   tty_setraw(2);
   sol_keepalive_reset();
#ifdef WIN32
   thnd = CreateThread(NULL, 0, &input_thread, NULL, 0, NULL);
   if (thnd == NULL) { 
       printm("CreateThread error, aborting\n");
       rv = -1; 
       sol_done = 1; 
   }
   console_open(fdebug);
#else
   if (bSolVer == 1) {   /* jump start the session v1.5 data */
      ibuf[0] = 0x1b; /*escape*/
      ibuf[1] = '{';
      rv = sol_input_handler(ibuf,2);
   }
#endif
   if (fdbglog) time((time_t *)&ltime1);
		   
   while (sol_done == 0) 
   {
       if (bKeepAlive > 0) {  /* send KeepAlive if needed */
           rv = sol_keepalive(bKeepAlive);   
	   /* if keepalive error, try to keep going anyway */
       }
       if (fdebug > 2) dbglog("os_select(%d,%d) called\n",istdin,fd);

       rv = os_select(istdin,fd, &read_fds, &error_fds);
       if (rv < 0) { 
	   dbglog("os_select(%d,%d) error %d\n",istdin,fd,rv);
	   perror("select"); 
           sol_done = 1;
       } else if (rv > 0) {
	   if (fdbglog) { 
	       time((time_t *)&ltime2);
	       dbglog("select rv = %d sockfd=%x stdin=%x time=%ld\n", rv,
			FD_ISSET(fd, &read_fds),FD_ISSET(istdin, &read_fds),
			(ltime2 - ltime1)); 
	   } 

           if (FD_ISSET(fd, &read_fds)) {
               SOL_RSP_PKT rs;
               rs.len = 0;
               /* receive output from BMC SOL */
               rv = sol_recv(&rs);
#ifdef WIN32
		/* Windows sometimes gets rv = -1 here */
               if (rv == -1) rv = -(WSAGetLastError());
#endif
               dbglog("output: recv rv = %d, len = %d\n",rv,rs.len);
               if (rv < 0) {
                   dbglog("Error %d reading SOL socket\n",rv);
                   printm("Error %d reading SOL socket\n",rv);
                   sol_done = 1;
               } else {
		   /*sol_output_handler sets fgotrecv*/
		   sol_output_handler(&rs);  
                   sol_keepalive_reset();
		   /* go back to select until there is no more socket data */
		   continue;  
	       }
           }  /*endif fd*/

           if (FD_ISSET(fd, &error_fds)) {
                   dbglog("Error selecting SOL socket\n",rv);
           }  /*endif fd*/

           if (FD_ISSET(istdin, &read_fds)) {
               /* input from stdin (user) if not WIN32 */
               memset(ibuf,0,szibuf);
               len = os_read(istdin, ibuf, szibuf);
               dbglog("stdin: read len=%d, %02x %02x %02x %02x\n",
					len,ibuf[0],ibuf[1],ibuf[2],ibuf[3]);
               if (len <= 0) {
                  dbglog("Error %d reading stdin\n",len);
                  printm("Error %d reading stdin\n",len);
                  sol_done = 1;
               } else {
                  rv = sol_input_handler(ibuf,len);
                  dbglog("sol_input: handler rv = %d\n",rv);
                  if (rv < 0) {
                      sol_done = 1;
                      if (rv == RV_END) {
		          dbglog("sol_data RV_END\n");
		          printf("\n%s exit via user input \n",progname);
                      } /* else drop through to show_outcomes */
                  }
		  else sol_keepalive_reset();
               }
           }  /*endif stdin*/

       }  /*endif select rv > 0 */
       else if (fScript) {   /*rv == 0*/
	   /* if we sent a script line, but no receive yet, keep waiting*/
	   if (fsentok && (fgotrecv == 0)) {
		dbglog("sol_data script sentok, but no recv\n");
		// continue; 
	   }
	   fsentok = 0;
	   /* read input stream from script file */
           memset(cbuf,0,szcbuf);
           /* read one line at a time */
	   rv = 0;
	   for (len = 0; len < szcbuf; ) {
                  c = fgetc(fp_scr);
		  if (c == EOF) { rv = -2; break; }
#ifdef WIN32
		  if ((c == '\n') && (lastc != '\r')) c1 = '\r'; /*Enter*/
		  else c1 = c;
		  Ascii2KeyCode((uchar)c1,&cbuf[len]);
		  len += 4;
#else
		  cbuf[len++] = c;
#endif
		  if (c == '\n') break; 
		  lastc = (uchar)c;
	   } 
           dbglog("script: read len=%d, %02x %02x %02x\n",
					len,cbuf[0],cbuf[1],cbuf[2]);
           if (rv != 0 || len == 0) {
                  dbglog("Stop reading file %s, rv=%d\n",file_scr,rv);
		  fclose(fp_scr); 
                  fScript = 0;
           } else {
		  /* TODO: add processing for some wait/sleep/prompt commands */
                  rv = sol_input_handler(cbuf,len);
                  dbglog("input: handler rv = %d\n",rv);
                  if (rv < 0) {
                      sol_done = 1;
                      if (rv == RV_END) {
		          dbglog("sol_data RV_END\n");
		          printf("\n%s exit via user input \n",progname);
                      } else printf("Error %d processing input\n",rv);
                  } else {
		      fsentok = 1;   /*sent a script line successfully*/
		      fgotrecv = 0;  /*need to wait for recv*/
		  }
           }
       } /*endif fScript*/
   }  /*end while*/
   if (rv == RV_END) rv = 0;
#ifdef WIN32
   os_usleep(0,5000);   /*wait 5 ms for thread to close itself*/ 
   CloseHandle(thnd);
   console_close();
#endif
   tty_setnormal(2);
   if (fScript) fclose(fp_scr); /* close file_scr */
   if (fTrace) fclose(fp_trc); /* close file_trc */
   return(rv);
}

static void show_usage(void)
{
                printf("Usage: %s [-acdeiolnrsvxz -NUPREFTVY]\n", progname);
                printf(" where -a     activate SOL console session\n");
                printf("       -d     deactivate SOL console session\n");
                printf("       -c'^'  set escape Char to '^', default='~'\n");
                printf("       -e     Encryption off for SOL session\n");
                printf("       -i file  Input script file\n");
                printf("       -o file  Output trace file\n");
                printf("       -l     Legacy mode for BIOS/DOS CR+LF\n");
                printf("       -n 1   Payload instance Number, default=1\n");
                printf("       -r     Raw termio, no VT-ANSI translation\n");
                printf("       -s NNN Slow link delay, default=100usec\n");
#ifdef NOT_DOCUMENTED
                printf("       -t N   SOL send timeout reTries, default=1\n");
                printf("       -u N   Wait time in msec for tuning, default=500\n");
#endif
#ifdef WIN32
                printf("       -w     do not use Windows Console buffer, use stdio\n");
#endif
                printf("       -v     debug log filename (default=isoldbg.log)\n");
                printf("       -x     show eXtra debug messages in debug log\n");
                printf("       -z     show even more debug messages\n");
		print_lan_opt_usage(1);
}

#ifdef METACOMMAND
int i_sol(int argc, char **argv)
#else
#ifdef WIN32
int __cdecl
#else
int
#endif
main(int argc, char **argv)
#endif
{
   int ret = 0;
   int i;
   uchar devrec[16];
   uchar bmcver[2];
   int c;

   printf("%s ver %s\n", progname,progver);

   parse_lan_options('V',"2",0);  /*default to user priv*/

   while ( (c = getopt( argc, argv,"ac:dei:k:ln:o:p:rs:t:u:wv:xzEF:J:N:P:R:T:U:V:YZ:?")) != EOF ) 
      switch(c) {
          case 'a': factivate = 1;   break;    /*activate*/
          case 'd': fdeactivate = 1; break;    /*deactivate*/
          case 'c':     		       /* escape char */
                if (strncmp(optarg,"0x",2) == 0) 
			sol_esc_ch = htoi(&optarg[2]);  /*hex char value*/
                else if (optarg[0] >= '0' && optarg[0] <= '9') 
			sol_esc_ch = atob(optarg); /*decimal char value*/
		else    sol_esc_ch = optarg[0];  /*single ascii char*/
		sol_esc_len = 1;  /*set single-char esc sequence */
		break;
          case 'e': fSolEncryption = 0; break; /*encryption off*/
          case 'k': i = atoi(optarg); /*sol keepalive timeout, default = 30*/
		    if (i < 0) 
			printf("Invalid keepalive timeout %d, ignoring.\n",i);
		    else sol_timeout = i;
		    if (i == 0) bKeepAlive = 0;
		    if (i == 32) bKeepAlive = 2; /*++++ for debug*/
		    break;
          case 'l': fCRLF = 1; break;          /*do legacy CR+LF translation*/
          case 'i': strncpy(file_scr,optarg,sizeof(file_scr)); 
		    fScript = 1;  break;    /*script input file*/
          case 'n': i = atoi(optarg);     /* payload_instance */
		    if ((i <= 0) || (i > 255)) 
			printf("Invalid payload instance %d, ignoring.\n",i);
		    else payload_instance = i;
		    break;
          case 'o': strncpy(file_trc,optarg,sizeof(file_trc));
		    fTrace = 1;  break;     /*trace output file*/
          case 'r': fRaw = 1; fCRLF = 0; fUseWinCon = 0;
			break; /*raw termio, xlate off*/
          case 's': sol_recvdelay = atoi(optarg); break; /*slow recv delay*/
          case 't': sol_retries = atoi(optarg); break;   /*timeout/retries*/
          case 'u': wait_time = atoi(optarg); break;  /*wait_time for tuning*/
          case 'v': strncpy(dbglog_name,optarg,sizeof(dbglog_name));
		    fdbglog = 1;  break;      /*name of debug log file*/
          case 'w': fUseWinCon = 0;  break;    /*do not use Console routines*/
          case 'x': 
		    if (fdebug == 0) fdebug = 2; /*only normal via dbglog */
		    else fdebug++;  /* -xx = full, -xxx = max */
		    break; 
          case 'z': fdebug = 3;  break;  /*full debug messages */
          case 'V':    /* priv level */
		fprivset = 1;
          case 'p':    /* port */
          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 'Y':    /* prompt for remote password */
          case 'Z':    /* set local MC address */
                parse_lan_options(c,optarg,fdebug);
                break;
	  default:
                show_usage();
		ret = ERR_USAGE;
		goto do_exit;
      }

   if (is_remote() == 0) {  /* no node specified */
      printf("Serial-Over-Lan Console requires a -N nodename\n");
      ret = ERR_BAD_PARAM;
      goto do_exit;
   }
   if (fdeactivate && !fprivset) 
      parse_lan_options('V',"4",0); /*deactivate requires admin priv */

   /* fdebug: 1=debug/no packets, 2=debug log only, 3=full debug, 4=max debug*/
   if (fdebug > 1) fdbglog = 1;

   if (factivate == 0 && fdeactivate == 0) {
      show_usage();
      printf("Error: Need either -a or -d for sol\n");
      ret = ERR_BAD_PARAM;
      goto do_exit;
   }
#ifdef WIN32
   else if (factivate) {
      if (fdebug) printf("%s activate, connecting ...\n", progname);
   }
#endif
   if (fdbglog) {
      // strcpy(log_name,dbglog_name);
      open_log(dbglog_name);
      dbglog("%s ver %s\r\n", progname,progver);
   }

   i = get_driver_type(); /*see if user explictly set type*/
   if (i == DRV_UNKNOWN) bDriver = DRV_UNKNOWN;  /*no driver type specified*/
   else bDriver = (uchar)i;

   ret = ipmi_getdeviceid(devrec,16,fdebug);
   if (ret != 0) {
      goto do_exit;
   } else {
      ipmi_maj = devrec[4] & 0x0f;
      ipmi_min = devrec[4] >> 4;
      vend_id  = devrec[6] + (devrec[7] << 8) + (devrec[8] << 16); 
      prod_id  = devrec[9] + (devrec[10] << 8);
      show_devid( devrec[2],  devrec[3], ipmi_maj, ipmi_min);
      bmcver[0] = devrec[2];
      bmcver[1] = devrec[3];
      switch(vend_id) {
	case VENDOR_DELL:  /*Dell == 0x0002A2*/
	   max_bmc_data = MAX_DELL_DATA; /*shorter max data*/
	   break;
	case VENDOR_SUPERMICRO:
	case VENDOR_LMC:
	case VENDOR_PEPPERCON: /* 0x0028C5  Peppercon/Raritan*/
           sol_timeout = 10;  /* shorter 10 sec SOL keepalive timeout */
	   max_bmc_data = MAX_OTHER_DATA;
	   break;
	case VENDOR_INTEL:
	   max_bmc_data = MAX_INTEL_DATA;
	   break;
	case VENDOR_KONTRON:
	   max_bmc_data = MAX_KONTRON_DATA; 
	   // bKeepAlive = 1;  
	   break;
	default:
	   max_bmc_data = MAX_OTHER_DATA;
	   break;
      }
   }

   ret = ipmi_getpicmg(devrec,16,fdebug);
   if (ret == 0) fpicmg = 1;

   dbglog("driver=%d fdebug=%d vend_id=%x bmcver=%x.%x ipmi %d.%d\n",
	  bDriver,fdebug,vend_id,bmcver[0],bmcver[1],ipmi_maj,ipmi_min); 
   /* check for SOL support */
   if (ipmi_maj >= 2) { 
       if ((bDriver == DRV_LAN) && (vend_id == VENDOR_INTEL)) {
          /* user specified to use IPMI LAN 1.5 SOL on Intel */
          bSolVer = 1;      /*IPMI 1.5 SOL*/
       } else {
          bSolVer = 2;      /*IPMI 2.0 SOL*/
          if (get_driver_type() == DRV_LAN) { /*now using IPMI LAN 1.5*/
	    char *ptyp; 
            ipmi_close_();           /*close current IPMI LAN 1.5*/
	    if (is_lan2intel(vend_id,prod_id)) ptyp = "lan2i";
            else ptyp = "lan2";
            i = set_driver_type(ptyp); /*switch to IPMI LAN 2.0*/
          }
       } 
   } else if (ipmi_maj == 1) {
       if (ipmi_min >= 5) bSolVer = 1;  /* IPMI 1.5 */
       else bSolVer = 0;  /* IPMI 1.0 */
   } else bSolVer = 0;
   if (bSolVer == 0) {
        printf("Serial Over Lan not supported for this IPMI version\n");
	ret = LAN_ERR_NOTSUPPORT;
        goto do_exit;
   }
#ifndef HAVE_LANPLUS
   if (bSolVer == 2) {
       printf("2.0 LanPlus module not available, trying 1.5 SOL instead\n");
       bSolVer = 1;
   }
#endif
   /* May also want to verify that SOL is implemented here */

   /*
    * Spawn a console raw terminal thread now, which will wait for the 
    * "Activating cmd (0x02)" on success 
    */
   if (fdeactivate) {
      /* Request admin privilege by default, since deactivate requires it. */
      ret = send_deactivate_sol(bSolVer); 
      dbglog("send_deactivate_sol rv = %d\n",ret);
   } else if (factivate) {
      ret = send_activate_sol(bSolVer);
      dbglog("send_activate_sol(%d) rv = %d\n",bSolVer,ret);
      if (bSolVer == 2) {
	 lan2_latency = lan2_get_latency();
	 retry_time = lan2_latency;
         dbglog("lan2_latency = %ld msec (slow > %ld msec)\n",
		lan2_latency,lan2_slow);
	 if ((sol_recvdelay == 0) && (lan2_latency > lan2_slow))
		sol_recvdelay = 500;  /* it is slow, set recvdelay */
	 if (sol_recvdelay != 0) {
            dbglog("set_recvdelay to %dus\n",sol_recvdelay);
 	    lanplus_set_recvdelay(sol_recvdelay);
	 }
      } /*endif bSolVer==2*/
      if (ret == 0) {
         ret = sol_data_loop();
      } /*endif activate ok*/
   }  /*endif do activate */

do_exit:
   if (ret != LAN_ERR_DROPPED) { /*if link dropped, no need to close*/
      ipmi_close_();
      os_usleep(0,(wait_time * 1000));  /*wait 0.5 sec for BMC teardown*/
   }
   if (fdebug) show_outcome(progname,ret);  
   if (fdbglog) close_log();
   return(ret);
}  /* end main()*/
#endif

/* end isol.c */