fs: implement size/fatsize/ext4size
authorStephen Warren <swarren@nvidia.com>
Wed, 11 Jun 2014 18:47:26 +0000 (12:47 -0600)
committerChanho Park <chanho61.park@samsung.com>
Thu, 6 Aug 2015 11:17:27 +0000 (20:17 +0900)
These commands may be used to determine the size of a file without
actually reading the whole file content into memory. This may be used
to determine if the file will fit into the memory buffer that will
contain it. In particular, the DFU code will use it for this purpose
in the next commit.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
Conflicts:
common/cmd_fs.c

Change-Id: I2dc81095d94901aa604f970a5578b342b0009390

common/cmd_ext4.c
common/cmd_fat.c
common/cmd_fs.c
fs/ext4/ext4fs.c
fs/fat/fat.c
fs/fs.c
fs/sandbox/sandboxfs.c
include/ext4fs.h
include/fat.h
include/fs.h
include/sandboxfs.h

index 706fd54a553b9fc394a18c3f4add6027838967c0..8d1c83b7a1403afa6c18a349346a3650ce2a94bc 100644 (file)
 #include <usb.h>
 #endif
 
+int do_ext4_size(cmd_tbl_t *cmdtp, int flag, int argc,
+                                               char *const argv[])
+{
+       return do_size(cmdtp, flag, argc, argv, FS_TYPE_EXT);
+}
+
 int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
                                                char *const argv[])
 {
@@ -127,6 +133,14 @@ U_BOOT_CMD(ext4write, 6, 1, do_ext4_write,
 
 #endif
 
+U_BOOT_CMD(
+       ext4size,       4,      0,      do_ext4_size,
+       "determine a file's size",
+       "<interface> <dev[:part]> <filename>\n"
+       "    - Find file 'filename' from 'dev' on 'interface'\n"
+       "      and determine its size."
+);
+
 U_BOOT_CMD(ext4ls, 4, 1, do_ext4_ls,
           "list files in a directory (default /)",
           "<interface> <dev[:part]> [directory]\n"
index dc1b566fdfcc9518d6a05aeddcf3f9cf7a5f7796..4b8c2747df06e093c083fe4dc001330ce4def198 100644 (file)
 #include <fat.h>
 #include <fs.h>
 
+int do_fat_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       return do_size(cmdtp, flag, argc, argv, FS_TYPE_FAT);
+}
+
+U_BOOT_CMD(
+       fatsize,        4,      0,      do_fat_size,
+       "determine a file's size",
+       "<interface> <dev[:part]> <filename>\n"
+       "    - Find file 'filename' from 'dev' on 'interface'\n"
+       "      and determine its size."
+);
+
 int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        return do_load(cmdtp, flag, argc, argv, FS_TYPE_FAT, 16);
index a681d03d1b1d80d515eb7aa82bdbfbf13ce7b84e..65324ac4feed7da610670ba6febc3da667934583 100644 (file)
 #include <command.h>
 #include <fs.h>
 
-int do_load_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_size_wrapper(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       return do_size(cmdtp, flag, argc, argv, FS_TYPE_ANY);
+}
+
+U_BOOT_CMD(
+       size,   4,      0,      do_size_wrapper,
+       "determine a file's size",
+       "<interface> <dev[:part]> <filename>\n"
+       "    - Find file 'filename' from 'dev' on 'interface'\n"
+       "      and determine its size."
+);
+
+static int do_load_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
+                               char * const argv[])
 {
        return do_load(cmdtp, flag, argc, argv, FS_TYPE_ANY, 0);
 }
index 6671402ecec97c90b950dce71929e8624aa34702..3d3d13ed3dc0712f74d0b01ca2b7878ef487cb3f 100644 (file)
@@ -194,6 +194,11 @@ int ext4fs_exists(const char *filename)
        return file_len >= 0;
 }
 
+int ext4fs_size(const char *filename)
+{
+       return ext4fs_open(filename);
+}
+
 int ext4fs_read(char *buf, unsigned len)
 {
        if (ext4fs_root == NULL || ext4fs_file == NULL)
index 4bd4a91fd1e76e3f65cc09f9207f990aeef6f834..1641f7a4d7b253ee53a43d4b8672467867a2a03c 100644 (file)
@@ -1259,6 +1259,11 @@ int fat_exists(const char *filename)
        return sz >= 0;
 }
 
+int fat_size(const char *filename)
+{
+       return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1);
+}
+
 long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
                      unsigned long maxsize)
 {
diff --git a/fs/fs.c b/fs/fs.c
index 05a50d331b2a3f994acb6453854069aca74de24f..bdbcf3ab1030ed252c77b11c5a20d5a6ed13df6a 100644 (file)
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -46,6 +46,11 @@ static inline int fs_exists_unsupported(const char *filename)
        return 0;
 }
 
