1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/** @file
The device path help function.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "NvmExpressPei.h"
//
// Template for an Nvm Express Device Path node
//
NVME_NAMESPACE_DEVICE_PATH mNvmeDevicePathNodeTemplate = {
{ // Header
MESSAGING_DEVICE_PATH,
MSG_NVME_NAMESPACE_DP,
{
(UINT8)(sizeof (NVME_NAMESPACE_DEVICE_PATH)),
(UINT8)((sizeof (NVME_NAMESPACE_DEVICE_PATH)) >> 8)
}
},
0x0, // NamespaceId
0x0 // NamespaceUuid
};
//
// Template for an End of entire Device Path node
//
EFI_DEVICE_PATH_PROTOCOL mNvmeEndDevicePathNodeTemplate = {
END_DEVICE_PATH_TYPE,
END_ENTIRE_DEVICE_PATH_SUBTYPE,
{
(UINT8)(sizeof (EFI_DEVICE_PATH_PROTOCOL)),
(UINT8)((sizeof (EFI_DEVICE_PATH_PROTOCOL)) >> 8)
}
};
/**
Get the size of the current device path instance.
@param[in] DevicePath A pointer to the EFI_DEVICE_PATH_PROTOCOL
structure.
@param[out] InstanceSize The size of the current device path instance.
@param[out] EntireDevicePathEnd Indicate whether the instance is the last
one in the device path strucure.
@retval EFI_SUCCESS The size of the current device path instance is fetched.
@retval Others Fails to get the size of the current device path instance.
**/
EFI_STATUS
GetDevicePathInstanceSize (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT UINTN *InstanceSize,
OUT BOOLEAN *EntireDevicePathEnd
)
{
EFI_DEVICE_PATH_PROTOCOL *Walker;
if ((DevicePath == NULL) || (InstanceSize == NULL) || (EntireDevicePathEnd == NULL)) {
return EFI_INVALID_PARAMETER;
}
//
// Find the end of the device path instance
//
Walker = DevicePath;
while (Walker->Type != END_DEVICE_PATH_TYPE) {
Walker = NextDevicePathNode (Walker);
}
//
// Check if 'Walker' points to the end of an entire device path
//
if (Walker->SubType == END_ENTIRE_DEVICE_PATH_SUBTYPE) {
*EntireDevicePathEnd = TRUE;
} else if (Walker->SubType == END_INSTANCE_DEVICE_PATH_SUBTYPE) {
*EntireDevicePathEnd = FALSE;
} else {
return EFI_INVALID_PARAMETER;
}
//
// Compute the size of the device path instance
//
*InstanceSize = ((UINTN)Walker - (UINTN)(DevicePath)) + sizeof (EFI_DEVICE_PATH_PROTOCOL);
return EFI_SUCCESS;
}
/**
Check the validity of the device path of a NVM Express host controller.
@param[in] DevicePath A pointer to the EFI_DEVICE_PATH_PROTOCOL
structure.
@param[in] DevicePathLength The length of the device path.
@retval EFI_SUCCESS The device path is valid.
@retval EFI_INVALID_PARAMETER The device path is invalid.
**/
EFI_STATUS
NvmeIsHcDevicePathValid (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
IN UINTN DevicePathLength
)
{
EFI_DEVICE_PATH_PROTOCOL *Start;
UINTN Size;
if (DevicePath == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// Validate the DevicePathLength is big enough to touch the first node.
//
if (DevicePathLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
return EFI_INVALID_PARAMETER;
}
Start = DevicePath;
while (!(DevicePath->Type == END_DEVICE_PATH_TYPE &&
DevicePath->SubType == END_ENTIRE_DEVICE_PATH_SUBTYPE))
{
DevicePath = NextDevicePathNode (DevicePath);
//
// Prevent overflow and invalid zero in the 'Length' field of a device path
// node.
//
if ((UINTN)DevicePath <= (UINTN)Start) {
return EFI_INVALID_PARAMETER;
}
//
// Prevent touching memory beyond given DevicePathLength.
//
if ((UINTN)DevicePath - (UINTN)Start >
DevicePathLength - sizeof (EFI_DEVICE_PATH_PROTOCOL))
{
return EFI_INVALID_PARAMETER;
}
}
//
// Check if the device path and its size match exactly with each other.
//
Size = ((UINTN)DevicePath - (UINTN)Start) + sizeof (EFI_DEVICE_PATH_PROTOCOL);
if (Size != DevicePathLength) {
return EFI_INVALID_PARAMETER;
}
return EFI_SUCCESS;
}
/**
Build the device path for an Nvm Express device with given namespace identifier
and namespace extended unique identifier.
@param[in] Private A pointer to the PEI_NVME_CONTROLLER_PRIVATE_DATA
data structure.
@param[in] NamespaceId The given namespace identifier.
@param[in] NamespaceUuid The given namespace extended unique identifier.
@param[out] DevicePathLength The length of the device path in bytes specified
by DevicePath.
@param[out] DevicePath The device path of Nvm Express device.
@retval EFI_SUCCESS The operation succeeds.
@retval EFI_INVALID_PARAMETER The parameters are invalid.
@retval EFI_OUT_OF_RESOURCES The operation fails due to lack of resources.
**/
EFI_STATUS
NvmeBuildDevicePath (
IN PEI_NVME_CONTROLLER_PRIVATE_DATA *Private,
IN UINT32 NamespaceId,
IN UINT64 NamespaceUuid,
OUT UINTN *DevicePathLength,
OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
)
{
EFI_DEVICE_PATH_PROTOCOL *DevicePathWalker;
NVME_NAMESPACE_DEVICE_PATH *NvmeDeviceNode;
if ((DevicePathLength == NULL) || (DevicePath == NULL)) {
return EFI_INVALID_PARAMETER;
}
*DevicePathLength = Private->DevicePathLength + sizeof (NVME_NAMESPACE_DEVICE_PATH);
*DevicePath = AllocatePool (*DevicePathLength);
if (*DevicePath == NULL) {
*DevicePathLength = 0;
return EFI_OUT_OF_RESOURCES;
}
//
// Construct the host controller part device nodes
//
DevicePathWalker = *DevicePath;
CopyMem (
DevicePathWalker,
Private->DevicePath,
Private->DevicePathLength - sizeof (EFI_DEVICE_PATH_PROTOCOL)
);
//
// Construct the Nvm Express device node
//
DevicePathWalker = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePathWalker +
(Private->DevicePathLength - sizeof (EFI_DEVICE_PATH_PROTOCOL)));
CopyMem (
DevicePathWalker,
&mNvmeDevicePathNodeTemplate,
sizeof (mNvmeDevicePathNodeTemplate)
);
NvmeDeviceNode = (NVME_NAMESPACE_DEVICE_PATH *)DevicePathWalker;
NvmeDeviceNode->NamespaceId = NamespaceId;
NvmeDeviceNode->NamespaceUuid = NamespaceUuid;
//
// Construct the end device node
//
DevicePathWalker = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePathWalker +
sizeof (NVME_NAMESPACE_DEVICE_PATH));
CopyMem (
DevicePathWalker,
&mNvmeEndDevicePathNodeTemplate,
sizeof (mNvmeEndDevicePathNodeTemplate)
);
return EFI_SUCCESS;
}
|