1 /* Copyright (C) 2007-2008 The Android Open Source Project
2 **
3 ** This software is licensed under the terms of the GNU General Public
4 ** License version 2, as published by the Free Software Foundation, and
5 ** may be copied, distributed, and modified under those terms.
6 **
7 ** This program is distributed in the hope that it will be useful,
8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 ** GNU General Public License for more details.
11 */
12 #include "android/hw-kmsg.h"
13 #include "qemu-char.h"
14 #include "charpipe.h"
15 #include "android/utils/debug.h"
16
17 static CharDriverState* android_kmsg_cs;
18
19 typedef struct {
20 CharDriverState* cs;
21 AndroidKmsgFlags flags;
22 } KernelLog;
23
24 static int
kernel_log_can_read(void * opaque)25 kernel_log_can_read( void* opaque )
26 {
27 return 1024;
28 }
29
30 static void
kernel_log_read(void * opaque,const uint8_t * from,int len)31 kernel_log_read( void* opaque, const uint8_t* from, int len )
32 {
33 KernelLog* k = opaque;
34
35 if (k->flags & ANDROID_KMSG_PRINT_MESSAGES)
36 printf( "%.*s", len, (const char*)from );
37
38 /* XXXX: TODO: save messages into in-memory buffer for later retrieval */
39 }
40
41 static void
kernel_log_init(KernelLog * k,AndroidKmsgFlags flags)42 kernel_log_init( KernelLog* k, AndroidKmsgFlags flags )
43 {
44 if ( qemu_chr_open_charpipe( &k->cs, &android_kmsg_cs ) < 0 ) {
45 derror( "could not create kernel log charpipe" );
46 exit(1);
47 }
48
49 qemu_chr_add_handlers( k->cs, kernel_log_can_read, kernel_log_read, NULL, k );
50
51 k->flags = flags;
52 }
53
54 static KernelLog _kernel_log[1];
55
56 void
android_kmsg_init(AndroidKmsgFlags flags)57 android_kmsg_init( AndroidKmsgFlags flags )
58 {
59 if (_kernel_log->cs == NULL)
60 kernel_log_init( _kernel_log, flags );
61 }
62
63
android_kmsg_get_cs(void)64 CharDriverState* android_kmsg_get_cs( void )
65 {
66 if (android_kmsg_cs == NULL) {
67 android_kmsg_init(0);
68 }
69 return android_kmsg_cs;
70 }
71