+static inline int fs_size_unsupported(const char *filename)
+{
+       return -1;
+}
+
 static inline int fs_read_unsupported(const char *filename, void *buf,
                                      int offset, int len)
 {
@@ -77,6 +82,7 @@ struct fstype_info {
                     disk_partition_t *fs_partition);
        int (*ls)(const char *dirname);
        int (*exists)(const char *filename);
+       int (*size)(const char *filename);
        int (*read)(const char *filename, void *buf, int offset, int len);
        int (*write)(const char *filename, void *buf, int offset, int len);
        void (*close)(void);
@@ -91,6 +97,7 @@ static struct fstype_info fstypes[] = {
                .close = fat_close,
                .ls = file_fat_ls,
                .exists = fat_exists,
+               .size = fat_size,
                .read = fat_read_file,
                .write = fs_write_unsupported,
        },
@@ -103,6 +110,7 @@ static struct fstype_info fstypes[] = {
                .close = ext4fs_close,
                .ls = ext4fs_ls,
                .exists = ext4fs_exists,
+               .size = ext4fs_size,
                .read = ext4_read_file,
                .write = fs_write_unsupported,
        },
@@ -115,6 +123,7 @@ static struct fstype_info fstypes[] = {
                .close = sandbox_fs_close,
                .ls = sandbox_fs_ls,
                .exists = sandbox_fs_exists,
+               .size = sandbox_fs_size,
                .read = fs_read_sandbox,
        },
 #endif
@@ -125,6 +134,7 @@ static struct fstype_info fstypes[] = {
                .close = fs_close_unsupported,
                .ls = fs_ls_unsupported,
                .exists = fs_exists_unsupported,
+               .size = fs_size_unsupported,
                .read = fs_read_unsupported,
                .write = fs_write_unsupported,
        },
@@ -222,6 +232,19 @@ int fs_exists(const char *filename)
        return ret;
 }
 
+int fs_size(const char *filename)
+{
+       int ret;
+
+       struct fstype_info *info = fs_get_info(fs_type);
+
+       ret = info->size(filename);
+
+       fs_close();
+
+       return ret;
+}
+
 int fs_read(const char *filename, ulong addr, int offset, int len)
 {
        struct fstype_info *info = fs_get_info(fs_type);
@@ -265,6 +288,26 @@ int fs_write(const char *filename, ulong addr, int offset, int len)
        return ret;
 }
 
+int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
+               int fstype)
+{
+       int size;
+
+       if (argc != 4)
+               return CMD_RET_USAGE;
+
+       if (fs_set_blk_dev(argv[1], argv[2], fstype))
+               return 1;
+
+       size = fs_size(argv[3]);
+       if (size < 0)
+               return CMD_RET_FAILURE;
+
+       setenv_hex("filesize", size);
+
+       return 0;
+}
+
 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
                int fstype, int cmdline_base)
 {
index 57ba608073eaec3fcb452e457c187f72ee59e842..3702782ca5ab57b3c310cc358b6a89dec0d0b6ed 100644 (file)
@@ -73,6 +73,11 @@ int sandbox_fs_exists(const char *filename)
        return sz >= 0;
 }
 
+int sandbox_fs_size(const char *filename)
+{
+       return os_get_filesize(filename);
+}
+
 void sandbox_fs_close(void)
 {
 }
index fbbb002b16735725e9440cd2a7920139076e9beb..6c419f3a233759c7e0998a1e5a25d7792a00bcfe 100644 (file)
@@ -136,6 +136,7 @@ void ext4fs_close(void);
 void ext4fs_reinit_global(void);
 int ext4fs_ls(const char *dirname);
 int ext4fs_exists(const char *filename);
+int ext4fs_size(const char *filename);
 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
 int ext4fs_devread(lbaint_t sector, int byte_offset, int byte_len, char *buf);
 void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
index 6950e0b819218f4f3ba0bf3528236d331ff0b069..a59d2f578b54880f690275cfdec3431f5ecaff1a 100644 (file)
@@ -298,6 +298,7 @@ int file_cd(const char *path);
 int file_fat_detectfs(void);
 int file_fat_ls(const char *dir);
 int fat_exists(const char *filename);
+int fat_size(const char *filename);
 long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
                      unsigned long maxsize);
 long file_fat_read(const char *filename, void *buffer, unsigned long maxsize);
index fb2f3ace3c21b01dbe09b008f8ae7c481bd102da..41198d99633ecc12c27b172d39b21e46fafd2ffb 100644 (file)
@@ -50,6 +50,13 @@ int fs_ls(const char *dirname);
  */
 int fs_exists(const char *filename);
 
+/*
+ * Determine a file's size
+ *
+ * Returns the file's size in bytes, or a negative value if it doesn't exist.
+ */
+int fs_size(const char *filename);
+
 /*
  * Read file "filename" from the partition previously set by fs_set_blk_dev(),
  * to address "addr", starting at byte offset "offset", and reading "len"
@@ -75,6 +82,8 @@ int fs_write(const char *filename, ulong addr, int offset, int len);
  * Common implementation for various filesystem commands, optionally limited
  * to a specific filesystem type via the fstype parameter.
  */
+int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
+               int fstype);
 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
                int fstype, int cmdline_base);
 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
index 12d9b1563de26b29e3609e3e7b56247bea8af1c3..15dee8db0d54ac464f872a79b7386c9208920657 100644 (file)
@@ -26,6 +26,7 @@ long sandbox_fs_read_at(const char *filename, unsigned long pos,
 void sandbox_fs_close(void);
 int sandbox_fs_ls(const char *dirname);
 int sandbox_fs_exists(const char *filename);
+int sandbox_fs_size(const char *filename);
 int fs_read_sandbox(const char *filename, void *buf, int offset, int len);
 
 #endif