source: rtems/testsuites/samples/fileio/init.c @ 36cb812

Last change on this file since 36cb812 was 36cb812, checked in by Thomas Doerfler <Thomas.Doerfler@…>, on 12/18/06 at 09:46:59

corrected bug in ata.c to avoid lockup of libblock
added remote frequest support to gen5200 BSP

  • Property mode set to 100644
File size: 14.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-1999.
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 *  init.c,v 1.11 2000/06/12 15:00:12 joel Exp
21 */
22
23#define CONFIGURE_INIT
24#include "system.h"
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <errno.h>
30#include <rtems.h>
31#include <fcntl.h>
32#include <rtems/error.h>
33#include <rtems/dosfs.h>
34#include <ctype.h>
35#include <rtems/ide_part_table.h>
36#include <rtems/libcsupport.h>
37#include <rtems/fsmount.h>
38
39/*
40 * Table of FAT file systems that will be mounted
41 * with the "fsmount" function.
42 * See cpukit/libmisc/fsmount for definition of fields
43 */
44fstab_t fs_table[] = {
45  {
46    "/dev/hda1","/mnt/hda1",
47    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
48    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
49    0
50  },
51  {
52    "/dev/hda2","/mnt/hda2",
53    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
54    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
55    0
56  },
57  {
58    "/dev/hda3","/mnt/hda3",
59    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
60    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
61    0
62  },
63  {
64    "/dev/hda4","/mnt/hda4",
65    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
66    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
67    0
68  },
69  {
70    "/dev/hdc1","/mnt/hdc1",
71    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
72    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
73    0
74  },
75  {
76    "/dev/hdc2","/mnt/hdc2",
77    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
78    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
79    0
80  },
81  {
82    "/dev/hdc3","/mnt/hdc3",
83    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
84    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
85    0
86  },
87  {
88    "/dev/hdc4","/mnt/hdc4",
89    &msdos_ops, RTEMS_FILESYSTEM_READ_WRITE,
90    FSMOUNT_MNT_OK | FSMOUNT_MNTPNT_CRTERR | FSMOUNT_MNT_FAILED,
91    0
92  }
93};
94
95#define MIN(a,b) (((a) > (b)) ? (b) : (a))
96
97#define USE_SHELL
98
99#ifdef USE_SHELL
100#include <rtems/shell.h>
101
102void fileio_start_shell(void)
103{
104  printf(" =========================\n");
105  printf(" starting shell\n");
106  printf(" =========================\n");
107  shell_init("SHLL",0,100,"/dev/console",
108             B9600 | CS8,
109             0);
110  rtems_task_suspend(RTEMS_SELF);
111}
112
113#endif /* USE_SHELL */
114
115void fileio_print_free_heap(void)
116{
117  printf("--- unused dynamic memory: %lu bytes ---\n",
118         (unsigned long) malloc_free_space());
119}
120
121
122void fileio_part_table_initialize(void)
123{
124  char devname[64];
125  rtems_status_code rc;
126
127  printf(" =========================\n");
128  printf(" Initialize partition table\n");
129  printf(" =========================\n");
130  fileio_print_free_heap();
131  printf(" Enter device to initialize ==>");
132  fflush(stdout);
133  fgets(devname,sizeof(devname)-1,stdin);
134  while (devname[strlen(devname)-1] == '\n') {
135    devname[strlen(devname)-1] = '\0';
136  }
137  /*
138   * call function
139   */
140  rc = rtems_ide_part_table_initialize(devname);
141  printf("result = %d\n",rc);
142  fileio_print_free_heap();
143}
144
145void fileio_fsmount(void)
146{
147  rtems_status_code rc;
148
149  printf(" =========================\n");
150  printf(" Process fsmount table\n");
151  printf(" =========================\n");
152  fileio_print_free_heap();
153  /*
154   * call function
155   */
156  rc = rtems_fsmount( fs_table,
157                      sizeof(fs_table)/sizeof(fs_table[0]),
158                      NULL);
159  printf("result = %d\n",rc);
160  fileio_print_free_heap();
161}
162
163void fileio_list_file(void)
164{
165  char fname[1024];
166  char *buf_ptr = NULL;
167  ssize_t   flen = 0;
168  int fd = -1;
169  ssize_t n;
170  size_t buf_size = 100;
171
172  rtems_interval start_tick,curr_tick,ticks_per_sec;
173
174  printf(" =========================\n");
175  printf(" LIST FILE ...            \n");
176  printf(" =========================\n");
177  fileio_print_free_heap();
178  printf(" Enter filename to list ==>");
179  fflush(stdout);
180  fgets(fname,sizeof(fname)-1,stdin);
181  while (fname[strlen(fname)-1] == '\n') {
182    fname[strlen(fname)-1] = '\0';
183  }
184  /*
185   * allocate buffer of given size
186   */
187  if (buf_size > 0) {
188    buf_ptr = malloc(buf_size);
189  }
190
191  if (buf_ptr != NULL) {
192    printf("\n Trying to open file \"%s\" for read\n",fname);
193    fd = open(fname,O_RDONLY);
194    if (fd < 0) {
195      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
196    }
197  }
198
199  if (fd >= 0) {
200    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
201    do {
202      n = read(fd,buf_ptr,buf_size);
203      if (n > 0) {
204        write(1,buf_ptr,n);
205        flen += n;
206      }
207    } while (n > 0);
208
209    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
210
211    printf("\n ******** End of file reached, flen = %d\n",flen);
212    close(fd);
213
214    rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
215    printf("time elapsed for read:  %g seconds\n",
216           ((double)curr_tick-start_tick)/ticks_per_sec);
217  }
218  /*
219   * free buffer
220   */
221  if (buf_ptr != NULL) {
222    free(buf_ptr);
223  }
224  fileio_print_free_heap();
225}
226
227/*
228 * convert a size string (like 34K or 12M) to actual byte count
229 */
230boolean fileio_str2size(const char *str,uint32_t   *res_ptr)
231{
232  boolean failed = FALSE;
233  unsigned long size;
234  char suffix = ' ';
235
236  if (1 > sscanf(str,"%lu%c",&size,&suffix)) {
237    failed = TRUE;
238  }
239  else if (toupper(suffix) == 'K') {
240    size *= 1024;
241  }
242  else if (toupper(suffix) == 'M') {
243    size *= 1024UL*1024UL;
244  }
245  else if (isalpha(suffix)) {
246    failed = TRUE;
247  }
248
249  if (!failed) {
250    *res_ptr = size;
251  }
252  return failed;
253}
254
255void fileio_write_file(void)
256{
257  char fname[1024];
258  char tmp_str[32];
259  uint32_t   file_size = 0;
260  uint32_t   buf_size  = 0;
261  size_t curr_pos,bytes_to_copy;
262  int fd = -1;
263  ssize_t n;
264  rtems_interval start_tick,curr_tick,ticks_per_sec;
265  char *bufptr = NULL;
266  boolean failed = FALSE;
267  static const char write_test_string[] =
268    "The quick brown fox jumps over the lazy dog\n";
269  static const char write_block_string[] =
270    "\n----- end of write buffer ------\n";
271
272  printf(" =========================\n");
273  printf(" WRITE FILE ...           \n");
274  printf(" =========================\n");
275  fileio_print_free_heap();
276  /*
277   * get number of ticks per second
278   */
279  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
280
281  /*
282   * get path to file to write
283   */
284  if (!failed) {
285    printf("Enter path/filename ==>");
286    fflush(stdout);
287    fgets(fname,sizeof(fname)-1,stdin);
288    while (fname[strlen(fname)-1] == '\n') {
289      fname[strlen(fname)-1] = '\0';
290    }
291    if (0 == strlen(fname)) {
292      printf("*** no filename entered, aborted\n");
293      failed = TRUE;
294    }
295  }
296  /*
297   * get total file size to write
298   */
299  if (!failed) {
300    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
301           "Enter filesize to write ==>");
302    fflush(stdout);
303    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
304    failed = fileio_str2size(tmp_str,&file_size);
305    if (failed) {
306      printf("*** illegal file size, aborted\n");
307    }
308  }
309  /*
310   * get block size to write
311   */
312  if (!failed) {
313    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
314           "Enter block size to use for write calls ==>");
315    fflush(stdout);
316    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
317    failed = fileio_str2size(tmp_str,&buf_size);
318    if (failed) {
319      printf("*** illegal block size, aborted\n");
320    }
321  }
322
323  /*
324   * allocate buffer
325   */
326  if (!failed) {
327    printf("... allocating %lu bytes of buffer for write data\n",
328           (unsigned long)buf_size);
329    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
330    if (bufptr == NULL) {
331      printf("*** malloc failed, aborted\n");
332      failed = TRUE;
333    }
334  }
335  /*
336   * fill buffer with test pattern
337   */
338  if (!failed) {
339    printf("... filling buffer with write data\n");
340    curr_pos = 0;
341    /*
342     * fill buffer with test string
343     */
344    while (curr_pos < buf_size) {
345      bytes_to_copy = MIN(buf_size-curr_pos,
346                          sizeof(write_test_string)-1);
347      memcpy(bufptr+curr_pos,write_test_string,bytes_to_copy);
348      curr_pos += bytes_to_copy;
349    }
350    /*
351     * put "end" mark at end of buffer
352     */
353    bytes_to_copy = sizeof(write_block_string)-1;
354    if (buf_size >= bytes_to_copy) {
355      memcpy(bufptr+buf_size-bytes_to_copy,
356             write_block_string,
357             bytes_to_copy);
358    }
359  }
360  /*
361   * create file
362   */
363  if (!failed) {
364    printf("... creating file \"%s\"\n",fname);
365    fd = open(fname,O_WRONLY | O_CREAT | O_TRUNC,S_IREAD|S_IWRITE);
366    if (fd < 0) {
367      printf("*** file create failed, errno = %d(%s)\n",errno,strerror(errno));
368      failed = TRUE;
369    }
370  }
371  /*
372   * write file
373   */
374  if (!failed) {
375    printf("... writing to file\n");
376    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
377    curr_pos = 0;
378    do {
379      bytes_to_copy = buf_size;
380      do {
381        n = write(fd,
382          bufptr + (buf_size-bytes_to_copy),
383                  MIN(bytes_to_copy,file_size-curr_pos));
384        if (n > 0) {
385          bytes_to_copy -= n;
386          curr_pos      += n;
387        }
388      } while ((bytes_to_copy > 0)  && (n > 0));
389    } while ((file_size > curr_pos) && (n > 0));
390    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
391    if (n < 0) {
392      failed = TRUE;
393      printf("*** file write failed, "
394             "%lu bytes written, "
395             "errno = %d(%s)\n",
396             (unsigned long)curr_pos,errno,strerror(errno));
397    }
398    else {
399      printf("time elapsed for write:  %g seconds\n",
400             ((double)curr_tick-start_tick)/ticks_per_sec);
401      printf("write data rate: %g KBytes/second\n",
402             (((double)file_size) / 1024.0 /
403              (((double)curr_tick-start_tick)/ticks_per_sec)));
404    }
405  }
406  if (fd >= 0) {
407    printf("... closing file\n");
408    close(fd);
409  }
410  if (bufptr != NULL) {
411    printf("... deallocating buffer\n");
412    free(bufptr);
413    bufptr = NULL;
414  }
415  printf("\n ******** End of file write\n");
416  fileio_print_free_heap();
417}
418
419void fileio_read_file(void)
420{
421  char fname[1024];
422  char tmp_str[32];
423  uint32_t   buf_size  = 0;
424  size_t curr_pos;
425  int fd = -1;
426  ssize_t n;
427  rtems_interval start_tick,curr_tick,ticks_per_sec;
428  char *bufptr = NULL;
429  boolean failed = FALSE;
430
431  printf(" =========================\n");
432  printf(" READ FILE ...            \n");
433  printf(" =========================\n");
434  fileio_print_free_heap();
435  /*
436   * get number of ticks per second
437   */
438  rtems_clock_get(RTEMS_CLOCK_GET_TICKS_PER_SECOND, &ticks_per_sec);
439
440  /*
441   * get path to file to read
442   */
443  if (!failed) {
444    printf("Enter path/filename ==>");
445    fflush(stdout);
446    fgets(fname,sizeof(fname)-1,stdin);
447    while (fname[strlen(fname)-1] == '\n') {
448      fname[strlen(fname)-1] = '\0';
449    }
450    if (0 == strlen(fname)) {
451      printf("*** no filename entered, aborted\n");
452      failed = TRUE;
453    }
454  }
455  /*
456   * get block size to read
457   */
458  if (!failed) {
459    printf("use suffix K for Kbytes, M for Mbytes or no suffix for bytes:\n"
460           "Enter block size to use for read calls ==>");
461    fflush(stdout);
462    fgets(tmp_str,sizeof(tmp_str)-1,stdin);
463    failed = fileio_str2size(tmp_str,&buf_size);
464    if (failed) {
465      printf("*** illegal block size, aborted\n");
466    }
467  }
468
469  /*
470   * allocate buffer
471   */
472  if (!failed) {
473    printf("... allocating %lu bytes of buffer for write data\n",
474           (unsigned long)buf_size);
475    bufptr = malloc(buf_size+1); /* extra space for terminating NUL char */
476    if (bufptr == NULL) {
477      printf("*** malloc failed, aborted\n");
478      failed = TRUE;
479    }
480  }
481  /*
482   * open file
483   */
484  if (!failed) {
485    printf("... opening file \"%s\"\n",fname);
486    fd = open(fname,O_RDONLY);
487    if (fd < 0) {
488      printf("*** file open failed, errno = %d(%s)\n",errno,strerror(errno));
489      failed = TRUE;
490    }
491  }
492  /*
493   * read file
494   */
495  if (!failed) {
496    printf("... reading from file\n");
497    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &start_tick);
498    curr_pos = 0;
499    do {
500      n = read(fd,
501               bufptr,
502               buf_size);
503      if (n > 0) {
504        curr_pos      += n;
505      }
506    } while (n > 0);
507    rtems_clock_get (RTEMS_CLOCK_GET_TICKS_SINCE_BOOT, &curr_tick);
508    if (n < 0) {
509      failed = TRUE;
510      printf("*** file read failed, "
511             "%lu bytes read, "
512             "errno = %d(%s)\n",
513             (unsigned long)curr_pos,errno,strerror(errno));
514    }
515    else {
516      printf("%lu bytes read\n",
517             (unsigned long)curr_pos);
518      printf("time elapsed for read:  %g seconds\n",
519             ((double)curr_tick-start_tick)/ticks_per_sec);
520      printf("read data rate: %g KBytes/second\n",
521             (((double)curr_pos) / 1024.0 /
522              (((double)curr_tick-start_tick)/ticks_per_sec)));
523    }
524  }
525  if (fd >= 0) {
526    printf("... closing file\n");
527    close(fd);
528  }
529  if (bufptr != NULL) {
530    printf("... deallocating buffer\n");
531    free(bufptr);
532    bufptr = NULL;
533  }
534  printf("\n ******** End of file read\n");
535  fileio_print_free_heap();
536
537}
538
539void fileio_menu (void)
540{
541  char inbuf[10];
542
543  /*
544   * Wait for characters from console terminal
545   */
546  for (;;) {
547    printf(" =========================\n");
548    printf(" RTEMS FILE I/O Test Menu \n");
549    printf(" =========================\n");
550    printf("   p -> part_table_initialize\n");
551    printf("   f -> mount all disks in fs_table\n");
552    printf("   l -> list  file\n");
553    printf("   r -> read  file\n");
554    printf("   w -> write file\n");
555#ifdef USE_SHELL
556    printf("   s -> start shell\n");
557#endif
558    printf("   Enter your selection ==>");
559    fflush(stdout);
560
561    inbuf[0] = '\0';
562    fgets(inbuf,sizeof(inbuf),stdin);
563    switch (inbuf[0]) {
564    case 'l':
565      fileio_list_file ();
566      break;
567    case 'r':
568      fileio_read_file ();
569      break;
570    case 'w':
571      fileio_write_file ();
572      break;
573    case 'p':
574      fileio_part_table_initialize ();
575      break;
576    case 'f':
577      fileio_fsmount ();
578      break;
579#ifdef USE_SHELL
580    case 's':
581      fileio_start_shell ();
582      break;
583#endif
584    default:
585      printf("Selection `%c` not implemented\n",inbuf[0]);
586      break;
587    }
588
589  }
590  exit (0);
591}
592
593int menu_tid;
594
595/*
596 * RTEMS Startup Task
597 */
598rtems_task
599Init (rtems_task_argument ignored)
600{
601  puts( "\n\n*** FILE I/O SAMPLE AND TEST ***" );
602
603  fileio_menu();
604}
Note: See TracBrowser for help on using the repository browser.