diff mbox

[U-Boot,2/2] Add unlzo command

Message ID 1345237184-20849-2-git-send-email-joe.hershberger@ni.com
State Changes Requested
Delegated to: Tom Rini
Headers show

Commit Message

Joe Hershberger Aug. 17, 2012, 8:59 p.m. UTC
Provide access to the lzo decompressor from the command line.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>
---
 common/Makefile    |  3 +++
 common/cmd_unlzo.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 common/cmd_unlzo.c

Comments

Mike Frysinger Aug. 17, 2012, 11:26 p.m. UTC | #1
On Friday 17 August 2012 16:59:44 Joe Hershberger wrote:
> --- a/common/Makefile
> +++ b/common/Makefile
>
>  ifdef CONFIG_LZMA
>  COBJS-$(CONFIG_CMD_UNLZMA) += cmd_unlzma.o
>  endif
> +ifdef CONFIG_LZO
> +COBJS-$(CONFIG_CMD_UNLZO) += cmd_unlzo.o
> +endif

imo, these ifdefs shouldn't exist.  the commands shouldn't get silently 
ignored because someone omitted an option.  add an #ifdef check to the .c file 
and have it #error out if the necessary config options aren't defined.

> --- /dev/null
> +++ b/common/cmd_unlzo.c
>
> +int do_unlzo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])

static

> +	ret = lzop_decompress((void *)src, src_len, (void *)dst, &dst_len);
> +	if (ret != LZO_E_OK) {
> +		printf("unlzo: uncompress or overwrite error %d\n", ret);
> +		return -1;

how about returning ret ?

> +	sprintf(buf, "%lX", (unsigned long) dst_len);
> +	setenv("filesize", buf);

setenv_ulong() ?

> +U_BOOT_CMD(
> +	unlzo,	5,	1,	do_unlzo,
> +	"unlzo a memory region",
> +	"srcaddr srcsize dstaddr [dstsize]"
> +);

isn't there a way you could "stream" this so you don't need the srcsize ?  or 
does the lzop API not support that ?
-mike
Joe Hershberger Sept. 18, 2012, 9:25 p.m. UTC | #2
Hi Mike,

On Fri, Aug 17, 2012 at 6:26 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> On Friday 17 August 2012 16:59:44 Joe Hershberger wrote:
>> --- a/common/Makefile
>> +++ b/common/Makefile
>>
>>  ifdef CONFIG_LZMA
>>  COBJS-$(CONFIG_CMD_UNLZMA) += cmd_unlzma.o
>>  endif
>> +ifdef CONFIG_LZO
>> +COBJS-$(CONFIG_CMD_UNLZO) += cmd_unlzo.o
>> +endif
>
> imo, these ifdefs shouldn't exist.  the commands shouldn't get silently
> ignored because someone omitted an option.  add an #ifdef check to the .c file
> and have it #error out if the necessary config options aren't defined.

OK

>> --- /dev/null
>> +++ b/common/cmd_unlzo.c
>>
>> +int do_unlzo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>
> static

OK

>> +     ret = lzop_decompress((void *)src, src_len, (void *)dst, &dst_len);
>> +     if (ret != LZO_E_OK) {
>> +             printf("unlzo: uncompress or overwrite error %d\n", ret);
>> +             return -1;
>
> how about returning ret ?

OK

>> +     sprintf(buf, "%lX", (unsigned long) dst_len);
>> +     setenv("filesize", buf);
>
> setenv_ulong() ?

OK

>> +U_BOOT_CMD(
>> +     unlzo,  5,      1,      do_unlzo,
>> +     "unlzo a memory region",
>> +     "srcaddr srcsize dstaddr [dstsize]"
>> +);
>
> isn't there a way you could "stream" this so you don't need the srcsize ?  or
> does the lzop API not support that ?

The lzo API uses the source size to check if the input is complete at
the end of the file.  I could pass in ~0 optionally and it would stop
at the end.  (It's not as clear how unlzma will handle it.)

So it would look like "unlzo srcaddr dstaddr [dstsize]"

Thanks,
-Joe
diff mbox

Patch

diff --git a/common/Makefile b/common/Makefile
index b8568ba..0793e50 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -156,6 +156,9 @@  COBJS-$(CONFIG_CMD_UNIVERSE) += cmd_universe.o
 ifdef CONFIG_LZMA
 COBJS-$(CONFIG_CMD_UNLZMA) += cmd_unlzma.o
 endif
+ifdef CONFIG_LZO
+COBJS-$(CONFIG_CMD_UNLZO) += cmd_unlzo.o
+endif
 COBJS-$(CONFIG_CMD_UNZIP) += cmd_unzip.o
 ifdef CONFIG_CMD_USB
 COBJS-y += cmd_usb.o
diff --git a/common/cmd_unlzo.c b/common/cmd_unlzo.c
new file mode 100644
index 0000000..f7312c5
--- /dev/null
+++ b/common/cmd_unlzo.c
@@ -0,0 +1,65 @@ 
+/*
+ * (C) Copyright 2011
+ * Joe Hershberger, National Instruments, joe.hershberger@ni.com
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h>
+#include <command.h>
+#include <linux/lzo.h>
+
+int do_unlzo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	unsigned long src, dst;
+	unsigned long src_len = ~0UL;
+	size_t dst_len = ~0UL;
+	int ret;
+	char buf[32];
+
+	switch (argc) {
+	case 5:
+		dst_len = simple_strtoul(argv[4], NULL, 16);
+		/* fall through */
+	case 4:
+		src = simple_strtoul(argv[1], NULL, 16);
+		src_len = simple_strtoul(argv[2], NULL, 16);
+		dst = simple_strtoul(argv[3], NULL, 16);
+		break;
+	default:
+		return CMD_RET_USAGE;
+	}
+
+	ret = lzop_decompress((void *)src, src_len, (void *)dst, &dst_len);
+	if (ret != LZO_E_OK) {
+		printf("unlzo: uncompress or overwrite error %d\n", ret);
+		return -1;
+	}
+	printf("Uncompressed size: %u\n", dst_len);
+	sprintf(buf, "%lX", (unsigned long) dst_len);
+	setenv("filesize", buf);
+
+	return 0;
+}
+
+U_BOOT_CMD(
+	unlzo,	5,	1,	do_unlzo,
+	"unlzo a memory region",
+	"srcaddr srcsize dstaddr [dstsize]"
+);