From patchwork Thu Jun 16 18:59:03 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: 582235 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5806:0:0:0:0 with SMTP id j6csp1702157max; Thu, 16 Jun 2022 11:59:23 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sddpf2dc/++ZMHB5VlWEWqphWRddb7lbgqe05COVb0+Tm3EXtK4ZF5LIYpFWCZFwMkmFKx X-Received: by 2002:aa7:d4c9:0:b0:42d:cbf0:bfb1 with SMTP id t9-20020aa7d4c9000000b0042dcbf0bfb1mr8338373edr.278.1655405963362; Thu, 16 Jun 2022 11:59:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655405963; cv=none; d=google.com; s=arc-20160816; b=p+BGeJ1hQz3rlwX5yg4OI04gokn/vXolBx1anFUD9e/7Ap9WCvDLKphehfBc7DXJTd rJEAsthe95UIt5ZZ8jH88NzmlyG0bEqQhr4guXxL5BJBCYXV+UV3ZRW9oGyUgr/0nql4 ZdL+4M2YsiM+dQkCrmOAZT9/+83sSL6RaczFtqH5+PEkdf797lxYkF0f0DS+1TdgWanD KpX+gg7taHOqQSrNFiGFSyaj3AfF+Xo52aT1kQddOfsLYoTX34yooO51EsXiA8MYie9o f/AmvQ2wEudN0qKFYdNwafuUvj+lnV2s9vV2mhp04qD9g3pS0XJsYnxS306KQdgktE3u /VaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=gc1akRlZOAA7yk3nDYZ83AZ/i262jrN5pgNw4YphW7E=; b=rQnlZCl92HGrJje8UryXWv8cQ/voLjnEj/2K0fh/7jpjB1a1sfiux/ZkBB5+2T+X2Y uJhqjF/nO0Hh2QFpU1COCgSmao7qjHUwifonW+m/zAzfdSoDxFqBhbWEU0bqtT0OgCb4 8mbLiiptUgl4yISVIjZJOsQAddnuKhR9ygwud8ZtTeJmwCcjp8VDGqsz8ox1RHZfujQW Q4trl0WhSnqB1bzYwqcDgKjytavMIApyVPb4a4Bl5Mr9h+ozbWhQfnwkSx7P6bQajEcY uuc5yrWYZh9gkhY/dAdIbOe0e9sEgS5nn1BL+KG0hpIhMFH/Rw+QRIo1ZULojYvviFCh O6cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ii4Lpy2j; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id sc35-20020a1709078a2300b006ff0c040fe8si1612365ejc.942.2022.06.16.11.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jun 2022 11:59:23 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ii4Lpy2j; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 56D1A84337; Thu, 16 Jun 2022 20:59:20 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ii4Lpy2j"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C10FC84355; Thu, 16 Jun 2022 20:59:18 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 093C5842C7 for ; Thu, 16 Jun 2022 20:59:16 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=zajec5@gmail.com Received: by mail-ed1-x534.google.com with SMTP id w27so3421121edl.7 for ; Thu, 16 Jun 2022 11:59:16 -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:mime-version :content-transfer-encoding; bh=gc1akRlZOAA7yk3nDYZ83AZ/i262jrN5pgNw4YphW7E=; b=ii4Lpy2jUz5trHuM9o1528TLib7bU6USKlWWNm6k+uMgK7BC2i2y0LtMM93yd+sj1h JisN2lr2duh2VYFFvub07ZckX1+CFw+2LQsUWYU/q0Bp3qFU9F4XgA3/o5FEtYuE2dPJ G+sxf5pWOWEveZr41+lUjrkiCJRGDUSWC89htkxrtCsmKMZabNYSGWeQwIfIzJIgOWeU z3nDGf4shYHAUKYtEKEQNkFGFWSNrRsOfue6fLAyhdeDhFh5RNK4lXjwoDdlzAe2KYfT +BQpd18JdRDCciOvMiqoAfqBaWTJAUR31KhwDHzE8TMe/cbOLf9q3lNE0BAga2J76c0L w/gw== 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:mime-version :content-transfer-encoding; bh=gc1akRlZOAA7yk3nDYZ83AZ/i262jrN5pgNw4YphW7E=; b=QbhLvB3ED+Ryedad2qysRxR5rvka02GWyczdTHyh4z2t9534Oz9sgy3flRasMlttLy jmLXyzRJn/NeeEn9BPL0bbttW5GWI9zR0p8yLOWDB0AcjCij9k+nSqN07+EfgFdbgGHl N4gYIicQ1hJf8GrBv+ie+q1RWaSp4/mxP+VPiFf46SsLfPQs86KtKTlbYPUYlkQ715yD OnZv7NJXsMw+xi2ykWF8ieYJHfE53NMclfgMIjL/amhnwy/TNWDnKBA2zqmCmAYRWQDe wUR/CO+HnLvySXPwsiwm/1EXuWXjPykPvRSa6ASxpsN6HKVeVfGj20h8xDWrFXC3Cg0l lqCw== X-Gm-Message-State: AJIora9wf8ojIlSAm7iJ/tQ/LnB54V6W+kAc4s9lncM/nuW7WWwQZ4NK ujkwKg2uEcc+jGkeUnuMgkM= X-Received: by 2002:a05:6402:2552:b0:431:5fc5:5bf with SMTP id l18-20020a056402255200b004315fc505bfmr8314271edb.287.1655405955548; Thu, 16 Jun 2022 11:59:15 -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 u14-20020aa7d88e000000b0042bc97322desm2270343edq.43.2022.06.16.11.59.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jun 2022 11:59:14 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Tom Rini , u-boot@lists.denx.de Cc: Ahmad Fatoum , =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH] fw_env: add fallback to Linux's NVMEM based access Date: Thu, 16 Jun 2022 20:59:03 +0200 Message-Id: <20220616185903.14426-1-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean From: Rafał Miłecki A new DT binding for describing environment data block has been added in Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment variables binding"). Once we get a proper Linux NVMEM driver it'll be possible to use Linux's binary interface for user-space as documented in the: https://www.kernel.org/doc/html/latest/driver-api/nvmem.html This commits makes fw_env fallback to looking for a compatible NVMEM device in case config file isn't present. In a long term this may make config files redundant and avoid code (info) duplication. Signed-off-by: Rafał Miłecki --- tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 31afef6f3b..be77bbf054 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -1713,6 +1713,67 @@ static int check_device_config(int dev) return rc; } +static int find_nvmem_device(void) +{ + const char *path = "/sys/bus/nvmem/devices"; + struct dirent *dent; + char *nvmem = NULL; + char comp[256]; + char buf[32]; + int bytes; + DIR *dir; + + dir = opendir(path); + if (!dir) { + return -EIO; + } + + while (!nvmem && (dent = readdir(dir))) { + FILE *fp; + + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) { + continue; + } + + bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name); + if (bytes < 0 || bytes == sizeof(comp)) { + continue; + } + + fp = fopen(comp, "r"); + if (!fp) { + continue; + } + + fread(buf, sizeof(buf), 1, fp); + + if (!strcmp(buf, "u-boot,env")) { + bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name); + if (bytes < 0) { + nvmem = NULL; + } + } + + fclose(fp); + } + + closedir(dir); + + if (nvmem) { + struct stat s; + + stat(nvmem, &s); + + DEVNAME(0) = nvmem; + DEVOFFSET(0) = 0; + ENVSIZE(0) = s.st_size; + + return 0; + } + + return -ENOENT; +} + static int parse_config(struct env_opts *opts) { int rc; @@ -1723,9 +1784,12 @@ static int parse_config(struct env_opts *opts) #if defined(CONFIG_FILE) /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ if (get_config(opts->config_file)) { - fprintf(stderr, "Cannot parse config file '%s': %m\n", - opts->config_file); - return -1; + if (find_nvmem_device()) { + fprintf(stderr, "Cannot parse config file '%s': %m\n", + opts->config_file); + fprintf(stderr, "Failed to find NVMEM device\n"); + return -1; + } } #else DEVNAME(0) = DEVICE1_NAME;