xref: /openbmc/ipmitool/lib/ipmi_mc.c (revision 2d79e69f)
1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistribution of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistribution in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind.
20  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
23  * SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
24  * FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
25  * OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
26  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
27  * OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
28  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
29  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  */
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <time.h>
37 
38 #include <ipmitool/helper.h>
39 #include <ipmitool/log.h>
40 #include <ipmitool/bswap.h>
41 #include <ipmitool/ipmi.h>
42 #include <ipmitool/ipmi_intf.h>
43 #include <ipmitool/ipmi_mc.h>
44 #include <ipmitool/ipmi_strings.h>
45 
46 extern int verbose;
47 
48 static int ipmi_sysinfo_main(struct ipmi_intf *intf, int argc, char ** argv,
49 		int is_set);
50 static void printf_sysinfo_usage(int full_help);
51 
52 /* ipmi_mc_reset  -  attempt to reset an MC
53  *
54  * @intf:	ipmi interface
55  * @cmd:	reset command to send
56  *              BMC_WARM_RESET or
57  *              BMC_COLD_RESET
58  *
59  * returns 0 on success
60  * returns -1 on error
61  */
62 static int
ipmi_mc_reset(struct ipmi_intf * intf,int cmd)63 ipmi_mc_reset(struct ipmi_intf * intf, int cmd)
64 {
65 	struct ipmi_rs * rsp;
66 	struct ipmi_rq req;
67 
68 	if( !intf->opened )
69 	intf->open(intf);
70 
71 	memset(&req, 0, sizeof(req));
72 	req.msg.netfn = IPMI_NETFN_APP;
73 	req.msg.cmd = cmd;
74 	req.msg.data_len = 0;
75 
76 	if (cmd == BMC_COLD_RESET)
77 		intf->noanswer = 1;
78 
79 	rsp = intf->sendrecv(intf, &req);
80 
81 	if (cmd == BMC_COLD_RESET)
82 		intf->abort = 1;
83 
84 	if (cmd == BMC_COLD_RESET && rsp == NULL) {
85 		/* This is expected. See 20.2 Cold Reset Command, p.243, IPMIv2.0 rev1.0 */
86 	} else if (rsp == NULL) {
87 		lprintf(LOG_ERR, "MC reset command failed.");
88 		return (-1);
89 	} else if (rsp->ccode > 0) {
90 		lprintf(LOG_ERR, "MC reset command failed: %s",
91 				val2str(rsp->ccode, completion_code_vals));
92 		return (-1);
93 	}
94 
95 	printf("Sent %s reset command to MC\n",
96 	       (cmd == BMC_WARM_RESET) ? "warm" : "cold");
97 
98 	return 0;
99 }
100 
101 #ifdef HAVE_PRAGMA_PACK
102 #pragma pack(1)
103 #endif
104 struct bmc_enables_data {
105 #if WORDS_BIGENDIAN
106 	uint8_t oem2		: 1;
107 	uint8_t oem1		: 1;
108 	uint8_t oem0		: 1;
109 	uint8_t __reserved	: 1;
110 	uint8_t system_event_log	: 1;
111 	uint8_t event_msgbuf	: 1;
112 	uint8_t event_msgbuf_intr	: 1;
113 	uint8_t receive_msg_intr	: 1;
114 #else
115 	uint8_t receive_msg_intr	: 1;
116 	uint8_t event_msgbuf_intr	: 1;
117 	uint8_t event_msgbuf	: 1;
118 	uint8_t system_event_log	: 1;
119 	uint8_t __reserved	: 1;
120 	uint8_t oem0		: 1;
121 	uint8_t oem1		: 1;
122 	uint8_t oem2		: 1;
123 #endif
124 } ATTRIBUTE_PACKING;
125 #ifdef HAVE_PRAGMA_PACK
126 #pragma pack(0)
127 #endif
128 
129 struct bitfield_data {
130 	const char * name;
131 	const char * desc;
132 	uint32_t mask;
133 } mc_enables_bf[] = {
134 	{
135 		.name = "recv_msg_intr",
136 		.desc = "Receive Message Queue Interrupt",
137 		.mask = 1<<0,
138 	},
139 	{
140 		.name = "event_msg_intr",
141 		.desc = "Event Message Buffer Full Interrupt",
142 		.mask = 1<<1,
143 	},
144 	{
145 		.name = "event_msg",
146 		.desc = "Event Message Buffer",
147 		.mask = 1<<2,
148 	},
149 	{
150 		.name = "system_event_log",
151 		.desc = "System Event Logging",
152 		.mask = 1<<3,
153 	},
154 	{
155 		.name = "oem0",
156 		.desc = "OEM 0",
157 		.mask = 1<<5,
158 	},
159 	{
160 		.name = "oem1",
161 		.desc = "OEM 1",
162 		.mask = 1<<6,
163 	},
164 	{
165 		.name = "oem2",
166 		.desc = "OEM 2",
167 		.mask = 1<<7,
168 	},
169 	{ NULL },
170 };
171 
172 static void
printf_mc_reset_usage(void)173 printf_mc_reset_usage(void)
174 {
175 	lprintf(LOG_NOTICE, "usage: mc reset <warm|cold>");
176 } /* printf_mc_reset_usage(void) */
177 
178 static void
printf_mc_usage(void)179 printf_mc_usage(void)
180 {
181 	struct bitfield_data * bf;
182 	lprintf(LOG_NOTICE, "MC Commands:");
183 	lprintf(LOG_NOTICE, "  reset <warm|cold>");
184 	lprintf(LOG_NOTICE, "  guid");
185 	lprintf(LOG_NOTICE, "  info");
186 	lprintf(LOG_NOTICE, "  watchdog <get|reset|off>");
187 	lprintf(LOG_NOTICE, "  selftest");
188 	lprintf(LOG_NOTICE, "  getenables");
189 	lprintf(LOG_NOTICE, "  setenables <option=on|off> ...");
190 	for (bf = mc_enables_bf; bf->name != NULL; bf++) {
191 		lprintf(LOG_NOTICE, "    %-20s  %s", bf->name, bf->desc);
192 	}
193 	printf_sysinfo_usage(0);
194 }
195 
196 static void
printf_sysinfo_usage(int full_help)197 printf_sysinfo_usage(int full_help)
198 {
199 	if (full_help != 0)
200 		lprintf(LOG_NOTICE, "usage:");
201 
202 	lprintf(LOG_NOTICE, "  getsysinfo <argument>");
203 
204 	if (full_help != 0) {
205 		lprintf(LOG_NOTICE,
206 				"    Retrieves system info from BMC for given argument");
207 	}
208 
209 	lprintf(LOG_NOTICE, "  setsysinfo <argument> <string>");
210 
211 	if (full_help != 0) {
212 		lprintf(LOG_NOTICE,
213 				"    Stores system info string for given argument to BMC");
214 		lprintf(LOG_NOTICE, "");
215 		lprintf(LOG_NOTICE, "  Valid arguments are:");
216 	}
217 	lprintf(LOG_NOTICE,
218 			"    system_fw_version   System firmware (e.g. BIOS) version");
219 	lprintf(LOG_NOTICE,
220 			"    primary_os_name     Primary operating system name");
221 	lprintf(LOG_NOTICE, "    os_name             Operating system name");
222 	lprintf(LOG_NOTICE,
223 			"    system_name         System Name of server(vendor dependent)");
224 	lprintf(LOG_NOTICE,
225 			"    delloem_os_version  Running version of operating system");
226 	lprintf(LOG_NOTICE, "    delloem_url         URL of BMC webserver");
227 	lprintf(LOG_NOTICE, "");
228 }
229 
230 static void
print_watchdog_usage(void)231 print_watchdog_usage(void)
232 {
233 	lprintf(LOG_NOTICE, "usage: watchdog <command>:");
234 	lprintf(LOG_NOTICE, "   get    :  Get Current Watchdog settings");
235 	lprintf(LOG_NOTICE, "   reset  :  Restart Watchdog timer based on most recent settings");
236 	lprintf(LOG_NOTICE, "   off    :  Shut off a running Watchdog timer");
237 }
238 
239 /* ipmi_mc_get_enables  -  print out MC enables
240  *
241  * @intf:	ipmi inteface
242  *
243  * returns 0 on success
244  * returns -1 on error
245  */
246 static int
ipmi_mc_get_enables(struct ipmi_intf * intf)247 ipmi_mc_get_enables(struct ipmi_intf * intf)
248 {
249 	struct ipmi_rs * rsp;
250 	struct ipmi_rq req;
251 	struct bitfield_data * bf;
252 
253 	memset(&req, 0, sizeof(req));
254 	req.msg.netfn = IPMI_NETFN_APP;
255 	req.msg.cmd = BMC_GET_GLOBAL_ENABLES;
256 
257 	rsp = intf->sendrecv(intf, &req);
258 	if (rsp == NULL) {
259 		lprintf(LOG_ERR, "Get Global Enables command failed");
260 		return -1;
261 	}
262 	if (rsp->ccode > 0) {
263 		lprintf(LOG_ERR, "Get Global Enables command failed: %s",
264 		       val2str(rsp->ccode, completion_code_vals));
265 		return -1;
266 	}
267 
268 	for (bf = mc_enables_bf; bf->name != NULL; bf++) {
269 		printf("%-40s : %sabled\n", bf->desc,
270 		       rsp->data[0] & bf->mask ? "en" : "dis");
271 	}
272 
273 	return 0;
274 }
275 
276 /* ipmi_mc_set_enables  -  set MC enable flags
277  *
278  * @intf:	ipmi inteface
279  * @argc:	argument count
280  * @argv:	argument list
281  *
282  * returns 0 on success
283  * returns -1 on error
284  */
285 static int
ipmi_mc_set_enables(struct ipmi_intf * intf,int argc,char ** argv)286 ipmi_mc_set_enables(struct ipmi_intf * intf, int argc, char ** argv)
287 {
288 	struct ipmi_rs * rsp;
289 	struct ipmi_rq req;
290 	struct bitfield_data * bf;
291 	uint8_t en;
292 	int i;
293 
294 	if (argc < 1) {
295 		printf_mc_usage();
296 		return (-1);
297 	}
298 	else if (strncmp(argv[0], "help", 4) == 0) {
299 		printf_mc_usage();
300 		return 0;
301 	}
302 
303 	memset(&req, 0, sizeof(req));
304 	req.msg.netfn = IPMI_NETFN_APP;
305 	req.msg.cmd = BMC_GET_GLOBAL_ENABLES;
306 
307 	rsp = intf->sendrecv(intf, &req);
308 	if (rsp == NULL) {
309 		lprintf(LOG_ERR, "Get Global Enables command failed");
310 		return -1;
311 	}
312 	if (rsp->ccode > 0) {
313 		lprintf(LOG_ERR, "Get Global Enables command failed: %s",
314 		       val2str(rsp->ccode, completion_code_vals));
315 		return -1;
316 	}
317 
318 	en = rsp->data[0];
319 
320 	for (i = 0; i < argc; i++) {
321 		for (bf = mc_enables_bf; bf->name != NULL; bf++) {
322 			int nl = strlen(bf->name);
323 			if (strncmp(argv[i], bf->name, nl) != 0)
324 				continue;
325 			if (strncmp(argv[i]+nl+1, "off", 3) == 0) {
326 					printf("Disabling %s\n", bf->desc);
327 					en &= ~bf->mask;
328 			}
329 			else if (strncmp(argv[i]+nl+1, "on", 2) == 0) {
330 					printf("Enabling %s\n", bf->desc);
331 					en |= bf->mask;
332 			}
333 			else {
334 				lprintf(LOG_ERR, "Unrecognized option: %s", argv[i]);
335 			}
336 		}
337 	}
338 
339 	if (en == rsp->data[0]) {
340 		printf("\nNothing to change...\n");
341 		ipmi_mc_get_enables(intf);
342 		return 0;
343 	}
344 
345 	req.msg.cmd = BMC_SET_GLOBAL_ENABLES;
346 	req.msg.data = &en;
347 	req.msg.data_len = 1;
348 
349 	rsp = intf->sendrecv(intf, &req);
350 	if (rsp == NULL) {
351 		lprintf(LOG_ERR, "Set Global Enables command failed");
352 		return -1;
353 	}
354 	else if (rsp->ccode > 0) {
355 		lprintf(LOG_ERR, "Set Global Enables command failed: %s",
356 		       val2str(rsp->ccode, completion_code_vals));
357 		return -1;
358 	}
359 
360 	printf("\nVerifying...\n");
361 	ipmi_mc_get_enables(intf);
362 
363 	return 0;
364 }
365 
366 /* IPM Device, Get Device ID Command - Additional Device Support */
367 const char *ipm_dev_adtl_dev_support[8] = {
368 	"Sensor Device",         /* bit 0 */
369 	"SDR Repository Device", /* bit 1 */
370 	"SEL Device",            /* bit 2 */
371 	"FRU Inventory Device",  /*  ...  */
372 	"IPMB Event Receiver",
373 	"IPMB Event Generator",
374 	"Bridge",
375 	"Chassis Device"         /* bit 7 */
376 };
377 
378 /* ipmi_mc_get_deviceid  -  print information about this MC
379  *
380  * @intf:	ipmi interface
381  *
382  * returns 0 on success
383  * returns -1 on error
384  */
385 static int
ipmi_mc_get_deviceid(struct ipmi_intf * intf)386 ipmi_mc_get_deviceid(struct ipmi_intf * intf)
387 {
388 	struct ipmi_rs * rsp;
389 	struct ipmi_rq req;
390 	struct ipm_devid_rsp *devid;
391 	int i;
392 	const char *product=NULL;
393 
394 	memset(&req, 0, sizeof(req));
395 	req.msg.netfn = IPMI_NETFN_APP;
396 	req.msg.cmd = BMC_GET_DEVICE_ID;
397 	req.msg.data_len = 0;
398 
399 	rsp = intf->sendrecv(intf, &req);
400 	if (rsp == NULL) {
401 		lprintf(LOG_ERR, "Get Device ID command failed");
402 		return -1;
403 	}
404 	if (rsp->ccode > 0) {
405 		lprintf(LOG_ERR, "Get Device ID command failed: %s",
406 			val2str(rsp->ccode, completion_code_vals));
407 		return -1;
408 	}
409 
410 	devid = (struct ipm_devid_rsp *) rsp->data;
411 	printf("Device ID                 : %i\n",
412 		devid->device_id);
413 	printf("Device Revision           : %i\n",
414 		devid->device_revision & IPM_DEV_DEVICE_ID_REV_MASK);
415 	printf("Firmware Revision         : %u.%02x\n",
416 		devid->fw_rev1 & IPM_DEV_FWREV1_MAJOR_MASK,
417 		devid->fw_rev2);
418 	printf("IPMI Version              : %x.%x\n",
419 		IPM_DEV_IPMI_VERSION_MAJOR(devid->ipmi_version),
420 		IPM_DEV_IPMI_VERSION_MINOR(devid->ipmi_version));
421 	printf("Manufacturer ID           : %lu\n",
422 		(long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id));
423 	printf("Manufacturer Name         : %s\n",
424 			val2str( (long)IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id),
425 				ipmi_oem_info) );
426 
427 	printf("Product ID                : %u (0x%02x%02x)\n",
428 		buf2short((uint8_t *)(devid->product_id)),
429 		devid->product_id[1], devid->product_id[0]);
430 
431 	product=oemval2str(IPM_DEV_MANUFACTURER_ID(devid->manufacturer_id),
432 							 (devid->product_id[1]<<8)+devid->product_id[0],
433 							 ipmi_oem_product_info);
434 
435 	if (product!=NULL) {
436 		printf("Product Name              : %s\n", product);
437 	}
438 
439 	printf("Device Available          : %s\n",
440 		(devid->fw_rev1 & IPM_DEV_FWREV1_AVAIL_MASK) ?
441 		"no" : "yes");
442 	printf("Provides Device SDRs      : %s\n",
443 		(devid->device_revision & IPM_DEV_DEVICE_ID_SDR_MASK) ?
444 		"yes" : "no");
445 	printf("Additional Device Support :\n");
446 	for (i = 0; i < IPM_DEV_ADTL_SUPPORT_BITS; i++) {
447 		if (devid->adtl_device_support & (1 << i)) {
448 			printf("    %s\n", ipm_dev_adtl_dev_support[i]);
449 		}
450 	}
451 	if (rsp->data_len == sizeof(*devid)) {
452 		printf("Aux Firmware Rev Info     : \n");
453 		/* These values could be looked-up by vendor if documented,
454 		 * so we put them on individual lines for better treatment later
455 		 */
456 		printf("    0x%02x\n    0x%02x\n    0x%02x\n    0x%02x\n",
457 			devid->aux_fw_rev[0],
458 			devid->aux_fw_rev[1],
459 			devid->aux_fw_rev[2],
460 			devid->aux_fw_rev[3]);
461 	}
462 	return 0;
463 }
464 
465 /* Structure follow the IPMI V.2 Rev 1.0
466  * See Table 20-10 */
467 #ifdef HAVE_PRAGMA_PACK
468 #pragma pack(1)
469 #endif
470 
471 struct ipmi_guid {
472 	uint32_t  time_low;	/* timestamp low field */
473 	uint16_t  time_mid;	/* timestamp middle field */
474 	uint16_t  time_hi_and_version; /* timestamp high field and version number */
475 	uint8_t   clock_seq_hi_variant;/* clock sequence high field and variant */
476 	uint8_t   clock_seq_low; /* clock sequence low field */
477 	uint8_t   node[6];	/* node */
478 } ATTRIBUTE_PACKING;
479 #ifdef HAVE_PRAGMA_PACK
480 #pragma pack(0)
481 #endif
482 
483 /* ipmi_mc_get_guid  -  print this MC GUID
484  *
485  * @intf:	ipmi interface
486  *
487  * returns 0 on success
488  * returns -1 on error
489  */
490 static int
ipmi_mc_get_guid(struct ipmi_intf * intf)491 ipmi_mc_get_guid(struct ipmi_intf * intf)
492 {
493 	struct ipmi_rs * rsp;
494 	struct ipmi_rq req;
495 	struct ipmi_guid guid;
496 
497 	memset(&req, 0, sizeof(req));
498 	req.msg.netfn = IPMI_NETFN_APP;
499 	req.msg.cmd = BMC_GET_GUID;
500 
501 	rsp = intf->sendrecv(intf, &req);
502 	if (rsp == NULL) {
503 		lprintf(LOG_ERR, "Get GUID command failed");
504 		return -1;
505 	}
506 	if (rsp->ccode > 0) {
507 		lprintf(LOG_ERR, "Get GUID command failed: %s",
508 			val2str(rsp->ccode, completion_code_vals));
509 		return -1;
510 	}
511 
512 	if (rsp->data_len == sizeof(struct ipmi_guid)) {
513 		char tbuf[40];
514 		time_t s;
515 		memset(tbuf, 0, 40);
516 		memset(&guid, 0, sizeof(struct ipmi_guid));
517 		memcpy(&guid, rsp->data, rsp->data_len);
518 
519 		/* Kipp - changed order of last field (node) to follow specification */
520 		printf("System GUID  : %08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x\n",
521 		       guid.time_low, guid.time_mid, guid.time_hi_and_version,
522 		       guid.clock_seq_hi_variant << 8 | guid.clock_seq_low,
523 		       guid.node[0], guid.node[1], guid.node[2],
524 		       guid.node[3], guid.node[4], guid.node[5]);
525 
526 		s = (time_t)guid.time_low; /* Kipp - removed the BSWAP_32, it was not needed here */
527 		strftime(tbuf, sizeof(tbuf), "%m/%d/%Y %H:%M:%S", localtime(&s));
528 		printf("Timestamp    : %s\n", tbuf);
529 	}
530 	else {
531 		lprintf(LOG_ERR, "Invalid GUID length %d", rsp->data_len);
532 	}
533 
534 	return 0;
535 }
536 
537 /* ipmi_mc_get_selftest -  returns and print selftest results
538  *
539  * @intf:	ipmi interface
540  */
ipmi_mc_get_selftest(struct ipmi_intf * intf)541 static int ipmi_mc_get_selftest(struct ipmi_intf * intf)
542 {
543    int rv = 0;
544 	struct ipmi_rs * rsp;
545 	struct ipmi_rq req;
546 	struct ipm_selftest_rsp *sft_res;
547 
548 	memset(&req, 0, sizeof(req));
549 	req.msg.netfn = IPMI_NETFN_APP;
550 	req.msg.cmd = BMC_GET_SELF_TEST;
551 	req.msg.data_len = 0;
552 
553 	rsp = intf->sendrecv(intf, &req);
554 	if (!rsp) {
555 		lprintf(LOG_ERR, "No response from devices\n");
556 		return -1;
557 	}
558 
559 	if (rsp->ccode) {
560 		lprintf(LOG_ERR, "Bad response: (%s)",
561 				val2str(rsp->ccode, completion_code_vals));
562 		return -1;
563 	}
564 
565 	sft_res = (struct ipm_selftest_rsp *) rsp->data;
566 
567 	if (sft_res->code == IPM_SFT_CODE_OK) {
568 		printf("Selftest: passed\n");
569 		rv = 0;
570 	}
571 
572 	else if (sft_res->code == IPM_SFT_CODE_NOT_IMPLEMENTED) {
573 		printf("Selftest: not implemented\n");
574 		rv = -1;
575 	}
576 
577 	else if (sft_res->code == IPM_SFT_CODE_DEV_CORRUPTED) {
578 		printf("Selftest: device corrupted\n");
579 		rv = -1;
580 
581 		if (sft_res->test & IPM_SELFTEST_SEL_ERROR) {
582 			printf(" -> SEL device not accessible\n");
583 		}
584 		if (sft_res->test & IPM_SELFTEST_SDR_ERROR) {
585 			printf(" -> SDR repository not accesible\n");
586 		}
587 		if (sft_res->test & IPM_SELFTEST_FRU_ERROR) {
588 			printf("FRU device not accessible\n");
589 		}
590 		if (sft_res->test & IPM_SELFTEST_IPMB_ERROR) {
591 			printf("IPMB signal lines do not respond\n");
592 		}
593 		if (sft_res->test & IPM_SELFTEST_SDRR_EMPTY) {
594 			printf("SDR repository empty\n");
595 		}
596 		if (sft_res->test & IPM_SELFTEST_INTERNAL_USE) {
597 			printf("Internal Use Area corrupted\n");
598 		}
599 		if (sft_res->test & IPM_SELFTEST_FW_BOOTBLOCK) {
600 			printf("Controller update boot block corrupted\n");
601 		}
602 		if (sft_res->test & IPM_SELFTEST_FW_CORRUPTED) {
603 			printf("controller operational firmware corrupted\n");
604 		}
605 	}
606 
607 	else if (sft_res->code == IPM_SFT_CODE_FATAL_ERROR) {
608 		printf("Selftest     : fatal error\n");
609 		printf("Failure code : %02x\n", sft_res->test);
610 		rv = -1;
611 	}
612 
613 	else if (sft_res->code == IPM_SFT_CODE_RESERVED) {
614 		printf("Selftest: N/A");
615 		rv = -1;
616 	}
617 
618 	else {
619 		printf("Selftest     : device specific (%02Xh)\n", sft_res->code);
620 		printf("Failure code : %02Xh\n", sft_res->test);
621 		rv = 0;
622 	}
623 
624 	return rv;
625 }
626 
627 /* ipmi_mc_get_watchdog
628  *
629  * @intf:	ipmi interface
630  *
631  * returns 0 on success
632  * returns -1 on error
633  */
634 
635 const char *wdt_use_string[8] = {
636 	"Reserved",
637 	"BIOS FRB2",
638 	"BIOS/POST",
639 	"OS Load",
640 	"SMS/OS",
641 	"OEM",
642 	"Reserved",
643 	"Reserved"
644 };
645 
646 const char *wdt_action_string[8] = {
647 	"No action",
648 	"Hard Reset",
649 	"Power Down",
650 	"Power Cycle",
651 	"Reserved",
652 	"Reserved",
653 	"Reserved",
654 	"Reserved"
655 };
656 
657 static int
ipmi_mc_get_watchdog(struct ipmi_intf * intf)658 ipmi_mc_get_watchdog(struct ipmi_intf * intf)
659 {
660 	struct ipmi_rs * rsp;
661 	struct ipmi_rq req;
662 	struct ipm_get_watchdog_rsp * wdt_res;
663 
664 	memset(&req, 0, sizeof(req));
665 	req.msg.netfn = IPMI_NETFN_APP;
666 	req.msg.cmd = BMC_GET_WATCHDOG_TIMER;
667 	req.msg.data_len = 0;
668 
669 	rsp = intf->sendrecv(intf, &req);
670 	if (rsp == NULL) {
671 		lprintf(LOG_ERR, "Get Watchdog Timer command failed");
672 		return -1;
673 	}
674 
675 	if (rsp->ccode) {
676 		lprintf(LOG_ERR, "Get Watchdog Timer command failed: %s",
677 			val2str(rsp->ccode, completion_code_vals));
678 		return -1;
679 	}
680 
681 	wdt_res = (struct ipm_get_watchdog_rsp *) rsp->data;
682 
683 	printf("Watchdog Timer Use:     %s (0x%02x)\n",
684 			wdt_use_string[(wdt_res->timer_use & 0x07 )], wdt_res->timer_use);
685 	printf("Watchdog Timer Is:      %s\n",
686 		wdt_res->timer_use & 0x40 ? "Started/Running" : "Stopped");
687 	printf("Watchdog Timer Actions: %s (0x%02x)\n",
688 		 wdt_action_string[(wdt_res->timer_actions&0x07)], wdt_res->timer_actions);
689 	printf("Pre-timeout interval:   %d seconds\n", wdt_res->pre_timeout);
690 	printf("Timer Expiration Flags: 0x%02x\n", wdt_res->timer_use_exp);
691 	printf("Initial Countdown:      %i sec\n",
692 			((wdt_res->initial_countdown_msb << 8) | wdt_res->initial_countdown_lsb)/10);
693 	printf("Present Countdown:      %i sec\n",
694 			(((wdt_res->present_countdown_msb << 8) | wdt_res->present_countdown_lsb)) / 10);
695 
696 	return 0;
697 }
698 
699 /* ipmi_mc_shutoff_watchdog
700  *
701  * @intf:	ipmi interface
702  *
703  * returns 0 on success
704  * returns -1 on error
705  */
706 static int
ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf)707 ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf)
708 {
709 	struct ipmi_rs * rsp;
710 	struct ipmi_rq req;
711 	unsigned char msg_data[6];
712 
713 	memset(&req, 0, sizeof(req));
714 	req.msg.netfn = IPMI_NETFN_APP;
715 	req.msg.cmd   = BMC_SET_WATCHDOG_TIMER;
716 	req.msg.data  = msg_data;
717 	req.msg.data_len = 6;
718 
719 	/*
720 	 * The only set cmd we're allowing is to shut off the timer.
721 	 * Turning on the timer should be the job of the ipmi watchdog driver.
722 	 * See 'modinfo ipmi_watchdog' for more info. (NOTE: the reset
723 	 * command will restart the timer if it's already been initialized.)
724 	 *
725 	 * Out-of-band watchdog set commands can still be sent via the raw
726 	 * command interface but this is a very dangerous thing to do since
727 	 * a periodic "poke"/reset over a network is unreliable.  This is
728 	 * not a recommended way to use the IPMI watchdog commands.
729 	 */
730 
731 	msg_data[0] = IPM_WATCHDOG_SMS_OS;
732 	msg_data[1] = IPM_WATCHDOG_NO_ACTION;
733 	msg_data[2] = 0x00;  /* pretimeout interval */
734 	msg_data[3] = IPM_WATCHDOG_CLEAR_SMS_OS;
735 	msg_data[4] = 0xb8;  /* countdown lsb (100 ms/count) */
736 	msg_data[5] = 0x0b;  /* countdown msb - 5 mins */
737 
738 	rsp = intf->sendrecv(intf, &req);
739 	if (rsp == NULL) {
740 		lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed!");
741 		return -1;
742 	}
743 
744 	if (rsp->ccode) {
745 		lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed! %s",
746 			val2str(rsp->ccode, completion_code_vals));
747 		return -1;
748 	}
749 
750 	printf("Watchdog Timer Shutoff successful -- timer stopped\n");
751 	return 0;
752 }
753 
754 
755 /* ipmi_mc_rst_watchdog
756  *
757  * @intf:	ipmi interface
758  *
759  * returns 0 on success
760  * returns -1 on error
761  */
762 static int
ipmi_mc_rst_watchdog(struct ipmi_intf * intf)763 ipmi_mc_rst_watchdog(struct ipmi_intf * intf)
764 {
765 	struct ipmi_rs * rsp;
766 	struct ipmi_rq req;
767 
768 	memset(&req, 0, sizeof(req));
769 	req.msg.netfn = IPMI_NETFN_APP;
770 	req.msg.cmd   = BMC_RESET_WATCHDOG_TIMER;
771 	req.msg.data_len = 0;
772 
773 	rsp = intf->sendrecv(intf, &req);
774 	if (rsp == NULL) {
775 		lprintf(LOG_ERR, "Reset Watchdog Timer command failed!");
776 		return -1;
777 	}
778 
779 	if (rsp->ccode) {
780 		lprintf(LOG_ERR, "Reset Watchdog Timer command failed: %s",
781 			(rsp->ccode == IPM_WATCHDOG_RESET_ERROR) ?
782 				"Attempt to reset unitialized watchdog" :
783 				val2str(rsp->ccode, completion_code_vals));
784 		return -1;
785 	}
786 
787 	printf("IPMI Watchdog Timer Reset -  countdown restarted!\n");
788 	return 0;
789 }
790 
791 /* ipmi_mc_main  -  top-level handler for MC functions
792  *
793  * @intf:	ipmi interface
794  * @argc:	number of arguments
795  * @argv:	argument list
796  *
797  * returns 0 on success
798  * returns -1 on error
799  */
800 int
ipmi_mc_main(struct ipmi_intf * intf,int argc,char ** argv)801 ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv)
802 {
803 	int rc = 0;
804 
805 	if (argc < 1) {
806 		lprintf(LOG_ERR, "Not enough parameters given.");
807 		printf_mc_usage();
808 		rc = (-1);
809 	}
810 	else if (strncmp(argv[0], "help", 4) == 0) {
811 		printf_mc_usage();
812 		rc = 0;
813 	}
814 	else if (strncmp(argv[0], "reset", 5) == 0) {
815 		if (argc < 2) {
816 			lprintf(LOG_ERR, "Not enough parameters given.");
817 			printf_mc_reset_usage();
818 			rc = (-1);
819 		}
820 		else if (strncmp(argv[1], "help", 4) == 0) {
821 			printf_mc_reset_usage();
822 			rc = 0;
823 		}
824 		else if (strncmp(argv[1], "cold", 4) == 0) {
825 			rc = ipmi_mc_reset(intf, BMC_COLD_RESET);
826 		}
827 		else if (strncmp(argv[1], "warm", 4) == 0) {
828 			rc = ipmi_mc_reset(intf, BMC_WARM_RESET);
829 		}
830 		else {
831 			lprintf(LOG_ERR, "Invalid mc/bmc %s command: %s", argv[0], argv[1]);
832 			printf_mc_reset_usage();
833 			rc = (-1);
834 		}
835 	}
836 	else if (strncmp(argv[0], "info", 4) == 0) {
837 		rc = ipmi_mc_get_deviceid(intf);
838 	}
839 	else if (strncmp(argv[0], "guid", 4) == 0) {
840 		rc = ipmi_mc_get_guid(intf);
841 	}
842 	else if (strncmp(argv[0], "getenables", 10) == 0) {
843 		rc = ipmi_mc_get_enables(intf);
844 	}
845 	else if (strncmp(argv[0], "setenables", 10) == 0) {
846 		rc = ipmi_mc_set_enables(intf, argc-1, &(argv[1]));
847 	}
848 	else if (!strncmp(argv[0], "selftest", 8)) {
849 		rc = ipmi_mc_get_selftest(intf);
850 	}
851 	else if (!strncmp(argv[0], "watchdog", 8)) {
852 		if (argc < 2) {
853 			lprintf(LOG_ERR, "Not enough parameters given.");
854 			print_watchdog_usage();
855 			rc = (-1);
856 		}
857 		else if (strncmp(argv[1], "help", 4) == 0) {
858 			print_watchdog_usage();
859 			rc = 0;
860 		}
861 		else if (strncmp(argv[1], "get", 3) == 0) {
862 			rc = ipmi_mc_get_watchdog(intf);
863 		}
864 		else if(strncmp(argv[1], "off", 3) == 0) {
865 			rc = ipmi_mc_shutoff_watchdog(intf);
866 		}
867 		else if(strncmp(argv[1], "reset", 5) == 0) {
868 			rc = ipmi_mc_rst_watchdog(intf);
869 		}
870 		else {
871 			lprintf(LOG_ERR, "Invalid mc/bmc %s command: %s", argv[0], argv[1]);
872 			print_watchdog_usage();
873 			rc = (-1);
874 		}
875 	}
876 	else if (strncmp(argv[0], "getsysinfo", 10) == 0) {
877 		rc = ipmi_sysinfo_main(intf, argc, argv, 0);
878 	}
879 	else if (strncmp(argv[0], "setsysinfo", 10) == 0) {
880 		rc = ipmi_sysinfo_main(intf, argc, argv, 1);
881 	}
882 	else {
883 		lprintf(LOG_ERR, "Invalid mc/bmc command: %s", argv[0]);
884 		printf_mc_usage();
885 		rc = (-1);
886 	}
887 	return rc;
888 }
889 
890 /*
891  * sysinfo_param() - function converts sysinfo param to int
892  *
893  * @str - user input string
894  * @maxset - ?
895  *
896  * returns (-1) on error
897  * returns > 0  on success
898  */
899 static int
sysinfo_param(const char * str,int * maxset)900 sysinfo_param(const char *str, int *maxset)
901 {
902 	if (!str || !maxset)
903 		return (-1);
904 
905 	*maxset = 4;
906 	if (!strcmp(str, "system_name"))
907 		return IPMI_SYSINFO_HOSTNAME;
908 	else if (!strcmp(str, "primary_os_name"))
909 		return IPMI_SYSINFO_PRIMARY_OS_NAME;
910 	else if (!strcmp(str, "os_name"))
911 		return IPMI_SYSINFO_OS_NAME;
912 	else if (!strcmp(str, "delloem_os_version"))
913 		return IPMI_SYSINFO_DELL_OS_VERSION;
914 	else if (!strcmp(str, "delloem_url")) {
915 		*maxset = 2;
916 		return IPMI_SYSINFO_DELL_URL;
917 	} else if (!strcmp(str, "system_fw_version")) {
918 		return IPMI_SYSINFO_SYSTEM_FW_VERSION;
919 	}
920 
921 	return (-1);
922 }
923 
924 /*
925  * ipmi_mc_getsysinfo() - function processes the IPMI Get System Info command
926  *
927  * @intf - ipmi interface
928  * @param - parameter eg. 0xC0..0xFF = OEM
929  * @block - number of block parameters
930  * @set - number of set parameters
931  * @len - length of buffer
932  * @buffer - pointer to buffer
933  *
934  * returns (-1) on failure
935  * returns   0  on success
936  * returns > 0  IPMI code
937  */
938 int
ipmi_mc_getsysinfo(struct ipmi_intf * intf,int param,int block,int set,int len,void * buffer)939 ipmi_mc_getsysinfo(struct ipmi_intf * intf, int param, int block, int set,
940 		int len, void *buffer)
941 {
942 	uint8_t data[4];
943 	struct ipmi_rs *rsp = NULL;
944 	struct ipmi_rq req = {0};
945 
946 	memset(buffer, 0, len);
947 	memset(data, 0, 4);
948 	req.msg.netfn = IPMI_NETFN_APP;
949 	req.msg.lun = 0;
950 	req.msg.cmd = IPMI_GET_SYS_INFO;
951 	req.msg.data_len = 4;
952 	req.msg.data = data;
953 
954 	if (verbose > 1)
955 		printf("getsysinfo: %.2x/%.2x/%.2x\n", param, block, set);
956 
957 	data[0] = 0; /* get/set */
958 	data[1] = param;
959 	data[2] = block;
960 	data[3] = set;
961 
962 	/*
963 	 * Format of get output is:
964 	 *   u8 param_rev
965 	 *   u8 selector
966 	 *   u8 encoding  bit[0-3];
967 	 *   u8 length
968 	 *   u8 data0[14]
969 	 */
970 	rsp = intf->sendrecv(intf, &req);
971 	if (rsp == NULL)
972 		return (-1);
973 
974 	if (rsp->ccode == 0) {
975 		if (len > rsp->data_len)
976 			len = rsp->data_len;
977 		if (len && buffer)
978 			memcpy(buffer, rsp->data, len);
979 	}
980 	return rsp->ccode;
981 }
982 
983 /*
984  * ipmi_mc_setsysinfo() - function processes the IPMI Set System Info command
985  *
986  * @intf - ipmi interface
987  * @len - length of buffer
988  * @buffer - pointer to buffer
989  *
990  * returns (-1) on failure
991  * returns   0  on success
992  * returns > 0  IPMI code
993  */
994 int
ipmi_mc_setsysinfo(struct ipmi_intf * intf,int len,void * buffer)995 ipmi_mc_setsysinfo(struct ipmi_intf * intf, int len, void *buffer)
996 {
997 	struct ipmi_rs *rsp = NULL;
998 	struct ipmi_rq req = {0};
999 
1000 	req.msg.netfn = IPMI_NETFN_APP;
1001 	req.msg.lun = 0;
1002 	req.msg.cmd = IPMI_SET_SYS_INFO;
1003 	req.msg.data_len = len;
1004 	req.msg.data = buffer;
1005 
1006 	/*
1007 	 * Format of set input:
1008 	 *   u8 param rev
1009 	 *   u8 selector
1010 	 *   u8 data1[16]
1011 	 */
1012 	rsp = intf->sendrecv(intf, &req);
1013 	if (rsp != NULL) {
1014 		return rsp->ccode;
1015 	}
1016 	return -1;
1017 }
1018 
1019 static int
ipmi_sysinfo_main(struct ipmi_intf * intf,int argc,char ** argv,int is_set)1020 ipmi_sysinfo_main(struct ipmi_intf *intf, int argc, char ** argv, int is_set)
1021 {
1022 	char *str;
1023 	unsigned char  infostr[256];
1024 	unsigned char  paramdata[18];
1025 	int len, maxset, param, pos, rc, set;
1026 
1027 	if (argc == 2 && strcmp(argv[1], "help") == 0) {
1028 		printf_sysinfo_usage(1);
1029 		return 0;
1030 	}
1031 	else if (argc < 2 || (is_set == 1 && argc < 3)) {
1032 		lprintf(LOG_ERR, "Not enough parameters given.");
1033 		printf_sysinfo_usage(1);
1034 		return (-1);
1035 	}
1036 
1037 	/* Get Parameters */
1038 	if ((param = sysinfo_param(argv[1], &maxset)) < 0) {
1039 		lprintf(LOG_ERR, "Invalid mc/bmc %s command: %s", argv[0], argv[1]);
1040 		printf_sysinfo_usage(1);
1041 		return (-1);
1042 	}
1043 
1044 	rc = 0;
1045 	if (is_set != 0) {
1046 		str = argv[2];
1047 		set = pos = 0;
1048 		len = strlen(str);
1049 
1050 		/* first block holds 14 bytes, all others hold 16 */
1051 		if ((len + 2 + 15) / 16 >= maxset)
1052 			len = (maxset * 16) - 2;
1053 
1054 		do {
1055 			memset(paramdata, 0, sizeof(paramdata));
1056 			paramdata[0] = param;
1057 			paramdata[1] = set;
1058 			if (set == 0) {
1059 				/* First block is special case */
1060 				paramdata[2] = 0;   /* ascii encoding */
1061 				paramdata[3] = len; /* length */
1062 				strncpy(paramdata + 4, str + pos, IPMI_SYSINFO_SET0_SIZE);
1063 				pos += IPMI_SYSINFO_SET0_SIZE;
1064 			}
1065 			else {
1066 				strncpy(paramdata + 2, str + pos, IPMI_SYSINFO_SETN_SIZE);
1067 				pos += IPMI_SYSINFO_SETN_SIZE;
1068 			}
1069 			rc = ipmi_mc_setsysinfo(intf, 18, paramdata);
1070 
1071 			if (rc)
1072 				break;
1073 
1074 			set++;
1075 		} while (pos < len);
1076 	}
1077 	else {
1078 		memset(infostr, 0, sizeof(infostr));
1079 		/* Read blocks of data */
1080 		pos = 0;
1081 		for (set = 0; set < maxset; set++) {
1082 			rc = ipmi_mc_getsysinfo(intf, param, set, 0, 18, paramdata);
1083 
1084 			if (rc)
1085 				break;
1086 
1087 			if (set == 0) {
1088 				/* First block is special case */
1089 				if ((paramdata[2] & 0xF) == 0) {
1090 					/* Determine max number of blocks to read */
1091 					maxset = ((paramdata[3] + 2) + 15) / 16;
1092 				}
1093 				memcpy(infostr + pos, paramdata + 4, IPMI_SYSINFO_SET0_SIZE);
1094 				pos += IPMI_SYSINFO_SET0_SIZE;
1095 			}
1096 			else {
1097 				memcpy(infostr + pos, paramdata + 2, IPMI_SYSINFO_SETN_SIZE);
1098 				pos += IPMI_SYSINFO_SETN_SIZE;
1099 			}
1100 		}
1101 		printf("%s\n", infostr);
1102 	}
1103 	if (rc < 0) {
1104 		lprintf(LOG_ERR, "%s %s set %d command failed", argv[0], argv[1], set);
1105 	}
1106 	else if (rc == 0x80) {
1107 		lprintf(LOG_ERR, "%s %s parameter not supported", argv[0], argv[1]);
1108 	}
1109 	else if (rc > 0) {
1110 		lprintf(LOG_ERR, "%s command failed: %s", argv[0],
1111 				val2str(rc, completion_code_vals));
1112 	}
1113 	return rc;
1114 }
1115