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
RevLine 
[ac0a2af]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 *
[7c1e6942]13 *  COPYRIGHT (c) 1989-2011.
[ac0a2af]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
[3e26377b]18 *  http://www.rtems.com/license/LICENSE.
[ac0a2af]19 *
[6dcb3b0]20 *  $Id$
[ac0a2af]21 */
22
[e313551]23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
[ac0a2af]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>
[7032a458]36#include <inttypes.h>
[ac0a2af]37#include <rtems/error.h>
[05153a1]38#include <rtems/dosfs.h>
[ac0a2af]39#include <ctype.h>
[3c02c9dd]40#include <rtems/bdpart.h>
[ac0a2af]41#include <rtems/libcsupport.h>
42#include <rtems/fsmount.h>
[7032a458]43#include <rtems/ramdisk.h>
44#include <rtems/nvdisk.h>
45#include <rtems/nvdisk-sram.h>
[7c9d27e]46#include <rtems/shell.h>
[ac0a2af]47
[c0ec0d82]48#if FILEIO_BUILD
[7032a458]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,
[96bb783a]95    size:   (size_t) 1024 * 1024,
[7032a458]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
[4534ba3]131#if 0
[7032a458]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}
[4534ba3]169#endif
[7032a458]170
[ac0a2af]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  {
[558a5f48]178    "/dev/hda1","/mnt/hda1", "dosfs",
179    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]180    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
181    0
182  },
183  {
[558a5f48]184    "/dev/hda2","/mnt/hda2", "dosfs",
185    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]186    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
187    0
188  },
189  {
[558a5f48]190    "/dev/hda3","/mnt/hda3", "dosfs",
191    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]192    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
193    0
194  },
195  {
[558a5f48]196    "/dev/hda4","/mnt/hda4", "dosfs",
197    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]198    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
199    0
200  },
201  {
[558a5f48]202    "/dev/hdc1","/mnt/hdc1", "dosfs",
203    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]204    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
205    0
206  },
207  {
[558a5f48]208    "/dev/hdc2","/mnt/hdc2", "dosfs",
209    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]210    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
211    0
212  },
213  {
[558a5f48]214    "/dev/hdc3","/mnt/hdc3", "dosfs",
215    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]216    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
217    0
218  },
219  {
[558a5f48]220    "/dev/hdc4","/mnt/hdc4", "dosfs",
221    RTEMS_FILESYSTEM_READ_WRITE,
[ac0a2af]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
[4534ba3]233static int
[7032a458]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
[4534ba3]266static int
[7032a458]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
[4534ba3]314static int
[7032a458]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
[4534ba3]327static int
[7032a458]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
[4534ba3]347static int
[7032a458]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
[4534ba3]389static int
[7032a458]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
[4534ba3]419static size_t
[7032a458]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':
[96bb783a]429      scalar = (size_t) 1000 * 1024;
[7032a458]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
[4534ba3]447static int
[7032a458]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
[4534ba3]520static int
[7032a458]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
[3c02c9dd]611static void writeFile(
[a645637]612  const char *name,
[3c02c9dd]613  mode_t      mode,
[a645637]614  const char *contents
615)
616{
617  int sc;
618  sc = setuid(0);
619  if ( sc ) {
[a3b2830]620    printf( "setuid failed: %s: %s\n", name, strerror(errno) );
[a645637]621  }
622
623  rtems_shell_write_file( name, contents );
[527ecc7a]624
625  sc = chmod ( name, mode );
[a645637]626  if ( sc ) {
[a3b2830]627    printf( "chmod %s: %s\n", name, strerror(errno) );
[a645637]628  }
629}
630
[527ecc7a]631#define writeScript( _name, _contents ) \
632        writeFile( _name, 0777, _contents )
633
[3c02c9dd]634static void fileio_start_shell(void)
[ac0a2af]635{
[a645637]636  int sc;
[527ecc7a]637
[a645637]638  sc = mkdir("/scripts", 0777);
639  if ( sc ) {
640    printf( "mkdir /scripts: %s:\n", strerror(errno) );
641  }
642
[527ecc7a]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
[a645637]670  writeScript(
[b1274bd9]671    "/scripts/js",
[a645637]672    "#! joel\n"
673    "\n"
674    "date\n"
675    "echo Script successfully ran\n"
676    "date\n"
677    "stackuse\n"
678  );
679
680  writeScript(
[b1274bd9]681    "/scripts/j1",
[a645637]682    "#! joel -s 20480 -t JESS\n"
683    "stackuse\n"
684  );
685
686  rtems_shell_write_file(
[b1274bd9]687    "/scripts/j2",
[a645637]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
[7032a458]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");
[ac0a2af]717  printf(" starting shell\n");
718  printf(" =========================\n");
[72cb077f]719  rtems_shell_init(
720    "SHLL",                          /* task_name */
721    RTEMS_MINIMUM_STACK_SIZE * 4,    /* task_stacksize */
722    100,                             /* task_priority */
723    "/dev/console",                  /* devname */
[7a18a320]724    false,                           /* forever */
725    true,                            /* wait */
[7032a458]726    NULL                             /* login */
[72cb077f]727  );
[ac0a2af]728}
729#endif /* USE_SHELL */
730
[3c02c9dd]731static void fileio_print_free_heap(void)
[ac0a2af]732{
733  printf("--- unused dynamic memory: %lu bytes ---\n",
734         (unsigned long) malloc_free_space());
735}
736
737
[3c02c9dd]738static void fileio_part_table_initialize(void)
[ac0a2af]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 ==>");
[3d14a45]748  fflush(stdout);
[ac0a2af]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   */
[3c02c9dd]756  rc = rtems_bdpart_register_from_disk(devname);
[ac0a2af]757  printf("result = %d\n",rc);
758  fileio_print_free_heap();
759}
760
[3c02c9dd]761static void fileio_fsmount(void)
[ac0a2af]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
[3c02c9dd]779static void fileio_list_file(void)
[ac0a2af]780{
781  char fname[1024];
782  char *buf_ptr = NULL;
[d66557f]783  ssize_t   flen = 0;
[ac0a2af]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 ==>");
[3d14a45]795  fflush(stdout);
[ac0a2af]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) {
[91c80547]816    start_tick = rtems_clock_get_ticks_since_boot();
[ac0a2af]817    do {
818      n = read(fd,buf_ptr,buf_size);
819      if (n > 0) {
[3c02c9dd]820        write(1,buf_ptr,(size_t) n);
[ac0a2af]821        flen += n;
822      }
823    } while (n > 0);
[8f71a36]824
[91c80547]825    curr_tick = rtems_clock_get_ticks_since_boot();
[8f71a36]826
[edfc0f9]827    printf("\n ******** End of file reached, flen = %zd\n",flen);
[ac0a2af]828    close(fd);
[8f71a36]829
[99f09711]830    ticks_per_sec = rtems_clock_get_ticks_per_second();
[ac0a2af]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 */
[3c02c9dd]846static bool fileio_str2size(const char *str,uint32_t   *res_ptr)
[ac0a2af]847{
[f0157b8]848  bool failed = false;
[ac0a2af]849  unsigned long size;
[8c8e3e0]850  unsigned char suffix = ' ';
[ac0a2af]851
852  if (1 > sscanf(str,"%lu%c",&size,&suffix)) {
[f0157b8]853    failed = true;
[ac0a2af]854  }
[558a5f48]855  else if (toupper((int)suffix) == 'K') {
[ac0a2af]856    size *= 1024;
857  }
[558a5f48]858  else if (toupper((int)suffix) == 'M') {
[ac0a2af]859    size *= 1024UL*1024UL;
860  }
[558a5f48]861  else if (isalpha((int)suffix)) {
[f0157b8]862    failed = true;
[ac0a2af]863  }
864
865  if (!failed) {
866    *res_ptr = size;
867  }
868  return failed;
869}
870
[3c02c9dd]871static void fileio_write_file(void)
[ac0a2af]872{
873  char fname[1024];
874  char tmp_str[32];
[4c84d7b]875  uint32_t   file_size = 0;
876  uint32_t   buf_size  = 0;
[ac0a2af]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;
[f0157b8]882  bool failed = false;
[8f71a36]883  static const char write_test_string[] =
[ac0a2af]884    "The quick brown fox jumps over the lazy dog\n";
[8f71a36]885  static const char write_block_string[] =
[ac0a2af]886    "\n----- end of write buffer ------\n";
[8f71a36]887
[ac0a2af]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   */
[99f09711]895  ticks_per_sec = rtems_clock_get_ticks_per_second();
[ac0a2af]896
897  /*
898   * get path to file to write
899   */
900  if (!failed) {
901    printf("Enter path/filename ==>");
[3d14a45]902    fflush(stdout);
[ac0a2af]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");
[f0157b8]909      failed = true;
[ac0a2af]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 ==>");
[3d14a45]918    fflush(stdout);
[ac0a2af]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 ==>");
[3d14a45]931    fflush(stdout);
[ac0a2af]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");
[f0157b8]948      failed = true;
[ac0a2af]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));
[f0157b8]984      failed = true;
[ac0a2af]985    }
986  }
987  /*
988   * write file
989   */
990  if (!failed) {
991    printf("... writing to file\n");
[91c80547]992    start_tick = rtems_clock_get_ticks_since_boot();
[ac0a2af]993    curr_pos = 0;
[8f71a36]994    do {
[ac0a2af]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) {
[3c02c9dd]1001          bytes_to_copy -= (size_t) n;
1002          curr_pos      += (size_t) n;
[ac0a2af]1003        }
1004      } while ((bytes_to_copy > 0)  && (n > 0));
1005    } while ((file_size > curr_pos) && (n > 0));
[91c80547]1006    curr_tick = rtems_clock_get_ticks_since_boot();
[ac0a2af]1007    if (n < 0) {
[f0157b8]1008      failed = true;
[ac0a2af]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
[3c02c9dd]1035static void fileio_read_file(void)
[ac0a2af]1036{
1037  char fname[1024];
1038  char tmp_str[32];
[4c84d7b]1039  uint32_t   buf_size  = 0;
[ac0a2af]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;
[f0157b8]1045  bool failed = false;
[8f71a36]1046
[ac0a2af]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   */
[99f09711]1054  ticks_per_sec = rtems_clock_get_ticks_per_second();
[ac0a2af]1055
1056  /*
1057   * get path to file to read
1058   */
1059  if (!failed) {
1060    printf("Enter path/filename ==>");
[3d14a45]1061    fflush(stdout);
[ac0a2af]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");
[f0157b8]1068      failed = true;
[ac0a2af]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 ==>");
[3d14a45]1077    fflush(stdout);
[ac0a2af]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");
[f0157b8]1094      failed = true;
[ac0a2af]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));
[f0157b8]1105      failed = true;
[ac0a2af]1106    }
1107  }
1108  /*
1109   * read file
1110   */
1111  if (!failed) {
1112    printf("... reading from file\n");
[91c80547]1113    start_tick = rtems_clock_get_ticks_since_boot();
[ac0a2af]1114    curr_pos = 0;
[8f71a36]1115    do {
[ac0a2af]1116      n = read(fd,
1117               bufptr,
1118               buf_size);
1119      if (n > 0) {
[3c02c9dd]1120        curr_pos      += (size_t) n;
[ac0a2af]1121      }
1122    } while (n > 0);
[91c80547]1123    curr_tick = rtems_clock_get_ticks_since_boot();
[ac0a2af]1124    if (n < 0) {
[f0157b8]1125      failed = true;
[ac0a2af]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",
[8f71a36]1137             (((double)curr_pos) / 1024.0 /
[ac0a2af]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
[3c02c9dd]1155static void fileio_menu (void)
[ac0a2af]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 ==>");
[3d14a45]1175    fflush(stdout);
[ac0a2af]1176
1177    inbuf[0] = '\0';
1178    fgets(inbuf,sizeof(inbuf),stdin);
1179    switch (inbuf[0]) {
1180    case 'l':
[8f71a36]1181      fileio_list_file ();
[ac0a2af]1182      break;
1183    case 'r':
[8f71a36]1184      fileio_read_file ();
[ac0a2af]1185      break;
1186    case 'w':
[8f71a36]1187      fileio_write_file ();
[ac0a2af]1188      break;
1189    case 'p':
[8f71a36]1190      fileio_part_table_initialize ();
[ac0a2af]1191      break;
1192    case 'f':
[8f71a36]1193      fileio_fsmount ();
[ac0a2af]1194      break;
1195#ifdef USE_SHELL
1196    case 's':
[8f71a36]1197      fileio_start_shell ();
[ac0a2af]1198      break;
1199#endif
1200    default:
1201      printf("Selection `%c` not implemented\n",inbuf[0]);
1202      break;
1203    }
[8f71a36]1204
[ac0a2af]1205  }
1206  exit (0);
1207}
1208
[60e5832]1209/*
1210 * RTEMS File Menu Task
1211 */
[3c02c9dd]1212static rtems_task
[60e5832]1213fileio_task (rtems_task_argument ignored)
1214{
1215  fileio_menu();
1216}
1217
[7c9d27e]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
[ac0a2af]1227/*
1228 * RTEMS Startup Task
1229 */
1230rtems_task
1231Init (rtems_task_argument ignored)
1232{
[60e5832]1233  rtems_name Task_name;
1234  rtems_id   Task_id;
1235  rtems_status_code status;
1236
[7c9d27e]1237  puts( "\n\n*** TEST FILE I/O SAMPLE ***" );
[60e5832]1238
[7c9d27e]1239  status = rtems_shell_wait_for_input(
1240    STDIN_FILENO,
1241    20,
1242    notification,
1243    NULL
[60e5832]1244  );
[7c9d27e]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" );
[60e5832]1254
[7c9d27e]1255    status = rtems_task_start( Task_id, fileio_task, 1 );
1256    directive_failed( status, "start" );
[60e5832]1257
[7c9d27e]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  }
[ac0a2af]1265}
[4c081a04]1266
[339fd66]1267#if defined(USE_SHELL)
[4c081a04]1268/*
1269 *  RTEMS Shell Configuration -- Add a command and an alias for it
1270 */
1271
[3c02c9dd]1272static int main_usercmd(int argc, char **argv)
[4c081a04]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
[3c02c9dd]1281static rtems_shell_cmd_t Shell_USERCMD_Command = {
[4c081a04]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
[3c02c9dd]1290static rtems_shell_alias_t Shell_USERECHO_Alias = {
[4c081a04]1291  "usercmd",                 /* command */
1292  "userecho"                 /* alias */
1293};
[b1274bd9]1294
[4c081a04]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
[d2a488f7]1300#define CONFIGURE_SHELL_MOUNT_MSDOS
[558a5f48]1301#define CONFIGURE_SHELL_MOUNT_RFS
1302#define CONFIGURE_SHELL_DEBUGRFS
[4c081a04]1303
1304#include <rtems/shellconfig.h>
[339fd66]1305#endif
[4c081a04]1306
[c0ec0d82]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.