aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2018-11-06 15:21:25 -0700
committerSimon Glass <sjg@chromium.org>2018-11-20 19:14:22 -0700
commit566bf3a8698780079196da742c363ca3b627ca31 (patch)
tree7e964e22c7aeda92a9a6e72ce3df124e971033a8
parenta5c13b68e7516d3680c87f7207dcf337a1b3dd7c (diff)
downloadu-boot-566bf3a8698780079196da742c363ca3b627ca31.zip
u-boot-566bf3a8698780079196da742c363ca3b627ca31.tar.gz
u-boot-566bf3a8698780079196da742c363ca3b627ca31.tar.bz2
sandbox: Add a function to read a host file
Add a way to read a file from the host filesystem. This can be useful for reading test data, for example. Also fix up the writing function which was not the right version, and drop the debugging lines. Signed-off-by: Simon Glass <sjg@chromium.org>
-rw-r--r--arch/sandbox/cpu/os.c44
-rw-r--r--include/os.h14
2 files changed, 55 insertions, 3 deletions
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 325ded5..3e0f4c3 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -98,9 +98,8 @@ void os_exit(int exit_code)
exit(exit_code);
}
-int os_write_file(const char *name, const void *buf, int size)
+int os_write_file(const char *fname, const void *buf, int size)
{
- char fname[256];
int fd;
fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
@@ -110,14 +109,53 @@ int os_write_file(const char *name, const void *buf, int size)
}
if (os_write(fd, buf, size) != size) {
printf("Cannot write to file '%s'\n", fname);
+ os_close(fd);
return -EIO;
}
os_close(fd);
- printf("Write '%s', size %#x (%d)\n", name, size, size);
return 0;
}
+int os_read_file(const char *fname, void **bufp, int *sizep)
+{
+ off_t size;
+ int ret = -EIO;
+ int fd;
+
+ fd = os_open(fname, OS_O_RDONLY);
+ if (fd < 0) {
+ printf("Cannot open file '%s'\n", fname);
+ goto err;
+ }
+ size = os_lseek(fd, 0, OS_SEEK_END);
+ if (size < 0) {
+ printf("Cannot seek to end of file '%s'\n", fname);
+ goto err;
+ }
+ if (os_lseek(fd, 0, OS_SEEK_SET) < 0) {
+ printf("Cannot seek to start of file '%s'\n", fname);
+ goto err;
+ }
+ *bufp = os_malloc(size);
+ if (!*bufp) {
+ printf("Not enough memory to read file '%s'\n", fname);
+ ret = -ENOMEM;
+ goto err;
+ }
+ if (os_read(fd, *bufp, size) != size) {
+ printf("Cannot read from file '%s'\n", fname);
+ goto err;
+ }
+ os_close(fd);
+ *sizep = size;
+
+ return 0;
+err:
+ os_close(fd);
+ return ret;
+}
+
/* Restore tty state when we exit */
static struct termios orig_term;
static bool term_setup;
diff --git a/include/os.h b/include/os.h
index 28eb625..6f33b08 100644
--- a/include/os.h
+++ b/include/os.h
@@ -350,4 +350,18 @@ int os_mprotect_allow(void *start, size_t len);
*/
int os_write_file(const char *name, const void *buf, int size);
+/**
+ * os_read_file() - Read a file from the host filesystem
+ *
+ * This can be useful when reading test data into sandbox for use by test
+ * routines. The data is allocated using os_malloc() and should be freed by
+ * the caller.
+ *
+ * @name: File path to read from
+ * @bufp: Returns buffer containing data read
+ * @sizep: Returns size of data
+ * @return 0 if OK, -ve on error
+ */
+int os_read_file(const char *name, void **bufp, int *sizep);
+
#endif