1 /* This FileX test concentrates on the media with hidden sectors.  */
2 
3 #include   "fx_api.h"
4 #include   "fx_ram_driver_test.h"
5 
6 #define     DEMO_STACK_SIZE         4096
7 #define     CACHE_SIZE              128*128
8 
9 #define     HIDDEN_SECTORS          8
10 
11 #define     SECTOR_SIZE             512
12 
13 
14 /* Define the ThreadX and FileX object control blocks...  */
15 
16 #ifndef FX_STANDALONE_ENABLE
17 static TX_THREAD               ftest_0;
18 #endif
19 static FX_MEDIA                ram_disk;
20 static FX_FILE                 my_file;
21 
22 /* Define the counters used in the test application...  */
23 
24 #ifndef FX_STANDALONE_ENABLE
25 static UCHAR                  *ram_disk_memory;
26 static UCHAR                  *cache_buffer;
27 #else
28 static UCHAR                   cache_buffer[CACHE_SIZE];
29 #endif
30 
31 
32 /* Define thread prototypes.  */
33 
34 void    filex_media_hidden_sectors_test_application_define(void *first_unused_memory);
35 static void    ftest_0_entry(ULONG thread_input);
36 
37 VOID  _fx_ram_driver(FX_MEDIA *media_ptr);
38 void  test_control_return(UINT status);
39 static VOID  hidden_sectors_driver(FX_MEDIA *media_ptr);
40 
41 
42 
43 /* Define what the initial system looks like.  */
44 
45 #ifdef CTEST
test_application_define(void * first_unused_memory)46 void test_application_define(void *first_unused_memory)
47 #else
48 void    filex_media_hidden_sectors_test_application_define(void *first_unused_memory)
49 #endif
50 {
51 
52 #ifndef FX_STANDALONE_ENABLE
53 UCHAR    *pointer;
54 
55 
56     /* Setup the working pointer.  */
57     pointer =  (UCHAR *) first_unused_memory;
58 
59     /* Create the main thread.  */
60     tx_thread_create(&ftest_0, "thread 0", ftest_0_entry, 0,
61             pointer, DEMO_STACK_SIZE,
62             4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
63 
64     pointer =  pointer + DEMO_STACK_SIZE;
65 
66     /* Setup memory for the RAM disk and the sector cache.  */
67     cache_buffer =  pointer;
68     pointer =  pointer + CACHE_SIZE;
69     ram_disk_memory =  pointer;
70 
71 #endif
72 
73     /* Initialize the FileX system.  */
74     fx_system_initialize();
75 #ifdef FX_STANDALONE_ENABLE
76     ftest_0_entry(0);
77 #endif
78 }
79 
80 
81 
82 /* Define the test threads.  */
83 
ftest_0_entry(ULONG thread_input)84 static void    ftest_0_entry(ULONG thread_input)
85 {
86 
87 UINT        status;
88 
89     FX_PARAMETER_NOT_USED(thread_input);
90 
91     /* Print out some test information banners.  */
92     printf("FileX Test:   Media Hidden Sectors Test..............................");
93 
94     /* Format the media.  This needs to be done before opening it!  */
95     status =  fx_media_format(&ram_disk,
96                             hidden_sectors_driver,  // Driver entry
97                             ram_disk_memory,        // RAM disk memory pointer
98                             cache_buffer,           // Media buffer pointer
99                             CACHE_SIZE,             // Media buffer size
100                             "MY_RAM_DISK",          // Volume Name
101                             1,                      // Number of FATs
102                             32,                     // Directory Entries
103                             HIDDEN_SECTORS,         // Hidden sectors
104                             256,                    // Total sectors
105                             SECTOR_SIZE,            // Sector size
106                             1,                      // Sectors per cluster
107                             1,                      // Heads
108                             1);                     // Sectors per track
109 
110     /* Determine if the format had an error.  */
111     if (status)
112     {
113 
114         printf("ERROR!\n");
115         test_control_return(2);
116     }
117 
118     memset(&ram_disk, 0, sizeof(ram_disk));
119 
120     /* Open the ram_disk.  */
121     status =  fx_media_open(&ram_disk, "RAM DISK", hidden_sectors_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
122 
123     /* Check the status.  */
124     if (status != FX_SUCCESS)
125     {
126 
127         /* Error, return error code.  */
128         printf("ERROR!\n");
129         test_control_return(3);
130     }
131 
132     /* Create a file called TEST.TXT in the root directory.  */
133     status =  fx_file_create(&ram_disk, "TEST.TXT");
134 
135     /* Check the create status.  */
136     if (status != FX_SUCCESS)
137     {
138 
139         printf("ERROR!\n");
140         test_control_return(4);
141     }
142 
143     /* Close the media.  */
144     status =  fx_media_close(&ram_disk);
145 
146     /* Check the file open status.  */
147     if (status != FX_SUCCESS)
148     {
149 
150         printf("ERROR!\n");
151         test_control_return(5);
152     }
153 
154     memset(&ram_disk, 0, sizeof(ram_disk));
155 
156     /* Open the ram_disk.  */
157     status =  fx_media_open(&ram_disk, "RAM DISK", hidden_sectors_driver, ram_disk_memory, cache_buffer, CACHE_SIZE);
158 
159     /* Check the status.  */
160     if (status != FX_SUCCESS)
161     {
162 
163         /* Error, return error code.  */
164         printf("ERROR!\n");
165         test_control_return(6);
166     }
167 
168     /* Open the test file.  */
169     status =  fx_file_open(&ram_disk, &my_file, "TEST.TXT", FX_OPEN_FOR_WRITE);
170 
171     /* Check the file open status.  */
172     if (status != FX_SUCCESS)
173     {
174 
175         printf("ERROR!\n");
176         test_control_return(7);
177     }
178 
179     /* Close the file.  */
180     status =  fx_file_close(&my_file);
181 
182     /* Check the file open status.  */
183     if (status != FX_SUCCESS)
184     {
185 
186         printf("ERROR!\n");
187         test_control_return(8);
188     }
189 
190     /* Close the media.  */
191     status =  fx_media_close(&ram_disk);
192 
193     /* Check the file open status.  */
194     if (status != FX_SUCCESS)
195     {
196 
197         printf("ERROR!\n");
198         test_control_return(9);
199     }
200 
201     /* Determine if the test was successful.  */
202     if (status != FX_SUCCESS)
203     {
204 
205         printf("ERROR!\n");
206         test_control_return(10);
207     }
208     else
209     {
210 
211         printf("SUCCESS!\n");
212         test_control_return(0);
213     }
214 }
215 
hidden_sectors_driver(FX_MEDIA * media_ptr)216 static VOID  hidden_sectors_driver(FX_MEDIA *media_ptr)
217 {
218 UCHAR       *source_buffer;
219 UCHAR       *destination_buffer;
220 UINT        bytes_per_sector;
221 
222     if (media_ptr -> fx_media_driver_request == FX_DRIVER_BOOT_READ)
223     {
224 
225         /* Read the boot record and return to the caller.  */
226 
227         /* Calculate the RAM disk boot sector offset, which is at the very beginning of the
228            RAM disk. Note the RAM disk memory is pointed to by the fx_media_driver_info pointer,
229            which is supplied by the application in the call to fx_media_open.  */
230         source_buffer =  (UCHAR *) media_ptr -> fx_media_driver_info;
231         source_buffer += SECTOR_SIZE * HIDDEN_SECTORS;
232 
233         /* For RAM driver, determine if the boot record is valid.  */
234         if ((source_buffer[0] != (UCHAR) 0xEB)  ||
235            ((source_buffer[1] != (UCHAR) 0x34)  &&
236             (source_buffer[1] != (UCHAR) 0x76)) ||
237             (source_buffer[2] != (UCHAR) 0x90))
238         {
239 
240             /* Invalid boot record, return an error!  */
241             media_ptr -> fx_media_driver_status =  FX_MEDIA_INVALID;
242             return;
243         }
244 
245         /* For RAM disk only, pickup the bytes per sector.  */
246         bytes_per_sector =  _fx_utility_16_unsigned_read(&source_buffer[FX_BYTES_SECTOR]);
247 
248 
249         /* Ensure this is less than the destination.  */
250 
251         /* Copy the RAM boot sector into the destination.  */
252         _fx_utility_memory_copy(source_buffer, media_ptr -> fx_media_driver_buffer, bytes_per_sector);
253 
254         /* Successful driver request.  */
255         media_ptr -> fx_media_driver_status =  FX_SUCCESS;
256     }
257     else if (media_ptr -> fx_media_driver_request == FX_DRIVER_BOOT_WRITE)
258     {
259 
260         /* Write the boot record and return to the caller.  */
261 
262         /* Calculate the RAM disk boot sector offset, which is at the very beginning of the
263            RAM disk. Note the RAM disk memory is pointed to by the fx_media_driver_info pointer,
264            which is supplied by the application in the call to fx_media_open.  */
265         destination_buffer =  (UCHAR *) media_ptr -> fx_media_driver_info;
266         destination_buffer += SECTOR_SIZE * HIDDEN_SECTORS;
267 
268         /* Copy the RAM boot sector into the destination.  */
269         _fx_utility_memory_copy(media_ptr -> fx_media_driver_buffer, destination_buffer, media_ptr -> fx_media_bytes_per_sector);
270 
271         /* Successful driver request.  */
272         media_ptr -> fx_media_driver_status =  FX_SUCCESS;
273     }
274     else
275     {
276         if ((media_ptr -> fx_media_driver_request == FX_DRIVER_WRITE) || (media_ptr -> fx_media_driver_request == FX_DRIVER_READ))
277         {
278             if (media_ptr -> fx_media_hidden_sectors == 0)
279             {
280                 media_ptr -> fx_media_driver_status =  FX_IO_ERROR;
281                 return;
282             }
283         }
284         _fx_ram_driver(media_ptr);
285     }
286 }
287