From patchwork Mon Jul 24 08:26:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 125075 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1888629vqg; Mon, 24 Jul 2023 08:53:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlGG3zaCggzbtqTDFQwQDCEPz6MBs312TUi+QYYQtq254Ql+6/odjee2BgKaK8dBaB9jZ4pH X-Received: by 2002:a17:907:7858:b0:994:1844:caec with SMTP id lb24-20020a170907785800b009941844caecmr7910351ejc.77.1690213992286; Mon, 24 Jul 2023 08:53:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690213992; cv=none; d=google.com; s=arc-20160816; b=bM7pMKNdqcW1lOtJDAo/hl2+/FXmLTqTwKdFtpzCbXcbqvR3sa0doCk+q5DRoyKx0x FPdGCjoozn7fydXdmZfkPgz0SzFmRFgyZTl7E2NkLoaCBc//lheXAHn5G396tUUUbJED MZMEz7shwkOFvpykgafPZs/wG+fh/cuPoxBfAv0QNSdmadnubgs9euvZhmzq/wFMILZH RzXYJbGSpTkwimF47J2w90Wz0/1BkOXzEaWusGPDCbi+ip4JXpk3QLiFT62tQ/71Odl4 eNGEiQco5uat0v0uSU+6kRKv5DbeG3eOUqiBiCddNmkVDFZKyFJo25VVnUcklBMwAfW9 KoMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=TJ7QSuT9JWuSM03vOOl8cZyXJ2t2WtKznVcR/4lhGHU=; fh=LpK/+DU52e2A6LHcsmnq9BYHnjjjRVFCHa6LQ3LP2l8=; b=BL47SA5mS25D2DW6l6JyFeUyV3Mf7qkEaFtpxqSpIgpW9WBMuEoMXHq1LkfKWhr9Ow qNELPZfc6H6vI8BAAigIz4FW5TUPs8DYwum0pkGxuzp9XVuaBTBL1nSENF+tYwbhFiki yIgMSOYu4m6sTLxGskVVUD7ef4zpDDGrCzL3jW06OQrpOlLtiHQoXaXVOI1Z4i/ULdzy FMs3dJOsp3+Iw25A93vKIbNWsW2bstY/GoTGz+dtG9wc/2ARY3LsyYaXpcAB6kmjB97v jvbAzzY6LiXWAhp0su7RPFfBRUDYlzV+D+TvFpnxYYJ9AVW1xmq/tsnIAIkc4hZIbvly /7vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=OSYHJkFk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ce14-20020a170906b24e00b00988565fbf63si7305432ejb.437.2023.07.24.08.52.47; Mon, 24 Jul 2023 08:53:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=OSYHJkFk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231349AbjGXPcY (ORCPT + 99 others); Mon, 24 Jul 2023 11:32:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjGXPcX (ORCPT ); Mon, 24 Jul 2023 11:32:23 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E5151B3; Mon, 24 Jul 2023 08:32:21 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-3fbc12181b6so43194705e9.2; Mon, 24 Jul 2023 08:32:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690212740; x=1690817540; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TJ7QSuT9JWuSM03vOOl8cZyXJ2t2WtKznVcR/4lhGHU=; b=OSYHJkFkFwuVl3QaFLvyH/uqjdwvPLQtROyZjl7dyXKSNkPChyjZnI9lzAg73nVY4r xDPhq1tRgEQ9aQM43/vMgc3x6XbhM7cellZSn9FHRz1yrmGM3XrsZcfb4QqLDeLfj75J EaenoBtKF4baIDTwoAFpFXIm70er/snQWX0W3kAl0duHiTqzqDZ76hlwykl8TqL2jWGQ eRcrg4tmJrN6JGHOeKElGtu55QXFZpUihJf05EUFeDFCNagWmx8BYEqdIMzKfuZL6orj vR+bjLtPQIorpzH2RP4XoL4+UmyObluCIVDHPNux37+rrIIyZchAUepkpwTYynkMM0NQ 7SqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690212740; x=1690817540; 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=TJ7QSuT9JWuSM03vOOl8cZyXJ2t2WtKznVcR/4lhGHU=; b=GvZEkRIgEqhKJtddVxQFgVA4yjpEC7m/SpI07PSMe3TyuABp+WOt9IuvojPjA3X3vh tAswhcJI5nJNh91qW/qzRUp7TxC5lbzWDVxWlfvjsgyg1G3hycSBZukzRLnzkzGnlS9Z nkVYqZzt1w6HublJGAJAQJgyjO9/kv+YjDrfMw6jkHjUUb6VZcRHF8UzrBHEbAv1iCk4 NA/rkFRc88BDXu5I38ohtCYxjbW8FqGEIKMyhTMUBDaJkRifHJNjp2j9KxlCmnsoXicI iBOgLkoFy0sQ3eBHFKGtUlcOrSEENErSrHxU7DAZ0ipH+RMxOkaDiKhV0hYzDfRddWDY AMAA== X-Gm-Message-State: ABy/qLbULXMpwIBrk/kYgqGQtJfZAqVrCcyJgWZ0wusQkjw4ptNGMCkU wNTUezB1tC+Mm1R0wXeciAg= X-Received: by 2002:a7b:ca4d:0:b0:3fc:70:2f76 with SMTP id m13-20020a7bca4d000000b003fc00702f76mr8242344wml.20.1690212739464; Mon, 24 Jul 2023 08:32:19 -0700 (PDT) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id l8-20020a7bc448000000b003fb40ec9475sm10676900wmi.11.2023.07.24.08.32.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 08:32:19 -0700 (PDT) From: Christian Marangi To: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [PATCH 1/3] dt-bindings: nvmem: u-boot,env: Add support for u-boot,env-size Date: Mon, 24 Jul 2023 10:26:30 +0200 Message-Id: <20230724082632.21133-1-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772317827235754409 X-GMAIL-MSGID: 1772317827235754409 Add support for u-boot,env-size new property. Permit to declare a custom size of the U-Boot env that differs than the partition size where the U-Boot env is located. U-Boot env is validated by calculating the CRC32 on the entire env and in some specific case, the env size might differ from the partition size resulting in wrong CRC32 calculation than the expected one saved at the start of the partition. This happens when U-Boot is compiled by hardcoding a specific env size but the env is actually placed in a bigger partition, resulting in needing to provide a custom value. Declaring this property, this value will be used for NVMEM size instead of the mtd partition. Add also an example to make it clear the scenario of mismatched partition size and actual U-Boot env. Signed-off-by: Christian Marangi --- .../devicetree/bindings/nvmem/u-boot,env.yaml | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml b/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml index 36d97fb87865..3970725a2c57 100644 --- a/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +++ b/Documentation/devicetree/bindings/nvmem/u-boot,env.yaml @@ -44,6 +44,24 @@ properties: reg: maxItems: 1 + u-boot,env-size: + description: | + Permit to declare a custom size of the U-Boot env that differs than the + partition size where the U-Boot env is located. + + U-Boot env is validated by calculating the CRC32 on the entire env + and in some specific case, the env size might differ from the partition + size resulting in wrong CRC32 calculation than the expected one saved at + the start of the partition. + + This happens when U-Boot is compiled by hardcoding a specific env size + but the env is actually placed in a bigger partition, resulting in needing + to provide a custom value. + + Declaring this property, this value will be used for NVMEM size instead of + the mtd partition. + $ref: /schemas/types.yaml#/definitions/uint32 + bootcmd: type: object description: Command to use for automatic booting @@ -99,3 +117,32 @@ examples: }; }; }; + - | + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + reg = <0x0 0xc80000>; + label = "qcadata"; + read-only; + }; + + partition@c80000 { + label = "APPSBL"; + reg = <0xc80000 0x500000>; + read-only; + }; + + partition@1180000 { + compatible = "u-boot,env"; + reg = <0x1180000 0x80000>; + + u-boot,env-size = <0x40000>; + + mac1: ethaddr { + #nvmem-cell-cells = <1>; + }; + }; + }; From patchwork Mon Jul 24 08:26:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 125073 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1882961vqg; Mon, 24 Jul 2023 08:42:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlHAgkE2yt877EMlTVbUsik1q140MJKAgNq+rPwDdN5eBW2z+j0UTYdnV5vQwaYU93X9mQgs X-Received: by 2002:a05:6358:27a8:b0:12b:da97:aba6 with SMTP id l40-20020a05635827a800b0012bda97aba6mr5259624rwb.24.1690213331079; Mon, 24 Jul 2023 08:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690213331; cv=none; d=google.com; s=arc-20160816; b=Xsj1kpD8g5Yprh/GInSz7LyE1Kenslk5ETVOYMWzDRRWa+p34j7oSRJfJgjpHE6ZBC bwJMH4YC4T3zlYIJX5mThvICa4DKdMtHNqzS/2Em8sqgyn+Su+KeUH4zGqYskQGc8KRW Q917AqeasIi979fJm8ueaD8LUTzOQaXWaKIk8SwP0LI61L5QeXSYdBf3WgRCSCRUUeHG BPMKSdYxqWCRWcTY6jBOAbQxjrMo34o3YSN9Hye+FOtp0XGP01YC5JiU9uhxsASIHDGB S2i2jpKbs3RueZwVAhQAmxd3CjX6qRh54e1dmi/x7hF3hHelgTR6gHwqkLfIiP7tJFNl bmYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Uh131th5kq2SQHF8kYtNRQvjlJTlYiaIhaFGooO8kUo=; fh=LpK/+DU52e2A6LHcsmnq9BYHnjjjRVFCHa6LQ3LP2l8=; b=OkJMZMphgCOaZOG4eCEbCHeFlAay1IgAj2zim5kgO19ORBn+ZMsxBd8UWu3wTwqsum md2sUBhvecIneWCgZcB1gf4hCQJBiNKcffFLmd4ByAaeAgUKCbW9UstglmniGUUXf4iP X/GZ8Ex7RR9CWkZvecF424dukFjmXS5/UDHou/YrY0yiH8Xo/t+xW6+2RtjmAhAHoUI8 29vu/W4Fmdd7uPLtktIHp8EO7UQcSlxOoRW05HJVG+9s50oYofnwwvLLwOXjkPbyeNH9 XRztJOm7ft74VeRapBz4MxXs2LY45za9WYXxZgl5ImR6JKAsUfqCd4BTMPA555NtP7lT eHGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=rMuYTyo8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h2-20020a635302000000b00548e140a1a6si7402486pgb.66.2023.07.24.08.41.57; Mon, 24 Jul 2023 08:42:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=rMuYTyo8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231389AbjGXPcb (ORCPT + 99 others); Mon, 24 Jul 2023 11:32:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231352AbjGXPc0 (ORCPT ); Mon, 24 Jul 2023 11:32:26 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9101EE57; Mon, 24 Jul 2023 08:32:22 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-3fbc0609cd6so34803575e9.1; Mon, 24 Jul 2023 08:32:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690212741; x=1690817541; 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=Uh131th5kq2SQHF8kYtNRQvjlJTlYiaIhaFGooO8kUo=; b=rMuYTyo8xrub4HIbmV+wNnF3zawKWt48DGl+hNOsmEvnykYpXH4QrT+L/D2lXI4/fd XrUZdfZ+c4oAQjQ+qqP1qc5ydHikAV181XknINdkQjAJozlQP87I6BHPqsi1r8Wo4sh7 DEFJnLOwQj5fx10GZ2RCO97YVdr+JX1gZCxheKeqVR+GreqTj9YjSaUUnITXEIcLFD/y 0vS7QgcHs8/eP5PATEDKZVmcBHHsvPMWnxHsB37cjEGJvUNxr7Ut4+0KldWH5g8zObE8 OG5ZTyLR20BVWVIVyEcato/71JCk9WGsuzyLpQi40ZOOpuNBenDPeLJ054/ORVLFnW9f TTLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690212741; x=1690817541; 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=Uh131th5kq2SQHF8kYtNRQvjlJTlYiaIhaFGooO8kUo=; b=jSNT7JtkJ7JMLV6aG4tavcaZuL9j7MQkcv3EKzg2rQLfuwZyv4aDuRcRVMrHcNeMO4 8UWVlMkwdPw6Rn+eXR1YhSDfb3ygwUxwFvQRky7/+n5xhQnL39RwfZygvmWDNagKfqG8 YBHtkWGysjLavZf9Hzrv94zwFAu4mGDVaiVvsEjwgMhQMBzZT0KhsknMjw6ROZc10R/L +KCJLEJgGhA5qbYHAU6zFfwIstHccPyQ21/nm1yeh+8OTehoUScLCyk6G4q/K+8mQCOt 6dFbU646lsuY7MSIjZiZ01iKdRwVQmhtFdB1HGNiJRWa0gF46GrNWbiUWapGE9QtK1es v/uQ== X-Gm-Message-State: ABy/qLbbdtNUicwfkLXDvSYqr5WtjXmo7nhcVFBww21xhYqJN0MztnDt MnG4vEO5thHkFZEo/GYctrM= X-Received: by 2002:a05:600c:22cf:b0:3fb:ffef:d058 with SMTP id 15-20020a05600c22cf00b003fbffefd058mr5972219wmg.0.1690212740858; Mon, 24 Jul 2023 08:32:20 -0700 (PDT) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id l8-20020a7bc448000000b003fb40ec9475sm10676900wmi.11.2023.07.24.08.32.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 08:32:20 -0700 (PDT) From: Christian Marangi To: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [PATCH 2/3] nvmem: u-boot-env: Permit to declare custom env-size Date: Mon, 24 Jul 2023 10:26:31 +0200 Message-Id: <20230724082632.21133-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230724082632.21133-1-ansuelsmth@gmail.com> References: <20230724082632.21133-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772317134108104194 X-GMAIL-MSGID: 1772317134108104194 Permit to declare a custom size of the U-Boot env that differs than the partition size where the U-Boot env is located. U-Boot env is validated by calculating the CRC32 on the entire env and in some specific case, the env size might differ from the partition size resulting in wrong CRC32 calculation than the expected one saved at the start of the partition. This happens when U-Boot is compiled by hardcoding a specific env size but the env is actually placed in a bigger partition, resulting in needing to provide a custom value. To declare a custom env-size, the new property 'u-boot,env-size' is introduced to handle this special case. If the property is not declared, the mtd size is used instead. Signed-off-by: Christian Marangi --- drivers/nvmem/u-boot-env.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c index ee9fd9989b6e..b64c37308789 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/u-boot-env.c @@ -24,6 +24,7 @@ enum u_boot_env_format { struct u_boot_env { struct device *dev; enum u_boot_env_format format; + unsigned int u_boot_env_size; struct mtd_info *mtd; @@ -149,14 +150,14 @@ static int u_boot_env_parse(struct u_boot_env *priv) uint8_t *buf; int err; - buf = kcalloc(1, priv->mtd->size, GFP_KERNEL); + buf = kcalloc(1, priv->u_boot_env_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) { + err = mtd_read(priv->mtd, 0, priv->u_boot_env_size, &bytes, buf); + if ((err && !mtd_is_bitflip(err)) || bytes != priv->u_boot_env_size) { dev_err(dev, "Failed to read from mtd: %d\n", err); goto err_kfree; } @@ -179,8 +180,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 = priv->u_boot_env_size - crc32_data_offset; + data_len = priv->u_boot_env_size - data_offset; calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; if (calc != crc32) { @@ -189,7 +190,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) goto err_kfree; } - buf[priv->mtd->size - 1] = '\0'; + buf[priv->u_boot_env_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); @@ -224,6 +225,10 @@ static int u_boot_env_probe(struct platform_device *pdev) return PTR_ERR(priv->mtd); } + /* In absence of a custom env size, use the full mtd partition size */ + if (of_property_read_u32(np, "u-boot,env-size", &priv->u_boot_env_size)) + priv->u_boot_env_size = priv->mtd->size; + err = u_boot_env_parse(priv); if (err) return err; @@ -232,7 +237,7 @@ static int u_boot_env_probe(struct platform_device *pdev) config.cells = priv->cells; config.ncells = priv->ncells; config.priv = priv; - config.size = priv->mtd->size; + config.size = priv->u_boot_env_size; return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); } From patchwork Mon Jul 24 08:26:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 125080 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp1898187vqg; Mon, 24 Jul 2023 09:07:51 -0700 (PDT) X-Google-Smtp-Source: APBJJlGCUAX+1+RKExOqp2Jw+T33N5qmtoGAZAyIN6CyGWmSwjdK6IoIpHAsmZMDFvXnonAXRqf1 X-Received: by 2002:a17:902:bd82:b0:1b3:d608:899a with SMTP id q2-20020a170902bd8200b001b3d608899amr9303356pls.68.1690214871240; Mon, 24 Jul 2023 09:07:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690214871; cv=none; d=google.com; s=arc-20160816; b=QA3Z7+ZvSmLdsOBS0nMRFjskMOd7hfJ/AF+ySzLb/fC67zYoaEgAos+YPKQmpNNKrd cl3LfPw14OncOq976olv+0LQw77BpbjmHoGhreMHCWLp0CMVJSsb2hPv4VuQb3ojfcOb RNiifb10qT33dlB1zxFXf80/1zPoeM8m8TgR5qm+SwLURXlBovha7uQF8VV6S4JH3zOq BCAQSWL8RNtdu1x+od/87552CeXVJ8q/SXbGfaGxU1lHtPWOCRoeFfMgOpQnGzeJIOfU c7JZr5Z8AdGhP++vWUezRVydvtZUSSjEmYSP0Xsk24VF1i31yaA33gEsOgJe2TvCLsmg AoAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MLAkbsYK7QIyvj28dGSLrTuJv0qXZGTFq3rKFWJSTa8=; fh=LpK/+DU52e2A6LHcsmnq9BYHnjjjRVFCHa6LQ3LP2l8=; b=LRjdDtPpSqjmsDDbrifB0brEPuNMLSeRtmB/xxXjS3Bs8oYvduQc+sv5+4OzYtkzZf M7Wm6d09WgYUmlcndb/WHiPful9kGDZVWZhwS9mjUW+nUsyrM+ReXtr3RagEz5BibCde fh2mK3G29bYGMFx5VArRl6I/C0awIjyQJNgxV3GBhOBcFrYK+QtLkHsVT8Ba6T78t/LM eMEF/3yMhb5MPVDy/BIOuvFYyVhVJFPvEy+N2+3an01cAYm3TEFotp3XNjJQZh7wH9Jg +VwYQX2jfte3/hwI6bMjCwXxHfETL7ZRX13WiP3oDlJ2h3XtaBYaie9MJ6qcMk5PKSQT FwPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=AnB+05kn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a7-20020a170902ecc700b001b84c7d4716si4648008plh.32.2023.07.24.09.07.37; Mon, 24 Jul 2023 09:07:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=AnB+05kn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbjGXPc3 (ORCPT + 99 others); Mon, 24 Jul 2023 11:32:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjGXPc0 (ORCPT ); Mon, 24 Jul 2023 11:32:26 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9DAB1B3; Mon, 24 Jul 2023 08:32:23 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fd2f298712so13734485e9.2; Mon, 24 Jul 2023 08:32:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690212742; x=1690817542; 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=MLAkbsYK7QIyvj28dGSLrTuJv0qXZGTFq3rKFWJSTa8=; b=AnB+05knb3qUpk3tgMEV76tSVJNMy4NFF1nCvQRvZp8KJfQFQgEQwzRWUKGTVhdnzy /h0ZwqLtXhp87T1XnU1FWg6VPt/SixTmvEHsAI2B8fHAMNFp0EjMcEiD78fMLsGRaBMD q/zzKiq4iPR4M4XqwiwkpyRNSwUPfyQPjwhWxIaLniP01aCeBJEeChUTTiu6ol7DL2iZ TmqAIkkJ82+IpPRSkKalt8klg0hDiS7kEO/TmgdUzE+BWnviES6WWXqHmzyL9R0jr0/d 5bqy81GLeqbzRP9couiZ2KnfjiGIkNW8XbsoTbh/rZajFFs7L0VIvW5/o/zHDibe8tdC yAFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690212742; x=1690817542; 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=MLAkbsYK7QIyvj28dGSLrTuJv0qXZGTFq3rKFWJSTa8=; b=BRD0Jx+Np+oDzqnnZK6mYtsGL5eODXxwPgD+3V0uV6/+9txqpduSue3RVD7Z4Wy438 anV39wlPvUg25bZXwZcFdY3BumJzNrkJ6PycJaHRxVv+kCUe1U0KzO+9TQRwFNY8WZj/ nEz0Xh/loKNWCQL+V3n7XhMdEWu4n34/ycuj+SkymYUIUzJZ8kbG0h88LHm0+FMt/M2P cMVaxh4sV8sjIYfHsFJkBG8+RJsZYv6mh0C2WHhu9w/sgh+rlPEjoPdGYdepMK9SXthG +Tny6I9GsrQcaAngJd/X2xw28NFBotrgfTmVfPPynEM15oiS5SGpjl2m3WyucTLXdTFA CYIQ== X-Gm-Message-State: ABy/qLZaWIx0qKtwyVQ2+jLqTcJOr2gxFxVseXarWjadDbGYncPJNNI5 iAPpXpQygHxOr++rcy7bnB0= X-Received: by 2002:a05:600c:446:b0:3fb:fda1:710c with SMTP id s6-20020a05600c044600b003fbfda1710cmr5821024wmb.2.1690212741966; Mon, 24 Jul 2023 08:32:21 -0700 (PDT) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id l8-20020a7bc448000000b003fb40ec9475sm10676900wmi.11.2023.07.24.08.32.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jul 2023 08:32:21 -0700 (PDT) From: Christian Marangi To: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [PATCH 3/3] nvmem: u-boot-env: Handle "reduced" ASCII address declaration Date: Mon, 24 Jul 2023 10:26:32 +0200 Message-Id: <20230724082632.21133-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230724082632.21133-1-ansuelsmth@gmail.com> References: <20230724082632.21133-1-ansuelsmth@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_06_12, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772318748999013021 X-GMAIL-MSGID: 1772318748999013021 Parsing ASCII values is hard as hex values can be declared in many ways and OEM never follow a genera rules. This is especially problematic for parsing MAC address writte in ASCII format where an hex values can be written in 2 different format: - a Full format with leading 0, 01 02 06 0A - a Reduced format with trimmed leading 0, 1 2 6 A The current U-Boot-env driver assume that the Full format is always used by parsting the NVMEM cell size and expect always the form of XX:XX:XX:XX:XX:XX to pass it directly to mac_pton that expects this format. Reality is that it's common for OEM to use the reduced format resulting in MAC address in the format of XX:a:XX:XX:XX:XX:XX or a:XX:XX:XX:XX:XX or XX:XX:XX:XX:XX:a. To handle these special format additional care is needed. Some additional logic are added to "normalize" the MAC address in ASCII to a format that is accepted by mac_pton. As using the NVMEM cell size is not enough anymore, some additional validation are done by checking if we have at least a format of X:X:X:X:X:X by checking if we have at least 5 ':' char while checking the NVMEM cell. The size validation is changed to check a range of ETH_ALEN + 5 (assuming a min valid MAC address of X:X:X:X:X:X) and the full form by checking 3 * ETH_ALEN -1 (for the full format XX:XX:XX:XX:XX:XX) The parsing function try to be as redable as possible while saving some code duplication and skip the use of memcpy function as the post_process is called very little time just on driver probe. Signed-off-by: Christian Marangi --- drivers/nvmem/u-boot-env.c | 50 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c index b64c37308789..e33565e872f8 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/u-boot-env.c @@ -76,12 +76,58 @@ static int u_boot_env_read(void *context, unsigned int offset, void *val, 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 *src_mac = buf; u8 mac[ETH_ALEN]; - if (bytes != 3 * ETH_ALEN - 1) + if (bytes < ETH_ALEN + 5 || bytes > 3 * ETH_ALEN - 1) return -EINVAL; - if (!mac_pton(buf, mac)) + /* ASCII address might need to be normalized, try to parse it */ + if (bytes != 3 * ETH_ALEN - 1) { + u8 tmp_mac[3 * ETH_ALEN - 1]; + int i = 0, j = 0; + + while (i < bytes) { + /* First check if we need to handle a reduced section + * or we are handling the last byte. + * Example of reduced section: + * - a:XX:XX:XX:XX:XX + * - XX:a:XX:XX:XX:XX + * - XX:XX:XX:XX:XX:a + */ + if (src_mac[i + 1] == ':' || i + 1 == bytes) { + tmp_mac[j] = '0'; + tmp_mac[j + 1] = src_mac[i]; + if (src_mac[i + 1] == ':') + tmp_mac[j + 2] = src_mac[i + 1]; + i += 2; + /* Handle a full section or we are handling the last 2 byte. + * Example of full section: + * - aa:XX:XX:XX:XX:XX + * - XX:aa:XX:XX:XX:XX + * - XX:XX:XX:XX:XX:aa + */ + } else if (src_mac[i + 2] == ':' || i + 2 == bytes) { + tmp_mac[j] = src_mac[i]; + tmp_mac[j + 1] = src_mac[i + 1]; + if (src_mac[i + 2] == ':') + tmp_mac[j + 2] = src_mac[i + 2]; + i += 3; + /* Return -EINVAL if we have a not valid ascii address. + * We can use the logic of missing ':' to validate a + * correct ASCII address. + */ + } else { + return -EINVAL; + } + j += 3; + } + + /* Parse the normalized mac instead of the nvmem cell */ + src_mac = tmp_mac; + } + + if (!mac_pton(src_mac, mac)) return -EINVAL; if (index)