116 lines
1.7 KiB
C
116 lines
1.7 KiB
C
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "bsp/uart.h"
|
|
|
|
extern char __heap_start[];
|
|
extern char __heap_end[];
|
|
|
|
static char *heap_cur;
|
|
|
|
void *_sbrk(ptrdiff_t incr)
|
|
{
|
|
if (heap_cur == NULL) {
|
|
heap_cur = __heap_start;
|
|
}
|
|
|
|
char *prev = heap_cur;
|
|
char *next = heap_cur + incr;
|
|
if (next < __heap_start || next > __heap_end) {
|
|
errno = ENOMEM;
|
|
return (void *)-1;
|
|
}
|
|
|
|
heap_cur = next;
|
|
return prev;
|
|
}
|
|
|
|
int _write(int fd, const void *buf, size_t count)
|
|
{
|
|
if (fd != 1 && fd != 2) {
|
|
errno = EBADF;
|
|
return -1;
|
|
}
|
|
|
|
const char *s = (const char *)buf;
|
|
for (size_t i = 0; i < count; ++i) {
|
|
bsp_uart_putc(s[i]);
|
|
}
|
|
return (int)count;
|
|
}
|
|
|
|
int _read(int fd, void *buf, size_t count)
|
|
{
|
|
if (fd != 0) {
|
|
errno = EBADF;
|
|
return -1;
|
|
}
|
|
|
|
char *s = (char *)buf;
|
|
size_t n = 0;
|
|
while (n < count) {
|
|
int ch = bsp_uart_getc_nonblocking();
|
|
if (ch < 0) {
|
|
break;
|
|
}
|
|
s[n++] = (char)ch;
|
|
}
|
|
return (int)n;
|
|
}
|
|
|
|
int _close(int fd)
|
|
{
|
|
(void)fd;
|
|
return -1;
|
|
}
|
|
|
|
int _fstat(int fd, struct stat *st)
|
|
{
|
|
if (fd < 0 || st == NULL) {
|
|
errno = EBADF;
|
|
return -1;
|
|
}
|
|
st->st_mode = S_IFCHR;
|
|
return 0;
|
|
}
|
|
|
|
int _isatty(int fd)
|
|
{
|
|
return (fd == 0 || fd == 1 || fd == 2) ? 1 : 0;
|
|
}
|
|
|
|
off_t _lseek(int fd, off_t offset, int whence)
|
|
{
|
|
(void)fd;
|
|
(void)offset;
|
|
(void)whence;
|
|
errno = ESPIPE;
|
|
return (off_t)-1;
|
|
}
|
|
|
|
int _getpid(void)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
int _kill(int pid, int sig)
|
|
{
|
|
(void)pid;
|
|
(void)sig;
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
|
|
void _exit(int status)
|
|
{
|
|
uint32_t code = 0x5a5a0000U | ((uint32_t)status & 0xffffU);
|
|
asm volatile ("csrw mscratch, %0" :: "r"(code) : "memory");
|
|
asm volatile ("fence" ::: "memory");
|
|
for (;;) {
|
|
asm volatile ("wfi");
|
|
}
|
|
}
|