From patchwork Tue Jul 2 09:20:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= X-Patchwork-Id: 809088 Delivered-To: patch@linaro.org Received: by 2002:adf:a199:0:b0:367:895a:4699 with SMTP id u25csp118066wru; Tue, 2 Jul 2024 02:28:21 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXVuD33Z3acQbUddlPdHy0TX97CbScqFDRYy5bHDrlpmXmNv9PqKQCSzUJk9hyT5ydXZAZ/rSNrRkktl3uvmvm6 X-Google-Smtp-Source: AGHT+IFHKPh7jSPHszfyMU+bHpPmII0W6MDpi6o2z+w6UPmvZj4P5XcZtCI3MvoggLAPuBfcMDOT X-Received: by 2002:a05:6214:300c:b0:6b5:8056:6c80 with SMTP id 6a1803df08f44-6b5b70d3928mr101970876d6.36.1719912501547; Tue, 02 Jul 2024 02:28:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1719912501; cv=none; d=google.com; s=arc-20160816; b=XoibLejepRGijkU8rvoF37u8kc/Y4yYumCo5j+jtc5Q2e4WZNObfUIoZGpTppDe6Q4 A348JuFVLdefDFY9v4qzZ1NedClrx+6CdZ3fEFttNRjbOa5tGLe2WyIB3FP+nFqNSDcN LhRi541YayjI5vDd5Vwmxm0dUg+dbmdB+ZkwjJBSlL5LCoBdw2mqkwtf8mL/ZdP0bWe9 2Z71uMLAKW/rzYN4pHfPaqkHw0J2dfBGZI5DIk+wEtWMAEFCPNLlLA8ZztCNAevvEpAs 4PwkBdYDhfAaiwZVWMiYZD+UZCTH+nmLkPOmyUELA+ac40VnDW3f4bckVaSzYS+M3yw5 SxXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AXCg3w5PVdeJgjz/MXSI7N25wc9KB2lQFNoBJeyQQpg=; fh=/TyiCVzPJVYkT4NtrOYZRXAcrkaiLXA/rwqp4QqIc5M=; b=PXo86qr9/K+dp6mX8c87lBMc5ft05Q8TeOIrCpsDyR1pfth2Ra1aka+f2i3SDr6vYf 6e/NBh0clc6lFtsFnEwor1Xz/E8WH5IeH+NXxGlXAnqT7OCEm0UgyoxbiFEkhwbU4QKm 8dkxI/1zL3qHVB7BXsON3pJ+PXJg+2/B0TcOFMx+SUNvqUTetQRE7xApRFcWQoypn/6m XB/UXkEn9lFDMgZ0YyhOtKR+9zIby61QJzDaiC7ft5XuHTyOj/I86cXUweJ4ciDHXRck g8khvKo23V0popV2Bk9F6mqU/XqXAn/3JFH4mUqmVR4JfsXDG2rRcaf01GiMyxd3xGcN +jPg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Wo4NjNFr; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 6a1803df08f44-6b59e60b635si100490116d6.487.2024.07.02.02.28.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 02 Jul 2024 02:28:21 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Wo4NjNFr; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOZmC-0000qE-3U; Tue, 02 Jul 2024 05:26:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOZm8-0000ZZ-Cd for qemu-devel@nongnu.org; Tue, 02 Jul 2024 05:26:12 -0400 Received: from mail-wr1-x42b.google.com ([2a00:1450:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sOZm4-00018H-B5 for qemu-devel@nongnu.org; Tue, 02 Jul 2024 05:26:10 -0400 Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-3674e1931b7so2694159f8f.2 for ; Tue, 02 Jul 2024 02:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719912366; x=1720517166; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AXCg3w5PVdeJgjz/MXSI7N25wc9KB2lQFNoBJeyQQpg=; b=Wo4NjNFrbZt8LAy5cJ9t/xD+9SAd3llgVHaiad0zYpPCo1rupOIoLWGnvo5HIldG4j ciVNSwEyFqyHotpYfdwAmrto9yahSenITRGTKtN7PmmmvoKy8So0IEZPSYGoRumeyLiL QYhQZs32rercr9Pg9Iop7SHmq5Yj0eyct9OWIysBEwts1Bf47IPkIOgvn/36Vdj9j/U1 DrzsN/Nk3ip/3pEZgT6u2zJFbodPnHtPOVfAX2m9mBE8ThSAWGvMFxjcFePTE0yQp/3B jJ8vkdJxnqcz9NcFlDtzLy3lpUhpHJ9sWDpGDFVAKgfiO2LSksPq0BxjHWqLVYq63ys1 pRMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719912366; x=1720517166; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AXCg3w5PVdeJgjz/MXSI7N25wc9KB2lQFNoBJeyQQpg=; b=GaO8fjjc8rQgfDZaOMtQFQcUSLVjEryeInUNYxQReYNPrZTzHXjvKX2ldQEq6FRQVy HjQCzoQUU2JO5jkDDuWim8r+q22iGdxc2EnseTpo8RnXDBzzkVRmwjnzbmBT8Pz6cxgY 0Hn79cwNG7dDqygJ3zXHsIVq2eXwrBuE6az/8TGmQRXl128W8W/EKek0tg7u7W37Sx7/ CJ34S6ARgBBtJER6C62JF22qmtoBX6B2Sjyz+UzDjgsh8x/O6hahPAkWEyge9EGh8sfQ XGtoxtkG4kl5zmdgEIvpDCBocxLhtRqiYdq5YNCYeywedkfBEBNYrkmLbnv0L4zZQBV+ KqFg== X-Gm-Message-State: AOJu0YxtgZiXWMLXYCdPNym92DDfKAgehH6T1YJwC4aEby52FTKp+qid SZwenz4bSZffbu478ZTcR6FJ50lRq8h4Vj7Ni1mDKqC+qhziLZn+0+aOZFDxSF0Q0skm984186J R X-Received: by 2002:a5d:525c:0:b0:35f:1d40:82fa with SMTP id ffacd0b85a97d-367756ad6f5mr4253182f8f.18.1719912366443; Tue, 02 Jul 2024 02:26:06 -0700 (PDT) Received: from m1x-phil.lan ([176.187.209.58]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3675a103366sm12572103f8f.102.2024.07.02.02.26.05 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 02 Jul 2024 02:26:06 -0700 (PDT) From: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?C?= =?utf-8?q?=C3=A9dric_Le_Goater?= Subject: [PULL 53/67] hw/sd/sdcard: Add sd_cmd_SET/CLR_WRITE_PROT handler (CMD28 & CMD29) Date: Tue, 2 Jul 2024 11:20:36 +0200 Message-ID: <20240702092051.45754-54-philmd@linaro.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240702092051.45754-1-philmd@linaro.org> References: <20240702092051.45754-1-philmd@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42b; envelope-from=philmd@linaro.org; helo=mail-wr1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, 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 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Signed-off-by: Philippe Mathieu-Daudé Tested-by: Cédric Le Goater Reviewed-by: Cédric Le Goater Message-Id: <20240628070216.92609-60-philmd@linaro.org> --- hw/sd/sd.c | 91 +++++++++++++++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/hw/sd/sd.c b/hw/sd/sd.c index e1c799c117..384ce77b36 100644 --- a/hw/sd/sd.c +++ b/hw/sd/sd.c @@ -241,7 +241,6 @@ static const char *sd_cmd_name(SDState *sd, uint8_t cmd) [21] = "DPS_spec", [25] = "WRITE_MULTIPLE_BLOCK", [26] = "MANUF_RSVD", - [28] = "SET_WRITE_PROT", [29] = "CLR_WRITE_PROT", [30] = "SEND_WRITE_PROT", [32] = "ERASE_WR_BLK_START", [33] = "ERASE_WR_BLK_END", [38] = "ERASE", @@ -1512,6 +1511,48 @@ static sd_rsp_type_t sd_cmd_PROGRAM_CSD(SDState *sd, SDRequest req) return sd_cmd_to_receivingdata(sd, req, 0, sizeof(sd->csd)); } +static sd_rsp_type_t sd_cmd_SET_CLR_WRITE_PROT(SDState *sd, SDRequest req, + bool is_write) +{ + uint64_t addr; + + if (sd->size > SDSC_MAX_CAPACITY) { + return sd_illegal; + } + + if (sd->state != sd_transfer_state) { + return sd_invalid_state_for_cmd(sd, req); + } + + addr = sd_req_get_address(sd, req); + if (!address_in_range(sd, is_write ? "SET_WRITE_PROT" : "CLR_WRITE_PROT", + addr, 1)) { + return sd_r1b; + } + + sd->state = sd_programming_state; + if (is_write) { + set_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap); + } else { + clear_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap); + } + /* Bzzzzzzztt .... Operation complete. */ + sd->state = sd_transfer_state; + return sd_r1; +} + +/* CMD28 */ +static sd_rsp_type_t sd_cmd_SET_WRITE_PROT(SDState *sd, SDRequest req) +{ + return sd_cmd_SET_CLR_WRITE_PROT(sd, req, true); +} + +/* CMD29 */ +static sd_rsp_type_t sd_cmd_CLR_WRITE_PROT(SDState *sd, SDRequest req) +{ + return sd_cmd_SET_CLR_WRITE_PROT(sd, req, false); +} + static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) { uint16_t rca; @@ -1602,50 +1643,6 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) return sd_cmd_to_receivingdata(sd, req, 0, sizeof(sd->cid)); /* Write protection (Class 6) */ - case 28: /* CMD28: SET_WRITE_PROT */ - if (sd->size > SDSC_MAX_CAPACITY) { - return sd_illegal; - } - addr = sd_req_get_address(sd, req); - switch (sd->state) { - case sd_transfer_state: - if (!address_in_range(sd, "SET_WRITE_PROT", addr, 1)) { - return sd_r1b; - } - - sd->state = sd_programming_state; - set_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap); - /* Bzzzzzzztt .... Operation complete. */ - sd->state = sd_transfer_state; - return sd_r1b; - - default: - break; - } - break; - - case 29: /* CMD29: CLR_WRITE_PROT */ - if (sd->size > SDSC_MAX_CAPACITY) { - return sd_illegal; - } - addr = sd_req_get_address(sd, req); - switch (sd->state) { - case sd_transfer_state: - if (!address_in_range(sd, "CLR_WRITE_PROT", addr, 1)) { - return sd_r1b; - } - - sd->state = sd_programming_state; - clear_bit(sd_addr_to_wpnum(addr), sd->wp_group_bmap); - /* Bzzzzzzztt .... Operation complete. */ - sd->state = sd_transfer_state; - return sd_r1b; - - default: - break; - } - break; - case 30: /* CMD30: SEND_WRITE_PROT */ if (sd->size > SDSC_MAX_CAPACITY) { return sd_illegal; @@ -2316,6 +2313,8 @@ static const SDProto sd_proto_spi = { [17] = {2, sd_spi, "READ_SINGLE_BLOCK", sd_cmd_READ_SINGLE_BLOCK}, [24] = {4, sd_spi, "WRITE_SINGLE_BLOCK", sd_cmd_WRITE_SINGLE_BLOCK}, [27] = {4, sd_spi, "PROGRAM_CSD", sd_cmd_PROGRAM_CSD}, + [28] = {6, sd_spi, "SET_WRITE_PROT", sd_cmd_SET_WRITE_PROT}, + [29] = {6, sd_spi, "CLR_WRITE_PROT", sd_cmd_CLR_WRITE_PROT}, [34] = {10, sd_spi, "READ_SEC_CMD", sd_cmd_optional}, [35] = {10, sd_spi, "WRITE_SEC_CMD", sd_cmd_optional}, [36] = {10, sd_spi, "SEND_PSI", sd_cmd_optional}, @@ -2354,6 +2353,8 @@ static const SDProto sd_proto_sd = { [23] = {2, sd_ac, "SET_BLOCK_COUNT", sd_cmd_SET_BLOCK_COUNT}, [24] = {4, sd_adtc, "WRITE_SINGLE_BLOCK", sd_cmd_WRITE_SINGLE_BLOCK}, [27] = {4, sd_adtc, "PROGRAM_CSD", sd_cmd_PROGRAM_CSD}, + [28] = {6, sd_ac, "SET_WRITE_PROT", sd_cmd_SET_WRITE_PROT}, + [29] = {6, sd_ac, "CLR_WRITE_PROT", sd_cmd_CLR_WRITE_PROT}, [34] = {10, sd_adtc, "READ_SEC_CMD", sd_cmd_optional}, [35] = {10, sd_adtc, "WRITE_SEC_CMD", sd_cmd_optional}, [36] = {10, sd_adtc, "SEND_PSI", sd_cmd_optional},