xref: /openbmc/u-boot/cmd/unzip.c (revision 730d2544)
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <command.h>
10 
11 static int do_unzip(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
12 {
13 	unsigned long src, dst;
14 	unsigned long src_len = ~0UL, dst_len = ~0UL;
15 
16 	switch (argc) {
17 		case 4:
18 			dst_len = simple_strtoul(argv[3], NULL, 16);
19 			/* fall through */
20 		case 3:
21 			src = simple_strtoul(argv[1], NULL, 16);
22 			dst = simple_strtoul(argv[2], NULL, 16);
23 			break;
24 		default:
25 			return CMD_RET_USAGE;
26 	}
27 
28 	if (gunzip((void *) dst, dst_len, (void *) src, &src_len) != 0)
29 		return 1;
30 
31 	printf("Uncompressed size: %ld = 0x%lX\n", src_len, src_len);
32 	setenv_hex("filesize", src_len);
33 
34 	return 0;
35 }
36 
37 U_BOOT_CMD(
38 	unzip,	4,	1,	do_unzip,
39 	"unzip a memory region",
40 	"srcaddr dstaddr [dstsize]"
41 );
42 
43 static int do_gzwrite(cmd_tbl_t *cmdtp, int flag,
44 		      int argc, char * const argv[])
45 {
46 	struct blk_desc *bdev;
47 	int ret;
48 	unsigned char *addr;
49 	unsigned long length;
50 	unsigned long writebuf = 1<<20;
51 	u64 startoffs = 0;
52 	u64 szexpected = 0;
53 
54 	if (argc < 5)
55 		return CMD_RET_USAGE;
56 	ret = blk_get_device_by_str(argv[1], argv[2], &bdev);
57 	if (ret < 0)
58 		return CMD_RET_FAILURE;
59 
60 	addr = (unsigned char *)simple_strtoul(argv[3], NULL, 16);
61 	length = simple_strtoul(argv[4], NULL, 16);
62 
63 	if (5 < argc) {
64 		writebuf = simple_strtoul(argv[5], NULL, 16);
65 		if (6 < argc) {
66 			startoffs = simple_strtoull(argv[6], NULL, 16);
67 			if (7 < argc)
68 				szexpected = simple_strtoull(argv[7],
69 							     NULL, 16);
70 		}
71 	}
72 
73 	ret = gzwrite(addr, length, bdev, writebuf, startoffs, szexpected);
74 
75 	return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
76 }
77 
78 U_BOOT_CMD(
79 	gzwrite, 8, 0, do_gzwrite,
80 	"unzip and write memory to block device",
81 	"<interface> <dev> <addr> length [wbuf=1M [offs=0 [outsize=0]]]\n"
82 	"\twbuf is the size in bytes (hex) of write buffer\n"
83 	"\t\tand should be padded to erase size for SSDs\n"
84 	"\toffs is the output start offset in bytes (hex)\n"
85 	"\toutsize is the size of the expected output (hex bytes)\n"
86 	"\t\tand is required for files with uncompressed lengths\n"
87 	"\t\t4 GiB or larger\n"
88 );
89