Basic stdio support Trusty.

Note that stderr is buffered because of how the Trusty kernel behaves.

Bug: 110161494
Change-Id: Ic690ebe026c12cd6b4f83e5c71a04359fa94eb68
diff --git a/include/stdio.h b/include/stdio.h
index 8fada3a..009e9dd 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -45,7 +45,8 @@
 #define _IOLBF 1
 #define _IONBF 2
 
-#define BUFSIZ 1024
+/* TRUSTY - reduce the buffer size to save memory. */
+#define BUFSIZ 120
 #define FILENAME_MAX 4096
 #define FOPEN_MAX 1000
 #define TMP_MAX 10000
diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h
index d7398f5..b613c43 100644
--- a/src/internal/stdio_impl.h
+++ b/src/internal/stdio_impl.h
@@ -2,7 +2,6 @@
 #define _STDIO_IMPL_H
 
 #include <stdio.h>
-#include "syscall.h"
 
 #define UNGET 8
 
diff --git a/src/stdio/__stdio_close.c b/src/stdio/__stdio_close.c
index 79452bd..eae010a 100644
--- a/src/stdio/__stdio_close.c
+++ b/src/stdio/__stdio_close.c
@@ -9,5 +9,6 @@
 
 int __stdio_close(FILE *f)
 {
-	return syscall(SYS_close, __aio_close(f->fd));
+	/* TRUSTY - no close */
+	return -1;
 }
diff --git a/src/stdio/__stdio_seek.c b/src/stdio/__stdio_seek.c
index 13e06a6..83d255e 100644
--- a/src/stdio/__stdio_seek.c
+++ b/src/stdio/__stdio_seek.c
@@ -2,12 +2,6 @@
 
 off_t __stdio_seek(FILE *f, off_t off, int whence)
 {
-	off_t ret;
-#ifdef SYS__llseek
-	if (syscall(SYS__llseek, f->fd, off>>32, off, &ret, whence)<0)
-		ret = -1;
-#else
-	ret = syscall(SYS_lseek, f->fd, off, whence);
-#endif
-	return ret;
+	/* TRUSTY - no seek */
+	return -1;
 }
diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
index d2d8947..a02cc74 100644
--- a/src/stdio/__stdio_write.c
+++ b/src/stdio/__stdio_write.c
@@ -1,5 +1,6 @@
 #include "stdio_impl.h"
 #include <sys/uio.h>
+#include <trusty_syscalls.h>
 
 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
 {
@@ -12,7 +13,7 @@
 	int iovcnt = 2;
 	ssize_t cnt;
 	for (;;) {
-		cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
+		cnt = _trusty_writev(f->fd, iov, iovcnt);
 		if (cnt == rem) {
 			f->wend = f->buf + f->buf_size;
 			f->wpos = f->wbase = f->buf;
diff --git a/src/stdio/stderr.c b/src/stdio/stderr.c
index f2bc464..0463402 100644
--- a/src/stdio/stderr.c
+++ b/src/stdio/stderr.c
@@ -2,13 +2,14 @@
 
 #undef stderr
 
-static unsigned char buf[UNGET];
+/* TRUSTY - buffer stderr to prevent log fragmentation. */
+static unsigned char buf[BUFSIZ+UNGET];
 hidden FILE __stderr_FILE = {
 	.buf = buf+UNGET,
-	.buf_size = 0,
+	.buf_size = sizeof buf-UNGET,
 	.fd = 2,
 	.flags = F_PERM | F_NORD,
-	.lbf = -1,
+	.lbf = '\n',
 	.write = __stdio_write,
 	.seek = __stdio_seek,
 	.close = __stdio_close,
diff --git a/src/stdio/stdout.c b/src/stdio/stdout.c
index 4985a41..a213a1d 100644
--- a/src/stdio/stdout.c
+++ b/src/stdio/stdout.c
@@ -9,7 +9,8 @@
 	.fd = 1,
 	.flags = F_PERM | F_NORD,
 	.lbf = '\n',
-	.write = __stdout_write,
+	/* TRUSTY - skip terminal size discovery. */
+	.write = __stdio_write,
 	.seek = __stdio_seek,
 	.close = __stdio_close,
 	.lock = -1,