dd:support "seek"

Now the dd command can use the "seek" parameter to adjust how many bytes need to be skipped before being written to the target.

Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
This commit is contained in:
chenrun1 2024-01-11 11:28:49 +08:00 committed by Xiang Xiao
parent 31908b3128
commit bd1cc85ad4
2 changed files with 18 additions and 2 deletions

View File

@ -188,7 +188,7 @@ static const struct cmdmap_s g_cmdmap[] =
#ifndef CONFIG_NSH_DISABLE_DD
CMD_MAP("dd", cmd_dd, 3, 7,
"if=<infile> of=<outfile> [bs=<sectsize>] [count=<sectors>] "
"[skip=<sectors>] [verify]"),
"[skip=<sectors>] [seek=<sectors>] [verify]"),
#endif
#if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE) && !defined(CONFIG_NSH_DISABLE_DELROUTE)

View File

@ -75,6 +75,7 @@ struct dd_s
int outfd; /* File descriptor of the output device */
uint32_t nsectors; /* Number of sectors to transfer */
uint32_t skip; /* The number of sectors skipped on input */
uint32_t seek; /* The number of bytes skipped on output */
bool eof; /* true: The end of the input or output file has been hit */
bool verify; /* true: Verify infile and outfile correctness */
size_t sectsize; /* Size of one sector */
@ -332,6 +333,10 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
dd.skip = atoi(&argv[i][5]);
}
else if (strncmp(argv[i], "seek=", 5) == 0)
{
dd.seek = atoi(&argv[i][5]);
}
else if (strncmp(argv[i], "verify", 6) == 0)
{
dd.verify = true;
@ -382,7 +387,18 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
ret = lseek(dd.infd, dd.skip * dd.sectsize, SEEK_SET);
if (ret < -1)
{
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "lseek", NSH_ERRNO);
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "skip lseek", NSH_ERRNO);
ret = ERROR;
goto errout_with_outf;
}
}
if (dd.seek)
{
ret = lseek(dd.outfd, dd.seek * dd.sectsize, SEEK_SET);
if (ret < -1)
{
nsh_error(vtbl, g_fmtcmdfailed, g_dd, "seek lseek", NSH_ERRNO);
ret = ERROR;
goto errout_with_outf;
}