Commit 7eb2c8d573ad932bf67095f11c6b3beba41064f2

Authored by Simon Glass
Committed by Tom Rini
1 parent a8f6ab5229

sandbox: fs: Add support for saving files to host filesystem

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>

Showing 4 changed files with 49 additions and 4 deletions Side-by-side Diff

common/cmd_sandbox.c
... ... @@ -32,9 +32,16 @@
32 32 return do_ls(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX);
33 33 }
34 34  
  35 +static int do_sandbox_save(cmd_tbl_t *cmdtp, int flag, int argc,
  36 + char * const argv[])
  37 +{
  38 + return do_save(cmdtp, flag, argc, argv, FS_TYPE_SANDBOX, 16);
  39 +}
  40 +
35 41 static cmd_tbl_t cmd_sandbox_sub[] = {
36   - U_BOOT_CMD_MKENT(load, 3, 0, do_sandbox_load, "", ""),
  42 + U_BOOT_CMD_MKENT(load, 7, 0, do_sandbox_load, "", ""),
37 43 U_BOOT_CMD_MKENT(ls, 3, 0, do_sandbox_ls, "", ""),
  44 + U_BOOT_CMD_MKENT(save, 6, 0, do_sandbox_save, "", ""),
38 45 };
39 46  
40 47 static int do_sandbox(cmd_tbl_t *cmdtp, int flag, int argc,
41 48  
... ... @@ -56,9 +63,12 @@
56 63 }
57 64  
58 65 U_BOOT_CMD(
59   - sb, 6, 1, do_sandbox,
  66 + sb, 8, 1, do_sandbox,
60 67 "Miscellaneous sandbox commands",
61   - "load host <addr> <filename> [<bytes> <offset>] - load a file from host\n"
62   - "sb ls host <filename> - save a file to host"
  68 + "load host <dev> <addr> <filename> [<bytes> <offset>] - "
  69 + "load a file from host\n"
  70 + "sb ls host <filename> - list files on host\n"
  71 + "sb save host <dev> <filename> <addr> <bytes> [<offset>] - "
  72 + "save a file to host\n"
63 73 );
... ... @@ -93,6 +93,7 @@
93 93 .close = sandbox_fs_close,
94 94 .ls = sandbox_fs_ls,
95 95 .read = fs_read_sandbox,
  96 + .write = fs_write_sandbox,
96 97 },
97 98 #endif
98 99 {
fs/sandbox/sandboxfs.c
... ... @@ -48,6 +48,26 @@
48 48 return size;
49 49 }
50 50  
  51 +long sandbox_fs_write_at(const char *filename, unsigned long pos,
  52 + void *buffer, unsigned long towrite)
  53 +{
  54 + ssize_t size;
  55 + int fd, ret;
  56 +
  57 + fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
  58 + if (fd < 0)
  59 + return fd;
  60 + ret = os_lseek(fd, pos, OS_SEEK_SET);
  61 + if (ret == -1) {
  62 + os_close(fd);
  63 + return ret;
  64 + }
  65 + size = os_write(fd, buffer, towrite);
  66 + os_close(fd);
  67 +
  68 + return size;
  69 +}
  70 +
51 71 int sandbox_fs_ls(const char *dirname)
52 72 {
53 73 struct os_dirent_node *head, *node;
... ... @@ -80,5 +100,18 @@
80 100 }
81 101  
82 102 return len_read;
  103 +}
  104 +
  105 +int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
  106 +{
  107 + int len_written;
  108 +
  109 + len_written = sandbox_fs_write_at(filename, offset, buf, len);
  110 + if (len_written == -1) {
  111 + printf("** Unable to write file %s **\n", filename);
  112 + return -1;
  113 + }
  114 +
  115 + return len_written;
83 116 }
... ... @@ -26,6 +26,7 @@
26 26 void sandbox_fs_close(void);
27 27 int sandbox_fs_ls(const char *dirname);
28 28 int fs_read_sandbox(const char *filename, void *buf, int offset, int len);
  29 +int fs_write_sandbox(const char *filename, void *buf, int offset, int len);
29 30  
30 31 #endif