]>
Commit | Line | Data |
---|---|---|
9071550e | 1 | /**@file\r |
b1f700a8 HT |
2 | Copyright (c) 2007 - 2009, Intel Corporation. All rights reserved.<BR>\r |
3 | This program and the accompanying materials\r | |
9071550e | 4 | are licensed and made available under the terms and conditions of the BSD License\r |
5 | which accompanies this distribution. The full text of the license may be found at\r | |
6 | http://opensource.org/licenses/bsd-license.php\r | |
7 | \r | |
8 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
9 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
10 | \r | |
11 | Module Name:\r | |
12 | \r | |
13 | FileIo.c\r | |
14 | \r | |
15 | Abstract:\r | |
16 | \r | |
17 | File operation for Firmware volume block driver\r | |
18 | \r | |
19 | **/\r | |
20 | #include "FileIo.h"\r | |
21 | \r | |
22 | //\r | |
23 | // Variable storage hot plug is supported but there are still some restrictions:\r | |
24 | // After plugging the storage back,\r | |
25 | // 1. Still use memory as NV if newly plugged storage is not same as the original one\r | |
26 | // 2. Still use memory as NV if there are some update operation during storage is unplugged.\r | |
27 | //\r | |
28 | \r | |
29 | \r | |
30 | EFI_STATUS\r | |
31 | FileWrite (\r | |
b0b961c8 | 32 | IN EFI_FILE_PROTOCOL *File,\r |
33 | IN UINTN Offset,\r | |
34 | IN UINTN Buffer,\r | |
35 | IN UINTN Size\r | |
9071550e | 36 | )\r |
37 | {\r | |
38 | EFI_STATUS Status;\r | |
39 | \r | |
40 | Status = File->SetPosition (File, Offset);\r | |
41 | ASSERT_EFI_ERROR (Status);\r | |
42 | if (!EFI_ERROR (Status)) {\r | |
43 | Status = File->Write (File, &Size, (VOID *) Buffer);\r | |
44 | ASSERT_EFI_ERROR (Status);\r | |
45 | }\r | |
46 | return Status;\r | |
47 | }\r | |
48 | \r | |
49 | EFI_STATUS\r | |
50 | CheckStore (\r | |
51 | IN EFI_HANDLE SimpleFileSystemHandle,\r | |
52 | IN UINT32 VolumeId,\r | |
53 | OUT EFI_DEVICE_PATH_PROTOCOL **Device\r | |
54 | )\r | |
55 | {\r | |
56 | #define BLOCK_SIZE 0x200\r | |
57 | #define FAT16_VOLUME_ID_OFFSET 39\r | |
58 | #define FAT32_VOLUME_ID_OFFSET 67\r | |
59 | EFI_STATUS Status;\r | |
60 | EFI_BLOCK_IO_PROTOCOL *BlkIo;\r | |
61 | UINT8 BootSector[BLOCK_SIZE];\r | |
62 | \r | |
63 | *Device = NULL;\r | |
64 | Status = gBS->HandleProtocol (\r | |
65 | SimpleFileSystemHandle,\r | |
66 | &gEfiBlockIoProtocolGuid, // BlockIo should be supported if it supports SimpleFileSystem\r | |
67 | (VOID*)&BlkIo\r | |
68 | );\r | |
69 | \r | |
70 | if (EFI_ERROR (Status)) {\r | |
71 | goto ErrHandle;\r | |
72 | }\r | |
73 | if (!BlkIo->Media->MediaPresent) {\r | |
74 | DEBUG ((EFI_D_ERROR, "FwhMappedFile: Media not present!\n"));\r | |
75 | Status = EFI_NO_MEDIA;\r | |
76 | goto ErrHandle;\r | |
77 | }\r | |
78 | if (BlkIo->Media->ReadOnly) {\r | |
79 | DEBUG ((EFI_D_ERROR, "FwhMappedFile: Media is read-only!\n"));\r | |
80 | Status = EFI_ACCESS_DENIED;\r | |
81 | goto ErrHandle;\r | |
82 | }\r | |
83 | \r | |
84 | Status = BlkIo->ReadBlocks(\r | |
85 | BlkIo,\r | |
86 | BlkIo->Media->MediaId,\r | |
87 | 0,\r | |
88 | BLOCK_SIZE,\r | |
89 | BootSector\r | |
90 | );\r | |
91 | ASSERT_EFI_ERROR (Status);\r | |
92 | if ((*(UINT32 *) &BootSector[FAT16_VOLUME_ID_OFFSET] != VolumeId) &&\r | |
93 | (*(UINT32 *) &BootSector[FAT32_VOLUME_ID_OFFSET] != VolumeId)\r | |
94 | ) {\r | |
95 | Status = EFI_NOT_FOUND;\r | |
96 | goto ErrHandle;\r | |
97 | }\r | |
98 | \r | |
99 | *Device = DuplicateDevicePath (DevicePathFromHandle (SimpleFileSystemHandle));\r | |
100 | ASSERT (*Device != NULL);\r | |
101 | \r | |
102 | ErrHandle:\r | |
103 | return Status;\r | |
104 | }\r | |
105 | \r | |
106 | EFI_STATUS\r | |
107 | CheckStoreExists (\r | |
108 | IN EFI_DEVICE_PATH_PROTOCOL *Device\r | |
109 | )\r | |
110 | {\r | |
111 | EFI_HANDLE Handle;\r | |
112 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r | |
113 | EFI_STATUS Status;\r | |
114 | \r | |
115 | Status = gBS->LocateDevicePath (\r | |
116 | &gEfiSimpleFileSystemProtocolGuid,\r | |
117 | &Device, \r | |
118 | &Handle\r | |
119 | );\r | |
120 | \r | |
121 | if (EFI_ERROR (Status)) {\r | |
122 | return Status;\r | |
123 | }\r | |
124 | \r | |
125 | Status = gBS->HandleProtocol (\r | |
126 | Handle,\r | |
127 | &gEfiSimpleFileSystemProtocolGuid,\r | |
128 | &Volume\r | |
129 | );\r | |
130 | if (EFI_ERROR (Status)) {\r | |
131 | return Status;\r | |
132 | }\r | |
133 | \r | |
134 | return EFI_SUCCESS;\r | |
135 | }\r | |
136 | \r | |
137 | VOID\r | |
138 | FileClose (\r | |
b0b961c8 | 139 | IN EFI_FILE_PROTOCOL *File\r |
9071550e | 140 | )\r |
141 | {\r | |
142 | File->Flush (File);\r | |
143 | File->Close (File);\r | |
144 | }\r | |
145 | EFI_STATUS\r | |
146 | FileOpen (\r | |
147 | IN EFI_DEVICE_PATH_PROTOCOL *Device,\r | |
148 | IN CHAR16 *MappedFile,\r | |
b0b961c8 | 149 | OUT EFI_FILE_PROTOCOL **File,\r |
9071550e | 150 | IN UINT64 OpenMode\r |
151 | )\r | |
152 | { \r | |
153 | EFI_HANDLE Handle;\r | |
154 | EFI_FILE_HANDLE Root;\r | |
155 | EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Volume;\r | |
156 | EFI_STATUS Status;\r | |
157 | \r | |
158 | *File = NULL;\r | |
159 | \r | |
160 | Status = gBS->LocateDevicePath (\r | |
161 | &gEfiSimpleFileSystemProtocolGuid,\r | |
162 | &Device, \r | |
163 | &Handle\r | |
164 | );\r | |
165 | \r | |
166 | if (EFI_ERROR (Status)) {\r | |
167 | return Status;\r | |
168 | }\r | |
169 | \r | |
170 | Status = gBS->HandleProtocol (\r | |
171 | Handle,\r | |
172 | &gEfiSimpleFileSystemProtocolGuid,\r | |
173 | &Volume\r | |
174 | );\r | |
175 | ASSERT_EFI_ERROR (Status);\r | |
176 | if (EFI_ERROR (Status)) {\r | |
177 | return Status;\r | |
178 | }\r | |
179 | \r | |
180 | //\r | |
181 | // Open the root directory of the volume\r | |
182 | //\r | |
183 | Root = NULL;\r | |
184 | Status = Volume->OpenVolume (\r | |
185 | Volume,\r | |
186 | &Root\r | |
187 | );\r | |
188 | ASSERT_EFI_ERROR (Status);\r | |
189 | ASSERT (Root != NULL);\r | |
190 | \r | |
191 | //\r | |
192 | // Open file\r | |
193 | //\r | |
194 | Status = Root->Open (\r | |
195 | Root,\r | |
196 | File,\r | |
197 | MappedFile,\r | |
198 | OpenMode,\r | |
199 | 0\r | |
200 | );\r | |
201 | if (EFI_ERROR (Status)) {\r | |
202 | *File = NULL;\r | |
203 | }\r | |
204 | \r | |
205 | //\r | |
206 | // Close the Root directory\r | |
207 | //\r | |
208 | Root->Close (Root);\r | |
209 | return Status;\r | |
210 | }\r |