1966
1587
atexit(socket_cleanup);
1970
static int send_all(int fd, const uint8_t *buf, int len1)
1976
ret = send(fd, buf, len, 0);
1979
errno = WSAGetLastError();
1980
if (errno != WSAEWOULDBLOCK) {
1983
} else if (ret == 0) {
1993
void socket_set_nonblock(int fd)
1995
unsigned long opt = 1;
1996
ioctlsocket(fd, FIONBIO, &opt);
2001
static int unix_write(int fd, const uint8_t *buf, int len1)
2007
ret = write(fd, buf, len);
2009
if (errno != EINTR && errno != EAGAIN)
2011
} else if (ret == 0) {
2021
static inline int send_all(int fd, const uint8_t *buf, int len1)
2023
return unix_write(fd, buf, len1);
2026
void socket_set_nonblock(int fd)
2028
fcntl(fd, F_SETFL, O_NONBLOCK);
2030
#endif /* !_WIN32 */
2039
#define STDIO_MAX_CLIENTS 1
2040
static int stdio_nb_clients = 0;
2042
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2044
FDCharDriver *s = chr->opaque;
2045
return unix_write(s->fd_out, buf, len);
2048
static int fd_chr_read_poll(void *opaque)
2050
CharDriverState *chr = opaque;
2051
FDCharDriver *s = chr->opaque;
2053
s->max_size = qemu_chr_can_read(chr);
2057
static void fd_chr_read(void *opaque)
2059
CharDriverState *chr = opaque;
2060
FDCharDriver *s = chr->opaque;
2065
if (len > s->max_size)
2069
size = read(s->fd_in, buf, len);
2071
/* FD has been closed. Remove it from the active list. */
2072
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2076
qemu_chr_read(chr, buf, size);
2080
static void fd_chr_update_read_handler(CharDriverState *chr)
2082
FDCharDriver *s = chr->opaque;
2084
if (s->fd_in >= 0) {
2085
if (nographic && s->fd_in == 0) {
2087
qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
2088
fd_chr_read, NULL, chr);
2093
static void fd_chr_close(struct CharDriverState *chr)
2095
FDCharDriver *s = chr->opaque;
2097
if (s->fd_in >= 0) {
2098
if (nographic && s->fd_in == 0) {
2100
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2107
/* open a character device to a unix fd */
2108
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
2110
CharDriverState *chr;
2113
chr = qemu_mallocz(sizeof(CharDriverState));
2116
s = qemu_mallocz(sizeof(FDCharDriver));
2124
chr->chr_write = fd_chr_write;
2125
chr->chr_update_read_handler = fd_chr_update_read_handler;
2126
chr->chr_close = fd_chr_close;
2128
qemu_chr_reset(chr);
2133
static CharDriverState *qemu_chr_open_file_out(const char *file_out)
2137
TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
2140
return qemu_chr_open_fd(-1, fd_out);
2143
static CharDriverState *qemu_chr_open_pipe(const char *filename)
2146
char filename_in[256], filename_out[256];
2148
snprintf(filename_in, 256, "%s.in", filename);
2149
snprintf(filename_out, 256, "%s.out", filename);
2150
TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2151
TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
2152
if (fd_in < 0 || fd_out < 0) {
2157
TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
2161
return qemu_chr_open_fd(fd_in, fd_out);
2165
/* for STDIO, we handle the case where several clients use it
2168
#define TERM_FIFO_MAX_SIZE 1
2170
static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
2171
static int term_fifo_size;
2173
static int stdio_read_poll(void *opaque)
2175
CharDriverState *chr = opaque;
2177
/* try to flush the queue if needed */
2178
if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2179
qemu_chr_read(chr, term_fifo, 1);
2182
/* see if we can absorb more chars */
2183
if (term_fifo_size == 0)
2189
static void stdio_read(void *opaque)
2193
CharDriverState *chr = opaque;
2195
size = read(0, buf, 1);
2197
/* stdin has been closed. Remove it from the active list. */
2198
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2202
if (qemu_chr_can_read(chr) > 0) {
2203
qemu_chr_read(chr, buf, 1);
2204
} else if (term_fifo_size == 0) {
2205
term_fifo[term_fifo_size++] = buf[0];
2210
/* init terminal so that we can grab keys */
2211
static struct termios oldtty;
2212
static int old_fd0_flags;
2213
static int term_atexit_done;
2215
static void term_exit(void)
2217
tcsetattr (0, TCSANOW, &oldtty);
2218
fcntl(0, F_SETFL, old_fd0_flags);
2221
static void term_init(void)
2225
tcgetattr (0, &tty);
2227
old_fd0_flags = fcntl(0, F_GETFL);
2229
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2230
|INLCR|IGNCR|ICRNL|IXON);
2231
tty.c_oflag |= OPOST;
2232
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2233
/* if graphical mode, we allow Ctrl-C handling */
2235
tty.c_lflag &= ~ISIG;
2236
tty.c_cflag &= ~(CSIZE|PARENB);
2239
tty.c_cc[VTIME] = 0;
2241
tcsetattr (0, TCSANOW, &tty);
2243
if (!term_atexit_done++)
2246
fcntl(0, F_SETFL, O_NONBLOCK);
2249
static void qemu_chr_close_stdio(struct CharDriverState *chr)
2253
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2257
static CharDriverState *qemu_chr_open_stdio(void)
2259
CharDriverState *chr;
2261
if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2263
chr = qemu_chr_open_fd(0, 1);
2264
chr->chr_close = qemu_chr_close_stdio;
2265
qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2273
/* Once Solaris has openpty(), this is going to be removed. */
2274
int openpty(int *amaster, int *aslave, char *name,
2275
struct termios *termp, struct winsize *winp)
2278
int mfd = -1, sfd = -1;
2280
*amaster = *aslave = -1;
2282
mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
2286
if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
2289
if ((slave = ptsname(mfd)) == NULL)
2292
if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
2295
if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
2296
(termp != NULL && tcgetattr(sfd, termp) < 0))
2304
ioctl(sfd, TIOCSWINSZ, winp);
2315
void cfmakeraw (struct termios *termios_p)
2317
termios_p->c_iflag &=
2318
~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
2319
termios_p->c_oflag &= ~OPOST;
2320
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
2321
termios_p->c_cflag &= ~(CSIZE|PARENB);
2322
termios_p->c_cflag |= CS8;
2324
termios_p->c_cc[VMIN] = 0;
2325
termios_p->c_cc[VTIME] = 0;
2329
#if defined(__linux__) || defined(__sun__)
2330
static CharDriverState *qemu_chr_open_pty(void)
2333
int master_fd, slave_fd;
2335
if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) {
2339
/* Set raw attributes on the pty. */
2341
tcsetattr(slave_fd, TCSAFLUSH, &tty);
2343
fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
2344
return qemu_chr_open_fd(master_fd, master_fd);
2347
static void tty_serial_init(int fd, int speed,
2348
int parity, int data_bits, int stop_bits)
2354
printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2355
speed, parity, data_bits, stop_bits);
2357
tcgetattr (fd, &tty);
2360
if (speed <= 50 * MARGIN)
2362
else if (speed <= 75 * MARGIN)
2364
else if (speed <= 300 * MARGIN)
2366
else if (speed <= 600 * MARGIN)
2368
else if (speed <= 1200 * MARGIN)
2370
else if (speed <= 2400 * MARGIN)
2372
else if (speed <= 4800 * MARGIN)
2374
else if (speed <= 9600 * MARGIN)
2376
else if (speed <= 19200 * MARGIN)
2378
else if (speed <= 38400 * MARGIN)
2380
else if (speed <= 57600 * MARGIN)
2382
else if (speed <= 115200 * MARGIN)
2387
cfsetispeed(&tty, spd);
2388
cfsetospeed(&tty, spd);
2390
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2391
|INLCR|IGNCR|ICRNL|IXON);
2392
tty.c_oflag |= OPOST;
2393
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
2394
tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
2415
tty.c_cflag |= PARENB;
2418
tty.c_cflag |= PARENB | PARODD;
2422
tty.c_cflag |= CSTOPB;
2424
tcsetattr (fd, TCSANOW, &tty);
2427
static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
2429
FDCharDriver *s = chr->opaque;
2432
case CHR_IOCTL_SERIAL_SET_PARAMS:
2434
QEMUSerialSetParams *ssp = arg;
2435
tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
2436
ssp->data_bits, ssp->stop_bits);
2439
case CHR_IOCTL_SERIAL_SET_BREAK:
2441
int enable = *(int *)arg;
2443
tcsendbreak(s->fd_in, 1);
2452
static CharDriverState *qemu_chr_open_tty(const char *filename)
2454
CharDriverState *chr;
2457
TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
2458
fcntl(fd, F_SETFL, O_NONBLOCK);
2459
tty_serial_init(fd, 115200, 'N', 8, 1);
2460
chr = qemu_chr_open_fd(fd, fd);
2465
chr->chr_ioctl = tty_serial_ioctl;
2466
qemu_chr_reset(chr);
2469
#else /* ! __linux__ && ! __sun__ */
2470
static CharDriverState *qemu_chr_open_pty(void)
2474
#endif /* __linux__ || __sun__ */
2476
#if defined(__linux__)
2480
} ParallelCharDriver;
2482
static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2484
if (s->mode != mode) {
2486
if (ioctl(s->fd, PPSETMODE, &m) < 0)
2493
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2495
ParallelCharDriver *drv = chr->opaque;
2500
case CHR_IOCTL_PP_READ_DATA:
2501
if (ioctl(fd, PPRDATA, &b) < 0)
2503
*(uint8_t *)arg = b;
2505
case CHR_IOCTL_PP_WRITE_DATA:
2506
b = *(uint8_t *)arg;
2507
if (ioctl(fd, PPWDATA, &b) < 0)
2510
case CHR_IOCTL_PP_READ_CONTROL:
2511
if (ioctl(fd, PPRCONTROL, &b) < 0)
2513
/* Linux gives only the lowest bits, and no way to know data
2514
direction! For better compatibility set the fixed upper
2516
*(uint8_t *)arg = b | 0xc0;
2518
case CHR_IOCTL_PP_WRITE_CONTROL:
2519
b = *(uint8_t *)arg;
2520
if (ioctl(fd, PPWCONTROL, &b) < 0)
2523
case CHR_IOCTL_PP_READ_STATUS:
2524
if (ioctl(fd, PPRSTATUS, &b) < 0)
2526
*(uint8_t *)arg = b;
2528
case CHR_IOCTL_PP_EPP_READ_ADDR:
2529
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2530
struct ParallelIOArg *parg = arg;
2531
int n = read(fd, parg->buffer, parg->count);
2532
if (n != parg->count) {
2537
case CHR_IOCTL_PP_EPP_READ:
2538
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2539
struct ParallelIOArg *parg = arg;
2540
int n = read(fd, parg->buffer, parg->count);
2541
if (n != parg->count) {
2546
case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2547
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2548
struct ParallelIOArg *parg = arg;
2549
int n = write(fd, parg->buffer, parg->count);
2550
if (n != parg->count) {
2555
case CHR_IOCTL_PP_EPP_WRITE:
2556
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2557
struct ParallelIOArg *parg = arg;
2558
int n = write(fd, parg->buffer, parg->count);
2559
if (n != parg->count) {
2570
static void pp_close(CharDriverState *chr)
2572
ParallelCharDriver *drv = chr->opaque;
2575
pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2576
ioctl(fd, PPRELEASE);
2581
static CharDriverState *qemu_chr_open_pp(const char *filename)
2583
CharDriverState *chr;
2584
ParallelCharDriver *drv;
2587
TFR(fd = open(filename, O_RDWR));
2591
if (ioctl(fd, PPCLAIM) < 0) {
2596
drv = qemu_mallocz(sizeof(ParallelCharDriver));
2602
drv->mode = IEEE1284_MODE_COMPAT;
2604
chr = qemu_mallocz(sizeof(CharDriverState));
2610
chr->chr_write = null_chr_write;
2611
chr->chr_ioctl = pp_ioctl;
2612
chr->chr_close = pp_close;
2615
qemu_chr_reset(chr);
2619
#endif /* __linux__ */
2625
HANDLE hcom, hrecv, hsend;
2626
OVERLAPPED orecv, osend;
2631
#define NSENDBUF 2048
2632
#define NRECVBUF 2048
2633
#define MAXCONNECT 1
2634
#define NTIMEOUT 5000
2636
static int win_chr_poll(void *opaque);
2637
static int win_chr_pipe_poll(void *opaque);
2639
static void win_chr_close(CharDriverState *chr)
2641
WinCharState *s = chr->opaque;
2644
CloseHandle(s->hsend);
2648
CloseHandle(s->hrecv);
2652
CloseHandle(s->hcom);
2656
qemu_del_polling_cb(win_chr_pipe_poll, chr);
2658
qemu_del_polling_cb(win_chr_poll, chr);
2661
static int win_chr_init(CharDriverState *chr, const char *filename)
2663
WinCharState *s = chr->opaque;
2665
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2670
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2672
fprintf(stderr, "Failed CreateEvent\n");
2675
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2677
fprintf(stderr, "Failed CreateEvent\n");
2681
s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2682
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2683
if (s->hcom == INVALID_HANDLE_VALUE) {
2684
fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2689
if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2690
fprintf(stderr, "Failed SetupComm\n");
2694
ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2695
size = sizeof(COMMCONFIG);
2696
GetDefaultCommConfig(filename, &comcfg, &size);
2697
comcfg.dcb.DCBlength = sizeof(DCB);
2698
CommConfigDialog(filename, NULL, &comcfg);
2700
if (!SetCommState(s->hcom, &comcfg.dcb)) {
2701
fprintf(stderr, "Failed SetCommState\n");
2705
if (!SetCommMask(s->hcom, EV_ERR)) {
2706
fprintf(stderr, "Failed SetCommMask\n");
2710
cto.ReadIntervalTimeout = MAXDWORD;
2711
if (!SetCommTimeouts(s->hcom, &cto)) {
2712
fprintf(stderr, "Failed SetCommTimeouts\n");
2716
if (!ClearCommError(s->hcom, &err, &comstat)) {
2717
fprintf(stderr, "Failed ClearCommError\n");
2720
qemu_add_polling_cb(win_chr_poll, chr);
2728
static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2730
WinCharState *s = chr->opaque;
2731
DWORD len, ret, size, err;
2734
ZeroMemory(&s->osend, sizeof(s->osend));
2735
s->osend.hEvent = s->hsend;
2738
ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2740
ret = WriteFile(s->hcom, buf, len, &size, NULL);
2742
err = GetLastError();
2743
if (err == ERROR_IO_PENDING) {
2744
ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2762
static int win_chr_read_poll(CharDriverState *chr)
2764
WinCharState *s = chr->opaque;
2766
s->max_size = qemu_chr_can_read(chr);
2770
static void win_chr_readfile(CharDriverState *chr)
2772
WinCharState *s = chr->opaque;
2777
ZeroMemory(&s->orecv, sizeof(s->orecv));
2778
s->orecv.hEvent = s->hrecv;
2779
ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2781
err = GetLastError();
2782
if (err == ERROR_IO_PENDING) {
2783
ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2788
qemu_chr_read(chr, buf, size);
2792
static void win_chr_read(CharDriverState *chr)
2794
WinCharState *s = chr->opaque;
2796
if (s->len > s->max_size)
2797
s->len = s->max_size;
2801
win_chr_readfile(chr);
2804
static int win_chr_poll(void *opaque)
2806
CharDriverState *chr = opaque;
2807
WinCharState *s = chr->opaque;
2811
ClearCommError(s->hcom, &comerr, &status);
2812
if (status.cbInQue > 0) {
2813
s->len = status.cbInQue;
2814
win_chr_read_poll(chr);
2821
static CharDriverState *qemu_chr_open_win(const char *filename)
2823
CharDriverState *chr;
2826
chr = qemu_mallocz(sizeof(CharDriverState));
2829
s = qemu_mallocz(sizeof(WinCharState));
2835
chr->chr_write = win_chr_write;
2836
chr->chr_close = win_chr_close;
2838
if (win_chr_init(chr, filename) < 0) {
2843
qemu_chr_reset(chr);
2847
static int win_chr_pipe_poll(void *opaque)
2849
CharDriverState *chr = opaque;
2850
WinCharState *s = chr->opaque;
2853
PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2856
win_chr_read_poll(chr);
2863
static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
2865
WinCharState *s = chr->opaque;
2873
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2875
fprintf(stderr, "Failed CreateEvent\n");
2878
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2880
fprintf(stderr, "Failed CreateEvent\n");
2884
snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
2885
s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
2886
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
2888
MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
2889
if (s->hcom == INVALID_HANDLE_VALUE) {
2890
fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
2895
ZeroMemory(&ov, sizeof(ov));
2896
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2897
ret = ConnectNamedPipe(s->hcom, &ov);
2899
fprintf(stderr, "Failed ConnectNamedPipe\n");
2903
ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
2905
fprintf(stderr, "Failed GetOverlappedResult\n");
2907
CloseHandle(ov.hEvent);
2914
CloseHandle(ov.hEvent);
2917
qemu_add_polling_cb(win_chr_pipe_poll, chr);
2926
static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
2928
CharDriverState *chr;
2931
chr = qemu_mallocz(sizeof(CharDriverState));
2934
s = qemu_mallocz(sizeof(WinCharState));
2940
chr->chr_write = win_chr_write;
2941
chr->chr_close = win_chr_close;
2943
if (win_chr_pipe_init(chr, filename) < 0) {
2948
qemu_chr_reset(chr);
2952
static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
2954
CharDriverState *chr;
2957
chr = qemu_mallocz(sizeof(CharDriverState));
2960
s = qemu_mallocz(sizeof(WinCharState));
2967
chr->chr_write = win_chr_write;
2968
qemu_chr_reset(chr);
2972
static CharDriverState *qemu_chr_open_win_con(const char *filename)
2974
return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
2977
static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
2981
fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
2982
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2983
if (fd_out == INVALID_HANDLE_VALUE)
2986
return qemu_chr_open_win_file(fd_out);
2988
#endif /* !_WIN32 */
2990
/***********************************************************/
2991
/* UDP Net console */
2995
struct sockaddr_in daddr;
3002
static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3004
NetCharDriver *s = chr->opaque;
3006
return sendto(s->fd, buf, len, 0,
3007
(struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
3010
static int udp_chr_read_poll(void *opaque)
3012
CharDriverState *chr = opaque;
3013
NetCharDriver *s = chr->opaque;
3015
s->max_size = qemu_chr_can_read(chr);
3017
/* If there were any stray characters in the queue process them
3020
while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3021
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3023
s->max_size = qemu_chr_can_read(chr);
3028
static void udp_chr_read(void *opaque)
3030
CharDriverState *chr = opaque;
3031
NetCharDriver *s = chr->opaque;
3033
if (s->max_size == 0)
3035
s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
3036
s->bufptr = s->bufcnt;
3041
while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3042
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3044
s->max_size = qemu_chr_can_read(chr);
3048
static void udp_chr_update_read_handler(CharDriverState *chr)
3050
NetCharDriver *s = chr->opaque;
3053
qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
3054
udp_chr_read, NULL, chr);
3058
int parse_host_port(struct sockaddr_in *saddr, const char *str);
3060
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
3062
int parse_host_src_port(struct sockaddr_in *haddr,
3063
struct sockaddr_in *saddr,
3066
static CharDriverState *qemu_chr_open_udp(const char *def)
3068
CharDriverState *chr = NULL;
3069
NetCharDriver *s = NULL;
3071
struct sockaddr_in saddr;
3073
chr = qemu_mallocz(sizeof(CharDriverState));
3076
s = qemu_mallocz(sizeof(NetCharDriver));
3080
fd = socket(PF_INET, SOCK_DGRAM, 0);
3082
perror("socket(PF_INET, SOCK_DGRAM)");
3086
if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
3087
printf("Could not parse: %s\n", def);
3091
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
3101
chr->chr_write = udp_chr_write;
3102
chr->chr_update_read_handler = udp_chr_update_read_handler;
3115
/***********************************************************/
3116
/* TCP Net console */
3127
static void tcp_chr_accept(void *opaque);
3129
static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3131
TCPCharDriver *s = chr->opaque;
3133
return send_all(s->fd, buf, len);
3135
/* XXX: indicate an error ? */
3140
static int tcp_chr_read_poll(void *opaque)
3142
CharDriverState *chr = opaque;
3143
TCPCharDriver *s = chr->opaque;
3146
s->max_size = qemu_chr_can_read(chr);
3151
#define IAC_BREAK 243
3152
static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
3154
uint8_t *buf, int *size)
3156
/* Handle any telnet client's basic IAC options to satisfy char by
3157
* char mode with no echo. All IAC options will be removed from
3158
* the buf and the do_telnetopt variable will be used to track the
3159
* state of the width of the IAC information.
3161
* IAC commands come in sets of 3 bytes with the exception of the
3162
* "IAC BREAK" command and the double IAC.
3168
for (i = 0; i < *size; i++) {
3169
if (s->do_telnetopt > 1) {
3170
if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3171
/* Double IAC means send an IAC */
3175
s->do_telnetopt = 1;
3177
if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3178
/* Handle IAC break commands by sending a serial break */
3179
qemu_chr_event(chr, CHR_EVENT_BREAK);
3184
if (s->do_telnetopt >= 4) {
3185
s->do_telnetopt = 1;
3188
if ((unsigned char)buf[i] == IAC) {
3189
s->do_telnetopt = 2;
3200
static void tcp_chr_read(void *opaque)
3202
CharDriverState *chr = opaque;
3203
TCPCharDriver *s = chr->opaque;
3207
if (!s->connected || s->max_size <= 0)
3210
if (len > s->max_size)
3212
size = recv(s->fd, buf, len, 0);
3214
/* connection closed */
3216
if (s->listen_fd >= 0) {
3217
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3219
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3222
} else if (size > 0) {
3223
if (s->do_telnetopt)
3224
tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3226
qemu_chr_read(chr, buf, size);
3230
static void tcp_chr_connect(void *opaque)
3232
CharDriverState *chr = opaque;
3233
TCPCharDriver *s = chr->opaque;
3236
qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3237
tcp_chr_read, NULL, chr);
3238
qemu_chr_reset(chr);
3241
#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3242
static void tcp_chr_telnet_init(int fd)
3245
/* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3246
IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */
3247
send(fd, (char *)buf, 3, 0);
3248
IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */
3249
send(fd, (char *)buf, 3, 0);
3250
IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */
3251
send(fd, (char *)buf, 3, 0);
3252
IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */
3253
send(fd, (char *)buf, 3, 0);
3256
static void socket_set_nodelay(int fd)
3259
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3262
static void tcp_chr_accept(void *opaque)
3264
CharDriverState *chr = opaque;
3265
TCPCharDriver *s = chr->opaque;
3266
struct sockaddr_in saddr;
3268
struct sockaddr_un uaddr;
3270
struct sockaddr *addr;
3277
len = sizeof(uaddr);
3278
addr = (struct sockaddr *)&uaddr;
3282
len = sizeof(saddr);
3283
addr = (struct sockaddr *)&saddr;
3285
fd = accept(s->listen_fd, addr, &len);
3286
if (fd < 0 && errno != EINTR) {
3288
} else if (fd >= 0) {
3289
if (s->do_telnetopt)
3290
tcp_chr_telnet_init(fd);
3294
socket_set_nonblock(fd);
3296
socket_set_nodelay(fd);
3298
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3299
tcp_chr_connect(chr);
3302
static void tcp_chr_close(CharDriverState *chr)
3304
TCPCharDriver *s = chr->opaque;
3307
if (s->listen_fd >= 0)
3308
closesocket(s->listen_fd);
3312
static CharDriverState *qemu_chr_open_tcp(const char *host_str,
3316
CharDriverState *chr = NULL;
3317
TCPCharDriver *s = NULL;
3318
int fd = -1, ret, err, val;
3320
int is_waitconnect = 1;
3323
struct sockaddr_in saddr;
3325
struct sockaddr_un uaddr;
3327
struct sockaddr *addr;
3332
addr = (struct sockaddr *)&uaddr;
3333
addrlen = sizeof(uaddr);
3334
if (parse_unix_path(&uaddr, host_str) < 0)
3339
addr = (struct sockaddr *)&saddr;
3340
addrlen = sizeof(saddr);
3341
if (parse_host_port(&saddr, host_str) < 0)
3346
while((ptr = strchr(ptr,','))) {
3348
if (!strncmp(ptr,"server",6)) {
3350
} else if (!strncmp(ptr,"nowait",6)) {
3352
} else if (!strncmp(ptr,"nodelay",6)) {
3355
printf("Unknown option: %s\n", ptr);
3362
chr = qemu_mallocz(sizeof(CharDriverState));
3365
s = qemu_mallocz(sizeof(TCPCharDriver));
3371
fd = socket(PF_UNIX, SOCK_STREAM, 0);
3374
fd = socket(PF_INET, SOCK_STREAM, 0);
3379
if (!is_waitconnect)
3380
socket_set_nonblock(fd);
3385
s->is_unix = is_unix;
3386
s->do_nodelay = do_nodelay && !is_unix;
3389
chr->chr_write = tcp_chr_write;
3390
chr->chr_close = tcp_chr_close;
3393
/* allow fast reuse */
3397
strncpy(path, uaddr.sun_path, 108);
3404
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3407
ret = bind(fd, addr, addrlen);
3411
ret = listen(fd, 0);
3416
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3418
s->do_telnetopt = 1;
3421
ret = connect(fd, addr, addrlen);
3423
err = socket_error();
3424
if (err == EINTR || err == EWOULDBLOCK) {
3425
} else if (err == EINPROGRESS) {
3428
} else if (err == WSAEALREADY) {
3440
socket_set_nodelay(fd);
3442
tcp_chr_connect(chr);
3444
qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3447
if (is_listen && is_waitconnect) {
3448
printf("QEMU waiting for connection on: %s\n", host_str);
3449
tcp_chr_accept(chr);
3450
socket_set_nonblock(s->listen_fd);
3462
CharDriverState *qemu_chr_open(const char *filename)
3466
if (!strcmp(filename, "vc")) {
3467
return text_console_init(&display_state, 0);
3468
} else if (strstart(filename, "vc:", &p)) {
3469
return text_console_init(&display_state, p);
3470
} else if (!strcmp(filename, "null")) {
3471
return qemu_chr_open_null();
3473
if (strstart(filename, "tcp:", &p)) {
3474
return qemu_chr_open_tcp(p, 0, 0);
3476
if (strstart(filename, "telnet:", &p)) {
3477
return qemu_chr_open_tcp(p, 1, 0);
3479
if (strstart(filename, "udp:", &p)) {
3480
return qemu_chr_open_udp(p);
3482
if (strstart(filename, "mon:", &p)) {
3483
CharDriverState *drv = qemu_chr_open(p);
3485
drv = qemu_chr_open_mux(drv);
3486
monitor_init(drv, !nographic);
3489
printf("Unable to open driver: %s\n", p);
3493
if (strstart(filename, "unix:", &p)) {
3494
return qemu_chr_open_tcp(p, 0, 1);
3495
} else if (strstart(filename, "file:", &p)) {
3496
return qemu_chr_open_file_out(p);
3497
} else if (strstart(filename, "pipe:", &p)) {
3498
return qemu_chr_open_pipe(p);
3499
} else if (!strcmp(filename, "pty")) {
3500
return qemu_chr_open_pty();
3501
} else if (!strcmp(filename, "stdio")) {
3502
return qemu_chr_open_stdio();
3504
#if defined(__linux__)
3505
if (strstart(filename, "/dev/parport", NULL)) {
3506
return qemu_chr_open_pp(filename);
3509
#if defined(__linux__) || defined(__sun__)
3510
if (strstart(filename, "/dev/", NULL)) {
3511
return qemu_chr_open_tty(filename);
3515
if (strstart(filename, "COM", NULL)) {
3516
return qemu_chr_open_win(filename);
3518
if (strstart(filename, "pipe:", &p)) {
3519
return qemu_chr_open_win_pipe(p);
3521
if (strstart(filename, "con:", NULL)) {
3522
return qemu_chr_open_win_con(filename);
3524
if (strstart(filename, "file:", &p)) {
3525
return qemu_chr_open_win_file_out(p);
3528
#ifdef CONFIG_BRLAPI
3529
if (!strcmp(filename, "braille")) {
3530
return chr_baum_init();
3538
void qemu_chr_close(CharDriverState *chr)
3541
chr->chr_close(chr);
3545
/***********************************************************/
3546
/* network device redirectors */
3548
__attribute__ (( unused ))
3549
static void hex_dump(FILE *f, const uint8_t *buf, int size)
3553
for(i=0;i<size;i+=16) {
3557
fprintf(f, "%08x ", i);
3560
fprintf(f, " %02x", buf[i+j]);
3565
for(j=0;j<len;j++) {
3567
if (c < ' ' || c > '~')
3569
fprintf(f, "%c", c);
3575
static int parse_macaddr(uint8_t *macaddr, const char *p)
3582
offset = strtol(p, &last_char, 0);
3583
if (0 == errno && '\0' == *last_char &&
3584
offset >= 0 && offset <= 0xFFFFFF) {
3585
macaddr[3] = (offset & 0xFF0000) >> 16;
3586
macaddr[4] = (offset & 0xFF00) >> 8;
3587
macaddr[5] = offset & 0xFF;
3590
for(i = 0; i < 6; i++) {
3591
macaddr[i] = strtol(p, (char **)&p, 16);
3596
if (*p != ':' && *p != '-')
3607
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3612
p1 = strchr(p, sep);
3618
if (len > buf_size - 1)
3620
memcpy(buf, p, len);
3627
int parse_host_src_port(struct sockaddr_in *haddr,
3628
struct sockaddr_in *saddr,
3629
const char *input_str)
3631
char *str = strdup(input_str);
3632
char *host_str = str;
3637
* Chop off any extra arguments at the end of the string which
3638
* would start with a comma, then fill in the src port information
3639
* if it was provided else use the "any address" and "any port".
3641
if ((ptr = strchr(str,',')))
3644
if ((src_str = strchr(input_str,'@'))) {
3649
if (parse_host_port(haddr, host_str) < 0)
3652
if (!src_str || *src_str == '\0')
3655
if (parse_host_port(saddr, src_str) < 0)
3666
int parse_host_port(struct sockaddr_in *saddr, const char *str)
3674
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3676
saddr->sin_family = AF_INET;
3677
if (buf[0] == '\0') {
3678
saddr->sin_addr.s_addr = 0;
3680
if (isdigit(buf[0])) {
3681
if (!inet_aton(buf, &saddr->sin_addr))
3684
if ((he = gethostbyname(buf)) == NULL)
3686
saddr->sin_addr = *(struct in_addr *)he->h_addr;
3689
port = strtol(p, (char **)&r, 0);
3692
saddr->sin_port = htons(port);
3697
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
3702
len = MIN(108, strlen(str));
3703
p = strchr(str, ',');
3705
len = MIN(len, p - str);
3707
memset(uaddr, 0, sizeof(*uaddr));
3709
uaddr->sun_family = AF_UNIX;
3710
memcpy(uaddr->sun_path, str, len);
3716
/* find or alloc a new VLAN */
3717
VLANState *qemu_find_vlan(int id)
3719
VLANState **pvlan, *vlan;
3720
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1592
/***********************************************************/
1593
/* Bluetooth support */
1596
static struct HCIInfo *hci_table[MAX_NICS];
1598
static struct bt_vlan_s {
1599
struct bt_scatternet_s net;
1601
struct bt_vlan_s *next;
1604
/* find or alloc a new bluetooth "VLAN" */
1605
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1607
struct bt_vlan_s **pvlan, *vlan;
1608
for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
3721
1609
if (vlan->id == id)
3724
vlan = qemu_mallocz(sizeof(VLANState));
1612
vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
3729
pvlan = &first_vlan;
1614
pvlan = &first_bt_vlan;
3730
1615
while (*pvlan != NULL)
3731
1616
pvlan = &(*pvlan)->next;
3736
VLANClientState *qemu_new_vlan_client(VLANState *vlan,
3737
IOReadHandler *fd_read,
3738
IOCanRWHandler *fd_can_read,
3741
VLANClientState *vc, **pvc;
3742
vc = qemu_mallocz(sizeof(VLANClientState));
3745
vc->fd_read = fd_read;
3746
vc->fd_can_read = fd_can_read;
3747
vc->opaque = opaque;
3751
pvc = &vlan->first_client;
3752
while (*pvc != NULL)
3753
pvc = &(*pvc)->next;
3758
int qemu_can_send_packet(VLANClientState *vc1)
3760
VLANState *vlan = vc1->vlan;
3761
VLANClientState *vc;
3763
for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3765
if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3772
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3774
VLANState *vlan = vc1->vlan;
3775
VLANClientState *vc;
3778
printf("vlan %d send:\n", vlan->id);
3779
hex_dump(stdout, buf, size);
3781
for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3783
vc->fd_read(vc->opaque, buf, size);
3788
#if defined(CONFIG_SLIRP)
3790
/* slirp network adapter */
3792
static int slirp_inited;
3793
static VLANClientState *slirp_vc;
3795
int slirp_can_output(void)
3797
return !slirp_vc || qemu_can_send_packet(slirp_vc);
3800
void slirp_output(const uint8_t *pkt, int pkt_len)
3803
printf("slirp output:\n");
3804
hex_dump(stdout, pkt, pkt_len);
3808
qemu_send_packet(slirp_vc, pkt, pkt_len);
3811
static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3814
printf("slirp input:\n");
3815
hex_dump(stdout, buf, size);
3817
slirp_input(buf, size);
3820
static int net_slirp_init(VLANState *vlan)
3822
if (!slirp_inited) {
3826
slirp_vc = qemu_new_vlan_client(vlan,
3827
slirp_receive, NULL, NULL);
3828
snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3832
static void net_slirp_redir(const char *redir_str)
3837
struct in_addr guest_addr;
3838
int host_port, guest_port;
3840
if (!slirp_inited) {
3846
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3848
if (!strcmp(buf, "tcp")) {
3850
} else if (!strcmp(buf, "udp")) {
3856
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3858
host_port = strtol(buf, &r, 0);
3862
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3864
if (buf[0] == '\0') {
3865
pstrcpy(buf, sizeof(buf), "10.0.2.15");
3867
if (!inet_aton(buf, &guest_addr))
3870
guest_port = strtol(p, &r, 0);
3874
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
3875
fprintf(stderr, "qemu: could not set up redirection\n");
3880
fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
3888
static void erase_dir(char *dir_name)
3892
char filename[1024];
3894
/* erase all the files in the directory */
3895
if ((d = opendir(dir_name)) != 0) {
3900
if (strcmp(de->d_name, ".") != 0 &&
3901
strcmp(de->d_name, "..") != 0) {
3902
snprintf(filename, sizeof(filename), "%s/%s",
3903
smb_dir, de->d_name);
3904
if (unlink(filename) != 0) /* is it a directory? */
3905
erase_dir(filename);
3913
/* automatic user mode samba server configuration */
3914
static void smb_exit(void)
3919
/* automatic user mode samba server configuration */
3920
static void net_slirp_smb(const char *exported_dir)
3922
char smb_conf[1024];
3923
char smb_cmdline[1024];
3926
if (!slirp_inited) {
3931
/* XXX: better tmp dir construction */
3932
snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
3933
if (mkdir(smb_dir, 0700) < 0) {
3934
fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
3937
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
3939
f = fopen(smb_conf, "w");
3941
fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
3948
"socket address=127.0.0.1\n"
3949
"pid directory=%s\n"
3950
"lock directory=%s\n"
3951
"log file=%s/log.smbd\n"
3952
"smb passwd file=%s/smbpasswd\n"
3953
"security = share\n"
3968
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
3969
SMBD_COMMAND, smb_conf);
3971
slirp_add_exec(0, smb_cmdline, 4, 139);
3974
#endif /* !defined(_WIN32) */
3975
void do_info_slirp(void)
3980
#endif /* CONFIG_SLIRP */
3982
#if !defined(_WIN32)
3984
typedef struct TAPState {
3985
VLANClientState *vc;
3987
char down_script[1024];
3990
static void tap_receive(void *opaque, const uint8_t *buf, int size)
3992
TAPState *s = opaque;
3995
ret = write(s->fd, buf, size);
3996
if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
4003
static void tap_send(void *opaque)
4005
TAPState *s = opaque;
4012
sbuf.maxlen = sizeof(buf);
4014
size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
4016
size = read(s->fd, buf, sizeof(buf));
4019
qemu_send_packet(s->vc, buf, size);
4025
static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
4029
s = qemu_mallocz(sizeof(TAPState));
4033
s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
4034
qemu_set_fd_handler(s->fd, tap_send, NULL, s);
4035
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
4039
#if defined (_BSD) || defined (__FreeBSD_kernel__)
4040
static int tap_open(char *ifname, int ifname_size)
4046
TFR(fd = open("/dev/tap", O_RDWR));
4048
fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
4053
dev = devname(s.st_rdev, S_IFCHR);
4054
pstrcpy(ifname, ifname_size, dev);
4056
fcntl(fd, F_SETFL, O_NONBLOCK);
4059
#elif defined(__sun__)
4060
#define TUNNEWPPA (('T'<<16) | 0x0001)
4062
* Allocate TAP device, returns opened fd.
4063
* Stores dev name in the first arg(must be large enough).
4065
int tap_alloc(char *dev)
4067
int tap_fd, if_fd, ppa = -1;
4068
static int ip_fd = 0;
4071
static int arp_fd = 0;
4072
int ip_muxid, arp_muxid;
4073
struct strioctl strioc_if, strioc_ppa;
4074
int link_type = I_PLINK;;
4076
char actual_name[32] = "";
4078
memset(&ifr, 0x0, sizeof(ifr));
4082
while( *ptr && !isdigit((int)*ptr) ) ptr++;
4086
/* Check if IP device was opened */
4090
TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
4092
syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
4096
TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
4098
syslog(LOG_ERR, "Can't open /dev/tap");
4102
/* Assign a new PPA and get its unit number. */
4103
strioc_ppa.ic_cmd = TUNNEWPPA;
4104
strioc_ppa.ic_timout = 0;
4105
strioc_ppa.ic_len = sizeof(ppa);
4106
strioc_ppa.ic_dp = (char *)&ppa;
4107
if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
4108
syslog (LOG_ERR, "Can't assign new interface");
4110
TFR(if_fd = open("/dev/tap", O_RDWR, 0));
4112
syslog(LOG_ERR, "Can't open /dev/tap (2)");
4115
if(ioctl(if_fd, I_PUSH, "ip") < 0){
4116
syslog(LOG_ERR, "Can't push IP module");
4120
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
4121
syslog(LOG_ERR, "Can't get flags\n");
4123
snprintf (actual_name, 32, "tap%d", ppa);
4124
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4127
/* Assign ppa according to the unit number returned by tun device */
4129
if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
4130
syslog (LOG_ERR, "Can't set PPA %d", ppa);
4131
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
4132
syslog (LOG_ERR, "Can't get flags\n");
4133
/* Push arp module to if_fd */
4134
if (ioctl (if_fd, I_PUSH, "arp") < 0)
4135
syslog (LOG_ERR, "Can't push ARP module (2)");
4137
/* Push arp module to ip_fd */
4138
if (ioctl (ip_fd, I_POP, NULL) < 0)
4139
syslog (LOG_ERR, "I_POP failed\n");
4140
if (ioctl (ip_fd, I_PUSH, "arp") < 0)
4141
syslog (LOG_ERR, "Can't push ARP module (3)\n");
4143
TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
4145
syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
4147
/* Set ifname to arp */
4148
strioc_if.ic_cmd = SIOCSLIFNAME;
4149
strioc_if.ic_timout = 0;
4150
strioc_if.ic_len = sizeof(ifr);
4151
strioc_if.ic_dp = (char *)𝔦
4152
if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
4153
syslog (LOG_ERR, "Can't set ifname to arp\n");
4156
if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
4157
syslog(LOG_ERR, "Can't link TAP device to IP");
4161
if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
4162
syslog (LOG_ERR, "Can't link TAP device to ARP");
4166
memset(&ifr, 0x0, sizeof(ifr));
4167
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4168
ifr.lifr_ip_muxid = ip_muxid;
4169
ifr.lifr_arp_muxid = arp_muxid;
4171
if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
4173
ioctl (ip_fd, I_PUNLINK , arp_muxid);
4174
ioctl (ip_fd, I_PUNLINK, ip_muxid);
4175
syslog (LOG_ERR, "Can't set multiplexor id");
4178
sprintf(dev, "tap%d", ppa);
4182
static int tap_open(char *ifname, int ifname_size)
4186
if( (fd = tap_alloc(dev)) < 0 ){
4187
fprintf(stderr, "Cannot allocate TAP device\n");
4190
pstrcpy(ifname, ifname_size, dev);
4191
fcntl(fd, F_SETFL, O_NONBLOCK);
4195
static int tap_open(char *ifname, int ifname_size)
4200
TFR(fd = open("/dev/net/tun", O_RDWR));
4202
fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4205
memset(&ifr, 0, sizeof(ifr));
4206
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
4207
if (ifname[0] != '\0')
4208
pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4210
pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
4211
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4213
fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4217
pstrcpy(ifname, ifname_size, ifr.ifr_name);
4218
fcntl(fd, F_SETFL, O_NONBLOCK);
4223
static int launch_script(const char *setup_script, const char *ifname, int fd)
4229
/* try to launch network script */
4233
int open_max = sysconf (_SC_OPEN_MAX), i;
4234
for (i = 0; i < open_max; i++)
4235
if (i != STDIN_FILENO &&
4236
i != STDOUT_FILENO &&
4237
i != STDERR_FILENO &&
4242
*parg++ = (char *)setup_script;
4243
*parg++ = (char *)ifname;
4245
execv(setup_script, args);
4248
while (waitpid(pid, &status, 0) != pid);
4249
if (!WIFEXITED(status) ||
4250
WEXITSTATUS(status) != 0) {
4251
fprintf(stderr, "%s: could not launch network script\n",
4259
static int net_tap_init(VLANState *vlan, const char *ifname1,
4260
const char *setup_script, const char *down_script)
4266
if (ifname1 != NULL)
4267
pstrcpy(ifname, sizeof(ifname), ifname1);
4270
TFR(fd = tap_open(ifname, sizeof(ifname)));
4274
if (!setup_script || !strcmp(setup_script, "no"))
4276
if (setup_script[0] != '\0') {
4277
if (launch_script(setup_script, ifname, fd))
4280
s = net_tap_fd_init(vlan, fd);
4283
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4284
"tap: ifname=%s setup_script=%s", ifname, setup_script);
4285
if (down_script && strcmp(down_script, "no"))
4286
snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
4290
#endif /* !_WIN32 */
4292
/* network connection */
4293
typedef struct NetSocketState {
4294
VLANClientState *vc;
4296
int state; /* 0 = getting length, 1 = getting data */
4300
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4303
typedef struct NetSocketListenState {
4306
} NetSocketListenState;
4308
/* XXX: we consider we can send the whole packet without blocking */
4309
static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
4311
NetSocketState *s = opaque;
4315
send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4316
send_all(s->fd, buf, size);
4319
static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4321
NetSocketState *s = opaque;
4322
sendto(s->fd, buf, size, 0,
4323
(struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4326
static void net_socket_send(void *opaque)
4328
NetSocketState *s = opaque;
4333
size = recv(s->fd, buf1, sizeof(buf1), 0);
4335
err = socket_error();
4336
if (err != EWOULDBLOCK)
4338
} else if (size == 0) {
4339
/* end of connection */
4341
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4347
/* reassemble a packet from the network */
4353
memcpy(s->buf + s->index, buf, l);
4357
if (s->index == 4) {
4359
s->packet_len = ntohl(*(uint32_t *)s->buf);
4365
l = s->packet_len - s->index;
4368
memcpy(s->buf + s->index, buf, l);
4372
if (s->index >= s->packet_len) {
4373
qemu_send_packet(s->vc, s->buf, s->packet_len);
4382
static void net_socket_send_dgram(void *opaque)
4384
NetSocketState *s = opaque;
4387
size = recv(s->fd, s->buf, sizeof(s->buf), 0);
4391
/* end of connection */
4392
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4395
qemu_send_packet(s->vc, s->buf, size);
4398
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4403
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4404
fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4405
inet_ntoa(mcastaddr->sin_addr),
4406
(int)ntohl(mcastaddr->sin_addr.s_addr));
4410
fd = socket(PF_INET, SOCK_DGRAM, 0);
4412
perror("socket(PF_INET, SOCK_DGRAM)");
4417
ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
4418
(const char *)&val, sizeof(val));
4420
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4424
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4430
/* Add host to multicast group */
4431
imr.imr_multiaddr = mcastaddr->sin_addr;
4432
imr.imr_interface.s_addr = htonl(INADDR_ANY);
4434
ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
4435
(const char *)&imr, sizeof(struct ip_mreq));
4437
perror("setsockopt(IP_ADD_MEMBERSHIP)");
4441
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
4443
ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
4444
(const char *)&val, sizeof(val));
4446
perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4450
socket_set_nonblock(fd);
4458
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
4461
struct sockaddr_in saddr;
4463
socklen_t saddr_len;
4466
/* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4467
* Because this may be "shared" socket from a "master" process, datagrams would be recv()
4468
* by ONLY ONE process: we must "clone" this dgram socket --jjo
4472
if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4474
if (saddr.sin_addr.s_addr==0) {
4475
fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4479
/* clone dgram socket */
4480
newfd = net_socket_mcast_create(&saddr);
4482
/* error already reported by net_socket_mcast_create() */
4486
/* clone newfd to fd, close newfd */
4491
fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4492
fd, strerror(errno));
4497
s = qemu_mallocz(sizeof(NetSocketState));
4502
s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
4503
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4505
/* mcast: save bound address as dst */
4506
if (is_connected) s->dgram_dst=saddr;
4508
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4509
"socket: fd=%d (%s mcast=%s:%d)",
4510
fd, is_connected? "cloned" : "",
4511
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4515
static void net_socket_connect(void *opaque)
4517
NetSocketState *s = opaque;
4518
qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4521
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
4525
s = qemu_mallocz(sizeof(NetSocketState));
4529
s->vc = qemu_new_vlan_client(vlan,
4530
net_socket_receive, NULL, s);
4531
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4532
"socket: fd=%d", fd);
4534
net_socket_connect(s);
4536
qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4541
static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
4544
int so_type=-1, optlen=sizeof(so_type);
4546
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
4547
(socklen_t *)&optlen)< 0) {
4548
fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
4553
return net_socket_fd_init_dgram(vlan, fd, is_connected);
4555
return net_socket_fd_init_stream(vlan, fd, is_connected);
4557
/* who knows ... this could be a eg. a pty, do warn and continue as stream */
4558
fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4559
return net_socket_fd_init_stream(vlan, fd, is_connected);
4564
static void net_socket_accept(void *opaque)
4566
NetSocketListenState *s = opaque;
4568
struct sockaddr_in saddr;
4573
len = sizeof(saddr);
4574
fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4575
if (fd < 0 && errno != EINTR) {
4577
} else if (fd >= 0) {
4581
s1 = net_socket_fd_init(s->vlan, fd, 1);
4585
snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
4586
"socket: connection from %s:%d",
4587
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4591
static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4593
NetSocketListenState *s;
4595
struct sockaddr_in saddr;
4597
if (parse_host_port(&saddr, host_str) < 0)
4600
s = qemu_mallocz(sizeof(NetSocketListenState));
4604
fd = socket(PF_INET, SOCK_STREAM, 0);
4609
socket_set_nonblock(fd);
4611
/* allow fast reuse */
4613
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
4615
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4620
ret = listen(fd, 0);
4627
qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4631
static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4634
int fd, connected, ret, err;
4635
struct sockaddr_in saddr;
4637
if (parse_host_port(&saddr, host_str) < 0)
4640
fd = socket(PF_INET, SOCK_STREAM, 0);
4645
socket_set_nonblock(fd);
4649
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4651
err = socket_error();
4652
if (err == EINTR || err == EWOULDBLOCK) {
4653
} else if (err == EINPROGRESS) {
4656
} else if (err == WSAEALREADY) {
4669
s = net_socket_fd_init(vlan, fd, connected);
4672
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4673
"socket: connect to %s:%d",
4674
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4678
static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4682
struct sockaddr_in saddr;
4684
if (parse_host_port(&saddr, host_str) < 0)
4688
fd = net_socket_mcast_create(&saddr);
4692
s = net_socket_fd_init(vlan, fd, 0);
4696
s->dgram_dst = saddr;
4698
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4699
"socket: mcast=%s:%d",
4700
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4705
static const char *get_opt_name(char *buf, int buf_size, const char *p)
4710
while (*p != '\0' && *p != '=') {
4711
if (q && (q - buf) < buf_size - 1)
4721
static const char *get_opt_value(char *buf, int buf_size, const char *p)
4726
while (*p != '\0') {
4728
if (*(p + 1) != ',')
4732
if (q && (q - buf) < buf_size - 1)
4742
static int get_param_value(char *buf, int buf_size,
4743
const char *tag, const char *str)
4750
p = get_opt_name(option, sizeof(option), p);
4754
if (!strcmp(tag, option)) {
4755
(void)get_opt_value(buf, buf_size, p);
4758
p = get_opt_value(NULL, 0, p);
4767
static int check_params(char *buf, int buf_size,
4768
char **params, const char *str)
4775
p = get_opt_name(buf, buf_size, p);
4779
for(i = 0; params[i] != NULL; i++)
4780
if (!strcmp(params[i], buf))
4782
if (params[i] == NULL)
4784
p = get_opt_value(NULL, 0, p);
4793
static int net_client_init(const char *str)
4804
while (*p != '\0' && *p != ',') {
4805
if ((q - device) < sizeof(device) - 1)
4813
if (get_param_value(buf, sizeof(buf), "vlan", p)) {
4814
vlan_id = strtol(buf, NULL, 0);
4816
vlan = qemu_find_vlan(vlan_id);
4818
fprintf(stderr, "Could not create vlan %d\n", vlan_id);
4821
if (!strcmp(device, "nic")) {
4825
if (nb_nics >= MAX_NICS) {
4826
fprintf(stderr, "Too Many NICs\n");
4829
nd = &nd_table[nb_nics];
4830
macaddr = nd->macaddr;
4836
macaddr[5] = 0x56 + nb_nics;
4838
if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
4839
if (parse_macaddr(macaddr, buf) < 0) {
4840
fprintf(stderr, "invalid syntax for ethernet address\n");
4844
if (get_param_value(buf, sizeof(buf), "model", p)) {
4845
nd->model = strdup(buf);
4849
vlan->nb_guest_devs++;
4852
if (!strcmp(device, "none")) {
4853
/* does nothing. It is needed to signal that no network cards
4858
if (!strcmp(device, "user")) {
4859
if (get_param_value(buf, sizeof(buf), "hostname", p)) {
4860
pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
4862
vlan->nb_host_devs++;
4863
ret = net_slirp_init(vlan);
4867
if (!strcmp(device, "tap")) {
4869
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4870
fprintf(stderr, "tap: no interface name\n");
4873
vlan->nb_host_devs++;
4874
ret = tap_win32_init(vlan, ifname);
4877
if (!strcmp(device, "tap")) {
4879
char setup_script[1024], down_script[1024];
4881
vlan->nb_host_devs++;
4882
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4883
fd = strtol(buf, NULL, 0);
4884
fcntl(fd, F_SETFL, O_NONBLOCK);
4886
if (net_tap_fd_init(vlan, fd))
4889
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
4892
if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
4893
pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
4895
if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
4896
pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
4898
ret = net_tap_init(vlan, ifname, setup_script, down_script);
4902
if (!strcmp(device, "socket")) {
4903
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
4905
fd = strtol(buf, NULL, 0);
4907
if (net_socket_fd_init(vlan, fd, 1))
4909
} else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
4910
ret = net_socket_listen_init(vlan, buf);
4911
} else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
4912
ret = net_socket_connect_init(vlan, buf);
4913
} else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
4914
ret = net_socket_mcast_init(vlan, buf);
4916
fprintf(stderr, "Unknown socket options: %s\n", p);
4919
vlan->nb_host_devs++;
4922
fprintf(stderr, "Unknown network device: %s\n", device);
4926
fprintf(stderr, "Could not initialize device '%s'\n", device);
4932
void do_info_network(void)
4935
VLANClientState *vc;
4937
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
4938
term_printf("VLAN %d devices:\n", vlan->id);
4939
for(vc = vlan->first_client; vc != NULL; vc = vc->next)
4940
term_printf(" %s\n", vc->info_str);
1621
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1625
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1630
static struct HCIInfo null_hci = {
1631
.cmd_send = null_hci_send,
1632
.sco_send = null_hci_send,
1633
.acl_send = null_hci_send,
1634
.bdaddr_set = null_hci_addr_set,
1637
struct HCIInfo *qemu_next_hci(void)
1639
if (cur_hci == nb_hcis)
1642
return hci_table[cur_hci++];
1645
static struct HCIInfo *hci_init(const char *str)
1648
struct bt_scatternet_s *vlan = 0;
1650
if (!strcmp(str, "null"))
1653
else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1655
return bt_host_hci(str[4] ? str + 5 : "hci0");
1656
else if (!strncmp(str, "hci", 3)) {
1659
if (!strncmp(str + 3, ",vlan=", 6)) {
1660
vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1665
vlan = qemu_find_bt_vlan(0);
1667
return bt_new_hci(vlan);
1670
fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1675
static int bt_hci_parse(const char *str)
1677
struct HCIInfo *hci;
1680
if (nb_hcis >= MAX_NICS) {
1681
fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1685
hci = hci_init(str);
1694
bdaddr.b[5] = 0x56 + nb_hcis;
1695
hci->bdaddr_set(hci, bdaddr.b);
1697
hci_table[nb_hcis++] = hci;
1702
static void bt_vhci_add(int vlan_id)
1704
struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1707
fprintf(stderr, "qemu: warning: adding a VHCI to "
1708
"an empty scatternet %i\n", vlan_id);
1710
bt_vhci_init(bt_new_hci(vlan));
1713
static struct bt_device_s *bt_device_add(const char *opt)
1715
struct bt_scatternet_s *vlan;
1717
char *endp = strstr(opt, ",vlan=");
1718
int len = (endp ? endp - opt : strlen(opt)) + 1;
1721
pstrcpy(devname, MIN(sizeof(devname), len), opt);
1724
vlan_id = strtol(endp + 6, &endp, 0);
1726
fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1731
vlan = qemu_find_bt_vlan(vlan_id);
1734
fprintf(stderr, "qemu: warning: adding a slave device to "
1735
"an empty scatternet %i\n", vlan_id);
1737
if (!strcmp(devname, "keyboard"))
1738
return bt_keyboard_init(vlan);
1740
fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1744
static int bt_parse(const char *opt)
1746
const char *endp, *p;
1749
if (strstart(opt, "hci", &endp)) {
1750
if (!*endp || *endp == ',') {
1752
if (!strstart(endp, ",vlan=", 0))
1755
return bt_hci_parse(opt);
1757
} else if (strstart(opt, "vhci", &endp)) {
1758
if (!*endp || *endp == ',') {
1760
if (strstart(endp, ",vlan=", &p)) {
1761
vlan = strtol(p, (char **) &endp, 0);
1763
fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1767
fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1776
} else if (strstart(opt, "device:", &endp))
1777
return !bt_device_add(endp);
1779
fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1783
/***********************************************************/
1784
/* QEMU Block devices */
4944
1786
#define HD_ALIAS "index=%d,media=disk"
4946
#define CDROM_ALIAS "index=1,media=cdrom"
4948
1787
#define CDROM_ALIAS "index=2,media=cdrom"
4950
1788
#define FD_ALIAS "index=%d,if=floppy"
4951
1789
#define PFLASH_ALIAS "if=pflash"
4952
1790
#define MTD_ALIAS "if=mtd"
4953
1791
#define SD_ALIAS "index=0,if=sd"
4955
static int drive_add(const char *file, const char *fmt, ...)
1793
QemuOpts *drive_add(const char *file, const char *fmt, ...)
4959
if (nb_drives_opt >= MAX_DRIVES) {
4960
fprintf(stderr, "qemu: too many drives\n");
4964
drives_opt[nb_drives_opt].file = file;
4965
1799
va_start(ap, fmt);
4966
vsnprintf(drives_opt[nb_drives_opt].opt,
4967
sizeof(drives_opt[0].opt), fmt, ap);
1800
vsnprintf(optstr, sizeof(optstr), fmt, ap);
4970
return nb_drives_opt++;
1803
opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
1805
fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1806
__FUNCTION__, optstr);
1810
qemu_opt_set(opts, "file", file);
4973
int drive_get_index(BlockInterfaceType type, int bus, int unit)
1814
DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
4977
1818
/* seek interface, bus and unit */
4979
for (index = 0; index < nb_drives; index++)
4980
if (drives_table[index].type == type &&
4981
drives_table[index].bus == bus &&
4982
drives_table[index].unit == unit)
1820
TAILQ_FOREACH(dinfo, &drives, next) {
1821
if (dinfo->type == type &&
1822
dinfo->bus == bus &&
1823
dinfo->unit == unit)
1830
DriveInfo *drive_get_by_id(const char *id)
1834
TAILQ_FOREACH(dinfo, &drives, next) {
1835
if (strcmp(id, dinfo->id))
4988
1842
int drive_get_max_bus(BlockInterfaceType type)
4994
for (index = 0; index < nb_drives; index++) {
4995
if(drives_table[index].type == type &&
4996
drives_table[index].bus > max_bus)
4997
max_bus = drives_table[index].bus;
1848
TAILQ_FOREACH(dinfo, &drives, next) {
1849
if(dinfo->type == type &&
1850
dinfo->bus > max_bus)
1851
max_bus = dinfo->bus;
4999
1853
return max_bus;
1856
const char *drive_get_serial(BlockDriverState *bdrv)
1860
TAILQ_FOREACH(dinfo, &drives, next) {
1861
if (dinfo->bdrv == bdrv)
1862
return dinfo->serial;
1868
BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
1872
TAILQ_FOREACH(dinfo, &drives, next) {
1873
if (dinfo->bdrv == bdrv)
1874
return dinfo->onerror;
1877
return BLOCK_ERR_STOP_ENOSPC;
5002
1880
static void bdrv_format_print(void *opaque, const char *name)
5004
1882
fprintf(stderr, " %s", name);
5007
static int drive_init(struct drive_opt *arg, int snapshot,
5008
QEMUMachine *machine)
1885
void drive_uninit(BlockDriverState *bdrv)
1889
TAILQ_FOREACH(dinfo, &drives, next) {
1890
if (dinfo->bdrv != bdrv)
1892
qemu_opts_del(dinfo->opts);
1893
TAILQ_REMOVE(&drives, dinfo, next);
1899
DriveInfo *drive_init(QemuOpts *opts, void *opaque,
1903
const char *file = NULL;
5012
1904
char devname[128];
5013
1906
const char *mediastr = "";
5014
1907
BlockInterfaceType type;
5015
1908
enum { MEDIA_DISK, MEDIA_CDROM } media;
5016
1909
int bus_id, unit_id;
5017
1910
int cyls, heads, secs, translation;
5018
BlockDriverState *bdrv;
5019
1911
BlockDriver *drv = NULL;
1912
QEMUMachine *machine = opaque;
5024
char *str = arg->opt;
5025
char *params[] = { "bus", "unit", "if", "index", "cyls", "heads",
5026
"secs", "trans", "media", "snapshot", "file",
5027
"cache", "format", NULL };
5029
if (check_params(buf, sizeof(buf), params, str) < 0) {
5030
fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
5036
cyls = heads = secs = 0;
1917
int bdrv_flags, onerror;
1918
const char *devaddr;
5039
1924
translation = BIOS_ATA_TRANSLATION_AUTO;
5043
if (!strcmp(machine->name, "realview") ||
5044
!strcmp(machine->name, "SS-5") ||
5045
!strcmp(machine->name, "SS-10") ||
5046
!strcmp(machine->name, "SS-600MP") ||
5047
!strcmp(machine->name, "versatilepb") ||
5048
!strcmp(machine->name, "versatileab")) {
1927
if (machine->use_scsi) {
5049
1928
type = IF_SCSI;
5050
1929
max_devs = MAX_SCSI_DEVS;
5051
strcpy(devname, "scsi");
1930
pstrcpy(devname, sizeof(devname), "scsi");
5054
1933
max_devs = MAX_IDE_DEVS;
5055
strcpy(devname, "ide");
1934
pstrcpy(devname, sizeof(devname), "ide");
5057
1936
media = MEDIA_DISK;
5059
1938
/* extract parameters */
5061
if (get_param_value(buf, sizeof(buf), "bus", str)) {
5062
bus_id = strtol(buf, NULL, 0);
5064
fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
5069
if (get_param_value(buf, sizeof(buf), "unit", str)) {
5070
unit_id = strtol(buf, NULL, 0);
5072
fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
5077
if (get_param_value(buf, sizeof(buf), "if", str)) {
5078
strncpy(devname, buf, sizeof(devname));
1939
bus_id = qemu_opt_get_number(opts, "bus", 0);
1940
unit_id = qemu_opt_get_number(opts, "unit", -1);
1941
index = qemu_opt_get_number(opts, "index", -1);
1943
cyls = qemu_opt_get_number(opts, "cyls", 0);
1944
heads = qemu_opt_get_number(opts, "heads", 0);
1945
secs = qemu_opt_get_number(opts, "secs", 0);
1947
snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
1949
file = qemu_opt_get(opts, "file");
1950
serial = qemu_opt_get(opts, "serial");
1952
if ((buf = qemu_opt_get(opts, "if")) != NULL) {
1953
pstrcpy(devname, sizeof(devname), buf);
5079
1954
if (!strcmp(buf, "ide")) {
5081
1956
max_devs = MAX_IDE_DEVS;