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