From patchwork Tue Dec 19 17:40:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 181148 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2105599dyi; Tue, 19 Dec 2023 09:41:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IFay0SgttAP7wlvoiCN8YJ9snQp/CjsG8xdDSGogmVN7KfbuwdzOdS8SkichvG2x5aefLmg X-Received: by 2002:ac8:588c:0:b0:427:851a:dde4 with SMTP id t12-20020ac8588c000000b00427851adde4mr5794qta.115.1703007668603; Tue, 19 Dec 2023 09:41:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703007668; cv=none; d=google.com; s=arc-20160816; b=elSkNn+kAwA2JMHTyE4h3PPtWFTizqF4DFgj+vMdM9jFY0bbqobnFHSCPrxLF9fP2N sR6B55wk1jD0boYqxsvCa9RSPgYd/kc6hBvzMxJGhcCPDC+MK+WOrReM1dKT29cMGSts FIZf/OTFPkEQHtDEvM/wi3wRkPe2csbIIBpkqAsKSmL8ReCVX5qKVVKJ9I+U1/p/I5Fc u1bq9Rqo6Rux37r4ytvlwHWTAXQDleICETaKoyYZOO6lBiQ5ToFi2yBcdhbCtnqujGPq JcTybnEGa5g+dUcfr6P60F0L0/5iRyZbT+7yrSKtRl9pFH9mQdkgl3HWu9A1R3qWvjIB IdWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=WUAvVSVhxfpG8xQnwKnlglz0LVM7Vn2JPxTl2XGCxyQ=; fh=dfLNqWOtm0UaHb8GCHMuBSVlbZCi7eV095mQO5nOqs8=; b=MgqOabLKBUaw80P6uwipvCb2Qi14HCeP85jEIvjLXP8V9H9ClJ719Wl+uLn/xX50k7 Z86l0/Qs484JdvtNLf8eXsWEtwWmxkWnTyLZ9/n2PQ0lLFkxM5ZC14wKhQQSQ0JQ1Vhb uQ9qj1VjiuDmFNMkuj5XLNeH35uI2nyHlbleYRy8w1xIJTJTuuy7KeNRmL6rOOW5SMOV aZh98WvsoOy+ARN6C8ImBvyuyE66MAAXjKcl0wyEcIMXCp59DHVm1Jn4NU4ATQ/395wH zGaAOCG8cOHKvZSNNkOlApmebpUzVL5YDBakSX0hhLhqAs//AYqzF7I4ExZyTkzUzGWX N9hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=GjaGNKMF; spf=pass (google.com: domain of linux-kernel+bounces-5704-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5704-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id k2-20020ac85fc2000000b00427781fd7e2si1612869qta.137.2023.12.19.09.41.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:41:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5704-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=GjaGNKMF; spf=pass (google.com: domain of linux-kernel+bounces-5704-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5704-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5939E1C23832 for ; Tue, 19 Dec 2023 17:41:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D30B237D01; Tue, 19 Dec 2023 17:40:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GjaGNKMF" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 350E037D18; Tue, 19 Dec 2023 17:40:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f48.google.com with SMTP id a640c23a62f3a-a2331e7058aso416904766b.2; Tue, 19 Dec 2023 09:40:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703007638; x=1703612438; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=WUAvVSVhxfpG8xQnwKnlglz0LVM7Vn2JPxTl2XGCxyQ=; b=GjaGNKMFhNze/ROkjNXef+t5EepiThV8hhyj7dbE2NA8AR0aR4IxFypP3TUxYIRDfn 4bohsXvJ6aVWyOJLiRWz5W/KrKDlQlWtUZLQ2nqpDKhikhXweHNxqoI9fI1i5Z20pw5R B3IsWqxBWuzPCIJUXbLVEfMI6PQShHrKf84AHvvSIi1OcYmM9pSXU1S4DQnxmtyeGVJ+ Kjr2QVOQKXPDbAybBM6CRVpD2hRG3Otie3tBsFpWFajzDqNN68F9jFj3zPk6Bqjcaz1t 85woP8ctCwYh63fRFjLRX+S0Kat8lkABcKKwBlxf0Az0GhhyKpTZ1tcZOC1FSZtM1CNx RPbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703007638; x=1703612438; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WUAvVSVhxfpG8xQnwKnlglz0LVM7Vn2JPxTl2XGCxyQ=; b=Gqz+quv4mPlNPK+gV+66oZkRJE5fd1iOjKd0nsdsQUymSxCv4cmPcTqoSDTaymQkAj Y21lJflFbLuNNTIJhRNEQyr2Do1Yh9B4X7Qeb2U4upaqU0D26I9dPJa5350BZr/g4pyn r0eaWwyZ0+7767+Ff3dH0Xe0d6a8r/m2qEsdRccXkNxSnnnlCdW2ZXra9FTVwkRmvHLI MyhO9hlAq9GbD+0ki4SY7DzLnay/h7AAEnoam6EcgBD8yIi+K9lBSBLdsTRklBhwnbiU XV3HYBFPsFZ+3L9Z46mcQsO6P+++po85CDNN/PAOm7vuyVE+4r2BVW5Z+y1xZV6ewzb5 vB/g== X-Gm-Message-State: AOJu0Yws5TAKrkP0ccYk3NaIZfsGrU41dmi9YSey8whxco8BlcKno687 nMQiZb6zZTHtlUAtdZp9Nik= X-Received: by 2002:a17:907:764c:b0:a26:8ab0:43b8 with SMTP id kj12-20020a170907764c00b00a268ab043b8mr48795ejc.41.1703007638248; Tue, 19 Dec 2023 09:40:38 -0800 (PST) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id tx27-20020a1709078e9b00b00a1c85124b08sm15834840ejc.94.2023.12.19.09.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:40:37 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Greg Kroah-Hartman , Michael Walle , Miquel Raynal , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, u-boot@lists.denx.de, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 1/5] dt-bindings: nvmem: layouts: add U-Boot environment variables layout Date: Tue, 19 Dec 2023 18:40:21 +0100 Message-Id: <20231219174025.15228-1-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785732968764880068 X-GMAIL-MSGID: 1785732968764880068 From: Rafał Miłecki U-Boot env data is a way of storing firmware variables. It's a format that can be used of top of various storage devices. Its binding should be an NVMEM layout instead of a standalone device. This patch adds layout binding which allows using it on top of MTD NVMEM device as well as any other. At the same time it deprecates the old combined binding. Signed-off-by: Rafał Miłecki --- V2: Add "-layout" suffix to compatibles to avoid conflict .../nvmem/layouts/u-boot,env-layout.yaml | 55 +++++++++++++++++++ .../devicetree/bindings/nvmem/u-boot,env.yaml | 6 ++ 2 files changed, 61 insertions(+) create mode 100644 Documentation/devicetree/bindings/nvmem/layouts/u-boot,env-layout.yaml diff --git a/Documentation/devicetree/bindings/nvmem/layouts/u-boot,env-layout.yaml b/Documentation/devicetree/bindings/nvmem/layouts/u-boot,env-layout.yaml new file mode 100644 index 000000000000..3b4d8f2a44e1 --- /dev/null +++ b/Documentation/devicetree/bindings/nvmem/layouts/u-boot,env-layout.yaml @@ -0,0 +1,55 @@ +# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/nvmem/layouts/u-boot,env-layout.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: NVMEM layout of U-Boot environment variables + +maintainers: + - Rafał Miłecki + +description: + U-Boot uses environment variables to store device parameters and + configuration. They may be used for booting process, setup or keeping end user + info. + + Data is stored using U-Boot specific formats (variant specific header and NUL + separated key-value pairs). + +properties: + compatible: + oneOf: + - description: A standalone env data block + const: u-boot,env-layout + - description: Two redundant blocks with active one flagged + const: u-boot,env-redundant-bool-layout + - description: Two redundant blocks with active having higher counter + const: u-boot,env-redundant-count-layout + - description: Broadcom's variant with custom header + const: brcm,env-layout + +additionalProperties: false + +examples: + - | + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + reg = <0x0 0x40000>; + label = "u-boot"; + read-only; + }; + + partition@40000 { + reg = <0x40000 0x10000>; + label = "u-boot-env"; + + nvmem-layout { + compatible = "u-boot,env-layout"; + }; + }; + }; diff --git a/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml b/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml index 9c36afc7084b..6c2a3ca5f051 100644 --- a/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +++ b/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml @@ -26,9 +26,15 @@ description: | Variables can be defined as NVMEM device subnodes. + Introduction of NVMEM layouts exposed a limitation of this binding design. + Description of NVMEM data content should be separated from NVMEM devices. + Since the introduction of U-Boot env data layout this binding is deprecated. + maintainers: - Rafał Miłecki +deprecated: true + properties: compatible: oneOf: From patchwork Tue Dec 19 17:40:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 181149 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2105693dyi; Tue, 19 Dec 2023 09:41:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IHMCggPjGOX4bmkrcjI34gpNRbZ8ysP6JsP06VepRFHwdiAkRotussl8x19EWuDPqkQLM2c X-Received: by 2002:a05:6e02:20ee:b0:35d:a39f:8ff9 with SMTP id q14-20020a056e0220ee00b0035da39f8ff9mr20038781ilv.11.1703007677304; Tue, 19 Dec 2023 09:41:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703007677; cv=none; d=google.com; s=arc-20160816; b=yKPpO1kpQLduRstHS4m8VnYHTbXcolGJ9mo6b4s0g8BhWyS4vAdv44IdGu0Zpf9J3V uTVM3v9lmz5s2+OF5Y8dXvu6K0W3HCZDNHAfpaJyC4U5GYJ+RA4zTu1UDTfHucKOrh0s ktpAIKPuqXr8mtwY5gtJZpT1M5jnt6pPvl6hIE2vVWvqFq5NgVkp75V6ezzxZhkOUwi8 /QoXWc5q6M6kzABhYOFUkM1hZglviVmHuFtZbudz+rbxk0qBXgo9WWJTj+Z+arChaC6a ISYdp041kWA723a4JyW+n446xhFSl30RTMCOyPh5O0tSNuE03tziXLMjPSQ2HCg2r75x bA1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=HS/utwklj/mQdHgmYltSxcVR9bMR4HdMDBmcgJXrZDU=; fh=dfLNqWOtm0UaHb8GCHMuBSVlbZCi7eV095mQO5nOqs8=; b=zdHbGbllWfuqcwCXlqapn+nBy4rjpP2SMrHFpKqUciocFxQGSThmHi5CYKlV7i90qu SEnKIGe7C3Rj9840EJTDIIfCQY3RLpQceSpzaCkDo4Qg6djC9OJltt7IM+mb2BLnFHwY SI/6E0DXZgXVGNQzSLkF4WwlCTpeDAHd7DGE81xBBYEdPY/8ts864qrqTLrsuuCLwDWE BvauPbs/q15IMks0h15fcx2BuEr7yf4QzEq77bEW55VvOnVsmsnN2fTWjAud34R2T3Hi b9CTVZU7iMFlBGHE1CbJa8CU7R0Dzi+5Z+4xJXE7u9yY29eauKgRV6H5kRmc2myBwSRT MLig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HhhI5cMS; spf=pass (google.com: domain of linux-kernel+bounces-5705-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5705-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c64-20020a633543000000b005b95ee3edc6si4556331pga.628.2023.12.19.09.41.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:41:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5705-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HhhI5cMS; spf=pass (google.com: domain of linux-kernel+bounces-5705-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5705-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id F35C1283768 for ; Tue, 19 Dec 2023 17:41:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 25E763455D; Tue, 19 Dec 2023 17:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HhhI5cMS" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4663437D1A; Tue, 19 Dec 2023 17:40:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a236d2cc372so221492266b.3; Tue, 19 Dec 2023 09:40:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703007640; x=1703612440; darn=vger.kernel.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=HS/utwklj/mQdHgmYltSxcVR9bMR4HdMDBmcgJXrZDU=; b=HhhI5cMSl5XcCLfTqX963wYSwRYBLiiRdOlWc6oZ8wLAfNpVoZTPH9QpwVBnN9NS3Y EOGTArgNl4MQtcvZr4RJXcMj1iHGHBz8I5KyRRU7K9UdAhuEe6GbfKr9Vy3mktrrBtZS es5NJ2ZT44IkPwTSt67A/rMcTd0K0vdIw0qVSjXLCel9dKvHhhIun30vDztyl63TfWjc 9/6DHx4NR3zr/URTHc7t4DWjde/HZA8Z0MD/PSvFQskAz2qbEo8qzFDYJnpAIUE5uxee IB6bqb0QsH4l7FhaH7DX/mUXqF5BlP4CoeXWmUtesucxcbOT8ZaoKpGBPoNI6JGzldtC WLcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703007640; x=1703612440; 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=HS/utwklj/mQdHgmYltSxcVR9bMR4HdMDBmcgJXrZDU=; b=Ln7zhZx0m8FlJ0vSzMJ+B33nyjSDEZv6hXmq3TNX7mAOyoUxzUVuXIxoXtnaJ2s1M7 Sf32snT7g3T+Fxop+nFeBIfy0puGKwb1bHLyDFTdqiKIQjFWFeTMOi12yhhKJpaEaS8B 4Jy1j1HtrF51kdFRhDXTV0dDJoiRwM85mv7wgqwzhDTxz45i/6r/jr7hCv/TDmgp+IGV EHi6YImLwgenuv1lGkPMVLQosVZ/LyeT7gHvjoeEEDuVaiQ1FcthsiNhwYP2fNxupNWI hvsBXv/7N/3phdeetNf3/iHt0QUAL4jF7dI9mJmL9VRuZWNwuuZTQ/XrUB0pYgY3opCt 3f+g== X-Gm-Message-State: AOJu0YxgZE6K+9WchTZrvxXHMEOJheKz8qx1bjhE7wWmTpJTlCzSjKrK FtHfEkKiQIvrCTSCBcG0Z8w= X-Received: by 2002:a17:906:74d0:b0:a26:8c0f:b0ca with SMTP id z16-20020a17090674d000b00a268c0fb0camr53272ejl.75.1703007640440; Tue, 19 Dec 2023 09:40:40 -0800 (PST) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id tx27-20020a1709078e9b00b00a1c85124b08sm15834840ejc.94.2023.12.19.09.40.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:40:40 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Greg Kroah-Hartman , Michael Walle , Miquel Raynal , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, u-boot@lists.denx.de, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 2/5] nvmem: core: add nvmem_dev_size() helper Date: Tue, 19 Dec 2023 18:40:22 +0100 Message-Id: <20231219174025.15228-2-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231219174025.15228-1-zajec5@gmail.com> References: <20231219174025.15228-1-zajec5@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785732978329651199 X-GMAIL-MSGID: 1785732978329651199 From: Rafał Miłecki This is required by layouts that need to read whole NVMEM content. It's especially useful for NVMEM devices without hardcoded layout (like U-Boot environment data block). Signed-off-by: Rafał Miłecki Reviewed-by: Miquel Raynal --- drivers/nvmem/core.c | 13 +++++++++++++ include/linux/nvmem-consumer.h | 1 + 2 files changed, 14 insertions(+) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 4ed54076346d..980123fb4dde 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -2163,6 +2163,19 @@ const char *nvmem_dev_name(struct nvmem_device *nvmem) } EXPORT_SYMBOL_GPL(nvmem_dev_name); +/** + * nvmem_dev_size() - Get the size of a given nvmem device. + * + * @nvmem: nvmem device. + * + * Return: size of the nvmem device. + */ +size_t nvmem_dev_size(struct nvmem_device *nvmem) +{ + return nvmem->size; +} +EXPORT_SYMBOL_GPL(nvmem_dev_size); + static int __init nvmem_init(void) { int ret; diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h index 2d306fa13b1a..34c0e58dfa26 100644 --- a/include/linux/nvmem-consumer.h +++ b/include/linux/nvmem-consumer.h @@ -81,6 +81,7 @@ int nvmem_device_cell_write(struct nvmem_device *nvmem, struct nvmem_cell_info *info, void *buf); const char *nvmem_dev_name(struct nvmem_device *nvmem); +size_t nvmem_dev_size(struct nvmem_device *nvmem); void nvmem_add_cell_lookups(struct nvmem_cell_lookup *entries, size_t nentries); From patchwork Tue Dec 19 17:40:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 181150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2106065dyi; Tue, 19 Dec 2023 09:41:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IH8LuKAZyv6PRiXjqQpL+DKgbbOiQWtf+M1a7rdsfXgoEmHmn7fLmHOgXZpNO+v7Spt4Dxb X-Received: by 2002:ac8:5753:0:b0:425:7f2b:3df2 with SMTP id 19-20020ac85753000000b004257f2b3df2mr2348427qtx.33.1703007716195; Tue, 19 Dec 2023 09:41:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703007716; cv=none; d=google.com; s=arc-20160816; b=D9PhGGB3tFX8V4EQ1btV+TW6+z59bgSgpvrSYddJCP2SlFpcmv96rLIZP8Km9PxVjB cqKQyqi68TuRPlBl6Je4Ksv2ds0lGSRDrVV2aR/hCjHVXPhNT1HS8ER4B8KFBSOZ+OT9 8O7UhJ0CmD2WRy0QY3ZqgvoMKq5mIRxEjhQUD+lLI5Ir0V7pKa1PkRm/bLWaEmbTgVqk MmoJNbn1K5MzMHBhTDZokI65cp6Gl8T1OuwnAYANxO77rVC4Ip0sseflQPWjUP2oCNZ/ uOtYNWhfm0L/yMg2VQ90tkgdHk8hiQeNhW3oFaUf1zluY7PRzFRNW6p/h+a4JMZQIycx sfMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=0ycf7wNvbkEl/9uPO9wSLuwfT5/8x2RoUUgzU3Md07E=; fh=dfLNqWOtm0UaHb8GCHMuBSVlbZCi7eV095mQO5nOqs8=; b=t8EKlYDMiqlCepDvfbQoxunDQ2R3/TLx+hALmPxh1k6TZHzwUgjYf56QQDm1nFo4ZQ ijCnViOH/XyO7FZOeM4aahsmP745fIKflbV+DtRzcbtCYNN0d5gN1KO2W6ltIwK5wyxo gUNoBTyIUyHdov3TIXtVobxBJEQkMjbUevGjap3XYqvmQu3AAljBbP8KlsenYRbvdGEH 86sDoPzQIVsSOIup0myz5i1qfCGZ6VmF9q3y8gO6OFKBRBbPeZw9bh1G3T9qpNehFZ9V swmh1YuG/ahlN1tDkdCaTcDhjHdZaZLhgLqnvG1dTqKTuLPJOwMeqqb37+IR2FZICkvz jwDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QVeTALbw; spf=pass (google.com: domain of linux-kernel+bounces-5707-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5707-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id x9-20020a05622a000900b00425f05c405asi15700173qtw.767.2023.12.19.09.41.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:41:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5707-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=QVeTALbw; spf=pass (google.com: domain of linux-kernel+bounces-5707-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5707-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E16A61C236E1 for ; Tue, 19 Dec 2023 17:41:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 990C538FBB; Tue, 19 Dec 2023 17:40:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QVeTALbw" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C40A0374E7; Tue, 19 Dec 2023 17:40:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-50e384cd6ebso3095066e87.3; Tue, 19 Dec 2023 09:40:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703007642; x=1703612442; darn=vger.kernel.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=0ycf7wNvbkEl/9uPO9wSLuwfT5/8x2RoUUgzU3Md07E=; b=QVeTALbwg0y+ZzLZCJuPv5qtmfDGKJdRHQ3Y/lJ9+/kxYZoDPiXROjP3cceapSlOA/ B4T5joQuQ10cC9rKmPRPj0x7WmGa7JXuLGhdVT7S3YRRTZuPSBH7uHuQkbJJRfIZjRMv J5Tt2wqkeeXRzRCc1WtY4a03EPZlYHPbG1nD6xFJLcwmI5iGwgttRVqGD9ytNPcerYLn c1a20awoPCajb2Z6NItiXjKHCfNeNStcGVvvi2BZLpv9RmZkngElko+SCNRBJurBlAaQ XwyTR5YfVzzFAKgd+jdyE92y+XW8ctLB3Jn5QU9sCHZB3sp1dDa2v0uLwlsVJh298h2g JiQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703007642; x=1703612442; 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=0ycf7wNvbkEl/9uPO9wSLuwfT5/8x2RoUUgzU3Md07E=; b=AedGyicT3ymK6t6xWTQ+HNn7eiVTZK3GZcmR0Q1oJAKAOAOW0mr3/5R03cQH7baB5n 55ZUG/UPyxBJyJaRAdXNN4sb2EGIld8cOriydM42Mp87x3NYavjvUhOzfx8kkrSFZvmx jiZWEuGa77BOWWzm+zukDtWKpwZ1qOT9qAa4GLpsWvJyNHEV/i71sgXJYEjssfpxhdEY AynPlo3FK4RyyK+2auUsGsWdBF53YHN2nnfV/cxy12hDWqBJZOQ6AfD2sRLSuCpk+r8z /oBBoW5h0Es8gUEGcbJUguWKjbH9949TVNLz3LEmL7I20Y+zOHF0jeUVEtVAy1fLbg+g H2lQ== X-Gm-Message-State: AOJu0YyNAUrbhiPQfSh+t/4U980NE51gIPPO9InbxcMFXK6ATTXFa989 1Ci/Sxe8c/LJT0xfi5e7BgI= X-Received: by 2002:ac2:4e11:0:b0:50b:eadf:f175 with SMTP id e17-20020ac24e11000000b0050beadff175mr6460266lfr.91.1703007642136; Tue, 19 Dec 2023 09:40:42 -0800 (PST) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id tx27-20020a1709078e9b00b00a1c85124b08sm15834840ejc.94.2023.12.19.09.40.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:40:41 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Greg Kroah-Hartman , Michael Walle , Miquel Raynal , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, u-boot@lists.denx.de, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 3/5] nvmem: u-boot-env: use more nvmem subsystem helpers Date: Tue, 19 Dec 2023 18:40:23 +0100 Message-Id: <20231219174025.15228-3-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231219174025.15228-1-zajec5@gmail.com> References: <20231219174025.15228-1-zajec5@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785733019161654588 X-GMAIL-MSGID: 1785733019161654588 From: Rafał Miłecki 1. Use nvmem_dev_size() and nvmem_device_read() to make this driver less mtd dependent 2. Use nvmem_add_one_cell() to simplify adding NVMEM cells Signed-off-by: Rafał Miłecki Reviewed-by: Miquel Raynal --- V2: Don't introduce memleak when handling nvmem_device_read() failures drivers/nvmem/u-boot-env.c | 78 ++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c index c4ae94af4af7..111905189341 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/u-boot-env.c @@ -23,13 +23,10 @@ enum u_boot_env_format { struct u_boot_env { struct device *dev; + struct nvmem_device *nvmem; enum u_boot_env_format format; struct mtd_info *mtd; - - /* Cells */ - struct nvmem_cell_info *cells; - int ncells; }; struct u_boot_env_image_single { @@ -94,70 +91,70 @@ static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, i static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf, size_t data_offset, size_t data_len) { + struct nvmem_device *nvmem = priv->nvmem; struct device *dev = priv->dev; char *data = buf + data_offset; char *var, *value, *eq; - int idx; - - priv->ncells = 0; - for (var = data; var < data + data_len && *var; var += strlen(var) + 1) - priv->ncells++; - - priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL); - if (!priv->cells) - return -ENOMEM; - for (var = data, idx = 0; + for (var = data; var < data + data_len && *var; - var = value + strlen(value) + 1, idx++) { + var = value + strlen(value) + 1) { + struct nvmem_cell_info info = {}; + eq = strchr(var, '='); if (!eq) break; *eq = '\0'; value = eq + 1; - priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL); - if (!priv->cells[idx].name) + info.name = devm_kstrdup(dev, var, GFP_KERNEL); + if (!info.name) return -ENOMEM; - priv->cells[idx].offset = data_offset + value - data; - priv->cells[idx].bytes = strlen(value); - priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name); + info.offset = data_offset + value - data; + info.bytes = strlen(value); + info.np = of_get_child_by_name(dev->of_node, info.name); if (!strcmp(var, "ethaddr")) { - priv->cells[idx].raw_len = strlen(value); - priv->cells[idx].bytes = ETH_ALEN; - priv->cells[idx].read_post_process = u_boot_env_read_post_process_ethaddr; + info.raw_len = strlen(value); + info.bytes = ETH_ALEN; + info.read_post_process = u_boot_env_read_post_process_ethaddr; } - } - if (WARN_ON(idx != priv->ncells)) - priv->ncells = idx; + nvmem_add_one_cell(nvmem, &info); + } return 0; } static int u_boot_env_parse(struct u_boot_env *priv) { + struct nvmem_device *nvmem = priv->nvmem; struct device *dev = priv->dev; size_t crc32_data_offset; size_t crc32_data_len; size_t crc32_offset; size_t data_offset; size_t data_len; + size_t dev_size; uint32_t crc32; uint32_t calc; - size_t bytes; uint8_t *buf; + int bytes; int err; - buf = kcalloc(1, priv->mtd->size, GFP_KERNEL); + dev_size = nvmem_dev_size(nvmem); + + buf = kcalloc(1, dev_size, GFP_KERNEL); if (!buf) { err = -ENOMEM; goto err_out; } - err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf); - if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) { - dev_err(dev, "Failed to read from mtd: %d\n", err); + bytes = nvmem_device_read(nvmem, 0, dev_size, buf); + if (bytes < 0) { + err = bytes; + goto err_kfree; + } else if (bytes != dev_size) { + err = -EIO; goto err_kfree; } @@ -179,8 +176,8 @@ static int u_boot_env_parse(struct u_boot_env *priv) break; } crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset)); - crc32_data_len = priv->mtd->size - crc32_data_offset; - data_len = priv->mtd->size - data_offset; + crc32_data_len = dev_size - crc32_data_offset; + data_len = dev_size - data_offset; calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; if (calc != crc32) { @@ -189,7 +186,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) goto err_kfree; } - buf[priv->mtd->size - 1] = '\0'; + buf[dev_size - 1] = '\0'; err = u_boot_env_add_cells(priv, buf, data_offset, data_len); if (err) dev_err(dev, "Failed to add cells: %d\n", err); @@ -209,7 +206,6 @@ static int u_boot_env_probe(struct platform_device *pdev) struct device *dev = &pdev->dev; struct device_node *np = dev->of_node; struct u_boot_env *priv; - int err; priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); if (!priv) @@ -224,17 +220,15 @@ static int u_boot_env_probe(struct platform_device *pdev) return PTR_ERR(priv->mtd); } - err = u_boot_env_parse(priv); - if (err) - return err; - config.dev = dev; - config.cells = priv->cells; - config.ncells = priv->ncells; config.priv = priv; config.size = priv->mtd->size; - return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); + priv->nvmem = devm_nvmem_register(dev, &config); + if (IS_ERR(priv->nvmem)) + return PTR_ERR(priv->nvmem); + + return u_boot_env_parse(priv); } static const struct of_device_id u_boot_env_of_match_table[] = { From patchwork Tue Dec 19 17:40:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 181162 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2111593dyi; Tue, 19 Dec 2023 09:51:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IF822U7jKEHXbjW40GlhqEVEtF6LbqG42i0yVsp8ZewN9Z4sxN+N7a9NxFOOyMyk3qeeNqt X-Received: by 2002:a05:6512:3c8d:b0:50e:4146:dcf8 with SMTP id h13-20020a0565123c8d00b0050e4146dcf8mr1389458lfv.133.1703008269596; Tue, 19 Dec 2023 09:51:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703008269; cv=none; d=google.com; s=arc-20160816; b=OlW43MeXLrizuYcWqYM7stRzq5Reiqrg09obCzTR6FuZFZuRFUFuvKI1EFkDhKBu9E jKKXHJG5PnKFbmY3tOziQ+uoy2hGNWfveTSyW/k/U6cfT4E2GTpVwWCWfERTgDHb1ZfF RF+YwfIdfJTucK4L/G/B8V+GMBh6edCwPcHWyjstlwyHordokpRaOHlo/MPF7hObMxKl iU1RFrGx8nxzvJYNlU/L2Qoa55nz3a0IR8wK3mVoi3VkCN1FIQcNnLLSnZ56PaetCCdb 9v9dLnAyOIM2ViUDaVb7fgoL+ePbvmLWFPrNDNktXn/x1Rn/WD1WKrpg6jof0rBxd9GI 16xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=lvbpVGdOkFoSIrqGEOI2u72GgRBjNjQRomAfCiRlrrE=; fh=dfLNqWOtm0UaHb8GCHMuBSVlbZCi7eV095mQO5nOqs8=; b=qmUoL56eX3vM2mwkLuhc3ubf7cofdOOzjVtWVC1tR0IV0okH5QqCS9kf7yIhBGq1a7 Bn+rTQnd88Qm1UVMOqmGh6Tj4T8Gdw6oZX3jXwv/L0J2leQn7grW0ufign7Rch3VHuyS Mp9UJZrW5kfLUiMLUXcdWeZwNoUffFNWPRTTiEzplDybaKbUlQBEMLOXIXcBpQ/GY8Yw TQbOXZ1Rl2srdK10+xZzXQhF7uiHsyqGDzFYzV/MUqALBrpeQ2Keu4SnQI0qiXgrp5cC f1CjAYX7SVYJ4YyX2qsCwMPUrfDHT9L9aO8OJMk78d2/oFZQQNGKcVYhPo3whxIpT0sP KJug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jL598i1n; spf=pass (google.com: domain of linux-kernel+bounces-5706-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5706-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id e18-20020a170906045200b00a24901b6057si631769eja.392.2023.12.19.09.51.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:51:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5706-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=jL598i1n; spf=pass (google.com: domain of linux-kernel+bounces-5706-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5706-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id F320F1F27663 for ; Tue, 19 Dec 2023 17:41:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0D4E238F85; Tue, 19 Dec 2023 17:40:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jL598i1n" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9E6137D02; Tue, 19 Dec 2023 17:40:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-a235500d0e1so322067266b.2; Tue, 19 Dec 2023 09:40:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703007644; x=1703612444; darn=vger.kernel.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=lvbpVGdOkFoSIrqGEOI2u72GgRBjNjQRomAfCiRlrrE=; b=jL598i1nG6qrlZgp6X8/F0jOHeTsKWpXzkau1Thp1dbXEsq+V7ECjJ+7Kf+p8e29E6 9UwsDwK7JBDiV9Dvs4KCg/vIaRgA0rBTSpe3kaC+Xu9pB0HX9xfdJbJf61gEucwlhXjL 1vWLlFZMunozo8iA2yrRGdflpgk3MWwJyVsfBqRDKNxE7KddLH7Kj4tNzLCJRyEyCdCI RalV3SJaKRft8kVthhNg90CmnEpSFOsnqNYH5RRwwm/WewdSXg577K8BubxjqS3F5GIc g4QpRzIn8pWXMz/2fpVEI9+u7m67VQIzOnT8Og2sxUMQ6j+cOM6BmwUSFNlXRi3Njx06 fufQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703007644; x=1703612444; 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=lvbpVGdOkFoSIrqGEOI2u72GgRBjNjQRomAfCiRlrrE=; b=Cj7cwxG9dYiKNqB3EaWkZBDr1kyRhTQiE83cseP0XH4y+pAU45E1pUYrdYOjQeG8jd nJY73abE5CRnDopl4Lj1dtKwMG/eT3bNrkrKjLUy/VNLq5ek7BfPGHshhJ3umzg/JBCe Z5l28cS0OymiE/IUkL1w+gm47fopBJu+R8SF7y4k1LaByq/7HsVP6n1jZnpz8xb+s6DG CLE+GrJBNRWUuYe5yEaB+XGIpJnbdYLvqu/GWgJzOTewV2Z8MWXy+Yzp4uUhM5meyL5T 2TVN4zQNStH0q9a9ozABlKkkhdh/32aedo9R0GQh4N5bVwL1Ls7+yrsEV3LcHksQ7wf6 hR1w== X-Gm-Message-State: AOJu0YyaIKBY3SvZkYOpc5cK6y1w79Z38379K5Vd5tBPIC4SkPvXiPI8 qQG3oJAzDdBuFmQ7gGWn+c8= X-Received: by 2002:a17:906:da04:b0:a26:8919:6d99 with SMTP id fi4-20020a170906da0400b00a2689196d99mr274634ejb.2.1703007643985; Tue, 19 Dec 2023 09:40:43 -0800 (PST) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id tx27-20020a1709078e9b00b00a1c85124b08sm15834840ejc.94.2023.12.19.09.40.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:40:43 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Greg Kroah-Hartman , Michael Walle , Miquel Raynal , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, u-boot@lists.denx.de, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 4/5] nvmem: u-boot-env: improve coding style Date: Tue, 19 Dec 2023 18:40:24 +0100 Message-Id: <20231219174025.15228-4-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231219174025.15228-1-zajec5@gmail.com> References: <20231219174025.15228-1-zajec5@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785733599248838565 X-GMAIL-MSGID: 1785733599248838565 From: Rafał Miłecki 1. Prefer kzalloc() over kcalloc() See memory-allocation.rst which says: "to be on the safe side it's best to use routines that set memory to zero, like kzalloc()" 2. Drop dev_err() for u_boot_env_add_cells() fail It can fail only on -ENOMEM. We don't want to print error then. 3. Add extra "crc32_addr" variable It makes code reading header's crc32 easier to understand / review. Signed-off-by: Rafał Miłecki Reviewed-by: Miquel Raynal --- V2: New patch with suggestions from Miquel drivers/nvmem/u-boot-env.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c index 111905189341..befbab156cda 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/u-boot-env.c @@ -132,6 +132,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) size_t crc32_data_offset; size_t crc32_data_len; size_t crc32_offset; + __le32 *crc32_addr; size_t data_offset; size_t data_len; size_t dev_size; @@ -143,7 +144,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) dev_size = nvmem_dev_size(nvmem); - buf = kcalloc(1, dev_size, GFP_KERNEL); + buf = kzalloc(dev_size, GFP_KERNEL); if (!buf) { err = -ENOMEM; goto err_out; @@ -175,7 +176,8 @@ static int u_boot_env_parse(struct u_boot_env *priv) data_offset = offsetof(struct u_boot_env_image_broadcom, data); break; } - crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset)); + crc32_addr = (__le32 *)(buf + crc32_offset); + crc32 = le32_to_cpu(*crc32_addr); crc32_data_len = dev_size - crc32_data_offset; data_len = dev_size - data_offset; @@ -188,8 +190,6 @@ static int u_boot_env_parse(struct u_boot_env *priv) buf[dev_size - 1] = '\0'; err = u_boot_env_add_cells(priv, buf, data_offset, data_len); - if (err) - dev_err(dev, "Failed to add cells: %d\n", err); err_kfree: kfree(buf); From patchwork Tue Dec 19 17:40:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 181151 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp2106167dyi; Tue, 19 Dec 2023 09:42:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHx+gehBiVOZQJO0+0AT8bjp3eZPlxxFMwR+jweXMlgGiSaD4Dx5aMIAVcJH0p9zPpNEUHB X-Received: by 2002:a05:6358:7e45:b0:170:f329:74bb with SMTP id p5-20020a0563587e4500b00170f32974bbmr12054837rwm.44.1703007724967; Tue, 19 Dec 2023 09:42:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703007724; cv=none; d=google.com; s=arc-20160816; b=iTFGwsl5b/1oKNdElJ5AjQ4zEhxivJeVb80yOAvOxQDUOzwHjGO+/ykNonkYfBxSpR pyWTOid2BhnYhSTJJEyIHJ5YxmPRqu9MMVCIDh5gk2lKt4so9BYvcObbYYXqovY5I6Y1 yPj6VyksDhl7vqCwHcoYC9o/HfzdcDQTsnZe85SqLjxz779VHExBwMYtEu1nMmD6cNYF dauxBVuZ30To1h607Xn7lPht1oemjpAqrfDEbCf2Y1JA6V6EnQna1DH+BtuE+6tV8Sn3 qHHAizxWeDDm6sC+t5K4yOSKNnB2WES5Iya8/FOAE/xcxMDgFhAvKG1mHB2bfMUEE+Xr M9Mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=h6I0LaF/FwsE9y8VwBtVa8qJgT7A2jlM6idtIufP+ic=; fh=dfLNqWOtm0UaHb8GCHMuBSVlbZCi7eV095mQO5nOqs8=; b=I2NUNlnhhX2fIvyuu3ZZH6Mnm9Wb6Yz0AHtDC/3RSZddFv37jCCflDXMoBXfqPU0td NZKRGrrXs26b3v9tGsCc9QIWteyp9QcWba0FTrUTYno25H80F2r5l0ERdvHJ0zALe3ux +S+Mta5OwZzjLIVmFZzKQX5RYODWTgGftJoPRqUZE8d/bwYslgYKpuB1FStgACD48A4L 61pnB6SJokWZw3yPKezC2IQiIw0bFFh2tuQLVCtramN5ZgAV8FiK0JDHCzUQSvw6t/Sm JtRUfOqS8d/yJY6p3JdVCND+DxoiuijJfszsrJfzEA84gvR9MqAeaHN0CHm0DMp1X53a 4x3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WsacLE8a; spf=pass (google.com: domain of linux-kernel+bounces-5708-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5708-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id q12-20020a056a00084c00b006d883ac33d8si3009511pfk.177.2023.12.19.09.42.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:42:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-5708-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=WsacLE8a; spf=pass (google.com: domain of linux-kernel+bounces-5708-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-5708-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id B21D028397D for ; Tue, 19 Dec 2023 17:42:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 10CB5341BA; Tue, 19 Dec 2023 17:40:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WsacLE8a" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A3C737D29; Tue, 19 Dec 2023 17:40:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-f43.google.com with SMTP id a640c23a62f3a-a2335d81693so641134466b.0; Tue, 19 Dec 2023 09:40:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703007646; x=1703612446; darn=vger.kernel.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=h6I0LaF/FwsE9y8VwBtVa8qJgT7A2jlM6idtIufP+ic=; b=WsacLE8a4ESwzZMDKZB8uua0+pxF1LgAaiKLOEPIH9V4Doqic/EocwOyJ5/d+01WEM mdvj2bsYb8xp7RVxAfUpKmECwwo7J1V4EkSJgD13JcFFPQFUzzXcb+z0TeoNQHZvkYUx MMECv7XTkfkfFK9FLtSJc4XK3Wl2mOo6Ltd0GWdNUmT/W9d5WDrnu1YIz0IE5/xV/GNa 93XchbjeelQwTECY21ZjbJ7etS88ggm1M/zNJBryieKs12oHLuKGlHQy9uXzEL+xHHty WDoECUygCnCvot9oC79Hge5882mYbKlsqsG3OvfE/JbSwLt/iwPZJ0IyGVY6lUYHVFqP aTdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703007646; x=1703612446; 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=h6I0LaF/FwsE9y8VwBtVa8qJgT7A2jlM6idtIufP+ic=; b=HM92kf5dRnVVNtXdJ6bOFJxJwZm4m5y469vjoP8/TRFDazwsJ82sSOskj38ab4KLSk ZSlTrZuhrb0ac0MlurszpmbFi+Aru1kIL+t6/CA2cwry/jPjzgNBAu+G9MCI+DLc8m87 40rrulfSfSsrMSbZDIberx3lqUhNVRdH9lS1Z+hEHbChpvTC0hSVPMB1I7h610B5qY7w I1ZMrJqRMnyy5JaVNypZPbr4gBcOU+OO6UccyO4lETktW2SqAPp2FgLCAQsIPV8P/jH0 FIAeeZT8+vyXoBg7QEfNpWILx3SgXdWMBq9xECXzFTRQ6L64tJsG9R202UAbIRznV+vE chsQ== X-Gm-Message-State: AOJu0YxnU7U665qy4b5G1pj4uS7ZTSHyIY/OU9hhBGhvEfXV5nLXHvk7 I31o8n6U827+GvMFpPazaMj4px078nA= X-Received: by 2002:a17:906:8459:b0:a23:62fd:e2f6 with SMTP id e25-20020a170906845900b00a2362fde2f6mr1278856ejy.30.1703007645592; Tue, 19 Dec 2023 09:40:45 -0800 (PST) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id tx27-20020a1709078e9b00b00a1c85124b08sm15834840ejc.94.2023.12.19.09.40.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 09:40:45 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Greg Kroah-Hartman , Michael Walle , Miquel Raynal , linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, u-boot@lists.denx.de, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2 5/5] nvmem: layouts: add U-Boot env layout Date: Tue, 19 Dec 2023 18:40:25 +0100 Message-Id: <20231219174025.15228-5-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231219174025.15228-1-zajec5@gmail.com> References: <20231219174025.15228-1-zajec5@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785733028004961997 X-GMAIL-MSGID: 1785733028004961997 From: Rafał Miłecki This patch moves all generic (NVMEM devices independent) code from NVMEM device driver to NVMEM layout driver. Then it adds a simple NVMEM layout code on top of it. Thanks to proper layout it's possible to support U-Boot env data stored on any kind of NVMEM device. For backward compatibility with old DT bindings we need to keep old NVMEM device driver functional. To avoid code duplication a parsing function is exported and reused in it. Signed-off-by: Rafał Miłecki Reviewed-by: Miquel Raynal --- V2: Support new compatibles & use device_get_match_data() helper IMPORTANT: This is based on top of the: [PATCH v6.8 1/2] nvmem: layouts: refactor .add_cells() callback arguments MAINTAINERS | 1 + drivers/nvmem/Kconfig | 3 +- drivers/nvmem/layouts/Kconfig | 11 ++ drivers/nvmem/layouts/Makefile | 1 + drivers/nvmem/layouts/u-boot-env.c | 204 +++++++++++++++++++++++++++++ drivers/nvmem/layouts/u-boot-env.h | 15 +++ drivers/nvmem/u-boot-env.c | 158 +--------------------- 7 files changed, 235 insertions(+), 158 deletions(-) create mode 100644 drivers/nvmem/layouts/u-boot-env.c create mode 100644 drivers/nvmem/layouts/u-boot-env.h diff --git a/MAINTAINERS b/MAINTAINERS index b589218605b4..1f7e6d74cd51 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22282,6 +22282,7 @@ U-BOOT ENVIRONMENT VARIABLES M: Rafał Miłecki S: Maintained F: Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +F: drivers/nvmem/layouts/u-boot-env.c F: drivers/nvmem/u-boot-env.c UACCE ACCELERATOR FRAMEWORK diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index 283134498fbc..d2c384f58028 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -363,8 +363,7 @@ config NVMEM_SUNXI_SID config NVMEM_U_BOOT_ENV tristate "U-Boot environment variables support" depends on OF && MTD - select CRC32 - select GENERIC_NET_UTILS + select NVMEM_LAYOUT_U_BOOT_ENV help U-Boot stores its setup as environment variables. This driver adds support for verifying & exporting such data. It also exposes variables diff --git a/drivers/nvmem/layouts/Kconfig b/drivers/nvmem/layouts/Kconfig index 9c6e672fc350..5e586dfebe47 100644 --- a/drivers/nvmem/layouts/Kconfig +++ b/drivers/nvmem/layouts/Kconfig @@ -26,6 +26,17 @@ config NVMEM_LAYOUT_ONIE_TLV If unsure, say N. +config NVMEM_LAYOUT_U_BOOT_ENV + tristate "U-Boot environment variables layout" + select CRC32 + select GENERIC_NET_UTILS + help + U-Boot stores its setup as environment variables. This driver adds + support for verifying & exporting such data. It also exposes variables + as NVMEM cells so they can be referenced by other drivers. + + If unsure, say N. + endmenu endif diff --git a/drivers/nvmem/layouts/Makefile b/drivers/nvmem/layouts/Makefile index 2974bd7d33ed..4940c9db0665 100644 --- a/drivers/nvmem/layouts/Makefile +++ b/drivers/nvmem/layouts/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o obj-$(CONFIG_NVMEM_LAYOUT_ONIE_TLV) += onie-tlv.o +obj-$(CONFIG_NVMEM_LAYOUT_U_BOOT_ENV) += u-boot-env.o diff --git a/drivers/nvmem/layouts/u-boot-env.c b/drivers/nvmem/layouts/u-boot-env.c new file mode 100644 index 000000000000..dcd2ffed503c --- /dev/null +++ b/drivers/nvmem/layouts/u-boot-env.c @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022 - 2023 Rafał Miłecki + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "u-boot-env.h" + +struct u_boot_env_image_single { + __le32 crc32; + uint8_t data[]; +} __packed; + +struct u_boot_env_image_redundant { + __le32 crc32; + u8 mark; + uint8_t data[]; +} __packed; + +struct u_boot_env_image_broadcom { + __le32 magic; + __le32 len; + __le32 crc32; + DECLARE_FLEX_ARRAY(uint8_t, data); +} __packed; + +static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index, + unsigned int offset, void *buf, size_t bytes) +{ + u8 mac[ETH_ALEN]; + + if (bytes != 3 * ETH_ALEN - 1) + return -EINVAL; + + if (!mac_pton(buf, mac)) + return -EINVAL; + + if (index) + eth_addr_add(mac, index); + + ether_addr_copy(buf, mac); + + return 0; +} + +static int u_boot_env_parse_cells(struct device *dev, struct nvmem_device *nvmem, uint8_t *buf, + size_t data_offset, size_t data_len) +{ + char *data = buf + data_offset; + char *var, *value, *eq; + + for (var = data; + var < data + data_len && *var; + var = value + strlen(value) + 1) { + struct nvmem_cell_info info = {}; + + eq = strchr(var, '='); + if (!eq) + break; + *eq = '\0'; + value = eq + 1; + + info.name = devm_kstrdup(dev, var, GFP_KERNEL); + if (!info.name) + return -ENOMEM; + info.offset = data_offset + value - data; + info.bytes = strlen(value); + info.np = of_get_child_by_name(dev->of_node, info.name); + if (!strcmp(var, "ethaddr")) { + info.raw_len = strlen(value); + info.bytes = ETH_ALEN; + info.read_post_process = u_boot_env_read_post_process_ethaddr; + } + + nvmem_add_one_cell(nvmem, &info); + } + + return 0; +} + +int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem, + enum u_boot_env_format format) +{ + size_t crc32_data_offset; + size_t crc32_data_len; + size_t crc32_offset; + __le32 *crc32_addr; + size_t data_offset; + size_t data_len; + size_t dev_size; + uint32_t crc32; + uint32_t calc; + uint8_t *buf; + int bytes; + int err; + + dev_size = nvmem_dev_size(nvmem); + + buf = kzalloc(dev_size, GFP_KERNEL); + if (!buf) { + err = -ENOMEM; + goto err_out; + } + + bytes = nvmem_device_read(nvmem, 0, dev_size, buf); + if (bytes < 0) { + err = bytes; + goto err_kfree; + } else if (bytes != dev_size) { + err = -EIO; + goto err_kfree; + } + + switch (format) { + case U_BOOT_FORMAT_SINGLE: + crc32_offset = offsetof(struct u_boot_env_image_single, crc32); + crc32_data_offset = offsetof(struct u_boot_env_image_single, data); + data_offset = offsetof(struct u_boot_env_image_single, data); + break; + case U_BOOT_FORMAT_REDUNDANT: + crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32); + crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data); + data_offset = offsetof(struct u_boot_env_image_redundant, data); + break; + case U_BOOT_FORMAT_BROADCOM: + crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32); + crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data); + data_offset = offsetof(struct u_boot_env_image_broadcom, data); + break; + } + crc32_addr = (__le32 *)(buf + crc32_offset); + crc32 = le32_to_cpu(*crc32_addr); + crc32_data_len = dev_size - crc32_data_offset; + data_len = dev_size - data_offset; + + calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; + if (calc != crc32) { + dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32); + err = -EINVAL; + goto err_kfree; + } + + buf[dev_size - 1] = '\0'; + err = u_boot_env_parse_cells(dev, nvmem, buf, data_offset, data_len); + +err_kfree: + kfree(buf); +err_out: + return err; +} +EXPORT_SYMBOL_GPL(u_boot_env_parse); + +static int u_boot_env_add_cells(struct nvmem_layout *layout) +{ + struct device *dev = &layout->dev; + enum u_boot_env_format format; + + format = (uintptr_t)device_get_match_data(dev); + + return u_boot_env_parse(dev, layout->nvmem, format); +} + +static int u_boot_env_probe(struct nvmem_layout *layout) +{ + layout->add_cells = u_boot_env_add_cells; + + return nvmem_layout_register(layout); +} + +static void u_boot_env_remove(struct nvmem_layout *layout) +{ + nvmem_layout_unregister(layout); +} + +static const struct of_device_id u_boot_env_of_match_table[] = { + { .compatible = "u-boot,env-layout", .data = (void *)U_BOOT_FORMAT_SINGLE, }, + { .compatible = "u-boot,env-redundant-bool-layout", .data = (void *)U_BOOT_FORMAT_REDUNDANT, }, + { .compatible = "u-boot,env-redundant-count-layout", .data = (void *)U_BOOT_FORMAT_REDUNDANT, }, + { .compatible = "brcm,env-layout", .data = (void *)U_BOOT_FORMAT_BROADCOM, }, + {}, +}; + +static struct nvmem_layout_driver u_boot_env_layout = { + .driver = { + .owner = THIS_MODULE, + .name = "u-boot-env-layout", + .of_match_table = u_boot_env_of_match_table, + }, + .probe = u_boot_env_probe, + .remove = u_boot_env_remove, +}; +module_nvmem_layout_driver(u_boot_env_layout); + +MODULE_AUTHOR("Rafał Miłecki"); +MODULE_LICENSE("GPL"); +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table); diff --git a/drivers/nvmem/layouts/u-boot-env.h b/drivers/nvmem/layouts/u-boot-env.h new file mode 100644 index 000000000000..dd5f280ac047 --- /dev/null +++ b/drivers/nvmem/layouts/u-boot-env.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H +#define _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H + +enum u_boot_env_format { + U_BOOT_FORMAT_SINGLE, + U_BOOT_FORMAT_REDUNDANT, + U_BOOT_FORMAT_BROADCOM, +}; + +int u_boot_env_parse(struct device *dev, struct nvmem_device *nvmem, + enum u_boot_env_format format); + +#endif /* ifndef _LINUX_NVMEM_LAYOUTS_U_BOOT_ENV_H */ diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c index befbab156cda..386b2b255c30 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/u-boot-env.c @@ -3,23 +3,15 @@ * Copyright (C) 2022 Rafał Miłecki */ -#include -#include -#include #include #include #include -#include #include #include #include #include -enum u_boot_env_format { - U_BOOT_FORMAT_SINGLE, - U_BOOT_FORMAT_REDUNDANT, - U_BOOT_FORMAT_BROADCOM, -}; +#include "layouts/u-boot-env.h" struct u_boot_env { struct device *dev; @@ -29,24 +21,6 @@ struct u_boot_env { struct mtd_info *mtd; }; -struct u_boot_env_image_single { - __le32 crc32; - uint8_t data[]; -} __packed; - -struct u_boot_env_image_redundant { - __le32 crc32; - u8 mark; - uint8_t data[]; -} __packed; - -struct u_boot_env_image_broadcom { - __le32 magic; - __le32 len; - __le32 crc32; - DECLARE_FLEX_ARRAY(uint8_t, data); -} __packed; - static int u_boot_env_read(void *context, unsigned int offset, void *val, size_t bytes) { @@ -69,134 +43,6 @@ static int u_boot_env_read(void *context, unsigned int offset, void *val, return 0; } -static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index, - unsigned int offset, void *buf, size_t bytes) -{ - u8 mac[ETH_ALEN]; - - if (bytes != 3 * ETH_ALEN - 1) - return -EINVAL; - - if (!mac_pton(buf, mac)) - return -EINVAL; - - if (index) - eth_addr_add(mac, index); - - ether_addr_copy(buf, mac); - - return 0; -} - -static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf, - size_t data_offset, size_t data_len) -{ - struct nvmem_device *nvmem = priv->nvmem; - struct device *dev = priv->dev; - char *data = buf + data_offset; - char *var, *value, *eq; - - for (var = data; - var < data + data_len && *var; - var = value + strlen(value) + 1) { - struct nvmem_cell_info info = {}; - - eq = strchr(var, '='); - if (!eq) - break; - *eq = '\0'; - value = eq + 1; - - info.name = devm_kstrdup(dev, var, GFP_KERNEL); - if (!info.name) - return -ENOMEM; - info.offset = data_offset + value - data; - info.bytes = strlen(value); - info.np = of_get_child_by_name(dev->of_node, info.name); - if (!strcmp(var, "ethaddr")) { - info.raw_len = strlen(value); - info.bytes = ETH_ALEN; - info.read_post_process = u_boot_env_read_post_process_ethaddr; - } - - nvmem_add_one_cell(nvmem, &info); - } - - return 0; -} - -static int u_boot_env_parse(struct u_boot_env *priv) -{ - struct nvmem_device *nvmem = priv->nvmem; - struct device *dev = priv->dev; - size_t crc32_data_offset; - size_t crc32_data_len; - size_t crc32_offset; - __le32 *crc32_addr; - size_t data_offset; - size_t data_len; - size_t dev_size; - uint32_t crc32; - uint32_t calc; - uint8_t *buf; - int bytes; - int err; - - dev_size = nvmem_dev_size(nvmem); - - buf = kzalloc(dev_size, GFP_KERNEL); - if (!buf) { - err = -ENOMEM; - goto err_out; - } - - bytes = nvmem_device_read(nvmem, 0, dev_size, buf); - if (bytes < 0) { - err = bytes; - goto err_kfree; - } else if (bytes != dev_size) { - err = -EIO; - goto err_kfree; - } - - switch (priv->format) { - case U_BOOT_FORMAT_SINGLE: - crc32_offset = offsetof(struct u_boot_env_image_single, crc32); - crc32_data_offset = offsetof(struct u_boot_env_image_single, data); - data_offset = offsetof(struct u_boot_env_image_single, data); - break; - case U_BOOT_FORMAT_REDUNDANT: - crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32); - crc32_data_offset = offsetof(struct u_boot_env_image_redundant, data); - data_offset = offsetof(struct u_boot_env_image_redundant, data); - break; - case U_BOOT_FORMAT_BROADCOM: - crc32_offset = offsetof(struct u_boot_env_image_broadcom, crc32); - crc32_data_offset = offsetof(struct u_boot_env_image_broadcom, data); - data_offset = offsetof(struct u_boot_env_image_broadcom, data); - break; - } - crc32_addr = (__le32 *)(buf + crc32_offset); - crc32 = le32_to_cpu(*crc32_addr); - crc32_data_len = dev_size - crc32_data_offset; - data_len = dev_size - data_offset; - - calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; - if (calc != crc32) { - dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32); - err = -EINVAL; - goto err_kfree; - } - - buf[dev_size - 1] = '\0'; - err = u_boot_env_add_cells(priv, buf, data_offset, data_len); - -err_kfree: - kfree(buf); -err_out: - return err; -} - static int u_boot_env_probe(struct platform_device *pdev) { struct nvmem_config config = { @@ -228,7 +74,7 @@ static int u_boot_env_probe(struct platform_device *pdev) if (IS_ERR(priv->nvmem)) return PTR_ERR(priv->nvmem); - return u_boot_env_parse(priv); + return u_boot_env_parse(dev, priv->nvmem, priv->format); } static const struct of_device_id u_boot_env_of_match_table[] = {