You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/11/14 12:40:50 UTC
[incubator-nuttx-apps] branch master updated (da30a87b8 -> 14465b85c)
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
from da30a87b8 Fix Error: version_main.c:124:42: error: format specifies type 'long' but the argument has type 'uint32_t' (aka 'unsigned int') [-Werror,-Wformat]
new c2f07a4d5 dd:simplified cmd_dd code steps
new 14465b85c cmd_dd:support dd can do verify
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
nshlib/nsh_command.c | 4 +-
nshlib/nsh_ddcmd.c | 147 +++++++++++++++++++++++++++++++++++++++++++--------
2 files changed, 126 insertions(+), 25 deletions(-)
[incubator-nuttx-apps] 01/02: dd:simplified cmd_dd code steps
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
commit c2f07a4d5afa8fb7b44d379ca65c9f4dca51b0b7
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Tue Nov 8 15:22:11 2022 +0800
dd:simplified cmd_dd code steps
Signed-off-by: anjiahao <an...@xiaomi.com>
---
nshlib/nsh_ddcmd.c | 40 ++++++++++++++++++----------------------
1 file changed, 18 insertions(+), 22 deletions(-)
diff --git a/nshlib/nsh_ddcmd.c b/nshlib/nsh_ddcmd.c
index e77ceaec0..467fa6e00 100644
--- a/nshlib/nsh_ddcmd.c
+++ b/nshlib/nsh_ddcmd.c
@@ -69,7 +69,6 @@ struct dd_s
int infd; /* File descriptor of the input device */
int outfd; /* File descriptor of the output device */
uint32_t nsectors; /* Number of sectors to transfer */
- uint32_t sector; /* The current sector number */
uint32_t skip; /* The number of sectors skipped on input */
bool eof; /* true: The end of the input or output file has been hit */
uint16_t sectsize; /* Size of one sector */
@@ -202,6 +201,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
uint64_t elapsed;
uint64_t total;
#endif
+ uint32_t sector = 0;
int ret = ERROR;
int i;
@@ -303,8 +303,18 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
clock_gettime(CLOCK_MONOTONIC, &ts0);
#endif
- dd.sector = 0;
- while (!dd.eof && dd.nsectors > 0)
+ if (dd.skip)
+ {
+ ret = lseek(dd.infd, dd.skip * dd.sectsize, SEEK_SET);
+ if (ret < -1)
+ {
+ nsh_error(vtbl, g_fmtcmdfailed, g_dd, "lseek", NSH_ERRNO);
+ ret = ERROR;
+ goto errout_with_outf;
+ }
+ }
+
+ while (!dd.eof && sector < dd.nsectors)
{
/* Read one sector from from the input */
@@ -318,31 +328,17 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
if (!dd.eof)
{
- /* Pad with zero if necessary (at the end of file only) */
-
- for (i = dd.nbytes; i < dd.sectsize; i++)
- {
- dd.buffer[i] = 0;
- }
-
/* Write one sector to the output file */
- if (dd.sector >= dd.skip)
+ ret = dd_write(&dd);
+ if (ret < 0)
{
- ret = dd_write(&dd);
- if (ret < 0)
- {
- goto errout_with_outf;
- }
-
- /* Decrement to show that a sector was written */
-
- dd.nsectors--;
+ goto errout_with_outf;
}
/* Increment the sector number */
- dd.sector++;
+ sector++;
}
}
@@ -355,7 +351,7 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
elapsed -= (((uint64_t)ts0.tv_sec * NSEC_PER_SEC) + ts0.tv_nsec);
elapsed /= NSEC_PER_USEC; /* usec */
- total = ((uint64_t)dd.sector * (uint64_t)dd.sectsize);
+ total = ((uint64_t)sector * (uint64_t)dd.sectsize);
nsh_output(vtbl, "%llu bytes copied, %u usec, ",
total, (unsigned int)elapsed);
[incubator-nuttx-apps] 02/02: cmd_dd:support dd can do verify
Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git
commit 14465b85c49678ab0e5ef58aff8d825876841101
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Tue Nov 8 11:53:33 2022 +0800
cmd_dd:support dd can do verify
After writing the file, compare the contents of the two files again
Signed-off-by: anjiahao <an...@xiaomi.com>
---
nshlib/nsh_command.c | 4 +-
nshlib/nsh_ddcmd.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index fa73d6b93..9adf0d058 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -160,9 +160,9 @@ static const struct cmdmap_s g_cmdmap[] =
#endif
#ifndef CONFIG_NSH_DISABLE_DD
- { "dd", cmd_dd, 3, 6,
+ { "dd", cmd_dd, 3, 7,
"if=<infile> of=<outfile> [bs=<sectsize>] [count=<sectors>] "
- "[skip=<sectors>]" },
+ "[skip=<sectors>] [verify]" },
# endif
#if defined(CONFIG_NET) && defined(CONFIG_NET_ROUTE) && !defined(CONFIG_NSH_DISABLE_DELROUTE)
diff --git a/nshlib/nsh_ddcmd.c b/nshlib/nsh_ddcmd.c
index 467fa6e00..3890616e8 100644
--- a/nshlib/nsh_ddcmd.c
+++ b/nshlib/nsh_ddcmd.c
@@ -71,6 +71,7 @@ struct dd_s
uint32_t nsectors; /* Number of sectors to transfer */
uint32_t skip; /* The number of sectors skipped on input */
bool eof; /* true: The end of the input or output file has been hit */
+ bool verify; /* true: Verify infile and outfile correctness */
uint16_t sectsize; /* Size of one sector */
uint16_t nbytes; /* Number of valid bytes in the buffer */
FAR uint8_t *buffer; /* Buffer of data to write to the output file */
@@ -171,7 +172,8 @@ static inline int dd_infopen(FAR const char *name, FAR struct dd_s *dd)
static inline int dd_outfopen(FAR const char *name, FAR struct dd_s *dd)
{
- dd->outfd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ dd->outfd = open(name, (dd->verify ? O_RDWR : O_WRONLY) |
+ O_CREAT | O_TRUNC, 0644);
if (dd->outfd < 0)
{
FAR struct nsh_vtbl_s *vtbl = dd->vtbl;
@@ -182,6 +184,100 @@ static inline int dd_outfopen(FAR const char *name, FAR struct dd_s *dd)
return OK;
}
+static int dd_verify(FAR const char *infile, FAR const char *outfile,
+ FAR struct dd_s *dd)
+{
+ FAR uint8_t *buffer;
+ int sector = 0;
+ int ret = OK;
+
+ ret = lseek(dd->infd, dd->skip ? dd->skip * dd->sectsize : 0, SEEK_SET);
+ if (ret < 0)
+ {
+ nsh_error(dd->vtbl, g_fmtcmdfailed, g_dd, "lseek", NSH_ERRNO);
+ return ret;
+ }
+
+ dd->eof = 0;
+ ret = lseek(dd->outfd, 0, SEEK_SET);
+ if (ret < 0)
+ {
+ nsh_error(dd->vtbl, g_fmtcmdfailed, g_dd, "lseek", NSH_ERRNO);
+ return ret;
+ }
+
+ buffer = malloc(dd->sectsize);
+ if (buffer == NULL)
+ {
+ return ERROR;
+ }
+
+ while (!dd->eof && sector < dd->nsectors)
+ {
+ ret = dd_read(dd);
+ if (ret < 0)
+ {
+ break;
+ }
+
+ ret = read(dd->outfd, buffer, dd->nbytes);
+ if (ret != dd->nbytes)
+ {
+ nsh_error(dd->vtbl, g_fmtcmdfailed, g_dd, "read", NSH_ERRNO);
+ break;
+ }
+
+ if (memcmp(dd->buffer, buffer, dd->nbytes) != 0)
+ {
+ int i;
+
+ nsh_output(dd->vtbl, "infile sector %d", sector);
+ for (i = 0; i < dd->nbytes; i++)
+ {
+ if (i % 16 == 0)
+ {
+ nsh_output(dd->vtbl, "\n");
+ }
+
+ nsh_output(dd->vtbl, "%02x", dd->buffer[i]);
+ if (i + 1 % 2 == 0)
+ {
+ nsh_output(dd->vtbl, " ");
+ }
+ }
+
+ nsh_output(dd->vtbl, "\noutfile sector %d", sector);
+ for (i = 0; i < dd->nbytes; i++)
+ {
+ if (i % 16 == 0)
+ {
+ nsh_output(dd->vtbl, "\n");
+ }
+
+ nsh_output(dd->vtbl, "%02x", dd->buffer[i]);
+ if (i + 1 % 2 == 0)
+ {
+ nsh_output(dd->vtbl, " ");
+ }
+ }
+
+ nsh_output(dd->vtbl, "\n");
+ ret = ERROR;
+ break;
+ }
+
+ sector++;
+ }
+
+ if (ret < 0)
+ {
+ nsh_error(dd->vtbl, g_fmtcmdfailed, g_dd, "dd_verify", ret);
+ }
+
+ free(buffer);
+ return ret;
+}
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -262,6 +358,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], "verify", 6) == 0)
+ {
+ dd.verify = true;
+ }
}
#ifndef CAN_PIPE_FROM_STD
@@ -360,6 +460,11 @@ int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
/ ((double)elapsed / USEC_PER_SEC)));
#endif
+ if (ret == 0 && dd.verify)
+ {
+ ret = dd_verify(infile, outfile, &dd);
+ }
+
errout_with_outf:
close(dd.outfd);