symbian-qemu-0.9.1-12/qemu-symbian-svp/qemu-char.c
changeset 1 2fb8b9db1c86
child 36 a587897e3bb2
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 /*
       
     2  * QEMU System Emulator
       
     3  *
       
     4  * Copyright (c) 2003-2008 Fabrice Bellard
       
     5  *
       
     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:
       
    12  *
       
    13  * The above copyright notice and this permission notice shall be included in
       
    14  * all copies or substantial portions of the Software.
       
    15  *
       
    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.
       
    23  */
       
    24 #include "qemu-common.h"
       
    25 #include "net.h"
       
    26 #include "console.h"
       
    27 #include "sysemu.h"
       
    28 #include "qemu-timer.h"
       
    29 #include "qemu-char.h"
       
    30 #include "block.h"
       
    31 #include "hw/usb.h"
       
    32 #include "hw/baum.h"
       
    33 
       
    34 #include <unistd.h>
       
    35 #include <fcntl.h>
       
    36 #include <signal.h>
       
    37 #include <time.h>
       
    38 #include <errno.h>
       
    39 #include <sys/time.h>
       
    40 #include <zlib.h>
       
    41 
       
    42 #ifndef _WIN32
       
    43 #include <sys/times.h>
       
    44 #include <sys/wait.h>
       
    45 #include <termios.h>
       
    46 #include <sys/mman.h>
       
    47 #include <sys/ioctl.h>
       
    48 #include <sys/resource.h>
       
    49 #include <sys/socket.h>
       
    50 #include <netinet/in.h>
       
    51 #include <net/if.h>
       
    52 #ifdef __NetBSD__
       
    53 #include <net/if_tap.h>
       
    54 #endif
       
    55 #ifdef __linux__
       
    56 #include <linux/if_tun.h>
       
    57 #endif
       
    58 #include <arpa/inet.h>
       
    59 #include <dirent.h>
       
    60 #include <netdb.h>
       
    61 #include <sys/select.h>
       
    62 #ifdef _BSD
       
    63 #include <sys/stat.h>
       
    64 #ifdef __FreeBSD__
       
    65 #include <libutil.h>
       
    66 #include <dev/ppbus/ppi.h>
       
    67 #include <dev/ppbus/ppbconf.h>
       
    68 #else
       
    69 #include <util.h>
       
    70 #endif
       
    71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
       
    72 #include <freebsd/stdlib.h>
       
    73 #else
       
    74 #ifdef __linux__
       
    75 #include <pty.h>
       
    76 
       
    77 #include <linux/ppdev.h>
       
    78 #include <linux/parport.h>
       
    79 #endif
       
    80 #ifdef __sun__
       
    81 #include <sys/stat.h>
       
    82 #include <sys/ethernet.h>
       
    83 #include <sys/sockio.h>
       
    84 #include <netinet/arp.h>
       
    85 #include <netinet/in.h>
       
    86 #include <netinet/in_systm.h>
       
    87 #include <netinet/ip.h>
       
    88 #include <netinet/ip_icmp.h> // must come after ip.h
       
    89 #include <netinet/udp.h>
       
    90 #include <netinet/tcp.h>
       
    91 #include <net/if.h>
       
    92 #include <syslog.h>
       
    93 #include <stropts.h>
       
    94 #endif
       
    95 #endif
       
    96 #endif
       
    97 
       
    98 #include "qemu_socket.h"
       
    99 
       
   100 /***********************************************************/
       
   101 /* character device */
       
   102 
       
   103 static void qemu_chr_event(CharDriverState *s, int event)
       
   104 {
       
   105     if (!s->chr_event)
       
   106         return;
       
   107     s->chr_event(s->handler_opaque, event);
       
   108 }
       
   109 
       
   110 static void qemu_chr_reset_bh(void *opaque)
       
   111 {
       
   112     CharDriverState *s = opaque;
       
   113     qemu_chr_event(s, CHR_EVENT_RESET);
       
   114     qemu_bh_delete(s->bh);
       
   115     s->bh = NULL;
       
   116 }
       
   117 
       
   118 void qemu_chr_reset(CharDriverState *s)
       
   119 {
       
   120     if (s->bh == NULL) {
       
   121 	s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
       
   122 	qemu_bh_schedule(s->bh);
       
   123     }
       
   124 }
       
   125 
       
   126 int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
       
   127 {
       
   128     return s->chr_write(s, buf, len);
       
   129 }
       
   130 
       
   131 int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
       
   132 {
       
   133     if (!s->chr_ioctl)
       
   134         return -ENOTSUP;
       
   135     return s->chr_ioctl(s, cmd, arg);
       
   136 }
       
   137 
       
   138 int qemu_chr_can_read(CharDriverState *s)
       
   139 {
       
   140     if (!s->chr_can_read)
       
   141         return 0;
       
   142     return s->chr_can_read(s->handler_opaque);
       
   143 }
       
   144 
       
   145 void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
       
   146 {
       
   147     s->chr_read(s->handler_opaque, buf, len);
       
   148 }
       
   149 
       
   150 void qemu_chr_accept_input(CharDriverState *s)
       
   151 {
       
   152     if (s->chr_accept_input)
       
   153         s->chr_accept_input(s);
       
   154 }
       
   155 
       
   156 void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
       
   157 {
       
   158     char buf[4096];
       
   159     va_list ap;
       
   160     va_start(ap, fmt);
       
   161     vsnprintf(buf, sizeof(buf), fmt, ap);
       
   162     qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
       
   163     va_end(ap);
       
   164 }
       
   165 
       
   166 void qemu_chr_send_event(CharDriverState *s, int event)
       
   167 {
       
   168     if (s->chr_send_event)
       
   169         s->chr_send_event(s, event);
       
   170 }
       
   171 
       
   172 void qemu_chr_add_handlers(CharDriverState *s,
       
   173                            IOCanRWHandler *fd_can_read,
       
   174                            IOReadHandler *fd_read,
       
   175                            IOEventHandler *fd_event,
       
   176                            void *opaque)
       
   177 {
       
   178     s->chr_can_read = fd_can_read;
       
   179     s->chr_read = fd_read;
       
   180     s->chr_event = fd_event;
       
   181     s->handler_opaque = opaque;
       
   182     if (s->chr_update_read_handler)
       
   183         s->chr_update_read_handler(s);
       
   184 }
       
   185 
       
   186 static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
       
   187 {
       
   188     return len;
       
   189 }
       
   190 
       
   191 static CharDriverState *qemu_chr_open_null(void)
       
   192 {
       
   193     CharDriverState *chr;
       
   194 
       
   195     chr = qemu_mallocz(sizeof(CharDriverState));
       
   196     if (!chr)
       
   197         return NULL;
       
   198     chr->chr_write = null_chr_write;
       
   199     return chr;
       
   200 }
       
   201 
       
   202 /* MUX driver for serial I/O splitting */
       
   203 static int term_timestamps;
       
   204 static int64_t term_timestamps_start;
       
   205 #define MAX_MUX 4
       
   206 #define MUX_BUFFER_SIZE 32	/* Must be a power of 2.  */
       
   207 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
       
   208 typedef struct {
       
   209     IOCanRWHandler *chr_can_read[MAX_MUX];
       
   210     IOReadHandler *chr_read[MAX_MUX];
       
   211     IOEventHandler *chr_event[MAX_MUX];
       
   212     void *ext_opaque[MAX_MUX];
       
   213     CharDriverState *drv;
       
   214     unsigned char buffer[MUX_BUFFER_SIZE];
       
   215     int prod;
       
   216     int cons;
       
   217     int mux_cnt;
       
   218     int term_got_escape;
       
   219     int max_size;
       
   220 } MuxDriver;
       
   221 
       
   222 
       
   223 static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
       
   224 {
       
   225     MuxDriver *d = chr->opaque;
       
   226     int ret;
       
   227     if (!term_timestamps) {
       
   228         ret = d->drv->chr_write(d->drv, buf, len);
       
   229     } else {
       
   230         int i;
       
   231 
       
   232         ret = 0;
       
   233         for(i = 0; i < len; i++) {
       
   234             ret += d->drv->chr_write(d->drv, buf+i, 1);
       
   235             if (buf[i] == '\n') {
       
   236                 char buf1[64];
       
   237                 int64_t ti;
       
   238                 int secs;
       
   239 
       
   240                 ti = qemu_get_clock(rt_clock);
       
   241                 if (term_timestamps_start == -1)
       
   242                     term_timestamps_start = ti;
       
   243                 ti -= term_timestamps_start;
       
   244                 secs = ti / 1000000000;
       
   245                 snprintf(buf1, sizeof(buf1),
       
   246                          "[%02d:%02d:%02d.%03d] ",
       
   247                          secs / 3600,
       
   248                          (secs / 60) % 60,
       
   249                          secs % 60,
       
   250                          (int)((ti / 1000000) % 1000));
       
   251                 d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
       
   252             }
       
   253         }
       
   254     }
       
   255     return ret;
       
   256 }
       
   257 
       
   258 static const char * const mux_help[] = {
       
   259     "% h    print this help\n\r",
       
   260     "% x    exit emulator\n\r",
       
   261     "% s    save disk data back to file (if -snapshot)\n\r",
       
   262     "% t    toggle console timestamps\n\r"
       
   263     "% b    send break (magic sysrq)\n\r",
       
   264     "% c    switch between console and monitor\n\r",
       
   265     "% %  sends %\n\r",
       
   266     NULL
       
   267 };
       
   268 
       
   269 int term_escape_char = 0x01; /* ctrl-a is used for escape */
       
   270 static void mux_print_help(CharDriverState *chr)
       
   271 {
       
   272     int i, j;
       
   273     char ebuf[15] = "Escape-Char";
       
   274     char cbuf[50] = "\n\r";
       
   275 
       
   276     if (term_escape_char > 0 && term_escape_char < 26) {
       
   277         snprintf(cbuf, sizeof(cbuf), "\n\r");
       
   278         snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a');
       
   279     } else {
       
   280         snprintf(cbuf, sizeof(cbuf),
       
   281                  "\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
       
   282                  term_escape_char);
       
   283     }
       
   284     chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
       
   285     for (i = 0; mux_help[i] != NULL; i++) {
       
   286         for (j=0; mux_help[i][j] != '\0'; j++) {
       
   287             if (mux_help[i][j] == '%')
       
   288                 chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
       
   289             else
       
   290                 chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
       
   291         }
       
   292     }
       
   293 }
       
   294 
       
   295 static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
       
   296 {
       
   297     if (d->term_got_escape) {
       
   298         d->term_got_escape = 0;
       
   299         if (ch == term_escape_char)
       
   300             goto send_char;
       
   301         switch(ch) {
       
   302         case '?':
       
   303         case 'h':
       
   304             mux_print_help(chr);
       
   305             break;
       
   306         case 'x':
       
   307             {
       
   308                  const char *term =  "QEMU: Terminated\n\r";
       
   309                  chr->chr_write(chr,(uint8_t *)term,strlen(term));
       
   310                  exit(0);
       
   311                  break;
       
   312             }
       
   313         case 's':
       
   314             {
       
   315                 int i;
       
   316                 for (i = 0; i < nb_drives; i++) {
       
   317                         bdrv_commit(drives_table[i].bdrv);
       
   318                 }
       
   319             }
       
   320             break;
       
   321         case 'b':
       
   322             qemu_chr_event(chr, CHR_EVENT_BREAK);
       
   323             break;
       
   324         case 'c':
       
   325             /* Switch to the next registered device */
       
   326             chr->focus++;
       
   327             if (chr->focus >= d->mux_cnt)
       
   328                 chr->focus = 0;
       
   329             break;
       
   330        case 't':
       
   331            term_timestamps = !term_timestamps;
       
   332            term_timestamps_start = -1;
       
   333            break;
       
   334         }
       
   335     } else if (ch == term_escape_char) {
       
   336         d->term_got_escape = 1;
       
   337     } else {
       
   338     send_char:
       
   339         return 1;
       
   340     }
       
   341     return 0;
       
   342 }
       
   343 
       
   344 static void mux_chr_accept_input(CharDriverState *chr)
       
   345 {
       
   346     int m = chr->focus;
       
   347     MuxDriver *d = chr->opaque;
       
   348 
       
   349     while (d->prod != d->cons &&
       
   350            d->chr_can_read[m] &&
       
   351            d->chr_can_read[m](d->ext_opaque[m])) {
       
   352         d->chr_read[m](d->ext_opaque[m],
       
   353                        &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
       
   354     }
       
   355 }
       
   356 
       
   357 static int mux_chr_can_read(void *opaque)
       
   358 {
       
   359     CharDriverState *chr = opaque;
       
   360     MuxDriver *d = chr->opaque;
       
   361 
       
   362     if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
       
   363         return 1;
       
   364     if (d->chr_can_read[chr->focus])
       
   365         return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
       
   366     return 0;
       
   367 }
       
   368 
       
   369 static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
       
   370 {
       
   371     CharDriverState *chr = opaque;
       
   372     MuxDriver *d = chr->opaque;
       
   373     int m = chr->focus;
       
   374     int i;
       
   375 
       
   376     mux_chr_accept_input (opaque);
       
   377 
       
   378     for(i = 0; i < size; i++)
       
   379         if (mux_proc_byte(chr, d, buf[i])) {
       
   380             if (d->prod == d->cons &&
       
   381                 d->chr_can_read[m] &&
       
   382                 d->chr_can_read[m](d->ext_opaque[m]))
       
   383                 d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
       
   384             else
       
   385                 d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
       
   386         }
       
   387 }
       
   388 
       
   389 static void mux_chr_event(void *opaque, int event)
       
   390 {
       
   391     CharDriverState *chr = opaque;
       
   392     MuxDriver *d = chr->opaque;
       
   393     int i;
       
   394 
       
   395     /* Send the event to all registered listeners */
       
   396     for (i = 0; i < d->mux_cnt; i++)
       
   397         if (d->chr_event[i])
       
   398             d->chr_event[i](d->ext_opaque[i], event);
       
   399 }
       
   400 
       
   401 static void mux_chr_update_read_handler(CharDriverState *chr)
       
   402 {
       
   403     MuxDriver *d = chr->opaque;
       
   404 
       
   405     if (d->mux_cnt >= MAX_MUX) {
       
   406         fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
       
   407         return;
       
   408     }
       
   409     d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
       
   410     d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
       
   411     d->chr_read[d->mux_cnt] = chr->chr_read;
       
   412     d->chr_event[d->mux_cnt] = chr->chr_event;
       
   413     /* Fix up the real driver with mux routines */
       
   414     if (d->mux_cnt == 0) {
       
   415         qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
       
   416                               mux_chr_event, chr);
       
   417     }
       
   418     chr->focus = d->mux_cnt;
       
   419     d->mux_cnt++;
       
   420 }
       
   421 
       
   422 static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
       
   423 {
       
   424     CharDriverState *chr;
       
   425     MuxDriver *d;
       
   426 
       
   427     chr = qemu_mallocz(sizeof(CharDriverState));
       
   428     if (!chr)
       
   429         return NULL;
       
   430     d = qemu_mallocz(sizeof(MuxDriver));
       
   431     if (!d) {
       
   432         free(chr);
       
   433         return NULL;
       
   434     }
       
   435 
       
   436     chr->opaque = d;
       
   437     d->drv = drv;
       
   438     chr->focus = -1;
       
   439     chr->chr_write = mux_chr_write;
       
   440     chr->chr_update_read_handler = mux_chr_update_read_handler;
       
   441     chr->chr_accept_input = mux_chr_accept_input;
       
   442     return chr;
       
   443 }
       
   444 
       
   445 
       
   446 #ifdef _WIN32
       
   447 int send_all(int fd, const void *buf, int len1)
       
   448 {
       
   449     int ret, len;
       
   450 
       
   451     len = len1;
       
   452     while (len > 0) {
       
   453         ret = send(fd, buf, len, 0);
       
   454         if (ret < 0) {
       
   455             errno = WSAGetLastError();
       
   456             if (errno != WSAEWOULDBLOCK) {
       
   457                 return -1;
       
   458             }
       
   459         } else if (ret == 0) {
       
   460             break;
       
   461         } else {
       
   462             buf += ret;
       
   463             len -= ret;
       
   464         }
       
   465     }
       
   466     return len1 - len;
       
   467 }
       
   468 
       
   469 #else
       
   470 
       
   471 static int unix_write(int fd, const uint8_t *buf, int len1)
       
   472 {
       
   473     int ret, len;
       
   474 
       
   475     len = len1;
       
   476     while (len > 0) {
       
   477         ret = write(fd, buf, len);
       
   478         if (ret < 0) {
       
   479             if (errno != EINTR && errno != EAGAIN)
       
   480                 return -1;
       
   481         } else if (ret == 0) {
       
   482             break;
       
   483         } else {
       
   484             buf += ret;
       
   485             len -= ret;
       
   486         }
       
   487     }
       
   488     return len1 - len;
       
   489 }
       
   490 
       
   491 int send_all(int fd, const void *buf, int len1)
       
   492 {
       
   493     return unix_write(fd, buf, len1);
       
   494 }
       
   495 #endif /* !_WIN32 */
       
   496 
       
   497 #ifndef _WIN32
       
   498 
       
   499 typedef struct {
       
   500     int fd_in, fd_out;
       
   501     int max_size;
       
   502 } FDCharDriver;
       
   503 
       
   504 #define STDIO_MAX_CLIENTS 1
       
   505 static int stdio_nb_clients = 0;
       
   506 
       
   507 static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
       
   508 {
       
   509     FDCharDriver *s = chr->opaque;
       
   510     return send_all(s->fd_out, buf, len);
       
   511 }
       
   512 
       
   513 static int fd_chr_read_poll(void *opaque)
       
   514 {
       
   515     CharDriverState *chr = opaque;
       
   516     FDCharDriver *s = chr->opaque;
       
   517 
       
   518     s->max_size = qemu_chr_can_read(chr);
       
   519     return s->max_size;
       
   520 }
       
   521 
       
   522 static void fd_chr_read(void *opaque)
       
   523 {
       
   524     CharDriverState *chr = opaque;
       
   525     FDCharDriver *s = chr->opaque;
       
   526     int size, len;
       
   527     uint8_t buf[1024];
       
   528 
       
   529     len = sizeof(buf);
       
   530     if (len > s->max_size)
       
   531         len = s->max_size;
       
   532     if (len == 0)
       
   533         return;
       
   534     size = read(s->fd_in, buf, len);
       
   535     if (size == 0) {
       
   536         /* FD has been closed. Remove it from the active list.  */
       
   537         qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
       
   538         return;
       
   539     }
       
   540     if (size > 0) {
       
   541         qemu_chr_read(chr, buf, size);
       
   542     }
       
   543 }
       
   544 
       
   545 static void fd_chr_update_read_handler(CharDriverState *chr)
       
   546 {
       
   547     FDCharDriver *s = chr->opaque;
       
   548 
       
   549     if (s->fd_in >= 0) {
       
   550         if (nographic && s->fd_in == 0) {
       
   551         } else {
       
   552             qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
       
   553                                  fd_chr_read, NULL, chr);
       
   554         }
       
   555     }
       
   556 }
       
   557 
       
   558 static void fd_chr_close(struct CharDriverState *chr)
       
   559 {
       
   560     FDCharDriver *s = chr->opaque;
       
   561 
       
   562     if (s->fd_in >= 0) {
       
   563         if (nographic && s->fd_in == 0) {
       
   564         } else {
       
   565             qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
       
   566         }
       
   567     }
       
   568 
       
   569     qemu_free(s);
       
   570 }
       
   571 
       
   572 /* open a character device to a unix fd */
       
   573 static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
       
   574 {
       
   575     CharDriverState *chr;
       
   576     FDCharDriver *s;
       
   577 
       
   578     chr = qemu_mallocz(sizeof(CharDriverState));
       
   579     if (!chr)
       
   580         return NULL;
       
   581     s = qemu_mallocz(sizeof(FDCharDriver));
       
   582     if (!s) {
       
   583         free(chr);
       
   584         return NULL;
       
   585     }
       
   586     s->fd_in = fd_in;
       
   587     s->fd_out = fd_out;
       
   588     chr->opaque = s;
       
   589     chr->chr_write = fd_chr_write;
       
   590     chr->chr_update_read_handler = fd_chr_update_read_handler;
       
   591     chr->chr_close = fd_chr_close;
       
   592 
       
   593     qemu_chr_reset(chr);
       
   594 
       
   595     return chr;
       
   596 }
       
   597 
       
   598 static CharDriverState *qemu_chr_open_file_out(const char *file_out)
       
   599 {
       
   600     int fd_out;
       
   601 
       
   602     TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
       
   603     if (fd_out < 0)
       
   604         return NULL;
       
   605     return qemu_chr_open_fd(-1, fd_out);
       
   606 }
       
   607 
       
   608 static CharDriverState *qemu_chr_open_pipe(const char *filename)
       
   609 {
       
   610     int fd_in, fd_out;
       
   611     char filename_in[256], filename_out[256];
       
   612 
       
   613     snprintf(filename_in, 256, "%s.in", filename);
       
   614     snprintf(filename_out, 256, "%s.out", filename);
       
   615     TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
       
   616     TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
       
   617     if (fd_in < 0 || fd_out < 0) {
       
   618 	if (fd_in >= 0)
       
   619 	    close(fd_in);
       
   620 	if (fd_out >= 0)
       
   621 	    close(fd_out);
       
   622         TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
       
   623         if (fd_in < 0)
       
   624             return NULL;
       
   625     }
       
   626     return qemu_chr_open_fd(fd_in, fd_out);
       
   627 }
       
   628 
       
   629 
       
   630 /* for STDIO, we handle the case where several clients use it
       
   631    (nographic mode) */
       
   632 
       
   633 #define TERM_FIFO_MAX_SIZE 1
       
   634 
       
   635 static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
       
   636 static int term_fifo_size;
       
   637 
       
   638 static int stdio_read_poll(void *opaque)
       
   639 {
       
   640     CharDriverState *chr = opaque;
       
   641 
       
   642     /* try to flush the queue if needed */
       
   643     if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
       
   644         qemu_chr_read(chr, term_fifo, 1);
       
   645         term_fifo_size = 0;
       
   646     }
       
   647     /* see if we can absorb more chars */
       
   648     if (term_fifo_size == 0)
       
   649         return 1;
       
   650     else
       
   651         return 0;
       
   652 }
       
   653 
       
   654 static void stdio_read(void *opaque)
       
   655 {
       
   656     int size;
       
   657     uint8_t buf[1];
       
   658     CharDriverState *chr = opaque;
       
   659 
       
   660     size = read(0, buf, 1);
       
   661     if (size == 0) {
       
   662         /* stdin has been closed. Remove it from the active list.  */
       
   663         qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
       
   664         return;
       
   665     }
       
   666     if (size > 0) {
       
   667         if (qemu_chr_can_read(chr) > 0) {
       
   668             qemu_chr_read(chr, buf, 1);
       
   669         } else if (term_fifo_size == 0) {
       
   670             term_fifo[term_fifo_size++] = buf[0];
       
   671         }
       
   672     }
       
   673 }
       
   674 
       
   675 /* init terminal so that we can grab keys */
       
   676 static struct termios oldtty;
       
   677 static int old_fd0_flags;
       
   678 static int term_atexit_done;
       
   679 
       
   680 static void term_exit(void)
       
   681 {
       
   682     tcsetattr (0, TCSANOW, &oldtty);
       
   683     fcntl(0, F_SETFL, old_fd0_flags);
       
   684 }
       
   685 
       
   686 static void term_init(void)
       
   687 {
       
   688     struct termios tty;
       
   689 
       
   690     tcgetattr (0, &tty);
       
   691     oldtty = tty;
       
   692     old_fd0_flags = fcntl(0, F_GETFL);
       
   693 
       
   694     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
       
   695                           |INLCR|IGNCR|ICRNL|IXON);
       
   696     tty.c_oflag |= OPOST;
       
   697     tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
       
   698     /* if graphical mode, we allow Ctrl-C handling */
       
   699     if (nographic)
       
   700         tty.c_lflag &= ~ISIG;
       
   701     tty.c_cflag &= ~(CSIZE|PARENB);
       
   702     tty.c_cflag |= CS8;
       
   703     tty.c_cc[VMIN] = 1;
       
   704     tty.c_cc[VTIME] = 0;
       
   705 
       
   706     tcsetattr (0, TCSANOW, &tty);
       
   707 
       
   708     if (!term_atexit_done++)
       
   709         atexit(term_exit);
       
   710 
       
   711     fcntl(0, F_SETFL, O_NONBLOCK);
       
   712 }
       
   713 
       
   714 static void qemu_chr_close_stdio(struct CharDriverState *chr)
       
   715 {
       
   716     term_exit();
       
   717     stdio_nb_clients--;
       
   718     qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
       
   719     fd_chr_close(chr);
       
   720 }
       
   721 
       
   722 static CharDriverState *qemu_chr_open_stdio(void)
       
   723 {
       
   724     CharDriverState *chr;
       
   725 
       
   726     if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
       
   727         return NULL;
       
   728     chr = qemu_chr_open_fd(0, 1);
       
   729     chr->chr_close = qemu_chr_close_stdio;
       
   730     qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
       
   731     stdio_nb_clients++;
       
   732     term_init();
       
   733 
       
   734     /* stdio is being used to communicate, so ignore ^C (a.k.a. SIGINT).  */
       
   735     signal (SIGINT, SIG_IGN);
       
   736     return chr;
       
   737 }
       
   738 
       
   739 #ifdef __sun__
       
   740 /* Once Solaris has openpty(), this is going to be removed. */
       
   741 int openpty(int *amaster, int *aslave, char *name,
       
   742             struct termios *termp, struct winsize *winp)
       
   743 {
       
   744         const char *slave;
       
   745         int mfd = -1, sfd = -1;
       
   746 
       
   747         *amaster = *aslave = -1;
       
   748 
       
   749         mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
       
   750         if (mfd < 0)
       
   751                 goto err;
       
   752 
       
   753         if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
       
   754                 goto err;
       
   755 
       
   756         if ((slave = ptsname(mfd)) == NULL)
       
   757                 goto err;
       
   758 
       
   759         if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
       
   760                 goto err;
       
   761 
       
   762         if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
       
   763             (termp != NULL && tcgetattr(sfd, termp) < 0))
       
   764                 goto err;
       
   765 
       
   766         if (amaster)
       
   767                 *amaster = mfd;
       
   768         if (aslave)
       
   769                 *aslave = sfd;
       
   770         if (winp)
       
   771                 ioctl(sfd, TIOCSWINSZ, winp);
       
   772 
       
   773         return 0;
       
   774 
       
   775 err:
       
   776         if (sfd != -1)
       
   777                 close(sfd);
       
   778         close(mfd);
       
   779         return -1;
       
   780 }
       
   781 
       
   782 void cfmakeraw (struct termios *termios_p)
       
   783 {
       
   784         termios_p->c_iflag &=
       
   785                 ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
       
   786         termios_p->c_oflag &= ~OPOST;
       
   787         termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
       
   788         termios_p->c_cflag &= ~(CSIZE|PARENB);
       
   789         termios_p->c_cflag |= CS8;
       
   790 
       
   791         termios_p->c_cc[VMIN] = 0;
       
   792         termios_p->c_cc[VTIME] = 0;
       
   793 }
       
   794 #endif
       
   795 
       
   796 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
       
   797     || defined(__NetBSD__) || defined(__OpenBSD__)
       
   798 
       
   799 typedef struct {
       
   800     int fd;
       
   801     int connected;
       
   802     int polling;
       
   803     int read_bytes;
       
   804     QEMUTimer *timer;
       
   805 } PtyCharDriver;
       
   806 
       
   807 static void pty_chr_update_read_handler(CharDriverState *chr);
       
   808 static void pty_chr_state(CharDriverState *chr, int connected);
       
   809 
       
   810 static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
       
   811 {
       
   812     PtyCharDriver *s = chr->opaque;
       
   813 
       
   814     if (!s->connected) {
       
   815         /* guest sends data, check for (re-)connect */
       
   816         pty_chr_update_read_handler(chr);
       
   817         return 0;
       
   818     }
       
   819     return send_all(s->fd, buf, len);
       
   820 }
       
   821 
       
   822 static int pty_chr_read_poll(void *opaque)
       
   823 {
       
   824     CharDriverState *chr = opaque;
       
   825     PtyCharDriver *s = chr->opaque;
       
   826 
       
   827     s->read_bytes = qemu_chr_can_read(chr);
       
   828     return s->read_bytes;
       
   829 }
       
   830 
       
   831 static void pty_chr_read(void *opaque)
       
   832 {
       
   833     CharDriverState *chr = opaque;
       
   834     PtyCharDriver *s = chr->opaque;
       
   835     int size, len;
       
   836     uint8_t buf[1024];
       
   837 
       
   838     len = sizeof(buf);
       
   839     if (len > s->read_bytes)
       
   840         len = s->read_bytes;
       
   841     if (len == 0)
       
   842         return;
       
   843     size = read(s->fd, buf, len);
       
   844     if ((size == -1 && errno == EIO) ||
       
   845         (size == 0)) {
       
   846         pty_chr_state(chr, 0);
       
   847         return;
       
   848     }
       
   849     if (size > 0) {
       
   850         pty_chr_state(chr, 1);
       
   851         qemu_chr_read(chr, buf, size);
       
   852     }
       
   853 }
       
   854 
       
   855 static void pty_chr_update_read_handler(CharDriverState *chr)
       
   856 {
       
   857     PtyCharDriver *s = chr->opaque;
       
   858 
       
   859     qemu_set_fd_handler2(s->fd, pty_chr_read_poll,
       
   860                          pty_chr_read, NULL, chr);
       
   861     s->polling = 1;
       
   862     /*
       
   863      * Short timeout here: just need wait long enougth that qemu makes
       
   864      * it through the poll loop once.  When reconnected we want a
       
   865      * short timeout so we notice it almost instantly.  Otherwise
       
   866      * read() gives us -EIO instantly, making pty_chr_state() reset the
       
   867      * timeout to the normal (much longer) poll interval before the
       
   868      * timer triggers.
       
   869      */
       
   870     qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
       
   871 }
       
   872 
       
   873 static void pty_chr_state(CharDriverState *chr, int connected)
       
   874 {
       
   875     PtyCharDriver *s = chr->opaque;
       
   876 
       
   877     if (!connected) {
       
   878         qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
       
   879         s->connected = 0;
       
   880         s->polling = 0;
       
   881         /* (re-)connect poll interval for idle guests: once per second.
       
   882          * We check more frequently in case the guests sends data to
       
   883          * the virtual device linked to our pty. */
       
   884         qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
       
   885     } else {
       
   886         if (!s->connected)
       
   887             qemu_chr_reset(chr);
       
   888         s->connected = 1;
       
   889     }
       
   890 }
       
   891 
       
   892 static void pty_chr_timer(void *opaque)
       
   893 {
       
   894     struct CharDriverState *chr = opaque;
       
   895     PtyCharDriver *s = chr->opaque;
       
   896 
       
   897     if (s->connected)
       
   898         return;
       
   899     if (s->polling) {
       
   900         /* If we arrive here without polling being cleared due
       
   901          * read returning -EIO, then we are (re-)connected */
       
   902         pty_chr_state(chr, 1);
       
   903         return;
       
   904     }
       
   905 
       
   906     /* Next poll ... */
       
   907     pty_chr_update_read_handler(chr);
       
   908 }
       
   909 
       
   910 static void pty_chr_close(struct CharDriverState *chr)
       
   911 {
       
   912     PtyCharDriver *s = chr->opaque;
       
   913 
       
   914     qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
       
   915     close(s->fd);
       
   916     qemu_free(s);
       
   917 }
       
   918 
       
   919 static CharDriverState *qemu_chr_open_pty(void)
       
   920 {
       
   921     CharDriverState *chr;
       
   922     PtyCharDriver *s;
       
   923     struct termios tty;
       
   924     int slave_fd, len;
       
   925 #if defined(__OpenBSD__)
       
   926     char pty_name[PATH_MAX];
       
   927 #define q_ptsname(x) pty_name
       
   928 #else
       
   929     char *pty_name = NULL;
       
   930 #define q_ptsname(x) ptsname(x)
       
   931 #endif
       
   932 
       
   933     chr = qemu_mallocz(sizeof(CharDriverState));
       
   934     if (!chr)
       
   935         return NULL;
       
   936     s = qemu_mallocz(sizeof(PtyCharDriver));
       
   937     if (!s) {
       
   938         qemu_free(chr);
       
   939         return NULL;
       
   940     }
       
   941 
       
   942     if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) {
       
   943         return NULL;
       
   944     }
       
   945 
       
   946     /* Set raw attributes on the pty. */
       
   947     tcgetattr(slave_fd, &tty);
       
   948     cfmakeraw(&tty);
       
   949     tcsetattr(slave_fd, TCSAFLUSH, &tty);
       
   950     close(slave_fd);
       
   951 
       
   952     len = strlen(q_ptsname(s->fd)) + 5;
       
   953     chr->filename = qemu_malloc(len);
       
   954     snprintf(chr->filename, len, "pty:%s", q_ptsname(s->fd));
       
   955     fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
       
   956 
       
   957     chr->opaque = s;
       
   958     chr->chr_write = pty_chr_write;
       
   959     chr->chr_update_read_handler = pty_chr_update_read_handler;
       
   960     chr->chr_close = pty_chr_close;
       
   961 
       
   962     s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr);
       
   963 
       
   964     return chr;
       
   965 }
       
   966 
       
   967 static void tty_serial_init(int fd, int speed,
       
   968                             int parity, int data_bits, int stop_bits)
       
   969 {
       
   970     struct termios tty;
       
   971     speed_t spd;
       
   972 
       
   973 #if 0
       
   974     printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
       
   975            speed, parity, data_bits, stop_bits);
       
   976 #endif
       
   977     tcgetattr (fd, &tty);
       
   978 
       
   979 #define MARGIN 1.1
       
   980     if (speed <= 50 * MARGIN)
       
   981         spd = B50;
       
   982     else if (speed <= 75 * MARGIN)
       
   983         spd = B75;
       
   984     else if (speed <= 300 * MARGIN)
       
   985         spd = B300;
       
   986     else if (speed <= 600 * MARGIN)
       
   987         spd = B600;
       
   988     else if (speed <= 1200 * MARGIN)
       
   989         spd = B1200;
       
   990     else if (speed <= 2400 * MARGIN)
       
   991         spd = B2400;
       
   992     else if (speed <= 4800 * MARGIN)
       
   993         spd = B4800;
       
   994     else if (speed <= 9600 * MARGIN)
       
   995         spd = B9600;
       
   996     else if (speed <= 19200 * MARGIN)
       
   997         spd = B19200;
       
   998     else if (speed <= 38400 * MARGIN)
       
   999         spd = B38400;
       
  1000     else if (speed <= 57600 * MARGIN)
       
  1001         spd = B57600;
       
  1002     else if (speed <= 115200 * MARGIN)
       
  1003         spd = B115200;
       
  1004     else
       
  1005         spd = B115200;
       
  1006 
       
  1007     cfsetispeed(&tty, spd);
       
  1008     cfsetospeed(&tty, spd);
       
  1009 
       
  1010     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
       
  1011                           |INLCR|IGNCR|ICRNL|IXON);
       
  1012     tty.c_oflag |= OPOST;
       
  1013     tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
       
  1014     tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
       
  1015     switch(data_bits) {
       
  1016     default:
       
  1017     case 8:
       
  1018         tty.c_cflag |= CS8;
       
  1019         break;
       
  1020     case 7:
       
  1021         tty.c_cflag |= CS7;
       
  1022         break;
       
  1023     case 6:
       
  1024         tty.c_cflag |= CS6;
       
  1025         break;
       
  1026     case 5:
       
  1027         tty.c_cflag |= CS5;
       
  1028         break;
       
  1029     }
       
  1030     switch(parity) {
       
  1031     default:
       
  1032     case 'N':
       
  1033         break;
       
  1034     case 'E':
       
  1035         tty.c_cflag |= PARENB;
       
  1036         break;
       
  1037     case 'O':
       
  1038         tty.c_cflag |= PARENB | PARODD;
       
  1039         break;
       
  1040     }
       
  1041     if (stop_bits == 2)
       
  1042         tty.c_cflag |= CSTOPB;
       
  1043 
       
  1044     tcsetattr (fd, TCSANOW, &tty);
       
  1045 }
       
  1046 
       
  1047 static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
       
  1048 {
       
  1049     FDCharDriver *s = chr->opaque;
       
  1050 
       
  1051     switch(cmd) {
       
  1052     case CHR_IOCTL_SERIAL_SET_PARAMS:
       
  1053         {
       
  1054             QEMUSerialSetParams *ssp = arg;
       
  1055             tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
       
  1056                             ssp->data_bits, ssp->stop_bits);
       
  1057         }
       
  1058         break;
       
  1059     case CHR_IOCTL_SERIAL_SET_BREAK:
       
  1060         {
       
  1061             int enable = *(int *)arg;
       
  1062             if (enable)
       
  1063                 tcsendbreak(s->fd_in, 1);
       
  1064         }
       
  1065         break;
       
  1066     case CHR_IOCTL_SERIAL_GET_TIOCM:
       
  1067         {
       
  1068             int sarg = 0;
       
  1069             int *targ = (int *)arg;
       
  1070             ioctl(s->fd_in, TIOCMGET, &sarg);
       
  1071             *targ = 0;
       
  1072             if (sarg | TIOCM_CTS)
       
  1073                 *targ |= CHR_TIOCM_CTS;
       
  1074             if (sarg | TIOCM_CAR)
       
  1075                 *targ |= CHR_TIOCM_CAR;
       
  1076             if (sarg | TIOCM_DSR)
       
  1077                 *targ |= CHR_TIOCM_DSR;
       
  1078             if (sarg | TIOCM_RI)
       
  1079                 *targ |= CHR_TIOCM_RI;
       
  1080             if (sarg | TIOCM_DTR)
       
  1081                 *targ |= CHR_TIOCM_DTR;
       
  1082             if (sarg | TIOCM_RTS)
       
  1083                 *targ |= CHR_TIOCM_RTS;
       
  1084         }
       
  1085         break;
       
  1086     case CHR_IOCTL_SERIAL_SET_TIOCM:
       
  1087         {
       
  1088             int sarg = *(int *)arg;
       
  1089             int targ = 0;
       
  1090             if (sarg | CHR_TIOCM_DTR)
       
  1091                 targ |= TIOCM_DTR;
       
  1092             if (sarg | CHR_TIOCM_RTS)
       
  1093                 targ |= TIOCM_RTS;
       
  1094             ioctl(s->fd_in, TIOCMSET, &targ);
       
  1095         }
       
  1096         break;
       
  1097     default:
       
  1098         return -ENOTSUP;
       
  1099     }
       
  1100     return 0;
       
  1101 }
       
  1102 
       
  1103 static CharDriverState *qemu_chr_open_tty(const char *filename)
       
  1104 {
       
  1105     CharDriverState *chr;
       
  1106     int fd;
       
  1107 
       
  1108     TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
       
  1109     tty_serial_init(fd, 115200, 'N', 8, 1);
       
  1110     chr = qemu_chr_open_fd(fd, fd);
       
  1111     if (!chr) {
       
  1112         close(fd);
       
  1113         return NULL;
       
  1114     }
       
  1115     chr->chr_ioctl = tty_serial_ioctl;
       
  1116     qemu_chr_reset(chr);
       
  1117     return chr;
       
  1118 }
       
  1119 #else  /* ! __linux__ && ! __sun__ */
       
  1120 static CharDriverState *qemu_chr_open_pty(void)
       
  1121 {
       
  1122     return NULL;
       
  1123 }
       
  1124 #endif /* __linux__ || __sun__ */
       
  1125 
       
  1126 #if defined(__linux__)
       
  1127 typedef struct {
       
  1128     int fd;
       
  1129     int mode;
       
  1130 } ParallelCharDriver;
       
  1131 
       
  1132 static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
       
  1133 {
       
  1134     if (s->mode != mode) {
       
  1135 	int m = mode;
       
  1136         if (ioctl(s->fd, PPSETMODE, &m) < 0)
       
  1137             return 0;
       
  1138 	s->mode = mode;
       
  1139     }
       
  1140     return 1;
       
  1141 }
       
  1142 
       
  1143 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
       
  1144 {
       
  1145     ParallelCharDriver *drv = chr->opaque;
       
  1146     int fd = drv->fd;
       
  1147     uint8_t b;
       
  1148 
       
  1149     switch(cmd) {
       
  1150     case CHR_IOCTL_PP_READ_DATA:
       
  1151         if (ioctl(fd, PPRDATA, &b) < 0)
       
  1152             return -ENOTSUP;
       
  1153         *(uint8_t *)arg = b;
       
  1154         break;
       
  1155     case CHR_IOCTL_PP_WRITE_DATA:
       
  1156         b = *(uint8_t *)arg;
       
  1157         if (ioctl(fd, PPWDATA, &b) < 0)
       
  1158             return -ENOTSUP;
       
  1159         break;
       
  1160     case CHR_IOCTL_PP_READ_CONTROL:
       
  1161         if (ioctl(fd, PPRCONTROL, &b) < 0)
       
  1162             return -ENOTSUP;
       
  1163 	/* Linux gives only the lowest bits, and no way to know data
       
  1164 	   direction! For better compatibility set the fixed upper
       
  1165 	   bits. */
       
  1166         *(uint8_t *)arg = b | 0xc0;
       
  1167         break;
       
  1168     case CHR_IOCTL_PP_WRITE_CONTROL:
       
  1169         b = *(uint8_t *)arg;
       
  1170         if (ioctl(fd, PPWCONTROL, &b) < 0)
       
  1171             return -ENOTSUP;
       
  1172         break;
       
  1173     case CHR_IOCTL_PP_READ_STATUS:
       
  1174         if (ioctl(fd, PPRSTATUS, &b) < 0)
       
  1175             return -ENOTSUP;
       
  1176         *(uint8_t *)arg = b;
       
  1177         break;
       
  1178     case CHR_IOCTL_PP_DATA_DIR:
       
  1179         if (ioctl(fd, PPDATADIR, (int *)arg) < 0)
       
  1180             return -ENOTSUP;
       
  1181         break;
       
  1182     case CHR_IOCTL_PP_EPP_READ_ADDR:
       
  1183 	if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
       
  1184 	    struct ParallelIOArg *parg = arg;
       
  1185 	    int n = read(fd, parg->buffer, parg->count);
       
  1186 	    if (n != parg->count) {
       
  1187 		return -EIO;
       
  1188 	    }
       
  1189 	}
       
  1190         break;
       
  1191     case CHR_IOCTL_PP_EPP_READ:
       
  1192 	if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
       
  1193 	    struct ParallelIOArg *parg = arg;
       
  1194 	    int n = read(fd, parg->buffer, parg->count);
       
  1195 	    if (n != parg->count) {
       
  1196 		return -EIO;
       
  1197 	    }
       
  1198 	}
       
  1199         break;
       
  1200     case CHR_IOCTL_PP_EPP_WRITE_ADDR:
       
  1201 	if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
       
  1202 	    struct ParallelIOArg *parg = arg;
       
  1203 	    int n = write(fd, parg->buffer, parg->count);
       
  1204 	    if (n != parg->count) {
       
  1205 		return -EIO;
       
  1206 	    }
       
  1207 	}
       
  1208         break;
       
  1209     case CHR_IOCTL_PP_EPP_WRITE:
       
  1210 	if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
       
  1211 	    struct ParallelIOArg *parg = arg;
       
  1212 	    int n = write(fd, parg->buffer, parg->count);
       
  1213 	    if (n != parg->count) {
       
  1214 		return -EIO;
       
  1215 	    }
       
  1216 	}
       
  1217         break;
       
  1218     default:
       
  1219         return -ENOTSUP;
       
  1220     }
       
  1221     return 0;
       
  1222 }
       
  1223 
       
  1224 static void pp_close(CharDriverState *chr)
       
  1225 {
       
  1226     ParallelCharDriver *drv = chr->opaque;
       
  1227     int fd = drv->fd;
       
  1228 
       
  1229     pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
       
  1230     ioctl(fd, PPRELEASE);
       
  1231     close(fd);
       
  1232     qemu_free(drv);
       
  1233 }
       
  1234 
       
  1235 static CharDriverState *qemu_chr_open_pp(const char *filename)
       
  1236 {
       
  1237     CharDriverState *chr;
       
  1238     ParallelCharDriver *drv;
       
  1239     int fd;
       
  1240 
       
  1241     TFR(fd = open(filename, O_RDWR));
       
  1242     if (fd < 0)
       
  1243         return NULL;
       
  1244 
       
  1245     if (ioctl(fd, PPCLAIM) < 0) {
       
  1246         close(fd);
       
  1247         return NULL;
       
  1248     }
       
  1249 
       
  1250     drv = qemu_mallocz(sizeof(ParallelCharDriver));
       
  1251     if (!drv) {
       
  1252         close(fd);
       
  1253         return NULL;
       
  1254     }
       
  1255     drv->fd = fd;
       
  1256     drv->mode = IEEE1284_MODE_COMPAT;
       
  1257 
       
  1258     chr = qemu_mallocz(sizeof(CharDriverState));
       
  1259     if (!chr) {
       
  1260 	qemu_free(drv);
       
  1261         close(fd);
       
  1262         return NULL;
       
  1263     }
       
  1264     chr->chr_write = null_chr_write;
       
  1265     chr->chr_ioctl = pp_ioctl;
       
  1266     chr->chr_close = pp_close;
       
  1267     chr->opaque = drv;
       
  1268 
       
  1269     qemu_chr_reset(chr);
       
  1270 
       
  1271     return chr;
       
  1272 }
       
  1273 #endif /* __linux__ */
       
  1274 
       
  1275 #if defined(__FreeBSD__)
       
  1276 static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
       
  1277 {
       
  1278     int fd = (int)chr->opaque;
       
  1279     uint8_t b;
       
  1280 
       
  1281     switch(cmd) {
       
  1282     case CHR_IOCTL_PP_READ_DATA:
       
  1283         if (ioctl(fd, PPIGDATA, &b) < 0)
       
  1284             return -ENOTSUP;
       
  1285         *(uint8_t *)arg = b;
       
  1286         break;
       
  1287     case CHR_IOCTL_PP_WRITE_DATA:
       
  1288         b = *(uint8_t *)arg;
       
  1289         if (ioctl(fd, PPISDATA, &b) < 0)
       
  1290             return -ENOTSUP;
       
  1291         break;
       
  1292     case CHR_IOCTL_PP_READ_CONTROL:
       
  1293         if (ioctl(fd, PPIGCTRL, &b) < 0)
       
  1294             return -ENOTSUP;
       
  1295         *(uint8_t *)arg = b;
       
  1296         break;
       
  1297     case CHR_IOCTL_PP_WRITE_CONTROL:
       
  1298         b = *(uint8_t *)arg;
       
  1299         if (ioctl(fd, PPISCTRL, &b) < 0)
       
  1300             return -ENOTSUP;
       
  1301         break;
       
  1302     case CHR_IOCTL_PP_READ_STATUS:
       
  1303         if (ioctl(fd, PPIGSTATUS, &b) < 0)
       
  1304             return -ENOTSUP;
       
  1305         *(uint8_t *)arg = b;
       
  1306         break;
       
  1307     default:
       
  1308         return -ENOTSUP;
       
  1309     }
       
  1310     return 0;
       
  1311 }
       
  1312 
       
  1313 static CharDriverState *qemu_chr_open_pp(const char *filename)
       
  1314 {
       
  1315     CharDriverState *chr;
       
  1316     int fd;
       
  1317 
       
  1318     fd = open(filename, O_RDWR);
       
  1319     if (fd < 0)
       
  1320         return NULL;
       
  1321 
       
  1322     chr = qemu_mallocz(sizeof(CharDriverState));
       
  1323     if (!chr) {
       
  1324         close(fd);
       
  1325         return NULL;
       
  1326     }
       
  1327     chr->opaque = (void *)fd;
       
  1328     chr->chr_write = null_chr_write;
       
  1329     chr->chr_ioctl = pp_ioctl;
       
  1330     return chr;
       
  1331 }
       
  1332 #endif
       
  1333 
       
  1334 #else /* _WIN32 */
       
  1335 
       
  1336 typedef struct {
       
  1337     int max_size;
       
  1338     HANDLE hcom, hrecv, hsend;
       
  1339     OVERLAPPED orecv, osend;
       
  1340     BOOL fpipe;
       
  1341     DWORD len;
       
  1342     BOOL quit_on_eof;
       
  1343 } WinCharState;
       
  1344 
       
  1345 #define NSENDBUF 2048
       
  1346 #define NRECVBUF 2048
       
  1347 #define MAXCONNECT 1
       
  1348 #define NTIMEOUT 5000
       
  1349 
       
  1350 static int win_chr_poll(void *opaque);
       
  1351 static int win_chr_pipe_poll(void *opaque);
       
  1352 
       
  1353 static void win_chr_close(CharDriverState *chr)
       
  1354 {
       
  1355     WinCharState *s = chr->opaque;
       
  1356 
       
  1357     if (s->hsend) {
       
  1358         CloseHandle(s->hsend);
       
  1359         s->hsend = NULL;
       
  1360     }
       
  1361     if (s->hrecv) {
       
  1362         CloseHandle(s->hrecv);
       
  1363         s->hrecv = NULL;
       
  1364     }
       
  1365     if (s->hcom) {
       
  1366         CloseHandle(s->hcom);
       
  1367         s->hcom = NULL;
       
  1368     }
       
  1369     if (s->fpipe)
       
  1370         qemu_del_polling_cb(win_chr_pipe_poll, chr);
       
  1371     else
       
  1372         qemu_del_polling_cb(win_chr_poll, chr);
       
  1373 }
       
  1374 
       
  1375 static int win_chr_init(CharDriverState *chr, const char *filename)
       
  1376 {
       
  1377     WinCharState *s = chr->opaque;
       
  1378     COMMCONFIG comcfg;
       
  1379     COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
       
  1380     COMSTAT comstat;
       
  1381     DWORD size;
       
  1382     DWORD err;
       
  1383 
       
  1384     s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
       
  1385     if (!s->hsend) {
       
  1386         fprintf(stderr, "Failed CreateEvent\n");
       
  1387         goto fail;
       
  1388     }
       
  1389     s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
       
  1390     if (!s->hrecv) {
       
  1391         fprintf(stderr, "Failed CreateEvent\n");
       
  1392         goto fail;
       
  1393     }
       
  1394 
       
  1395     s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
       
  1396                       OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
       
  1397     if (s->hcom == INVALID_HANDLE_VALUE) {
       
  1398         fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
       
  1399         s->hcom = NULL;
       
  1400         goto fail;
       
  1401     }
       
  1402 
       
  1403     if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
       
  1404         fprintf(stderr, "Failed SetupComm\n");
       
  1405         goto fail;
       
  1406     }
       
  1407 
       
  1408     ZeroMemory(&comcfg, sizeof(COMMCONFIG));
       
  1409     size = sizeof(COMMCONFIG);
       
  1410     GetDefaultCommConfig(filename, &comcfg, &size);
       
  1411     comcfg.dcb.DCBlength = sizeof(DCB);
       
  1412     CommConfigDialog(filename, NULL, &comcfg);
       
  1413 
       
  1414     if (!SetCommState(s->hcom, &comcfg.dcb)) {
       
  1415         fprintf(stderr, "Failed SetCommState\n");
       
  1416         goto fail;
       
  1417     }
       
  1418 
       
  1419     if (!SetCommMask(s->hcom, EV_ERR)) {
       
  1420         fprintf(stderr, "Failed SetCommMask\n");
       
  1421         goto fail;
       
  1422     }
       
  1423 
       
  1424     cto.ReadIntervalTimeout = MAXDWORD;
       
  1425     if (!SetCommTimeouts(s->hcom, &cto)) {
       
  1426         fprintf(stderr, "Failed SetCommTimeouts\n");
       
  1427         goto fail;
       
  1428     }
       
  1429 
       
  1430     if (!ClearCommError(s->hcom, &err, &comstat)) {
       
  1431         fprintf(stderr, "Failed ClearCommError\n");
       
  1432         goto fail;
       
  1433     }
       
  1434     qemu_add_polling_cb(win_chr_poll, chr);
       
  1435     return 0;
       
  1436 
       
  1437  fail:
       
  1438     win_chr_close(chr);
       
  1439     return -1;
       
  1440 }
       
  1441 
       
  1442 static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
       
  1443 {
       
  1444     WinCharState *s = chr->opaque;
       
  1445     DWORD len, ret, size, err;
       
  1446 
       
  1447     len = len1;
       
  1448     ZeroMemory(&s->osend, sizeof(s->osend));
       
  1449     s->osend.hEvent = s->hsend;
       
  1450     while (len > 0) {
       
  1451         if (s->hsend)
       
  1452             ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
       
  1453         else
       
  1454             ret = WriteFile(s->hcom, buf, len, &size, NULL);
       
  1455         if (!ret) {
       
  1456             err = GetLastError();
       
  1457             if (err == ERROR_IO_PENDING) {
       
  1458                 ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
       
  1459                 if (ret) {
       
  1460                     buf += size;
       
  1461                     len -= size;
       
  1462                 } else {
       
  1463                     break;
       
  1464                 }
       
  1465             } else {
       
  1466                 break;
       
  1467             }
       
  1468         } else {
       
  1469             buf += size;
       
  1470             len -= size;
       
  1471         }
       
  1472     }
       
  1473     return len1 - len;
       
  1474 }
       
  1475 
       
  1476 static int win_chr_read_poll(CharDriverState *chr)
       
  1477 {
       
  1478     WinCharState *s = chr->opaque;
       
  1479 
       
  1480     s->max_size = qemu_chr_can_read(chr);
       
  1481     return s->max_size;
       
  1482 }
       
  1483 
       
  1484 static void win_chr_readfile(CharDriverState *chr)
       
  1485 {
       
  1486     WinCharState *s = chr->opaque;
       
  1487     int ret, err;
       
  1488     uint8_t buf[1024];
       
  1489     DWORD size;
       
  1490 
       
  1491     ZeroMemory(&s->orecv, sizeof(s->orecv));
       
  1492     s->orecv.hEvent = s->hrecv;
       
  1493     ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
       
  1494     if (!ret) {
       
  1495         err = GetLastError();
       
  1496         if (err == ERROR_IO_PENDING) {
       
  1497             ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
       
  1498         }
       
  1499     }
       
  1500 
       
  1501     if (size > 0) {
       
  1502         qemu_chr_read(chr, buf, size);
       
  1503     }
       
  1504 }
       
  1505 
       
  1506 static void win_chr_read(CharDriverState *chr)
       
  1507 {
       
  1508     WinCharState *s = chr->opaque;
       
  1509 
       
  1510     if (s->len > s->max_size)
       
  1511         s->len = s->max_size;
       
  1512     if (s->len == 0)
       
  1513         return;
       
  1514 
       
  1515     win_chr_readfile(chr);
       
  1516 }
       
  1517 
       
  1518 static int win_chr_poll(void *opaque)
       
  1519 {
       
  1520     CharDriverState *chr = opaque;
       
  1521     WinCharState *s = chr->opaque;
       
  1522     COMSTAT status;
       
  1523     DWORD comerr;
       
  1524 
       
  1525     ClearCommError(s->hcom, &comerr, &status);
       
  1526     if (status.cbInQue > 0) {
       
  1527         s->len = status.cbInQue;
       
  1528         win_chr_read_poll(chr);
       
  1529         win_chr_read(chr);
       
  1530         return 1;
       
  1531     }
       
  1532     return 0;
       
  1533 }
       
  1534 
       
  1535 static CharDriverState *qemu_chr_open_win(const char *filename)
       
  1536 {
       
  1537     CharDriverState *chr;
       
  1538     WinCharState *s;
       
  1539 
       
  1540     chr = qemu_mallocz(sizeof(CharDriverState));
       
  1541     if (!chr)
       
  1542         return NULL;
       
  1543     s = qemu_mallocz(sizeof(WinCharState));
       
  1544     if (!s) {
       
  1545         free(chr);
       
  1546         return NULL;
       
  1547     }
       
  1548     chr->opaque = s;
       
  1549     chr->chr_write = win_chr_write;
       
  1550     chr->chr_close = win_chr_close;
       
  1551 
       
  1552     if (win_chr_init(chr, filename) < 0) {
       
  1553         free(s);
       
  1554         free(chr);
       
  1555         return NULL;
       
  1556     }
       
  1557     qemu_chr_reset(chr);
       
  1558     return chr;
       
  1559 }
       
  1560 
       
  1561 static int win_chr_pipe_poll(void *opaque)
       
  1562 {
       
  1563     CharDriverState *chr = opaque;
       
  1564     WinCharState *s = chr->opaque;
       
  1565     DWORD size;
       
  1566 
       
  1567     PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
       
  1568     if (size > 0) {
       
  1569         s->len = size;
       
  1570         win_chr_read_poll(chr);
       
  1571         win_chr_read(chr);
       
  1572         return 1;
       
  1573     }
       
  1574     return 0;
       
  1575 }
       
  1576 
       
  1577 static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
       
  1578 {
       
  1579     WinCharState *s = chr->opaque;
       
  1580     OVERLAPPED ov;
       
  1581     int ret;
       
  1582     DWORD size;
       
  1583     char openname[256];
       
  1584 
       
  1585     s->fpipe = TRUE;
       
  1586 
       
  1587     s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
       
  1588     if (!s->hsend) {
       
  1589         fprintf(stderr, "Failed CreateEvent\n");
       
  1590         goto fail;
       
  1591     }
       
  1592     s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
       
  1593     if (!s->hrecv) {
       
  1594         fprintf(stderr, "Failed CreateEvent\n");
       
  1595         goto fail;
       
  1596     }
       
  1597 
       
  1598     snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
       
  1599     s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
       
  1600                               PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
       
  1601                               PIPE_WAIT,
       
  1602                               MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
       
  1603     if (s->hcom == INVALID_HANDLE_VALUE) {
       
  1604         fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
       
  1605         s->hcom = NULL;
       
  1606         goto fail;
       
  1607     }
       
  1608 
       
  1609     ZeroMemory(&ov, sizeof(ov));
       
  1610     ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
       
  1611     ret = ConnectNamedPipe(s->hcom, &ov);
       
  1612     if (ret) {
       
  1613         fprintf(stderr, "Failed ConnectNamedPipe\n");
       
  1614         goto fail;
       
  1615     }
       
  1616 
       
  1617     ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
       
  1618     if (!ret) {
       
  1619         fprintf(stderr, "Failed GetOverlappedResult\n");
       
  1620         if (ov.hEvent) {
       
  1621             CloseHandle(ov.hEvent);
       
  1622             ov.hEvent = NULL;
       
  1623         }
       
  1624         goto fail;
       
  1625     }
       
  1626 
       
  1627     if (ov.hEvent) {
       
  1628         CloseHandle(ov.hEvent);
       
  1629         ov.hEvent = NULL;
       
  1630     }
       
  1631     qemu_add_polling_cb(win_chr_pipe_poll, chr);
       
  1632     return 0;
       
  1633 
       
  1634  fail:
       
  1635     win_chr_close(chr);
       
  1636     return -1;
       
  1637 }
       
  1638 
       
  1639 
       
  1640 static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
       
  1641 {
       
  1642     CharDriverState *chr;
       
  1643     WinCharState *s;
       
  1644 
       
  1645     chr = qemu_mallocz(sizeof(CharDriverState));
       
  1646     if (!chr)
       
  1647         return NULL;
       
  1648     s = qemu_mallocz(sizeof(WinCharState));
       
  1649     if (!s) {
       
  1650         free(chr);
       
  1651         return NULL;
       
  1652     }
       
  1653     chr->opaque = s;
       
  1654     chr->chr_write = win_chr_write;
       
  1655     chr->chr_close = win_chr_close;
       
  1656 
       
  1657     if (win_chr_pipe_init(chr, filename) < 0) {
       
  1658         free(s);
       
  1659         free(chr);
       
  1660         return NULL;
       
  1661     }
       
  1662     qemu_chr_reset(chr);
       
  1663     return chr;
       
  1664 }
       
  1665 
       
  1666 static int win_chr_stdio_poll(void *opaque)
       
  1667 {
       
  1668     CharDriverState *chr = opaque;
       
  1669     WinCharState *s = chr->opaque;
       
  1670     HANDLE input = GetStdHandle (STD_INPUT_HANDLE);
       
  1671     DWORD numBytes;
       
  1672 
       
  1673     BOOL r = PeekNamedPipe (input, NULL, 0, NULL, &numBytes, NULL);
       
  1674     if (r == 0)
       
  1675 	{
       
  1676 	    qemu_del_polling_cb(win_chr_poll, chr);
       
  1677 	    if (s->quit_on_eof)
       
  1678 		exit (0);
       
  1679 	}
       
  1680 
       
  1681     if (numBytes) {
       
  1682 	char buf[1024];
       
  1683 	DWORD size_read;
       
  1684 
       
  1685 	int size = numBytes;
       
  1686 	int max_size = qemu_chr_can_read(chr);
       
  1687 	if (size > max_size)
       
  1688 	    size = max_size;
       
  1689 	if (size > 1024)
       
  1690 	    size = 1024;
       
  1691 	ReadFile (input, buf, size, &size_read, NULL);
       
  1692 	if (size_read > 0)
       
  1693 	    qemu_chr_read(chr, buf, size_read);
       
  1694 	return size_read;
       
  1695     }
       
  1696     return 0;
       
  1697 }
       
  1698 
       
  1699 static void win_stdio_chr_close(CharDriverState *chr)
       
  1700 {
       
  1701     /* Unlike win_chr_close, we don't want to close
       
  1702        any handles, as we did not create them.
       
  1703        Only unregister polling callback.  */
       
  1704     qemu_del_polling_cb(win_chr_poll, chr);
       
  1705 }
       
  1706 
       
  1707 static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
       
  1708 {
       
  1709     CharDriverState *chr;
       
  1710     WinCharState *s;
       
  1711 
       
  1712     chr = qemu_mallocz(sizeof(CharDriverState));
       
  1713     if (!chr)
       
  1714         return NULL;
       
  1715     s = qemu_mallocz(sizeof(WinCharState));
       
  1716     if (!s) {
       
  1717         free(chr);
       
  1718         return NULL;
       
  1719     }
       
  1720     s->hcom = fd_out;
       
  1721     chr->opaque = s;
       
  1722     chr->chr_write = win_chr_write;
       
  1723     qemu_chr_reset(chr);
       
  1724     return chr;
       
  1725 }
       
  1726 
       
  1727 static CharDriverState *qemu_chr_open_win_stdio(const char *filename)
       
  1728 {
       
  1729     char *ptr = filename;
       
  1730     /* Set up write support.  */
       
  1731     CharDriverState *chr =
       
  1732 	qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
       
  1733     WinCharState *s = chr->opaque;
       
  1734     chr->chr_close = win_stdio_chr_close;
       
  1735     qemu_add_polling_cb(win_chr_stdio_poll, chr);
       
  1736 
       
  1737     while((ptr = strchr(ptr,','))) {
       
  1738         ptr++;
       
  1739         if (!strncmp(ptr,"quit-on-eof",11)) {
       
  1740 	    s->quit_on_eof = 1;
       
  1741 	}
       
  1742     }
       
  1743     /* stdio is being used to communicate, so ignore ^C */
       
  1744     SetConsoleCtrlHandler (NULL, TRUE);
       
  1745 
       
  1746     return chr;
       
  1747 }
       
  1748 
       
  1749 static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
       
  1750 {
       
  1751     HANDLE fd_out;
       
  1752 
       
  1753     fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
       
  1754                         OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
       
  1755     if (fd_out == INVALID_HANDLE_VALUE)
       
  1756         return NULL;
       
  1757 
       
  1758     return qemu_chr_open_win_file(fd_out);
       
  1759 }
       
  1760 #endif /* !_WIN32 */
       
  1761 
       
  1762 /***********************************************************/
       
  1763 /* UDP Net console */
       
  1764 
       
  1765 typedef struct {
       
  1766     int fd;
       
  1767     struct sockaddr_in daddr;
       
  1768     uint8_t buf[1024];
       
  1769     int bufcnt;
       
  1770     int bufptr;
       
  1771     int max_size;
       
  1772 } NetCharDriver;
       
  1773 
       
  1774 static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
       
  1775 {
       
  1776     NetCharDriver *s = chr->opaque;
       
  1777 
       
  1778     return sendto(s->fd, buf, len, 0,
       
  1779                   (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
       
  1780 }
       
  1781 
       
  1782 static int udp_chr_read_poll(void *opaque)
       
  1783 {
       
  1784     CharDriverState *chr = opaque;
       
  1785     NetCharDriver *s = chr->opaque;
       
  1786 
       
  1787     s->max_size = qemu_chr_can_read(chr);
       
  1788 
       
  1789     /* If there were any stray characters in the queue process them
       
  1790      * first
       
  1791      */
       
  1792     while (s->max_size > 0 && s->bufptr < s->bufcnt) {
       
  1793         qemu_chr_read(chr, &s->buf[s->bufptr], 1);
       
  1794         s->bufptr++;
       
  1795         s->max_size = qemu_chr_can_read(chr);
       
  1796     }
       
  1797     return s->max_size;
       
  1798 }
       
  1799 
       
  1800 static void udp_chr_read(void *opaque)
       
  1801 {
       
  1802     CharDriverState *chr = opaque;
       
  1803     NetCharDriver *s = chr->opaque;
       
  1804 
       
  1805     if (s->max_size == 0)
       
  1806         return;
       
  1807     s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
       
  1808     s->bufptr = s->bufcnt;
       
  1809     if (s->bufcnt <= 0)
       
  1810         return;
       
  1811 
       
  1812     s->bufptr = 0;
       
  1813     while (s->max_size > 0 && s->bufptr < s->bufcnt) {
       
  1814         qemu_chr_read(chr, &s->buf[s->bufptr], 1);
       
  1815         s->bufptr++;
       
  1816         s->max_size = qemu_chr_can_read(chr);
       
  1817     }
       
  1818 }
       
  1819 
       
  1820 static void udp_chr_update_read_handler(CharDriverState *chr)
       
  1821 {
       
  1822     NetCharDriver *s = chr->opaque;
       
  1823 
       
  1824     if (s->fd >= 0) {
       
  1825         qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
       
  1826                              udp_chr_read, NULL, chr);
       
  1827     }
       
  1828 }
       
  1829 
       
  1830 static CharDriverState *qemu_chr_open_udp(const char *def)
       
  1831 {
       
  1832     CharDriverState *chr = NULL;
       
  1833     NetCharDriver *s = NULL;
       
  1834     int fd = -1;
       
  1835     struct sockaddr_in saddr;
       
  1836 
       
  1837     chr = qemu_mallocz(sizeof(CharDriverState));
       
  1838     if (!chr)
       
  1839         goto return_err;
       
  1840     s = qemu_mallocz(sizeof(NetCharDriver));
       
  1841     if (!s)
       
  1842         goto return_err;
       
  1843 
       
  1844     fd = socket(PF_INET, SOCK_DGRAM, 0);
       
  1845     if (fd < 0) {
       
  1846         perror("socket(PF_INET, SOCK_DGRAM)");
       
  1847         goto return_err;
       
  1848     }
       
  1849 
       
  1850     if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
       
  1851         printf("Could not parse: %s\n", def);
       
  1852         goto return_err;
       
  1853     }
       
  1854 
       
  1855     if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
       
  1856     {
       
  1857         perror("bind");
       
  1858         goto return_err;
       
  1859     }
       
  1860 
       
  1861     s->fd = fd;
       
  1862     s->bufcnt = 0;
       
  1863     s->bufptr = 0;
       
  1864     chr->opaque = s;
       
  1865     chr->chr_write = udp_chr_write;
       
  1866     chr->chr_update_read_handler = udp_chr_update_read_handler;
       
  1867     return chr;
       
  1868 
       
  1869 return_err:
       
  1870     if (chr)
       
  1871         free(chr);
       
  1872     if (s)
       
  1873         free(s);
       
  1874     if (fd >= 0)
       
  1875         closesocket(fd);
       
  1876     return NULL;
       
  1877 }
       
  1878 
       
  1879 /***********************************************************/
       
  1880 /* TCP Net console */
       
  1881 
       
  1882 typedef struct {
       
  1883     int fd, listen_fd;
       
  1884     int connected;
       
  1885     int max_size;
       
  1886     int do_telnetopt;
       
  1887     int do_nodelay;
       
  1888     int is_unix;
       
  1889 } TCPCharDriver;
       
  1890 
       
  1891 static void tcp_chr_accept(void *opaque);
       
  1892 
       
  1893 static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
       
  1894 {
       
  1895     TCPCharDriver *s = chr->opaque;
       
  1896     if (s->connected) {
       
  1897         return send_all(s->fd, buf, len);
       
  1898     } else {
       
  1899         /* XXX: indicate an error ? */
       
  1900         return len;
       
  1901     }
       
  1902 }
       
  1903 
       
  1904 static int tcp_chr_read_poll(void *opaque)
       
  1905 {
       
  1906     CharDriverState *chr = opaque;
       
  1907     TCPCharDriver *s = chr->opaque;
       
  1908     if (!s->connected)
       
  1909         return 0;
       
  1910     s->max_size = qemu_chr_can_read(chr);
       
  1911     return s->max_size;
       
  1912 }
       
  1913 
       
  1914 #define IAC 255
       
  1915 #define IAC_BREAK 243
       
  1916 static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
       
  1917                                       TCPCharDriver *s,
       
  1918                                       uint8_t *buf, int *size)
       
  1919 {
       
  1920     /* Handle any telnet client's basic IAC options to satisfy char by
       
  1921      * char mode with no echo.  All IAC options will be removed from
       
  1922      * the buf and the do_telnetopt variable will be used to track the
       
  1923      * state of the width of the IAC information.
       
  1924      *
       
  1925      * IAC commands come in sets of 3 bytes with the exception of the
       
  1926      * "IAC BREAK" command and the double IAC.
       
  1927      */
       
  1928 
       
  1929     int i;
       
  1930     int j = 0;
       
  1931 
       
  1932     for (i = 0; i < *size; i++) {
       
  1933         if (s->do_telnetopt > 1) {
       
  1934             if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
       
  1935                 /* Double IAC means send an IAC */
       
  1936                 if (j != i)
       
  1937                     buf[j] = buf[i];
       
  1938                 j++;
       
  1939                 s->do_telnetopt = 1;
       
  1940             } else {
       
  1941                 if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
       
  1942                     /* Handle IAC break commands by sending a serial break */
       
  1943                     qemu_chr_event(chr, CHR_EVENT_BREAK);
       
  1944                     s->do_telnetopt++;
       
  1945                 }
       
  1946                 s->do_telnetopt++;
       
  1947             }
       
  1948             if (s->do_telnetopt >= 4) {
       
  1949                 s->do_telnetopt = 1;
       
  1950             }
       
  1951         } else {
       
  1952             if ((unsigned char)buf[i] == IAC) {
       
  1953                 s->do_telnetopt = 2;
       
  1954             } else {
       
  1955                 if (j != i)
       
  1956                     buf[j] = buf[i];
       
  1957                 j++;
       
  1958             }
       
  1959         }
       
  1960     }
       
  1961     *size = j;
       
  1962 }
       
  1963 
       
  1964 static void tcp_chr_read(void *opaque)
       
  1965 {
       
  1966     CharDriverState *chr = opaque;
       
  1967     TCPCharDriver *s = chr->opaque;
       
  1968     uint8_t buf[1024];
       
  1969     int len, size;
       
  1970 
       
  1971     if (!s->connected || s->max_size <= 0)
       
  1972         return;
       
  1973     len = sizeof(buf);
       
  1974     if (len > s->max_size)
       
  1975         len = s->max_size;
       
  1976     size = recv(s->fd, buf, len, 0);
       
  1977     if (size == 0) {
       
  1978         /* connection closed */
       
  1979         s->connected = 0;
       
  1980         if (s->listen_fd >= 0) {
       
  1981             qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
       
  1982         }
       
  1983         qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
       
  1984         closesocket(s->fd);
       
  1985         s->fd = -1;
       
  1986     } else if (size > 0) {
       
  1987         if (s->do_telnetopt)
       
  1988             tcp_chr_process_IAC_bytes(chr, s, buf, &size);
       
  1989         if (size > 0)
       
  1990             qemu_chr_read(chr, buf, size);
       
  1991     }
       
  1992 }
       
  1993 
       
  1994 static void tcp_chr_connect(void *opaque)
       
  1995 {
       
  1996     CharDriverState *chr = opaque;
       
  1997     TCPCharDriver *s = chr->opaque;
       
  1998 
       
  1999     s->connected = 1;
       
  2000     qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
       
  2001                          tcp_chr_read, NULL, chr);
       
  2002     qemu_chr_reset(chr);
       
  2003 }
       
  2004 
       
  2005 #define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
       
  2006 static void tcp_chr_telnet_init(int fd)
       
  2007 {
       
  2008     char buf[3];
       
  2009     /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
       
  2010     IACSET(buf, 0xff, 0xfb, 0x01);  /* IAC WILL ECHO */
       
  2011     send(fd, (char *)buf, 3, 0);
       
  2012     IACSET(buf, 0xff, 0xfb, 0x03);  /* IAC WILL Suppress go ahead */
       
  2013     send(fd, (char *)buf, 3, 0);
       
  2014     IACSET(buf, 0xff, 0xfb, 0x00);  /* IAC WILL Binary */
       
  2015     send(fd, (char *)buf, 3, 0);
       
  2016     IACSET(buf, 0xff, 0xfd, 0x00);  /* IAC DO Binary */
       
  2017     send(fd, (char *)buf, 3, 0);
       
  2018 }
       
  2019 
       
  2020 static void socket_set_nodelay(int fd)
       
  2021 {
       
  2022     int val = 1;
       
  2023     setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
       
  2024 }
       
  2025 
       
  2026 static void tcp_chr_accept(void *opaque)
       
  2027 {
       
  2028     CharDriverState *chr = opaque;
       
  2029     TCPCharDriver *s = chr->opaque;
       
  2030     struct sockaddr_in saddr;
       
  2031 #ifndef _WIN32
       
  2032     struct sockaddr_un uaddr;
       
  2033 #endif
       
  2034     struct sockaddr *addr;
       
  2035     socklen_t len;
       
  2036     int fd;
       
  2037 
       
  2038     for(;;) {
       
  2039 #ifndef _WIN32
       
  2040 	if (s->is_unix) {
       
  2041 	    len = sizeof(uaddr);
       
  2042 	    addr = (struct sockaddr *)&uaddr;
       
  2043 	} else
       
  2044 #endif
       
  2045 	{
       
  2046 	    len = sizeof(saddr);
       
  2047 	    addr = (struct sockaddr *)&saddr;
       
  2048 	}
       
  2049         fd = accept(s->listen_fd, addr, &len);
       
  2050         if (fd < 0 && errno != EINTR) {
       
  2051             return;
       
  2052         } else if (fd >= 0) {
       
  2053             if (s->do_telnetopt)
       
  2054                 tcp_chr_telnet_init(fd);
       
  2055             break;
       
  2056         }
       
  2057     }
       
  2058     socket_set_nonblock(fd);
       
  2059     if (s->do_nodelay)
       
  2060         socket_set_nodelay(fd);
       
  2061     s->fd = fd;
       
  2062     qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
       
  2063     tcp_chr_connect(chr);
       
  2064 }
       
  2065 
       
  2066 static void tcp_chr_close(CharDriverState *chr)
       
  2067 {
       
  2068     TCPCharDriver *s = chr->opaque;
       
  2069     if (s->fd >= 0)
       
  2070         closesocket(s->fd);
       
  2071     if (s->listen_fd >= 0)
       
  2072         closesocket(s->listen_fd);
       
  2073     qemu_free(s);
       
  2074 }
       
  2075 
       
  2076 static CharDriverState *qemu_chr_open_tcp(const char *host_str,
       
  2077                                           int is_telnet,
       
  2078 					  int is_unix)
       
  2079 {
       
  2080     CharDriverState *chr = NULL;
       
  2081     TCPCharDriver *s = NULL;
       
  2082     int fd = -1, offset = 0;
       
  2083     int is_listen = 0;
       
  2084     int is_waitconnect = 1;
       
  2085     int do_nodelay = 0;
       
  2086     const char *ptr;
       
  2087 
       
  2088     ptr = host_str;
       
  2089     while((ptr = strchr(ptr,','))) {
       
  2090         ptr++;
       
  2091         if (!strncmp(ptr,"server",6)) {
       
  2092             is_listen = 1;
       
  2093         } else if (!strncmp(ptr,"nowait",6)) {
       
  2094             is_waitconnect = 0;
       
  2095         } else if (!strncmp(ptr,"nodelay",6)) {
       
  2096             do_nodelay = 1;
       
  2097         } else if (!strncmp(ptr,"to=",3)) {
       
  2098             /* nothing, inet_listen() parses this one */;
       
  2099         } else {
       
  2100             printf("Unknown option: %s\n", ptr);
       
  2101             goto fail;
       
  2102         }
       
  2103     }
       
  2104     if (!is_listen)
       
  2105         is_waitconnect = 0;
       
  2106 
       
  2107     chr = qemu_mallocz(sizeof(CharDriverState));
       
  2108     if (!chr)
       
  2109         goto fail;
       
  2110     s = qemu_mallocz(sizeof(TCPCharDriver));
       
  2111     if (!s)
       
  2112         goto fail;
       
  2113 
       
  2114     if (is_listen) {
       
  2115         chr->filename = qemu_malloc(256);
       
  2116         if (is_unix) {
       
  2117             strcpy(chr->filename, "unix:");
       
  2118         } else if (is_telnet) {
       
  2119             strcpy(chr->filename, "telnet:");
       
  2120         } else {
       
  2121             strcpy(chr->filename, "tcp:");
       
  2122         }
       
  2123         offset = strlen(chr->filename);
       
  2124     }
       
  2125     if (is_unix) {
       
  2126         if (is_listen) {
       
  2127             fd = unix_listen(host_str, chr->filename + offset, 256 - offset);
       
  2128         } else {
       
  2129             fd = unix_connect(host_str);
       
  2130         }
       
  2131     } else {
       
  2132         if (is_listen) {
       
  2133             fd = inet_listen(host_str, chr->filename + offset, 256 - offset,
       
  2134                              SOCK_STREAM, 0);
       
  2135         } else {
       
  2136             fd = inet_connect(host_str, SOCK_STREAM);
       
  2137         }
       
  2138     }
       
  2139     if (fd < 0)
       
  2140         goto fail;
       
  2141 
       
  2142     if (!is_waitconnect)
       
  2143         socket_set_nonblock(fd);
       
  2144 
       
  2145     s->connected = 0;
       
  2146     s->fd = -1;
       
  2147     s->listen_fd = -1;
       
  2148     s->is_unix = is_unix;
       
  2149     s->do_nodelay = do_nodelay && !is_unix;
       
  2150 
       
  2151     chr->opaque = s;
       
  2152     chr->chr_write = tcp_chr_write;
       
  2153     chr->chr_close = tcp_chr_close;
       
  2154 
       
  2155     if (is_listen) {
       
  2156         s->listen_fd = fd;
       
  2157         qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
       
  2158         if (is_telnet)
       
  2159             s->do_telnetopt = 1;
       
  2160     } else {
       
  2161         s->connected = 1;
       
  2162         s->fd = fd;
       
  2163         socket_set_nodelay(fd);
       
  2164         tcp_chr_connect(chr);
       
  2165     }
       
  2166 
       
  2167     if (is_listen && is_waitconnect) {
       
  2168         printf("QEMU waiting for connection on: %s\n",
       
  2169                chr->filename ? chr->filename : host_str);
       
  2170         tcp_chr_accept(chr);
       
  2171         socket_set_nonblock(s->listen_fd);
       
  2172     }
       
  2173 
       
  2174     return chr;
       
  2175  fail:
       
  2176     if (fd >= 0)
       
  2177         closesocket(fd);
       
  2178     qemu_free(s);
       
  2179     qemu_free(chr);
       
  2180     return NULL;
       
  2181 }
       
  2182 
       
  2183 static TAILQ_HEAD(CharDriverStateHead, CharDriverState) chardevs
       
  2184 = TAILQ_HEAD_INITIALIZER(chardevs);
       
  2185 
       
  2186 CharDriverState *qemu_chr_open(const char *label, const char *filename)
       
  2187 {
       
  2188     const char *p;
       
  2189     CharDriverState *chr;
       
  2190 
       
  2191     if (!strcmp(filename, "vc")) {
       
  2192         chr = new_text_console(0);
       
  2193     } else
       
  2194     if (strstart(filename, "vc:", &p)) {
       
  2195         chr = new_text_console(p);
       
  2196     } else
       
  2197     if (!strcmp(filename, "null")) {
       
  2198         chr = qemu_chr_open_null();
       
  2199     } else
       
  2200     if (strstart(filename, "tcp:", &p)) {
       
  2201         chr = qemu_chr_open_tcp(p, 0, 0);
       
  2202     } else
       
  2203     if (strstart(filename, "telnet:", &p)) {
       
  2204         chr = qemu_chr_open_tcp(p, 1, 0);
       
  2205     } else
       
  2206     if (strstart(filename, "udp:", &p)) {
       
  2207         chr = qemu_chr_open_udp(p);
       
  2208     } else
       
  2209     if (strstart(filename, "mon:", &p)) {
       
  2210         chr = qemu_chr_open(label, p);
       
  2211         if (chr) {
       
  2212             chr = qemu_chr_open_mux(chr);
       
  2213             monitor_init(chr, !nographic);
       
  2214         } else {
       
  2215             printf("Unable to open driver: %s\n", p);
       
  2216         }
       
  2217     } else
       
  2218 #ifndef _WIN32
       
  2219     if (strstart(filename, "unix:", &p)) {
       
  2220 	chr = qemu_chr_open_tcp(p, 0, 1);
       
  2221     } else if (strstart(filename, "file:", &p)) {
       
  2222         chr = qemu_chr_open_file_out(p);
       
  2223     } else if (strstart(filename, "pipe:", &p)) {
       
  2224         chr = qemu_chr_open_pipe(p);
       
  2225     } else if (!strcmp(filename, "pty")) {
       
  2226         chr = qemu_chr_open_pty();
       
  2227     } else if (!strcmp(filename, "stdio")) {
       
  2228         chr = qemu_chr_open_stdio();
       
  2229     } else
       
  2230 #if defined(__linux__)
       
  2231     if (strstart(filename, "/dev/parport", NULL)) {
       
  2232         chr = qemu_chr_open_pp(filename);
       
  2233     } else
       
  2234 #elif defined(__FreeBSD__)
       
  2235     if (strstart(filename, "/dev/ppi", NULL)) {
       
  2236         chr = qemu_chr_open_pp(filename);
       
  2237     } else
       
  2238 #endif
       
  2239 #if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
       
  2240     || defined(__NetBSD__) || defined(__OpenBSD__)
       
  2241     if (strstart(filename, "/dev/", NULL)) {
       
  2242         chr = qemu_chr_open_tty(filename);
       
  2243     } else
       
  2244 #endif
       
  2245 #else /* !_WIN32 */
       
  2246     if (strstart(filename, "COM", NULL)) {
       
  2247         chr = qemu_chr_open_win(filename);
       
  2248     } else
       
  2249     if (strstart(filename, "pipe:", &p)) {
       
  2250         chr = qemu_chr_open_win_pipe(p);
       
  2251     } else
       
  2252     if (strstart(filename, "file:", &p)) {
       
  2253         chr = qemu_chr_open_win_file_out(p);
       
  2254     } else
       
  2255     if (strstart(filename, "stdio", &p)) {
       
  2256         return qemu_chr_open_win_stdio(filename);
       
  2257     } else
       
  2258 #endif
       
  2259 #ifdef CONFIG_BRLAPI
       
  2260     if (!strcmp(filename, "braille")) {
       
  2261         chr = chr_baum_init();
       
  2262     } else
       
  2263 #endif
       
  2264     {
       
  2265         chr = NULL;
       
  2266     }
       
  2267 
       
  2268     if (chr) {
       
  2269         if (!chr->filename)
       
  2270             chr->filename = qemu_strdup(filename);
       
  2271         chr->label = qemu_strdup(label);
       
  2272         TAILQ_INSERT_TAIL(&chardevs, chr, next);
       
  2273     }
       
  2274     return chr;
       
  2275 }
       
  2276 
       
  2277 void qemu_chr_close(CharDriverState *chr)
       
  2278 {
       
  2279     TAILQ_REMOVE(&chardevs, chr, next);
       
  2280     if (chr->chr_close)
       
  2281         chr->chr_close(chr);
       
  2282     qemu_free(chr->filename);
       
  2283     qemu_free(chr->label);
       
  2284     qemu_free(chr);
       
  2285 }
       
  2286 
       
  2287 void qemu_chr_info(void)
       
  2288 {
       
  2289     CharDriverState *chr;
       
  2290 
       
  2291     TAILQ_FOREACH(chr, &chardevs, next) {
       
  2292         term_printf("%s: filename=%s\n", chr->label, chr->filename);
       
  2293     }
       
  2294 }