[U-Boot] [PATCH 03/15] sandbox: Add a way to write data to the host filesystem

Simon Glass
2018-10-01T17:55:08Z

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

arch/sandbox/cpu/os.c | 20 ++++++++++++++++++++
include/os.h | 13 +++++++++++++
2 files changed, 33 insertions(+)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index 2d20cfe1fa6..3a46038c5fc 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -106,6 +106,26 @@ void os_exit(int exit_code)
exit(exit_code);
}

+int os_write_file(const char *name, const void *buf, int size)
+{
+ char fname[256];
+ int fd;
+
+ fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC);
+ if (fd < 0) {
+ printf("Cannot open file '%s'\n", fname);
+ return -EIO;
+ }
+ if (os_write(fd, buf, size) != size) {
+ printf("Cannot write to file '%s'\n", fname);
+ return -EIO;
+ }
+ os_close(fd);
+ printf("Write '%s', size %#x (%d)\n", name, size, size);
+
+ return 0;
+}
+
/* 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 9e3a561b65e..efa9e52d124 100644
--- a/include/os.h
+++ b/include/os.h
@@ -347,4 +347,17 @@ void os_abort(void);
*/
int os_mprotect_allow(void *start, size_t len);

+/**
+ * os_write_file() - Write a file to the host filesystem
+ *
+ * This can be useful when debugging for writing data out of sandbox for
+ * inspection by external tools.
+ *
+ * @name: File path to write to
+ * @buf: Data to write
+ * @size: Size of data to write
+ * @return 0 if OK, -ve on error
+ */
+int os_write_file(const char *name, const void *buf, int size);
+
#endif
--
2.19.0.605.g01d371f741-goog