From patchwork Wed Apr 6 14:32:25 2022 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: 558322 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp968606map; Wed, 6 Apr 2022 07:33:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbkNDyg3x85NaVWG/7fAcuQNGZNqE1doSDD9alBb4p1RPCCf7vunB6jOg4GpYa/k8iQjS1 X-Received: by 2002:a05:6a00:1907:b0:4f7:945:14cf with SMTP id y7-20020a056a00190700b004f7094514cfmr9113382pfi.47.1649255587357; Wed, 06 Apr 2022 07:33:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649255587; cv=none; d=google.com; s=arc-20160816; b=BCNlWziST+vHLw4QDxBtPwLijb7Iv3wwyKVj8yrGWollEIJG39iXjht1HiNkMgUc6s GJRbUBo8COOqQe7mAaS19uSSPxJKyGALb9eRMPa+XuTXb0YrPG54lIlHMDb+nRvD137Y 95muNDziIjS+DfirDTCkVV3ob8ZlEQQ28kjIp8luhPzIMsay27oQMCJlKR2Z6v9iOWV3 PkKMB/M77wFexw3yVErMUZdYgrn+vn3ftBS2278oQeotLY8hrjxr6U+WaIuyuH4nrlBl Szzhxc9G1EoAhOFOKEBfqMqJOI4KDEoQuGf7sCA+ZslvkzDpk3zO4ryusHIOPR4QTkdB AmLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:sender:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-signature; bh=FGkwLJrCcjMYoRdHvyXuE1eJhWVdg5oQ1h4Exibv6ug=; b=oPXSXVdu6PaOBcFBXfoITdV5c/WS/Raz7zQgky7A0QTVB6dsgN+VobixRFZwPva58K gu38BqO5uuPU2ELykqDLM6Nl2rM/P8rl+pPmSUwy59WXrsKdxTtAfax23j1UffNcu0On XGyTLPxWFLgyAR3NCGqBIRg85foWSR+JrZ+11kus6NIUEwlF5JwyRs7VLwgnCUXZjP+2 Zd6Haf3LwuQiD5NsKRJ3dK8ePFgsEziZ1LVO1Md8AwLnuJY7xpSyD2zHxqP1J8PFCHIU QS63pgI/e7kRu3XtfZA0xHOYjnzRSx+B37kzav1HqkEAaq/exGIs5tqkl3LvGFR1Eblr 4rJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20210309 header.b=vwadh0wf; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=NtMfuEDA; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from bombadil.infradead.org (bombadil.infradead.org. [2607:7c80:54:e::133]) by mx.google.com with ESMTPS id v24-20020aa78098000000b004fa3a8dffeesi15217137pff.165.2022.04.06.07.33.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 07:33:07 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) client-ip=2607:7c80:54:e::133; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.infradead.org header.s=bombadil.20210309 header.b=vwadh0wf; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=NtMfuEDA; spf=pass (google.com: best guess record for domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2607:7c80:54:e::133 as permitted sender) smtp.mailfrom="linux-mtd-bounces+patch=linaro.org@lists.infradead.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=FGkwLJrCcjMYoRdHvyXuE1eJhWVdg5oQ1h4Exibv6ug=; b=vwadh0wfELBc7B ngXDOnIXYTv66SFYeJf7gCwODeT2K5yhzvS6JD1H0d5DRzpQyVq3XL/d/fztESwWuxM7MqnFEprXg qrjB7w0ugrdAi+cF/6v6luWhnYq10bupHcWSpgQQ+c1j5IVjC13rugCOgmmD3St8bjnC+LCGXz9Cu AnkcCAuISx2aWCBzpEjXQVxF4DRK/r5yXNF0OFvayWleNRH3Nfy7vmH8PRxwrO2scWCP8OgeYjcVX Dgut+HQl+fDFCi8rzcVQg0QzvXpLMo9K33peXoVc0xfd6wRdPwIUxI0hI996mVoUokuQ9MPoKu6Re JKn91uPaqBfWCNiVJlJA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nc6iQ-006WmC-Qh; Wed, 06 Apr 2022 14:32:58 +0000 Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nc6i8-006Wh9-9U; Wed, 06 Apr 2022 14:32:42 +0000 Received: by mail-lj1-x22b.google.com with SMTP id q14so3436689ljc.12; Wed, 06 Apr 2022 07:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NABR9zRxLXkNYB7eIxR0PQ9FujLjijWeL/y+MwKaVDE=; b=NtMfuEDAGevNr32acucqkGgWjDPBLrn+lcVr0HmE9eAGJkzciQE5kE7xalHumiGcI5 EKmsGFRMKlKAx80yg8nfxEnBuWY/44t8U5uthhB2a9UuqH5JDX+p9XEZ2Bw9hHjcCFsW /IcQ0C5oL4NVjHAWGni+9ldKSgoQvGovWdStdAgGmaKD3jmWwIPWQ2eFkU4cFYCPuNtr 0GJegkhCrd8shl3bngjXPSw4SSRXT9V/BCYeVRGdv0mnGXZm6AUYJGZlZ2aw7rvxKaSX Pt8xcO59iSy57s5XZ5808rUnXYpbJ8xf7EqKiQP/AquCfR/yd5FVLcSaNKsVtnKGdO6o Xlww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NABR9zRxLXkNYB7eIxR0PQ9FujLjijWeL/y+MwKaVDE=; b=xj2Upe8f5lOAit4u82XiCA44HEi0n8J8p1cLJi7/pwqfSXfITb0Ah7Zh1wrg0PwT5g K0uc3gM4e5r5BV61NHMNMjsUTkdMnH1e3t1POWCp6W/P29TgHdsEIgcxaXhYmvgNUdWK TdPBE6e0+ySY9AQfsOtstf0MyzT+edGBW4Ol4qe/0bGynogZ9oDHDmLKDeWpnGHDRSwh NCJQi5e8xo6XbqXmA3MSDeGKSBpBJiatWs5Rx7UQvs4MjdDKdNPzFCmXAMSUcFxeskAm oYDPcFBd+MLrHBrBdjSrP7LgBa2P1YQ57Q4JuNxZR01jvn6WbKCZvZX+z41H2jDwTM7S 7oKQ== X-Gm-Message-State: AOAM5327hDI8f5H2AZwoOKken6VnpZ5oFH0+Q3gslM1CC8REb23Rwuqq SUXGcKBI6O+SUxewg5M0Y34= X-Received: by 2002:a2e:b5b6:0:b0:24b:1762:5db with SMTP id f22-20020a2eb5b6000000b0024b176205dbmr5360527ljn.50.1649255556191; Wed, 06 Apr 2022 07:32:36 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id s2-20020a197702000000b0044a693649b9sm1843857lfc.131.2022.04.06.07.32.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 07:32:35 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra Cc: Tom Rini , linux-mtd@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, u-boot@lists.denx.de, devicetree@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1p?= =?utf-8?b?xYJlY2tp?= Subject: [PATCH 2/2] nvmem: add driver handling U-Boot environment variables Date: Wed, 6 Apr 2022 16:32:25 +0200 Message-Id: <20220406143225.28107-2-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220406143225.28107-1-zajec5@gmail.com> References: <20220406143225.28107-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220406_073240_359079_80E6601D X-CRM114-Status: GOOD ( 27.92 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?utf-8?q?Spam_detection_software=2C_running_on_the_system_?= =?utf-8?q?=22bombadil=2Einfradead=2Eorg=22=2C?= =?utf-8?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_original?= =?utf-8?q?_message_has_been_attached_to_this_so_you_can_view_it_or_label?= =?utf-8?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?utf-8?q?_the_administrator_of_that_system_for_details=2E?= =?utf-8?q?_?= =?utf-8?q?_Content_preview=3A__From=3A_Rafa=C3=85=C2=82_Mi=C3=85=C2=82ecki_U?= =?utf-8?q?-Boot_stores_its_setup_as_environment?= =?utf-8?q?_variables=2E_It=27s_a_list_of_key-value_pairs_stored_on_flash_dev?= =?utf-8?q?ice_with_a_custom?= =?utf-8?q?_header=2E_This_commit_adds_an_NVMEM_driver_that=3A_1=2E_Provides_?= =?utf-8?q?NVMEM_access_to?= =?utf-8?q?_environment_vars_binary_data_2=2E_Extracts_variables_as_NVMEM_cel?= =?utf-8?q?ls_?= =?utf-8?q?_?= =?utf-8?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_required?= =?utf-8?q?=29?= =?utf-8?q?_?= =?utf-8?q?_pts_rule_name______________description?= =?utf-8?q?_----_----------------------_-------------------------------------?= =?utf-8?q?-------------?= =?utf-8?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_at_https?= =?utf-8?q?=3A//www=2Ednswl=2Eorg/=2C?= =?utf-8?q?_no_trust?= =?utf-8?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoyMmIgbGlzdGVkIGluXQ==?= =?utf-8?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?utf-8?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_record?= =?utf-8?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publish_an_?= =?utf-8?q?SPF_Record?= =?utf-8?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_abused_en?= =?utf-8?q?duser_mail?= =?utf-8?q?_provider?= =?utf-8?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?utf-8?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freemail_use?= =?utf-8?q?rname_ends?= =?utf-8?q?_in_digit?= =?utf-8?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?utf-8?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM_or_DK_?= =?utf-8?q?signature_from?= =?utf-8?q?_author=27s_domain?= =?utf-8?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_valid_DK?= =?utf-8?q?IM_or_DK_signature?= =?utf-8?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_signature?= =?utf-8?q?=2C_not_necessarily?= =?utf-8?q?_valid?= =?utf-8?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM_or_DK_?= =?utf-8?q?signature_from?= =?utf-8?q?_envelope-from_domain?= X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org From: Rafał Miłecki U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header. This commit adds an NVMEM driver that: 1. Provides NVMEM access to environment vars binary data 2. Extracts variables as NVMEM cells It can be used for: 1. Accessing env variables from user-space 2. Reading NVMEM cells by Linux drivers Signed-off-by: Rafał Miłecki --- MAINTAINERS | 1 + drivers/nvmem/Kconfig | 12 ++ drivers/nvmem/Makefile | 2 + drivers/nvmem/u-boot-env.c | 236 +++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+) create mode 100644 drivers/nvmem/u-boot-env.c diff --git a/MAINTAINERS b/MAINTAINERS index 4cb7fd127e68..122bf4230507 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20121,6 +20121,7 @@ U-BOOT ENVIRONMENT VARIABLES M: Rafał Miłecki S: Maintained F: Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +F: drivers/nvmem/u-boot-env.c UACCE ACCELERATOR FRAMEWORK M: Zhangfei Gao diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index 555aa77a574d..17a78b1ba077 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -324,4 +324,16 @@ config NVMEM_SUNPLUS_OCOTP This driver can also be built as a module. If so, the module will be called nvmem-sunplus-ocotp. +config NVMEM_U_BOOT_ENV + tristate "U-Boot environment variables support" + depends on ARCH_BCM4908 || COMPILE_TEST + depends on OF && MTD + select CRC32 + 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 compiled as module it will be called nvmem_u-boot-env. + endif diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index 891958e29d25..84c8d75e59cf 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -65,3 +65,5 @@ obj-$(CONFIG_NVMEM_LAYERSCAPE_SFP) += nvmem-layerscape-sfp.o nvmem-layerscape-sfp-y := layerscape-sfp.o obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP) += nvmem_sunplus_ocotp.o nvmem_sunplus_ocotp-y := sunplus-ocotp.o +obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o +nvmem_u-boot-env-y := u-boot-env.o diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c new file mode 100644 index 000000000000..c7945d7d5750 --- /dev/null +++ b/drivers/nvmem/u-boot-env.c @@ -0,0 +1,236 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022 Rafał Miłecki + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +enum u_boot_env_format { + U_BOOT_FORMAT_SINGLE, + U_BOOT_FORMAT_REDUNDANT, +}; + +struct u_boot_env { + struct device *dev; + enum u_boot_env_format format; + + /* Parent device */ + struct mtd_info *mtd; + size_t offset; + size_t size; + + /* Cells */ + struct nvmem_cell_info *cells; + int ncells; +}; + +struct u_boot_env_image_single { + __le32 crc32; + uint8_t data[0]; +} __packed; + +struct u_boot_env_image_redundant { + __le32 crc32; + u8 mark; + uint8_t data[0]; +} __packed; + +static int u_boot_env_read(void *context, unsigned int offset, void *val, + size_t bytes) +{ + struct u_boot_env *priv = context; + struct device *dev = priv->dev; + size_t bytes_read; + int err; + + err = mtd_read(priv->mtd, priv->offset + offset, bytes, &bytes_read, val); + if (err && !mtd_is_bitflip(err)) { + dev_err(dev, "Failed to read from mtd: %d\n", err); + return err; + } + + if (bytes_read != bytes) { + dev_err(dev, "Failed to read %zd bytes\n", bytes); + return -EIO; + } + + 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 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; + var < data + data_len && *var; + var = value + strlen(value) + 1, idx++) { + 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) + return -ENOMEM; + priv->cells[idx].offset = data_offset + value - data; + priv->cells[idx].bytes = strlen(value); + } + + if (WARN_ON(idx != priv->ncells)) + priv->ncells = idx; + + return 0; +} + +static int u_boot_env_parse(struct u_boot_env *priv) +{ + 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; + uint32_t crc32; + uint32_t calc; + size_t bytes; + uint8_t *buf; + int err; + + buf = kcalloc(1, priv->size, GFP_KERNEL); + if (!buf) { + err = -ENOMEM; + goto err_out; + } + + err = mtd_read(priv->mtd, priv->offset, priv->size, &bytes, buf); + if ((err && !mtd_is_bitflip(err)) || bytes != priv->size) { + dev_err(dev, "Failed to read from mtd: %d\n", err); + 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, mark); + data_offset = offsetof(struct u_boot_env_image_redundant, data); + break; + } + crc32 = le32_to_cpu(*(uint32_t *)(buf + crc32_offset)); + crc32_data_len = priv->size - crc32_data_offset; + data_len = priv->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[priv->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); +err_out: + return err; +} + +static const struct of_device_id u_boot_env_of_match_table[] = { + { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, }, + { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, }, + { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, }, + {}, +}; + +static int u_boot_env_probe(struct platform_device *pdev) +{ + struct nvmem_config config = { + .name = "u-boot-env", + .reg_read = u_boot_env_read, + }; + struct device *dev = &pdev->dev; + struct device_node *np = dev->of_node; + const struct of_device_id *of_id; + struct u_boot_env *priv; + const char *label; + int err; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + priv->dev = dev; + + of_id = of_match_device(u_boot_env_of_match_table, dev); + if (!of_id) + return -EINVAL; + priv->format = (uintptr_t)of_id->data; + + if (of_property_read_u32(np, "reg", (u32 *)&priv->offset) || + of_property_read_u32_index(np, "reg", 1, (u32 *)&priv->size)) { + dev_err(dev, "Failed to read \"reg\" property\n"); + return -EINVAL; + } + + label = of_get_property(np->parent, "label", NULL); + if (!label) + label = np->parent->name; + + priv->mtd = get_mtd_device_nm(label); + if (IS_ERR(priv->mtd)) { + dev_err(dev, "Failed to find \"%s\" MTD device: %ld\n", label, PTR_ERR(priv->mtd)); + 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->size; + + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); +} + +static struct platform_driver u_boot_env_driver = { + .probe = u_boot_env_probe, + .driver = { + .name = "u_boot_env", + .of_match_table = u_boot_env_of_match_table, + }, +}; +module_platform_driver(u_boot_env_driver); + +MODULE_AUTHOR("Rafał Miłecki"); +MODULE_LICENSE("GPL"); +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);