2 * QEMU Random Number Generator Backend
4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "sysemu/rng.h"
15 #include "sysemu/char.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "hw/qdev.h" /* just for DEFINE_PROP_CHR */
20 #define TYPE_RNG_EGD "rng-egd"
21 #define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD)
31 static void rng_egd_request_entropy(RngBackend
*b
, RngRequest
*req
)
33 RngEgd
*s
= RNG_EGD(b
);
34 size_t size
= req
->size
;
38 uint8_t len
= MIN(size
, 255);
40 /* synchronous entropy request */
44 /* XXX this blocks entire thread. Rewrite to use
45 * qemu_chr_fe_write and background I/O callbacks */
46 qemu_chr_fe_write_all(s
->chr
, header
, sizeof(header
));
52 static int rng_egd_chr_can_read(void *opaque
)
54 RngEgd
*s
= RNG_EGD(opaque
);
58 QSIMPLEQ_FOREACH(req
, &s
->parent
.requests
, next
) {
59 size
+= req
->size
- req
->offset
;
65 static void rng_egd_chr_read(void *opaque
, const uint8_t *buf
, int size
)
67 RngEgd
*s
= RNG_EGD(opaque
);
68 size_t buf_offset
= 0;
70 while (size
> 0 && !QSIMPLEQ_EMPTY(&s
->parent
.requests
)) {
71 RngRequest
*req
= QSIMPLEQ_FIRST(&s
->parent
.requests
);
72 int len
= MIN(size
, req
->size
- req
->offset
);
74 memcpy(req
->data
+ req
->offset
, buf
+ buf_offset
, len
);
79 if (req
->offset
== req
->size
) {
80 req
->receive_entropy(req
->opaque
, req
->data
, req
->size
);
82 rng_backend_finalize_request(&s
->parent
, req
);
87 static void rng_egd_opened(RngBackend
*b
, Error
**errp
)
89 RngEgd
*s
= RNG_EGD(b
);
91 if (s
->chr_name
== NULL
) {
92 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
93 "chardev", "a valid character device");
97 s
->chr
= qemu_chr_find(s
->chr_name
);
99 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
100 "Device '%s' not found", s
->chr_name
);
104 if (qemu_chr_fe_claim(s
->chr
) != 0) {
105 error_setg(errp
, QERR_DEVICE_IN_USE
, s
->chr_name
);
109 /* FIXME we should resubmit pending requests when the CDS reconnects. */
110 qemu_chr_add_handlers(s
->chr
, rng_egd_chr_can_read
, rng_egd_chr_read
,
114 static void rng_egd_set_chardev(Object
*obj
, const char *value
, Error
**errp
)
116 RngBackend
*b
= RNG_BACKEND(obj
);
117 RngEgd
*s
= RNG_EGD(b
);
120 error_setg(errp
, QERR_PERMISSION_DENIED
);
123 s
->chr_name
= g_strdup(value
);
127 static char *rng_egd_get_chardev(Object
*obj
, Error
**errp
)
129 RngEgd
*s
= RNG_EGD(obj
);
131 if (s
->chr
&& s
->chr
->label
) {
132 return g_strdup(s
->chr
->label
);
138 static void rng_egd_init(Object
*obj
)
140 object_property_add_str(obj
, "chardev",
141 rng_egd_get_chardev
, rng_egd_set_chardev
,
145 static void rng_egd_finalize(Object
*obj
)
147 RngEgd
*s
= RNG_EGD(obj
);
150 qemu_chr_add_handlers(s
->chr
, NULL
, NULL
, NULL
, NULL
);
151 qemu_chr_fe_release(s
->chr
);
157 static void rng_egd_class_init(ObjectClass
*klass
, void *data
)
159 RngBackendClass
*rbc
= RNG_BACKEND_CLASS(klass
);
161 rbc
->request_entropy
= rng_egd_request_entropy
;
162 rbc
->opened
= rng_egd_opened
;
165 static const TypeInfo rng_egd_info
= {
166 .name
= TYPE_RNG_EGD
,
167 .parent
= TYPE_RNG_BACKEND
,
168 .instance_size
= sizeof(RngEgd
),
169 .class_init
= rng_egd_class_init
,
170 .instance_init
= rng_egd_init
,
171 .instance_finalize
= rng_egd_finalize
,
174 static void register_types(void)
176 type_register_static(&rng_egd_info
);
179 type_init(register_types
);