xref: /openbmc/qemu/backends/rng.c (revision 6683d7bc)
1 /*
2  * QEMU Random Number Generator Backend
3  *
4  * Copyright IBM, Corp. 2012
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
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.
11  */
12 
13 #include "sysemu/rng.h"
14 #include "qapi/qmp/qerror.h"
15 
16 void rng_backend_request_entropy(RngBackend *s, size_t size,
17                                  EntropyReceiveFunc *receive_entropy,
18                                  void *opaque)
19 {
20     RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
21 
22     if (k->request_entropy) {
23         k->request_entropy(s, size, receive_entropy, opaque);
24     }
25 }
26 
27 void rng_backend_cancel_requests(RngBackend *s)
28 {
29     RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
30 
31     if (k->cancel_requests) {
32         k->cancel_requests(s);
33     }
34 }
35 
36 static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
37 {
38     RngBackend *s = RNG_BACKEND(obj);
39 
40     return s->opened;
41 }
42 
43 void rng_backend_open(RngBackend *s, Error **errp)
44 {
45     object_property_set_bool(OBJECT(s), true, "opened", errp);
46 }
47 
48 static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
49 {
50     RngBackend *s = RNG_BACKEND(obj);
51     RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
52 
53     if (value == s->opened) {
54         return;
55     }
56 
57     if (!value && s->opened) {
58         error_set(errp, QERR_PERMISSION_DENIED);
59         return;
60     }
61 
62     if (k->opened) {
63         k->opened(s, errp);
64     }
65 
66     if (!error_is_set(errp)) {
67         s->opened = value;
68     }
69 }
70 
71 static void rng_backend_init(Object *obj)
72 {
73     object_property_add_bool(obj, "opened",
74                              rng_backend_prop_get_opened,
75                              rng_backend_prop_set_opened,
76                              NULL);
77 }
78 
79 static const TypeInfo rng_backend_info = {
80     .name = TYPE_RNG_BACKEND,
81     .parent = TYPE_OBJECT,
82     .instance_size = sizeof(RngBackend),
83     .instance_init = rng_backend_init,
84     .class_size = sizeof(RngBackendClass),
85     .abstract = true,
86 };
87 
88 static void register_types(void)
89 {
90     type_register_static(&rng_backend_info);
91 }
92 
93 type_init(register_types);
94