Skip to content
Snippets Groups Projects
nsh_ddcmd.c 15.8 KiB
Newer Older
patacongo's avatar
patacongo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
/****************************************************************************
 * examples/nsh/nsh_ddcmd.c
 *
 *   Copyright (C) 2008 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <debug.h>
#include <errno.h>

#include <nuttx/fs.h>
#include "nsh.h"

#if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_EXAMPLES_NSH_DISABLE_DD)

/****************************************************************************
 * Definitions
 ****************************************************************************/

/* If no sector size is specified wity BS=, then the following default value
 * is used.
 */

#define DEFAULT_SECTSIZE 512

/* At present, piping of input and output are not support, i.e., both of=
 * and if= arguments are required.
 */

#undef CAN_PIPE_FROM_STD

/* Function pointer calls are only need if block drivers are supported
 * (or, rather, if mount points are supported in the file system)
 */

#ifndef CONFIG_DISABLE_MOUNTPOINT
#  define DD_INFD         ((dd)->inf.fd)
#  define DD_INHANDLE     ((dd)->inf.handle)
#  define DD_OUTFD        ((dd)->outf.fd)
#  define DD_OUTHANDLE    ((dd)->outf.handle)
#  define DD_READ(dd)     ((dd)->infread(dd))
#  define DD_WRITE(dd)    ((dd)->outfwrite(dd))
#  define DD_INCLOSE(dd)  ((dd)->infclose(dd))
#  define DD_OUTCLOSE(dd) ((dd)->outfclose(dd))
#else
#  define DD_INFD         ((dd)->infd)
#  undef  DD_INHANDLE
#  define DD_OUTFD        ((dd)->outfd)
#  undef  DD_OUTHANDLE
#  define DD_READ(dd)     dd_readch(dd)
#  define DD_WRITE(dd)    dd_writech(dd)
#  define DD_INCLOSE(dd)  dd_infclosech(dd)
#  define DD_OUTCLOSE(dd) dd_outfclosech(dd)
#endif

/****************************************************************************
 * Private Types
 ****************************************************************************/

struct dd_s
{
  FAR struct nsh_vtbl_s *vtbl;

#ifndef CONFIG_DISABLE_MOUNTPOINT
  union
  {
    FAR void *handle;  /* BCH lib handle for block device*/
    int fd;            /* File descriptor of the character device */
  } inf;
#else
  int infd;            /* File descriptor of the input device */ 
#endif

#ifndef CONFIG_DISABLE_MOUNTPOINT
  union
  {
    FAR void *handle;  /* BCH lib handle for block device*/
    int fd;            /* File descriptor of the character device */
  } outf;
#else
  int outfd;          /* File descriptor of the output device */ 
#endif

  uint32  nsectors;    /* Number of sectors to transfer */
  uint32  sector;      /* The current sector number */
  uint32  skip;        /* The number of sectors skipped on input */
  boolean eof;         /* TRUE:  The of the input or output file has been hit */
  uint16  sectsize;    /* Size of one sector */
  uint16  nbytes;      /* Number of valid bytes in the buffer */
  ubyte  *buffer;      /* Buffer of data to write to the output file */

  /* Function pointers to handle differences between block and character devices */

#ifndef CONFIG_DISABLE_MOUNTPOINT
  int  (*infread)(struct dd_s *dd);
  void (*infclose)(struct dd_s *dd);
  int  (*outfwrite)(struct dd_s *dd);
  void (*outfclose)(struct dd_s *dd);
#endif
};

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

static const char g_dd[] = "dd";

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: dd_outfcloseblk
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static void dd_outfcloseblk(struct dd_s *dd)
{
  (void)bchlib_teardown(DD_OUTHANDLE);
}
#endif


/****************************************************************************
 * Name: dd_outfclosech
 ****************************************************************************/

static void dd_outfclosech(struct dd_s *dd)
{
  (void)close(DD_OUTFD);
}

/****************************************************************************
 * Name: dd_infcloseblk
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static void dd_infcloseblk(struct dd_s *dd)
{
  (void)bchlib_teardown(DD_INHANDLE);
}
#endif

/****************************************************************************
 * Name: dd_infclosech
 ****************************************************************************/

static void dd_infclosech(struct dd_s *dd)
{
  (void)close(DD_INFD);
}

