1 /* This FileX test concentrates on the file seek 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              128*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 static UCHAR                  buffer[128];
32 
33 /* Define thread prototypes.  */
34 
35 void    filex_file_seek_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_file_seek_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:   File seek test.........................................");
98 
99     /* Format the media.  This needs to be done before opening it!  */
100     status =  fx_media_format(&ram_disk,
101                             _fx_ram_driver,         // Driver entry
102                             ram_disk_memory,        // RAM disk memory pointer
103                             cache_buffer,           // Media buffer pointer
104                             CACHE_SIZE,             // Media buffer size
105                             "MY_RAM_DISK",          // Volume Name
106                             1,                      // Number of FATs
107                             32,                     // Directory Entries
108                             0,                      // Hidden sectors
109                             512,                    // Total sectors
110                             128,                    // Sector size
111                             1,                      // Sectors per cluster
112                             1,                      // Heads
113                             1);                     // Sectors per track
114 
115     /* Determine if the format had an error.  */
116     if (status)
117     {
118 
119         printf("ERROR!\n");
120         test_control_return(2);
121     }
122 
123     /* Attempt to seek before the media is opened to generate an error */
124     status = fx_file_extended_seek(&my_file, 0);
125     if (status != FX_NOT_OPEN)
126     {
127         printf("ERROR!\n");
128         test_control_return(2);
129     }
130 
131     /* Attempt to seek before the media is opened to generate an error */
132     status = fx_file_extended_relative_seek(&my_file, 0, 0);
133     if (status != FX_NOT_OPEN)
134     {
135         printf("ERROR!\n");
136         test_control_return(3);
137     }
138 
139     /* Open the ram_disk.  */
140     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
141 
142     /* Check the status.  */
143     if (status != FX_SUCCESS)
144     {
145 
146         /* Error, return error code.  */
147         printf("ERROR!\n");
148         test_control_return(4);
149     }
150 
151     /* Create a file called TEST.TXT in the root directory.  */
152     status =  fx_file_create(&ram_disk, "TEST.TXT");
153 
154     /* Check the create status.  */
155     if (status != FX_SUCCESS)
156     {
157 
158         printf("ERROR!\n");
159         test_control_return(5);
160     }
161 
162     /* Open the test file.  */
163     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
164 
165     /* Check the file open status.  */
166     if (status != FX_SUCCESS)
167     {
168 
169         printf("ERROR!\n");
170         test_control_return(6);
171     }
172 
173     /* Attempt to seek before the media is opened to generate an error */
174     status = fx_file_extended_relative_seek(&my_file, 0xFFFFFFFFFFFFFFFF, FX_SEEK_BACK);
175     if (status != FX_SUCCESS)
176     {
177         printf("ERROR!\n");
178         test_control_return(7);
179     }
180 
181 /* test error checking */
182 #ifndef FX_DISABLE_ERROR_CHECKING
183     /* send null pointer to generate an error */
184     status = fx_media_space_available(FX_NULL, FX_NULL);
185     if (status != FX_PTR_ERROR)
186     {
187         printf("ERROR!\n");
188         test_control_return(8);
189     }
190 
191     /* send an invalid option to generate an error */
192     status = fx_file_relative_seek(&my_file, 0xFFFFFFFF, 4);
193     if (status != FX_INVALID_OPTION)
194     {
195         printf("ERROR!\n");
196         test_control_return(9);
197     }
198 
199     /* send null pointer to generate an error */
200     status = fx_file_extended_seek(FX_NULL, 0xFF);
201     if (status != FX_PTR_ERROR)
202     {
203         printf("ERROR!\n");
204         test_control_return(10);
205     }
206 
207     /* send null pointer to generate an error */
208     status = fx_file_extended_relative_seek(FX_NULL, 0xFF, FX_SEEK_BEGIN);
209     if (status != FX_PTR_ERROR)
210     {
211         printf("ERROR!\n");
212         test_control_return(11);
213     }
214 
215     /* send null pointer to generate an error */
216     status = fx_file_extended_relative_seek(&my_file, 0xFF, 4);
217     if (status != FX_INVALID_OPTION)
218     {
219         printf("ERROR!\n");
220         test_control_return(12);
221     }
222 
223 #endif /* FX_DISABLE_ERROR_CHECKING */
224 
225     /* Pickup the available bytes in the media.  */
226     status =  fx_media_space_available(&ram_disk, &available_bytes);
227 
228     /* Check for available bytes error.  */
229     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
230     {
231 
232         printf("ERROR!\n");
233         test_control_return(13);
234     }
235 
236     /* Loop to write successive bytes out to the file.... to fill the media!  */
237     i =  0;
238     write_value =  0;
239     while (i < available_bytes)
240     {
241 
242         /* Write 4 bytes to the file.  */
243         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
244 
245         /* Check the file write status.  */
246         if (status != FX_SUCCESS)
247         {
248 
249             printf("ERROR!\n");
250             test_control_return(14);
251         }
252 
253         /* Increment byte count.  */
254         i =  i + sizeof(ULONG);
255 
256         /* Increment write value.  */
257         write_value++;
258     }
259 
260     /* Pickup the available bytes in the media again.  */
261     status =  fx_media_space_available(&ram_disk, &i);
262 
263     /* Check for available bytes error.  */
264     if ((status != FX_SUCCESS) || (i != 0))
265     {
266 
267         printf("ERROR!\n");
268         test_control_return(15);
269     }
270 
271 #ifndef FX_DISABLE_CACHE
272     /* At this point, we should invalidate the (which also flushes the cache) media to ensure that all
273        dirty sectors are written.  */
274     status =  fx_media_cache_invalidate(&ram_disk);
275 
276     /* Check for invalidate errors.  */
277     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
278     {
279 
280         printf("ERROR!\n");
281         test_control_return(16);
282     }
283 
284     /* See if any sectors are still valid in the cache.  */
285     for (i = 0; i < FX_MAX_SECTOR_CACHE; i++)
286     {
287 
288         /* Determine if this cache entry is still valid.  */
289         if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
290         {
291 
292             printf("ERROR!\n");
293             test_control_return(17);
294         }
295     }
296 #endif
297 
298     /* Seek to the beginning of the test file.  */
299     status =  fx_file_seek(&my_file, 0);
300 
301     /* Check the file seek status.  */
302     if (status != FX_SUCCESS)
303     {
304 
305         printf("ERROR!\n");
306         test_control_return(18);
307     }
308 
309     /* Read the 4 bytes at the front of the file... should be 0!  */
310     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
311 
312     /* Determine if it is correct.  */
313     if ((status) || (read_value != 0) || (actual != sizeof(ULONG)))
314     {
315 
316         printf("ERROR!\n");
317         test_control_return(19);
318     }
319 
320     /* Read the next 4 bytes at the front of the file... should be 1!  */
321     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
322 
323     /* Determine if it is correct.  */
324     if ((status) || (read_value != 1) || (actual != sizeof(ULONG)))
325     {
326 
327         printf("ERROR!\n");
328         test_control_return(20);
329     }
330 
331     /* Seek to near the last 4 bytes of the file.  */
332     status =  fx_file_seek(&my_file, available_bytes - 4);
333 
334     /* Read the last 4 bytes of the file...  should be available_bytes/sizeof(ULONG)!  */
335     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
336 
337     /* Determine if it is correct.  */
338     if ((status) || (read_value != (available_bytes/sizeof(ULONG) - 1)) || (actual != sizeof(ULONG)))
339     {
340 
341         printf("ERROR!\n");
342         test_control_return(21);
343     }
344 
345     /* Read the past the end of the file...  should get an error in this case.   */
346     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
347 
348     /* Determine if it is correct.  */
349     if (status != FX_END_OF_FILE)
350     {
351 
352         printf("ERROR!\n");
353         test_control_return(22);
354     }
355 
356     /* Seek to the middle of the file.  */
357     status =  fx_file_seek(&my_file, available_bytes/2);
358 
359     /* Read the middle 4 bytes of the file...  should be (available_bytes/2)/sizeof(ULONG)!  */
360     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
361 
362     /* Determine if it is correct.  */
363     if ((status) || (read_value != (available_bytes/(2*sizeof(ULONG)))) || (actual != sizeof(ULONG)))
364     {
365 
366         printf("ERROR!\n");
367         test_control_return(23);
368     }
369 
370     /* Seek to the end of the file.  */
371     status =  fx_file_seek(&my_file, 0xFFFFFFFF);
372 
373     /* Determine if it is correct.  */
374     if (status != FX_SUCCESS)
375     {
376 
377         printf("ERROR!\n");
378         test_control_return(24);
379     }
380 
381     /* Close the file.  */
382     status +=  fx_file_close(&my_file);
383 
384     /* Open the test file for fast reading.  */
385     status +=  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ_FAST);
386 
387     /* Check the file open status.  */
388     if (status != FX_SUCCESS)
389     {
390 
391         printf("ERROR!\n");
392         test_control_return(25);
393     }
394 
395     /* Read the 4 bytes at the front of the file... should be 0!  */
396     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
397 
398     /* Determine if it is correct.  */
399     if ((status) || (read_value != 0) || (actual != sizeof(ULONG)))
400     {
401 
402         printf("ERROR!\n");
403         test_control_return(26);
404     }
405 
406     /* Read the next 4 bytes at the front of the file... should be 1!  */
407     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
408 
409     /* Determine if it is correct.  */
410     if ((status) || (read_value != 1) || (actual != sizeof(ULONG)))
411     {
412 
413         printf("ERROR!\n");
414         test_control_return(27);
415     }
416 
417     /* Seek to near the last 4 bytes of the file.  */
418     status =  fx_file_seek(&my_file, available_bytes - 4);
419 
420     /* Read the last 4 bytes of the file...  should be available_bytes/sizeof(ULONG)!  */
421     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
422 
423     /* Determine if it is correct.  */
424     if ((status) || (read_value != (available_bytes/sizeof(ULONG) - 1)) || (actual != sizeof(ULONG)))
425     {
426 
427         printf("ERROR!\n");
428         test_control_return(28);
429     }
430 
431     /* Read the past the end of the file...  should get an error in this case.   */
432     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
433 
434     /* Determine if it is correct.  */
435     if (status != FX_END_OF_FILE)
436     {
437 
438         printf("ERROR!\n");
439         test_control_return(29);
440     }
441 
442     /* Seek to the middle of the file.  */
443     status =  fx_file_seek(&my_file, available_bytes/2);
444 
445     /* Read the middle 4 bytes of the file...  should be (available_bytes/2)/sizeof(ULONG)!  */
446     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
447 
448     /* Determine if it is correct.  */
449     if ((status) || (read_value != (available_bytes/(2*sizeof(ULONG)))) || (actual != sizeof(ULONG)))
450     {
451 
452         printf("ERROR!\n");
453         test_control_return(30);
454     }
455 
456     /* Seek to the end of the file.  */
457     status =  fx_file_seek(&my_file, 0xFFFFFFFF);
458 
459     /* Determine if it is correct.  */
460     if (status != FX_SUCCESS)
461     {
462 
463         printf("ERROR!\n");
464         test_control_return(31);
465     }
466 
467     /* Close the file.  */
468     status +=  fx_file_close(&my_file);
469 
470     /* Open the test file for reading.  */
471     status +=  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
472 
473     /* Check the file open status.  */
474     if (status != FX_SUCCESS)
475     {
476 
477         printf("ERROR!\n");
478         test_control_return(32);
479     }
480 
481 /* Only run this if error checking is enabled */
482 #ifndef FX_DISABLE_ERROR_CHECKING
483     /* send null pointer to generate an error */
484     status = fx_file_relative_seek(FX_NULL, 0, FX_SEEK_BEGIN);
485     if (status != FX_PTR_ERROR)
486     {
487         printf("ERROR!\n");
488         test_control_return(33);
489     }
490 #endif /* FX_DISABLE_ERROR_CHECKING */
491 
492     /* Read the 4 bytes at the front of the file... should be 0!  */
493     status =  fx_file_relative_seek(&my_file, 0, FX_SEEK_BEGIN);
494     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
495 
496     /* Determine if it is correct.  */
497     if ((status) || (read_value != 0) || (actual != sizeof(ULONG)))
498     {
499 
500         printf("ERROR!\n");
501         test_control_return(34);
502     }
503 
504     /* Read the next 4 bytes at the front of the file... should be 1!  */
505     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
506 
507     /* Determine if it is correct.  */
508     if ((status) || (read_value != 1) || (actual != sizeof(ULONG)))
509     {
510 
511         printf("ERROR!\n");
512         test_control_return(35);
513     }
514 
515     /* Seek to near the last 4 bytes of the file.  */
516     status =  fx_file_relative_seek(&my_file, 0xFFFFFFFF, FX_SEEK_BEGIN);
517     status += fx_file_relative_seek(&my_file, 4, FX_SEEK_BACK);
518 
519     /* Read the last 4 bytes of the file...  should be available_bytes/sizeof(ULONG)!  */
520     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
521 
522     /* Determine if it is correct.  */
523     if ((status) || (read_value != (available_bytes/sizeof(ULONG) - 1)) || (actual != sizeof(ULONG)))
524     {
525 
526         printf("ERROR!\n");
527         test_control_return(36);
528     }
529 
530     /* Read the past the end of the file...  should get an error in this case.   */
531     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
532 
533     /* Determine if it is correct.  */
534     if (status != FX_END_OF_FILE)
535     {
536 
537         printf("ERROR!\n");
538         test_control_return(37);
539     }
540 
541     /* Seek to the middle of the file.  */
542     status =  fx_file_relative_seek(&my_file, 0xFFFFFFFF, FX_SEEK_END);
543     status += fx_file_relative_seek(&my_file, available_bytes/2, FX_SEEK_FORWARD);
544 
545     /* Read the middle 4 bytes of the file...  should be (available_bytes/2)/sizeof(ULONG)!  */
546     status +=  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
547 
548     /* Determine if it is correct.  */
549     if ((status) || (read_value != (available_bytes/(2*sizeof(ULONG)))) || (actual != sizeof(ULONG)))
550     {
551 
552         printf("ERROR!\n");
553         test_control_return(38);
554     }
555 
556     /* Seek to the end of the file.  */
557     status =  fx_file_relative_seek(&my_file, 0, FX_SEEK_END);
558 
559     /* Determine if it is correct.  */
560     if (status != FX_SUCCESS)
561     {
562 
563         printf("ERROR!\n");
564         test_control_return(39);
565     }
566 
567     /* Close the file.  */
568     status +=  fx_file_close(&my_file);
569 
570     /* Close the media.  */
571     status +=  fx_media_close(&ram_disk);
572 
573     /* Determine if the test was successful.  */
574     if (status != FX_SUCCESS)
575     {
576 
577         printf("ERROR!\n");
578         test_control_return(40);
579     }
580 
581     /* Test corner cases of extended seek.  */
582 
583     /* Format the media.  This needs to be done before opening it!  */
584     status =  fx_media_format(&ram_disk,
585                             _fx_ram_driver,         // Driver entry
586                             ram_disk_memory,        // RAM disk memory pointer
587                             cache_buffer,           // Media buffer pointer
588                             CACHE_SIZE,             // Media buffer size
589                             "MY_RAM_DISK",          // Volume Name
590                             1,                      // Number of FATs
591                             32,                     // Directory Entries
592                             0,                      // Hidden sectors
593                             512,                    // Total sectors
594                             128,                    // Sector size
595                             1,                      // Sectors per cluster
596                             1,                      // Heads
597                             1);                     // Sectors per track
598 
599     /* Determine if the format had an error.  */
600     if (status)
601     {
602 
603         printf("ERROR!\n");
604         test_control_return(41);
605     }
606 
607     /* Open the ram_disk.  */
608     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
609 
610     /* Check the status.  */
611     if (status != FX_SUCCESS)
612     {
613 
614         /* Error, return error code.  */
615         printf("ERROR!\n");
616         test_control_return(42);
617     }
618 
619     /* Create a file called TEST.TXT in the root directory.  */
620     status =  fx_file_create(&ram_disk, "TEST.TXT");
621 
622     /* Check the create status.  */
623     if (status != FX_SUCCESS)
624     {
625 
626         printf("ERROR!\n");
627         test_control_return(43);
628     }
629 
630     /* Open the test file.  */
631     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
632 
633     /* Check the file open status.  */
634     if (status != FX_SUCCESS)
635     {
636 
637         printf("ERROR!\n");
638         test_control_return(44);
639     }
640 
641     /* Write data to the file.  */
642     status =  fx_file_write(&my_file, buffer, 128);
643     status += fx_file_write(&my_file, buffer, 128);
644     status += fx_file_write(&my_file, buffer, 128);
645     status += fx_file_write(&my_file, buffer, 128);
646 
647     status += fx_file_write(&my_file, buffer, 128);
648     status += fx_file_write(&my_file, buffer, 128);
649     status += fx_file_write(&my_file, buffer, 128);
650     status += fx_file_write(&my_file, buffer, 128);
651 
652     /* Check the status.  */
653     if (status != FX_SUCCESS)
654     {
655 
656         printf("ERROR!\n");
657         test_control_return(45);
658     }
659 
660     /* Seek to the beginning of the file.  */
661     status = fx_file_extended_seek(&my_file, 0);
662     if (status != FX_SUCCESS)
663     {
664         printf("ERROR!\n");
665         test_control_return(46);
666     }
667 
668     /* Seek to the second to beginning of the last cluster of the file.  */
669     status = fx_file_extended_seek(&my_file, 1024-128);
670     if (status != FX_SUCCESS)
671     {
672         printf("ERROR!\n");
673         test_control_return(47);
674     }
675 
676     /* Seek to the end of the file with an I/O error.  */
677     _fx_utility_fat_entry_read_error_request =  1;
678     status = fx_file_extended_seek(&my_file, 1024);
679     _fx_utility_fat_entry_read_error_request =  0;
680     if (status != FX_IO_ERROR)
681     {
682         printf("ERROR!\n");
683         test_control_return(48);
684     }
685 
686     /* Seek to the end of the file with a FAT entry of 1 error.  */
687     status =  fx_file_extended_seek(&my_file, 0);
688     _fx_utility_fat_entry_read_error_request =  10001;
689     status += fx_file_extended_seek(&my_file, 1024);
690     _fx_utility_fat_entry_read_error_request =  0;
691     if (status != FX_FILE_CORRUPT)
692     {
693         printf("ERROR!\n");
694         test_control_return(49);
695     }
696 
697     /* Seek to the end of the file with a FAT entry of max fat value error.  */
698     _fx_utility_fat_entry_read_error_request =  20001;
699     status = fx_file_extended_seek(&my_file, 1024);
700     _fx_utility_fat_entry_read_error_request =  0;
701     if (status != FX_FILE_CORRUPT)
702     {
703         printf("ERROR!\n");
704         test_control_return(50);
705     }
706 
707     /* Seek to the end of the file with a FAT entry of minimal value error.  */
708     _fx_utility_fat_entry_read_error_request =  10008;
709     status = fx_file_extended_seek(&my_file, 1024);
710     _fx_utility_fat_entry_read_error_request =  0;
711     if (status != FX_SUCCESS)
712     {
713         printf("ERROR!\n");
714         test_control_return(51);
715     }
716 
717     /* Close the file and the media.  */
718     status =  fx_file_close(&my_file);
719     status += fx_media_close(&ram_disk);
720     if (status != FX_SUCCESS)
721     {
722         printf("ERROR!\n");
723         test_control_return(52);
724     }
725 
726     /* Test zero divisor checking in fx_file_extended_seek.  */
727     /* Format the media.  This needs to be done before opening it!  */
728     status = fx_media_format(&ram_disk,
729         _fx_ram_driver,         // Driver entry
730         ram_disk_memory,        // RAM disk memory pointer
731         cache_buffer,           // Media buffer pointer
732         CACHE_SIZE,             // Media buffer size
733         "MY_RAM_DISK",          // Volume Name
734         1,                      // Number of FATs
735         32,                     // Directory Entries
736         0,                      // Hidden sectors
737         512,                    // Total sectors
738         128,                    // Sector size
739         1,                      // Sectors per cluster
740         1,                      // Heads
741         1);                     // Sectors per track
742 
743     /* Determine if the format had an error.  */
744     if (status)
745     {
746 
747         printf("ERROR!\n");
748         test_control_return(53);
749     }
750 
751     /* Open the ram_disk.  */
752     status = fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
753 
754     /* Check the status.  */
755     if (status != FX_SUCCESS)
756     {
757 
758         /* Error, return error code.  */
759         printf("ERROR!\n");
760         test_control_return(54);
761     }
762 
763     /* Create a file called TEST.TXT in the root directory.  */
764     status = fx_file_create(&ram_disk, "TEST.TXT");
765 
766     /* Check the create status.  */
767     if (status != FX_SUCCESS)
768     {
769 
770         printf("ERROR!\n");
771         test_control_return(55);
772     }
773 
774     /* Open the test file.  */
775     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
776 
777     /* Check the file open status.  */
778     if (status != FX_SUCCESS)
779     {
780 
781         printf("ERROR!\n");
782         test_control_return(56);
783     }
784 
785 
786     /* Check the status.  */
787     if (status != FX_SUCCESS)
788     {
789 
790         printf("ERROR!\n");
791         test_control_return(57);
792     }
793 
794     /* Corrupt the media.  */
795     ram_disk.fx_media_bytes_per_sector = 0;
796 
797     status = fx_file_extended_seek(&my_file, 1);
798 
799     if (status != FX_MEDIA_INVALID)
800     {
801         printf("ERROR!\n");
802         test_control_return(58);
803     }
804     ram_disk.fx_media_bytes_per_sector = 128;
805 
806     /* Write data to the file.  */
807     status = fx_file_write(&my_file, buffer, 128);
808 
809     /* Corrupt the media.  */
810     ram_disk.fx_media_sectors_per_FAT = 0;
811 
812     status = fx_file_write(&my_file, buffer, 128);
813 
814     if (status != FX_SUCCESS)
815     {
816         printf("ERROR!\n");
817         test_control_return(59);
818     }
819 
820     /* Close the file and the media.  */
821     status = fx_file_close(&my_file);
822 
823     /* Check the file open status.  */
824     if (status != FX_SUCCESS)
825     {
826 
827         printf("ERROR!\n");
828         test_control_return(60);
829     }
830 
831     /* Corrupt the media.  */
832     ram_disk.fx_media_bytes_per_sector = 0;
833 
834     /* Open the test file.  */
835     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
836 
837     /* Check the file open status.  */
838     if (status != FX_MEDIA_INVALID)
839     {
840 
841         printf("ERROR!\n");
842         test_control_return(56);
843     }
844     ram_disk.fx_media_bytes_per_sector = 128;
845 
846     status = fx_media_close(&ram_disk);
847 
848     if (status != FX_SUCCESS)
849     {
850         printf("ERROR!\n");
851         test_control_return(60);
852     }
853     else
854     {
855 
856         printf("SUCCESS!\n");
857         test_control_return(0);
858     }
859 }
860 
861