From: Lothar Waßmann Date: Wed, 29 Jun 2016 09:06:58 +0000 (+0200) Subject: fs/fs.c: correctly interpret the '(max)len' parameter to fs_read() X-Git-Tag: KARO-TX-2016-07-05~4 X-Git-Url: https://git.kernelconcepts.de/?p=karo-tx-uboot.git;a=commitdiff_plain;h=01ced72cdb2dde4d334069f6170abf0f27b56dbc fs/fs.c: correctly interpret the '(max)len' parameter to fs_read() The 'len' parameter passed to fs_read() actually denotes the maximum number of bytes that fit into the callers buffer, not an expected amount of data to be read. Rename the parameter accordingly and honor the maxlen requested by the caller appropriately. Also remove the bogus warning message printed when the number of bytes read is smaller than maxlen. --- diff --git a/board/karo/common/mmc.c b/board/karo/common/mmc.c index 0048bfb4af..6143e15756 100644 --- a/board/karo/common/mmc.c +++ b/board/karo/common/mmc.c @@ -165,17 +165,15 @@ int karo_load_mmc_part(const char *part, void *addr, size_t len) } else if (partnum == 0) { loff_t len_read; - debug("Reading file %s from mmc partition %d\n", part, - partnum); + debug("Trying to read (%u) byte from file '%s' in mmc partition %d\n", + len, part, partnum); ret = fs_read(part, (ulong)addr, 0, len, &len_read); if (ret < 0) { - printf("Failed to read %u byte from mmc partition %d\n", - len, partnum); + printf("Failed to read %u byte from %s in mmc partition %d; err: %d\n", + len, part, partnum, ret); goto out; } - if (len_read < len) { - printf("Read only %llu of %u bytes\n", (u64)len_read, len); - } + debug("Read %llu bytes from %s\n", len_read, part); } else { ret = partnum; goto out; diff --git a/fs/fs.c b/fs/fs.c index 827b143e85..0299c4b138 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -285,24 +285,30 @@ int fs_size(const char *filename, loff_t *size) return ret; } -int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, +int fs_read(const char *filename, ulong addr, loff_t offset, loff_t maxlen, loff_t *actread) { struct fstype_info *info = fs_get_info(fs_type); void *buf; int ret; + loff_t len; - /* - * We don't actually know how many bytes are being read, since len==0 - * means read the whole file. - */ + ret = info->size(filename, &len); + if (ret) { + printf("Failed to determine size of file %s: %d\n", + filename, ret); + goto err; + } + if (maxlen == 0) + maxlen = len; + else if (len > maxlen) + printf("** File %s larger than buffer size; truncating to %llu of %llu bytes\n", + filename, maxlen, len); + len = min(len, maxlen); buf = map_sysmem(addr, len); ret = info->read(filename, buf, offset, len, actread); +err: unmap_sysmem(buf); - - /* If we requested a specific number of bytes, check we got it */ - if (ret == 0 && len && *actread != len) - printf("** %s shorter than offset + len **\n", filename); fs_close(); return ret;