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 /* _ipmi_mc_get_guid - Gets BMCs GUID according to (22.14)
466  *
467  * @intf:	ipmi interface
468  * @guid:       pointer where to store BMC GUID
469  *
470  * returns - negative number means error, positive is a ccode.
471  */
472 int
_ipmi_mc_get_guid(struct ipmi_intf * intf,struct ipmi_guid_t * guid)473 _ipmi_mc_get_guid(struct ipmi_intf *intf, struct ipmi_guid_t *guid)
474 {
475 	struct ipmi_rs *rsp;
476 	struct ipmi_rq req;
477 	if (guid == NULL) {
478 		return (-3);
479 	}
480 
481 	memset(guid, 0, sizeof(struct ipmi_guid_t));
482 	memset(&req, 0, sizeof(req));
483 	req.msg.netfn = IPMI_NETFN_APP;
484 	req.msg.cmd = BMC_GET_GUID;
485 
486 	rsp = intf->sendrecv(intf, &req);
487 	if (rsp == NULL) {
488 		return (-1);
489 	} else if (rsp->ccode > 0) {
490 		return rsp->ccode;
491 	} else if (rsp->data_len != 16
492 			|| rsp->data_len != sizeof(struct ipmi_guid_t)) {
493 		return (-2);
494 	}
495 	memcpy(guid, &rsp->data[0], sizeof(struct ipmi_guid_t));
496 	return 0;
497 }
498 
499 /* ipmi_mc_print_guid - print-out given BMC GUID
500  *
501  * @guid - struct with GUID.
502  *
503  * returns 0
504  */
505 static int
ipmi_mc_print_guid(struct ipmi_guid_t guid)506 ipmi_mc_print_guid(struct ipmi_guid_t guid)
507 {
508 	char tbuf[40];
509 	time_t s;
510 	memset(tbuf, 0, 40);
511 	/* Kipp - changed order of last field (node) to follow specification */
512 	printf("System GUID  : %08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x\n",
513 	       guid.time_low, guid.time_mid, guid.time_hi_and_version,
514 	       guid.clock_seq_hi_variant << 8 | guid.clock_seq_low,
515 	       guid.node[0], guid.node[1], guid.node[2],
516 	       guid.node[3], guid.node[4], guid.node[5]);
517 
518 	s = (time_t)guid.time_low; /* Kipp - removed the BSWAP_32, it was not needed here */
519 	strftime(tbuf, sizeof(tbuf), "%m/%d/%Y %H:%M:%S", localtime(&s));
520 	printf("Timestamp    : %s\n", tbuf);
521 	return 0;
522 }
523 
524 /* ipmi_mc_get_guid - Gets and prints-out System GUID */
525 int
ipmi_mc_get_guid(struct ipmi_intf * intf)526 ipmi_mc_get_guid(struct ipmi_intf *intf)
527 {
528 	struct ipmi_guid_t guid;
529 	int rc;
530 	rc = _ipmi_mc_get_guid(intf, &guid);
531 	if (eval_ccode(rc) != 0) {
532 		return (-1);
533 	}
534 	rc = ipmi_mc_print_guid(guid);
535 	return rc;
536 }
537 
538 /* ipmi_mc_get_selftest -  returns and print selftest results
539  *
540  * @intf:	ipmi interface
541  */
ipmi_mc_get_selftest(struct ipmi_intf * intf)542 static int ipmi_mc_get_selftest(struct ipmi_intf * intf)
543 {
544    int rv = 0;
545 	struct ipmi_rs * rsp;
546 	struct ipmi_rq req;
547 	struct ipm_selftest_rsp *sft_res;
548 
549 	memset(&req, 0, sizeof(req));
550 	req.msg.netfn = IPMI_NETFN_APP;
551 	req.msg.cmd = BMC_GET_SELF_TEST;
552 	req.msg.data_len = 0;
553 
554 	rsp = intf->sendrecv(intf, &req);
555 	if (!rsp) {
556 		lprintf(LOG_ERR, "No response from devices\n");
557 		return -1;
558 	}
559 
560 	if (rsp->ccode) {
561 		lprintf(LOG_ERR, "Bad response: (%s)",
562 				val2str(rsp->ccode, completion_code_vals));
563 		return -1;
564 	}
565 
566 	sft_res = (struct ipm_selftest_rsp *) rsp->data;
567 
568 	if (sft_res->code == IPM_SFT_CODE_OK) {
569 		printf("Selftest: passed\n");
570 		rv = 0;
571 	}
572 
573 	else if (sft_res->code == IPM_SFT_CODE_NOT_IMPLEMENTED) {
574 		printf("Selftest: not implemented\n");
575 		rv = -1;
576 	}
577 
578 	else if (sft_res->code == IPM_SFT_CODE_DEV_CORRUPTED) {
579 		printf("Selftest: device corrupted\n");
580 		rv = -1;
581 
582 		if (sft_res->test & IPM_SELFTEST_SEL_ERROR) {
583 			printf(" -> SEL device not accessible\n");
584 		}
585 		if (sft_res->test & IPM_SELFTEST_SDR_ERROR) {
586 			printf(" -> SDR repository not accesible\n");
587 		}
588 		if (sft_res->test & IPM_SELFTEST_FRU_ERROR) {
589 			printf("FRU device not accessible\n");
590 		}
591 		if (sft_res->test & IPM_SELFTEST_IPMB_ERROR) {
592 			printf("IPMB signal lines do not respond\n");
593 		}
594 		if (sft_res->test & IPM_SELFTEST_SDRR_EMPTY) {
595 			printf("SDR repository empty\n");
596 		}
597 		if (sft_res->test & IPM_SELFTEST_INTERNAL_USE) {
598 			printf("Internal Use Area corrupted\n");
599 		}
600 		if (sft_res->test & IPM_SELFTEST_FW_BOOTBLOCK) {
601 			printf("Controller update boot block corrupted\n");
602 		}
603 		if (sft_res->test & IPM_SELFTEST_FW_CORRUPTED) {
604 			printf("controller operational firmware corrupted\n");
605 		}
606 	}
607 
608 	else if (sft_res->code == IPM_SFT_CODE_FATAL_ERROR) {
609 		printf("Selftest     : fatal error\n");
610 		printf("Failure code : %02x\n", sft_res->test);
611 		rv = -1;
612 	}
613 
614 	else if (sft_res->code == IPM_SFT_CODE_RESERVED) {
615 		printf("Selftest: N/A");
616 		rv = -1;
617 	}
618 
619 	else {
620 		printf("Selftest     : device specific (%02Xh)\n", sft_res->code);
621 		printf("Failure code : %02Xh\n", sft_res->test);
622 		rv = 0;
623 	}
624 
625 	return rv;
626 }
627 
628 /* ipmi_mc_get_watchdog
629  *
630  * @intf:	ipmi interface
631  *
632  * returns 0 on success
633  * returns -1 on error
634  */
635 
636 const char *wdt_use_string[8] = {
637 	"Reserved",
638 	"BIOS FRB2",
639 	"BIOS/POST",
640 	"OS Load",
641 	"SMS/OS",
642 	"OEM",
643 	"Reserved",
644 	"Reserved"
645 };
646 
647 const char *wdt_action_string[8] = {
648 	"No action",
649 	"Hard Reset",
650 	"Power Down",
651 	"Power Cycle",
652 	"Reserved",
653 	"Reserved",
654 	"Reserved",
655 	"Reserved"
656 };
657 
658 static int
ipmi_mc_get_watchdog(struct ipmi_intf * intf)659 ipmi_mc_get_watchdog(struct ipmi_intf * intf)
660 {
661 	struct ipmi_rs * rsp;
662 	struct ipmi_rq req;
663 	struct ipm_get_watchdog_rsp * wdt_res;
664 
665 	memset(&req, 0, sizeof(req));
666 	req.msg.netfn = IPMI_NETFN_APP;
667 	req.msg.cmd = BMC_GET_WATCHDOG_TIMER;
668 	req.msg.data_len = 0;
669 
670 	rsp = intf->sendrecv(intf, &req);
671 	if (rsp == NULL) {
672 		lprintf(LOG_ERR, "Get Watchdog Timer command failed");
673 		return -1;
674 	}
675 
676 	if (rsp->ccode) {
677 		lprintf(LOG_ERR, "Get Watchdog Timer command failed: %s",
678 			val2str(rsp->ccode, completion_code_vals));
679 		return -1;
680 	}
681 
682 	wdt_res = (struct ipm_get_watchdog_rsp *) rsp->data;
683 
684 	printf("Watchdog Timer Use:     %s (0x%02x)\n",
685 			wdt_use_string[(wdt_res->timer_use & 0x07 )], wdt_res->timer_use);
686 	printf("Watchdog Timer Is:      %s\n",
687 		wdt_res->timer_use & 0x40 ? "Started/Running" : "Stopped");
688 	printf("Watchdog Timer Actions: %s (0x%02x)\n",
689 		 wdt_action_string[(wdt_res->timer_actions&0x07)], wdt_res->timer_actions);
690 	printf("Pre-timeout interval:   %d seconds\n", wdt_res->pre_timeout);
691 	printf("Timer Expiration Flags: 0x%02x\n", wdt_res->timer_use_exp);
692 	printf("Initial Countdown:      %i sec\n",
693 			((wdt_res->initial_countdown_msb << 8) | wdt_res->initial_countdown_lsb)/10);
694 	printf("Present Countdown:      %i sec\n",
695 			(((wdt_res->present_countdown_msb << 8) | wdt_res->present_countdown_lsb)) / 10);
696 
697 	return 0;
698 }
699 
700 /* ipmi_mc_shutoff_watchdog
701  *
702  * @intf:	ipmi interface
703  *
704  * returns 0 on success
705  * returns -1 on error
706  */
707 static int
ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf)708 ipmi_mc_shutoff_watchdog(struct ipmi_intf * intf)
709 {
710 	struct ipmi_rs * rsp;
711 	struct ipmi_rq req;
712 	unsigned char msg_data[6];
713 
714 	memset(&req, 0, sizeof(req));
715 	req.msg.netfn = IPMI_NETFN_APP;
716 	req.msg.cmd   = BMC_SET_WATCHDOG_TIMER;
717 	req.msg.data  = msg_data;
718 	req.msg.data_len = 6;
719 
720 	/*
721 	 * The only set cmd we're allowing is to shut off the timer.
722 	 * Turning on the timer should be the job of the ipmi watchdog driver.
723 	 * See 'modinfo ipmi_watchdog' for more info. (NOTE: the reset
724 	 * command will restart the timer if it's already been initialized.)
725 	 *
726 	 * Out-of-band watchdog set commands can still be sent via the raw
727 	 * command interface but this is a very dangerous thing to do since
728 	 * a periodic "poke"/reset over a network is unreliable.  This is
729 	 * not a recommended way to use the IPMI watchdog commands.
730 	 */
731 
732 	msg_data[0] = IPM_WATCHDOG_SMS_OS;
733 	msg_data[1] = IPM_WATCHDOG_NO_ACTION;
734 	msg_data[2] = 0x00;  /* pretimeout interval */
735 	msg_data[3] = IPM_WATCHDOG_CLEAR_SMS_OS;
736 	msg_data[4] = 0xb8;  /* countdown lsb (100 ms/count) */
737 	msg_data[5] = 0x0b;  /* countdown msb - 5 mins */
738 
739 	rsp = intf->sendrecv(intf, &req);
740 	if (rsp == NULL) {
741 		lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed!");
742 		return -1;
743 	}
744 
745 	if (rsp->ccode) {
746 		lprintf(LOG_ERR, "Watchdog Timer Shutoff command failed! %s",
747 			val2str(rsp->ccode, completion_code_vals));
748 		return -1;
749 	}
750 
751 	printf("Watchdog Timer Shutoff successful -- timer stopped\n");
752 	return 0;
753 }
754 
755 
756 /* ipmi_mc_rst_watchdog
757  *
758  * @intf:	ipmi interface
759  *
760  * returns 0 on success
761  * returns -1 on error
762  */
763 static int
ipmi_mc_rst_watchdog(struct ipmi_intf * intf)764 ipmi_mc_rst_watchdog(struct ipmi_intf * intf)
765 {
766 	struct ipmi_rs * rsp;
767 	struct ipmi_rq req;
768 
769 	memset(&req, 0, sizeof(req));
770 	req.msg.netfn = IPMI_NETFN_APP;
771 	req.msg.cmd   = BMC_RESET_WATCHDOG_TIMER;
772 	req.msg.data_len = 0;
773 
774 	rsp = intf->sendrecv(intf, &req);
775 	if (rsp == NULL) {
776 		lprintf(LOG_ERR, "Reset Watchdog Timer command failed!");
777 		return -1;
778 	}
779 
780 	if (rsp->ccode) {
781 		lprintf(LOG_ERR, "Reset Watchdog Timer command failed: %s",
782 			(rsp->ccode == IPM_WATCHDOG_RESET_ERROR) ?
783 				"Attempt to reset uninitialized watchdog" :
784 				val2str(rsp->ccode, completion_code_vals));
785 		return -1;
786 	}
787 
788 	printf("IPMI Watchdog Timer Reset -  countdown restarted!\n");
789 	return 0;
790 }
791 
792 /* ipmi_mc_main  -  top-level handler for MC functions
793  *
794  * @intf:	ipmi interface
795  * @argc:	number of arguments
796  * @argv:	argument list
797  *
798  * returns 0 on success
799  * returns -1 on error
800  */
801 int
ipmi_mc_main(struct ipmi_intf * intf,int argc,char ** argv)802 ipmi_mc_main(struct ipmi_intf * intf, int argc, char ** argv)
803 {
804 	int rc = 0;
805 
806 	if (argc < 1) {
807 		lprintf(LOG_ERR, "Not enough parameters given.");
808 		printf_mc_usage();
809 		rc = (-1);
810 	}
811 	else if (strncmp(argv[0], "help", 4) == 0) {
812 		printf_mc_usage();
813 		rc = 0;
814 	}
815 	else if (strncmp(argv[0], "reset", 5) == 0) {
816 		if (argc < 2) {
817 			lprintf(LOG_ERR, "Not enough parameters given.");
818 			printf_mc_reset_usage();
819 			rc = (-1);
820 		}
821 		else if (strncmp(argv[1], "help", 4) == 0) {
822 			printf_mc_reset_usage();
823 			rc = 0;
824 		}
825 		else if (strncmp(argv[1], "cold", 4) == 0) {
826 			rc = ipmi_mc_reset(intf, BMC_COLD_RESET);
827 		}
828 		else if (strncmp(argv[1], "warm", 4) == 0) {
829 			rc = ipmi_mc_reset(intf, BMC_WARM_RESET);
830 		}
831 		else {
832 			lprintf(LOG_ERR, "Invalid mc/bmc %s command: %s", argv[0], argv[1]);
833 			printf_mc_reset_usage();
834 			rc = (-1);
835 		}
836 	}
837 	else if (strncmp(argv[0], "info", 4) == 0) {
838 		rc = ipmi_mc_get_deviceid(intf);
839 	}
840 	else if (strncmp(argv[0], "guid", 4) == 0) {
841 		rc = ipmi_mc_get_guid(intf);
842 	}
843 	else if (strncmp(argv[0], "getenables", 10) == 0) {
844 		rc = ipmi_mc_get_enables(intf);
845 	}
846 	else if (strncmp(argv[0], "setenables", 10) == 0) {
847 		rc = ipmi_mc_set_enables(intf, argc-1, &(argv[1]));
848 	}
849 	else if (!strncmp(argv[0], "selftest", 8)) {
850 		rc = ipmi_mc_get_selftest(intf);
851 	}
852 	else if (!strncmp(argv[0], "watchdog", 8)) {
853 		if (argc < 2) {
854 			lprintf(LOG_ERR, "Not enough parameters given.");
855 			print_watchdog_usage();
856 			rc = (-1);
857 		}
858 		else if (strncmp(argv[1], "help", 4) == 0) {
859 			print_watchdog_usage();
860 			rc = 0;
861 		}
862 		else if (strncmp(argv[1], "get", 3) == 0) {
863 			rc = ipmi_mc_get_watchdog(intf);
864 		}
865 		else if(strncmp(argv[1], "off", 3) == 0) {
866 			rc = ipmi_mc_shutoff_watchdog(intf);
867 		}
868 		else if(strncmp(argv[1], "reset", 5) == 0) {
869 			rc = ipmi_mc_rst_watchdog(intf);
870 		}
871 		else {
872 			lprintf(LOG_ERR, "Invalid mc/bmc %s command: %s", argv[0], argv[1]);
873 			print_watchdog_usage();
874 			rc = (-1);
875 		}
876 	}
877 	else if (strncmp(argv[0], "getsysinfo", 10) == 0) {
878 		rc = ipmi_sysinfo_main(intf, argc, argv, 0);
879 	}
880 	else if (strncmp(argv[0], "setsysinfo", 10) == 0) {
881 		rc = ipmi_sysinfo_main(intf, argc, argv, 1);
882 	}
883 	else {
884 		lprintf(LOG_ERR, "Invalid mc/bmc command: %s", argv[0]);
885 		printf_mc_usage();
886 		rc = (-1);
887 	}
888 	return rc;
889 }
890 
891 /*
892  * sysinfo_param() - function converts sysinfo param to int
893  *
894  * @str - user input string
895  * @maxset - ?
896  *
897  * returns (-1) on error
898  * returns > 0  on success
899  */
900 static int
sysinfo_param(const char * str,int * maxset)901 sysinfo_param(const char *str, int *maxset)
902 {
903 	if (!str || !maxset)
904 		return (-1);
905 
906 	*maxset = 4;
907 	if (!strcmp(str, "system_name"))
908 		return IPMI_SYSINFO_HOSTNAME;
909 	else if (!strcmp(str, "primary_os_name"))
910 		return IPMI_SYSINFO_PRIMARY_OS_NAME;
911 	else if (!strcmp(str, "os_name"))
912 		return IPMI_SYSINFO_OS_NAME;
913 	else if (!strcmp(str, "delloem_os_version"))
914 		return IPMI_SYSINFO_DELL_OS_VERSION;
915 	else if (!strcmp(str, "delloem_url")) {
916 		*maxset = 2;
917 		return IPMI_SYSINFO_DELL_URL;
918 	} else if (!strcmp(str, "system_fw_version")) {
919 		return IPMI_SYSINFO_SYSTEM_FW_VERSION;
920 	}
921 
922 	return (-1);
923 }
924 
925 /*
926  * ipmi_mc_getsysinfo() - function processes the IPMI Get System Info command
927  *
928  * @intf - ipmi interface
929  * @param - parameter eg. 0xC0..0xFF = OEM
930  * @block - number of block parameters
931  * @set - number of set parameters
932  * @len - length of buffer
933  * @buffer - pointer to buffer
934  *
935  * returns (-1) on failure
936  * returns   0  on success
937  * returns > 0  IPMI code
938  */
939 int
ipmi_mc_getsysinfo(struct ipmi_intf * intf,int param,int block,int set,int len,void * buffer)940 ipmi_mc_getsysinfo(struct ipmi_intf * intf, int param, int block, int set,
941 		int len, void *buffer)
942 {
943 	uint8_t data[4];
944 	struct ipmi_rs *rsp = NULL;
945 	struct ipmi_rq req = {0};
946 
947 	memset(buffer, 0, len);
948 	memset(data, 0, 4);
949 	req.msg.netfn = IPMI_NETFN_APP;
950 	req.msg.lun = 0;
951 	req.msg.cmd = IPMI_GET_SYS_INFO;
952 	req.msg.data_len = 4;
953 	req.msg.data = data;
954 
955 	if (verbose > 1)
956 		printf("getsysinfo: %.2x/%.2x/%.2x\n", param, block, set);
957 
958 	data[0] = 0; /* get/set */
959 	data[1] = param;
960 	data[2] = block;
961 	data[3] = set;
962 
963 	/*
964 	 * Format of get output is:
965 	 *   u8 param_rev
966 	 *   u8 selector
967 	 *   u8 encoding  bit[0-3];
968 	 *   u8 length
969 	 *   u8 data0[14]
970 	 */
971 	rsp = intf->sendrecv(intf, &req);
972 	if (rsp == NULL)
973 		return (-1);
974 
975 	if (rsp->ccode == 0) {
976 		if (len > rsp->data_len)
977 			len = rsp->data_len;
978 		if (len && buffer)
979 			memcpy(buffer, rsp->data, len);
980 	}
981 	return rsp->ccode;
982 }
983 
984 /*
985  * ipmi_mc_setsysinfo() - function processes the IPMI Set System Info command
986  *
987  * @intf - ipmi interface
988  * @len - length of buffer
989  * @buffer - pointer to buffer
990  *
991  * returns (-1) on failure
992  * returns   0  on success
993  * returns > 0  IPMI code
994  */
995 int
ipmi_mc_setsysinfo(struct ipmi_intf * intf,int len,void * buffer)996 ipmi_mc_setsysinfo(struct ipmi_intf * intf, int len, void *buffer)
997 {
998 	struct ipmi_rs *rsp = NULL;
999 	struct ipmi_rq req = {0};
1000 
1001 	req.msg.netfn = IPMI_NETFN_APP;
1002 	req.msg.lun = 0;
1003 	req.msg.cmd = IPMI_SET_SYS_INFO;
1004 	req.msg.data_len = len;
1005 	req.msg.data = buffer;
1006 
1007 	/*
1008 	 * Format of set input:
1009 	 *   u8 param rev
1010 	 *   u8 selector
1011 	 *   u8 data1[16]
1012 	 */
1013 	rsp = intf->sendrecv(intf, &req);
1014 	if (rsp != NULL) {
1015 		return rsp->ccode;
1016 	}
1017 	return -1;
1018 }
1019 
1020 static int
ipmi_sysinfo_main(struct ipmi_intf * intf,int argc,char ** argv,int is_set)1021 ipmi_sysinfo_main(struct ipmi_intf *intf, int argc, char ** argv, int is_set)
1022 {
1023 	char *str;
1024 	unsigned char  infostr[256];
1025 	unsigned char  paramdata[18];
1026 	int len, maxset, param, pos, rc, set;
1027 
1028 	if (argc == 2 && strcmp(argv[1], "help") == 0) {
1029 		printf_sysinfo_usage(1);
1030 		return 0;
1031 	}
1032 	else if (argc < 2 || (is_set == 1 && argc < 3)) {
1033 		lprintf(LOG_ERR, "Not enough parameters given.");
1034 		printf_sysinfo_usage(1);
1035 		return (-1);
1036 	}
1037 
1038 	/* Get Parameters */
1039 	if ((param = sysinfo_param(argv[1], &maxset)) < 0) {
1040 		lprintf(LOG_ERR, "Invalid mc/bmc %s command: %s", argv[0], argv[1]);
1041 		printf_sysinfo_usage(1);
1042 		return (-1);
1043 	}
1044 
1045 	rc = 0;
1046 	if (is_set != 0) {
1047 		str = argv[2];
1048 		set = pos = 0;
1049 		len = strlen(str);
1050 
1051 		/* first block holds 14 bytes, all others hold 16 */
1052 		if ((len + 2 + 15) / 16 >= maxset)
1053 			len = (maxset * 16) - 2;
1054 
1055 		do {
1056 			memset(paramdata, 0, sizeof(paramdata));
1057 			paramdata[0] = param;
1058 			paramdata[1] = set;
1059 			if (set == 0) {
1060 				/* First block is special case */
1061 				paramdata[2] = 0;   /* ascii encoding */
1062 				paramdata[3] = len; /* length */
1063 				strncpy(paramdata + 4, str + pos, IPMI_SYSINFO_SET0_SIZE);
1064 				pos += IPMI_SYSINFO_SET0_SIZE;
1065 			}
1066 			else {
1067 				strncpy(paramdata + 2, str + pos, IPMI_SYSINFO_SETN_SIZE);
1068 				pos += IPMI_SYSINFO_SETN_SIZE;
1069 			}
1070 			rc = ipmi_mc_setsysinfo(intf, 18, paramdata);
1071 
1072 			if (rc)
1073 				break;
1074 
1075 			set++;
1076 		} while (pos < len);
1077 	}
1078 	else {
1079 		memset(infostr, 0, sizeof(infostr));
1080 		/* Read blocks of data */
1081 		pos = 0;
1082 		for (set = 0; set < maxset; set++) {
1083 			rc = ipmi_mc_getsysinfo(intf, param, set, 0, 18, paramdata);
1084 
1085 			if (rc)
1086 				break;
1087 
1088 			if (set == 0) {
1089 				/* First block is special case */
1090 				if ((paramdata[2] & 0xF) == 0) {
1091 					/* Determine max number of blocks to read */
1092 					maxset = ((paramdata[3] + 2) + 15) / 16;
1093 				}
1094 				memcpy(infostr + pos, paramdata + 4, IPMI_SYSINFO_SET0_SIZE);
1095 				pos += IPMI_SYSINFO_SET0_SIZE;
1096 			}
1097 			else {
1098 				memcpy(infostr + pos, paramdata + 2, IPMI_SYSINFO_SETN_SIZE);
1099 				pos += IPMI_SYSINFO_SETN_SIZE;
1100 			}
1101 		}
1102 		printf("%s\n", infostr);
1103 	}
1104 	if (rc < 0) {
1105 		lprintf(LOG_ERR, "%s %s set %d command failed", argv[0], argv[1], set);
1106 	}
1107 	else if (rc == 0x80) {
1108 		lprintf(LOG_ERR, "%s %s parameter not supported", argv[0], argv[1]);
1109 	}
1110 	else if (rc > 0) {
1111 		lprintf(LOG_ERR, "%s command failed: %s", argv[0],
1112 				val2str(rc, completion_code_vals));
1113 	}
1114 	return rc;
1115 }
1116