summaryrefslogtreecommitdiff
path: root/lib/ipmi_raw.c
blob: a8a42a058c807e21f5b0df31fb7eb56544e62390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
 * 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 <string.h>
#include <stdlib.h>
#include <stdio.h>

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

#define IPMI_I2C_MASTER_MAX_SIZE	0x40 /* 64 bytes */

static int is_valid_param(const char *input_param, uint8_t *uchr_ptr,
		const char *label);
int ipmi_spd_print(uint8_t *, int);

/* ipmi_master_write_read  -  Perform I2C write/read transactions
 *
 * This function performs an I2C master write-read function through
 * IPMI interface.  It has a maximum transfer size of 32 bytes.
 *
 * @intf:	ipmi interface
 * @bus:	channel number, i2c bus id and type
 * @addr:	i2c slave address
 * @wdata:	data to write
 * @wsize:	length of data to write (max 64 bytes)
 * @rsize:	length of data to read (max 64 bytes)
 *
 * Returns pointer to IPMI Response
 */
struct ipmi_rs *
ipmi_master_write_read(struct ipmi_intf * intf, uint8_t bus, uint8_t addr,
		       uint8_t * wdata, uint8_t wsize, uint8_t rsize)
{
	struct ipmi_rq req;
	struct ipmi_rs * rsp;
	uint8_t rqdata[IPMI_I2C_MASTER_MAX_SIZE + 3];

	if (rsize > IPMI_I2C_MASTER_MAX_SIZE) {
		lprintf(LOG_ERR, "Master Write-Read: Too many bytes (%d) to read", rsize);
		return NULL;
	}
	if (wsize > IPMI_I2C_MASTER_MAX_SIZE) {
		lprintf(LOG_ERR, "Master Write-Read: Too many bytes (%d) to write", wsize);
		return NULL;
	}

	memset(&req, 0, sizeof(struct ipmi_rq));
	req.msg.netfn = IPMI_NETFN_APP;
	req.msg.cmd = 0x52;	/* master write-read */
	req.msg.data = rqdata;
	req.msg.data_len = 3;

	memset(rqdata, 0, IPMI_I2C_MASTER_MAX_SIZE + 3);
	rqdata[0] = bus;	/* channel number, bus id, bus type */
	rqdata[1] = addr;	/* slave address */
	rqdata[2] = rsize;      /* number of bytes to read */

	if (wsize > 0) {
		/* copy in data to write */
		memcpy(rqdata+3, wdata, wsize);
		req.msg.data_len += wsize;
		lprintf(LOG_DEBUG, "Writing %d bytes to i2cdev %02Xh", wsize, addr);
	}

	if (rsize > 0) {
		lprintf(LOG_DEBUG, "Reading %d bytes from i2cdev %02Xh", rsize, addr);
	}

	rsp = intf->sendrecv(intf, &req);
	if (!rsp) {
		lprintf(LOG_ERR, "I2C Master Write-Read command failed");
		return NULL;
	}
	else if (rsp->ccode) {
		switch (rsp->ccode) {
		case 0x81:
			lprintf(LOG_ERR, "I2C Master Write-Read command failed: Lost Arbitration");
			break;
		case 0x82:
			lprintf(LOG_ERR, "I2C Master Write-Read command failed: Bus Error");
			break;
		case 0x83:
			lprintf(LOG_ERR, "I2C Master Write-Read command failed: NAK on Write");
			break;
		case 0x84:
			lprintf(LOG_ERR, "I2C Master Write-Read command failed: Truncated Read");
			break;
		default:
			lprintf(LOG_ERR, "I2C Master Write-Read command failed: %s",
				val2str(rsp->ccode, completion_code_vals));
			break;
		}
		return NULL;
	}

	return rsp;
}

#define RAW_SPD_SIZE	512

