Explorar o código

Returns -1 when legacy getchar function fails.

Ref: https://github.com/riscv-software-src/opensbi/pull/76/files#diff-a3ecbce4af91d3e4271388be8f36b1bbbb401baf37b76f1caadaa119d1110127R51-R56 and SBI specification section 4.3
luojia65 %!s(int64=3) %!d(string=hai) anos
pai
achega
a895104d53
Modificáronse 3 ficheiros con 8 adicións e 4 borrados
  1. 1 0
      CHANGELOG.md
  2. 2 1
      src/ecall/legacy.rs
  3. 5 3
      src/legacy_stdio.rs

+ 1 - 0
CHANGELOG.md

@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Fixed
 - Test kernel console now will lock before `println` line is finished
 - Non-legacy supervisor IPI extension is fixed
+- Returns -1 other than 0 when legacy console getchar function fails; thank you @duskmoon314
 
 ## [0.1.1] - 2021-02-01
 ### Added

+ 2 - 1
src/ecall/legacy.rs

@@ -11,10 +11,11 @@ pub fn console_putchar(param0: usize) -> SbiRet {
     SbiRet::ok(0) // the return value 0 is ignored in legacy
 }
 
+// Returns a character, or -1 when failed.
 #[inline]
 pub fn console_getchar() -> SbiRet {
     let ch = legacy_stdio_getchar();
-    SbiRet::legacy_ok(ch as usize)
+    SbiRet::legacy_ok(ch)
 }
 
 #[inline]

+ 5 - 3
src/legacy_stdio.rs

@@ -89,11 +89,13 @@ pub fn legacy_stdio_putchar(ch: u8) {
     }
 }
 
-pub fn legacy_stdio_getchar() -> u8 {
+pub fn legacy_stdio_getchar() -> usize {
     if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
-        stdio.getchar()
+        stdio.getchar() as usize
     } else {
-        0 // default: always return 0
+        // According to RISC-V SBI spec 0.3.1-rc1, Section 4.3, this function returns -1 
+        // when fails to read from debug console. Thank you @duskmoon314
+        usize::from_ne_bytes(isize::to_ne_bytes(-1)) 
     }
 }