summaryrefslogtreecommitdiff
path: root/lib/ipmi_sol.c
blob: 3acd5bb5a9cc8d2a76b72c60ce69b430c7f9e45b (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
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
/*                                 
 * Copyright (c) 2003 Sun Microsystems, 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:
 * 
 * Redistribution of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistribution 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.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
 * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
 * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
 * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 */
#define _XOPEN_SOURCE
#define _BSD_SOURCE || \
	(_XOPEN_SOURCE >= 500 || \
                       _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) && \
	!(_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700)

#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>

#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif

#if defined(HAVE_TERMIOS_H)
# include <termios.h>
#elif defined (HAVE_SYS_TERMIOS_H)
# include <sys/termios.h>
#endif

#include <ipmitool/helper.h>
#include <ipmitool/log.h>
#include <ipmitool/ipmi.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_sol.h>
#include <ipmitool/ipmi_strings.h>
#include <ipmitool/bswap.h>


#define SOL_PARAMETER_SET_IN_PROGRESS           0x00
#define SOL_PARAMETER_SOL_ENABLE                0x01
#define SOL_PARAMETER_SOL_AUTHENTICATION        0x02
#define SOL_PARAMETER_CHARACTER_INTERVAL        0x03
#define SOL_PARAMETER_SOL_RETRY                 0x04
#define SOL_PARAMETER_SOL_NON_VOLATILE_BIT_RATE 0x05
#define SOL_PARAMETER_SOL_VOLATILE_BIT_RATE     0x06
#define SOL_PARAMETER_SOL_PAYLOAD_CHANNEL       0x07
#define SOL_PARAMETER_SOL_PAYLOAD_PORT          0x08

#define MAX_SOL_RETRY 6

const struct valstr sol_parameter_vals[] = {
	{ SOL_PARAMETER_SET_IN_PROGRESS,           "Set In Progress (0)" },
	{ SOL_PARAMETER_SOL_ENABLE,                "Enable (1)" },
	{ SOL_PARAMETER_SOL_AUTHENTICATION,        "Authentication (2)" },
	{ SOL_PARAMETER_CHARACTER_INTERVAL,        "Character Interval (3)" },
	{ SOL_PARAMETER_SOL_RETRY,                 "Retry (4)" },
	{ SOL_PARAMETER_SOL_NON_VOLATILE_BIT_RATE, "Nonvolatile Bitrate (5)" },
	{ SOL_PARAMETER_SOL_VOLATILE_BIT_RATE,     "Volatile Bitrate (6)" },
	{ SOL_PARAMETER_SOL_PAYLOAD_CHANNEL,       "Payload Channel (7)" },
	{ SOL_PARAMETER_SOL_PAYLOAD_PORT,          "Payload Port (8)" },
	{ 0x00, NULL },
};


static struct timeval _start_keepalive;
static struct termios _saved_tio;
static int            _in_raw_mode = 0;
static int            _disable_keepalive = 0;
static int            _use_sol_for_keepalive = 0;

extern int verbose;

/*
 * ipmi_sol_payload_access
 */
int
ipmi_sol_payload_access(struct ipmi_intf * intf, uint8_t channel,
		uint8_t userid, int enable)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	int rc = (-1);
	uint8_t data[6];

	memset(&req, 0, sizeof(req));
	req.msg.netfn = IPMI_NETFN_APP;
	req.msg.cmd = IPMI_SET_USER_PAYLOAD_ACCESS;
	req.msg.data = data;
	req.msg.data_len = 6;

	memset(data, 0, 6);
	/* channel */
	data[0] = channel & 0xf;
	/* user id */
	data[1] = userid & 0x3f;
	if (!enable) {
		/* disable */
		data[1] |= 0x40;
	}
	/* payload 1 is SOL */
	data[2] = 0x02;
	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error %sabling SOL payload for user %d on channel %d",
				enable ? "en" : "dis", userid, channel);
		rc = (-1);
	} else if (rsp->ccode != 0) {
		lprintf(LOG_ERR, "Error %sabling SOL payload for user %d on channel %d: %s",
				enable ? "en" : "dis", userid, channel,
				val2str(rsp->ccode, completion_code_vals));
		rc = (-1);
	} else {
		rc = 0;
	}
	return rc;
}

int
ipmi_sol_payload_access_status(struct ipmi_intf * intf,
				uint8_t channel,
				uint8_t userid)
{
	struct ipmi_rq req;
	struct ipmi_rs *rsp;
	uint8_t data[2];

	memset(&req, 0, sizeof(req));
	req.msg.netfn    = IPMI_NETFN_APP;
	req.msg.cmd      = IPMI_GET_USER_PAYLOAD_ACCESS;
	req.msg.data     = data;
	req.msg.data_len = sizeof(data);

	data[0] = channel & 0xf;	/* channel */
	data[1] = userid & 0x3f;	/* user id */
	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error. No valid response received.");
		return -1;
	}

	switch(rsp->ccode) {
		case 0x00:
			if (rsp->data_len != 4) {
				lprintf(LOG_ERR, "Error parsing SOL payload status for user %d on channel %d",
					userid, channel);
				return -1;
			}

			printf("User %d on channel %d is %sabled\n",
				userid, channel, (rsp->data[0] & 0x02) ? "en":"dis");
			return 0;

		default:
			lprintf(LOG_ERR, "Error getting SOL payload status for user %d on channel %d: %s",
				userid, channel, 
				val2str(rsp->ccode, completion_code_vals));
			return -1;
	}
}


/*
 * ipmi_get_sol_info
 */
int
ipmi_get_sol_info(
				  struct ipmi_intf * intf,
				  uint8_t channel,
				  struct sol_config_parameters * params)
{
	struct ipmi_rs * rsp;
	struct ipmi_rq req;
	uint8_t data[4];

	memset(&req, 0, sizeof(req));
	req.msg.netfn    = IPMI_NETFN_TRANSPORT;
	req.msg.cmd      = IPMI_GET_SOL_CONFIG_PARAMETERS;
	req.msg.data_len = 4;
	req.msg.data     = data;

