Update OpenBIOS images to r771
[qemu/aliguori-queue.git] / qemu-char.c
blobac65a1c806893b897e52eb9a2635469ea0c3c3dd
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "net.h"
26 #include "monitor.h"
27 #include "console.h"
28 #include "sysemu.h"
29 #include "qemu-timer.h"
30 #include "qemu-char.h"
31 #include "block.h"
32 #include "hw/usb.h"
33 #include "hw/baum.h"
34 #include "hw/msmouse.h"
35 #include "qemu-objects.h"
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <signal.h>
40 #include <time.h>
41 #include <errno.h>
42 #include <sys/time.h>
43 #include <zlib.h>
45 #ifndef _WIN32
46 #include <sys/times.h>
47 #include <sys/wait.h>
48 #include <termios.h>
49 #include <sys/mman.h>
50 #include <sys/ioctl.h>
51 #include <sys/resource.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <net/if.h>
55 #include <arpa/inet.h>
56 #include <dirent.h>
57 #include <netdb.h>
58 #include <sys/select.h>
59 #ifdef CONFIG_BSD
60 #include <sys/stat.h>
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
62 #include <libutil.h>
63 #include <dev/ppbus/ppi.h>
64 #include <dev/ppbus/ppbconf.h>
65 #if defined(__GLIBC__)
66 #include <pty.h>
67 #endif
68 #elif defined(__DragonFly__)
69 #include <libutil.h>
70 #include <dev/misc/ppi/ppi.h>
71 #include <bus/ppbus/ppbconf.h>
72 #else
73 #include <util.h>
74 #endif
75 #else
76 #ifdef __linux__
77 #include <pty.h>
79 #include <linux/ppdev.h>
80 #include <linux/parport.h>
81 #endif
82 #ifdef __sun__
83 #include <sys/stat.h>
84 #include <sys/ethernet.h>
85 #include <sys/sockio.h>
86 #include <netinet/arp.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_icmp.h> // must come after ip.h
91 #include <netinet/udp.h>
92 #include <netinet/tcp.h>
93 #include <net/if.h>
94 #include <syslog.h>
95 #include <stropts.h>
96 #endif
97 #endif
98 #endif
100 #include "qemu_socket.h"
102 #define READ_BUF_LEN 4096
104 /***********************************************************/
105 /* character device */
107 static QTAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs =
108 QTAILQ_HEAD_INITIALIZER(chardevs);
110 static void qemu_chr_event(CharDriverState *s, int event)
112 /* Keep track if the char device is open */
113 switch (event) {
114 case CHR_EVENT_OPENED:
115 s->opened = 1;
116 break;
117 case CHR_EVENT_CLOSED:
118 s->opened = 0;
119 break;
122 if (!s->chr_event)
123 return;
124 s->chr_event(s->handler_opaque, event);
127 static void qemu_chr_generic_open_bh(void *opaque)
129 CharDriverState *s = opaque;
130 qemu_chr_event(s, CHR_EVENT_OPENED);
131 qemu_bh_delete(s->bh);
132 s->bh = NULL;
135 void qemu_chr_generic_open(CharDriverState *s)
137 if (s->bh == NULL) {
138 s->bh = qemu_bh_new(qemu_chr_generic_open_bh, s);
139 qemu_bh_schedule(s->bh);
143 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
145 return s->chr_write(s, buf, len);
148 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
150 if (!s->chr_ioctl)
151 return -ENOTSUP;
152 return s->chr_ioctl(s, cmd, arg);
155 int qemu_chr_can_read(CharDriverState *s)
157 if (!s->chr_can_read)
158 return 0;
159 return s->chr_can_read(s->handler_opaque);
162 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
164 s->chr_read(s->handler_opaque, buf, len);
167 int qemu_chr_get_msgfd(CharDriverState *s)
169 return s->get_msgfd ? s->get_msgfd(s) : -1;
172 void qemu_chr_accept_input(CharDriverState *s)
174 if (s->chr_accept_input)
175 s->chr_accept_input(s);
178 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
180 char buf[READ_BUF_LEN];
181 va_list ap;
182 va_start(ap, fmt);
183 vsnprintf(buf, sizeof(buf), fmt, ap);
184 qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
185 va_end(ap);
188 void qemu_chr_send_event(CharDriverState *s, int event)
190 if (s->chr_send_event)
191 s->chr_send_event(s, event);
194 void qemu_chr_add_handlers(CharDriverState *s,
195 IOCanReadHandler *fd_can_read,
196 IOReadHandler *fd_read,
197 IOEventHandler *fd_event,
198 void *opaque)
200 s->chr_can_read = fd_can_read;
201 s->chr_read = fd_read;
202 s->chr_event = fd_event;
203 s->handler_opaque = opaque;
204 if (s->chr_update_read_handler)
205 s->chr_update_read_handler(s);
207 /* We're connecting to an already opened device, so let's make sure we
208 also get the open event */
209 if (s->opened) {
210 qemu_chr_generic_open(s);
214 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
216 return len;
219 static CharDriverState *qemu_chr_open_null(QemuOpts *opts)
221 CharDriverState *chr;
223 chr = qemu_mallocz(sizeof(CharDriverState));
224 chr->chr_write = null_chr_write;
225 return chr;
228 /* MUX driver for serial I/O splitting */
229 #define MAX_MUX 4
230 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */
231 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
232 typedef struct {
233 IOCanReadHandler *chr_can_read[MAX_MUX];
234 IOReadHandler *chr_read[MAX_MUX];
235 IOEventHandler *chr_event[MAX_MUX];
236 void *ext_opaque[MAX_MUX];
237 CharDriverState *drv;
238 int focus;
239 int mux_cnt;
240 int term_got_escape;
241 int max_size;
242 /* Intermediate input buffer allows to catch escape sequences even if the
243 currently active device is not accepting any input - but only until it
244 is full as well. */
245 unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
246 int prod[MAX_MUX];
247 int cons[MAX_MUX];
248 int timestamps;
249 int linestart;
250 int64_t timestamps_start;
251 } MuxDriver;
254 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
256 MuxDriver *d = chr->opaque;
257 int ret;
258 if (!d->timestamps) {
259 ret = d->drv->chr_write(d->drv, buf, len);
260 } else {
261 int i;
263 ret = 0;
264 for (i = 0; i < len; i++) {
265 if (d->linestart) {
266 char buf1[64];
267 int64_t ti;
268 int secs;
270 ti = qemu_get_clock(rt_clock);
271 if (d->timestamps_start == -1)
272 d->timestamps_start = ti;
273 ti -= d->timestamps_start;
274 secs = ti / 1000;
275 snprintf(buf1, sizeof(buf1),
276 "[%02d:%02d:%02d.%03d] ",
277 secs / 3600,
278 (secs / 60) % 60,
279 secs % 60,
280 (int)(ti % 1000));
281 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
282 d->linestart = 0;
284 ret += d->drv->chr_write(d->drv, buf+i, 1);
285 if (buf[i] == '\n') {
286 d->linestart = 1;
290 return ret;
293 static const char * const mux_help[] = {
294 "% h print this help\n\r",
295 "% x exit emulator\n\r",
296 "% s save disk data back to file (if -snapshot)\n\r",
297 "% t toggle console timestamps\n\r"
298 "% b send break (magic sysrq)\n\r",
299 "% c switch between console and monitor\n\r",
300 "% % sends %\n\r",
301 NULL
304 int term_escape_char = 0x01; /* ctrl-a is used for escape */
305 static void mux_print_help(CharDriverState *chr)
307 int i, j;
308 char ebuf[15] = "Escape-Char";
309 char cbuf[50] = "\n\r";
311 if (term_escape_char > 0 && term_escape_char < 26) {
312 snprintf(cbuf, sizeof(cbuf), "\n\r");
313 snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
314 } else {
315 snprintf(cbuf, sizeof(cbuf),
316 "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
317 term_escape_char);
319 chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
320 for (i = 0; mux_help[i] != NULL; i++) {
321 for (j=0; mux_help[i][j] != '\0'; j++) {
322 if (mux_help[i][j] == '%')
323 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
324 else
325 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
330 static void mux_chr_send_event(MuxDriver *d, int mux_nr, int event)
332 if (d->chr_event[mux_nr])
333 d->chr_event[mux_nr](d->ext_opaque[mux_nr], event);
336 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
338 if (d->term_got_escape) {
339 d->term_got_escape = 0;
340 if (ch == term_escape_char)
341 goto send_char;
342 switch(ch) {
343 case '?':
344 case 'h':
345 mux_print_help(chr);
346 break;
347 case 'x':
349 const char *term = "QEMU: Terminated\n\r";
350 chr->chr_write(chr,(uint8_t *)term,strlen(term));
351 exit(0);
352 break;
354 case 's':
356 DriveInfo *dinfo;
357 QTAILQ_FOREACH(dinfo, &drives, next) {
358 bdrv_commit(dinfo->bdrv);
361 break;
362 case 'b':
363 qemu_chr_event(chr, CHR_EVENT_BREAK);
364 break;
365 case 'c':
366 /* Switch to the next registered device */
367 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
368 d->focus++;
369 if (d->focus >= d->mux_cnt)
370 d->focus = 0;
371 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
372 break;
373 case 't':
374 d->timestamps = !d->timestamps;
375 d->timestamps_start = -1;
376 d->linestart = 0;
377 break;
379 } else if (ch == term_escape_char) {
380 d->term_got_escape = 1;
381 } else {
382 send_char:
383 return 1;
385 return 0;
388 static void mux_chr_accept_input(CharDriverState *chr)
390 MuxDriver *d = chr->opaque;
391 int m = d->focus;
393 while (d->prod[m] != d->cons[m] &&
394 d->chr_can_read[m] &&
395 d->chr_can_read[m](d->ext_opaque[m])) {
396 d->chr_read[m](d->ext_opaque[m],
397 &d->buffer[m][d->cons[m]++ & MUX_BUFFER_MASK], 1);
401 static int mux_chr_can_read(void *opaque)
403 CharDriverState *chr = opaque;
404 MuxDriver *d = chr->opaque;
405 int m = d->focus;
407 if ((d->prod[m] - d->cons[m]) < MUX_BUFFER_SIZE)
408 return 1;
409 if (d->chr_can_read[m])
410 return d->chr_can_read[m](d->ext_opaque[m]);
411 return 0;
414 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
416 CharDriverState *chr = opaque;
417 MuxDriver *d = chr->opaque;
418 int m = d->focus;
419 int i;
421 mux_chr_accept_input (opaque);
423 for(i = 0; i < size; i++)
424 if (mux_proc_byte(chr, d, buf[i])) {
425 if (d->prod[m] == d->cons[m] &&
426 d->chr_can_read[m] &&
427 d->chr_can_read[m](d->ext_opaque[m]))
428 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
429 else
430 d->buffer[m][d->prod[m]++ & MUX_BUFFER_MASK] = buf[i];
434 static void mux_chr_event(void *opaque, int event)
436 CharDriverState *chr = opaque;
437 MuxDriver *d = chr->opaque;
438 int i;
440 /* Send the event to all registered listeners */
441 for (i = 0; i < d->mux_cnt; i++)
442 mux_chr_send_event(d, i, event);
445 static void mux_chr_update_read_handler(CharDriverState *chr)
447 MuxDriver *d = chr->opaque;
449 if (d->mux_cnt >= MAX_MUX) {
450 fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
451 return;
453 d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
454 d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
455 d->chr_read[d->mux_cnt] = chr->chr_read;
456 d->chr_event[d->mux_cnt] = chr->chr_event;
457 /* Fix up the real driver with mux routines */
458 if (d->mux_cnt == 0) {
459 qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
460 mux_chr_event, chr);
462 if (d->focus != -1) {
463 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
465 d->focus = d->mux_cnt;
466 d->mux_cnt++;
467 mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_IN);
470 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
472 CharDriverState *chr;
473 MuxDriver *d;
475 chr = qemu_mallocz(sizeof(CharDriverState));
476 d = qemu_mallocz(sizeof(MuxDriver));
478 chr->opaque = d;
479 d->drv = drv;
480 d->focus = -1;
481 chr->chr_write = mux_chr_write;
482 chr->chr_update_read_handler = mux_chr_update_read_handler;
483 chr->chr_accept_input = mux_chr_accept_input;
485 /* Muxes are always open on creation */
486 qemu_chr_generic_open(chr);
488 return chr;
492 #ifdef _WIN32
493 int send_all(int fd, const void *buf, int len1)
495 int ret, len;
497 len = len1;
498 while (len > 0) {
499 ret = send(fd, buf, len, 0);
500 if (ret < 0) {
501 errno = WSAGetLastError();
502 if (errno != WSAEWOULDBLOCK) {
503 return -1;
505 } else if (ret == 0) {
506 break;
507 } else {
508 buf += ret;
509 len -= ret;
512 return len1 - len;
515 #else
517 static int unix_write(int fd, const uint8_t *buf, int len1)
519 int ret, len;
521 len = len1;
522 while (len > 0) {
523 ret = write(fd, buf, len);
524 if (ret < 0) {
525 if (errno != EINTR && errno != EAGAIN)
526 return -1;
527 } else if (ret == 0) {
528 break;
529 } else {
530 buf += ret;
531 len -= ret;
534 return len1 - len;
537 int send_all(int fd, const void *buf, int len1)
539 return unix_write(fd, buf, len1);
541 #endif /* !_WIN32 */
543 #ifndef _WIN32
545 typedef struct {
546 int fd_in, fd_out;
547 int max_size;
548 } FDCharDriver;
550 #define STDIO_MAX_CLIENTS 1
551 static int stdio_nb_clients = 0;
553 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
555 FDCharDriver *s = chr->opaque;
556 return send_all(s->fd_out, buf, len);
559 static int fd_chr_read_poll(void *opaque)
561 CharDriverState *chr = opaque;
562 FDCharDriver *s = chr->opaque;
564 s->max_size = qemu_chr_can_read(chr);
565 return s->max_size;
568 static void fd_chr_read(void *opaque)
570 CharDriverState *chr = opaque;
571 FDCharDriver *s = chr->opaque;
572 int size, len;
573 uint8_t buf[READ_BUF_LEN];
575 len = sizeof(buf);
576 if (len > s->max_size)
577 len = s->max_size;
578 if (len == 0)
579 return;
580 size = read(s->fd_in, buf, len);
581 if (size == 0) {
582 /* FD has been closed. Remove it from the active list. */
583 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
584 qemu_chr_event(chr, CHR_EVENT_CLOSED);
585 return;
587 if (size > 0) {
588 qemu_chr_read(chr, buf, size);
592 static void fd_chr_update_read_handler(CharDriverState *chr)
594 FDCharDriver *s = chr->opaque;
596 if (s->fd_in >= 0) {
597 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
598 } else {
599 qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
600 fd_chr_read, NULL, chr);
605 static void fd_chr_close(struct CharDriverState *chr)
607 FDCharDriver *s = chr->opaque;
609 if (s->fd_in >= 0) {
610 if (display_type == DT_NOGRAPHIC && s->fd_in == 0) {
611 } else {
612 qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
616 qemu_free(s);
617 qemu_chr_event(chr, CHR_EVENT_CLOSED);
620 /* open a character device to a unix fd */
621 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
623 CharDriverState *chr;
624 FDCharDriver *s;
626 chr = qemu_mallocz(sizeof(CharDriverState));
627 s = qemu_mallocz(sizeof(FDCharDriver));
628 s->fd_in = fd_in;
629 s->fd_out = fd_out;
630 chr->opaque = s;
631 chr->chr_write = fd_chr_write;
632 chr->chr_update_read_handler = fd_chr_update_read_handler;
633 chr->chr_close = fd_chr_close;
635 qemu_chr_generic_open(chr);
637 return chr;
640 static CharDriverState *qemu_chr_open_file_out(QemuOpts *opts)
642 int fd_out;
644 TFR(fd_out = qemu_open(qemu_opt_get(opts, "path"),
645 O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
646 if (fd_out < 0)
647 return NULL;
648 return qemu_chr_open_fd(-1, fd_out);
651 static CharDriverState *qemu_chr_open_pipe(QemuOpts *opts)
653 int fd_in, fd_out;
654 char filename_in[256], filename_out[256];
655 const char *filename = qemu_opt_get(opts, "path");
657 if (filename == NULL) {
658 fprintf(stderr, "chardev: pipe: no filename given\n");
659 return NULL;
662 snprintf(filename_in, 256, "%s.in", filename);
663 snprintf(filename_out, 256, "%s.out", filename);
664 TFR(fd_in = qemu_open(filename_in, O_RDWR | O_BINARY));
665 TFR(fd_out = qemu_open(filename_out, O_RDWR | O_BINARY));
666 if (fd_in < 0 || fd_out < 0) {
667 if (fd_in >= 0)
668 close(fd_in);
669 if (fd_out >= 0)
670 close(fd_out);
671 TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
672 if (fd_in < 0)
673 return NULL;
675 return qemu_chr_open_fd(fd_in, fd_out);
679 /* for STDIO, we handle the case where several clients use it
680 (nographic mode) */
682 #define TERM_FIFO_MAX_SIZE 1
684 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
685 static int term_fifo_size;
687 static int stdio_read_poll(void *opaque)
689 CharDriverState *chr = opaque;
691 /* try to flush the queue if needed */
692 if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
693 qemu_chr_read(chr, term_fifo, 1);
694 term_fifo_size = 0;
696 /* see if we can absorb more chars */
697 if (term_fifo_size == 0)
698 return 1;
699 else
700 return 0;
703 static void stdio_read(void *opaque)
705 int size;
706 uint8_t buf[1];
707 CharDriverState *chr = opaque;
709 size = read(0, buf, 1);
710 if (size == 0) {
711 /* stdin has been closed. Remove it from the active list. */
712 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
713 qemu_chr_event(chr, CHR_EVENT_CLOSED);
714 return;
716 if (size > 0) {
717 if (qemu_chr_can_read(chr) > 0) {
718 qemu_chr_read(chr, buf, 1);
719 } else if (term_fifo_size == 0) {
720 term_fifo[term_fifo_size++] = buf[0];
725 /* init terminal so that we can grab keys */
726 static struct termios oldtty;
727 static int old_fd0_flags;
728 static int term_atexit_done;
730 static void term_exit(void)
732 tcsetattr (0, TCSANOW, &oldtty);
733 fcntl(0, F_SETFL, old_fd0_flags);
736 static void term_init(QemuOpts *opts)
738 struct termios tty;
740 tcgetattr (0, &tty);
741 oldtty = tty;
742 old_fd0_flags = fcntl(0, F_GETFL);
744 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
745 |INLCR|IGNCR|ICRNL|IXON);
746 tty.c_oflag |= OPOST;
747 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
748 /* if graphical mode, we allow Ctrl-C handling */
749 if (!qemu_opt_get_bool(opts, "signal", display_type != DT_NOGRAPHIC))
750 tty.c_lflag &= ~ISIG;
751 tty.c_cflag &= ~(CSIZE|PARENB);
752 tty.c_cflag |= CS8;
753 tty.c_cc[VMIN] = 1;
754 tty.c_cc[VTIME] = 0;
756 tcsetattr (0, TCSANOW, &tty);
758 if (!term_atexit_done++)
759 atexit(term_exit);
761 fcntl(0, F_SETFL, O_NONBLOCK);
764 static void qemu_chr_close_stdio(struct CharDriverState *chr)
766 term_exit();
767 stdio_nb_clients--;
768 qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
769 fd_chr_close(chr);
772 static CharDriverState *qemu_chr_open_stdio(QemuOpts *opts)
774 CharDriverState *chr;
776 if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
777 return NULL;
778 chr = qemu_chr_open_fd(0, 1);
779 chr->chr_close = qemu_chr_close_stdio;
780 qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
781 stdio_nb_clients++;
782 term_init(opts);
784 return chr;
787 #ifdef __sun__
788 /* Once Solaris has openpty(), this is going to be removed. */
789 static int openpty(int *amaster, int *aslave, char *name,
790 struct termios *termp, struct winsize *winp)
792 const char *slave;
793 int mfd = -1, sfd = -1;
795 *amaster = *aslave = -1;
797 mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
798 if (mfd < 0)
799 goto err;
801 if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
802 goto err;
804 if ((slave = ptsname(mfd)) == NULL)
805 goto err;
807 if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
808 goto err;
810 if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
811 (termp != NULL && tcgetattr(sfd, termp) < 0))
812 goto err;
814 if (amaster)
815 *amaster = mfd;
816 if (aslave)
817 *aslave = sfd;
818 if (winp)
819 ioctl(sfd, TIOCSWINSZ, winp);
821 return 0;
823 err:
824 if (sfd != -1)
825 close(sfd);
826 close(mfd);
827 return -1;
830 static void cfmakeraw (struct termios *termios_p)
832 termios_p->c_iflag &=
833 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
834 termios_p->c_oflag &= ~OPOST;
835 termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
836 termios_p->c_cflag &= ~(CSIZE|PARENB);
837 termios_p->c_cflag |= CS8;
839 termios_p->c_cc[VMIN] = 0;
840 termios_p->c_cc[VTIME] = 0;
842 #endif
844 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
845 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
846 || defined(__GLIBC__)
848 typedef struct {
849 int fd;
850 int connected;
851 int polling;
852 int read_bytes;
853 QEMUTimer *timer;
854 } PtyCharDriver;
856 static void pty_chr_update_read_handler(CharDriverState *chr);
857 static void pty_chr_state(CharDriverState *chr, int connected);
859 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
861 PtyCharDriver *s = chr->opaque;
863 if (!s->connected) {
864 /* guest sends data, check for (re-)connect */
865 pty_chr_update_read_handler(chr);
866 return 0;
868 return send_all(s->fd, buf, len);
871 static int pty_chr_read_poll(void *opaque)
873 CharDriverState *chr = opaque;
874 PtyCharDriver *s = chr->opaque;
876 s->read_bytes = qemu_chr_can_read(chr);
877 return s->read_bytes;
880 static void pty_chr_read(void *opaque)
882 CharDriverState *chr = opaque;
883 PtyCharDriver *s = chr->opaque;
884 int size, len;
885 uint8_t buf[READ_BUF_LEN];
887 len = sizeof(buf);
888 if (len > s->read_bytes)
889 len = s->read_bytes;
890 if (len == 0)
891 return;
892 size = read(s->fd, buf, len);
893 if ((size == -1 && errno == EIO) ||
894 (size == 0)) {
895 pty_chr_state(chr, 0);
896 return;
898 if (size > 0) {
899 pty_chr_state(chr, 1);
900 qemu_chr_read(chr, buf, size);
904 static void pty_chr_update_read_handler(CharDriverState *chr)
906 PtyCharDriver *s = chr->opaque;
908 qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
909 pty_chr_read, NULL, chr);
910 s->polling = 1;
912 * Short timeout here: just need wait long enougth that qemu makes
913 * it through the poll loop once. When reconnected we want a
914 * short timeout so we notice it almost instantly. Otherwise
915 * read() gives us -EIO instantly, making pty_chr_state() reset the
916 * timeout to the normal (much longer) poll interval before the
917 * timer triggers.
919 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
922 static void pty_chr_state(CharDriverState *chr, int connected)
924 PtyCharDriver *s = chr->opaque;
926 if (!connected) {
927 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
928 s->connected = 0;
929 s->polling = 0;
930 /* (re-)connect poll interval for idle guests: once per second.
931 * We check more frequently in case the guests sends data to
932 * the virtual device linked to our pty. */
933 qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
934 } else {
935 if (!s->connected)
936 qemu_chr_generic_open(chr);
937 s->connected = 1;
941 static void pty_chr_timer(void *opaque)
943 struct CharDriverState *chr = opaque;
944 PtyCharDriver *s = chr->opaque;
946 if (s->connected)
947 return;
948 if (s->polling) {
949 /* If we arrive here without polling being cleared due
950 * read returning -EIO, then we are (re-)connected */
951 pty_chr_state(chr, 1);
952 return;
955 /* Next poll ... */
956 pty_chr_update_read_handler(chr);
959 static void pty_chr_close(struct CharDriverState *chr)
961 PtyCharDriver *s = chr->opaque;
963 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
964 close(s->fd);
965 qemu_del_timer(s->timer);
966 qemu_free_timer(s->timer);
967 qemu_free(s);
968 qemu_chr_event(chr, CHR_EVENT_CLOSED);
971 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
973 CharDriverState *chr;
974 PtyCharDriver *s;
975 struct termios tty;
976 int slave_fd, len;
977 #if defined(__OpenBSD__) || defined(__DragonFly__)
978 char pty_name[PATH_MAX];
979 #define q_ptsname(x) pty_name
980 #else
981 char *pty_name = NULL;
982 #define q_ptsname(x) ptsname(x)
983 #endif
985 chr = qemu_mallocz(sizeof(CharDriverState));
986 s = qemu_mallocz(sizeof(PtyCharDriver));
988 if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
989 return NULL;
992 /* Set raw attributes on the pty. */
993 tcgetattr(slave_fd, &tty);
994 cfmakeraw(&tty);
995 tcsetattr(slave_fd, TCSAFLUSH, &tty);
996 close(slave_fd);
998 len = strlen(q_ptsname(s->fd)) + 5;
999 chr->filename = qemu_malloc(len);
1000 snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
1001 qemu_opt_set(opts, "path", q_ptsname(s->fd));
1002 fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
1004 chr->opaque = s;
1005 chr->chr_write = pty_chr_write;
1006 chr->chr_update_read_handler = pty_chr_update_read_handler;
1007 chr->chr_close = pty_chr_close;
1009 s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
1011 return chr;
1014 static void tty_serial_init(int fd, int speed,
1015 int parity, int data_bits, int stop_bits)
1017 struct termios tty;
1018 speed_t spd;
1020 #if 0
1021 printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
1022 speed, parity, data_bits, stop_bits);
1023 #endif
1024 tcgetattr (fd, &tty);
1025 if (!term_atexit_done) {
1026 oldtty = tty;
1029 #define check_speed(val) if (speed <= val) { spd = B##val; break; }
1030 speed = speed * 10 / 11;
1031 do {
1032 check_speed(50);
1033 check_speed(75);
1034 check_speed(110);
1035 check_speed(134);
1036 check_speed(150);
1037 check_speed(200);
1038 check_speed(300);
1039 check_speed(600);
1040 check_speed(1200);
1041 check_speed(1800);
1042 check_speed(2400);
1043 check_speed(4800);
1044 check_speed(9600);
1045 check_speed(19200);
1046 check_speed(38400);
1047 /* Non-Posix values follow. They may be unsupported on some systems. */
1048 check_speed(57600);
1049 check_speed(115200);
1050 #ifdef B230400
1051 check_speed(230400);
1052 #endif
1053 #ifdef B460800
1054 check_speed(460800);
1055 #endif
1056 #ifdef B500000
1057 check_speed(500000);
1058 #endif
1059 #ifdef B576000
1060 check_speed(576000);
1061 #endif
1062 #ifdef B921600
1063 check_speed(921600);
1064 #endif
1065 #ifdef B1000000
1066 check_speed(1000000);
1067 #endif
1068 #ifdef B1152000
1069 check_speed(1152000);
1070 #endif
1071 #ifdef B1500000
1072 check_speed(1500000);
1073 #endif
1074 #ifdef B2000000
1075 check_speed(2000000);
1076 #endif
1077 #ifdef B2500000
1078 check_speed(2500000);
1079 #endif
1080 #ifdef B3000000
1081 check_speed(3000000);
1082 #endif
1083 #ifdef B3500000
1084 check_speed(3500000);
1085 #endif
1086 #ifdef B4000000
1087 check_speed(4000000);
1088 #endif
1089 spd = B115200;
1090 } while (0);
1092 cfsetispeed(&tty, spd);
1093 cfsetospeed(&tty, spd);
1095 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1096 |INLCR|IGNCR|ICRNL|IXON);
1097 tty.c_oflag |= OPOST;
1098 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
1099 tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
1100 switch(data_bits) {
1101 default:
1102 case 8:
1103 tty.c_cflag |= CS8;
1104 break;
1105 case 7:
1106 tty.c_cflag |= CS7;
1107 break;
1108 case 6:
1109 tty.c_cflag |= CS6;
1110 break;
1111 case 5:
1112 tty.c_cflag |= CS5;
1113 break;
1115 switch(parity) {
1116 default:
1117 case 'N':
1118 break;
1119 case 'E':
1120 tty.c_cflag |= PARENB;
1121 break;
1122 case 'O':
1123 tty.c_cflag |= PARENB | PARODD;
1124 break;
1126 if (stop_bits == 2)
1127 tty.c_cflag |= CSTOPB;
1129 tcsetattr (fd, TCSANOW, &tty);
1132 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
1134 FDCharDriver *s = chr->opaque;
1136 switch(cmd) {
1137 case CHR_IOCTL_SERIAL_SET_PARAMS:
1139 QEMUSerialSetParams *ssp = arg;
1140 tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
1141 ssp->data_bits, ssp->stop_bits);
1143 break;
1144 case CHR_IOCTL_SERIAL_SET_BREAK:
1146 int enable = *(int *)arg;
1147 if (enable)
1148 tcsendbreak(s->fd_in, 1);
1150 break;
1151 case CHR_IOCTL_SERIAL_GET_TIOCM:
1153 int sarg = 0;
1154 int *targ = (int *)arg;
1155 ioctl(s->fd_in, TIOCMGET, &sarg);
1156 *targ = 0;
1157 if (sarg & TIOCM_CTS)
1158 *targ |= CHR_TIOCM_CTS;
1159 if (sarg & TIOCM_CAR)
1160 *targ |= CHR_TIOCM_CAR;
1161 if (sarg & TIOCM_DSR)
1162 *targ |= CHR_TIOCM_DSR;
1163 if (sarg & TIOCM_RI)
1164 *targ |= CHR_TIOCM_RI;
1165 if (sarg & TIOCM_DTR)
1166 *targ |= CHR_TIOCM_DTR;
1167 if (sarg & TIOCM_RTS)
1168 *targ |= CHR_TIOCM_RTS;
1170 break;
1171 case CHR_IOCTL_SERIAL_SET_TIOCM:
1173 int sarg = *(int *)arg;
1174 int targ = 0;
1175 ioctl(s->fd_in, TIOCMGET, &targ);
1176 targ &= ~(CHR_TIOCM_CTS | CHR_TIOCM_CAR | CHR_TIOCM_DSR
1177 | CHR_TIOCM_RI | CHR_TIOCM_DTR | CHR_TIOCM_RTS);
1178 if (sarg & CHR_TIOCM_CTS)
1179 targ |= TIOCM_CTS;
1180 if (sarg & CHR_TIOCM_CAR)
1181 targ |= TIOCM_CAR;
1182 if (sarg & CHR_TIOCM_DSR)
1183 targ |= TIOCM_DSR;
1184 if (sarg & CHR_TIOCM_RI)
1185 targ |= TIOCM_RI;
1186 if (sarg & CHR_TIOCM_DTR)
1187 targ |= TIOCM_DTR;
1188 if (sarg & CHR_TIOCM_RTS)
1189 targ |= TIOCM_RTS;
1190 ioctl(s->fd_in, TIOCMSET, &targ);
1192 break;
1193 default:
1194 return -ENOTSUP;
1196 return 0;
1199 static void tty_exit(void)
1201 tcsetattr(0, TCSANOW, &oldtty);
1204 static void qemu_chr_close_tty(CharDriverState *chr)
1206 FDCharDriver *s = chr->opaque;
1207 int fd = -1;
1209 if (s) {
1210 fd = s->fd_in;
1213 fd_chr_close(chr);
1215 if (fd >= 0) {
1216 close(fd);
1220 static CharDriverState *qemu_chr_open_tty(QemuOpts *opts)
1222 const char *filename = qemu_opt_get(opts, "path");
1223 CharDriverState *chr;
1224 int fd;
1226 TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
1227 if (fd < 0) {
1228 return NULL;
1230 tty_serial_init(fd, 115200, 'N', 8, 1);
1231 chr = qemu_chr_open_fd(fd, fd);
1232 if (!chr) {
1233 close(fd);
1234 return NULL;
1236 chr->chr_ioctl = tty_serial_ioctl;
1237 chr->chr_close = qemu_chr_close_tty;
1238 if (!term_atexit_done++)
1239 atexit(tty_exit);
1240 return chr;
1242 #else /* ! __linux__ && ! __sun__ */
1243 static CharDriverState *qemu_chr_open_pty(QemuOpts *opts)
1245 return NULL;
1247 #endif /* __linux__ || __sun__ */
1249 #if defined(__linux__)
1250 typedef struct {
1251 int fd;
1252 int mode;
1253 } ParallelCharDriver;
1255 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
1257 if (s->mode != mode) {
1258 int m = mode;
1259 if (ioctl(s->fd, PPSETMODE, &m) < 0)
1260 return 0;
1261 s->mode = mode;
1263 return 1;
1266 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1268 ParallelCharDriver *drv = chr->opaque;
1269 int fd = drv->fd;
1270 uint8_t b;
1272 switch(cmd) {
1273 case CHR_IOCTL_PP_READ_DATA:
1274 if (ioctl(fd, PPRDATA, &b) < 0)
1275 return -ENOTSUP;
1276 *(uint8_t *)arg = b;
1277 break;
1278 case CHR_IOCTL_PP_WRITE_DATA:
1279 b = *(uint8_t *)arg;
1280 if (ioctl(fd, PPWDATA, &b) < 0)
1281 return -ENOTSUP;
1282 break;
1283 case CHR_IOCTL_PP_READ_CONTROL:
1284 if (ioctl(fd, PPRCONTROL, &b) < 0)
1285 return -ENOTSUP;
1286 /* Linux gives only the lowest bits, and no way to know data
1287 direction! For better compatibility set the fixed upper
1288 bits. */
1289 *(uint8_t *)arg = b | 0xc0;
1290 break;
1291 case CHR_IOCTL_PP_WRITE_CONTROL:
1292 b = *(uint8_t *)arg;
1293 if (ioctl(fd, PPWCONTROL, &b) < 0)
1294 return -ENOTSUP;
1295 break;
1296 case CHR_IOCTL_PP_READ_STATUS:
1297 if (ioctl(fd, PPRSTATUS, &b) < 0)
1298 return -ENOTSUP;
1299 *(uint8_t *)arg = b;
1300 break;
1301 case CHR_IOCTL_PP_DATA_DIR:
1302 if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
1303 return -ENOTSUP;
1304 break;
1305 case CHR_IOCTL_PP_EPP_READ_ADDR:
1306 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1307 struct ParallelIOArg *parg = arg;
1308 int n = read(fd, parg->buffer, parg->count);
1309 if (n != parg->count) {
1310 return -EIO;
1313 break;
1314 case CHR_IOCTL_PP_EPP_READ:
1315 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1316 struct ParallelIOArg *parg = arg;
1317 int n = read(fd, parg->buffer, parg->count);
1318 if (n != parg->count) {
1319 return -EIO;
1322 break;
1323 case CHR_IOCTL_PP_EPP_WRITE_ADDR:
1324 if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
1325 struct ParallelIOArg *parg = arg;
1326 int n = write(fd, parg->buffer, parg->count);
1327 if (n != parg->count) {
1328 return -EIO;
1331 break;
1332 case CHR_IOCTL_PP_EPP_WRITE:
1333 if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
1334 struct ParallelIOArg *parg = arg;
1335 int n = write(fd, parg->buffer, parg->count);
1336 if (n != parg->count) {
1337 return -EIO;
1340 break;
1341 default:
1342 return -ENOTSUP;
1344 return 0;
1347 static void pp_close(CharDriverState *chr)
1349 ParallelCharDriver *drv = chr->opaque;
1350 int fd = drv->fd;
1352 pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
1353 ioctl(fd, PPRELEASE);
1354 close(fd);
1355 qemu_free(drv);
1356 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1359 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1361 const char *filename = qemu_opt_get(opts, "path");
1362 CharDriverState *chr;
1363 ParallelCharDriver *drv;
1364 int fd;
1366 TFR(fd = open(filename, O_RDWR));
1367 if (fd < 0)
1368 return NULL;
1370 if (ioctl(fd, PPCLAIM) < 0) {
1371 close(fd);
1372 return NULL;
1375 drv = qemu_mallocz(sizeof(ParallelCharDriver));
1376 drv->fd = fd;
1377 drv->mode = IEEE1284_MODE_COMPAT;
1379 chr = qemu_mallocz(sizeof(CharDriverState));
1380 chr->chr_write = null_chr_write;
1381 chr->chr_ioctl = pp_ioctl;
1382 chr->chr_close = pp_close;
1383 chr->opaque = drv;
1385 qemu_chr_generic_open(chr);
1387 return chr;
1389 #endif /* __linux__ */
1391 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
1392 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
1394 int fd = (int)(long)chr->opaque;
1395 uint8_t b;
1397 switch(cmd) {
1398 case CHR_IOCTL_PP_READ_DATA:
1399 if (ioctl(fd, PPIGDATA, &b) < 0)
1400 return -ENOTSUP;
1401 *(uint8_t *)arg = b;
1402 break;
1403 case CHR_IOCTL_PP_WRITE_DATA:
1404 b = *(uint8_t *)arg;
1405 if (ioctl(fd, PPISDATA, &b) < 0)
1406 return -ENOTSUP;
1407 break;
1408 case CHR_IOCTL_PP_READ_CONTROL:
1409 if (ioctl(fd, PPIGCTRL, &b) < 0)
1410 return -ENOTSUP;
1411 *(uint8_t *)arg = b;
1412 break;
1413 case CHR_IOCTL_PP_WRITE_CONTROL:
1414 b = *(uint8_t *)arg;
1415 if (ioctl(fd, PPISCTRL, &b) < 0)
1416 return -ENOTSUP;
1417 break;
1418 case CHR_IOCTL_PP_READ_STATUS:
1419 if (ioctl(fd, PPIGSTATUS, &b) < 0)
1420 return -ENOTSUP;
1421 *(uint8_t *)arg = b;
1422 break;
1423 default:
1424 return -ENOTSUP;
1426 return 0;
1429 static CharDriverState *qemu_chr_open_pp(QemuOpts *opts)
1431 const char *filename = qemu_opt_get(opts, "path");
1432 CharDriverState *chr;
1433 int fd;
1435 fd = open(filename, O_RDWR);
1436 if (fd < 0)
1437 return NULL;
1439 chr = qemu_mallocz(sizeof(CharDriverState));
1440 chr->opaque = (void *)(long)fd;
1441 chr->chr_write = null_chr_write;
1442 chr->chr_ioctl = pp_ioctl;
1443 return chr;
1445 #endif
1447 #else /* _WIN32 */
1449 typedef struct {
1450 int max_size;
1451 HANDLE hcom, hrecv, hsend;
1452 OVERLAPPED orecv, osend;
1453 BOOL fpipe;
1454 DWORD len;
1455 } WinCharState;
1457 #define NSENDBUF 2048
1458 #define NRECVBUF 2048
1459 #define MAXCONNECT 1
1460 #define NTIMEOUT 5000
1462 static int win_chr_poll(void *opaque);
1463 static int win_chr_pipe_poll(void *opaque);
1465 static void win_chr_close(CharDriverState *chr)
1467 WinCharState *s = chr->opaque;
1469 if (s->hsend) {
1470 CloseHandle(s->hsend);
1471 s->hsend = NULL;
1473 if (s->hrecv) {
1474 CloseHandle(s->hrecv);
1475 s->hrecv = NULL;
1477 if (s->hcom) {
1478 CloseHandle(s->hcom);
1479 s->hcom = NULL;
1481 if (s->fpipe)
1482 qemu_del_polling_cb(win_chr_pipe_poll, chr);
1483 else
1484 qemu_del_polling_cb(win_chr_poll, chr);
1486 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1489 static int win_chr_init(CharDriverState *chr, const char *filename)
1491 WinCharState *s = chr->opaque;
1492 COMMCONFIG comcfg;
1493 COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
1494 COMSTAT comstat;
1495 DWORD size;
1496 DWORD err;
1498 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1499 if (!s->hsend) {
1500 fprintf(stderr, "Failed CreateEvent\n");
1501 goto fail;
1503 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1504 if (!s->hrecv) {
1505 fprintf(stderr, "Failed CreateEvent\n");
1506 goto fail;
1509 s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
1510 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
1511 if (s->hcom == INVALID_HANDLE_VALUE) {
1512 fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
1513 s->hcom = NULL;
1514 goto fail;
1517 if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
1518 fprintf(stderr, "Failed SetupComm\n");
1519 goto fail;
1522 ZeroMemory(&comcfg, sizeof(COMMCONFIG));
1523 size = sizeof(COMMCONFIG);
1524 GetDefaultCommConfig(filename, &comcfg, &size);
1525 comcfg.dcb.DCBlength = sizeof(DCB);
1526 CommConfigDialog(filename, NULL, &comcfg);
1528 if (!SetCommState(s->hcom, &comcfg.dcb)) {
1529 fprintf(stderr, "Failed SetCommState\n");
1530 goto fail;
1533 if (!SetCommMask(s->hcom, EV_ERR)) {
1534 fprintf(stderr, "Failed SetCommMask\n");
1535 goto fail;
1538 cto.ReadIntervalTimeout = MAXDWORD;
1539 if (!SetCommTimeouts(s->hcom, &cto)) {
1540 fprintf(stderr, "Failed SetCommTimeouts\n");
1541 goto fail;
1544 if (!ClearCommError(s->hcom, &err, &comstat)) {
1545 fprintf(stderr, "Failed ClearCommError\n");
1546 goto fail;
1548 qemu_add_polling_cb(win_chr_poll, chr);
1549 return 0;
1551 fail:
1552 win_chr_close(chr);
1553 return -1;
1556 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
1558 WinCharState *s = chr->opaque;
1559 DWORD len, ret, size, err;
1561 len = len1;
1562 ZeroMemory(&s->osend, sizeof(s->osend));
1563 s->osend.hEvent = s->hsend;
1564 while (len > 0) {
1565 if (s->hsend)
1566 ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
1567 else
1568 ret = WriteFile(s->hcom, buf, len, &size, NULL);
1569 if (!ret) {
1570 err = GetLastError();
1571 if (err == ERROR_IO_PENDING) {
1572 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
1573 if (ret) {
1574 buf += size;
1575 len -= size;
1576 } else {
1577 break;
1579 } else {
1580 break;
1582 } else {
1583 buf += size;
1584 len -= size;
1587 return len1 - len;
1590 static int win_chr_read_poll(CharDriverState *chr)
1592 WinCharState *s = chr->opaque;
1594 s->max_size = qemu_chr_can_read(chr);
1595 return s->max_size;
1598 static void win_chr_readfile(CharDriverState *chr)
1600 WinCharState *s = chr->opaque;
1601 int ret, err;
1602 uint8_t buf[READ_BUF_LEN];
1603 DWORD size;
1605 ZeroMemory(&s->orecv, sizeof(s->orecv));
1606 s->orecv.hEvent = s->hrecv;
1607 ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
1608 if (!ret) {
1609 err = GetLastError();
1610 if (err == ERROR_IO_PENDING) {
1611 ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
1615 if (size > 0) {
1616 qemu_chr_read(chr, buf, size);
1620 static void win_chr_read(CharDriverState *chr)
1622 WinCharState *s = chr->opaque;
1624 if (s->len > s->max_size)
1625 s->len = s->max_size;
1626 if (s->len == 0)
1627 return;
1629 win_chr_readfile(chr);
1632 static int win_chr_poll(void *opaque)
1634 CharDriverState *chr = opaque;
1635 WinCharState *s = chr->opaque;
1636 COMSTAT status;
1637 DWORD comerr;
1639 ClearCommError(s->hcom, &comerr, &status);
1640 if (status.cbInQue > 0) {
1641 s->len = status.cbInQue;
1642 win_chr_read_poll(chr);
1643 win_chr_read(chr);
1644 return 1;
1646 return 0;
1649 static CharDriverState *qemu_chr_open_win(QemuOpts *opts)
1651 const char *filename = qemu_opt_get(opts, "path");
1652 CharDriverState *chr;
1653 WinCharState *s;
1655 chr = qemu_mallocz(sizeof(CharDriverState));
1656 s = qemu_mallocz(sizeof(WinCharState));
1657 chr->opaque = s;
1658 chr->chr_write = win_chr_write;
1659 chr->chr_close = win_chr_close;
1661 if (win_chr_init(chr, filename) < 0) {
1662 free(s);
1663 free(chr);
1664 return NULL;
1666 qemu_chr_generic_open(chr);
1667 return chr;
1670 static int win_chr_pipe_poll(void *opaque)
1672 CharDriverState *chr = opaque;
1673 WinCharState *s = chr->opaque;
1674 DWORD size;
1676 PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
1677 if (size > 0) {
1678 s->len = size;
1679 win_chr_read_poll(chr);
1680 win_chr_read(chr);
1681 return 1;
1683 return 0;
1686 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
1688 WinCharState *s = chr->opaque;
1689 OVERLAPPED ov;
1690 int ret;
1691 DWORD size;
1692 char openname[256];
1694 s->fpipe = TRUE;
1696 s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
1697 if (!s->hsend) {
1698 fprintf(stderr, "Failed CreateEvent\n");
1699 goto fail;
1701 s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
1702 if (!s->hrecv) {
1703 fprintf(stderr, "Failed CreateEvent\n");
1704 goto fail;
1707 snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
1708 s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
1709 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
1710 PIPE_WAIT,
1711 MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
1712 if (s->hcom == INVALID_HANDLE_VALUE) {
1713 fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
1714 s->hcom = NULL;
1715 goto fail;
1718 ZeroMemory(&ov, sizeof(ov));
1719 ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1720 ret = ConnectNamedPipe(s->hcom, &ov);
1721 if (ret) {
1722 fprintf(stderr, "Failed ConnectNamedPipe\n");
1723 goto fail;
1726 ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
1727 if (!ret) {
1728 fprintf(stderr, "Failed GetOverlappedResult\n");
1729 if (ov.hEvent) {
1730 CloseHandle(ov.hEvent);
1731 ov.hEvent = NULL;
1733 goto fail;
1736 if (ov.hEvent) {
1737 CloseHandle(ov.hEvent);
1738 ov.hEvent = NULL;
1740 qemu_add_polling_cb(win_chr_pipe_poll, chr);
1741 return 0;
1743 fail:
1744 win_chr_close(chr);
1745 return -1;
1749 static CharDriverState *qemu_chr_open_win_pipe(QemuOpts *opts)
1751 const char *filename = qemu_opt_get(opts, "path");
1752 CharDriverState *chr;
1753 WinCharState *s;
1755 chr = qemu_mallocz(sizeof(CharDriverState));
1756 s = qemu_mallocz(sizeof(WinCharState));
1757 chr->opaque = s;
1758 chr->chr_write = win_chr_write;
1759 chr->chr_close = win_chr_close;
1761 if (win_chr_pipe_init(chr, filename) < 0) {
1762 free(s);
1763 free(chr);
1764 return NULL;
1766 qemu_chr_generic_open(chr);
1767 return chr;
1770 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
1772 CharDriverState *chr;
1773 WinCharState *s;
1775 chr = qemu_mallocz(sizeof(CharDriverState));
1776 s = qemu_mallocz(sizeof(WinCharState));
1777 s->hcom = fd_out;
1778 chr->opaque = s;
1779 chr->chr_write = win_chr_write;
1780 qemu_chr_generic_open(chr);
1781 return chr;
1784 static CharDriverState *qemu_chr_open_win_con(QemuOpts *opts)
1786 return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
1789 static CharDriverState *qemu_chr_open_win_file_out(QemuOpts *opts)
1791 const char *file_out = qemu_opt_get(opts, "path");
1792 HANDLE fd_out;
1794 fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
1795 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1796 if (fd_out == INVALID_HANDLE_VALUE)
1797 return NULL;
1799 return qemu_chr_open_win_file(fd_out);
1801 #endif /* !_WIN32 */
1803 /***********************************************************/
1804 /* UDP Net console */
1806 typedef struct {
1807 int fd;
1808 uint8_t buf[READ_BUF_LEN];
1809 int bufcnt;
1810 int bufptr;
1811 int max_size;
1812 } NetCharDriver;
1814 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1816 NetCharDriver *s = chr->opaque;
1818 return send(s->fd, (const void *)buf, len, 0);
1821 static int udp_chr_read_poll(void *opaque)
1823 CharDriverState *chr = opaque;
1824 NetCharDriver *s = chr->opaque;
1826 s->max_size = qemu_chr_can_read(chr);
1828 /* If there were any stray characters in the queue process them
1829 * first
1831 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1832 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1833 s->bufptr++;
1834 s->max_size = qemu_chr_can_read(chr);
1836 return s->max_size;
1839 static void udp_chr_read(void *opaque)
1841 CharDriverState *chr = opaque;
1842 NetCharDriver *s = chr->opaque;
1844 if (s->max_size == 0)
1845 return;
1846 s->bufcnt = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1847 s->bufptr = s->bufcnt;
1848 if (s->bufcnt <= 0)
1849 return;
1851 s->bufptr = 0;
1852 while (s->max_size > 0 && s->bufptr < s->bufcnt) {
1853 qemu_chr_read(chr, &s->buf[s->bufptr], 1);
1854 s->bufptr++;
1855 s->max_size = qemu_chr_can_read(chr);
1859 static void udp_chr_update_read_handler(CharDriverState *chr)
1861 NetCharDriver *s = chr->opaque;
1863 if (s->fd >= 0) {
1864 qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
1865 udp_chr_read, NULL, chr);
1869 static void udp_chr_close(CharDriverState *chr)
1871 NetCharDriver *s = chr->opaque;
1872 if (s->fd >= 0) {
1873 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1874 closesocket(s->fd);
1876 qemu_free(s);
1877 qemu_chr_event(chr, CHR_EVENT_CLOSED);
1880 static CharDriverState *qemu_chr_open_udp(QemuOpts *opts)
1882 CharDriverState *chr = NULL;
1883 NetCharDriver *s = NULL;
1884 int fd = -1;
1886 chr = qemu_mallocz(sizeof(CharDriverState));
1887 s = qemu_mallocz(sizeof(NetCharDriver));
1889 fd = inet_dgram_opts(opts);
1890 if (fd < 0) {
1891 fprintf(stderr, "inet_dgram_opts failed\n");
1892 goto return_err;
1895 s->fd = fd;
1896 s->bufcnt = 0;
1897 s->bufptr = 0;
1898 chr->opaque = s;
1899 chr->chr_write = udp_chr_write;
1900 chr->chr_update_read_handler = udp_chr_update_read_handler;
1901 chr->chr_close = udp_chr_close;
1902 return chr;
1904 return_err:
1905 if (chr)
1906 free(chr);
1907 if (s)
1908 free(s);
1909 if (fd >= 0)
1910 closesocket(fd);
1911 return NULL;
1914 /***********************************************************/
1915 /* TCP Net console */
1917 typedef struct {
1918 int fd, listen_fd;
1919 int connected;
1920 int max_size;
1921 int do_telnetopt;
1922 int do_nodelay;
1923 int is_unix;
1924 int msgfd;
1925 } TCPCharDriver;
1927 static void tcp_chr_accept(void *opaque);
1929 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1931 TCPCharDriver *s = chr->opaque;
1932 if (s->connected) {
1933 return send_all(s->fd, buf, len);
1934 } else {
1935 /* XXX: indicate an error ? */
1936 return len;
1940 static int tcp_chr_read_poll(void *opaque)
1942 CharDriverState *chr = opaque;
1943 TCPCharDriver *s = chr->opaque;
1944 if (!s->connected)
1945 return 0;
1946 s->max_size = qemu_chr_can_read(chr);
1947 return s->max_size;
1950 #define IAC 255
1951 #define IAC_BREAK 243
1952 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
1953 TCPCharDriver *s,
1954 uint8_t *buf, int *size)
1956 /* Handle any telnet client's basic IAC options to satisfy char by
1957 * char mode with no echo. All IAC options will be removed from
1958 * the buf and the do_telnetopt variable will be used to track the
1959 * state of the width of the IAC information.
1961 * IAC commands come in sets of 3 bytes with the exception of the
1962 * "IAC BREAK" command and the double IAC.
1965 int i;
1966 int j = 0;
1968 for (i = 0; i < *size; i++) {
1969 if (s->do_telnetopt > 1) {
1970 if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
1971 /* Double IAC means send an IAC */
1972 if (j != i)
1973 buf[j] = buf[i];
1974 j++;
1975 s->do_telnetopt = 1;
1976 } else {
1977 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
1978 /* Handle IAC break commands by sending a serial break */
1979 qemu_chr_event(chr, CHR_EVENT_BREAK);
1980 s->do_telnetopt++;
1982 s->do_telnetopt++;
1984 if (s->do_telnetopt >= 4) {
1985 s->do_telnetopt = 1;
1987 } else {
1988 if ((unsigned char)buf[i] == IAC) {
1989 s->do_telnetopt = 2;
1990 } else {
1991 if (j != i)
1992 buf[j] = buf[i];
1993 j++;
1997 *size = j;
2000 static int tcp_get_msgfd(CharDriverState *chr)
2002 TCPCharDriver *s = chr->opaque;
2003 int fd = s->msgfd;
2004 s->msgfd = -1;
2005 return fd;
2008 #ifndef _WIN32
2009 static void unix_process_msgfd(CharDriverState *chr, struct msghdr *msg)
2011 TCPCharDriver *s = chr->opaque;
2012 struct cmsghdr *cmsg;
2014 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
2015 int fd;
2017 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
2018 cmsg->cmsg_level != SOL_SOCKET ||
2019 cmsg->cmsg_type != SCM_RIGHTS)
2020 continue;
2022 fd = *((int *)CMSG_DATA(cmsg));
2023 if (fd < 0)
2024 continue;
2026 if (s->msgfd != -1)
2027 close(s->msgfd);
2028 s->msgfd = fd;
2032 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2034 TCPCharDriver *s = chr->opaque;
2035 struct msghdr msg = { NULL, };
2036 struct iovec iov[1];
2037 union {
2038 struct cmsghdr cmsg;
2039 char control[CMSG_SPACE(sizeof(int))];
2040 } msg_control;
2041 ssize_t ret;
2043 iov[0].iov_base = buf;
2044 iov[0].iov_len = len;
2046 msg.msg_iov = iov;
2047 msg.msg_iovlen = 1;
2048 msg.msg_control = &msg_control;
2049 msg.msg_controllen = sizeof(msg_control);
2051 ret = recvmsg(s->fd, &msg, 0);
2052 if (ret > 0 && s->is_unix)
2053 unix_process_msgfd(chr, &msg);
2055 return ret;
2057 #else
2058 static ssize_t tcp_chr_recv(CharDriverState *chr, char *buf, size_t len)
2060 TCPCharDriver *s = chr->opaque;
2061 return recv(s->fd, buf, len, 0);
2063 #endif
2065 static void tcp_chr_read(void *opaque)
2067 CharDriverState *chr = opaque;
2068 TCPCharDriver *s = chr->opaque;
2069 uint8_t buf[READ_BUF_LEN];
2070 int len, size;
2072 if (!s->connected || s->max_size <= 0)
2073 return;
2074 len = sizeof(buf);
2075 if (len > s->max_size)
2076 len = s->max_size;
2077 size = tcp_chr_recv(chr, (void *)buf, len);
2078 if (size == 0) {
2079 /* connection closed */
2080 s->connected = 0;
2081 if (s->listen_fd >= 0) {
2082 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2084 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2085 closesocket(s->fd);
2086 s->fd = -1;
2087 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2088 } else if (size > 0) {
2089 if (s->do_telnetopt)
2090 tcp_chr_process_IAC_bytes(chr, s, buf, &size);
2091 if (size > 0)
2092 qemu_chr_read(chr, buf, size);
2096 static void tcp_chr_connect(void *opaque)
2098 CharDriverState *chr = opaque;
2099 TCPCharDriver *s = chr->opaque;
2101 s->connected = 1;
2102 qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
2103 tcp_chr_read, NULL, chr);
2104 qemu_chr_generic_open(chr);
2107 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
2108 static void tcp_chr_telnet_init(int fd)
2110 char buf[3];
2111 /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
2112 IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
2113 send(fd, (char *)buf, 3, 0);
2114 IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
2115 send(fd, (char *)buf, 3, 0);
2116 IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
2117 send(fd, (char *)buf, 3, 0);
2118 IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
2119 send(fd, (char *)buf, 3, 0);
2122 static void socket_set_nodelay(int fd)
2124 int val = 1;
2125 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2128 static void tcp_chr_accept(void *opaque)
2130 CharDriverState *chr = opaque;
2131 TCPCharDriver *s = chr->opaque;
2132 struct sockaddr_in saddr;
2133 #ifndef _WIN32
2134 struct sockaddr_un uaddr;
2135 #endif
2136 struct sockaddr *addr;
2137 socklen_t len;
2138 int fd;
2140 for(;;) {
2141 #ifndef _WIN32
2142 if (s->is_unix) {
2143 len = sizeof(uaddr);
2144 addr = (struct sockaddr *)&uaddr;
2145 } else
2146 #endif
2148 len = sizeof(saddr);
2149 addr = (struct sockaddr *)&saddr;
2151 fd = qemu_accept(s->listen_fd, addr, &len);
2152 if (fd < 0 && errno != EINTR) {
2153 return;
2154 } else if (fd >= 0) {
2155 if (s->do_telnetopt)
2156 tcp_chr_telnet_init(fd);
2157 break;
2160 socket_set_nonblock(fd);
2161 if (s->do_nodelay)
2162 socket_set_nodelay(fd);
2163 s->fd = fd;
2164 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2165 tcp_chr_connect(chr);
2168 static void tcp_chr_close(CharDriverState *chr)
2170 TCPCharDriver *s = chr->opaque;
2171 if (s->fd >= 0) {
2172 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2173 closesocket(s->fd);
2175 if (s->listen_fd >= 0) {
2176 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
2177 closesocket(s->listen_fd);
2179 qemu_free(s);
2180 qemu_chr_event(chr, CHR_EVENT_CLOSED);
2183 static CharDriverState *qemu_chr_open_socket(QemuOpts *opts)
2185 CharDriverState *chr = NULL;
2186 TCPCharDriver *s = NULL;
2187 int fd = -1;
2188 int is_listen;
2189 int is_waitconnect;
2190 int do_nodelay;
2191 int is_unix;
2192 int is_telnet;
2194 is_listen = qemu_opt_get_bool(opts, "server", 0);
2195 is_waitconnect = qemu_opt_get_bool(opts, "wait", 1);
2196 is_telnet = qemu_opt_get_bool(opts, "telnet", 0);
2197 do_nodelay = !qemu_opt_get_bool(opts, "delay", 1);
2198 is_unix = qemu_opt_get(opts, "path") != NULL;
2199 if (!is_listen)
2200 is_waitconnect = 0;
2202 chr = qemu_mallocz(sizeof(CharDriverState));
2203 s = qemu_mallocz(sizeof(TCPCharDriver));
2205 if (is_unix) {
2206 if (is_listen) {
2207 fd = unix_listen_opts(opts);
2208 } else {
2209 fd = unix_connect_opts(opts);
2211 } else {
2212 if (is_listen) {
2213 fd = inet_listen_opts(opts, 0);
2214 } else {
2215 fd = inet_connect_opts(opts);
2218 if (fd < 0)
2219 goto fail;
2221 if (!is_waitconnect)
2222 socket_set_nonblock(fd);
2224 s->connected = 0;
2225 s->fd = -1;
2226 s->listen_fd = -1;
2227 s->msgfd = -1;
2228 s->is_unix = is_unix;
2229 s->do_nodelay = do_nodelay && !is_unix;
2231 chr->opaque = s;
2232 chr->chr_write = tcp_chr_write;
2233 chr->chr_close = tcp_chr_close;
2234 chr->get_msgfd = tcp_get_msgfd;
2236 if (is_listen) {
2237 s->listen_fd = fd;
2238 qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
2239 if (is_telnet)
2240 s->do_telnetopt = 1;
2242 } else {
2243 s->connected = 1;
2244 s->fd = fd;
2245 socket_set_nodelay(fd);
2246 tcp_chr_connect(chr);
2249 /* for "info chardev" monitor command */
2250 chr->filename = qemu_malloc(256);
2251 if (is_unix) {
2252 snprintf(chr->filename, 256, "unix:%s%s",
2253 qemu_opt_get(opts, "path"),
2254 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2255 } else if (is_telnet) {
2256 snprintf(chr->filename, 256, "telnet:%s:%s%s",
2257 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2258 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2259 } else {
2260 snprintf(chr->filename, 256, "tcp:%s:%s%s",
2261 qemu_opt_get(opts, "host"), qemu_opt_get(opts, "port"),
2262 qemu_opt_get_bool(opts, "server", 0) ? ",server" : "");
2265 if (is_listen && is_waitconnect) {
2266 printf("QEMU waiting for connection on: %s\n",
2267 chr->filename);
2268 tcp_chr_accept(chr);
2269 socket_set_nonblock(s->listen_fd);
2271 return chr;
2273 fail:
2274 if (fd >= 0)
2275 closesocket(fd);
2276 qemu_free(s);
2277 qemu_free(chr);
2278 return NULL;
2281 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
2283 char host[65], port[33], width[8], height[8];
2284 int pos;
2285 const char *p;
2286 QemuOpts *opts;
2288 opts = qemu_opts_create(&qemu_chardev_opts, label, 1);
2289 if (NULL == opts)
2290 return NULL;
2292 if (strstart(filename, "mon:", &p)) {
2293 filename = p;
2294 qemu_opt_set(opts, "mux", "on");
2297 if (strcmp(filename, "null") == 0 ||
2298 strcmp(filename, "pty") == 0 ||
2299 strcmp(filename, "msmouse") == 0 ||
2300 strcmp(filename, "braille") == 0 ||
2301 strcmp(filename, "stdio") == 0) {
2302 qemu_opt_set(opts, "backend", filename);
2303 return opts;
2305 if (strstart(filename, "vc", &p)) {
2306 qemu_opt_set(opts, "backend", "vc");
2307 if (*p == ':') {
2308 if (sscanf(p+1, "%8[0-9]x%8[0-9]", width, height) == 2) {
2309 /* pixels */
2310 qemu_opt_set(opts, "width", width);
2311 qemu_opt_set(opts, "height", height);
2312 } else if (sscanf(p+1, "%8[0-9]Cx%8[0-9]C", width, height) == 2) {
2313 /* chars */
2314 qemu_opt_set(opts, "cols", width);
2315 qemu_opt_set(opts, "rows", height);
2316 } else {
2317 goto fail;
2320 return opts;
2322 if (strcmp(filename, "con:") == 0) {
2323 qemu_opt_set(opts, "backend", "console");
2324 return opts;
2326 if (strstart(filename, "COM", NULL)) {
2327 qemu_opt_set(opts, "backend", "serial");
2328 qemu_opt_set(opts, "path", filename);
2329 return opts;
2331 if (strstart(filename, "file:", &p)) {
2332 qemu_opt_set(opts, "backend", "file");
2333 qemu_opt_set(opts, "path", p);
2334 return opts;
2336 if (strstart(filename, "pipe:", &p)) {
2337 qemu_opt_set(opts, "backend", "pipe");
2338 qemu_opt_set(opts, "path", p);
2339 return opts;
2341 if (strstart(filename, "tcp:", &p) ||
2342 strstart(filename, "telnet:", &p)) {
2343 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2344 host[0] = 0;
2345 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1)
2346 goto fail;
2348 qemu_opt_set(opts, "backend", "socket");
2349 qemu_opt_set(opts, "host", host);
2350 qemu_opt_set(opts, "port", port);
2351 if (p[pos] == ',') {
2352 if (qemu_opts_do_parse(opts, p+pos+1, NULL) != 0)
2353 goto fail;
2355 if (strstart(filename, "telnet:", &p))
2356 qemu_opt_set(opts, "telnet", "on");
2357 return opts;
2359 if (strstart(filename, "udp:", &p)) {
2360 qemu_opt_set(opts, "backend", "udp");
2361 if (sscanf(p, "%64[^:]:%32[^@,]%n", host, port, &pos) < 2) {
2362 host[0] = 0;
2363 if (sscanf(p, ":%32[^@,]%n", port, &pos) < 1) {
2364 goto fail;
2367 qemu_opt_set(opts, "host", host);
2368 qemu_opt_set(opts, "port", port);
2369 if (p[pos] == '@') {
2370 p += pos + 1;
2371 if (sscanf(p, "%64[^:]:%32[^,]%n", host, port, &pos) < 2) {
2372 host[0] = 0;
2373 if (sscanf(p, ":%32[^,]%n", port, &pos) < 1) {
2374 goto fail;
2377 qemu_opt_set(opts, "localaddr", host);
2378 qemu_opt_set(opts, "localport", port);
2380 return opts;
2382 if (strstart(filename, "unix:", &p)) {
2383 qemu_opt_set(opts, "backend", "socket");
2384 if (qemu_opts_do_parse(opts, p, "path") != 0)
2385 goto fail;
2386 return opts;
2388 if (strstart(filename, "/dev/parport", NULL) ||
2389 strstart(filename, "/dev/ppi", NULL)) {
2390 qemu_opt_set(opts, "backend", "parport");
2391 qemu_opt_set(opts, "path", filename);
2392 return opts;
2394 if (strstart(filename, "/dev/", NULL)) {
2395 qemu_opt_set(opts, "backend", "tty");
2396 qemu_opt_set(opts, "path", filename);
2397 return opts;
2400 fail:
2401 qemu_opts_del(opts);
2402 return NULL;
2405 static const struct {
2406 const char *name;
2407 CharDriverState *(*open)(QemuOpts *opts);
2408 } backend_table[] = {
2409 { .name = "null", .open = qemu_chr_open_null },
2410 { .name = "socket", .open = qemu_chr_open_socket },
2411 { .name = "udp", .open = qemu_chr_open_udp },
2412 { .name = "msmouse", .open = qemu_chr_open_msmouse },
2413 { .name = "vc", .open = text_console_init },
2414 #ifdef _WIN32
2415 { .name = "file", .open = qemu_chr_open_win_file_out },
2416 { .name = "pipe", .open = qemu_chr_open_win_pipe },
2417 { .name = "console", .open = qemu_chr_open_win_con },
2418 { .name = "serial", .open = qemu_chr_open_win },
2419 #else
2420 { .name = "file", .open = qemu_chr_open_file_out },
2421 { .name = "pipe", .open = qemu_chr_open_pipe },
2422 { .name = "pty", .open = qemu_chr_open_pty },
2423 { .name = "stdio", .open = qemu_chr_open_stdio },
2424 #endif
2425 #ifdef CONFIG_BRLAPI
2426 { .name = "braille", .open = chr_baum_init },
2427 #endif
2428 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
2429 || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) \
2430 || defined(__FreeBSD_kernel__)
2431 { .name = "tty", .open = qemu_chr_open_tty },
2432 #endif
2433 #if defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) \
2434 || defined(__FreeBSD_kernel__)
2435 { .name = "parport", .open = qemu_chr_open_pp },
2436 #endif
2439 CharDriverState *qemu_chr_open_opts(QemuOpts *opts,
2440 void (*init)(struct CharDriverState *s))
2442 CharDriverState *chr;
2443 int i;
2445 if (qemu_opts_id(opts) == NULL) {
2446 fprintf(stderr, "chardev: no id specified\n");
2447 return NULL;
2450 for (i = 0; i < ARRAY_SIZE(backend_table); i++) {
2451 if (strcmp(backend_table[i].name, qemu_opt_get(opts, "backend")) == 0)
2452 break;
2454 if (i == ARRAY_SIZE(backend_table)) {
2455 fprintf(stderr, "chardev: backend \"%s\" not found\n",
2456 qemu_opt_get(opts, "backend"));
2457 return NULL;
2460 chr = backend_table[i].open(opts);
2461 if (!chr) {
2462 fprintf(stderr, "chardev: opening backend \"%s\" failed\n",
2463 qemu_opt_get(opts, "backend"));
2464 return NULL;
2467 if (!chr->filename)
2468 chr->filename = qemu_strdup(qemu_opt_get(opts, "backend"));
2469 chr->init = init;
2470 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2472 if (qemu_opt_get_bool(opts, "mux", 0)) {
2473 CharDriverState *base = chr;
2474 int len = strlen(qemu_opts_id(opts)) + 6;
2475 base->label = qemu_malloc(len);
2476 snprintf(base->label, len, "%s-base", qemu_opts_id(opts));
2477 chr = qemu_chr_open_mux(base);
2478 chr->filename = base->filename;
2479 QTAILQ_INSERT_TAIL(&chardevs, chr, next);
2481 chr->label = qemu_strdup(qemu_opts_id(opts));
2482 return chr;
2485 CharDriverState *qemu_chr_open(const char *label, const char *filename, void (*init)(struct CharDriverState *s))
2487 const char *p;
2488 CharDriverState *chr;
2489 QemuOpts *opts;
2491 if (strstart(filename, "chardev:", &p)) {
2492 return qemu_chr_find(p);
2495 opts = qemu_chr_parse_compat(label, filename);
2496 if (!opts)
2497 return NULL;
2499 chr = qemu_chr_open_opts(opts, init);
2500 if (chr && qemu_opt_get_bool(opts, "mux", 0)) {
2501 monitor_init(chr, MONITOR_USE_READLINE);
2503 return chr;
2506 void qemu_chr_close(CharDriverState *chr)
2508 QTAILQ_REMOVE(&chardevs, chr, next);
2509 if (chr->chr_close)
2510 chr->chr_close(chr);
2511 qemu_free(chr->filename);
2512 qemu_free(chr->label);
2513 qemu_free(chr);
2516 static void qemu_chr_qlist_iter(QObject *obj, void *opaque)
2518 QDict *chr_dict;
2519 Monitor *mon = opaque;
2521 chr_dict = qobject_to_qdict(obj);
2522 monitor_printf(mon, "%s: filename=%s\n", qdict_get_str(chr_dict, "label"),
2523 qdict_get_str(chr_dict, "filename"));
2526 void qemu_chr_info_print(Monitor *mon, const QObject *ret_data)
2528 qlist_iter(qobject_to_qlist(ret_data), qemu_chr_qlist_iter, mon);
2532 * qemu_chr_info(): Character devices information
2534 * Each device is represented by a QDict. The returned QObject is a QList
2535 * of all devices.
2537 * The QDict contains the following:
2539 * - "label": device's label
2540 * - "filename": device's file
2542 * Example:
2544 * [ { "label": "monitor", "filename", "stdio" },
2545 * { "label": "serial0", "filename": "vc" } ]
2547 void qemu_chr_info(Monitor *mon, QObject **ret_data)
2549 QList *chr_list;
2550 CharDriverState *chr;
2552 chr_list = qlist_new();
2554 QTAILQ_FOREACH(chr, &chardevs, next) {
2555 QObject *obj = qobject_from_jsonf("{ 'label': %s, 'filename': %s }",
2556 chr->label, chr->filename);
2557 qlist_append_obj(chr_list, obj);
2560 *ret_data = QOBJECT(chr_list);
2563 CharDriverState *qemu_chr_find(const char *name)
2565 CharDriverState *chr;
2567 QTAILQ_FOREACH(chr, &chardevs, next) {
2568 if (strcmp(chr->label, name) != 0)
2569 continue;
2570 return chr;
2572 return NULL;