spl: Set FAT bl_len to ARCH_DMA_MINALIGN

Instead of relying on the presence of filename to determine whether we are
dealing with a FAT filesystem (and should DMA-align the buffer), have FAT set
bl_len to ARCH_DMA_MINALIGN instead. With this done, we can remove the
special-case logic checking for the presence of filename.

Because filesystems are not block-based, we may read less than the size passed
to spl_load_info.read. This can happen if the file size is not DMA-aligned. This
is fine as long as we read the amount we originally wanted to. Modify the
conditions for callers of spl_load_info.read to check against the original,
unaligned size to avoid failing spuriously.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/common/spl/spl_blk_fs.c b/common/spl/spl_blk_fs.c
index 144c8a6..c105c55 100644
--- a/common/spl/spl_blk_fs.c
+++ b/common/spl/spl_blk_fs.c
@@ -9,6 +9,7 @@
 #include <spl.h>
 #include <image.h>
 #include <fs.h>
+#include <asm/cache.h>
 #include <asm/io.h>
 
 struct blk_dev {
@@ -85,7 +86,7 @@
 
 		debug("Found FIT\n");
 		load.read = spl_fit_read;
-		load.bl_len = 1;
+		load.bl_len = ARCH_DMA_MINALIGN;
 		load.filename = (void *)filename;
 		load.priv = &dev;
 
diff --git a/common/spl/spl_fat.c b/common/spl/spl_fat.c
index 6172e7b..5b72345 100644
--- a/common/spl/spl_fat.c
+++ b/common/spl/spl_fat.c
@@ -97,7 +97,7 @@
 
 		debug("Found FIT\n");
 		load.read = spl_fit_read;
-		load.bl_len = 1;
+		load.bl_len = ARCH_DMA_MINALIGN;
 		load.filename = (void *)filename;
 
 		return spl_load_simple_fit(spl_image, &load, 0, header);
diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c
index ce7ef0e..0df4e6d 100644
--- a/common/spl/spl_fit.c
+++ b/common/spl/spl_fit.c
@@ -14,7 +14,6 @@
 #include <mapmem.h>
 #include <spl.h>
 #include <sysinfo.h>
-#include <asm/cache.h>
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <linux/libfdt.h>
@@ -172,28 +171,11 @@
 
 static int get_aligned_image_offset(struct spl_load_info *info, int offset)
 {
-	/*
-	 * If it is a FS read, get the first address before offset which is
-	 * aligned to ARCH_DMA_MINALIGN. If it is raw read return the
-	 * block number to which offset belongs.
-	 */
-	if (info->filename)
-		return offset & ~(ARCH_DMA_MINALIGN - 1);
-
 	return ALIGN_DOWN(offset, info->bl_len);
 }
 
 static int get_aligned_image_overhead(struct spl_load_info *info, int offset)
 {
-	/*
-	 * If it is a FS read, get the difference between the offset and
-	 * the first address before offset which is aligned to
-	 * ARCH_DMA_MINALIGN. If it is raw read return the offset within the
-	 * block.
-	 */
-	if (info->filename)
-		return offset & (ARCH_DMA_MINALIGN - 1);
-
 	return offset & (info->bl_len - 1);
 }
 
@@ -202,9 +184,6 @@
 {
 	data_size = data_size + get_aligned_image_overhead(info, offset);
 
-	if (info->filename)
-		return data_size;
-
 	return ALIGN(data_size, info->bl_len);
 }
 
@@ -295,7 +274,7 @@
 		if (info->read(info,
 			       fit_offset +
 			       get_aligned_image_offset(info, offset), size,
-			       src_ptr) != size)
+			       src_ptr) < length)
 			return -EIO;
 
 		debug("External data: dst=%p, offset=%x, size=%lx\n",
diff --git a/common/spl/spl_imx_container.c b/common/spl/spl_imx_container.c
index ad89a99..7cd674f 100644
--- a/common/spl/spl_imx_container.c
+++ b/common/spl/spl_imx_container.c
@@ -45,7 +45,8 @@
 	      container, offset, size);
 	if (info->read(info, offset, size,
 		       map_sysmem(images[image_index].dst - overhead,
-				  images[image_index].size)) != size) {
+				  images[image_index].size)) <
+	    images[image_index].size) {
 		printf("%s wrong\n", __func__);
 		return NULL;
 	}
@@ -77,7 +78,8 @@
 
 	debug("%s: container: %p offset: %lu size: %u\n", __func__,
 	      container, offset, size);
-	if (info->read(info, offset, size, container) != size) {
+	if (info->read(info, offset, size, container) <
+	    CONTAINER_HDR_ALIGNMENT) {
 		ret = -EIO;
 		goto end;
 	}
@@ -107,7 +109,7 @@
 
 		debug("%s: container: %p offset: %lu size: %u\n",
 		      __func__, container, offset, size);
-		if (info->read(info, offset, size, container) != size) {
+		if (info->read(info, offset, size, container) < length) {
 			ret = -EIO;
 			goto end;
 		}