]>
Commit | Line | Data |
---|---|---|
adbcbf8f | 1 | /** @file\r |
2 | Partition driver that produces logical BlockIo devices from a physical\r | |
3 | BlockIo device. The logical BlockIo devices are based on the format\r | |
4 | of the raw block devices media. Currently "El Torito CD-ROM", Legacy\r | |
5 | MBR, and GPT partition schemes are supported.\r | |
6 | \r | |
69c0fbd2 | 7 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r |
e5eed7d3 | 8 | This program and the accompanying materials\r |
f42be642 | 9 | are licensed and made available under the terms and conditions of the BSD License\r |
10 | which accompanies this distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php\r | |
adbcbf8f | 12 | \r |
f42be642 | 13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
adbcbf8f | 15 | \r |
16 | **/\r | |
17 | \r | |
18 | \r | |
19 | #include "Partition.h"\r | |
20 | \r | |
21 | //\r | |
ff61847d | 22 | // Partition Driver Global Variables.\r |
adbcbf8f | 23 | //\r |
24 | EFI_DRIVER_BINDING_PROTOCOL gPartitionDriverBinding = {\r | |
25 | PartitionDriverBindingSupported,\r | |
26 | PartitionDriverBindingStart,\r | |
27 | PartitionDriverBindingStop,\r | |
c86b273d RN |
28 | //\r |
29 | // Grub4Dos copies the BPB of the first partition to the MBR. If the \r | |
30 | // DriverBindingStart() of the Fat driver gets run before that of Partition \r | |
31 | // driver only the first partition can be recognized.\r | |
32 | // Let the driver binding version of Partition driver be higher than that of\r | |
33 | // Fat driver to make sure the DriverBindingStart() of the Partition driver\r | |
34 | // gets run before that of Fat driver so that all the partitions can be recognized.\r | |
35 | //\r | |
36 | 0xb,\r | |
adbcbf8f | 37 | NULL,\r |
38 | NULL\r | |
39 | };\r | |
40 | \r | |
ff61847d | 41 | //\r |
48557c65 | 42 | // Prioritized function list to detect partition table. \r |
ff61847d | 43 | //\r |
adbcbf8f | 44 | PARTITION_DETECT_ROUTINE mPartitionDetectRoutineTable[] = {\r |
45 | PartitionInstallGptChildHandles,\r | |
46 | PartitionInstallElToritoChildHandles,\r | |
47 | PartitionInstallMbrChildHandles,\r | |
48 | NULL\r | |
49 | };\r | |
50 | \r | |
adbcbf8f | 51 | /**\r |
52 | Test to see if this driver supports ControllerHandle. Any ControllerHandle\r | |
490b5ea1 | 53 | than contains a BlockIo and DiskIo protocol or a BlockIo2 protocol can be\r |
54 | supported.\r | |
adbcbf8f | 55 | \r |
490b5ea1 | 56 | @param[in] This Protocol instance pointer.\r |
57 | @param[in] ControllerHandle Handle of device to test.\r | |
58 | @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r | |
59 | device to start.\r | |
adbcbf8f | 60 | \r |
61 | @retval EFI_SUCCESS This driver supports this device\r | |
62 | @retval EFI_ALREADY_STARTED This driver is already running on this device\r | |
63 | @retval other This driver does not support this device\r | |
64 | \r | |
65 | **/\r | |
66 | EFI_STATUS\r | |
67 | EFIAPI\r | |
68 | PartitionDriverBindingSupported (\r | |
69 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
70 | IN EFI_HANDLE ControllerHandle,\r | |
71 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
72 | )\r | |
73 | {\r | |
74 | EFI_STATUS Status;\r | |
75 | EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r | |
76 | EFI_DISK_IO_PROTOCOL *DiskIo;\r | |
77 | EFI_DEV_PATH *Node;\r | |
78 | \r | |
9be29006 | 79 | //\r |
80 | // Check RemainingDevicePath validation\r | |
81 | //\r | |
adbcbf8f | 82 | if (RemainingDevicePath != NULL) {\r |
9be29006 | 83 | //\r |
84 | // Check if RemainingDevicePath is the End of Device Path Node, \r | |
85 | // if yes, go on checking other conditions\r | |
86 | //\r | |
87 | if (!IsDevicePathEnd (RemainingDevicePath)) {\r | |
88 | //\r | |
89 | // If RemainingDevicePath isn't the End of Device Path Node,\r | |
90 | // check its validation\r | |
91 | //\r | |
92 | Node = (EFI_DEV_PATH *) RemainingDevicePath;\r | |
93 | if (Node->DevPath.Type != MEDIA_DEVICE_PATH ||\r | |
adbcbf8f | 94 | Node->DevPath.SubType != MEDIA_HARDDRIVE_DP ||\r |
9be29006 | 95 | DevicePathNodeLength (&Node->DevPath) != sizeof (HARDDRIVE_DEVICE_PATH)) {\r |
490b5ea1 | 96 | return EFI_UNSUPPORTED;\r |
9be29006 | 97 | }\r |
adbcbf8f | 98 | }\r |
99 | }\r | |
9be29006 | 100 | \r |
adbcbf8f | 101 | //\r |
102 | // Open the IO Abstraction(s) needed to perform the supported test\r | |
103 | //\r | |
104 | Status = gBS->OpenProtocol (\r | |
105 | ControllerHandle,\r | |
9be29006 | 106 | &gEfiDiskIoProtocolGuid,\r |
107 | (VOID **) &DiskIo,\r | |
adbcbf8f | 108 | This->DriverBindingHandle,\r |
109 | ControllerHandle,\r | |
110 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
111 | );\r | |
112 | if (Status == EFI_ALREADY_STARTED) {\r | |
113 | return EFI_SUCCESS;\r | |
114 | }\r | |
adbcbf8f | 115 | if (EFI_ERROR (Status)) {\r |
116 | return Status;\r | |
117 | }\r | |
118 | //\r | |
119 | // Close the I/O Abstraction(s) used to perform the supported test\r | |
120 | //\r | |
121 | gBS->CloseProtocol (\r | |
ff61847d | 122 | ControllerHandle,\r |
9be29006 | 123 | &gEfiDiskIoProtocolGuid,\r |
ff61847d | 124 | This->DriverBindingHandle,\r |
125 | ControllerHandle\r | |
126 | );\r | |
adbcbf8f | 127 | \r |
128 | //\r | |
9be29006 | 129 | // Open the EFI Device Path protocol needed to perform the supported test\r |
adbcbf8f | 130 | //\r |
131 | Status = gBS->OpenProtocol (\r | |
132 | ControllerHandle,\r | |
9be29006 | 133 | &gEfiDevicePathProtocolGuid,\r |
134 | (VOID **) &ParentDevicePath,\r | |
adbcbf8f | 135 | This->DriverBindingHandle,\r |
136 | ControllerHandle,\r | |
137 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
138 | );\r | |
139 | if (Status == EFI_ALREADY_STARTED) {\r | |
140 | return EFI_SUCCESS;\r | |
141 | }\r | |
142 | \r | |
143 | if (EFI_ERROR (Status)) {\r | |
144 | return Status;\r | |
145 | }\r | |
9be29006 | 146 | \r |
adbcbf8f | 147 | //\r |
9be29006 | 148 | // Close protocol, don't use device path protocol in the Support() function\r |
adbcbf8f | 149 | //\r |
150 | gBS->CloseProtocol (\r | |
151 | ControllerHandle,\r | |
9be29006 | 152 | &gEfiDevicePathProtocolGuid,\r |
adbcbf8f | 153 | This->DriverBindingHandle,\r |
154 | ControllerHandle\r | |
155 | );\r | |
156 | \r | |
157 | //\r | |
158 | // Open the IO Abstraction(s) needed to perform the supported test\r | |
159 | //\r | |
160 | Status = gBS->OpenProtocol (\r | |
161 | ControllerHandle,\r | |
162 | &gEfiBlockIoProtocolGuid,\r | |
163 | NULL,\r | |
164 | This->DriverBindingHandle,\r | |
165 | ControllerHandle,\r | |
166 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r | |
167 | );\r | |
21e1018b | 168 | \r |
69c0fbd2 | 169 | return Status;\r |
adbcbf8f | 170 | }\r |
171 | \r | |
adbcbf8f | 172 | /**\r |
490b5ea1 | 173 | Start this driver on ControllerHandle by opening a Block IO or a Block IO2\r |
174 | or both, and Disk IO protocol, reading Device Path, and creating a child\r | |
175 | handle with a Disk IO and device path protocol.\r | |
adbcbf8f | 176 | \r |
490b5ea1 | 177 | @param[in] This Protocol instance pointer.\r |
178 | @param[in] ControllerHandle Handle of device to bind driver to\r | |
179 | @param[in] RemainingDevicePath Optional parameter use to pick a specific child\r | |
180 | device to start.\r | |
adbcbf8f | 181 | \r |
182 | @retval EFI_SUCCESS This driver is added to ControllerHandle\r | |
183 | @retval EFI_ALREADY_STARTED This driver is already running on ControllerHandle\r | |
184 | @retval other This driver does not support this device\r | |
185 | \r | |
186 | **/\r | |
187 | EFI_STATUS\r | |
188 | EFIAPI\r | |
189 | PartitionDriverBindingStart (\r | |
190 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
191 | IN EFI_HANDLE ControllerHandle,\r | |
192 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r | |
193 | )\r | |
194 | {\r | |
195 | EFI_STATUS Status;\r | |
196 | EFI_STATUS OpenStatus;\r | |
197 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
490b5ea1 | 198 | EFI_BLOCK_IO2_PROTOCOL *BlockIo2;\r |
adbcbf8f | 199 | EFI_DISK_IO_PROTOCOL *DiskIo;\r |
493d8e3a | 200 | EFI_DISK_IO2_PROTOCOL *DiskIo2;\r |
adbcbf8f | 201 | EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r |
202 | PARTITION_DETECT_ROUTINE *Routine;\r | |
9afd0514 | 203 | BOOLEAN MediaPresent;\r |
15cc67e6 | 204 | EFI_TPL OldTpl;\r |
adbcbf8f | 205 | \r |
21e1018b | 206 | BlockIo2 = NULL;\r |
15cc67e6 | 207 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK); \r |
9be29006 | 208 | //\r |
209 | // Check RemainingDevicePath validation\r | |
210 | //\r | |
211 | if (RemainingDevicePath != NULL) {\r | |
212 | //\r | |
213 | // Check if RemainingDevicePath is the End of Device Path Node, \r | |
214 | // if yes, return EFI_SUCCESS\r | |
215 | //\r | |
216 | if (IsDevicePathEnd (RemainingDevicePath)) {\r | |
15cc67e6 | 217 | Status = EFI_SUCCESS;\r |
218 | goto Exit;\r | |
9be29006 | 219 | }\r |
220 | }\r | |
221 | \r | |
490b5ea1 | 222 | //\r |
223 | // Try to open BlockIO and BlockIO2. If BlockIO would be opened, continue,\r | |
224 | // otherwise, return error.\r | |
225 | //\r | |
adbcbf8f | 226 | Status = gBS->OpenProtocol (\r |
227 | ControllerHandle,\r | |
228 | &gEfiBlockIoProtocolGuid,\r | |
229 | (VOID **) &BlockIo,\r | |
230 | This->DriverBindingHandle,\r | |
231 | ControllerHandle,\r | |
232 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
233 | );\r | |
234 | if (EFI_ERROR (Status)) {\r | |
15cc67e6 | 235 | goto Exit;\r |
adbcbf8f | 236 | }\r |
490b5ea1 | 237 | \r |
238 | Status = gBS->OpenProtocol (\r | |
239 | ControllerHandle,\r | |
240 | &gEfiBlockIo2ProtocolGuid,\r | |
241 | (VOID **) &BlockIo2,\r | |
242 | This->DriverBindingHandle,\r | |
243 | ControllerHandle,\r | |
493d8e3a | 244 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r |
490b5ea1 | 245 | );\r |
493d8e3a RN |
246 | if (EFI_ERROR (Status)) {\r |
247 | BlockIo2 = NULL;\r | |
248 | }\r | |
490b5ea1 | 249 | \r |
adbcbf8f | 250 | //\r |
490b5ea1 | 251 | // Get the Device Path Protocol on ControllerHandle's handle.\r |
adbcbf8f | 252 | //\r |
253 | Status = gBS->OpenProtocol (\r | |
254 | ControllerHandle,\r | |
255 | &gEfiDevicePathProtocolGuid,\r | |
256 | (VOID **) &ParentDevicePath,\r | |
257 | This->DriverBindingHandle,\r | |
258 | ControllerHandle,\r | |
259 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
260 | );\r | |
261 | if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r | |
15cc67e6 | 262 | goto Exit;\r |
adbcbf8f | 263 | }\r |
264 | \r | |
493d8e3a RN |
265 | //\r |
266 | // Get the DiskIo and DiskIo2.\r | |
267 | //\r | |
adbcbf8f | 268 | Status = gBS->OpenProtocol (\r |
269 | ControllerHandle,\r | |
270 | &gEfiDiskIoProtocolGuid,\r | |
271 | (VOID **) &DiskIo,\r | |
272 | This->DriverBindingHandle,\r | |
273 | ControllerHandle,\r | |
274 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
275 | );\r | |
276 | if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r | |
277 | gBS->CloseProtocol (\r | |
278 | ControllerHandle,\r | |
279 | &gEfiDevicePathProtocolGuid,\r | |
280 | This->DriverBindingHandle,\r | |
281 | ControllerHandle\r | |
282 | );\r | |
15cc67e6 | 283 | goto Exit;\r |
adbcbf8f | 284 | }\r |
285 | \r | |
286 | OpenStatus = Status;\r | |
287 | \r | |
493d8e3a RN |
288 | Status = gBS->OpenProtocol (\r |
289 | ControllerHandle,\r | |
290 | &gEfiDiskIo2ProtocolGuid,\r | |
291 | (VOID **) &DiskIo2,\r | |
292 | This->DriverBindingHandle,\r | |
293 | ControllerHandle,\r | |
294 | EFI_OPEN_PROTOCOL_BY_DRIVER\r | |
295 | );\r | |
296 | if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r | |
297 | DiskIo2 = NULL;\r | |
298 | }\r | |
299 | \r | |
adbcbf8f | 300 | //\r |
9afd0514 | 301 | // Try to read blocks when there's media or it is removable physical partition.\r |
adbcbf8f | 302 | //\r |
9afd0514 | 303 | Status = EFI_UNSUPPORTED;\r |
304 | MediaPresent = BlockIo->Media->MediaPresent;\r | |
305 | if (BlockIo->Media->MediaPresent ||\r | |
306 | (BlockIo->Media->RemovableMedia && !BlockIo->Media->LogicalPartition)) {\r | |
adbcbf8f | 307 | //\r |
308 | // Try for GPT, then El Torito, and then legacy MBR partition types. If the\r | |
309 | // media supports a given partition type install child handles to represent\r | |
310 | // the partitions described by the media.\r | |
311 | //\r | |
312 | Routine = &mPartitionDetectRoutineTable[0];\r | |
313 | while (*Routine != NULL) {\r | |
314 | Status = (*Routine) (\r | |
315 | This,\r | |
316 | ControllerHandle,\r | |
317 | DiskIo,\r | |
493d8e3a | 318 | DiskIo2,\r |
adbcbf8f | 319 | BlockIo,\r |
490b5ea1 | 320 | BlockIo2,\r |
adbcbf8f | 321 | ParentDevicePath\r |
322 | );\r | |
9afd0514 | 323 | if (!EFI_ERROR (Status) || Status == EFI_MEDIA_CHANGED || Status == EFI_NO_MEDIA) {\r |
adbcbf8f | 324 | break;\r |
325 | }\r | |
326 | Routine++;\r | |
327 | }\r | |
328 | }\r | |
329 | //\r | |
330 | // In the case that the driver is already started (OpenStatus == EFI_ALREADY_STARTED),\r | |
331 | // the DevicePathProtocol and the DiskIoProtocol are not actually opened by the\r | |
332 | // driver. So don't try to close them. Otherwise, we will break the dependency\r | |
333 | // between the controller and the driver set up before.\r | |
334 | //\r | |
9afd0514 | 335 | // In the case that when the media changes on a device it will Reinstall the \r |
336 | // BlockIo interaface. This will cause a call to our Stop(), and a subsequent\r | |
337 | // reentrant call to our Start() successfully. We should leave the device open\r | |
338 | // when this happen. The "media change" case includes either the status is\r | |
339 | // EFI_MEDIA_CHANGED or it is a "media" to "no media" change. \r | |
340 | // \r | |
341 | if (EFI_ERROR (Status) &&\r | |
342 | !EFI_ERROR (OpenStatus) &&\r | |
343 | Status != EFI_MEDIA_CHANGED &&\r | |
344 | !(MediaPresent && Status == EFI_NO_MEDIA)) {\r | |
adbcbf8f | 345 | gBS->CloseProtocol (\r |
346 | ControllerHandle,\r | |
347 | &gEfiDiskIoProtocolGuid,\r | |
348 | This->DriverBindingHandle,\r | |
349 | ControllerHandle\r | |
350 | );\r | |
490b5ea1 | 351 | //\r |
80c83a69 | 352 | // Close Parent DiskIo2 if has.\r |
490b5ea1 | 353 | // \r |
354 | gBS->CloseProtocol (\r | |
355 | ControllerHandle,\r | |
80c83a69 | 356 | &gEfiDiskIo2ProtocolGuid,\r |
490b5ea1 | 357 | This->DriverBindingHandle,\r |
358 | ControllerHandle\r | |
359 | );\r | |
adbcbf8f | 360 | \r |
361 | gBS->CloseProtocol (\r | |
362 | ControllerHandle,\r | |
363 | &gEfiDevicePathProtocolGuid,\r | |
364 | This->DriverBindingHandle,\r | |
365 | ControllerHandle\r | |
366 | );\r | |
367 | }\r | |
368 | \r | |
15cc67e6 | 369 | Exit:\r |
370 | gBS->RestoreTPL (OldTpl);\r | |
adbcbf8f | 371 | return Status;\r |
372 | }\r | |
373 | \r | |
adbcbf8f | 374 | /**\r |
48557c65 | 375 | Stop this driver on ControllerHandle. Support stopping any child handles\r |
adbcbf8f | 376 | created by this driver.\r |
377 | \r | |
378 | @param This Protocol instance pointer.\r | |
379 | @param ControllerHandle Handle of device to stop driver on\r | |
380 | @param NumberOfChildren Number of Handles in ChildHandleBuffer. If number of\r | |
381 | children is zero stop the entire bus driver.\r | |
382 | @param ChildHandleBuffer List of Child Handles to Stop.\r | |
383 | \r | |
384 | @retval EFI_SUCCESS This driver is removed ControllerHandle\r | |
385 | @retval other This driver was not removed from this device\r | |
386 | \r | |
387 | **/\r | |
388 | EFI_STATUS\r | |
389 | EFIAPI\r | |
390 | PartitionDriverBindingStop (\r | |
391 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
392 | IN EFI_HANDLE ControllerHandle,\r | |
393 | IN UINTN NumberOfChildren,\r | |
394 | IN EFI_HANDLE *ChildHandleBuffer\r | |
395 | )\r | |
396 | {\r | |
397 | EFI_STATUS Status;\r | |
398 | UINTN Index;\r | |
399 | EFI_BLOCK_IO_PROTOCOL *BlockIo;\r | |
490b5ea1 | 400 | EFI_BLOCK_IO2_PROTOCOL *BlockIo2;\r |
adbcbf8f | 401 | BOOLEAN AllChildrenStopped;\r |
402 | PARTITION_PRIVATE_DATA *Private;\r | |
403 | EFI_DISK_IO_PROTOCOL *DiskIo;\r | |
404 | \r | |
490b5ea1 | 405 | BlockIo = NULL;\r |
406 | BlockIo2 = NULL;\r | |
407 | Private = NULL;\r | |
408 | \r | |
adbcbf8f | 409 | if (NumberOfChildren == 0) {\r |
e3325721 HW |
410 | //\r |
411 | // In the case of re-entry of the PartitionDriverBindingStop, the\r | |
412 | // NumberOfChildren may not reflect the actual number of children on the\r | |
413 | // bus driver. Hence, additional check is needed here.\r | |
414 | //\r | |
415 | if (HasChildren (ControllerHandle)) {\r | |
416 | DEBUG((EFI_D_ERROR, "PartitionDriverBindingStop: Still has child.\n"));\r | |
417 | return EFI_DEVICE_ERROR;\r | |
418 | }\r | |
419 | \r | |
adbcbf8f | 420 | //\r |
421 | // Close the bus driver\r | |
422 | //\r | |
423 | gBS->CloseProtocol (\r | |
424 | ControllerHandle,\r | |
425 | &gEfiDiskIoProtocolGuid,\r | |
426 | This->DriverBindingHandle,\r | |
427 | ControllerHandle\r | |
428 | );\r | |
490b5ea1 | 429 | //\r |
430 | // Close Parent BlockIO2 if has.\r | |
431 | // \r | |
432 | gBS->CloseProtocol (\r | |
433 | ControllerHandle,\r | |
493d8e3a | 434 | &gEfiDiskIo2ProtocolGuid,\r |
490b5ea1 | 435 | This->DriverBindingHandle,\r |
436 | ControllerHandle\r | |
437 | );\r | |
adbcbf8f | 438 | \r |
439 | gBS->CloseProtocol (\r | |
440 | ControllerHandle,\r | |
441 | &gEfiDevicePathProtocolGuid,\r | |
442 | This->DriverBindingHandle,\r | |
443 | ControllerHandle\r | |
444 | );\r | |
adbcbf8f | 445 | return EFI_SUCCESS;\r |
446 | }\r | |
447 | \r | |
448 | AllChildrenStopped = TRUE;\r | |
449 | for (Index = 0; Index < NumberOfChildren; Index++) {\r | |
490b5ea1 | 450 | gBS->OpenProtocol (\r |
451 | ChildHandleBuffer[Index],\r | |
452 | &gEfiBlockIoProtocolGuid,\r | |
453 | (VOID **) &BlockIo,\r | |
454 | This->DriverBindingHandle,\r | |
455 | ControllerHandle,\r | |
456 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
457 | );\r | |
458 | //\r | |
459 | // Try to locate BlockIo2.\r | |
460 | //\r | |
461 | gBS->OpenProtocol (\r | |
462 | ChildHandleBuffer[Index],\r | |
463 | &gEfiBlockIo2ProtocolGuid,\r | |
464 | (VOID **) &BlockIo2,\r | |
465 | This->DriverBindingHandle,\r | |
466 | ControllerHandle,\r | |
467 | EFI_OPEN_PROTOCOL_GET_PROTOCOL\r | |
468 | ); \r | |
469 | \r | |
d0844d13 | 470 | \r |
471 | Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo);\r | |
e3325721 HW |
472 | if (Private->InStop) {\r |
473 | //\r | |
474 | // If the child handle is going to be stopped again during the re-entry\r | |
475 | // of DriverBindingStop, just do nothing.\r | |
476 | //\r | |
477 | break;\r | |
478 | }\r | |
479 | Private->InStop = TRUE;\r | |
adbcbf8f | 480 | \r |
e3325721 HW |
481 | BlockIo->FlushBlocks (BlockIo);\r |
482 | \r | |
483 | if (BlockIo2 != NULL) {\r | |
484 | Status = BlockIo2->FlushBlocksEx (BlockIo2, NULL);\r | |
485 | DEBUG((EFI_D_ERROR, "PartitionDriverBindingStop: FlushBlocksEx returned with %r\n", Status));\r | |
486 | } else {\r | |
487 | Status = EFI_SUCCESS;\r | |
488 | }\r | |
489 | \r | |
490 | gBS->CloseProtocol (\r | |
491 | ControllerHandle,\r | |
492 | &gEfiDiskIoProtocolGuid,\r | |
493 | This->DriverBindingHandle,\r | |
494 | ChildHandleBuffer[Index]\r | |
495 | );\r | |
490b5ea1 | 496 | //\r |
497 | // All Software protocols have be freed from the handle so remove it.\r | |
498 | // Remove the BlockIo Protocol if has.\r | |
499 | // Remove the BlockIo2 Protocol if has.\r | |
500 | //\r | |
501 | if (BlockIo2 != NULL) {\r | |
e3325721 HW |
502 | //\r |
503 | // Some device drivers might re-install the BlockIO(2) protocols for a\r | |
504 | // media change condition. Therefore, if the FlushBlocksEx returned with\r | |
505 | // EFI_MEDIA_CHANGED, just let the BindingStop fail to avoid potential\r | |
506 | // reference of already stopped child handle.\r | |
507 | //\r | |
508 | if (Status != EFI_MEDIA_CHANGED) {\r | |
509 | Status = gBS->UninstallMultipleProtocolInterfaces (\r | |
510 | ChildHandleBuffer[Index],\r | |
511 | &gEfiDevicePathProtocolGuid,\r | |
512 | Private->DevicePath,\r | |
513 | &gEfiBlockIoProtocolGuid,\r | |
514 | &Private->BlockIo,\r | |
515 | &gEfiBlockIo2ProtocolGuid,\r | |
516 | &Private->BlockIo2,\r | |
517 | Private->EspGuid,\r | |
518 | NULL,\r | |
519 | NULL\r | |
520 | );\r | |
521 | }\r | |
490b5ea1 | 522 | } else {\r |
490b5ea1 | 523 | Status = gBS->UninstallMultipleProtocolInterfaces (\r |
524 | ChildHandleBuffer[Index],\r | |
525 | &gEfiDevicePathProtocolGuid,\r | |
526 | Private->DevicePath,\r | |
527 | &gEfiBlockIoProtocolGuid,\r | |
528 | &Private->BlockIo,\r | |
529 | Private->EspGuid,\r | |
530 | NULL,\r | |
531 | NULL\r | |
532 | );\r | |
533 | }\r | |
adbcbf8f | 534 | \r |
490b5ea1 | 535 | if (EFI_ERROR (Status)) {\r |
e3325721 | 536 | Private->InStop = FALSE;\r |
490b5ea1 | 537 | gBS->OpenProtocol (\r |
538 | ControllerHandle,\r | |
539 | &gEfiDiskIoProtocolGuid,\r | |
540 | (VOID **) &DiskIo,\r | |
541 | This->DriverBindingHandle,\r | |
542 | ChildHandleBuffer[Index],\r | |
543 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r | |
544 | );\r | |
545 | } else {\r | |
546 | FreePool (Private->DevicePath);\r | |
547 | FreePool (Private);\r | |
adbcbf8f | 548 | }\r |
549 | \r | |
550 | if (EFI_ERROR (Status)) {\r | |
551 | AllChildrenStopped = FALSE;\r | |
e3325721 HW |
552 | if (Status == EFI_MEDIA_CHANGED) {\r |
553 | break;\r | |
554 | }\r | |
adbcbf8f | 555 | }\r |
556 | }\r | |
557 | \r | |
558 | if (!AllChildrenStopped) {\r | |
559 | return EFI_DEVICE_ERROR;\r | |
560 | }\r | |
561 | \r | |
562 | return EFI_SUCCESS;\r | |
563 | }\r | |
564 | \r | |
565 | \r | |
566 | /**\r | |
567 | Reset the Block Device.\r | |
568 | \r | |
569 | @param This Protocol instance pointer.\r | |
570 | @param ExtendedVerification Driver may perform diagnostics on reset.\r | |
571 | \r | |
572 | @retval EFI_SUCCESS The device was reset.\r | |
573 | @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r | |
574 | not be reset.\r | |
575 | \r | |
576 | **/\r | |
adbcbf8f | 577 | EFI_STATUS\r |
578 | EFIAPI\r | |
579 | PartitionReset (\r | |
580 | IN EFI_BLOCK_IO_PROTOCOL *This,\r | |
581 | IN BOOLEAN ExtendedVerification\r | |
582 | )\r | |
583 | {\r | |
584 | PARTITION_PRIVATE_DATA *Private;\r | |
585 | \r | |
586 | Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r | |
587 | \r | |
588 | return Private->ParentBlockIo->Reset (\r | |
589 | Private->ParentBlockIo,\r | |
590 | ExtendedVerification\r | |
591 | );\r | |
592 | }\r | |
593 | \r | |
fcf5e49d RN |
594 | /**\r |
595 | Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED\r | |
596 | for no media or media change case. Otherwise DefaultStatus is returned.\r | |
597 | \r | |
598 | @param DiskIo Pointer to the DiskIo instance.\r | |
599 | @param MediaId Id of the media, changes every time the media is replaced.\r | |
600 | @param DefaultStatus The default status to return when it's not the no media\r | |
601 | or media change case.\r | |
602 | \r | |
603 | @retval EFI_NO_MEDIA There is no media.\r | |
604 | @retval EFI_MEDIA_CHANGED The media was changed.\r | |
605 | @retval others The default status to return.\r | |
606 | **/\r | |
607 | EFI_STATUS\r | |
608 | ProbeMediaStatus (\r | |
609 | IN EFI_DISK_IO_PROTOCOL *DiskIo,\r | |
610 | IN UINT32 MediaId,\r | |
611 | IN EFI_STATUS DefaultStatus\r | |
612 | )\r | |
613 | {\r | |
614 | EFI_STATUS Status;\r | |
df473cc1 | 615 | UINT8 Buffer[1];\r |
fcf5e49d RN |
616 | \r |
617 | //\r | |
df473cc1 RC |
618 | // Read 1 byte from offset 0 to check if the MediaId is still valid.\r |
619 | // The reading operation is synchronious thus it is not worth it to\r | |
620 | // allocate a buffer from the pool. The destination buffer for the\r | |
621 | // data is in the stack.\r | |
fcf5e49d | 622 | //\r |
df473cc1 | 623 | Status = DiskIo->ReadDisk (DiskIo, MediaId, 0, 1, (VOID*)Buffer);\r |
fcf5e49d RN |
624 | if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r |
625 | return Status;\r | |
626 | }\r | |
627 | return DefaultStatus;\r | |
628 | }\r | |
adbcbf8f | 629 | \r |
630 | /**\r | |
631 | Read by using the Disk IO protocol on the parent device. Lba addresses\r | |
632 | must be converted to byte offsets.\r | |
633 | \r | |
634 | @param This Protocol instance pointer.\r | |
635 | @param MediaId Id of the media, changes every time the media is replaced.\r | |
636 | @param Lba The starting Logical Block Address to read from\r | |
637 | @param BufferSize Size of Buffer, must be a multiple of device block size.\r | |
638 | @param Buffer Buffer containing read data\r | |
639 | \r | |
640 | @retval EFI_SUCCESS The data was read correctly from the device.\r | |
641 | @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r | |
642 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
643 | @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.\r | |
644 | @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r | |
645 | @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not\r | |
646 | valid for the device.\r | |
647 | \r | |
648 | **/\r | |
adbcbf8f | 649 | EFI_STATUS\r |
650 | EFIAPI\r | |
651 | PartitionReadBlocks (\r | |
652 | IN EFI_BLOCK_IO_PROTOCOL *This,\r | |
653 | IN UINT32 MediaId,\r | |
654 | IN EFI_LBA Lba,\r | |
655 | IN UINTN BufferSize,\r | |
656 | OUT VOID *Buffer\r | |
657 | )\r | |
658 | {\r | |
659 | PARTITION_PRIVATE_DATA *Private;\r | |
660 | UINT64 Offset;\r | |
661 | \r | |
662 | Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r | |
663 | \r | |
664 | if (BufferSize % Private->BlockSize != 0) {\r | |
fcf5e49d | 665 | return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_BAD_BUFFER_SIZE);\r |
adbcbf8f | 666 | }\r |
667 | \r | |
668 | Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r | |
669 | if (Offset + BufferSize > Private->End) {\r | |
fcf5e49d | 670 | return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_INVALID_PARAMETER);\r |
adbcbf8f | 671 | }\r |
672 | //\r | |
673 | // Because some kinds of partition have different block size from their parent\r | |
674 | // device, we call the Disk IO protocol on the parent device, not the Block IO\r | |
675 | // protocol\r | |
676 | //\r | |
677 | return Private->DiskIo->ReadDisk (Private->DiskIo, MediaId, Offset, BufferSize, Buffer);\r | |
678 | }\r | |
679 | \r | |
680 | /**\r | |
681 | Write by using the Disk IO protocol on the parent device. Lba addresses\r | |
682 | must be converted to byte offsets.\r | |
683 | \r | |
490b5ea1 | 684 | @param[in] This Protocol instance pointer.\r |
685 | @param[in] MediaId Id of the media, changes every time the media is replaced.\r | |
686 | @param[in] Lba The starting Logical Block Address to read from\r | |
687 | @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r | |
688 | @param[in] Buffer Buffer containing data to be written to device.\r | |
adbcbf8f | 689 | \r |
690 | @retval EFI_SUCCESS The data was written correctly to the device.\r | |
691 | @retval EFI_WRITE_PROTECTED The device can not be written to.\r | |
692 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r | |
693 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
694 | @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r | |
695 | @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r | |
696 | @retval EFI_INVALID_PARAMETER The write request contains a LBA that is not\r | |
697 | valid for the device.\r | |
698 | \r | |
699 | **/\r | |
adbcbf8f | 700 | EFI_STATUS\r |
701 | EFIAPI\r | |
702 | PartitionWriteBlocks (\r | |
703 | IN EFI_BLOCK_IO_PROTOCOL *This,\r | |
704 | IN UINT32 MediaId,\r | |
705 | IN EFI_LBA Lba,\r | |
706 | IN UINTN BufferSize,\r | |
490b5ea1 | 707 | IN VOID *Buffer\r |
adbcbf8f | 708 | )\r |
709 | {\r | |
710 | PARTITION_PRIVATE_DATA *Private;\r | |
711 | UINT64 Offset;\r | |
712 | \r | |
713 | Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r | |
714 | \r | |
715 | if (BufferSize % Private->BlockSize != 0) {\r | |
fcf5e49d | 716 | return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_BAD_BUFFER_SIZE);\r |
adbcbf8f | 717 | }\r |
718 | \r | |
719 | Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r | |
720 | if (Offset + BufferSize > Private->End) {\r | |
fcf5e49d | 721 | return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_INVALID_PARAMETER);\r |
adbcbf8f | 722 | }\r |
723 | //\r | |
724 | // Because some kinds of partition have different block size from their parent\r | |
725 | // device, we call the Disk IO protocol on the parent device, not the Block IO\r | |
726 | // protocol\r | |
727 | //\r | |
728 | return Private->DiskIo->WriteDisk (Private->DiskIo, MediaId, Offset, BufferSize, Buffer);\r | |
729 | }\r | |
730 | \r | |
731 | \r | |
732 | /**\r | |
733 | Flush the parent Block Device.\r | |
734 | \r | |
735 | @param This Protocol instance pointer.\r | |
736 | \r | |
737 | @retval EFI_SUCCESS All outstanding data was written to the device\r | |
738 | @retval EFI_DEVICE_ERROR The device reported an error while writting back the data\r | |
739 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
740 | \r | |
741 | **/\r | |
adbcbf8f | 742 | EFI_STATUS\r |
743 | EFIAPI\r | |
744 | PartitionFlushBlocks (\r | |
745 | IN EFI_BLOCK_IO_PROTOCOL *This\r | |
746 | )\r | |
747 | {\r | |
748 | PARTITION_PRIVATE_DATA *Private;\r | |
749 | \r | |
750 | Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r | |
751 | \r | |
752 | return Private->ParentBlockIo->FlushBlocks (Private->ParentBlockIo);\r | |
753 | }\r | |
754 | \r | |
65fd3952 RN |
755 | /**\r |
756 | Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED\r | |
757 | for no media or media change case. Otherwise DefaultStatus is returned.\r | |
758 | \r | |
493d8e3a | 759 | @param DiskIo2 Pointer to the DiskIo2 instance.\r |
65fd3952 RN |
760 | @param MediaId Id of the media, changes every time the media is replaced.\r |
761 | @param DefaultStatus The default status to return when it's not the no media\r | |
762 | or media change case.\r | |
763 | \r | |
764 | @retval EFI_NO_MEDIA There is no media.\r | |
765 | @retval EFI_MEDIA_CHANGED The media was changed.\r | |
766 | @retval others The default status to return.\r | |
767 | **/\r | |
768 | EFI_STATUS\r | |
769 | ProbeMediaStatusEx (\r | |
493d8e3a | 770 | IN EFI_DISK_IO2_PROTOCOL *DiskIo2,\r |
65fd3952 RN |
771 | IN UINT32 MediaId,\r |
772 | IN EFI_STATUS DefaultStatus\r | |
773 | )\r | |
774 | {\r | |
775 | EFI_STATUS Status;\r | |
776 | \r | |
777 | //\r | |
493d8e3a | 778 | // Read 1 byte from offset 0 but passing NULL as buffer pointer\r |
65fd3952 | 779 | //\r |
493d8e3a | 780 | Status = DiskIo2->ReadDiskEx (DiskIo2, MediaId, 0, NULL, 1, NULL);\r |
65fd3952 RN |
781 | if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r |
782 | return Status;\r | |
783 | }\r | |
784 | return DefaultStatus;\r | |
785 | }\r | |
786 | \r | |
490b5ea1 | 787 | /**\r |
788 | Reset the Block Device throught Block I/O2 protocol.\r | |
789 | \r | |
790 | @param This Protocol instance pointer.\r | |
791 | @param ExtendedVerification Driver may perform diagnostics on reset.\r | |
792 | \r | |
793 | @retval EFI_SUCCESS The device was reset.\r | |
794 | @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r | |
795 | not be reset.\r | |
796 | \r | |
797 | **/\r | |
798 | EFI_STATUS\r | |
799 | EFIAPI\r | |
800 | PartitionResetEx (\r | |
801 | IN EFI_BLOCK_IO2_PROTOCOL *This,\r | |
802 | IN BOOLEAN ExtendedVerification\r | |
803 | )\r | |
804 | {\r | |
805 | PARTITION_PRIVATE_DATA *Private;\r | |
806 | \r | |
807 | Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r | |
808 | \r | |
809 | return Private->ParentBlockIo2->Reset (\r | |
810 | Private->ParentBlockIo2,\r | |
811 | ExtendedVerification\r | |
812 | );\r | |
813 | }\r | |
814 | \r | |
493d8e3a RN |
815 | /**\r |
816 | The general callback for the DiskIo2 interfaces.\r | |
817 | @param Event Event whose notification function is being invoked.\r | |
818 | @param Context The pointer to the notification function's context,\r | |
819 | which points to the PARTITION_ACCESS_TASK instance.\r | |
820 | **/\r | |
821 | VOID\r | |
822 | EFIAPI\r | |
823 | PartitionOnAccessComplete (\r | |
824 | IN EFI_EVENT Event,\r | |
825 | IN VOID *Context\r | |
826 | )\r | |
827 | {\r | |
828 | PARTITION_ACCESS_TASK *Task;\r | |
829 | \r | |
830 | Task = (PARTITION_ACCESS_TASK *) Context;\r | |
831 | \r | |
832 | gBS->CloseEvent (Event);\r | |
833 | \r | |
834 | Task->BlockIo2Token->TransactionStatus = Task->DiskIo2Token.TransactionStatus;\r | |
835 | gBS->SignalEvent (Task->BlockIo2Token->Event);\r | |
836 | \r | |
837 | FreePool (Task);\r | |
838 | }\r | |
839 | \r | |
840 | /**\r | |
841 | Create a new PARTITION_ACCESS_TASK instance.\r | |
842 | \r | |
843 | @param Token Pointer to the EFI_BLOCK_IO2_TOKEN.\r | |
844 | \r | |
845 | @return Pointer to the created PARTITION_ACCESS_TASK instance or NULL upon failure.\r | |
846 | **/\r | |
847 | PARTITION_ACCESS_TASK *\r | |
848 | PartitionCreateAccessTask (\r | |
849 | IN EFI_BLOCK_IO2_TOKEN *Token\r | |
850 | )\r | |
851 | {\r | |
852 | EFI_STATUS Status;\r | |
853 | PARTITION_ACCESS_TASK *Task;\r | |
854 | \r | |
855 | Task = AllocatePool (sizeof (*Task));\r | |
856 | if (Task == NULL) {\r | |
857 | return NULL;\r | |
858 | }\r | |
859 | \r | |
860 | Status = gBS->CreateEvent (\r | |
861 | EVT_NOTIFY_SIGNAL,\r | |
80c83a69 | 862 | TPL_NOTIFY,\r |
493d8e3a RN |
863 | PartitionOnAccessComplete,\r |
864 | Task,\r | |
865 | &Task->DiskIo2Token.Event\r | |
866 | );\r | |
867 | if (EFI_ERROR (Status)) {\r | |
868 | FreePool (Task);\r | |
869 | return NULL;\r | |
870 | }\r | |
871 | \r | |
872 | Task->BlockIo2Token = Token;\r | |
873 | \r | |
874 | return Task;\r | |
875 | }\r | |
876 | \r | |
490b5ea1 | 877 | /**\r |
878 | Read BufferSize bytes from Lba into Buffer.\r | |
879 | \r | |
880 | This function reads the requested number of blocks from the device. All the\r | |
881 | blocks are read, or an error is returned.\r | |
882 | If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and\r | |
883 | non-blocking I/O is being used, the Event associated with this request will\r | |
884 | not be signaled.\r | |
885 | \r | |
886 | @param[in] This Indicates a pointer to the calling context.\r | |
887 | @param[in] MediaId Id of the media, changes every time the media is \r | |
888 | replaced.\r | |
889 | @param[in] Lba The starting Logical Block Address to read from.\r | |
890 | @param[in, out] Token A pointer to the token associated with the transaction.\r | |
891 | @param[in] BufferSize Size of Buffer, must be a multiple of device block size. \r | |
892 | @param[out] Buffer A pointer to the destination buffer for the data. The \r | |
893 | caller is responsible for either having implicit or \r | |
894 | explicit ownership of the buffer.\r | |
895 | \r | |
896 | @retval EFI_SUCCESS The read request was queued if Token->Event is\r | |
897 | not NULL.The data was read correctly from the\r | |
898 | device if the Token->Event is NULL.\r | |
899 | @retval EFI_DEVICE_ERROR The device reported an error while performing\r | |
900 | the read.\r | |
901 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
902 | @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r | |
903 | @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the\r | |
904 | intrinsic block size of the device.\r | |
905 | @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid, \r | |
906 | or the buffer is not on proper alignment.\r | |
907 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r | |
908 | of resources.\r | |
909 | **/\r | |
910 | EFI_STATUS\r | |
911 | EFIAPI\r | |
912 | PartitionReadBlocksEx (\r | |
913 | IN EFI_BLOCK_IO2_PROTOCOL *This,\r | |
914 | IN UINT32 MediaId,\r | |
915 | IN EFI_LBA Lba,\r | |
916 | IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r | |
917 | IN UINTN BufferSize,\r | |
918 | OUT VOID *Buffer\r | |
919 | )\r | |
920 | {\r | |
493d8e3a | 921 | EFI_STATUS Status;\r |
490b5ea1 | 922 | PARTITION_PRIVATE_DATA *Private;\r |
923 | UINT64 Offset;\r | |
493d8e3a | 924 | PARTITION_ACCESS_TASK *Task;\r |
490b5ea1 | 925 | \r |
65fd3952 RN |
926 | Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r |
927 | \r | |
490b5ea1 | 928 | if (BufferSize % Private->BlockSize != 0) {\r |
493d8e3a | 929 | return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_BAD_BUFFER_SIZE);\r |
490b5ea1 | 930 | }\r |
931 | \r | |
932 | Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r | |
933 | if (Offset + BufferSize > Private->End) {\r | |
493d8e3a | 934 | return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r |
490b5ea1 | 935 | }\r |
936 | \r | |
493d8e3a RN |
937 | if ((Token != NULL) && (Token->Event != NULL)) {\r |
938 | Task = PartitionCreateAccessTask (Token);\r | |
939 | if (Task == NULL) {\r | |
940 | return EFI_OUT_OF_RESOURCES;\r | |
941 | }\r | |
490b5ea1 | 942 | \r |
493d8e3a RN |
943 | Status = Private->DiskIo2->ReadDiskEx (Private->DiskIo2, MediaId, Offset, &Task->DiskIo2Token, BufferSize, Buffer);\r |
944 | if (EFI_ERROR (Status)) {\r | |
945 | gBS->CloseEvent (Task->DiskIo2Token.Event);\r | |
946 | FreePool (Task);\r | |
947 | }\r | |
948 | } else {\r | |
949 | Status = Private->DiskIo2->ReadDiskEx (Private->DiskIo2, MediaId, Offset, NULL, BufferSize, Buffer);\r | |
490b5ea1 | 950 | }\r |
951 | \r | |
493d8e3a | 952 | return Status;\r |
490b5ea1 | 953 | }\r |
954 | \r | |
955 | /**\r | |
956 | Write BufferSize bytes from Lba into Buffer.\r | |
957 | \r | |
958 | This function writes the requested number of blocks to the device. All blocks\r | |
959 | are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,\r | |
960 | EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is\r | |
961 | being used, the Event associated with this request will not be signaled.\r | |
962 | \r | |
963 | @param[in] This Indicates a pointer to the calling context.\r | |
964 | @param[in] MediaId The media ID that the write request is for.\r | |
965 | @param[in] Lba The starting logical block address to be written. The\r | |
966 | caller is responsible for writing to only legitimate\r | |
967 | locations.\r | |
968 | @param[in, out] Token A pointer to the token associated with the transaction.\r | |
969 | @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r | |
970 | @param[in] Buffer A pointer to the source buffer for the data.\r | |
971 | \r | |
972 | @retval EFI_SUCCESS The write request was queued if Event is not NULL.\r | |
973 | The data was written correctly to the device if\r | |
974 | the Event is NULL.\r | |
975 | @retval EFI_WRITE_PROTECTED The device can not be written to.\r | |
976 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
977 | @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r | |
978 | @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r | |
979 | @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r | |
980 | @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid, \r | |
981 | or the buffer is not on proper alignment.\r | |
982 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r | |
983 | of resources.\r | |
984 | \r | |
985 | **/\r | |
986 | EFI_STATUS\r | |
987 | EFIAPI\r | |
988 | PartitionWriteBlocksEx (\r | |
989 | IN EFI_BLOCK_IO2_PROTOCOL *This,\r | |
990 | IN UINT32 MediaId,\r | |
991 | IN EFI_LBA Lba,\r | |
992 | IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r | |
993 | IN UINTN BufferSize,\r | |
994 | IN VOID *Buffer\r | |
995 | )\r | |
996 | {\r | |
493d8e3a | 997 | EFI_STATUS Status;\r |
490b5ea1 | 998 | PARTITION_PRIVATE_DATA *Private;\r |
999 | UINT64 Offset;\r | |
493d8e3a | 1000 | PARTITION_ACCESS_TASK *Task;\r |
490b5ea1 | 1001 | \r |
65fd3952 RN |
1002 | Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r |
1003 | \r | |
490b5ea1 | 1004 | if (BufferSize % Private->BlockSize != 0) {\r |
493d8e3a | 1005 | return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_BAD_BUFFER_SIZE);\r |
490b5ea1 | 1006 | }\r |
1007 | \r | |
1008 | Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r | |
1009 | if (Offset + BufferSize > Private->End) {\r | |
493d8e3a | 1010 | return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r |
490b5ea1 | 1011 | }\r |
493d8e3a RN |
1012 | \r |
1013 | if ((Token != NULL) && (Token->Event != NULL)) {\r | |
1014 | Task = PartitionCreateAccessTask (Token);\r | |
1015 | if (Task == NULL) {\r | |
1016 | return EFI_OUT_OF_RESOURCES;\r | |
1017 | }\r | |
490b5ea1 | 1018 | \r |
493d8e3a RN |
1019 | Status = Private->DiskIo2->WriteDiskEx (Private->DiskIo2, MediaId, Offset, &Task->DiskIo2Token, BufferSize, Buffer);\r |
1020 | if (EFI_ERROR (Status)) {\r | |
1021 | gBS->CloseEvent (Task->DiskIo2Token.Event);\r | |
1022 | FreePool (Task);\r | |
1023 | }\r | |
1024 | } else {\r | |
1025 | Status = Private->DiskIo2->WriteDiskEx (Private->DiskIo2, MediaId, Offset, NULL, BufferSize, Buffer);\r | |
490b5ea1 | 1026 | }\r |
493d8e3a | 1027 | return Status;\r |
490b5ea1 | 1028 | }\r |
1029 | \r | |
1030 | /**\r | |
1031 | Flush the Block Device.\r | |
1032 | \r | |
1033 | If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED\r | |
1034 | is returned and non-blocking I/O is being used, the Event associated with\r | |
1035 | this request will not be signaled. \r | |
1036 | \r | |
1037 | @param[in] This Indicates a pointer to the calling context.\r | |
86d8e199 | 1038 | @param[in, out] Token A pointer to the token associated with the transaction\r |
490b5ea1 | 1039 | \r |
1040 | @retval EFI_SUCCESS The flush request was queued if Event is not NULL.\r | |
1041 | All outstanding data was written correctly to the\r | |
1042 | device if the Event is NULL.\r | |
1043 | @retval EFI_DEVICE_ERROR The device reported an error while writting back\r | |
1044 | the data.\r | |
1045 | @retval EFI_WRITE_PROTECTED The device cannot be written to.\r | |
1046 | @retval EFI_NO_MEDIA There is no media in the device.\r | |
1047 | @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r | |
1048 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r | |
1049 | of resources.\r | |
1050 | \r | |
1051 | **/\r | |
1052 | EFI_STATUS\r | |
1053 | EFIAPI\r | |
1054 | PartitionFlushBlocksEx (\r | |
1055 | IN EFI_BLOCK_IO2_PROTOCOL *This,\r | |
1056 | IN OUT EFI_BLOCK_IO2_TOKEN *Token\r | |
1057 | )\r | |
1058 | {\r | |
493d8e3a | 1059 | EFI_STATUS Status;\r |
490b5ea1 | 1060 | PARTITION_PRIVATE_DATA *Private;\r |
493d8e3a | 1061 | PARTITION_ACCESS_TASK *Task;\r |
490b5ea1 | 1062 | \r |
1063 | Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r | |
1064 | \r | |
493d8e3a RN |
1065 | if ((Token != NULL) && (Token->Event != NULL)) {\r |
1066 | Task = PartitionCreateAccessTask (Token);\r | |
1067 | if (Task == NULL) {\r | |
1068 | return EFI_OUT_OF_RESOURCES;\r | |
1069 | }\r | |
490b5ea1 | 1070 | \r |
493d8e3a RN |
1071 | Status = Private->DiskIo2->FlushDiskEx (Private->DiskIo2, &Task->DiskIo2Token);\r |
1072 | if (EFI_ERROR (Status)) {\r | |
1073 | gBS->CloseEvent (Task->DiskIo2Token.Event);\r | |
1074 | FreePool (Task);\r | |
1075 | }\r | |
1076 | } else {\r | |
1077 | Status = Private->DiskIo2->FlushDiskEx (Private->DiskIo2, NULL);\r | |
1078 | }\r | |
1079 | return Status;\r | |
490b5ea1 | 1080 | }\r |
adbcbf8f | 1081 | \r |
1082 | \r | |
1083 | /**\r | |
1084 | Create a child handle for a logical block device that represents the\r | |
1085 | bytes Start to End of the Parent Block IO device.\r | |
1086 | \r | |
490b5ea1 | 1087 | @param[in] This Protocol instance pointer.\r |
1088 | @param[in] ParentHandle Parent Handle for new child.\r | |
1089 | @param[in] ParentDiskIo Parent DiskIo interface.\r | |
493d8e3a | 1090 | @param[in] ParentDiskIo2 Parent DiskIo2 interface.\r |
490b5ea1 | 1091 | @param[in] ParentBlockIo Parent BlockIo interface.\r |
1092 | @param[in] ParentBlockIo2 Parent BlockIo2 interface.\r | |
1093 | @param[in] ParentDevicePath Parent Device Path.\r | |
1094 | @param[in] DevicePathNode Child Device Path node.\r | |
1095 | @param[in] Start Start Block.\r | |
1096 | @param[in] End End Block.\r | |
1097 | @param[in] BlockSize Child block size.\r | |
1098 | @param[in] InstallEspGuid Flag to install EFI System Partition GUID on handle.\r | |
1099 | \r | |
1100 | @retval EFI_SUCCESS A child handle was added.\r | |
1101 | @retval other A child handle was not added.\r | |
adbcbf8f | 1102 | \r |
1103 | **/\r | |
1104 | EFI_STATUS\r | |
1105 | PartitionInstallChildHandle (\r | |
1106 | IN EFI_DRIVER_BINDING_PROTOCOL *This,\r | |
1107 | IN EFI_HANDLE ParentHandle,\r | |
1108 | IN EFI_DISK_IO_PROTOCOL *ParentDiskIo,\r | |
493d8e3a | 1109 | IN EFI_DISK_IO2_PROTOCOL *ParentDiskIo2,\r |
adbcbf8f | 1110 | IN EFI_BLOCK_IO_PROTOCOL *ParentBlockIo,\r |
490b5ea1 | 1111 | IN EFI_BLOCK_IO2_PROTOCOL *ParentBlockIo2,\r |
adbcbf8f | 1112 | IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r |
1113 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePathNode,\r | |
1114 | IN EFI_LBA Start,\r | |
1115 | IN EFI_LBA End,\r | |
1116 | IN UINT32 BlockSize,\r | |
1117 | IN BOOLEAN InstallEspGuid\r | |
1118 | )\r | |
1119 | {\r | |
1120 | EFI_STATUS Status;\r | |
1121 | PARTITION_PRIVATE_DATA *Private;\r | |
1122 | \r | |
490b5ea1 | 1123 | Status = EFI_SUCCESS;\r |
adbcbf8f | 1124 | Private = AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA));\r |
1125 | if (Private == NULL) {\r | |
1126 | return EFI_OUT_OF_RESOURCES;\r | |
1127 | }\r | |
1128 | \r | |
1129 | Private->Signature = PARTITION_PRIVATE_DATA_SIGNATURE;\r | |
1130 | \r | |
1131 | Private->Start = MultU64x32 (Start, ParentBlockIo->Media->BlockSize);\r | |
1132 | Private->End = MultU64x32 (End + 1, ParentBlockIo->Media->BlockSize);\r | |
1133 | \r | |
1134 | Private->BlockSize = BlockSize;\r | |
1135 | Private->ParentBlockIo = ParentBlockIo;\r | |
490b5ea1 | 1136 | Private->ParentBlockIo2 = ParentBlockIo2;\r |
adbcbf8f | 1137 | Private->DiskIo = ParentDiskIo;\r |
493d8e3a | 1138 | Private->DiskIo2 = ParentDiskIo2;\r |
adbcbf8f | 1139 | \r |
d0844d13 | 1140 | //\r |
1141 | // Set the BlockIO into Private Data.\r | |
1142 | //\r | |
1143 | Private->BlockIo.Revision = ParentBlockIo->Revision;\r | |
1144 | \r | |
1145 | Private->BlockIo.Media = &Private->Media;\r | |
1146 | CopyMem (Private->BlockIo.Media, ParentBlockIo->Media, sizeof (EFI_BLOCK_IO_MEDIA));\r | |
490b5ea1 | 1147 | \r |
d0844d13 | 1148 | Private->BlockIo.Reset = PartitionReset;\r |
1149 | Private->BlockIo.ReadBlocks = PartitionReadBlocks;\r | |
1150 | Private->BlockIo.WriteBlocks = PartitionWriteBlocks;\r | |
1151 | Private->BlockIo.FlushBlocks = PartitionFlushBlocks;\r | |
490b5ea1 | 1152 | \r |
d0844d13 | 1153 | //\r |
1154 | // Set the BlockIO2 into Private Data.\r | |
1155 | //\r | |
493d8e3a RN |
1156 | if (Private->DiskIo2 != NULL) {\r |
1157 | ASSERT (Private->ParentBlockIo2 != NULL);\r | |
490b5ea1 | 1158 | Private->BlockIo2.Media = &Private->Media2;\r |
1159 | CopyMem (Private->BlockIo2.Media, ParentBlockIo2->Media, sizeof (EFI_BLOCK_IO_MEDIA));\r | |
1160 | \r | |
1161 | Private->BlockIo2.Reset = PartitionResetEx;\r | |
1162 | Private->BlockIo2.ReadBlocksEx = PartitionReadBlocksEx;\r | |
1163 | Private->BlockIo2.WriteBlocksEx = PartitionWriteBlocksEx;\r | |
1164 | Private->BlockIo2.FlushBlocksEx = PartitionFlushBlocksEx; \r | |
1165 | }\r | |
67f802ed | 1166 | \r |
67f802ed | 1167 | Private->Media.IoAlign = 0;\r |
490b5ea1 | 1168 | Private->Media.LogicalPartition = TRUE;\r |
adbcbf8f | 1169 | Private->Media.LastBlock = DivU64x32 (\r |
1170 | MultU64x32 (\r | |
1171 | End - Start + 1,\r | |
d0844d13 | 1172 | ParentBlockIo->Media->BlockSize\r |
adbcbf8f | 1173 | ),\r |
490b5ea1 | 1174 | BlockSize\r |
adbcbf8f | 1175 | ) - 1;\r |
1176 | \r | |
67f802ed | 1177 | Private->Media.BlockSize = (UINT32) BlockSize;\r |
adbcbf8f | 1178 | \r |
493d8e3a | 1179 | Private->Media2.IoAlign = 0;\r |
490b5ea1 | 1180 | Private->Media2.LogicalPartition = TRUE;\r |
1181 | Private->Media2.LastBlock = Private->Media.LastBlock;\r | |
1182 | Private->Media2.BlockSize = (UINT32) BlockSize;\r | |
1183 | \r | |
fe3b68bb | 1184 | //\r |
0e87144e | 1185 | // Per UEFI Spec, LowestAlignedLba, LogicalBlocksPerPhysicalBlock and OptimalTransferLengthGranularity must be 0\r |
fe3b68bb RN |
1186 | // for logical partitions.\r |
1187 | //\r | |
1188 | if (Private->BlockIo.Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION2) {\r | |
c582ad45 EB |
1189 | Private->Media.LowestAlignedLba = 0;\r |
1190 | Private->Media.LogicalBlocksPerPhysicalBlock = 0;\r | |
1191 | Private->Media2.LowestAlignedLba = 0;\r | |
1192 | Private->Media2.LogicalBlocksPerPhysicalBlock = 0;\r | |
0e87144e | 1193 | if (Private->BlockIo.Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION3) {\r |
c582ad45 EB |
1194 | Private->Media.OptimalTransferLengthGranularity = 0;\r |
1195 | Private->Media2.OptimalTransferLengthGranularity = 0;\r | |
0e87144e | 1196 | }\r |
fe3b68bb RN |
1197 | }\r |
1198 | \r | |
490b5ea1 | 1199 | Private->DevicePath = AppendDevicePathNode (ParentDevicePath, DevicePathNode);\r |
adbcbf8f | 1200 | \r |
1201 | if (Private->DevicePath == NULL) {\r | |
1202 | FreePool (Private);\r | |
1203 | return EFI_OUT_OF_RESOURCES;\r | |
1204 | }\r | |
1205 | \r | |
1206 | if (InstallEspGuid) {\r | |
1207 | Private->EspGuid = &gEfiPartTypeSystemPartGuid;\r | |
1208 | } else {\r | |
1209 | //\r | |
1210 | // If NULL InstallMultipleProtocolInterfaces will ignore it.\r | |
1211 | //\r | |
1212 | Private->EspGuid = NULL;\r | |
1213 | }\r | |
490b5ea1 | 1214 | \r |
adbcbf8f | 1215 | //\r |
490b5ea1 | 1216 | // Create the new handle. \r |
adbcbf8f | 1217 | //\r |
1218 | Private->Handle = NULL;\r | |
493d8e3a | 1219 | if (Private->DiskIo2 != NULL) {\r |
490b5ea1 | 1220 | Status = gBS->InstallMultipleProtocolInterfaces (\r |
1221 | &Private->Handle,\r | |
1222 | &gEfiDevicePathProtocolGuid,\r | |
1223 | Private->DevicePath,\r | |
1224 | &gEfiBlockIoProtocolGuid,\r | |
1225 | &Private->BlockIo,\r | |
1226 | &gEfiBlockIo2ProtocolGuid,\r | |
1227 | &Private->BlockIo2,\r | |
1228 | Private->EspGuid,\r | |
1229 | NULL,\r | |
1230 | NULL\r | |
1231 | );\r | |
d0844d13 | 1232 | } else { \r |
1233 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1234 | &Private->Handle,\r | |
1235 | &gEfiDevicePathProtocolGuid,\r | |
1236 | Private->DevicePath,\r | |
1237 | &gEfiBlockIoProtocolGuid,\r | |
1238 | &Private->BlockIo,\r | |
1239 | Private->EspGuid,\r | |
1240 | NULL,\r | |
1241 | NULL\r | |
1242 | );\r | |
490b5ea1 | 1243 | }\r |
adbcbf8f | 1244 | \r |
1245 | if (!EFI_ERROR (Status)) {\r | |
1246 | //\r | |
1247 | // Open the Parent Handle for the child\r | |
1248 | //\r | |
1249 | Status = gBS->OpenProtocol (\r | |
1250 | ParentHandle,\r | |
1251 | &gEfiDiskIoProtocolGuid,\r | |
1252 | (VOID **) &ParentDiskIo,\r | |
1253 | This->DriverBindingHandle,\r | |
1254 | Private->Handle,\r | |
1255 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r | |
1256 | );\r | |
1257 | } else {\r | |
1258 | FreePool (Private->DevicePath);\r | |
1259 | FreePool (Private);\r | |
1260 | }\r | |
1261 | \r | |
1262 | return Status;\r | |
1263 | }\r | |
1264 | \r | |
1265 | \r | |
1266 | /**\r | |
1267 | The user Entry Point for module Partition. The user code starts with this function.\r | |
1268 | \r | |
1269 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
1270 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
1271 | \r | |
1272 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
1273 | @retval other Some error occurs when executing this entry point.\r | |
1274 | \r | |
1275 | **/\r | |
1276 | EFI_STATUS\r | |
1277 | EFIAPI\r | |
1278 | InitializePartition (\r | |
1279 | IN EFI_HANDLE ImageHandle,\r | |
1280 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1281 | )\r | |
1282 | {\r | |
1283 | EFI_STATUS Status;\r | |
1284 | \r | |
1285 | //\r | |
1286 | // Install driver model protocol(s).\r | |
1287 | //\r | |
d38a0f44 | 1288 | Status = EfiLibInstallDriverBindingComponentName2 (\r |
adbcbf8f | 1289 | ImageHandle,\r |
1290 | SystemTable,\r | |
1291 | &gPartitionDriverBinding,\r | |
1292 | ImageHandle,\r | |
1293 | &gPartitionComponentName,\r | |
d38a0f44 | 1294 | &gPartitionComponentName2\r |
adbcbf8f | 1295 | );\r |
1296 | ASSERT_EFI_ERROR (Status);\r | |
1297 | \r | |
1298 | \r | |
1299 | return Status;\r | |
1300 | }\r | |
1301 | \r | |
e3325721 HW |
1302 | \r |
1303 | /**\r | |
1304 | Test to see if there is any child on ControllerHandle.\r | |
1305 | \r | |
1306 | @param[in] ControllerHandle Handle of device to test.\r | |
1307 | \r | |
1308 | @retval TRUE There are children on the ControllerHandle.\r | |
1309 | @retval FALSE No child is on the ControllerHandle.\r | |
1310 | \r | |
1311 | **/\r | |
1312 | BOOLEAN\r | |
1313 | HasChildren (\r | |
1314 | IN EFI_HANDLE ControllerHandle\r | |
1315 | )\r | |
1316 | {\r | |
1317 | EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;\r | |
1318 | UINTN EntryCount;\r | |
1319 | EFI_STATUS Status;\r | |
1320 | UINTN Index;\r | |
1321 | \r | |
1322 | Status = gBS->OpenProtocolInformation (\r | |
1323 | ControllerHandle,\r | |
1324 | &gEfiDiskIoProtocolGuid,\r | |
1325 | &OpenInfoBuffer,\r | |
1326 | &EntryCount\r | |
1327 | );\r | |
1328 | ASSERT_EFI_ERROR (Status);\r | |
1329 | \r | |
1330 | for (Index = 0; Index < EntryCount; Index++) {\r | |
1331 | if ((OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) != 0) {\r | |
1332 | break;\r | |
1333 | }\r | |
1334 | }\r | |
1335 | FreePool (OpenInfoBuffer);\r | |
1336 | \r | |
1337 | return (BOOLEAN) (Index < EntryCount);\r | |
1338 | }\r | |
1339 | \r |