[RFT PATCH v5 2/3] image: Add a common compression type detection function.

Atish Patra atish.patra at wdc.com
Fri Mar 6 01:24:22 CET 2020


Currently, there is no method that can detect compression types
given a file. This is very useful where a compressed kernel image
is loaded directly to the memory.

Inspect initial few bytes to figure out compression type of the
image. It will be used in booti method for now but can be reused
any other function in future as well.

Signed-off-by: Atish Patra <atish.patra at wdc.com>
Reviewed-by: Tom Rini <trini at konsulko.com>
---
 common/image.c  | 23 +++++++++++++++++++++++
 include/image.h | 21 +++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/common/image.c b/common/image.c
index 94873cb6ed50..d8d14e871c64 100644
--- a/common/image.c
+++ b/common/image.c
@@ -202,6 +202,14 @@ struct table_info {
 	const table_entry_t *table;
 };
 
+static const struct comp_magic_map image_comp[] = {
+	{	IH_COMP_BZIP2,	"bzip2",	{0x42, 0x5a},},
+	{	IH_COMP_GZIP,	"gzip",		{0x1f, 0x8b},},
+	{	IH_COMP_LZMA,	"lzma",		{0x5d, 0x00},},
+	{	IH_COMP_LZO,	"lzo",		{0x89, 0x4c},},
+	{	IH_COMP_NONE,	"none",		{},	},
+};
+
 static const struct table_info table_info[IH_COUNT] = {
 	{ "architecture", IH_ARCH_COUNT, uimage_arch },
 	{ "compression", IH_COMP_COUNT, uimage_comp },
@@ -407,6 +415,21 @@ static void print_decomp_msg(int comp_type, int type, bool is_xip)
 		printf("   Uncompressing %s\n", name);
 }
 
+int image_decomp_type(const unsigned char *buf, ulong len)
+{
+	const struct comp_magic_map *cmagic = image_comp;
+
+	if (len < 2)
+		return -EINVAL;
+
+	for (; cmagic->comp_id > 0; cmagic++) {
+		if (!memcmp(buf, cmagic->magic, 2))
+			break;
+	}
+
+	return cmagic->comp_id;
+}
+
 int image_decomp(int comp, ulong load, ulong image_start, int type,
 		 void *load_buf, void *image_buf, ulong image_len,
 		 uint unc_len, ulong *load_end)
diff --git a/include/image.h b/include/image.h
index b316d167d8d7..9bbb1acfb3af 100644
--- a/include/image.h
+++ b/include/image.h
@@ -452,6 +452,15 @@ typedef struct table_entry {
 	char	*lname;		/* long (output) name to print for messages */
 } table_entry_t;
 
+/*
+ * Compression type and magic number mapping table.
+ */
+struct comp_magic_map {
+	int		comp_id;
+	const char	*name;
+	unsigned char	magic[2];
+};
+
 /*
  * get_table_entry_id() scans the translation table trying to find an
  * entry that matches the given short name. If a matching entry is
@@ -868,6 +877,18 @@ static inline int image_check_target_arch(const image_header_t *hdr)
 }
 #endif /* USE_HOSTCC */
 
+/**
+ * image_decomp_type() - Find out compression type of an image
+ *
+ * @buf:	Address in U-Boot memory where image is loaded.
+ * @len:	Length of the compressed image.
+ * @return	compression type or IH_COMP_NONE if not compressed.
+ *
+ * Note: Only following compression types are supported now.
+ * lzo, lzma, gzip, bzip2
+ */
+int image_decomp_type(const unsigned char *buf, ulong len);
+
 /**
  * image_decomp() - decompress an image
  *
-- 
2.24.0



More information about the U-Boot mailing list