2012-07-10 11:12:31 +02:00
|
|
|
/*
|
|
|
|
* Common code for block device models
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc.
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
* later. See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-06-29 10:12:57 +02:00
|
|
|
#ifndef HW_BLOCK_H
|
|
|
|
#define HW_BLOCK_H
|
2012-07-10 11:12:31 +02:00
|
|
|
|
pflash: Require backend size to match device, improve errors
We reject undersized backends with a rather enigmatic "failed to read
the initial flash content" error. For instance:
$ qemu-system-ppc64 -S -display none -M sam460ex -drive if=pflash,format=raw,file=eins.img
qemu-system-ppc64: Initialization of device cfi.pflash02 failed: failed to read the initial flash content
We happily accept oversized images, ignoring their tail. Throwing
away parts of firmware that way is pretty much certain to end in an
even more enigmatic failure to boot.
Require the backend's size to match the device's size exactly. Report
mismatch like this:
qemu-system-ppc64: Initialization of device cfi.pflash01 failed: device requires 1048576 bytes, block backend provides 512 bytes
Improve the error for actual read failures to "can't read block
backend".
To avoid duplicating even more code between the two pflash device
models, do all that in new helper blk_check_size_and_read_all().
The error reporting can still be confusing. For instance:
qemu-system-ppc64 -S -display none -M taihu -drive if=pflash,format=raw,file=eins.img -drive if=pflash,unit=1,format=raw,file=zwei.img
qemu-system-ppc64: Initialization of device cfi.pflash02 failed: device requires 2097152 bytes, block backend provides 512 bytes
Leaves the user guessing which of the two -drive is wrong. Mention
the issue in a TODO comment.
Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190319163551.32499-2-armbru@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-03-19 17:35:50 +01:00
|
|
|
#include "exec/hwaddr.h"
|
2018-02-11 10:36:01 +01:00
|
|
|
#include "qapi/qapi-types-block-core.h"
|
2020-12-11 23:05:12 +01:00
|
|
|
#include "hw/qdev-properties-system.h"
|
2012-07-10 11:12:31 +02:00
|
|
|
|
2012-07-11 15:08:36 +02:00
|
|
|
/* Configuration */
|
|
|
|
|
|
|
|
typedef struct BlockConf {
|
2014-10-07 13:59:18 +02:00
|
|
|
BlockBackend *blk;
|
2021-07-05 15:04:57 +02:00
|
|
|
OnOffAuto backend_defaults;
|
2020-05-29 00:55:14 +02:00
|
|
|
uint32_t physical_block_size;
|
|
|
|
uint32_t logical_block_size;
|
|
|
|
uint32_t min_io_size;
|
2012-07-11 15:08:36 +02:00
|
|
|
uint32_t opt_io_size;
|
|
|
|
int32_t bootindex;
|
|
|
|
uint32_t discard_granularity;
|
|
|
|
/* geometry, not all devices use this */
|
|
|
|
uint32_t cyls, heads, secs;
|
2019-10-16 18:41:39 +02:00
|
|
|
uint32_t lcyls, lheads, lsecs;
|
2016-06-23 15:12:35 +02:00
|
|
|
OnOffAuto wce;
|
2017-01-24 13:58:00 +01:00
|
|
|
bool share_rw;
|
2022-08-24 11:50:44 +02:00
|
|
|
OnOffAuto account_invalid, account_failed;
|
2016-06-29 17:41:35 +02:00
|
|
|
BlockdevOnError rerror;
|
|
|
|
BlockdevOnError werror;
|
2012-07-11 15:08:36 +02:00
|
|
|
} BlockConf;
|
|
|
|
|
|
|
|
static inline unsigned int get_physical_block_exp(BlockConf *conf)
|
|
|
|
{
|
|
|
|
unsigned int exp = 0, size;
|
|
|
|
|
|
|
|
for (size = conf->physical_block_size;
|
|
|
|
size > conf->logical_block_size;
|
|
|
|
size >>= 1) {
|
|
|
|
exp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return exp;
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:40:14 +02:00
|
|
|
#define DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf) \
|
2021-07-05 15:04:57 +02:00
|
|
|
DEFINE_PROP_ON_OFF_AUTO("backend_defaults", _state, \
|
|
|
|
_conf.backend_defaults, ON_OFF_AUTO_AUTO), \
|
2012-07-11 15:08:36 +02:00
|
|
|
DEFINE_PROP_BLOCKSIZE("logical_block_size", _state, \
|
2015-02-16 12:47:58 +01:00
|
|
|
_conf.logical_block_size), \
|
2012-07-11 15:08:36 +02:00
|
|
|
DEFINE_PROP_BLOCKSIZE("physical_block_size", _state, \
|
2015-02-16 12:47:58 +01:00
|
|
|
_conf.physical_block_size), \
|
2020-05-29 00:55:14 +02:00
|
|
|
DEFINE_PROP_SIZE32("min_io_size", _state, _conf.min_io_size, 0), \
|
|
|
|
DEFINE_PROP_SIZE32("opt_io_size", _state, _conf.opt_io_size, 0), \
|
|
|
|
DEFINE_PROP_SIZE32("discard_granularity", _state, \
|
2019-10-16 18:41:37 +02:00
|
|
|
_conf.discard_granularity, -1), \
|
|
|
|
DEFINE_PROP_ON_OFF_AUTO("write-cache", _state, _conf.wce, \
|
|
|
|
ON_OFF_AUTO_AUTO), \
|
2022-08-24 11:50:44 +02:00
|
|
|
DEFINE_PROP_BOOL("share-rw", _state, _conf.share_rw, false), \
|
|
|
|
DEFINE_PROP_ON_OFF_AUTO("account-invalid", _state, \
|
|
|
|
_conf.account_invalid, ON_OFF_AUTO_AUTO), \
|
|
|
|
DEFINE_PROP_ON_OFF_AUTO("account-failed", _state, \
|
|
|
|
_conf.account_failed, ON_OFF_AUTO_AUTO)
|
2012-07-11 15:08:36 +02:00
|
|
|
|
2019-04-29 17:40:14 +02:00
|
|
|
#define DEFINE_BLOCK_PROPERTIES(_state, _conf) \
|
|
|
|
DEFINE_PROP_DRIVE("drive", _state, _conf.blk), \
|
|
|
|
DEFINE_BLOCK_PROPERTIES_BASE(_state, _conf)
|
|
|
|
|
2019-10-16 18:41:37 +02:00
|
|
|
#define DEFINE_BLOCK_CHS_PROPERTIES(_state, _conf) \
|
|
|
|
DEFINE_PROP_UINT32("cyls", _state, _conf.cyls, 0), \
|
|
|
|
DEFINE_PROP_UINT32("heads", _state, _conf.heads, 0), \
|
2019-10-16 18:41:39 +02:00
|
|
|
DEFINE_PROP_UINT32("secs", _state, _conf.secs, 0), \
|
|
|
|
DEFINE_PROP_UINT32("lcyls", _state, _conf.lcyls, 0), \
|
|
|
|
DEFINE_PROP_UINT32("lheads", _state, _conf.lheads, 0), \
|
|
|
|
DEFINE_PROP_UINT32("lsecs", _state, _conf.lsecs, 0)
|
2012-07-11 15:08:36 +02:00
|
|
|
|
2016-06-29 17:41:35 +02:00
|
|
|
#define DEFINE_BLOCK_ERROR_PROPERTIES(_state, _conf) \
|
|
|
|
DEFINE_PROP_BLOCKDEV_ON_ERROR("rerror", _state, _conf.rerror, \
|
|
|
|
BLOCKDEV_ON_ERROR_AUTO), \
|
|
|
|
DEFINE_PROP_BLOCKDEV_ON_ERROR("werror", _state, _conf.werror, \
|
|
|
|
BLOCKDEV_ON_ERROR_AUTO)
|
|
|
|
|
pflash: Require backend size to match device, improve errors
We reject undersized backends with a rather enigmatic "failed to read
the initial flash content" error. For instance:
$ qemu-system-ppc64 -S -display none -M sam460ex -drive if=pflash,format=raw,file=eins.img
qemu-system-ppc64: Initialization of device cfi.pflash02 failed: failed to read the initial flash content
We happily accept oversized images, ignoring their tail. Throwing
away parts of firmware that way is pretty much certain to end in an
even more enigmatic failure to boot.
Require the backend's size to match the device's size exactly. Report
mismatch like this:
qemu-system-ppc64: Initialization of device cfi.pflash01 failed: device requires 1048576 bytes, block backend provides 512 bytes
Improve the error for actual read failures to "can't read block
backend".
To avoid duplicating even more code between the two pflash device
models, do all that in new helper blk_check_size_and_read_all().
The error reporting can still be confusing. For instance:
qemu-system-ppc64 -S -display none -M taihu -drive if=pflash,format=raw,file=eins.img -drive if=pflash,unit=1,format=raw,file=zwei.img
qemu-system-ppc64: Initialization of device cfi.pflash02 failed: device requires 2097152 bytes, block backend provides 512 bytes
Leaves the user guessing which of the two -drive is wrong. Mention
the issue in a TODO comment.
Suggested-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190319163551.32499-2-armbru@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-03-19 17:35:50 +01:00
|
|
|
/* Backend access helpers */
|
|
|
|
|
|
|
|
bool blk_check_size_and_read_all(BlockBackend *blk, void *buf, hwaddr size,
|
|
|
|
Error **errp);
|
|
|
|
|
2012-07-11 15:08:37 +02:00
|
|
|
/* Configuration helpers */
|
|
|
|
|
2017-11-22 04:08:44 +01:00
|
|
|
bool blkconf_geometry(BlockConf *conf, int *trans,
|
2014-08-12 04:12:54 +02:00
|
|
|
unsigned cyls_max, unsigned heads_max, unsigned secs_max,
|
|
|
|
Error **errp);
|
2020-05-29 00:55:10 +02:00
|
|
|
bool blkconf_blocksizes(BlockConf *conf, Error **errp);
|
2017-11-22 04:08:44 +01:00
|
|
|
bool blkconf_apply_backend_options(BlockConf *conf, bool readonly,
|
2017-01-24 13:43:31 +01:00
|
|
|
bool resizable, Error **errp);
|
2012-07-11 15:08:36 +02:00
|
|
|
|
2012-07-10 11:12:31 +02:00
|
|
|
/* Hard disk geometry */
|
|
|
|
|
2014-10-07 13:59:18 +02:00
|
|
|
void hd_geometry_guess(BlockBackend *blk,
|
2012-07-10 11:12:41 +02:00
|
|
|
uint32_t *pcyls, uint32_t *pheads, uint32_t *psecs,
|
2012-07-10 11:12:37 +02:00
|
|
|
int *ptrans);
|
2012-07-10 11:12:53 +02:00
|
|
|
int hd_bios_chs_auto_trans(uint32_t cyls, uint32_t heads, uint32_t secs);
|
2012-07-10 11:12:31 +02:00
|
|
|
|
|
|
|
#endif
|