/****************************************************************************
 * Name: dd_writeblk
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static int dd_writeblk(struct dd_s *dd)
{
  ssize_t nbytes;
  off_t   offset = (dd->sector - dd->skip) * dd->sectsize;

  nbytes = bchlib_write(DD_OUTHANDLE, (char*)dd->buffer, offset, dd->sectsize);
  if (nbytes < 0)
    {
      nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "bshlib_write", NSH_ERRNO);
      return ERROR;
    }

  return OK;
}
#endif

/****************************************************************************
 * Name: dd_writech
 ****************************************************************************/

static int dd_writech(struct dd_s *dd)
{
  ubyte *buffer = dd->buffer;
  uint16 written ;
  ssize_t nbytes;

  /* Is the out buffer full (or is this the last one)? */

  written = 0;
  do
    {
      nbytes = write(DD_OUTFD, buffer, dd->sectsize - written);
      if (nbytes < 0)
        {
           nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "write", NSH_ERRNO);
           return ERROR;
        }

      written += nbytes;
      buffer  += nbytes;
    }
  while (written < dd->sectsize);

  return OK;
}

/****************************************************************************
 * Name: dd_readblk
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static int dd_readblk(struct dd_s *dd)
{
  ssize_t nbytes;
  off_t   offset = dd->sector * dd->sectsize;

  nbytes = bchlib_read(DD_INHANDLE, (char*)dd->buffer, offset, dd->sectsize);
  if (nbytes < 0)
    {
      nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "bshlib_read", NSH_ERRNO);
      return ERROR;
    }

  dd->nbytes = nbytes;
  return OK;
}
#endif

/****************************************************************************
 * Name: dd_readch
 ****************************************************************************/

static int dd_readch(struct dd_s *dd)
{
  ubyte *buffer = dd->buffer;
  ssize_t nbytes;

  dd->nbytes = 0;
  do
    {
      nbytes = read(DD_INFD, buffer, dd->sectsize - dd->nbytes);
      if (nbytes < 0)
        {
           nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "read", NSH_ERRNO);
           return ERROR;
        }

      dd->nbytes += nbytes;
      buffer     += nbytes;
    }
  while (dd->nbytes < dd->sectsize && nbytes > 0);

  dd->eof |= (dd->nbytes == 0);
  return OK;
}

/****************************************************************************
 * Name: dd_infopen
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static int dd_filetype(const char *filename)
{
  struct stat sb;
  int ret;

  /* Get the type of the file */

  ret = stat(filename, &sb);
  if (ret < 0)
    {
      return ERROR;  /* Return -1 on failure */
    }

  return S_ISBLK(sb.st_mode); /* Return TRUE(1) if block, FALSE(0) if char */
}
#endif

/****************************************************************************
 * Name: dd_infopen
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static inline int dd_infopen(const char *name, struct dd_s *dd)
{
  int ret;
  int type;

  /* Get the type of the input file */

  type = dd_filetype(name);
  if (type < 0)
    {
      nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "stat", NSH_ERRNO);
      return type;
    }

  /* Open the input file */

  if (!type)
    {
      DD_INFD = open(name, O_RDONLY);
      if (DD_INFD < 0)
        {
          nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
          return ERROR;
        }

      dd->infread  = dd_readch;  /* Character oriented read */
      dd->infclose = dd_infclosech;
    }
  else
    {
      ret = bchlib_setup(name, TRUE, &DD_INHANDLE);
      if (ret < 0)
        {
          return ERROR;
        }

      dd->infread  = dd_readblk;
      dd->infclose = dd_infcloseblk;
    }
  return OK;
}
#else
static inline int dd_infopen(const char *name, struct dd_s *dd)
{
  DD_INFD = open(name, O_RDONLY);
  if (DD_INFD < 0)
    {
      nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
      return ERROR;
    }
  return OK;
}
#endif

/****************************************************************************
 * Name: dd_outfopen
 ****************************************************************************/

