]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Disk/PartitionDxe/Partition.c
SecurityPkg: Enable Opal password solution build.
[mirror_edk2.git] / MdeModulePkg / Universal / Disk / PartitionDxe / Partition.c
CommitLineData
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 7Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 8This program and the accompanying materials\r
f42be642 9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
adbcbf8f 12\r
f42be642 13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT 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
24EFI_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 44PARTITION_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
66EFI_STATUS\r
67EFIAPI\r
68PartitionDriverBindingSupported (\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
187EFI_STATUS\r
188EFIAPI\r
189PartitionDriverBindingStart (\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 369Exit:\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
388EFI_STATUS\r
389EFIAPI\r
390PartitionDriverBindingStop (\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
410 //\r
411 // Close the bus driver\r
412 //\r
413 gBS->CloseProtocol (\r
414 ControllerHandle,\r
415 &gEfiDiskIoProtocolGuid,\r
416 This->DriverBindingHandle,\r
417 ControllerHandle\r
418 );\r
490b5ea1 419 //\r
420 // Close Parent BlockIO2 if has.\r
421 // \r
422 gBS->CloseProtocol (\r
423 ControllerHandle,\r
493d8e3a 424 &gEfiDiskIo2ProtocolGuid,\r
490b5ea1 425 This->DriverBindingHandle,\r
426 ControllerHandle\r
427 );\r
adbcbf8f 428\r
429 gBS->CloseProtocol (\r
430 ControllerHandle,\r
431 &gEfiDevicePathProtocolGuid,\r
432 This->DriverBindingHandle,\r
433 ControllerHandle\r
434 );\r
adbcbf8f 435 return EFI_SUCCESS;\r
436 }\r
437\r
438 AllChildrenStopped = TRUE;\r
439 for (Index = 0; Index < NumberOfChildren; Index++) {\r
490b5ea1 440 gBS->OpenProtocol (\r
441 ChildHandleBuffer[Index],\r
442 &gEfiBlockIoProtocolGuid,\r
443 (VOID **) &BlockIo,\r
444 This->DriverBindingHandle,\r
445 ControllerHandle,\r
446 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
447 );\r
448 //\r
449 // Try to locate BlockIo2.\r
450 //\r
451 gBS->OpenProtocol (\r
452 ChildHandleBuffer[Index],\r
453 &gEfiBlockIo2ProtocolGuid,\r
454 (VOID **) &BlockIo2,\r
455 This->DriverBindingHandle,\r
456 ControllerHandle,\r
457 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
458 ); \r
459\r
d0844d13 460\r
461 Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (BlockIo);\r
adbcbf8f 462\r
490b5ea1 463 Status = gBS->CloseProtocol (\r
464 ControllerHandle,\r
465 &gEfiDiskIoProtocolGuid,\r
466 This->DriverBindingHandle,\r
467 ChildHandleBuffer[Index]\r
468 );\r
469 //\r
470 // All Software protocols have be freed from the handle so remove it.\r
471 // Remove the BlockIo Protocol if has.\r
472 // Remove the BlockIo2 Protocol if has.\r
473 //\r
474 if (BlockIo2 != NULL) {\r
adbcbf8f 475 BlockIo->FlushBlocks (BlockIo);\r
490b5ea1 476 BlockIo2->FlushBlocksEx (BlockIo2, NULL);\r
adbcbf8f 477 Status = gBS->UninstallMultipleProtocolInterfaces (\r
490b5ea1 478 ChildHandleBuffer[Index],\r
479 &gEfiDevicePathProtocolGuid,\r
480 Private->DevicePath,\r
481 &gEfiBlockIoProtocolGuid,\r
482 &Private->BlockIo,\r
483 &gEfiBlockIo2ProtocolGuid,\r
484 &Private->BlockIo2,\r
485 Private->EspGuid,\r
486 NULL,\r
487 NULL\r
488 );\r
489 } else {\r
490 BlockIo->FlushBlocks (BlockIo);\r
491 Status = gBS->UninstallMultipleProtocolInterfaces (\r
492 ChildHandleBuffer[Index],\r
493 &gEfiDevicePathProtocolGuid,\r
494 Private->DevicePath,\r
495 &gEfiBlockIoProtocolGuid,\r
496 &Private->BlockIo,\r
497 Private->EspGuid,\r
498 NULL,\r
499 NULL\r
500 );\r
501 }\r
adbcbf8f 502\r
490b5ea1 503 if (EFI_ERROR (Status)) {\r
504 gBS->OpenProtocol (\r
505 ControllerHandle,\r
506 &gEfiDiskIoProtocolGuid,\r
507 (VOID **) &DiskIo,\r
508 This->DriverBindingHandle,\r
509 ChildHandleBuffer[Index],\r
510 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
511 );\r
512 } else {\r
513 FreePool (Private->DevicePath);\r
514 FreePool (Private);\r
adbcbf8f 515 }\r
516\r
517 if (EFI_ERROR (Status)) {\r
518 AllChildrenStopped = FALSE;\r
519 }\r
520 }\r
521\r
522 if (!AllChildrenStopped) {\r
523 return EFI_DEVICE_ERROR;\r
524 }\r
525\r
526 return EFI_SUCCESS;\r
527}\r
528\r
529\r
530/**\r
531 Reset the Block Device.\r
532\r
533 @param This Protocol instance pointer.\r
534 @param ExtendedVerification Driver may perform diagnostics on reset.\r
535\r
536 @retval EFI_SUCCESS The device was reset.\r
537 @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r
538 not be reset.\r
539\r
540**/\r
adbcbf8f 541EFI_STATUS\r
542EFIAPI\r
543PartitionReset (\r
544 IN EFI_BLOCK_IO_PROTOCOL *This,\r
545 IN BOOLEAN ExtendedVerification\r
546 )\r
547{\r
548 PARTITION_PRIVATE_DATA *Private;\r
549\r
550 Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r
551\r
552 return Private->ParentBlockIo->Reset (\r
553 Private->ParentBlockIo,\r
554 ExtendedVerification\r
555 );\r
556}\r
557\r
fcf5e49d
RN
558/**\r
559 Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED\r
560 for no media or media change case. Otherwise DefaultStatus is returned.\r
561\r
562 @param DiskIo Pointer to the DiskIo instance.\r
563 @param MediaId Id of the media, changes every time the media is replaced.\r
564 @param DefaultStatus The default status to return when it's not the no media\r
565 or media change case.\r
566\r
567 @retval EFI_NO_MEDIA There is no media.\r
568 @retval EFI_MEDIA_CHANGED The media was changed.\r
569 @retval others The default status to return.\r
570**/\r
571EFI_STATUS\r
572ProbeMediaStatus (\r
573 IN EFI_DISK_IO_PROTOCOL *DiskIo,\r
574 IN UINT32 MediaId,\r
575 IN EFI_STATUS DefaultStatus\r
576 )\r
577{\r
578 EFI_STATUS Status;\r
df473cc1 579 UINT8 Buffer[1];\r
fcf5e49d
RN
580\r
581 //\r
df473cc1
RC
582 // Read 1 byte from offset 0 to check if the MediaId is still valid.\r
583 // The reading operation is synchronious thus it is not worth it to\r
584 // allocate a buffer from the pool. The destination buffer for the\r
585 // data is in the stack.\r
fcf5e49d 586 //\r
df473cc1 587 Status = DiskIo->ReadDisk (DiskIo, MediaId, 0, 1, (VOID*)Buffer);\r
fcf5e49d
RN
588 if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r
589 return Status;\r
590 }\r
591 return DefaultStatus;\r
592}\r
adbcbf8f 593\r
594/**\r
595 Read by using the Disk IO protocol on the parent device. Lba addresses\r
596 must be converted to byte offsets.\r
597\r
598 @param This Protocol instance pointer.\r
599 @param MediaId Id of the media, changes every time the media is replaced.\r
600 @param Lba The starting Logical Block Address to read from\r
601 @param BufferSize Size of Buffer, must be a multiple of device block size.\r
602 @param Buffer Buffer containing read data\r
603\r
604 @retval EFI_SUCCESS The data was read correctly from the device.\r
605 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.\r
606 @retval EFI_NO_MEDIA There is no media in the device.\r
607 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.\r
608 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
609 @retval EFI_INVALID_PARAMETER The read request contains device addresses that are not\r
610 valid for the device.\r
611\r
612**/\r
adbcbf8f 613EFI_STATUS\r
614EFIAPI\r
615PartitionReadBlocks (\r
616 IN EFI_BLOCK_IO_PROTOCOL *This,\r
617 IN UINT32 MediaId,\r
618 IN EFI_LBA Lba,\r
619 IN UINTN BufferSize,\r
620 OUT VOID *Buffer\r
621 )\r
622{\r
623 PARTITION_PRIVATE_DATA *Private;\r
624 UINT64 Offset;\r
625\r
626 Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r
627\r
628 if (BufferSize % Private->BlockSize != 0) {\r
fcf5e49d 629 return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_BAD_BUFFER_SIZE);\r
adbcbf8f 630 }\r
631\r
632 Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
633 if (Offset + BufferSize > Private->End) {\r
fcf5e49d 634 return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_INVALID_PARAMETER);\r
adbcbf8f 635 }\r
636 //\r
637 // Because some kinds of partition have different block size from their parent\r
638 // device, we call the Disk IO protocol on the parent device, not the Block IO\r
639 // protocol\r
640 //\r
641 return Private->DiskIo->ReadDisk (Private->DiskIo, MediaId, Offset, BufferSize, Buffer);\r
642}\r
643\r
644/**\r
645 Write by using the Disk IO protocol on the parent device. Lba addresses\r
646 must be converted to byte offsets.\r
647\r
490b5ea1 648 @param[in] This Protocol instance pointer.\r
649 @param[in] MediaId Id of the media, changes every time the media is replaced.\r
650 @param[in] Lba The starting Logical Block Address to read from\r
651 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
652 @param[in] Buffer Buffer containing data to be written to device.\r
adbcbf8f 653\r
654 @retval EFI_SUCCESS The data was written correctly to the device.\r
655 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
656 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
657 @retval EFI_NO_MEDIA There is no media in the device.\r
658 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
659 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
660 @retval EFI_INVALID_PARAMETER The write request contains a LBA that is not\r
661 valid for the device.\r
662\r
663**/\r
adbcbf8f 664EFI_STATUS\r
665EFIAPI\r
666PartitionWriteBlocks (\r
667 IN EFI_BLOCK_IO_PROTOCOL *This,\r
668 IN UINT32 MediaId,\r
669 IN EFI_LBA Lba,\r
670 IN UINTN BufferSize,\r
490b5ea1 671 IN VOID *Buffer\r
adbcbf8f 672 )\r
673{\r
674 PARTITION_PRIVATE_DATA *Private;\r
675 UINT64 Offset;\r
676\r
677 Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r
678\r
679 if (BufferSize % Private->BlockSize != 0) {\r
fcf5e49d 680 return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_BAD_BUFFER_SIZE);\r
adbcbf8f 681 }\r
682\r
683 Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
684 if (Offset + BufferSize > Private->End) {\r
fcf5e49d 685 return ProbeMediaStatus (Private->DiskIo, MediaId, EFI_INVALID_PARAMETER);\r
adbcbf8f 686 }\r
687 //\r
688 // Because some kinds of partition have different block size from their parent\r
689 // device, we call the Disk IO protocol on the parent device, not the Block IO\r
690 // protocol\r
691 //\r
692 return Private->DiskIo->WriteDisk (Private->DiskIo, MediaId, Offset, BufferSize, Buffer);\r
693}\r
694\r
695\r
696/**\r
697 Flush the parent Block Device.\r
698\r
699 @param This Protocol instance pointer.\r
700\r
701 @retval EFI_SUCCESS All outstanding data was written to the device\r
702 @retval EFI_DEVICE_ERROR The device reported an error while writting back the data\r
703 @retval EFI_NO_MEDIA There is no media in the device.\r
704\r
705**/\r
adbcbf8f 706EFI_STATUS\r
707EFIAPI\r
708PartitionFlushBlocks (\r
709 IN EFI_BLOCK_IO_PROTOCOL *This\r
710 )\r
711{\r
712 PARTITION_PRIVATE_DATA *Private;\r
713\r
714 Private = PARTITION_DEVICE_FROM_BLOCK_IO_THIS (This);\r
715\r
716 return Private->ParentBlockIo->FlushBlocks (Private->ParentBlockIo);\r
717}\r
718\r
65fd3952
RN
719/**\r
720 Probe the media status and return EFI_NO_MEDIA or EFI_MEDIA_CHANGED\r
721 for no media or media change case. Otherwise DefaultStatus is returned.\r
722\r
493d8e3a 723 @param DiskIo2 Pointer to the DiskIo2 instance.\r
65fd3952
RN
724 @param MediaId Id of the media, changes every time the media is replaced.\r
725 @param DefaultStatus The default status to return when it's not the no media\r
726 or media change case.\r
727\r
728 @retval EFI_NO_MEDIA There is no media.\r
729 @retval EFI_MEDIA_CHANGED The media was changed.\r
730 @retval others The default status to return.\r
731**/\r
732EFI_STATUS\r
733ProbeMediaStatusEx (\r
493d8e3a 734 IN EFI_DISK_IO2_PROTOCOL *DiskIo2,\r
65fd3952
RN
735 IN UINT32 MediaId,\r
736 IN EFI_STATUS DefaultStatus\r
737 )\r
738{\r
739 EFI_STATUS Status;\r
740\r
741 //\r
493d8e3a 742 // Read 1 byte from offset 0 but passing NULL as buffer pointer\r
65fd3952 743 //\r
493d8e3a 744 Status = DiskIo2->ReadDiskEx (DiskIo2, MediaId, 0, NULL, 1, NULL);\r
65fd3952
RN
745 if ((Status == EFI_NO_MEDIA) || (Status == EFI_MEDIA_CHANGED)) {\r
746 return Status;\r
747 }\r
748 return DefaultStatus;\r
749}\r
750\r
490b5ea1 751/**\r
752 Reset the Block Device throught Block I/O2 protocol.\r
753\r
754 @param This Protocol instance pointer.\r
755 @param ExtendedVerification Driver may perform diagnostics on reset.\r
756\r
757 @retval EFI_SUCCESS The device was reset.\r
758 @retval EFI_DEVICE_ERROR The device is not functioning properly and could\r
759 not be reset.\r
760\r
761**/\r
762EFI_STATUS\r
763EFIAPI\r
764PartitionResetEx (\r
765 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
766 IN BOOLEAN ExtendedVerification\r
767 )\r
768{\r
769 PARTITION_PRIVATE_DATA *Private;\r
770\r
771 Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
772\r
773 return Private->ParentBlockIo2->Reset (\r
774 Private->ParentBlockIo2,\r
775 ExtendedVerification\r
776 );\r
777}\r
778\r
493d8e3a
RN
779/**\r
780 The general callback for the DiskIo2 interfaces.\r
781 @param Event Event whose notification function is being invoked.\r
782 @param Context The pointer to the notification function's context,\r
783 which points to the PARTITION_ACCESS_TASK instance.\r
784**/\r
785VOID\r
786EFIAPI\r
787PartitionOnAccessComplete (\r
788 IN EFI_EVENT Event,\r
789 IN VOID *Context\r
790 )\r
791{\r
792 PARTITION_ACCESS_TASK *Task;\r
793\r
794 Task = (PARTITION_ACCESS_TASK *) Context;\r
795\r
796 gBS->CloseEvent (Event);\r
797\r
798 Task->BlockIo2Token->TransactionStatus = Task->DiskIo2Token.TransactionStatus;\r
799 gBS->SignalEvent (Task->BlockIo2Token->Event);\r
800\r
801 FreePool (Task);\r
802}\r
803\r
804/**\r
805 Create a new PARTITION_ACCESS_TASK instance.\r
806\r
807 @param Token Pointer to the EFI_BLOCK_IO2_TOKEN.\r
808\r
809 @return Pointer to the created PARTITION_ACCESS_TASK instance or NULL upon failure.\r
810**/\r
811PARTITION_ACCESS_TASK *\r
812PartitionCreateAccessTask (\r
813 IN EFI_BLOCK_IO2_TOKEN *Token\r
814 )\r
815{\r
816 EFI_STATUS Status;\r
817 PARTITION_ACCESS_TASK *Task;\r
818\r
819 Task = AllocatePool (sizeof (*Task));\r
820 if (Task == NULL) {\r
821 return NULL;\r
822 }\r
823\r
824 Status = gBS->CreateEvent (\r
825 EVT_NOTIFY_SIGNAL,\r
80c83a69 826 TPL_NOTIFY,\r
493d8e3a
RN
827 PartitionOnAccessComplete,\r
828 Task,\r
829 &Task->DiskIo2Token.Event\r
830 );\r
831 if (EFI_ERROR (Status)) {\r
832 FreePool (Task);\r
833 return NULL;\r
834 }\r
835\r
836 Task->BlockIo2Token = Token;\r
837\r
838 return Task;\r
839}\r
840\r
490b5ea1 841/**\r
842 Read BufferSize bytes from Lba into Buffer.\r
843 \r
844 This function reads the requested number of blocks from the device. All the\r
845 blocks are read, or an error is returned.\r
846 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_or EFI_MEDIA_CHANGED is returned and\r
847 non-blocking I/O is being used, the Event associated with this request will\r
848 not be signaled.\r
849\r
850 @param[in] This Indicates a pointer to the calling context.\r
851 @param[in] MediaId Id of the media, changes every time the media is \r
852 replaced.\r
853 @param[in] Lba The starting Logical Block Address to read from.\r
854 @param[in, out] Token A pointer to the token associated with the transaction.\r
855 @param[in] BufferSize Size of Buffer, must be a multiple of device block size. \r
856 @param[out] Buffer A pointer to the destination buffer for the data. The \r
857 caller is responsible for either having implicit or \r
858 explicit ownership of the buffer.\r
859\r
860 @retval EFI_SUCCESS The read request was queued if Token->Event is\r
861 not NULL.The data was read correctly from the\r
862 device if the Token->Event is NULL.\r
863 @retval EFI_DEVICE_ERROR The device reported an error while performing\r
864 the read.\r
865 @retval EFI_NO_MEDIA There is no media in the device.\r
866 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
867 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the\r
868 intrinsic block size of the device.\r
869 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid, \r
870 or the buffer is not on proper alignment.\r
871 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
872 of resources.\r
873**/\r
874EFI_STATUS\r
875EFIAPI\r
876PartitionReadBlocksEx (\r
877 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
878 IN UINT32 MediaId,\r
879 IN EFI_LBA Lba,\r
880 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
881 IN UINTN BufferSize,\r
882 OUT VOID *Buffer\r
883 )\r
884{\r
493d8e3a 885 EFI_STATUS Status;\r
490b5ea1 886 PARTITION_PRIVATE_DATA *Private;\r
887 UINT64 Offset;\r
493d8e3a 888 PARTITION_ACCESS_TASK *Task;\r
490b5ea1 889\r
65fd3952
RN
890 Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
891\r
490b5ea1 892 if (BufferSize % Private->BlockSize != 0) {\r
493d8e3a 893 return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_BAD_BUFFER_SIZE);\r
490b5ea1 894 }\r
895\r
896 Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
897 if (Offset + BufferSize > Private->End) {\r
493d8e3a 898 return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r
490b5ea1 899 }\r
900\r
493d8e3a
RN
901 if ((Token != NULL) && (Token->Event != NULL)) {\r
902 Task = PartitionCreateAccessTask (Token);\r
903 if (Task == NULL) {\r
904 return EFI_OUT_OF_RESOURCES;\r
905 }\r
490b5ea1 906\r
493d8e3a
RN
907 Status = Private->DiskIo2->ReadDiskEx (Private->DiskIo2, MediaId, Offset, &Task->DiskIo2Token, BufferSize, Buffer);\r
908 if (EFI_ERROR (Status)) {\r
909 gBS->CloseEvent (Task->DiskIo2Token.Event);\r
910 FreePool (Task);\r
911 }\r
912 } else {\r
913 Status = Private->DiskIo2->ReadDiskEx (Private->DiskIo2, MediaId, Offset, NULL, BufferSize, Buffer);\r
490b5ea1 914 }\r
915\r
493d8e3a 916 return Status;\r
490b5ea1 917}\r
918\r
919/**\r
920 Write BufferSize bytes from Lba into Buffer.\r
921\r
922 This function writes the requested number of blocks to the device. All blocks\r
923 are written, or an error is returned.If EFI_DEVICE_ERROR, EFI_NO_MEDIA,\r
924 EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED is returned and non-blocking I/O is\r
925 being used, the Event associated with this request will not be signaled.\r
926\r
927 @param[in] This Indicates a pointer to the calling context.\r
928 @param[in] MediaId The media ID that the write request is for.\r
929 @param[in] Lba The starting logical block address to be written. The\r
930 caller is responsible for writing to only legitimate\r
931 locations.\r
932 @param[in, out] Token A pointer to the token associated with the transaction.\r
933 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.\r
934 @param[in] Buffer A pointer to the source buffer for the data.\r
935\r
936 @retval EFI_SUCCESS The write request was queued if Event is not NULL.\r
937 The data was written correctly to the device if\r
938 the Event is NULL.\r
939 @retval EFI_WRITE_PROTECTED The device can not be written to.\r
940 @retval EFI_NO_MEDIA There is no media in the device.\r
941 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.\r
942 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.\r
943 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.\r
944 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid, \r
945 or the buffer is not on proper alignment.\r
946 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
947 of resources.\r
948\r
949**/\r
950EFI_STATUS\r
951EFIAPI\r
952PartitionWriteBlocksEx (\r
953 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
954 IN UINT32 MediaId,\r
955 IN EFI_LBA Lba,\r
956 IN OUT EFI_BLOCK_IO2_TOKEN *Token,\r
957 IN UINTN BufferSize,\r
958 IN VOID *Buffer\r
959 )\r
960{\r
493d8e3a 961 EFI_STATUS Status;\r
490b5ea1 962 PARTITION_PRIVATE_DATA *Private;\r
963 UINT64 Offset;\r
493d8e3a 964 PARTITION_ACCESS_TASK *Task;\r
490b5ea1 965\r
65fd3952
RN
966 Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
967\r
490b5ea1 968 if (BufferSize % Private->BlockSize != 0) {\r
493d8e3a 969 return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_BAD_BUFFER_SIZE);\r
490b5ea1 970 }\r
971\r
972 Offset = MultU64x32 (Lba, Private->BlockSize) + Private->Start;\r
973 if (Offset + BufferSize > Private->End) {\r
493d8e3a 974 return ProbeMediaStatusEx (Private->DiskIo2, MediaId, EFI_INVALID_PARAMETER);\r
490b5ea1 975 }\r
493d8e3a
RN
976 \r
977 if ((Token != NULL) && (Token->Event != NULL)) {\r
978 Task = PartitionCreateAccessTask (Token);\r
979 if (Task == NULL) {\r
980 return EFI_OUT_OF_RESOURCES;\r
981 }\r
490b5ea1 982\r
493d8e3a
RN
983 Status = Private->DiskIo2->WriteDiskEx (Private->DiskIo2, MediaId, Offset, &Task->DiskIo2Token, BufferSize, Buffer);\r
984 if (EFI_ERROR (Status)) {\r
985 gBS->CloseEvent (Task->DiskIo2Token.Event);\r
986 FreePool (Task);\r
987 }\r
988 } else {\r
989 Status = Private->DiskIo2->WriteDiskEx (Private->DiskIo2, MediaId, Offset, NULL, BufferSize, Buffer);\r
490b5ea1 990 }\r
493d8e3a 991 return Status;\r
490b5ea1 992}\r
993\r
994/**\r
995 Flush the Block Device.\r
996 \r
997 If EFI_DEVICE_ERROR, EFI_NO_MEDIA,_EFI_WRITE_PROTECTED or EFI_MEDIA_CHANGED\r
998 is returned and non-blocking I/O is being used, the Event associated with\r
999 this request will not be signaled. \r
1000\r
1001 @param[in] This Indicates a pointer to the calling context.\r
86d8e199 1002 @param[in, out] Token A pointer to the token associated with the transaction\r
490b5ea1 1003\r
1004 @retval EFI_SUCCESS The flush request was queued if Event is not NULL.\r
1005 All outstanding data was written correctly to the\r
1006 device if the Event is NULL.\r
1007 @retval EFI_DEVICE_ERROR The device reported an error while writting back\r
1008 the data.\r
1009 @retval EFI_WRITE_PROTECTED The device cannot be written to.\r
1010 @retval EFI_NO_MEDIA There is no media in the device.\r
1011 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1012 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
1013 of resources.\r
1014\r
1015**/\r
1016EFI_STATUS\r
1017EFIAPI\r
1018PartitionFlushBlocksEx (\r
1019 IN EFI_BLOCK_IO2_PROTOCOL *This,\r
1020 IN OUT EFI_BLOCK_IO2_TOKEN *Token\r
1021 )\r
1022{\r
493d8e3a 1023 EFI_STATUS Status;\r
490b5ea1 1024 PARTITION_PRIVATE_DATA *Private;\r
493d8e3a 1025 PARTITION_ACCESS_TASK *Task;\r
490b5ea1 1026\r
1027 Private = PARTITION_DEVICE_FROM_BLOCK_IO2_THIS (This);\r
1028\r
493d8e3a
RN
1029 if ((Token != NULL) && (Token->Event != NULL)) {\r
1030 Task = PartitionCreateAccessTask (Token);\r
1031 if (Task == NULL) {\r
1032 return EFI_OUT_OF_RESOURCES;\r
1033 }\r
490b5ea1 1034\r
493d8e3a
RN
1035 Status = Private->DiskIo2->FlushDiskEx (Private->DiskIo2, &Task->DiskIo2Token);\r
1036 if (EFI_ERROR (Status)) {\r
1037 gBS->CloseEvent (Task->DiskIo2Token.Event);\r
1038 FreePool (Task);\r
1039 }\r
1040 } else {\r
1041 Status = Private->DiskIo2->FlushDiskEx (Private->DiskIo2, NULL);\r
1042 }\r
1043 return Status;\r
490b5ea1 1044}\r
adbcbf8f 1045\r
1046\r
1047/**\r
1048 Create a child handle for a logical block device that represents the\r
1049 bytes Start to End of the Parent Block IO device.\r
1050\r
490b5ea1 1051 @param[in] This Protocol instance pointer.\r
1052 @param[in] ParentHandle Parent Handle for new child.\r
1053 @param[in] ParentDiskIo Parent DiskIo interface.\r
493d8e3a 1054 @param[in] ParentDiskIo2 Parent DiskIo2 interface.\r
490b5ea1 1055 @param[in] ParentBlockIo Parent BlockIo interface.\r
1056 @param[in] ParentBlockIo2 Parent BlockIo2 interface.\r
1057 @param[in] ParentDevicePath Parent Device Path.\r
1058 @param[in] DevicePathNode Child Device Path node.\r
1059 @param[in] Start Start Block.\r
1060 @param[in] End End Block.\r
1061 @param[in] BlockSize Child block size.\r
1062 @param[in] InstallEspGuid Flag to install EFI System Partition GUID on handle.\r
1063\r
1064 @retval EFI_SUCCESS A child handle was added.\r
1065 @retval other A child handle was not added.\r
adbcbf8f 1066\r
1067**/\r
1068EFI_STATUS\r
1069PartitionInstallChildHandle (\r
1070 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
1071 IN EFI_HANDLE ParentHandle,\r
1072 IN EFI_DISK_IO_PROTOCOL *ParentDiskIo,\r
493d8e3a 1073 IN EFI_DISK_IO2_PROTOCOL *ParentDiskIo2,\r
adbcbf8f 1074 IN EFI_BLOCK_IO_PROTOCOL *ParentBlockIo,\r
490b5ea1 1075 IN EFI_BLOCK_IO2_PROTOCOL *ParentBlockIo2,\r
adbcbf8f 1076 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r
1077 IN EFI_DEVICE_PATH_PROTOCOL *DevicePathNode,\r
1078 IN EFI_LBA Start,\r
1079 IN EFI_LBA End,\r
1080 IN UINT32 BlockSize,\r
1081 IN BOOLEAN InstallEspGuid\r
1082 )\r
1083{\r
1084 EFI_STATUS Status;\r
1085 PARTITION_PRIVATE_DATA *Private;\r
1086\r
490b5ea1 1087 Status = EFI_SUCCESS;\r
adbcbf8f 1088 Private = AllocateZeroPool (sizeof (PARTITION_PRIVATE_DATA));\r
1089 if (Private == NULL) {\r
1090 return EFI_OUT_OF_RESOURCES;\r
1091 }\r
1092\r
1093 Private->Signature = PARTITION_PRIVATE_DATA_SIGNATURE;\r
1094\r
1095 Private->Start = MultU64x32 (Start, ParentBlockIo->Media->BlockSize);\r
1096 Private->End = MultU64x32 (End + 1, ParentBlockIo->Media->BlockSize);\r
1097\r
1098 Private->BlockSize = BlockSize;\r
1099 Private->ParentBlockIo = ParentBlockIo;\r
490b5ea1 1100 Private->ParentBlockIo2 = ParentBlockIo2;\r
adbcbf8f 1101 Private->DiskIo = ParentDiskIo;\r
493d8e3a 1102 Private->DiskIo2 = ParentDiskIo2;\r
adbcbf8f 1103\r
d0844d13 1104 //\r
1105 // Set the BlockIO into Private Data.\r
1106 //\r
1107 Private->BlockIo.Revision = ParentBlockIo->Revision;\r
1108 \r
1109 Private->BlockIo.Media = &Private->Media;\r
1110 CopyMem (Private->BlockIo.Media, ParentBlockIo->Media, sizeof (EFI_BLOCK_IO_MEDIA));\r
490b5ea1 1111\r
d0844d13 1112 Private->BlockIo.Reset = PartitionReset;\r
1113 Private->BlockIo.ReadBlocks = PartitionReadBlocks;\r
1114 Private->BlockIo.WriteBlocks = PartitionWriteBlocks;\r
1115 Private->BlockIo.FlushBlocks = PartitionFlushBlocks;\r
490b5ea1 1116\r
d0844d13 1117 //\r
1118 // Set the BlockIO2 into Private Data.\r
1119 //\r
493d8e3a
RN
1120 if (Private->DiskIo2 != NULL) {\r
1121 ASSERT (Private->ParentBlockIo2 != NULL);\r
490b5ea1 1122 Private->BlockIo2.Media = &Private->Media2;\r
1123 CopyMem (Private->BlockIo2.Media, ParentBlockIo2->Media, sizeof (EFI_BLOCK_IO_MEDIA));\r
1124\r
1125 Private->BlockIo2.Reset = PartitionResetEx;\r
1126 Private->BlockIo2.ReadBlocksEx = PartitionReadBlocksEx;\r
1127 Private->BlockIo2.WriteBlocksEx = PartitionWriteBlocksEx;\r
1128 Private->BlockIo2.FlushBlocksEx = PartitionFlushBlocksEx; \r
1129 }\r
67f802ed 1130\r
67f802ed 1131 Private->Media.IoAlign = 0;\r
490b5ea1 1132 Private->Media.LogicalPartition = TRUE;\r
adbcbf8f 1133 Private->Media.LastBlock = DivU64x32 (\r
1134 MultU64x32 (\r
1135 End - Start + 1,\r
d0844d13 1136 ParentBlockIo->Media->BlockSize\r
adbcbf8f 1137 ),\r
490b5ea1 1138 BlockSize\r
adbcbf8f 1139 ) - 1;\r
1140\r
67f802ed 1141 Private->Media.BlockSize = (UINT32) BlockSize;\r
adbcbf8f 1142\r
493d8e3a 1143 Private->Media2.IoAlign = 0;\r
490b5ea1 1144 Private->Media2.LogicalPartition = TRUE;\r
1145 Private->Media2.LastBlock = Private->Media.LastBlock;\r
1146 Private->Media2.BlockSize = (UINT32) BlockSize;\r
1147\r
fe3b68bb 1148 //\r
0e87144e 1149 // Per UEFI Spec, LowestAlignedLba, LogicalBlocksPerPhysicalBlock and OptimalTransferLengthGranularity must be 0\r
fe3b68bb
RN
1150 // for logical partitions.\r
1151 //\r
1152 if (Private->BlockIo.Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION2) {\r
c582ad45
EB
1153 Private->Media.LowestAlignedLba = 0;\r
1154 Private->Media.LogicalBlocksPerPhysicalBlock = 0;\r
1155 Private->Media2.LowestAlignedLba = 0;\r
1156 Private->Media2.LogicalBlocksPerPhysicalBlock = 0;\r
0e87144e 1157 if (Private->BlockIo.Revision >= EFI_BLOCK_IO_PROTOCOL_REVISION3) {\r
c582ad45
EB
1158 Private->Media.OptimalTransferLengthGranularity = 0;\r
1159 Private->Media2.OptimalTransferLengthGranularity = 0;\r
0e87144e 1160 }\r
fe3b68bb
RN
1161 }\r
1162\r
490b5ea1 1163 Private->DevicePath = AppendDevicePathNode (ParentDevicePath, DevicePathNode);\r
adbcbf8f 1164\r
1165 if (Private->DevicePath == NULL) {\r
1166 FreePool (Private);\r
1167 return EFI_OUT_OF_RESOURCES;\r
1168 }\r
1169\r
1170 if (InstallEspGuid) {\r
1171 Private->EspGuid = &gEfiPartTypeSystemPartGuid;\r
1172 } else {\r
1173 //\r
1174 // If NULL InstallMultipleProtocolInterfaces will ignore it.\r
1175 //\r
1176 Private->EspGuid = NULL;\r
1177 }\r
490b5ea1 1178\r
adbcbf8f 1179 //\r
490b5ea1 1180 // Create the new handle. \r
adbcbf8f 1181 //\r
1182 Private->Handle = NULL;\r
493d8e3a 1183 if (Private->DiskIo2 != NULL) {\r
490b5ea1 1184 Status = gBS->InstallMultipleProtocolInterfaces (\r
1185 &Private->Handle,\r
1186 &gEfiDevicePathProtocolGuid,\r
1187 Private->DevicePath,\r
1188 &gEfiBlockIoProtocolGuid,\r
1189 &Private->BlockIo,\r
1190 &gEfiBlockIo2ProtocolGuid,\r
1191 &Private->BlockIo2,\r
1192 Private->EspGuid,\r
1193 NULL,\r
1194 NULL\r
1195 );\r
d0844d13 1196 } else { \r
1197 Status = gBS->InstallMultipleProtocolInterfaces (\r
1198 &Private->Handle,\r
1199 &gEfiDevicePathProtocolGuid,\r
1200 Private->DevicePath,\r
1201 &gEfiBlockIoProtocolGuid,\r
1202 &Private->BlockIo,\r
1203 Private->EspGuid,\r
1204 NULL,\r
1205 NULL\r
1206 );\r
490b5ea1 1207 }\r
adbcbf8f 1208\r
1209 if (!EFI_ERROR (Status)) {\r
1210 //\r
1211 // Open the Parent Handle for the child\r
1212 //\r
1213 Status = gBS->OpenProtocol (\r
1214 ParentHandle,\r
1215 &gEfiDiskIoProtocolGuid,\r
1216 (VOID **) &ParentDiskIo,\r
1217 This->DriverBindingHandle,\r
1218 Private->Handle,\r
1219 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
1220 );\r
1221 } else {\r
1222 FreePool (Private->DevicePath);\r
1223 FreePool (Private);\r
1224 }\r
1225\r
1226 return Status;\r
1227}\r
1228\r
1229\r
1230/**\r
1231 The user Entry Point for module Partition. The user code starts with this function.\r
1232\r
1233 @param[in] ImageHandle The firmware allocated handle for the EFI image. \r
1234 @param[in] SystemTable A pointer to the EFI System Table.\r
1235 \r
1236 @retval EFI_SUCCESS The entry point is executed successfully.\r
1237 @retval other Some error occurs when executing this entry point.\r
1238\r
1239**/\r
1240EFI_STATUS\r
1241EFIAPI\r
1242InitializePartition (\r
1243 IN EFI_HANDLE ImageHandle,\r
1244 IN EFI_SYSTEM_TABLE *SystemTable\r
1245 )\r
1246{\r
1247 EFI_STATUS Status;\r
1248\r
1249 //\r
1250 // Install driver model protocol(s).\r
1251 //\r
d38a0f44 1252 Status = EfiLibInstallDriverBindingComponentName2 (\r
adbcbf8f 1253 ImageHandle,\r
1254 SystemTable,\r
1255 &gPartitionDriverBinding,\r
1256 ImageHandle,\r
1257 &gPartitionComponentName,\r
d38a0f44 1258 &gPartitionComponentName2\r
adbcbf8f 1259 );\r
1260 ASSERT_EFI_ERROR (Status);\r
1261\r
1262\r
1263 return Status;\r
1264}\r
1265\r