riscv_semihosting/
hio.rs

1//! Host I/O
2
3// Fixing this lint requires a breaking change that does not add much value
4#![allow(clippy::result_unit_err)]
5
6use crate::nr;
7use core::{fmt, slice};
8
9/// A byte stream to the host (e.g., host's stdout or stderr).
10#[derive(Clone, Copy)]
11pub struct HostStream {
12    fd: usize,
13}
14
15impl HostStream {
16    /// Attempts to write an entire `buffer` into this sink
17    pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
18        write_all(self.fd, buffer)
19    }
20}
21
22impl fmt::Write for HostStream {
23    fn write_str(&mut self, s: &str) -> fmt::Result {
24        self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
25    }
26}
27
28/// Construct a new handle to the host's standard error.
29pub fn hstderr() -> Result<HostStream, ()> {
30    // There is actually no stderr access in ARM Semihosting documentation. Use
31    // convention used in libgloss.
32    // See: libgloss/arm/syscalls.c, line 139.
33    // https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/arm/syscalls.c#l139
34    open(":tt\0", nr::open::W_APPEND)
35}
36
37/// Construct a new handle to the host's standard output.
38pub fn hstdout() -> Result<HostStream, ()> {
39    open(":tt\0", nr::open::W_TRUNC)
40}
41
42fn open(name: &str, mode: usize) -> Result<HostStream, ()> {
43    let name = name.as_bytes();
44    match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as isize {
45        -1 => Err(()),
46        fd => Ok(HostStream { fd: fd as usize }),
47    }
48}
49
50fn write_all(fd: usize, mut buffer: &[u8]) -> Result<(), ()> {
51    while !buffer.is_empty() {
52        match unsafe { syscall!(WRITE, fd, buffer.as_ptr(), buffer.len()) } {
53            // Done
54            0 => return Ok(()),
55            // `n` bytes were not written
56            n if n <= buffer.len() => {
57                let offset = (buffer.len() - n) as isize;
58                buffer = unsafe { slice::from_raw_parts(buffer.as_ptr().offset(offset), n) }
59            }
60            #[cfg(feature = "jlink-quirks")]
61            // Error (-1) - should be an error but JLink can return -1, -2, -3,...
62            // For good measure, we allow up to negative 15.
63            n if n > 0xfffffff0 => return Ok(()),
64            // Error
65            _ => return Err(()),
66        }
67    }
68    Ok(())
69}