1 /* This FileX test concentrates on the Fault-Tolerant enable feature.  */
2 /*
3 For FAT 12, 16, 32, one cluster size is 1024 bytes;
4 Check Fault tolerrant Log file:
5 Step1: Format and open the media;
6 Step2: Check the fault tolerant enable flag and boot index value;
7 Step3: Enable fault tolerant feature;
8 Step4: Check the fault tolerant enable flag, boot index value and fault tolerant header value;
9 */
10 
11 #ifndef FX_STANDALONE_ENABLE
12 #include   "tx_api.h"
13 #include   "tx_thread.h"
14 #include   "tx_timer.h"
15 #endif
16 #include   "fx_api.h"
17 #include   "fx_utility.h"
18 #include   "fx_fault_tolerant.h"
19 #include   <stdio.h>
20 #include   "fx_ram_driver_test.h"
21 extern void    test_control_return(UINT status);
22 void    filex_fault_tolerant_enable_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 UCHAR                    *pointer;
38 
39 /* Define the counters used in the test application...  */
40 
41 #ifndef FX_STANDALONE_ENABLE
42 static UCHAR                    *cache_buffer;
43 static UCHAR                    *fault_tolerant_buffer;
44 #else
45 static UCHAR                    cache_buffer[CACHE_SIZE];
46 static UCHAR                    fault_tolerant_buffer[FAULT_TOLERANT_SIZE];
47 #endif
48 
49 #define TEST_COUNT              3
50 
51 /* Define thread prototypes.  */
52 
53 static void    ftest_0_entry(ULONG thread_input);
54 extern void    _fx_ram_driver(FX_MEDIA *media_ptr);
55 extern void    test_control_return(UINT status);
56 
57 
58 /* Define what the initial system looks like.  */
59 
60 #ifdef CTEST
test_application_define(void * first_unused_memory)61 void test_application_define(void *first_unused_memory)
62 #else
63 void    filex_fault_tolerant_enable_test_application_define(void *first_unused_memory)
64 #endif
65 {
66 
67 
68 #ifndef FX_STANDALONE_ENABLE
69     /* Setup the working pointer.  */
70     pointer =  (UCHAR *) first_unused_memory;
71 
72     /* Create the main thread.  */
73 
74     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
75             pointer, DEMO_STACK_SIZE,
76             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
77 
78     pointer =  pointer + DEMO_STACK_SIZE;
79 
80     /* Setup memory for the RAM disk and the sector cache.  */
81     cache_buffer =  pointer;
82     pointer += CACHE_SIZE;
83     fault_tolerant_buffer = pointer;
84     pointer += FAULT_TOLERANT_SIZE;
85 #endif
86 
87     /* Initialize the FileX system.  */
88     fx_system_initialize();
89 #ifdef FX_STANDALONE_ENABLE
90     ftest_0_entry(0);
91 #endif
92 }
93 
94 
95 /* Define the test threads.  */
96 
ftest_0_entry(ULONG thread_input)97 static void    ftest_0_entry(ULONG thread_input)
98 {
99 
100 UINT        status, temp1;
101 UINT        i = 0, j;
102 ULONG       start_cluster;
103 ULONG       clusters;
104 ULONG       bytes_per_cluster;
105 ULONG       value;
106 ULONG       start_sector, temp;
107 UCHAR       buffer[2048];
108 UCHAR       *source_buffer;
109 ULONG       checksum;
110 UCHAR       flag;
111 UINT        size;
112 
113     FX_PARAMETER_NOT_USED(thread_input);
114 
115     /* Print out some test information banners.  */
116     printf("FileX Test:   Fault Tolerant Enable Test.............................");
117 
118     /* Loop to test FAT 12, 16, 32.   */
119     for (i = 0; i < TEST_COUNT; i ++)
120     {
121         if (i == 0)
122         {
123             /* Format the media with FAT12.  This needs to be done before opening it!  */
124             status =  fx_media_format(&ram_disk,
125                                      _fx_ram_driver,         // Driver entry
126                                      ram_disk_memory_large,  // RAM disk memory pointer
127                                      cache_buffer,           // Media buffer pointer
128                                      CACHE_SIZE,             // Media buffer size
129                                      "MY_RAM_DISK",          // Volume Name
130                                      1,                      // Number of FATs
131                                      32,                     // Directory Entries
132                                      0,                      // Hidden sectors
133                                      256,                    // Total sectors
134                                      256,                    // Sector size
135                                      8,                      // Sectors per cluster
136                                      1,                      // Heads
137                                      1);                     // Sectors per track
138         }
139         else if (i == 1)
140         {
141             /* Format the media with FAT16.  This needs to be done before opening it!  */
142             status =  fx_media_format(&ram_disk,
143                                      _fx_ram_driver,         // Driver entry
144                                      ram_disk_memory_large,  // RAM disk memory pointer
145                                      cache_buffer,           // Media buffer pointer
146                                      CACHE_SIZE,             // Media buffer size
147                                      "MY_RAM_DISK",          // Volume Name
148                                      1,                      // Number of FATs
149                                      32,                     // Directory Entries
150                                      0,                      // Hidden sectors
151                                      4200 * 8,               // Total sectors
152                                      256,                    // Sector size
153                                      8,                      // Sectors per cluster
154                                      1,                      // Heads
155                                      1);                     // Sectors per track
156         }
157         else if (i == 2)
158         {
159             /* Format the media with FAT32.  This needs to be done before opening it!  */
160             status =  fx_media_format(&ram_disk,
161                                      _fx_ram_driver,         // Driver entry
162                                      ram_disk_memory_large,  // RAM disk memory pointer
163                                      cache_buffer,           // Media buffer pointer
164                                      CACHE_SIZE,             // Media buffer size
165                                      "MY_RAM_DISK",          // Volume Name
166                                      1,                      // Number of FATs
167                                      32,                     // Directory Entries
168                                      0,                      // Hidden sectors
169                                      70000 * 8,              // Total sectors
170                                      256,                    // Sector size
171                                      8,                      // Sectors per cluster
172                                      1,                      // Heads
173                                      1);                     // Sectors per track
174         }
175         return_if_fail( status == FX_SUCCESS);
176 
177         /* Open the ram_disk.  */
178         status =  fx_media_open(&ram_disk, "RAM DISK", _fx_ram_driver, ram_disk_memory_large, cache_buffer, CACHE_SIZE);
179         return_if_fail( status == FX_SUCCESS);
180         return_if_fail(ram_disk.fx_media_fault_tolerant_enabled != FX_TRUE);
181 
182         /* Read the boot sector.  */
183         source_buffer = ((UCHAR *) ram_disk.fx_media_driver_info);
184 
185         /* Copy the RAM sector into the destination.  */
186         _fx_utility_memory_copy(source_buffer, buffer, ram_disk.fx_media_bytes_per_sector);
187 
188         /* Check whether the boot index is used. */
189         start_cluster = _fx_utility_32_unsigned_read(buffer + FX_FAULT_TOLERANT_BOOT_INDEX);
190         return_if_fail( start_cluster == 0);
191 
192         /* Atempt to call fx_fault_tolerant_enable with invalid parameters. */
193         temp = ram_disk.fx_media_sectors_per_cluster;
194         ram_disk.fx_media_sectors_per_cluster = 0;
195         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
196         ram_disk.fx_media_sectors_per_cluster = temp;
197         return_if_fail( status == FX_MEDIA_INVALID);
198 
199         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, 0);
200         return_if_fail( status == FX_NOT_ENOUGH_MEMORY);
201 
202         /* Make an IO ERROR while reading boot sector. */
203         _fx_ram_driver_io_error_request = 1;
204         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
205         return_if_fail( status == FX_BOOT_ERROR);
206 
207         /* What will happen if there is no space? */
208         temp = ram_disk.fx_media_available_clusters;
209         ram_disk.fx_media_available_clusters = 0;
210         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
211         return_if_fail( status == FX_NO_MORE_SPACE);
212         ram_disk.fx_media_available_clusters = temp;
213 
214         temp = ram_disk.fx_media_total_clusters;
215         ram_disk.fx_media_total_clusters = 0;
216         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
217         return_if_fail( status == FX_NO_MORE_SPACE);
218         ram_disk.fx_media_total_clusters = temp;
219 
220         /* Make IO error in _fx_fault_tolerant_reset_log_file. */
221         if ( i == 0)
222         {
223             _fx_ram_driver_io_error_request = 4;
224             status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
225             return_if_fail( status == FX_IO_ERROR);
226         }
227 
228         /* Enable the Fault-tolerant feature.  */
229         status = fx_fault_tolerant_enable(&ram_disk, fault_tolerant_buffer, FAULT_TOLERANT_SIZE);
230         return_if_fail( status == FX_SUCCESS);
231 
232         /* Check the fault tolerant feature flag.  */
233         return_if_fail( ram_disk.fx_media_fault_tolerant_enabled == FX_TRUE);
234 
235         /* Atempt to exceed the space limitation in _fx_fault_tolerant_add_FAT_log. */
236         temp = ram_disk.fx_media_fault_tolerant_file_size;
237         temp1 = ram_disk.fx_media_fault_tolerant_state;
238         ram_disk.fx_media_fault_tolerant_state |= FX_FAULT_TOLERANT_STATE_STARTED;
239         ram_disk.fx_media_fault_tolerant_state &= (UCHAR)~FX_FAULT_TOLERANT_STATE_SET_FAT_CHAIN;
240         ram_disk.fx_media_fault_tolerant_file_size = 0xffff0000;
241         status = _fx_utility_FAT_entry_write( &ram_disk, 1, 2);
242         ram_disk.fx_media_fault_tolerant_file_size = temp;
243         ram_disk.fx_media_fault_tolerant_state = (UCHAR)temp1;
244         return_if_fail( status == FX_NO_MORE_SPACE);
245 
246         /* Read the boot sector.  */
247         source_buffer = ((UCHAR *) ram_disk.fx_media_driver_info);
248 
249         /* Copy the RAM sector into the destination.  */
250         _fx_utility_memory_copy(source_buffer, buffer, ram_disk.fx_media_bytes_per_sector);
251 
252         /* Check whether the boot index is used. */
253         start_cluster = _fx_utility_32_unsigned_read(buffer + FX_FAULT_TOLERANT_BOOT_INDEX);
254         return_if_fail( start_cluster != 0);
255 
256         /* Get the FAT table address.  */
257         source_buffer = ((UCHAR *) ram_disk.fx_media_driver_info) + ((ram_disk.fx_media_reserved_sectors + ram_disk.fx_media_hidden_sectors) * ram_disk.fx_media_bytes_per_sector);
258 
259         bytes_per_cluster = ram_disk.fx_media_bytes_per_sector * ram_disk.fx_media_sectors_per_cluster;
260         clusters = (FX_FAULT_TOLERANT_MINIMAL_BUFFER_SIZE + bytes_per_cluster - 1) / bytes_per_cluster;
261 
262         for (j = 0; j < clusters; j++)
263         {
264             status = _fx_utility_FAT_entry_read(&ram_disk, start_cluster + j, &value);
265             return_if_fail (status == FX_SUCCESS);
266 
267             if (j < (clusters - 1))
268             {
269                 return_if_fail (value == (start_cluster + j + 1));
270             }
271             else
272             {
273                 return_if_fail (value == ram_disk.fx_media_fat_last);
274             }
275         }
276 
277         /* Get the start sector. */
278         start_sector = (start_cluster - FX_FAT_ENTRY_START) * ram_disk.fx_media_sectors_per_cluster + ram_disk.fx_media_data_sector_start;
279 
280         /* Read the log sector.  */
281         source_buffer =  ((UCHAR *) ram_disk.fx_media_driver_info) + ((start_sector + ram_disk.fx_media_hidden_sectors) * ram_disk.fx_media_bytes_per_sector);
282 
283         /* Copy the one cluster into the destination.  */
284         _fx_utility_memory_copy(source_buffer, buffer, 1 * ram_disk.fx_media_sectors_per_cluster * ram_disk.fx_media_bytes_per_sector);
285 
286         /* Verify ID field. */
287         return_if_fail(_fx_utility_32_unsigned_read(buffer) == FX_FAULT_TOLERANT_ID);
288 
289         /* Get the header size.  */
290         size = _fx_utility_16_unsigned_read(buffer + 4);
291 
292         /* Verify Size field. */
293         return_if_fail( size == FX_FAULT_TOLERANT_LOG_HEADER_SIZE + FX_FAULT_TOLERANT_FAT_CHAIN_SIZE);
294 
295         /* Calculate checksum of header. */
296         checksum = _fx_fault_tolerant_calculate_checksum(buffer, FX_FAULT_TOLERANT_LOG_HEADER_SIZE);
297         return_if_fail( checksum == 0);
298 
299         /* Verify checksum of undo logs. */
300         checksum = _fx_fault_tolerant_calculate_checksum(buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET, FX_FAULT_TOLERANT_FAT_CHAIN_SIZE);
301         return_if_fail( checksum == 0);
302 
303         /* Check undo log. */
304         flag = *(buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET + 2);
305         return_if_fail( flag == 0);
306 
307         /* Close the media.  */
308         status =  fx_media_close(&ram_disk);
309         return_if_fail( status == FX_SUCCESS);
310     }
311 
312     /* Output successful.  */
313     printf("SUCCESS!\n");
314     test_control_return(0);
315 }
316 
317 #else
318 
319 #ifdef CTEST
test_application_define(void * first_unused_memory)320 void test_application_define(void *first_unused_memory)
321 #else
322 void    filex_fault_tolerant_enable_test_application_define(void *first_unused_memory)
323 #endif
324 {
325 
326     FX_PARAMETER_NOT_USED(first_unused_memory);
327 
328     /* Print out some test information banners.  */
329     printf("FileX Test:   Fault Tolerant Enable Test.............................N/A\n");
330 
331     test_control_return(255);
332 }
333 #endif
334