]> git.proxmox.com Git - mirror_edk2.git/blame - FatPkg/EnhancedFatDxe/Init.c
Add some NULL pointer check by using ASSERT()
[mirror_edk2.git] / FatPkg / EnhancedFatDxe / Init.c
CommitLineData
b9ec9330
QH
1/*++\r
2\r
6163cc98
HT
3Copyright (c) 2005 - 2010, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials are licensed and made available\r
b9ec9330
QH
5under the terms and conditions of the BSD License which accompanies this\r
6distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12\r
13Module Name:\r
14\r
15 Init.c\r
16\r
17Abstract:\r
18\r
19 Initialization routines\r
20\r
21--*/\r
22\r
23#include "Fat.h"\r
24\r
25EFI_STATUS\r
26FatAllocateVolume (\r
27 IN EFI_HANDLE Handle,\r
28 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
29 IN EFI_BLOCK_IO_PROTOCOL *BlockIo\r
30 )\r
31/*++\r
32\r
33Routine Description:\r
34\r
35 Allocates volume structure, detects FAT file system, installs protocol,\r
36 and initialize cache.\r
37\r
38Arguments:\r
39\r
40 Handle - The handle of parent device.\r
41 DiskIo - The DiskIo of parent device.\r
42 BlockIo - The BlockIo of parent devicel\r
43\r
44Returns:\r
45\r
46 EFI_SUCCESS - Allocate a new volume successfully.\r
47 EFI_OUT_OF_RESOURCES - Can not allocate the memory.\r
48 Others - Allocating a new volume failed.\r
49\r
50--*/\r
51{\r
52 EFI_STATUS Status;\r
53 FAT_VOLUME *Volume;\r
5251cb9d 54\r
b9ec9330
QH
55 //\r
56 // Allocate a volume structure\r
57 //\r
58 Volume = AllocateZeroPool (sizeof (FAT_VOLUME));\r
59 if (Volume == NULL) {\r
60 return EFI_OUT_OF_RESOURCES;\r
61 }\r
5251cb9d 62\r
b9ec9330
QH
63 //\r
64 // Initialize the structure\r
65 //\r
66 Volume->Signature = FAT_VOLUME_SIGNATURE;\r
67 Volume->Handle = Handle;\r
68 Volume->DiskIo = DiskIo;\r
69 Volume->BlockIo = BlockIo;\r
70 Volume->MediaId = BlockIo->Media->MediaId;\r
71 Volume->ReadOnly = BlockIo->Media->ReadOnly;\r
72 Volume->VolumeInterface.Revision = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION;\r
73 Volume->VolumeInterface.OpenVolume = FatOpenVolume;\r
74 InitializeListHead (&Volume->CheckRef);\r
75 InitializeListHead (&Volume->DirCacheList);\r
76 //\r
77 // Initialize Root Directory entry\r
78 //\r
79 Volume->RootDirEnt.FileString = Volume->RootFileString;\r
80 Volume->RootDirEnt.Entry.Attributes = FAT_ATTRIBUTE_DIRECTORY;\r
81 //\r
82 // Check to see if there's a file system on the volume\r
83 //\r
84 Status = FatOpenDevice (Volume);\r
85 if (EFI_ERROR (Status)) {\r
86 goto Done;\r
87 }\r
88 //\r
89 // Initialize cache\r
90 //\r
91 Status = FatInitializeDiskCache (Volume);\r
92 if (EFI_ERROR (Status)) {\r
93 goto Done;\r
94 }\r
95 //\r
96 // Install our protocol interfaces on the device's handle\r
97 //\r
98 Status = gBS->InstallMultipleProtocolInterfaces (\r
99 &Volume->Handle,\r
100 &gEfiSimpleFileSystemProtocolGuid,\r
101 &Volume->VolumeInterface,\r
102 NULL\r
103 );\r
104 if (EFI_ERROR (Status)) {\r
105 goto Done;\r
106 }\r
107 //\r
108 // Volume installed\r
109 //\r
c22c8322 110 DEBUG ((EFI_D_INIT, "Installed Fat filesystem on %p\n", Handle));\r
b9ec9330
QH
111 Volume->Valid = TRUE;\r
112\r
113Done:\r
b9ec9330
QH
114 if (EFI_ERROR (Status)) {\r
115 FatFreeVolume (Volume);\r
116 }\r
117\r
118 return Status;\r
119}\r
120\r
121EFI_STATUS\r
122FatAbandonVolume (\r
123 IN FAT_VOLUME *Volume\r
124 )\r
125/*++\r
126\r
127Routine Description:\r
128\r
129 Called by FatDriverBindingStop(), Abandon the volume.\r
130\r
131Arguments:\r
132\r
133 Volume - The volume to be abandoned.\r
134\r
135Returns:\r
136\r
137 EFI_SUCCESS - Abandoned the volume successfully.\r
138 Others - Can not uninstall the protocol interfaces.\r
139\r
140--*/\r
141{\r
142 EFI_STATUS Status;\r
143 BOOLEAN LockedByMe;\r
144\r
145 //\r
146 // Uninstall the protocol interface.\r
147 //\r
148 if (Volume->Handle != NULL) {\r
149 Status = gBS->UninstallMultipleProtocolInterfaces (\r
150 Volume->Handle,\r
151 &gEfiSimpleFileSystemProtocolGuid,\r
152 &Volume->VolumeInterface,\r
153 NULL\r
154 );\r
155 if (EFI_ERROR (Status)) {\r
156 return Status;\r
157 }\r
158 }\r
159\r
160 LockedByMe = FALSE;\r
161\r
162 //\r
163 // Acquire the lock.\r
164 // If the caller has already acquired the lock (which\r
165 // means we are in the process of some Fat operation),\r
166 // we can not acquire again.\r
167 //\r
25ce9b1f
QH
168 Status = FatAcquireLockOrFail ();\r
169 if (!EFI_ERROR (Status)) {\r
b9ec9330 170 LockedByMe = TRUE;\r
b9ec9330
QH
171 }\r
172 //\r
173 // The volume is still being used. Hence, set error flag for all OFiles still in\r
174 // use. In two cases, we could get here. One is EFI_MEDIA_CHANGED, the other is\r
175 // EFI_NO_MEDIA.\r
176 //\r
177 if (Volume->Root != NULL) {\r
178 FatSetVolumeError (\r
179 Volume->Root,\r
180 Volume->BlockIo->Media->MediaPresent ? EFI_MEDIA_CHANGED : EFI_NO_MEDIA\r
181 );\r
182 }\r
183\r
184 Volume->Valid = FALSE;\r
185\r
186 //\r
187 // Release the lock.\r
188 // If locked by me, this means DriverBindingStop is NOT\r
189 // called within an on-going Fat operation, so we should\r
190 // take responsibility to cleanup and free the volume.\r
191 // Otherwise, the DriverBindingStop is called within an on-going\r
192 // Fat operation, we shouldn't check reference, so just let outer\r
193 // FatCleanupVolume do the task.\r
194 //\r
195 if (LockedByMe) {\r
196 FatCleanupVolume (Volume, NULL, EFI_SUCCESS);\r
197 FatReleaseLock ();\r
198 }\r
199\r
200 return EFI_SUCCESS;\r
201}\r
202\r
203EFI_STATUS\r
204FatOpenDevice (\r
205 IN OUT FAT_VOLUME *Volume\r
206 )\r
207/*++\r
208\r
209Routine Description:\r
210\r
211 Detects FAT file system on Disk and set relevant fields of Volume\r
212\r
213Arguments:\r
214\r
215 Volume - The volume structure.\r
216\r
217Returns:\r
218\r
219 EFI_SUCCESS - The Fat File System is detected successfully\r
220 EFI_UNSUPPORTED - The volume is not FAT file system.\r
221 EFI_VOLUME_CORRUPTED - The volume is corrupted.\r
222\r
223--*/\r
224{\r
225 EFI_STATUS Status;\r
226 UINT32 BlockSize;\r
227 UINT32 DirtyMask;\r
228 EFI_DISK_IO_PROTOCOL *DiskIo;\r
229 FAT_BOOT_SECTOR FatBs;\r
230 FAT_VOLUME_TYPE FatType;\r
231 UINTN RootDirSectors;\r
232 UINTN FatLba;\r
233 UINTN RootLba;\r
234 UINTN FirstClusterLba;\r
235 UINTN Sectors;\r
236 UINTN SectorsPerFat;\r
237 UINT8 SectorsPerClusterAlignment;\r
238 UINT8 BlockAlignment;\r
239\r
240 //\r
241 // Read the FAT_BOOT_SECTOR BPB info\r
242 // This is the only part of FAT code that uses parent DiskIo,\r
243 // Others use FatDiskIo which utilizes a Cache.\r
244 //\r
245 DiskIo = Volume->DiskIo;\r
246 Status = DiskIo->ReadDisk (DiskIo, Volume->MediaId, 0, sizeof (FatBs), &FatBs);\r
247\r
248 if (EFI_ERROR (Status)) {\r
249 DEBUG ((EFI_D_INIT, "FatOpenDevice: read of part_lba failed %r\n", Status));\r
250 return Status;\r
251 }\r
252\r
253 FatType = FatUndefined;\r
254\r
255 //\r
256 // Use LargeSectors if Sectors is 0\r
257 //\r
258 Sectors = FatBs.FatBsb.Sectors;\r
259 if (Sectors == 0) {\r
260 Sectors = FatBs.FatBsb.LargeSectors;\r
261 }\r
262\r
263 SectorsPerFat = FatBs.FatBsb.SectorsPerFat;\r
264 if (SectorsPerFat == 0) {\r
265 SectorsPerFat = FatBs.FatBse.Fat32Bse.LargeSectorsPerFat;\r
266 FatType = FAT32;\r
267 }\r
268 //\r
269 // Is boot sector a fat sector?\r
270 // (Note that so far we only know if the sector is FAT32 or not, we don't\r
271 // know if the sector is Fat16 or Fat12 until later when we can compute\r
272 // the volume size)\r
273 //\r
274 if (FatBs.FatBsb.ReservedSectors == 0 || FatBs.FatBsb.NumFats == 0 || Sectors == 0) {\r
275 return EFI_UNSUPPORTED;\r
276 }\r
277\r
278 if ((FatBs.FatBsb.SectorSize & (FatBs.FatBsb.SectorSize - 1)) != 0) {\r
279 return EFI_UNSUPPORTED;\r
280 }\r
281\r
282 BlockAlignment = (UINT8) HighBitSet32 (FatBs.FatBsb.SectorSize);\r
283 if (BlockAlignment > MAX_BLOCK_ALIGNMENT || BlockAlignment < MIN_BLOCK_ALIGNMENT) {\r
284 return EFI_UNSUPPORTED;\r
285 }\r
286\r
287 if ((FatBs.FatBsb.SectorsPerCluster & (FatBs.FatBsb.SectorsPerCluster - 1)) != 0) {\r
288 return EFI_UNSUPPORTED;\r
289 }\r
290\r
291 SectorsPerClusterAlignment = (UINT8) HighBitSet32 (FatBs.FatBsb.SectorsPerCluster);\r
292 if (SectorsPerClusterAlignment > MAX_SECTORS_PER_CLUSTER_ALIGNMENT) {\r
293 return EFI_UNSUPPORTED;\r
294 }\r
295\r
296 if (FatBs.FatBsb.Media <= 0xf7 &&\r
297 FatBs.FatBsb.Media != 0xf0 &&\r
298 FatBs.FatBsb.Media != 0x00 &&\r
299 FatBs.FatBsb.Media != 0x01\r
300 ) {\r
301 return EFI_UNSUPPORTED;\r
302 }\r
303 //\r
304 // Initialize fields the volume information for this FatType\r
305 //\r
306 if (FatType != FAT32) {\r
307 if (FatBs.FatBsb.RootEntries == 0) {\r
308 return EFI_UNSUPPORTED;\r
309 }\r
310 //\r
311 // Unpack fat12, fat16 info\r
312 //\r
313 Volume->RootEntries = FatBs.FatBsb.RootEntries;\r
314 } else {\r
315 //\r
316 // If this is fat32, refuse to mount mirror-disabled volumes\r
317 //\r
318 if ((SectorsPerFat == 0 || FatBs.FatBse.Fat32Bse.FsVersion != 0) || (FatBs.FatBse.Fat32Bse.ExtendedFlags & 0x80)) {\r
319 return EFI_UNSUPPORTED;\r
320 }\r
321 //\r
322 // Unpack fat32 info\r
323 //\r
324 Volume->RootCluster = FatBs.FatBse.Fat32Bse.RootDirFirstCluster;\r
325 }\r
326\r
327 Volume->NumFats = FatBs.FatBsb.NumFats;\r
328 //\r
329 // Compute some fat locations\r
330 //\r
331 BlockSize = FatBs.FatBsb.SectorSize;\r
332 RootDirSectors = ((Volume->RootEntries * sizeof (FAT_DIRECTORY_ENTRY)) + (BlockSize - 1)) / BlockSize;\r
333\r
334 FatLba = FatBs.FatBsb.ReservedSectors;\r
335 RootLba = FatBs.FatBsb.NumFats * SectorsPerFat + FatLba;\r
336 FirstClusterLba = RootLba + RootDirSectors;\r
337\r
338 Volume->FatPos = FatLba * BlockSize;\r
339 Volume->FatSize = SectorsPerFat * BlockSize;\r
340\r
341 Volume->VolumeSize = LShiftU64 (Sectors, BlockAlignment);\r
342 Volume->RootPos = LShiftU64 (RootLba, BlockAlignment);\r
343 Volume->FirstClusterPos = LShiftU64 (FirstClusterLba, BlockAlignment);\r
344 Volume->MaxCluster = (Sectors - FirstClusterLba) >> SectorsPerClusterAlignment;\r
345 Volume->ClusterAlignment = (UINT8)(BlockAlignment + SectorsPerClusterAlignment);\r
346 Volume->ClusterSize = (UINTN)1 << (Volume->ClusterAlignment);\r
347\r
348 //\r
349 // If this is not a fat32, determine if it's a fat16 or fat12\r
350 //\r
351 if (FatType != FAT32) {\r
352 if (Volume->MaxCluster >= FAT_MAX_FAT16_CLUSTER) {\r
353 return EFI_VOLUME_CORRUPTED;\r
354 }\r
355\r
356 FatType = Volume->MaxCluster < FAT_MAX_FAT12_CLUSTER ? FAT12 : FAT16;\r
357 //\r
358 // fat12 & fat16 fat-entries are 2 bytes\r
359 //\r
360 Volume->FatEntrySize = sizeof (UINT16);\r
361 DirtyMask = FAT16_DIRTY_MASK;\r
362 } else {\r
363 if (Volume->MaxCluster < FAT_MAX_FAT16_CLUSTER) {\r
364 return EFI_VOLUME_CORRUPTED;\r
365 }\r
366 //\r
367 // fat32 fat-entries are 4 bytes\r
368 //\r
369 Volume->FatEntrySize = sizeof (UINT32);\r
370 DirtyMask = FAT32_DIRTY_MASK;\r
371 }\r
372 //\r
373 // Get the DirtyValue and NotDirtyValue\r
374 // We should keep the initial value as the NotDirtyValue\r
375 // in case the volume is dirty already\r
376 //\r
377 if (FatType != FAT12) {\r
378 Status = FatAccessVolumeDirty (Volume, READ_DISK, &Volume->NotDirtyValue);\r
379 if (EFI_ERROR (Status)) {\r
380 return Status;\r
381 }\r
382\r
383 Volume->DirtyValue = Volume->NotDirtyValue & DirtyMask;\r
384 }\r
385 //\r
386 // If present, read the fat hint info\r
387 //\r
388 if (FatType == FAT32) {\r
389 Volume->FreeInfoPos = FatBs.FatBse.Fat32Bse.FsInfoSector * BlockSize;\r
390 if (FatBs.FatBse.Fat32Bse.FsInfoSector != 0) {\r
391 FatDiskIo (Volume, READ_DISK, Volume->FreeInfoPos, sizeof (FAT_INFO_SECTOR), &Volume->FatInfoSector);\r
392 if (Volume->FatInfoSector.Signature == FAT_INFO_SIGNATURE &&\r
393 Volume->FatInfoSector.InfoBeginSignature == FAT_INFO_BEGIN_SIGNATURE &&\r
394 Volume->FatInfoSector.InfoEndSignature == FAT_INFO_END_SIGNATURE &&\r
395 Volume->FatInfoSector.FreeInfo.ClusterCount <= Volume->MaxCluster\r
396 ) {\r
397 Volume->FreeInfoValid = TRUE;\r
398 }\r
399 }\r
400 }\r
401 //\r
402 // Just make up a FreeInfo.NextCluster for use by allocate cluster\r
403 //\r
404 if (FAT_MIN_CLUSTER > Volume->FatInfoSector.FreeInfo.NextCluster ||\r
405 Volume->FatInfoSector.FreeInfo.NextCluster > Volume->MaxCluster + 1\r
406 ) {\r
407 Volume->FatInfoSector.FreeInfo.NextCluster = FAT_MIN_CLUSTER;\r
408 }\r
409 //\r
410 // We are now defining FAT Type\r
411 //\r
412 Volume->FatType = FatType;\r
413 ASSERT (FatType != FatUndefined);\r
414\r
415 return EFI_SUCCESS;\r
416}\r