1 /* This FileX test concentrates on the Fault-Tolerant file random seek operation.  */
2 /*
3 1. Format and open the media;
4 2. Enable fault tolerant feature;
5 3. Create and write 16K bytes into TEST.TXT;
6 4. Seek to the random position of the file;
7 5. Write random size of data into TEST.TXT;
8 6. Verify the data;
9 7. Loop 4 - 6 10000 times.
10 */
11 
12 #ifndef FX_STANDALONE_ENABLE
13 #include   "tx_api.h"
14 #else
15 #include <time.h>
16 #endif
17 #include   "fx_api.h"
18 #include   "fx_system.h"
19 #include   "fx_fault_tolerant.h"
20 #include   <stdio.h>
21 #include   "fx_ram_driver_test.h"
22 extern void    test_control_return(UINT status);
23 void    filex_fault_tolerant_file_random_seek_test_application_define(void *first_unused_memory);
24 
25 #if defined (FX_ENABLE_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT) && defined (FX_FAULT_TOLERANT_DATA)
26 
27 #define DEMO_STACK_SIZE         4096
28 #define CACHE_SIZE                  (16 * 1024)
29 #define FAULT_TOLERANT_SIZE         FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE
30 
31 #define TOTAL_SIZE              (16 * 1024)
32 
33 #define OVERWRITE_LOOP          100000
34 
35 
36 /* Define the ThreadX and FileX object control blocks...  */
37 #ifndef FX_STANDALONE_ENABLE
38 static TX_THREAD                ftest_0;
39 #endif
40 static FX_MEDIA                 ram_disk;
41 static FX_FILE                  my_file;
42 static UCHAR                    *pointer;
43 
44 /* Define the counters used in the test application...  */
45 
46 static UCHAR                    cache_buffer[CACHE_SIZE];
47 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
48 static CHAR                     write_buffer[TOTAL_SIZE];
49 static CHAR                     overwrite_buffer[TOTAL_SIZE];
50 static CHAR                     read_buffer[TOTAL_SIZE];
51 
52 #define TEST_COUNT              4            /* FAT12, 16, 32, 16.  */
53 
54 /* Define thread prototypes.  */
55 
56 static void    ftest_0_entry(ULONG thread_input);
57 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
58 extern void    test_control_return(UINT status);
59 
60 
61 
62 /* Define what the initial system looks like.  */
63 
64 #ifdef CTEST
test_application_define(void * first_unused_memory)65 void test_application_define(void *first_unused_memory)
66 #else
67 void    filex_fault_tolerant_file_random_seek_test_application_define(void *first_unused_memory)
68 #endif
69 {
70 
71 #ifndef FX_STANDALONE_ENABLE
72     /* Setup the working pointer.  */
73     pointer =  (UCHAR *) first_unused_memory;
74 
75     /* Create the main thread.  */
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     pointer =  pointer + DEMO_STACK_SIZE;
80 
81     /* Setup memory for the RAM disk and the sector cache.  */
82     pointer += DEMO_STACK_SIZE;
83 #endif
84 
85     /* Initialize the FileX system.  */
86     fx_system_initialize();
87 #ifdef FX_STANDALONE_ENABLE
88     ftest_0_entry(0);
89 #endif
90 }
91 
92 
93 
94 /* Define the test threads.  */
95 
ftest_0_entry(ULONG thread_input)96 static void    ftest_0_entry(ULONG thread_input)
97 {
98 
99 UINT        status;
100 ULONG       actual;
101 UINT        i;
102 UINT        j;
103 UINT        seek_position;
104 UINT        overwrite_position;
105 UINT        overwrite_size;
106 
107     FX_PARAMETER_NOT_USED(thread_input);
108 
109     /* Print out some test information banners.  */
110     printf("FileX Test:   Fault Tolerant File Random Seek Test...................");
111 
112     srand((UINT)time(0));
113 
114     /* Loop to test FAT 12, 16, 32.   */
115     for (i = 0; i < TEST_COUNT; i ++)
116     {
117 
118         /* Initialize the write and overwrite_buffer. */
119         for (j = 0; j < sizeof(write_buffer); j++)
120         {
121             write_buffer[j] = (CHAR)(rand() & 0xFF);
122             overwrite_buffer[j] = (CHAR)(rand() & 0xFF);
123         }
124 
125         if (i == 0)
126         {
127             /* Format the media with FAT12.  This needs to be done before opening it!  */
128             status =  fx_media_format(&ram_disk,
129                                      _fx_ram_driver,         // Driver entry
130                                      ram_disk_memory_large,  // RAM disk memory pointer
131                                      cache_buffer,           // Media buffer pointer
132                                      CACHE_SIZE,             // Media buffer size
133                                      "MY_RAM_DISK",          // Volume Name
134                                      1,                      // Number of FATs
135                                      32,                     // Directory Entries
136                                      0,                      // Hidden sectors
137                                      256,                    // Total sectors
138                                      512,                    // Sector size
139                                      8,                      // Sectors per cluster
140                                      1,                      // Heads
141                                      1);                     // Sectors per track
142         }
143         else if (i == 1)
144         {
145             /* Format the media with FAT16.  This needs to be done before opening it!  */
146             status =  fx_media_format(&ram_disk,
147                                      _fx_ram_driver,         // Driver entry
148                                      ram_disk_memory_large,  // RAM disk memory pointer
149                                      cache_buffer,           // Media buffer pointer
150                                      CACHE_SIZE,             // Media buffer size
151                                      "MY_RAM_DISK",          // Volume Name
152                                      1,                      // Number of FATs
153                                      32,                     // Directory Entries
154                                      0,                      // Hidden sectors
155                                      4200 * 8,               // Total sectors
156                                      512,                    // Sector size
157                                      8,                      // Sectors per cluster
158                                      1,                      // Heads
159                                      1);                     // Sectors per track
160         }
161         else if (i == 2)
162         {
163             /* Format the media with FAT32.  This needs to be done before opening it!  */
164             status =  fx_media_format(&ram_disk,
165                                      _fx_ram_driver,         // Driver entry
166                                      ram_disk_memory_large,  // RAM disk memory pointer
167                                      cache_buffer,           // Media buffer pointer
168                                      CACHE_SIZE,             // Media buffer size
169                                      "MY_RAM_DISK",          // Volume Name
170                                      1,                      // Number of FATs
171                                      32,                     // Directory Entries
172                                      0,                      // Hidden sectors
173                                      70000 * 8,              // Total sectors
174                                      512,                    // Sector size
175                                      8,                      // Sectors per cluster
176                                      1,                      // Heads
177                                      1);                     // Sectors per track
178         }
179         else if (i == 3)
180         {
181             /* Format the media with FAT16 with sector size 2048 bytes.  This needs to be done before opening it!  */
182             status =  fx_media_format(&ram_disk,
183                                      _fx_ram_driver,         // Driver entry
184                                      ram_disk_memory_large,  // RAM disk memory pointer
185                                      cache_buffer,           // Media buffer pointer
186                                      CACHE_SIZE,             // Media buffer size
187                                      "MY_RAM_DISK",          // Volume Name
188                                      1,                      // Number of FATs
189                                      32,                     // Directory Entries
190                                      0,                      // Hidden sectors
191                                      4200 * 8,               // Total sectors
192                                      2048,                   // Sector size
193                                      1,                      // Sectors per cluster
194                                      1,                      // Heads
195                                      1);                     // Sectors per track
196         }
197         return_if_fail(status == FX_SUCCESS);
198 
199         /* Open the ram_disk.  */
200         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
201         return_if_fail(status == FX_SUCCESS);
202 
203         /* Enable the Fault-tolerant feature.  */
204         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
205         return_if_fail(status == FX_SUCCESS);
206 
207         /* Prepare a file for test. */
208         status = fx_file_create(&ram_disk, "TEST.TXT");
209         return_if_fail(status == FX_SUCCESS);
210 
211         status = fx_file_create(&ram_disk, "TEST.TXT");
212         return_if_fail(status == FX_ALREADY_CREATED);
213 
214         status = fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
215         return_if_fail(status == FX_SUCCESS);
216 
217         /* Write data. */
218         status = fx_file_write(&my_file, &write_buffer[0], TOTAL_SIZE);
219         return_if_fail(status == FX_SUCCESS);
220 
221         for (j = 0; j < OVERWRITE_LOOP; j++)
222         {
223             seek_position = (UINT)(rand() % TOTAL_SIZE);
224             overwrite_position = (UINT)(rand() % TOTAL_SIZE);
225             if (seek_position > overwrite_position)
226             {
227                 overwrite_size = (UINT)rand() % (TOTAL_SIZE - seek_position);
228             }
229             else
230             {
231                 overwrite_size = (UINT)rand() % (TOTAL_SIZE - overwrite_position);
232             }
233 
234             /* Seek to the beginning of file. */
235             status = fx_file_relative_seek(&my_file, seek_position, FX_SEEK_BEGIN);
236             return_if_fail(status == FX_SUCCESS);
237 
238             /* Overwrite the data. */
239             memcpy(&write_buffer[seek_position], &overwrite_buffer[overwrite_position],
240                    overwrite_size);
241             status = fx_file_write(&my_file, &overwrite_buffer[overwrite_position], overwrite_size);
242             return_if_fail(status == FX_SUCCESS);
243 
244             status = fx_file_relative_seek(&my_file, 0, FX_SEEK_BEGIN);
245             return_if_fail(status == FX_SUCCESS);
246 
247             /* Read the bytes of the test file.  */
248             memset(read_buffer, 0xFF, sizeof(read_buffer));
249             status = fx_file_read(&my_file, read_buffer, sizeof(read_buffer), &actual);
250             return_if_fail((status == FX_SUCCESS) && (actual == sizeof(read_buffer)));
251 
252             /* Check the data of overwrite part. */
253             return_if_fail(memcmp(&read_buffer[0], &write_buffer[0], sizeof(write_buffer)) == 0);
254         }
255 
256         /* Close the file. */
257         status = fx_file_close(&my_file);
258         return_if_fail(status == FX_SUCCESS);
259 
260         /* Close the media.  */
261         status =  fx_media_close(&ram_disk);
262         return_if_fail(status == FX_SUCCESS);
263     }
264 
265     /* Output successful.  */
266     printf("SUCCESS!\n");
267     test_control_return(0);
268 }
269 #else
270 
271 #ifdef CTEST
test_application_define(void * first_unused_memory)272 void test_application_define(void *first_unused_memory)
273 #else
274 void    filex_fault_tolerant_file_random_seek_test_application_define(void *first_unused_memory)
275 #endif
276 {
277 
278     FX_PARAMETER_NOT_USED(first_unused_memory);
279 
280     /* Print out some test information banners.  */
281     printf("FileX Test:   Fault Tolerant File Random Seek Test...................N/A\n");
282 
283     test_control_return(255);
284 }
285 #endif
286 
287