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