1 /* 2 * QEMU System Emulator 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 #include "qemu/osdep.h" 25 #include "chardev/char.h" 26 #include "qmp-commands.h" 27 #include "qemu/base64.h" 28 29 /* Ring buffer chardev */ 30 31 typedef struct { 32 Chardev parent; 33 size_t size; 34 size_t prod; 35 size_t cons; 36 uint8_t *cbuf; 37 } RingBufChardev; 38 39 #define RINGBUF_CHARDEV(obj) \ 40 OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF) 41 42 static size_t ringbuf_count(const Chardev *chr) 43 { 44 const RingBufChardev *d = RINGBUF_CHARDEV(chr); 45 46 return d->prod - d->cons; 47 } 48 49 static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len) 50 { 51 RingBufChardev *d = RINGBUF_CHARDEV(chr); 52 int i; 53 54 if (!buf || (len < 0)) { 55 return -1; 56 } 57 58 for (i = 0; i < len; i++) { 59 d->cbuf[d->prod++ & (d->size - 1)] = buf[i]; 60 if (d->prod - d->cons > d->size) { 61 d->cons = d->prod - d->size; 62 } 63 } 64 65 return len; 66 } 67 68 static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len) 69 { 70 RingBufChardev *d = RINGBUF_CHARDEV(chr); 71 int i; 72 73 qemu_mutex_lock(&chr->chr_write_lock); 74 for (i = 0; i < len && d->cons != d->prod; i++) { 75 buf[i] = d->cbuf[d->cons++ & (d->size - 1)]; 76 } 77 qemu_mutex_unlock(&chr->chr_write_lock); 78 79 return i; 80 } 81 82 static void char_ringbuf_finalize(Object *obj) 83 { 84 RingBufChardev *d = RINGBUF_CHARDEV(obj); 85 86 g_free(d->cbuf); 87 } 88 89 static void qemu_chr_open_ringbuf(Chardev *chr, 90 ChardevBackend *backend, 91 bool *be_opened, 92 Error **errp) 93 { 94 ChardevRingbuf *opts = backend->u.ringbuf.data; 95 RingBufChardev *d = RINGBUF_CHARDEV(chr); 96 97 d->size = opts->has_size ? opts->size : 65536; 98 99 /* The size must be power of 2 */ 100 if (d->size & (d->size - 1)) { 101 error_setg(errp, "size of ringbuf chardev must be power of two"); 102 return; 103 } 104 105 d->prod = 0; 106 d->cons = 0; 107 d->cbuf = g_malloc0(d->size); 108 } 109 110 void qmp_ringbuf_write(const char *device, const char *data, 111 bool has_format, enum DataFormat format, 112 Error **errp) 113 { 114 Chardev *chr; 115 const uint8_t *write_data; 116 int ret; 117 gsize write_count; 118 119 chr = qemu_chr_find(device); 120 if (!chr) { 121 error_setg(errp, "Device '%s' not found", device); 122 return; 123 } 124 125 if (!CHARDEV_IS_RINGBUF(chr)) { 126 error_setg(errp, "%s is not a ringbuf device", device); 127 return; 128 } 129 130 if (has_format && (format == DATA_FORMAT_BASE64)) { 131 write_data = qbase64_decode(data, -1, 132 &write_count, 133 errp); 134 if (!write_data) { 135 return; 136 } 137 } else { 138 write_data = (uint8_t *)data; 139 write_count = strlen(data); 140 } 141 142 ret = ringbuf_chr_write(chr, write_data, write_count); 143 144 if (write_data != (uint8_t *)data) { 145 g_free((void *)write_data); 146 } 147 148 if (ret < 0) { 149 error_setg(errp, "Failed to write to device %s", device); 150 return; 151 } 152 } 153 154 char *qmp_ringbuf_read(const char *device, int64_t size, 155 bool has_format, enum DataFormat format, 156 Error **errp) 157 { 158 Chardev *chr; 159 uint8_t *read_data; 160 size_t count; 161 char *data; 162 163 chr = qemu_chr_find(device); 164 if (!chr) { 165 error_setg(errp, "Device '%s' not found", device); 166 return NULL; 167 } 168 169 if (!CHARDEV_IS_RINGBUF(chr)) { 170 error_setg(errp, "%s is not a ringbuf device", device); 171 return NULL; 172 } 173 174 if (size <= 0) { 175 error_setg(errp, "size must be greater than zero"); 176 return NULL; 177 } 178 179 count = ringbuf_count(chr); 180 size = size > count ? count : size; 181 read_data = g_malloc(size + 1); 182 183 ringbuf_chr_read(chr, read_data, size); 184 185 if (has_format && (format == DATA_FORMAT_BASE64)) { 186 data = g_base64_encode(read_data, size); 187 g_free(read_data); 188 } else { 189 /* 190 * FIXME should read only complete, valid UTF-8 characters up 191 * to @size bytes. Invalid sequences should be replaced by a 192 * suitable replacement character. Except when (and only 193 * when) ring buffer lost characters since last read, initial 194 * continuation characters should be dropped. 195 */ 196 read_data[size] = 0; 197 data = (char *)read_data; 198 } 199 200 return data; 201 } 202 203 static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend, 204 Error **errp) 205 { 206 int val; 207 ChardevRingbuf *ringbuf; 208 209 backend->type = CHARDEV_BACKEND_KIND_RINGBUF; 210 ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1); 211 qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf)); 212 213 val = qemu_opt_get_size(opts, "size", 0); 214 if (val != 0) { 215 ringbuf->has_size = true; 216 ringbuf->size = val; 217 } 218 } 219 220 static void char_ringbuf_class_init(ObjectClass *oc, void *data) 221 { 222 ChardevClass *cc = CHARDEV_CLASS(oc); 223 224 cc->parse = qemu_chr_parse_ringbuf; 225 cc->open = qemu_chr_open_ringbuf; 226 cc->chr_write = ringbuf_chr_write; 227 } 228 229 static const TypeInfo char_ringbuf_type_info = { 230 .name = TYPE_CHARDEV_RINGBUF, 231 .parent = TYPE_CHARDEV, 232 .class_init = char_ringbuf_class_init, 233 .instance_size = sizeof(RingBufChardev), 234 .instance_finalize = char_ringbuf_finalize, 235 }; 236 237 /* Bug-compatibility: */ 238 static const TypeInfo char_memory_type_info = { 239 .name = TYPE_CHARDEV_MEMORY, 240 .parent = TYPE_CHARDEV_RINGBUF, 241 }; 242 243 static void register_types(void) 244 { 245 type_register_static(&char_ringbuf_type_info); 246 type_register_static(&char_memory_type_info); 247 } 248 249 type_init(register_types); 250