From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A40723394B for ; Sat, 30 May 2026 20:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780174438; cv=none; b=RCNeaFOK6ui1cGYP/vXxHxyl0kq30Q3msQYpAD1VvzcFFCGeQFj6Tn4A4iHAhmre/kLz6Ros5Ol+splHWxqG6Wt5UmoxtclFsQe8nN8FNbykH5IWRFSGAxkcUgRcnrIjqbbtS+Qg3OBhjVtgBDDIIwxsO0ihKkpydT20ldzP9YY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780174438; c=relaxed/simple; bh=TNHYGkyp3xfyVIxY2ECO0+lD4n56o0D2pDpgc5UuGaE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iu9/l9kdKT4ljH/Ft9bxUBiKUotsknJHI/5lrEYl8Fmp1PXyWH1McxZelrvtG4TdnkPUarqDDBh3yK8wjmz7m7z7qiUbYMohYtF4/SQ7ZqwiKhMHxDGdG1kjIhEWiudh8scrBuWnuQgEW8eB0LLhnWhW5LOemz2CHOjjYJhCwxE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d1z1yTVm; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="d1z1yTVm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EB631F00898; Sat, 30 May 2026 20:53:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1780174434; bh=M8j3m8SOPvYuDNHwpjx/2B7iWxxbeE81C+bhO3kYu/s=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=d1z1yTVmHhn1wiOjVVHMU7Slm/SiFDmeo9kAquVAJuDtjtIFpJFC21y476GGd4N0Y EluNBu8cfBlzduAeZrKEWc6KxKQ5JqXT1E63MaCymR3a9qAfj9fHrVCJrfG+fKwcmp kVfv9xq5tXPnfayvwENR7uZ4omYODF061KZEvHJ3nK5QjwF4U9PCN9mdn3hF6VkSES FB8knmZ7RH4/C0rsyQ/6nClCVdVofFzSMl9MCWbIzcki5btkrmj3Qd35AZpFfw8YI0 5Jw2apDXybZQ4lELcw+BGfn3rf0O7f7uyOh/SODvte7pSiTrQFWTr5puUour6IuOTM jYLbPAdj8SbIw== From: srini@kernel.org To: gregkh@linuxfoundation.org Cc: linux-kernel@vger.kernel.org, Marek Vasut , Bartosz Golaszewski , Srinivas Kandagatla Subject: [PATCH 03/14] nvmem: core: Default to read-only if wp-gpios present Date: Sat, 30 May 2026 21:53:22 +0100 Message-ID: <20260530205333.117458-4-srini@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260530205333.117458-1-srini@kernel.org> References: <20260530205333.117458-1-srini@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Marek Vasut In case the nvmem DT node contains "wp-gpios" DT property, the device currently defaults to read-write and the force_ro sysfs attribute reads 0. Switch to the default read-only, which is both safer, and aligned with eMMC HW BOOT partition force_ro sysfs attribute behavior, which also defaults to read-only. The adjustment of nvmem->read_only value to read-only in case wp-gpios DT property is present must be done only after the device_add() got called because device_add() does internally call nvmem_bin_attr_get_umode(), which configures the permissions of 'nvmem' bin attr based on the value of nvmem->read_only that is only parsed from DT property 'read-only', without any adjustment. This way, if DT property 'read-only' is present, the 'nvmem' attribute is always read-only. Otherwise, if the device is writeable, then 'nvmem' attribute is writeable, and nvmem->read_only defaults to read-only, but can be switched to read-write at runtime via the 'force_ro' attribute. The updated behavior can be tested as follows: Current content: " $ cat /sys/bus/nvmem/devices/logging7/force_ro 1 $ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem 00000000 66 6f 6f 0a ff ff ff ff " Write into default-read-only device: " $ echo bar > /sys/bus/nvmem/devices/logging7/nvmem bash: echo: write error: Operation not permitted $ cat /sys/bus/nvmem/devices/logging7/force_ro 1 " Unlock and write into device: " $ echo 0 > /sys/bus/nvmem/devices/logging7/force_ro $ cat /sys/bus/nvmem/devices/logging7/force_ro 0 $ echo bar > /sys/bus/nvmem/devices/logging7/nvmem $ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem 00000000 62 61 72 0a ff ff ff ff " Relock and write into device, fails because device is read-only again: " $ echo 1 > /sys/bus/nvmem/devices/logging7/force_ro $ echo baz > /sys/bus/nvmem/devices/logging7/nvmem bash: echo: write error: Operation not permitted $ hexdump -C /sys/bus/nvmem/devices/logging7/nvmem 00000000 62 61 72 0a ff ff ff ff " Reviewed-by: Bartosz Golaszewski Signed-off-by: Marek Vasut Signed-off-by: Srinivas Kandagatla --- drivers/nvmem/core.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 311cb2e5a5c0..be28a366f603 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -1019,6 +1019,10 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_dev; + /* If the device has WP GPIO, default to read-only */ + if (nvmem->wp_gpio) + nvmem->read_only = true; + #ifdef CONFIG_NVMEM_SYSFS rval = nvmem_populate_sysfs_cells(nvmem); if (rval) -- 2.53.0