#ifndef CONFIG_DISABLE_MOUNTPOINT
static inline int dd_outfopen(const char *name, struct dd_s *dd)
{
  int type;
  int ret = OK;

  /* Get the type of the output file */

  type = dd_filetype(name);

  /* Open the block driver for input */

  if (type == TRUE)
    {
      ret = bchlib_setup(name, TRUE, &DD_OUTHANDLE);
      if (ret < 0)
        {
          return ERROR;
        }

      dd->outfwrite = dd_writeblk;  /* Block oriented write */
      dd->outfclose = dd_outfcloseblk;
    }

  /* Otherwise, the file is character oriented or does not exist */

  else
    {
      DD_OUTFD = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
      if (DD_OUTFD < 0)
        {
          nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
          return ERROR;
        }

      dd->outfwrite = dd_writech;  /* Character oriented write */
      dd->outfclose = dd_outfclosech;
    }
  return OK;
}
#else
static inline int dd_outfopen(const char *name, struct dd_s *dd)
{
  DD_OUTFD = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644);
  if (DD_OUTFD < 0)
    {
      nsh_output(dd->vtbl, g_fmtcmdfailed, g_dd, "open", NSH_ERRNO);
      return ERROR;
    }
  return OK;
}
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: cmd_dd
 ****************************************************************************/

int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
  struct dd_s dd;
  const char *infile = NULL;
  const char *outfile = NULL;
  int ret;
  int i;

  /* Initialize the dd structure */

  memset(&dd, 0, sizeof(struct dd_s));
  dd.vtbl      = vtbl;              /* For nsh_output */
  dd.sectsize  = DEFAULT_SECTSIZE;  /* Sector size if 'bs=' not provided */
  dd.nsectors  = 0xffffffff;        /* MAX_UINT32 */

  /* If no IF= option is provided on the command line, then read
   * from stdin.
   */

#ifdef CAN_PIPE_FROM_STD
  DD_INFD      = 0;       /* stdin */
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_DD
  dd.infread   = readch;  /* Character oriented read */
  dd.infclose  = noclose; /* Don't close stdin */
#endif
#endif
  /* If no OF= option is provided on the command line, then write
   * to stdout.
   */

#ifdef CAN_PIPE_FROM_STD
  DD_OUTDF     = 1;       /* stdout */
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_DD
  dd.outfwrite = writech; /* Character oriented write */
  dd.outfclose = noclose; /* Don't close stdout */
#endif
#endif

  /* Parse command line parameters */

  for (i = 1; i < argc; i++)
    {
      if (strncmp(argv[i], "if=", 3) == 0)
        {
          infile = &argv[i][3];
        }
      else if (strncmp(argv[i], "of=", 3) == 0)
        {
          outfile = &argv[i][3];
        }
      else if (strncmp(argv[i], "bs=", 3) == 0)
        {
          dd.sectsize = atoi(&argv[i][3]);
        }
      else if (strncmp(argv[i], "count=", 6) == 0)
        {
          dd.nsectors = atoi(&argv[i][6]);
        }
      else if (strncmp(argv[i], "skip=", 5) == 0)
        {
          dd.skip = atoi(&argv[i][5]);
        }
    }

#ifndef CAN_PIPE_FROM_STD
  if (!infile || !outfile)
    {
      nsh_output(vtbl, g_fmtargrequired, g_dd);
      return ERROR;
    }
#endif

  if (dd.skip < 0 || dd.skip > dd.nsectors)
    {
      nsh_output(vtbl, g_fmtarginvalid, g_dd);
      return ERROR;
    }

  /* Allocate the I/O buffer */

  dd.buffer = malloc(dd.sectsize);
  if (!dd.buffer)
    {
      nsh_output(vtbl, g_fmtcmdoutofmemory, g_dd);
      return ERROR;
    }

  /* Open the input file */

  ret = dd_infopen(infile, &dd);
  if (ret < 0)
    {
      return ret;
    }

  /* Open the output file */

  ret = dd_outfopen(outfile, &dd);
  if (ret < 0)
    {
      goto errout_with_inf;
    }

  /* Then perform the data transfer */

  dd.sector = 0;
  while (!dd.eof && dd.nsectors > 0)
    {
      /* Read one sector from from the input */

      ret = DD_READ(&dd);
      if (ret < 0)
        {
          goto errout_with_outf;
        }

      /* 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)
            {
              goto errout_with_outf;
            }

          /* Decrement to show that a sector was written */

          dd.nsectors--;
        }

      /* Increment the sector number */

      dd.sector++;
    }

errout_with_outf:
  DD_INCLOSE(&dd);
errout_with_inf:
  DD_OUTCLOSE(&dd);
  free(dd.buffer);
  return ret;
}

#endif /* CONFIG_NFILE_DESCRIPTORS && !CONFIG_EXAMPLES_NSH_DISABLE_DD */