diff options
-rw-r--r-- | semihosting.c | 12 | ||||
-rw-r--r-- | semihosting.h | 4 | ||||
-rw-r--r-- | usertest.c | 66 |
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); @@ -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 */ |