]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootImpl.c
NetworkPkg:Enable Http Boot over Ipv6 stack
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootImpl.c
CommitLineData
d933e70a
JW
1/** @file\r
2 The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.\r
3\r
4Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials are licensed and made available under \r
6the terms and conditions of the BSD License that accompanies this distribution. \r
7The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php. \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "HttpBootDxe.h"\r
16\r
17/**\r
18 Enable the use of UEFI HTTP boot function.\r
19\r
20 @param[in] Private The pointer to the driver's private data.\r
b659408b
ZL
21 @param[in] UsingIpv6 Specifies the type of IP addresses that are to be\r
22 used during the session that is being started.\r
23 Set to TRUE for IPv6, and FALSE for IPv4.\r
d933e70a
JW
24\r
25 @retval EFI_SUCCESS HTTP boot was successfully enabled.\r
26 @retval EFI_INVALID_PARAMETER Private is NULL.\r
27 @retval EFI_ALREADY_STARTED The driver is already in started state.\r
28 \r
29**/\r
30EFI_STATUS\r
31HttpBootStart (\r
b659408b
ZL
32 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
33 IN BOOLEAN UsingIpv6\r
d933e70a
JW
34 )\r
35{\r
b659408b
ZL
36 UINTN Index;\r
37 EFI_STATUS Status;\r
d933e70a
JW
38\r
39 if (Private == NULL) {\r
40 return EFI_INVALID_PARAMETER;\r
41 }\r
42\r
43 if (Private->Started) {\r
44 return EFI_ALREADY_STARTED;\r
45 }\r
46\r
b659408b
ZL
47 //\r
48 // Detect whether using ipv6 or not, and set it to the private data.\r
49 //\r
50 if (UsingIpv6 && Private->Ip6Nic != NULL) {\r
51 Private->UsingIpv6 = TRUE;\r
52 } else if (!UsingIpv6 && Private->Ip4Nic != NULL) {\r
53 Private->UsingIpv6 = FALSE;\r
54 } else {\r
55 return EFI_UNSUPPORTED;\r
56 }\r
57 \r
58 //\r
59 // Init the content of cached DHCP offer list.\r
60 //\r
61 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
d933e70a 62 if (!Private->UsingIpv6) {\r
d933e70a
JW
63 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
64 Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_BOOT_DHCP4_PACKET_MAX_SIZE;\r
65 }\r
66 } else {\r
b659408b
ZL
67 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
68 Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_BOOT_DHCP6_PACKET_MAX_SIZE;\r
69 }\r
d933e70a
JW
70 }\r
71\r
b659408b
ZL
72 if (Private->UsingIpv6) {\r
73 //\r
74 // Set Ip6 policy to Automatic to start the Ip6 router discovery.\r
75 //\r
76 Status = HttpBootSetIp6Policy (Private);\r
77 if (EFI_ERROR (Status)) {\r
78 return Status;\r
79 }\r
80 }\r
d933e70a
JW
81 Private->Started = TRUE;\r
82\r
83 return EFI_SUCCESS;\r
84}\r
85\r
86/**\r
b659408b 87 Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.\r
d933e70a
JW
88\r
89 @param[in] Private The pointer to the driver's private data.\r
90\r
91 @retval EFI_SUCCESS Boot info was successfully retrieved.\r
92 @retval EFI_INVALID_PARAMETER Private is NULL.\r
93 @retval EFI_NOT_STARTED The driver is in stopped state.\r
94 @retval EFI_DEVICE_ERROR An unexpected network error occurred.\r
95 @retval Others Other errors as indicated.\r
96 \r
97**/\r
98EFI_STATUS\r
99HttpBootDhcp (\r
100 IN HTTP_BOOT_PRIVATE_DATA *Private\r
101 )\r
102{\r
103 EFI_STATUS Status;\r
104\r
105 if (Private == NULL) {\r
106 return EFI_INVALID_PARAMETER;\r
107 }\r
108 \r
109 if (!Private->Started) {\r
110 return EFI_NOT_STARTED;\r
111 }\r
112\r
113 Status = EFI_DEVICE_ERROR;\r
114\r
115 if (!Private->UsingIpv6) {\r
b659408b
ZL
116 //\r
117 // Start D.O.R.A process to get a IPv4 address and other boot information.\r
118 //\r
d933e70a
JW
119 Status = HttpBootDhcp4Dora (Private);\r
120 } else {\r
b659408b
ZL
121 //\r
122 // Start S.A.R.R process to get a IPv6 address and other boot information.\r
123 //\r
124 Status = HttpBootDhcp6Sarr (Private);\r
d933e70a
JW
125 }\r
126\r
127 return Status;\r
128}\r
129\r
130/**\r
131 Attempt to download the boot file through HTTP message exchange.\r
132\r
133 @param[in] Private The pointer to the driver's private data.\r
134 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
135 code of EFI_SUCCESS, the amount of data transferred to\r
136 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
137 the size of Buffer required to retrieve the requested file.\r
138 @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,\r
139 then the size of the requested file is returned in\r
140 BufferSize.\r
141\r
142 @retval EFI_SUCCESS Boot file was loaded successfully.\r
143 @retval EFI_INVALID_PARAMETER Private is NULL.\r
144 @retval EFI_NOT_STARTED The driver is in stopped state.\r
145 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has \r
146 been updated with the size needed to complete the request.\r
147 @retval EFI_DEVICE_ERROR An unexpected network error occurred.\r
148 @retval Others Other errors as indicated.\r
149 \r
150**/\r
151EFI_STATUS\r
152HttpBootLoadFile (\r
153 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
154 IN OUT UINTN *BufferSize,\r
155 IN VOID *Buffer OPTIONAL\r
156 )\r
157{\r
158 EFI_STATUS Status;\r
159\r
160 if (Private == NULL) {\r
161 return EFI_INVALID_PARAMETER;\r
162 }\r
163 \r
164 if (!Private->Started) {\r
165 return EFI_NOT_STARTED;\r
166 }\r
167\r
168 Status = EFI_DEVICE_ERROR;\r
169\r
170 if (Private->BootFileUri == NULL) {\r
171 //\r
172 // Parse the cached offer to get the boot file URL first.\r
173 //\r
174 Status = HttpBootDiscoverBootInfo (Private);\r
175 if (EFI_ERROR (Status)) {\r
176 return Status;\r
177 }\r
178 }\r
179\r
180 if (!Private->HttpCreated) {\r
181 //\r
182 // Create HTTP child.\r
183 //\r
184 Status = HttpBootCreateHttpIo (Private);\r
185 if (EFI_ERROR (Status)) {\r
186 return Status;\r
187 }\r
188 }\r
189\r
190 if (Private->BootFileSize == 0) {\r
191 //\r
192 // Discover the information about the bootfile if we haven't.\r
193 //\r
194\r
195 //\r
196 // Try to use HTTP HEAD method.\r
197 //\r
198 Status = HttpBootGetBootFile (\r
199 Private,\r
200 TRUE,\r
201 &Private->BootFileSize,\r
202 NULL\r
203 );\r
204 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
205 //\r
206 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.\r
207 //\r
208 ASSERT (Private->BootFileSize == 0);\r
209 Status = HttpBootGetBootFile (\r
210 Private,\r
211 FALSE,\r
212 &Private->BootFileSize,\r
213 NULL\r
214 );\r
215 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
216 return Status;\r
217 }\r
218 }\r
219 }\r
220\r
221 if (*BufferSize < Private->BootFileSize) {\r
222 *BufferSize = Private->BootFileSize;\r
223 return EFI_BUFFER_TOO_SMALL;\r
224 }\r
225\r
226 //\r
227 // Load the boot file into Buffer\r
228 //\r
229 return HttpBootGetBootFile (\r
230 Private,\r
231 FALSE,\r
232 BufferSize,\r
233 Buffer\r
234 );\r
235}\r
236\r
237/**\r
238 Disable the use of UEFI HTTP boot function.\r
239\r
240 @param[in] Private The pointer to the driver's private data.\r
241\r
242 @retval EFI_SUCCESS HTTP boot was successfully disabled.\r
243 @retval EFI_NOT_STARTED The driver is already in stopped state.\r
244 @retval EFI_INVALID_PARAMETER Private is NULL.\r
245 @retval Others Unexpected error when stop the function.\r
246 \r
247**/\r
248EFI_STATUS\r
249HttpBootStop (\r
250 IN HTTP_BOOT_PRIVATE_DATA *Private\r
251 )\r
252{\r
253 UINTN Index;\r
254\r
255 if (Private == NULL) {\r
256 return EFI_INVALID_PARAMETER;\r
257 }\r
258 \r
259 if (!Private->Started) {\r
260 return EFI_NOT_STARTED;\r
261 }\r
262 \r
263 if (Private->HttpCreated) {\r
264 HttpIoDestroyIo (&Private->HttpIo);\r
265 Private->HttpCreated = FALSE;\r
266 }\r
267 \r
268 Private->Started = FALSE;\r
269 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
270 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
271 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
272 Private->Port = 0;\r
273 Private->BootFileUri = NULL;\r
274 Private->BootFileUriParser = NULL;\r
275 Private->BootFileSize = 0;\r
276 Private->SelectIndex = 0;\r
b659408b 277 Private->SelectProxyType = HttpOfferTypeMax; \r
d933e70a
JW
278\r
279 if (!Private->UsingIpv6) {\r
280 //\r
281 // Stop and release the DHCP4 child.\r
282 //\r
283 Private->Dhcp4->Stop (Private->Dhcp4);\r
284 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
285\r
286 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
287 if (Private->OfferBuffer[Index].Dhcp4.UriParser) {\r
288 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);\r
289 }\r
290 }\r
291 } else {\r
b659408b
ZL
292 //\r
293 // Stop and release the DHCP6 child.\r
294 //\r
295 Private->Dhcp6->Stop (Private->Dhcp6);\r
296 Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
297 \r
298 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
299 if (Private->OfferBuffer[Index].Dhcp6.UriParser) {\r
300 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);\r
301 }\r
302 }\r
d933e70a
JW
303 }\r
304 \r
305 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
306 Private->OfferNum = 0;\r
307 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
308 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
309\r
310 return EFI_SUCCESS;\r
311}\r
312\r
313/**\r
314 Causes the driver to load a specified file.\r
315\r
316 @param This Protocol instance pointer.\r
317 @param FilePath The device specific path of the file to load.\r
318 @param BootPolicy If TRUE, indicates that the request originates from the\r
319 boot manager is attempting to load FilePath as a boot\r
320 selection. If FALSE, then FilePath must match as exact file\r
321 to be loaded.\r
322 @param BufferSize On input the size of Buffer in bytes. On output with a return\r
323 code of EFI_SUCCESS, the amount of data transferred to\r
324 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
325 the size of Buffer required to retrieve the requested file.\r
326 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
327 then the size of the requested file is returned in\r
328 BufferSize.\r
329\r
330 @retval EFI_SUCCESS The file was loaded.\r
331 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy\r
332 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
333 BufferSize is NULL.\r
334 @retval EFI_NO_MEDIA No medium was present to load the file.\r
335 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
336 @retval EFI_NO_RESPONSE The remote system did not respond.\r
337 @retval EFI_NOT_FOUND The file was not found.\r
338 @retval EFI_ABORTED The file load process was manually cancelled.\r
339 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
340 BufferSize has been updated with the size needed to complete\r
341 the request.\r
342\r
343**/\r
344EFI_STATUS\r
345EFIAPI\r
346HttpBootDxeLoadFile (\r
347 IN EFI_LOAD_FILE_PROTOCOL *This,\r
348 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
349 IN BOOLEAN BootPolicy,\r
350 IN OUT UINTN *BufferSize,\r
351 IN VOID *Buffer OPTIONAL\r
352 )\r
353{\r
354 HTTP_BOOT_PRIVATE_DATA *Private;\r
b659408b 355 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;\r
d933e70a 356 BOOLEAN MediaPresent;\r
b659408b 357 BOOLEAN UsingIpv6;\r
d933e70a
JW
358 EFI_STATUS Status;\r
359\r
360 if (This == NULL || BufferSize == NULL) {\r
361 return EFI_INVALID_PARAMETER;\r
362 }\r
363\r
364 //\r
365 // Only support BootPolicy\r
366 //\r
367 if (!BootPolicy) {\r
368 return EFI_UNSUPPORTED;\r
369 }\r
370\r
b659408b
ZL
371 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);\r
372 Private = VirtualNic->Private;\r
373 UsingIpv6 = FALSE;\r
374 \r
d933e70a
JW
375 //\r
376 // Check media status before HTTP boot start\r
377 //\r
378 MediaPresent = TRUE;\r
379 NetLibDetectMedia (Private->Controller, &MediaPresent);\r
380 if (!MediaPresent) {\r
381 return EFI_NO_MEDIA;\r
382 }\r
383\r
b659408b
ZL
384 //\r
385 // Check whether the virtual nic is using IPv6 or not.\r
386 //\r
387 if (VirtualNic == Private->Ip6Nic) {\r
388 UsingIpv6 = TRUE;\r
389 }\r
390 \r
d933e70a
JW
391 //\r
392 // Initialize HTTP boot and load the boot file.\r
393 //\r
b659408b
ZL
394 Status = HttpBootStart (Private, UsingIpv6);\r
395 if (Status == EFI_ALREADY_STARTED && UsingIpv6 != Private->UsingIpv6) {\r
396 //\r
397 // Http boot Driver has already been started but not on the required IP version, restart it.\r
398 //\r
399 Status = HttpBootStop (Private);\r
400 if (!EFI_ERROR (Status)) {\r
401 Status = HttpBootStart (Private, UsingIpv6);\r
402 }\r
403 }\r
d933e70a
JW
404 if (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED) {\r
405 Status = HttpBootLoadFile (Private, BufferSize, Buffer);\r
406 }\r
407\r
408 if (Status != EFI_SUCCESS && Status != EFI_BUFFER_TOO_SMALL) {\r
409 HttpBootStop (Private);\r
410 } else {\r
b659408b
ZL
411 if (!Private->UsingIpv6) {\r
412 //\r
413 // Stop and release the DHCP4 child.\r
414 //\r
415 Private->Dhcp4->Stop (Private->Dhcp4);\r
416 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
417 } else {\r
418 //\r
419 // Stop and release the DHCP6 child.\r
420 //\r
421 Private->Dhcp6->Stop (Private->Dhcp6);\r
422 Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
423 }\r
d933e70a
JW
424 }\r
425\r
426 return Status;\r
427}\r
428\r
429///\r
430/// Load File Protocol instance\r
431///\r
432GLOBAL_REMOVE_IF_UNREFERENCED \r
433EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {\r
434 HttpBootDxeLoadFile\r
435};\r