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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 It's usually easier to understand code & follow it if all arguments are
10 passed explicitly. Many coding styles also discourage using global
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.
18 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
20 tools/env/fw_env.c | 32 ++++++++++++++++----------------
21 1 file changed, 16 insertions(+), 16 deletions(-)
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
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);
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);
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");
43 @@ -1185,7 +1185,8 @@ static int flash_flag_obsolete(int dev,
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,
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)
57 - return ubi_write(fd_target, environment.image, CUR_ENVSIZE);
58 + return ubi_write(fd_target, buf, count);
61 - rc = flash_write_buf(dev_target, fd_target, environment.image,
63 + rc = flash_write_buf(dev_target, fd_target, buf, count);
67 @@ -1235,17 +1235,17 @@ static int flash_write(int fd_current, i
71 -static int flash_read(int fd)
72 +static int flash_read(int fd, void *buf, size_t count)
76 if (IS_UBI(dev_current)) {
77 DEVTYPE(dev_current) = MTD_ABSENT;
79 - return ubi_read(fd, environment.image, CUR_ENVSIZE);
80 + return ubi_read(fd, buf, count);
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)
88 @@ -1291,7 +1291,7 @@ err:
92 -static int flash_io_write(int fd_current)
93 +static int flash_io_write(int fd_current, void *buf, size_t count)
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;
101 - rc = flash_write(fd_current, fd_target, dev_target);
102 + rc = flash_write(fd_current, fd_target, dev_target, buf, count);
104 if (fsync(fd_current) && !(errno == EINVAL || errno == EROFS)) {
106 @@ -1377,7 +1377,7 @@ static int flash_io_write(int fd_current
110 -static int flash_io(int mode)
111 +static int flash_io(int mode, void *buf, size_t count)
115 @@ -1391,9 +1391,9 @@ static int flash_io(int mode)
118 if (mode == O_RDWR) {
119 - rc = flash_io_write(fd_current);
120 + rc = flash_io_write(fd_current, buf, count);
122 - rc = flash_read(fd_current);
123 + rc = flash_read(fd_current, buf, count);
126 if (close(fd_current)) {
127 @@ -1455,7 +1455,7 @@ int fw_env_open(struct env_opts *opts)
131 - if (flash_io(O_RDONLY)) {
132 + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) {
136 @@ -1490,7 +1490,7 @@ int fw_env_open(struct env_opts *opts)
137 * other pointers in environment still point inside addr0
139 environment.image = addr1;
140 - if (flash_io(O_RDONLY)) {
141 + if (flash_io(O_RDONLY, environment.image, CUR_ENVSIZE)) {