aboutsummaryrefslogtreecommitdiff
path: root/cortex-m-semihosting/src/hio.rs
blob: b6b6c7b7bcc4e9a0e95681dd34da025e3bf57310 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Host I/O

use crate::nr;
use core::{fmt, slice};

/// A byte stream to the host (e.g., host's stdout or stderr).
#[derive(Clone, Copy)]
pub struct HostStream {
    fd: usize,
}

impl HostStream {
    /// Attempts to write an entire `buffer` into this sink
    pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
        write_all(self.fd, buffer)
    }
}

impl fmt::Write for HostStream {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
    }
}

/// Construct a new handle to the host's standard error.
pub fn hstderr() -> Result<HostStream, ()> {
    // There is actually no stderr access in ARM Semihosting documentation. Use
    // convention used in libgloss.
    // See: libgloss/arm/syscalls.c, line 139.
    // https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/arm/syscalls.c#l139
    open(":tt\0", nr::open::W_APPEND)
}

/// Construct a new handle to the host's standard output.
pub fn hstdout() -> Result<HostStream, ()> {
    open(":tt\0", nr::open::W_TRUNC)
}

fn open(name: &str, mode: usize) -> Result<HostStream, ()> {
    let name = name.as_bytes();
    match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as isize {
        -1 => Err(()),
        fd => Ok(HostStream { fd: fd as usize }),
    }
}

fn write_all(fd: usize, mut buffer: &[u8]) -> Result<(), ()> {
    while !buffer.is_empty() {
        match unsafe { syscall!(WRITE, fd, buffer.as_ptr(), buffer.len()) } {
            // Done
            0 => return Ok(()),
            // `n` bytes were not written
            n if n <= buffer.len() => {
                let offset = (buffer.len() - n) as isize;
                buffer = unsafe { slice::from_raw_parts(buffer.as_ptr().offset(offset), n) }
            }
            #[cfg(feature = "jlink-quirks")]
            // Error (-1) - should be an error but JLink can return -1, -2, -3,...
            // For good measure, we allow up to negative 15.
            n if n > 0xfffffff0 => return Ok(()),
            // Error
            _ => return Err(()),
        }
    }
    Ok(())
}
alue=''/>
path: root/src/string_immutable.zig (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-07-23feat: add .PHONY on makefile targets (#847)Gravatar darker 1-19/+113
2022-07-22[bun install] Fix issue with URL path when sending requestGravatar Jarred Sumner 1-1/+55
2022-07-22Fix missing function bugbun-v0.1.5Gravatar Jarred Sumner 1-0/+1
2022-07-22[bun wiptest] This test does not seem to run?Gravatar Jarred Sumner 1-2/+2
2022-07-22`bun install` use custom BUN_CONFIG_REGISTRY port (#823)Gravatar SheetJSDev 1-1/+1
2022-07-22[docker] wipGravatar Jarred Sumner 3-13/+15
2022-07-22[docker] wipGravatar Jarred Sumner 1-2/+2
2022-07-22[docker] Use gcrGravatar Jarred Sumner 1-7/+21
2022-07-22[bun dev] Add flag to force hmrGravatar Jarred Sumner 1-3/+3
2022-07-22Fix link command on macOSGravatar Jarred Sumner 1-1/+1
2022-07-22[bun upgrade] Fix version display name for canary buildGravatar Jarred Sumner 1-1/+34
2022-07-22[bun upgrade] Fix name used in temporary dir for canary buildsGravatar Jarred Sumner 2-6/+26
2022-07-22WIP fix workflow runGravatar Jarred Sumner 1-1/+1
2022-07-22ClarifyGravatar Jarred Sumner 1-1/+1
2022-07-22Mention WSL version requirementGravatar Jarred Sumner 1-0/+2
2022-07-22Only run canary release on push to mainGravatar Jarred Sumner 1-4/+1
2022-07-22[bun upgrade] Implement `--canary` and `BUN_CANARY=1`Gravatar Jarred Sumner 3-20/+113
2022-07-22Fix mistake in Next.js Example README.Gravatar Aaditey Nair 1-1/+1
2022-07-22Update WebKitGravatar Jarred Sumner 1-0/+0
2022-07-22Workaround submodules issueGravatar Jarred Sumner 1-0/+4
2022-07-22Delete plus100-napiGravatar Jarred Sumner 1-0/+0
2022-07-22Rename linux amd64 -> linux x64Gravatar Jarred Sumner 1-18/+18
2022-07-22Mark as executableGravatar Jarred Sumner 4-11/+33
2022-07-22fix: remove suffix arg for mktemp compatibility (#825)Gravatar Connor Lurring 5-5/+5
2022-07-22Canary builds (Linux) (#824)canaryGravatar Jarred Sumner 12-220/+334
2022-07-21Separate Dockerfile for devcontainerGravatar Jarred Sumner 2-1/+100
2022-07-21BumpGravatar Jarred Sumner 1-1/+1
2022-07-21Redo the dockerfileGravatar Jarred SUmner 5-222/+203
2022-07-21docs(templates): Update README.md (#783)Gravatar Sakib Hasan 1-0/+8
2022-07-20chore(vscode): set tab size and tab format (#810)Gravatar Carter Snook 1-1/+2
2022-07-20feat(node/fs): implement more stat methods (#807)Gravatar Carter Snook 3-5/+108
2022-07-20doc: added an helper for the huge MakefileGravatar sanix-darker 1-6/+10
2022-07-20fix install script colorsGravatar Alexander 1-4/+6
2022-07-19Allow blankGravatar Jarred Sumner 1-1/+1
2022-07-19fix(api): stop double-free of prop array (#793)Gravatar Carter Snook 2-8/+6
2022-07-19refactor(installer): renovate install script (#745)Gravatar Wallunen 1-122/+162