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