[U-Boot] [PATCH V2 1/2] fs: implement size/fatsize/ext4size

From: Stephen Warren swarren@nvidia.com
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 --- v2: New patch --- common/cmd_ext4.c | 14 ++++++++++++++ common/cmd_fat.c | 13 +++++++++++++ common/cmd_fs.c | 13 +++++++++++++ fs/ext4/ext4fs.c | 5 +++++ fs/fat/fat.c | 5 +++++ fs/fs.c | 43 +++++++++++++++++++++++++++++++++++++++++++ fs/sandbox/sandboxfs.c | 5 +++++ include/ext4fs.h | 1 + include/fat.h | 1 + include/fs.h | 9 +++++++++ include/sandboxfs.h | 1 + 11 files changed, 110 insertions(+)
diff --git a/common/cmd_ext4.c b/common/cmd_ext4.c index 68b047ba6aed..6d75dd2b89c6 100644 --- a/common/cmd_ext4.c +++ b/common/cmd_ext4.c @@ -42,6 +42,12 @@ #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[]) { @@ -113,6 +119,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" diff --git a/common/cmd_fat.c b/common/cmd_fat.c index fbe33466fcf0..b9d1ad6d790e 100644 --- a/common/cmd_fat.c +++ b/common/cmd_fat.c @@ -18,6 +18,19 @@ #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); diff --git a/common/cmd_fs.c b/common/cmd_fs.c index 91a205ac1e69..7aeebc6f2eda 100644 --- a/common/cmd_fs.c +++ b/common/cmd_fs.c @@ -20,6 +20,19 @@ #include <command.h> #include <fs.h>
+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." +); + 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); diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c index 417ce7b63bf0..cbdc22026deb 100644 --- a/fs/ext4/ext4fs.c +++ b/fs/ext4/ext4fs.c @@ -182,6 +182,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) diff --git a/fs/fat/fat.c b/fs/fat/fat.c index 54f42eae0d05..561921fa2d36 100644 --- a/fs/fat/fat.c +++ b/fs/fat/fat.c @@ -1243,6 +1243,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 79d432d58fe0..bc57c1d3fb63 100644 --- 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, .write = fs_write_sandbox, }, @@ -126,6 +135,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, }, @@ -223,6 +233,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); @@ -266,6 +289,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) { diff --git a/fs/sandbox/sandboxfs.c b/fs/sandbox/sandboxfs.c index 85079788c990..ba6402c81c0a 100644 --- a/fs/sandbox/sandboxfs.c +++ b/fs/sandbox/sandboxfs.c @@ -80,6 +80,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) { } diff --git a/include/ext4fs.h b/include/ext4fs.h index fbbb002b1673..6c419f3a2337 100644 --- a/include/ext4fs.h +++ b/include/ext4fs.h @@ -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); diff --git a/include/fat.h b/include/fat.h index 63cf78779bd5..20ca3f3dca7d 100644 --- a/include/fat.h +++ b/include/fat.h @@ -198,6 +198,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); diff --git a/include/fs.h b/include/fs.h index 26de0539f7d9..06a45f278832 100644 --- a/include/fs.h +++ b/include/fs.h @@ -51,6 +51,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" * bytes. "offset" may be 0 to read from the start of the file. "len" may be @@ -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 do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], diff --git a/include/sandboxfs.h b/include/sandboxfs.h index a51ad13044e1..e7c32623e101 100644 --- a/include/sandboxfs.h +++ b/include/sandboxfs.h @@ -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); int fs_write_sandbox(const char *filename, void *buf, int offset, int len);

