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