uboot-envtools: support NVMEM based access
[openwrt/staging/pepe2k.git] / package / boot / uboot-envtools / patches / 102-fw_env-add-fallback-to-Linux-s-NVMEM-based-access.patch
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
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
13 in the:
14 https://www.kernel.org/doc/html/latest/driver-api/nvmem.html
15
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.
19
20 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
21 ---
22 tools/env/fw_env.c | 70 ++++++++++++++++++++++++++++++++++++++++++++--
23 1 file changed, 67 insertions(+), 3 deletions(-)
24
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)
28 return rc;
29 }
30
31 +static int find_nvmem_device(void)
32 +{
33 + const char *path = "/sys/bus/nvmem/devices";
34 + struct dirent *dent;
35 + char *nvmem = NULL;
36 + char comp[256];
37 + char buf[32];
38 + int bytes;
39 + DIR *dir;
40 +
41 + dir = opendir(path);
42 + if (!dir) {
43 + return -EIO;
44 + }
45 +
46 + while (!nvmem && (dent = readdir(dir))) {
47 + FILE *fp;
48 +
49 + if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
50 + continue;
51 + }
52 +
53 + bytes = snprintf(comp, sizeof(comp), "%s/%s/of_node/compatible", path, dent->d_name);
54 + if (bytes < 0 || bytes == sizeof(comp)) {
55 + continue;
56 + }
57 +
58 + fp = fopen(comp, "r");
59 + if (!fp) {
60 + continue;
61 + }
62 +
63 + fread(buf, sizeof(buf), 1, fp);
64 +
65 + if (!strcmp(buf, "u-boot,env")) {
66 + bytes = asprintf(&nvmem, "%s/%s/nvmem", path, dent->d_name);
67 + if (bytes < 0) {
68 + nvmem = NULL;
69 + }
70 + }
71 +
72 + fclose(fp);
73 + }
74 +
75 + closedir(dir);
76 +
77 + if (nvmem) {
78 + struct stat s;
79 +
80 + stat(nvmem, &s);
81 +
82 + DEVNAME(0) = nvmem;
83 + DEVOFFSET(0) = 0;
84 + ENVSIZE(0) = s.st_size;
85 +
86 + return 0;
87 + }
88 +
89 + return -ENOENT;
90 +}
91 +
92 static int parse_config(struct env_opts *opts)
93 {
94 int rc;
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);
101 - return -1;
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");
106 + return -1;
107 + }
108 }
109 #else
110 DEVNAME(0) = DEVICE1_NAME;