1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
10 |
|
|
**************************************************************************/ |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/**************************************************************************/ |
15 |
|
|
/** */ |
16 |
|
|
/** FileX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** Media */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
#define FX_SOURCE_CODE |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
/* Include necessary system files. */ |
27 |
|
|
|
28 |
|
|
#include "fx_api.h" |
29 |
|
|
#include "fx_system.h" |
30 |
|
|
#include "fx_directory.h" |
31 |
|
|
#include "fx_media.h" |
32 |
|
|
#include "fx_utility.h" |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
/**************************************************************************/ |
36 |
|
|
/* */ |
37 |
|
|
/* FUNCTION RELEASE */ |
38 |
|
|
/* */ |
39 |
|
|
/* _fx_media_volume_set PORTABLE C */ |
40 |
|
|
/* 6.1 */ |
41 |
|
|
/* AUTHOR */ |
42 |
|
|
/* */ |
43 |
|
|
/* William E. Lamie, Microsoft Corporation */ |
44 |
|
|
/* */ |
45 |
|
|
/* DESCRIPTION */ |
46 |
|
|
/* */ |
47 |
|
|
/* This function sets the volume name to the name supplied by the */ |
48 |
|
|
/* caller. */ |
49 |
|
|
/* */ |
50 |
|
|
/* INPUT */ |
51 |
|
|
/* */ |
52 |
|
|
/* media_ptr Media control block pointer */ |
53 |
|
|
/* volume_name New volume name */ |
54 |
|
|
/* */ |
55 |
|
|
/* OUTPUT */ |
56 |
|
|
/* */ |
57 |
|
|
/* return status */ |
58 |
|
|
/* */ |
59 |
|
|
/* CALLS */ |
60 |
|
|
/* */ |
61 |
|
|
/* _fx_directory_entry_read Read a directory entry */ |
62 |
|
|
/* _fx_utility_logical_sector_read Read directory sector */ |
63 |
|
|
/* _fx_utility_logical_sector_write Write directory sector */ |
64 |
|
|
/* */ |
65 |
|
|
/* CALLED BY */ |
66 |
|
|
/* */ |
67 |
|
|
/* Application Code */ |
68 |
|
|
/* */ |
69 |
|
|
/**************************************************************************/ |
70 |
|
1028 |
UINT _fx_media_volume_set(FX_MEDIA *media_ptr, CHAR *volume_name) |
71 |
|
|
{ |
72 |
|
|
|
73 |
|
|
ULONG i, j; |
74 |
|
|
FX_DIR_ENTRY dir_entry, dir_entry1; |
75 |
|
|
UINT status, offset; |
76 |
|
|
UCHAR *work_ptr; |
77 |
|
|
CHAR alpha; |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
/* Check the media to make sure it is open. */ |
81 |
✓✓ |
1028 |
if (media_ptr -> fx_media_id != FX_MEDIA_ID) |
82 |
|
|
{ |
83 |
|
|
|
84 |
|
|
/* Return the media not opened error. */ |
85 |
|
1001 |
return(FX_MEDIA_NOT_OPEN); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
27 |
dir_entry.fx_dir_entry_log_sector = 0; |
89 |
|
27 |
dir_entry.fx_dir_entry_byte_offset = 0; |
90 |
|
|
|
91 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
92 |
|
|
FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_VOLUME_SET, media_ptr, volume_name, 0, 0, FX_TRACE_MEDIA_EVENTS, 0, 0) |
93 |
|
|
|
94 |
|
|
/* Protect against other threads accessing the media. */ |
95 |
|
27 |
FX_PROTECT |
96 |
|
|
|
97 |
|
|
|
98 |
|
|
/* First, check for an invalid volume name. */ |
99 |
✓✓ |
27 |
if (volume_name[0] == 0) |
100 |
|
|
{ |
101 |
|
|
|
102 |
|
|
/* Yes, volume name is invalid. Return an error. */ |
103 |
|
1 |
return(FX_INVALID_NAME); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
/* Read the logical directory sector 0 - we just do this to get a memory_buffer pointer */ |
107 |
|
26 |
status = _fx_utility_logical_sector_read(media_ptr, ((ULONG64) 0), |
108 |
|
26 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); |
109 |
|
|
|
110 |
|
|
/* Check the read status. */ |
111 |
✓✓ |
26 |
if (status != FX_SUCCESS) |
112 |
|
|
{ |
113 |
|
|
|
114 |
|
|
/* Release media protection. */ |
115 |
|
1 |
FX_UNPROTECT |
116 |
|
|
|
117 |
|
|
/* Return the error status. */ |
118 |
|
1 |
return(status); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
122 |
|
|
|
123 |
|
|
/* Increment the number of driver read boot sector requests. */ |
124 |
|
25 |
media_ptr -> fx_media_driver_boot_read_requests++; |
125 |
|
|
#endif |
126 |
|
|
|
127 |
|
|
/* Build a driver request to read the boot record. */ |
128 |
|
25 |
media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_READ; |
129 |
|
25 |
media_ptr -> fx_media_driver_status = FX_IO_ERROR; |
130 |
|
25 |
media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_memory_buffer; |
131 |
|
25 |
media_ptr -> fx_media_driver_sectors = 1; |
132 |
|
25 |
media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR; |
133 |
|
|
|
134 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
135 |
|
|
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_READ, media_ptr, media_ptr -> fx_media_memory_buffer, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) |
136 |
|
|
|
137 |
|
|
/* Invoke the driver to read the boot sector. */ |
138 |
|
25 |
(media_ptr -> fx_media_driver_entry) (media_ptr); |
139 |
|
|
|
140 |
|
|
/* Determine if the request is successful. */ |
141 |
✓✓ |
25 |
if (media_ptr -> fx_media_driver_status) |
142 |
|
|
{ |
143 |
|
|
|
144 |
|
|
/* Release media protection. */ |
145 |
|
1 |
FX_UNPROTECT |
146 |
|
|
|
147 |
|
|
/* An error occurred in the driver. */ |
148 |
|
1 |
return(media_ptr -> fx_media_driver_status); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
/* Calculate the offset based on the FAT present. */ |
152 |
✓✓ |
24 |
if (media_ptr -> fx_media_32_bit_FAT) |
153 |
|
|
{ |
154 |
|
|
|
155 |
|
|
/* FAT32 is present. */ |
156 |
|
5 |
offset = FX_VOLUME_LABEL_32; |
157 |
|
|
} |
158 |
|
|
else |
159 |
|
|
{ |
160 |
|
|
|
161 |
|
|
/* FAT12/16 is present. */ |
162 |
|
19 |
offset = FX_VOLUME_LABEL; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
/* Loop to store the volume name. */ |
166 |
✓✓ |
169 |
for (i = 0; volume_name[i]; i++) |
167 |
|
|
{ |
168 |
|
|
|
169 |
|
|
/* Have we reached the end? */ |
170 |
✓✓ |
147 |
if (i == 11) |
171 |
|
|
{ |
172 |
|
|
|
173 |
|
2 |
break; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
/* Pickup volume name byte. */ |
177 |
|
145 |
alpha = volume_name[i]; |
178 |
|
|
|
179 |
|
|
/* Determine if alpha needs to be converted to upper case. */ |
180 |
✓✓✓✓
|
145 |
if ((alpha >= 'a') && (alpha <= 'z')) |
181 |
|
|
{ |
182 |
|
|
|
183 |
|
|
/* Convert alpha to upper case. */ |
184 |
|
25 |
alpha = (CHAR)((INT)alpha - 0x20); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
/* Store a byte of the volume name. */ |
188 |
|
145 |
media_ptr -> fx_media_memory_buffer[offset + i] = (UCHAR)alpha; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
/* Now pad with spaces. */ |
192 |
✓✓ |
143 |
for (; i < 11; i++) |
193 |
|
|
{ |
194 |
|
|
|
195 |
|
|
/* Append space character to volume name. */ |
196 |
|
119 |
media_ptr -> fx_media_memory_buffer[offset + i] = 0x20; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
200 |
|
|
|
201 |
|
|
/* Increment the number of driver write boot sector requests. */ |
202 |
|
24 |
media_ptr -> fx_media_driver_boot_write_requests++; |
203 |
|
|
#endif |
204 |
|
|
|
205 |
|
|
/* Write the boot sector with the new volume name. */ |
206 |
|
24 |
media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_WRITE; |
207 |
|
24 |
media_ptr -> fx_media_driver_status = FX_IO_ERROR; |
208 |
|
24 |
media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_memory_buffer; |
209 |
|
24 |
media_ptr -> fx_media_driver_sectors = 1; |
210 |
|
24 |
media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR; |
211 |
|
|
|
212 |
|
|
/* Set the system write flag since we are writing the boot sector. */ |
213 |
|
24 |
media_ptr -> fx_media_driver_system_write = FX_TRUE; |
214 |
|
|
|
215 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
216 |
|
|
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_WRITE, media_ptr, media_ptr -> fx_media_memory_buffer, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) |
217 |
|
|
|
218 |
|
|
/* Invoke the driver to write the boot sector. */ |
219 |
|
24 |
(media_ptr -> fx_media_driver_entry) (media_ptr); |
220 |
|
|
|
221 |
|
|
/* Clear the system write flag. */ |
222 |
|
24 |
media_ptr -> fx_media_driver_system_write = FX_FALSE; |
223 |
|
|
|
224 |
|
|
/* Determine if the request is successful. */ |
225 |
✓✓ |
24 |
if (media_ptr -> fx_media_driver_status) |
226 |
|
|
{ |
227 |
|
|
|
228 |
|
|
/* Release media protection. */ |
229 |
|
1 |
FX_UNPROTECT |
230 |
|
|
|
231 |
|
|
/* An error occurred in the driver. */ |
232 |
|
1 |
return(media_ptr -> fx_media_driver_status); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
/* Setup pointer to media name buffer. */ |
236 |
|
23 |
dir_entry1.fx_dir_entry_name = media_ptr -> fx_media_name_buffer; |
237 |
|
|
|
238 |
|
|
/* Clear the short name string. */ |
239 |
|
23 |
dir_entry1.fx_dir_entry_short_name[0] = 0; |
240 |
|
|
|
241 |
|
|
/* Now we need to find the copy of the volume name in the root directory. */ |
242 |
|
23 |
i = 0; |
243 |
|
23 |
j = media_ptr -> fx_media_root_directory_entries + 1; |
244 |
|
|
do |
245 |
|
|
{ |
246 |
|
|
|
247 |
|
|
/* Read an entry from the root directory. */ |
248 |
|
156 |
status = _fx_directory_entry_read(media_ptr, FX_NULL, &i, &dir_entry1); |
249 |
|
|
|
250 |
|
|
/* Check for error status. */ |
251 |
✓✓ |
156 |
if (status != FX_SUCCESS) |
252 |
|
|
{ |
253 |
|
|
|
254 |
|
|
/* Release media protection. */ |
255 |
|
1 |
FX_UNPROTECT |
256 |
|
|
|
257 |
|
|
/* Return to caller. */ |
258 |
|
1 |
return(status); |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
/* Determine if this is an empty entry. */ |
262 |
✓✓✓✓
|
155 |
if ((dir_entry1.fx_dir_entry_name[0] == (CHAR)FX_DIR_ENTRY_FREE) && (dir_entry1.fx_dir_entry_short_name[0] == 0)) |
263 |
|
|
{ |
264 |
|
|
|
265 |
|
|
/* Yes, this is free entry. Is it the first? */ |
266 |
✓✓ |
6 |
if (i < j) |
267 |
|
|
{ |
268 |
|
|
|
269 |
|
|
/* Yes, first free entry - remember it. */ |
270 |
|
5 |
dir_entry = dir_entry1; |
271 |
|
5 |
j = i; |
272 |
|
|
} |
273 |
|
|
} |
274 |
|
|
/* Determine if the directory entries are exhausted. */ |
275 |
✓✓ |
149 |
else if (dir_entry1.fx_dir_entry_name[0] == FX_DIR_ENTRY_DONE) |
276 |
|
|
{ |
277 |
|
|
|
278 |
|
|
/* Yes, this we are at the end of the directory. Have there |
279 |
|
|
been any other free entries? */ |
280 |
✓✓ |
10 |
if (i < j) |
281 |
|
|
{ |
282 |
|
|
|
283 |
|
|
/* No, we need to remember this as the free entry. */ |
284 |
|
8 |
dir_entry = dir_entry1; |
285 |
|
8 |
j = i; |
286 |
|
|
} |
287 |
|
10 |
break; |
288 |
|
|
} |
289 |
|
|
/* Check for a volume name. */ |
290 |
✓✓ |
139 |
else if (dir_entry1.fx_dir_entry_attributes & FX_VOLUME) |
291 |
|
|
{ |
292 |
|
|
|
293 |
|
|
/* Yes, we have found a previously set volume name - use this entry. */ |
294 |
|
9 |
dir_entry = dir_entry1; |
295 |
|
9 |
j = i; |
296 |
|
9 |
break; |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
/* Move to next directory entry. */ |
300 |
|
136 |
i++; |
301 |
✓✓ |
136 |
} while (i < media_ptr -> fx_media_root_directory_entries); |
302 |
|
|
|
303 |
|
|
/* Determine if a volume entry was not found and there are no more empty slots. */ |
304 |
✓✓ |
22 |
if (i == media_ptr -> fx_media_root_directory_entries) |
305 |
|
|
{ |
306 |
|
|
|
307 |
|
|
/* Determine if there was a free or previous volume name. */ |
308 |
✓✓ |
3 |
if (j == (media_ptr -> fx_media_root_directory_entries + 1)) |
309 |
|
|
{ |
310 |
|
|
|
311 |
|
|
/* No, nothing was available in the root directory. */ |
312 |
|
|
|
313 |
|
|
/* Release media protection. */ |
314 |
|
1 |
FX_UNPROTECT |
315 |
|
|
|
316 |
|
|
/* No, existing volume name or space in the root directly was found, return an error. */ |
317 |
|
1 |
return(FX_MEDIA_INVALID); |
318 |
|
|
} |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
/* Now set the volume name and attribute. */ |
322 |
|
|
|
323 |
|
|
/* Read the logical directory sector. */ |
324 |
|
21 |
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) dir_entry.fx_dir_entry_log_sector, |
325 |
|
21 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_BOOT_SECTOR); |
326 |
|
|
|
327 |
|
|
/* Check the status of reading the directory entry. */ |
328 |
✓✓ |
21 |
if (status != FX_SUCCESS) |
329 |
|
|
{ |
330 |
|
|
|
331 |
|
|
/* Release media protection. */ |
332 |
|
1 |
FX_UNPROTECT |
333 |
|
|
|
334 |
|
|
/* Return the error status. */ |
335 |
|
1 |
return(status); |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
/* Calculate pointer into directory buffer. */ |
339 |
|
20 |
work_ptr = media_ptr -> fx_media_memory_buffer + |
340 |
|
20 |
(UINT)dir_entry.fx_dir_entry_byte_offset; |
341 |
|
|
|
342 |
|
|
/* Copy the volume name into the directory entry. */ |
343 |
✓✓ |
151 |
for (i = 0; volume_name[i]; i++) |
344 |
|
|
{ |
345 |
|
|
|
346 |
|
|
/* Have we reached the end? */ |
347 |
✓✓ |
133 |
if (i == 11) |
348 |
|
|
{ |
349 |
|
|
|
350 |
|
2 |
break; |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
/* Pickup volume name byte. */ |
354 |
|
131 |
alpha = volume_name[i]; |
355 |
|
|
|
356 |
|
|
/* Determine if alpha needs to be converted to upper case. */ |
357 |
✓✓✓✓
|
131 |
if ((alpha >= 'a') && (alpha <= 'z')) |
358 |
|
|
{ |
359 |
|
|
|
360 |
|
|
/* Convert alpha to upper case. */ |
361 |
|
25 |
alpha = (CHAR)((INT)alpha - 0x20); |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
/* Store volume name. */ |
365 |
|
131 |
work_ptr[i] = (UCHAR)alpha; |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
/* Pad with space characters. */ |
369 |
✓✓ |
109 |
for (; i < 11; i++) |
370 |
|
|
{ |
371 |
|
89 |
work_ptr[i] = 0x20; |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
/* Set the appropriate attributes. */ |
375 |
|
20 |
work_ptr[11] = FX_VOLUME | FX_ARCHIVE; |
376 |
|
|
|
377 |
|
|
/* Set the other parts of the volume entry. */ |
378 |
|
|
|
379 |
|
|
/* Clear the hi word of cluster. */ |
380 |
|
20 |
work_ptr[20] = 0; |
381 |
|
20 |
work_ptr[21] = 0; |
382 |
|
|
|
383 |
|
|
/* Clear the low word of cluster. */ |
384 |
|
20 |
work_ptr[26] = 0; |
385 |
|
20 |
work_ptr[27] = 0; |
386 |
|
|
|
387 |
|
|
/* Clear the file size. */ |
388 |
|
20 |
work_ptr[28] = 0; |
389 |
|
20 |
work_ptr[29] = 0; |
390 |
|
20 |
work_ptr[30] = 0; |
391 |
|
20 |
work_ptr[31] = 0; |
392 |
|
|
|
393 |
|
|
/* Write the directory sector to the media. */ |
394 |
|
20 |
status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) dir_entry.fx_dir_entry_log_sector, |
395 |
|
20 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); |
396 |
|
|
|
397 |
|
|
/* Release media protection. */ |
398 |
|
20 |
FX_UNPROTECT |
399 |
|
|
|
400 |
|
|
/* Return the status. */ |
401 |
|
20 |
return(status); |
402 |
|
|
} |
403 |
|
|
|