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