	/*
	 * set in progress
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                       /* channel number     */
	data[1] = SOL_PARAMETER_SET_IN_PROGRESS; /* parameter selector */
	data[2] = 0x00;                          /* set selector       */
	data[3] = 0x00;                          /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 2) {
				params->set_in_progress = rsp->data[1];
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL enable
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                  /* channel number     */
	data[1] = SOL_PARAMETER_SOL_ENABLE; /* parameter selector */
	data[2] = 0x00;                     /* set selector       */
	data[3] = 0x00;                     /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 2) {
				params->enabled = rsp->data[1];
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL authentication
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                          /* channel number     */
	data[1] = SOL_PARAMETER_SOL_AUTHENTICATION; /* parameter selector */
	data[2] = 0x00;                             /* set selector       */
	data[3] = 0x00;                             /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 2) {
				params->force_encryption     = ((rsp->data[1] & 0x80)? 1 : 0);
				params->force_authentication = ((rsp->data[1] & 0x40)? 1 : 0);
				params->privilege_level      = rsp->data[1] & 0x0F;
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * Character accumulate interval and character send interval
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                          /* channel number     */
	data[1] = SOL_PARAMETER_CHARACTER_INTERVAL; /* parameter selector */
	data[2] = 0x00;                             /* set selector       */
	data[3] = 0x00;                             /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 3) {
				params->character_accumulate_level = rsp->data[1];
				params->character_send_threshold   = rsp->data[2];
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL retry
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                 /* channel number     */
	data[1] = SOL_PARAMETER_SOL_RETRY; /* parameter selector */
	data[2] = 0x00;                    /* set selector       */
	data[3] = 0x00;                    /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 3) {
				params->retry_count    = rsp->data[1];
				params->retry_interval = rsp->data[2];
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL non-volatile bit rate
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                                 /* channel number     */
	data[1] = SOL_PARAMETER_SOL_NON_VOLATILE_BIT_RATE; /* parameter selector */
	data[2] = 0x00;                                    /* set selector       */
	data[3] = 0x00;                                    /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 2) {
				params->non_volatile_bit_rate = rsp->data[1] & 0x0F;
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL volatile bit rate
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                             /* channel number     */
	data[1] = SOL_PARAMETER_SOL_VOLATILE_BIT_RATE; /* parameter selector */
	data[2] = 0x00;                                /* set selector       */
	data[3] = 0x00;                                /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 2) {
				params->volatile_bit_rate = rsp->data[1] & 0x0F;
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported",
					val2str(data[1], sol_parameter_vals));
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL payload channel
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                           /* channel number     */
	data[1] = SOL_PARAMETER_SOL_PAYLOAD_CHANNEL; /* parameter selector */
	data[2] = 0x00;                              /* set selector       */
	data[3] = 0x00;                              /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 2) {
				params->payload_channel = rsp->data[1];
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported - defaulting to 0x%02x",
					val2str(data[1], sol_parameter_vals), channel);
			params->payload_channel = channel;
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
					val2str(data[1], sol_parameter_vals),
					val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	/*
	 * SOL payload port
	 */
	memset(data, 0, sizeof(data));
	data[0] = channel;                        /* channel number     */
	data[1] = SOL_PARAMETER_SOL_PAYLOAD_PORT; /* parameter selector */
	data[2] = 0x00;                           /* set selector       */
	data[3] = 0x00;                           /* block selector     */

	rsp = intf->sendrecv(intf, &req);
	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error: No response requesting SOL parameter '%s'",
				val2str(data[1], sol_parameter_vals));
		return (-1);
	}

	switch (rsp->ccode) {
		case 0x00:
			if (rsp->data_len == 3) {
				params->payload_port = (rsp->data[1]) | (rsp->data[2] << 8);
			} else {
				lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						"for SOL parameter '%s'",
						rsp->data_len,
						val2str(data[1], sol_parameter_vals));
			}
			break;
		case 0x80:
			if( intf->session != NULL ) {
				lprintf(LOG_ERR, "Info: SOL parameter '%s' not supported - defaulting to %d",
						val2str(data[1], sol_parameter_vals), intf->ssn_params.port);
				params->payload_port = intf->ssn_params.port;
			} else {
				lprintf(LOG_ERR,
						"Info: SOL parameter '%s' not supported - can't determine which "
						"payload port to use on NULL session",
						val2str(data[1], sol_parameter_vals));
						return (-1);               
			}
			break;
		default:
			lprintf(LOG_ERR, "Error requesting SOL parameter '%s': %s",
				val2str(data[1], sol_parameter_vals),
				val2str(rsp->ccode, completion_code_vals));
			return (-1);
	}

	return 0;
}



/*
 * ipmi_print_sol_info
 */
static int
ipmi_print_sol_info(struct ipmi_intf * intf, uint8_t channel)
{
	struct sol_config_parameters params = {0};
	if (ipmi_get_sol_info(intf, channel, &params))
		return -1;

	if (csv_output)
	{
		printf("%s,",
			   val2str(params.set_in_progress & 0x03,
					   ipmi_set_in_progress_vals));
		printf("%s,", params.enabled?"true": "false");
		printf("%s,", params.force_encryption?"true":"false");
		printf("%s,", params.force_encryption?"true":"false");
		printf("%s,",
			   val2str(params.privilege_level, ipmi_privlvl_vals));
		printf("%d,", params.character_accumulate_level * 5);
		printf("%d,", params.character_send_threshold);
		printf("%d,", params.retry_count);
		printf("%d,", params.retry_interval * 10);

		printf("%s,",
			   val2str(params.volatile_bit_rate, ipmi_bit_rate_vals));

		printf("%s,",
			   val2str(params.non_volatile_bit_rate, ipmi_bit_rate_vals));

		printf("%d,", params.payload_channel);
		printf("%d\n", params.payload_port);
	}
	else
	{
		printf("Set in progress                 : %s\n",
			   val2str(params.set_in_progress & 0x03,
					   ipmi_set_in_progress_vals));
		printf("Enabled                         : %s\n",
			   params.enabled?"true": "false");
		printf("Force Encryption                : %s\n",
			   params.force_encryption?"true":"false");
		printf("Force Authentication            : %s\n",
			   params.force_authentication?"true":"false");
		printf("Privilege Level                 : %s\n",
			   val2str(params.privilege_level, ipmi_privlvl_vals));
		printf("Character Accumulate Level (ms) : %d\n",
			   params.character_accumulate_level * 5);
		printf("Character Send Threshold        : %d\n",
			   params.character_send_threshold);
		printf("Retry Count                     : %d\n",
			   params.retry_count);
		printf("Retry Interval (ms)             : %d\n",
			   params.retry_interval * 10);

		printf("Volatile Bit Rate (kbps)        : %s\n",
			   val2str(params.volatile_bit_rate, ipmi_bit_rate_vals));

		printf("Non-Volatile Bit Rate (kbps)    : %s\n",
			   val2str(params.non_volatile_bit_rate, ipmi_bit_rate_vals));

		printf("Payload Channel                 : %d (0x%02x)\n",
			   params.payload_channel, params.payload_channel);
		printf("Payload Port                    : %d\n",
			   params.payload_port);
	}

	return 0;
}



