diff mbox

[U-Boot,v3,16/17] sandbox: fs: Add support for saving files to host filesystem

Message ID 1366483372-16523-17-git-send-email-sjg@chromium.org
State Accepted, archived
Delegated to: Simon Glass
Headers show

Commit Message

Simon Glass April 20, 2013, 6:42 p.m. UTC
This allows write of files from the host filesystem in sandbox. There is
currently no concept of overwriting the file and removing its existing
contents - all writing is done on top of what is there. This means that
writing 10 bytes to the start of a 1KB file will only update those 10
bytes, not truncate the file to 10 byte slong.

If the file does not exist it is created.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
Changes in v3: None
Changes in v2: None

 common/cmd_sandbox.c   | 18 ++++++++++++++----
 fs/fs.c                |  1 +
 fs/sandbox/sandboxfs.c | 33 +++++++++++++++++++++++++++++++++
 include/sandboxfs.h    |  1 +
 4 files changed, 49 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/common/cmd_sandbox.c b/common/cmd_sandbox.c
index 206a486..a28a844 100644
--- a/common/cmd_sandbox.c
+++ b/common/cmd_sandbox.c
@@ -32,9 +32,16 @@  static int do_sandbox_ls(cmd_tbl_t *cmdtp, int flag, int argc,
 	return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
 }
 
+static int do_sandbox_save(cmd_tbl_t *cmdtp, int flag, int argc,
+			   char * const argv[])
+{
+	return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX, 16);
+}
+
 static cmd_tbl_t cmd_sandbox_sub[] = {
-	U_BOOT_CMD_MKENT(load, 3, 0, do_sandbox_load, "", ""),
+	U_BOOT_CMD_MKENT(load, 7, 0, do_sandbox_load, "", ""),
 	U_BOOT_CMD_MKENT(ls, 3, 0, do_sandbox_ls, "", ""),
+	U_BOOT_CMD_MKENT(save, 6, 0, do_sandbox_save, "", ""),
 };
 
 static int do_sandbox(cmd_tbl_t *cmdtp, int flag, int argc,
@@ -56,8 +63,11 @@  static int do_sandbox(cmd_tbl_t *cmdtp, int flag, int argc,
 }
 
 U_BOOT_CMD(
-	sb,	6,	1,	do_sandbox,
+	sb,	8,	1,	do_sandbox,
 	"Miscellaneous sandbox commands",
-	"load host <addr> <filename> [<bytes> <offset>]  - load a file from host\n"
-	"sb ls host <filename>      - save a file to host"
+	"load host <dev> <addr> <filename> [<bytes> <offset>]  - "
+		"load a file from host\n"
+	"sb ls host <filename>                      - list files on host\n"
+	"sb save host <dev> <filename> <addr> <bytes> [<offset>] - "
+		"save a file to host\n"
 );
diff --git a/fs/fs.c b/fs/fs.c
index eee7e23..99e516a 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -93,6 +93,7 @@  static struct fstype_info fstypes[] = {
 		.close = sandbox_fs_close,
 		.ls = sandbox_fs_ls,
 		.read = fs_read_sandbox,
+		.write = fs_write_sandbox,
 	},
 #endif
 	{
diff --git a/fs/sandbox/sandboxfs.c b/fs/sandbox/sandboxfs.c
index 02d26ff..89769e8 100644
--- a/fs/sandbox/sandboxfs.c
+++ b/fs/sandbox/sandboxfs.c
@@ -48,6 +48,26 @@  long sandbox_fs_read_at(const char *filename, unsigned long pos,
 	return size;
 }
 
+long sandbox_fs_write_at(const char *filename, unsigned long pos,
+			 void *buffer, unsigned long towrite)
+{
+	ssize_t size;
+	int fd, ret;
+
+	fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
+	if (fd < 0)
+		return fd;
+	ret = os_lseek(fd, pos, OS_SEEK_SET);
+	if (ret == -1) {
+		os_close(fd);
+		return ret;
+	}
+	size = os_write(fd, buffer, towrite);
+	os_close(fd);
+
+	return size;
+}
+
 int sandbox_fs_ls(const char *dirname)
 {
 	struct os_dirent_node *head, *node;
@@ -81,3 +101,16 @@  int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
 
 	return len_read;
 }
+
+int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
+{
+	int len_written;
+
+	len_written = sandbox_fs_write_at(filename, offset, buf, len);
+	if (len_written == -1) {
+		printf("** Unable to write file %s **\n", filename);
+		return -1;
+	}
+
+	return len_written;
+}
diff --git a/include/sandboxfs.h b/include/sandboxfs.h
index f5213ac..8ea8cb7 100644
--- a/include/sandboxfs.h
+++ b/include/sandboxfs.h
@@ -26,5 +26,6 @@  long sandbox_fs_read_at(const char *filename, unsigned long pos,
 void sandbox_fs_close(void);
 int sandbox_fs_ls(const char *dirname);
 int fs_read_sandbox(const char *filename, void *buf, int offset, int len);
+int fs_write_sandbox(const char *filename, void *buf, int offset, int len);
 
 #endif