uboot-envtools: support NVMEM based access
[openwrt/staging/pepe2k.git] / package / boot / uboot-envtools / patches / 100-fw_env-make-flash_io-take-buffer-as-an-argument.patch
1 From f178f7c9550c4fd9c644f79a1eb2dafa5bcdce25 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Wed, 12 Jan 2022 12:47:05 +0100
4 Subject: [PATCH] fw_env: make flash_io() take buffer as an argument
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 It's usually easier to understand code & follow it if all arguments are
10 passed explicitly. Many coding styles also discourage using global
11 variables.
12
13 Behaviour of flash_io() was a bit unintuitive as it was writing to a
14 buffer referenced in a global struct. That required developers to
15 remember how it works and sometimes required hacking "environment"
16 global struct variable to read data into a proper buffer.
17
18 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
19 ---
20 tools/env/fw_env.c | 32 ++++++++++++++++----------------
21 1 file changed, 16 insertions(+), 16 deletions(-)
22
23 --- a/tools/env/fw_env.c
24 +++ b/tools/env/fw_env.c
25 @@ -346,7 +346,7 @@ static int ubi_write(int fd, const void
26 return 0;
27 }
28
29 -static int flash_io(int mode);
30 +static int flash_io(int mode, void *buf, size_t count);
31 static int parse_config(struct env_opts *opts);
32
33 #if defined(CONFIG_FILE)
34 @@ -516,7 +516,7 @@ int fw_env_flush(struct env_opts *opts)
35 *environment.crc = crc32(0, (uint8_t *) environment.data, ENV_SIZE);
36
37 /* write environment back to flash */
38 - if (flash_io(O_RDWR)) {
39 + if (flash_io(O_RDWR, environment.image, CUR_ENVSIZE)) {
40 fprintf(stderr, "Error: can't write fw_env to flash\n");
41 return -1;
42 }
43 @@ -1185,7 +1185,8 @@ static int flash_flag_obsolete(int dev,
44 return rc;
45 }
46
47 -static int flash_write(int fd_current, int fd_target, int dev_target)
48 +static int flash_write(int fd_current, int fd_target, int dev_target, void *buf,
49 + size_t count)
50 {
51 int rc;
52
53 @@ -1212,11 +1213,10 @@ static int flash_write(int fd_current, i
54 if (IS_UBI(dev_target)) {
55 if (ubi_update_start(fd_target, CUR_ENVSIZE) < 0)
56 return -1;
57 - return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
58 + return ubi_write(fd_target, buf, count);
59 }
60
61 - rc = flash_write_buf(dev_target, fd_target, environment.image,
62 - CUR_ENVSIZE);
63 + rc = flash_write_buf(dev_target, fd_target, buf, count);
64 if (rc < 0)
65 return rc;
66
67 @@ -1235,17 +1235,17 @@ static int flash_write(int fd_current, i
68 return 0;
69 }
70
71 -static int flash_read(int fd)
72 +static int flash_read(int fd, void *buf, size_t count)
73 {
74 int rc;
75
76 if (IS_UBI(dev_current)) {
77 DEVTYPE(dev_current) = MTD_ABSENT;
78
79 - return ubi_read(fd, environment.image, CUR_ENVSIZE);
80 + return ubi_read(fd, buf, count);
81 }
82
83 - rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
84 + rc = flash_read_buf(dev_current, fd, buf, count,
85 DEVOFFSET(dev_current));
86 if (rc != CUR_ENVSIZE)
87 return -1;
88 @@ -1291,7 +1291,7 @@ err:
89 return rc;
90 }
91
92 -static int flash_io_write(int fd_current)
93 +static int flash_io_write(int fd_current, void *buf, size_t count)
94 {
95 int fd_target = -1, rc, dev_target;
96 const char *dname, *target_temp = NULL;
97 @@ -1322,7 +1322,7 @@ static int flash_io_write(int fd_current
98 fd_target = fd_current;
99 }
100
101 - rc = flash_write(fd_current, fd_target, dev_target);
102 + rc = flash_write(fd_current, fd_target, dev_target, buf, count);
103
104 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
105 fprintf(stderr,
106 @@ -1377,7 +1377,7 @@ static int flash_io_write(int fd_current
107 return rc;
108 }
109
110 -static int flash_io(int mode)
111 +static int flash_io(int mode, void *buf, size_t count)
112 {
113 int fd_current, rc;
114
115 @@ -1391,9 +1391,9 @@ static int flash_io(int mode)
116 }
117
118 if (mode == O_RDWR) {
119 - rc = flash_io_write(fd_current);
120 + rc = flash_io_write(fd_current, buf, count);
121 } else {
122 - rc = flash_read(fd_current);
123 + rc = flash_read(fd_current, buf, count);
124 }
125
126 if (close(fd_current)) {
127 @@ -1455,7 +1455,7 @@ int fw_env_open(struct env_opts *opts)
128 }
129
130 dev_current = 0;
131 - if (flash_io(O_RDONLY)) {
132 + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) {
133 ret = -EIO;
134 goto open_cleanup;
135 }
136 @@ -1490,7 +1490,7 @@ int fw_env_open(struct env_opts *opts)
137 * other pointers in environment still point inside addr0
138 */
139 environment.image = addr1;
140 - if (flash_io(O_RDONLY)) {
141 + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) {
142 ret = -EIO;
143 goto open_cleanup;
144 }