1 /* This FileX test concentrates on the media flush operation.  */
2 
3 #ifndef FX_STANDALONE_ENABLE
4 #include   "tx_api.h"
5 #endif
6 #include   "fx_api.h"
7 #include   <stdio.h>
8 #include   "fx_ram_driver_test.h"
9 
10 #define     DEMO_STACK_SIZE         4096
11 #define     CACHE_SIZE              16*128
12 
13 
14 /* Define the ThreadX and FileX object control blocks...  */
15 
16 #ifndef FX_STANDALONE_ENABLE
17 static TX_THREAD               ftest_0;
18 #endif
19 static FX_MEDIA                ram_disk;
20 static FX_FILE                 my_file;
21 
22 
23 /* Define the counters used in the test application...  */
24 
25 #ifndef FX_STANDALONE_ENABLE
26 static UCHAR                  *ram_disk_memory;
27 static UCHAR                  *cache_buffer;
28 #else
29 static UCHAR                   cache_buffer[CACHE_SIZE];
30 #endif
31 
32 
33 /* Define thread prototypes.  */
34 
35 void    filex_media_flush_application_define(void *first_unused_memory);
36 static void    ftest_0_entry(ULONG thread_input);
37 
38 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
39 void  test_control_return(UINT status);
40 
41 
42 
43 /* Define what the initial system looks like.  */
44 
45 #ifdef CTEST
test_application_define(void * first_unused_memory)46 void test_application_define(void *first_unused_memory)
47 #else
48 void    filex_media_flush_application_define(void *first_unused_memory)
49 #endif
50 {
51 
52 #ifndef FX_STANDALONE_ENABLE
53 UCHAR    *pointer;
54 
55 
56     /* Setup the working pointer.  */
57     pointer =  (UCHAR *) first_unused_memory;
58 
59     /* Create the main thread.  */
60     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
61             pointer, DEMO_STACK_SIZE,
62             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
63 
64     pointer =  pointer + DEMO_STACK_SIZE;
65 
66     /* Setup memory for the RAM disk and the sector cache.  */
67     cache_buffer =  pointer;
68     pointer =  pointer + CACHE_SIZE;
69     ram_disk_memory =  pointer;
70 
71 #endif
72 
73     /* Initialize the FileX system.  */
74     fx_system_initialize();
75 #ifdef FX_STANDALONE_ENABLE
76     ftest_0_entry(0);
77 #endif
78 }
79 
80 
81 
82 /* Define the test threads.  */
83 
ftest_0_entry(ULONG thread_input)84 static void    ftest_0_entry(ULONG thread_input)
85 {
86 
87 UINT        status;
88 ULONG       actual;
89 ULONG       read_value;
90 ULONG       write_value;
91 ULONG       available_bytes;
92 ULONG       i;
93 
94     FX_PARAMETER_NOT_USED(thread_input);
95 
96     /* Print out some test information banners.  */
97     printf("FileX Test:   Media flush test.......................................");
98 
99 /* Only run this if error checking is enabled */
100 #ifndef FX_DISABLE_ERROR_CHECKING
101     /* send null pointer to generate an error */
102     status = fx_media_format(FX_NULL, _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE, "MY_RAM_DISK", 1, 32, 0, 256, 128, 1, 1, 1);
103     if (status != FX_PTR_ERROR)
104     {
105         printf("ERROR!\n");
106         test_control_return(11);
107     }
108 #endif /* FX_DISABLE_ERROR_CHECKING */
109 
110     /* Format the media.  This needs to be done before opening it!  */
111     status =  fx_media_format(&ram_disk,
112                             _fx_ram_driver,         // Driver entry
113                             ram_disk_memory,        // RAM disk memory pointer
114                             cache_buffer,           // Media buffer pointer
115                             CACHE_SIZE,             // Media buffer size
116                             "MY_RAM_DISK",          // Volume Name
117                             1,                      // Number of FATs
118                             32,                     // Directory Entries
119                             0,                      // Hidden sectors
120                             256,                    // Total sectors
121                             128,                    // Sector size
122                             1,                      // Sectors per cluster
123                             1,                      // Heads
124                             1);                     // Sectors per track
125 
126     /* Determine if the format had an error.  */
127     if (status)
128     {
129 
130         printf("ERROR!\n");
131         test_control_return(2);
132     }
133 
134     /* try to flush the media before it is opened */
135     status = fx_media_flush(&ram_disk);
136     if (status != FX_MEDIA_NOT_OPEN)
137     {
138         printf("ERROR!\n");
139         test_control_return(11);
140     }
141 
142     /* Open the ram_disk.  */
143     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
144 
145     /* Check the status.  */
146     if (status != FX_SUCCESS)
147     {
148 
149         /* Error, return error code.  */
150         printf("ERROR!\n");
151         test_control_return(21);
152     }
153 
154     /* try to flush the media while it is write protected */
155     ram_disk.fx_media_driver_write_protect = FX_TRUE;
156     status = fx_media_flush(&ram_disk);
157     if (status != FX_WRITE_PROTECT)
158     {
159         printf("ERROR!\n");
160         test_control_return(11);
161     }
162     ram_disk.fx_media_driver_write_protect = FX_FALSE;
163 
164     /* Create a file called TEST.TXT in the root directory.  */
165     status =  fx_file_create(&ram_disk, "TEST.TXT");
166 
167     /* Check the create status.  */
168     if (status != FX_SUCCESS)
169     {
170 
171         printf("ERROR!\n");
172         test_control_return(3);
173     }
174 
175     /* Open the test file.  */
176     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
177 
178     /* Check the file open status.  */
179     if (status != FX_SUCCESS)
180     {
181 
182         printf("ERROR!\n");
183         test_control_return(4);
184     }
185 
186     /* Pickup the available bytes in the media.  */
187     status =  fx_media_space_available(&ram_disk, &available_bytes);
188 
189     /* Check for available bytes error.  */
190     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
191     {
192 
193         printf("ERROR!\n");
194         test_control_return(5);
195     }
196 
197     /* Loop to write successive bytes out to the file.... to fill the media!  */
198     i =  0;
199     write_value =  0;
200     while (i < available_bytes)
201     {
202 
203         /* Write 4 bytes to the file.  */
204         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
205 
206         /* Check the file write status.  */
207         if (status != FX_SUCCESS)
208         {
209 
210             printf("ERROR!\n");
211             test_control_return(6);
212         }
213 
214         /* Increment byte count.  */
215         i =  i + sizeof(ULONG);
216 
217         /* Increment write value.  */
218         write_value++;
219     }
220 
221     /* Pickup the available bytes in the media again.  */
222     status =  fx_media_space_available(&ram_disk, &i);
223 
224     /* Check for available bytes error.  */
225     if ((status != FX_SUCCESS) || (i != 0))
226     {
227 
228         printf("ERROR!\n");
229         test_control_return(7);
230     }
231 
232 /* Only run this if error checking is enabled */
233 #ifndef FX_DISABLE_ERROR_CHECKING
234     /* send null pointer to generate an error */
235     status = fx_media_flush(FX_NULL);
236     if (status != FX_PTR_ERROR)
237     {
238         printf("ERROR!\n");
239         test_control_return(11);
240     }
241 #endif /* FX_DISABLE_ERROR_CHECKING */
242 
243     /* At this point, we should flush the media to ensure that all
244        dirty sectors are written.  */
245     status =  fx_media_flush(&ram_disk);
246 
247 #ifndef FX_DISABLE_CACHE
248     /* Check for flush errors.  */
249     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
250     {
251 
252         printf("ERROR!\n");
253         test_control_return(8);
254     }
255 #endif
256 
257     /* Seek to the beginning of the test file.  */
258     status =  fx_file_seek(&my_file, 0);
259 
260     /* Check the file seek status.  */
261     if (status != FX_SUCCESS)
262     {
263 
264         printf("ERROR!\n");
265         test_control_return(9);
266     }
267 
268     /* Now read in all the bytes again to make sure the file contents are really there.  */
269     i =  0;
270     read_value =  0;
271     while (i < available_bytes)
272     {
273 
274         /* Read 4 bytes from the file.  */
275         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
276 
277         /* Check the file read status.  */
278         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
279         {
280 
281             printf("ERROR!\n");
282             test_control_return(10);
283         }
284 
285         /* Increment byte count.  */
286         i =  i + sizeof(ULONG);
287     }
288 
289     /* Close the test file.  */
290     status =  fx_file_close(&my_file);
291 
292     /* Check the file close status.  */
293     if (status != FX_SUCCESS)
294     {
295 
296         printf("ERROR!\n");
297         test_control_return(11);
298     }
299 
300     /* Close the media.  */
301     status =  fx_media_close(&ram_disk);
302 
303     /* Check the media close status.  */
304     if (status != FX_SUCCESS)
305     {
306 
307         printf("ERROR!\n");
308         test_control_return(12);
309     }
310 
311     /* Reformat the media.  This needs to be done before opening it!  */
312     status =  fx_media_format(&ram_disk,
313                             _fx_ram_driver,         // Driver entry
314                             ram_disk_memory,        // RAM disk memory pointer
315                             cache_buffer,           // Media buffer pointer
316                             CACHE_SIZE,             // Media buffer size
317                             "MY_RAM_DISK",          // Volume Name
318                             1,                      // Number of FATs
319                             32,                     // Directory Entries
320                             0,                      // Hidden sectors
321                             256,                    // Total sectors
322                             128,                    // Sector size
323                             1,                      // Sectors per cluster
324                             1,                      // Heads
325                             1);                     // Sectors per track
326 
327     /* Determine if the format had an error.  */
328     if (status)
329     {
330 
331         printf("ERROR!\n");
332         test_control_return(13);
333     }
334 
335     /* Open the ram_disk, but do so to ensure non-hashed algorithm is used by supplying CACHE_SIZE-1.  */
336     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE-1);
337 
338     /* Check the status.  */
339     if (status != FX_SUCCESS)
340     {
341 
342         /* Error, return error code.  */
343         printf("ERROR!\n");
344         test_control_return(14);
345     }
346 
347     /* Create a file called TEST.TXT in the root directory.  */
348     status =  fx_file_create(&ram_disk, "TEST.TXT");
349 
350     /* Check the create status.  */
351     if (status != FX_SUCCESS)
352     {
353 
354         printf("ERROR!\n");
355         test_control_return(15);
356     }
357 
358     /* Open the test file.  */
359     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
360 
361     /* Check the file open status.  */
362     if (status != FX_SUCCESS)
363     {
364 
365         printf("ERROR!\n");
366         test_control_return(16);
367     }
368 
369     /* Pickup the available bytes in the media.  */
370     status =  fx_media_space_available(&ram_disk, &available_bytes);
371 
372     /* Check for available bytes error.  */
373     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
374     {
375 
376         printf("ERROR!\n");
377         test_control_return(17);
378     }
379 
380     /* Loop to write successive bytes out to the file.... to fill the media!  */
381     i =  0;
382     write_value =  0;
383     while (i < available_bytes)
384     {
385 
386         /* Write 4 bytes to the file.  */
387         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
388 
389         /* Check the file write status.  */
390         if (status != FX_SUCCESS)
391         {
392 
393             printf("ERROR!\n");
394             test_control_return(18);
395         }
396 
397         /* Increment byte count.  */
398         i =  i + sizeof(ULONG);
399 
400         /* Increment write value.  */
401         write_value++;
402     }
403 
404     /* Pickup the available bytes in the media again.  */
405     status =  fx_media_space_available(&ram_disk, &i);
406 
407     /* Check for available bytes error.  */
408     if ((status != FX_SUCCESS) || (i != 0))
409     {
410 
411         printf("ERROR!\n");
412         test_control_return(19);
413     }
414 
415     /* At this point, we should flush the media to ensure that all
416        dirty sectors are written.  */
417     status =  fx_media_flush(&ram_disk);
418 
419 #ifndef FX_DISABLE_CACHE
420     /* Check for flush errors.  */
421     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
422     {
423 
424         printf("ERROR!\n");
425         test_control_return(20);
426     }
427 #endif
428 
429     /* Seek to the beginning of the test file.  */
430     status =  fx_file_seek(&my_file, 0);
431 
432     /* Check the file seek status.  */
433     if (status != FX_SUCCESS)
434     {
435 
436         printf("ERROR!\n");
437         test_control_return(22);
438     }
439 
440     /* Now read in all the bytes again to make sure the file contents are really there.  */
441     i =  0;
442     read_value =  0;
443     while (i < available_bytes)
444     {
445 
446         /* Read 4 bytes from the file.  */
447         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
448 
449         /* Check the file read status.  */
450         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
451         {
452 
453             printf("ERROR!\n");
454             test_control_return(23);
455         }
456 
457         /* Increment byte count.  */
458         i =  i + sizeof(ULONG);
459     }
460 
461     /* Close the test file.  */
462     status =  fx_file_close(&my_file);
463 
464     /* Check the file close status.  */
465     if (status != FX_SUCCESS)
466     {
467 
468         printf("ERROR!\n");
469         test_control_return(24);
470     }
471 
472     /* Close the media.  */
473     status =  fx_media_close(&ram_disk);
474 
475     /* Check the media close status.  */
476     if (status != FX_SUCCESS)
477     {
478 
479         printf("ERROR!\n");
480         test_control_return(25);
481     }
482 
483     /* Determine if the test was successful.  */
484     if (status != FX_SUCCESS)
485     {
486 
487         printf("ERROR!\n");
488         test_control_return(26);
489     }
490     else
491     {
492 
493         printf("SUCCESS!\n");
494         test_control_return(0);
495     }
496 }
497 
498