xref: /openbmc/qemu/backends/rng-egd.c (revision b917da4c)
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 "sysemu/char.h"
16 #include "qapi/qmp/qerror.h"
17 #include "hw/qdev.h" /* just for DEFINE_PROP_CHR */
18 
19 #define TYPE_RNG_EGD "rng-egd"
20 #define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD)
21 
22 typedef struct RngEgd
23 {
24     RngBackend parent;
25 
26     CharDriverState *chr;
27     char *chr_name;
28 } RngEgd;
29 
30 static void rng_egd_request_entropy(RngBackend *b, RngRequest *req)
31 {
32     RngEgd *s = RNG_EGD(b);
33     size_t size = req->size;
34 
35     while (size > 0) {
36         uint8_t header[2];
37         uint8_t len = MIN(size, 255);
38 
39         /* synchronous entropy request */
40         header[0] = 0x02;
41         header[1] = len;
42 
43         qemu_chr_fe_write(s->chr, header, sizeof(header));
44 
45         size -= len;
46     }
47 }
48 
49 static int rng_egd_chr_can_read(void *opaque)
50 {
51     RngEgd *s = RNG_EGD(opaque);
52     RngRequest *req;
53     int size = 0;
54 
55     QSIMPLEQ_FOREACH(req, &s->parent.requests, next) {
56         size += req->size - req->offset;
57     }
58 
59     return size;
60 }
61 
62 static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)
63 {
64     RngEgd *s = RNG_EGD(opaque);
65     size_t buf_offset = 0;
66 
67     while (size > 0 && !QSIMPLEQ_EMPTY(&s->parent.requests)) {
68         RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
69         int len = MIN(size, req->size - req->offset);
70 
71         memcpy(req->data + req->offset, buf + buf_offset, len);
72         buf_offset += len;
73         req->offset += len;
74         size -= len;
75 
76         if (req->offset == req->size) {
77             req->receive_entropy(req->opaque, req->data, req->size);
78 
79             rng_backend_finalize_request(&s->parent, req);
80         }
81     }
82 }
83 
84 static void rng_egd_opened(RngBackend *b, Error **errp)
85 {
86     RngEgd *s = RNG_EGD(b);
87 
88     if (s->chr_name == NULL) {
89         error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
90                    "chardev", "a valid character device");
91         return;
92     }
93 
94     s->chr = qemu_chr_find(s->chr_name);
95     if (s->chr == NULL) {
96         error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
97                   "Device '%s' not found", s->chr_name);
98         return;
99     }
100 
101     if (qemu_chr_fe_claim(s->chr) != 0) {
102         error_setg(errp, QERR_DEVICE_IN_USE, s->chr_name);
103         return;
104     }
105 
106     /* FIXME we should resubmit pending requests when the CDS reconnects. */
107     qemu_chr_add_handlers(s->chr, rng_egd_chr_can_read, rng_egd_chr_read,
108                           NULL, s);
109 }
110 
111 static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
112 {
113     RngBackend *b = RNG_BACKEND(obj);
114     RngEgd *s = RNG_EGD(b);
115 
116     if (b->opened) {
117         error_setg(errp, QERR_PERMISSION_DENIED);
118     } else {
119         g_free(s->chr_name);
120         s->chr_name = g_strdup(value);
121     }
122 }
123 
124 static char *rng_egd_get_chardev(Object *obj, Error **errp)
125 {
126     RngEgd *s = RNG_EGD(obj);
127 
128     if (s->chr && s->chr->label) {
129         return g_strdup(s->chr->label);
130     }
131 
132     return NULL;
133 }
134 
135 static void rng_egd_init(Object *obj)
136 {
137     object_property_add_str(obj, "chardev",
138                             rng_egd_get_chardev, rng_egd_set_chardev,
139                             NULL);
140 }
141 
142 static void rng_egd_finalize(Object *obj)
143 {
144     RngEgd *s = RNG_EGD(obj);
145 
146     if (s->chr) {
147         qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
148         qemu_chr_fe_release(s->chr);
149     }
150 
151     g_free(s->chr_name);
152 }
153 
154 static void rng_egd_class_init(ObjectClass *klass, void *data)
155 {
156     RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
157 
158     rbc->request_entropy = rng_egd_request_entropy;
159     rbc->opened = rng_egd_opened;
160 }
161 
162 static const TypeInfo rng_egd_info = {
163     .name = TYPE_RNG_EGD,
164     .parent = TYPE_RNG_BACKEND,
165     .instance_size = sizeof(RngEgd),
166     .class_init = rng_egd_class_init,
167     .instance_init = rng_egd_init,
168     .instance_finalize = rng_egd_finalize,
169 };
170 
171 static void register_types(void)
172 {
173     type_register_static(&rng_egd_info);
174 }
175 
176 type_init(register_types);
177