1 /* This FileX test concentrates on the basic media read/write sector operation.  */
2 /* While FX_FAULT_TOLERANT is not defined, fx_utility_logical_sector_write will not cache other types of sectors but FX_DATA_SECTOR. */
3 
4 #ifndef FX_STANDALONE_ENABLE
5 #include   "tx_api.h"
6 #endif
7 #include   "fx_api.h"
8 #include   "fx_utility.h"
9 #include   "fx_ram_driver_test.h"
10 #include   <stdio.h>
11 
12 void  test_control_return(UINT status);
13 #ifndef FX_DISABLE_CACHE
14 #define     DEMO_STACK_SIZE         4096
15 #define     CACHE_SIZE              256*128
16 
17 
18 /* Define the ThreadX and FileX object control blocks...  */
19 
20 #ifndef FX_STANDALONE_ENABLE
21 static TX_THREAD               ftest_0;
22 #endif
23 static FX_MEDIA                ram_disk;
24 
25 static UCHAR                   raw_sector_buffer[128];
26 
27 
28 /* Define the counters used in the test application...  */
29 
30 #ifndef FX_STANDALONE_ENABLE
31 static UCHAR                  *ram_disk_memory;
32 static UCHAR                  *direct_buffer;
33 static UCHAR                  *cache_buffer;
34 #else
35 static UCHAR                   direct_buffer[20*128];
36 static UCHAR                   cache_buffer[CACHE_SIZE];
37 #endif
38 
39 
40 /* Define thread prototypes.  */
41 
42 void    filex_media_read_write_sector_application_define(void *first_unused_memory);
43 static void    ftest_0_entry(ULONG thread_input);
44 
45 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
46 
47 
48 
49 /* Define what the initial system looks like.  */
50 
51 #ifdef CTEST
test_application_define(void * first_unused_memory)52 void test_application_define(void *first_unused_memory)
53 #else
54 void    filex_media_read_write_sector_application_define(void *first_unused_memory)
55 #endif
56 {
57 
58 #ifndef FX_STANDALONE_ENABLE
59 UCHAR    *pointer;
60 
61 
62     /* Setup the working pointer.  */
63     pointer =  (UCHAR *) first_unused_memory;
64 
65     /* Create the main thread.  */
66     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
67             pointer, DEMO_STACK_SIZE,
68             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
69 
70     pointer =  pointer + DEMO_STACK_SIZE;
71 
72     /* Setup memory for the RAM disk and the sector cache.  */
73     cache_buffer =  pointer;
74     pointer =  pointer + CACHE_SIZE;
75     direct_buffer =  pointer;
76     pointer =  pointer + 20*128;
77     ram_disk_memory =  pointer;
78 
79 #endif
80 
81     /* Initialize the FileX system.  */
82     fx_system_initialize();
83 #ifdef FX_STANDALONE_ENABLE
84     ftest_0_entry(0);
85 #endif
86 }
87 
88 
89 
90 /* Define the test threads.  */
91 
ftest_0_entry(ULONG thread_input)92 static void    ftest_0_entry(ULONG thread_input)
93 {
94 
95 UINT        status;
96 #ifdef FX_FAULT_TOLERANT
97 UINT        first_test = 1;
98 #endif /* FX_FAULT_TOLERANT */
99 
100 ULONG64     logical_sector;
101 UINT        i, j;
102 ULONG       fat_entry;
103 ULONG       temp;
104 ULONG64     accessed_sectors[300];
105 
106     FX_PARAMETER_NOT_USED(thread_input);
107 
108     /* Print out some test information banners.  */
109     printf("FileX Test:   Media read/write sector test...........................");
110 
111 #ifdef FX_FAULT_TOLERANT
112 TEST_START:
113 #endif /* FX_FAULT_TOLERANT */
114 
115     /* Format the media.  This needs to be done before opening it!  */
116     status =  fx_media_format(&ram_disk,
117                             _fx_ram_driver,         // Driver entry
118                             ram_disk_memory,        // RAM disk memory pointer
119                             cache_buffer,           // Media buffer pointer
120                             CACHE_SIZE,             // Media buffer size
121                             "MY_RAM_DISK",          // Volume Name
122                             2,                      // Number of FATs
123                             32,                     // Directory Entries
124                             0,                      // Hidden sectors
125                             256,                    // Total sectors
126                             128,                    // Sector size
127                             1,                      // Sectors per cluster
128                             1,                      // Heads
129                             1);                     // Sectors per track
130     return_if_fail( status == FX_SUCCESS);
131 
132     /* try to read before the media has been opened */
133     status =  fx_media_read(&ram_disk, 1, (VOID *) raw_sector_buffer);
134     return_if_fail( status == FX_MEDIA_NOT_OPEN);
135 
136     /* try to write before the media has been opened  */
137     status =  fx_media_write(&ram_disk, 1, (VOID *) raw_sector_buffer);
138     return_if_fail( status == FX_MEDIA_NOT_OPEN);
139 
140     /* Open the ram_disk.  */
141     status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
142     return_if_fail( status == FX_SUCCESS);
143 
144     /* try to write while the media is write protected  */
145     ram_disk.fx_media_driver_write_protect = FX_TRUE;
146     status =  fx_media_write(&ram_disk, 1, (VOID *) raw_sector_buffer);
147     ram_disk.fx_media_driver_write_protect = FX_FALSE;
148     return_if_fail( status == FX_WRITE_PROTECT);
149 
150 /* Only run this if error checking is enabled */
151 #ifndef FX_DISABLE_ERROR_CHECKING
152 
153     /* send null pointer to generate an error */
154     status = fx_media_read(FX_NULL, 0, (void *) raw_sector_buffer);
155     return_if_fail( status == FX_PTR_ERROR);
156 
157     /* send null pointer to generate an error */
158     status = fx_media_write(FX_NULL, 0, (void *) raw_sector_buffer);
159     return_if_fail( status == FX_PTR_ERROR);
160 
161 #endif /* FX_DISABLE_ERROR_CHECKING */
162 
163     /* Read the first FAT sector.  */
164     status =  fx_media_read(&ram_disk, 1, (VOID *) raw_sector_buffer);
165     return_if_fail ((status == FX_SUCCESS) &&
166         (raw_sector_buffer[0] == 0xF8) &&    /* _fx_media_format_media_type value set during media format */
167         (raw_sector_buffer[1] == 0xFF) &&
168         (raw_sector_buffer[2] == 0xFF));
169 
170     /* Write the same, unchanged sector back.  */
171     status =  fx_media_write(&ram_disk, 1, (VOID *) raw_sector_buffer);
172     return_if_fail( status == FX_SUCCESS);
173 
174     /* Close the media.  */
175     status =  fx_media_close(&ram_disk);
176     return_if_fail( status == FX_SUCCESS);
177 
178     /* Format the media.  This needs to be done before opening it!  */
179     status =  fx_media_format(&ram_disk,
180                             _fx_ram_driver,         // Driver entry
181                             ram_disk_memory,        // RAM disk memory pointer
182                             cache_buffer,           // Media buffer pointer
183                             CACHE_SIZE,             // Media buffer size
184                             "MY_RAM_DISK",          // Volume Name
185                             2,                      // Number of FATs
186                             32,                     // Directory Entries
187                             0,                      // Hidden sectors
188                             7000,                   // Total sectors
189                             128,                    // Sector size
190                             1,                      // Sectors per cluster
191                             1,                      // Heads
192                             1);                     // Sectors per track
193     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
194     return_if_fail( status == FX_SUCCESS);
195 
196     /* Test for invalid sector.  */
197     logical_sector = ram_disk.fx_media_total_sectors;
198     status = _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
199     return_if_fail( status == FX_SECTOR_INVALID);
200 
201     /* Test for invalid sector.  */
202     logical_sector =  0;
203     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
204     return_if_fail( status == FX_SECTOR_INVALID);
205 
206     /* Test for invalid sector.  */
207     logical_sector =  0xFFFFFFFF;
208     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
209     return_if_fail( status == FX_SECTOR_INVALID);
210 
211     /* Test for invalid sector.  */
212     logical_sector =  ram_disk.fx_media_total_sectors;
213     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 4, FX_DATA_SECTOR);
214     return_if_fail( status == FX_SECTOR_INVALID);
215 
216     /* Test for invalid sector.  */
217     logical_sector =  0;
218     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
219     return_if_fail( status == FX_SECTOR_INVALID);
220 
221     /* Test for invalid sector.  */
222     logical_sector =  0xFFFFFFFF;
223     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
224     return_if_fail( status == FX_SECTOR_INVALID);
225 
226     /* Test for invalid sector.  */
227     logical_sector =  ram_disk.fx_media_total_sectors;
228     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
229     return_if_fail( status == FX_SECTOR_INVALID);
230 
231     /* Write to a sector not in the cache.  */
232     status =  _fx_utility_logical_sector_write(&ram_disk, 6900, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
233 
234 
235     /* Read/write 256 sectors, filling the caches.  */
236     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
237     for (i = 0; i < 256; i++)
238     {
239 
240         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
241 #ifdef FX_FAULT_TOLERANT
242         if (first_test)
243         {
244             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
245         }
246         else
247         {
248 #endif
249             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
250 #ifdef FX_FAULT_TOLERANT
251         }
252 #endif
253         logical_sector++;
254     }
255 
256     /* At this point the cache is complete full.  */
257 
258     /* Now perform direct buffer read ram_disk.fx_media_data_sector_start on sectors before the cache.  */
259     logical_sector =  + 2;
260     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 4, FX_DIRECTORY_SECTOR);
261 
262     /* Now perform direct buffer write.  */
263 #ifdef FX_FAULT_TOLERANT
264     if (first_test)
265     {
266         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
267     }
268     else
269     {
270 #endif
271         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DIRECTORY_SECTOR);
272 #ifdef FX_FAULT_TOLERANT
273     }
274 #endif
275 
276     /* Perform a flush.  */
277     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_TRUE);
278 
279     /* Perform another flush with nothing in the cache.  */
280     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_TRUE);
281 
282     /* Read/write 256 sectors, filling the caches.  */
283     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
284     for (i = 0; i < 256; i++)
285     {
286 
287         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
288 #ifdef FX_FAULT_TOLERANT
289         if (first_test)
290         {
291             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
292         }
293         else
294         {
295 #endif
296             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
297 #ifdef FX_FAULT_TOLERANT
298         }
299 #endif
300         logical_sector++;
301     }
302 
303     /* Now perform direct buffer read ram_disk.fx_media_data_sector_start on sectors before the cache.  */
304     logical_sector = (ULONG64)-2;
305     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 4, FX_DIRECTORY_SECTOR);
306 
307     /* Now perform direct buffer write.  */
308 #ifdef FX_FAULT_TOLERANT
309     if (first_test)
310     {
311         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
312     }
313     else
314     {
315 #endif
316         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DIRECTORY_SECTOR);
317 #ifdef FX_FAULT_TOLERANT
318     }
319 #endif
320 
321     /* Perform a flush.  */
322     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_FALSE);
323 
324     /* Read/write 128 sectors, filling the caches and causing cache misses.  */
325     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
326     for (i = 0; i < 512; i++)
327     {
328 
329         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
330 #ifdef FX_FAULT_TOLERANT
331         if (first_test)
332         {
333             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
334         }
335         else
336         {
337 #endif
338             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
339 #ifdef FX_FAULT_TOLERANT
340         }
341 #endif
342         logical_sector++;
343     }
344 
345     /* Perform a flush.  */
346     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_FALSE);
347 
348     /* Read 256 sectors, filling the caches.  */
349     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
350     for (i = 0; i < 256; i++)
351     {
352 
353         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
354         logical_sector++;
355     }
356 
357     /* Write same 256 sectors in reverse order.  */
358     for (i = 0; i < 256; i++)
359     {
360 
361         logical_sector--;
362 #ifdef FX_FAULT_TOLERANT
363         if (first_test)
364         {
365             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
366         }
367         else
368         {
369 #endif
370             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
371 #ifdef FX_FAULT_TOLERANT
372         }
373 #endif
374     }
375 
376     /* Update FAT entires.  */
377     for (i = 2; i < 6000; i++)
378     {
379         status +=  _fx_utility_FAT_entry_write(&ram_disk, (ULONG) i, (ULONG) i+1);
380     }
381 
382     /* Flush the FAT.  */
383     status += _fx_utility_FAT_map_flush(&ram_disk);
384 
385     /* Indicate that the driver needs to know!  */
386     ram_disk.fx_media_driver_free_sector_update =  FX_TRUE;
387 
388     /* Update FAT entries again, but this time with free marker.  */
389     for (i = 2; i < 6000; i++)
390     {
391         status +=  _fx_utility_FAT_entry_write(&ram_disk, (ULONG) i, FX_FREE_CLUSTER);
392     }
393 
394     /* Flush the FAT.  */
395     status += _fx_utility_FAT_map_flush(&ram_disk);
396     ram_disk.fx_media_driver_free_sector_update =  FX_FALSE;
397 
398     /* Random test of 300 sectors.  */
399     for (i = 0; i < 300; i++)
400     {
401         accessed_sectors[i] =  logical_sector + (ULONG64)(rand() % 5000);
402         status += _fx_utility_logical_sector_read(&ram_disk, accessed_sectors[i], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
403         if ((i > 0) && (rand() & 1))
404         {
405             j = ((UINT)rand() % i);
406             status += _fx_utility_logical_sector_write(&ram_disk, accessed_sectors[j], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
407         }
408     }
409 
410     /* Random test of 300 sectors with random logical sector flush.  */
411     for (i = 0; i < 300; i++)
412     {
413         accessed_sectors[i] =  logical_sector + (ULONG64)(rand() % 5000);
414         status += _fx_utility_logical_sector_read(&ram_disk, accessed_sectors[i], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
415         if ((i > 0) && (rand() & 1))
416         {
417             j = ((UINT)rand() % i);
418             status += _fx_utility_logical_sector_write(&ram_disk, accessed_sectors[j], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
419         }
420         if ((i > 0) && (rand() & 1))
421         {
422             j = ((UINT)rand() % i);
423             if (rand() & 1)
424                 status += _fx_utility_logical_sector_flush(&ram_disk, accessed_sectors[j], 1, FX_TRUE);
425             else
426                 status += _fx_utility_logical_sector_flush(&ram_disk, accessed_sectors[j], 1, FX_FALSE);
427         }
428     }
429 
430     /* Random test of 1000 FAT entry reads/writes.  */
431     for (i = 0; i < 1000; i++)
432     {
433 
434         j = ((UINT)rand() % 100);
435         status +=  _fx_utility_FAT_entry_read(&ram_disk, j+2, &fat_entry);
436 
437         if (rand() & 1)
438         {
439             j = ((UINT)rand() % 100);
440             if (rand() & 1)
441                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, j+3);
442             else
443                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, FX_FREE_CLUSTER);
444         }
445     }
446 
447     /* Random test of 1000 FAT entry reads/writes with random FAT flush.  */
448     for (i = 0; i < 1000; i++)
449     {
450 
451         j = ((UINT)rand() % 100);
452         status +=  _fx_utility_FAT_entry_read(&ram_disk, j+2, &fat_entry);
453 
454         if (rand() & 1)
455         {
456             j = ((UINT)rand() % 100);
457             if (rand() & 1)
458                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, j+3);
459             else
460                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, FX_FREE_CLUSTER);
461         }
462 
463         if (rand() & 1)
464         {
465 
466             status += _fx_utility_FAT_flush(&ram_disk);
467         }
468 
469         if (rand() & 1)
470         {
471 
472             status += _fx_utility_FAT_map_flush(&ram_disk);
473         }
474     }
475     return_if_fail( status == FX_SUCCESS);
476 
477     /* Set the free sector update.  */
478     ram_disk.fx_media_driver_free_sector_update  =  FX_TRUE;
479 
480     /* Random test of 1000 FAT entry reads/writes with random FAT flush and random errors.  */
481     _fx_utility_FAT_flush(&ram_disk);
482     _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
483     _fx_ram_driver_io_error_request =  1000;
484     for (i = 0; i < 300000; i++)
485     {
486 
487         j =  ((UINT)rand() % 100);
488          _fx_utility_FAT_entry_read(&ram_disk, j+2, &fat_entry);
489 
490         if (rand() & 1)
491         {
492             j = ((UINT)rand() % 100);
493             if (rand() & 1)
494                 _fx_utility_FAT_entry_write(&ram_disk, j+2, j+3);
495             else
496                 _fx_utility_FAT_entry_write(&ram_disk, j+2, FX_FREE_CLUSTER);
497         }
498 
499         if (i & 1)
500             _fx_utility_FAT_flush(&ram_disk);
501         else
502             _fx_utility_FAT_map_flush(&ram_disk);
503         _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
504     }
505     _fx_ram_driver_io_error_request =  0;
506 
507     /* Set the free sector update.  */
508     ram_disk.fx_media_driver_free_sector_update  =  FX_FALSE;
509 
510     /* Flush out the logical sector cache.  */
511     status = _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
512 
513     /* Read/write 256 sectors, filling the caches.  */
514     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
515     for (i = 0; i < 256; i++)
516     {
517 
518         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
519 #ifdef FX_FAULT_TOLERANT
520         if (first_test)
521         {
522             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
523         }
524         else
525         {
526 #endif
527             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
528 #ifdef FX_FAULT_TOLERANT
529         }
530 #endif
531         logical_sector++;
532     }
533 
534     /* Now test the flush logic with write protection enabled.  */
535     ram_disk.fx_media_driver_write_protect =  FX_TRUE;
536     status += _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
537     ram_disk.fx_media_driver_write_protect =  FX_FALSE;
538     return_if_fail( status == FX_SUCCESS);
539 
540     /* Flush out the logical sector cache.  */
541     _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
542 
543     /* Read/write 256 sectors, filling the caches.  */
544     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
545     for (i = 0; i < 256; i++)
546     {
547 
548         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
549 #ifdef FX_FAULT_TOLERANT
550         if (first_test)
551         {
552             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
553         }
554         else
555         {
556 #endif
557             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
558 #ifdef FX_FAULT_TOLERANT
559         }
560 #endif
561         logical_sector++;
562     }
563 
564     /* Now test the flush logic with an I/O error.  */
565     _fx_ram_driver_io_error_request =  1;
566     status += _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
567     _fx_ram_driver_io_error_request =  0;
568 
569     /* Flush out the logical sector cache.  */
570     status =  _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
571 
572     /* Read/write 256 sectors, filling the caches.  */
573     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
574     for (i = 0; i < 256; i++)
575     {
576 
577         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
578 #ifdef FX_FAULT_TOLERANT
579         if (first_test)
580         {
581             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
582         }
583         else
584         {
585 #endif
586             status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
587 #ifdef FX_FAULT_TOLERANT
588         }
589 #endif
590         logical_sector++;
591     }
592 
593     /* Now test the flush logic with an I/O error.  */
594     status += _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
595 
596     /* Close the media.  */
597     status += fx_media_close(&ram_disk);
598     return_if_fail( status == FX_SUCCESS);
599 
600     /* Format the media.  This needs to be done before opening it!  */
601     status =  fx_media_format(&ram_disk,
602                             _fx_ram_driver,         // Driver entry
603                             ram_disk_memory,        // RAM disk memory pointer
604                             cache_buffer,           // Media buffer pointer
605                             CACHE_SIZE,             // Media buffer size
606                             "MY_RAM_DISK",          // Volume Name
607                             2,                      // Number of FATs
608                             32,                     // Directory Entries
609                             0,                      // Hidden sectors
610                             7000,                   // Total sectors
611                             128,                    // Sector size
612                             1,                      // Sectors per cluster
613                             1,                      // Heads
614                             1);                     // Sectors per track
615 
616     /* Open the ram_disk.  */
617     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, CACHE_SIZE-1);
618     return_if_fail( status == FX_SUCCESS);
619 
620     /* Test for invalid sector.  */
621     logical_sector =  ram_disk.fx_media_total_sectors;
622     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
623     return_if_fail( status == FX_SECTOR_INVALID);
624 
625     /* Test for invalid sector.  */
626     logical_sector =  0;
627     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
628     return_if_fail( status == FX_SECTOR_INVALID);
629 
630     /* Test for invalid sector.  */
631     logical_sector =  0xFFFFFFFF;
632     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
633     return_if_fail( status == FX_SECTOR_INVALID);
634 
635     /* Test for invalid sector.  */
636     logical_sector =  ram_disk.fx_media_total_sectors;
637     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
638     return_if_fail( status == FX_SECTOR_INVALID);
639 
640     /* Test for invalid sector.  */
641     logical_sector =  0;
642     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
643     return_if_fail( status == FX_SECTOR_INVALID);
644 
645     /* Test for invalid sector.  */
646     logical_sector =  0xFFFFFFFF;
647     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
648     return_if_fail( status == FX_SECTOR_INVALID);
649 
650     /* Test for invalid sector.  */
651     logical_sector =  ram_disk.fx_media_total_sectors;
652     status =  _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
653     return_if_fail( status == FX_SECTOR_INVALID);
654 
655     status =  0;
656 
657     /* Read/write 256 sectors, filling the caches.  */
658     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
659     for (i = 0; i < 256; i++)
660     {
661 
662         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
663         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
664         logical_sector++;
665     }
666 
667     /* At this point the cache is complete full.  */
668 
669     /* Now perform direct buffer read ram_disk.fx_media_data_sector_start on sectors before the cache.  */
670     logical_sector =  + 2;
671     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 4, FX_DIRECTORY_SECTOR);
672 
673     /* Now perform direct buffer write.  */
674     status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
675 
676     /* Perform a flush.  */
677     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_TRUE);
678 
679     /* Perform another flush with nothing in the cache.  */
680     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_TRUE);
681 
682     /* Read/write 256 sectors, filling the caches.  */
683     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
684     for (i = 0; i < 256; i++)
685     {
686 
687         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
688         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
689         logical_sector++;
690     }
691 
692     /* Now perform direct buffer read ram_disk.fx_media_data_sector_start on sectors before the cache.  */
693     logical_sector = (ULONG64)-2;
694     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 4, FX_DIRECTORY_SECTOR);
695 
696     /* Now perform direct buffer write.  */
697     status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 4, FX_DATA_SECTOR);
698 
699     /* Perform a flush.  */
700     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_FALSE);
701 
702     /* Read/write 300 sectors, filling the caches and causing cache misses.  */
703     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
704     for (i = 0; i < 300; i++)
705     {
706 
707         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
708         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
709         logical_sector++;
710     }
711 
712     /* Perform a flush.  */
713     status += _fx_utility_logical_sector_flush(&ram_disk, logical_sector, 300, FX_FALSE);
714 
715     /* Read 256 sectors, filling the caches.  */
716     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
717     for (i = 0; i < 256; i++)
718     {
719 
720         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
721         logical_sector++;
722     }
723 
724     /* Write same 256 sectors in reverse order.  */
725     for (i = 0; i < 256; i++)
726     {
727 
728         logical_sector--;
729         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
730     }
731 
732     /* Update FAT entires.  */
733     for (i = 2; i < 6000; i++)
734     {
735         status +=  _fx_utility_FAT_entry_write(&ram_disk, (ULONG) i, (ULONG) i+1);
736     }
737 
738     /* Flush the FAT.  */
739     status += _fx_utility_FAT_map_flush(&ram_disk);
740 
741     /* Indicate that the driver needs to know!  */
742     ram_disk.fx_media_driver_free_sector_update =  FX_TRUE;
743 
744     /* Update FAT entries again, but this time with free marker.  */
745     for (i = 2; i < 6000; i++)
746     {
747         status +=  _fx_utility_FAT_entry_write(&ram_disk, (ULONG) i, FX_FREE_CLUSTER);
748     }
749 
750     /* Flush the FAT.  */
751     status += _fx_utility_FAT_map_flush(&ram_disk);
752     ram_disk.fx_media_driver_free_sector_update =  FX_FALSE;
753 
754     /* Test the case where the logical sector cache has something in it, but the write hasn't been found. */
755     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
756     status += _fx_utility_logical_sector_read(&ram_disk, logical_sector+1, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
757     status += _fx_utility_logical_sector_read(&ram_disk, logical_sector+2, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
758     status += _fx_utility_logical_sector_read(&ram_disk, logical_sector+3, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
759     status += _fx_utility_logical_sector_read(&ram_disk, logical_sector+4, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
760     status += _fx_utility_logical_sector_write(&ram_disk, logical_sector+1, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
761 
762     /* Random test of 300 sectors.  */
763     for (i = 0; i < 300; i++)
764     {
765         accessed_sectors[i] = logical_sector + (ULONG64)(rand() % 5000);
766         status += _fx_utility_logical_sector_read(&ram_disk, accessed_sectors[i], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
767         if ((i > 0) && (rand() & 1))
768         {
769             j = ((UINT)rand() % i);
770             status += _fx_utility_logical_sector_write(&ram_disk, accessed_sectors[j], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
771         }
772     }
773 
774     /* Random test of 300 sectors with random logical sector flush.  */
775     for (i = 0; i < 300; i++)
776     {
777         accessed_sectors[i] = logical_sector + (ULONG64)(rand() % 5000);
778         status += _fx_utility_logical_sector_read(&ram_disk, accessed_sectors[i], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
779         if ((i > 0) && (rand() & 1))
780         {
781             j =  ((UINT)rand() % i);
782             status += _fx_utility_logical_sector_write(&ram_disk, accessed_sectors[j], ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
783         }
784         if ((i > 0) && (rand() & 1))
785         {
786             j =  ((UINT)rand() % i);
787             if (rand() & 1)
788                 status += _fx_utility_logical_sector_flush(&ram_disk, accessed_sectors[j], 1, FX_TRUE);
789             else
790                 status += _fx_utility_logical_sector_flush(&ram_disk, accessed_sectors[j], 1, FX_FALSE);
791         }
792     }
793 
794     /* Random test of 1000 FAT entry reads/writes. */
795     for (i = 0; i < 1000; i++)
796     {
797 
798         j = ((UINT)rand() % 100);
799         status +=  _fx_utility_FAT_entry_read(&ram_disk, j+2, &fat_entry);
800 
801         if (rand() & 1)
802         {
803             j = ((UINT)rand() % 100);
804             if (rand() & 1)
805                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, j+3);
806             else
807                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, FX_FREE_CLUSTER);
808         }
809     }
810 
811     /* Random test of 1000 FAT entry reads/writes with random FAT flush.  */
812     for (i = 0; i < 1000; i++)
813     {
814 
815         j = ((UINT)rand() % 100);
816         status +=  _fx_utility_FAT_entry_read(&ram_disk, j+2, &fat_entry);
817 
818         if (rand() & 1)
819         {
820             j = ((UINT)rand() % 100);
821             if (rand() & 1)
822                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, j+3);
823             else
824                 status +=  _fx_utility_FAT_entry_write(&ram_disk, j+2, FX_FREE_CLUSTER);
825         }
826 
827         if (rand() & 1)
828         {
829 
830             status += _fx_utility_FAT_flush(&ram_disk);
831         }
832 
833         if (rand() & 1)
834         {
835 
836             status += _fx_utility_FAT_map_flush(&ram_disk);
837         }
838     }
839     return_if_fail( status == FX_SUCCESS);
840 
841     /* Set the free sector update.  */
842     ram_disk.fx_media_driver_free_sector_update  =  FX_TRUE;
843 
844     /* Random test of 1000 FAT entry reads/writes with random FAT flush and random errors.  */
845     _fx_utility_FAT_flush(&ram_disk);
846     _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
847     _fx_ram_driver_io_error_request =  1000;
848     for (i = 0; i < 300000; i++)
849     {
850 
851 
852         j = ((UINT)rand() % 100);
853         _fx_utility_logical_sector_read(&ram_disk, (ULONG64) j, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
854 
855         if (rand() & 1)
856             _fx_utility_logical_sector_write(&ram_disk, (ULONG64) j, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
857 
858         j = ((UINT)rand() % 100);
859          _fx_utility_FAT_entry_read(&ram_disk, j+2, &fat_entry);
860 
861         if (rand() & 1)
862         {
863             j = ((UINT)rand() % 100);
864             if (rand() & 1)
865                 _fx_utility_FAT_entry_write(&ram_disk, j+2, j+3);
866             else
867                 _fx_utility_FAT_entry_write(&ram_disk, j+2, FX_FREE_CLUSTER);
868         }
869 
870         if (i & 1)
871             _fx_utility_FAT_flush(&ram_disk);
872         else
873             _fx_utility_FAT_map_flush(&ram_disk);
874     }
875     _fx_ram_driver_io_error_request =  0;
876 
877     /* Set the free sector update.  */
878     ram_disk.fx_media_driver_free_sector_update  =  FX_FALSE;
879 
880     /* Read/write 256 sectors, filling the caches.  */
881     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
882     for (i = 0; i < 256; i++)
883     {
884 
885         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
886         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
887         logical_sector++;
888     }
889 
890     /* Now perform direct buffer read ram_disk.fx_media_data_sector_start on sectors before the cache.  */
891     logical_sector =  ram_disk.fx_media_data_sector_start;
892     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 12, FX_DIRECTORY_SECTOR);
893 
894     /* Now perform direct buffer write.  */
895     status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, direct_buffer, 12, FX_DATA_SECTOR);
896 
897     /* Flush out the logical sector cache.  */
898     status += _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
899 
900     /* Read/write 256 sectors, filling the caches.  */
901     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
902     for (i = 0; i < 256; i++)
903     {
904 
905         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
906         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
907         logical_sector++;
908     }
909     return_if_fail( status == FX_SUCCESS);
910 
911     /* Now direct read a logical sector outside the cache and cause an I/O error in the process.  */
912     _fx_ram_driver_io_error_request =  2;
913     logical_sector =  ram_disk.fx_media_data_sector_start;
914     status =  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 1, FX_DIRECTORY_SECTOR);
915     _fx_ram_driver_io_error_request =  0;
916 
917     /* Flush out the logical sector cache.  */
918     status = _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
919 
920     /* Read/write 256 sectors, filling the caches.  */
921     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
922     for (i = 0; i < 256; i++)
923     {
924 
925         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
926         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
927         logical_sector++;
928     }
929 
930     /* Perform a direct read that exceeds the amount that can fit into the cache easily.  */
931     logical_sector =  6800;
932     status +=  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 20, FX_UNKNOWN_SECTOR);
933     return_if_fail( status == FX_SUCCESS);
934 
935     /* Flush out the logical sector cache.  */
936     status = _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
937 
938     /* Read/write 256 sectors, filling the caches.  */
939     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
940     for (i = 0; i < 256; i++)
941     {
942 
943         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
944         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
945         logical_sector++;
946     }
947 
948     /* Perform a direct read that suffers an I/O error.  */
949     logical_sector =  6800;
950     _fx_ram_driver_io_error_request =  1;
951     status +=  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 1, FX_UNKNOWN_SECTOR);
952     _fx_ram_driver_io_error_request =  0;
953 
954     /* Flush out the logical sector cache.  */
955     status = _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
956 
957     /* Read/write 256 sectors, filling the caches.  */
958     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
959     for (i = 0; i < 256; i++)
960     {
961 
962         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
963         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
964         logical_sector++;
965     }
966 
967     /* Perform a direct read that should be successful.  */
968     logical_sector =  6800;
969     status +=  _fx_utility_logical_sector_read(&ram_disk, logical_sector, direct_buffer, 1, FX_UNKNOWN_SECTOR);
970     return_if_fail( status == FX_SUCCESS);
971 
972     /* Flush out the logical sector cache.  */
973     status = _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
974 
975     /* Read/write 256 sectors, filling the caches.  */
976     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
977     for (i = 0; i < 256; i++)
978     {
979 
980         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
981         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
982         logical_sector++;
983     }
984 
985     /* Now test the flush logic with write protection enabled.  */
986     ram_disk.fx_media_driver_write_protect =  FX_TRUE;
987     status += _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
988     ram_disk.fx_media_driver_write_protect =  FX_FALSE;
989     return_if_fail( status == FX_SUCCESS);
990 
991     /* Flush out the logical sector cache.  */
992     _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
993 
994     /* Read/write 256 sectors, filling the caches.  */
995     logical_sector =  ram_disk.fx_media_data_sector_start + 4;
996     for (i = 0; i < 256; i++)
997     {
998 
999         status += _fx_utility_logical_sector_read(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DIRECTORY_SECTOR);
1000         status += _fx_utility_logical_sector_write(&ram_disk, logical_sector, ram_disk.fx_media_memory_buffer, 1, FX_DATA_SECTOR);
1001         logical_sector++;
1002     }
1003 
1004     /* Now test the flush logic with an I/O error.  */
1005     _fx_ram_driver_io_error_request =  1;
1006     status += _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
1007     _fx_ram_driver_io_error_request =  0;
1008 
1009     /* Flush the FAT.  */
1010     _fx_utility_logical_sector_flush(&ram_disk, ram_disk.fx_media_data_sector_start, 60000, FX_TRUE);
1011     _fx_utility_FAT_flush(&ram_disk);
1012 
1013     /* Now test the FAT map flush with an I/O error on the read of the primary FAT sector.  */
1014     _fx_ram_driver_io_error_request =  1;
1015     status =  _fx_utility_FAT_map_flush(&ram_disk);
1016     _fx_ram_driver_io_error_request =  0;
1017 
1018     /* Now test the FAT map flush with an I/O error on the write of the secondary FAT sector.  */
1019     _fx_ram_driver_io_error_request =  3;
1020     status =  _fx_utility_FAT_map_flush(&ram_disk);
1021     _fx_ram_driver_io_error_request =  0;
1022 
1023     /* Close the media.  */
1024     status = fx_media_close(&ram_disk);
1025     return_if_fail( status == FX_SUCCESS);
1026 
1027     /* FAT12 FAT flush I/O error testing.  */
1028 
1029     /* Format the media.  This needs to be done before opening it!  */
1030     status =  fx_media_format(&ram_disk,
1031                             _fx_ram_driver,         // Driver entry
1032                             ram_disk_memory,        // RAM disk memory pointer
1033                             cache_buffer,           // Media buffer pointer
1034                             CACHE_SIZE,             // Media buffer size
1035                             "MY_RAM_DISK",          // Volume Name
1036                             2,                      // Number of FATs
1037                             32,                     // Directory Entries
1038                             0,                      // Hidden sectors
1039                             2000,                   // Total sectors
1040                             128,                    // Sector size
1041                             1,                      // Sectors per cluster
1042                             1,                      // Heads
1043                             1);                     // Sectors per track
1044 
1045     /* Open the ram_disk.  */
1046     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, 128);
1047     return_if_fail( status == FX_SUCCESS);
1048 
1049     /* Now loop to write the FAT entries out with random I/O erorrs.  */
1050     fat_entry =  4;
1051     for (i = 0; i < 500000; i++)
1052     {
1053 
1054         _fx_ram_driver_io_error_request = ((UINT)rand() % 5);
1055         if (_fx_ram_driver_io_error_request == 0)
1056             _fx_ram_driver_io_error_request = 1;
1057         _fx_utility_FAT_entry_read(&ram_disk, fat_entry, &temp);
1058         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1059         _fx_utility_FAT_entry_write(&ram_disk, fat_entry, fat_entry+1);
1060         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1061 
1062         /* Move to next FAT entry.  */
1063         fat_entry++;
1064         if (fat_entry >= (ram_disk.fx_media_available_clusters-1))
1065             fat_entry =  4;
1066     }
1067     for (i = 0; i < 500000; i++)
1068     {
1069 
1070         _fx_ram_driver_io_error_request = ((UINT)rand() % 5);
1071         if (_fx_ram_driver_io_error_request == 0)
1072             _fx_ram_driver_io_error_request = 1;
1073         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1074         _fx_utility_FAT_entry_write(&ram_disk, fat_entry, fat_entry+1);
1075 
1076         /* Move to next FAT entry.  */
1077         fat_entry++;
1078         if (fat_entry >= (ram_disk.fx_media_available_clusters-1))
1079             fat_entry =  4;
1080     }
1081     _fx_ram_driver_io_error_request =  0;
1082 
1083     /* Set the error flag to make logical sector write return an error.  */
1084     _fx_utility_logical_sector_write_error_request =  1;
1085     status =  _fx_utility_FAT_flush(&ram_disk);
1086     _fx_utility_logical_sector_write_error_request =  70000;    /* This should not hit, but satisfy the code coverage.  */
1087     _fx_utility_FAT_flush(&ram_disk);
1088     _fx_utility_logical_sector_write_error_request =  0;
1089 
1090     /* Close the media.  */
1091     status = fx_media_close(&ram_disk);
1092     return_if_fail( status == FX_SUCCESS);
1093 
1094     /* FAT16 FAT flush I/O error testing.  */
1095 
1096     /* Format the media.  This needs to be done before opening it!  */
1097     status =  fx_media_format(&ram_disk,
1098                             _fx_ram_driver,         // Driver entry
1099                             ram_disk_memory,        // RAM disk memory pointer
1100                             cache_buffer,           // Media buffer pointer
1101                             CACHE_SIZE,             // Media buffer size
1102                             "MY_RAM_DISK",          // Volume Name
1103                             2,                      // Number of FATs
1104                             32,                     // Directory Entries
1105                             0,                      // Hidden sectors
1106                             7000,                   // Total sectors
1107                             128,                    // Sector size
1108                             1,                      // Sectors per cluster
1109                             1,                      // Heads
1110                             1);                     // Sectors per track
1111     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, 128);
1112     return_if_fail( status == FX_SUCCESS);
1113 
1114     /* Now loop to write the FAT entries out with random I/O erorrs.  */
1115     fat_entry =  4;
1116     for (i = 0; i < 500000; i++)
1117     {
1118 
1119         _fx_ram_driver_io_error_request = ((UINT)rand() % 5);
1120         if (_fx_ram_driver_io_error_request == 0)
1121             _fx_ram_driver_io_error_request = 1;
1122         _fx_utility_FAT_entry_read(&ram_disk, fat_entry, &temp);
1123         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1124         _fx_utility_FAT_entry_write(&ram_disk, fat_entry, fat_entry+1);
1125         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1126 
1127         /* Move to next FAT entry.  */
1128         fat_entry++;
1129         if (fat_entry >= (ram_disk.fx_media_available_clusters-1))
1130             fat_entry =  4;
1131     }
1132     for (i = 0; i < 500000; i++)
1133     {
1134 
1135         _fx_ram_driver_io_error_request =  ((UINT)rand() % 5);
1136         if (_fx_ram_driver_io_error_request == 0)
1137             _fx_ram_driver_io_error_request = 1;
1138         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1139         _fx_utility_FAT_entry_write(&ram_disk, fat_entry, fat_entry+1);
1140 
1141         /* Move to next FAT entry.  */
1142         fat_entry++;
1143         if (fat_entry >= (ram_disk.fx_media_available_clusters-1))
1144             fat_entry =  4;
1145     }
1146     _fx_ram_driver_io_error_request = 0;
1147 
1148     /* Set the error flag to make logical sector write return an error.  */
1149     _fx_utility_logical_sector_write_error_request =  1;
1150     status =  _fx_utility_FAT_flush(&ram_disk);
1151     _fx_utility_logical_sector_write_error_request =  70000;    /* This should not hit, but satisfy the code coverage.  */
1152     _fx_utility_FAT_flush(&ram_disk);
1153     _fx_utility_logical_sector_write_error_request =  0;
1154 
1155     /* Close the media.  */
1156     status = fx_media_close(&ram_disk);
1157     return_if_fail( status == FX_SUCCESS);
1158 
1159     /* FAT32 FAT flush I/O error testing.  */
1160 
1161     /* Format the media.  This needs to be done before opening it!  */
1162     status =  fx_media_format(&ram_disk,
1163                             _fx_ram_driver,         // Driver entry
1164                             ram_disk_memory,        // RAM disk memory pointer
1165                             cache_buffer,           // Media buffer pointer
1166                             CACHE_SIZE,             // Media buffer size
1167                             "MY_RAM_DISK",          // Volume Name
1168                             2,                      // Number of FATs
1169                             32,                     // Directory Entries
1170                             0,                      // Hidden sectors
1171                             70128,                   // Total sectors
1172                             128,                    // Sector size
1173                             1,                      // Sectors per cluster
1174                             1,                      // Heads
1175                             1);                     // Sectors per track
1176     status += fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory, cache_buffer, 128);
1177     return_if_fail( status == FX_SUCCESS);
1178 
1179     /* Now loop to write the FAT entries out with random I/O erorrs.  */
1180     fat_entry =  4;
1181     for (i = 0; i < 500000; i++)
1182     {
1183 
1184         _fx_ram_driver_io_error_request =  ((UINT)rand() % 5);
1185         if (_fx_ram_driver_io_error_request == 0)
1186             _fx_ram_driver_io_error_request = 1;
1187         _fx_utility_FAT_entry_read(&ram_disk, fat_entry, &temp);
1188         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1189         _fx_utility_FAT_entry_write(&ram_disk, fat_entry, fat_entry+1);
1190         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1191 
1192         /* Move to next FAT entry.  */
1193         fat_entry++;
1194         if (fat_entry >= (ram_disk.fx_media_available_clusters-1))
1195             fat_entry =  4;
1196     }
1197     for (i = 0; i < 500000; i++)
1198     {
1199 
1200         _fx_ram_driver_io_error_request =  ((UINT)rand() % 5);
1201         if (_fx_ram_driver_io_error_request == 0)
1202             _fx_ram_driver_io_error_request = 1;
1203         _fx_utility_logical_sector_flush(&ram_disk, 1, 60000, FX_TRUE);
1204         _fx_utility_FAT_entry_write(&ram_disk, fat_entry, fat_entry+1);
1205 
1206         /* Move to next FAT entry.  */
1207         fat_entry++;
1208         if (fat_entry >= (ram_disk.fx_media_available_clusters-1))
1209             fat_entry =  4;
1210     }
1211 
1212     _fx_ram_driver_io_error_request =  0;
1213 
1214     /* Set the error flag to make logical sector write return an error.  */
1215     _fx_utility_logical_sector_write_error_request =  1;
1216     status =  _fx_utility_FAT_flush(&ram_disk);
1217     _fx_utility_logical_sector_write_error_request =  70000;    /* This should not hit, but satisfy the code coverage.  */
1218     _fx_utility_FAT_flush(&ram_disk);
1219     _fx_utility_logical_sector_write_error_request =  0;
1220 
1221     /* Close the media.  */
1222     status = fx_media_close(&ram_disk);
1223     return_if_fail( status == FX_SUCCESS);
1224 
1225 #ifdef FX_FAULT_TOLERANT
1226     if (first_test)
1227     {
1228         first_test = 0;
1229         goto TEST_START;
1230     }
1231 #endif
1232     printf("SUCCESS!\n");
1233     test_control_return(0);
1234 }
1235 
1236 
1237 #else
1238 #ifdef CTEST
test_application_define(void * first_unused_memory)1239 void test_application_define(void *first_unused_memory)
1240 #else
1241 void    filex_media_read_write_sector_application_define(void *first_unused_memory)
1242 #endif
1243 {
1244 
1245     FX_PARAMETER_NOT_USED(first_unused_memory);
1246 
1247     /* Print out some test information banners.  */
1248     printf("FileX Test:   Media read/write sector test...........................N/A\n");
1249 
1250     test_control_return(255);
1251 }
1252 #endif
1253 
1254