summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2019-09-09 17:40:13 +0100
committerPeter Maydell <peter.maydell@linaro.org>2019-09-10 11:09:09 +0100
commit337391a7055eeda8dcb1387b32af2767411fbea9 (patch)
tree3e1006bf4addaf2c1ec5029e18b62ec600a9feae
parenta47f15b2cd67f00f6352825f52997f7d590e5a6e (diff)
semihosting tests: Add tests for SYS_SEEK
Add basic tests of SYS_SEEK. Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
-rw-r--r--semihosting.c12
-rw-r--r--semihosting.h4
-rw-r--r--usertest.c66
3 files changed, 82 insertions, 0 deletions
diff --git a/semihosting.c b/semihosting.c
index 6211b59..6bd20b8 100644
--- a/semihosting.c
+++ b/semihosting.c
@@ -90,6 +90,18 @@ int semi_istty(int fd)
return __semi_call(SYS_ISTTY, &args);
}
+int semi_seek(int fd, intptr_t pos)
+{
+ struct {
+ intptr_t fd;
+ intptr_t pos;
+ } args;
+
+ args.fd = fd;
+ args.pos = pos;
+ return __semi_call(SYS_SEEK, &args);
+}
+
int semi_get_cmdline(char *buffer, int size, int *length)
{
int result;
diff --git a/semihosting.h b/semihosting.h
index dc097af..731335d 100644
--- a/semihosting.h
+++ b/semihosting.h
@@ -19,6 +19,8 @@
#ifndef SEMIHOSTING_H
#define SEMIHOSTING_H
+#include <inttypes.h>
+
#define SYS_OPEN 1
#define OPEN_RDONLY 1
#define SYS_CLOSE 2
@@ -26,6 +28,7 @@
#define SYS_WRITE0 4
#define SYS_READ 6
#define SYS_ISTTY 9
+#define SYS_SEEK 0x0A
#define SYS_FLEN 0x0C
#define SYS_GET_CMDLINE 0x15
#define SYS_REPORTEXC 0x18
@@ -42,6 +45,7 @@ void semi_writec(char c);
int semi_read(int fd, char *buffer, int length);
int semi_flen(int fd);
int semi_istty(int fd);
+int semi_seek(int fd, intptr_t pos);
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 8bb6e5b..17006c2 100644
--- a/usertest.c
+++ b/usertest.c
@@ -95,6 +95,68 @@ static int test_istty(void)
return 0;
}
+/*
+ * Caution: don't pass a 'pos' that would seek off the end of the file or
+ * so close to the end that the read fails. We check for this, but it's
+ * a test-coding bug.
+ */
+#define CHUNK_SZ 12
+static int test_one_seek(int fd, int pos)
+{
+ int i;
+
+ if ((pos + CHUNK_SZ) >= sizeof(file)) {
+ semi_write0("FAIL test bug: test_one_seek called with pos too large\n");
+ return 1;
+ }
+
+ if (semi_seek(fd, pos) != 0) {
+ semi_write0("FAIL could not seek to byte position\n");
+ return 1;
+ }
+
+ if (semi_read(fd, filebuf, CHUNK_SZ)) {
+ semi_write0("FAIL could not read from seeked-to position\n");
+ return 1;
+ }
+
+ for (i = 0; i < CHUNK_SZ; i++) {
+ if (filebuf[i] != file[i + pos]) {
+ semi_write0("FAIL mismatch in data read from seeked-to position\n");
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+static int test_seek(void)
+{
+ int fd;
+ int fail = 0;
+
+ fd = semi_open(TESTDATA_FILE, OPEN_RDONLY);
+ if (fd == -1) {
+ semi_write0("FAIL could not open test data file\n");
+ return 1;
+ }
+
+ /* Seek forwards */
+ if (test_one_seek(fd, 6)) {
+ return 1;
+ }
+ semi_write0("PASS seek to position 6 and read data\n");
+
+ /* and then backwards */
+ if (test_one_seek(fd, 2)) {
+ return 1;
+ }
+ semi_write0("PASS seek to position 2 and read data\n");
+
+ semi_close(fd);
+ return 0;
+}
+
int main(void)
{
void *bufp;
@@ -134,6 +196,10 @@ int main(void)
return 1;
}
+ if (test_seek()) {
+ return 1;
+ }
+
semi_write0("ALL TESTS PASSED\n");
semi_exit(0);
/* not reached */