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