summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2019-09-09 18:03:17 +0100
committerPeter Maydell <peter.maydell@linaro.org>2019-09-10 11:09:33 +0100
commit22ba1a364d876f31c3db54e6a3dc8a97aa2652d9 (patch)
tree381c7a44354f84500d3d56710a54e80d31d36ef1
parent337391a7055eeda8dcb1387b32af2767411fbea9 (diff)
semihosting tests: Add basic test of SYS_WRITE.
Add a basic test of SYS_WRITE. Note that since we use a fixed filename for the temporary test file, trying to run multiple instances of the test in parallel from the same directory will not work. Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
-rw-r--r--semihosting.c27
-rw-r--r--semihosting.h5
-rw-r--r--usertest.c55
3 files changed, 87 insertions, 0 deletions
diff --git a/semihosting.c b/semihosting.c
index 6bd20b8..9c284af 100644
--- a/semihosting.c
+++ b/semihosting.c
@@ -70,6 +70,21 @@ int semi_read(int fd, char *buffer, int length)
return __semi_call(SYS_READ, &args);
}
+int semi_write(int fd, const char *buffer, int length)
+{
+ struct {
+ intptr_t fd;
+ const char *buffer;
+ intptr_t length;
+ } args;
+
+ args.fd = fd;
+ args.buffer = buffer;
+ args.length = length;
+
+ return __semi_call(SYS_WRITE, &args);
+}
+
int semi_flen(int fd)
{
struct {
@@ -102,6 +117,18 @@ int semi_seek(int fd, intptr_t pos)
return __semi_call(SYS_SEEK, &args);
}
+int semi_remove(const char *filename)
+{
+ struct {
+ const char *filename;
+ intptr_t flen;
+ } args;
+
+ args.filename = filename;
+ args.flen = strlen(filename);
+ return __semi_call(SYS_REMOVE, &args);
+}
+
int semi_get_cmdline(char *buffer, int size, int *length)
{
int result;
diff --git a/semihosting.h b/semihosting.h
index 731335d..8361ffd 100644
--- a/semihosting.h
+++ b/semihosting.h
@@ -23,13 +23,16 @@
#define SYS_OPEN 1
#define OPEN_RDONLY 1
+#define OPEN_WRONLY 4
#define SYS_CLOSE 2
#define SYS_WRITEC 3
#define SYS_WRITE0 4
+#define SYS_WRITE 5
#define SYS_READ 6
#define SYS_ISTTY 9
#define SYS_SEEK 0x0A
#define SYS_FLEN 0x0C
+#define SYS_REMOVE 0x0E
#define SYS_GET_CMDLINE 0x15
#define SYS_REPORTEXC 0x18
#define REPORTEXC_REASON_APP_EXIT 0x20026
@@ -43,9 +46,11 @@ int semi_close(int fd);
int semi_write0(char const *string);
void semi_writec(char c);
int semi_read(int fd, char *buffer, int length);
+int semi_write(int fd, const char *buffer, int length);
int semi_flen(int fd);
int semi_istty(int fd);
int semi_seek(int fd, intptr_t pos);
+int semi_remove(const char *filename);
int semi_get_cmdline(char *buffer, int size, int *length);
int semi_reportexc(int reason, int subcode);
void semi_fatal(char const *message);
diff --git a/usertest.c b/usertest.c
index 17006c2..6204073 100644
--- a/usertest.c
+++ b/usertest.c
@@ -40,6 +40,9 @@ void semi_putc(void *p, char c)
#define TESTDATA_FILE "testdata.txt"
const char file[] = "Small file of text data for test.\n";
+#define TESTWRITE_FILE "tempdata.txt"
+const char writedata[] = "Data to write to temporary file.\n";
+
static int test_istty(void)
{
int fd;
@@ -157,6 +160,54 @@ static int test_seek(void)
return 0;
}
+static int test_write(void)
+{
+ int fd, i;
+ void *bufp;
+ unsigned int sz;
+
+ fd = semi_open(TESTWRITE_FILE, OPEN_WRONLY);
+ if (fd == -1) {
+ semi_write0("FAIL could not open temp data file for write\n");
+ return 1;
+ }
+
+ /* Write some data */
+ if (semi_write(fd, writedata, sizeof(writedata) - 1) != 0) {
+ semi_write0("FAIL could not write data\n");
+ return 1;
+ }
+
+ semi_close(fd);
+
+ /* Read it back and check for a match */
+ bufp = filebuf;
+ sz = sizeof(filebuf);
+ if (semi_load_file(&bufp, &sz, TESTWRITE_FILE) < 0) {
+ semi_write0("FAIL could not read back written data\n");
+ return 1;
+ }
+
+ if (sz == sizeof(writedata) - 1) {
+ semi_write0("PASS written data file size matches expected\n");
+ } else {
+ semi_write0("FAIL written data file size incorrect\n");
+ return 1;
+ }
+
+ for (i = 0; i < sz; i++) {
+ if (filebuf[i] != writedata[i]) {
+ semi_write0("FAIL written file contents don't match expected\n");
+ return 1;
+ }
+ }
+ semi_write0("PASS written data file contents match\n");
+
+ semi_remove(TESTWRITE_FILE);
+
+ return 0;
+}
+
int main(void)
{
void *bufp;
@@ -200,6 +251,10 @@ int main(void)
return 1;
}
+ if (test_write()) {
+ return 1;
+ }
+
semi_write0("ALL TESTS PASSED\n");
semi_exit(0);
/* not reached */