source: rtems/testsuites/samples/fileio/init.c @ 0b3fcf5

4.115
Last change on this file since 0b3fcf5 was ab085c9, checked in by Sebastian Huber <sebastian.huber@…>, on 11/18/14 at 09:42:34

samples/fileio: Fix warning

  • Property mode set to 100644
File size: 30.1 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2012.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.org/license/LICENSE.
8 */
9
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
14#define CONFIGURE_INIT
15#include "system.h"
16#include <crypt.h>
17#include <stdio.h>
18#include <string.h>
19#include <unistd.h>
20#include <stdlib.h>
21#include <errno.h>
22#include <rtems.h>
23#include <fcntl.h>
24#include <inttypes.h>
25#include <rtems/error.h>
26#include <rtems/dosfs.h>
27#include <ctype.h>
28#include <rtems/bdpart.h>
29#include <rtems/libcsupport.h>
30#include <rtems/fsmount.h>
31#include <rtems/ramdisk.h>
32#include <rtems/nvdisk.h>
33#include <rtems/nvdisk-sram.h>
34#include <rtems/shell.h>
35
36const char rtems_test_name[] = "FILE I/O";
37
38#if FILEIO_BUILD
39
40/**
41 * Let the IO system allocation the next available major number.
42 */
43#define RTEMS_DRIVER_AUTO_MAJOR (0)
44
45/*
46 * RAM disk driver so you can create a RAM disk from the shell prompt.
47 */
48/**
49 * The RAM Disk configuration.
50 */
51rtems_ramdisk_config rtems_ramdisk_configuration[] =
52{
53  {
54    block_size: 512,
55    block_num:  1024,
56    location:   NULL
57  }
58};
59
60/**
61 * The number of RAM Disk configurations.
62 */
63size_t rtems_ramdisk_configuration_size = 1;
64
65/**
66 * Create the RAM Disk Driver entry.
67 */
68rtems_driver_address_table rtems_ramdisk_io_ops = {
69  initialization_entry: ramdisk_initialize,
70  open_entry:           rtems_blkdev_generic_open,
71  close_entry:          rtems_blkdev_generic_close,
72  read_entry:           rtems_blkdev_generic_read,
73  write_entry:          rtems_blkdev_generic_write,
74  control_entry:        rtems_blkdev_generic_ioctl
75};
76
77/**
78 * The NV Device descriptor. For this test it is just DRAM.
79 */
80rtems_nvdisk_device_desc rtems_nv_heap_device_descriptor[] =
81{
82  {
83    flags:  0,
84    base:   0,
85    size:   (size_t) 1024 * 1024,
86    nv_ops: &rtems_nvdisk_sram_handlers
87  }
88};
89
90/**
91 * The NV Disk configuration.
92 */
93const rtems_nvdisk_config rtems_nvdisk_configuration[] =
94{
95  {
96    block_size:         512,
97    device_count:       1,
98    devices:            &rtems_nv_heap_device_descriptor[0],
99    flags:              0,
100    info_level:         0
101  }
102};
103
104/**
105 * The number of NV Disk configurations.
106 */
107uint32_t rtems_nvdisk_configuration_size = 1;
108
109/**
110 * Create the NV Disk Driver entry.
111 */
112rtems_driver_address_table rtems_nvdisk_io_ops = {
113  initialization_entry: rtems_nvdisk_initialize,
114  open_entry:           rtems_blkdev_generic_open,
115  close_entry:          rtems_blkdev_generic_close,
116  read_entry:           rtems_blkdev_generic_read,
117  write_entry:          rtems_blkdev_generic_write,
118  control_entry:        rtems_blkdev_generic_ioctl
119};
120
121#if 0
122int
123setup_nvdisk (const char* mntpath)
124{
125  rtems_device_major_number major;
126  rtems_status_code         sc;
127
128  /*
129   * For our test we do not have any static RAM or EEPROM devices so
130   * we allocate the memory from the heap.
131   */
132  rtems_nv_heap_device_descriptor[0].base =
133    malloc (rtems_nv_heap_device_descriptor[0].size);
134
135  if (!rtems_nv_heap_device_descriptor[0].base)
136  {
137    printf ("error: no memory for NV disk\n");
138    return 1;
139  }
140 
141  /*
142   * Register the NV Disk driver.
143   */
144  printf ("Register NV Disk Driver: ");
145  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
146                                 &rtems_nvdisk_io_ops,
147                                 &major);
148  if (sc != RTEMS_SUCCESSFUL)
149  {
150    printf ("error: nvdisk driver not initialised: %s\n",
151            rtems_status_text (sc));
152    return 1;
153  }
154 
155  printf ("successful\n");
156
157  return 0;
158}
159#endif
160
161/*
162 * Table of FAT file systems that will be mounted
163 * with the "fsmount" function.
164 * See cpukit/libmisc/fsmount for definition of fields
165 */
166fstab_t fs_table[] = {
167  {
168    "/dev/hda1","/mnt/hda1", "dosfs",
169    RTEMS_FILESYSTEM_READ_WRITE,
170    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
171    0
172  },
173  {
174    "/dev/hda2","/mnt/hda2", "dosfs",
175    RTEMS_FILESYSTEM_READ_WRITE,
176    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
177    0
178  },
179  {
180    "/dev/hda3","/mnt/hda3", "dosfs",
181    RTEMS_FILESYSTEM_READ_WRITE,
182    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
183    0
184  },
185  {
186    "/dev/hda4","/mnt/hda4", "dosfs",
187    RTEMS_FILESYSTEM_READ_WRITE,
188    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
189    0
190  },
191  {
192    "/dev/hdc1","/mnt/hdc1", "dosfs",
193    RTEMS_FILESYSTEM_READ_WRITE,
194    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
195    0
196  },
197  {
198    "/dev/hdc2","/mnt/hdc2", "dosfs",
199    RTEMS_FILESYSTEM_READ_WRITE,
200    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
201    0
202  },
203  {
204    "/dev/hdc3","/mnt/hdc3", "dosfs",
205    RTEMS_FILESYSTEM_READ_WRITE,
206    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
207    0
208  },
209  {
210    "/dev/hdc4","/mnt/hdc4", "dosfs",
211    RTEMS_FILESYSTEM_READ_WRITE,
212    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
213    0
214  }
215};
216
217#ifndef MIN
218#define MIN(a,b) (((a) > (b)) ? (b) : (a))
219#endif
220
221#define USE_SHELL
222
223#ifdef USE_SHELL
224
225static int
226shell_nvdisk_trace (int argc, char* argv[])
227{
228  const char* driver;
229  int         level;
230
231  if (argc != 3)
232  {
233    printf ("error: invalid number of options\n");
234    return 1;
235  }
236
237  driver = argv[1];
238  level  = strtoul (argv[2], 0, 0);
239 
240  int fd = open (driver, O_WRONLY, 0);
241  if (fd < 0)
242  {
243    printf ("error: driver open failed: %s\n", strerror (errno));
244    return 1;
245  }
246 
247  if (ioctl (fd, RTEMS_NVDISK_IOCTL_INFO_LEVEL, level) < 0)
248  {
249    printf ("error: driver set level failed: %s\n", strerror (errno));
250    return 1;
251  }
252 
253  close (fd);
254 
255  return 0;
256}
257
258static int
259shell_nvdisk_erase (int argc, char* argv[])
260{
261  const char* driver = NULL;
262  int         arg;
263  int         fd;
264 
265  for (arg = 1; arg < argc; arg++)
266  {
267    if (argv[arg][0] == '-')
268    {
269      printf ("error: invalid option: %s\n", argv[arg]);
270      return 1;
271    }
272    else
273    {
274      if (!driver)
275        driver = argv[arg];
276      else
277      {
278        printf ("error: only one driver name allowed: %s\n", argv[arg]);
279        return 1;
280      }
281    }
282  }
283 
284  printf ("erase nv disk: %s\n", driver);
285 
286  fd = open (driver, O_WRONLY, 0);
287  if (fd < 0)
288  {
289    printf ("error: nvdisk driver open failed: %s\n", strerror (errno));
290    return 1;
291  }
292 
293  if (ioctl (fd, RTEMS_NVDISK_IOCTL_ERASE_DISK) < 0)
294  {
295    printf ("error: nvdisk driver erase failed: %s\n", strerror (errno));
296    return 1;
297  }
298 
299  close (fd);
300 
301  printf ("nvdisk erased successful\n");
302
303  return 0;
304}
305
306static int
307shell_bdbuf_trace (int argc, char* argv[])
308{
309#if RTEMS_BDBUF_TRACE
310  extern bool rtems_bdbuf_tracer;
311  rtems_bdbuf_tracer = !rtems_bdbuf_tracer;
312  printf ("bdbuf trace: %d\n", rtems_bdbuf_tracer);
313#else
314  printf ("bdbuf trace disabled. Rebuild with enabled.\n");
315#endif
316  return 0;
317}
318
319static int
320disk_test_set_block_size (rtems_disk_device *dd, size_t size)
321{
322  return dd->ioctl (dd, RTEMS_BLKIO_SETBLKSIZE, &size);
323}
324
325static int
326disk_test_write_blocks (dev_t dev, int start, int count, size_t size)
327{
328  int                 block;
329  uint32_t*           ip;
330  uint32_t            value = 0;
331  int                 i;
332  rtems_bdbuf_buffer* bd;
333  rtems_status_code   sc;
334  int                 rv = 0;
335  rtems_disk_device* dd;
336 
337  dd = rtems_disk_obtain (dev);
338  if (!dd)
339  {
340    printf ("error: cannot obtain disk\n");
341    rv = 1;
342  }
343 
344  if (rv == 0 && disk_test_set_block_size (dd, size) < 0)
345  {
346    printf ("error: set block size failed: %s\n", strerror (errno));
347    rv = 1;
348  }
349
350  for (block = start; rv == 0 && block < (start + count); block++)
351  {
352    sc = rtems_bdbuf_read (dd, block, &bd);
353    if (sc == RTEMS_SUCCESSFUL)
354    {
355      ip = (uint32_t*) bd->buffer;
356      for (i = 0; i < (size / sizeof (uint32_t)); i++, ip++, value++)
357        *ip = (size << 16) | value;
358
359      sc = rtems_bdbuf_release_modified (bd);
360      if (sc != RTEMS_SUCCESSFUL)
361      {
362        printf ("error: release block %d bd failed: %s\n",
363                block, rtems_status_text (sc));
364        rv = 1;
365      }
366    }
367    else
368    {
369      printf ("error: get block %d bd failed: %s\n",
370              block, rtems_status_text (sc));
371      rv = 1;
372    }
373  }
374
375  rtems_disk_release (dd);
376
377  return rv;
378}
379
380static int
381disk_test_block_sizes (int argc, char *argv[])
382{
383  struct stat st;
384  char*       name;
385  int         start;
386  int         count;
387  int         size;
388 
389  if (argc != (4 + 1))
390  {
391    printf ("error: need to supply a device path, start, block and size\n");
392    return 1;
393  }
394
395  name = argv[1];
396 
397  if (stat (name, &st) < 0)
398  {
399    printf ("error: stat '%s' failed: %s\n", name, strerror (errno));
400    return 1;
401  }
402
403  start = strtoul (argv[2], 0, 0);
404  count = strtoul (argv[3], 0, 0);
405  size  = strtoul (argv[4], 0, 0);
406 
407  return disk_test_write_blocks (st.st_rdev, start, count, size);
408}
409
410static size_t
411parse_size_arg (const char* arg)
412{
413  size_t size;
414  size_t scalar = 1;
415 
416  size = strtoul (arg, 0, 0);
417  switch (arg[strlen (arg) - 1])
418  {
419    case 'M':
420      scalar = (size_t) 1000 * 1024;
421      break;
422    case 'm':
423      scalar = 1000000;
424      break;
425    case 'K':
426      scalar = 1024;
427      break;
428    case 'k':
429      scalar = 1000;
430      break;
431    default:
432      printf ("error: invalid scalar (M/m/K/k): %c\n", arg[strlen (arg) - 1]);
433      return 0;
434  }
435  return size * scalar;
436 }
437
438static int
439create_ramdisk (int argc, char *argv[])
440{
441  rtems_device_major_number major;
442  rtems_status_code         sc;
443  int                       arg;
444  size_t                    size = 0;
445  size_t                    block_size = 0;
446
447  for (arg = 0; arg < argc; ++arg)
448  {
449    if (argv[arg][0] == '-')
450    {
451      switch (argv[arg][0])
452      {
453        case 's':
454          ++arg;
455          if (arg == argc)
456          {
457            printf ("error: -s needs a size\n");
458            return 1;
459          }
460          size = parse_size_arg (argv[arg]);
461          if (size == 0)
462            return 1;
463          break;
464        case 'b':
465          ++arg;
466          if (arg == argc)
467          {
468            printf ("error: -b needs a size\n");
469            return 1;
470          }
471          block_size = parse_size_arg (argv[arg]);
472          if (size == 0)
473            return 1;
474          break;
475        default:
476          printf ("error: invalid option: %s\n", argv[arg]);
477          return 1;
478      }
479    }
480  }
481
482  if (block_size)
483    rtems_ramdisk_configuration[0].block_size = block_size;
484  if (size)
485    rtems_ramdisk_configuration[0].block_num =
486      size / rtems_ramdisk_configuration[0].block_size;
487   
488  /*
489   * Register the RAM Disk driver.
490   */
491  printf ("Register RAM Disk Driver [blocks=%" PRIu32 \
492          " block-size=%" PRIu32"]:",
493          rtems_ramdisk_configuration[0].block_num,
494          rtems_ramdisk_configuration[0].block_size);
495 
496  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
497                                 &rtems_ramdisk_io_ops,
498                                 &major);
499  if (sc != RTEMS_SUCCESSFUL)
500  {
501    printf ("error: ramdisk driver not initialised: %s\n",
502            rtems_status_text (sc));
503    return 1;
504  }
505 
506  printf ("successful\n");
507
508  return 0;
509}
510
511static int
512create_nvdisk (int argc, char *argv[])
513{
514  rtems_device_major_number major;
515  rtems_status_code         sc;
516  int                       arg;
517  size_t                    size = 0;
518#if ADD_WHEN_NVDISK_HAS_CHANGED
519  size_t                    block_size = 0;
520#endif
521 
522  for (arg = 0; arg < argc; ++arg)
523  {
524    if (argv[arg][0] == '-')
525    {
526      switch (argv[arg][0])
527      {
528        case 's':
529          ++arg;
530          if (arg == argc)
531          {
532            printf ("error: -s needs a size\n");
533            return 1;
534          }
535          size = parse_size_arg (argv[arg]);
536          if (size == 0)
537            return 1;
538          break;
539#if ADD_WHEN_NVDISK_HAS_CHANGED
540        case 'b':
541          ++arg;
542          if (arg == argc)
543          {
544            printf ("error: -b needs a size\n");
545            return 1;
546          }
547          block_size = parse_size_arg (argv[arg]);
548          if (size == 0)
549            return 1;
550          break;
551#endif
552        default:
553          printf ("error: invalid option: %s\n", argv[arg]);
554          return 1;
555      }
556    }
557  }
558
559#if ADD_WHEN_NVDISK_HAS_CHANGED
560  if (block_size)
561    rtems_nvdisk_configuration[0].block_size = block_size;
562#endif
563  if (size)
564    rtems_nv_heap_device_descriptor[0].size = size;
565   
566  /*
567   * For our test we do not have any static RAM or EEPROM devices so
568   * we allocate the memory from the heap.
569   */
570  rtems_nv_heap_device_descriptor[0].base =
571    malloc (rtems_nv_heap_device_descriptor[0].size);
572
573  if (!rtems_nv_heap_device_descriptor[0].base)
574  {
575    printf ("error: no memory for NV disk\n");
576    return 1;
577  }
578 
579  /*
580   * Register the RAM Disk driver.
581   */
582  printf ("Register NV Disk Driver [size=%" PRIu32 \
583          " block-size=%" PRIu32"]:",
584          rtems_nv_heap_device_descriptor[0].size,
585          rtems_nvdisk_configuration[0].block_size);
586 
587  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
588                                 &rtems_nvdisk_io_ops,
589                                 &major);
590  if (sc != RTEMS_SUCCESSFUL)
591  {
592    printf ("error: nvdisk driver not initialised: %s\n",
593            rtems_status_text (sc));
594    return 1;
595  }
596 
597  printf ("successful\n");
598
599  return 0;
600}
601
602static void writeFile(
603  const char *name,
604  mode_t      mode,
605  const char *contents
606)
607{
608  int sc;
609  sc = setuid(0);
610  if ( sc ) {
611    printf( "setuid failed: %s: %s\n", name, strerror(errno) );
612  }
613
614  rtems_shell_write_file( name, contents );
615
616  sc = chmod ( name, mode );
617  if ( sc ) {
618    printf( "chmod %s: %s\n", name, strerror(errno) );
619  }
620}
621
622#define writeScript( _name, _contents ) \
623        writeFile( _name, 0777, _contents )
624
625static void fileio_start_shell(void)
626{
627  int sc;
628
629  sc = mkdir("/scripts", 0777);
630  if ( sc ) {
631    printf( "mkdir /scripts: %s:\n", strerror(errno) );
632  }
633
634  sc = mkdir("/etc", 0777);
635  if ( sc ) {
636    printf( "mkdir /etc: %s:\n", strerror(errno) );
637  }
638
639  printf(
640    "Creating /etc/passwd and group with three useable accounts\n"
641    "root/pwd , test/pwd, rtems/NO PASSWORD"
642  );
643
644  writeFile(
645    "/etc/passwd",
646    0644,
647    "root:$6$$FuPOhnllx6lhW2qqlnmWvZQLJ8Thr/09I7ESTdb9VbnTOn5.65"
648      "/Vh2Mqa6FoKXwT0nHS/O7F0KfrDc6Svb/sH.:0:0:root::/:/bin/sh\n"
649    "rtems::1:1:RTEMS Application::/:/bin/sh\n"
650    "test:$1$$oPu1Xt2Pw0ngIc7LyDHqu1:2:2:test account::/:/bin/sh\n"
651    "tty:*:3:3:tty owner::/:/bin/false\n"
652  );
653  writeFile(
654    "/etc/group",
655    0644,
656    "root:x:0:root\n"
657    "rtems:x:1:rtems\n"
658    "test:x:2:test\n"
659    "tty:x:3:tty\n"
660  );
661
662  writeScript(
663    "/scripts/js",
664    "#! joel\n"
665    "\n"
666    "date\n"
667    "echo Script successfully ran\n"
668    "date\n"
669    "stackuse\n"
670  );
671
672  writeScript(
673    "/scripts/j1",
674    "#! joel -s 20480 -t JESS\n"
675    "stackuse\n"
676  );
677
678  rtems_shell_write_file(
679    "/scripts/j2",
680    "echo j2 TEST FILE\n"
681    "echo j2   SHOULD BE non-executable AND\n"
682    "echo j2   DOES NOT have the magic first line\n"
683  );
684
685  rtems_shell_add_cmd ("mkrd", "files",
686                       "Create a RAM disk driver", create_ramdisk);
687  rtems_shell_add_cmd ("mknvd", "files",
688                       "Create a NV disk driver", create_nvdisk);
689  rtems_shell_add_cmd ("nverase", "misc",
690                       "nverase driver", shell_nvdisk_erase);
691  rtems_shell_add_cmd ("nvtrace", "misc",
692                       "nvtrace driver level", shell_nvdisk_trace);
693  rtems_shell_add_cmd ("bdbuftrace", "files",
694                       "bdbuf trace toggle", shell_bdbuf_trace);
695  rtems_shell_add_cmd ("td", "files",
696                       "Test disk", disk_test_block_sizes);
697#if RTEMS_RFS_TRACE
698  rtems_shell_add_cmd ("rfs", "files",
699                       "RFS trace",
700                       rtems_rfs_trace_shell_command);
701#endif
702#if RTEMS_RFS_RTEMS_TRACE
703  rtems_shell_add_cmd ("rrfs", "files",
704                       "RTEMS RFS trace",
705                       rtems_rfs_rtems_trace_shell_command);
706#endif
707
708  printf("\n =========================\n");
709  printf(" starting shell\n");
710  printf(" =========================\n");
711  rtems_shell_init(
712    "SHLL",                          /* task_name */
713    RTEMS_MINIMUM_STACK_SIZE * 4,    /* task_stacksize */
714    100,                             /* task_priority */
715    "/dev/foobar",                   /* devname */
716    /* device is currently ignored by the shell if it is not a pty */
717    false,                           /* forever */
718    true,                            /* wait */
719    rtems_shell_login_check          /* login */
720  );
721}
722#endif /* USE_SHELL */
723
724static void fileio_print_free_heap(void)
725{
726  printf("--- unused dynamic memory: %lu bytes ---\n",
727         (unsigned long) malloc_free_space());
728}
729
730
731static void fileio_part_table_initialize(void)
732{
733  char devname[64];
734  rtems_status_code rc;
735
736  printf(" =========================\n");
737  printf(" Initialize partition table\n");
738  printf(" =========================\n");
739  fileio_print_free_heap();
740  printf(" Enter device to initialize ==>");
741  fflush(stdout);
742  fgets(devname,sizeof(devname)-1,stdin);
743  while (devname[strlen(devname)-1] == '\n') {
744    devname[strlen(devname)-1] = '\0';
745  }
746  /*
747   * call function
748   */
749  rc = rtems_bdpart_register_from_disk(devname);
750  printf("result = %d\n",rc);
751  fileio_print_free_heap();
752}
753
754static void fileio_fsmount(void)
755{
756  rtems_status_code rc;
757
758  printf(" =========================\n");
759  printf(" Process fsmount table\n");
760  printf(" =========================\n");
761  fileio_print_free_heap();
762  /*
763   * call function
764   */
765  rc = rtems_fsmount( fs_table,
766                      sizeof(fs_table)/sizeof(fs_table[0]),
767                      NULL);
768  printf("result = %d\n",rc);
769  fileio_print_free_heap();
770}
771
772static void fileio_list_file(void)
773{
774  char fname[1024];
775  char *buf_ptr = NULL;
776  ssize_t   flen = 0;
777  int fd = -1;
778  ssize_t n;
779  size_t buf_size = 100;
780
781  rtems_interval start_tick,curr_tick,ticks_per_sec;
782
783  printf(" =========================\n");
784  printf(" LIST FILE ...            \n");
785  printf(" =========================\n");
786  fileio_print_free_heap();
787  printf(" Enter filename to list ==>");
788  fflush(stdout);
789  fgets(fname,sizeof(fname)-1,stdin);
790  while (fname[strlen(fname)-1] == '\n') {
791    fname[strlen(fname)-1] = '\0';
792  }
793  /*
794   * allocate buffer of given size
795   */
796  if (buf_size > 0) {
797    buf_ptr = malloc(buf_size);
798  }
799
800  if (buf_ptr != NULL) {
801    printf("\n Trying to open file \"%s\" for read\n",fname);
802    fd = open(fname,O_RDONLY);
803    if (fd < 0) {
804      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
805    }
806  }
807
808  if (fd >= 0) {
809    start_tick = rtems_clock_get_ticks_since_boot();
810    do {
811      n = read(fd,buf_ptr,buf_size);
812      if (n > 0) {
813        write(1,buf_ptr,(size_t) n);
814        flen += n;
815      }
816    } while (n > 0);
817
818    curr_tick = rtems_clock_get_ticks_since_boot();
819
820    printf("\n ******** End of file reached, flen = %zd\n",flen);
821    close(fd);
822
823    ticks_per_sec = rtems_clock_get_ticks_per_second();
824    printf("time elapsed for read:  %g seconds\n",
825           ((double)curr_tick-start_tick)/ticks_per_sec);
826  }
827  /*
828   * free buffer
829   */
830  if (buf_ptr != NULL) {
831    free(buf_ptr);
832  }
833  fileio_print_free_heap();
834}
835
836/*
837 * convert a size string (like 34K or 12M) to actual byte count
838 */
839static bool fileio_str2size(const char *str,uint32_t   *res_ptr)
840{
841  bool failed = false;
842  unsigned long size;
843  unsigned char suffix = ' ';
844
845  if (1 > sscanf(str,"%lu%c",&size,&suffix)) {
846    failed = true;
847  }
848  else if (toupper((int)suffix) == 'K') {
849    size *= 1024;
850  }
851  else if (toupper((int)suffix) == 'M') {
852    size *= 1024UL*1024UL;
853  }
854  else if (isalpha((int)suffix)) {
855    failed = true;
856  }
857
858  if (!failed) {
859    *res_ptr = size;
860  }
861  return failed;
862}
863
864static void fileio_write_file(void)
865{
866  char fname[1024];
867  char tmp_str[32];
868  uint32_t   file_size = 0;
869  uint32_t   buf_size  = 0;
870  size_t curr_pos,bytes_to_copy;
871  int fd = -1;
872  ssize_t n;
873  rtems_interval start_tick,curr_tick,ticks_per_sec;
874  char *bufptr = NULL;
875  bool failed = false;
876  static const char write_test_string[] =
877    "The quick brown fox jumps over the lazy dog\n";
878  static const char write_block_string[] =
879    "\n----- end of write buffer ------\n";
880
881  printf(" =========================\n");
882  printf(" WRITE FILE ...           \n");
883  printf(" =========================\n");
884  fileio_print_free_heap();
885  /*
886   * get number of ticks per second
887   */
888  ticks_per_sec = rtems_clock_get_ticks_per_second();
889
890  /*
891   * get path to file to write
892   */
893  if (!failed) {
894    printf("Enter path/filename ==>");
895    fflush(stdout);
896    fgets(fname,sizeof(fname)-1,stdin);
897    while (fname[strlen(fname)-1] == '\n') {
898      fname[strlen(fname)-1] = '\0';
899    }
900    if (0 == strlen(fname)) {
901      printf("*** no filename entered, aborted\n");
902      failed = true;
903    }
904  }
905  /*
906   * get total file size to write
907   */
908  if (!failed) {
909    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
910           "Enter filesize to write ==>");
911    fflush(stdout);
912    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
913    failed = fileio_str2size(tmp_str,&file_size);
914    if (failed) {
915      printf("*** illegal file size, aborted\n");
916    }
917  }
918  /*
919   * get block size to write
920   */
921  if (!failed) {
922    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
923           "Enter block size to use for write calls ==>");
924    fflush(stdout);
925    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
926    failed = fileio_str2size(tmp_str,&buf_size);
927    if (failed) {
928      printf("*** illegal block size, aborted\n");
929    }
930  }
931
932  /*
933   * allocate buffer
934   */
935  if (!failed) {
936    printf("... allocating %lu bytes of buffer for write data\n",
937           (unsigned long)buf_size);
938    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
939    if (bufptr == NULL) {
940      printf("*** malloc failed, aborted\n");
941      failed = true;
942    }
943  }
944  /*
945   * fill buffer with test pattern
946   */
947  if (!failed) {
948    printf("... filling buffer with write data\n");
949    curr_pos = 0;
950    /*
951     * fill buffer with test string
952     */
953    while (curr_pos < buf_size) {
954      bytes_to_copy = MIN(buf_size-curr_pos,
955                          sizeof(write_test_string)-1);
956      memcpy(bufptr+curr_pos,write_test_string,bytes_to_copy);
957      curr_pos += bytes_to_copy;
958    }
959    /*
960     * put "end" mark at end of buffer
961     */
962    bytes_to_copy = sizeof(write_block_string)-1;
963    if (buf_size >= bytes_to_copy) {
964      memcpy(bufptr+buf_size-bytes_to_copy,
965             write_block_string,
966             bytes_to_copy);
967    }
968  }
969  /*
970   * create file
971   */
972  if (!failed) {
973    printf("... creating file \"%s\"\n",fname);
974    fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,S_IREAD|S_IWRITE);
975    if (fd < 0) {
976      printf("*** file create failed, errno = %d(%s)\n",errno,strerror(errno));
977      failed = true;
978    }
979  }
980  /*
981   * write file
982   */
983  if (!failed) {
984    printf("... writing to file\n");
985    start_tick = rtems_clock_get_ticks_since_boot();
986    curr_pos = 0;
987    do {
988      bytes_to_copy = buf_size;
989      do {
990        n = write(fd,
991          bufptr + (buf_size-bytes_to_copy),
992                  MIN(bytes_to_copy,file_size-curr_pos));
993        if (n > 0) {
994          bytes_to_copy -= (size_t) n;
995          curr_pos      += (size_t) n;
996        }
997      } while ((bytes_to_copy > 0)  && (n > 0));
998    } while ((file_size > curr_pos) && (n > 0));
999    curr_tick = rtems_clock_get_ticks_since_boot();
1000    if (n < 0) {
1001      failed = true;
1002      printf("*** file write failed, "
1003             "%lu bytes written, "
1004             "errno = %d(%s)\n",
1005             (unsigned long)curr_pos,errno,strerror(errno));
1006    }
1007    else {
1008      printf("time elapsed for write:  %g seconds\n",
1009             ((double)curr_tick-start_tick)/ticks_per_sec);
1010      printf("write data rate: %g KBytes/second\n",
1011             (((double)file_size) / 1024.0 /
1012              (((double)curr_tick-start_tick)/ticks_per_sec)));
1013    }
1014  }
1015  if (fd >= 0) {
1016    printf("... closing file\n");
1017    close(fd);
1018  }
1019  if (bufptr != NULL) {
1020    printf("... deallocating buffer\n");
1021    free(bufptr);
1022    bufptr = NULL;
1023  }
1024  printf("\n ******** End of file write\n");
1025  fileio_print_free_heap();
1026}
1027
1028static void fileio_read_file(void)
1029{
1030  char fname[1024];
1031  char tmp_str[32];
1032  uint32_t   buf_size  = 0;
1033  size_t curr_pos;
1034  int fd = -1;
1035  ssize_t n;
1036  rtems_interval start_tick,curr_tick,ticks_per_sec;
1037  char *bufptr = NULL;
1038  bool failed = false;
1039
1040  printf(" =========================\n");
1041  printf(" READ FILE ...            \n");
1042  printf(" =========================\n");
1043  fileio_print_free_heap();
1044  /*
1045   * get number of ticks per second
1046   */
1047  ticks_per_sec = rtems_clock_get_ticks_per_second();
1048
1049  /*
1050   * get path to file to read
1051   */
1052  if (!failed) {
1053    printf("Enter path/filename ==>");
1054    fflush(stdout);
1055    fgets(fname,sizeof(fname)-1,stdin);
1056    while (fname[strlen(fname)-1] == '\n') {
1057      fname[strlen(fname)-1] = '\0';
1058    }
1059    if (0 == strlen(fname)) {
1060      printf("*** no filename entered, aborted\n");
1061      failed = true;
1062    }
1063  }
1064  /*
1065   * get block size to read
1066   */
1067  if (!failed) {
1068    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
1069           "Enter block size to use for read calls ==>");
1070    fflush(stdout);
1071    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
1072    failed = fileio_str2size(tmp_str,&buf_size);
1073    if (failed) {
1074      printf("*** illegal block size, aborted\n");
1075    }
1076  }
1077
1078  /*
1079   * allocate buffer
1080   */
1081  if (!failed) {
1082    printf("... allocating %lu bytes of buffer for write data\n",
1083           (unsigned long)buf_size);
1084    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
1085    if (bufptr == NULL) {
1086      printf("*** malloc failed, aborted\n");
1087      failed = true;
1088    }
1089  }
1090  /*
1091   * open file
1092   */
1093  if (!failed) {
1094    printf("... opening file \"%s\"\n",fname);
1095    fd = open(fname,O_RDONLY);
1096    if (fd < 0) {
1097      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
1098      failed = true;
1099    }
1100  }
1101  /*
1102   * read file
1103   */
1104  if (!failed) {
1105    printf("... reading from file\n");
1106    start_tick = rtems_clock_get_ticks_since_boot();
1107    curr_pos = 0;
1108    do {
1109      n = read(fd,
1110               bufptr,
1111               buf_size);
1112      if (n > 0) {
1113        curr_pos      += (size_t) n;
1114      }
1115    } while (n > 0);
1116    curr_tick = rtems_clock_get_ticks_since_boot();
1117    if (n < 0) {
1118      failed = true;
1119      printf("*** file read failed, "
1120             "%lu bytes read, "
1121             "errno = %d(%s)\n",
1122             (unsigned long)curr_pos,errno,strerror(errno));
1123    }
1124    else {
1125      printf("%lu bytes read\n",
1126             (unsigned long)curr_pos);
1127      printf("time elapsed for read:  %g seconds\n",
1128             ((double)curr_tick-start_tick)/ticks_per_sec);
1129      printf("read data rate: %g KBytes/second\n",
1130             (((double)curr_pos) / 1024.0 /
1131              (((double)curr_tick-start_tick)/ticks_per_sec)));
1132    }
1133  }
1134  if (fd >= 0) {
1135    printf("... closing file\n");
1136    close(fd);
1137  }
1138  if (bufptr != NULL) {
1139    printf("... deallocating buffer\n");
1140    free(bufptr);
1141    bufptr = NULL;
1142  }
1143  printf("\n ******** End of file read\n");
1144  fileio_print_free_heap();
1145
1146}
1147
1148static void fileio_menu (void)
1149{
1150  char inbuf[10];
1151
1152  /*
1153   * Wait for characters from console terminal
1154   */
1155  for (;;) {
1156    printf(" =========================\n");
1157    printf(" RTEMS FILE I/O Test Menu \n");
1158    printf(" =========================\n");
1159    printf("   p -> part_table_initialize\n");
1160    printf("   f -> mount all disks in fs_table\n");
1161    printf("   l -> list  file\n");
1162    printf("   r -> read  file\n");
1163    printf("   w -> write file\n");
1164#ifdef USE_SHELL
1165    printf("   s -> start shell\n");
1166#endif
1167    printf("   Enter your selection ==>");
1168    fflush(stdout);
1169
1170    inbuf[0] = '\0';
1171    fgets(inbuf,sizeof(inbuf),stdin);
1172    switch (inbuf[0]) {
1173    case 'l':
1174      fileio_list_file ();
1175      break;
1176    case 'r':
1177      fileio_read_file ();
1178      break;
1179    case 'w':
1180      fileio_write_file ();
1181      break;
1182    case 'p':
1183      fileio_part_table_initialize ();
1184      break;
1185    case 'f':
1186      fileio_fsmount ();
1187      break;
1188#ifdef USE_SHELL
1189    case 's':
1190      fileio_start_shell ();
1191      break;
1192#endif
1193    default:
1194      printf("Selection `%c` not implemented\n",inbuf[0]);
1195      break;
1196    }
1197
1198  }
1199  exit (0);
1200}
1201
1202/*
1203 * RTEMS File Menu Task
1204 */
1205static rtems_task
1206fileio_task (rtems_task_argument ignored)
1207{
1208  fileio_menu();
1209}
1210
1211static void
1212notification (int fd, int seconds_remaining, void *arg)
1213{
1214  printf(
1215    "Press any key to start file I/O sample (%is remaining)\n",
1216    seconds_remaining
1217  );
1218}
1219
1220/*
1221 * RTEMS Startup Task
1222 */
1223rtems_task
1224Init (rtems_task_argument ignored)
1225{
1226  rtems_name Task_name;
1227  rtems_id   Task_id;
1228  rtems_status_code status;
1229
1230  TEST_BEGIN();
1231
1232  crypt_add_format(&crypt_md5_format);
1233  crypt_add_format(&crypt_sha512_format);
1234
1235  status = rtems_shell_wait_for_input(
1236    STDIN_FILENO,
1237    20,
1238    notification,
1239    NULL
1240  );
1241  if (status == RTEMS_SUCCESSFUL) {
1242    Task_name = rtems_build_name('F','M','N','U');
1243
1244    status = rtems_task_create(
1245      Task_name, 1, RTEMS_MINIMUM_STACK_SIZE * 2,
1246      RTEMS_DEFAULT_MODES ,
1247      RTEMS_FLOATING_POINT | RTEMS_DEFAULT_ATTRIBUTES, &Task_id
1248    );
1249    directive_failed( status, "create" );
1250
1251    status = rtems_task_start( Task_id, fileio_task, 1 );
1252    directive_failed( status, "start" );
1253
1254    status = rtems_task_delete( RTEMS_SELF );
1255    directive_failed( status, "delete" );
1256  } else {
1257    TEST_END();
1258
1259    rtems_test_exit( 0 );
1260  }
1261}
1262
1263#if defined(USE_SHELL)
1264/*
1265 *  RTEMS Shell Configuration -- Add a command and an alias for it
1266 */
1267
1268static int main_usercmd(int argc, char **argv)
1269{
1270  int i;
1271  printf( "UserCommand: argc=%d\n", argc );
1272  for (i=0 ; i<argc ; i++ )
1273    printf( "argv[%d]= %s\n", i, argv[i] );
1274  return 0;
1275}
1276
1277static rtems_shell_cmd_t Shell_USERCMD_Command = {
1278  "usercmd",                                       /* name */
1279  "usercmd n1 [n2 [n3...]]     # echo arguments",  /* usage */
1280  "user",                                          /* topic */
1281  main_usercmd,                                    /* command */
1282  NULL,                                            /* alias */
1283  NULL                                             /* next */
1284};
1285
1286static rtems_shell_alias_t Shell_USERECHO_Alias = {
1287  "usercmd",                 /* command */
1288  "userecho"                 /* alias */
1289};
1290
1291
1292#define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command
1293#define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
1294#define CONFIGURE_SHELL_COMMANDS_INIT
1295#define CONFIGURE_SHELL_COMMANDS_ALL
1296#define CONFIGURE_SHELL_MOUNT_MSDOS
1297#define CONFIGURE_SHELL_MOUNT_RFS
1298#define CONFIGURE_SHELL_DEBUGRFS
1299
1300#include <rtems/shellconfig.h>
1301#endif
1302
1303#else
1304/*
1305 * RTEMS Startup Task
1306 */
1307rtems_task
1308Init (rtems_task_argument ignored)
1309{
1310  puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
1311  puts( "\n\n*** NOT ENOUGH MEMORY TO BUILD AND RUN ***" );
1312}
1313#endif
Note: See TracBrowser for help on using the repository browser.