rtems support: Rudimentary test harness now working.
[yaffs2.git] / rtems / rtems-y-test / yaffs-rtems-test.c
1 /*
2  *  Simple test program -- demonstrating use of IMFS
3  */
4
5 #include <bsp.h>
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include <rtems/libio.h>
13 #include <yaffs/rtems_yaffs.h>
14
15 #include "yaffs-rtems-flashsim.h"
16
17 //#define YPATH "/yaffs_mount_pt/"
18 #define YPATH ""
19
20 void yaffs_bug_fn(const char *file_name, int line_no)
21 {
22         printf("Yaffs bug at %s:%d\n", file_name, line_no);
23 }
24
25 int filesystem_init(const char *mount_target)
26 {
27         struct yaffs_dev *device;
28         rtems_yaffs_default_os_context *os_context;
29         rtems_yaffs_mount_data mount_args;
30
31
32         rtems_filesystem_register(RTEMS_FILESYSTEM_TYPE_YAFFS,
33                                   rtems_yaffs_mount_handler);
34
35         // We mount the filesystem by passing an appropriate
36         // rtems_yaffs_mount_data as the last argument to mount(). mount_data is
37         // used to pass a yaffs_dev pointer by-value.
38
39
40         device = yaffs_rtems_flashsim_setup();
41
42         // Initialize callback storage for RTEMS's VFS inside the yaffs_dev.
43         os_context = malloc(sizeof(rtems_yaffs_default_os_context));
44         rtems_yaffs_initialize_default_os_context(os_context);
45
46         device->os_context = os_context;
47
48         mount_args.dev = device;
49
50         if (mount_and_make_target_path(NULL,
51                               NULL /*mount_target */,
52                               RTEMS_FILESYSTEM_TYPE_YAFFS,
53                               RTEMS_FILESYSTEM_READ_WRITE,
54                               &mount_args) < 0) {
55                 return errno;
56         } else {
57                 return 0;
58         }
59 }
60
61
62 void set_uint8_t_buffer(uint8_t *buf, uint32_t n, uint8_t start, uint8_t inc)
63 {
64         while (n) {
65                 *buf = start;
66                 buf++;
67                 start += inc;
68                 n--;
69         }
70 }
71
72 int run_basic_file_test(void)
73 {
74         int fd;
75         int ret;
76         uint8_t buf[100];
77         uint8_t buf2[100];
78
79         fd = open(YPATH"/test", O_RDWR | O_CREAT | O_TRUNC, 0666);
80         printf("open = %d\n", fd);
81
82         set_uint8_t_buffer(buf, sizeof(buf), 0xAA, 1);
83
84         ret = write(fd, buf, sizeof(buf));
85
86         printf("write returned %d\n", ret);
87
88         if (ret == -1)
89                 perror("writing file");
90
91         ret = lseek(fd, 0, SEEK_END);
92
93         printf("lseek end ret = %d\n", ret);
94
95         ret = lseek(fd, 0, SEEK_SET);
96         printf("lseek start ret = %d\n", ret);
97
98         ret = read(fd, buf2, sizeof(buf2));
99
100         printf("reading file ret = %d\n", ret);
101
102         if (ret == -1)
103                 perror("reading file");
104
105
106         return ret;
107
108 #if 0
109
110
111    fd = open("test1", O_CREAT);
112    printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) );
113    close(fd);
114
115    fd = open("test", O_RDONLY);
116    if (fd == -1) {
117      printf("Starting on the wrong foot....\n");
118      exit(-1);
119    }
120
121    printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) );
122
123    fp = fdopen(fd, "r");
124    if (fp == NULL) {
125       printf("Nothing ever goes my way!\n");
126       close(fd);
127       exit(-1);
128    } else {
129       printf("Soon, I will be able to take over the world!\n");
130       fgets(str, 200, fp);
131       printf("%s\n", str);
132       fclose(fp);
133    }
134 #endif
135 }
136
137
138 rtems_task Init(
139   rtems_task_argument ignored
140 )
141 {
142         int err;
143
144          printf("Starting\n");
145
146         err = filesystem_init(YPATH);
147
148         printf("filesystem_init(\"%s\") returned %d\n", YPATH, err);
149
150         run_basic_file_test();
151
152
153    exit(0);
154 }
155
156
157
158
159 #if 0
160 So basically, we are registering our NAND-specific callbacks with YAFFS
161 and registering the RTEMS-YAFFS filesystem callbacks with RTEMS.
162 The rtems_filesystem_register() associates the mount() system call with
163 a callback function to handle that system call, in this case
164 rtems_yaffs_mount_handler().  rtems_yaffs_mount_handler() and
165 RTEMS_FILESYSTEM_TYPE_YAFFS (just a string) are provided
166 by the rtems-yaffs fork.
167
168 mount_and_make_target_path() is provided by RTEMS: it combines a
169 mkdir -p` with mount(), passing the mount_args to the
170 previously-registered handler.
171 #endif
172
173
174 /* configuration information */
175
176 /* NOTICE: the clock driver is explicitly disabled */
177 #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
178 #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
179
180 #define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
181 #define CONFIGURE_MAXIMUM_FILE_DESCRIPTORS 6
182
183 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
184 #define CONFIGURE_MAXIMUM_TASKS 1
185
186 #define CONFIGURE_MAXIMUM_SEMAPHORES        20
187
188 #define CONFIGURE_INIT
189
190 #include <rtems/confdefs.h>
191 /* end of file */