Fix warnings generated by GCC.
[edk2.git] / DuetPkg / FSVariable / FileStorage.c
blob1792767b076e536d04ff99ac74412b2db87d1fb0
1 /*++
3 Copyright (c) 2006 - 2009, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
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.
12 Module Name:
14 FileStorage.c
16 Abstract:
18 handles variable store/reads on file
20 Revision History
22 --*/
23 #include "FSVariable.h"
25 VOID *mSFSRegistration;
28 // Prototypes
31 VOID
32 EFIAPI
33 OnVirtualAddressChangeFs (
34 IN EFI_EVENT Event,
35 IN VOID *Context
38 EFI_STATUS
39 EFIAPI
40 FileEraseStore(
41 IN VARIABLE_STORAGE *This
44 EFI_STATUS
45 EFIAPI
46 FileWriteStore (
47 IN VARIABLE_STORAGE *This,
48 IN UINTN Offset,
49 IN UINTN BufferSize,
50 IN VOID *Buffer
53 EFI_STATUS
54 OpenStore (
55 IN EFI_DEVICE_PATH_PROTOCOL *Device,
56 IN CHAR16 *FilePathName,
57 IN UINT64 OpenMode,
58 OUT EFI_FILE_PROTOCOL **File
62 // Implementation below:
64 VOID
65 FileClose (
66 IN EFI_FILE_PROTOCOL *File
69 EFI_STATUS Status;
71 Status = File->Flush (File);
72 ASSERT_EFI_ERROR (Status);
74 Status = File->Close (File);
75 ASSERT_EFI_ERROR (Status);
78 EFI_STATUS
79 CheckStore (
80 IN EFI_HANDLE SimpleFileSystemHandle,
81 IN UINT32 VolumeId,
82 OUT EFI_DEVICE_PATH_PROTOCOL **Device
85 #define BLOCK_SIZE 0x200
86 #define FAT16_VOLUME_ID_OFFSET 39
87 #define FAT32_VOLUME_ID_OFFSET 67
88 EFI_STATUS Status;
89 EFI_BLOCK_IO_PROTOCOL *BlkIo;
90 UINT8 BootSector[BLOCK_SIZE];
92 *Device = NULL;
93 Status = gBS->HandleProtocol (
94 SimpleFileSystemHandle,
95 &gEfiBlockIoProtocolGuid, // BlockIo should be supported if it supports SimpleFileSystem
96 (VOID*)&BlkIo
99 if (EFI_ERROR (Status)) {
100 goto ErrHandle;
102 if (!BlkIo->Media->MediaPresent) {
103 DEBUG ((EFI_D_ERROR, "FileStorage: Media not present!\n"));
104 Status = EFI_NO_MEDIA;
105 goto ErrHandle;
107 if (BlkIo->Media->ReadOnly) {
108 DEBUG ((EFI_D_ERROR, "FileStorage: Media is read-only!\n"));
109 Status = EFI_ACCESS_DENIED;
110 goto ErrHandle;
113 Status = BlkIo->ReadBlocks(
114 BlkIo,
115 BlkIo->Media->MediaId,
117 BLOCK_SIZE,
118 BootSector
120 ASSERT_EFI_ERROR (Status);
121 if ((*(UINT32 *) &BootSector[FAT16_VOLUME_ID_OFFSET] != VolumeId) &&
122 (*(UINT32 *) &BootSector[FAT32_VOLUME_ID_OFFSET] != VolumeId)
124 Status = EFI_NOT_FOUND;
125 goto ErrHandle;
128 *Device = DuplicateDevicePath (DevicePathFromHandle (SimpleFileSystemHandle));
129 ASSERT (*Device != NULL);
131 ErrHandle:
132 return Status;
135 EFI_STATUS
136 CheckStoreExists (
137 IN EFI_DEVICE_PATH_PROTOCOL *Device
140 EFI_HANDLE Handle;
141 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
142 EFI_STATUS Status;
144 Status = gBS->LocateDevicePath (
145 &gEfiSimpleFileSystemProtocolGuid,
146 &Device,
147 &Handle
150 if (EFI_ERROR (Status)) {
151 return Status;
154 Status = gBS->HandleProtocol (
155 Handle,
156 &gEfiSimpleFileSystemProtocolGuid,
157 (VOID **) &Volume
159 if (EFI_ERROR (Status)) {
160 return Status;
163 return EFI_SUCCESS;
166 // this routine is still running in BS period, no limitation
167 // call FileInitStorage(), which load variable content file to memory
168 // read the store_header, init store_header if it has not been inited (read sth. about format/heathy)
169 // reclaim space using scratch memory
171 VOID
172 EFIAPI
173 OnSimpleFileSystemInstall (
174 IN EFI_EVENT Event,
175 IN VOID *Context
178 EFI_STATUS Status;
179 UINTN HandleSize;
180 EFI_HANDLE Handle;
181 EFI_DEVICE_PATH_PROTOCOL *Device;
182 VS_DEV *Dev;
183 EFI_FILE_PROTOCOL *File;
184 UINTN NumBytes;
186 Dev = (VS_DEV *) Context;
188 if (VAR_FILE_DEVICEPATH (Dev) != NULL &&
189 !EFI_ERROR (CheckStoreExists (VAR_FILE_DEVICEPATH (Dev)))
191 DEBUG ((EFI_D_ERROR, "FileStorage: Already mapped!\n"));
192 return ;
195 while (TRUE) {
196 HandleSize = sizeof (EFI_HANDLE);
197 Status = gBS->LocateHandle (
198 ByRegisterNotify,
199 NULL,
200 mSFSRegistration,
201 &HandleSize,
202 &Handle
204 if (EFI_ERROR (Status)) {
205 return ;
208 Status = CheckStore (Handle, VAR_FILE_VOLUMEID (Dev), &Device);
209 if (!EFI_ERROR (Status)) {
210 break;
214 VAR_FILE_DEVICEPATH (Dev) = Device;
215 Status = OpenStore (
216 VAR_FILE_DEVICEPATH (Dev),
217 VAR_FILE_FILEPATH (Dev),
218 EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ | EFI_FILE_MODE_CREATE,
219 &File
221 ASSERT_EFI_ERROR (Status);
223 NumBytes = Dev->Size;
224 Status = File->Write (File, &NumBytes, VAR_DATA_PTR (Dev));
225 ASSERT_EFI_ERROR (Status);
226 // KEN: bugbug here if closing file, volume handle will be free,
227 // and system will be hang when accessing volume handle in future.
228 //FileClose (File);
229 DEBUG ((EFI_D_ERROR, "FileStorage: Mapped to file!\n"));
232 EFI_STATUS
233 FileStorageConstructor (
234 OUT VARIABLE_STORAGE **VarStore,
235 OUT EFI_EVENT_NOTIFY *GoVirtualEvent,
236 IN EFI_PHYSICAL_ADDRESS NvStorageBase,
237 IN UINTN Size,
238 IN UINT32 VolumeId,
239 IN CHAR16 *FilePath
242 VS_DEV *Dev;
243 EFI_STATUS Status;
244 EFI_EVENT Event;
246 Status = gBS->AllocatePool (EfiRuntimeServicesData, sizeof(VS_DEV), (VOID **) &Dev);
247 ASSERT_EFI_ERROR (Status);
248 ZeroMem (Dev, sizeof(VS_DEV));
250 Dev->Signature = VS_DEV_SIGNATURE;
251 Dev->Size = Size;
252 VAR_DATA_PTR (Dev) = (UINT8 *) (UINTN) NvStorageBase;
253 VAR_FILE_VOLUMEID (Dev) = VolumeId;
254 StrCpy (VAR_FILE_FILEPATH (Dev), FilePath);
255 Dev->VarStore.Erase = FileEraseStore;
256 Dev->VarStore.Write = FileWriteStore;
258 DEBUG ((EFI_D_ERROR, "FileStorageConstructor(0x%0x:0x%0x): added!\n", NvStorageBase, Size));
260 // add notify on SFS's installation.
262 Status = gBS->CreateEvent (
263 EVT_NOTIFY_SIGNAL,
264 TPL_CALLBACK,
265 OnSimpleFileSystemInstall,
266 Dev,
267 &Event
269 ASSERT_EFI_ERROR (Status);
271 Status = gBS->RegisterProtocolNotify (
272 &gEfiSimpleFileSystemProtocolGuid,
273 Event,
274 &mSFSRegistration
276 ASSERT_EFI_ERROR (Status);
278 *VarStore = &Dev->VarStore;
279 *GoVirtualEvent = OnVirtualAddressChangeFs;
280 return EFI_SUCCESS;
283 EFI_STATUS
284 EFIAPI
285 FileEraseStore(
286 IN VARIABLE_STORAGE *This
289 EFI_STATUS Status;
290 VS_DEV *Dev;
291 EFI_FILE_PROTOCOL *File;
292 UINTN NumBytes;
294 Status = EFI_SUCCESS;
295 Dev = DEV_FROM_THIS(This);
297 SetMem (VAR_DATA_PTR (Dev), Dev->Size, VAR_DEFAULT_VALUE);
299 if (!EfiAtRuntime () && VAR_FILE_DEVICEPATH (Dev) != NULL) {
300 Status = OpenStore (
301 VAR_FILE_DEVICEPATH (Dev),
302 VAR_FILE_FILEPATH (Dev),
303 EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ,
304 &File
306 ASSERT_EFI_ERROR (Status);
307 NumBytes = Dev->Size;
308 Status = File->Write (File, &NumBytes, VAR_DATA_PTR (Dev));
309 ASSERT_EFI_ERROR (Status);
310 FileClose (File);
313 return Status;
316 EFI_STATUS
317 EFIAPI
318 FileWriteStore (
319 IN VARIABLE_STORAGE *This,
320 IN UINTN Offset,
321 IN UINTN BufferSize,
322 IN VOID *Buffer
325 EFI_STATUS Status;
326 VS_DEV *Dev;
327 EFI_FILE_PROTOCOL *File;
329 Status = EFI_SUCCESS;
330 Dev = DEV_FROM_THIS(This);
332 ASSERT (Buffer != NULL);
333 ASSERT (Offset + BufferSize <= Dev->Size);
335 CopyMem (VAR_DATA_PTR (Dev) + Offset, Buffer, BufferSize);
337 if (!EfiAtRuntime () && VAR_FILE_DEVICEPATH (Dev) != NULL) {
338 Status = OpenStore (
339 VAR_FILE_DEVICEPATH (Dev),
340 VAR_FILE_FILEPATH (Dev),
341 EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ,
342 &File
344 Status = File->SetPosition (File, Offset);
345 ASSERT_EFI_ERROR (Status);
346 Status = File->Write (File, &BufferSize, Buffer);
347 ASSERT_EFI_ERROR (Status);
348 FileClose (File);
350 return Status;
353 VOID
354 EFIAPI
355 OnVirtualAddressChangeFs (
356 IN EFI_EVENT Event,
357 IN VOID *Context
360 VS_DEV *Dev;
362 Dev = DEV_FROM_THIS (Context);
364 EfiConvertPointer (0, (VOID **) &VAR_DATA_PTR (Dev));
365 EfiConvertPointer (0, (VOID **) &Dev->VarStore.Erase);
366 EfiConvertPointer (0, (VOID **) &Dev->VarStore.Write);
369 EFI_STATUS
370 OpenStore (
371 IN EFI_DEVICE_PATH_PROTOCOL *Device,
372 IN CHAR16 *FilePathName,
373 IN UINT64 OpenMode,
374 OUT EFI_FILE_PROTOCOL **File
377 EFI_HANDLE Handle;
378 EFI_FILE_HANDLE Root;
379 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;
380 EFI_STATUS Status;
382 *File = NULL;
384 Status = gBS->LocateDevicePath (
385 &gEfiSimpleFileSystemProtocolGuid,
386 &Device,
387 &Handle
390 if (EFI_ERROR (Status)) {
391 return Status;
394 Status = gBS->HandleProtocol (
395 Handle,
396 &gEfiSimpleFileSystemProtocolGuid,
397 (VOID **) &Volume
399 if (EFI_ERROR (Status)) {
400 return Status;
404 // Open the root directory of the volume
406 Root = NULL;
407 Status = Volume->OpenVolume (
408 Volume,
409 &Root
411 ASSERT_EFI_ERROR (Status);
412 ASSERT (Root != NULL);
415 // Open file
417 Status = Root->Open (
418 Root,
419 File,
420 FilePathName,
421 OpenMode,
424 if (EFI_ERROR (Status)) {
425 *File = NULL;
429 // Close the Root directory
431 Root->Close (Root);
432 return Status;