char: cadence: check baud rate generator and divider values
[qemu/ar7.git] / block / raw_bsd.c
blob7c9bebb507b61a110611e888197c7df89c7e84c7
1 /* BlockDriver implementation for "raw"
3 * Copyright (C) 2010-2016 Red Hat, Inc.
4 * Copyright (C) 2010, Blue Swirl <blauwirbel@gmail.com>
5 * Copyright (C) 2009, Anthony Liguori <aliguori@us.ibm.com>
7 * Author:
8 * Laszlo Ersek <lersek@redhat.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to
12 * deal in the Software without restriction, including without limitation the
13 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 * sell copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 * IN THE SOFTWARE.
29 #include "qemu/osdep.h"
30 #include "block/block_int.h"
31 #include "qapi/error.h"
32 #include "qemu/option.h"
34 typedef struct BDRVRawState {
35 uint64_t offset;
36 uint64_t size;
37 bool has_size;
38 } BDRVRawState;
40 static QemuOptsList raw_runtime_opts = {
41 .name = "raw",
42 .head = QTAILQ_HEAD_INITIALIZER(raw_runtime_opts.head),
43 .desc = {
45 .name = "offset",
46 .type = QEMU_OPT_SIZE,
47 .help = "offset in the disk where the image starts",
50 .name = "size",
51 .type = QEMU_OPT_SIZE,
52 .help = "virtual disk size",
54 { /* end of list */ }
58 static QemuOptsList raw_create_opts = {
59 .name = "raw-create-opts",
60 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
61 .desc = {
63 .name = BLOCK_OPT_SIZE,
64 .type = QEMU_OPT_SIZE,
65 .help = "Virtual disk size"
67 { /* end of list */ }
71 static int raw_read_options(QDict *options, BlockDriverState *bs,
72 BDRVRawState *s, Error **errp)
74 Error *local_err = NULL;
75 QemuOpts *opts = NULL;
76 int64_t real_size = 0;
77 int ret;
79 real_size = bdrv_getlength(bs->file->bs);
80 if (real_size < 0) {
81 error_setg_errno(errp, -real_size, "Could not get image size");
82 return real_size;
85 opts = qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort);
86 qemu_opts_absorb_qdict(opts, options, &local_err);
87 if (local_err) {
88 error_propagate(errp, local_err);
89 ret = -EINVAL;
90 goto end;
93 s->offset = qemu_opt_get_size(opts, "offset", 0);
94 if (qemu_opt_find(opts, "size") != NULL) {
95 s->size = qemu_opt_get_size(opts, "size", 0);
96 s->has_size = true;
97 } else {
98 s->has_size = false;
99 s->size = real_size - s->offset;
102 /* Check size and offset */
103 if (real_size < s->offset || (real_size - s->offset) < s->size) {
104 error_setg(errp, "The sum of offset (%" PRIu64 ") and size "
105 "(%" PRIu64 ") has to be smaller or equal to the "
106 " actual size of the containing file (%" PRId64 ")",
107 s->offset, s->size, real_size);
108 ret = -EINVAL;
109 goto end;
112 /* Make sure size is multiple of BDRV_SECTOR_SIZE to prevent rounding
113 * up and leaking out of the specified area. */
114 if (!QEMU_IS_ALIGNED(s->size, BDRV_SECTOR_SIZE)) {
115 error_setg(errp, "Specified size is not multiple of %llu",
116 BDRV_SECTOR_SIZE);
117 ret = -EINVAL;
118 goto end;
121 ret = 0;
123 end:
125 qemu_opts_del(opts);
127 return ret;
130 static int raw_reopen_prepare(BDRVReopenState *reopen_state,
131 BlockReopenQueue *queue, Error **errp)
133 assert(reopen_state != NULL);
134 assert(reopen_state->bs != NULL);
136 reopen_state->opaque = g_new0(BDRVRawState, 1);
138 return raw_read_options(
139 reopen_state->options,
140 reopen_state->bs,
141 reopen_state->opaque,
142 errp);
145 static void raw_reopen_commit(BDRVReopenState *state)
147 BDRVRawState *new_s = state->opaque;
148 BDRVRawState *s = state->bs->opaque;
150 memcpy(s, new_s, sizeof(BDRVRawState));
152 g_free(state->opaque);
153 state->opaque = NULL;
156 static void raw_reopen_abort(BDRVReopenState *state)
158 g_free(state->opaque);
159 state->opaque = NULL;
162 static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset,
163 uint64_t bytes, QEMUIOVector *qiov,
164 int flags)
166 BDRVRawState *s = bs->opaque;
168 if (offset > UINT64_MAX - s->offset) {
169 return -EINVAL;
171 offset += s->offset;
173 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
174 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
177 static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
178 uint64_t bytes, QEMUIOVector *qiov,
179 int flags)
181 BDRVRawState *s = bs->opaque;
182 void *buf = NULL;
183 BlockDriver *drv;
184 QEMUIOVector local_qiov;
185 int ret;
187 if (s->has_size && (offset > s->size || bytes > (s->size - offset))) {
188 /* There's not enough space for the data. Don't write anything and just
189 * fail to prevent leaking out of the size specified in options. */
190 return -ENOSPC;
193 if (offset > UINT64_MAX - s->offset) {
194 ret = -EINVAL;
195 goto fail;
198 if (bs->probed && offset < BLOCK_PROBE_BUF_SIZE && bytes) {
199 /* Handling partial writes would be a pain - so we just
200 * require that guests have 512-byte request alignment if
201 * probing occurred */
202 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512);
203 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);
204 assert(offset == 0 && bytes >= BLOCK_PROBE_BUF_SIZE);
206 buf = qemu_try_blockalign(bs->file->bs, 512);
207 if (!buf) {
208 ret = -ENOMEM;
209 goto fail;
212 ret = qemu_iovec_to_buf(qiov, 0, buf, 512);
213 if (ret != 512) {
214 ret = -EINVAL;
215 goto fail;
218 drv = bdrv_probe_all(buf, 512, NULL);
219 if (drv != bs->drv) {
220 ret = -EPERM;
221 goto fail;
224 /* Use the checked buffer, a malicious guest might be overwriting its
225 * original buffer in the background. */
226 qemu_iovec_init(&local_qiov, qiov->niov + 1);
227 qemu_iovec_add(&local_qiov, buf, 512);
228 qemu_iovec_concat(&local_qiov, qiov, 512, qiov->size - 512);
229 qiov = &local_qiov;
232 offset += s->offset;
234 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
235 ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
237 fail:
238 if (qiov == &local_qiov) {
239 qemu_iovec_destroy(&local_qiov);
241 qemu_vfree(buf);
242 return ret;
245 static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
246 int64_t sector_num,
247 int nb_sectors, int *pnum,
248 BlockDriverState **file)
250 BDRVRawState *s = bs->opaque;
251 *pnum = nb_sectors;
252 *file = bs->file->bs;
253 sector_num += s->offset / BDRV_SECTOR_SIZE;
254 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
255 (sector_num << BDRV_SECTOR_BITS);
258 static int coroutine_fn raw_co_pwrite_zeroes(BlockDriverState *bs,
259 int64_t offset, int count,
260 BdrvRequestFlags flags)
262 BDRVRawState *s = bs->opaque;
263 if (offset > UINT64_MAX - s->offset) {
264 return -EINVAL;
266 offset += s->offset;
267 return bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
270 static int coroutine_fn raw_co_pdiscard(BlockDriverState *bs,
271 int64_t offset, int count)
273 BDRVRawState *s = bs->opaque;
274 if (offset > UINT64_MAX - s->offset) {
275 return -EINVAL;
277 offset += s->offset;
278 return bdrv_co_pdiscard(bs->file->bs, offset, count);
281 static int64_t raw_getlength(BlockDriverState *bs)
283 int64_t len;
284 BDRVRawState *s = bs->opaque;
286 /* Update size. It should not change unless the file was externally
287 * modified. */
288 len = bdrv_getlength(bs->file->bs);
289 if (len < 0) {
290 return len;
293 if (len < s->offset) {
294 s->size = 0;
295 } else {
296 if (s->has_size) {
297 /* Try to honour the size */
298 s->size = MIN(s->size, len - s->offset);
299 } else {
300 s->size = len - s->offset;
304 return s->size;
307 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
309 return bdrv_get_info(bs->file->bs, bdi);
312 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
314 if (bs->probed) {
315 /* To make it easier to protect the first sector, any probed
316 * image is restricted to read-modify-write on sub-sector
317 * operations. */
318 bs->bl.request_alignment = BDRV_SECTOR_SIZE;
322 static int raw_truncate(BlockDriverState *bs, int64_t offset)
324 BDRVRawState *s = bs->opaque;
326 if (s->has_size) {
327 return -ENOTSUP;
330 if (INT64_MAX - offset < s->offset) {
331 return -EINVAL;
334 s->size = offset;
335 offset += s->offset;
336 return bdrv_truncate(bs->file->bs, offset);
339 static int raw_media_changed(BlockDriverState *bs)
341 return bdrv_media_changed(bs->file->bs);
344 static void raw_eject(BlockDriverState *bs, bool eject_flag)
346 bdrv_eject(bs->file->bs, eject_flag);
349 static void raw_lock_medium(BlockDriverState *bs, bool locked)
351 bdrv_lock_medium(bs->file->bs, locked);
354 static int raw_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
356 BDRVRawState *s = bs->opaque;
357 if (s->offset || s->has_size) {
358 return -ENOTSUP;
360 return bdrv_co_ioctl(bs->file->bs, req, buf);
363 static int raw_has_zero_init(BlockDriverState *bs)
365 return bdrv_has_zero_init(bs->file->bs);
368 static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
370 return bdrv_create_file(filename, opts, errp);
373 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
374 Error **errp)
376 BDRVRawState *s = bs->opaque;
377 int ret;
379 bs->sg = bs->file->bs->sg;
380 bs->supported_write_flags = BDRV_REQ_FUA &
381 bs->file->bs->supported_write_flags;
382 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
383 bs->file->bs->supported_zero_flags;
385 if (bs->probed && !bdrv_is_read_only(bs)) {
386 fprintf(stderr,
387 "WARNING: Image format was not specified for '%s' and probing "
388 "guessed raw.\n"
389 " Automatically detecting the format is dangerous for "
390 "raw images, write operations on block 0 will be restricted.\n"
391 " Specify the 'raw' format explicitly to remove the "
392 "restrictions.\n",
393 bs->file->bs->filename);
396 ret = raw_read_options(options, bs, s, errp);
397 if (ret < 0) {
398 return ret;
401 if (bs->sg && (s->offset || s->has_size)) {
402 error_setg(errp, "Cannot use offset/size with SCSI generic devices");
403 return -EINVAL;
406 return 0;
409 static void raw_close(BlockDriverState *bs)
413 static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
415 /* smallest possible positive score so that raw is used if and only if no
416 * other block driver works
418 return 1;
421 static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
423 BDRVRawState *s = bs->opaque;
424 int ret;
426 ret = bdrv_probe_blocksizes(bs->file->bs, bsz);
427 if (ret < 0) {
428 return ret;
431 if (!QEMU_IS_ALIGNED(s->offset, MAX(bsz->log, bsz->phys))) {
432 return -ENOTSUP;
435 return 0;
438 static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
440 BDRVRawState *s = bs->opaque;
441 if (s->offset || s->has_size) {
442 return -ENOTSUP;
444 return bdrv_probe_geometry(bs->file->bs, geo);
447 BlockDriver bdrv_raw = {
448 .format_name = "raw",
449 .instance_size = sizeof(BDRVRawState),
450 .bdrv_probe = &raw_probe,
451 .bdrv_reopen_prepare = &raw_reopen_prepare,
452 .bdrv_reopen_commit = &raw_reopen_commit,
453 .bdrv_reopen_abort = &raw_reopen_abort,
454 .bdrv_open = &raw_open,
455 .bdrv_close = &raw_close,
456 .bdrv_create = &raw_create,
457 .bdrv_co_preadv = &raw_co_preadv,
458 .bdrv_co_pwritev = &raw_co_pwritev,
459 .bdrv_co_pwrite_zeroes = &raw_co_pwrite_zeroes,
460 .bdrv_co_pdiscard = &raw_co_pdiscard,
461 .bdrv_co_get_block_status = &raw_co_get_block_status,
462 .bdrv_truncate = &raw_truncate,
463 .bdrv_getlength = &raw_getlength,
464 .has_variable_length = true,
465 .bdrv_get_info = &raw_get_info,
466 .bdrv_refresh_limits = &raw_refresh_limits,
467 .bdrv_probe_blocksizes = &raw_probe_blocksizes,
468 .bdrv_probe_geometry = &raw_probe_geometry,
469 .bdrv_media_changed = &raw_media_changed,
470 .bdrv_eject = &raw_eject,
471 .bdrv_lock_medium = &raw_lock_medium,
472 .bdrv_co_ioctl = &raw_co_ioctl,
473 .create_opts = &raw_create_opts,
474 .bdrv_has_zero_init = &raw_has_zero_init
477 static void bdrv_raw_init(void)
479 bdrv_register(&bdrv_raw);
482 block_init(bdrv_raw_init);