1 /* This FileX test concentrates on the Fault-Tolerant delete large data test.  */
2 /*
3 For FAT 12, 16, 32, one cluster size is 1024 bytes;
4 Check media full operation:
5 Step1: Format and open the media;
6 Step2: Enable fault tolerant feature;
7 Step3: Create new file called "TEST.TXT";
8 Step4: Get the media available bytes;
9 Step5: Delete large file from the media;
10 Step6: Check the media available bytes.
11 Step7: Loop to read and check the file data from the media;
12 */
13 #ifndef FX_STANDALONE_ENABLE
14 #include   "tx_api.h"
15 #endif
16 #include   "fx_api.h"
17 #include   "fx_fault_tolerant.h"
18 #include   <stdio.h>
19 #include   <time.h>
20 #include   "fx_ram_driver_test.h"
21 extern void    test_control_return(UINT status);
22 void    filex_fault_tolerant_delete_large_data_test_application_define(void *first_unused_memory);
23 
24 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT)
25 
26 #define DEMO_STACK_SIZE         4096
27 #define CACHE_SIZE                  2048
28 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
29 
30 
31 /* Define the ThreadX and FileX object control blocks...  */
32 
33 #ifndef FX_STANDALONE_ENABLE
34 static TX_THREAD                ftest_0;
35 #endif
36 static FX_MEDIA                 ram_disk;
37 static FX_FILE                  my_file;
38 static UCHAR                    *pointer;
39 
40 /* Define the counters used in the test application...  */
41 
42 #ifndef FX_STANDALONE_ENABLE
43 static UCHAR                    *cache_buffer;
44 static UCHAR                    *fault_tolerant_buffer;
45 #else
46 static UCHAR                    cache_buffer[CACHE_SIZE];
47 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
48 #endif
49 
50 #define TEST_COUNT              3
51 
52 /* Define thread prototypes.  */
53 
54 static void    ftest_0_entry(ULONG thread_input);
55 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
56 extern void    test_control_return(UINT status);
57 
58 
59 /* Define what the initial system looks like.  */
60 
61 #ifdef CTEST
test_application_define(void * first_unused_memory)62 void test_application_define(void *first_unused_memory)
63 #else
64 void    filex_fault_tolerant_delete_large_data_test_application_define(void *first_unused_memory)
65 #endif
66 {
67 
68 
69 
70 #ifndef FX_STANDALONE_ENABLE
71     /* Setup the working pointer.  */
72     pointer =  (UCHAR *) first_unused_memory;
73 
74     /* Create the main thread.  */
75 
76     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
77             pointer, DEMO_STACK_SIZE,
78             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
79 
80     pointer =  pointer + DEMO_STACK_SIZE;
81 
82     /* Setup memory for the RAM disk and the sector cache.  */
83     cache_buffer =  pointer;
84     pointer += CACHE_SIZE;
85     fault_tolerant_buffer = pointer;
86     pointer += FAULT_TOLERANT_SIZE;
87 #endif
88 
89     /* Initialize the FileX system.  */
90     fx_system_initialize();
91 #ifdef FX_STANDALONE_ENABLE
92     ftest_0_entry(0);
93 #endif
94 }
95 
96 
97 /* Define the test threads.  */
98 
ftest_0_entry(ULONG thread_input)99 static void    ftest_0_entry(ULONG thread_input)
100 {
101 
102 UINT        status;
103 ULONG       actual;
104 ULONG       available_bytes;
105 ULONG       i, j;
106 ULONG       data_value;
107 
108     FX_PARAMETER_NOT_USED(thread_input);
109 
110     /* Print out some test information banners.  */
111     printf("FileX Test:   Fault Tolerant Delete Large Data Test..................");
112 
113     /* Generate a random number for write data.  */
114     srand((ULONG)time(NULL));
115     data_value = (ULONG)(rand() & 0xFF) | (ULONG)((rand() & 0xFF) << 8) | (ULONG)((rand() & 0xFF) << 16) | (ULONG)((rand() & 0xFF) << 24);
116 
117     /* Genearte the write data.  */
118     for (j = 0; j < large_data_buffer_size / sizeof(ULONG); j ++)
119     {
120         ((ULONG*)large_data_buffer)[j] = data_value++;
121     }
122 
123     /* Roll back the value for later verification use.  */
124     data_value -= (large_data_buffer_size / sizeof(ULONG));
125 
126     /* Loop to test FAT 12, 16, 32.   */
127     for (i = 0; i < TEST_COUNT; i ++)
128     {
129         if (i == 0)
130         {
131             /* Format the media with FAT12.  This needs to be done before opening it!  */
132             status =  fx_media_format(&ram_disk,
133                                      _fx_ram_driver,         // Driver entry
134                                      ram_disk_memory_large,  // RAM disk memory pointer
135                                      cache_buffer,           // Media buffer pointer
136                                      CACHE_SIZE,             // Media buffer size
137                                      "MY_RAM_DISK",          // Volume Name
138                                      1,                      // Number of FATs
139                                      32,                     // Directory Entries
140                                      0,                      // Hidden sectors
141                                      4000 * 8,               // Total sectors
142                                      256,                    // Sector size
143                                      8,                      // Sectors per cluster
144                                      1,                      // Heads
145                                      1);                     // Sectors per track
146         }
147         else if (i == 1)
148         {
149             /* Format the media with FAT16.  This needs to be done before opening it!  */
150             status =  fx_media_format(&ram_disk,
151                                      _fx_ram_driver,         // Driver entry
152                                      ram_disk_memory_large,  // RAM disk memory pointer
153                                      cache_buffer,           // Media buffer pointer
154                                      CACHE_SIZE,             // Media buffer size
155                                      "MY_RAM_DISK",          // Volume Name
156                                      1,                      // Number of FATs
157                                      32,                     // Directory Entries
158                                      0,                      // Hidden sectors
159                                      60000 * 8,              // Total sectors
160                                      256,                    // Sector size
161                                      8,                      // Sectors per cluster
162                                      1,                      // Heads
163                                      1);                     // Sectors per track
164         }
165         else if (i == 2)
166         {
167             /* Format the media with FAT32.  This needs to be done before opening it!  */
168             status =  fx_media_format(&ram_disk,
169                                      _fx_ram_driver,         // Driver entry
170                                      ram_disk_memory_large,  // RAM disk memory pointer
171                                      cache_buffer,           // Media buffer pointer
172                                      CACHE_SIZE,             // Media buffer size
173                                      "MY_RAM_DISK",          // Volume Name
174                                      1,                      // Number of FATs
175                                      32,                     // Directory Entries
176                                      0,                      // Hidden sectors
177                                      400000 * 8,             // Total sectors
178                                      256,                    // Sector size
179                                      8,                      // Sectors per cluster
180                                      1,                      // Heads
181                                      1);                     // Sectors per track
182         }
183 
184         /* Determine if the format had an error.  */
185         if (status)
186         {
187 
188             printf("ERROR!\n");
189             test_control_return(1);
190         }
191 
192         /* Open the ram_disk.  */
193         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
194 
195         /* Check the status.  */
196         if (status != FX_SUCCESS)
197         {
198 
199             /* Error, return error code.  */
200             printf("ERROR!\n");
201             test_control_return(2);
202         }
203 
204         /* Enable the Fault-tolerant feature.  */
205         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
206 
207         /* Check status.   */
208         if (status)
209         {
210 
211             printf("ERROR!\n");
212             test_control_return(3);
213         }
214 
215         /* Create a file called TEST.TXT in the root directory.  */
216         status =  fx_file_create(&ram_disk, "TEST.TXT");
217 
218         /* Check the create status.  */
219         if (status != FX_SUCCESS)
220         {
221 
222             printf("ERROR!\n");
223             test_control_return(4);
224         }
225 
226         /* Open the test file.  */
227         status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
228 
229         /* Check the file open status.  */
230         if (status != FX_SUCCESS)
231         {
232 
233             printf("ERROR!\n");
234             test_control_return(5);
235         }
236 
237         /* Pickup the available bytes in the media.  */
238         status =  fx_media_space_available(&ram_disk, &available_bytes);
239 
240         /* Check for available bytes error.  */
241         if ((status != FX_SUCCESS) || (available_bytes < sizeof(ULONG)))
242         {
243 
244             printf("ERROR!\n");
245             test_control_return(6);
246         }
247 
248         /* Write the data to fill the media one time.  */
249         status =  fx_file_write(&my_file, (void *) large_data_buffer, available_bytes);
250 
251         /* Check the file write status.  */
252         if (status != FX_SUCCESS)
253         {
254 
255             printf("ERROR!\n");
256             test_control_return(7);
257         }
258 
259         /* Pickup the available bytes in the media again.  */
260         status =  fx_media_space_available(&ram_disk, &j);
261 
262         /* Check for available bytes error.  */
263         if ((status != FX_SUCCESS) || (j != 0))
264         {
265 
266             printf("ERROR!\n");
267             test_control_return(8);
268         }
269 
270 #ifndef FX_DISABLE_CACHE
271         /* At this point, we should invalidate the (which also flushes the cache) media to ensure that all
272         dirty sectors are written.  */
273         status =  fx_media_cache_invalidate(&ram_disk);
274 
275         /* Check for flush errors.  */
276         if ((status != FX_SUCCESS) || (ram_disk.fx_media_sector_cache_dirty_count))
277         {
278 
279             printf("ERROR!\n");
280             test_control_return(9);
281         }
282 
283         /* See if any sectors are still valid in the cache.  */
284         for (j = 0; j < ram_disk.fx_media_sector_cache_size; j++)
285         {
286 
287             /* Determine if this cache entry is still valid.  */
288             if (ram_disk.fx_media_sector_cache[j].fx_cached_sector_valid)
289             {
290 
291                 printf("ERROR!\n");
292                 test_control_return(10);
293             }
294         }
295 #endif /* FX_DISABLE_CACHE */
296 
297         /* Seek to the beginning of the test file.  */
298         status =  fx_file_seek(&my_file, 0);
299 
300         /* Check the file seek status.  */
301         if (status != FX_SUCCESS)
302         {
303 
304             printf("ERROR!\n");
305             test_control_return(11);
306         }
307 
308         /* Now read in all the bytes again to make sure the file contents are really there.  */
309         status =  fx_file_read(&my_file, (void *) large_data_buffer, large_data_buffer_size, &actual);
310 
311         /* Check the file read status.  */
312         if ((status != FX_SUCCESS) || (actual != available_bytes))
313         {
314 
315             printf("ERROR!\n");
316             test_control_return(12);
317         }
318 
319         /* Close the test file.  */
320         status =  fx_file_close(&my_file);
321 
322         /* Check the file close status.  */
323         if (status != FX_SUCCESS)
324         {
325 
326             printf("ERROR!\n");
327             test_control_return(13);
328         }
329 
330         /* Delete the data from the file.  */
331         status =  fx_file_delete(&ram_disk, "TEST.TXT");
332 
333         /* Check the file write status.  */
334         if (status != FX_SUCCESS)
335         {
336 
337             printf("ERROR!\n");
338             test_control_return(14);
339         }
340 
341         /* Close the media.  */
342         status =  fx_media_close(&ram_disk);
343 
344         /* Determine if the test was successful.  */
345         if (status != FX_SUCCESS)
346         {
347 
348             printf("ERROR!\n");
349             test_control_return(15);
350         }
351     }
352 
353     /* Output successful.  */
354     printf("SUCCESS!\n");
355     test_control_return(0);
356 }
357 
358 #else
359 
360 #ifdef CTEST
test_application_define(void * first_unused_memory)361 void test_application_define(void *first_unused_memory)
362 #else
363 void    filex_fault_tolerant_delete_large_data_test_application_define(void *first_unused_memory)
364 #endif
365 {
366 
367     FX_PARAMETER_NOT_USED(first_unused_memory);
368 
369     /* Print out some test information banners.  */
370     printf("FileX Test:   Fault Tolerant Delete Large Data Test..................N/A\n");
371 
372     test_control_return(255);
373 }
374 #endif
375