From: Stephen Warren swarren@nvidia.com
DFU read support appears to rely upon dfu->read_medium() updating the passed-by-reference len parameter to indicate the remaining size available for reading.
dfu_read_medium_mmc() never does this, and the implementation of dfu_read_medium_nand() will only work if called just once; it hard-codes the value to the total size of the NAND device irrespective of read offset.
I believe that overloading dfu->read_medium() is confusing. As such, this patch introduces a new function dfu->get_medium_size() which can be used to explicitly find out the medium size, and nothing else. dfu_read() is modified to use this function to set the initial value for dfu->r_left, rather than attempting to use the side-effects of dfu->read_medium() for this purpose.
Due to this change, dfu_read() must initially set dfu->b_left to 0, since no data has been read.
dfu_read_buffer_fill() must also be modified not to adjust dfu->r_left when simply copying data from dfu->i_buf_start to the upload request buffer. r_left represents the amount of data left to be read from HW. That value is not affected by the memcpy(), but only by calls to dfu->read_medium().
After this change, I can read from either a 4MB or 1.5MB chunk of a 4MB eMMC boot partion with CONFIG_SYS_DFU_DATA_BUF_SIZE==1MB. Without this change, attempting to do that would result in DFU read returning no data at all due to r_left never being set.
Signed-off-by: Stephen Warren swarren@nvidia.com --- v2: * Fix dfu_get_medium_size_mmc() to handle DFU_FS_FAT/EXT5 layouts too, by calling the recently introduced "size" command. --- drivers/dfu/dfu.c | 20 ++++++++++++----- drivers/dfu/dfu_mmc.c | 59 ++++++++++++++++++++++++++++++++++++++++++-------- drivers/dfu/dfu_nand.c | 7 +++++- drivers/dfu/dfu_ram.c | 11 +++++----- include/dfu.h | 3 +++ 5 files changed, 79 insertions(+), 21 deletions(-)
diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index dc09ff6466e6..dab5e7048ed5 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -267,7 +267,6 @@ static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size)
dfu->i_buf += chunk; dfu->b_left -= chunk; - dfu->r_left -= chunk; size -= chunk; buf += chunk; readn += chunk; @@ -313,10 +312,19 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) if (dfu->i_buf_start == NULL) return -ENOMEM;
- ret = dfu->read_medium(dfu, 0, dfu->i_buf_start, &dfu->r_left); - if (ret != 0) { - debug("%s: failed to get r_left\n", __func__); - return ret; + dfu->r_left = dfu->get_medium_size(dfu); + if (dfu->r_left < 0) + return dfu->r_left; + switch (dfu->layout) { + case DFU_RAW_ADDR: + case DFU_RAM_ADDR: + break; + default: + if (dfu->r_left >= dfu_buf_size) { + printf("%s: File too big for buffer\n", + __func__); + return -EOVERFLOW; + } }
debug("%s: %s %ld [B]\n", __func__, dfu->name, dfu->r_left); @@ -326,7 +334,7 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) dfu->offset = 0; dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; dfu->i_buf = dfu->i_buf_start; - dfu->b_left = min(dfu_buf_size, dfu->r_left); + dfu->b_left = 0;
dfu->bad_skip = 0;
diff --git a/drivers/dfu/dfu_mmc.c b/drivers/dfu/dfu_mmc.c index 63cc876612c9..322bd8c5d2de 100644 --- a/drivers/dfu/dfu_mmc.c +++ b/drivers/dfu/dfu_mmc.c @@ -12,6 +12,8 @@ #include <errno.h> #include <div64.h> #include <dfu.h> +#include <ext4fs.h> +#include <fat.h> #include <mmc.h>
static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE) @@ -113,22 +115,17 @@ static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len) static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, void *buf, long *len) { + const char *fsname, *opname; char cmd_buf[DFU_CMD_BUF_SIZE]; char *str_env; int ret;
switch (dfu->layout) { case DFU_FS_FAT: - sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s", - op == DFU_OP_READ ? "load" : "write", - dfu->data.mmc.dev, dfu->data.mmc.part, - (unsigned int) buf, dfu->name); + fsname = "fat"; break; case DFU_FS_EXT4: - sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s", - op == DFU_OP_READ ? "load" : "write", - dfu->data.mmc.dev, dfu->data.mmc.part, - (unsigned int) buf, dfu->name); + fsname = "ext4"; break; default: printf("%s: Layout (%s) not (yet) supported!\n", __func__, @@ -136,6 +133,28 @@ static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, return -1; }
+ switch (op) { + case DFU_OP_READ: + opname = "load"; + break; + case DFU_OP_WRITE: + opname = "write"; + break; + case DFU_OP_SIZE: + opname = "size"; + break; + default: + return -1; + } + + sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname, + dfu->data.mmc.dev, dfu->data.mmc.part); + + if (op != DFU_OP_SIZE) + sprintf(cmd_buf + strlen(cmd_buf), " 0x%x", (unsigned int)buf); + + sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name); + if (op == DFU_OP_WRITE) sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
@@ -147,7 +166,7 @@ static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, return ret; }
- if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) { + if (op != DFU_OP_WRITE) { str_env = getenv("filesize"); if (str_env == NULL) { puts("dfu: Wrong file size!\n"); @@ -196,6 +215,27 @@ int dfu_flush_medium_mmc(struct dfu_entity *dfu) return ret; }
+long dfu_get_medium_size_mmc(struct dfu_entity *dfu) +{ + int ret; + long len; + + switch (dfu->layout) { + case DFU_RAW_ADDR: + return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size; + case DFU_FS_FAT: + case DFU_FS_EXT4: + ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len); + if (ret < 0) + return ret; + return len; + default: + printf("%s: Layout (%s) not (yet) supported!\n", __func__, + dfu_get_layout(dfu->layout)); + return -1; + } +} + int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, long *len) { @@ -316,6 +356,7 @@ int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) }
dfu->dev_type = DFU_DEV_MMC; + dfu->get_medium_size = dfu_get_medium_size_mmc; dfu->read_medium = dfu_read_medium_mmc; dfu->write_medium = dfu_write_medium_mmc; dfu->flush_medium = dfu_flush_medium_mmc; diff --git a/drivers/dfu/dfu_nand.c b/drivers/dfu/dfu_nand.c index ccdbef6b75f2..e1c9a8849246 100644 --- a/drivers/dfu/dfu_nand.c +++ b/drivers/dfu/dfu_nand.c @@ -114,6 +114,11 @@ static int dfu_write_medium_nand(struct dfu_entity *dfu, return ret; }
+long dfu_get_medium_size_nand(struct dfu_entity *dfu) +{ + return dfu->data.nand.size; +} + static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf, long *len) { @@ -121,7 +126,6 @@ static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
switch (dfu->layout) { case DFU_RAW_ADDR: - *len = dfu->data.nand.size; ret = nand_block_read(dfu, offset, buf, len); break; default: @@ -220,6 +224,7 @@ int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s) return -1; }
+ dfu->get_medium_size = dfu_get_medium_size_nand; dfu->read_medium = dfu_read_medium_nand; dfu->write_medium = dfu_write_medium_nand; dfu->flush_medium = dfu_flush_medium_nand; diff --git a/drivers/dfu/dfu_ram.c b/drivers/dfu/dfu_ram.c index 335a8e1f2491..b6c6e60c443c 100644 --- a/drivers/dfu/dfu_ram.c +++ b/drivers/dfu/dfu_ram.c @@ -41,14 +41,14 @@ static int dfu_write_medium_ram(struct dfu_entity *dfu, u64 offset, return dfu_transfer_medium_ram(DFU_OP_WRITE, dfu, offset, buf, len); }
+long dfu_get_medium_size_ram(struct dfu_entity *dfu) +{ + return dfu->data.ram.size; +} + static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset, void *buf, long *len) { - if (!*len) { - *len = dfu->data.ram.size; - return 0; - } - return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset, buf, len); }
@@ -69,6 +69,7 @@ int dfu_fill_entity_ram(struct dfu_entity *dfu, char *s) dfu->data.ram.size = simple_strtoul(s, &s, 16);
dfu->write_medium = dfu_write_medium_ram; + dfu->get_medium_size = dfu_get_medium_size_ram; dfu->read_medium = dfu_read_medium_ram;
dfu->inited = 0; diff --git a/include/dfu.h b/include/dfu.h index 26ffbc8e81d2..df720310f2cc 100644 --- a/include/dfu.h +++ b/include/dfu.h @@ -35,6 +35,7 @@ enum dfu_layout { enum dfu_op { DFU_OP_READ = 1, DFU_OP_WRITE, + DFU_OP_SIZE, };
struct mmc_internal_data { @@ -96,6 +97,8 @@ struct dfu_entity { struct ram_internal_data ram; } data;
+ long (*get_medium_size)(struct dfu_entity *dfu); + int (*read_medium)(struct dfu_entity *dfu, u64 offset, void *buf, long *len);

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
DFU read support appears to rely upon dfu->read_medium() updating the passed-by-reference len parameter to indicate the remaining size available for reading.
dfu_read_medium_mmc() never does this, and the implementation of dfu_read_medium_nand() will only work if called just once; it hard-codes the value to the total size of the NAND device irrespective of read offset.
I believe that overloading dfu->read_medium() is confusing. As such, this patch introduces a new function dfu->get_medium_size() which can be used to explicitly find out the medium size, and nothing else. dfu_read() is modified to use this function to set the initial value for dfu->r_left, rather than attempting to use the side-effects of dfu->read_medium() for this purpose.
Due to this change, dfu_read() must initially set dfu->b_left to 0, since no data has been read.
dfu_read_buffer_fill() must also be modified not to adjust dfu->r_left when simply copying data from dfu->i_buf_start to the upload request buffer. r_left represents the amount of data left to be read from HW. That value is not affected by the memcpy(), but only by calls to dfu->read_medium().
After this change, I can read from either a 4MB or 1.5MB chunk of a 4MB eMMC boot partion with CONFIG_SYS_DFU_DATA_BUF_SIZE==1MB. Without this change, attempting to do that would result in DFU read returning no data at all due to r_left never being set.
Signed-off-by: Stephen Warren swarren@nvidia.com
Stephen thanks for the patch.
Acked-by: Lukasz Majewski l.majewski@samsung.com
Test HW: Trats2 (Exynos4412): Tested-by: Lukasz Majewski l.majewski@samsung.com
I will pull this patch to -dfu tree when I receive Tom's and Marek's responde to the first patch in this series.
v2:
- Fix dfu_get_medium_size_mmc() to handle DFU_FS_FAT/EXT5 layouts too, by calling the recently introduced "size" command.
drivers/dfu/dfu.c | 20 ++++++++++++----- drivers/dfu/dfu_mmc.c | 59 ++++++++++++++++++++++++++++++++++++++++++-------- drivers/dfu/dfu_nand.c | 7 +++++- drivers/dfu/dfu_ram.c | 11 +++++----- include/dfu.h | 3 +++ 5 files changed, 79 insertions(+), 21 deletions(-)
diff --git a/drivers/dfu/dfu.c b/drivers/dfu/dfu.c index dc09ff6466e6..dab5e7048ed5 100644 --- a/drivers/dfu/dfu.c +++ b/drivers/dfu/dfu.c @@ -267,7 +267,6 @@ static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) dfu->i_buf += chunk; dfu->b_left -= chunk;
dfu->r_left -= chunk; size -= chunk; buf += chunk; readn += chunk;
@@ -313,10 +312,19 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) if (dfu->i_buf_start == NULL) return -ENOMEM;
ret = dfu->read_medium(dfu, 0, dfu->i_buf_start,
&dfu->r_left);
if (ret != 0) {
debug("%s: failed to get r_left\n",
__func__);
return ret;
dfu->r_left = dfu->get_medium_size(dfu);
if (dfu->r_left < 0)
return dfu->r_left;
switch (dfu->layout) {
case DFU_RAW_ADDR:
case DFU_RAM_ADDR:
break;
default:
if (dfu->r_left >= dfu_buf_size) {
printf("%s: File too big for
buffer\n",
__func__);
return -EOVERFLOW;
}
}
debug("%s: %s %ld [B]\n", __func__, dfu->name,
dfu->r_left); @@ -326,7 +334,7 @@ int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) dfu->offset = 0; dfu->i_buf_end = dfu_get_buf() + dfu_buf_size; dfu->i_buf = dfu->i_buf_start;
dfu->b_left = min(dfu_buf_size, dfu->r_left);
dfu->b_left = 0;
dfu->bad_skip = 0;
diff --git a/drivers/dfu/dfu_mmc.c b/drivers/dfu/dfu_mmc.c index 63cc876612c9..322bd8c5d2de 100644 --- a/drivers/dfu/dfu_mmc.c +++ b/drivers/dfu/dfu_mmc.c @@ -12,6 +12,8 @@ #include <errno.h> #include <div64.h> #include <dfu.h> +#include <ext4fs.h> +#include <fat.h> #include <mmc.h>
static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE) @@ -113,22 +115,17 @@ static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len) static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, void *buf, long *len) {
const char *fsname, *opname; char cmd_buf[DFU_CMD_BUF_SIZE]; char *str_env; int ret;
switch (dfu->layout) { case DFU_FS_FAT:
sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
op == DFU_OP_READ ? "load" : "write",
dfu->data.mmc.dev, dfu->data.mmc.part,
(unsigned int) buf, dfu->name);
break; case DFU_FS_EXT4:fsname = "fat";
sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
op == DFU_OP_READ ? "load" : "write",
dfu->data.mmc.dev, dfu->data.mmc.part,
(unsigned int) buf, dfu->name);
break; default: printf("%s: Layout (%s) not (yet) supported!\n",fsname = "ext4";
__func__, @@ -136,6 +133,28 @@ static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, return -1; }
- switch (op) {
- case DFU_OP_READ:
opname = "load";
break;
- case DFU_OP_WRITE:
opname = "write";
break;
- case DFU_OP_SIZE:
opname = "size";
break;
- default:
return -1;
- }
- sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
dfu->data.mmc.dev, dfu->data.mmc.part);
- if (op != DFU_OP_SIZE)
sprintf(cmd_buf + strlen(cmd_buf), " 0x%x",
(unsigned int)buf); +
- sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
- if (op == DFU_OP_WRITE) sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
@@ -147,7 +166,7 @@ static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, return ret; }
- if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
- if (op != DFU_OP_WRITE) { str_env = getenv("filesize"); if (str_env == NULL) { puts("dfu: Wrong file size!\n");
@@ -196,6 +215,27 @@ int dfu_flush_medium_mmc(struct dfu_entity *dfu) return ret; }
+long dfu_get_medium_size_mmc(struct dfu_entity *dfu) +{
- int ret;
- long len;
- switch (dfu->layout) {
- case DFU_RAW_ADDR:
return dfu->data.mmc.lba_size *
dfu->data.mmc.lba_blk_size;
- case DFU_FS_FAT:
- case DFU_FS_EXT4:
ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
if (ret < 0)
return ret;
return len;
- default:
printf("%s: Layout (%s) not (yet) supported!\n",
__func__,
dfu_get_layout(dfu->layout));
return -1;
- }
+}
int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, long *len) { @@ -316,6 +356,7 @@ int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) }
dfu->dev_type = DFU_DEV_MMC;
- dfu->get_medium_size = dfu_get_medium_size_mmc; dfu->read_medium = dfu_read_medium_mmc; dfu->write_medium = dfu_write_medium_mmc; dfu->flush_medium = dfu_flush_medium_mmc;
diff --git a/drivers/dfu/dfu_nand.c b/drivers/dfu/dfu_nand.c index ccdbef6b75f2..e1c9a8849246 100644 --- a/drivers/dfu/dfu_nand.c +++ b/drivers/dfu/dfu_nand.c @@ -114,6 +114,11 @@ static int dfu_write_medium_nand(struct dfu_entity *dfu, return ret; }
+long dfu_get_medium_size_nand(struct dfu_entity *dfu) +{
- return dfu->data.nand.size;
+}
static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf, long *len) { @@ -121,7 +126,6 @@ static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf, switch (dfu->layout) { case DFU_RAW_ADDR:
ret = nand_block_read(dfu, offset, buf, len); break; default:*len = dfu->data.nand.size;
@@ -220,6 +224,7 @@ int dfu_fill_entity_nand(struct dfu_entity *dfu, char *s) return -1; }
- dfu->get_medium_size = dfu_get_medium_size_nand; dfu->read_medium = dfu_read_medium_nand; dfu->write_medium = dfu_write_medium_nand; dfu->flush_medium = dfu_flush_medium_nand;
diff --git a/drivers/dfu/dfu_ram.c b/drivers/dfu/dfu_ram.c index 335a8e1f2491..b6c6e60c443c 100644 --- a/drivers/dfu/dfu_ram.c +++ b/drivers/dfu/dfu_ram.c @@ -41,14 +41,14 @@ static int dfu_write_medium_ram(struct dfu_entity *dfu, u64 offset, return dfu_transfer_medium_ram(DFU_OP_WRITE, dfu, offset, buf, len); }
+long dfu_get_medium_size_ram(struct dfu_entity *dfu) +{
- return dfu->data.ram.size;
+}
static int dfu_read_medium_ram(struct dfu_entity *dfu, u64 offset, void *buf, long *len) {
- if (!*len) {
*len = dfu->data.ram.size;
return 0;
- }
- return dfu_transfer_medium_ram(DFU_OP_READ, dfu, offset,
buf, len); }
@@ -69,6 +69,7 @@ int dfu_fill_entity_ram(struct dfu_entity *dfu, char *s) dfu->data.ram.size = simple_strtoul(s, &s, 16);
dfu->write_medium = dfu_write_medium_ram;
dfu->get_medium_size = dfu_get_medium_size_ram; dfu->read_medium = dfu_read_medium_ram;
dfu->inited = 0;
diff --git a/include/dfu.h b/include/dfu.h index 26ffbc8e81d2..df720310f2cc 100644 --- a/include/dfu.h +++ b/include/dfu.h @@ -35,6 +35,7 @@ enum dfu_layout { enum dfu_op { DFU_OP_READ = 1, DFU_OP_WRITE,
- DFU_OP_SIZE,
};
struct mmc_internal_data { @@ -96,6 +97,8 @@ struct dfu_entity { struct ram_internal_data ram; } data;
- long (*get_medium_size)(struct dfu_entity *dfu);
- int (*read_medium)(struct dfu_entity *dfu, u64 offset, void *buf, long *len);

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
DFU read support appears to rely upon dfu->read_medium() updating the passed-by-reference len parameter to indicate the remaining size available for reading.
dfu_read_medium_mmc() never does this, and the implementation of dfu_read_medium_nand() will only work if called just once; it hard-codes the value to the total size of the NAND device irrespective of read offset.
I believe that overloading dfu->read_medium() is confusing. As such, this patch introduces a new function dfu->get_medium_size() which can be used to explicitly find out the medium size, and nothing else. dfu_read() is modified to use this function to set the initial value for dfu->r_left, rather than attempting to use the side-effects of dfu->read_medium() for this purpose.
Due to this change, dfu_read() must initially set dfu->b_left to 0, since no data has been read.
dfu_read_buffer_fill() must also be modified not to adjust dfu->r_left when simply copying data from dfu->i_buf_start to the upload request buffer. r_left represents the amount of data left to be read from HW. That value is not affected by the memcpy(), but only by calls to dfu->read_medium().
After this change, I can read from either a 4MB or 1.5MB chunk of a 4MB eMMC boot partion with CONFIG_SYS_DFU_DATA_BUF_SIZE==1MB. Without this change, attempting to do that would result in DFU read returning no data at all due to r_left never being set.
Signed-off-by: Stephen Warren swarren@nvidia.com
Applied to u-boot-dfu
Thanks for the patch.

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
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
v2: New patch
Stephen thanks for the patch.
Acked-by: Lukasz Majewski l.majewski@samsung.com
Test HW: Trats2 (Exynos4412): Tested-by: Lukasz Majewski l.majewski@samsung.com
Organizational issue:
File systems don't have their own trees, so if Tom or Marek don't mind I would like to take this patch to -dfu tree.
Justification: This patch was written solely for fixing flaws of DFU, so I think that it is appropriate to take it to -dfu tree and in this way provide consistency with the following DFU patch.

On Tue, Jun 17, 2014 at 10:57:19AM +0200, Lukasz Majewski wrote:
Hi Stephen,
From: Stephen Warren swarren@nvidia.com
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
v2: New patch
Stephen thanks for the patch.
Acked-by: Lukasz Majewski l.majewski@samsung.com
Test HW: Trats2 (Exynos4412): Tested-by: Lukasz Majewski l.majewski@samsung.com
Organizational issue:
File systems don't have their own trees, so if Tom or Marek don't mind I would like to take this patch to -dfu tree.
Justification: This patch was written solely for fixing flaws of DFU, so I think that it is appropriate to take it to -dfu tree and in this way provide consistency with the following DFU patch.
Works for me, thanks.

Hi Stephen,
From: Stephen Warren swarren@nvidia.com
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
Applied to u-boot-dfu
Thanks for the patch.
participants (3)
-
Lukasz Majewski
-
Stephen Warren
-
Tom Rini