On 14.09.25 14:46, Jan Kiszka wrote:
> From: Jan Kiszka <[email protected]>
>
> 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 <[email protected]>
> Reviewed-by: Warner Losh <[email protected]>
> ---
> CC: Warner Losh <[email protected]>
> CC: Cédric Le Goater <[email protected]>
> CC: Joel Stanley <[email protected]>
> CC: Alistair Francis <[email protected]>
> CC: Alexander Bulekov <[email protected]>
> ---
> hw/sd/sd.c | 61 +++++++++++++++++++++++++++++++++++++-----------------
> 1 file changed, 42 insertions(+), 19 deletions(-)
>
> diff --git a/hw/sd/sd.c b/hw/sd/sd.c
> index d7a496d77c..b42cd01d1f 100644
> --- a/hw/sd/sd.c
> +++ b/hw/sd/sd.c
> @@ -2759,6 +2759,28 @@ 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);
> @@ -2781,25 +2803,26 @@ static void sd_realize(DeviceState *dev, Error **errp)
> 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);
> -
> + blk_size = blk_getlength(sd->blk) - sd->boot_part_size * 2;
> + if (blk_size > SDSC_MAX_CAPACITY) {
> + if (sd_is_emmc(sd) && blk_size % (1 << HWBLOCK_SHIFT) != 0) {
> + 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)) {
> + 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");
Cedric, I just played with some ast* machines and noticed that they now
trigger that error above when no eMMC disk image is specified
("qemu-system-aarch64 -M ast2700a1-evb"). Is that a valid error, i.e. we
shouldn't have tried to boot without an eMMC at all so far, or would
this be a regression?
Jan
> return;
> }
>
--
Siemens AG, Foundational Technologies
Linux Expert Center