17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5c7bf320johnlev * Common Development and Distribution License (the "License").
6c7bf320johnlev * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
228014889Surya Prakki * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate/*
277c478bdstevel@tonic-gate * PROM I/O backend
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
307c478bdstevel@tonic-gate#include <sys/param.h>
317c478bdstevel@tonic-gate#include <sys/types.h>
327c478bdstevel@tonic-gate#include <sys/obpdefs.h>
337c478bdstevel@tonic-gate#include <string.h>
347c478bdstevel@tonic-gate#include <errno.h>
35c7bf320johnlev#include <ctype.h>
367c478bdstevel@tonic-gate
377c478bdstevel@tonic-gate#include <mdb/mdb_modapi.h>
387c478bdstevel@tonic-gate#include <mdb/mdb_io_impl.h>
397c478bdstevel@tonic-gate#include <kmdb/kmdb_promif.h>
407c478bdstevel@tonic-gate#include <kmdb/kmdb_io.h>
417c478bdstevel@tonic-gate#include <mdb/mdb_debug.h>
427c478bdstevel@tonic-gate#include <mdb/mdb_err.h>
437c478bdstevel@tonic-gate#include <mdb/mdb.h>
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#define	PIO_FL_TIO_READ	0x001
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gatetypedef struct pio_data {
487c478bdstevel@tonic-gate	char		pio_name[MAXPATHLEN];
497c478bdstevel@tonic-gate	ihandle_t	pio_fd;
507c478bdstevel@tonic-gate	uint_t		pio_flags;
517c478bdstevel@tonic-gate	struct termios	pio_ti;
527c478bdstevel@tonic-gate} pio_data_t;
537c478bdstevel@tonic-gate
547c478bdstevel@tonic-gatestatic pid_t pio_pgrp;
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gatestatic ssize_t
577c478bdstevel@tonic-gatepio_read(mdb_io_t *io, void *buf, size_t nbytes)
587c478bdstevel@tonic-gate{
597c478bdstevel@tonic-gate	pio_data_t *pdp = io->io_data;
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	if (io->io_next == NULL)
627c478bdstevel@tonic-gate		return (kmdb_prom_read(buf, nbytes, &pdp->pio_ti));
637c478bdstevel@tonic-gate
647c478bdstevel@tonic-gate	return (IOP_READ(io->io_next, buf, nbytes));
657c478bdstevel@tonic-gate}
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gatestatic ssize_t
687c478bdstevel@tonic-gatepio_write(mdb_io_t *io, const void *buf, size_t nbytes)
697c478bdstevel@tonic-gate{
707c478bdstevel@tonic-gate	pio_data_t *pdp = io->io_data;
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate	if (io->io_next == NULL)
737c478bdstevel@tonic-gate		return (kmdb_prom_write(buf, nbytes, &pdp->pio_ti));
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate	return (IOP_WRITE(io->io_next, buf, nbytes));
767c478bdstevel@tonic-gate}
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gatestatic off64_t
797c478bdstevel@tonic-gatepio_seek(mdb_io_t *io, off64_t offset, int whence)
807c478bdstevel@tonic-gate{
817c478bdstevel@tonic-gate	if (io->io_next == NULL)
827c478bdstevel@tonic-gate		return (set_errno(ENOTSUP));
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate	return (IOP_SEEK(io->io_next, offset, whence));
857c478bdstevel@tonic-gate}
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gatestatic int
887c478bdstevel@tonic-gatepio_ctl(mdb_io_t *io, int req, void *arg)
897c478bdstevel@tonic-gate{
907c478bdstevel@tonic-gate	pio_data_t *pdp = io->io_data;
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate	if (io->io_next != NULL)
937c478bdstevel@tonic-gate		return (IOP_CTL(io->io_next, req, arg));
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate	switch (req) {
967c478bdstevel@tonic-gate	case TIOCGWINSZ:
977c478bdstevel@tonic-gate		return (kmdb_prom_term_ctl(TIOCGWINSZ, arg));
987c478bdstevel@tonic-gate
997c478bdstevel@tonic-gate	case TCGETS: {
1007c478bdstevel@tonic-gate		struct termios *ti = arg;
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate		if (!(pdp->pio_flags & PIO_FL_TIO_READ)) {
1037c478bdstevel@tonic-gate			(void) kmdb_prom_term_ctl(TCGETS, &pdp->pio_ti);
1047c478bdstevel@tonic-gate			pdp->pio_flags |= PIO_FL_TIO_READ;
1057c478bdstevel@tonic-gate		}
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate		bcopy(&pdp->pio_ti, ti, sizeof (struct termios));
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gets: i: 0%o o: 0%o c: "
1107c478bdstevel@tonic-gate		    "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
1117c478bdstevel@tonic-gate		    ti->c_lflag);
1127c478bdstevel@tonic-gate		return (0);
1137c478bdstevel@tonic-gate	}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	case TCSETSW: {
1167c478bdstevel@tonic-gate		struct termios *ti = arg;
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: setsw: i: 0%o o: 0%o c: "
1197c478bdstevel@tonic-gate		    "0%o l: 0%o\n", ti->c_iflag, ti->c_oflag, ti->c_cflag,
1207c478bdstevel@tonic-gate		    ti->c_lflag);
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gate		bcopy(ti, &pdp->pio_ti, sizeof (struct termios));
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate		return (0);
1257c478bdstevel@tonic-gate	}
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	case TIOCSPGRP:
1287c478bdstevel@tonic-gate		pio_pgrp = *(pid_t *)arg;
1297c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: spgrp: %ld\n",
1307c478bdstevel@tonic-gate		    (long)pio_pgrp);
1317c478bdstevel@tonic-gate		return (0);
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	case TIOCGPGRP:
1347c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: gpgrp: %ld\n",
1357c478bdstevel@tonic-gate		    (long)pio_pgrp);
1367c478bdstevel@tonic-gate		*(pid_t *)arg = pio_pgrp;
1377c478bdstevel@tonic-gate		return (0);
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gate	case MDB_IOC_CTTY:
1407c478bdstevel@tonic-gate		mdb_dprintf(MDB_DBG_CMDBUF, "pio_ctl: ignoring MDB_IOC_CTTY\n");
1417c478bdstevel@tonic-gate		return (0);
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate	case MDB_IOC_GETFD:
1447c478bdstevel@tonic-gate		return (set_errno(ENOTSUP));
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate	default:
1477c478bdstevel@tonic-gate		warn("Unknown ioctl %d\n", req);
1487c478bdstevel@tonic-gate		return (set_errno(EINVAL));
1497c478bdstevel@tonic-gate	}
1507c478bdstevel@tonic-gate}
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gatevoid
1537c478bdstevel@tonic-gatepio_close(mdb_io_t *io)
1547c478bdstevel@tonic-gate{
1557c478bdstevel@tonic-gate	pio_data_t *pdp = io->io_data;
1567c478bdstevel@tonic-gate
1577c478bdstevel@tonic-gate	mdb_free(pdp, sizeof (pio_data_t));
1587c478bdstevel@tonic-gate}
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gatestatic const char *
1617c478bdstevel@tonic-gatepio_name(mdb_io_t *io)
1627c478bdstevel@tonic-gate{
1637c478bdstevel@tonic-gate	pio_data_t *pdp = io->io_data;
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	if (io->io_next == NULL)
1667c478bdstevel@tonic-gate		return (pdp->pio_name);
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate	return (IOP_NAME(io->io_next));
1697c478bdstevel@tonic-gate}
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gatestatic const mdb_io_ops_t promio_ops = {
1727c478bdstevel@tonic-gate	pio_read,
1737c478bdstevel@tonic-gate	pio_write,
1747c478bdstevel@tonic-gate	pio_seek,
1757c478bdstevel@tonic-gate	pio_ctl,
1767c478bdstevel@tonic-gate	pio_close,
1777c478bdstevel@tonic-gate	pio_name,
1787c478bdstevel@tonic-gate	no_io_link,
1797c478bdstevel@tonic-gate	no_io_unlink,
1807c478bdstevel@tonic-gate	no_io_setattr,
1817c478bdstevel@tonic-gate	no_io_suspend,
1827c478bdstevel@tonic-gate	no_io_resume
1837c478bdstevel@tonic-gate};
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gatemdb_io_t *
1867c478bdstevel@tonic-gatekmdb_promio_create(char *name)
1877c478bdstevel@tonic-gate{
1887c478bdstevel@tonic-gate	mdb_io_t *io;
1897c478bdstevel@tonic-gate	pio_data_t *pdp;
1907c478bdstevel@tonic-gate	ihandle_t hdl = kmdb_prom_get_handle(name);
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate	if (hdl == -1)
1937c478bdstevel@tonic-gate		return (NULL);
1947c478bdstevel@tonic-gate
1957c478bdstevel@tonic-gate	io = mdb_zalloc(sizeof (mdb_io_t), UM_SLEEP);
1967c478bdstevel@tonic-gate	pdp = mdb_zalloc(sizeof (pio_data_t), UM_SLEEP);
1977c478bdstevel@tonic-gate
1988014889Surya Prakki	(void) strlcpy(pdp->pio_name, name, MAXPATHLEN);
1997c478bdstevel@tonic-gate	pdp->pio_fd = hdl;
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate#ifdef __sparc
2027c478bdstevel@tonic-gate	pdp->pio_ti.c_oflag |= ONLCR;
2037c478bdstevel@tonic-gate	pdp->pio_ti.c_iflag |= ICRNL;
2047c478bdstevel@tonic-gate#endif
2057c478bdstevel@tonic-gate	pdp->pio_ti.c_lflag |= ECHO;
2067c478bdstevel@tonic-gate
2077c478bdstevel@tonic-gate	io->io_data = pdp;
2087c478bdstevel@tonic-gate	io->io_ops = &promio_ops;
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate	return (io);
2117c478bdstevel@tonic-gate}
212c7bf320johnlev
213c7bf320johnlevchar
214c7bf320johnlevkmdb_getchar(void)
215c7bf320johnlev{
216c7bf320johnlev	char c;
217c7bf320johnlev
218c7bf320johnlev	while (IOP_READ(mdb.m_term, &c, 1) != 1)
219c7bf320johnlev		continue;
220c7bf320johnlev	if (isprint(c) && c != '\n')
221c7bf320johnlev		mdb_iob_printf(mdb.m_out, "%c", c);
222c7bf320johnlev	mdb_iob_printf(mdb.m_out, "\n");
223c7bf320johnlev
224c7bf320johnlev	return (c);
225c7bf320johnlev}
226