1 /* This FileX test concentrates on the file read/write operation.  */
2 
3 #ifndef FX_STANDALONE_ENABLE
4 #include   "tx_api.h"
5 #endif
6 #include   "fx_api.h"
7 #include   "fx_utility.h"
8 #include   "fx_ram_driver_test.h"
9 #include   <stdio.h>
10 
11 #define     DEMO_STACK_SIZE         4096
12 #define     CACHE_SIZE              128*128
13 
14 
15 /* Define the ThreadX and FileX object control blocks...  */
16 
17 #ifndef FX_STANDALONE_ENABLE
18 static TX_THREAD               ftest_0;
19 #endif
20 static FX_MEDIA                ram_disk;
21 static FX_FILE                 my_file;
22 static FX_FILE                 my_file1;
23 static FX_FILE                 my_file2;
24 static FX_FILE                 my_file3;
25 static FX_FILE                 my_file4;
26 static FX_FILE                 read_only;
27 static ULONG                   my_buffer[128];
28 static UCHAR                   fat_buffer[128];
29 static UCHAR                   buffer[128*3];
30 
31 
32 /* Define the counters used in the test application...  */
33 
34 #ifndef FX_STANDALONE_ENABLE
35 static UCHAR                  *ram_disk_memory;
36 static UCHAR                  *cache_buffer;
37 #else
38 static UCHAR                   cache_buffer[CACHE_SIZE];
39 #endif
40 
41 
42 /* Define thread prototypes.  */
43 
44 void    filex_file_read_write_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 void  test_control_return(UINT status);
49 
50 
51 
52 /* Define what the initial system looks like.  */
53 
54 #ifdef CTEST
test_application_define(void * first_unused_memory)55 void test_application_define(void *first_unused_memory)
56 #else
57 void    filex_file_read_write_application_define(void *first_unused_memory)
58 #endif
59 {
60 
61 #ifndef FX_STANDALONE_ENABLE
62 UCHAR    *pointer;
63 
64 
65     /* Setup the working pointer.  */
66     pointer =  (UCHAR *) first_unused_memory;
67 
68     /* Create the main thread.  */
69     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
70             pointer, DEMO_STACK_SIZE,
71             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
72 
73     pointer =  pointer + DEMO_STACK_SIZE;
74 
75     /* Setup memory for the RAM disk and the sector cache.  */
76     cache_buffer =  pointer;
77     pointer =  pointer + CACHE_SIZE;
78     ram_disk_memory =  pointer;
79 
80 #endif
81 
82     /* Initialize the FileX system.  */
83     fx_system_initialize();
84 #ifdef FX_STANDALONE_ENABLE
85     ftest_0_entry(0);
86 #endif
87 }
88 
89 
90 
91 /* Define the test threads.  */
92 
ftest_0_entry(ULONG thread_input)93 static void    ftest_0_entry(ULONG thread_input)
94 {
95 
96 UINT        status;
97 ULONG       actual;
98 ULONG       actual1;
99 ULONG       actual2;
100 ULONG       temp;
101 ULONG       temp1;
102 ULONG       read_value;
103 ULONG       write_value;
104 ULONG       available_bytes;
105 ULONG       i, j;
106 
107     FX_PARAMETER_NOT_USED(thread_input);
108 
109     /* Print out some test information banners.  */
110     printf("FileX Test:   File read/write test...................................");
111 
112     /* Format the media.  This needs to be done before opening it!  */
113     status =  fx_media_format(&ram_disk,
114                             _fx_ram_driver,         // Driver entry
115                             ram_disk_memory,        // RAM disk memory pointer
116                             cache_buffer,           // Media buffer pointer
117                             CACHE_SIZE,             // Media buffer size
118                             "MY_RAM_DISK",          // Volume Name
119                             1,                      // Number of FATs
120                             32,                     // Directory Entries
121                             0,                      // Hidden sectors
122                             511,                    // Total sectors
123                             128,                    // Sector size
124                             1,                      // Sectors per cluster
125                             1,                      // Heads
126                             1);                     // Sectors per track
127 
128     /* Determine if the format had an error.  */
129     if (status)
130     {
131 
132         printf("ERROR!\n");
133         test_control_return(2);
134     }
135 
136     /* Open the ram_disk.  */
137     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
138 
139     /* Check the status.  */
140     if (status != FX_SUCCESS)
141     {
142 
143         /* Error, return error code.  */
144         printf("ERROR!\n");
145         test_control_return(21);
146     }
147 
148     /* Create a file called TEST.TXT in the root directory.  */
149     status =  fx_file_create(&ram_disk, "TEST.TXT");
150     status += fx_file_create(&ram_disk, "READ_ONLY.TXT");
151 
152     /* Check the create status.  */
153     if (status != FX_SUCCESS)
154     {
155 
156         printf("ERROR!\n");
157         test_control_return(3);
158     }
159 
160     /* try to write to a file before it has been opened  */
161     status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
162     if (status != FX_NOT_OPEN)
163     {
164         printf("ERROR!\n");
165         test_control_return(23);
166     }
167 
168     /* Open the test file.  */
169     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
170     status += fx_file_open(&ram_disk, &read_only, "READ_ONLY.TXT", FX_OPEN_FOR_READ);
171 
172     /* Check the file open status.  */
173     if (status != FX_SUCCESS)
174     {
175 
176         printf("ERROR!\n");
177         test_control_return(4);
178     }
179 
180     /* try to write to a file while it is write protected  */
181     ram_disk.fx_media_driver_write_protect = FX_TRUE;
182     status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
183     if (status != FX_WRITE_PROTECT)
184     {
185         printf("ERROR!\n");
186         test_control_return(23);
187     }
188     ram_disk.fx_media_driver_write_protect = FX_FALSE;
189 
190     /* try to write to a file that is not opened for writing  */
191     status =  fx_file_write(&read_only, (void *) &write_value, sizeof(ULONG));
192     if (status != FX_ACCESS_ERROR)
193     {
194         printf("ERROR!\n");
195         test_control_return(23);
196     }
197 
198     ram_disk.fx_media_bytes_per_sector = 0;
199 
200     /* Try to write to a file when media is corrupted.  */
201     status = fx_file_write(&my_file, (void*)& write_value, sizeof(ULONG));
202     if (status != FX_MEDIA_INVALID)
203     {
204         printf("ERROR!\n");
205         test_control_return(23);
206     }
207 
208     ram_disk.fx_media_bytes_per_sector = 128;
209 
210     /* Pickup the available bytes in the media.  */
211     status =  fx_media_space_available(&ram_disk, &available_bytes);
212 
213     /* Check for available bytes error.  */
214     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
215     {
216 
217         printf("ERROR!\n");
218         test_control_return(5);
219     }
220 
221 /* Only run this if error checking is enabled */
222 #ifndef FX_DISABLE_ERROR_CHECKING
223     /* send null pointer to generate an error */
224     status = fx_file_write(FX_NULL, (void *) &write_value, 0);
225     if (status != FX_PTR_ERROR)
226     {
227         printf("ERROR!\n");
228         test_control_return(11);
229     }
230 #endif /* FX_DISABLE_ERROR_CHECKING */
231 
232     /* Loop to write successive bytes out to the file.... to fill the media!  */
233     i =  0;
234     write_value =  0;
235     while (i < available_bytes)
236     {
237 
238         /* Write 4 bytes to the file.  */
239         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
240 
241         /* Check the file write status.  */
242         if (status != FX_SUCCESS)
243         {
244 
245             printf("ERROR!\n");
246             test_control_return(6);
247         }
248 
249         /* Increment byte count.  */
250         i =  i + sizeof(ULONG);
251 
252         /* Increment write value.  */
253         write_value++;
254     }
255 
256     /* Pickup the available bytes in the media again.  */
257     status =  fx_media_space_available(&ram_disk, &i);
258 
259     /* Check for available bytes error.  */
260     if ((status != FX_SUCCESS) || (i != 0))
261     {
262 
263         printf("ERROR!\n");
264         test_control_return(7);
265     }
266 
267 #ifndef FX_DISABLE_CACHE
268     /* At this point, we should invalidate the (which also flushes the cache) media to ensure that all
269        dirty sectors are written.  */
270     status =  fx_media_cache_invalidate(&ram_disk);
271 
272     /* Check for flush errors.  */
273     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
274     {
275 
276         printf("ERROR!\n");
277         test_control_return(8);
278     }
279 
280     /* See if any sectors are still valid in the cache.  */
281     for (i = 0; i < FX_MAX_SECTOR_CACHE; i++)
282     {
283 
284         /* Determine if this cache entry is still valid.  */
285         if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
286         {
287 
288             printf("ERROR!\n");
289             test_control_return(81);
290         }
291     }
292 #endif
293 
294     /* Seek to the beginning of the test file.  */
295     status =  fx_file_seek(&my_file, 0);
296 
297     /* Check the file seek status.  */
298     if (status != FX_SUCCESS)
299     {
300 
301         printf("ERROR!\n");
302         test_control_return(9);
303     }
304 
305 /* Only run this if error checking is enabled */
306 #ifndef FX_DISABLE_ERROR_CHECKING
307     /* send null pointer to generate an error */
308     status = fx_file_seek(FX_NULL, 0);
309     if (status != FX_PTR_ERROR)
310     {
311         printf("ERROR!\n");
312         test_control_return(11);
313     }
314 #endif /* FX_DISABLE_ERROR_CHECKING */
315 
316     /* Now read in all the bytes again to make sure the file contents are really there.  */
317     i =  0;
318     read_value =  0;
319     while (i < available_bytes)
320     {
321 
322         /* Read 4 bytes from the file.  */
323         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
324 
325         /* Check the file read status.  */
326         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
327         {
328 
329             printf("ERROR!\n");
330             test_control_return(10);
331         }
332 
333         /* Increment byte count.  */
334         i =  i + sizeof(ULONG);
335     }
336 
337     /* Close the test file.  */
338     status =  fx_file_close(&my_file);
339 
340     /* Check the file close status.  */
341     if (status != FX_SUCCESS)
342     {
343 
344         printf("ERROR!\n");
345         test_control_return(11);
346     }
347 
348     /* Close the media.  */
349     status =  fx_media_close(&ram_disk);
350 
351     /* Check the media close status.  */
352     if (status != FX_SUCCESS)
353     {
354 
355         printf("ERROR!\n");
356         test_control_return(12);
357     }
358 
359     /* Reformat the media.  This needs to be done before opening it!  */
360     status =  fx_media_format(&ram_disk,
361                             _fx_ram_driver,         // Driver entry
362                             ram_disk_memory,        // RAM disk memory pointer
363                             cache_buffer,           // Media buffer pointer
364                             CACHE_SIZE,             // Media buffer size
365                             "MY_RAM_DISK",          // Volume Name
366                             1,                      // Number of FATs
367                             32,                     // Directory Entries
368                             0,                      // Hidden sectors
369                             511,                    // Total sectors (ensure clusters divisible by 4)
370                             128,                    // Sector size
371                             1,                      // Sectors per cluster
372                             1,                      // Heads
373                             1);                     // Sectors per track
374 
375     /* Determine if the format had an error.  */
376     if (status)
377     {
378 
379         printf("ERROR!\n");
380         test_control_return(13);
381     }
382 
383     /* Open the ram_disk, but do so to ensure non-hashed algorithm is used by supplying CACHE_SIZE-1.  */
384     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE-1);
385 
386     /* Check the status.  */
387     if (status != FX_SUCCESS)
388     {
389 
390         /* Error, return error code.  */
391         printf("ERROR!\n");
392         test_control_return(14);
393     }
394 
395     /* Read 4 bytes from the file.  */
396     status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
397     if (status != FX_NOT_OPEN)
398     {
399         printf("ERROR!\n");
400         test_control_return(23);
401     }
402 
403     /* Create a file called TEST.TXT in the root directory.  */
404     status =  fx_file_create(&ram_disk, "TEST.TXT");
405 
406     /* Check the create status.  */
407     if (status != FX_SUCCESS)
408     {
409 
410         printf("ERROR!\n");
411         test_control_return(15);
412     }
413 
414     /* Open the test file.  */
415     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
416 
417     /* Check the file open status.  */
418     if (status != FX_SUCCESS)
419     {
420 
421         printf("ERROR!\n");
422         test_control_return(16);
423     }
424 
425     /* Pickup the available bytes in the media.  */
426     status =  fx_media_space_available(&ram_disk, &available_bytes);
427 
428     /* Check for available bytes error.  */
429     if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
430     {
431 
432         printf("ERROR!\n");
433         test_control_return(17);
434     }
435 
436     /* Loop to write successive bytes out to the file.... to fill the media!  */
437     i =  0;
438     write_value =  0;
439     while (i < available_bytes)
440     {
441 
442         /* Write 4 bytes to the file.  */
443         status =  fx_file_write(&my_file, (void *) &write_value, sizeof(ULONG));
444 
445         /* Check the file write status.  */
446         if (status != FX_SUCCESS)
447         {
448 
449             printf("ERROR!\n");
450             test_control_return(18);
451         }
452 
453         /* Increment byte count.  */
454         i =  i + sizeof(ULONG);
455 
456         /* Increment write value.  */
457         write_value++;
458     }
459 
460     /* Pickup the available bytes in the media again.  */
461     status =  fx_media_space_available(&ram_disk, &i);
462 
463     /* Check for available bytes error.  */
464     if ((status != FX_SUCCESS) || (i != 0))
465     {
466 
467         printf("ERROR!\n");
468         test_control_return(19);
469     }
470 
471 #ifndef FX_DISABLE_CACHE
472     /* At this point, we should invalidate the media to ensure that all
473        dirty sectors are written.  */
474     status =  fx_media_cache_invalidate(&ram_disk);
475 
476     /* Check for flush errors.  */
477     if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
478     {
479 
480         printf("ERROR!\n");
481         test_control_return(20);
482     }
483 
484     /* See if any sectors are still valid in the cache.  */
485     for (i = 0; i < ram_disk.fx_media_sector_cache_size; i++)
486     {
487 
488         /* Determine if this cache entry is still valid.  */
489         if (ram_disk.fx_media_sector_cache[i].fx_cached_sector_valid)
490         {
491 
492             printf("ERROR!\n");
493             test_control_return(81);
494         }
495     }
496 #endif
497 
498     /* Seek to the beginning of the test file.  */
499     status =  fx_file_seek(&my_file, 0);
500 
501     /* Check the file seek status.  */
502     if (status != FX_SUCCESS)
503     {
504 
505         printf("ERROR!\n");
506         test_control_return(22);
507     }
508 
509     /* Now read in all the bytes again to make sure the file contents are really there.  */
510     i =  0;
511     read_value =  0;
512     while (i < available_bytes)
513     {
514 
515         /* Read 4 bytes from the file.  */
516         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
517 
518         /* Check the file read status.  */
519         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
520         {
521 
522             printf("ERROR!\n");
523             test_control_return(23);
524         }
525 
526         /* Increment byte count.  */
527         i =  i + sizeof(ULONG);
528     }
529 
530     /* Close the test file.  */
531     status =  fx_file_close(&my_file);
532 
533     /* Check the file close status.  */
534     if (status != FX_SUCCESS)
535     {
536 
537         printf("ERROR!\n");
538         test_control_return(24);
539     }
540 
541     /* Close the media.  */
542     status =  fx_media_close(&ram_disk);
543 
544     /* Open the ram_disk.  */
545     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
546 
547     /* Check the status.  */
548     if (status != FX_SUCCESS)
549     {
550 
551         /* Error, return error code.  */
552         printf("ERROR!\n");
553         test_control_return(25);
554     }
555 
556     /* Open the file.  */
557     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
558 
559     /* Check the file open status.  */
560     if (status != FX_SUCCESS)
561     {
562 
563         /* Error opening file.  Return to caller.  */
564         printf("ERROR!\n");
565         test_control_return(26);
566     }
567 
568     /* Now read in all the bytes again to make sure the file contents are really there.  */
569     i =  0;
570     read_value =  0;
571     while (i < available_bytes)
572     {
573 
574         /* Read as much as 4 sectors full of bytes from the file.  */
575         status =  fx_file_read(&my_file, (void *) my_buffer, sizeof(my_buffer), &actual);
576 
577         /* Check the file read status.  */
578         if (status != FX_SUCCESS)
579         {
580 
581             printf("ERROR!\n");
582             test_control_return(27);
583         }
584 
585         /* Determine if the contents are what is expected.  */
586         for (j = 0; j < actual/sizeof(ULONG); j++)
587         {
588 
589             /* Determine if the buffer is correct.  */
590             if (read_value != my_buffer[j])
591             {
592 
593                 printf("ERROR!\n");
594                 test_control_return(28);
595             }
596 
597             read_value++;
598         }
599 
600         /* Increment byte count.  */
601         i =  i + actual;
602     }
603 
604     /* Close the file.  */
605     status =  fx_file_close(&my_file);
606 
607     /* Open the file again but with the fast option.  */
608     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ_FAST);
609 
610     /* Check the file open status.  */
611     if (status != FX_SUCCESS)
612     {
613 
614         /* Error opening file.  Return to caller.  */
615         printf("ERROR!\n");
616         test_control_return(29);
617     }
618 
619     /* Now read in all the bytes again to make sure the file contents are really there.  */
620     i =  0;
621     read_value =  0;
622     while (i < available_bytes)
623     {
624 
625         /* Read as much as 4 sectors full of bytes from the file.  */
626         status =  fx_file_read(&my_file, (void *) my_buffer, sizeof(my_buffer), &actual);
627 
628         /* Check the file read status.  */
629         if (status != FX_SUCCESS)
630         {
631 
632             printf("ERROR!\n");
633             test_control_return(30);
634         }
635 
636         /* Determine if the contents are what is expected.  */
637         for (j = 0; j < actual/sizeof(ULONG); j++)
638         {
639 
640             /* Determine if the buffer is correct.  */
641             if (read_value != my_buffer[j])
642             {
643 
644                 printf("ERROR!\n");
645                 test_control_return(31);
646             }
647 
648             read_value++;
649         }
650 
651         /* Increment byte count.  */
652         i =  i + actual;
653     }
654 
655     /* Close the file.  */
656     status =  fx_file_close(&my_file);
657 
658     /* Check the file close status.  */
659     if (status != FX_SUCCESS)
660     {
661 
662         printf("ERROR!\n");
663         test_control_return(32);
664     }
665 
666     /* Delete the file.  */
667     status =  fx_file_delete(&ram_disk, "TEST.TXT");
668 
669     /* Check the file delete status.  */
670     if (status != FX_SUCCESS)
671     {
672 
673         printf("ERROR!\n");
674         test_control_return(33);
675     }
676 
677     /* Write the file in blocks and then read ulong at a time.  */
678     status =  fx_file_create(&ram_disk, "TEST.TXT");
679     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
680 
681     /* Check the file open status.  */
682     if (status != FX_SUCCESS)
683     {
684 
685         printf("ERROR!\n");
686         test_control_return(34);
687     }
688 
689     /* Now write the big buffer at the same time.  */
690     /* Now read in all the bytes again to make sure the file contents are really there.  */
691     i =  0;
692     read_value =  0;
693     while (i < available_bytes)
694     {
695 
696         /* Build the buffer.  */
697         for (j = 0; j < actual/sizeof(ULONG); j++)
698         {
699 
700             /* Build the buffer entry.  */
701             my_buffer[j] =  read_value++;
702         }
703 
704 
705         /* Write 4 sectors at a time.  */
706         status =  fx_file_write(&my_file, (void *) my_buffer, sizeof(my_buffer));
707 
708         /* Check the file write status.  */
709         if (status != FX_SUCCESS)
710         {
711 
712             printf("ERROR!\n");
713             test_control_return(35);
714         }
715 
716         /* Increment byte count.  */
717         i =  i + sizeof(my_buffer);
718     }
719 
720     /* At this point, seek to the beginning of the file and read every 4 bytes.  */
721     status =  fx_file_seek(&my_file, 0);
722 
723     i =  0;
724     read_value =  0;
725     while (i < available_bytes)
726     {
727 
728         /* Read 4 bytes from the file.  */
729         status =  fx_file_read(&my_file, (void *) &read_value, sizeof(ULONG), &actual);
730 
731         /* Check the file read status.  */
732         if ((status != FX_SUCCESS) || (actual != 4) || (read_value != i/4))
733         {
734 
735             printf("ERROR!\n");
736             test_control_return(36);
737         }
738 
739         /* Increment byte count.  */
740         i =  i + sizeof(ULONG);
741     }
742 
743     /* Close the file.  */
744     status +=  fx_file_close(&my_file);
745 
746     /* Close the media.  */
747     status +=  fx_media_close(&ram_disk);
748 
749     /* Determine if the test was successful.  */
750     if (status != FX_SUCCESS)
751     {
752 
753         printf("ERROR!\n");
754         test_control_return(37);
755     }
756 
757     /* Test the update of files open for reading while the write is happening.  */
758 
759     /* Format the media.  This needs to be done before opening it!  */
760     status =  fx_media_format(&ram_disk,
761                             _fx_ram_driver,         // Driver entry
762                             ram_disk_memory,        // RAM disk memory pointer
763                             cache_buffer,           // Media buffer pointer
764                             CACHE_SIZE,             // Media buffer size
765                             "MY_RAM_DISK",          // Volume Name
766                             1,                      // Number of FATs
767                             32,                     // Directory Entries
768                             0,                      // Hidden sectors
769                             6000,                   // Total sectors - FAT16
770                             128,                    // Sector size
771                             1,                      // Sectors per cluster
772                             1,                      // Heads
773                             1);                     // Sectors per track
774 
775     /* Determine if the format had an error.  */
776     if (status)
777     {
778 
779         printf("ERROR!\n");
780         test_control_return(38);
781     }
782 
783     /* Open the ram_disk.  */
784     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
785 
786     /* Check the status.  */
787     if (status != FX_SUCCESS)
788     {
789 
790         /* Error, return error code.  */
791         printf("ERROR!\n");
792         test_control_return(39);
793     }
794 
795     /* Create a file called TEST.TXT in the root directory.  */
796     status =  fx_file_create(&ram_disk, "TEST.TXT");
797     status += fx_file_create(&ram_disk, "TEST1.TXT");
798     status += fx_file_create(&ram_disk, "TEST2.TXT");
799     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
800     status += fx_file_open(&ram_disk, &my_file1, "TEST.TXT", FX_OPEN_FOR_READ);
801     status += fx_file_open(&ram_disk, &my_file2, "TEST.TXT", FX_OPEN_FOR_WRITE);
802     status += fx_file_open(&ram_disk, &my_file3, "TEST1.TXT", FX_OPEN_FOR_WRITE);
803     status += fx_file_open(&ram_disk, &my_file4, "TEST2.TXT", FX_OPEN_FOR_WRITE);
804     status += fx_file_write(&my_file4, my_buffer, 128);
805 
806     /* Now loop through the maximum of clusters to fill and read the file.  */
807     i =  0;
808     while (ram_disk.fx_media_available_clusters)
809     {
810 
811         if (i == 4)
812         {
813             status += fx_file_close(&my_file4);
814             status += fx_file_delete(&ram_disk, "TEST2.TXT");
815         }
816 
817         /* Write to the writable file.  */
818         status +=  fx_file_write(&my_file2, my_buffer, 128);
819 
820         /* Read the data in the file from the other 2 file handles.  */
821         status +=  fx_file_read(&my_file1, my_buffer, 128, &actual1);
822         status +=  fx_file_read(&my_file, my_buffer, 128, &actual2);
823 
824         /* Check the status.  */
825         if (status)
826             break;
827         i++;
828     }
829 
830     /* Close the files and the media.  */
831     status += fx_file_close(&my_file);
832     status += fx_file_close(&my_file1);
833     status += fx_file_close(&my_file2);
834     status += fx_media_close(&ram_disk);
835 
836     /* Check the status.  */
837     if (status != FX_SUCCESS)
838     {
839 
840         /* Error, return error code.  */
841         printf("ERROR!\n");
842         test_control_return(40);
843     }
844 
845     /* Test the update of files open for reading while the write is happening with multiple sectors per cluster.  */
846 
847     /* Format the media.  This needs to be done before opening it!  */
848     status =  fx_media_format(&ram_disk,
849                             _fx_ram_driver,         // Driver entry
850                             ram_disk_memory,        // RAM disk memory pointer
851                             cache_buffer,           // Media buffer pointer
852                             CACHE_SIZE,             // Media buffer size
853                             "MY_RAM_DISK",          // Volume Name
854                             1,                      // Number of FATs
855                             32,                     // Directory Entries
856                             0,                      // Hidden sectors
857                             6000,                   // Total sectors - FAT16
858                             128,                    // Sector size
859                             2,                      // Sectors per cluster
860                             1,                      // Heads
861                             1);                     // Sectors per track
862 
863     /* Determine if the format had an error.  */
864     if (status)
865     {
866 
867         printf("ERROR!\n");
868         test_control_return(41);
869     }
870 
871     /* Open the ram_disk.  */
872     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
873 
874     /* Check the status.  */
875     if (status != FX_SUCCESS)
876     {
877 
878         /* Error, return error code.  */
879         printf("ERROR!\n");
880         test_control_return(42);
881     }
882 
883     /* Create a file called TEST.TXT in the root directory.  */
884     status =  fx_file_create(&ram_disk, "TEST.TXT");
885     status += fx_file_create(&ram_disk, "TEST1.TXT");
886     status += fx_file_create(&ram_disk, "TEST2.TXT");
887     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
888     status += fx_file_open(&ram_disk, &my_file1, "TEST.TXT", FX_OPEN_FOR_READ);
889     status += fx_file_open(&ram_disk, &my_file2, "TEST.TXT", FX_OPEN_FOR_WRITE);
890     status += fx_file_open(&ram_disk, &my_file3, "TEST1.TXT", FX_OPEN_FOR_WRITE);
891     status += fx_file_open(&ram_disk, &my_file4, "TEST2.TXT", FX_OPEN_FOR_WRITE);
892     status += fx_file_write(&my_file4, my_buffer, 128);
893 
894     /* Now loop through the maximum of clusters to fill and read the file.  */
895     i =  0;
896     while (ram_disk.fx_media_available_clusters)
897     {
898 
899         if (i == 4)
900         {
901             status += fx_file_close(&my_file4);
902             status += fx_file_delete(&ram_disk, "TEST2.TXT");
903         }
904 
905         /* Write to the writable file.  */
906         status +=  fx_file_write(&my_file2, my_buffer, 128);
907         status +=  fx_file_write(&my_file2, my_buffer, 128);
908 
909         /* Read the data in the file from the other 2 file handles.  */
910         status +=  fx_file_read(&my_file1, my_buffer, 128, &actual1);
911         status +=  fx_file_read(&my_file, my_buffer, 128, &actual2);
912         status +=  fx_file_read(&my_file1, my_buffer, 128, &actual1);
913         status +=  fx_file_read(&my_file, my_buffer, 128, &actual2);
914 
915         /* Check the status.  */
916         if (status)
917             break;
918         i++;
919     }
920 
921     /* Check the status.  */
922     if (status != FX_SUCCESS)
923     {
924 
925         /* Error, return error code.  */
926         printf("ERROR!\n");
927         test_control_return(43);
928     }
929 
930     /* Now force a wrap of the FAT search.  */
931     ram_disk.fx_media_available_clusters++;
932 
933     status =  fx_file_write(&my_file2, my_buffer, 128);
934 
935     /* Did we get an error?  */
936     if (status != FX_NO_MORE_SPACE)
937     {
938 
939         /* Error, return error code.  */
940         printf("ERROR!\n");
941         test_control_return(44);
942     }
943 
944     /* Close the files and the media.  */
945     status = fx_file_close(&my_file);
946     status += fx_file_close(&my_file1);
947     status += fx_file_close(&my_file2);
948     status += fx_media_close(&ram_disk);
949 
950     /* Check the status.  */
951     if (status != FX_SUCCESS)
952     {
953 
954         /* Error, return error code.  */
955         printf("ERROR!\n");
956         test_control_return(43);
957     }
958 
959     /* Test the update of files open for reading while the write is happening - with random errors!  */
960 
961     /* Format the media.  This needs to be done before opening it!  */
962     status =  fx_media_format(&ram_disk,
963                             _fx_ram_driver,         // Driver entry
964                             ram_disk_memory,        // RAM disk memory pointer
965                             cache_buffer,           // Media buffer pointer
966                             CACHE_SIZE,             // Media buffer size
967                             "MY_RAM_DISK",          // Volume Name
968                             1,                      // Number of FATs
969                             32,                     // Directory Entries
970                             0,                      // Hidden sectors
971                             6000,                   // Total sectors - FAT16
972                             128,                    // Sector size
973                             1,                      // Sectors per cluster
974                             1,                      // Heads
975                             1);                     // Sectors per track
976 
977     /* Determine if the format had an error.  */
978     if (status)
979     {
980 
981         printf("ERROR!\n");
982         test_control_return(44);
983     }
984 
985     /* Open the ram_disk.  */
986     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
987 
988     /* Check the status.  */
989     if (status != FX_SUCCESS)
990     {
991 
992         /* Error, return error code.  */
993         printf("ERROR!\n");
994         test_control_return(45);
995     }
996 
997     /* Create a file called TEST.TXT in the root directory.  */
998     fx_file_create(&ram_disk, "TEST.TXT");
999     fx_file_create(&ram_disk, "TEST1.TXT");
1000     fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_READ);
1001     fx_file_open(&ram_disk, &my_file1, "TEST.TXT", FX_OPEN_FOR_READ);
1002     fx_file_open(&ram_disk, &my_file2, "TEST.TXT", FX_OPEN_FOR_WRITE);
1003     fx_file_open(&ram_disk, &my_file3, "TEST1.TXT", FX_OPEN_FOR_WRITE);
1004 
1005     /* Now loop through the maximum of clusters to fill and read the file.  */
1006     i =  0;
1007     while (ram_disk.fx_media_available_clusters)
1008     {
1009 
1010         /* Flush the media.  */
1011         fx_media_flush(&ram_disk);
1012         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1013 
1014         /* Setup the random I/O Error.  */
1015         _fx_ram_driver_io_error_request =  (rand() & 4);
1016         if (_fx_ram_driver_io_error_request == 0)
1017             _fx_ram_driver_io_error_request = 1;
1018         _fx_utility_fat_entry_write_error_request =  _fx_ram_driver_io_error_request;
1019         _fx_utility_fat_entry_read_error_request =  _fx_ram_driver_io_error_request;
1020         _fx_utility_logical_sector_write_error_request =  _fx_ram_driver_io_error_request;
1021         _fx_utility_logical_sector_read_error_request =  _fx_ram_driver_io_error_request;
1022 
1023         /* Write to the writable file.  */
1024         if (i & 1)
1025         {
1026             fx_file_write(&my_file2, my_buffer, 128);
1027         }
1028         else
1029         {
1030             fx_file_write(&my_file2, my_buffer, 32);
1031             fx_file_write(&my_file2, my_buffer, 32);
1032             fx_file_write(&my_file2, my_buffer, 32);
1033             fx_file_write(&my_file2, my_buffer, 32);
1034         }
1035 
1036         /* Setup the random I/O Error.  */
1037         _fx_ram_driver_io_error_request =  (rand() & 4);
1038         if (_fx_ram_driver_io_error_request == 0)
1039             _fx_ram_driver_io_error_request = 1;
1040         _fx_utility_fat_entry_write_error_request =  _fx_ram_driver_io_error_request;
1041         _fx_utility_fat_entry_read_error_request =  _fx_ram_driver_io_error_request;
1042         _fx_utility_logical_sector_write_error_request =  _fx_ram_driver_io_error_request;
1043         _fx_utility_logical_sector_read_error_request =  _fx_ram_driver_io_error_request;
1044 
1045         /* Read the data in the file from the other 2 file handles.  */
1046         if (i & 1)
1047         {
1048             fx_file_read(&my_file1, my_buffer, 128, &actual1);
1049         }
1050         else
1051         {
1052             fx_file_read(&my_file1, my_buffer, 32, &actual1);
1053             fx_file_read(&my_file1, my_buffer, 32, &actual1);
1054             fx_file_read(&my_file1, my_buffer, 32, &actual1);
1055             fx_file_read(&my_file1, my_buffer, 32, &actual1);
1056         }
1057 
1058         /* Setup the random I/O Error.  */
1059         _fx_ram_driver_io_error_request =  (rand() & 4);
1060         if (_fx_ram_driver_io_error_request == 0)
1061             _fx_ram_driver_io_error_request = 1;
1062         _fx_utility_fat_entry_write_error_request =  _fx_ram_driver_io_error_request;
1063         _fx_utility_fat_entry_read_error_request =  _fx_ram_driver_io_error_request;
1064         _fx_utility_logical_sector_write_error_request =  _fx_ram_driver_io_error_request;
1065         _fx_utility_logical_sector_read_error_request =  _fx_ram_driver_io_error_request;
1066 
1067         fx_file_read(&my_file, my_buffer, 128, &actual2);
1068 
1069         _fx_ram_driver_io_error_request = 0;
1070         _fx_utility_fat_entry_write_error_request =  _fx_ram_driver_io_error_request;
1071         _fx_utility_fat_entry_read_error_request =  _fx_ram_driver_io_error_request;
1072         _fx_utility_logical_sector_write_error_request =  _fx_ram_driver_io_error_request;
1073         _fx_utility_logical_sector_read_error_request =  _fx_ram_driver_io_error_request;
1074 
1075         i++;
1076     }
1077 
1078     /* Close the files and the media.  */
1079     fx_file_close(&my_file);
1080     fx_file_close(&my_file1);
1081     fx_file_close(&my_file2);
1082     fx_media_close(&ram_disk);
1083 
1084 
1085     /* Test the write of partial cluster and direct I/O over non-contigous clusters.  */
1086 
1087 
1088     /* Format the media.  This needs to be done before opening it!  */
1089     status =  fx_media_format(&ram_disk,
1090                             _fx_ram_driver,         // Driver entry
1091                             ram_disk_memory,        // RAM disk memory pointer
1092                             cache_buffer,           // Media buffer pointer
1093                             CACHE_SIZE,             // Media buffer size
1094                             "MY_RAM_DISK",          // Volume Name
1095                             1,                      // Number of FATs
1096                             32,                     // Directory Entries
1097                             0,                      // Hidden sectors
1098                             6000,                   // Total sectors - FAT16
1099                             128,                    // Sector size
1100                             3,                      // Sectors per cluster
1101                             1,                      // Heads
1102                             1);                     // Sectors per track
1103 
1104     /* Determine if the format had an error.  */
1105     if (status)
1106     {
1107 
1108         printf("ERROR!\n");
1109         test_control_return(46);
1110     }
1111 
1112     /* Open the ram_disk.  */
1113     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1114 
1115     /* Check the status.  */
1116     if (status != FX_SUCCESS)
1117     {
1118 
1119         /* Error, return error code.  */
1120         printf("ERROR!\n");
1121         test_control_return(47);
1122     }
1123 
1124     /* Create a file called TEST.TXT in the root directory.  */
1125     status =  fx_file_create(&ram_disk, "TEST.TXT");
1126     status += fx_file_create(&ram_disk, "TEST1.TXT");
1127     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1128     status += fx_file_open(&ram_disk, &my_file4,"TEST1.TXT", FX_OPEN_FOR_WRITE);
1129 
1130     /* Write a small amount of data first to cause an unalignment and partial sector write.  */
1131     status += fx_file_write(&my_file, my_buffer, 32);
1132     status += fx_file_write(&my_file4, my_buffer, 32);
1133     status += fx_file_write(&my_file, my_buffer, 128*4);
1134     status += fx_file_write(&my_file, my_buffer, 128*4);
1135 
1136     /* Now loop through the maximum of clusters to fill up the rest of the FAT table.  */
1137     i =  0;
1138     while (ram_disk.fx_media_available_clusters)
1139     {
1140 
1141 
1142         /* Write to the writable file.  */
1143         status +=  fx_file_write(&my_file, my_buffer, 32);
1144         status +=  fx_file_write(&my_file4, my_buffer, 32);
1145         i++;
1146     }
1147 
1148     /* Check the status.  */
1149     if (status != FX_SUCCESS)
1150     {
1151 
1152         /* Error, return error code.  */
1153         printf("ERROR!\n");
1154         test_control_return(48);
1155     }
1156 
1157     /* Now read the file in as big of chunks as possible to excersise the partial read paths.  */
1158     status =  fx_file_seek(&my_file, 0);
1159     do
1160     {
1161         /* Read chunks of the file.  */
1162         status =  fx_file_read(&my_file, my_buffer, 128*4, &actual);
1163 
1164     } while (status != FX_END_OF_FILE);
1165 
1166 
1167     /* Now delete the second file to leave holes in the FAT table.  */
1168     status += fx_file_close(&my_file4);
1169     status += fx_file_delete(&ram_disk, "TEST1.TXT");
1170 
1171     /* Finally, perform a direct write with that can't be done with contigous clusters.  */
1172     status =  fx_file_write(&my_file, my_buffer, 128*4);
1173 
1174     /* Did we get an error?  */
1175     if (status != FX_SUCCESS)
1176     {
1177 
1178         /* Error, return error code.  */
1179         printf("ERROR!\n");
1180         test_control_return(49);
1181     }
1182 
1183     /* Close the files and the media.  */
1184     status = fx_file_close(&my_file);
1185     status += fx_media_close(&ram_disk);
1186 
1187     /* Check the status.  */
1188     if (status != FX_SUCCESS)
1189     {
1190 
1191         /* Error, return error code.  */
1192         printf("ERROR!\n");
1193         test_control_return(50);
1194     }
1195 
1196 
1197     /* Test the write of partial cluster and direct I/O when the FAT chain is broken.  */
1198 
1199     /* Format the media.  This needs to be done before opening it!  */
1200     status =  fx_media_format(&ram_disk,
1201                             _fx_ram_driver,         // Driver entry
1202                             ram_disk_memory,        // RAM disk memory pointer
1203                             cache_buffer,           // Media buffer pointer
1204                             CACHE_SIZE,             // Media buffer size
1205                             "MY_RAM_DISK",          // Volume Name
1206                             1,                      // Number of FATs
1207                             32,                     // Directory Entries
1208                             0,                      // Hidden sectors
1209                             14000,                   // Total sectors - FAT16
1210                             128,                    // Sector size
1211                             2,                      // Sectors per cluster
1212                             1,                      // Heads
1213                             1);                     // Sectors per track
1214 
1215     /* Determine if the format had an error.  */
1216     if (status)
1217     {
1218 
1219         printf("ERROR!\n");
1220         test_control_return(51);
1221     }
1222 
1223     /* Open the ram_disk.  */
1224     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1225 
1226     /* Check the status.  */
1227     if (status != FX_SUCCESS)
1228     {
1229 
1230         /* Error, return error code.  */
1231         printf("ERROR!\n");
1232         test_control_return(52);
1233     }
1234 
1235     /* Create a file called TEST.TXT in the root directory.  */
1236     status =  fx_file_create(&ram_disk, "TEST.TXT");
1237     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1238 
1239     /* Write a data first to build a FAT chain.  */
1240     status += fx_file_write(&my_file, my_buffer, 128*4);
1241     status += fx_file_write(&my_file, my_buffer, 128*4);
1242     status += fx_file_write(&my_file, my_buffer, 128*4);
1243     status += fx_file_write(&my_file, my_buffer, 128*4);
1244     status += fx_file_seek(&my_file, 0);
1245     status += fx_media_flush(&ram_disk);
1246     _fx_utility_FAT_flush(&ram_disk);
1247     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1248     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
1249     {
1250         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
1251         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
1252     }
1253 
1254     /* Read the first FAT sector.  */
1255     status += fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
1256 
1257     /* Add a FAT entry randomly in the FAT table.  */
1258     fat_buffer[6] =  1;
1259     fat_buffer[7] =  0;
1260     fat_buffer[8] =  1;
1261     fat_buffer[9] =  0;
1262 
1263     /* Write the FAT corruption out.  */
1264     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
1265 
1266     /* See if we are okay.  */
1267     if (status != FX_SUCCESS)
1268     {
1269 
1270         /* Error, return error code.  */
1271         printf("ERROR!\n");
1272         test_control_return(53);
1273     }
1274 
1275     /* Now attempt to write a partial sector.  */
1276     status =  fx_file_write(&my_file, my_buffer, 128*4);
1277 
1278     /* See if we get the file corrupt error.  */
1279     if (status != FX_FILE_CORRUPT)
1280     {
1281 
1282         /* Error, return error code.  */
1283         printf("ERROR!\n");
1284         test_control_return(54);
1285     }
1286 
1287     status =  fx_file_write(&my_file, my_buffer, 128*4);
1288     status +=  fx_file_write(&my_file, my_buffer, 128*4);
1289 
1290     /* See if we get the file corrupt error.  */
1291     if (status != FX_FILE_CORRUPT)
1292     {
1293 
1294         /* Error, return error code.  */
1295         printf("ERROR!\n");
1296         test_control_return(55);
1297     }
1298 
1299 
1300     /* Close the files and the media.  */
1301     status = fx_file_close(&my_file);
1302     status += fx_media_close(&ram_disk);
1303 
1304     /* Check the status.  */
1305     if (status != FX_SUCCESS)
1306     {
1307 
1308         /* Error, return error code.  */
1309         printf("ERROR!\n");
1310         test_control_return(56);
1311     }
1312 
1313     /* Test the maximum write size.  */
1314 
1315     /* Format the media.  This needs to be done before opening it!  */
1316     status =  fx_media_format(&ram_disk,
1317                             _fx_ram_driver,         // Driver entry
1318                             ram_disk_memory,        // RAM disk memory pointer
1319                             cache_buffer,           // Media buffer pointer
1320                             CACHE_SIZE,             // Media buffer size
1321                             "MY_RAM_DISK",          // Volume Name
1322                             1,                      // Number of FATs
1323                             32,                     // Directory Entries
1324                             0,                      // Hidden sectors
1325                             1000,                   // Total sectors - FAT12
1326                             128,                    // Sector size
1327                             1,                      // Sectors per cluster
1328                             1,                      // Heads
1329                             1);                     // Sectors per track
1330 
1331     /* Determine if the format had an error.  */
1332     if (status)
1333     {
1334 
1335         printf("ERROR!\n");
1336         test_control_return(57);
1337     }
1338 
1339     /* Open the ram_disk.  */
1340     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1341 
1342     /* Check the status.  */
1343     if (status != FX_SUCCESS)
1344     {
1345 
1346         /* Error, return error code.  */
1347         printf("ERROR!\n");
1348         test_control_return(58);
1349     }
1350 
1351     /* Create a file called TEST.TXT in the root directory.  */
1352     status =  fx_file_create(&ram_disk, "TEST.TXT");
1353     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1354 
1355     /* Write a data first to build a FAT chain.  */
1356     status += fx_file_write(&my_file, my_buffer, 128);
1357 
1358     /* Check the status.  */
1359     if (status != FX_SUCCESS)
1360     {
1361 
1362         /* Error, return error code.  */
1363         printf("ERROR!\n");
1364         test_control_return(59);
1365     }
1366 
1367     /* Now manually setup the file offset to force an error.  */
1368     temp =  (ULONG)my_file.fx_file_current_file_offset;
1369     my_file.fx_file_current_file_offset =  0xFFFFFFF0;
1370 
1371     /* Write a data first to force the overflow.  */
1372     status = fx_file_write(&my_file, my_buffer, 32);
1373 
1374     /* Check the status.  */
1375     if (status != FX_NO_MORE_SPACE)
1376     {
1377 
1378         /* Error, return error code.  */
1379         printf("ERROR!\n");
1380         test_control_return(60);
1381     }
1382 
1383     /* Also save, adjust the current available so that one new cluster will be written.  */
1384     my_file.fx_file_current_available_size = 0xFFFFFFF0;
1385     temp1 = (ULONG)my_file.fx_file_current_available_size;
1386 
1387     /* Now attempt to write a massive file to exercise the maximum available file size logic.  */
1388     status = fx_file_write(&my_file, my_buffer, 1);
1389 
1390     /* Check the status.  */
1391     if (status != FX_SUCCESS)
1392     {
1393 
1394         /* Error, return error code.  */
1395         printf("ERROR!\n");
1396         test_control_return(61);
1397     }
1398 
1399     /* Now restore the original offset.  */
1400     my_file.fx_file_current_file_offset =  temp;
1401     my_file.fx_file_current_available_size =  temp1;
1402 
1403     /* Close the file.  */
1404     status =  fx_file_close(&my_file);
1405     status += fx_media_close(&ram_disk);
1406 
1407     /* Check the status.  */
1408     if (status != FX_SUCCESS)
1409     {
1410 
1411         /* Error, return error code.  */
1412         printf("ERROR!\n");
1413         test_control_return(62);
1414     }
1415 
1416 
1417     /* Test the remaining I/O error paths in file write.  */
1418 
1419     /* Format the media.  This needs to be done before opening it!  */
1420     status =  fx_media_format(&ram_disk,
1421                             _fx_ram_driver,         // Driver entry
1422                             ram_disk_memory,        // RAM disk memory pointer
1423                             cache_buffer,           // Media buffer pointer
1424                             CACHE_SIZE,             // Media buffer size
1425                             "MY_RAM_DISK",          // Volume Name
1426                             1,                      // Number of FATs
1427                             32,                     // Directory Entries
1428                             0,                      // Hidden sectors
1429                             1000,                   // Total sectors - FAT12
1430                             128,                    // Sector size
1431                             3,                      // Sectors per cluster
1432                             1,                      // Heads
1433                             1);                     // Sectors per track
1434 
1435     /* Determine if the format had an error.  */
1436     if (status)
1437     {
1438 
1439         printf("ERROR!\n");
1440         test_control_return(63);
1441     }
1442 
1443     /* Open the ram_disk.  */
1444     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1445 
1446     /* Check the status.  */
1447     if (status != FX_SUCCESS)
1448     {
1449 
1450         /* Error, return error code.  */
1451         printf("ERROR!\n");
1452         test_control_return(64);
1453     }
1454 
1455     /* Create a file called TEST.TXT in the root directory.  */
1456     status = fx_file_create(&ram_disk, "TEST.TXT");
1457     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1458 
1459     /* Check the status.  */
1460     if (status != FX_SUCCESS)
1461     {
1462 
1463         /* Error, return error code.  */
1464         printf("ERROR!\n");
1465         test_control_return(65);
1466     }
1467 
1468     /* Write a data first to build a FAT chain, with an I/O error on liking the FAT chain.  */
1469     _fx_utility_fat_entry_write_error_request =  1;
1470     status = fx_file_write(&my_file, my_buffer, 128*4);
1471     _fx_utility_fat_entry_write_error_request =  0;
1472 
1473     /* Check the status.  */
1474     if (status != FX_IO_ERROR)
1475     {
1476 
1477         /* Error, return error code.  */
1478         printf("ERROR!\n");
1479         test_control_return(66);
1480     }
1481 
1482     /* Close the file.  */
1483     status =  fx_file_close(&my_file);
1484     status += fx_media_close(&ram_disk);
1485 
1486     /* Check the status.  */
1487     if (status != FX_SUCCESS)
1488     {
1489 
1490         /* Error, return error code.  */
1491         printf("ERROR!\n");
1492         test_control_return(67);
1493     }
1494 
1495     /* Format the media.  This needs to be done before opening it!  */
1496     status =  fx_media_format(&ram_disk,
1497                             _fx_ram_driver,         // Driver entry
1498                             ram_disk_memory,        // RAM disk memory pointer
1499                             cache_buffer,           // Media buffer pointer
1500                             CACHE_SIZE,             // Media buffer size
1501                             "MY_RAM_DISK",          // Volume Name
1502                             1,                      // Number of FATs
1503                             32,                     // Directory Entries
1504                             0,                      // Hidden sectors
1505                             1000,                   // Total sectors - FAT12
1506                             128,                    // Sector size
1507                             3,                      // Sectors per cluster
1508                             1,                      // Heads
1509                             1);                     // Sectors per track
1510 
1511     /* Determine if the format had an error.  */
1512     if (status)
1513     {
1514 
1515         printf("ERROR!\n");
1516         test_control_return(68);
1517     }
1518 
1519     /* Open the ram_disk.  */
1520     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1521 
1522     /* Check the status.  */
1523     if (status != FX_SUCCESS)
1524     {
1525 
1526         /* Error, return error code.  */
1527         printf("ERROR!\n");
1528         test_control_return(69);
1529     }
1530 
1531     /* Create a file called TEST.TXT in the root directory.  */
1532     status = fx_file_create(&ram_disk, "TEST.TXT");
1533     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1534 
1535     /* Check the status.  */
1536     if (status != FX_SUCCESS)
1537     {
1538 
1539         /* Error, return error code.  */
1540         printf("ERROR!\n");
1541         test_control_return(70);
1542     }
1543 
1544     /* Write a data first to build a FAT chain, with an I/O error on writing EOF at the end of the FAT chain.  */
1545     _fx_utility_fat_entry_write_error_request =  2;
1546     status = fx_file_write(&my_file, my_buffer, 128*4);
1547     _fx_utility_fat_entry_write_error_request =  0;
1548 
1549     /* Check the status.  */
1550     if (status != FX_IO_ERROR)
1551     {
1552 
1553         /* Error, return error code.  */
1554         printf("ERROR!\n");
1555         test_control_return(71);
1556     }
1557 
1558     /* Close the file.  */
1559     status =  fx_file_close(&my_file);
1560     status += fx_media_close(&ram_disk);
1561 
1562     /* Check the status.  */
1563     if (status != FX_SUCCESS)
1564     {
1565 
1566         /* Error, return error code.  */
1567         printf("ERROR!\n");
1568         test_control_return(72);
1569     }
1570 
1571 
1572     /* Format the media.  This needs to be done before opening it!  */
1573     status =  fx_media_format(&ram_disk,
1574                             _fx_ram_driver,         // Driver entry
1575                             ram_disk_memory,        // RAM disk memory pointer
1576                             cache_buffer,           // Media buffer pointer
1577                             CACHE_SIZE,             // Media buffer size
1578                             "MY_RAM_DISK",          // Volume Name
1579                             1,                      // Number of FATs
1580                             32,                     // Directory Entries
1581                             0,                      // Hidden sectors
1582                             1000,                   // Total sectors - FAT12
1583                             128,                    // Sector size
1584                             3,                      // Sectors per cluster
1585                             1,                      // Heads
1586                             1);                     // Sectors per track
1587 
1588     /* Determine if the format had an error.  */
1589     if (status)
1590     {
1591 
1592         printf("ERROR!\n");
1593         test_control_return(73);
1594     }
1595 
1596     /* Open the ram_disk.  */
1597     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1598 
1599     /* Check the status.  */
1600     if (status != FX_SUCCESS)
1601     {
1602 
1603         /* Error, return error code.  */
1604         printf("ERROR!\n");
1605         test_control_return(74);
1606     }
1607 
1608     /* Create a file called TEST.TXT in the root directory.  */
1609     status = fx_file_create(&ram_disk, "TEST.TXT");
1610     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1611     status += fx_file_write(&my_file, my_buffer, 128*4);
1612 
1613     /* Check the status.  */
1614     if (status != FX_SUCCESS)
1615     {
1616 
1617         /* Error, return error code.  */
1618         printf("ERROR!\n");
1619         test_control_return(75);
1620     }
1621 
1622     /* Write a data first to build a FAT chain, with an I/O error on linking the new FAT chain to existing FAT chain.  */
1623     _fx_utility_fat_entry_write_error_request =  2;
1624     status = fx_file_write(&my_file, my_buffer, 128*4);
1625     _fx_utility_fat_entry_write_error_request =  0;
1626 
1627     /* Check the status.  */
1628     if (status != FX_IO_ERROR)
1629     {
1630 
1631         /* Error, return error code.  */
1632         printf("ERROR!\n");
1633         test_control_return(76);
1634     }
1635 
1636     /* Close the file.  */
1637     status =  fx_file_close(&my_file);
1638     status += fx_media_close(&ram_disk);
1639 
1640     /* Check the status.  */
1641     if (status != FX_SUCCESS)
1642     {
1643 
1644         /* Error, return error code.  */
1645         printf("ERROR!\n");
1646         test_control_return(77);
1647     }
1648 
1649     /* Format the media.  This needs to be done before opening it!  */
1650     status =  fx_media_format(&ram_disk,
1651                             _fx_ram_driver,         // Driver entry
1652                             ram_disk_memory,        // RAM disk memory pointer
1653                             cache_buffer,           // Media buffer pointer
1654                             CACHE_SIZE,             // Media buffer size
1655                             "MY_RAM_DISK",          // Volume Name
1656                             1,                      // Number of FATs
1657                             32,                     // Directory Entries
1658                             0,                      // Hidden sectors
1659                             1000,                   // Total sectors - FAT12
1660                             128,                    // Sector size
1661                             3,                      // Sectors per cluster
1662                             1,                      // Heads
1663                             1);                     // Sectors per track
1664 
1665     /* Determine if the format had an error.  */
1666     if (status)
1667     {
1668 
1669         printf("ERROR!\n");
1670         test_control_return(78);
1671     }
1672 
1673     /* Open the ram_disk.  */
1674     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1675 
1676     /* Check the status.  */
1677     if (status != FX_SUCCESS)
1678     {
1679 
1680         /* Error, return error code.  */
1681         printf("ERROR!\n");
1682         test_control_return(79);
1683     }
1684 
1685     /* Create a file called TEST.TXT in the root directory.  */
1686     status = fx_file_create(&ram_disk, "TEST.TXT");
1687     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1688     status += fx_file_write(&my_file, my_buffer, 128+64);
1689 
1690     /* Check the status.  */
1691     if (status != FX_SUCCESS)
1692     {
1693 
1694         /* Error, return error code.  */
1695         printf("ERROR!\n");
1696         test_control_return(80);
1697     }
1698 
1699     /* Write data that will require another cluster, even though we have only written one sector of the first allocated cluster.  */
1700     status = fx_file_write(&my_file, my_buffer, 128*4);
1701 
1702     /* Check the status.  */
1703     if (status != FX_SUCCESS)
1704     {
1705 
1706         /* Error, return error code.  */
1707         printf("ERROR!\n");
1708         test_control_return(81);
1709     }
1710 
1711     /* Close the file.  */
1712     status =  fx_file_close(&my_file);
1713     status += fx_media_close(&ram_disk);
1714 
1715     /* Check the status.  */
1716     if (status != FX_SUCCESS)
1717     {
1718 
1719         /* Error, return error code.  */
1720         printf("ERROR!\n");
1721         test_control_return(82);
1722     }
1723 
1724     /* Test the read of partial cluster and direct I/O when the FAT chain is broken.  */
1725 
1726     /* Format the media.  This needs to be done before opening it!  */
1727     status =  fx_media_format(&ram_disk,
1728                             _fx_ram_driver,         // Driver entry
1729                             ram_disk_memory,        // RAM disk memory pointer
1730                             cache_buffer,           // Media buffer pointer
1731                             CACHE_SIZE,             // Media buffer size
1732                             "MY_RAM_DISK",          // Volume Name
1733                             1,                      // Number of FATs
1734                             32,                     // Directory Entries
1735                             0,                      // Hidden sectors
1736                             14000,                   // Total sectors - FAT16
1737                             128,                    // Sector size
1738                             2,                      // Sectors per cluster
1739                             1,                      // Heads
1740                             1);                     // Sectors per track
1741 
1742     /* Determine if the format had an error.  */
1743     if (status)
1744     {
1745 
1746         printf("ERROR!\n");
1747         test_control_return(83);
1748     }
1749 
1750     /* Open the ram_disk.  */
1751     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1752 
1753     /* Check the status.  */
1754     if (status != FX_SUCCESS)
1755     {
1756 
1757         /* Error, return error code.  */
1758         printf("ERROR!\n");
1759         test_control_return(84);
1760     }
1761 
1762     /* Create a file called TEST.TXT in the root directory.  */
1763     status =  fx_file_create(&ram_disk, "TEST.TXT");
1764     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1765 
1766     /* Write a data first to build a FAT chain.  */
1767     status += fx_file_write(&my_file, my_buffer, 128*4);
1768     status += fx_file_write(&my_file, my_buffer, 128*4);
1769     status += fx_file_write(&my_file, my_buffer, 128*4);
1770     status += fx_file_write(&my_file, my_buffer, 128*4);
1771     status += fx_file_seek(&my_file, 0);
1772     status += fx_media_flush(&ram_disk);
1773     _fx_utility_FAT_flush(&ram_disk);
1774     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1775     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
1776     {
1777         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
1778         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
1779     }
1780 
1781     /* Read the first FAT sector.  */
1782     status += fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
1783 
1784     /* Add a FAT entry randomly in the FAT table.  */
1785     fat_buffer[6] =  1;
1786     fat_buffer[7] =  0;
1787     fat_buffer[8] =  1;
1788     fat_buffer[9] =  0;
1789 
1790     /* Write the FAT corruption out.  */
1791     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
1792 
1793     /* See if we are okay.  */
1794     if (status != FX_SUCCESS)
1795     {
1796 
1797         /* Error, return error code.  */
1798         printf("ERROR!\n");
1799         test_control_return(85);
1800     }
1801 
1802     /* Read the file to get the file corruption error.  */
1803     status =  fx_file_read(&my_file, my_buffer, 128*4, &actual);
1804 
1805     /* See if the file is corrupted - it should be.  */
1806     if (status != FX_FILE_CORRUPT)
1807     {
1808 
1809         /* Error, return error code.  */
1810         printf("ERROR!\n");
1811         test_control_return(86);
1812     }
1813 
1814     /* Close everything down.  */
1815     fx_file_close(&my_file);
1816     fx_media_abort(&ram_disk);
1817 
1818     /* Test the read of partial cluster and direct I/O when the FAT chain is broken - at the beginning of the FAT chain!  */
1819 
1820     /* Format the media.  This needs to be done before opening it!  */
1821     status =  fx_media_format(&ram_disk,
1822                             _fx_ram_driver,         // Driver entry
1823                             ram_disk_memory,        // RAM disk memory pointer
1824                             cache_buffer,           // Media buffer pointer
1825                             CACHE_SIZE,             // Media buffer size
1826                             "MY_RAM_DISK",          // Volume Name
1827                             1,                      // Number of FATs
1828                             32,                     // Directory Entries
1829                             0,                      // Hidden sectors
1830                             14000,                   // Total sectors - FAT16
1831                             128,                    // Sector size
1832                             2,                      // Sectors per cluster
1833                             1,                      // Heads
1834                             1);                     // Sectors per track
1835 
1836     /* Determine if the format had an error.  */
1837     if (status)
1838     {
1839 
1840         printf("ERROR!\n");
1841         test_control_return(87);
1842     }
1843 
1844     /* Open the ram_disk.  */
1845     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1846 
1847     /* Check the status.  */
1848     if (status != FX_SUCCESS)
1849     {
1850 
1851         /* Error, return error code.  */
1852         printf("ERROR!\n");
1853         test_control_return(88);
1854     }
1855 
1856     /* Create a file called TEST.TXT in the root directory.  */
1857     status =  fx_file_create(&ram_disk, "TEST.TXT");
1858     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1859 
1860     /* Write a data first to build a FAT chain.  */
1861     status += fx_file_write(&my_file, my_buffer, 128*4);
1862     status += fx_file_write(&my_file, my_buffer, 128*4);
1863     status += fx_file_write(&my_file, my_buffer, 128*4);
1864     status += fx_file_write(&my_file, my_buffer, 128*4);
1865     status += fx_file_seek(&my_file, 0);
1866     status += fx_media_flush(&ram_disk);
1867     _fx_utility_FAT_flush(&ram_disk);
1868     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1869     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
1870     {
1871         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
1872         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
1873     }
1874 
1875     /* Read the first FAT sector.  */
1876     status += fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
1877 
1878     /* Add a FAT entry randomly in the FAT table.  */
1879     fat_buffer[4] =  1;
1880     fat_buffer[5] =  0;
1881     fat_buffer[6] =  1;
1882     fat_buffer[7] =  0;
1883     fat_buffer[8] =  1;
1884     fat_buffer[9] =  0;
1885 
1886     /* Write the FAT corruption out.  */
1887     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
1888 
1889     /* See if we are okay.  */
1890     if (status != FX_SUCCESS)
1891     {
1892 
1893         /* Error, return error code.  */
1894         printf("ERROR!\n");
1895         test_control_return(89);
1896     }
1897 
1898     /* Read the file to get the file corruption error.  */
1899     status =  fx_file_read(&my_file, my_buffer, 128*4, &actual);
1900 
1901     /* See if the file is corrupted - it should be.  */
1902     if (status != FX_FILE_CORRUPT)
1903     {
1904 
1905         /* Error, return error code.  */
1906         printf("ERROR!\n");
1907         test_control_return(90);
1908     }
1909 
1910     /* Close everything down.  */
1911     fx_file_close(&my_file);
1912     fx_media_abort(&ram_disk);
1913 
1914 
1915     /* Test the read of partial cluster and direct I/O when the FAT chain is broken - at the beginning of the FAT chain and with FFs instead of 1!  */
1916 
1917     /* Format the media.  This needs to be done before opening it!  */
1918     status =  fx_media_format(&ram_disk,
1919                             _fx_ram_driver,         // Driver entry
1920                             ram_disk_memory,        // RAM disk memory pointer
1921                             cache_buffer,           // Media buffer pointer
1922                             CACHE_SIZE,             // Media buffer size
1923                             "MY_RAM_DISK",          // Volume Name
1924                             1,                      // Number of FATs
1925                             32,                     // Directory Entries
1926                             0,                      // Hidden sectors
1927                             14000,                   // Total sectors - FAT16
1928                             128,                    // Sector size
1929                             2,                      // Sectors per cluster
1930                             1,                      // Heads
1931                             1);                     // Sectors per track
1932 
1933     /* Determine if the format had an error.  */
1934     if (status)
1935     {
1936 
1937         printf("ERROR!\n");
1938         test_control_return(91);
1939     }
1940 
1941     /* Open the ram_disk.  */
1942     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
1943 
1944     /* Check the status.  */
1945     if (status != FX_SUCCESS)
1946     {
1947 
1948         /* Error, return error code.  */
1949         printf("ERROR!\n");
1950         test_control_return(92);
1951     }
1952 
1953     /* Create a file called TEST.TXT in the root directory.  */
1954     status =  fx_file_create(&ram_disk, "TEST.TXT");
1955     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
1956 
1957     /* Write a data first to build a FAT chain.  */
1958     status += fx_file_write(&my_file, my_buffer, 128*4);
1959     status += fx_file_write(&my_file, my_buffer, 128*4);
1960     status += fx_file_write(&my_file, my_buffer, 128*4);
1961     status += fx_file_write(&my_file, my_buffer, 128*4);
1962     status += fx_file_seek(&my_file, 0);
1963     status += fx_media_flush(&ram_disk);
1964     _fx_utility_FAT_flush(&ram_disk);
1965     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1966     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
1967     {
1968         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
1969         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
1970     }
1971 
1972     /* Read the first FAT sector.  */
1973     status += fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
1974 
1975     /* Add a FAT entry randomly in the FAT table.  */
1976     fat_buffer[4] =  0xFF;
1977     fat_buffer[5] =  0xFF;
1978     fat_buffer[6] =  0xFF;
1979     fat_buffer[7] =  0xFF;
1980     fat_buffer[8] =  0xFF;
1981     fat_buffer[9] =  0xFF;
1982 
1983     /* Write the FAT corruption out.  */
1984     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
1985 
1986     /* See if we are okay.  */
1987     if (status != FX_SUCCESS)
1988     {
1989 
1990         /* Error, return error code.  */
1991         printf("ERROR!\n");
1992         test_control_return(93);
1993     }
1994 
1995     /* Read the file to get the file corruption error.  */
1996     status =  fx_file_read(&my_file, my_buffer, 128*4, &actual);
1997 
1998     /* See if the file is corrupted - it should be.  */
1999     if (status != FX_FILE_CORRUPT)
2000     {
2001 
2002         /* Error, return error code.  */
2003         printf("ERROR!\n");
2004         test_control_return(94);
2005     }
2006 
2007     /* Close everything down.  */
2008     fx_file_close(&my_file);
2009     fx_media_abort(&ram_disk);
2010 
2011 
2012     /* Test the read with I/O FAT read error.  */
2013 
2014     /* Format the media.  This needs to be done before opening it!  */
2015     status =  fx_media_format(&ram_disk,
2016                             _fx_ram_driver,         // Driver entry
2017                             ram_disk_memory,        // RAM disk memory pointer
2018                             cache_buffer,           // Media buffer pointer
2019                             CACHE_SIZE,             // Media buffer size
2020                             "MY_RAM_DISK",          // Volume Name
2021                             1,                      // Number of FATs
2022                             32,                     // Directory Entries
2023                             0,                      // Hidden sectors
2024                             14000,                   // Total sectors - FAT16
2025                             128,                    // Sector size
2026                             2,                      // Sectors per cluster
2027                             1,                      // Heads
2028                             1);                     // Sectors per track
2029 
2030     /* Determine if the format had an error.  */
2031     if (status)
2032     {
2033 
2034         printf("ERROR!\n");
2035         test_control_return(95);
2036     }
2037 
2038     /* Open the ram_disk.  */
2039     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
2040 
2041     /* Check the status.  */
2042     if (status != FX_SUCCESS)
2043     {
2044 
2045         /* Error, return error code.  */
2046         printf("ERROR!\n");
2047         test_control_return(96);
2048     }
2049 
2050     /* Create a file called TEST.TXT in the root directory.  */
2051     status =  fx_file_create(&ram_disk, "TEST.TXT");
2052     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2053 
2054     /* Write a data first to build a FAT chain.  */
2055     status += fx_file_write(&my_file, my_buffer, 128*4);
2056     status += fx_file_write(&my_file, my_buffer, 128*4);
2057     status += fx_file_write(&my_file, my_buffer, 128*4);
2058     status += fx_file_write(&my_file, my_buffer, 128*4);
2059     status += fx_file_seek(&my_file, 0);
2060     status += fx_media_flush(&ram_disk);
2061     _fx_utility_FAT_flush(&ram_disk);
2062     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2063     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2064     {
2065         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2066         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2067     }
2068 
2069     /* Read the file to get the file IO error.  */
2070     _fx_utility_fat_entry_read_error_request =  1;
2071     status =  fx_file_read(&my_file, my_buffer, 128*4, &actual);
2072     _fx_utility_fat_entry_read_error_request =  0;
2073 
2074     /* See if the file is corrupted - it should be.  */
2075     if (status != FX_IO_ERROR)
2076     {
2077 
2078         /* Error, return error code.  */
2079         printf("ERROR!\n");
2080         test_control_return(97);
2081     }
2082 
2083     /* Close everything down.  */
2084     fx_file_close(&my_file);
2085     fx_media_abort(&ram_disk);
2086 
2087 
2088     /* Test the read with I/O logical sector read error.  */
2089 
2090     /* Format the media.  This needs to be done before opening it!  */
2091     status =  fx_media_format(&ram_disk,
2092                             _fx_ram_driver,         // Driver entry
2093                             ram_disk_memory,        // RAM disk memory pointer
2094                             cache_buffer,           // Media buffer pointer
2095                             CACHE_SIZE,             // Media buffer size
2096                             "MY_RAM_DISK",          // Volume Name
2097                             1,                      // Number of FATs
2098                             32,                     // Directory Entries
2099                             0,                      // Hidden sectors
2100                             14000,                   // Total sectors - FAT16
2101                             128,                    // Sector size
2102                             2,                      // Sectors per cluster
2103                             1,                      // Heads
2104                             1);                     // Sectors per track
2105 
2106     /* Determine if the format had an error.  */
2107     if (status)
2108     {
2109 
2110         printf("ERROR!\n");
2111         test_control_return(95);
2112     }
2113 
2114     /* Open the ram_disk.  */
2115     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
2116 
2117     /* Check the status.  */
2118     if (status != FX_SUCCESS)
2119     {
2120 
2121         /* Error, return error code.  */
2122         printf("ERROR!\n");
2123         test_control_return(96);
2124     }
2125 
2126     /* Create a file called TEST.TXT in the root directory.  */
2127     status =  fx_file_create(&ram_disk, "TEST.TXT");
2128     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2129 
2130     /* Write a data first to build a FAT chain.  */
2131     status += fx_file_write(&my_file, my_buffer, 128*4);
2132     status += fx_file_write(&my_file, my_buffer, 128*4);
2133     status += fx_file_write(&my_file, my_buffer, 128*4);
2134     status += fx_file_write(&my_file, my_buffer, 128*4);
2135     status += fx_file_seek(&my_file, 0);
2136     status += fx_media_flush(&ram_disk);
2137     _fx_utility_FAT_flush(&ram_disk);
2138     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2139     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2140     {
2141         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2142         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2143     }
2144 
2145     /* Read the file to get the file IO error.  */
2146     _fx_ram_driver_io_error_request =  2;
2147     status =  fx_file_read(&my_file, my_buffer, 128*4, &actual);
2148     _fx_ram_driver_io_error_request =  0;
2149 
2150     /* See if the file is corrupted - it should be.  */
2151     if (status != FX_IO_ERROR)
2152     {
2153 
2154         /* Error, return error code.  */
2155         printf("ERROR!\n");
2156         test_control_return(97);
2157     }
2158 
2159     /* Close everything down.  */
2160     fx_file_close(&my_file);
2161     fx_media_abort(&ram_disk);
2162 
2163 
2164     /* Test the file open with I/O read errors when walking the FAT chain.  */
2165 
2166     /* Format the media.  This needs to be done before opening it!  */
2167     status =  fx_media_format(&ram_disk,
2168                             _fx_ram_driver,         // Driver entry
2169                             ram_disk_memory,        // RAM disk memory pointer
2170                             cache_buffer,           // Media buffer pointer
2171                             CACHE_SIZE,             // Media buffer size
2172                             "MY_RAM_DISK",          // Volume Name
2173                             1,                      // Number of FATs
2174                             32,                     // Directory Entries
2175                             0,                      // Hidden sectors
2176                             7000,                   // Total sectors - FAT16
2177                             128,                    // Sector size
2178                             1,                      // Sectors per cluster
2179                             1,                      // Heads
2180                             1);                     // Sectors per track
2181 
2182     /* Determine if the format had an error.  */
2183     if (status)
2184     {
2185 
2186         printf("ERROR!\n");
2187         test_control_return(98);
2188     }
2189 
2190     /* Open the ram_disk.  */
2191     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
2192 
2193     /* Check the status.  */
2194     if (status != FX_SUCCESS)
2195     {
2196 
2197         /* Error, return error code.  */
2198         printf("ERROR!\n");
2199         test_control_return(99);
2200     }
2201 
2202     /* Create a file called TEST.TXT in the root directory.  */
2203     status =  fx_file_create(&ram_disk, "TEST.TXT");
2204     status += fx_file_create(&ram_disk, "TEST1.TXT");
2205     status += fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2206     status += fx_file_open(&ram_disk, &my_file1, "TEST1.TXT", FX_OPEN_FOR_WRITE);
2207 
2208     /* Write a data first to build a FAT chain.  */
2209     status += fx_file_write(&my_file, my_buffer, 128*4);
2210     status += fx_file_write(&my_file, my_buffer, 128*4);
2211     status += fx_file_write(&my_file, my_buffer, 128*4);
2212     status += fx_file_write(&my_file, my_buffer, 128*4);
2213     status += fx_file_write(&my_file1, my_buffer, 128*4);
2214     status += fx_file_write(&my_file, my_buffer, 128*4);
2215 
2216     /* Close the file.  */
2217     status += fx_file_close(&my_file);
2218     status += fx_file_close(&my_file1);
2219 
2220     /* Check status.  */
2221     if (status != FX_SUCCESS)
2222     {
2223 
2224         /* Error, return error code.  */
2225         printf("ERROR!\n");
2226         test_control_return(100);
2227     }
2228 
2229     /* Now flush everything out.  */
2230     fx_media_flush(&ram_disk);
2231     _fx_utility_FAT_flush(&ram_disk);
2232     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2233     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2234     {
2235         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2236         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2237     }
2238 
2239 
2240     /* Now open the file with an I/O error on the FAT entry read when walking the FAT chain.  */
2241     _fx_utility_fat_entry_read_error_request =  1;
2242     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2243     _fx_utility_fat_entry_read_error_request =  0;
2244 
2245     /* See if we got the I/O Error.  */
2246     if (status != FX_IO_ERROR)
2247     {
2248 
2249         /* Error, return error code.  */
2250         printf("ERROR!\n");
2251         test_control_return(101);
2252     }
2253 
2254     /* Now break the FAT chain and try to open the file.  */
2255 
2256     /* Now flush everything out.  */
2257     fx_media_flush(&ram_disk);
2258     _fx_utility_FAT_flush(&ram_disk);
2259     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2260     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2261     {
2262         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2263         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2264     }
2265 
2266     /* Read the first FAT sector.  */
2267     status = fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
2268 
2269     /* Add a FAT entry randomly in the FAT table.  */
2270     fat_buffer[4] =  2;
2271 
2272     /* Write the FAT corruption out.  */
2273     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
2274 
2275     /* See if we are okay.  */
2276     if (status != FX_SUCCESS)
2277     {
2278 
2279         /* Error, return error code.  */
2280         printf("ERROR!\n");
2281         test_control_return(102);
2282     }
2283 
2284     /* Now open the file with a corrupted FAT entry which will cause an error when walking the FAT chain.  */
2285     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2286 
2287     /* See if we got the FAT chain.  */
2288     if (status != FX_FAT_READ_ERROR)
2289     {
2290 
2291         /* Error, return error code.  */
2292         printf("ERROR!\n");
2293         test_control_return(103);
2294     }
2295 
2296     /* Now flush everything out.  */
2297     fx_media_flush(&ram_disk);
2298     _fx_utility_FAT_flush(&ram_disk);
2299     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2300     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2301     {
2302         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2303         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2304     }
2305 
2306     /* Read the first FAT sector.  */
2307     status = fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
2308 
2309     /* Add a FAT entry randomly in the FAT table.  */
2310     fat_buffer[4] =  0xF0;
2311     fat_buffer[5] =  0xFF;
2312 
2313     /* Write the FAT corruption out.  */
2314     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
2315 
2316     /* See if we are okay.  */
2317     if (status != FX_SUCCESS)
2318     {
2319 
2320         /* Error, return error code.  */
2321         printf("ERROR!\n");
2322         test_control_return(104);
2323     }
2324 
2325     /* Now open the file with a corrupted FAT entry which will cause an error when walking the FAT chain.  */
2326     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2327 
2328     /* See if we got the file corrupt error.  */
2329     if (status != FX_FILE_CORRUPT)
2330     {
2331 
2332         /* Error, return error code.  */
2333         printf("ERROR!\n");
2334         test_control_return(105);
2335     }
2336 
2337     /* Now flush everything out.  */
2338     fx_media_flush(&ram_disk);
2339     _fx_utility_FAT_flush(&ram_disk);
2340     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2341     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2342     {
2343         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2344         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2345     }
2346 
2347     /* Read the first FAT sector.  */
2348     status = fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
2349 
2350     /* Fix the FAT chain.  */
2351     fat_buffer[4] =  3;
2352     fat_buffer[5] =  0;
2353 
2354     /* Write the FAT corruption out.  */
2355     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
2356 
2357     /* See if we are okay.  */
2358     if (status != FX_SUCCESS)
2359     {
2360 
2361         /* Error, return error code.  */
2362         printf("ERROR!\n");
2363         test_control_return(106);
2364     }
2365 
2366     /* Now test the total clusters check when traversing the FAT chain.  */
2367     temp =  ram_disk.fx_media_total_clusters;
2368     ram_disk.fx_media_total_clusters =  4;
2369     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2370     ram_disk.fx_media_total_clusters =  temp;
2371 
2372     /* Check status.  */
2373     if (status != FX_FAT_READ_ERROR)
2374     {
2375 
2376         /* Error, return error code.  */
2377         printf("ERROR!\n");
2378         test_control_return(107);
2379     }
2380 
2381     /* Now open the file with a good FAT chain.  */
2382     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2383 
2384     /* Check status.  */
2385     if (status != FX_SUCCESS)
2386     {
2387 
2388         /* Error, return error code.  */
2389         printf("ERROR!\n");
2390         test_control_return(108);
2391     }
2392 
2393     /* Now allocate another cluster to the end of the file.  */
2394     status =  fx_file_allocate(&my_file, 256);
2395     status += fx_file_close(&my_file);
2396 
2397     /* Check status.  */
2398     if (status != FX_SUCCESS)
2399     {
2400 
2401         /* Error, return error code.  */
2402         printf("ERROR!\n");
2403         test_control_return(109);
2404     }
2405 
2406     /* Now open the file again with an extra cluster at the end of the file...  */
2407     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2408 
2409     /* Check status.  */
2410     if (status != FX_SUCCESS)
2411     {
2412 
2413         /* Error, return error code.  */
2414         printf("ERROR!\n");
2415         test_control_return(110);
2416     }
2417 
2418     /* Close the file again.  */
2419     fx_file_close(&my_file);
2420 
2421     /* Now flush everything out.  */
2422     fx_media_flush(&ram_disk);
2423     _fx_utility_FAT_flush(&ram_disk);
2424     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2425     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2426     {
2427         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2428         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2429     }
2430 
2431     /* Read the first FAT sector.  */
2432     status = fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
2433 
2434     /* Fix the FAT chain.  */
2435     fat_buffer[4] =  0;
2436     fat_buffer[5] =  0;
2437 
2438     /* Write the FAT corruption out.  */
2439     status += fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
2440 
2441     /* See if we are okay.  */
2442     if (status != FX_SUCCESS)
2443     {
2444 
2445         /* Error, return error code.  */
2446         printf("ERROR!\n");
2447         test_control_return(111);
2448     }
2449 
2450     /* Now open the file again with an invalid last cluster at the end of the file...  */
2451     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2452 
2453     /* Check status.  */
2454     if (status != FX_FILE_CORRUPT)
2455     {
2456 
2457         /* Error, return error code.  */
2458         printf("ERROR!\n");
2459         test_control_return(112);
2460     }
2461 
2462     /* Now flush everything out.  */
2463     fx_media_flush(&ram_disk);
2464     _fx_utility_FAT_flush(&ram_disk);
2465     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2466     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2467     {
2468         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2469         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2470     }
2471 
2472     /* Read the first FAT sector.  */
2473     status = fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
2474 
2475     /* Fix the FAT chain.  */
2476     fat_buffer[4] =  3;
2477     fat_buffer[5] =  0;
2478     fat_buffer[0x32] =  0;
2479     fat_buffer[0x33] =  0;
2480 
2481     /* Write the FAT corruption out.  */
2482     fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
2483 
2484     /* Now open the file again with an invalid last cluster at the end of the file...  */
2485     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2486 
2487     /* Check status.  */
2488     if (status != FX_FILE_CORRUPT)
2489     {
2490 
2491         /* Error, return error code.  */
2492         printf("ERROR!\n");
2493         test_control_return(113);
2494     }
2495 
2496     /* Now flush everything out.  */
2497     fx_media_flush(&ram_disk);
2498     _fx_utility_FAT_flush(&ram_disk);
2499     _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
2500     for (i = 0; i < FX_MAX_FAT_CACHE; i++)
2501     {
2502         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_cluster =  0;
2503         ram_disk.fx_media_fat_cache[i].fx_fat_cache_entry_value =  0;
2504     }
2505 
2506     /* Read the first FAT sector.  */
2507     status = fx_media_read(&ram_disk, 1, (VOID *) fat_buffer);
2508 
2509     /* Fix the FAT chain.  */
2510     fat_buffer[0x32] =  0xff;
2511     fat_buffer[0x33] =  0xff;
2512 
2513     /* Write the FAT corruption out.  */
2514     fx_media_write(&ram_disk, 1, (VOID *) fat_buffer);
2515 
2516     /* Open the file.  */
2517     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2518     status += fx_file_write(&my_file, buffer, 128*3);
2519     status += fx_file_seek(&my_file, 0);
2520 
2521     /* Set the flag to cause the cache update to bypass.  */
2522     _fx_utility_logical_sector_read_1_error_request =  1;
2523     status += fx_file_read(&my_file, buffer, 128*3, &actual);
2524     _fx_utility_logical_sector_read_1_error_request =  0;
2525 
2526     /* Set the flag to cause the cache update to bypass - this time with a larger error value to make sure that path is taken.  */
2527     _fx_utility_logical_sector_read_1_error_request =  1000000;
2528     status += fx_file_read(&my_file, buffer, 128*3, &actual);
2529     _fx_utility_logical_sector_read_1_error_request =  0;
2530 
2531 #ifndef FX_ENABLE_FAULT_TOLERANT
2532     /* Call the logical sector flush with a large error value to get that path taken as well.  */
2533     _fx_utility_logical_sector_flush_error_request =   1000000;
2534     status +=  _fx_utility_logical_sector_flush(&ram_disk, 0, 60000, FX_FALSE);
2535     _fx_utility_logical_sector_flush_error_request =   0;
2536 
2537     /* Check status.  */
2538     if (status != FX_SUCCESS)
2539     {
2540 
2541         /* Error, return error code.  */
2542         printf("ERROR!\n");
2543         test_control_return(116);
2544     }
2545 #endif
2546 
2547     /* Close everything down.  */
2548     fx_file_close(&my_file);
2549     fx_media_abort(&ram_disk);
2550 
2551     /* FAT32. */
2552     status = fx_media_format(&ram_disk,
2553                             _fx_ram_driver,         // Driver entry
2554                             ram_disk_memory,        // RAM disk memory pointer
2555                             cache_buffer,           // Media buffer pointer
2556                             CACHE_SIZE,             // Media buffer size
2557                             "MY_RAM_DISK",          // Volume Name
2558                             1,                      // Number of FATs
2559                             32,                     // Directory Entries
2560                             0,                      // Hidden sectors
2561                             70000,                  // Total sectors - FAT16
2562                             512,                    // Sector size
2563                             1,                      // Sectors per cluster
2564                             1,                      // Heads
2565                             1);                     // Sectors per track
2566     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
2567     status += fx_file_create(&ram_disk, "TEST.TXT");
2568     return_if_fail(FX_SUCCESS == status);
2569 
2570     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2571     status += fx_file_allocate(&my_file, 512);
2572     status += fx_file_close(&my_file);
2573     return_if_fail(FX_SUCCESS == status);
2574 
2575     /* Open the file with a bad FAT chain. */
2576     _fx_utility_FAT_entry_write(&ram_disk, 3, 0);
2577     status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
2578     return_if_fail(FX_FILE_CORRUPT == status);
2579 
2580     fx_file_close(&my_file);
2581     fx_media_close(&ram_disk);
2582 
2583     printf("SUCCESS!\n");
2584     test_control_return(0);
2585 }
2586 
2587