2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* this file contains various functions used by all libhardware modules
18 * that support QEMU emulation
21 #define LOG_TAG "hardware-qemu"
22 #include <cutils/log.h>
23 #include <cutils/properties.h>
24 #include <cutils/sockets.h>
34 # define D(...) ALOGD(__VA_ARGS__)
36 # define D(...) ((void)0)
39 #include "hardware/qemu_pipe.h"
44 static int in_qemu = -1;
46 if (__builtin_expect(in_qemu < 0,0)) {
47 char propBuf[PROPERTY_VALUE_MAX];
48 property_get("ro.kernel.qemu", propBuf, "");
49 in_qemu = (propBuf[0] == '1');
55 qemu_fd_write( int fd, const char* cmd, int len )
59 len2 = write(fd, cmd, len);
60 } while (len2 < 0 && errno == EINTR);
65 qemu_fd_read( int fd, char* buff, int len )
69 len2 = read(fd, buff, len);
70 } while (len2 < 0 && errno == EINTR);
75 qemu_channel_open_qemud_pipe( QemuChannel* channel,
81 snprintf(pipe_name, sizeof(pipe_name), "qemud:%s", name);
82 fd = qemu_pipe_open(pipe_name);
84 D("no qemud pipe: %s", strerror(errno));
88 channel->is_qemud = 1;
94 qemu_channel_open_qemud( QemuChannel* channel,
97 int fd, ret, namelen = strlen(name);
100 fd = socket_local_client( "qemud",
101 ANDROID_SOCKET_NAMESPACE_RESERVED,
104 D("no qemud control socket: %s", strerror(errno));
108 /* send service name to connect */
109 if (qemu_fd_write(fd, name, namelen) != namelen) {
110 D("can't send service name to qemud: %s",
116 /* read answer from daemon */
117 if (qemu_fd_read(fd, answer, 2) != 2 ||
118 answer[0] != 'O' || answer[1] != 'K') {
119 D("cant' connect to %s service through qemud", name);
124 channel->is_qemud = 1;
131 qemu_channel_open_qemud_old( QemuChannel* channel,
136 snprintf(channel->device, sizeof channel->device,
139 fd = socket_local_client( channel->device,
140 ANDROID_SOCKET_NAMESPACE_RESERVED,
143 D("no '%s' control socket available: %s",
144 channel->device, strerror(errno));
149 channel->is_qemud_old = 1;
155 qemu_channel_open_tty( QemuChannel* channel,
159 char key[PROPERTY_KEY_MAX];
160 char prop[PROPERTY_VALUE_MAX];
163 ret = snprintf(key, sizeof key, "ro.kernel.android.%s", name);
164 if (ret >= (int)sizeof key)
167 if (property_get(key, prop, "") == 0) {
168 D("no kernel-provided %s device name", name);
172 ret = snprintf(channel->device, sizeof channel->device,
174 if (ret >= (int)sizeof channel->device) {
175 D("%s device name too long: '%s'", name, prop);
179 channel->is_tty = !memcmp("/dev/tty", channel->device, 8);
184 qemu_channel_open( QemuChannel* channel,
190 /* initialize the channel is needed */
191 if (!channel->is_inited)
193 channel->is_inited = 1;
196 if (qemu_channel_open_qemud_pipe(channel, name) == 0)
199 if (qemu_channel_open_qemud(channel, name) == 0)
202 if (qemu_channel_open_qemud_old(channel, name) == 0)
205 if (qemu_channel_open_tty(channel, name, mode) == 0)
208 channel->is_available = 0;
212 channel->is_available = 1;
215 /* try to open the file */
216 if (!channel->is_available) {
221 if (channel->is_qemud) {
222 return dup(channel->fd);
225 if (channel->is_qemud_old) {
227 fd = socket_local_client( channel->device,
228 ANDROID_SOCKET_NAMESPACE_RESERVED,
230 } while (fd < 0 && errno == EINTR);
232 else /* /dev/ttySn ? */
235 fd = open(channel->device, mode);
236 } while (fd < 0 && errno == EINTR);
238 /* disable ECHO on serial lines */
239 if (fd >= 0 && channel->is_tty) {
241 tcgetattr( fd, &ios );
242 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
243 tcsetattr( fd, TCSANOW, &ios );
251 qemu_command_vformat( char* buffer,
262 len = vsnprintf(buffer+4, buffer_size-4, format, args);
263 if (len >= buffer_size-4)
266 snprintf(header, sizeof header, "%04x", len);
267 memcpy(buffer, header, 4);
272 qemu_command_format( char* buffer,
280 va_start(args, format);
281 ret = qemu_command_format(buffer, buffer_size, format, args);
288 qemu_control_fd(void)
290 static QemuChannel channel[1];
293 fd = qemu_channel_open( channel, "hw-control", O_RDWR );
295 D("%s: could not open control channel: %s", __FUNCTION__,
302 qemu_control_send(const char* cmd, int len)
311 fd = qemu_control_fd();
315 len2 = qemu_fd_write(fd, cmd, len);
318 D("%s: could not send everything %d < %d",
319 __FUNCTION__, len2, len);
327 qemu_control_command( const char* fmt, ... )
334 len = qemu_command_vformat( command, sizeof command, fmt, args );
337 if (len < 0 || len >= (int)sizeof command) {
339 D("%s: could not send: %s", __FUNCTION__, strerror(errno));
341 D("%s: too large %d > %d", __FUNCTION__, len, (int)(sizeof command));
347 return qemu_control_send( command, len );
350 extern int qemu_control_query( const char* question, int questionlen,
351 char* answer, int answersize )
353 int ret, fd, len, result = -1;
354 char header[5], *end;
356 if (questionlen <= 0) {
361 fd = qemu_control_fd();
365 ret = qemu_fd_write( fd, question, questionlen );
366 if (ret != questionlen) {
367 D("%s: could not write all: %d < %d", __FUNCTION__,
372 /* read a 4-byte header giving the length of the following content */
373 ret = qemu_fd_read( fd, header, 4 );
375 D("%s: could not read header (%d != 4)",
381 len = strtol( header, &end, 16 );
382 if ( len < 0 || end == NULL || end != header+4 || len > answersize ) {
383 D("%s: could not parse header: '%s'",
384 __FUNCTION__, header);
388 /* read the answer */
389 ret = qemu_fd_read( fd, answer, len );
391 D("%s: could not read all of answer %d < %d",
392 __FUNCTION__, ret, len);