2 The device path help function.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // Template for a SATA Device Path node
22 SATA_DEVICE_PATH mAhciSataDevicePathNodeTemplate
= {
24 MESSAGING_DEVICE_PATH
,
27 (UINT8
) (sizeof (SATA_DEVICE_PATH
)),
28 (UINT8
) ((sizeof (SATA_DEVICE_PATH
)) >> 8)
32 0xFFFF, // PortMultiplierPortNumber
37 // Template for an End of entire Device Path node
39 EFI_DEVICE_PATH_PROTOCOL mAhciEndDevicePathNodeTemplate
= {
41 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
43 (UINT8
) (sizeof (EFI_DEVICE_PATH_PROTOCOL
)),
44 (UINT8
) ((sizeof (EFI_DEVICE_PATH_PROTOCOL
)) >> 8)
49 Returns the 16-bit Length field of a device path node.
51 Returns the 16-bit Length field of the device path node specified by Node.
52 Node is not required to be aligned on a 16-bit boundary, so it is recommended
53 that a function such as ReadUnaligned16() be used to extract the contents of
56 If Node is NULL, then ASSERT().
58 @param Node A pointer to a device path node data structure.
60 @return The 16-bit Length field of the device path node specified by Node.
64 DevicePathNodeLength (
68 ASSERT (Node
!= NULL
);
69 return ReadUnaligned16 ((UINT16
*)&((EFI_DEVICE_PATH_PROTOCOL
*)(Node
))->Length
[0]);
73 Returns a pointer to the next node in a device path.
75 If Node is NULL, then ASSERT().
77 @param Node A pointer to a device path node data structure.
79 @return a pointer to the device path node that follows the device path node
83 EFI_DEVICE_PATH_PROTOCOL
*
88 ASSERT (Node
!= NULL
);
89 return (EFI_DEVICE_PATH_PROTOCOL
*)((UINT8
*)(Node
) + DevicePathNodeLength(Node
));
93 Get the size of the current device path instance.
95 @param[in] DevicePath A pointer to the EFI_DEVICE_PATH_PROTOCOL
97 @param[out] InstanceSize The size of the current device path instance.
98 @param[out] EntireDevicePathEnd Indicate whether the instance is the last
99 one in the device path strucure.
101 @retval EFI_SUCCESS The size of the current device path instance is fetched.
102 @retval Others Fails to get the size of the current device path instance.
106 GetDevicePathInstanceSize (
107 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
108 OUT UINTN
*InstanceSize
,
109 OUT BOOLEAN
*EntireDevicePathEnd
112 EFI_DEVICE_PATH_PROTOCOL
*Walker
;
114 if (DevicePath
== NULL
|| InstanceSize
== NULL
|| EntireDevicePathEnd
== NULL
) {
115 return EFI_INVALID_PARAMETER
;
119 // Find the end of the device path instance
122 while (Walker
->Type
!= END_DEVICE_PATH_TYPE
) {
123 Walker
= NextDevicePathNode (Walker
);
127 // Check if 'Walker' points to the end of an entire device path
129 if (Walker
->SubType
== END_ENTIRE_DEVICE_PATH_SUBTYPE
) {
130 *EntireDevicePathEnd
= TRUE
;
131 } else if (Walker
->SubType
== END_INSTANCE_DEVICE_PATH_SUBTYPE
) {
132 *EntireDevicePathEnd
= FALSE
;
134 return EFI_INVALID_PARAMETER
;
138 // Compute the size of the device path instance
140 *InstanceSize
= ((UINTN
) Walker
- (UINTN
) (DevicePath
)) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
146 Check the validity of the device path of a ATA AHCI host controller.
148 @param[in] DevicePath A pointer to the EFI_DEVICE_PATH_PROTOCOL
150 @param[in] DevicePathLength The length of the device path.
152 @retval EFI_SUCCESS The device path is valid.
153 @retval EFI_INVALID_PARAMETER The device path is invalid.
157 AhciIsHcDevicePathValid (
158 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
159 IN UINTN DevicePathLength
162 EFI_DEVICE_PATH_PROTOCOL
*Start
;
165 if (DevicePath
== NULL
) {
166 return EFI_INVALID_PARAMETER
;
170 // Validate the DevicePathLength is big enough to touch the first node.
172 if (DevicePathLength
< sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
173 return EFI_INVALID_PARAMETER
;
177 while (!(DevicePath
->Type
== END_DEVICE_PATH_TYPE
&&
178 DevicePath
->SubType
== END_ENTIRE_DEVICE_PATH_SUBTYPE
)) {
179 DevicePath
= NextDevicePathNode (DevicePath
);
182 // Prevent overflow and invalid zero in the 'Length' field of a device path
185 if ((UINTN
) DevicePath
<= (UINTN
) Start
) {
186 return EFI_INVALID_PARAMETER
;
190 // Prevent touching memory beyond given DevicePathLength.
192 if ((UINTN
) DevicePath
- (UINTN
) Start
>
193 DevicePathLength
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)) {
194 return EFI_INVALID_PARAMETER
;
199 // Check if the device path and its size match each other.
201 Size
= ((UINTN
) DevicePath
- (UINTN
) Start
) + sizeof (EFI_DEVICE_PATH_PROTOCOL
);
202 if (Size
!= DevicePathLength
) {
203 return EFI_INVALID_PARAMETER
;
210 Build the device path for an ATA device with given port and port multiplier number.
212 @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA
214 @param[in] Port The given port number.
215 @param[in] PortMultiplierPort The given port multiplier number.
216 @param[out] DevicePathLength The length of the device path in bytes specified
218 @param[out] DevicePath The device path of ATA device.
220 @retval EFI_SUCCESS The operation succeeds.
221 @retval EFI_INVALID_PARAMETER The parameters are invalid.
222 @retval EFI_OUT_OF_RESOURCES The operation fails due to lack of resources.
226 AhciBuildDevicePath (
227 IN PEI_AHCI_CONTROLLER_PRIVATE_DATA
*Private
,
229 IN UINT16 PortMultiplierPort
,
230 OUT UINTN
*DevicePathLength
,
231 OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
234 EFI_DEVICE_PATH_PROTOCOL
*DevicePathWalker
;
235 SATA_DEVICE_PATH
*SataDeviceNode
;
237 if (DevicePathLength
== NULL
|| DevicePath
== NULL
) {
238 return EFI_INVALID_PARAMETER
;
241 *DevicePathLength
= Private
->DevicePathLength
+ sizeof (SATA_DEVICE_PATH
);
242 *DevicePath
= AllocatePool (*DevicePathLength
);
243 if (*DevicePath
== NULL
) {
244 *DevicePathLength
= 0;
245 return EFI_OUT_OF_RESOURCES
;
249 // Construct the host controller part device nodes
251 DevicePathWalker
= *DevicePath
;
255 Private
->DevicePathLength
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)
259 // Construct the SATA device node
261 DevicePathWalker
= (EFI_DEVICE_PATH_PROTOCOL
*) ((UINT8
*)DevicePathWalker
+
262 (Private
->DevicePathLength
- sizeof (EFI_DEVICE_PATH_PROTOCOL
)));
265 &mAhciSataDevicePathNodeTemplate
,
266 sizeof (mAhciSataDevicePathNodeTemplate
)
268 SataDeviceNode
= (SATA_DEVICE_PATH
*)DevicePathWalker
;
269 SataDeviceNode
->HBAPortNumber
= Port
;
270 SataDeviceNode
->PortMultiplierPortNumber
= PortMultiplierPort
;
273 // Construct the end device node
275 DevicePathWalker
= (EFI_DEVICE_PATH_PROTOCOL
*) ((UINT8
*)DevicePathWalker
+
276 sizeof (SATA_DEVICE_PATH
));
279 &mAhciEndDevicePathNodeTemplate
,
280 sizeof (mAhciEndDevicePathNodeTemplate
)