source: rtems/testsuites/samples/fileio/init.c @ 9db3cc21

5
Last change on this file since 9db3cc21 was af43554, checked in by Sebastian Huber <sebastian.huber@…>, on 10/26/17 at 11:59:11

tests: Remove TEST_INIT

The TEST_EXTERN is a used only by the system.h style tests and they use
CONFIGURE_INIT appropriately.

Update #3170.
Update #3199.

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