[U-Boot] [PATCH 1/2] cmd/imxtract: Move decompression part to separate function

Alexey Ignatov lexszero at gmail.com
Sat May 20 06:50:19 UTC 2017


Signed-off-by: Alexey Ignatov <lexszero at gmail.com>
---
 cmd/ximg.c | 142 +++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 76 insertions(+), 66 deletions(-)

diff --git a/cmd/ximg.c b/cmd/ximg.c
index d033c15b62..73a571b52b 100644
--- a/cmd/ximg.c
+++ b/cmd/ximg.c
@@ -28,6 +28,81 @@
 #define CONFIG_SYS_XIMG_LEN	0x800000
 #endif
 
+static int decompress_data(ulong dest, ulong data, ulong len,
+		uint8_t comp, int part)
+{
+#ifdef CONFIG_GZIP
+	uint		unc_len = CONFIG_SYS_XIMG_LEN;
+#endif
+
+	switch (comp) {
+	case IH_COMP_NONE:
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+		{
+			size_t l = len;
+			size_t tail;
+			void *to = (void *) dest;
+			void *from = (void *)data;
+
+			printf("   Loading part %d ... ", part);
+
+			while (l > 0) {
+				tail = (l > CHUNKSZ) ? CHUNKSZ : l;
+				WATCHDOG_RESET();
+				memmove(to, from, tail);
+				to += tail;
+				from += tail;
+				l -= tail;
+			}
+		}
+#else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
+		printf("   Loading part %d ... ", part);
+		memmove((char *) dest, (char *)data, len);
+#endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
+		break;
+#ifdef CONFIG_GZIP
+	case IH_COMP_GZIP:
+		printf("   Uncompressing part %d ... ", part);
+		if (gunzip((void *) dest, unc_len,
+			   (uchar *) data, &len) != 0) {
+			puts("GUNZIP ERROR - image not loaded\n");
+			return 1;
+		}
+		break;
+#endif
+#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
+	case IH_COMP_BZIP2:
+		{
+			int i;
+
+			printf("   Uncompressing part %d ... ", part);
+			/*
+			 * If we've got less than 4 MB of malloc()
+			 * space, use slower decompression algorithm
+			 * which requires at most 2300 KB of memory.
+			 */
+			i = BZ2_bzBuffToBuffDecompress(
+				map_sysmem(ntohl(hdr->ih_load), 0),
+				&unc_len, (char *)data, len,
+				CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
+				0);
+			if (i != BZ_OK) {
+				printf("BUNZIP2 ERROR %d - "
+					"image not loaded\n", i);
+				return 1;
+			}
+		}
+		break;
+#endif /* CONFIG_BZIP2 */
+	default:
+		printf("Unimplemented compression type %d\n", comp);
+		return 1;
+	}
+	puts("OK\n");
+
+	return 0;
+}
+
 static int
 do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 {
@@ -47,9 +122,6 @@ do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 	const void	*fit_data;
 	size_t		fit_len;
 #endif
-#ifdef CONFIG_GZIP
-	uint		unc_len = CONFIG_SYS_XIMG_LEN;
-#endif
 	uint8_t		comp;
 
 	verify = getenv_yesno("verify");
@@ -183,70 +255,8 @@ do_imgextract(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
 	}
 
 	if (argc > 3) {
-		switch (comp) {
-		case IH_COMP_NONE:
-#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
-			{
-				size_t l = len;
-				size_t tail;
-				void *to = (void *) dest;
-				void *from = (void *)data;
-
-				printf("   Loading part %d ... ", part);
-
-				while (l > 0) {
-					tail = (l > CHUNKSZ) ? CHUNKSZ : l;
-					WATCHDOG_RESET();
-					memmove(to, from, tail);
-					to += tail;
-					from += tail;
-					l -= tail;
-				}
-			}
-#else	/* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
-			printf("   Loading part %d ... ", part);
-			memmove((char *) dest, (char *)data, len);
-#endif	/* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
-			break;
-#ifdef CONFIG_GZIP
-		case IH_COMP_GZIP:
-			printf("   Uncompressing part %d ... ", part);
-			if (gunzip((void *) dest, unc_len,
-				   (uchar *) data, &len) != 0) {
-				puts("GUNZIP ERROR - image not loaded\n");
-				return 1;
-			}
-			break;
-#endif
-#if defined(CONFIG_BZIP2) && defined(CONFIG_IMAGE_FORMAT_LEGACY)
-		case IH_COMP_BZIP2:
-			{
-				int i;
-
-				printf("   Uncompressing part %d ... ", part);
-				/*
-				 * If we've got less than 4 MB of malloc()
-				 * space, use slower decompression algorithm
-				 * which requires at most 2300 KB of memory.
-				 */
-				i = BZ2_bzBuffToBuffDecompress(
-					map_sysmem(ntohl(hdr->ih_load), 0),
-					&unc_len, (char *)data, len,
-					CONFIG_SYS_MALLOC_LEN < (4096 * 1024),
-					0);
-				if (i != BZ_OK) {
-					printf("BUNZIP2 ERROR %d - "
-						"image not loaded\n", i);
-					return 1;
-				}
-			}
-			break;
-#endif /* CONFIG_BZIP2 */
-		default:
-			printf("Unimplemented compression type %d\n", comp);
+		if (!decompress_data(dest, data, len, comp, part))
 			return 1;
-		}
-		puts("OK\n");
 	}
 
 	flush_cache(dest, len);
-- 
2.12.2



More information about the U-Boot mailing list