]> git.proxmox.com Git - mirror_edk2.git/blob - FatPkg/EnhancedFatDxe/Misc.c
1. Correct File header to ## @file 2. Remove unnecessary .common] postfix on section.
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Misc.c
1 /*++
2
3 Copyright (c) 2005, Intel Corporation
4 All rights reserved. This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 Module Name:
14
15 Misc.c
16
17 Abstract:
18
19 Miscellaneous functions
20
21 Revision History
22
23 --*/
24
25 #include "Fat.h"
26
27 EFI_STATUS
28 FatAccessVolumeDirty (
29 IN FAT_VOLUME *Volume,
30 IN IO_MODE IoMode,
31 IN VOID *DirtyValue
32 )
33 /*++
34
35 Routine Description:
36
37 Set the volume as dirty or not
38
39 Arguments:
40
41 Volume - FAT file system volume.
42 IoMode - The access mode.
43 DirtyValue - Set the volume as dirty or not.
44
45 Returns:
46
47 EFI_SUCCESS - Set the new FAT entry value sucessfully.
48 other - An error occurred when operation the FAT entries.
49
50 --*/
51 {
52 UINTN WriteCount;
53
54 WriteCount = Volume->FatEntrySize;
55 return FatDiskIo (Volume, IoMode, Volume->FatPos + WriteCount, WriteCount, DirtyValue);
56 }
57
58 EFI_STATUS
59 FatDiskIo (
60 IN FAT_VOLUME *Volume,
61 IN IO_MODE IoMode,
62 IN UINT64 Offset,
63 IN UINTN BufferSize,
64 IN OUT VOID *Buffer
65 )
66 /*++
67
68 Routine Description:
69
70 General disk access function
71
72 Arguments:
73
74 Volume - FAT file system volume.
75 IoMode - The access mode (disk read/write or cache access).
76 Offset - The starting byte offset to read from.
77 BufferSize - Size of Buffer.
78 Buffer - Buffer containing read data.
79
80 Returns:
81
82 EFI_SUCCESS - The operation is performed successfully.
83 EFI_VOLUME_CORRUPTED - The accesss is
84 Others - The status of read/write the disk
85
86 --*/
87 {
88 EFI_STATUS Status;
89 EFI_DISK_IO_PROTOCOL *DiskIo;
90 EFI_DISK_READ IoFunction;
91
92 //
93 // Verify the IO is in devices range
94 //
95 Status = EFI_VOLUME_CORRUPTED;
96 if (Offset + BufferSize <= Volume->VolumeSize) {
97 if (CACHE_ENABLED (IoMode)) {
98 //
99 // Access cache
100 //
101 Status = FatAccessCache (Volume, CACHE_TYPE (IoMode), RAW_ACCESS (IoMode), Offset, BufferSize, Buffer);
102 } else {
103 //
104 // Access disk directly
105 //
106 DiskIo = Volume->DiskIo;
107 IoFunction = (IoMode == READ_DISK) ? DiskIo->ReadDisk : DiskIo->WriteDisk;
108 Status = IoFunction (DiskIo, Volume->MediaId, Offset, BufferSize, Buffer);
109 }
110 }
111
112 if (EFI_ERROR (Status)) {
113 Volume->DiskError = TRUE;
114 DEBUG ((EFI_D_INFO, "FatDiskIo: error %r\n", Status));
115 }
116
117 return Status;
118 }
119
120 VOID
121 FatAcquireLock (
122 VOID
123 )
124 /*++
125
126 Routine Description:
127
128 Lock the volume.
129
130 Arguments:
131
132 None.
133
134 Returns:
135
136 None.
137
138 --*/
139 {
140 EfiAcquireLock (&FatFsLock);
141 }
142
143 BOOLEAN
144 FatIsLocked (
145 VOID
146 )
147 /*++
148
149 Routine Description:
150
151 Get the locking status of the volume.
152
153 Arguments:
154
155 None.
156
157 Returns:
158
159 TRUE - The volume is locked.
160 FALSE - The volume is not locked.
161
162 --*/
163 {
164 return (BOOLEAN) (FatFsLock.Lock);
165 }
166
167 VOID
168 FatReleaseLock (
169 VOID
170 )
171 /*++
172
173 Routine Description:
174
175 Unlock the volume.
176
177 Arguments:
178
179 Null.
180
181 Returns:
182
183 None.
184
185 --*/
186 {
187 EfiReleaseLock (&FatFsLock);
188 }
189
190 VOID
191 FatFreeDirEnt (
192 IN FAT_DIRENT *DirEnt
193 )
194 /*++
195
196 Routine Description:
197
198 Free directory entry.
199
200 Arguments:
201
202 DirEnt - The directory entry to be freed.
203
204 Returns:
205
206 None.
207
208 --*/
209 {
210 if (DirEnt->FileString != NULL) {
211 FreePool (DirEnt->FileString);
212 }
213
214 FreePool (DirEnt);
215 }
216
217 VOID
218 FatFreeVolume (
219 IN FAT_VOLUME *Volume
220 )
221 /*++
222
223 Routine Description:
224
225 Free volume structure (including the contents of directory cache and disk cache).
226
227 Arguments:
228
229 Volume - The volume structure to be freed.
230
231 Returns:
232
233 None.
234
235 --*/
236 {
237 //
238 // Free disk cache
239 //
240 if (Volume->CacheBuffer != NULL) {
241 FreePool (Volume->CacheBuffer);
242 }
243 //
244 // Free directory cache
245 //
246 FatCleanupODirCache (Volume);
247 FreePool (Volume);
248 }
249
250 VOID
251 FatEfiTimeToFatTime (
252 IN EFI_TIME *ETime,
253 OUT FAT_DATE_TIME *FTime
254 )
255 /*++
256
257 Routine Description:
258
259 Translate EFI time to FAT time.
260
261 Arguments:
262
263 ETime - The time of EFI_TIME.
264 FTime - The time of FAT_DATE_TIME.
265
266 Returns:
267
268 None.
269
270 --*/
271 {
272 //
273 // ignores timezone info in source ETime
274 //
275 if (ETime->Year > 1980) {
276 FTime->Date.Year = (UINT16) (ETime->Year - 1980);
277 }
278
279 if (ETime->Year >= 1980 + FAT_MAX_YEAR_FROM_1980) {
280 FTime->Date.Year = FAT_MAX_YEAR_FROM_1980;
281 }
282
283 FTime->Date.Month = ETime->Month;
284 FTime->Date.Day = ETime->Day;
285 FTime->Time.Hour = ETime->Hour;
286 FTime->Time.Minute = ETime->Minute;
287 FTime->Time.DoubleSecond = (UINT16) (ETime->Second / 2);
288 }
289
290 VOID
291 FatFatTimeToEfiTime (
292 IN FAT_DATE_TIME *FTime,
293 OUT EFI_TIME *ETime
294 )
295 /*++
296
297 Routine Description:
298
299 Translate Fat time to EFI time.
300
301 Arguments:
302
303 FTime - The time of FAT_DATE_TIME.
304 ETime - The time of EFI_TIME.
305
306 Returns:
307
308 None.
309
310 --*/
311 {
312 ETime->Year = (UINT16) (FTime->Date.Year + 1980);
313 ETime->Month = (UINT8) FTime->Date.Month;
314 ETime->Day = (UINT8) FTime->Date.Day;
315 ETime->Hour = (UINT8) FTime->Time.Hour;
316 ETime->Minute = (UINT8) FTime->Time.Minute;
317 ETime->Second = (UINT8) (FTime->Time.DoubleSecond * 2);
318 ETime->Nanosecond = 0;
319 ETime->TimeZone = EFI_UNSPECIFIED_TIMEZONE;
320 ETime->Daylight = 0;
321 }
322
323 VOID
324 FatGetCurrentFatTime (
325 OUT FAT_DATE_TIME *FatNow
326 )
327 /*++
328
329 Routine Description:
330
331 Get Current FAT time.
332
333 Arguments:
334
335 FatNow - Current FAT time.
336
337 Returns:
338
339 None.
340
341 --*/
342 {
343 EFI_TIME Now;
344 gRT->GetTime (&Now, NULL);
345 FatEfiTimeToFatTime (&Now, FatNow);
346 }
347
348 BOOLEAN
349 FatIsValidTime (
350 IN EFI_TIME *Time
351 )
352 /*++
353
354 Routine Description:
355
356 Check whether a time is valid.
357
358 Arguments:
359
360 Time - The time of EFI_TIME.
361
362 Returns:
363
364 TRUE - The time is valid.
365 FALSE - The time is not valid.
366
367 --*/
368 {
369 static UINT8 MonthDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
370 UINTN Day;
371 BOOLEAN ValidTime;
372
373 ValidTime = TRUE;
374
375 //
376 // Check the fields for range problems
377 // Fat can only support from 1980
378 //
379 if (Time->Year < 1980 ||
380 Time->Month < 1 ||
381 Time->Month > 12 ||
382 Time->Day < 1 ||
383 Time->Day > 31 ||
384 Time->Hour > 23 ||
385 Time->Minute > 59 ||
386 Time->Second > 59 ||
387 Time->Nanosecond > 999999999
388 ) {
389
390 ValidTime = FALSE;
391
392 } else {
393 //
394 // Perform a more specific check of the day of the month
395 //
396 Day = MonthDays[Time->Month - 1];
397 if (Time->Month == 2 && IS_LEAP_YEAR (Time->Year)) {
398 Day += 1;
399 //
400 // 1 extra day this month
401 //
402 }
403 if (Time->Day > Day) {
404 ValidTime = FALSE;
405 }
406 }
407
408 return ValidTime;
409 }