1 From 8142c4554ffaa927529f24427a35f7ee2861793a Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Thu, 16 Jun 2022 20:59:03 +0200
4 Subject: [PATCH] fw_env: add fallback to Linux's NVMEM based access
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 A new DT binding for describing environment data block has been added in
10 Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment
11 variables binding"). Once we get a proper Linux NVMEM driver it'll be
12 possible to use Linux's binary interface for user-space as documented
14 https://www.kernel.org/doc/html/latest/driver-api/nvmem.html
16 This commits makes fw_env fallback to looking for a compatible NVMEM
17 device in case config file isn't present. In a long term this may make
18 config files redundant and avoid code (info) duplication.
20 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
22 tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++++++++++++++--
23 1 file changed, 67 insertions(+), 3 deletions(-)
25 --- a/tools/env/fw_env.c
26 +++ b/tools/env/fw_env.c
27 @@ -1713,6 +1713,67 @@ static int check_device_config(int dev)
31 +static int find_nvmem_device(void)
33 + const char *path = "/sys/bus/nvmem/devices";
34 + struct dirent *dent;
41 + dir = opendir(path);
46 + while (!nvmem && (dent = readdir(dir))) {
49 + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
53 + bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name);
54 + if (bytes < 0 || bytes == sizeof(comp)) {
58 + fp = fopen(comp, "r");
63 + fread(buf, sizeof(buf), 1, fp);
65 + if (!strcmp(buf, "u-boot,env")) {
66 + bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
84 + ENVSIZE(0) = s.st_size;
92 static int parse_config(struct env_opts *opts)
95 @@ -1723,9 +1784,12 @@ static int parse_config(struct env_opts
96 #if defined(CONFIG_FILE)
97 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
98 if (get_config(opts->config_file)) {
99 - fprintf(stderr, "Cannot parse config file '%s': %m\n",
100 - opts->config_file);
102 + if (find_nvmem_device()) {
103 + fprintf(stderr, "Cannot parse config file '%s': %m\n",
104 + opts->config_file);
105 + fprintf(stderr, "Failed to find NVMEM device\n");
110 DEVNAME(0) = DEVICE1_NAME;