xref: /openbmc/ipmitool/src/plugins/open/open.c (revision 6ca88cb6)
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 <stdio.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/ioctl.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 
43 #include <ipmitool/ipmi.h>
44 #include <ipmitool/ipmi_intf.h>
45 #include <ipmitool/helper.h>
46 #include <ipmitool/log.h>
47 
48 #if defined(HAVE_CONFIG_H)
49 # include <config.h>
50 #endif
51 
52 #if defined(HAVE_SYS_IOCCOM_H)
53 # include <sys/ioccom.h>
54 #endif
55 
56 #if defined(HAVE_OPENIPMI_H)
57 # if defined(HAVE_LINUX_COMPILER_H)
58 #  include <linux/compiler.h>
59 # endif
60 # include <linux/ipmi.h>
61 #elif defined(HAVE_FREEBSD_IPMI_H)
62 /* FreeBSD OpenIPMI-compatible header */
63 # include <sys/ipmi.h>
64 #else
65 # include "open.h"
66 #endif
67 
68 /**
69  * Maximum input message size for KCS/SMIC is 40 with 2 utility bytes and
70  * 38 bytes of data.
71  * Maximum input message size for BT is 42 with 4 utility bytes and
72  * 38 bytes of data.
73  */
74 #define IPMI_OPENIPMI_MAX_RQ_DATA_SIZE 38
75 
76 /**
77  * Maximum output message size for KCS/SMIC is 38 with 2 utility bytes, a byte
78  * for completion code and 35 bytes of data.
79  * Maximum output message size for BT is 40 with 4 utility bytes, a byte
80  * for completion code and 35 bytes of data.
81  */
82 #define IPMI_OPENIPMI_MAX_RS_DATA_SIZE 35
83 
84 extern int verbose;
85 
86 static int
87 ipmi_openipmi_open(struct ipmi_intf * intf)
88 {
89 	int i = 0;
90 
91 	char ipmi_dev[16];
92 	char ipmi_devfs[16];
93 	char ipmi_devfs2[16];
94 	int devnum = 0;
95 
96 	devnum = intf->devnum;
97 
98 	sprintf(ipmi_dev, "/dev/ipmi%d", devnum);
99 	sprintf(ipmi_devfs, "/dev/ipmi/%d", devnum);
100 	sprintf(ipmi_devfs2, "/dev/ipmidev/%d", devnum);
101 	lprintf(LOG_DEBUG, "Using ipmi device %d", devnum);
102 
103 	intf->fd = open(ipmi_dev, O_RDWR);
104 
105 	if (intf->fd < 0) {
106 		intf->fd = open(ipmi_devfs, O_RDWR);
107 		if (intf->fd < 0) {
108 			intf->fd = open(ipmi_devfs2, O_RDWR);
109 		}
110 		if (intf->fd < 0) {
111 			lperror(LOG_ERR, "Could not open device at %s or %s or %s",
112 			ipmi_dev, ipmi_devfs , ipmi_devfs2);
113 			return -1;
114 		}
115 	}
116 
117 	if (ioctl(intf->fd, IPMICTL_SET_GETS_EVENTS_CMD, &i) < 0) {
118 		lperror(LOG_ERR, "Could not enable event receiver");
119 		return -1;
120 	}
121 
122 	intf->opened = 1;
123 
124    /* This is never set to 0, the default is IPMI_BMC_SLAVE_ADDR */
125 	if (intf->my_addr != 0) {
126 		if (intf->set_my_addr(intf, intf->my_addr) < 0) {
127 			lperror(LOG_ERR, "Could not set IPMB address");
128 			return -1;
129 		}
130 		lprintf(LOG_DEBUG, "Set IPMB address to 0x%x",
131 			intf->my_addr );
132 	}
133 
134 	intf->manufacturer_id = ipmi_get_oem(intf);
135 	return intf->fd;
136 }
137 static int
138 ipmi_openipmi_set_my_addr(struct ipmi_intf *intf, uint8_t addr)
139 {
140 	unsigned int a = addr;
141 	if (ioctl(intf->fd, IPMICTL_SET_MY_ADDRESS_CMD, &a) < 0) {
142 		lperror(LOG_ERR, "Could not set IPMB address");
143 		return -1;
144 	}
145 	intf->my_addr = addr;
146 	return 0;
147 }
148 
149 static void
150 ipmi_openipmi_close(struct ipmi_intf * intf)
151 {
152 	if (intf->fd >= 0) {
153 		close(intf->fd);
154 		intf->fd = -1;
155 	}
156 
157 	intf->opened = 0;
158 	intf->manufacturer_id = IPMI_OEM_UNKNOWN;
159 }
160 
161 static struct ipmi_rs *
162 ipmi_openipmi_send_cmd(struct ipmi_intf * intf, struct ipmi_rq * req)
163 {
164 	struct ipmi_recv recv;
165 	struct ipmi_addr addr;
166 	struct ipmi_system_interface_addr bmc_addr = {
167 		addr_type:	IPMI_SYSTEM_INTERFACE_ADDR_TYPE,
168 		channel:	IPMI_BMC_CHANNEL,
169 	};
170 	struct ipmi_ipmb_addr ipmb_addr = {
171 		addr_type:	IPMI_IPMB_ADDR_TYPE,
172 	};
173 	struct ipmi_req _req;
174 	static struct ipmi_rs rsp;
175 	static int curr_seq = 0;
176 	fd_set rset;
177 
178 	uint8_t * data = NULL;
179 	int data_len = 0;
180 
181 
182 	if (intf == NULL || req == NULL)
183 		return NULL;
184 
185 	ipmb_addr.channel = intf->target_channel & 0x0f;
186 
187 	if (intf->opened == 0 && intf->open != NULL)
188 		if (intf->open(intf) < 0)
189 			return NULL;
190 
191 	if (verbose > 2) {
192 		fprintf(stderr, "OpenIPMI Request Message Header:\n");
193 		fprintf(stderr, "  netfn     = 0x%x\n",  req->msg.netfn );
194 		fprintf(stderr, "  cmd       = 0x%x\n", req->msg.cmd);
195 		printbuf(req->msg.data, req->msg.data_len, "OpenIPMI Request Message Data");
196 	}
197 
198 
199 
200 	/*
201 	 * setup and send message
202 	 */
203 
204 	memset(&_req, 0, sizeof(struct ipmi_req));
205 
206 	if (intf->target_addr != 0 &&
207 	    intf->target_addr != intf->my_addr) {
208 		/* use IPMB address if needed */
209 		ipmb_addr.slave_addr = intf->target_addr;
210 		ipmb_addr.lun = req->msg.lun;
211 		lprintf(LOG_DEBUG, "Sending request 0x%x to "
212 			"IPMB target @ 0x%x:0x%x (from 0x%x)",
213 			req->msg.cmd,
214 			intf->target_addr,intf->target_channel, intf->my_addr);
215 
216 		if(intf->transit_addr != 0 && intf->transit_addr != intf->my_addr) {
217 		   uint8_t index = 0;
218 
219 		   lprintf(LOG_DEBUG, "Encapsulating data sent to "
220 			   "end target [0x%02x,0x%02x] using transit [0x%02x,0x%02x] from 0x%x ",
221 			   (0x40 | intf->target_channel),
222 			   intf->target_addr,
223 			   intf->transit_channel,
224 			   intf->transit_addr,
225 			   intf->my_addr
226 			   );
227 
228 		   /* Convert Message to 'Send Message' */
229 		   /* Supplied req : req , internal req : _req  */
230 
231 		   if (verbose > 4) {
232 		      fprintf(stderr, "Converting message:\n");
233 		      fprintf(stderr, "  netfn     = 0x%x\n",  req->msg.netfn );
234 		      fprintf(stderr, "  cmd       = 0x%x\n", req->msg.cmd);
235 		      if (req->msg.data && req->msg.data_len) {
236 			 fprintf(stderr, "  data_len  = %d\n", req->msg.data_len);
237 			 fprintf(stderr, "  data      = %s\n",
238 				 buf2str(req->msg.data,req->msg.data_len));
239 		      }
240 		   }
241 
242 		   /* Modify target address to use 'transit' instead */
243 		   ipmb_addr.slave_addr = intf->transit_addr;
244 		   ipmb_addr.channel    = intf->transit_channel;
245 
246 		   /* FIXME backup "My address" */
247 		   data_len = req->msg.data_len + 8;
248 		   data = malloc(data_len);
249 		   if (data == NULL) {
250 		      lprintf(LOG_ERR, "ipmitool: malloc failure");
251 		      return NULL;
252 		   }
253 
254 		   memset(data, 0, data_len);
255 
256 		   data[index++] = (0x40|intf->target_channel);
257 		   data[index++] = intf->target_addr;
258 		   data[index++] = (  req->msg.netfn << 2 ) |  req->msg.lun ;
259 		   data[index++] = ipmi_csum(data+1, 2);
260 		   data[index++] = 0xFF;    /* normally 0x20 , overwritten by IPMC  */
261 		   data[index++] = ( (0)  << 2) | 0 ;           /* FIXME */
262 		   data[index++] = req->msg.cmd;
263 		   memcpy( (data+index) , req->msg.data, req->msg.data_len);
264 		   index += req->msg.data_len;
265 		   data[index++] = ipmi_csum( (data+4),(req->msg.data_len + 3)  );
266 
267 		   if (verbose > 4) {
268 		      fprintf(stderr, "Encapsulated message:\n");
269 		      fprintf(stderr, "  netfn     = 0x%x\n", IPMI_NETFN_APP  );
270 		      fprintf(stderr, "  cmd       = 0x%x\n", 0x34 );
271 		      if (data && data_len) {
272 			 fprintf(stderr, "  data_len  = %d\n", data_len);
273 			 fprintf(stderr, "  data      = %s\n",
274 				 buf2str(data,data_len));
275 		      }
276 		   }
277 		}
278 		_req.addr = (unsigned char *) &ipmb_addr;
279 		_req.addr_len = sizeof(ipmb_addr);
280 	} else {
281 	   /* otherwise use system interface */
282 	   lprintf(LOG_DEBUG+2, "Sending request 0x%x to "
283 		   "System Interface", req->msg.cmd);
284 	   bmc_addr.lun = req->msg.lun;
285 	   _req.addr = (unsigned char *) &bmc_addr;
286 	   _req.addr_len = sizeof(bmc_addr);
287 	}
288 
289 	_req.msgid = curr_seq++;
290 
291 	/* In case of a bridge request */
292 	if( data != NULL && data_len != 0 ) {
293 	   _req.msg.data = data;
294 	   _req.msg.data_len = data_len;
295 	   _req.msg.netfn = IPMI_NETFN_APP;
296 	   _req.msg.cmd   = 0x34;
297 
298 	} else {
299 	   _req.msg.data = req->msg.data;
300 	   _req.msg.data_len = req->msg.data_len;
301 	   _req.msg.netfn = req->msg.netfn;
302 	   _req.msg.cmd = req->msg.cmd;
303 	}
304 
305 	if (ioctl(intf->fd, IPMICTL_SEND_COMMAND, &_req) < 0) {
306 	   lperror(LOG_ERR, "Unable to send command");
307 	   if (data != NULL) {
308 	      free(data);
309 				data = NULL;
310 		 }
311 	   return NULL;
312 	}
313 
314 	/*
315 	 * wait for and retrieve response
316 	 */
317 
318 	if (intf->noanswer) {
319 	   if (data != NULL) {
320 	      free(data);
321 				data = NULL;
322 		 }
323 	   return NULL;
324 	}
325 
326 	FD_ZERO(&rset);
327 	FD_SET(intf->fd, &rset);
328 
329 	if (select(intf->fd+1, &rset, NULL, NULL, NULL) < 0) {
330 	   lperror(LOG_ERR, "I/O Error");
331 	   if (data != NULL) {
332 	      free(data);
333 				data = NULL;
334 		 }
335 	   return NULL;
336 	}
337 	if (FD_ISSET(intf->fd, &rset) == 0) {
338 	   lprintf(LOG_ERR, "No data available");
339 	   if (data != NULL) {
340 	      free(data);
341 				data = NULL;
342 		 }
343 	   return NULL;
344 	}
345 
346 	recv.addr = (unsigned char *) &addr;
347 	recv.addr_len = sizeof(addr);
348 	recv.msg.data = rsp.data;
349 	recv.msg.data_len = sizeof(rsp.data);
350 
351 	/* get data */
352 	if (ioctl(intf->fd, IPMICTL_RECEIVE_MSG_TRUNC, &recv) < 0) {
353 	   lperror(LOG_ERR, "Error receiving message");
354 	   if (errno != EMSGSIZE) {
355 	      if (data != NULL) {
356 					free(data);
357 					data = NULL;
358 				}
359 	      return NULL;
360 	   }
361 	}
362 
363 	if (verbose > 4) {
364 	   fprintf(stderr, "Got message:");
365 	   fprintf(stderr, "  type      = %d\n", recv.recv_type);
366 	   fprintf(stderr, "  channel   = 0x%x\n", addr.channel);
367 	   fprintf(stderr, "  msgid     = %ld\n", recv.msgid);
368 	   fprintf(stderr, "  netfn     = 0x%x\n", recv.msg.netfn);
369 	   fprintf(stderr, "  cmd       = 0x%x\n", recv.msg.cmd);
370 	   if (recv.msg.data && recv.msg.data_len) {
371 	      fprintf(stderr, "  data_len  = %d\n", recv.msg.data_len);
372 	      fprintf(stderr, "  data      = %s\n",
373 		      buf2str(recv.msg.data, recv.msg.data_len));
374 	   }
375 	}
376 
377 	if(intf->transit_addr != 0 && intf->transit_addr != intf->my_addr) {
378 	   uint8_t index = 0;
379 
380 	   /* ipmb_addr.transit_slave_addr = intf->transit_addr; */
381 	   lprintf(LOG_DEBUG, "Decapsulating data received from transit "
382 		   "IPMB target @ 0x%x", intf->transit_addr);
383 
384 	   /* comp code */
385 	   /* Check data */
386 
387 	   if( recv.msg.data[0] == 0 ) {
388 	      recv.msg.netfn = recv.msg.data[2] >> 2;
389 	      recv.msg.cmd   = recv.msg.data[6];
390 
391 	      recv.msg.data = memmove(recv.msg.data ,recv.msg.data+7 , recv.msg.data_len - 7);
392 	      recv.msg.data_len -=8;
393 
394 	      if (verbose > 4) {
395 		 fprintf(stderr, "Decapsulated  message:\n");
396 		 fprintf(stderr, "  netfn     = 0x%x\n",   recv.msg.netfn );
397 		 fprintf(stderr, "  cmd       = 0x%x\n",  recv.msg.cmd);
398 		 if (recv.msg.data && recv.msg.data_len) {
399 		    fprintf(stderr, "  data_len  = %d\n",  recv.msg.data_len);
400 		    fprintf(stderr, "  data      = %s\n",
401 			    buf2str(recv.msg.data,recv.msg.data_len));
402 		 }
403 	      }
404 	   }
405 	}
406 
407 	/* save completion code */
408 	rsp.ccode = recv.msg.data[0];
409 	rsp.data_len = recv.msg.data_len - 1;
410 
411 	/* save response data for caller */
412 	if (rsp.ccode == 0 && rsp.data_len > 0) {
413 	   memmove(rsp.data, rsp.data + 1, rsp.data_len);
414 	   rsp.data[rsp.data_len] = 0;
415 	}
416 
417 	if (data != NULL) {
418 	   free(data);
419 		 data = NULL;
420 	}
421 
422 	return &rsp;
423 }
424 
425 int ipmi_openipmi_setup(struct ipmi_intf * intf)
426 {
427 	/* set default payload size */
428 	intf->max_request_data_size = IPMI_OPENIPMI_MAX_RQ_DATA_SIZE;
429 	intf->max_response_data_size = IPMI_OPENIPMI_MAX_RS_DATA_SIZE;
430 
431 	return 0;
432 }
433 
434 struct ipmi_intf ipmi_open_intf = {
435 	name:		"open",
436 	desc:		"Linux OpenIPMI Interface",
437 	setup:		ipmi_openipmi_setup,
438 	open:		ipmi_openipmi_open,
439 	close:		ipmi_openipmi_close,
440 	sendrecv:	ipmi_openipmi_send_cmd,
441 	set_my_addr:	ipmi_openipmi_set_my_addr,
442 	my_addr:	IPMI_BMC_SLAVE_ADDR,
443 	target_addr:	0, /* init so -m local_addr does not cause bridging */
444 };
445