Skip to contentSkip to conversation

tools: env: Handle shorter read calls

PATCH In review Thibault Ferrante 2 participants 3 messages Aug 25, 2021 – Sep 2, 2021

lore
Share & tools
lore
#0 lore raw

cover tools: env: Handle shorter read calls

On specific devices, read can return less bytes than expected. I noticed this problem on a EEPROM chip with 16Kbyte of memory. Defining an environment with more than 4Kbyte leads to less bytes read than expected and failures from fw_printenv/fw_setenv.
This patch target to handle this issue by doing multiple reads instead of exiting directly.

Thibault Ferrante (1):
tools: env: Handle shorter read calls

 tools/env/fw_env.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 tools: env: Handle shorter read calls

On some cases, the actual number of bytes read can be shorter than what was requested. This can be handled gracefully by taking this difference into account instead of exiting.

---

 tools/env/fw_env.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
tools/env/fw_env.c +12 -10
index 2a61a5d6f0..e39c39e23a 100644
@@ -951,21 +951,23 @@ static int flash_read_buf(int dev, int fd, void *buf, size_t count,
 				DEVNAME(dev), strerror(errno));
 			return -1;
 		}
-		if (rc != readlen) {
-			fprintf(stderr,
-				"Read error on %s: Attempted to read %zd bytes but got %d\n",
-				DEVNAME(dev), readlen, rc);
-			return -1;
-		}
 #ifdef DEBUG
 		fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
 			rc, (unsigned long long)blockstart + block_seek,
 			DEVNAME(dev));
 #endif
-		processed += readlen;
-		readlen = min(blocklen, count - processed);
-		block_seek = 0;
-		blockstart += blocklen;
+		processed += rc;
+		if (rc != readlen) {
+			fprintf(stderr,
+				"Warning on %s: Attempted to read %zd bytes but got %d\n",
+				DEVNAME(dev), readlen, rc);
+			readlen -= rc;
+			block_seek += rc;
+		} else {
+			blockstart += blocklen;
+			readlen = min(blocklen, count - processed);
+			block_seek = 0;
+		}
 	}
 
 	return processed;
Signature · 1 line
2.25.1
#2 lore raw
Quoted 5 lines · Thibault Ferrante

On some cases, the actual number of bytes read can be shorter than what was requested. This can be handled gracefully by taking this difference into account instead of exiting.

Applied to u-boot/next, thanks!

Signature · 1 line
Tom