From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793080963; cv=none; d=zohomail.com; s=zohoarc; b=KB6u3XHy5loI3MnikrL+X44Z8wkUuEo08f1LyoY0eBHGgCu6nR8jikV0dpNO2WPvFQLFZZWV2oRuyac6QJQ10MSadp8vXCeqWNJro906M1bvBvRe0JQfhsFhCKqy6oI9U2J4ThqDHbsdHOKdmQtFnNHnqXQVK6qmESbqxMUDfXA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793080963; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=MvEx57NIAgJI0ijP3mUpfKJH9oHR1otTCVEVwZ8ExXE=; b=Z+JllNW93iFwGyqaGuJJnqLNzFyHPg03MqP24pny/4XjBObLir0PmnGXQt5JzWi16ZA2uP9B4p+PosPrLlhiVvjpcF+7XYXWl70JpPHYWVWnRVfHUnsR/OnY0IOJlNOwZQuHb7i1lShXwQG9ctgDqJry7Isz/cu3JNJEC1Yq8wc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793080963577939.0891516385358; Mon, 26 Oct 2026 23:02:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaGM-0007rw-SR; Tue, 27 Oct 2026 02:02:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3VD7gagoKCq8hiPcaTnYWjVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--stanleyjhu.bounces.google.com>) id 1xLaG3-0007pb-2A for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:03 -0400 Received: from mail-pg1-x545.google.com ([2607:f8b0:4864:20::545]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3VD7gagoKCq8hiPcaTnYWjVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--stanleyjhu.bounces.google.com>) id 1xLaG0-00067X-EI for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:02 -0400 Received: by mail-pg1-x545.google.com with SMTP id 41be03b00d2f7-cd68b8579f2so1473324a12.3 for ; Mon, 26 Oct 2026 23:01:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080917; x=1793685717; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=MvEx57NIAgJI0ijP3mUpfKJH9oHR1otTCVEVwZ8ExXE=; b=AMt5qbDeislEFSuMUpj9fgN9bOK7CXZ6+o0cSA7HoPAYBuufT05Cnas6vpGWpDPLIn rvCvVg6NDpcvp+KR+fdkdEWZ8aelMsOR9BSObqzHqWudD5iGKrIna4/ci3yYtv07qykp bAedDPE218qsp8ZKHyOSrJsjlz0d6E3YEWkpXFl9LNDkFCtsY4Xr1yiIYv+I3XMDOrkP vLDDvPEgiIXPRyWTxJQ13nrDabQmWfLFkoInef1zyFLjfrkAFIDowyHKSoD1q/RisgKZ 3mr7H84JoF4vbXdfh9Tkdk3BlvCkRbmvETYtWfvH3B1zdxVuz0kWpGmeKgyGoGAWUH4Y ERHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080917; x=1793685717; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MvEx57NIAgJI0ijP3mUpfKJH9oHR1otTCVEVwZ8ExXE=; b=r6fdUKImZCM2FDHnZ7/xb1meG8MhLJCuOYXdUxgpAOhHM2Bu28nU7mxBnnYZotawYY 5+/J7+ktKabvSOmMG7mUEsOuwB/HWymJZiOVaOe6J4Wn2s0IAqfP3FBuAt0aM3uPqn6/ a/27Y+S+TCbVIfa41gJXxWjMVNcflbYYuHW39XBGXMLYmPuAdkvgGrXzqerUGz6ddZfs 6k76Pcwr0XOCMeYDq5DqwbaLIOIMKcrXTKtVA0AN0hjUX+COvWOMZJ5EGyPyd1lOvc0x lEl2I4POJM8EhBvkXuuMPZZZCLCypuJrf0+ITPBgpwU6rIOtNNfLwdKVjthQhl1+8Icm 1RvA== X-Gm-Message-State: AFq9FYJUGvDmUXC3vbmNe4OTtJVxOFlfGJbdoeI6x7xTuHEKLbWV/VZy J4L7WP5ko3CFEeLhuWR8h/f1z79nmVsbmNQo23DYKeHfrf+AtvEU2sClFiakHCRq4JQ1Ez7FBh4 TgJUQTU318kxrnK3Arn+7AQ== X-Received: from pfblc1.prod.google.com ([2002:a05:6a00:4f41:b0:89e:65fd:e92]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:3c92:b0:886:9078:546 with SMTP id d2e1a72fcca58-8a366c7db93mr6784850b3a.26.1793080916730; Mon, 26 Oct 2026 23:01:56 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:46 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-2-stanleyjhu@google.com> Subject: [PATCH 1/7] include/scsi, block/ufs: Add security protocol and RPMB definitions From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::545; envelope-from=3VD7gagoKCq8hiPcaTnYWjVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--stanleyjhu.bounces.google.com; helo=mail-pg1-x545.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793080966342158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" UFS Replay Protected Memory Block (RPMB) accesses are transported over SCSI SECURITY PROTOCOL IN (0xa2) and SECURITY PROTOCOL OUT (0xb5) commands directed to the RPMB well-known logical unit (W-LUN 0xc4). Add SECURITY_PROTOCOL_IN and SECURITY_PROTOCOL_OUT in numeric order to include/scsi/constants.h, update RpmbUnitDescriptor to expose bRPMBRegionEnable and bRPMBRegion0Size..bRPMBRegion3Size per JESD220H Table 14.15 while preserving its 35-byte packed layout, and define the 512-byte UfsRpmbFrame structure, geometry constants, request/response types, and operation result codes from JESD220H Section 12.4. Signed-off-by: Stanley Jhu --- include/block/ufs.h | 58 +++++++++++++++++++++++++++++++++++++--- include/scsi/constants.h | 2 ++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/include/block/ufs.h b/include/block/ufs.h index a0bbe873d6..339c3bf5b4 100644 --- a/include/block/ufs.h +++ b/include/block/ufs.h @@ -387,6 +387,10 @@ typedef struct QEMU_PACKED UnitDescriptor { uint32_t lu_num_write_booster_buffer_alloc_units; } UnitDescriptor; =20 +#define UFS_RPMB_MAX_REGIONS 4 +#define UFS_RPMB_LOGICAL_BLOCK_SIZE 8 /* 2^8 =3D 256 bytes */ +#define UFS_RPMB_BLOCKS_PER_UNIT 512 /* 128 KiB / 256 B */ + typedef struct QEMU_PACKED RpmbUnitDescriptor { uint8_t length; uint8_t descriptor_idn; @@ -397,15 +401,62 @@ typedef struct QEMU_PACKED RpmbUnitDescriptor { uint8_t lu_queue_depth; uint8_t psa_sensitive; uint8_t memory_type; - uint8_t reserved; + uint8_t rpmb_region_enable; uint8_t logical_block_size; uint64_t logical_block_count; - uint32_t erase_block_size; + uint8_t rpmb_region_size[UFS_RPMB_MAX_REGIONS]; uint8_t provisioning_type; uint64_t phy_mem_resource_count; - uint8_t reserved2[3]; + uint8_t reserved[3]; } RpmbUnitDescriptor; =20 +#define UFS_RPMB_FRAME_LEN 512 +#define UFS_RPMB_DATA_LEN 256 +#define UFS_RPMB_KEY_MAC_LEN 32 +#define UFS_RPMB_NONCE_LEN 16 +#define UFS_RPMB_HASH_OFFSET 228 +#define UFS_RPMB_HASH_LEN (UFS_RPMB_FRAME_LEN - UFS_RPMB_HASH_OFFSET) + +#define UFS_RPMB_SEC_PROTOCOL 0xec +#define UFS_RPMB_SEC_PROTOCOL_ID 0x01 + +enum UfsRpmbRequest { + UFS_RPMB_REQ_PROGRAM_KEY =3D 0x0001, + UFS_RPMB_REQ_READ_COUNTER =3D 0x0002, + UFS_RPMB_REQ_AUTH_DATA_WRITE =3D 0x0003, + UFS_RPMB_REQ_AUTH_DATA_READ =3D 0x0004, + UFS_RPMB_REQ_READ_RESULT =3D 0x0005, + + UFS_RPMB_RESP_PROGRAM_KEY =3D 0x0100, + UFS_RPMB_RESP_READ_COUNTER =3D 0x0200, + UFS_RPMB_RESP_AUTH_DATA_WRITE =3D 0x0300, + UFS_RPMB_RESP_AUTH_DATA_READ =3D 0x0400, +}; + +enum UfsRpmbResult { + UFS_RPMB_RES_OK =3D 0x0000, + UFS_RPMB_RES_GENERAL_FAILURE =3D 0x0001, + UFS_RPMB_RES_AUTH_FAILURE =3D 0x0002, + UFS_RPMB_RES_COUNT_FAILURE =3D 0x0003, + UFS_RPMB_RES_ADDR_FAILURE =3D 0x0004, + UFS_RPMB_RES_WRITE_FAILURE =3D 0x0005, + UFS_RPMB_RES_READ_FAILURE =3D 0x0006, + UFS_RPMB_RES_NO_AUTH_KEY =3D 0x0007, + UFS_RPMB_RES_COUNT_EXPIRED =3D 0x0080, +}; + +typedef struct QEMU_PACKED UfsRpmbFrame { + uint8_t stuff[196]; + uint8_t key_mac[UFS_RPMB_KEY_MAC_LEN]; + uint8_t data[UFS_RPMB_DATA_LEN]; + uint8_t nonce[UFS_RPMB_NONCE_LEN]; + uint32_t write_counter; + uint16_t addr; + uint16_t block_count; + uint16_t result; + uint16_t req_resp; +} UfsRpmbFrame; + typedef struct QEMU_PACKED PowerParametersDescriptor { uint8_t length; uint8_t descriptor_idn; @@ -1397,6 +1448,7 @@ static inline void _ufs_check_size(void) QEMU_BUILD_BUG_ON(sizeof(GeometryDescriptor) !=3D 105); QEMU_BUILD_BUG_ON(sizeof(UnitDescriptor) !=3D 45); QEMU_BUILD_BUG_ON(sizeof(RpmbUnitDescriptor) !=3D 35); + QEMU_BUILD_BUG_ON(sizeof(UfsRpmbFrame) !=3D UFS_RPMB_FRAME_LEN); QEMU_BUILD_BUG_ON(sizeof(PowerParametersDescriptor) !=3D 98); QEMU_BUILD_BUG_ON(sizeof(InterconnectDescriptor) !=3D 6); QEMU_BUILD_BUG_ON(sizeof(StringDescriptor) !=3D 254); diff --git a/include/scsi/constants.h b/include/scsi/constants.h index bb3981a779..5a738e89bf 100644 --- a/include/scsi/constants.h +++ b/include/scsi/constants.h @@ -127,6 +127,7 @@ #define WRITE_LONG_16 0x9f #define REPORT_LUNS 0xa0 #define ATA_PASSTHROUGH_12 0xa1 +#define SECURITY_PROTOCOL_IN 0xa2 #define MAINTENANCE_IN 0xa3 #define MAINTENANCE_OUT 0xa4 #define MOVE_MEDIUM 0xa5 @@ -142,6 +143,7 @@ #define SEARCH_HIGH_12 0xb0 #define SEARCH_EQUAL_12 0xb1 #define SEARCH_LOW_12 0xb2 +#define SECURITY_PROTOCOL_OUT 0xb5 #define READ_ELEMENT_STATUS 0xb8 #define SEND_VOLUME_TAG 0xb6 #define READ_DEFECT_DATA_12 0xb7 --=20 2.56.0.307.g5fe7bd680c-goog From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793080998; cv=none; d=zohomail.com; s=zohoarc; b=fYj9KlqugUGAO802nEOsHzg96/PIaAmem0Ki4rgCVMRoUK79yFSjsYDQjD0VM35s+kuCFvCPoUuEJ9oPELZEzEtw9fB9ro89dt5zIKAiGR7yj/fO4faDOS/iHmLO8KMAX+B8/WFyUhhqm2flU+ZmFjhqoPaT/a06xV5lmSt3d0Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793080998; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=BFY+bA0qsHUbVR6vyGfTOqdgqE3nwT9nGY9B/UCmqYY=; b=gZEQo/J8WMERZA621Clz4dNwHvC2QW79X5s7GuK11lkmqZ6nyRhs3usteqAxBYVrK9rPvkEYjQwNBSyPEZU+Ct7ombZR7jYARpBnHG45znjYJ+6tt9fOM1oGs5GK7WH+7dgobvja6ZENdqjIhJX9GvR1b5l3upBIDsJ0ZI9S+N4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793080998739111.41244660581901; Mon, 26 Oct 2026 23:03:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaH7-00082z-Sa; Tue, 27 Oct 2026 02:03:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3Vj7gagoKCrEjkRecVpaYlXffXcV.TfdhVdl-UVmVcefeXel.fiX@flex--stanleyjhu.bounces.google.com>) id 1xLaG3-0007pe-NI for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:06 -0400 Received: from mail-pf1-x446.google.com ([2607:f8b0:4864:20::446]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3Vj7gagoKCrEjkRecVpaYlXffXcV.TfdhVdl-UVmVcefeXel.fiX@flex--stanleyjhu.bounces.google.com>) id 1xLaG0-00067x-Vg for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:03 -0400 Received: by mail-pf1-x446.google.com with SMTP id d2e1a72fcca58-8a01ea9e318so5378171b3a.2 for ; Mon, 26 Oct 2026 23:02:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080919; x=1793685719; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=BFY+bA0qsHUbVR6vyGfTOqdgqE3nwT9nGY9B/UCmqYY=; b=UlPK6VtXReqCJPfQTQ3UeJWB3LqcKsw6czr/rZPGf+eCbIIXlXpHOF8IsfVJ4l+cLC 7xk5JdXri/xfEyzZovu8Mi/dRhjAzSvU1WyBUFVkSiPj8c8x7CF210w46PInC2U30D0j Wm9P9bbBQoeOfN+cqR5MJMCIciDnyHWSC+hDkEPVyuWs942QfaLJox267iLFC+iHjLie 5u76mcXPdeQlHiqTkAEHsletSYRa0FbxocYRwPzs9rhVAzPdeofaO5mB8zlRZnP7NHDb ttbkAvMbnlyryALAgR1A6Zore251l3g9Y/Gk7sj6YPl43jzhQ/tSZl6IXd/puD3pDkWY C29A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080919; x=1793685719; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=BFY+bA0qsHUbVR6vyGfTOqdgqE3nwT9nGY9B/UCmqYY=; b=AXSpNenOVqj8KluNezn/KsGprplVs99/YRHs2BB4j9icW63fwpRiq9Hm5TsaUT81XD 9e/egSZxC2jPJI+Lo6srkX+yfNcyDmcDL2YC0CzFcpN6BGOWrG9cCTFsyNEh1kQX7u+d MwJLpoBHk9bcsz28aEYlNAZsGKrU5KWDylR32R/K0GsPcLcvRAYd+HkwCJ1tDFkCgbJP 6wyT/ZmUhMFU2Uavoiz1NhtugmA8SFwNlyuq838TegvBsy8uavckAUbADv4ifHGMRU3F hTouqhDiXH28VdGyhL9YiosxEyt/7VK+a2RhK5+bdy8M9XF4ypmH6stWyR7uB2d4R9oQ 7BhA== X-Gm-Message-State: AFq9FYIM32t8p5HI8qv1rG+uT+lYpyCkySZMUhEz35BDYgpoq4owvzb5 3DwUQJ5WGd9BkNz+wHxt4Ujtsptc7eiuG/BWxZ7IreqhXAkvnkZnrDyqcKGqNcZ+pe54Yln0O8B H5pMa/U5CqFb04Zhqj5WBdg== X-Received: from pfbcb8.prod.google.com ([2002:a05:6a00:4308:b0:8a0:8d65:ebfc]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:12d6:b0:89f:7ac5:9d4f with SMTP id d2e1a72fcca58-8a364e5d92dmr7137893b3a.2.1793080918670; Mon, 26 Oct 2026 23:01:58 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:47 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-3-stanleyjhu@google.com> Subject: [PATCH 2/7] hw/ufs: Add RPMB unit descriptor and SECURITY PROTOCOL IN/OUT dispatch From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::446; envelope-from=3Vj7gagoKCrEjkRecVpaYlXffXcV.TfdhVdl-UVmVcefeXel.fiX@flex--stanleyjhu.bounces.google.com; helo=mail-pf1-x446.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793081000706158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The UFS device model creates the RPMB well-known logical unit (W-LUN 0xc4), but leaves bLUEnable set to 0 in RpmbUnitDescriptor, leaves bSecurityLU set to 0 in DeviceDescriptor, and rejects SECURITY PROTOCOL IN and SECURITY PROTOCOL OUT commands with ILLEGAL REQUEST. Set bSecurityLU to 0x01 in DeviceDescriptor (JESD220H Table 14.4), enable four 4 MiB RPMB regions (32 * 128 KiB each, 256-byte logical blocks) in RpmbUnitDescriptor, and populate the big-endian block count fields during unit descriptor reads using shared UFS_RPMB_* geometry constants. Add per-region state in UfsHc with an embedded Result Register frame, wire SECURITY PROTOCOL IN and SECURITY PROTOCOL OUT CDB validation, PRDT buffer capacity validation via ufs_fit_data_buffer(), and Result Read (0x0005) handling in hw/ufs/rpmb.c per JESD220H Section 12.4.6.1, and clear volatile RPMB state in ufs_hce_reset(). Requests not yet implemented return UFS_RPMB_RES_GENERAL_FAILURE (0x0001). Signed-off-by: Stanley Jhu --- hw/ufs/lu.c | 34 +++++++++ hw/ufs/meson.build | 2 +- hw/ufs/rpmb.c | 170 ++++++++++++++++++++++++++++++++++++++++++++ hw/ufs/trace-events | 3 + hw/ufs/ufs.c | 21 +++++- hw/ufs/ufs.h | 24 +++++++ 6 files changed, 250 insertions(+), 4 deletions(-) create mode 100644 hw/ufs/rpmb.c diff --git a/hw/ufs/lu.c b/hw/ufs/lu.c index 4fe1247547..0e99ec5f7b 100644 --- a/hw/ufs/lu.c +++ b/hw/ufs/lu.c @@ -1137,6 +1137,40 @@ static UfsReqResult ufs_emulate_scsi_cmd(UfsLu *lu, = UfsRequest *req) true); scsi_status =3D GOOD; break; + case SECURITY_PROTOCOL_IN: + if (lun =3D=3D UFS_UPIU_RPMB_WLUN) { + uint32_t transferred_len =3D 0; + UfsReqResult ret; + + ret =3D ufs_rpmb_sec_in(req->hc, req, &scsi_status, sense_buf, + &transferred_len); + if (ret !=3D UFS_REQUEST_SUCCESS) { + return ret; + } + ufs_build_scsi_response_upiu(req, sense_buf, sizeof(sense_buf), + transferred_len, scsi_status); + return UFS_REQUEST_SUCCESS; + } + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_OPCODE)); + scsi_status =3D CHECK_CONDITION; + break; + case SECURITY_PROTOCOL_OUT: + if (lun =3D=3D UFS_UPIU_RPMB_WLUN) { + uint32_t transferred_len =3D 0; + UfsReqResult ret; + + ret =3D ufs_rpmb_sec_out(req->hc, req, &scsi_status, sense_buf, + &transferred_len); + if (ret !=3D UFS_REQUEST_SUCCESS) { + return ret; + } + ufs_build_scsi_response_upiu(req, sense_buf, sizeof(sense_buf), + transferred_len, scsi_status); + return UFS_REQUEST_SUCCESS; + } + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_OPCODE)); + scsi_status =3D CHECK_CONDITION; + break; case START_STOP: /* TODO: Revisit it when Power Management is implemented */ if (lun =3D=3D UFS_UPIU_UFS_DEVICE_WLUN) { diff --git a/hw/ufs/meson.build b/hw/ufs/meson.build index a84627b96a..9e5e6ad913 100644 --- a/hw/ufs/meson.build +++ b/hw/ufs/meson.build @@ -1,3 +1,3 @@ -system_ss.add(when: 'CONFIG_UFS', if_true: files('ufs.c', 'lu.c')) +system_ss.add(when: 'CONFIG_UFS', if_true: files('ufs.c', 'lu.c', 'rpmb.c'= )) system_ss.add(when: 'CONFIG_UFS_PCI', if_true: files('ufs-pci.c')) system_ss.add(when: 'CONFIG_UFS_SYSBUS', if_true: files('ufs-sysbus.c')) diff --git a/hw/ufs/rpmb.c b/hw/ufs/rpmb.c new file mode 100644 index 0000000000..3032e0e098 --- /dev/null +++ b/hw/ufs/rpmb.c @@ -0,0 +1,170 @@ +/* + * QEMU UFS RPMB Well-Known Logical Unit Emulation + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "trace.h" +#include "ufs.h" + +static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion *r, + const UfsRpmbFrame *frames, + uint32_t nframes) +{ + const UfsRpmbFrame *req =3D &frames[0]; + uint16_t req_type =3D lduw_be_p(&req->req_resp); + uint16_t result =3D UFS_RPMB_RES_GENERAL_FAILURE; + + if (req_type =3D=3D UFS_RPMB_REQ_READ_RESULT) { + trace_ufs_rpmb_request(req_type, lduw_be_p(&r->resp.result)); + return; + } + + r->read_pending =3D false; + memset(&r->resp, 0, sizeof(r->resp)); + + switch (req_type) { + default: + r->resp.req_resp =3D cpu_to_be16(req_type); + result =3D UFS_RPMB_RES_GENERAL_FAILURE; + break; + } + + if (r->write_counter =3D=3D 0xffffffff) { + result |=3D UFS_RPMB_RES_COUNT_EXPIRED; + } + r->resp.result =3D cpu_to_be16(result); + + trace_ufs_rpmb_request(req_type, result); +} + +static bool ufs_rpmb_parse_cdb(const uint8_t *cdb, uint8_t *region_out, + uint32_t *xfer_len_out, uint8_t *sense_buf) +{ + uint8_t sec_proto =3D cdb[1]; + uint16_t spsp =3D lduw_be_p(&cdb[2]); + uint8_t region =3D (spsp >> 8) & 0xff; + uint8_t proto_id =3D spsp & 0xff; + bool inc_512 =3D (cdb[4] & 0x80) !=3D 0; + + /* + * JESD220H Section 12.4.6.1 (Table 12.14) and Section 12.4.7.1/.2 + * (Tables 12.16/12.17; JESD220F Tables 12.21/12.22): when SECURITY + * PROTOCOL is ECh, INC_512 (CDB byte 4 bit 7) shall be set to 0; + * otherwise terminate with CHECK CONDITION / ILLEGAL REQUEST / + * INVALID FIELD IN CDB. + */ + if (sec_proto !=3D UFS_RPMB_SEC_PROTOCOL || + proto_id !=3D UFS_RPMB_SEC_PROTOCOL_ID || + region >=3D UFS_RPMB_MAX_REGIONS || inc_512) { + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); + return false; + } + + *region_out =3D region; + *xfer_len_out =3D ldl_be_p(&cdb[6]); + return true; +} + +void ufs_rpmb_reset(UfsHc *u) +{ + for (int i =3D 0; i < UFS_RPMB_MAX_REGIONS; i++) { + u->rpmb[i].read_pending =3D false; + memset(&u->rpmb[i].resp, 0, sizeof(u->rpmb[i].resp)); + } +} + +UfsReqResult ufs_rpmb_sec_out(UfsHc *u, UfsRequest *req, uint8_t *scsi_sta= tus, + uint8_t *sense_buf, uint32_t *transferred_le= n) +{ + g_autofree UfsRpmbFrame *frames =3D NULL; + dma_addr_t residual =3D 0; + uint8_t region; + uint32_t xfer_len; + uint32_t nframes; + uint8_t max_rw =3D u->geometry_desc.rpmb_read_write_size; + uint16_t req_type; + + *transferred_len =3D 0; + if (!ufs_rpmb_parse_cdb(req->req_upiu.sc.cdb, ®ion, &xfer_len, + sense_buf)) { + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + if (xfer_len =3D=3D 0 || (xfer_len % UFS_RPMB_FRAME_LEN) !=3D 0 || + xfer_len > (uint32_t)max_rw * UFS_RPMB_FRAME_LEN) { + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + if (!ufs_fit_data_buffer(req, xfer_len)) { + return UFS_REQUEST_MISMATCH_DATA_BUF; + } + + nframes =3D xfer_len / UFS_RPMB_FRAME_LEN; + frames =3D g_new0(UfsRpmbFrame, nframes); + if (dma_buf_write(frames, xfer_len, &residual, req->sg, + MEMTXATTRS_UNSPECIFIED) !=3D MEMTX_OK) { + return UFS_REQUEST_FAIL; + } + + req_type =3D lduw_be_p(&frames[0].req_resp); + if (req_type !=3D UFS_RPMB_REQ_AUTH_DATA_WRITE && nframes !=3D 1) { + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + + ufs_rpmb_handle_req(u, &u->rpmb[region], frames, nframes); + *transferred_len =3D xfer_len - residual; + *scsi_status =3D GOOD; + return UFS_REQUEST_SUCCESS; +} + +UfsReqResult ufs_rpmb_sec_in(UfsHc *u, UfsRequest *req, uint8_t *scsi_stat= us, + uint8_t *sense_buf, uint32_t *transferred_len) +{ + UfsRpmbRegion *r; + dma_addr_t residual =3D 0; + uint8_t region; + uint32_t alloc_len; + uint32_t in_frames; + uint8_t max_rw =3D u->geometry_desc.rpmb_read_write_size; + + *transferred_len =3D 0; + if (!ufs_rpmb_parse_cdb(req->req_upiu.sc.cdb, ®ion, &alloc_len, + sense_buf)) { + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + if (alloc_len < UFS_RPMB_FRAME_LEN || + (alloc_len % UFS_RPMB_FRAME_LEN) !=3D 0 || + alloc_len > (uint32_t)max_rw * UFS_RPMB_FRAME_LEN) { + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + + r =3D &u->rpmb[region]; + in_frames =3D alloc_len / UFS_RPMB_FRAME_LEN; + + if (in_frames !=3D 1) { + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + if (!ufs_fit_data_buffer(req, alloc_len)) { + return UFS_REQUEST_MISMATCH_DATA_BUF; + } + + if (dma_buf_read(&r->resp, alloc_len, &residual, req->sg, + MEMTXATTRS_UNSPECIFIED) !=3D MEMTX_OK) { + return UFS_REQUEST_FAIL; + } + + *transferred_len =3D alloc_len - residual; + *scsi_status =3D GOOD; + return UFS_REQUEST_SUCCESS; +} diff --git a/hw/ufs/trace-events b/hw/ufs/trace-events index e1d29d6670..cb7aaa765f 100644 --- a/hw/ufs/trace-events +++ b/hw/ufs/trace-events @@ -67,3 +67,6 @@ ufs_zbc_report_zones(uint8_t lun, uint64_t lba, uint32_t = alloc_len, uint8_t opti ufs_zbc_zone_op(uint8_t lun, uint8_t action, uint64_t zone_id, bool all) "= lun 0x%"PRIx8", action 0x%"PRIx8", zone id %"PRIu64", all %d" ufs_zbc_write_rejected(uint8_t lun, uint64_t lba, uint32_t len, uint64_t w= p, uint8_t cond, uint8_t asc, uint8_t ascq) "lun 0x%"PRIx8", lba %"PRIu64",= len %"PRIu32", wp %"PRIu64", cond 0x%"PRIx8", asc 0x%"PRIx8", ascq 0x%"PRI= x8"" ufs_zbc_write_undone(uint8_t lun, uint64_t zone_start, uint64_t wp) "lun 0= x%"PRIx8", zone start %"PRIu64", wp back to %"PRIu64"" + +# rpmb.c +ufs_rpmb_request(uint16_t req, uint16_t result) "req 0x%04"PRIx16", result= 0x%04"PRIx16"" diff --git a/hw/ufs/ufs.c b/hw/ufs/ufs.c index f8da7c9554..bc0f2ce4eb 100644 --- a/hw/ufs/ufs.c +++ b/hw/ufs/ufs.c @@ -961,6 +961,7 @@ static void ufs_hce_reset(UfsHc *u) scsi_device_purge_requests(u->lus[i]->scsi_dev, SENSE_CODE(RES= ET)); } } + ufs_rpmb_reset(u); =20 /* 3. Reset standard request list slots, doorbells, and status registe= rs */ for (i =3D 0; i < u->params.nutrs; i++) { @@ -2325,9 +2326,18 @@ static QueryRespCode ufs_exec_query_attr(UfsRequest = *req, int op) =20 static const RpmbUnitDescriptor rpmb_unit_desc =3D { .length =3D sizeof(RpmbUnitDescriptor), - .descriptor_idn =3D 2, + .descriptor_idn =3D UFS_QUERY_DESC_IDN_UNIT, .unit_index =3D UFS_UPIU_RPMB_WLUN, - .lu_enable =3D 0, + .lu_enable =3D 0x01, + .memory_type =3D 0x0f, + .rpmb_region_enable =3D (1U << UFS_RPMB_MAX_REGIONS) - 1, + .logical_block_size =3D UFS_RPMB_LOGICAL_BLOCK_SIZE, + .rpmb_region_size =3D { + UFS_RPMB_REGION_UNITS, + UFS_RPMB_REGION_UNITS, + UFS_RPMB_REGION_UNITS, + UFS_RPMB_REGION_UNITS, + }, }; =20 static QueryRespCode ufs_read_unit_desc(UfsRequest *req) @@ -2342,7 +2352,11 @@ static QueryRespCode ufs_read_unit_desc(UfsRequest *= req) } =20 if (lun =3D=3D UFS_UPIU_RPMB_WLUN) { - memcpy(&req->rsp_upiu.qr.data, &rpmb_unit_desc, rpmb_unit_desc.len= gth); + RpmbUnitDescriptor desc =3D rpmb_unit_desc; + + desc.logical_block_count =3D cpu_to_be64(UFS_RPMB_TOTAL_BLOCKS); + desc.phy_mem_resource_count =3D cpu_to_be64(UFS_RPMB_TOTAL_BLOCKS); + memcpy(&req->rsp_upiu.qr.data, &desc, desc.length); } else { memcpy(&req->rsp_upiu.qr.data, &u->lus[lun]->unit_desc, sizeof(u->lus[lun]->unit_desc)); @@ -3162,6 +3176,7 @@ static void ufs_init_hc(UfsHc *u) /* TODO: Revisit it when Power Management is implemented */ u->device_desc.init_power_mode =3D 0x01; /* Active Mode */ u->device_desc.high_priority_lun =3D 0x7F; /* Same Priority */ + u->device_desc.security_lu =3D 0x01; u->device_desc.spec_version =3D cpu_to_be16(UFS_SPEC_VER); u->device_desc.manufacturer_name =3D 0x00; u->device_desc.product_name =3D 0x01; diff --git a/hw/ufs/ufs.h b/hw/ufs/ufs.h index a1e63bd0c8..5120ade082 100644 --- a/hw/ufs/ufs.h +++ b/hw/ufs/ufs.h @@ -245,6 +245,24 @@ typedef struct UfsWb { uint64_t pinned_total_written_bytes; } UfsWb; =20 +/* Each region unit in bRPMBRegionXSize is 128 KiB =3D 512 x 256 B blocks = */ +#define UFS_RPMB_REGION_UNITS 32 +#define UFS_RPMB_REGION_BLOCKS (UFS_RPMB_REGION_UNITS * \ + UFS_RPMB_BLOCKS_PER_UNIT) +#define UFS_RPMB_REGION_BYTES (UFS_RPMB_REGION_BLOCKS * \ + UFS_RPMB_DATA_LEN) +#define UFS_RPMB_TOTAL_BLOCKS (UFS_RPMB_MAX_REGIONS * \ + UFS_RPMB_REGION_BLOCKS) + +typedef struct UfsRpmbRegion { + uint8_t key[UFS_RPMB_KEY_MAC_LEN]; + uint8_t *storage; + uint32_t write_counter; + bool key_set; + bool read_pending; + UfsRpmbFrame resp; +} UfsRpmbRegion; + typedef struct UfsHc { DeviceState *dev; AddressSpace *dma_as; @@ -262,6 +280,7 @@ typedef struct UfsHc { UfsLu dev_wlu; UfsLu boot_wlu; UfsLu rpmb_wlu; + UfsRpmbRegion rpmb[UFS_RPMB_MAX_REGIONS]; DeviceDescriptor device_desc; GeometryDescriptor geometry_desc; Attributes attributes; @@ -412,6 +431,11 @@ void ufs_complete_req(UfsRequest *req, UfsReqResult re= q_result); bool ufs_fit_data_buffer(UfsRequest *req, uint64_t len); void ufs_wb_update_avail_buffer(UfsHc *u); void ufs_init_wlu(UfsLu *wlu, uint8_t wlun); +UfsReqResult ufs_rpmb_sec_in(UfsHc *u, UfsRequest *req, uint8_t *scsi_stat= us, + uint8_t *sense_buf, uint32_t *transferred_len= ); +void ufs_rpmb_reset(UfsHc *u); +UfsReqResult ufs_rpmb_sec_out(UfsHc *u, UfsRequest *req, uint8_t *scsi_sta= tus, + uint8_t *sense_buf, uint32_t *transferred_le= n); bool ufs_realize(UfsHc *u, DeviceState *dev, AddressSpace *dma_as, Error **errp); void ufs_unrealize(UfsHc *u); --=20 2.56.0.307.g5fe7bd680c-goog From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793081003; cv=none; d=zohomail.com; s=zohoarc; b=e277aR+n7ea5V6EyXdEFXP3adHRDqm1WzHJSCzh7ZfhKPj9IB1pETBKWYAVZXnFUFX1coQzjTtE8qAG3uZrabKk/1PX58fLU3j6XeZ44kJUDHXoHdXPBEvQnVwoJfIeLz1P3uLFulUr8c7swo79zMh5FlPSrc88HsEd/7zTvZAw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793081003; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+N8SJHFjywkKiUNGdjZGN9m+mrASByhvUTgzA59edFs=; b=je0WkCsttHFfSzqi7ADnWPxibA0nw2o8S7inrnHpJjmEm34uUi6NKUg/pNsQs0xC2xKFDreYINDaqHZjN8ibMJjvS3olskEg0boT2LFdvnwhaS8es5YwBhDV466E6sNvhrOQqGBalFbwkJeyMO6ud/Qk8KD5xcaNYaLYiY2jD7U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793081003653178.07559148084192; Mon, 26 Oct 2026 23:03:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaH9-0008G8-NW; Tue, 27 Oct 2026 02:03:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3WD7gagoKCrMlmTgeXrcanZhhZeX.VhfjXfn-WXoXeghgZgn.hkZ@flex--stanleyjhu.bounces.google.com>) id 1xLaG7-0007qC-MI for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:09 -0400 Received: from mail-pg1-x548.google.com ([2607:f8b0:4864:20::548]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3WD7gagoKCrMlmTgeXrcanZhhZeX.VhfjXfn-WXoXeghgZgn.hkZ@flex--stanleyjhu.bounces.google.com>) id 1xLaG4-00068K-Ux for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:07 -0400 Received: by mail-pg1-x548.google.com with SMTP id 41be03b00d2f7-cd0b795aa8cso420550a12.0 for ; Mon, 26 Oct 2026 23:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080921; x=1793685721; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=+N8SJHFjywkKiUNGdjZGN9m+mrASByhvUTgzA59edFs=; b=pNIBymJcG2yjWnezhVaouVsJT2asbD2XIjU+AQOnv4hJPudUhdWlsm4oSx2Rwnax9b N6I1mdeQ9YWtVzUC2bGwdiNMWeBuYCuIjktTIV4gWMo98KVLinsoX4TdrE3oyYHsLnKs /giUf+bI0dxhWHQ+/ij85t7c45IAw/hHzDypCrSUgDCWI/o4QVSN0f8fFq07OF7X0BMi z2NFWq9F2XFZq8Y35QPgI71HCvtEIn5nb/mPf2qVTbfdndhPxnsVgU+WJxzzUlan2X5l CnzCqFGRt0+ch2UKP6j/KL2XDJZD9nTlr/SFWlXrdXJybxB6caxtvamY2SSeCTrj3Lxf 6/Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080921; x=1793685721; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=+N8SJHFjywkKiUNGdjZGN9m+mrASByhvUTgzA59edFs=; b=27HbJ/4NxHsz9yuNjF3xnvQpVhruYiueDRwwpB6/QiuAZ10nlaCrLY/+8WqpFddD99 0lMwRB2M85inC0ShM/tHDzx9/XMp+pa1WZzzzA5PQc0yB1aFvOD0dElyoCqCRfn+04K1 qJGbZGkBGF/6r0I9EWFaxS/7majyegUKWvK4SsXHiAS86wBKLB6wte1ASWiean/8hkbz yosER3a62SDuM/3gOp97C/YKr8KYS6Uro+Gvt8MHPMxMhTFBkshRkMb8muUlKqxvyOH9 GJfsSeXml26w2BkZC3L4wsWdquEmNyFnQTC7aidEilGZ1Ng5lQfoE7br9t5Vnir4Tta6 sSAA== X-Gm-Message-State: AFq9FYLyGn0BMU3MhG55LI838oypAYMzKuhCU4cfLbdbpYeX2ZODa52x BOUovxPN3XXPkoe14g/jP9IIhVG4lV2VAZf/hvgqmsQ2c/mQGoLi1sH7h8OtwLr7ywH3bURQJcm 1lqFkASdhbekawM5XOWh/5Q== X-Received: from pfay35.prod.google.com ([2002:a05:6a00:1823:b0:896:c545:14b]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:489:b0:3de:3f4d:f2bd with SMTP id adf61e73a8af0-3e8eaa5c3f6mr961661637.6.1793080920628; Mon, 26 Oct 2026 23:02:00 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:48 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-4-stanleyjhu@google.com> Subject: [PATCH 3/7] tests/qtest/ufs-test: Add RPMB descriptor and CDB validation test From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::548; envelope-from=3WD7gagoKCrMlmTgeXrcanZhhZeX.VhfjXfn-WXoXeghgZgn.hkZ@flex--stanleyjhu.bounces.google.com; helo=mail-pg1-x548.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793081004901158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add ufstest_rpmb_descriptor_cdb() to verify bSecurityLU in DeviceDescriptor (JESD220H Table 14.4) and RpmbUnitDescriptor fields (bLUEnable, bRPMBRegionEnable, bLogicalBlockSize, qLogicalBlockCount, bRPMBRegion0Size..3Size, and qPhyMemResourceCount per JESD220H Table 14.15), initial zeroed Result Register frame reads, ILLEGAL REQUEST / INVALID FIELD IN CDB sense responses on invalid security protocol, INC_512=3D1, unaligned transfer length, and out-of-range region 4, INVALID_OPCODE rejection of START_STOP on W-LUN 0xc4 and SECURITY PROTOCOL IN/OUT on W-LUN 0xd0, and UFS_OCS_MISMATCH_DATA_BUF_SIZE on short or empty PRDT buffers. Signed-off-by: Stanley Jhu --- tests/qtest/ufs-test.c | 285 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 285 insertions(+) diff --git a/tests/qtest/ufs-test.c b/tests/qtest/ufs-test.c index d8f7f502d6..9b43422d4b 100644 --- a/tests/qtest/ufs-test.c +++ b/tests/qtest/ufs-test.c @@ -3975,6 +3975,289 @@ static void *ufs_zufs_test_setup(GString *cmd_line,= void *arg) return arg; } =20 +static void ufs_rpmb_build_cdb(uint8_t cdb[UFS_CDB_SIZE], uint8_t opcode, + uint8_t protocol, uint16_t spsp, uint32_t l= en) +{ + memset(cdb, 0, UFS_CDB_SIZE); + cdb[0] =3D opcode; + cdb[1] =3D protocol; + stw_be_p(&cdb[2], spsp); + stl_be_p(&cdb[6], len); +} + +static void ufs_rpmb_sec_out_frames(QUfs *ufs, uint8_t region, + const UfsRpmbFrame *frames, size_t nfr= ames, + UtpUpiuRsp *rsp) +{ + uint8_t cdb[UFS_CDB_SIZE]; + uint16_t spsp =3D ((uint16_t)region << 8) | UFS_RPMB_SEC_PROTOCOL_ID; + uint32_t len =3D nframes * sizeof(UfsRpmbFrame); + enum UtpOcsCodes ocs; + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_OUT, UFS_RPMB_SEC_PROTOCOL, + spsp, len); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + (const uint8_t *)frames, len, NULL, 0, rsp= ); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); +} + +static void ufs_rpmb_sec_in_frames(QUfs *ufs, uint8_t region, + UfsRpmbFrame *frames, size_t nframes, + UtpUpiuRsp *rsp) +{ + uint8_t cdb[UFS_CDB_SIZE]; + uint16_t spsp =3D ((uint16_t)region << 8) | UFS_RPMB_SEC_PROTOCOL_ID; + uint32_t len =3D nframes * sizeof(UfsRpmbFrame); + enum UtpOcsCodes ocs; + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_IN, UFS_RPMB_SEC_PROTOCOL, + spsp, len); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + NULL, 0, (uint8_t *)frames, len, rsp); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); +} + +static void ufs_rpmb_read_result(QUfs *ufs, uint8_t region, UfsRpmbFrame *= resp) +{ + UfsRpmbFrame req =3D { 0 }; + UtpUpiuRsp rsp_upiu; + + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_RESULT); + ufs_rpmb_sec_out_frames(ufs, region, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(resp, 0xff, sizeof(*resp)); + ufs_rpmb_sec_in_frames(ufs, region, resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); +} + +static enum UtpOcsCodes +ufs_rpmb_send_short_prdt(QUfs *ufs, const uint8_t cdb[UFS_CDB_SIZE], + uint32_t exp_len, uint32_t prdt_bytes, bool is_wr= ite) +{ + int cmd_desc_slot =3D alloc_cmd_desc_slot(ufs); + uint64_t req_upiu_addr =3D + ufs->cmd_desc_addr + cmd_desc_slot * UTP_COMMAND_DESCRIPTOR_SIZE; + uint64_t prdt_addr =3D req_upiu_addr + UTP_PRDT_UPIU_OFFSET; + UtpUpiuReq req_upiu =3D { 0 }; + uint16_t prd_table_length =3D 0; + UtpTransferReqDesc utrd; + enum UtpOcsCodes ocs; + + req_upiu.header.trans_type =3D UFS_UPIU_TRANSACTION_COMMAND; + req_upiu.header.flags =3D + is_write ? UFS_UPIU_CMD_FLAGS_WRITE : UFS_UPIU_CMD_FLAGS_READ; + req_upiu.header.lun =3D UFS_UPIU_RPMB_WLUN; + req_upiu.header.task_tag =3D cmd_desc_slot; + req_upiu.sc.exp_data_transfer_len =3D cpu_to_be32(exp_len); + memcpy(req_upiu.sc.cdb, cdb, UFS_CDB_SIZE); + qtest_memwrite(ufs->dev.bus->qts, req_upiu_addr, &req_upiu, + sizeof(req_upiu)); + + if (prdt_bytes > 0) { + UfshcdSgEntry entry =3D { + .addr =3D cpu_to_le64(ufs->data_buffer_addr), + .size =3D cpu_to_le32(prdt_bytes - 1), + }; + qtest_memwrite(ufs->dev.bus->qts, prdt_addr, &entry, sizeof(entry)= ); + prd_table_length =3D 1; + } + + utrd =3D ufs_build_req_utrd( + req_upiu_addr, + is_write ? UFS_UTP_HOST_TO_DEVICE : UFS_UTP_DEVICE_TO_HOST, + prd_table_length); + ocs =3D ufs_send_transfer_request_sync(ufs, UFS_UPIU_RPMB_WLUN, &utrd); + release_cmd_desc_slot(ufs, cmd_desc_slot); + return ocs; +} + +static void ufstest_rpmb_descriptor_cdb(void *obj, void *data, + QGuestAllocator *alloc) +{ + QUfs *ufs =3D obj; + UtpUpiuRsp rsp_upiu; + enum UtpOcsCodes ocs; + DeviceDescriptor dev_desc; + const RpmbUnitDescriptor *rpmb_desc; + UfsRpmbFrame req =3D { 0 }, resp, zero_frame; + uint8_t cdb[UFS_CDB_SIZE]; + int i; + + ufs_init(ufs, alloc); + + /* + * 1. Verify Device Descriptor bSecurityLU (JESD220H Table 14.4) and R= PMB + * Unit Descriptor fields (JESD220H Table 14.15). + */ + ocs =3D ufs_send_query(ufs, UFS_UPIU_QUERY_FUNC_STANDARD_READ_REQUEST, + UFS_UPIU_QUERY_OPCODE_READ_DESC, + UFS_QUERY_DESC_IDN_DEVICE, 0, 0, 0, &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.response, =3D=3D, UFS_COMMAND_RESULT_= SUCCESS); + memcpy(&dev_desc, &rsp_upiu.qr.data[0], sizeof(dev_desc)); + g_assert_cmpuint(dev_desc.length, =3D=3D, sizeof(DeviceDescriptor)); + g_assert_cmpuint(dev_desc.descriptor_idn, =3D=3D, UFS_QUERY_DESC_IDN_D= EVICE); + g_assert_cmpuint(dev_desc.security_lu, =3D=3D, 0x01); + + ocs =3D ufs_send_query(ufs, UFS_UPIU_QUERY_FUNC_STANDARD_READ_REQUEST, + UFS_UPIU_QUERY_OPCODE_READ_DESC, + UFS_QUERY_DESC_IDN_UNIT, UFS_UPIU_RPMB_WLUN, 0, 0, + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.response, =3D=3D, UFS_COMMAND_RESULT_= SUCCESS); + rpmb_desc =3D (const RpmbUnitDescriptor *)&rsp_upiu.qr.data[0]; + g_assert_cmpuint(rpmb_desc->length, =3D=3D, sizeof(RpmbUnitDescriptor)= ); + g_assert_cmpuint(rpmb_desc->descriptor_idn, =3D=3D, UFS_QUERY_DESC_IDN= _UNIT); + g_assert_cmpuint(rpmb_desc->unit_index, =3D=3D, UFS_UPIU_RPMB_WLUN); + g_assert_cmpuint(rpmb_desc->lu_enable, =3D=3D, 0x01); + g_assert_cmpuint(rpmb_desc->rpmb_region_enable, =3D=3D, 0x0f); + g_assert_cmpuint(rpmb_desc->logical_block_size, =3D=3D, + UFS_RPMB_LOGICAL_BLOCK_SIZE); + g_assert_cmpuint(be64_to_cpu(rpmb_desc->logical_block_count), =3D=3D, + UFS_RPMB_MAX_REGIONS * 32 * UFS_RPMB_BLOCKS_PER_UNIT); + for (i =3D 0; i < UFS_RPMB_MAX_REGIONS; i++) { + g_assert_cmpuint(rpmb_desc->rpmb_region_size[i], =3D=3D, 32); + } + g_assert_cmpuint(be64_to_cpu(rpmb_desc->phy_mem_resource_count), =3D= =3D, + UFS_RPMB_MAX_REGIONS * 32 * UFS_RPMB_BLOCKS_PER_UNIT); + + /* 2. Result read before any request returns a zeroed frame */ + memset(&zero_frame, 0, sizeof(zero_frame)); + memset(&resp, 0xff, sizeof(resp)); + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpmem(&resp, sizeof(resp), &zero_frame, sizeof(zero_frame)); + + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpmem(&resp, sizeof(resp), &zero_frame, sizeof(zero_frame)); + + /* + * 3. Invalid protocol, INC_512=3D1, and unaligned length CDB sense ch= ecks. + */ + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_OUT, 0xee, + UFS_RPMB_SEC_PROTOCOL_ID, sizeof(req)); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + (const uint8_t *)&req, sizeof(req), NULL, = 0, + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_OUT, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, sizeof(req)); + cdb[4] =3D 0x80; /* INC_512 =3D 1 */ + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + (const uint8_t *)&req, sizeof(req), NULL, = 0, + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_IN, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, sizeof(resp)); + cdb[4] =3D 0x80; /* INC_512 =3D 1 */ + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + NULL, 0, (uint8_t *)&resp, sizeof(resp), + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_OUT, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, 256); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + (const uint8_t *)&req, 256, NULL, 0, + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + /* + * 4. START_STOP on RPMB W-LUN (0xc4) and SECURITY PROTOCOL IN/OUT on + * UFS Device W-LUN (0xd0) must fail with INVALID_OPCODE + * (ILLEGAL_REQUEST, ASC 0x20, ASCQ 0x00). + */ + memset(cdb, 0, UFS_CDB_SIZE); + cdb[0] =3D START_STOP; + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + NULL, 0, NULL, 0, &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x20); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_IN, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, sizeof(resp)); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_UFS_DEVICE_WLUN, cdb, + NULL, 0, (uint8_t *)&resp, sizeof(resp), + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x20); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_OUT, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, sizeof(req)); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_UFS_DEVICE_WLUN, cdb, + (const uint8_t *)&req, sizeof(req), NULL, = 0, + &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x20); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + /* 5. Region 4 (out of range 0..3) rejected with INVALID_FIELD */ + memset(&req, 0, sizeof(req)); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_COUNTER); + ufs_rpmb_sec_out_frames(ufs, 4, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + ufs_rpmb_sec_in_frames(ufs, 4, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + /* + * 6. Short PRDT (256 bytes and empty 0-entry PRDT) for SECURITY PROTO= COL + * OUT and IN must return UFS_OCS_MISMATCH_DATA_BUF_SIZE before any= DMA + * or volatile state mutation. + */ + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_OUT, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, UFS_RPMB_FRAME_LEN); + memset(&req, 0, sizeof(req)); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_COUNTER); + qtest_memwrite(ufs->dev.bus->qts, ufs->data_buffer_addr, &req, 256); + ocs =3D ufs_rpmb_send_short_prdt(ufs, cdb, UFS_RPMB_FRAME_LEN, 256, tr= ue); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_MISMATCH_DATA_BUF_SIZE); + ocs =3D ufs_rpmb_send_short_prdt(ufs, cdb, UFS_RPMB_FRAME_LEN, 0, true= ); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_MISMATCH_DATA_BUF_SIZE); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_IN, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, UFS_RPMB_FRAME_LEN); + ocs =3D ufs_rpmb_send_short_prdt(ufs, cdb, UFS_RPMB_FRAME_LEN, 256, fa= lse); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_MISMATCH_DATA_BUF_SIZE); + ocs =3D ufs_rpmb_send_short_prdt(ufs, cdb, UFS_RPMB_FRAME_LEN, 0, fals= e); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_MISMATCH_DATA_BUF_SIZE); + + ufs_exit(ufs, alloc); +} + static void ufs_register_nodes(void) { const char *arch; @@ -4068,6 +4351,8 @@ static void ufs_register_nodes(void) &zufs_test_opts); qos_add_test("zufs-more-cmds", "ufs", ufstest_zufs_more_cmds, &zufs_test_opts); + qos_add_test("rpmb-descriptor-cdb", "ufs", ufstest_rpmb_descriptor_cdb, + &io_test_opts); } =20 libqos_init(ufs_register_nodes); --=20 2.56.0.307.g5fe7bd680c-goog From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793081032; cv=none; d=zohomail.com; s=zohoarc; b=btzvp2Q6GKthdTlwNoT0FgFW2dpnKHDBGuvdcJQc0xx/U7paxGVH4gUPbR1qI78IjFEdQjOWFqRenqNq2udSj2+4F7x3ztZmUV+Mrte+tgmFmgbjFS7Ab5RPcwhqP1RpvtYWceW5+hxnZzPU/MQBo4zis+j2z5soiwKTwDPcZIE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793081032; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=GNPLe2N/OPKmVpGx/oLc6OH7h+zcVBsEsqwKPANF6lo=; b=Q4pg+cb3gZ60NEufk2phK1ohptaAdQrKgeQbTHeEFcPyhUtNJgpzQ0YCcNMPxAovWC3KWiK7DiWH/1hHKRKtsaFBVKFQJqMXgEcfSNque6pkui+TTGI2l3l41opqnwGawTKV2eDsQ3TRVJVDjhw+ifkcLeVG6/orl/kjuV6+FCE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793081032869995.822403195116; Mon, 26 Oct 2026 23:03:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaHH-0008Re-65; Tue, 27 Oct 2026 02:03:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3Wj7gagoKCrUnoVigZtecpbjjbgZ.XjhlZhp-YZqZgijibip.jmb@flex--stanleyjhu.bounces.google.com>) id 1xLaGA-0007qZ-9y for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:12 -0400 Received: from mail-pg1-x546.google.com ([2607:f8b0:4864:20::546]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3Wj7gagoKCrUnoVigZtecpbjjbgZ.XjhlZhp-YZqZgijibip.jmb@flex--stanleyjhu.bounces.google.com>) id 1xLaG6-00068f-QV for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:08 -0400 Received: by mail-pg1-x546.google.com with SMTP id 41be03b00d2f7-ccacc75a997so1581529a12.0 for ; Mon, 26 Oct 2026 23:02:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080923; x=1793685723; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=GNPLe2N/OPKmVpGx/oLc6OH7h+zcVBsEsqwKPANF6lo=; b=dch7OB0wmBufIhhdRLeE7duAacmOKUJrDhjyBTEenfT34etW7QOdn944ghnlBXQgxp bE9N2aFSIsDrS2js5jU0nIRMyBmYqohrklkvinSZLJSP3QVFZf+eN507Dos1cQBa8dIO HJsQfN7A4g9kVAAiqZXguULA5/0dK6wvAjJxdSmgnB4XoPPx8ZTkBg1ZMxx5n/+G+cRS Z5gXgXdRZwWzMGO/xvAQ6TFkQOwOoss80i9NJQPjSugmQ3Gp0cq4fScAehSbM/TfLWwl bEh8FWsMd5/TsL3RvuaqN60n9nai8UDxzifzbae+MnpCfig5wstsVF47nTu0pJAOuk60 /bkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080923; x=1793685723; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=GNPLe2N/OPKmVpGx/oLc6OH7h+zcVBsEsqwKPANF6lo=; b=mzK9f8m21xdF6fh7lboY0ptZpY9MH4kLrDq9dqRDCQdvnqVKgyHVwzATnwgMAbR9Oe dDdeK4uKA2W392MuzOx3oD3OfKyqb7WufOeuRstoTYy9/Qx+IlqnLUVOAagdwXchLWjG 6LiWSIfy9P51/OJPb3CsJcfEZd7BZOfTze4Z4cq6WeQUN9azAp/ZpK5mf4wVIGIPsBVj /JofoBKaTn+USqNvLrE8TfSauPtW3Kx3B0BwmAv/jsHJDhz7tIjSrvOCIb9Y9BIz7y4a 5L5cSrvlx2Cp253oVFni0ezU9dX21JdcvBA8AdWK5htSvufFGVVbjXpzqJVxG4tFRieC ERZA== X-Gm-Message-State: AFq9FYJntdwdRW9S7JhFXii5FFhlo60UNaKM0UpxAs8J8zQkEM421S29 t/S83Eb7PIXNcfEy7V9eICBohrHUKqozIeUHtfCk2d1KdfcFN3rXLSX02v94k7bwmANSTF1lMYC CUBN2FIX2/XU+JIbxiRkFHw== X-Received: from pgbda2-n1.prod.google.com ([2002:a05:6a02:2382:10b0:cd8:c484:3c40]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:398:b0:3dd:f9ff:f5a with SMTP id adf61e73a8af0-3e7af7e9a36mr6453727637.9.1793080922559; Mon, 26 Oct 2026 23:02:02 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:49 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-5-stanleyjhu@google.com> Subject: [PATCH 4/7] hw/ufs: Add RPMB key programming and write counter read handling From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::546; envelope-from=3Wj7gagoKCrUnoVigZtecpbjjbgZ.XjhlZhp-YZqZgijibip.jmb@flex--stanleyjhu.bounces.google.com; helo=mail-pg1-x546.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793081034951158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add HMAC-SHA256 calculation (ufs_rpmb_calc_hmac() over frame bytes [228:511] per JESD220H Section 12.4.4.2) using QCryptoHmac, and handle authentication key programming (0x0001 -> 0x0100 with zeroed Key/MAC per JESD220H Section 12.4.7.3.1 Figure 12.5, rejecting duplicate key writes with UFS_RPMB_RES_WRITE_FAILURE per Section 12.4.3.1) and write counter read requests (0x0002 -> 0x0200, returning UFS_RPMB_RES_NO_AUTH_KEY before a key is programmed and a signed response frame thereafter). Signed-off-by: Stanley Jhu --- hw/ufs/rpmb.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/hw/ufs/rpmb.c b/hw/ufs/rpmb.c index 3032e0e098..edd4b91fd8 100644 --- a/hw/ufs/rpmb.c +++ b/hw/ufs/rpmb.c @@ -6,9 +6,34 @@ =20 #include "qemu/osdep.h" #include "qapi/error.h" +#include "crypto/hmac.h" #include "trace.h" #include "ufs.h" =20 +/* + * JESD220H Section 12.4.4.2: the HMAC-SHA256 input message is the + * concatenation of bytes [228:511] (UFS_RPMB_HASH_OFFSET..511, 284 bytes) + * of each RPMB Message Data Frame in the order in which the frames are se= nt. + */ +static void ufs_rpmb_calc_hmac(const UfsRpmbRegion *r, + const UfsRpmbFrame *frames, + uint16_t count, uint8_t *mac) +{ + g_autoptr(QCryptoHmac) hmac =3D NULL; + size_t mac_len =3D UFS_RPMB_KEY_MAC_LEN; + g_autofree struct iovec *iov =3D g_new(struct iovec, count); + uint16_t i; + + hmac =3D qcrypto_hmac_new(QCRYPTO_HASH_ALGO_SHA256, r->key, + UFS_RPMB_KEY_MAC_LEN, &error_abort); + for (i =3D 0; i < count; i++) { + iov[i].iov_base =3D (uint8_t *)&frames[i] + UFS_RPMB_HASH_OFFSET; + iov[i].iov_len =3D UFS_RPMB_HASH_LEN; + } + + qcrypto_hmac_bytesv(hmac, iov, count, &mac, &mac_len, &error_abort); +} + static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion *r, const UfsRpmbFrame *frames, uint32_t nframes) @@ -16,6 +41,7 @@ static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion *= r, const UfsRpmbFrame *req =3D &frames[0]; uint16_t req_type =3D lduw_be_p(&req->req_resp); uint16_t result =3D UFS_RPMB_RES_GENERAL_FAILURE; + bool sign_resp =3D false; =20 if (req_type =3D=3D UFS_RPMB_REQ_READ_RESULT) { trace_ufs_rpmb_request(req_type, lduw_be_p(&r->resp.result)); @@ -26,6 +52,39 @@ static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion = *r, memset(&r->resp, 0, sizeof(r->resp)); =20 switch (req_type) { + case UFS_RPMB_REQ_PROGRAM_KEY: + /* + * JESD220H Section 12.4.7.3.1 Figure 12.5: the Authentication Key + * Programming Result Read response (0x0100) has bytes [196:227] + * (Key/MAC) and [484:499] (Nonce) set to 0...00h. + */ + r->resp.req_resp =3D cpu_to_be16(UFS_RPMB_RESP_PROGRAM_KEY); + /* + * JESD220H Section 12.4.7.3.1 (Figure 12.5) and Section 12.4.3.1: + * programming an Authentication Key after one is already programm= ed + * fails with Write failure (0x0005). + */ + if (r->key_set) { + result =3D UFS_RPMB_RES_WRITE_FAILURE; + break; + } + memcpy(r->key, req->key_mac, UFS_RPMB_KEY_MAC_LEN); + r->key_set =3D true; + result =3D UFS_RPMB_RES_OK; + break; + + case UFS_RPMB_REQ_READ_COUNTER: + memcpy(r->resp.nonce, req->nonce, UFS_RPMB_NONCE_LEN); + r->resp.req_resp =3D cpu_to_be16(UFS_RPMB_RESP_READ_COUNTER); + if (!r->key_set) { + result =3D UFS_RPMB_RES_NO_AUTH_KEY; + break; + } + r->resp.write_counter =3D cpu_to_be32(r->write_counter); + result =3D UFS_RPMB_RES_OK; + sign_resp =3D true; + break; + default: r->resp.req_resp =3D cpu_to_be16(req_type); result =3D UFS_RPMB_RES_GENERAL_FAILURE; @@ -37,6 +96,10 @@ static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion = *r, } r->resp.result =3D cpu_to_be16(result); =20 + if (sign_resp) { + ufs_rpmb_calc_hmac(r, &r->resp, 1, r->resp.key_mac); + } + trace_ufs_rpmb_request(req_type, result); } =20 --=20 2.56.0.307.g5fe7bd680c-goog From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793080998; cv=none; d=zohomail.com; s=zohoarc; b=Is/A6rULhch2+yZaFI0BDwy0AFKok/IRNGcX1kmrOTrO07lLtdobqjGMOs5ZTFDPazKE0nxRvg4CB3z5ufDTOEboHZHXdQa7CYheuHZPgbRGFP2kxA79I0cbXUPWhNXU7Fu8SV4+oMWkSdTXFF0FrmRlHDfkhdqZ++/LOWn1Uhw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793080998; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=6vy9oe1+vFMGFNwMvDWhH9l5IpR7kofBbXkWGlg3dU4=; b=McV5LXv6fbMToGBY7Lj7IbLobaMVsSPil1G1fBJueHwCq1+n9c4+Oc//YAFL2Vo4Y32hrPgAXVOlE3+HRJHvWbgb+JWLx2bCMQpWkcxxhfL4yDYhIqCVMxB2FyT42iQYZtAemlFLH2H18pM4y6ttvZAp+hbxkLgDgOvH49UjXFY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793080998033741.6497680686851; Mon, 26 Oct 2026 23:03:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaGy-0007wZ-8G; Tue, 27 Oct 2026 02:03:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3XD7gagoKCrcpqXkibvgerdlldib.Zljnbjr-absbiklkdkr.lod@flex--stanleyjhu.bounces.google.com>) id 1xLaGD-0007rZ-W1 for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:15 -0400 Received: from mail-pf1-x448.google.com ([2607:f8b0:4864:20::448]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3XD7gagoKCrcpqXkibvgerdlldib.Zljnbjr-absbiklkdkr.lod@flex--stanleyjhu.bounces.google.com>) id 1xLaG9-00068x-Uu for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:12 -0400 Received: by mail-pf1-x448.google.com with SMTP id d2e1a72fcca58-89ff475a0deso3127855b3a.2 for ; Mon, 26 Oct 2026 23:02:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080925; x=1793685725; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6vy9oe1+vFMGFNwMvDWhH9l5IpR7kofBbXkWGlg3dU4=; b=Y4XOmMFVt+/ioGZ2rzXSEbTsVz81dhHhi3QGOz4bNbEP5o79ippzJlq3KbO+INojfo vamObxfu909CFKwY+Tj4jCUtI47TofoO43amtsOzbz8r8Y/Hd3qhn8JUcLMQSINuVAX8 aqW2bhKKq1GH0LdLngKRX1oLVKlrW025u9PLhh/8u+2wYANR4HTElx1wRqDXr5zoZM2h Fbxe6/b/I8LSGhU5dugPcDBFtx1z6E7VFQ/zqqZUWv8Csm6jLKADW0F1Hn8doX4J22uC t5rfnI1yzyxf6mObmXB5Ey4PFWGNZ5Y1kTe/uyxJq7NxiEb+c6YFtseQW5ggz5QOtLhR 1VTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080925; x=1793685725; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6vy9oe1+vFMGFNwMvDWhH9l5IpR7kofBbXkWGlg3dU4=; b=1sLD/Er/+djY9cHsQx8A9VHi/D4rrFmBrqWoIqg0AKq+A5Qjb0DnCC3d3ygAfR43G7 P8MRU2ryoGcC5RxtTAadq18j4xAoLkWYPPRw11m6g4HLYjMgMgv4cA0WawF0ZabxGoKT Bic/71bnIX7CliBySA+xlFxebzQ7vdfw+td54beo+qVDy8ysB6F6CNB4tiFIbz8lXukf DPTIbRFsg1eQ/UCbcW0r2N76+Eq8IL/EhzlH/OMmfGkVjy1TrHQ5ORhXVIpnpQDq+cdq dunrRt32No1uIbLyHfSTtMLOe5Ww8MJ01QnhRyNq8mnTtrq4nzZ8wUM6HAAqCbewdcqU kbRw== X-Gm-Message-State: AFq9FYIqLlVhv6mx+hrmcUXLQDWPWQOBKpzDkmuEtuWGQ2B4KVoe0SlX I2y2juKKJ4V3ZAxiuDPlG2g3k5qe4xXnhzgbsN+rvDTAx3wIo6Gq8VG+T4SRWSuNT7ZjkJGzuLr JXMI8kWwCFjBuDkCDcfIMGg== X-Received: from pfbih21.prod.google.com ([2002:a05:6a00:8c15:b0:8a1:d11:a833]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:9512:b0:874:705d:f64e with SMTP id d2e1a72fcca58-8a36689d88amr7171435b3a.28.1793080924447; Mon, 26 Oct 2026 23:02:04 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:50 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-6-stanleyjhu@google.com> Subject: [PATCH 5/7] tests/qtest/ufs-test: Add RPMB key programming and write counter test From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::448; envelope-from=3XD7gagoKCrcpqXkibvgerdlldib.Zljnbjr-absbiklkdkr.lod@flex--stanleyjhu.bounces.google.com; helo=mail-pf1-x448.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793080998926158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add ufstest_rpmb_key_counter() and shared helper ufs_rpmb_program_key() to test write counter reads before key programming (0x0007), key programming (0x0000 with zeroed Key/MAC per JESD220H Figure 12.5), duplicate key programming rejection (0x0005), and authenticated write counter reads with HMAC-SHA256 verification. Signed-off-by: Stanley Jhu --- tests/qtest/ufs-test.c | 105 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/tests/qtest/ufs-test.c b/tests/qtest/ufs-test.c index 9b43422d4b..9fce5fff82 100644 --- a/tests/qtest/ufs-test.c +++ b/tests/qtest/ufs-test.c @@ -3985,6 +3985,24 @@ static void ufs_rpmb_build_cdb(uint8_t cdb[UFS_CDB_S= IZE], uint8_t opcode, stl_be_p(&cdb[6], len); } =20 +static void ufs_rpmb_compute_hmac(const uint8_t key[UFS_RPMB_KEY_MAC_LEN], + const UfsRpmbFrame *frames, size_t nfram= es, + uint8_t mac[UFS_RPMB_KEY_MAC_LEN]) +{ + GHmac *hmac =3D g_hmac_new(G_CHECKSUM_SHA256, key, UFS_RPMB_KEY_MAC_LE= N); + size_t mac_len =3D UFS_RPMB_KEY_MAC_LEN; + size_t i; + + for (i =3D 0; i < nframes; i++) { + g_hmac_update(hmac, + (const guchar *)&frames[i] + UFS_RPMB_HASH_OFFSET, + UFS_RPMB_HASH_LEN); + } + g_hmac_get_digest(hmac, mac, &mac_len); + g_assert_cmpuint(mac_len, =3D=3D, UFS_RPMB_KEY_MAC_LEN); + g_hmac_unref(hmac); +} + static void ufs_rpmb_sec_out_frames(QUfs *ufs, uint8_t region, const UfsRpmbFrame *frames, size_t nfr= ames, UtpUpiuRsp *rsp) @@ -4258,6 +4276,91 @@ static void ufstest_rpmb_descriptor_cdb(void *obj, v= oid *data, ufs_exit(ufs, alloc); } =20 +static const uint8_t ufs_rpmb_test_key[UFS_RPMB_KEY_MAC_LEN] =3D { + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, +}; + +static void ufs_rpmb_program_key(QUfs *ufs, uint8_t region, + const uint8_t *key) +{ + UfsRpmbFrame req =3D { 0 }, resp; + UtpUpiuRsp rsp_upiu; + uint8_t zero_mac[UFS_RPMB_KEY_MAC_LEN] =3D { 0 }; + + memcpy(req.key_mac, key, UFS_RPMB_KEY_MAC_LEN); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_PROGRAM_KEY); + ufs_rpmb_sec_out_frames(ufs, region, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_read_result(ufs, region, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, UFS_RPMB_RESP_PROG= RAM_KEY); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + zero_mac, UFS_RPMB_KEY_MAC_LEN); +} + +static void ufstest_rpmb_key_counter(void *obj, void *data, + QGuestAllocator *alloc) +{ + QUfs *ufs =3D obj; + UtpUpiuRsp rsp_upiu; + UfsRpmbFrame req, resp; + uint8_t mac[UFS_RPMB_KEY_MAC_LEN]; + + ufs_init(ufs, alloc); + + /* 1. Read write counter before key is programmed -> 0x0007 (NO_AUTH_K= EY) */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xa1, sizeof(req.nonce)); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_COUNTER); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, UFS_RPMB_RESP_READ= _COUNTER); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_NO_AUTH= _KEY); + g_assert_cmpmem(resp.nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + + /* 2. Program key -> 0x0000 (OK) with zeroed Key/MAC (JESD220H Fig 12.= 5) */ + ufs_rpmb_program_key(ufs, 0, ufs_rpmb_test_key); + + /* 3. Re-program key -> 0x0005 (WRITE_FAILURE) */ + memset(&req, 0, sizeof(req)); + memcpy(req.key_mac, ufs_rpmb_test_key, UFS_RPMB_KEY_MAC_LEN); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_PROGRAM_KEY); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, UFS_RPMB_RESP_PROG= RAM_KEY); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_WRITE_F= AILURE); + + /* 4. Read write counter -> 0x0000, counter =3D=3D 0, valid MAC */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xb2, sizeof(req.nonce)); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_COUNTER); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, UFS_RPMB_RESP_READ= _COUNTER); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 0); + g_assert_cmpmem(resp.nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + ufs_rpmb_compute_hmac(ufs_rpmb_test_key, &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + ufs_exit(ufs, alloc); +} + static void ufs_register_nodes(void) { const char *arch; @@ -4353,6 +4456,8 @@ static void ufs_register_nodes(void) &zufs_test_opts); qos_add_test("rpmb-descriptor-cdb", "ufs", ufstest_rpmb_descriptor_cdb, &io_test_opts); + qos_add_test("rpmb-key-counter", "ufs", ufstest_rpmb_key_counter, + &io_test_opts); } =20 libqos_init(ufs_register_nodes); --=20 2.56.0.307.g5fe7bd680c-goog From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793081024; cv=none; d=zohomail.com; s=zohoarc; b=cO3J5yeXbm8mm4/Fd7HNewGnH3u3rh2NNc+8n/s/K6vzI4F59tZfpCO4wdzO3aalUey5DvL4fQC5ba31LRD8QXKEBKGsgN7ampW2GdHPIimVOyxCcik5Q1BH6Fo6L+eXwdcaU/erUDVstZMVxjgwJKs6Bi6rNP/Ood+xtoc9+L8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793081024; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Jqg0A5WeRaJl7VqkqFHuoxyU+sLfvSzxG/NUgUHv9eQ=; b=Peqn/N8qNjKYQUGaVKSqtzHSv3qOgUD0cR4XywlcEsXvtWlUuz/o4eRNn5TjSjKizpbnQ6uOChE7lbvb7wiuh5hAkQ67QIcB+PVmJImorhrB7OcVSnMKc47Xm3fwfzFS00VrL5mi90D4tkzJtlpYdxwX7buPM3ocjVxnM+PKdd4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793081024602589.1435118000622; Mon, 26 Oct 2026 23:03:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaH9-0008Ez-CH; Tue, 27 Oct 2026 02:03:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3Xj7gagoKCrkrsZmkdxigtfnnfkd.bnlpdlt-cdudkmnmfmt.nqf@flex--stanleyjhu.bounces.google.com>) id 1xLaGF-0007s3-O7 for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:20 -0400 Received: from mail-pf1-x445.google.com ([2607:f8b0:4864:20::445]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3Xj7gagoKCrkrsZmkdxigtfnnfkd.bnlpdlt-cdudkmnmfmt.nqf@flex--stanleyjhu.bounces.google.com>) id 1xLaGB-00069L-RG for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:13 -0400 Received: by mail-pf1-x445.google.com with SMTP id d2e1a72fcca58-89e6ddd4554so2905883b3a.3 for ; Mon, 26 Oct 2026 23:02:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080927; x=1793685727; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Jqg0A5WeRaJl7VqkqFHuoxyU+sLfvSzxG/NUgUHv9eQ=; b=vBdWLRsiRQSX3J9pbkljMbr0QJhBXVmKqvtZLYh1sO5ZWqQinBme4JRkyv66cmDkDn o9Pl3ZjubVOzqfXaZTS4PJk5obj3UjYNaPbfIX9GjS1mwPJVh0ni6RtQZu5d5cEWavII vedTVF16vvaSHDxXnPealQ+jJdQPia0Bt9ubx2GWU5Xvyp/i8zgFPPmFuRzwBQzVFTXY pFSexWMQEaQPqpl0VySEUM4lkGwx+8ysPYy7mUTrpQ1ECz/0QkZUf7gbRc28ZOGusgIx e+nCjhLVng3ZRaUrSZhNg/2Ol/PHs4lIhjX3H2VlIYEbr2wlRaq5y05E5kefC8dxo30R 8YUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080927; x=1793685727; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Jqg0A5WeRaJl7VqkqFHuoxyU+sLfvSzxG/NUgUHv9eQ=; b=KnJukkNcRaB7zHf8lS1GjZalONegXYrroFoMHOlj1E3RY5ssnhyYTTLTvPYDHYNRHF N8FubKrWfxQxDubZ9MGwgGW8JE/ImMOTaqxFYtExKqnYY/j8ulfvDb7CTFP0+DCR70ol xVOYzI7RRYexmSHEUlj1L4rAGq6ZxWqr6wqo/pKatEMospuTaqgHoB9JxXR1B3Tf0Wu/ rtCESnxeUL7Pc1zXOrpk691xSI9gRp6fIEJpvL+JvEAQ2n8PJHCeoS7w544W6A7P8mHZ GYLwz30DsraOzWX8nABc8lAoYuEdmTBbPSz5NROD25BL1oT1PhOrXMtcBLNamsLy2WUA 7eog== X-Gm-Message-State: AFq9FYKDtGPpWX8ALqMZ511HSg5rU7PS5WW5WL4jHZHG0MLarbW95CbS PnxYNN8MIkIbuYP7XPYPjhEQ4xqlpn7Dc+hEz8SzxkUQf1Ef+dpxWa2bZFNEWQWXLz72k9AR31g iP5+POSH5H42XCk7YvHFLDQ== X-Received: from pfbih13.prod.google.com ([2002:a05:6a00:8c0d:b0:89f:2a5b:4e34]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:12cb:b0:8a0:2cae:c805 with SMTP id d2e1a72fcca58-8a36486723bmr6959759b3a.2.1793080926495; Mon, 26 Oct 2026 23:02:06 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:51 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-7-stanleyjhu@google.com> Subject: [PATCH 6/7] hw/ufs: Add RPMB authenticated data write and read handling From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::445; envelope-from=3Xj7gagoKCrkrsZmkdxigtfnnfkd.bnlpdlt-cdudkmnmfmt.nqf@flex--stanleyjhu.bounces.google.com; helo=mail-pf1-x445.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793081026994158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Complete RPMB well-known logical unit emulation in hw/ufs/rpmb.c and update docs/system/devices/ufs.rst: - Support single- and multi-frame authenticated data writes (0x0003), checking write counter expiration, address bounds, block count and multi-frame header consistency, MAC, and write counter in the order specified by JESD220H Section 12.4.7.3.3, and incrementing the write counter once per command. - Support single- and multi-frame authenticated data reads (0x0004), validating PRDT buffer capacity before clearing read_pending, checking address bounds before block count, and building the response via ufs_rpmb_do_read() with Stuff bytes [0:195] and Write counter [500:503] zeroed per JESD220H Section 12.4.7.3.4 and Figure 12.8. - Materialize the signed 1-frame 0x0400 response into the Result Register when Result Read (0x0005) arrives while a 0x0004 read is pending without a prior SECURITY PROTOCOL IN. - Free allocated region storage buffers in ufs_unrealize(). Signed-off-by: Stanley Jhu --- docs/system/devices/ufs.rst | 6 +- hw/ufs/rpmb.c | 214 ++++++++++++++++++++++++++++++++++++ hw/ufs/ufs.c | 5 + 3 files changed, 223 insertions(+), 2 deletions(-) diff --git a/docs/system/devices/ufs.rst b/docs/system/devices/ufs.rst index 36f5b958cf..7bf7352c4c 100644 --- a/docs/system/devices/ufs.rst +++ b/docs/system/devices/ufs.rst @@ -139,6 +139,8 @@ Limitations requests are supported; other task management functions such as ``LOGICAL UNIT RESET`` return ``TASK MANAGEMENT FUNCTION NOT SUPPORTED``. -* The Boot and RPMB well-known logical units only answer basic SCSI - commands such as INQUIRY; boot and RPMB data access are not emulated. +* The Boot well-known logical unit only answers basic SCSI commands + such as INQUIRY; boot data access is not emulated. The RPMB + well-known logical unit emulates four 4 MiB regions in host memory + (volatile across QEMU restarts). * The device cannot be migrated. diff --git a/hw/ufs/rpmb.c b/hw/ufs/rpmb.c index edd4b91fd8..344f0708ad 100644 --- a/hw/ufs/rpmb.c +++ b/hw/ufs/rpmb.c @@ -10,6 +10,13 @@ #include "trace.h" #include "ufs.h" =20 +static void ufs_rpmb_ensure_storage(UfsRpmbRegion *r) +{ + if (!r->storage) { + r->storage =3D g_malloc0(UFS_RPMB_REGION_BYTES); + } +} + /* * JESD220H Section 12.4.4.2: the HMAC-SHA256 input message is the * concatenation of bytes [228:511] (UFS_RPMB_HASH_OFFSET..511, 284 bytes) @@ -34,6 +41,142 @@ static void ufs_rpmb_calc_hmac(const UfsRpmbRegion *r, qcrypto_hmac_bytesv(hmac, iov, count, &mac, &mac_len, &error_abort); } =20 +static uint16_t ufs_rpmb_do_write(UfsHc *u, UfsRpmbRegion *r, + const UfsRpmbFrame *frames, + uint32_t nframes) +{ + uint8_t mac[UFS_RPMB_KEY_MAC_LEN]; + uint16_t addr =3D lduw_be_p(&frames[0].addr); + uint16_t blk_cnt =3D lduw_be_p(&frames[0].block_count); + uint32_t req_wc =3D ldl_be_p(&frames[0].write_counter); + uint8_t max_rw =3D u->geometry_desc.rpmb_read_write_size; + uint32_t i; + + /* + * JESD220H Section 12.4.7.3.3: check write counter expiration (0x0085= ), + * target address and address + block count bounds (0x0004), block cou= nt + * and frame fields (0x0001), MAC (0x0002), and write counter (0x0003)= in + * order. + */ + if (!r->key_set) { + return UFS_RPMB_RES_NO_AUTH_KEY; + } + if (r->write_counter =3D=3D 0xffffffff) { + return UFS_RPMB_RES_WRITE_FAILURE; + } + if (addr >=3D UFS_RPMB_REGION_BLOCKS || + (uint32_t)addr + blk_cnt > UFS_RPMB_REGION_BLOCKS) { + return UFS_RPMB_RES_ADDR_FAILURE; + } + if (blk_cnt =3D=3D 0 || blk_cnt > max_rw || blk_cnt !=3D nframes) { + return UFS_RPMB_RES_GENERAL_FAILURE; + } + /* + * JESD220H Section 12.4.7.3.3: every frame of a multi-block Authentic= ated + * Data Write carries identical req_resp, block_count, addr, and + * write_counter values, or fails with General failure (0x0001). + */ + for (i =3D 0; i < nframes; i++) { + if (lduw_be_p(&frames[i].req_resp) !=3D UFS_RPMB_REQ_AUTH_DATA_WRI= TE || + lduw_be_p(&frames[i].block_count) !=3D blk_cnt || + lduw_be_p(&frames[i].addr) !=3D addr || + ldl_be_p(&frames[i].write_counter) !=3D req_wc) { + return UFS_RPMB_RES_GENERAL_FAILURE; + } + } + ufs_rpmb_calc_hmac(r, frames, nframes, mac); + if (memcmp(mac, frames[nframes - 1].key_mac, UFS_RPMB_KEY_MAC_LEN) != =3D 0) { + return UFS_RPMB_RES_AUTH_FAILURE; + } + if (req_wc !=3D r->write_counter) { + return UFS_RPMB_RES_COUNT_FAILURE; + } + + ufs_rpmb_ensure_storage(r); + for (i =3D 0; i < nframes; i++) { + memcpy(r->storage + ((uint32_t)addr + i) * UFS_RPMB_DATA_LEN, + frames[i].data, UFS_RPMB_DATA_LEN); + } + /* + * JESD220H Section 12.4.3.1 & Section 12.4.7.3.3: the write counter is + * incremented by one per successful authenticated data write command. + */ + r->write_counter++; + return UFS_RPMB_RES_OK; +} + +/* + * JESD220H Section 12.4.7.3.4 (Figure 12.8): build the Authenticated Data + * Read response (0x0400) for in_frames blocks (with bytes [0:195] Stuff b= ytes + * and [500:503] Write counter set to 0...00h) and update the region's lat= ched + * Result Register (r->resp). On error, sign a single error frame (when + * key_set) and replicate it across all returned frames, matching the eMMC + * RPMB read model in emmc_rpmb_blk_read() (hw/sd/sd.c). + */ +static void ufs_rpmb_do_read(UfsHc *u, UfsRpmbRegion *r, uint32_t in_frame= s, + UfsRpmbFrame *out_frames) +{ + uint16_t addr =3D lduw_be_p(&r->resp.addr); + uint16_t req_blk_cnt =3D lduw_be_p(&r->resp.block_count); + uint8_t max_rw =3D u->geometry_desc.rpmb_read_write_size; + uint16_t result; + bool ok =3D false; + uint32_t i; + + /* + * JESD220H Section 12.4.7.3.4: check address and address + block count + * bounds (0x0004) before block count > bRPMB_ReadWriteSize (0x0001). + */ + if (!r->key_set) { + result =3D UFS_RPMB_RES_NO_AUTH_KEY; + } else if (addr >=3D UFS_RPMB_REGION_BLOCKS || + (uint32_t)addr + in_frames > UFS_RPMB_REGION_BLOCKS || + (uint32_t)addr + req_blk_cnt > UFS_RPMB_REGION_BLOCKS) { + result =3D UFS_RPMB_RES_ADDR_FAILURE; + } else if (req_blk_cnt > max_rw) { + result =3D UFS_RPMB_RES_GENERAL_FAILURE; + } else { + result =3D UFS_RPMB_RES_OK; + ok =3D true; + } + if (r->write_counter =3D=3D 0xffffffff) { + result |=3D UFS_RPMB_RES_COUNT_EXPIRED; + } + + if (!ok) { + memset(&out_frames[0], 0, sizeof(out_frames[0])); + memcpy(out_frames[0].nonce, r->resp.nonce, UFS_RPMB_NONCE_LEN); + out_frames[0].addr =3D r->resp.addr; + out_frames[0].block_count =3D cpu_to_be16(in_frames); + out_frames[0].result =3D cpu_to_be16(result); + out_frames[0].req_resp =3D cpu_to_be16(UFS_RPMB_RESP_AUTH_DATA_REA= D); + if (r->key_set) { + ufs_rpmb_calc_hmac(r, &out_frames[0], 1, out_frames[0].key_mac= ); + } + for (i =3D 1; i < in_frames; i++) { + out_frames[i] =3D out_frames[0]; + } + r->resp =3D out_frames[0]; + return; + } + + ufs_rpmb_ensure_storage(r); + for (i =3D 0; i < in_frames; i++) { + memset(&out_frames[i], 0, sizeof(out_frames[i])); + memcpy(out_frames[i].nonce, r->resp.nonce, UFS_RPMB_NONCE_LEN); + out_frames[i].addr =3D r->resp.addr; + out_frames[i].block_count =3D cpu_to_be16(in_frames); + out_frames[i].result =3D cpu_to_be16(result); + out_frames[i].req_resp =3D cpu_to_be16(UFS_RPMB_RESP_AUTH_DATA_REA= D); + memcpy(out_frames[i].data, + r->storage + ((uint32_t)addr + i) * UFS_RPMB_DATA_LEN, + UFS_RPMB_DATA_LEN); + } + ufs_rpmb_calc_hmac(r, out_frames, in_frames, + out_frames[in_frames - 1].key_mac); + r->resp =3D out_frames[in_frames - 1]; +} + static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion *r, const UfsRpmbFrame *frames, uint32_t nframes) @@ -43,7 +186,27 @@ static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion= *r, uint16_t result =3D UFS_RPMB_RES_GENERAL_FAILURE; bool sign_resp =3D false; =20 + /* + * JESD220H Sections 12.4.7.3.1 & 12.4.7.3.3 state that any request ot= her + * than Result read request (0x0005) overwrites the Result Register of= the + * RPMB region, and Section 12.4.7.3.4 (Figure 12.8) specifies that wh= en an + * Authenticated Data Read request (0x0004) is received, the device ch= ecks + * the address and block count, fetches the data, sets Result, and com= putes + * the MAC for the 0x0400 response (with Stuff bytes [0:195] and Write + * counter [500:503] set to 0...00h). Because normal multi-frame reads + * defer ufs_rpmb_do_read() until SECURITY PROTOCOL IN supplies the + * allocation frame count, if a Result read request (0x0005) arrives w= hile + * read_pending is still set (no IN was issued), materialize the 1-fra= me + * 0x0400 response into r->resp and clear read_pending so the following + * 1-frame SECURITY PROTOCOL IN returns a complete, signed 0x0400 fram= e. + */ if (req_type =3D=3D UFS_RPMB_REQ_READ_RESULT) { + if (r->read_pending) { + UfsRpmbFrame tmp =3D { 0 }; + + ufs_rpmb_do_read(u, r, 1, &tmp); + r->read_pending =3D false; + } trace_ufs_rpmb_request(req_type, lduw_be_p(&r->resp.result)); return; } @@ -85,6 +248,23 @@ static void ufs_rpmb_handle_req(UfsHc *u, UfsRpmbRegion= *r, sign_resp =3D true; break; =20 + case UFS_RPMB_REQ_AUTH_DATA_WRITE: + r->resp.addr =3D req->addr; + r->resp.req_resp =3D cpu_to_be16(UFS_RPMB_RESP_AUTH_DATA_WRITE); + result =3D ufs_rpmb_do_write(u, r, frames, nframes); + r->resp.write_counter =3D cpu_to_be32(r->write_counter); + sign_resp =3D r->key_set; + break; + + case UFS_RPMB_REQ_AUTH_DATA_READ: + r->read_pending =3D true; + memcpy(r->resp.nonce, req->nonce, UFS_RPMB_NONCE_LEN); + r->resp.addr =3D req->addr; + r->resp.block_count =3D req->block_count; + r->resp.req_resp =3D cpu_to_be16(UFS_RPMB_RESP_AUTH_DATA_READ); + trace_ufs_rpmb_request(req_type, 0); + return; + default: r->resp.req_resp =3D cpu_to_be16(req_type); result =3D UFS_RPMB_RES_GENERAL_FAILURE; @@ -213,6 +393,40 @@ UfsReqResult ufs_rpmb_sec_in(UfsHc *u, UfsRequest *req= , uint8_t *scsi_status, r =3D &u->rpmb[region]; in_frames =3D alloc_len / UFS_RPMB_FRAME_LEN; =20 + if (r->read_pending) { + g_autofree UfsRpmbFrame *out_frames =3D NULL; + uint16_t req_blk_cnt =3D lduw_be_p(&r->resp.block_count); + + /* + * JESD220H Section 12.4.7.2 (Table 12.17; JESD220F Table 12.22) + * specifies an expected data transfer length of 512 * Block Count + * for Authenticated Data Read responses. When the request frame + * left block_count as 0 (as allowed by the Linux RPMB subsystem + * and hw/sd/sd.c), use the transfer frame count from SECURITY + * PROTOCOL IN. + */ + if (req_blk_cnt > 0 && in_frames !=3D req_blk_cnt) { + scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); + *scsi_status =3D CHECK_CONDITION; + return UFS_REQUEST_SUCCESS; + } + if (!ufs_fit_data_buffer(req, alloc_len)) { + return UFS_REQUEST_MISMATCH_DATA_BUF; + } + + r->read_pending =3D false; + out_frames =3D g_new0(UfsRpmbFrame, in_frames); + ufs_rpmb_do_read(u, r, in_frames, out_frames); + + if (dma_buf_read(out_frames, alloc_len, &residual, req->sg, + MEMTXATTRS_UNSPECIFIED) !=3D MEMTX_OK) { + return UFS_REQUEST_FAIL; + } + *transferred_len =3D alloc_len - residual; + *scsi_status =3D GOOD; + return UFS_REQUEST_SUCCESS; + } + if (in_frames !=3D 1) { scsi_build_sense(sense_buf, SENSE_CODE(INVALID_FIELD)); *scsi_status =3D CHECK_CONDITION; diff --git a/hw/ufs/ufs.c b/hw/ufs/ufs.c index bc0f2ce4eb..42fa374fca 100644 --- a/hw/ufs/ufs.c +++ b/hw/ufs/ufs.c @@ -3382,6 +3382,11 @@ void ufs_unrealize(UfsHc *u) u->cq[i] =3D NULL; } } + + for (int i =3D 0; i < UFS_RPMB_MAX_REGIONS; i++) { + g_free(u->rpmb[i].storage); + u->rpmb[i].storage =3D NULL; + } } =20 static bool ufs_bus_check_address(BusState *qbus, DeviceState *qdev, --=20 2.56.0.307.g5fe7bd680c-goog From nobody Fri Nov 20 20:37:58 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1793081037; cv=none; d=zohomail.com; s=zohoarc; b=QeKD6h79xRrDYwHirQf/eS17uFfkmWMAw0zmuqX21rU3dvNOyC7yU0X5wL9lmCVV/LvjeJWWYayEnQO5mJ4KSSzqZZhaBdrmFiZ5vHdg7Tr0lgrsJHamfjHmRXD0mixn8Eq9oDDoQRg73Y51U9yeK/xUTj+XcYn/NwA6EzWeVn4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793081037; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Yft30FkmJDDTZVwy0Ny6JZHOmjsTdVtf/Cg87eODgCY=; b=HhSh90Xzeva9/mt7382EIRggkj+z1fFOzoB49NPYkyzUm5Pr3AkB1hOqiCXShQSArqR2mASUheMHzX7dWnU9jPg+uhPkgDknCP1kJQPQsRgHGdtPCuzSMgVQgMvNRWK61hA0uz7ht/Dr9ZvnJGEkaoigWot0LQlh5pT7TVCCIew= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793081037227591.4047226126851; Mon, 26 Oct 2026 23:03:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLaHH-0008Rd-67; Tue, 27 Oct 2026 02:03:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3YD7gagoKCrstubomfzkivhpphmf.dpnrfnv-efwfmopohov.psh@flex--stanleyjhu.bounces.google.com>) id 1xLaGH-0007sE-4b for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:22 -0400 Received: from mail-pg1-x548.google.com ([2607:f8b0:4864:20::548]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3YD7gagoKCrstubomfzkivhpphmf.dpnrfnv-efwfmopohov.psh@flex--stanleyjhu.bounces.google.com>) id 1xLaGD-0006A2-M5 for qemu-devel@nongnu.org; Tue, 27 Oct 2026 02:02:16 -0400 Received: by mail-pg1-x548.google.com with SMTP id 41be03b00d2f7-cd652c0385bso2338194a12.2 for ; Mon, 26 Oct 2026 23:02:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1793080930; x=1793685730; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Yft30FkmJDDTZVwy0Ny6JZHOmjsTdVtf/Cg87eODgCY=; b=ip6eMoOApHT5z2/D/ZILFpBHt3agZ7cyiXqBvZN5LMMBlCK9AtzRBlZVy6Q0I3l8ag Uwz6Co1My4/IJIMP3+eg9O0C8QG44oi5LWP2D5pYvs+yjn8xLHYGPnw/udzzwl4qikhn 0TES3QMV7hxeGhHF1/iLGlJhltoCQAjpKjjU+5epnnIwgwp00y6XJVU7dRfTyj9i5od4 P9k3FRjkpVBPtlqMGhqUOKS0lja4IjpqVjDvkFmn+tXMS5gPfW6ETiDlRKvN36DkmiYV C/93z5xVkGSBAU5ulR7ZZXG60E3hDS53xGlkp4tYZCb6+ZPKhlb93/eDQzxjZ/aA53ew gzCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793080930; x=1793685730; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Yft30FkmJDDTZVwy0Ny6JZHOmjsTdVtf/Cg87eODgCY=; b=PuFKF62tHMSq2rjZm2jYNbh6viAaaNE0kfvlp3L7A8scXNetHnT+vJ56rt88SpR+2I 4Vj27Oo+dSdhuiCJHSeLVGMcm71euOfsc4quLTz438MGywHQt6cVILSrai7mLtvDWWmM Hn0iQ7xPb3G1fKHxPsRM5D2AJ84c+rkTUD53GEaEkW4X/+h5ule9u68bOY1ymFFrX84Q l2QkQ+OmyIhA/V1C0LzCxu5/ilMc03Uaefsvoj7B8EXTmaIM7eHng/fa7QpAs+coU/Kt HwVyA8aHeG5oL5WZ/EaJs6bAxSZ3wpmXVo/5p+qw8QvFA9WBPcpZivPjkjbLSWU9gQVm gSVA== X-Gm-Message-State: AFq9FYKKaXhGpB3i9nDkXAS0k3RqFdxGu3PCCXWIFxd2jE4Htwx5uuk4 7eNj9d/7C8Z2Ee+ENwQH7EPtTdjCbrErK9g/qPRBB96ZSS8heNLzJD1T6Hr72MEMhKwppFmCN9p uIwF/i3Fm6BU5VaU46zxwzw== X-Received: from pgca14-n1.prod.google.com ([2002:a05:6a02:638e:10b0:cd8:b4f6:1c8f]) (user=stanleyjhu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3c8e:b0:3e2:b295:7678 with SMTP id adf61e73a8af0-3e7bfe01e08mr7489422637.50.1793080928680; Mon, 26 Oct 2026 23:02:08 -0700 (PDT) Date: Tue, 27 Oct 2026 14:01:52 +0800 In-Reply-To: <20261027060153.1842910-1-stanleyjhu@google.com> Mime-Version: 1.0 References: <20261027060153.1842910-1-stanleyjhu@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261027060153.1842910-8-stanleyjhu@google.com> Subject: [PATCH 7/7] tests/qtest/ufs-test: Add RPMB authenticated data read and write test From: Stanley Jhu To: Jeuk Kim , Jeuk Kim Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org, Paolo Bonzini , Fam Zheng , Kevin Wolf , Hanna Reitz , Fabiano Rosas , Laurent Vivier , Pierrick Bouvier , Jinyoung Choi Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::548; envelope-from=3YD7gagoKCrstubomfzkivhpphmf.dpnrfnv-efwfmopohov.psh@flex--stanleyjhu.bounces.google.com; helo=mail-pg1-x548.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1793081038851158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add ufstest_rpmb_read_write() to test single-frame and multi-frame (N =3D 4) authenticated data writes and reads with HMAC-SHA256 verification, Result Read (0x0005) both after a multi-frame read and while a pending 0x0004 read has had no SECURITY PROTOCOL IN (verifying zeroed stuff bytes and zero write_counter per JESD220H Figure 12.8), residual underflow accounting, JESD220H Sections 12.4.7.3.3 and 12.4.7.3.4 check ordering and UFS_RPMB_RES_GENERAL_FAILURE (0x0001) cases, multi-frame reads past region end, SECURITY PROTOCOL IN block count mismatch sense reporting, multi-region key and data isolation across regions 0..3, short-PRDT rejection while read_pending is set, and HCE 1->0->1 reset clearing volatile state while preserving keys, write counters, and stored blocks. Signed-off-by: Stanley Jhu --- tests/qtest/ufs-test.c | 569 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 569 insertions(+) diff --git a/tests/qtest/ufs-test.c b/tests/qtest/ufs-test.c index 9fce5fff82..c118ee86be 100644 --- a/tests/qtest/ufs-test.c +++ b/tests/qtest/ufs-test.c @@ -4361,6 +4361,573 @@ static void ufstest_rpmb_key_counter(void *obj, voi= d *data, ufs_exit(ufs, alloc); } =20 +static void ufstest_rpmb_read_write(void *obj, void *data, + QGuestAllocator *alloc) +{ + QUfs *ufs =3D obj; + UtpUpiuRsp rsp_upiu; + enum UtpOcsCodes ocs; + UfsRpmbFrame req, resp, zero_frame; + UfsRpmbFrame wframes[4], rframes[4]; + uint8_t mac[UFS_RPMB_KEY_MAC_LEN]; + uint8_t zero_mac[UFS_RPMB_KEY_MAC_LEN] =3D { 0 }; + uint8_t cdb[UFS_CDB_SIZE]; + const uint8_t *key =3D ufs_rpmb_test_key; + uint8_t region_keys[UFS_RPMB_MAX_REGIONS][UFS_RPMB_KEY_MAC_LEN]; + int i, j, r; + + ufs_init(ufs, alloc); + memset(&zero_frame, 0, sizeof(zero_frame)); + ufs_rpmb_program_key(ufs, 0, key); + + /* 1. Single-frame authenticated write (block 3, counter 0 -> 1) + rea= d */ + memset(&req, 0, sizeof(req)); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + req.data[i] =3D (uint8_t)(0x5a ^ i); + } + stl_be_p(&req.write_counter, 0); + stw_be_p(&req.addr, 3); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_WRITE); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(lduw_be_p(&resp.addr), =3D=3D, 3); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 1); + ufs_rpmb_compute_hmac(key, &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xc3, sizeof(req.nonce)); + stw_be_p(&req.addr, 3); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(lduw_be_p(&resp.addr), =3D=3D, 3); + g_assert_cmpuint(lduw_be_p(&resp.block_count), =3D=3D, 1); + g_assert_cmpmem(resp.nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + g_assert_cmpuint(resp.data[i], =3D=3D, (uint8_t)(0x5a ^ i)); + } + ufs_rpmb_compute_hmac(key, &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + /* 2. Multi-frame authenticated write (N =3D 4, addr 10..13, wc 1 -> 2= ) */ + memset(wframes, 0, sizeof(wframes)); + for (i =3D 0; i < 4; i++) { + for (j =3D 0; j < UFS_RPMB_DATA_LEN; j++) { + wframes[i].data[j] =3D (uint8_t)((i + 1) * 0x11 + j); + } + stl_be_p(&wframes[i].write_counter, 1); + stw_be_p(&wframes[i].addr, 10); + stw_be_p(&wframes[i].block_count, 4); + stw_be_p(&wframes[i].req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + } + ufs_rpmb_compute_hmac(key, wframes, 4, wframes[3].key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, wframes, 4, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_WRITE); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(lduw_be_p(&resp.addr), =3D=3D, 10); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + ufs_rpmb_compute_hmac(key, &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + /* Multi-frame authenticated read (N =3D 4, addr 10..13) with MAC chec= k */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xd4, sizeof(req.nonce)); + stw_be_p(&req.addr, 10); + stw_be_p(&req.block_count, 4); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(rframes, 0, sizeof(rframes)); + ufs_rpmb_sec_in_frames(ufs, 0, rframes, 4, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + for (i =3D 0; i < 4; i++) { + g_assert_cmpuint(lduw_be_p(&rframes[i].req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&rframes[i].result), =3D=3D, UFS_RPMB_R= ES_OK); + g_assert_cmpuint(lduw_be_p(&rframes[i].addr), =3D=3D, 10); + g_assert_cmpuint(lduw_be_p(&rframes[i].block_count), =3D=3D, 4); + g_assert_cmpmem(rframes[i].nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + g_assert_cmpmem(rframes[i].data, UFS_RPMB_DATA_LEN, + wframes[i].data, UFS_RPMB_DATA_LEN); + if (i < 3) { + g_assert_cmpmem(rframes[i].key_mac, UFS_RPMB_KEY_MAC_LEN, + zero_mac, UFS_RPMB_KEY_MAC_LEN); + } + } + ufs_rpmb_compute_hmac(key, rframes, 4, mac); + g_assert_cmpmem(rframes[3].key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + /* + * READ_RESULT + 1-frame SECURITY PROTOCOL IN after a multi-frame + * AUTH_DATA_READ returns the latched result frame without failing the + * multi-frame block count check. + */ + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpmem(resp.stuff, sizeof(resp.stuff), + zero_frame.stuff, sizeof(resp.stuff)); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 0); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(lduw_be_p(&resp.addr), =3D=3D, 10); + g_assert_cmpuint(lduw_be_p(&resp.block_count), =3D=3D, 4); + g_assert_cmpmem(&resp, sizeof(resp), &rframes[3], sizeof(resp)); + + /* + * Pending AUTH_DATA_READ (OUT 0x0004) immediately followed by READ_RE= SULT + * (OUT 0x0005) + 1-frame SECURITY PROTOCOL IN (512 bytes, with no pri= or IN + * issued for the read) returns a fully initialized, signed 0x0400 fra= me + * with stuff[0..195] =3D=3D 0 and write_counter =3D=3D 0 per JESD220H= Figure 12.8. + */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xd9, sizeof(req.nonce)); + stw_be_p(&req.addr, 3); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpmem(resp.stuff, sizeof(resp.stuff), + zero_frame.stuff, sizeof(resp.stuff)); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 0); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(lduw_be_p(&resp.addr), =3D=3D, 3); + g_assert_cmpuint(lduw_be_p(&resp.block_count), =3D=3D, 1); + g_assert_cmpmem(resp.nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + g_assert_cmpuint(resp.data[i], =3D=3D, (uint8_t)(0x5a ^ i)); + } + ufs_rpmb_compute_hmac(key, &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_IN, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, sizeof(resp)); + ocs =3D ufs_send_scsi_command(ufs, UFS_UPIU_RPMB_WLUN, cdb, + NULL, 0, (uint8_t *)rframes, + 2 * sizeof(UfsRpmbFrame), &rsp_upiu); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_SUCCESS); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(rsp_upiu.header.flags & UFS_UPIU_FLAG_UNDERFLOW, =3D= =3D, + UFS_UPIU_FLAG_UNDERFLOW); + g_assert_cmpuint(be32_to_cpu(rsp_upiu.sr.residual_transfer_count), =3D= =3D, + sizeof(UfsRpmbFrame)); + + /* + * 3. Write check order per JESD220H Section 12.4.7.3.3: address range + * (0x0004) before block count > max (0x0001), MAC (0x0002), and wr= ite + * counter (0x0003), plus N-frame read past region end -> 0x0004 on= all + * returned frames without out-of-bounds read. + */ + memset(&req, 0, sizeof(req)); + stl_be_p(&req.write_counter, 99); + stw_be_p(&req.addr, 32 * UFS_RPMB_BLOCKS_PER_UNIT); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + req.key_mac[0] ^=3D 0xff; + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_ADDR_FA= ILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + /* Bad address + block_count > bRPMB_ReadWriteSize (64) -> 0x0004 */ + memset(&req, 0, sizeof(req)); + stl_be_p(&req.write_counter, 2); + stw_be_p(&req.addr, 32 * UFS_RPMB_BLOCKS_PER_UNIT); + stw_be_p(&req.block_count, 65); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_ADDR_FA= ILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + /* Good address + bad MAC (+ bad counter) -> 0x0002 */ + memset(&req, 0, sizeof(req)); + stl_be_p(&req.write_counter, 99); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + req.key_mac[0] ^=3D 0xff; + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_AUTH_FA= ILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + /* Good address + good MAC + bad counter -> 0x0003 */ + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_COUNT_F= AILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + memset(wframes, 0, sizeof(wframes)); + for (i =3D 0; i < 2; i++) { + stl_be_p(&wframes[i].write_counter, 2); + stw_be_p(&wframes[i].addr, 32 * UFS_RPMB_BLOCKS_PER_UNIT - 1); + stw_be_p(&wframes[i].block_count, 2); + stw_be_p(&wframes[i].req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + } + ufs_rpmb_compute_hmac(key, wframes, 2, wframes[1].key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, wframes, 2, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_ADDR_FA= ILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + /* N-frame read near end of region (block_count=3D0 in OUT, N=3D2 in I= N) */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xe5, sizeof(req.nonce)); + stw_be_p(&req.addr, 32 * UFS_RPMB_BLOCKS_PER_UNIT - 1); + stw_be_p(&req.block_count, 0); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(rframes, 0, sizeof(rframes)); + ufs_rpmb_sec_in_frames(ufs, 0, rframes, 2, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_compute_hmac(key, &rframes[0], 1, mac); + for (i =3D 0; i < 2; i++) { + g_assert_cmpuint(lduw_be_p(&rframes[i].req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&rframes[i].result), =3D=3D, + UFS_RPMB_RES_ADDR_FAILURE); + g_assert_cmpuint(lduw_be_p(&rframes[i].addr), =3D=3D, + 32 * UFS_RPMB_BLOCKS_PER_UNIT - 1); + g_assert_cmpuint(lduw_be_p(&rframes[i].block_count), =3D=3D, 2); + g_assert_cmpmem(rframes[i].nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + g_assert_cmpmem(rframes[i].key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + } + + /* N-frame read near end of region (block_count=3D4 in OUT, N=3D4 in I= N) */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xf6, sizeof(req.nonce)); + stw_be_p(&req.addr, 32 * UFS_RPMB_BLOCKS_PER_UNIT - 2); + stw_be_p(&req.block_count, 4); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(rframes, 0, sizeof(rframes)); + ufs_rpmb_sec_in_frames(ufs, 0, rframes, 4, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_compute_hmac(key, &rframes[0], 1, mac); + for (i =3D 0; i < 4; i++) { + g_assert_cmpuint(lduw_be_p(&rframes[i].req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&rframes[i].result), =3D=3D, + UFS_RPMB_RES_ADDR_FAILURE); + g_assert_cmpuint(lduw_be_p(&rframes[i].addr), =3D=3D, + 32 * UFS_RPMB_BLOCKS_PER_UNIT - 2); + g_assert_cmpuint(lduw_be_p(&rframes[i].block_count), =3D=3D, 4); + g_assert_cmpmem(rframes[i].nonce, UFS_RPMB_NONCE_LEN, + req.nonce, UFS_RPMB_NONCE_LEN); + g_assert_cmpmem(rframes[i].key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + } + + /* + * Read check order per JESD220H Section 12.4.7.3.4: bad address + + * block_count > bRPMB_ReadWriteSize (64) -> 0x0004 (before 0x0001). + */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xf7, sizeof(req.nonce)); + stw_be_p(&req.addr, 32 * UFS_RPMB_BLOCKS_PER_UNIT); + stw_be_p(&req.block_count, 65); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_ADDR_FA= ILURE); + + /* + * UFS_RPMB_RES_GENERAL_FAILURE (0x0001) paths: + * (a) 2-frame write with mismatched addr across frames + * (b) 1-frame write with block_count !=3D nframes + * (c) 0x0004 read with valid addr and block_count > bRPMB_ReadWriteSi= ze + * (d) unknown request type 0x0006 + */ + memset(wframes, 0, sizeof(wframes)); + for (i =3D 0; i < 2; i++) { + stl_be_p(&wframes[i].write_counter, 2); + stw_be_p(&wframes[i].block_count, 2); + stw_be_p(&wframes[i].req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + } + stw_be_p(&wframes[0].addr, 4); + stw_be_p(&wframes[1].addr, 5); + ufs_rpmb_compute_hmac(key, wframes, 2, wframes[1].key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, wframes, 2, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_WRITE); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, + UFS_RPMB_RES_GENERAL_FAILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + memset(&req, 0, sizeof(req)); + stl_be_p(&req.write_counter, 2); + stw_be_p(&req.addr, 4); + stw_be_p(&req.block_count, 2); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_WRITE); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, + UFS_RPMB_RES_GENERAL_FAILURE); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 2); + + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0xf8, sizeof(req.nonce)); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 65); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, + UFS_RPMB_RES_GENERAL_FAILURE); + + memset(&req, 0, sizeof(req)); + stw_be_p(&req.req_resp, 0x0006); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, 0x0006); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, + UFS_RPMB_RES_GENERAL_FAILURE); + + /* + * 4. Authenticated data read SECURITY PROTOCOL IN block count mismatch + * sense check. + */ + memset(&req, 0, sizeof(req)); + stw_be_p(&req.addr, 10); + stw_be_p(&req.block_count, 2); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, CHECK_CONDITION); + g_assert_cmpuint(rsp_upiu.sr.sense_data[2] & 0x0f, =3D=3D, ILLEGAL_REQ= UEST); + g_assert_cmpuint(rsp_upiu.sr.sense_data[12], =3D=3D, 0x24); + g_assert_cmpuint(rsp_upiu.sr.sense_data[13], =3D=3D, 0x00); + + /* + * 5. Multi-region (regions 1..3) key programming and cross-region data + * isolation at block 0. + */ + memset(&req, 0, sizeof(req)); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + req.data[i] =3D (uint8_t)(0xa5 ^ i); + } + stl_be_p(&req.write_counter, 2); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(key, &req, 1, req.key_mac); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + ufs_rpmb_read_result(ufs, 0, &resp); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 3); + + memcpy(region_keys[0], key, UFS_RPMB_KEY_MAC_LEN); + for (r =3D 1; r < UFS_RPMB_MAX_REGIONS; r++) { + for (i =3D 0; i < UFS_RPMB_KEY_MAC_LEN; i++) { + region_keys[r][i] =3D (uint8_t)(key[i] ^ (r * 0x33)); + } + ufs_rpmb_program_key(ufs, r, region_keys[r]); + } + + memset(&req, 0, sizeof(req)); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + req.data[i] =3D (uint8_t)(0x3c ^ i); + } + stl_be_p(&req.write_counter, 0); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_WRITE); + ufs_rpmb_compute_hmac(region_keys[1], &req, 1, req.key_mac); + ufs_rpmb_sec_out_frames(ufs, 1, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_read_result(ufs, 1, &resp); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_WRITE); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 1); + ufs_rpmb_compute_hmac(region_keys[1], &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + for (r =3D 0; r < UFS_RPMB_MAX_REGIONS; r++) { + memset(&req, 0, sizeof(req)); + memset(req.nonce, (uint8_t)(0x90 + r), sizeof(req.nonce)); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, r, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(&resp, 0, sizeof(resp)); + ufs_rpmb_sec_in_frames(ufs, r, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + uint8_t expected =3D 0; + + if (r =3D=3D 0) { + expected =3D (uint8_t)(0xa5 ^ i); + } else if (r =3D=3D 1) { + expected =3D (uint8_t)(0x3c ^ i); + } + g_assert_cmpuint(resp.data[i], =3D=3D, expected); + } + ufs_rpmb_compute_hmac(region_keys[r], &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + } + + /* + * 6. Short-PRDT SECURITY_PROTOCOL_IN (256 bytes) while read_pending is + * set must return UFS_OCS_MISMATCH_DATA_BUF_SIZE without clearing + * read_pending. + */ + memset(&req, 0, sizeof(req)); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_rpmb_build_cdb(cdb, SECURITY_PROTOCOL_IN, UFS_RPMB_SEC_PROTOCOL, + UFS_RPMB_SEC_PROTOCOL_ID, UFS_RPMB_FRAME_LEN); + ocs =3D ufs_rpmb_send_short_prdt(ufs, cdb, UFS_RPMB_FRAME_LEN, 256, fa= lse); + g_assert_cmpuint(ocs, =3D=3D, UFS_OCS_MISMATCH_DATA_BUF_SIZE); + /* read_pending stays armed until a valid IN or reset */ + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(lduw_be_p(&resp.req_resp), =3D=3D, + UFS_RPMB_RESP_AUTH_DATA_READ); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + + /* + * 7. HCE 1->0->1 reset clears volatile read_pending and Result Regist= er + * state while keeping programmed keys, write counters, and stored + * blocks intact. + */ + memset(&req, 0, sizeof(req)); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 2); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 0, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0x5a, sizeof(req.nonce)); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_COUNTER); + ufs_rpmb_sec_out_frames(ufs, 1, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + ufs_wreg(ufs, A_HCE, 0); + ufs_exit(ufs, alloc); + ufs_init(ufs, alloc); + + /* read_pending was 2 frames on region 0, so 1-frame IN returns zero */ + memset(&resp, 0xff, sizeof(resp)); + ufs_rpmb_sec_in_frames(ufs, 0, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpmem(&resp, sizeof(resp), &zero_frame, sizeof(zero_frame)); + + memset(&resp, 0xff, sizeof(resp)); + ufs_rpmb_sec_in_frames(ufs, 1, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpmem(&resp, sizeof(resp), &zero_frame, sizeof(zero_frame)); + + /* Key, write counter (1), and block 0 data on region 1 still persist = */ + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0x3c, sizeof(req.nonce)); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_READ_COUNTER); + ufs_rpmb_sec_out_frames(ufs, 1, &req, 1, &rsp_upiu); + ufs_rpmb_sec_in_frames(ufs, 1, &resp, 1, &rsp_upiu); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + g_assert_cmpuint(ldl_be_p(&resp.write_counter), =3D=3D, 1); + ufs_rpmb_compute_hmac(region_keys[1], &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + memset(&req, 0, sizeof(req)); + memset(req.nonce, 0x7e, sizeof(req.nonce)); + stw_be_p(&req.addr, 0); + stw_be_p(&req.block_count, 1); + stw_be_p(&req.req_resp, UFS_RPMB_REQ_AUTH_DATA_READ); + ufs_rpmb_sec_out_frames(ufs, 1, &req, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + + memset(&resp, 0, sizeof(resp)); + ufs_rpmb_sec_in_frames(ufs, 1, &resp, 1, &rsp_upiu); + g_assert_cmpuint(rsp_upiu.header.scsi_status, =3D=3D, GOOD); + g_assert_cmpuint(lduw_be_p(&resp.result), =3D=3D, UFS_RPMB_RES_OK); + for (i =3D 0; i < UFS_RPMB_DATA_LEN; i++) { + g_assert_cmpuint(resp.data[i], =3D=3D, (uint8_t)(0x3c ^ i)); + } + ufs_rpmb_compute_hmac(region_keys[1], &resp, 1, mac); + g_assert_cmpmem(resp.key_mac, UFS_RPMB_KEY_MAC_LEN, + mac, UFS_RPMB_KEY_MAC_LEN); + + ufs_exit(ufs, alloc); +} + static void ufs_register_nodes(void) { const char *arch; @@ -4458,6 +5025,8 @@ static void ufs_register_nodes(void) &io_test_opts); qos_add_test("rpmb-key-counter", "ufs", ufstest_rpmb_key_counter, &io_test_opts); + qos_add_test("rpmb-read-write", "ufs", ufstest_rpmb_read_write, + &io_test_opts); } =20 libqos_init(ufs_register_nodes); --=20 2.56.0.307.g5fe7bd680c-goog