xref: /openbmc/qemu/backends/rng.c (revision b2c623a3)
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 #include "qom/object_interfaces.h"
16 
17 void rng_backend_request_entropy(RngBackend *s, size_t size,
18                                  EntropyReceiveFunc *receive_entropy,
19                                  void *opaque)
20 {
21     RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
22 
23     if (k->request_entropy) {
24         k->request_entropy(s, size, receive_entropy, opaque);
25     }
26 }
27 
28 void rng_backend_cancel_requests(RngBackend *s)
29 {
30     RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
31 
32     if (k->cancel_requests) {
33         k->cancel_requests(s);
34     }
35 }
36 
37 static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
38 {
39     RngBackend *s = RNG_BACKEND(obj);
40 
41     return s->opened;
42 }
43 
44 static void rng_backend_complete(UserCreatable *uc, Error **errp)
45 {
46     object_property_set_bool(OBJECT(uc), true, "opened", errp);
47 }
48 
49 static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
50 {
51     RngBackend *s = RNG_BACKEND(obj);
52     RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
53 
54     if (value == s->opened) {
55         return;
56     }
57 
58     if (!value && s->opened) {
59         error_set(errp, QERR_PERMISSION_DENIED);
60         return;
61     }
62 
63     if (k->opened) {
64         k->opened(s, errp);
65     }
66 
67     if (!error_is_set(errp)) {
68         s->opened = value;
69     }
70 }
71 
72 static void rng_backend_init(Object *obj)
73 {
74     object_property_add_bool(obj, "opened",
75                              rng_backend_prop_get_opened,
76                              rng_backend_prop_set_opened,
77                              NULL);
78 }
79 
80 static void rng_backend_class_init(ObjectClass *oc, void *data)
81 {
82     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
83 
84     ucc->complete = rng_backend_complete;
85 }
86 
87 static const TypeInfo rng_backend_info = {
88     .name = TYPE_RNG_BACKEND,
89     .parent = TYPE_OBJECT,
90     .instance_size = sizeof(RngBackend),
91     .instance_init = rng_backend_init,
92     .class_size = sizeof(RngBackendClass),
93     .class_init = rng_backend_class_init,
94     .abstract = true,
95     .interfaces = (InterfaceInfo[]) {
96         { TYPE_USER_CREATABLE },
97         { }
98     }
99 };
100 
101 static void register_types(void)
102 {
103     type_register_static(&rng_backend_info);
104 }
105 
106 type_init(register_types);
107