[PATCH] lib: utils/serial: Fix semihosting_getc

Xiang W wxjstz at 126.com
Tue Nov 8 19:54:27 PST 2022


semihosting_read returns buffer_length - bytes_read, so a successful
read should return 0. This patch fixes this issue.

Signed-off-by: Xiang W <wxjstz at 126.com>
---
 lib/utils/serial/semihosting.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Below is the code comment from openocd about SYS_READ

		case SEMIHOSTING_SYS_READ:	/* 0x06 */
			/*
			 * Reads the contents of a file into a buffer. The file position
			 * is specified either:
			 * - Explicitly by a SYS_SEEK.
			 * - Implicitly one byte beyond the previous SYS_READ or
			 * SYS_WRITE request.
			 *
			 * The file position is at the start of the file when it is
			 * opened, and is lost when the file is closed. Perform the
			 * file operation as a single action whenever possible. For
			 * example, do not split a read of 16KB into four 4KB chunks
			 * unless there is no alternative.
			 *
			 * Entry
			 * On entry, the PARAMETER REGISTER contains a pointer to a
			 * three-field data block:
			 * - field 1 Contains a handle for a file previously opened
			 * with SYS_OPEN.
			 * - field 2 Points to a buffer.
			 * - field 3 Contains the number of bytes to read to the buffer
			 * from the file.
			 *
			 * Return
			 * On exit, the RETURN REGISTER contains the number of bytes not
			 * filled in the buffer (buffer_length - bytes_read) as follows:
			 * - If the RETURN REGISTER is 0, the entire buffer was
			 * successfully filled.
			 * - If the RETURN REGISTER is the same as field 3, no bytes
			 * were read (EOF can be assumed).
			 * - If the RETURN REGISTER contains a value smaller than
			 * field 3, the read succeeded but the buffer was only partly
			 * filled. For interactive devices, this is the most common
			 * return value.
			 */

diff --git a/lib/utils/serial/semihosting.c b/lib/utils/serial/semihosting.c
index 5012fa1..79eb583 100644
--- a/lib/utils/serial/semihosting.c
+++ b/lib/utils/serial/semihosting.c
@@ -157,7 +157,7 @@ static int semihosting_getc(void)
 		ch = semihosting_trap(SYSREADC, NULL);
 		ret = ch > -1 ? ch : -1;
 	} else
-		ret = semihosting_read(semihosting_infd, &ch, 1) > 0 ? ch : -1;
+		ret = semihosting_read(semihosting_infd, &ch, 1) == 0 ? ch : -1;
 
 	return ret;
 }
-- 
2.30.2




More information about the opensbi mailing list