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 "sysemu/char.h"
14#include "android/charpipe.h"
15#include "android/utils/debug.h"
16
17static CharDriverState*  android_kmsg_cs;
18
19typedef struct {
20    CharDriverState*  cs;
21    AndroidKmsgFlags  flags;
22} KernelLog;
23
24static int
25kernel_log_can_read( void*  opaque )
26{
27    return 1024;
28}
29
30static void
31kernel_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
41static void
42kernel_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
54static KernelLog  _kernel_log[1];
55
56void
57android_kmsg_init( AndroidKmsgFlags  flags )
58{
59    if (_kernel_log->cs == NULL)
60        kernel_log_init( _kernel_log, flags );
61}
62
63
64CharDriverState*  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