int
ipmi_rawspd_main(struct ipmi_intf * intf, int argc, char ** argv)
{
	struct ipmi_rs *rsp;
	uint8_t msize = IPMI_I2C_MASTER_MAX_SIZE; /* allow to override default */
	uint8_t channel = 0;
	uint8_t i2cbus = 0;
	uint8_t i2caddr = 0;
	uint8_t spd_data[RAW_SPD_SIZE];
	int i;

	memset(spd_data, 0, RAW_SPD_SIZE);

	if (argc < 2 || !strcmp(argv[0], "help")) {
		lprintf(LOG_NOTICE, "usage: spd <i2cbus> <i2caddr> [channel] [maxread]");
		return 0;
	}

	if (is_valid_param(argv[0], &i2cbus, "i2cbus") != 0)
		return (-1);

	if (is_valid_param(argv[1], &i2caddr, "i2caddr") != 0)
		return (-1);

	if (argc >= 3) {
		if (is_valid_param(argv[2], &channel, "channel") != 0)
			return (-1);
	}

	if (argc >= 4) {
		if (is_valid_param(argv[3], &msize, "maxread") != 0)
			return (-1);
	}

	i2cbus = ((channel & 0xF) << 4) | ((i2cbus & 7) << 1) | 1;

	for (i = 0; i < RAW_SPD_SIZE; i+= msize) {
		rsp = ipmi_master_write_read(intf, i2cbus, i2caddr,
					     (uint8_t *)&i, 1, msize );
		if (!rsp) {
			lprintf(LOG_ERR, "Unable to perform I2C Master Write-Read");
			return -1;
		}

		memcpy(spd_data+i, rsp->data, msize);
	}

	ipmi_spd_print(spd_data, i);
	return 0;
}

static void rawi2c_usage(void)
{
	lprintf(LOG_NOTICE, "usage: i2c [bus=public|# [chan=#] <i2caddr> <read bytes> [write data]");
	lprintf(LOG_NOTICE, "            bus=public is default");
	lprintf(LOG_NOTICE, "            chan=0 is default, bus= must be specified to use chan=");
}

#define BUS_KW "bus="
#define CHAN_KW "chan="

int
ipmi_rawi2c_main(struct ipmi_intf * intf, int argc, char ** argv)
{
	struct ipmi_rs * rsp;
	uint8_t wdata[IPMI_I2C_MASTER_MAX_SIZE];
	uint8_t i2caddr = 0;
	uint8_t rsize = 0;
	uint8_t wsize = 0;
	unsigned int rbus = 0;
	uint8_t bus = 0;
	int i = 0;

	/* handle bus= argument */
	if (argc > 2 && !strncmp(argv[0], BUS_KW, strlen(BUS_KW))) {
		i = 1;
		if (!strcmp(argv[0], BUS_KW "public"))
			bus = 0;
		else if (sscanf(argv[0], BUS_KW "%u", &rbus) == 1)
			bus = ((rbus & 7) << 1) | 1;
		else
			bus = 0;

		/* handle channel= argument
		 * the bus= argument must be supplied first on command line */
		if (argc > 3 && !strncmp(argv[1], CHAN_KW, strlen(CHAN_KW))) {
			i = 2;
			if (sscanf(argv[1], CHAN_KW "%u", &rbus) == 1)
				bus |= rbus << 4;
		}
	}

	if ((argc - i) < 2 || !strcmp(argv[0], "help")) {
		rawi2c_usage();
		return 0;
	}
	else if (argc-i-2 > IPMI_I2C_MASTER_MAX_SIZE) {
		lprintf(LOG_ERR, "Raw command input limit (%d bytes) exceeded",
			IPMI_I2C_MASTER_MAX_SIZE);
		return -1;
	}

	if (is_valid_param(argv[i++], &i2caddr, "i2caddr") != 0)
		return (-1);

	if (is_valid_param(argv[i++], &rsize, "read size") != 0)
		return (-1);

	if (i2caddr == 0) {
		lprintf(LOG_ERR, "Invalid I2C address 0");
		rawi2c_usage();
		return -1;
	}

	memset(wdata, 0, IPMI_I2C_MASTER_MAX_SIZE);
	for (; i < argc; i++) {
		uint8_t val = 0;

		if (is_valid_param(argv[i], &val, "parameter") != 0)
			return (-1);

		wdata[wsize] = val;
		wsize++;
	}

	lprintf(LOG_INFO, "RAW I2C REQ (i2caddr=%x readbytes=%d writebytes=%d)",
		i2caddr, rsize, wsize);
	printbuf(wdata, wsize, "WRITE DATA");

	rsp = ipmi_master_write_read(intf, bus, i2caddr, wdata, wsize, rsize);
	if (!rsp) {
		lprintf(LOG_ERR, "Unable to perform I2C Master Write-Read");
		return -1;
	}

	if (wsize > 0) {
		if (verbose || rsize == 0)
			printf("Wrote %d bytes to I2C device %02Xh\n", wsize, i2caddr);
	}

	if (rsize > 0) {
		if (verbose || wsize == 0)
			printf("Read %d bytes from I2C device %02Xh\n", rsp->data_len, i2caddr);

		if (rsp->data_len < rsize)
			return -1;

		/* print the raw response buffer */
		for (i=0; i<rsp->data_len; i++) {
			if (((i%16) == 0) && (i != 0))
				printf("\n");
			printf(" %2.2x", rsp->data[i]);
		}
		printf("\n");

		if (rsp->data_len <= 4) {
			uint32_t bit;
			int j;
			for (i = 0; i < rsp->data_len; i++) {
				for (j = 1, bit = 0x80; bit > 0; bit /= 2, j++) {
					printf("%s", (rsp->data[i] & bit) ? "1" : "0");
				}
				printf(" ");
			}
			printf("\n");
		}
	}

	return 0;
}

