source: rtems/testsuites/samples/fileio/init.c @ 4534ba3

4.115
Last change on this file since 4534ba3 was 4534ba3, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/13/11 at 09:36:01

2011-12-13 Ralf Corsépius <ralf.corsepius@…>

  • fileio/init.c: Comment out setup_nvdisk (Unused). Make shell_nvdisk_trace, shell_nvdisk_erase, shell_bdbuf_trace, disk_test_set_block_size, disk_test_write_blocks, disk_test_block_sizes, parse_size_arg, create_ramdisk, create_nvdisk static.
  • nsecs/init.c: Make my_ctime, subtract_em static.
  • Property mode set to 100644
File size: 30.2 KB
Line 
1/*  Init
2 *
3 *  This routine is the initialization task for this test program.
4 *  It is called from init_exec and has the responsibility for creating
5 *  and starting the tasks that make up the test.  If the time of day
6 *  clock is required for the test, it should also be set to a known
7 *  value by this function.
8 *
9 *  Input parameters:  NONE
10 *
11 *  Output parameters:  NONE
12 *
13 *  COPYRIGHT (c) 1989-2011.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.com/license/LICENSE.
19 *
20 *  $Id$
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#define CONFIGURE_INIT
28#include "system.h"
29#include <stdio.h>
30#include <string.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <rtems.h>
35#include <fcntl.h>
36#include <inttypes.h>
37#include <rtems/error.h>
38#include <rtems/dosfs.h>
39#include <ctype.h>
40#include <rtems/bdpart.h>
41#include <rtems/libcsupport.h>
42#include <rtems/fsmount.h>
43#include <rtems/ramdisk.h>
44#include <rtems/nvdisk.h>
45#include <rtems/nvdisk-sram.h>
46#include <rtems/shell.h>
47
48#if FILEIO_BUILD
49
50/**
51 * Let the IO system allocation the next available major number.
52 */
53#define RTEMS_DRIVER_AUTO_MAJOR (0)
54
55/*
56 * RAM disk driver so you can create a RAM disk from the shell prompt.
57 */
58/**
59 * The RAM Disk configuration.
60 */
61rtems_ramdisk_config rtems_ramdisk_configuration[] =
62{
63  {
64    block_size: 512,
65    block_num:  1024,
66    location:   NULL
67  }
68};
69
70/**
71 * The number of RAM Disk configurations.
72 */
73size_t rtems_ramdisk_configuration_size = 1;
74
75/**
76 * Create the RAM Disk Driver entry.
77 */
78rtems_driver_address_table rtems_ramdisk_io_ops = {
79  initialization_entry: ramdisk_initialize,
80  open_entry:           rtems_blkdev_generic_open,
81  close_entry:          rtems_blkdev_generic_close,
82  read_entry:           rtems_blkdev_generic_read,
83  write_entry:          rtems_blkdev_generic_write,
84  control_entry:        rtems_blkdev_generic_ioctl
85};
86
87/**
88 * The NV Device descriptor. For this test it is just DRAM.
89 */
90rtems_nvdisk_device_desc rtems_nv_heap_device_descriptor[] =
91{
92  {
93    flags:  0,
94    base:   0,
95    size:   (size_t) 1024 * 1024,
96    nv_ops: &rtems_nvdisk_sram_handlers
97  }
98};
99
100/**
101 * The NV Disk configuration.
102 */
103const rtems_nvdisk_config rtems_nvdisk_configuration[] =
104{
105  {
106    block_size:         512,
107    device_count:       1,
108    devices:            &rtems_nv_heap_device_descriptor[0],
109    flags:              0,
110    info_level:         0
111  }
112};
113
114/**
115 * The number of NV Disk configurations.
116 */
117uint32_t rtems_nvdisk_configuration_size = 1;
118
119/**
120 * Create the NV Disk Driver entry.
121 */
122rtems_driver_address_table rtems_nvdisk_io_ops = {
123  initialization_entry: rtems_nvdisk_initialize,
124  open_entry:           rtems_blkdev_generic_open,
125  close_entry:          rtems_blkdev_generic_close,
126  read_entry:           rtems_blkdev_generic_read,
127  write_entry:          rtems_blkdev_generic_write,
128  control_entry:        rtems_blkdev_generic_ioctl
129};
130
131#if 0
132int
133setup_nvdisk (const char* mntpath)
134{
135  rtems_device_major_number major;
136  rtems_status_code         sc;
137
138  /*
139   * For our test we do not have any static RAM or EEPROM devices so
140   * we allocate the memory from the heap.
141   */
142  rtems_nv_heap_device_descriptor[0].base =
143    malloc (rtems_nv_heap_device_descriptor[0].size);
144
145  if (!rtems_nv_heap_device_descriptor[0].base)
146  {
147    printf ("error: no memory for NV disk\n");
148    return 1;
149  }
150 
151  /*
152   * Register the NV Disk driver.
153   */
154  printf ("Register NV Disk Driver: ");
155  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
156                                 &rtems_nvdisk_io_ops,
157                                 &major);
158  if (sc != RTEMS_SUCCESSFUL)
159  {
160    printf ("error: nvdisk driver not initialised: %s\n",
161            rtems_status_text (sc));
162    return 1;
163  }
164 
165  printf ("successful\n");
166
167  return 0;
168}
169#endif
170
171/*
172 * Table of FAT file systems that will be mounted
173 * with the "fsmount" function.
174 * See cpukit/libmisc/fsmount for definition of fields
175 */
176fstab_t fs_table[] = {
177  {
178    "/dev/hda1","/mnt/hda1", "dosfs",
179    RTEMS_FILESYSTEM_READ_WRITE,
180    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
181    0
182  },
183  {
184    "/dev/hda2","/mnt/hda2", "dosfs",
185    RTEMS_FILESYSTEM_READ_WRITE,
186    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
187    0
188  },
189  {
190    "/dev/hda3","/mnt/hda3", "dosfs",
191    RTEMS_FILESYSTEM_READ_WRITE,
192    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
193    0
194  },
195  {
196    "/dev/hda4","/mnt/hda4", "dosfs",
197    RTEMS_FILESYSTEM_READ_WRITE,
198    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
199    0
200  },
201  {
202    "/dev/hdc1","/mnt/hdc1", "dosfs",
203    RTEMS_FILESYSTEM_READ_WRITE,
204    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
205    0
206  },
207  {
208    "/dev/hdc2","/mnt/hdc2", "dosfs",
209    RTEMS_FILESYSTEM_READ_WRITE,
210    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
211    0
212  },
213  {
214    "/dev/hdc3","/mnt/hdc3", "dosfs",
215    RTEMS_FILESYSTEM_READ_WRITE,
216    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
217    0
218  },
219  {
220    "/dev/hdc4","/mnt/hdc4", "dosfs",
221    RTEMS_FILESYSTEM_READ_WRITE,
222    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
223    0
224  }
225};
226
227#define MIN(a,b) (((a) > (b)) ? (b) : (a))
228
229#define USE_SHELL
230
231#ifdef USE_SHELL
232
233static int
234shell_nvdisk_trace (int argc, char* argv[])
235{
236  const char* driver;
237  int         level;
238
239  if (argc != 3)
240  {
241    printf ("error: invalid number of options\n");
242    return 1;
243  }
244
245  driver = argv[1];
246  level  = strtoul (argv[2], 0, 0);
247 
248  int fd = open (driver, O_WRONLY, 0);
249  if (fd < 0)
250  {
251    printf ("error: driver open failed: %s\n", strerror (errno));
252    return 1;
253  }
254 
255  if (ioctl (fd, RTEMS_NVDISK_IOCTL_INFO_LEVEL, level) < 0)
256  {
257    printf ("error: driver set level failed: %s\n", strerror (errno));
258    return 1;
259  }
260 
261  close (fd);
262 
263  return 0;
264}
265
266static int
267shell_nvdisk_erase (int argc, char* argv[])
268{
269  const char* driver = NULL;
270  int         arg;
271  int         fd;
272 
273  for (arg = 1; arg < argc; arg++)
274  {
275    if (argv[arg][0] == '-')
276    {
277      printf ("error: invalid option: %s\n", argv[arg]);
278      return 1;
279    }
280    else
281    {
282      if (!driver)
283        driver = argv[arg];
284      else
285      {
286        printf ("error: only one driver name allowed: %s\n", argv[arg]);
287        return 1;
288      }
289    }
290  }
291 
292  printf ("erase nv disk: %s\n", driver);
293 
294  fd = open (driver, O_WRONLY, 0);
295  if (fd < 0)
296  {
297    printf ("error: nvdisk driver open failed: %s\n", strerror (errno));
298    return 1;
299  }
300 
301  if (ioctl (fd, RTEMS_NVDISK_IOCTL_ERASE_DISK) < 0)
302  {
303    printf ("error: nvdisk driver erase failed: %s\n", strerror (errno));
304    return 1;
305  }
306 
307  close (fd);
308 
309  printf ("nvdisk erased successful\n");
310
311  return 0;
312}
313
314static int
315shell_bdbuf_trace (int argc, char* argv[])
316{
317#if RTEMS_BDBUF_TRACE
318  extern bool rtems_bdbuf_tracer;
319  rtems_bdbuf_tracer = !rtems_bdbuf_tracer;
320  printf ("bdbuf trace: %d\n", rtems_bdbuf_tracer);
321#else
322  printf ("bdbuf trace disabled. Rebuild with enabled.\n");
323#endif
324  return 0;
325}
326
327static int
328disk_test_set_block_size (dev_t dev, size_t size)
329{
330  rtems_disk_device* dd;
331  int                rc;
332 
333  dd = rtems_disk_obtain (dev);
334  if (!dd)
335  {
336    printf ("error: cannot obtain disk\n");
337    return 1;
338  }
339 
340  rc = dd->ioctl (dd, RTEMS_BLKIO_SETBLKSIZE, &size);
341
342  rtems_disk_release (dd);
343
344  return rc;
345}
346
347static int
348disk_test_write_blocks (dev_t dev, int start, int count, size_t size)
349{
350  int                 block;
351  uint32_t*           ip;
352  uint32_t            value = 0;
353  int                 i;
354  rtems_bdbuf_buffer* bd;
355  rtems_status_code   sc;
356 
357  if (disk_test_set_block_size (dev, size) < 0)
358  {
359    printf ("error: set block size failed: %s\n", strerror (errno));
360    return 1;
361  }
362
363  for (block = start; block < (start + count); block++)
364  {
365    sc = rtems_bdbuf_read (dev, block, &bd);
366    if (sc != RTEMS_SUCCESSFUL)
367    {
368      printf ("error: get block %d bd failed: %s\n",
369              block, rtems_status_text (sc));
370      return 1;
371    }
372
373    ip = (uint32_t*) bd->buffer;
374    for (i = 0; i < (size / sizeof (uint32_t)); i++, ip++, value++)
375      *ip = (size << 16) | value;
376
377    sc = rtems_bdbuf_release_modified (bd);
378    if (sc != RTEMS_SUCCESSFUL)
379    {
380      printf ("error: release block %d bd failed: %s\n",
381              block, rtems_status_text (sc));
382      return 1;
383    }
384  }
385
386  return 0;
387}
388
389static int
390disk_test_block_sizes (int argc, char *argv[])
391{
392  struct stat st;
393  char*       name;
394  int         start;
395  int         count;
396  int         size;
397 
398  if (argc != (4 + 1))
399  {
400    printf ("error: need to supply a device path, start, block and size\n");
401    return 1;
402  }
403
404  name = argv[1];
405 
406  if (stat (name, &st) < 0)
407  {
408    printf ("error: stat '%s' failed: %s\n", name, strerror (errno));
409    return 1;
410  }
411
412  start = strtoul (argv[2], 0, 0);
413  count = strtoul (argv[3], 0, 0);
414  size  = strtoul (argv[4], 0, 0);
415 
416  return disk_test_write_blocks (st.st_rdev, start, count, size);
417}
418
419static size_t
420parse_size_arg (const char* arg)
421{
422  size_t size;
423  size_t scalar = 1;
424 
425  size = strtoul (arg, 0, 0);
426  switch (arg[strlen (arg) - 1])
427  {
428    case 'M':
429      scalar = (size_t) 1000 * 1024;
430      break;
431    case 'm':
432      scalar = 1000000;
433      break;
434    case 'K':
435      scalar = 1024;
436      break;
437    case 'k':
438      scalar = 1000;
439      break;
440    default:
441      printf ("error: invalid scalar (M/m/K/k): %c\n", arg[strlen (arg) - 1]);
442      return 0;
443  }
444  return size * scalar;
445 }
446
447static int
448create_ramdisk (int argc, char *argv[])
449{
450  rtems_device_major_number major;
451  rtems_status_code         sc;
452  int                       arg;
453  size_t                    size = 0;
454  size_t                    block_size = 0;
455
456  for (arg = 0; arg < argc; ++arg)
457  {
458    if (argv[arg][0] == '-')
459    {
460      switch (argv[arg][0])
461      {
462        case 's':
463          ++arg;
464          if (arg == argc)
465          {
466            printf ("error: -s needs a size\n");
467            return 1;
468          }
469          size = parse_size_arg (argv[arg]);
470          if (size == 0)
471            return 1;
472          break;
473        case 'b':
474          ++arg;
475          if (arg == argc)
476          {
477            printf ("error: -b needs a size\n");
478            return 1;
479          }
480          block_size = parse_size_arg (argv[arg]);
481          if (size == 0)
482            return 1;
483          break;
484        default:
485          printf ("error: invalid option: %s\n", argv[arg]);
486          return 1;
487      }
488    }
489  }
490
491  if (block_size)
492    rtems_ramdisk_configuration[0].block_size = block_size;
493  if (size)
494    rtems_ramdisk_configuration[0].block_num =
495      size / rtems_ramdisk_configuration[0].block_size;
496   
497  /*
498   * Register the RAM Disk driver.
499   */
500  printf ("Register RAM Disk Driver [blocks=%" PRIu32 \
501          " block-size=%" PRIu32"]:",
502          rtems_ramdisk_configuration[0].block_num,
503          rtems_ramdisk_configuration[0].block_size);
504 
505  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
506                                 &rtems_ramdisk_io_ops,
507                                 &major);
508  if (sc != RTEMS_SUCCESSFUL)
509  {
510    printf ("error: ramdisk driver not initialised: %s\n",
511            rtems_status_text (sc));
512    return 1;
513  }
514 
515  printf ("successful\n");
516
517  return 0;
518}
519
520static int
521create_nvdisk (int argc, char *argv[])
522{
523  rtems_device_major_number major;
524  rtems_status_code         sc;
525  int                       arg;
526  size_t                    size = 0;
527#if ADD_WHEN_NVDISK_HAS_CHANGED
528  size_t                    block_size = 0;
529#endif
530 
531  for (arg = 0; arg < argc; ++arg)
532  {
533    if (argv[arg][0] == '-')
534    {
535      switch (argv[arg][0])
536      {
537        case 's':
538          ++arg;
539          if (arg == argc)
540          {
541            printf ("error: -s needs a size\n");
542            return 1;
543          }
544          size = parse_size_arg (argv[arg]);
545          if (size == 0)
546            return 1;
547          break;
548#if ADD_WHEN_NVDISK_HAS_CHANGED
549        case 'b':
550          ++arg;
551          if (arg == argc)
552          {
553            printf ("error: -b needs a size\n");
554            return 1;
555          }
556          block_size = parse_size_arg (argv[arg]);
557          if (size == 0)
558            return 1;
559          break;
560#endif
561        default:
562          printf ("error: invalid option: %s\n", argv[arg]);
563          return 1;
564      }
565    }
566  }
567
568#if ADD_WHEN_NVDISK_HAS_CHANGED
569  if (block_size)
570    rtems_nvdisk_configuration[0].block_size = block_size;
571#endif
572  if (size)
573    rtems_nv_heap_device_descriptor[0].size = size;
574   
575  /*
576   * For our test we do not have any static RAM or EEPROM devices so
577   * we allocate the memory from the heap.
578   */
579  rtems_nv_heap_device_descriptor[0].base =
580    malloc (rtems_nv_heap_device_descriptor[0].size);
581
582  if (!rtems_nv_heap_device_descriptor[0].base)
583  {
584    printf ("error: no memory for NV disk\n");
585    return 1;
586  }
587 
588  /*
589   * Register the RAM Disk driver.
590   */
591  printf ("Register NV Disk Driver [size=%" PRIu32 \
592          " block-size=%" PRIu32"]:",
593          rtems_nv_heap_device_descriptor[0].size,
594          rtems_nvdisk_configuration[0].block_size);
595 
596  sc = rtems_io_register_driver (RTEMS_DRIVER_AUTO_MAJOR,
597                                 &rtems_nvdisk_io_ops,
598                                 &major);
599  if (sc != RTEMS_SUCCESSFUL)
600  {
601    printf ("error: nvdisk driver not initialised: %s\n",
602            rtems_status_text (sc));
603    return 1;
604  }
605 
606  printf ("successful\n");
607
608  return 0;
609}
610
611static void writeFile(
612  const char *name,
613  mode_t      mode,
614  const char *contents
615)
616{
617  int sc;
618  sc = setuid(0);
619  if ( sc ) {
620    printf( "setuid failed: %s: %s\n", name, strerror(errno) );
621  }
622
623  rtems_shell_write_file( name, contents );
624
625  sc = chmod ( name, mode );
626  if ( sc ) {
627    printf( "chmod %s: %s\n", name, strerror(errno) );
628  }
629}
630
631#define writeScript( _name, _contents ) \
632        writeFile( _name, 0777, _contents )
633
634static void fileio_start_shell(void)
635{
636  int sc;
637
638  sc = mkdir("/scripts", 0777);
639  if ( sc ) {
640    printf( "mkdir /scripts: %s:\n", strerror(errno) );
641  }
642
643  sc = mkdir("/etc", 0777);
644  if ( sc ) {
645    printf( "mkdir /etc: %s:\n", strerror(errno) );
646  }
647
648  printf(
649    "Creating /etc/passwd and group with three useable accounts\n"
650    "root/pwd , test/pwd, rtems/NO PASSWORD"
651  );
652
653  writeFile(
654    "/etc/passwd",
655    0644,
656    "root:7QR4o148UPtb.:0:0:root::/:/bin/sh\n"
657    "rtems:*:1:1:RTEMS Application::/:/bin/sh\n"
658    "test:8Yy.AaxynxbLI:2:2:test account::/:/bin/sh\n"
659    "tty:!:3:3:tty owner::/:/bin/false\n"
660  );
661  writeFile(
662    "/etc/group",
663    0644,
664    "root:x:0:root\n"
665    "rtems:x:1:rtems\n"
666    "test:x:2:test\n"
667    "tty:x:3:tty\n"
668  );
669
670  writeScript(
671    "/scripts/js",
672    "#! joel\n"
673    "\n"
674    "date\n"
675    "echo Script successfully ran\n"
676    "date\n"
677    "stackuse\n"
678  );
679
680  writeScript(
681    "/scripts/j1",
682    "#! joel -s 20480 -t JESS\n"
683    "stackuse\n"
684  );
685
686  rtems_shell_write_file(
687    "/scripts/j2",
688    "echo j2 TEST FILE\n"
689    "echo j2   SHOULD BE non-executable AND\n"
690    "echo j2   DOES NOT have the magic first line\n"
691  );
692
693  rtems_shell_add_cmd ("mkrd", "files",
694                       "Create a RAM disk driver", create_ramdisk);
695  rtems_shell_add_cmd ("mknvd", "files",
696                       "Create a NV disk driver", create_nvdisk);
697  rtems_shell_add_cmd ("nverase", "misc",
698                       "nverase driver", shell_nvdisk_erase);
699  rtems_shell_add_cmd ("nvtrace", "misc",
700                       "nvtrace driver level", shell_nvdisk_trace);
701  rtems_shell_add_cmd ("bdbuftrace", "files",
702                       "bdbuf trace toggle", shell_bdbuf_trace);
703  rtems_shell_add_cmd ("td", "files",
704                       "Test disk", disk_test_block_sizes);
705#if RTEMS_RFS_TRACE
706  rtems_shell_add_cmd ("rfs", "files",
707                       "RFS trace",
708                       rtems_rfs_trace_shell_command);
709#endif
710#if RTEMS_RFS_RTEMS_TRACE
711  rtems_shell_add_cmd ("rrfs", "files",
712                       "RTEMS RFS trace",
713                       rtems_rfs_rtems_trace_shell_command);
714#endif
715
716  printf("\n =========================\n");
717  printf(" starting shell\n");
718  printf(" =========================\n");
719  rtems_shell_init(
720    "SHLL",                          /* task_name */
721    RTEMS_MINIMUM_STACK_SIZE * 4,    /* task_stacksize */
722    100,                             /* task_priority */
723    "/dev/console",                  /* devname */
724    false,                           /* forever */
725    true,                            /* wait */
726    NULL                             /* 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  puts( "\n\n*** TEST FILE I/O SAMPLE ***" );
1238
1239  status = rtems_shell_wait_for_input(
1240    STDIN_FILENO,
1241    20,
1242    notification,
1243    NULL
1244  );
1245  if (status == RTEMS_SUCCESSFUL) {
1246    Task_name = rtems_build_name('F','M','N','U');
1247
1248    status = rtems_task_create(
1249      Task_name, 1, RTEMS_MINIMUM_STACK_SIZE * 2,
1250      RTEMS_DEFAULT_MODES ,
1251      RTEMS_FLOATING_POINT | RTEMS_DEFAULT_ATTRIBUTES, &Task_id
1252    );
1253    directive_failed( status, "create" );
1254
1255    status = rtems_task_start( Task_id, fileio_task, 1 );
1256    directive_failed( status, "start" );
1257
1258    status = rtems_task_delete( RTEMS_SELF );
1259    directive_failed( status, "delete" );
1260  } else {
1261    puts( "*** END OF TEST FILE I/O SAMPLE ***" );
1262
1263    rtems_test_exit( 0 );
1264  }
1265}
1266
1267#if defined(USE_SHELL)
1268/*
1269 *  RTEMS Shell Configuration -- Add a command and an alias for it
1270 */
1271
1272static int main_usercmd(int argc, char **argv)
1273{
1274  int i;
1275  printf( "UserCommand: argc=%d\n", argc );
1276  for (i=0 ; i<argc ; i++ )
1277    printf( "argv[%d]= %s\n", i, argv[i] );
1278  return 0;
1279}
1280
1281static rtems_shell_cmd_t Shell_USERCMD_Command = {
1282  "usercmd",                                       /* name */
1283  "usercmd n1 [n2 [n3...]]     # echo arguments",  /* usage */
1284  "user",                                          /* topic */
1285  main_usercmd,                                    /* command */
1286  NULL,                                            /* alias */
1287  NULL                                             /* next */
1288};
1289
1290static rtems_shell_alias_t Shell_USERECHO_Alias = {
1291  "usercmd",                 /* command */
1292  "userecho"                 /* alias */
1293};
1294
1295
1296#define CONFIGURE_SHELL_USER_COMMANDS &Shell_USERCMD_Command
1297#define CONFIGURE_SHELL_USER_ALIASES &Shell_USERECHO_Alias
1298#define CONFIGURE_SHELL_COMMANDS_INIT
1299#define CONFIGURE_SHELL_COMMANDS_ALL
1300#define CONFIGURE_SHELL_MOUNT_MSDOS
1301#define CONFIGURE_SHELL_MOUNT_RFS
1302#define CONFIGURE_SHELL_DEBUGRFS
1303
1304#include <rtems/shellconfig.h>
1305#endif
1306
1307#else
1308/*
1309 * RTEMS Startup Task
1310 */
1311rtems_task
1312Init (rtems_task_argument ignored)
1313{
1314  puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
1315  puts( "\n\n*** NOT ENOUGH MEMORY TO BUILD AND RUN ***" );
1316}
1317#endif
Note: See TracBrowser for help on using the repository browser.