1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 John Chau <john@harmon.hk>
14 #define BUFSIZE (1 * 1024 * 1024)
15 static int do_clone(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
18 struct blk_desc *srcdesc, *destdesc;
19 int srcbz, destbz, ret;
21 unsigned long wrcnt, rdcnt, requested, srcblk, destblk;
23 const unsigned long buffersize = 1024 * 1024;
28 srcdev = blk_get_device_by_str(argv[1], argv[2], &srcdesc);
29 destdev = blk_get_device_by_str(argv[3], argv[4], &destdesc);
31 printf("Unable to open source device\n");
33 } else if (destdev < 0) {
34 printf("Unable to open destination device\n");
37 requested = simple_strtoul(argv[5], &unit, 10);
38 srcbz = srcdesc->blksz;
39 destbz = destdesc->blksz;
41 if ((srcbz * (buffersize / srcbz) != buffersize) &&
42 (destbz * (buffersize / destbz) != buffersize)) {
43 printf("failed: cannot match device block sizes\n");
47 unsigned long a = srcdesc->lba * srcdesc->blksz;
48 unsigned long b = destdesc->lba * destdesc->blksz;
68 printf("Copying %ld bytes from %s:%s to %s:%s\n",
69 requested, argv[1], argv[2], argv[3], argv[4]);
72 buf = (char *)malloc(BUFSIZE);
76 while (wrcnt < requested) {
77 unsigned long toread = BUFSIZE / srcbz;
78 unsigned long towrite = BUFSIZE / destbz;
79 unsigned long offset = 0;
82 ret = blk_dread(srcdesc, srcblk, toread, buf + offset);
84 printf("Src read error @blk %ld\n", srcblk);
91 offset += ret * srcbz;
96 ret = blk_dwrite(destdesc, destblk, towrite, buf + offset);
98 printf("Dest write error @blk %ld\n", srcblk);
101 wrcnt += ret * destbz;
105 offset += ret * destbz;
111 timer = get_timer(timer);
112 timer = 1000 * timer / CONFIG_SYS_HZ;
113 printf("%ld read\n", rdcnt);
114 printf("%ld written\n", wrcnt);
115 printf("%ldms, %ldkB/s\n", timer, wrcnt / timer);
122 clone, 6, 1, do_clone,
123 "simple storage cloning",
124 "<src interface> <src dev> <dest interface> <dest dev> <size[K/M/G]>\n"
125 "clone storage from 'src dev' on 'src interface' to 'dest dev' on 'dest interface' with maximum 'size' bytes (or 0 for clone to end)"