From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Jan Kiszka <jan.kiszka@siemens.com>, qemu-devel <qemu-devel@nongnu.org>
Cc: "Bin Meng" <bmeng.cn@gmail.com>,
qemu-block@nongnu.org,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Jan Lübbe" <jlu@pengutronix.de>,
"Jerome Forissier" <jerome.forissier@linaro.org>,
"Warner Losh" <imp@bsdimp.com>, "Cédric Le Goater" <clg@kaod.org>,
"Joel Stanley" <joel@jms.id.au>,
"Alistair Francis" <alistair@alistair23.me>,
"Alexander Bulekov" <alxndr@bu.edu>
Subject: Re: [PATCH v5 1/6] hw/sd/sdcard: Fix size check for backing block image
Date: Thu, 30 Oct 2025 23:10:09 +0100 [thread overview]
Message-ID: <fb5dee7c-c7c7-4a18-9282-f990f17a32c1@linaro.org> (raw)
In-Reply-To: <484a8687e5cd44d94597512502180ba1aba57572.1760702638.git.jan.kiszka@siemens.com>
On 17/10/25 14:03, Jan Kiszka wrote:
> From: Jan Kiszka <jan.kiszka@siemens.com>
>
> Alignment rules apply the the individual partitions (user, boot, later
> on also RPMB) and depend both on the size of the image and the type of
> the device. Up to and including 2GB, the power-of-2 rule applies to the
> user data area. For larger images, multiples of 512 sectors must be used
> for eMMC and multiples of 512K for SD-cards. Fix the check accordingly
> and also detect if the image is too small to even hold the boot
> partitions.
>
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
> ---
> CC: Warner Losh <imp@bsdimp.com>
> CC: Cédric Le Goater <clg@kaod.org>
> CC: Joel Stanley <joel@jms.id.au>
> CC: Alistair Francis <alistair@alistair23.me>
> CC: Alexander Bulekov <alxndr@bu.edu>
> ---
> hw/sd/sd.c | 65 +++++++++++++++++++++++++++++++++++++-----------------
> 1 file changed, 45 insertions(+), 20 deletions(-)
>
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index d7a496d77c..d1e1bb4f0e 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -2759,9 +2759,32 @@ static void sd_instance_finalize(Object *obj)
> timer_free(sd->ocr_power_timer);
> }
>
> +static void sd_blk_size_error(SDState *sd, int64_t blk_size,
> + int64_t blk_size_aligned, const char *rule,
> + Error **errp)
> +{
> + const char *dev_type = sd_is_emmc(sd) ? "eMMC" : "SD card";
> + char *blk_size_str;
> +
> + blk_size_str = size_to_str(blk_size);
> + error_setg(errp, "Invalid %s size: %s", dev_type, blk_size_str);
> + g_free(blk_size_str);
> +
> + blk_size_str = size_to_str(blk_size_aligned);
> + error_append_hint(errp,
> + "%s size has to be %s, e.g. %s.\n"
> + "You can resize disk images with"
> + " 'qemu-img resize <imagefile> <new-size>'\n"
> + "(note that this will lose data if you make the"
> + " image smaller than it currently is).\n",
> + dev_type, rule, blk_size_str);
> + g_free(blk_size_str);
> +}
> +
> static void sd_realize(DeviceState *dev, Error **errp)
> {
> SDState *sd = SDMMC_COMMON(dev);
> + int64_t blk_size = -ENOMEDIUM;
> int ret;
>
> switch (sd->spec_version) {
> @@ -2774,32 +2797,34 @@ static void sd_realize(DeviceState *dev, Error **errp)
> }
>
> if (sd->blk) {
> - int64_t blk_size;
> -
> if (!blk_supports_write_perm(sd->blk)) {
> error_setg(errp, "Cannot use read-only drive as SD card");
> return;
> }
>
> blk_size = blk_getlength(sd->blk);
> - if (blk_size > 0 && !is_power_of_2(blk_size)) {
> - int64_t blk_size_aligned = pow2ceil(blk_size);
> - char *blk_size_str;
> -
> - blk_size_str = size_to_str(blk_size);
> - error_setg(errp, "Invalid SD card size: %s", blk_size_str);
> - g_free(blk_size_str);
> -
> - blk_size_str = size_to_str(blk_size_aligned);
> - error_append_hint(errp,
> - "SD card size has to be a power of 2, e.g. %s.\n"
> - "You can resize disk images with"
> - " 'qemu-img resize <imagefile> <new-size>'\n"
> - "(note that this will lose data if you make the"
> - " image smaller than it currently is).\n",
> - blk_size_str);
> - g_free(blk_size_str);
> -
> + }
> + if (blk_size >= 0) {
> + blk_size -= sd->boot_part_size * 2;
> + if (blk_size > SDSC_MAX_CAPACITY) {
> + if (sd_is_emmc(sd) && blk_size % (1 << HWBLOCK_SHIFT) != 0) {
I'll replace by:
!QEMU_IS_ALIGNED(blk_size, 1 << HWBLOCK_SHIFT)
> + int64_t blk_size_aligned =
> + ((blk_size >> HWBLOCK_SHIFT) + 1) << HWBLOCK_SHIFT;
> + sd_blk_size_error(sd, blk_size, blk_size_aligned,
> + "multiples of 512", errp);
> + return;
> + } else if (!sd_is_emmc(sd) && blk_size % (512 * KiB)) {
and:
!QEMU_IS_ALIGNED(blk_size, 512 * KiB)
> + int64_t blk_size_aligned = ((blk_size >> 19) + 1) << 19;
> + sd_blk_size_error(sd, blk_size, blk_size_aligned,
> + "multiples of 512K", errp);
> + return;
> + }
> + } else if (blk_size > 0 && !is_power_of_2(blk_size)) {
> + sd_blk_size_error(sd, blk_size, pow2ceil(blk_size), "a power of 2",
> + errp);
> + return;
> + } else if (blk_size < 0) {
> + error_setg(errp, "eMMC image smaller than boot partitions");
> return;
> }
>
Long due cleanup, thank you very much!!!
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
next prev parent reply other threads:[~2025-10-30 22:12 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-17 12:03 [PATCH v5 0/6] sd: Add RPMB emulation to eMMC model Jan Kiszka
2025-10-17 12:03 ` [PATCH v5 1/6] hw/sd/sdcard: Fix size check for backing block image Jan Kiszka
2025-10-20 7:29 ` Cédric Le Goater
2025-10-30 22:10 ` Philippe Mathieu-Daudé [this message]
2025-10-17 12:03 ` [PATCH v5 2/6] hw/sd/sdcard: Allow user-instantiated eMMC Jan Kiszka
2025-10-27 11:55 ` Daniel P. Berrangé
2025-10-27 12:23 ` Philippe Mathieu-Daudé
2025-10-27 12:35 ` Daniel P. Berrangé
2025-10-27 12:44 ` Philippe Mathieu-Daudé
2025-10-30 14:17 ` Jan Lübbe
2025-10-27 12:45 ` Jan Lübbe
2025-10-30 16:50 ` Jan Lübbe
2025-10-30 18:10 ` Jan Kiszka
2025-10-17 12:03 ` [PATCH v5 3/6] hw/sd/sdcard: Add basic support for RPMB partition Jan Kiszka
2025-11-03 15:08 ` Philippe Mathieu-Daudé
2025-11-04 6:34 ` Jan Kiszka
2025-10-17 12:03 ` [PATCH v5 4/6] hw/sd/sdcard: Handle RPMB MAC field Jan Kiszka
2025-11-03 15:18 ` Philippe Mathieu-Daudé
2025-11-04 6:43 ` Jan Kiszka
2025-10-17 12:03 ` [PATCH v5 5/6] scripts: Add helper script to generate eMMC block device images Jan Kiszka
2025-11-03 13:12 ` Philippe Mathieu-Daudé
2025-11-04 6:30 ` Jan Kiszka
2025-11-04 9:26 ` Jan Kiszka
2025-11-04 12:33 ` Philippe Mathieu-Daudé
2025-11-04 12:40 ` Jan Kiszka
2025-11-04 14:14 ` Jan Kiszka
2025-10-17 12:03 ` [PATCH v5 6/6] docs: Add eMMC device model description Jan Kiszka
2025-11-03 13:15 ` Philippe Mathieu-Daudé
2025-10-20 7:28 ` [PATCH v5 0/6] sd: Add RPMB emulation to eMMC model Cédric Le Goater
2025-10-27 11:15 ` Jan Kiszka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=fb5dee7c-c7c7-4a18-9282-f990f17a32c1@linaro.org \
--to=philmd@linaro.org \
--cc=alex.bennee@linaro.org \
--cc=alistair@alistair23.me \
--cc=alxndr@bu.edu \
--cc=bmeng.cn@gmail.com \
--cc=clg@kaod.org \
--cc=ilias.apalodimas@linaro.org \
--cc=imp@bsdimp.com \
--cc=jan.kiszka@siemens.com \
--cc=jerome.forissier@linaro.org \
--cc=jlu@pengutronix.de \
--cc=joel@jms.id.au \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).