Misc HW patches for 2025-11-04
The following changes since commit e9c692eabbbb7f395347605a6ef33a32d398ea25:
Merge tag 'next-pr-pull-request' of https://gitlab.com/berrange/qemu into staging (2025-11-04 15:17:31 +0100)
are available in the Git repository at:
https://github.com/philmd/qemu.git tags/hw-misc-20251104
for you to fetch changes up to f79fec0275ee7a5151ced9b893c4af676c7f5db6:
hw/audio: Remove dead code from ac97_realize (2025-11-04 18:19:16 +0100)
----------------------------------------------------------------
Misc HW patches
- Add RPMB emulation to eMMC model :)
- Use generic MachineState::fdt field in microvm machine
- Remove dead code in ac97_realize()
----------------------------------------------------------------
Jan Kiszka (5):
hw/sd/sdcard: Fix size check for backing block image hw/sd/sdcard: Add basic support for RPMB partition hw/sd/sdcard: Handle RPMB MAC field
scripts: Add helper script to generate eMMC block device images
docs: Add eMMC device model description
Jan Luebbe (1):
hw/sd/sdcard: Allow user creation of eMMCs
Luigi Leonardi (1):
hw/i386/microvm: Use fdt field from MachineState
Osama Abdelkader (1):
hw/audio: Remove dead code from ac97_realize
docs/system/device-emulation.rst | 1 + docs/system/devices/emmc.rst | 55 +++++ hw/sd/sdmmc-internal.h | 22 ++ include/hw/i386/microvm.h | 2 - hw/audio/ac97.c | 19 -- hw/i386/microvm-dt.c | 96 ++++---- hw/sd/sd.c | 368 ++++++++++++++++++++++++++++--- hw/sd/trace-events | 2 + scripts/mkemmc.sh | 216 ++++++++++++++++++ 9 files changed, 686 insertions(+), 95 deletions(-) create mode 100644 docs/system/devices/emmc.rst create mode 100755 scripts/mkemmc.sh
Signature · 1 line
2.51.0
[PULL 1/8] hw/sd/sdcard: Fix size check for backing block image
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.
Message-ID: <591f6d8a9dc86428723cb6876df6e717cc41a70e.1762261430.git.jan.kiszka@siemens.com>
--- hw/sd/sd.c | 69 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 21 deletions(-)
hw/sd/sd.c +48 -21
@@ -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,36 @@ 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) && + !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) && + !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 ; }
@@ -2810,7 +2837,7 @@ static void sd_realize(DeviceState *dev, Error **errp) }blk_set_dev_ops (sd->blk, &sd_block_ops, sd); } -if (sd->boot_part_size % (128 * KiB) || +if (!QEMU_IS_ALIGNED (sd->boot_part_size,128 * KiB) || sd->boot_part_size >255 *128 * KiB) { g_autofree char *size_str = size_to_str(sd->boot_part_size);
Signature · 1 line
2.51.0
[PULL 5/8] scripts: Add helper script to generate eMMC block device images
In reply to Philippe Mathieu-DaudéFrom: Jan Kiszka <[email protected]>
As an eMMC block device image may consist of more than just the user data partition, provide a helper script that can compose the image from boot partitions, an RPMB partition and the user data image. The script also does the required size validation and/or rounding.
Message-ID: <[email protected]>
--- scripts/mkemmc.sh | 216 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100755 scripts/mkemmc.sh
scripts/mkemmc.sh new file +216 -0
@@ -0,0 +1,216 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0-only +# +# Create eMMC block device image from boot, RPMB and user data images +# +# Copyright (c) Siemens, 2025 +# +# Authors: +# Jan Kiszka <[email protected]> +# + +usage() { + echo "$0 [OPTIONS] USER_IMG[:SIZE] OUTPUT_IMG" + echo "" + echo "SIZE must be a power of 2 up to 2G and multiples of 512 byte from there on." + echo "If no SIZE is specified, the size of USER_ING will be used (rounded up)." + echo "" + echo "Supported options:" + echo " -b BOOT1_IMG[:SIZE] Add boot partitions. SIZE must be multiples of 128K. If" + echo " no SIZE is specified, the size of BOOT1_IMG will be" + echo " used (rounded up). BOOT1_IMG will be stored in boot" + echo " partition 1, and a boot partition 2 of the same size" + echo " will be created as empty (all zeros) unless -B is" + echo " specified as well." + echo " -B BOOT2_IMG Fill boot partition 2 with BOOT2_IMG. Must be combined" + echo " with -b which is also defining the partition size." + echo " -r RPMB_IMG[:SIZE] Add RPMB partition. SIZE must be multiples of 128K. If" + echo " no SIZE is specified, the size of RPMB_IMG will be" + echo " used (rounded up)." + echo " -h, --help This help" + echo "" + echo "All SIZE parameters support the units K, M, G. If SIZE is smaller than the" + echo "associated image, it will be truncated in the output image." + exit "$1" +} + +process_size() { + name=$1 + image_file=$2 + alignment=$3 + image_arg=$4 + if [ "${image_arg#*:}" = "$image_arg" ]; then + if ! size=$(wc -c < "$image_file" 2>/dev/null); then + echo "Missing $name image '$image_file'." >&2 + exit 1 + fi + if [ "$alignment" = 128 ]; then + size=$(( (size + 128 * 1024 - 1) & ~(128 * 1024 - 1) )) + elif [ $size -gt $((2 * 1024 * 1024 * 1024)) ]; then + size=$(( (size + 511) & ~511 )) + elif [ $(( size & (size - 1) )) -gt 0 ]; then + n=0 + while [ "$size" -gt 0 ]; do + size=$((size >> 1)) + n=$((n + 1)) + done + size=$((1 << n)) + fi + else + value="${image_arg#*:}" + if [ "${value%K}" != "$value" ]; then + size=${value%K} + multiplier=1024 + elif [ "${value%M}" != "$value" ]; then + size=${value%M} + multiplier=$((1024 * 1024)) + elif [ "${value%G}" != "$value" ]; then + size=${value%G} + multiplier=$((1024 * 1024 * 1024)) + else + size=$value + multiplier=1 + fi + # check if "$size" is a valid integer by doing a self-comparison + if [ "$size" -eq "$size" ] 2>/dev/null; then + size=$((size * multiplier)) + else + echo "Invalid value '$value' specified for $image_file image size." >&2 + exit 1 + fi + if [ "$alignment" = 128 ]; then + if [ $(( size & (128 * 1024 - 1) )) -ne 0 ]; then + echo "The $name image size must be multiples of 128K." >&2 + exit 1 + fi + elif [ $size -gt $((2 * 1024 * 1024 * 1024)) ]; then + if [ $(( size & 511)) -ne 0 ]; then + echo "The $name image size must be multiples of 512 (if >2G)." >&2 + exit 1 + fi + elif [ $(( size & (size - 1) )) -gt 0 ]; then + echo "The $name image size must be power of 2 (up to 2G)." >&2 + exit 1 + fi + fi + echo $size +} + +check_truncation() { + image_file=$1 + output_size=$2 + if [ "$image_file" = "/dev/zero" ]; then + return + fi + if ! actual_size=$(wc -c < "$image_file" 2>/dev/null); then + echo "Missing image '$image_file'." >&2 + exit 1 + fi + if [ "$actual_size" -gt "$output_size" ]; then + echo "Warning: image '$image_file' will be truncated on output." + fi +} + +userimg= +outimg= +bootimg1= +bootimg2=/dev/zero +bootsz=0 +rpmbimg= +rpmbsz=0 + +while [ $# -gt 0 ]; do + case "$1" in + -b) + shift + [ $# -ge 1 ] || usage 1 + bootimg1=${1%%:*} + bootsz=$(process_size boot "$bootimg1" 128 "$1") + shift + ;; + -B) + shift + [ $# -ge 1 ] || usage 1 + bootimg2=$1 + shift + ;; + -r) + shift + [ $# -ge 1 ] || usage 1 + rpmbimg=${1%%:*} + rpmbsz=$(process_size RPMB "$rpmbimg" 128 "$1") + shift + ;; + -h|--help) + usage 0 + ;; + *) + if [ -z "$userimg" ]; then + userimg=${1%%:*} + usersz=$(process_size user "$userimg" U "$1") + elif [ -z "$outimg" ]; then + outimg=$1 + else + usage 1 + fi + shift + ;; + esac +done + +[ -n "$outimg" ] || usage 1 + +if [ "$bootsz" -gt $((32640 * 1024)) ]; then + echo "Boot image size is larger than 32640K." >&2 + exit 1 +fi +if [ "$rpmbsz" -gt $((16384 * 1024)) ]; then + echo "RPMB image size is larger than 16384K." >&2 + exit 1 +fi + +echo "Creating eMMC image" + +truncate -s 0 "$outimg" +pos=0 + +if [ "$bootsz" -gt 0 ]; then + echo " Boot partition 1 and 2: $((bootsz / 1024))K each" + blocks=$(( bootsz / (128 * 1024) )) + check_truncation "$bootimg1" "$bootsz" + dd if="$bootimg1" of="$outimg" conv=sparse bs=128K count=$blocks \ + status=none + check_truncation "$bootimg2" "$bootsz" + dd if="$bootimg2" of="$outimg" conv=sparse bs=128K count=$blocks \ + seek=$blocks status=none + pos=$((2 * bootsz)) +fi + +if [ "$rpmbsz" -gt 0 ]; then + echo " RPMB partition: $((rpmbsz / 1024))K" + blocks=$(( rpmbsz / (128 * 1024) )) + check_truncation "$rpmbimg" "$rpmbsz" + dd if="$rpmbimg" of="$outimg" conv=sparse bs=128K count=$blocks \ + seek=$(( pos / (128 * 1024) )) status=none + pos=$((pos + rpmbsz)) +fi + +if [ "$usersz" -lt 1024 ]; then + echo " User data: $usersz bytes" +elif [ "$usersz" -lt $((1024 * 1024)) ]; then + echo " User data: $(( (usersz + 1023) / 1024 ))K ($usersz)" +elif [ "$usersz" -lt $((1024 * 1024 * 1024)) ]; then + echo " User data: $(( (usersz + 1048575) / 1048576))M ($usersz)" +else + echo " User data: $(( (usersz + 1073741823) / 1073741824))G ($usersz)" +fi +check_truncation "$userimg" "$usersz" +dd if="$userimg" of="$outimg" conv=sparse bs=128K seek=$(( pos / (128 * 1024) )) \ + count=$(( (usersz + 128 * 1024 - 1) / (128 * 1024) )) status=none +pos=$((pos + usersz)) +truncate -s $pos "$outimg" + +echo "" +echo "Instantiate by appending to the qemu command line:" +echo " -drive file=$outimg,if=none,format=raw,id=emmc-img" +echo " -device emmc,boot-partition-size=$bootsz,rpmb-partition-size=$rpmbsz,drive=emmc-img"
Signature · 1 line
2.51.0
[PULL 2/8] hw/sd/sdcard: Allow user creation of eMMCs
In reply to Philippe Mathieu-DaudéFrom: Jan Luebbe <[email protected]>
For testing eMMC-specific functionality (such as handling boot partitions), it would be very useful to attach them to generic VMs such as x86_64 via the sdhci-pci device:
...
-drive if=none,id=emmc-drive,file=emmc.img,format=raw \
-device sdhci-pci \
-device emmc,id=emmc0,drive=emmc-drive,boot-partition-size=1048576 \
...
While most eMMCs are soldered to boards, they can also be connected to SD controllers with just a passive adapter, such as:
https://docs.radxa.com/en/accessories/emmc-to-usd
https://github.com/voltlog/emmc-wfbga153-microsd
The only change necessary to make the options above work is to avoid disabling user_creatable, so do that. The SDHCI-PCI driver in the Linux kernel already supports this just fine.
Message-ID: <[email protected]>
Enable user-instantiation so that eMMCs can be created for PCI-attached SD/MMC host controllers (such as sdhci-pci) on virt machines, for QA purposes for the eMMC model itself and for complex firmware/OS integrations using the upcoming RPMB partition support.
Message-ID: <e515cc80de261ff03b3141724298f20313259a85.1762261430.git.jan.kiszka@siemens.com>
--- hw/sd/sd.c | 2 -- 1 file changed, 2 deletions(-)
hw/sd/sd.c +0 -2
@@ -2930,8 +2930,6 @@ static void emmc_class_init(ObjectClass *klass, const void *data) dc->desc ="eMMC" ; dc->realize = emmc_realize;device_class_set_props (dc, emmc_properties); -/* Reason: Soldered on board */ - dc->user_creatable = false; sc->proto = &sd_proto_emmc;
Signature · 1 line
2.51.0
[PULL 3/8] hw/sd/sdcard: Add basic support for RPMB partition
In reply to Philippe Mathieu-DaudéFrom: Jan Kiszka <[email protected]>
The Replay Protected Memory Block (RPMB) is available since eMMC 4.4 which has been obsoleted by 4.41. Therefore lift the provided EXT_CSD_REV to 5 (4.41) and provide the basic logic to implement basic support for it. This allows to set the authentication key, read the write counter and authenticated perform data read and write requests. Those aren't actually authenticated yet, support for that will be added later.
The RPMB image needs to be added to backing block images after potential boot partitions and before the user data. It's size is controlled by the rpmb-partition-size property.
Also missing in this version (and actually not only for RPMB bits) is persistence of registers that are supposed to survive power cycles. Most prominent are the write counters or the authentication key. This feature can be added later, e.g. by append a state structure to the backing block image.
Message-ID: <d57388b599e47f5c95f30be7571b77f9016289eb.1762261430.git.jan.kiszka@siemens.com>
--- hw/sd/sdmmc-internal.h | 22 +++++ hw/sd/sd.c | 217 +++++++++++++++++++++++++++++++++++++++-- hw/sd/trace-events | 2 + 3 files changed, 233 insertions(+), 8 deletions(-)
hw/sd/sdmmc-internal.h +22 -0
@@ -118,9 +118,31 @@ DECLARE_OBJ_CHECKERS(SDState, SDCardClass, SDMMC_COMMON, TYPE_SDMMC_COMMON)#define EXT_CSD_PART_CONFIG_ACC_DEFAULT (0x0)#define EXT_CSD_PART_CONFIG_ACC_BOOT1 (0x1)#define EXT_CSD_PART_CONFIG_ACC_BOOT2 (0x2) +#define EXT_CSD_PART_CONFIG_ACC_RPMB (0x3)#define EXT_CSD_PART_CONFIG_EN_MASK (0x7 << 3)#define EXT_CSD_PART_CONFIG_EN_BOOT0 (0x1 << 3)#define EXT_CSD_PART_CONFIG_EN_USER (0x7 << 3) +#define RPMB_REQ_PROGRAM_AUTH_KEY (1) +#define RPMB_REQ_READ_WRITE_COUNTER (2) +#define RPMB_REQ_AUTH_DATA_WRITE (3) +#define RPMB_REQ_AUTH_DATA_READ (4) +#define RPMB_REQ_READ_RESULT (5) +#define RPMB_REQ_AUTH_CONFIG_WRITE (6) +#define RPMB_REQ_AUTH_CONFIG_READ (7) + +#define RPMB_RESP (__req__) ((__req__) << 8) + +#define RPMB_RESULT_OK (0) +#define RPMB_RESULT_GENERAL_FAILURE (1) +#define RPMB_RESULT_AUTH_FAILURE (2) +#define RPMB_RESULT_COUNTER_FAILURE (3) +#define RPMB_RESULT_ADDRESS_FAILURE (4) +#define RPMB_RESULT_WRITE_FAILURE (5) +#define RPMB_RESULT_READ_FAILURE (6) +#define RPMB_RESULT_NO_AUTH_KEY (7) + +#define RPMB_RESULT_COUTER_EXPIRED (0x80) +#endif
hw/sd/sd.c +209 -8
335 lines not on this page · raw message
hw/sd/trace-events +2 -0
@@ -59,6 +59,8 @@ sdcard_read_data(const char *proto, const char *cmd_desc, uint8_t cmd, uint32_t sdcard_set_voltage(uint16_t millivolts) "%u mV" sdcard_ext_csd_update(unsigned index, uint8_t oval, uint8_t nval) "index %u: 0x%02x -> 0x%02x" sdcard_switch(unsigned access, unsigned index, unsigned value, unsigned set) "SWITCH acc:%u idx:%u val:%u set:%u" +sdcard_rpmb_read_block(uint16_t resp, uint16_t read_addr, uint16_t result) "resp 0x%x read_addr 0x%x result 0x%x" +sdcard_rpmb_write_block(uint16_t req, uint16_t result) "req 0x%x result 0x%x" # pl181.c pl181_command_send(uint8_t cmd, uint32_t arg) "sending CMD%02d arg 0x%08" PRIx32
Signature · 1 line
2.51.0
[PULL 6/8] docs: Add eMMC device model description
In reply to Philippe Mathieu-DaudéFrom: Jan Kiszka <[email protected]>
Message-ID: <b9c3ff21e7170fef5d0e7d08698a113d2a64e649.1762261430.git.jan.kiszka@siemens.com>
--- docs/system/device-emulation.rst | 1 + docs/system/devices/emmc.rst | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 docs/system/devices/emmc.rst
docs/system/device-emulation.rst +1 -0
@@ -88,6 +88,7 @@ Emulated Devices devices/canokey.rst devices/ccid.rst devices/cxl.rst + devices/emmc.rst devices/igb.rst devices/ivshmem-flat.rst devices/ivshmem.rst
docs/system/devices/emmc.rst new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.51.0
[PULL 8/8] hw/audio: Remove dead code from ac97_realize
In reply to Philippe Mathieu-DaudéFrom: Osama Abdelkader <[email protected]>
Remove unnecessary PCI configuration register initialization that was marked with TODO comments indicating it's redundant:
- PCI_COMMAND register is already 0x00 by default, no need to override
- PCI_BASE_ADDRESS_0 registers are automatically set by pci_register_bar()
This aligns the code with the pattern used by other PCI audio devices in QEMU (via-ac97, intel-hda, es1370) and removes 15 lines of dead code.
Message-ID: <[email protected]>
--- hw/audio/ac97.c | 19 ------------------- 1 file changed, 19 deletions(-)
hw/audio/ac97.c +0 -19
@@ -1279,30 +1279,11 @@ static void ac97_realize(PCIDevice *dev, Error **errp)return ; } -/* TODO: no need to override */ - c[PCI_COMMAND ] =0x00 ;/* pcicmd pci command rw, ro */ - c[PCI_COMMAND +1 ] =0x00 ; - -/* TODO: */ c[PCI_STATUS ] =PCI_STATUS_FAST_BACK ;/* pcists pci status rwc, ro */ c[PCI_STATUS +1 ] =PCI_STATUS_DEVSEL_MEDIUM >>8 ; c[PCI_CLASS_PROG ] =0x00 ;/* pi programming interface ro */ -/* TODO set when bar is registered. no need to override. */ -/* nabmar native audio mixer base address rw */ - c[PCI_BASE_ADDRESS_0 ] =PCI_BASE_ADDRESS_SPACE_IO ; - c[PCI_BASE_ADDRESS_0 +1 ] =0x00 ; - c[PCI_BASE_ADDRESS_0 +2 ] =0x00 ; - c[PCI_BASE_ADDRESS_0 +3 ] =0x00 ; - -/* TODO set when bar is registered. no need to override. */ -/* nabmbar native audio bus mastering base address rw */ - c[PCI_BASE_ADDRESS_0 +4 ] =PCI_BASE_ADDRESS_SPACE_IO ; - c[PCI_BASE_ADDRESS_0 +5 ] =0x00 ; - c[PCI_BASE_ADDRESS_0 +6 ] =0x00 ; - c[PCI_BASE_ADDRESS_0 +7 ] =0x00 ; - c[PCI_INTERRUPT_LINE ] =0x00 ;/* intr_ln interrupt line rw */ c[PCI_INTERRUPT_PIN ] =0x01 ;/* intr_pn interrupt pin ro */
Signature · 1 line
2.51.0
[PULL 4/8] hw/sd/sdcard: Handle RPMB MAC field
In reply to Philippe Mathieu-DaudéFrom: Jan Kiszka <[email protected]>
Implement correct setting of the MAC field when passing RPMB frames back to the guest. Also check the MAC on authenticated write requests.
This depends on HMAC support for QCRYPTO_HASH_ALGO_SHA256 which is always available via glib - assert this, just to be safe.
Message-ID: <b6f5698c0ca017871d54834f0c7bd4b4b6316bbd.1762261430.git.jan.kiszka@siemens.com>
--- hw/sd/sd.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
hw/sd/sd.c +82 -0
@@ -51,6 +51,7 @@#include "qemu/module.h" #include "sdmmc-internal.h" #include "trace.h" +#include "crypto/hmac.h" //#define DEBUG_SD 1
@@ -121,6 +122,7 @@ typedef struct SDProto {#define RPMB_KEY_MAC_LEN 32#define RPMB_DATA_LEN 256/* one RPMB block is half a sector */ #define RPMB_NONCE_LEN 16 +#define RPMB_HASH_LEN 284 typedef struct QEMU_PACKED { uint8_t stuff_bytes[RPMB_STUFF_LEN];
@@ -1128,6 +1130,67 @@ static void sd_blk_write(SDState *sd, uint64_t addr, uint32_t len) } } +static bool rpmb_calc_hmac (SDState *sd,const RPMBDataFrame *frame, +unsigned int num_blocks,uint8_t *mac) +{ +g_autoptr (QCryptoHmac) hmac =NULL ; +size_t mac_len =RPMB_KEY_MAC_LEN ; +bool success = true; +Error *err =NULL ; +uint64_t offset; + + hmac =qcrypto_hmac_new (QCRYPTO_HASH_ALGO_SHA256 , sd->rpmb.key, +RPMB_KEY_MAC_LEN , &err); +if (!hmac) { +error_report_err (err); +return false; + } + +/* +* This implies a read request because we only support single-block write +* requests so far. +*/ +if (num_blocks >1 ) { +/* +* Unfortunately, the underlying crypto libraries do not allow us to +* migrate an active QCryptoHmac state. Therefore, we have to calculate +* the HMAC in one run. To avoid buffering a complete read sequence in +* SDState, reconstruct all frames except for the last one. +*/ +void *buf = sd->data; + +assert (RPMB_HASH_LEN <=sizeof (sd->data)); + +memcpy ((uint8_t *)buf +RPMB_DATA_LEN , &frame->data[RPMB_DATA_LEN ], +RPMB_HASH_LEN -RPMB_DATA_LEN ); + offset =lduw_be_p (&frame->address) *RPMB_DATA_LEN +sd_part_offset (sd); +do { +if (blk_pread (sd->blk, offset,RPMB_DATA_LEN , buf,0 ) <0 ) { +error_report ("sd_blk_read: read error on host side" ); + success = false; +break ; + } +if (qcrypto_hmac_bytes (hmac, buf,RPMB_HASH_LEN ,NULL ,NULL , + &err) <0 ) { +error_report_err (err); + success = false; +break ; + } + offset +=RPMB_DATA_LEN ; + }while (--num_blocks >1 ); + } + +if (success && +qcrypto_hmac_bytes (hmac, frame->data,RPMB_HASH_LEN , &mac, + &mac_len, &err) <0 ) { +error_report_err (err); + success = false; + } +assert (!success || mac_len ==RPMB_KEY_MAC_LEN ); + +return success; +} +static void emmc_rpmb_blk_read (SDState *sd,uint64_t addr,uint32_t len) {uint16_t resp =lduw_be_p (&sd->rpmb.result.req_resp);
@@ -1152,6 +1215,17 @@ static void emmc_rpmb_blk_read(SDState *sd, uint64_t addr, uint32_t len)RPMB_RESULT_READ_FAILURE | (result & RPMB_RESULT_COUTER_EXPIRED)); } +if (sd->multi_blk_cnt ==1 && + !rpmb_calc_hmac (sd, &sd->rpmb.result, +lduw_be_p (&sd->rpmb.result.block_count), + sd->rpmb.result.key_mac)) { +memset (sd->rpmb.result.data,0 ,sizeof (sd->rpmb.result.data)); +stw_be_p (&sd->rpmb.result.result,RPMB_RESULT_AUTH_FAILURE ); + } + }else if (!rpmb_calc_hmac (sd, &sd->rpmb.result,1 , + sd->rpmb.result.key_mac)) { +memset (sd->rpmb.result.data,0 ,sizeof (sd->rpmb.result.data)); +stw_be_p (&sd->rpmb.result.result,RPMB_RESULT_AUTH_FAILURE ); }memcpy (sd->data, &sd->rpmb.result,sizeof (sd->rpmb.result));
@@ -1163,6 +1237,7 @@ static void emmc_rpmb_blk_write(SDState *sd, uint64_t addr, uint32_t len) {RPMBDataFrame *frame = (RPMBDataFrame *)sd->data;uint16_t req =lduw_be_p (&frame->req_resp); +uint8_t mac[RPMB_KEY_MAC_LEN ];if (req ==RPMB_REQ_READ_RESULT ) {/* just return the current result register */
@@ -1200,6 +1275,11 @@ static void emmc_rpmb_blk_write(SDState *sd, uint64_t addr, uint32_t len)stw_be_p (&sd->rpmb.result.result,RPMB_RESULT_WRITE_FAILURE );break ; } +if (!rpmb_calc_hmac (sd, frame,1 , mac) || +memcmp (frame->key_mac, mac,RPMB_KEY_MAC_LEN ) !=0 ) { +stw_be_p (&sd->rpmb.result.result,RPMB_RESULT_AUTH_FAILURE ); +break ; + }if (ldl_be_p (&frame->write_counter) != sd->rpmb.write_counter) {stw_be_p (&sd->rpmb.result.result,RPMB_RESULT_COUNTER_FAILURE );break ;
@@ -3128,6 +3208,8 @@ static void emmc_class_init(ObjectClass *klass, const void *data)DeviceClass *dc =DEVICE_CLASS (klass);SDCardClass *sc =SDMMC_COMMON_CLASS (klass); +assert (qcrypto_hmac_supports (QCRYPTO_HASH_ALGO_SHA256 )); + dc->desc ="eMMC" ; dc->realize = emmc_realize;device_class_set_props (dc, emmc_properties);
Signature · 1 line
2.51.0
[PULL 7/8] hw/i386/microvm: Use fdt field from MachineState
In reply to Philippe Mathieu-DaudéFrom: Luigi Leonardi <[email protected]>
MachineState already provides an fdt field, remove it from MicrovmMachineState and use that instead.
This is useful when using the dumpdtb option, as QEMU expects the device tree to be stored in the MachineState struct, otherwise it will return this error:
qemu-system-x86_64: This machine doesn't have an FDT
Message-ID: <[email protected]>
--- include/hw/i386/microvm.h | 2 - hw/i386/microvm-dt.c | 96 +++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 46 deletions(-)
include/hw/i386/microvm.h +0 -2
@@ -102,8 +102,6 @@ struct MicrovmMachineState {Notifier powerdown_req;struct GPEXConfig gpex; -/* device tree */ -void *fdt;uint32_t ioapic_phandle[2 ]; };
hw/i386/microvm-dt.c +52 -44
216 lines not on this page · raw message
Signature · 1 line
2.51.0
Hi,
Quoted 9 lines · Philippe Mathieu-Daudé
The following changes since commit
e9c692eabbbb7f395347605a6ef33a32d398ea25:Merge tag 'next-pr-pull-request' of https://gitlab.com/berrange/qemu into staging (2025-11-04 15:17:31 +0100)are available in the Git repository at:
https://github.com/philmd/qemu.git tags/hw-misc-20251104for you to fetch changes up to
f79fec0275ee7a5151ced9b893c4af676c7f5db6:
I missed Cédric Le Goater provided his Tested-by tag via IRC, so amended to the relevant commits and regenerated the tag (no other change). New tag hash is 1eb18789f8863e7ee89c1dc06b2de806442b4927.
Note I also ignore this checkpatch error:
ERROR: line over 90 characters
#242: FILE: scripts/mkemmc.sh:216:
+echo " -device emmc,boot-partition-size=$bootsz,rpmb-partition-size=$rpmbsz,drive=emmc-img"
Quoted 7 lines
----------------------------------------------------------------
Misc HW patches- Add RPMB emulation to eMMC model :)
- Use genericMachineState::fdtfield in microvm machine
- Remove dead code inac97_realize()
----------------------------------------------------------------
Regards,
Phil.
Richard Henderson
AppliedQuoted 26 lines · Philippe Mathieu-Daudé
Hi,
Quoted 9 lines · Philippe Mathieu-Daudé
The following changes since commit
e9c692eabbbb7f395347605a6ef33a32d398ea25:Merge tag 'next-pr-pull-request' of https://gitlab.com/berrange/qemu into staging (2025-11-04 15:17:31 +0100)
are available in the Git repository at:
https://github.com/philmd/qemu.git tags/hw-misc-20251104
for you to fetch changes up to
f79fec0275ee7a5151ced9b893c4af676c7f5db6:I missed Cédric Le Goater provided his Tested-by tag via IRC, so amended to the relevant commits and regenerated the tag (no other change). New tag hash is
1eb18789f8863e7ee89c1dc06b2de806442b4927.Note I also ignore this checkpatch error:
ERROR: line over 90 characters
#242: FILE:scripts/mkemmc.sh:216:
+echo " -device emmc,boot-partition-size=$bootsz,rpmb-partition-size=$rpmbsz,drive=emmc-img"Quoted 3 lines
----------------------------------------------------------------
Misc HW patches
Quoted 7 lines
Quoted 4 lines
- Add RPMB emulation to eMMC model :)
- Use genericMachineState::fdtfield in microvm machine
- Remove dead code inac97_realize()
----------------------------------------------------------------Regards,
Phil.
Applied, thanks. Please update https://wiki.qemu.org/ChangeLog/10.2 as appropriate.
r~