/*
 * Small function to validate that user-supplied SOL
 * configuration parameter values we store in uint8_t
 * data type falls within valid range.  With minval
 * and maxval parameters we can use the same function
 * to validate parameters that have different ranges
 * of values.
 *
 * function will return -1 if value is not valid, or
 * will return 0 if valid.
 */
int ipmi_sol_set_param_isvalid_uint8_t( const char *strval,
					const char *name,
					int base,
					uint8_t minval,
					uint8_t maxval,
					uint8_t *out_value)
{
	if (str2uchar(strval, out_value) != 0 || (*out_value < minval)
			|| (*out_value > maxval)) {
		lprintf(LOG_ERR, "Invalid value %s for parameter %s",
			strval, name);
		lprintf(LOG_ERR, "Valid values are %d-%d", minval, maxval);
		return -1;
	}
	return 0;
}


/*
 * ipmi_sol_set_param
 *
 * Set the specified Serial Over LAN value to the specified
 * value
 *
 * return 0 on success,
 *        -1 on failure
 */
static int
ipmi_sol_set_param(struct ipmi_intf * intf,
		   uint8_t            channel,
		   const char       * param,
		   const char       * value,
		   uint8_t            guarded)
{
	struct ipmi_rs * rsp;
	struct ipmi_rq   req;
	uint8_t          data[4];
	int              bGuarded = guarded; /* Use set-in-progress indicator? */

	memset(&req, 0, sizeof(req));
	req.msg.netfn    = IPMI_NETFN_TRANSPORT;           /* 0x0c */
	req.msg.cmd      = IPMI_SET_SOL_CONFIG_PARAMETERS; /* 0x21 */
	req.msg.data     = data;

	data[0] = channel;

	/*
	 * set-in-progress
	 */
	if (! strcmp(param, "set-in-progress"))
	{
		bGuarded = 0; /* We _ARE_ the set-in-progress indicator */
		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SET_IN_PROGRESS;

		if (! strcmp(value, "set-complete"))
			data[2] = 0x00;
		else if (! strcmp(value, "set-in-progress"))
			data[2] = 0x01;
		else if (! strcmp(value, "commit-write"))
			data[2] = 0x02;
		else
		{
			lprintf(LOG_ERR, "Invalid value %s for parameter %s",
				   value, param);
			lprintf(LOG_ERR, "Valid values are set-complete, set-in-progress "
				   "and commit-write");
			return -1;
		}
	}


	/*
	 * enabled
	 */
	else if (! strcmp(param, "enabled"))
	{
		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SOL_ENABLE;

		if (! strcmp(value, "true"))
			data[2] = 0x01;
		else if (! strcmp(value, "false"))
			data[2] = 0x00;
		else
		{
			lprintf(LOG_ERR, "Invalid value %s for parameter %s",
				   value, param);
			lprintf(LOG_ERR, "Valid values are true and false");
			return -1;
		}
	}


	/*
	 * force-payload-encryption 
	 */
	else if (! strcmp(param, "force-encryption"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SOL_AUTHENTICATION;

		if (! strcmp(value, "true"))
			data[2] = 0x80;
		else if (! strcmp(value, "false"))
			data[2] = 0x00;
		else
		{
			lprintf(LOG_ERR, "Invalid value %s for parameter %s",
				   value, param);
			lprintf(LOG_ERR, "Valid values are true and false");
			return -1;
		}


		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[2] |= params.force_authentication? 0x40 : 0x00;
		data[2] |= params.privilege_level;
	}


	/*
	 * force-payload-authentication
	 */
	else if (! strcmp(param, "force-authentication"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SOL_AUTHENTICATION;

		if (! strcmp(value, "true"))
			data[2] = 0x40;
		else if (! strcmp(value, "false"))
			data[2] = 0x00;
		else
		{
			lprintf(LOG_ERR, "Invalid value %s for parameter %s",
				   value, param);
			lprintf(LOG_ERR, "Valid values are true and false");
			return -1;
		}


		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[2] |= params.force_encryption? 0x80 : 0x00;
		data[2] |= params.privilege_level;
	}


	/*
	 * privilege-level
	 */
	else if (! strcmp(param, "privilege-level"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SOL_AUTHENTICATION;

		if (! strcmp(value, "user"))
			data[2] = 0x02;
		else if (! strcmp(value, "operator"))
			data[2] = 0x03;
		else if (! strcmp(value, "admin"))
			data[2] = 0x04;
		else if (! strcmp(value, "oem"))
			data[2] = 0x05;
		else
		{
			lprintf(LOG_ERR, "Invalid value %s for parameter %s",
				   value, param);
			lprintf(LOG_ERR, "Valid values are user, operator, admin, and oem");
			return -1;
		}


		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[2] |= params.force_encryption?     0x80 : 0x00;
		data[2] |= params.force_authentication? 0x40 : 0x00;
	}


	/*
	 * character-accumulate-level
	 */
	else if (! strcmp(param, "character-accumulate-level"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 4;
		data[1] = SOL_PARAMETER_CHARACTER_INTERVAL;

		/* validate user-supplied input */
		if (ipmi_sol_set_param_isvalid_uint8_t(value, param, 0, 1, 255, &data[2]))
			return -1;

		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[3] = params.character_send_threshold;
	}


	/*
	 * character-send-threshold
	 */
	else if (! strcmp(param, "character-send-threshold"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 4;
		data[1] = SOL_PARAMETER_CHARACTER_INTERVAL;

		/* validate user-supplied input */
		if (ipmi_sol_set_param_isvalid_uint8_t(value, param, 0, 0, 255, &data[3]))
			return -1;

		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[2] = params.character_accumulate_level;
	}


	/*
	 * retry-count
	 */
	else if (! strcmp(param, "retry-count"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 4;
		data[1] = SOL_PARAMETER_SOL_RETRY;

		/* validate user input, 7 is max value */
		if (ipmi_sol_set_param_isvalid_uint8_t(value, param, 0, 0, 7, &data[2]))
			return -1;

		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[3] = params.retry_interval;
	}


	/*
	 * retry-interval
	 */
	else if (! strcmp(param, "retry-interval"))
	{
		struct sol_config_parameters params;

		req.msg.data_len = 4;
		data[1] = SOL_PARAMETER_SOL_RETRY;

		/* validate user-supplied input */
		if (ipmi_sol_set_param_isvalid_uint8_t(value, param, 0, 0, 255, &data[3]))
			return -1;

		/* We need other values to complete the request */
		if (ipmi_get_sol_info(intf, channel, &params))
		{
			lprintf(LOG_ERR, "Error fetching SOL parameters for %s update",
				   param);
			return -1;
		}

		data[2] = params.retry_count;
	}


	/*
	 * non-volatile-bit-rate
	 */
	else if (! strcmp(param, "non-volatile-bit-rate"))
	{
		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SOL_NON_VOLATILE_BIT_RATE;

		if (!strcmp(value, "serial"))
		{
			data[2] = 0x00;
		}
		else if (!strcmp(value, "9.6"))
		{
			data[2] = 0x06;
		}
		else if (!strcmp(value, "19.2"))
		{
			data[2] = 0x07;
		}
		else if (!strcmp(value, "38.4"))
		{
			data[2] = 0x08;
		}
		else if (!strcmp(value, "57.6"))
		{
			data[2] = 0x09;
		}
		else if (!strcmp(value, "115.2"))
		{
			data[2] = 0x0A;
		}
		else
		{
			lprintf(LOG_ERR, "Invalid value \"%s\" for parameter \"%s\"",
				   value,
				   param);
			lprintf(LOG_ERR, "Valid values are serial, 9.6 19.2, 38.4, 57.6 and 115.2");
			return -1;
		}
	}


	/*
	 * volatile-bit-rate
	 */
	else if (! strcmp(param, "volatile-bit-rate"))
	{
		req.msg.data_len = 3;
		data[1] = SOL_PARAMETER_SOL_VOLATILE_BIT_RATE;

		if (!strcmp(value, "serial"))
		{
			data[2] = 0x00;
		}
		else if (!strcmp(value, "9.6"))
		{
			data[2] = 0x06;
		}
		else if (!strcmp(value, "19.2"))
		{
			data[2] = 0x07;
		}
		else if (!strcmp(value, "38.4"))
		{
			data[2] = 0x08;
		}
		else if (!strcmp(value, "57.6"))
		{
			data[2] = 0x09;
		}
		else if (!strcmp(value, "115.2"))
		{
			data[2] = 0x0A;
		}
		else
		{
			lprintf(LOG_ERR, "Invalid value \"%s\" for parameter \"%s\"",
				   value,
				   param);
			lprintf(LOG_ERR, "Valid values are serial, 9.6 19.2, 38.4, 57.6 and 115.2");
			return -1;
		}
	}
	else
	{
		lprintf(LOG_ERR, "Error: invalid SOL parameter %s", param);
		return -1;
	}


	/*
	 * Execute the request
	 */
	if (bGuarded &&
		(ipmi_sol_set_param(intf,
				    channel,
				    "set-in-progress",
				    "set-in-progress",
				    bGuarded)))
	{
		lprintf(LOG_ERR, "Error: set of parameter \"%s\" failed", param);
		return -1;
	}


	/* The command proper */
	rsp = intf->sendrecv(intf, &req);

	if (rsp == NULL) {
		lprintf(LOG_ERR, "Error setting SOL parameter '%s'", param);
		return -1;
	}

	if (!(!strncmp(param, "set-in-progress", 15) && !strncmp(value, "commit-write", 12)) &&
	    rsp->ccode > 0) {
		switch (rsp->ccode) {
		case 0x80:
			lprintf(LOG_ERR, "Error setting SOL parameter '%s': "
				"Parameter not supported", param);
			break;
		case 0x81:
			lprintf(LOG_ERR, "Error setting SOL parameter '%s': "
				"Attempt to set set-in-progress when not in set-complete state",
				param);
			break;
		case 0x82:
			lprintf(LOG_ERR, "Error setting SOL parameter '%s': "
				"Attempt to write read-only parameter", param);
			break;
		case 0x83:
			lprintf(LOG_ERR, "Error setting SOL parameter '%s': "
				"Attempt to read write-only parameter", param);
			break;
		default:
			lprintf(LOG_ERR, "Error setting SOL parameter '%s' to '%s': %s",
				param, value, val2str(rsp->ccode, completion_code_vals));
			break;
		}

		if (bGuarded &&
			(ipmi_sol_set_param(intf,
					    channel,
					    "set-in-progress",
					    "set-complete",
					    bGuarded)))
		{
			lprintf(LOG_ERR, "Error could not set \"set-in-progress\" "
				   "to \"set-complete\"");
		}

		return -1;
	}


	/*
	 * The commit write could very well fail, but that's ok.
	 * It may not be implemented.
	 */
	if (bGuarded)
		ipmi_sol_set_param(intf,
				   channel,
				   "set-in-progress",
				   "commit-write",
				   bGuarded);


	if (bGuarded &&
 		ipmi_sol_set_param(intf,
				   channel,
				   "set-in-progress",
				   "set-complete",
				   bGuarded))
	{
		lprintf(LOG_ERR, "Error could not set \"set-in-progress\" "
			   "to \"set-complete\"");
		return -1;
	}

	return 0;
}



void
leave_raw_mode(void)
{
	if (!_in_raw_mode)
		return;
	if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1)
		perror("tcsetattr");
	else
		_in_raw_mode = 0;
}



void
enter_raw_mode(void)
{
	struct termios tio;
	if (tcgetattr(fileno(stdin), &tio) == -1) {
		perror("tcgetattr");
		return;
	}
	_saved_tio = tio;
	tio.c_iflag |= IGNPAR;
	tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
	tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
	//	#ifdef IEXTEN
	tio.c_lflag &= ~IEXTEN;
	//	#endif
	tio.c_oflag &= ~OPOST;
	tio.c_cc[VMIN] = 1;
	tio.c_cc[VTIME] = 0;
	if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1)
		perror("tcsetattr");
	else
		_in_raw_mode = 1;
}


static void
sendBreak(struct ipmi_intf * intf)
{
	struct ipmi_v2_payload  v2_payload;

	memset(&v2_payload, 0, sizeof(v2_payload));

	v2_payload.payload.sol_packet.character_count = 0;
	v2_payload.payload.sol_packet.generate_break  = 1;

	intf->send_sol(intf, &v2_payload);
}



/*
 * suspendSelf
 *
 * Put ourself in the background
 *
 * param bRestoreTty specifies whether we will put our self back
 *       in raw mode when we resume
 */
static void
suspendSelf(int bRestoreTty)
{
	leave_raw_mode();
	kill(getpid(), SIGTSTP);

	if (bRestoreTty)
		enter_raw_mode();
}



/*
 * printSolEscapeSequences
 *
 * Send some useful documentation to the user
 */
static void
printSolEscapeSequences(struct ipmi_intf * intf)
{
	printf(
		   "%c?\n\
	Supported escape sequences:\n\
	%c.  - terminate connection\n\
	%c^Z - suspend ipmitool\n\
	%c^X - suspend ipmitool, but don't restore tty on restart\n\
	%cB  - send break\n\
	%c?  - this message\n\
	%c%c  - send the escape character by typing it twice\n\
	(Note that escapes are only recognized immediately after newline.)\n",
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char,
		   intf->ssn_params.sol_escape_char);
}



/*
 * output
 *
 * Send the specified data to stdout
 */
static void
output(struct ipmi_rs * rsp)
{
	/* Add checks to make sure it is actually SOL data, in general I see
	 * outside code mostly trying to guard against this happening, but
	 * some places fail to do so, so I do so here to make sure nothing gets
	 * through.  If non-sol data comes through here, there is probably 
	 * a packet that won't get processed somewhere else, but the alternative
	 * of outputting corrupt data is worse.  Generally I see the get device
	 * id response make it here somehow.  I assume it is a heartbeat and the
	 * other code will retry if it cares about the response and misses it.
	 */
	if (rsp &&
	    (rsp->session.authtype    == IPMI_SESSION_AUTHTYPE_RMCP_PLUS) &&
	    (rsp->session.payloadtype == IPMI_PAYLOAD_TYPE_SOL))
	{
		int i;

		for (i = 0; i < rsp->data_len; ++i)
			putc(rsp->data[i], stdout);

		fflush(stdout);
	}
}



/*
 * ipmi_sol_deactivate
 */
static int
ipmi_sol_deactivate(struct ipmi_intf * intf, int instance)
{
	struct ipmi_rs * rsp;
	struct ipmi_rq   req;
	uint8_t          data[6];

	if ((instance <= 0) || (instance > 15)) {
		lprintf(LOG_ERR, "Error: Instance must range from 1 to 15");
		return -1;
	}

	memset(&req, 0, sizeof(req));
	req.msg.netfn    = IPMI_NETFN_APP;
	req.msg.cmd      = IPMI_DEACTIVATE_PAYLOAD;
	req.msg.data_len = 6;
	req.msg.data     = data;

	memset(data, 0, sizeof(data));
	data[0] = IPMI_PAYLOAD_TYPE_SOL;  /* payload type      */
	data[1] = instance;               /* payload instance. */

	/* Lots of important data */
	data[2] = 0;
	data[3] = 0;
	data[4] = 0;
	data[5] = 0;

	rsp = intf->sendrecv(intf, &req);

	if (NULL != rsp) {
		switch (rsp->ccode) {
			case 0x00:
				return 0;
			case 0x80:
				lprintf(LOG_ERR, "Info: SOL payload already de-activated");
				break;
			case 0x81:
				lprintf(LOG_ERR, "Info: SOL payload type disabled");
				break;
			default:
				lprintf(LOG_ERR, "Error de-activating SOL payload: %s",
					val2str(rsp->ccode, completion_code_vals));
				break;
		}
	} else {
		lprintf(LOG_ERR, "Error: No response de-activating SOL payload");
	}

	return -1;
}



/*
 * processSolUserInput
 *
 * Act on user input into the SOL session.  The only reason this
 * is complicated is that we have to process escape sequences.
 *
 * return   0 on success
 *          1 if we should exit
 *        < 0 on error (BMC probably closed the session)
 */
static int
processSolUserInput(
					struct ipmi_intf * intf,
					uint8_t    * input,
					uint16_t   buffer_length)
{
	static int escape_pending = 0;
	static int last_was_cr    = 1;
	struct ipmi_v2_payload v2_payload;
	int  length               = 0;
	int  retval               = 0;
	char ch;
	int  i;

	memset(&v2_payload, 0, sizeof(v2_payload));

	/*
	 * Our first order of business is to check the input for escape
	 * sequences to act on.
	 */
	for (i = 0; i < buffer_length; ++i)
	{
		ch = input[i];

		if (escape_pending){
			escape_pending = 0;

			/*
			 * Process a possible escape sequence.
			 */
			switch (ch) {
			case '.':
				printf("%c. [terminated ipmitool]\n",
				       intf->ssn_params.sol_escape_char);
				retval = 1;
				break;

			case 'Z' - 64:
				printf("%c^Z [suspend ipmitool]\n",
				       intf->ssn_params.sol_escape_char);
				suspendSelf(1); /* Restore tty back to raw */
				continue;

			case 'X' - 64:
				printf("%c^Z [suspend ipmitool]\n",
				       intf->ssn_params.sol_escape_char);
				suspendSelf(0); /* Don't restore to raw mode */
				continue;

			case 'B':
				printf("%cB [send break]\n",
				       intf->ssn_params.sol_escape_char);
				sendBreak(intf);
				continue;

			case '?':
				printSolEscapeSequences(intf);
				continue;

			default:
				if (ch != intf->ssn_params.sol_escape_char)
					v2_payload.payload.sol_packet.data[length++] =
						intf->ssn_params.sol_escape_char;
				v2_payload.payload.sol_packet.data[length++] = ch;
			}
		}

		else
		{
			if (last_was_cr && (ch == intf->ssn_params.sol_escape_char)) {
				escape_pending = 1;
				continue;
			}

			v2_payload.payload.sol_packet.data[length++] =	ch;
		}


		/*
		 * Normal character.  Record whether it was a newline.
		 */
		last_was_cr = (ch == '\r' || ch == '\n');
	}


	/*
	 * If there is anything left to process we dispatch it to the BMC,
	 * send intf->session->sol_data.max_outbound_payload_size bytes
	 * at a time.
	 */
	if (length)
	{
		struct ipmi_rs * rsp = NULL;
		int try = 0;

		while (try < intf->ssn_params.retry) {

			v2_payload.payload.sol_packet.character_count = length;

			rsp = intf->send_sol(intf, &v2_payload);

			if (rsp)
			{
				break;
			}

			usleep(5000);
			try++;
		}

		if (! rsp)
		{
			lprintf(LOG_ERR, "Error sending SOL data: FAIL");
			retval = -1;
		}

		/* If the sequence number is set we know we have new data */
		if (retval == 0)
			if ((rsp->session.authtype == IPMI_SESSION_AUTHTYPE_RMCP_PLUS) &&
			    (rsp->session.payloadtype == IPMI_PAYLOAD_TYPE_SOL)        &&
			    (rsp->payload.sol_packet.packet_sequence_number))
				output(rsp);
	}

	return retval;
}

static int
ipmi_sol_keepalive_using_sol(struct ipmi_intf * intf)
{
	struct ipmi_v2_payload v2_payload;
	struct timeval end;

	if (_disable_keepalive)
		return 0;

	gettimeofday(&end, 0);

	if (end.tv_sec - _start_keepalive.tv_sec > SOL_KEEPALIVE_TIMEOUT) {
		memset(&v2_payload, 0, sizeof(v2_payload));
		v2_payload.payload.sol_packet.character_count = 0;
		if (intf->send_sol(intf, &v2_payload) == NULL)
			return -1;
		/* good return, reset start time */
		gettimeofday(&_start_keepalive, 0);
	}
	return 0;
}

static int
ipmi_sol_keepalive_using_getdeviceid(struct ipmi_intf * intf)
{
	struct timeval  end;

	if (_disable_keepalive)
		return 0;

	gettimeofday(&end, 0);

	if (end.tv_sec - _start_keepalive.tv_sec > SOL_KEEPALIVE_TIMEOUT) {
		if (intf->keepalive(intf) != 0)
         		return -1;
		/* good return, reset start time */
		gettimeofday(&_start_keepalive, 0);
   	}
	return 0;
}



/*
 * ipmi_sol_red_pill
 */
static int
ipmi_sol_red_pill(struct ipmi_intf * intf, int instance)
{
	char   * buffer;
	int    numRead;
	int    bShouldExit       = 0;
	int    bBmcClosedSession = 0;
	fd_set read_fds;
	struct timeval tv;
	int    retval;
	int    buffer_size = intf->session->sol_data.max_inbound_payload_size;
	int    keepAliveRet = 0;
	int    retrySol = 0;

	/* Subtract SOL header from max_inbound_payload_size */
	if (buffer_size > 4)
		buffer_size -= 4;

	buffer = (char*)malloc(buffer_size);
	if (buffer == NULL) {
		lprintf(LOG_ERR, "ipmitool: malloc failure"); 
		return -1;
	}

	/* Initialize keepalive start time */
	gettimeofday(&_start_keepalive, 0);

	enter_raw_mode();

	while (! bShouldExit)
	{
		FD_ZERO(&read_fds);
		FD_SET(0, &read_fds);
		FD_SET(intf->fd, &read_fds);

		if (!ipmi_oem_active(intf,"i82571spt"))
		{
			/* Send periodic keepalive packet */
			if(_use_sol_for_keepalive == 0)
			{
				keepAliveRet = ipmi_sol_keepalive_using_getdeviceid(intf);
			}
			else
			{
				keepAliveRet = ipmi_sol_keepalive_using_sol(intf);
			}
		
			if (keepAliveRet != 0)
			{
				/*
				 * Retrying the keep Alive before declaring a communication
				 * lost state with the IPMC. Helpful when the payload is
				 * reset and brings down the connection temporarily. Otherwise,
				 * if we send getDevice Id to check the status of IPMC during
				 * this down time when the connection is restarting, SOL will
				 * exit even though the IPMC is available and the session is open.
				 */
				if (retrySol == MAX_SOL_RETRY)
				{
					/* no response to Get Device ID keepalive message */
					bShouldExit = 1;
					continue;
				}
				else
				{
					retrySol++;
				}
			}
			else
			{
				/* if the keep Alive is successful reset retries to zero */
				retrySol = 0;
			}
		} /* !oem="i82571spt" */
		/* Wait up to half a second */
		tv.tv_sec =  0;
		tv.tv_usec = 500000;

		retval = select(intf->fd + 1, &read_fds, NULL, NULL, &tv);

		if (retval)
		{
			if (retval == -1)
			{
				/* ERROR */
				perror("select");
				return -1;
			}


			/*
			 * Process input from the user
			 */
			if (FD_ISSET(0, &read_fds))
	 		{
				memset(buffer, 0, buffer_size);
				numRead = read(fileno(stdin),
							   buffer,
							   buffer_size);

				if (numRead > 0)
				{
					int rc = processSolUserInput(intf, (uint8_t *)buffer, numRead);

					if (rc)
					{
						if (rc < 0)
							bShouldExit = bBmcClosedSession = 1;
						else
							bShouldExit = 1;
					}
				}
				else
				{
					bShouldExit = 1;
				}
			}


			/*
			 * Process input from the BMC
			 */
			else if (FD_ISSET(intf->fd, &read_fds))
			{
				struct ipmi_rs * rs =intf->recv_sol(intf);
				if (rs) {
					output(rs);
				} else {
					bShouldExit = bBmcClosedSession = 1;
				}
 			}


			/*
			 * ERROR in select
			 */
 			else
			{
				lprintf(LOG_ERR, "Error: Select returned with nothing to read");
				bShouldExit = 1;
			}
		}
	}

	leave_raw_mode();

	if (keepAliveRet != 0)
	{
		lprintf(LOG_ERR, "Error: No response to keepalive - Terminating session");
		/* attempt to clean up anyway */
		ipmi_sol_deactivate(intf, instance);
		exit(1);
	}

	if (bBmcClosedSession)
	{
		lprintf(LOG_ERR, "SOL session closed by BMC");
		exit(1);
	}
	else
		ipmi_sol_deactivate(intf, instance);

	return 0;
}




/*
 * ipmi_sol_activate
 */
static int
ipmi_sol_activate(struct ipmi_intf * intf, int looptest, int interval,
		int instance)
{
	struct ipmi_rs * rsp;
	struct ipmi_rq   req;
	struct activate_payload_rsp ap_rsp;
	uint8_t    data[6];
	uint8_t    bSolEncryption     = 1;
	uint8_t    bSolAuthentication = 1;

	/*
	 * This command is only available over RMCP+ (the lanplus
	 * interface).
	 */
	if (strncmp(intf->name, "lanplus", 7) != 0)
	{
		lprintf(LOG_ERR, "Error: This command is only available over the "
			   "lanplus interface");
		return -1;
	}

	if ((instance <= 0) || (instance > 15)) {
		lprintf(LOG_ERR, "Error: Instance must range from 1 to 15");
		return -1;
	}


	/*
	 * Setup a callback so that the lanplus processing knows what
	 * to do with packets that come unexpectedly (while waiting for
	 * an ACK, perhaps.
	 */
	intf->session->sol_data.sol_input_handler = output;


	memset(&req, 0, sizeof(req));
	req.msg.netfn    = IPMI_NETFN_APP;
	req.msg.cmd      = IPMI_ACTIVATE_PAYLOAD;
	req.msg.data_len = 6;
	req.msg.data     = data;

	data[0] = IPMI_PAYLOAD_TYPE_SOL;  /* payload type     */
	data[1] = instance;               /* payload instance */

	/* Lots of important data.  Most is default */
	data[2]  = bSolEncryption?     0x80 : 0;
	data[2] |= bSolAuthentication? 0x40 : 0;
	data[2] |= IPMI_SOL_SERIAL_ALERT_MASK_DEFERRED;

	if (ipmi_oem_active(intf, "intelplus")) {
		data[2] |= IPMI_SOL_BMC_ASSERTS_CTS_MASK_TRUE;
	} else if (ipmi_oem_active(intf, "i82571spt")) {
		/*
		 * A quote from Intel: "Engineering believes the problem
		 * lies within the Auxiliary data being sent with the
		 * 'Activate Payload' command from IPMITool.  IPMITool
		 * sends a C6h which sets some bits having to do with
		 * encryption and some behavior dealing with CTS DCD/DSR.
		 * I recommend that the customer modify this request
		 * to send 08h instead. This is what our internal utility
		 * sends and it works without issue. I will work with
		 * engineering to ensure the settings that IPMITool uses
		 * (C6h) are supported in the future.
		 */
		data[2] = 0x08;
	} else {
		data[2] |= IPMI_SOL_BMC_ASSERTS_CTS_MASK_FALSE;
	}

	data[3] = 0x00; /* reserved */
	data[4] = 0x00; /* reserved */
	data[5] = 0x00; /* reserved */

	rsp = intf->sendrecv(intf, &req);

	if (NULL != rsp) {
		switch (rsp->ccode) {
			case 0x00: 
				if (rsp->data_len == 12) {
					break;
				} else {
					lprintf(LOG_ERR, "Error: Unexpected data length (%d) received "
						   "in payload activation response",
						   rsp->data_len);
					return -1;
				}
				break;
			case 0x80:
				lprintf(LOG_ERR, "Info: SOL payload already active on another session");
				return -1;
			case 0x81:
				lprintf(LOG_ERR, "Info: SOL payload disabled");
				return -1;
			case 0x82:
				lprintf(LOG_ERR, "Info: SOL payload activation limit reached");
				return -1;
			case 0x83:
				lprintf(LOG_ERR, "Info: cannot activate SOL payload with encryption");
				return -1;
			case 0x84:
				lprintf(LOG_ERR, "Info: cannot activate SOL payload without encryption");
				return -1;
			default:
				lprintf(LOG_ERR, "Error activating SOL payload: %s",
					val2str(rsp->ccode, completion_code_vals));
				return -1;
		}
	} else {
		lprintf(LOG_ERR, "Error: No response activating SOL payload");
		return -1;
	}


	memcpy(&ap_rsp, rsp->data, sizeof(struct activate_payload_rsp));

	intf->session->sol_data.max_inbound_payload_size =
		(ap_rsp.inbound_payload_size[1] << 8) |
		ap_rsp.inbound_payload_size[0];

	intf->session->sol_data.max_outbound_payload_size =
		(ap_rsp.outbound_payload_size[1] << 8) |
		ap_rsp.outbound_payload_size[0];

	intf->session->sol_data.port =
		(ap_rsp.payload_udp_port[1] << 8) |
		ap_rsp.payload_udp_port[0];

	intf->session->timeout = 1;


	/* NOTE: the spec does allow for SOL traffic to be sent on
	 * a different port.  we do not yet support that feature. */
	if (intf->session->sol_data.port != intf->ssn_params.port)
	{
		/* try byteswapping port in case BMC sent it incorrectly */
		uint16_t portswap = BSWAP_16(intf->session->sol_data.port);

		if (portswap == intf->ssn_params.port) {
			intf->session->sol_data.port = portswap;
		}
		else {
			lprintf(LOG_ERR, "Error: BMC requests SOL session on different port");
			return -1;
		}
	}

	printf("[SOL Session operational.  Use %c? for help]\n",
	       intf->ssn_params.sol_escape_char);

	if(looptest == 1)
	{
		ipmi_sol_deactivate(intf, instance);
		usleep(interval*1000);
		return 0;
	}

	/*
	 * At this point we are good to go with our SOL session.  We
	 * need to listen to
	 * 1) STDIN for user input
	 * 2) The FD for incoming SOL packets
	 */
	if (ipmi_sol_red_pill(intf, instance))
	{
		lprintf(LOG_ERR, "Error in SOL session");
		return -1;
	}

	return 0;
}



/*
 * print_sol_usage
 */
static void
print_sol_usage(void)
{
	lprintf(LOG_NOTICE, "SOL Commands: info [<channel number>]");
	lprintf(LOG_NOTICE, "              set <parameter> <value> [channel]");
	lprintf(LOG_NOTICE, "              payload <enable|disable|status> [channel] [userid]");
	lprintf(LOG_NOTICE, "              activate [<usesolkeepalive|nokeepalive>] [instance=<number>]");
	lprintf(LOG_NOTICE, "              deactivate [instance=<number>]");
	lprintf(LOG_NOTICE, "              looptest [<loop times> [<loop interval(in ms)> [<instance>]]]");
}



/*
 * print_sol_set_usage
 */
static void
print_sol_set_usage(void)
{
	lprintf(LOG_NOTICE, "\nSOL set parameters and values: \n");
  	lprintf(LOG_NOTICE, "  set-in-progress             set-complete | "
		"set-in-progress | commit-write");
	lprintf(LOG_NOTICE, "  enabled                     true | false");
	lprintf(LOG_NOTICE, "  force-encryption            true | false");
	lprintf(LOG_NOTICE, "  force-authentication        true | false");
	lprintf(LOG_NOTICE, "  privilege-level             user | operator | admin | oem");
	lprintf(LOG_NOTICE, "  character-accumulate-level  <in 5 ms increments>");
	lprintf(LOG_NOTICE, "  character-send-threshold    N");
	lprintf(LOG_NOTICE, "  retry-count                 N");
	lprintf(LOG_NOTICE, "  retry-interval              <in 10 ms increments>");
	lprintf(LOG_NOTICE, "  non-volatile-bit-rate       "
		"serial | 9.6 | 19.2 | 38.4 | 57.6 | 115.2");
	lprintf(LOG_NOTICE, "  volatile-bit-rate           "
		"serial | 9.6 | 19.2 | 38.4 | 57.6 | 115.2");
	lprintf(LOG_NOTICE, "");
}



/* ipmi_sol_main */
int
ipmi_sol_main(struct ipmi_intf * intf, int argc, char ** argv)
{
	int retval = 0;
	if (!argc || !strncmp(argv[0], "help", 4)) {
		/* Help */
		print_sol_usage();
	} else if (!strncmp(argv[0], "info", 4)) {
		/* Info */
		uint8_t channel;
		if (argc == 1) {
			/* Ask about the current channel */
			channel = 0x0E;
		} else if (argc == 2) {
			if (is_ipmi_channel_num(argv[1], &channel) != 0) {
				return (-1);
			}
		} else {
			print_sol_usage();
			return -1;
		}
		retval = ipmi_print_sol_info(intf, channel);
	} else if (!strncmp(argv[0], "payload", 7)) {
		/* Payload enable or disable */
		uint8_t channel = 0xe;
		uint8_t userid = 1;
		int enable = -1;
		if (argc == 1 || argc > 4) {
			print_sol_usage();
			return -1;
		}
		if (argc >= 3) {
			if (is_ipmi_channel_num(argv[2], &channel) != 0) {
				return (-1);
			}
		}
		if (argc == 4) {
			if (is_ipmi_user_id(argv[3], &userid) != 0) {
				return (-1);
			}
		}
		if (!strncmp(argv[1], "enable", 6)) {
			enable = 1;
		} else if (!strncmp(argv[1], "disable", 7)) {
			enable = 0;
		} else if (!strncmp(argv[1], "status", 6)) {
			return ipmi_sol_payload_access_status(intf, channel, userid);
		} else {
			print_sol_usage();
			return -1;
		}
		retval = ipmi_sol_payload_access(intf, channel, userid, enable);
	} else if (!strncmp(argv[0], "set", 3)) {
		/* Set a parameter value */
		uint8_t channel = 0xe;
		uint8_t guard = 1;
		if (argc == 3) {
			channel = 0xe;
		} else if (argc == 4) {
			if (!strncmp(argv[3], "noguard", 7)) {
				guard = 0;
			} else {
				if (is_ipmi_channel_num(argv[3], &channel) != 0) {
					return (-1);
				}
			}
		} else if (argc == 5) {
			if (is_ipmi_channel_num(argv[3], &channel) != 0) {
				return (-1);
			}
			if (!strncmp(argv[4], "noguard", 7)) {
				guard = 0;
			}
		} else {
			print_sol_set_usage();
			return -1;
		}
		retval = ipmi_sol_set_param(intf, channel, argv[1], argv[2], guard);
	} else if (!strncmp(argv[0], "activate", 8)) {
		/* Activate */
		int i;
		uint8_t instance = 1;
		for (i = 1; i < argc; i++) {
			if (!strncmp(argv[i], "usesolkeepalive", 15)) {
				_use_sol_for_keepalive = 1;
			} else if (!strncmp(argv[i], "nokeepalive", 11)) {
				_disable_keepalive = 1;
			} else if (!strncmp(argv[i], "instance=", 9)) {
				if (str2uchar(argv[i] + 9, &instance) != 0) {
					lprintf(LOG_ERR, "Given instance '%s' is invalid.", argv[i] + 9);
					print_sol_usage();
					return -1;
				}
			} else {
				print_sol_usage();
				return -1;
			}
		}
		retval = ipmi_sol_activate(intf, 0, 0, instance);
	} else if (!strncmp(argv[0], "deactivate", 10)) {
		/* Dectivate */
		int i;
		uint8_t instance = 1;
		for (i = 1; i < argc; i++) {
			if (!strncmp(argv[i], "instance=", 9)) {
				if (str2uchar(argv[i] + 9, &instance) != 0) {
					lprintf(LOG_ERR,
							"Given instance '%s' is invalid.",
							argv[i] + 9);
					print_sol_usage();
					return -1;
				}
			} else {
				print_sol_usage();
				return -1;
			}
		}
		retval = ipmi_sol_deactivate(intf, instance);
	} else if (!strncmp(argv[0], "looptest", 8)) {
		/* SOL loop test: Activate and then Dectivate */
		int cnt = 200;
		int interval = 100; /* Unit is: ms */
		uint8_t instance = 1;
		if (argc > 4) {
			print_sol_usage();
			return -1;
		}
		if (argc != 1) {
			/* at least 2 */
			if (str2int(argv[1], &cnt) != 0) {
				lprintf(LOG_ERR, "Given cnt '%s' is invalid.",
						argv[1]);
				return (-1);
			}
			if (cnt <= 0) {
				cnt = 200;
			}
		}
		if (argc >= 3) {
			if (str2int(argv[2], &interval) != 0) {
				lprintf(LOG_ERR, "Given interval '%s' is invalid.",
						argv[2]);
				return (-1);
			}
			if (interval < 0) {
				interval = 0;
			}
		}
		if (argc >= 4) {
			if (str2uchar(argv[3], &instance) != 0) {
				lprintf(LOG_ERR, "Given instance '%s' is invalid.",
						argv[3]);
				print_sol_usage();
				return -1;
			}
		}

		while (cnt > 0) {
			printf("remain loop test counter: %d\n", cnt);
			retval = ipmi_sol_activate(intf, 1, interval, instance);
			if (retval) {
				printf("SOL looptest failed: %d\n",
						retval);
				break;
			}
			cnt -= 1;
		}
	} else {
		print_sol_usage();
		retval = -1;
	}
	return retval;
}