/* ipmi_raw_help() - print 'raw' help text
 *
 * returns void
 */
void
ipmi_raw_help()
{
	lprintf(LOG_NOTICE, "RAW Commands:  raw <netfn> <cmd> [data]");
	print_valstr(ipmi_netfn_vals, "Network Function Codes", LOG_NOTICE);
	lprintf(LOG_NOTICE, "(can also use raw hex values)");
} /* ipmi_raw_help() */

int
ipmi_raw_main(struct ipmi_intf * intf, int argc, char ** argv)
{
	struct ipmi_rs * rsp;
	struct ipmi_rq req;
	uint8_t netfn, cmd, lun;
	uint16_t netfn_tmp = 0;
	int i;
	uint8_t data[256];

	if (argc == 1 && !strcmp(argv[0], "help")) {
		ipmi_raw_help();
		return 0;
	}
	else if (argc < 2) {
		lprintf(LOG_ERR, "Not enough parameters given.");
		ipmi_raw_help();
		return (-1);
	}
	else if (argc > sizeof(data))
	{
		lprintf(LOG_NOTICE, "Raw command input limit (256 bytes) exceeded");
		return -1;
	}

	lun = intf->target_lun;
	netfn_tmp = str2val(argv[0], ipmi_netfn_vals);
	if (netfn_tmp == 0xff) {
		if (is_valid_param(argv[0], &netfn, "netfn") != 0)
			return (-1);
	} else {
		if (netfn_tmp >= UINT8_MAX) {
			lprintf(LOG_ERR, "Given netfn \"%s\" is out of range.", argv[0]);
			return (-1);
		}
		netfn = netfn_tmp;
	}

	if (is_valid_param(argv[1], &cmd, "command") != 0)
		return (-1);

	memset(data, 0, sizeof(data));
	memset(&req, 0, sizeof(req));
	req.msg.netfn = netfn;
	req.msg.lun = lun;
	req.msg.cmd = cmd;
	req.msg.data = data;

	for (i=2; i<argc; i++) {
		uint8_t val = 0;

		if (is_valid_param(argv[i], &val, "data") != 0)
			return (-1);

		req.msg.data[i-2] = val;
		req.msg.data_len++;
	}

	lprintf(LOG_INFO, 
           "RAW REQ (channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x data_len=%d)",
           intf->target_channel & 0x0f, req.msg.netfn,req.msg.lun , 
           req.msg.cmd, req.msg.data_len);

	printbuf(req.msg.data, req.msg.data_len, "RAW REQUEST");

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

	if (!rsp) {
		lprintf(LOG_ERR, "Unable to send RAW command "
			"(channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x)",
			intf->target_channel & 0x0f, req.msg.netfn, req.msg.lun, req.msg.cmd);
		return -1;
	}
	if (rsp->ccode) {
		lprintf(LOG_ERR, "Unable to send RAW command "
			"(channel=0x%x netfn=0x%x lun=0x%x cmd=0x%x rsp=0x%x): %s",
			intf->target_channel & 0x0f, req.msg.netfn, req.msg.lun, req.msg.cmd, rsp->ccode,
			val2str(rsp->ccode, completion_code_vals));
		return -1;
	}

	lprintf(LOG_INFO, "RAW RSP (%d bytes)", rsp->data_len);

	/* print the raw response buffer */
	for (i=0; i<rsp->data_len; i++) {
		if (((i%16) == 0) && (i != 0))
			printf("\n");
		printf(" %2.2x", rsp->data[i]);
	}
	printf("\n");

	return 0;
}

/* is_valid_param - 
 *
 * @input_param: string to convert from
 * @uchr_ptr: pointer where to store converted value
 * @label: string used in error message
 *
 * returns   0  if parameter is valid
 * returns (-1) if parameter is invalid/on error
 */
int
is_valid_param(const char *input_param, uint8_t *uchr_ptr, const char *label) {
	if (!input_param || !label) {
		lprintf(LOG_ERROR, "ERROR: NULL pointer passed.");
		return (-1);
	}
	if (str2uchar(input_param, uchr_ptr) == 0)
		return 0;

	lprintf(LOG_ERR, "Given %s \"%s\" is invalid.", label, input_param);
	return (-1);
}