Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 8 | #include <env.h> |
Simon Glass | 1a974af | 2019-08-01 09:46:36 -0600 | [diff] [blame] | 9 | #include <gzip.h> |
Simon Glass | 90392d9 | 2021-11-19 13:23:52 -0700 | [diff] [blame] | 10 | #include <mapmem.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <part.h> |
Tom Rini | 5c1444f | 2024-04-27 08:10:59 -0600 | [diff] [blame] | 12 | #include <vsprintf.h> |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 13 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | static int do_unzip(struct cmd_tbl *cmdtp, int flag, int argc, |
| 15 | char *const argv[]) |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 16 | { |
| 17 | unsigned long src, dst; |
| 18 | unsigned long src_len = ~0UL, dst_len = ~0UL; |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 19 | |
| 20 | switch (argc) { |
| 21 | case 4: |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 22 | dst_len = hextoul(argv[3], NULL); |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 23 | /* fall through */ |
| 24 | case 3: |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 25 | src = hextoul(argv[1], NULL); |
| 26 | dst = hextoul(argv[2], NULL); |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 27 | break; |
| 28 | default: |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 29 | return CMD_RET_USAGE; |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Simon Glass | 90392d9 | 2021-11-19 13:23:52 -0700 | [diff] [blame] | 32 | if (gunzip(map_sysmem(dst, dst_len), dst_len, map_sysmem(src, 0), |
| 33 | &src_len) != 0) |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 34 | return 1; |
| 35 | |
Heinrich Schuchardt | 16f2935 | 2019-01-06 12:34:16 +0100 | [diff] [blame] | 36 | printf("Uncompressed size: %lu = 0x%lX\n", src_len, src_len); |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 37 | env_set_hex("filesize", src_len); |
Mike Frysinger | a227490 | 2011-04-02 21:40:19 -0400 | [diff] [blame] | 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | U_BOOT_CMD( |
| 43 | unzip, 4, 1, do_unzip, |
| 44 | "unzip a memory region", |
| 45 | "srcaddr dstaddr [dstsize]" |
| 46 | ); |
Eric Nelson | 20072f8 | 2015-02-15 16:16:07 -0700 | [diff] [blame] | 47 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 48 | static int do_gzwrite(struct cmd_tbl *cmdtp, int flag, |
| 49 | int argc, char *const argv[]) |
Eric Nelson | 20072f8 | 2015-02-15 16:16:07 -0700 | [diff] [blame] | 50 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 51 | struct blk_desc *bdev; |
Eric Nelson | 20072f8 | 2015-02-15 16:16:07 -0700 | [diff] [blame] | 52 | int ret; |
| 53 | unsigned char *addr; |
| 54 | unsigned long length; |
| 55 | unsigned long writebuf = 1<<20; |
| 56 | u64 startoffs = 0; |
| 57 | u64 szexpected = 0; |
| 58 | |
| 59 | if (argc < 5) |
| 60 | return CMD_RET_USAGE; |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 61 | ret = blk_get_device_by_str(argv[1], argv[2], &bdev); |
Eric Nelson | 20072f8 | 2015-02-15 16:16:07 -0700 | [diff] [blame] | 62 | if (ret < 0) |
| 63 | return CMD_RET_FAILURE; |
| 64 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 65 | addr = (unsigned char *)hextoul(argv[3], NULL); |
| 66 | length = hextoul(argv[4], NULL); |
Eric Nelson | 20072f8 | 2015-02-15 16:16:07 -0700 | [diff] [blame] | 67 | |
| 68 | if (5 < argc) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 69 | writebuf = hextoul(argv[5], NULL); |
Eric Nelson | 20072f8 | 2015-02-15 16:16:07 -0700 | [diff] [blame] | 70 | if (6 < argc) { |
| 71 | startoffs = simple_strtoull(argv[6], NULL, 16); |
| 72 | if (7 < argc) |
| 73 | szexpected = simple_strtoull(argv[7], |
| 74 | NULL, 16); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | ret = gzwrite(addr, length, bdev, writebuf, startoffs, szexpected); |
| 79 | |
| 80 | return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | U_BOOT_CMD( |
| 84 | gzwrite, 8, 0, do_gzwrite, |
| 85 | "unzip and write memory to block device", |
| 86 | "<interface> <dev> <addr> length [wbuf=1M [offs=0 [outsize=0]]]\n" |
| 87 | "\twbuf is the size in bytes (hex) of write buffer\n" |
| 88 | "\t\tand should be padded to erase size for SSDs\n" |
| 89 | "\toffs is the output start offset in bytes (hex)\n" |
| 90 | "\toutsize is the size of the expected output (hex bytes)\n" |
| 91 | "\t\tand is required for files with uncompressed lengths\n" |
| 92 | "\t\t4 GiB or larger\n" |
| 93 | ); |