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
|
/*
* 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.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <ipmitool/bswap.h>
#include <ipmitool/helper.h>
#include <ipmitool/ipmi.h>
#include <ipmitool/log.h>
#include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi_strings.h>
#include <ipmitool/ipmi_chassis.h>
#include <ipmitool/ipmi_time.h>
#define CHASSIS_BOOT_MBOX_IANA_SZ 3
#define CHASSIS_BOOT_MBOX_BLOCK_SZ 16
#define CHASSIS_BOOT_MBOX_BLOCK0_SZ \
(CHASSIS_BOOT_MBOX_BLOCK_SZ - CHASSIS_BOOT_MBOX_IANA_SZ)
#define CHASSIS_BOOT_MBOX_MAX_BLOCK 0xFF
#define CHASSIS_BOOT_MBOX_MAX_BLOCKS (CHASSIS_BOOT_MBOX_MAX_BLOCK + 1)
typedef struct {
uint8_t iana[CHASSIS_BOOT_MBOX_IANA_SZ];
uint8_t data[CHASSIS_BOOT_MBOX_BLOCK0_SZ];
} mbox_b0_data_t;
typedef struct {
uint8_t block;
union {
uint8_t data[CHASSIS_BOOT_MBOX_BLOCK_SZ];
mbox_b0_data_t b0;
};
} mbox_t;
extern int verbose;
static const struct valstr get_bootparam_cc_vals[] = {
{ 0x80, "Unsupported parameter" },
{ 0x00, NULL }
};
static const struct valstr set_bootparam_cc_vals[] = {
{ 0x80, "Unsupported parameter" },
{ 0x81, "Attempt to set 'in progress' while not in 'complete' state" },
{ 0x82, "Parameter is read-only" },
{ 0x00, NULL }
};
int
ipmi_chassis_power_status(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x1;
req.msg.data_len = 0;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Unable to get Chassis Power Status");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Get Chassis Power Status failed: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
return rsp->data[0] & 1;
}
static int
ipmi_chassis_print_power_status(struct ipmi_intf * intf)
{
int ps = ipmi_chassis_power_status(intf);
if (ps < 0)
return -1;
printf("Chassis Power is %s\n", ps ? "on" : "off");
return 0;
}
int
ipmi_chassis_power_control(struct ipmi_intf * intf, uint8_t ctl)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x2;
req.msg.data = &ctl;
req.msg.data_len = 1;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Unable to set Chassis Power Control to %s",
val2str(ctl, ipmi_chassis_power_control_vals));
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Set Chassis Power Control to %s failed: %s",
val2str(ctl, ipmi_chassis_power_control_vals),
val2str(rsp->ccode, completion_code_vals));
return -1;
}
printf("Chassis Power Control: %s\n",
val2str(ctl, ipmi_chassis_power_control_vals));
return 0;
}
static int
ipmi_chassis_identify(struct ipmi_intf * intf, char * arg)
{
struct ipmi_rq req;
struct ipmi_rs * rsp;
int rc = (-3);
struct {
uint8_t interval;
uint8_t force_on;
} identify_data = { .interval = 0, .force_on = 0 };
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x4;
if (arg) {
if (strncmp(arg, "force", 5) == 0) {
identify_data.force_on = 1;
} else {
if ( (rc = str2uchar(arg, &identify_data.interval)) != 0) {
if (rc == (-2)) {
lprintf(LOG_ERR, "Invalid interval given.");
} else {
lprintf(LOG_ERR, "Given interval is too big.");
}
return (-1);
}
}
req.msg.data = (uint8_t *)&identify_data;
/* The Force Identify On byte is optional and not
* supported by all devices-- if force is not specified,
* we pass only one data byte; if specified, we pass two
* data bytes and check for an error completion code
*/
req.msg.data_len = (identify_data.force_on) ? 2 : 1;
}
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Unable to set Chassis Identify");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Set Chassis Identify failed: %s",
val2str(rsp->ccode, completion_code_vals));
if (identify_data.force_on != 0) {
/* Intel SE7501WV2 F/W 1.2 returns CC 0xC7, but
* the IPMI v1.5 spec does not standardize a CC
* if unsupported, so we warn
*/
lprintf(LOG_WARNING, "Chassis may not support Force Identify On\n");
}
return -1;
}
printf("Chassis identify interval: ");
if (!arg) {
printf("default (15 seconds)\n");
} else {
if (identify_data.force_on != 0) {
printf("indefinite\n");
} else {
if (identify_data.interval == 0)
printf("off\n");
else
printf("%i seconds\n", identify_data.interval);
}
}
return 0;
}
static int
ipmi_chassis_poh(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
uint8_t mins_per_count;
uint32_t count;
float minutes;
uint32_t days, hours;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0xf;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Unable to get Chassis Power-On-Hours");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Get Chassis Power-On-Hours failed: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
mins_per_count = rsp->data[0];
memcpy(&count, rsp->data+1, 4);
#if WORDS_BIGENDIAN
count = BSWAP_32(count);
#endif
minutes = (float)count * mins_per_count;
days = minutes / 1440;
minutes -= (float)days * 1440;
hours = minutes / 60;
minutes -= hours * 60;
if (mins_per_count < 60) {
printf("POH Counter : %i days, %i hours, %li minutes\n",
days, hours, (long)minutes);
} else {
printf("POH Counter : %i days, %i hours\n", days, hours);
}
return 0;
}
static int
ipmi_chassis_restart_cause(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x7;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Unable to get Chassis Restart Cause");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Get Chassis Restart Cause failed: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
printf("System restart cause: ");
switch (rsp->data[0] & 0xf) {
case 0:
printf("unknown\n");
break;
case 1:
printf("chassis power control command\n");
break;
case 2:
printf("reset via pushbutton\n");
break;
case 3:
printf("power-up via pushbutton\n");
break;
case 4:
printf("watchdog expired\n");
break;
case 5:
printf("OEM\n");
break;
case 6:
printf("power-up due to always-restore power policy\n");
break;
case 7:
printf("power-up due to restore-previous power policy\n");
break;
case 8:
printf("reset via PEF\n");
break;
case 9:
printf("power-cycle via PEF\n");
break;
default:
printf("invalid\n");
}
return 0;
}
int
ipmi_chassis_status(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x1;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Error sending Chassis Status command");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Error sending Chassis Status command: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
/* byte 1 */
printf("System Power : %s\n", (rsp->data[0] & 0x1) ? "on" : "off");
printf("Power Overload : %s\n", (rsp->data[0] & 0x2) ? "true" : "false");
printf("Power Interlock : %s\n", (rsp->data[0] & 0x4) ? "active" : "inactive");
printf("Main Power Fault : %s\n", (rsp->data[0] & 0x8) ? "true" : "false");
printf("Power Control Fault : %s\n", (rsp->data[0] & 0x10) ? "true" : "false");
printf("Power Restore Policy : ");
switch ((rsp->data[0] & 0x60) >> 5) {
case 0x0:
printf("always-off\n");
break;
case 0x1:
printf("previous\n");
break;
case 0x2:
printf("always-on\n");
break;
case 0x3:
default:
printf("unknown\n");
}
/* byte 2 */
printf("Last Power Event : ");
if (rsp->data[1] & 0x1)
printf("ac-failed ");
if (rsp->data[1] & 0x2)
printf("overload ");
if (rsp->data[1] & 0x4)
printf("interlock ");
if (rsp->data[1] & 0x8)
printf("fault ");
if (rsp->data[1] & 0x10)
printf("command");
printf("\n");
/* byte 3 */
printf("Chassis Intrusion : %s\n", (rsp->data[2] & 0x1) ? "active" : "inactive");
printf("Front-Panel Lockout : %s\n", (rsp->data[2] & 0x2) ? "active" : "inactive");
printf("Drive Fault : %s\n", (rsp->data[2] & 0x4) ? "true" : "false");
printf("Cooling/Fan Fault : %s\n", (rsp->data[2] & 0x8) ? "true" : "false");
if (rsp->data_len > 3) {
/* optional byte 4 */
if (rsp->data[3] == 0) {
printf("Front Panel Control : none\n");
} else {
printf("Sleep Button Disable : %s\n", (rsp->data[3] & 0x80) ? "allowed" : "not allowed");
printf("Diag Button Disable : %s\n", (rsp->data[3] & 0x40) ? "allowed" : "not allowed");
printf("Reset Button Disable : %s\n", (rsp->data[3] & 0x20) ? "allowed" : "not allowed");
printf("Power Button Disable : %s\n", (rsp->data[3] & 0x10) ? "allowed" : "not allowed");
printf("Sleep Button Disabled: %s\n", (rsp->data[3] & 0x08) ? "true" : "false");
printf("Diag Button Disabled : %s\n", (rsp->data[3] & 0x04) ? "true" : "false");
printf("Reset Button Disabled: %s\n", (rsp->data[3] & 0x02) ? "true" : "false");
printf("Power Button Disabled: %s\n", (rsp->data[3] & 0x01) ? "true" : "false");
}
}
return 0;
}
static int
ipmi_chassis_selftest(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_APP;
req.msg.cmd = 0x4;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Error sending Get Self Test command");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Error sending Get Self Test command: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
printf("Self Test Results : ");
switch (rsp->data[0]) {
case 0x55:
printf("passed\n");
break;
case 0x56:
printf("not implemented\n");
break;
case 0x57:
{
int i;
const struct valstr broken_dev_vals[] = {
{ 0, "firmware corrupted" },
{ 1, "boot block corrupted" },
{ 2, "FRU Internal Use Area corrupted" },
{ 3, "SDR Repository empty" },
{ 4, "IPMB not responding" },
{ 5, "cannot access BMC FRU" },
{ 6, "cannot access SDR Repository" },
{ 7, "cannot access SEL Device" },
{ 0xff, NULL },
};
printf("device error\n");
for (i=0; i<8; i++) {
if (rsp->data[1] & (1<<i)) {
printf(" [%s]\n",
val2str(i, broken_dev_vals));
}
}
}
break;
case 0x58:
printf("Fatal hardware error: %02xh\n", rsp->data[1]);
break;
default:
printf("Device-specific failure %02xh:%02xh\n",
rsp->data[0], rsp->data[1]);
break;
}
return 0;
}
static int
ipmi_chassis_set_bootparam(struct ipmi_intf * intf,
uint8_t param, void *data, int len)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
struct {
uint8_t param;
uint8_t data[];
} *msg_data;
int rc = -1;
size_t msgsize = 1 + len; /* Single-byte parameter plus the data */
static const uint8_t BOOTPARAM_MASK = 0x7F;
msg_data = malloc(msgsize);
if (!msg_data) {
goto out;
}
memset(msg_data, 0, msgsize);
msg_data->param = param & BOOTPARAM_MASK;
memcpy(msg_data->data, data, len);
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x8;
req.msg.data = (uint8_t *)msg_data;
req.msg.data_len = msgsize;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Error setting Chassis Boot Parameter %d", param);
return -1;
}
rc = rsp->ccode;
if (rc) {
if (param != 0) {
lprintf(LOG_ERR,
"Set Chassis Boot Parameter %d failed: %s",
param,
specific_val2str(rsp->ccode,
set_bootparam_cc_vals,
completion_code_vals));
}
goto out;
}
lprintf(LOG_DEBUG, "Chassis Set Boot Parameter %d to %s", param, buf2str(data, len));
out:
free_n(&msg_data);
return rc;
}
/* Flags to ipmi_chassis_get_bootparam() */
typedef enum {
PARAM_NO_GENERIC_INFO, /* Do not print generic boot parameter info */
PARAM_NO_DATA_DUMP, /* Do not dump parameter data */
PARAM_NO_RANGE_ERROR, /* Do not report out of range info to user */
PARAM_SPECIFIC /* Parameter-specific flags start with this */
} chassis_bootparam_flags_t;
/* Flags to ipmi_chassis_get_bootparam() for Boot Mailbox parameter (7) */
typedef enum {
MBOX_PARSE_USE_TEXT = PARAM_SPECIFIC, /* Use text output vs. hex */
MBOX_PARSE_ALLBLOCKS /* Parse all blocks, not just one */
} chassis_bootmbox_parse_t;
#define BP_FLAG(x) (1 << (x))
static
void
chassis_bootmailbox_parse(void *buf, size_t len, int flags)
{
void *blockdata;
size_t datalen;
bool use_text = flags & BP_FLAG(MBOX_PARSE_USE_TEXT);
bool all_blocks = flags & BP_FLAG(MBOX_PARSE_ALLBLOCKS);
mbox_t *mbox;
if (!buf || !len) {
return;
}
mbox = buf;
blockdata = mbox->data;
datalen = len - sizeof(mbox->block);
if (!all_blocks) {
/* Print block selector only if a single block is printed */
printf(" Selector : %d\n", mbox->block);
}
if (!mbox->block) {
uint32_t iana = ipmi24toh(mbox->b0.iana);
/* For block zero print the IANA Private Enterprise Number */
printf(" IANA PEN : %" PRIu32 " [%s]\n",
iana,
val2str(iana, ipmi_oem_info));
blockdata = mbox->b0.data;
datalen -= sizeof(mbox->b0.iana);
}
printf(" Block ");
if (all_blocks) {
printf("%3" PRIu8 " Data : ", mbox->block);
}
else {
printf("Data : ");
}
if (use_text) {
/* Ensure the data string is null-terminated */
unsigned char text[CHASSIS_BOOT_MBOX_BLOCK_SZ + 1] = { 0 };
memcpy(text, blockdata, datalen);
printf("'%s'\n", text);
}
else {
printf("%s\n", buf2str(blockdata, datalen));
}
}
static int
ipmi_chassis_get_bootparam(struct ipmi_intf * intf,
int argc, char *argv[], int flags)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
uint8_t msg_data[3];
uint8_t param_id = 0;
bool skip_generic = flags & BP_FLAG(PARAM_NO_GENERIC_INFO);
bool skip_data = flags & BP_FLAG(PARAM_NO_DATA_DUMP);
bool skip_range = flags & BP_FLAG(PARAM_NO_RANGE_ERROR);
int rc = -1;
if (argc < 1 || !argv[0]) {
goto out;
}
if (str2uchar(argv[0], ¶m_id)) {
lprintf(LOG_ERR,
"Invalid parameter '%s' given instead of bootparam.",
argv[0]);
goto out;
}
--argc;
++argv;
memset(msg_data, 0, 3);
msg_data[0] = param_id & 0x7f;
if (argc) {
if (str2uchar(argv[0], &msg_data[1])) {
lprintf(LOG_ERR,
"Invalid argument '%s' given to"
" bootparam %" PRIu8,
argv[0], msg_data[1]);
goto out;
}
}
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x9;
req.msg.data = msg_data;
req.msg.data_len = 3;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR,
"Error Getting Chassis Boot Parameter %" PRIu8,
msg_data[0]);
return -1;
}
if (IPMI_CC_PARAM_OUT_OF_RANGE == rsp->ccode && skip_range) {
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR,
"Get Chassis Boot Parameter %" PRIu8 " failed: %s",
msg_data[0],
specific_val2str(rsp->ccode,
get_bootparam_cc_vals,
completion_code_vals));
return -1;
}
if (verbose > 2)
printbuf(rsp->data, rsp->data_len, "Boot Option");
param_id = 0;
param_id = (rsp->data[1] & 0x7f);
if (!skip_generic) {
printf("Boot parameter version: %d\n", rsp->data[0]);
printf("Boot parameter %d is %s\n", rsp->data[1] & 0x7f,
(rsp->data[1] & 0x80)
? "invalid/locked"
: "valid/unlocked");
if (!skip_data) {
printf("Boot parameter data: %s\n",
buf2str(rsp->data+2, rsp->data_len - 2));
}
}
switch(param_id)
{
case 0:
{
printf(" Set In Progress : ");
switch((rsp->data[2]) &0x03)
{
case 0: printf("set complete\n"); break;
case 1: printf("set in progress\n"); break;
case 2: printf("commit write\n"); break;
default: printf("error, reserved bit\n"); break;
}
}
break;
case 1:
{
printf(" Service Partition Selector : ");
if((rsp->data[2]) == 0)
{
printf("unspecified\n");
}
else
{
printf("%d\n",(rsp->data[2]));
}
}
break;
case 2:
{
printf( " Service Partition Scan :\n");
if((rsp->data[2]&0x03) != 0)
{
if((rsp->data[2]&0x01) == 0x01)
printf(" - Request BIOS to scan\n");
if((rsp->data[2]&0x02) == 0x02)
printf(" - Service Partition Discovered\n");
}
else
{
printf(" No flag set\n");
}
}
break;
case 3:
{
printf( " BMC boot flag valid bit clearing :\n");
if((rsp->data[2]&0x1f) != 0)
{
if((rsp->data[2]&0x10) == 0x10)
printf(" - Don't clear valid bit on reset/power cycle cause by PEF\n");
if((rsp->data[2]&0x08) == 0x08)
printf(" - Don't automatically clear boot flag valid bit on timeout\n");
if((rsp->data[2]&0x04) == 0x04)
printf(" - Don't clear valid bit on reset/power cycle cause by watchdog\n");
if((rsp->data[2]&0x02) == 0x02)
printf(" - Don't clear valid bit on push button reset // soft reset\n");
if((rsp->data[2]&0x01) == 0x01)
printf(" - Don't clear valid bit on power up via power push button or wake event\n");
}
else
{
printf(" No flag set\n");
}
}
break;
case 4:
{
printf( " Boot Info Acknowledge :\n");
if((rsp->data[3]&0x1f) != 0)
{
if((rsp->data[3]&0x10) == 0x10)
printf(" - OEM has handled boot info\n");
if((rsp->data[3]&0x08) == 0x08)
printf(" - SMS has handled boot info\n");
if((rsp->data[3]&0x04) == 0x04)
printf(" - OS // service partition has handled boot info\n");
if((rsp->data[3]&0x02) == 0x02)
printf(" - OS Loader has handled boot info\n");
if((rsp->data[3]&0x01) == 0x01)
printf(" - BIOS/POST has handled boot info\n");
}
else
{
printf(" No flag set\n");
}
}
break;
case 5:
{
printf( " Boot Flags :\n");
if((rsp->data[2]&0x80) == 0x80)
printf(" - Boot Flag Valid\n");
else
printf(" - Boot Flag Invalid\n");
if((rsp->data[2]&0x40) == 0x40)
printf(" - Options apply to all future boots\n");
else
printf(" - Options apply to only next boot\n");
if((rsp->data[2]&0x20) == 0x20)
printf(" - BIOS EFI boot \n");
else
printf(" - BIOS PC Compatible (legacy) boot \n");
if((rsp->data[3]&0x80) == 0x80)
printf(" - CMOS Clear\n");
if((rsp->data[3]&0x40) == 0x40)
printf(" - Lock Keyboard\n");
printf(" - Boot Device Selector : ");
switch( ((rsp->data[3]>>2)&0x0f))
{
case 0: printf("No override\n"); break;
case 1: printf("Force PXE\n"); break;
case 2: printf("Force Boot from default Hard-Drive\n"); break;
case 3: printf("Force Boot from default Hard-Drive, request Safe-Mode\n"); break;
case 4: printf("Force Boot from Diagnostic Partition\n"); break;
case 5: printf("Force Boot from CD/DVD\n"); break;
case 6: printf("Force Boot into BIOS Setup\n"); break;
case 15: printf("Force Boot from Floppy/primary removable media\n"); break;
default: printf("Flag error\n"); break;
}
if((rsp->data[3]&0x02) == 0x02)
printf(" - Screen blank\n");
if((rsp->data[3]&0x01) == 0x01)
printf(" - Lock out Reset buttons\n");
if((rsp->data[4]&0x80) == 0x80)
printf(" - Lock out (power off/sleep request) vi Power Button\n");
printf(" - Console Redirection control : ");
switch( ((rsp->data[4]>>5)&0x03))
{
case 0: printf("System Default\n"); break;
case 1: printf("Request Quiet Display\n"); break;
case 2: printf("Request Verbose Display\n"); break;
default: printf("Flag error\n"); break;
}
if((rsp->data[4]&0x10) == 0x10)
printf(" - Force progress event traps\n");
if((rsp->data[4]&0x08) == 0x08)
printf(" - User password bypass\n");
if((rsp->data[4]&0x04) == 0x04)
printf(" - Lock Out Sleep Button\n");
if((rsp->data[4]&0x02) == 0x02)
printf(" - Lock Out Sleep Button\n");
printf(" - BIOS verbosity : ");
switch( ((rsp->data[4]>>0)&0x03))
{
case 0: printf("Console redirection occurs per BIOS configuration setting (default)\n"); break;
case 1: printf("Suppress (skip) console redirection if enabled\n"); break;
case 2: printf("Request console redirection be enabled\n"); break;
default: printf("Flag error\n"); break;
}
if((rsp->data[5]&0x08) == 0x08)
printf(" - BIOS Shared Mode Override\n");
printf(" - BIOS Mux Control Override : ");
switch( ((rsp->data[5]>>0)&0x07))
{
case 0: printf("BIOS uses recommended setting of the mux at the end of POST\n"); break;
case 1: printf("Requests BIOS to force mux to BMC at conclusion of POST/start of OS boot\n"); break;
case 2: printf("Requests BIOS to force mux to system at conclusion of POST/start of OS boot\n"); break;
default: printf("Flag error\n"); break;
}
}
break;
case 6:
{
unsigned long session_id;
uint32_t timestamp;
session_id = ((unsigned long) rsp->data[3]);
session_id |= (((unsigned long) rsp->data[4])<<8);
session_id |= (((unsigned long) rsp->data[5])<<16);
session_id |= (((unsigned long) rsp->data[6])<<24);
timestamp = ipmi32toh(&rsp->data[7]);
printf(" Boot Initiator Info :\n");
printf(" Channel Number : %d\n", (rsp->data[2] & 0x0f));
printf(" Session Id : %08lXh\n",session_id);
printf(" Timestamp : %s\n", ipmi_timestamp_numeric(timestamp));
}
break;
case 7:
chassis_bootmailbox_parse(rsp->data + 2,
rsp->data_len - 2,
flags);
break;
default:
printf(" Unsupported parameter %" PRIu8 "\n", param_id);
break;
}
rc = IPMI_CC_OK;
out:
return rc;
}
static int
get_bootparam_options(char *optstring,
unsigned char *set_flag, unsigned char *clr_flag)
{
char *token;
char *saveptr = NULL;
int optionError = 0;
*set_flag = 0;
*clr_flag = 0;
static struct {
char *name;
unsigned char value;
char *desc;
} options[] = {
{"PEF", 0x10,
"Clear valid bit on reset/power cycle cause by PEF"},
{"timeout", 0x08,
"Automatically clear boot flag valid bit on timeout"},
{"watchdog", 0x04,
"Clear valid bit on reset/power cycle cause by watchdog"},
{"reset", 0x02,
"Clear valid bit on push button reset/soft reset"},
{"power", 0x01,
"Clear valid bit on power up via power push button or wake event"},
{NULL} /* End marker */
}, *op;
if (strncmp(optstring, "options=", 8) != 0) {
lprintf(LOG_ERR, "No options= keyword found \"%s\"", optstring);
return -1;
}
token = strtok_r(optstring + 8, ",", &saveptr);
while (token) {
int setbit = 0;
if (strcmp(token, "help") == 0) {
optionError = 1;
break;
}
if (strncmp(token, "no-", 3) == 0) {
setbit = 1;
token += 3;
}
for (op = options; op->name; ++op) {
if (strncmp(token, op->name, strlen(op->name)) == 0) {
if (setbit) {
*set_flag |= op->value;
} else {
*clr_flag |= op->value;
}
break;
}
}
if (!op->name) {
/* Option not found */
optionError = 1;
if (setbit) {
token -=3;
}
lprintf(LOG_ERR, "Invalid option: %s", token);
}
token = strtok_r(NULL, ",", &saveptr);
}
if (optionError) {
lprintf(LOG_NOTICE, " Legal options are:");
lprintf(LOG_NOTICE, " %-8s: print this message", "help");
for (op = options; op->name; ++op) {
lprintf(LOG_NOTICE, " %-8s: %s", op->name, op->desc);
}
lprintf(LOG_NOTICE, " Any Option may be prepended with no-"
" to invert sense of operation\n");
return (-1);
}
return (0);
}
static int
ipmi_chassis_get_bootvalid(struct ipmi_intf * intf)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
uint8_t msg_data[3];
uint8_t param_id = IPMI_CHASSIS_BOOTPARAM_FLAG_VALID;
memset(msg_data, 0, 3);
msg_data[0] = param_id & 0x7f;
msg_data[1] = 0;
msg_data[2] = 0;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x9;
req.msg.data = msg_data;
req.msg.data_len = 3;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR,
"Error Getting Chassis Boot Parameter %d", param_id);
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Get Chassis Boot Parameter %d failed: %s",
param_id,
specific_val2str(rsp->ccode,
get_bootparam_cc_vals,
completion_code_vals));
return -1;
}
if (verbose > 2)
printbuf(rsp->data, rsp->data_len, "Boot Option");
return(rsp->data[2]);
}
typedef enum {
SET_COMPLETE,
SET_IN_PROGRESS,
COMMIT_WRITE,
RESERVED
} progress_t;
static
void
chassis_bootparam_set_in_progress(struct ipmi_intf *intf, progress_t progress)
{
/*
* By default try to set/clear set-in-progress parameter before/after
* changing any boot parameters. If setting fails, the code will set
* this flag to false and stop trying to fiddle with it for future
* requests.
*/
static bool use_progress = true;
uint8_t flag = progress;
int rc;
if (!use_progress) {
return;
}
rc = ipmi_chassis_set_bootparam(intf,
IPMI_CHASSIS_BOOTPARAM_SET_IN_PROGRESS,
&flag, 1);
/*
* Only disable future checks if set in progress status setting failed.
* Setting of other statuses may fail legitimately.
*/
if (rc && SET_IN_PROGRESS == progress) {
use_progress = false;
}
}
typedef enum {
BIOS_POST_ACK = 1 << 0,
OS_LOADER_ACK = 1 << 1,
OS_SERVICE_PARTITION_ACK = 1 << 2,
SMS_ACK = 1 << 3,
OEM_ACK = 1 << 4,
RESERVED_ACK_MASK = 7 << 5
} bootinfo_ack_t;
static
int
chassis_bootparam_clear_ack(struct ipmi_intf *intf, bootinfo_ack_t flag)
{
uint8_t flags[2] = { flag & ~RESERVED_ACK_MASK,
flag & ~RESERVED_ACK_MASK };
return ipmi_chassis_set_bootparam(intf,
IPMI_CHASSIS_BOOTPARAM_INFO_ACK,
flags, 2);
}
static int
ipmi_chassis_set_bootvalid(struct ipmi_intf *intf, uint8_t set_flag, uint8_t clr_flag)
{
int bootvalid;
uint8_t flags[2];
int rc;
chassis_bootparam_set_in_progress(intf, SET_IN_PROGRESS);
rc = chassis_bootparam_clear_ack(intf, BIOS_POST_ACK);
if (rc) {
goto out;
}
bootvalid = ipmi_chassis_get_bootvalid(intf);
if (bootvalid < 0) {
lprintf(LOG_ERR, "Failed to read boot valid flag");
rc = bootvalid;
goto out;
}
flags[0] = (bootvalid & ~clr_flag) | set_flag;
rc = ipmi_chassis_set_bootparam(intf,
IPMI_CHASSIS_BOOTPARAM_FLAG_VALID,
flags, 1);
if (IPMI_CC_OK == rc) {
chassis_bootparam_set_in_progress(intf, COMMIT_WRITE);
}
out:
chassis_bootparam_set_in_progress(intf, SET_COMPLETE);
return rc;
}
static int
ipmi_chassis_set_bootdev(struct ipmi_intf * intf, char * arg, uint8_t *iflags)
{
uint8_t flags[5];
int rc;
chassis_bootparam_set_in_progress(intf, SET_IN_PROGRESS);
rc = chassis_bootparam_clear_ack(intf, BIOS_POST_ACK);
if (rc < 0) {
goto out;
}
if (!iflags)
memset(flags, 0, sizeof(flags));
else
memcpy(flags, iflags, sizeof (flags));
if (!arg)
flags[1] |= 0x00;
else if (strncmp(arg, "none", 4) == 0)
flags[1] |= 0x00;
else if (strncmp(arg, "pxe", 3) == 0 ||
strncmp(arg, "force_pxe", 9) == 0)
flags[1] |= 0x04;
else if (strncmp(arg, "disk", 4) == 0 ||
strncmp(arg, "force_disk", 10) == 0)
flags[1] |= 0x08;
else if (strncmp(arg, "safe", 4) == 0 ||
strncmp(arg, "force_safe", 10) == 0)
flags[1] |= 0x0c;
else if (strncmp(arg, "diag", 4) == 0 ||
strncmp(arg, "force_diag", 10) == 0)
flags[1] |= 0x10;
else if (strncmp(arg, "cdrom", 5) == 0 ||
strncmp(arg, "force_cdrom", 11) == 0)
flags[1] |= 0x14;
else if (strncmp(arg, "floppy", 6) == 0 ||
strncmp(arg, "force_floppy", 12) == 0)
flags[1] |= 0x3c;
else if (strncmp(arg, "bios", 4) == 0 ||
strncmp(arg, "force_bios", 10) == 0)
flags[1] |= 0x18;
else {
lprintf(LOG_ERR, "Invalid argument: %s", arg);
rc = -1;
goto out;
}
/* set flag valid bit */
flags[0] |= 0x80;
rc = ipmi_chassis_set_bootparam(intf,
IPMI_CHASSIS_BOOTPARAM_BOOT_FLAGS,
flags, 5);
if (IPMI_CC_OK == rc) {
chassis_bootparam_set_in_progress(intf, COMMIT_WRITE);
printf("Set Boot Device to %s\n", arg);
}
out:
chassis_bootparam_set_in_progress(intf, SET_COMPLETE);
return rc;
}
static void chassis_bootmailbox_help()
{
lprintf(LOG_NOTICE,
"bootmbox get [text] [block <block>]\n"
" Read the entire Boot Initiator Mailbox or the specified <block>.\n"
" If 'text' option is specified, the data is output as plain text, otherwise\n"
" hex dump mode is used.\n"
"\n"
"bootmbox set text [block <block>] <IANA_PEN> \"<data_string>\"\n"
"bootmbox set [block <block>] <IANA_PEN> <data_byte> [<data_byte> ...]\n"
" Write the specified <block> or the entire Boot Initiator Mailbox.\n"
" It is required to specify a decimal IANA Enterprise Number recognized\n"
" by the boot initiator on the target system. Refer to your target system\n"
" manufacturer for details. The rest of the arguments are either separate\n"
" data byte values separated by spaces, or a single text string argument.\n"
"\n"
" When single block write is requested, the total length of <data> may not\n"
" exceed 13 bytes for block 0, or 16 bytes otherwise.\n"
"\n"
"bootmbox help\n"
" Show this help.");
}
static
int
chassis_set_bootmailbox(struct ipmi_intf *intf, int16_t block, bool use_text,
int argc, char *argv[])
{
int rc = -1;
int32_t iana = 0;
size_t blocks = 0;
size_t datasize = 0;
off_t string_offset = 0;
lprintf(LOG_INFO, "Writing Boot Mailbox...");
if (argc < 1 || str2int(argv[0], &iana)) {
lprintf(LOG_ERR,
"No valid IANA PEN specified!\n");
chassis_bootmailbox_help();
goto out;
}
++argv;
--argc;
if (argc < 1) {
lprintf(LOG_ERR,
"No data provided!\n");
chassis_bootmailbox_help();
goto out;
}
/*
* Initialize the data size. For text mode it is just the
* single argument string length plus one byte for \0 termination.
* For byte mode the length is the number of byte arguments without
* any additional termination.
*/
if (!use_text) {
datasize = argc;
}
else {
datasize = strlen(argv[0]) + 1; /* Include the terminator */
}
lprintf(LOG_INFO, "Data size: %u", datasize);
/* Decide how many blocks we will be writing */
if (block >= 0) {
blocks = 1;
}
else {
/*
* We need to write all data, so calculate the data
* size in blocks and set the starting block to zero.
*/
blocks = datasize;
blocks += CHASSIS_BOOT_MBOX_BLOCK_SZ - 1;
blocks /= CHASSIS_BOOT_MBOX_BLOCK_SZ;
block = 0;
}
lprintf(LOG_INFO, "Blocks to write: %d", blocks);
if (blocks > CHASSIS_BOOT_MBOX_MAX_BLOCKS) {
lprintf(LOG_ERR,
"Data size %zu exceeds maximum (%d)",
datasize,
(CHASSIS_BOOT_MBOX_BLOCK_SZ
* CHASSIS_BOOT_MBOX_MAX_BLOCKS)
- CHASSIS_BOOT_MBOX_IANA_SZ);
goto out;
}
/* Indicate that we're touching the boot parameters */
chassis_bootparam_set_in_progress(intf, SET_IN_PROGRESS);
for (size_t bindex = 0;
datasize > 0 && bindex < blocks;
++bindex, ++block)
{
/* The request data structure */
mbox_t mbox = { .block = block, {{0}} };
/* Destination for input data */
uint8_t *data = mbox.data;
/* The maximum amount of data this block may hold */
size_t maxblocksize = sizeof(mbox.data);
/* The actual amount of data in this block */
size_t blocksize;
off_t unused = 0;
/* Block 0 needs special care as it has IANA PEN specifier */
if (!block) {
data = mbox.b0.data;
maxblocksize = sizeof(mbox.b0.data);
htoipmi24(iana, mbox.b0.iana);
}
/*
* Find out how many bytes we are going to write to this
* block.
*/
if (datasize > maxblocksize) {
blocksize = maxblocksize;
}
else {
blocksize = datasize;
}
/* Remember how much data remains */
datasize -= blocksize;
if (!use_text) {
args2buf(argc, argv, data, blocksize);
argc -= blocksize;
argv += blocksize;
}
else {
memcpy(data, argv[0] + string_offset, blocksize);
string_offset += blocksize;
}
lprintf(LOG_INFO, "Block %3" PRId16 ": %s", block,
buf2str_extended(data, blocksize, " "));
unused = maxblocksize - blocksize;
rc = ipmi_chassis_set_bootparam(intf,
IPMI_CHASSIS_BOOTPARAM_INIT_MBOX,
&mbox,
sizeof(mbox) - unused);
if (IPMI_CC_PARAM_OUT_OF_RANGE == rc) {
lprintf(LOG_ERR,
"Hit end of mailbox writing block %" PRId16,
block);
}
if (rc) {
goto complete;
}
}
lprintf(LOG_INFO,
"Wrote %zu blocks of Boot Initiator Mailbox",
blocks);
chassis_bootparam_set_in_progress(intf, COMMIT_WRITE);
rc = chassis_bootparam_clear_ack(intf, BIOS_POST_ACK | OS_LOADER_ACK);
complete:
chassis_bootparam_set_in_progress(intf, SET_COMPLETE);
out:
return rc;
}
static
int
chassis_get_bootmailbox(struct ipmi_intf *intf,
int16_t block, bool use_text)
{
int rc = IPMI_CC_UNSPECIFIED_ERROR;
char param_str[2]; /* Max "7" */
char block_str[4]; /* Max "255" */
char *bpargv[] = { param_str, block_str };
int flags;
flags = use_text ? BP_FLAG(MBOX_PARSE_USE_TEXT) : 0;
snprintf(param_str, sizeof(param_str),
"%" PRIu8, IPMI_CHASSIS_BOOTPARAM_INIT_MBOX);
if (block >= 0) {
snprintf(block_str, sizeof(block_str),
"%" PRIu8, (uint8_t)block);
rc = ipmi_chassis_get_bootparam(intf,
ARRAY_SIZE(bpargv),
bpargv,
flags);
}
else {
int currblk;
flags |= BP_FLAG(MBOX_PARSE_ALLBLOCKS);
for (currblk = 0; currblk <= UCHAR_MAX; ++currblk) {
snprintf(block_str, sizeof(block_str),
"%" PRIu8, (uint8_t)currblk);
if (currblk) {
/*
* If block 0 succeeded, we don't want to
* print generic info for each next block,
* and we don't want range error to be
* reported when we hit the end of blocks.
*/
flags |= BP_FLAG(PARAM_NO_GENERIC_INFO);
flags |= BP_FLAG(PARAM_NO_RANGE_ERROR);
}
rc = ipmi_chassis_get_bootparam(intf,
ARRAY_SIZE(bpargv),
bpargv,
flags);
if (rc) {
if (currblk) {
rc = IPMI_CC_OK;
}
break;
}
}
}
return rc;
}
static
int
chassis_bootmailbox(struct ipmi_intf *intf, int argc, char *argv[])
{
int rc = IPMI_CC_UNSPECIFIED_ERROR;
bool use_text = false; /* Default to data dump I/O mode */
int16_t block = -1; /* By default print all blocks */
const char *cmd;
if ((argc < 1) || !strcmp(argv[0], "help")) {
chassis_bootmailbox_help();
goto out;
} else {
cmd = argv[0];
++argv;
--argc;
if (argc > 0 && !strcmp(argv[0], "text")) {
use_text = true;
++argv;
--argc;
}
if (argc > 0 && !strcmp(argv[0], "block")) {
if (argc < 2) {
chassis_bootmailbox_help();
goto out;
}
if(str2short(argv[1], &block)) {
lprintf(LOG_ERR,
"Invalid block %s", argv[1]);
goto out;
}
argv += 2;
argc -= 2;
}
if (!strcmp(cmd, "get")) {
rc = chassis_get_bootmailbox(intf, block, use_text);
}
else if (!strcmp(cmd, "set")) {
rc = chassis_set_bootmailbox(intf, block, use_text,
argc, argv);
}
}
out:
return rc;
}
static int
ipmi_chassis_power_policy(struct ipmi_intf * intf, uint8_t policy)
{
struct ipmi_rs * rsp;
struct ipmi_rq req;
memset(&req, 0, sizeof(req));
req.msg.netfn = IPMI_NETFN_CHASSIS;
req.msg.cmd = 0x6;
req.msg.data = &policy;
req.msg.data_len = 1;
rsp = intf->sendrecv(intf, &req);
if (!rsp) {
lprintf(LOG_ERR, "Error in Power Restore Policy command");
return -1;
}
if (rsp->ccode) {
lprintf(LOG_ERR, "Power Restore Policy command failed: %s",
val2str(rsp->ccode, completion_code_vals));
return -1;
}
if (policy == IPMI_CHASSIS_POLICY_NO_CHANGE) {
printf("Supported chassis power policy: ");
if (rsp->data[0] & (1<<IPMI_CHASSIS_POLICY_ALWAYS_OFF))
printf("always-off ");
if (rsp->data[0] & (1<<IPMI_CHASSIS_POLICY_ALWAYS_ON))
printf("always-on ");
if (rsp->data[0] & (1<<IPMI_CHASSIS_POLICY_PREVIOUS))
printf("previous");
printf("\n");
}
else {
printf("Set chassis power restore policy to ");
switch (policy) {
case IPMI_CHASSIS_POLICY_ALWAYS_ON:
printf("always-on\n");
break;
case IPMI_CHASSIS_POLICY_ALWAYS_OFF:
printf("always-off\n");
break;
case IPMI_CHASSIS_POLICY_PREVIOUS:
printf("previous\n");
break;
default:
printf("unknown\n");
}
}
return 0;
}
int
ipmi_power_main(struct ipmi_intf * intf, int argc, char ** argv)
{
int rc = 0;
uint8_t ctl = 0;
if ((argc < 1) || (strncmp(argv[0], "help", 4) == 0)) {
lprintf(LOG_NOTICE, "chassis power Commands: status, on, off, cycle, reset, diag, soft");
return 0;
}
if (strncmp(argv[0], "status", 6) == 0) {
rc = ipmi_chassis_print_power_status(intf);
return rc;
}
if ((strncmp(argv[0], "up", 2) == 0) || (strncmp(argv[0], "on", 2) == 0))
ctl = IPMI_CHASSIS_CTL_POWER_UP;
else if ((strncmp(argv[0], "down", 4) == 0) || (strncmp(argv[0], "off", 3) == 0))
ctl = IPMI_CHASSIS_CTL_POWER_DOWN;
else if (strncmp(argv[0], "cycle", 5) == 0)
ctl = IPMI_CHASSIS_CTL_POWER_CYCLE;
else if (strncmp(argv[0], "reset", 5) == 0)
ctl = IPMI_CHASSIS_CTL_HARD_RESET;
else if (strncmp(argv[0], "diag", 4) == 0)
ctl = IPMI_CHASSIS_CTL_PULSE_DIAG;
else if ((strncmp(argv[0], "acpi", 4) == 0) || (strncmp(argv[0], "soft", 4) == 0))
ctl = IPMI_CHASSIS_CTL_ACPI_SOFT;
else {
lprintf(LOG_ERR, "Invalid chassis power command: %s", argv[0]);
return -1;
}
rc = ipmi_chassis_power_control(intf, ctl);
return rc;
}
void
ipmi_chassis_set_bootflag_help()
{
unsigned char set_flag;
unsigned char clr_flag;
lprintf(LOG_NOTICE, "bootparam set bootflag <device> [options=...]");
lprintf(LOG_NOTICE, " Legal devices are:");
lprintf(LOG_NOTICE, " none : No override");
lprintf(LOG_NOTICE, " force_pxe : Force PXE boot");
lprintf(LOG_NOTICE, " force_disk : Force boot from default Hard-drive");
lprintf(LOG_NOTICE, " force_safe : Force boot from default Hard-drive, request Safe Mode");
lprintf(LOG_NOTICE, " force_diag : Force boot from Diagnostic Partition");
lprintf(LOG_NOTICE, " force_cdrom : Force boot from CD/DVD");
lprintf(LOG_NOTICE, " force_bios : Force boot into BIOS Setup");
get_bootparam_options("options=help", &set_flag, &clr_flag);
}
int
ipmi_chassis_main(struct ipmi_intf * intf, int argc, char ** argv)
{
int rc = 0;
if ((argc == 0) || (strncmp(argv[0], "help", 4) == 0)) {
lprintf(LOG_NOTICE, "Chassis Commands:\n"
" status, power, policy, restart_cause\n"
" poh, identify, selftest,\n"
" bootdev, bootparam, bootmbox");
}
else if (strncmp(argv[0], "status", 6) == 0) {
rc = ipmi_chassis_status(intf);
}
else if (strncmp(argv[0], "selftest", 8) == 0) {
rc = ipmi_chassis_selftest(intf);
}
else if (strncmp(argv[0], "power", 5) == 0) {
uint8_t ctl = 0;
if ((argc < 2) || (strncmp(argv[1], "help", 4) == 0)) {
lprintf(LOG_NOTICE, "chassis power Commands: status, on, off, cycle, reset, diag, soft");
return 0;
}
if (strncmp(argv[1], "status", 6) == 0) {
rc = ipmi_chassis_print_power_status(intf);
return rc;
}
if ((strncmp(argv[1], "up", 2) == 0) || (strncmp(argv[1], "on", 2) == 0))
ctl = IPMI_CHASSIS_CTL_POWER_UP;
else if ((strncmp(argv[1], "down", 4) == 0) || (strncmp(argv[1], "off", 3) == 0))
ctl = IPMI_CHASSIS_CTL_POWER_DOWN;
else if (strncmp(argv[1], "cycle", 5) == 0)
ctl = IPMI_CHASSIS_CTL_POWER_CYCLE;
else if (strncmp(argv[1], "reset", 5) == 0)
ctl = IPMI_CHASSIS_CTL_HARD_RESET;
else if (strncmp(argv[1], "diag", 4) == 0)
ctl = IPMI_CHASSIS_CTL_PULSE_DIAG;
else if ((strncmp(argv[1], "acpi", 4) == 0) || (strncmp(argv[1], "soft", 4) == 0))
ctl = IPMI_CHASSIS_CTL_ACPI_SOFT;
else {
lprintf(LOG_ERR, "Invalid chassis power command: %s", argv[1]);
return -1;
}
rc = ipmi_chassis_power_control(intf, ctl);
}
else if (strncmp(argv[0], "identify", 8) == 0) {
if (argc < 2) {
rc = ipmi_chassis_identify(intf, NULL);
}
else if (strncmp(argv[1], "help", 4) == 0) {
lprintf(LOG_NOTICE, "chassis identify <interval>");
lprintf(LOG_NOTICE, " default is 15 seconds");
lprintf(LOG_NOTICE, " 0 to turn off");
lprintf(LOG_NOTICE, " force to turn on indefinitely");
} else {
rc = ipmi_chassis_identify(intf, argv[1]);
}
}
else if (strncmp(argv[0], "poh", 3) == 0) {
rc = ipmi_chassis_poh(intf);
}
else if (strncmp(argv[0], "restart_cause", 13) == 0) {
rc = ipmi_chassis_restart_cause(intf);
}
else if (strncmp(argv[0], "policy", 4) == 0) {
if ((argc < 2) || (strncmp(argv[1], "help", 4) == 0)) {
lprintf(LOG_NOTICE, "chassis policy <state>");
lprintf(LOG_NOTICE, " list : return supported policies");
lprintf(LOG_NOTICE, " always-on : turn on when power is restored");
lprintf(LOG_NOTICE, " previous : return to previous state when power is restored");
lprintf(LOG_NOTICE, " always-off : stay off after power is restored");
} else {
uint8_t ctl;
if (strncmp(argv[1], "list", 4) == 0)
ctl = IPMI_CHASSIS_POLICY_NO_CHANGE;
else if (strncmp(argv[1], "always-on", 9) == 0)
ctl = IPMI_CHASSIS_POLICY_ALWAYS_ON;
else if (strncmp(argv[1], "previous", 8) == 0)
ctl = IPMI_CHASSIS_POLICY_PREVIOUS;
else if (strncmp(argv[1], "always-off", 10) == 0)
ctl = IPMI_CHASSIS_POLICY_ALWAYS_OFF;
else {
lprintf(LOG_ERR, "Invalid chassis policy: %s", argv[1]);
return -1;
}
rc = ipmi_chassis_power_policy(intf, ctl);
}
}
else if (strncmp(argv[0], "bootparam", 9) == 0) {
if ((argc < 3) || (strncmp(argv[1], "help", 4) == 0)) {
lprintf(LOG_NOTICE, "bootparam get <param #>");
ipmi_chassis_set_bootflag_help();
}
else {
if (strncmp(argv[1], "get", 3) == 0) {
rc = ipmi_chassis_get_bootparam(intf,
argc - 2,
argv + 2,
0);
}
else if (strncmp(argv[1], "set", 3) == 0) {
unsigned char set_flag=0;
unsigned char clr_flag=0;
if (strncmp(argv[2], "help", 4) == 0 ||
argc < 4 || (argc >= 4 &&
strncmp(argv[2], "bootflag", 8) != 0)) {
ipmi_chassis_set_bootflag_help();
} else {
if (argc == 5) {
get_bootparam_options(argv[4], &set_flag, &clr_flag);
}
rc = ipmi_chassis_set_bootdev(intf, argv[3], NULL);
if (argc == 5 && (set_flag != 0 || clr_flag != 0)) {
rc = ipmi_chassis_set_bootvalid(intf, set_flag, clr_flag);
}
}
}
else
lprintf(LOG_NOTICE, "bootparam get|set <option> [value ...]");
}
}
else if (strncmp(argv[0], "bootdev", 7) == 0) {
if ((argc < 2) || (strncmp(argv[1], "help", 4) == 0)) {
lprintf(LOG_NOTICE, "bootdev <device> [clear-cmos=yes|no]");
lprintf(LOG_NOTICE, "bootdev <device> [options=help,...]");
lprintf(LOG_NOTICE, " none : Do not change boot device order");
lprintf(LOG_NOTICE, " pxe : Force PXE boot");
lprintf(LOG_NOTICE, " disk : Force boot from default Hard-drive");
lprintf(LOG_NOTICE, " safe : Force boot from default Hard-drive, request Safe Mode");
lprintf(LOG_NOTICE, " diag : Force boot from Diagnostic Partition");
lprintf(LOG_NOTICE, " cdrom : Force boot from CD/DVD");
lprintf(LOG_NOTICE, " bios : Force boot into BIOS Setup");
lprintf(LOG_NOTICE, " floppy: Force boot from Floppy/primary removable media");
} else {
if (argc < 3)
rc = ipmi_chassis_set_bootdev(intf, argv[1], NULL);
else if (strncmp(argv[2], "clear-cmos=", 11) == 0) {
if (strncmp(argv[2]+11, "yes", 3) == 0) {
uint8_t flags[5] = {0, (1<<7), 0, 0, 0};
rc = ipmi_chassis_set_bootdev(intf, argv[1], flags);
} else
rc = ipmi_chassis_set_bootdev(intf, argv[1], NULL);
}
else if (strncmp(argv[2], "options=", 8) == 0) {
char *token;
char *saveptr = NULL;
int optionError = 0;
unsigned char flags[5];
static struct {
char *name;
int i;
unsigned char mask;
unsigned char value;
char *desc;
} options[] = {
/* data 1 */
{"valid", 0, (1<<7), (1<<7),
"Boot flags valid"},
{"persistent", 0, (1<<6), (1<<6),
"Changes are persistent for all future boots"},
{"efiboot", 0, (1<<5), (1<<5),
"Extensible Firmware Interface Boot (EFI)"},
/* data 2 */
{"clear-cmos", 1, (1<<7), (1<<7),
"CMOS clear"},
{"lockkbd", 1, (1<<6), (1<<6),
"Lock Keyboard"},
/* data2[5:2] is parsed elsewhere */
{"screenblank", 1, (1<<1), (1<<1),
"Screen Blank"},
{"lockoutreset", 1, (1<<0), (1<<0),
"Lock out Resetbuttons"},
/* data 3 */
{"lockout_power", 2, (1<<7), (1<<7),
"Lock out (power off/sleep request) via Power Button"},
{"verbose=default", 2, (3<<5), (0<<5),
"Request quiet BIOS display"},
{"verbose=no", 2, (3<<5), (1<<5),
"Request quiet BIOS display"},
{"verbose=yes", 2, (3<<5), (2<<5),
"Request verbose BIOS display"},
{"force_pet", 2, (1<<4), (1<<4),
"Force progress event traps"},
{"upw_bypass", 2, (1<<3), (1<<3),
"User password bypass"},
{"lockout_sleep", 2, (1<<2), (1<<2),
"Log Out Sleep Button"},
{"cons_redirect=default", 2, (3<<0), (0<<0),
"Console redirection occurs per BIOS configuration setting"},
{"cons_redirect=skip", 2, (3<<0), (1<<0),
"Suppress (skip) console redirection if enabled"},
{"cons_redirect=enable", 2, (3<<0), (2<<0),
"Suppress (skip) console redirection if enabled"},
/* data 4 */
/* data4[7:4] reserved */
/* data4[3] BIOS Shared Mode Override, not implemented here */
/* data4[2:0] BIOS Mux Control Override, not implemented here */
/* data5 reserved */
{NULL} /* End marker */
}, *op;
memset(&flags[0], 0, sizeof(flags));
token = strtok_r(argv[2] + 8, ",", &saveptr);
while (token) {
if (strcmp(token, "help") == 0) {
optionError = 1;
break;
}
for (op = options; op->name; ++op) {
if (strcmp(token, op->name) == 0) {
flags[op->i] &= op->mask;
flags[op->i] |= op->value;
break;
}
}
if (!op->name) {
/* Option not found */
optionError = 1;
lprintf(LOG_ERR, "Invalid option: %s", token);
}
token = strtok_r(NULL, ",", &saveptr);
}
if (optionError) {
lprintf(LOG_NOTICE, "Legal options settings are:");
lprintf(LOG_NOTICE, "\thelp:\tprint this message");
for (op = options; op->name; ++op) {
lprintf(LOG_NOTICE, "\t%s:\t%s", op->name, op->desc);
}
return (-1);
}
rc = ipmi_chassis_set_bootdev(intf, argv[1], flags);
}
else
rc = ipmi_chassis_set_bootdev(intf, argv[1], NULL);
}
}
else if (!strcmp(argv[0], "bootmbox")) {
rc = chassis_bootmailbox(intf, argc -1, argv + 1);
}
else {
lprintf(LOG_ERR, "Invalid chassis command: %s", argv[0]);
return -1;
}
return rc;
}
|