xref: /freebsd/usr.sbin/smbmsg/smbmsg.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2004 Joerg Wunsch
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * Send or receive messages over an SMBus.
31  */
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sysexits.h>
40 #include <unistd.h>
41 
42 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 
45 #include <dev/smbus/smb.h>
46 
47 #include "pathnames.h"
48 
49 static const char *dev = PATH_DEFAULTSMBDEV;
50 static const char *bytefmt = "0x%02x";
51 static const char *wordfmt = "0x%04x";
52 static const char *fmt;
53 
54 static int fd;			/* file descriptor for /dev/smbX */
55 static int cflag = -1;		/* SMBus cmd */
56 static int iflag = -1;		/* input data */
57 static int oflag = -1;		/* output data */
58 static int pflag;		/* probe bus */
59 static int slave = -1;		/* slave address */
60 static int wflag;		/* word IO */
61 
62 static unsigned char ibuf[SMB_MAXBLOCKSIZE];
63 static unsigned char obuf[SMB_MAXBLOCKSIZE];
64 static unsigned short oword;
65 
66 /*
67  * The I2C specs say that all addresses below 16 and above or equal
68  * 240 are reserved.  Address 0 is the global address, but we do not
69  * care for this detail.
70  */
71 #define MIN_I2C_ADDR	16
72 #define MAX_I2C_ADDR	240
73 
74 static int	do_io(void);
75 static int	getnum(const char *s);
76 static void	probe_i2c(void);
77 static void	usage(void);
78 
79 static void
80 usage(void)
81 {
82 	fprintf(stderr,
83 		"usage: smbmsg [-f dev] -p\n"
84 		"       smbmsg [-f dev] -s slave [-F fmt] [-c cmd] [-w] "
85 		"[-i incnt] [-o outcnt] [outdata ...]\n");
86 	exit(EX_USAGE);
87 }
88 
89 static int
90 getnum(const char *s)
91 {
92 	char *endp;
93 	unsigned long l;
94 
95 	l = strtoul(s, &endp, 0);
96 	if (*s != '\0' && *endp == '\0')
97 		return (int)l;
98 	return (-1);
99 }
100 
101 static void
102 probe_i2c(void)
103 {
104 	unsigned char addr;
105 	int flags;
106 #define IS_READABLE	1
107 #define IS_WRITEABLE	2
108 	struct smbcmd c;
109 
110 	printf("Probing for devices on %s:\n", dev);
111 
112 	for (addr = MIN_I2C_ADDR; addr < MAX_I2C_ADDR; addr += 2) {
113 		c.slave = addr;
114 		flags = 0;
115 		if (ioctl(fd, SMB_RECVB, &c) != -1)
116 			flags = IS_READABLE;
117 		if (ioctl(fd, SMB_QUICK_WRITE, &c) != -1)
118 			flags |= IS_WRITEABLE;
119 		if (flags != 0) {
120 			printf("Device @0x%02x: ", addr);
121 			if (flags & IS_READABLE)
122 				putchar('r');
123 			if (flags & IS_WRITEABLE)
124 				putchar('w');
125 			putchar('\n');
126 		}
127 	}
128 }
129 
130 static int
131 do_io(void)
132 {
133 	struct smbcmd c;
134 	int i;
135 
136 	c.slave = slave;
137 	c.cmd = cflag;
138 	c.rcount = 0;
139 	c.wcount = 0;
140 
141 	if (fmt == NULL && iflag > 0)
142 		fmt = wflag? wordfmt: bytefmt;
143 
144 	if (cflag == -1) {
145 		/* operations that do not require a command byte */
146 		if (iflag == -1 && oflag == 0)
147 			/* 0 bytes output: quick write operation */
148 			return (ioctl(fd, SMB_QUICK_WRITE, &c));
149 		else if (iflag == 0 && oflag == -1)
150 			/* 0 bytes input: quick read operation */
151 			return (ioctl(fd, SMB_QUICK_READ, &c));
152 		else if (iflag == 1 && oflag == -1) {
153 			/* no command, 1 byte input: receive byte op. */
154 			if (ioctl(fd, SMB_RECVB, &c) == -1)
155 				return (-1);
156 			printf(fmt, (unsigned char)c.cmd);
157 			putchar('\n');
158 			return (0);
159 		} else if (iflag == -1 && oflag == 1) {
160 			/* no command, 1 byte output: send byte op. */
161 			c.cmd = obuf[0];
162 			return (ioctl(fd, SMB_SENDB, &c));
163 		} else
164 			return (-2);
165 	}
166 	if (iflag == 1 && oflag == -1) {
167 		/* command + 1 byte input: read byte op. */
168 		if (ioctl(fd, SMB_READB, &c) == -1)
169 			return (-1);
170 		printf(fmt, (unsigned char)c.rdata.byte);
171 		putchar('\n');
172 		return (0);
173 	} else if (iflag == -1 && oflag == 1) {
174 		/* command + 1 byte output: write byte op. */
175 		c.wdata.byte = obuf[0];
176 		return (ioctl(fd, SMB_WRITEB, &c));
177 	} else if (wflag && iflag == 2 && oflag == -1) {
178 		/* command + 2 bytes input: read word op. */
179 		if (ioctl(fd, SMB_READW, &c) == -1)
180 			return (-1);
181 		printf(fmt, (unsigned short)c.rdata.word);
182 		putchar('\n');
183 		return (0);
184 	} else if (wflag && iflag == -1 && oflag == 2) {
185 		/* command + 2 bytes output: write word op. */
186 		c.wdata.word = oword;
187 		return (ioctl(fd, SMB_WRITEW, &c));
188 	} else if (wflag && iflag == 2 && oflag == 2) {
189 		/*
190 		 * command + 2 bytes output + 2 bytes input:
191 		 * "process call" op.
192 		 */
193 		c.wdata.word = oword;
194 		if (ioctl(fd, SMB_PCALL, &c) == -1)
195 			return (-1);
196 		printf(fmt, (unsigned short)c.rdata.word);
197 		putchar('\n');
198 		return (0);
199 	} else if (iflag > 1 && oflag == -1) {
200 		/* command + > 1 bytes of input: block read */
201 		c.rbuf = ibuf;
202 		c.rcount = iflag;
203 		if (ioctl(fd, SMB_BREAD, &c) == -1)
204 			return (-1);
205 		for (i = 0; i < c.rcount; i++) {
206 			if (i != 0)
207 				putchar(' ');
208 			printf(fmt, ibuf[i]);
209 		}
210 		putchar('\n');
211 		return (0);
212 	} else if (iflag == -1 && oflag > 1) {
213 		/* command + > 1 bytes of output: block write */
214 		c.wbuf = obuf;
215 		c.wcount = oflag;
216 		return (ioctl(fd, SMB_BWRITE, &c));
217 	}
218 
219 	return (-2);
220 }
221 
222 
223 int
224 main(int argc, char **argv)
225 {
226 	int i, n, errs = 0;
227 	int savederrno;
228 
229 	while ((i = getopt(argc, argv, "F:c:f:i:o:ps:w")) != -1)
230 		switch (i) {
231 		case 'F':
232 			fmt = optarg;
233 			break;
234 
235 		case 'c':
236 			if ((cflag = getnum(optarg)) == -1)
237 				errx(EX_USAGE, "Invalid number: %s", optarg);
238 			if (cflag < 0 || cflag >= 256)
239 				errx(EX_USAGE,
240 				     "CMD out of range: %d",
241 				     cflag);
242 			break;
243 
244 		case 'f':
245 			dev = optarg;
246 			break;
247 
248 		case 'i':
249 			if ((iflag = getnum(optarg)) == -1)
250 				errx(EX_USAGE, "Invalid number: %s", optarg);
251 			if (iflag < 0 || iflag > SMB_MAXBLOCKSIZE)
252 				errx(EX_USAGE,
253 				     "# input bytes out of range: %d",
254 				     iflag);
255 			break;
256 
257 		case 'o':
258 			if ((oflag = getnum(optarg)) == -1)
259 				errx(EX_USAGE, "Invalid number: %s", optarg);
260 			if (oflag < 0 || oflag > SMB_MAXBLOCKSIZE)
261 				errx(EX_USAGE,
262 				     "# output bytes out of range: %d",
263 				     oflag);
264 			break;
265 
266 		case 'p':
267 			pflag = 1;
268 			break;
269 
270 		case 's':
271 			if ((slave = getnum(optarg)) == -1)
272 				errx(EX_USAGE, "Invalid number: %s", optarg);
273 
274 			if (slave < MIN_I2C_ADDR || slave >= MAX_I2C_ADDR)
275 				errx(EX_USAGE,
276 				     "Slave address out of range: %d",
277 				     slave);
278 			break;
279 
280 		case 'w':
281 			wflag = 1;
282 			break;
283 
284 		default:
285 			errs++;
286 		}
287 	argc -= optind;
288 	argv += optind;
289 	if (errs || (slave != -1 && pflag) || (slave == -1 && !pflag))
290 		usage();
291 	if (wflag &&
292 	    !((iflag == 2 && oflag == -1) ||
293 	      (iflag == -1 && oflag == 2) ||
294 	      (iflag == 2 && oflag == 2)))
295 		errx(EX_USAGE, "Illegal # IO bytes for word IO");
296 	if (!pflag && iflag == -1 && oflag == -1)
297 		errx(EX_USAGE, "Nothing to do");
298 	if (pflag && (cflag != -1 || iflag != -1 || oflag != -1 || wflag != 0))
299 		usage();
300 	if (oflag > 0) {
301 		if (oflag == 2 && wflag) {
302 			if (argc == 0)
303 				errx(EX_USAGE, "Too few arguments for -o count");
304 			if ((n = getnum(*argv)) == -1)
305 				errx(EX_USAGE, "Invalid number: %s", *argv);
306 			if (n < 0 || n >= 65535)
307 				errx(EX_USAGE, "Value out of range: %d", n);
308 			oword = n;
309 			argc--;
310 			argv++;
311 		} else for (i = 0; i < oflag; i++, argv++, argc--) {
312 			if (argc == 0)
313 				errx(EX_USAGE, "Too few arguments for -o count");
314 			if ((n = getnum(*argv)) == -1)
315 				errx(EX_USAGE, "Invalid number: %s", *argv);
316 			if (n < 0 || n >= 256)
317 				errx(EX_USAGE, "Value out of range: %d", n);
318 			obuf[i] = n;
319 		}
320 	}
321 	if (argc != 0)
322 		usage();
323 
324 	if ((fd = open(dev, O_RDWR)) == -1)
325 		err(EX_UNAVAILABLE, "Cannot open %s", dev);
326 
327 	i = 0;
328 	if (pflag)
329 		probe_i2c();
330 	else
331 		i = do_io();
332 
333 	savederrno = errno;
334 	close(fd);
335 	errno = savederrno;
336 
337 	if (i == -1)
338 		err(EX_UNAVAILABLE, "Error performing SMBus IO");
339 	else if (i == -2)
340 		errx(EX_USAGE, "Invalid option combination");
341 
342 	return (0);
343 }
344