]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootImpl.c
ArmVirtPkg ArmVirtDxeHobLib: Implement BuildFv3Hob
[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
bb4831c0 4Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
44a7d08b 5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
d933e70a
JW
6This program and the accompanying materials are licensed and made available under \r
7the terms and conditions of the BSD License that accompanies this distribution. \r
8The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php. \r
10 \r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include "HttpBootDxe.h"\r
17\r
95b5c32f
FS
18/**\r
19 Install HTTP Boot Callback Protocol if not installed before.\r
20\r
21 @param[in] Private Pointer to HTTP Boot private data.\r
22\r
23 @retval EFI_SUCCESS HTTP Boot Callback Protocol installed succesfully.\r
24 @retval Others Failed to install HTTP Boot Callback Protocol.\r
25\r
26**/\r
27EFI_STATUS\r
28HttpBootInstallCallback (\r
29 IN HTTP_BOOT_PRIVATE_DATA *Private\r
30 )\r
31{\r
32 EFI_STATUS Status;\r
33 EFI_HANDLE ControllerHandle;\r
34\r
35 if (!Private->UsingIpv6) {\r
36 ControllerHandle = Private->Ip4Nic->Controller;\r
37 } else {\r
38 ControllerHandle = Private->Ip6Nic->Controller;\r
39 }\r
40\r
41 //\r
42 // Check whether gEfiHttpBootCallbackProtocolGuid already installed.\r
43 //\r
44 Status = gBS->HandleProtocol (\r
45 ControllerHandle,\r
46 &gEfiHttpBootCallbackProtocolGuid,\r
47 (VOID **) &Private->HttpBootCallback\r
48 );\r
49 if (Status == EFI_UNSUPPORTED) {\r
50\r
51 CopyMem (\r
52 &Private->LoadFileCallback,\r
53 &gHttpBootDxeHttpBootCallback,\r
54 sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL)\r
55 );\r
56\r
57 //\r
58 // Install a default callback if user didn't offer one.\r
59 //\r
60 Status = gBS->InstallProtocolInterface (\r
61 &ControllerHandle,\r
62 &gEfiHttpBootCallbackProtocolGuid,\r
63 EFI_NATIVE_INTERFACE,\r
64 &Private->LoadFileCallback\r
65 );\r
66 if (EFI_ERROR (Status)) {\r
67 return Status;\r
68 }\r
69 Private->HttpBootCallback = &Private->LoadFileCallback;\r
70 }\r
71\r
72 return EFI_SUCCESS;\r
73}\r
74\r
75/**\r
76 Uninstall HTTP Boot Callback Protocol if it's installed by this driver.\r
77\r
78 @param[in] Private Pointer to HTTP Boot private data.\r
79\r
80**/\r
81VOID\r
82HttpBootUninstallCallback (\r
83 IN HTTP_BOOT_PRIVATE_DATA *Private\r
84 )\r
85{\r
86 if (Private->HttpBootCallback == &Private->LoadFileCallback) {\r
87 gBS->UninstallProtocolInterface (\r
88 Private->Controller,\r
89 &gEfiHttpBootCallbackProtocolGuid,\r
90 &Private->HttpBootCallback\r
91 );\r
92 Private->HttpBootCallback = NULL;\r
93 }\r
94}\r
95\r
d933e70a
JW
96/**\r
97 Enable the use of UEFI HTTP boot function.\r
98\r
587d204c
FS
99 If the driver has already been started but not satisfy the requirement (IP stack and \r
100 specified boot file path), this function will stop the driver and start it again.\r
101\r
d933e70a 102 @param[in] Private The pointer to the driver's private data.\r
b659408b
ZL
103 @param[in] UsingIpv6 Specifies the type of IP addresses that are to be\r
104 used during the session that is being started.\r
105 Set to TRUE for IPv6, and FALSE for IPv4.\r
fa848a40 106 @param[in] FilePath The device specific path of the file to load.\r
d933e70a
JW
107\r
108 @retval EFI_SUCCESS HTTP boot was successfully enabled.\r
587d204c
FS
109 @retval EFI_INVALID_PARAMETER Private is NULL or FilePath is NULL.\r
110 @retval EFI_INVALID_PARAMETER The FilePath doesn't contain a valid URI device path node.\r
d933e70a 111 @retval EFI_ALREADY_STARTED The driver is already in started state.\r
fa848a40 112 @retval EFI_OUT_OF_RESOURCES There are not enough resources.\r
d933e70a
JW
113 \r
114**/\r
115EFI_STATUS\r
116HttpBootStart (\r
b659408b 117 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
fa848a40
FS
118 IN BOOLEAN UsingIpv6,\r
119 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
d933e70a
JW
120 )\r
121{\r
b659408b
ZL
122 UINTN Index;\r
123 EFI_STATUS Status;\r
587d204c
FS
124 CHAR8 *Uri;\r
125 \r
d933e70a 126\r
fa848a40 127 if (Private == NULL || FilePath == NULL) {\r
d933e70a
JW
128 return EFI_INVALID_PARAMETER;\r
129 }\r
587d204c
FS
130 \r
131 //\r
132 // Check the URI in the input FilePath, in order to see whether it is\r
133 // required to boot from a new specified boot file. \r
134 //\r
135 Status = HttpBootParseFilePath (FilePath, &Uri);\r
136 if (EFI_ERROR (Status)) {\r
137 return EFI_INVALID_PARAMETER;\r
138 }\r
139 \r
140 //\r
141 // Check whether we need to stop and restart the HTTP boot driver.\r
142 //\r
d933e70a 143 if (Private->Started) {\r
587d204c
FS
144 //\r
145 // Restart is needed in 2 cases:\r
146 // 1. Http boot driver has already been started but not on the required IP stack.\r
147 // 2. The specified boot file URI in FilePath is different with the one we have\r
148 // recorded before.\r
149 //\r
150 if ((UsingIpv6 != Private->UsingIpv6) || \r
151 ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0))) {\r
152 //\r
153 // Restart is required, first stop then continue this start function.\r
154 //\r
155 Status = HttpBootStop (Private);\r
156 if (EFI_ERROR (Status)) {\r
157 return Status;\r
158 }\r
159 } else {\r
160 //\r
161 // Restart is not required.\r
162 //\r
163 if (Uri != NULL) {\r
164 FreePool (Uri);\r
165 }\r
166 return EFI_ALREADY_STARTED;\r
167 }\r
d933e70a
JW
168 }\r
169\r
b659408b
ZL
170 //\r
171 // Detect whether using ipv6 or not, and set it to the private data.\r
172 //\r
173 if (UsingIpv6 && Private->Ip6Nic != NULL) {\r
174 Private->UsingIpv6 = TRUE;\r
175 } else if (!UsingIpv6 && Private->Ip4Nic != NULL) {\r
176 Private->UsingIpv6 = FALSE;\r
177 } else {\r
587d204c
FS
178 if (Uri != NULL) {\r
179 FreePool (Uri);\r
180 }\r
b659408b
ZL
181 return EFI_UNSUPPORTED;\r
182 }\r
fa848a40
FS
183\r
184 //\r
587d204c 185 // Record the specified URI and prepare the URI parser if needed.\r
fa848a40 186 //\r
587d204c 187 Private->FilePathUri = Uri;\r
fa848a40
FS
188 if (Private->FilePathUri != NULL) {\r
189 Status = HttpParseUrl (\r
190 Private->FilePathUri,\r
191 (UINT32) AsciiStrLen (Private->FilePathUri),\r
192 FALSE,\r
193 &Private->FilePathUriParser\r
194 );\r
195 if (EFI_ERROR (Status)) {\r
587d204c 196 FreePool (Private->FilePathUri);\r
fa848a40
FS
197 return Status;\r
198 }\r
199 }\r
b659408b
ZL
200 \r
201 //\r
202 // Init the content of cached DHCP offer list.\r
203 //\r
204 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
d933e70a 205 if (!Private->UsingIpv6) {\r
d933e70a 206 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
632dcfd6 207 Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_CACHED_DHCP4_PACKET_MAX_SIZE;\r
d933e70a
JW
208 }\r
209 } else {\r
b659408b 210 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
632dcfd6 211 Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_CACHED_DHCP6_PACKET_MAX_SIZE;\r
b659408b 212 }\r
d933e70a
JW
213 }\r
214\r
b659408b
ZL
215 if (Private->UsingIpv6) {\r
216 //\r
217 // Set Ip6 policy to Automatic to start the Ip6 router discovery.\r
218 //\r
219 Status = HttpBootSetIp6Policy (Private);\r
220 if (EFI_ERROR (Status)) {\r
221 return Status;\r
222 }\r
223 }\r
587d204c 224 Private->Started = TRUE;\r
95b5c32f 225 Print (L"\n>>Start HTTP Boot over IPv%d", Private->UsingIpv6 ? 6 : 4);\r
d933e70a
JW
226\r
227 return EFI_SUCCESS;\r
228}\r
229\r
230/**\r
b659408b 231 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
232\r
233 @param[in] Private The pointer to the driver's private data.\r
234\r
235 @retval EFI_SUCCESS Boot info was successfully retrieved.\r
236 @retval EFI_INVALID_PARAMETER Private is NULL.\r
237 @retval EFI_NOT_STARTED The driver is in stopped state.\r
238 @retval EFI_DEVICE_ERROR An unexpected network error occurred.\r
239 @retval Others Other errors as indicated.\r
240 \r
241**/\r
242EFI_STATUS\r
243HttpBootDhcp (\r
244 IN HTTP_BOOT_PRIVATE_DATA *Private\r
245 )\r
246{\r
247 EFI_STATUS Status;\r
248\r
249 if (Private == NULL) {\r
250 return EFI_INVALID_PARAMETER;\r
251 }\r
252 \r
253 if (!Private->Started) {\r
254 return EFI_NOT_STARTED;\r
255 }\r
256\r
257 Status = EFI_DEVICE_ERROR;\r
258\r
259 if (!Private->UsingIpv6) {\r
b659408b
ZL
260 //\r
261 // Start D.O.R.A process to get a IPv4 address and other boot information.\r
262 //\r
d933e70a
JW
263 Status = HttpBootDhcp4Dora (Private);\r
264 } else {\r
b659408b
ZL
265 //\r
266 // Start S.A.R.R process to get a IPv6 address and other boot information.\r
267 //\r
268 Status = HttpBootDhcp6Sarr (Private);\r
d933e70a
JW
269 }\r
270\r
271 return Status;\r
272}\r
273\r
274/**\r
275 Attempt to download the boot file through HTTP message exchange.\r
276\r
277 @param[in] Private The pointer to the driver's private data.\r
278 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
279 code of EFI_SUCCESS, the amount of data transferred to\r
280 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
281 the size of Buffer required to retrieve the requested file.\r
282 @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,\r
283 then the size of the requested file is returned in\r
284 BufferSize.\r
587d204c 285 @param[out] ImageType The image type of the downloaded file.\r
d933e70a
JW
286\r
287 @retval EFI_SUCCESS Boot file was loaded successfully.\r
587d204c
FS
288 @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL.\r
289 @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL.\r
d933e70a
JW
290 @retval EFI_NOT_STARTED The driver is in stopped state.\r
291 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has \r
292 been updated with the size needed to complete the request.\r
293 @retval EFI_DEVICE_ERROR An unexpected network error occurred.\r
294 @retval Others Other errors as indicated.\r
295 \r
296**/\r
297EFI_STATUS\r
298HttpBootLoadFile (\r
299 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
300 IN OUT UINTN *BufferSize,\r
587d204c
FS
301 IN VOID *Buffer, OPTIONAL\r
302 OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r
d933e70a
JW
303 )\r
304{\r
305 EFI_STATUS Status;\r
306\r
587d204c
FS
307 if (Private == NULL || ImageType == NULL || BufferSize == NULL ) {\r
308 return EFI_INVALID_PARAMETER;\r
309 }\r
310\r
311 if (*BufferSize != 0 && Buffer == NULL) {\r
d933e70a
JW
312 return EFI_INVALID_PARAMETER;\r
313 }\r
314 \r
315 if (!Private->Started) {\r
316 return EFI_NOT_STARTED;\r
317 }\r
318\r
95b5c32f
FS
319 Status = HttpBootInstallCallback (Private);\r
320 if (EFI_ERROR(Status)) {\r
321 goto ON_EXIT;\r
322 }\r
d933e70a
JW
323\r
324 if (Private->BootFileUri == NULL) {\r
325 //\r
326 // Parse the cached offer to get the boot file URL first.\r
327 //\r
328 Status = HttpBootDiscoverBootInfo (Private);\r
329 if (EFI_ERROR (Status)) {\r
95b5c32f 330 goto ON_EXIT;\r
d933e70a
JW
331 }\r
332 }\r
333\r
334 if (!Private->HttpCreated) {\r
335 //\r
336 // Create HTTP child.\r
337 //\r
338 Status = HttpBootCreateHttpIo (Private);\r
339 if (EFI_ERROR (Status)) {\r
95b5c32f 340 goto ON_EXIT;\r
d933e70a
JW
341 }\r
342 }\r
343\r
344 if (Private->BootFileSize == 0) {\r
345 //\r
346 // Discover the information about the bootfile if we haven't.\r
347 //\r
348\r
349 //\r
350 // Try to use HTTP HEAD method.\r
351 //\r
352 Status = HttpBootGetBootFile (\r
353 Private,\r
354 TRUE,\r
355 &Private->BootFileSize,\r
587d204c 356 NULL,\r
44a7d08b 357 &Private->ImageType\r
d933e70a
JW
358 );\r
359 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
360 //\r
361 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.\r
362 //\r
363 ASSERT (Private->BootFileSize == 0);\r
364 Status = HttpBootGetBootFile (\r
365 Private,\r
366 FALSE,\r
367 &Private->BootFileSize,\r
587d204c 368 NULL,\r
44a7d08b 369 &Private->ImageType\r
d933e70a
JW
370 );\r
371 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
95b5c32f 372 goto ON_EXIT;\r
d933e70a
JW
373 }\r
374 }\r
375 }\r
376\r
377 if (*BufferSize < Private->BootFileSize) {\r
378 *BufferSize = Private->BootFileSize;\r
44a7d08b 379 *ImageType = Private->ImageType;\r
95b5c32f
FS
380 Status = EFI_BUFFER_TOO_SMALL;\r
381 goto ON_EXIT;\r
d933e70a
JW
382 }\r
383\r
384 //\r
385 // Load the boot file into Buffer\r
386 //\r
95b5c32f
FS
387 Status = HttpBootGetBootFile (\r
388 Private,\r
389 FALSE,\r
390 BufferSize,\r
391 Buffer,\r
392 ImageType\r
393 );\r
394 \r
395ON_EXIT:\r
396 HttpBootUninstallCallback (Private);\r
397 return Status;\r
d933e70a
JW
398}\r
399\r
400/**\r
401 Disable the use of UEFI HTTP boot function.\r
402\r
403 @param[in] Private The pointer to the driver's private data.\r
404\r
405 @retval EFI_SUCCESS HTTP boot was successfully disabled.\r
406 @retval EFI_NOT_STARTED The driver is already in stopped state.\r
407 @retval EFI_INVALID_PARAMETER Private is NULL.\r
408 @retval Others Unexpected error when stop the function.\r
409 \r
410**/\r
411EFI_STATUS\r
412HttpBootStop (\r
413 IN HTTP_BOOT_PRIVATE_DATA *Private\r
414 )\r
415{\r
416 UINTN Index;\r
417\r
418 if (Private == NULL) {\r
419 return EFI_INVALID_PARAMETER;\r
420 }\r
421 \r
422 if (!Private->Started) {\r
423 return EFI_NOT_STARTED;\r
424 }\r
425 \r
426 if (Private->HttpCreated) {\r
427 HttpIoDestroyIo (&Private->HttpIo);\r
428 Private->HttpCreated = FALSE;\r
429 }\r
430 \r
431 Private->Started = FALSE;\r
432 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
433 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
434 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
435 Private->Port = 0;\r
436 Private->BootFileUri = NULL;\r
437 Private->BootFileUriParser = NULL;\r
438 Private->BootFileSize = 0;\r
439 Private->SelectIndex = 0;\r
b659408b 440 Private->SelectProxyType = HttpOfferTypeMax; \r
d933e70a
JW
441\r
442 if (!Private->UsingIpv6) {\r
443 //\r
444 // Stop and release the DHCP4 child.\r
445 //\r
446 Private->Dhcp4->Stop (Private->Dhcp4);\r
447 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
448\r
449 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
450 if (Private->OfferBuffer[Index].Dhcp4.UriParser) {\r
451 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);\r
452 }\r
453 }\r
454 } else {\r
b659408b
ZL
455 //\r
456 // Stop and release the DHCP6 child.\r
457 //\r
458 Private->Dhcp6->Stop (Private->Dhcp6);\r
459 Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
460 \r
461 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
462 if (Private->OfferBuffer[Index].Dhcp6.UriParser) {\r
463 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);\r
464 }\r
465 }\r
d933e70a 466 }\r
fa848a40 467\r
7b1dbd15
JW
468 if (Private->DnsServerIp != NULL) {\r
469 FreePool (Private->DnsServerIp);\r
470 Private->DnsServerIp = NULL;\r
471 }\r
472\r
fa848a40
FS
473 if (Private->FilePathUri!= NULL) {\r
474 FreePool (Private->FilePathUri);\r
475 HttpUrlFreeParser (Private->FilePathUriParser);\r
476 Private->FilePathUri = NULL;\r
477 Private->FilePathUriParser = NULL;\r
478 }\r
d933e70a
JW
479 \r
480 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
481 Private->OfferNum = 0;\r
482 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
483 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
fa848a40
FS
484 \r
485 HttpBootFreeCacheList (Private);\r
486 \r
d933e70a
JW
487 return EFI_SUCCESS;\r
488}\r
489\r
490/**\r
491 Causes the driver to load a specified file.\r
492\r
493 @param This Protocol instance pointer.\r
494 @param FilePath The device specific path of the file to load.\r
495 @param BootPolicy If TRUE, indicates that the request originates from the\r
496 boot manager is attempting to load FilePath as a boot\r
497 selection. If FALSE, then FilePath must match as exact file\r
498 to be loaded.\r
499 @param BufferSize On input the size of Buffer in bytes. On output with a return\r
500 code of EFI_SUCCESS, the amount of data transferred to\r
501 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
502 the size of Buffer required to retrieve the requested file.\r
503 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
504 then the size of the requested file is returned in\r
505 BufferSize.\r
506\r
507 @retval EFI_SUCCESS The file was loaded.\r
508 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy\r
509 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
510 BufferSize is NULL.\r
511 @retval EFI_NO_MEDIA No medium was present to load the file.\r
512 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
513 @retval EFI_NO_RESPONSE The remote system did not respond.\r
514 @retval EFI_NOT_FOUND The file was not found.\r
515 @retval EFI_ABORTED The file load process was manually cancelled.\r
516 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
517 BufferSize has been updated with the size needed to complete\r
518 the request.\r
519\r
520**/\r
521EFI_STATUS\r
522EFIAPI\r
523HttpBootDxeLoadFile (\r
524 IN EFI_LOAD_FILE_PROTOCOL *This,\r
525 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
526 IN BOOLEAN BootPolicy,\r
527 IN OUT UINTN *BufferSize,\r
528 IN VOID *Buffer OPTIONAL\r
529 )\r
530{\r
531 HTTP_BOOT_PRIVATE_DATA *Private;\r
b659408b 532 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;\r
d933e70a 533 BOOLEAN MediaPresent;\r
b659408b 534 BOOLEAN UsingIpv6;\r
d933e70a 535 EFI_STATUS Status;\r
587d204c 536 HTTP_BOOT_IMAGE_TYPE ImageType;\r
d933e70a 537\r
fa848a40 538 if (This == NULL || BufferSize == NULL || FilePath == NULL) {\r
d933e70a
JW
539 return EFI_INVALID_PARAMETER;\r
540 }\r
541\r
542 //\r
543 // Only support BootPolicy\r
544 //\r
545 if (!BootPolicy) {\r
546 return EFI_UNSUPPORTED;\r
547 }\r
548\r
b659408b
ZL
549 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);\r
550 Private = VirtualNic->Private;\r
b659408b 551 \r
d933e70a
JW
552 //\r
553 // Check media status before HTTP boot start\r
554 //\r
555 MediaPresent = TRUE;\r
556 NetLibDetectMedia (Private->Controller, &MediaPresent);\r
557 if (!MediaPresent) {\r
558 return EFI_NO_MEDIA;\r
559 }\r
fa848a40 560 \r
b659408b
ZL
561 //\r
562 // Check whether the virtual nic is using IPv6 or not.\r
563 //\r
fa848a40 564 UsingIpv6 = FALSE;\r
b659408b
ZL
565 if (VirtualNic == Private->Ip6Nic) {\r
566 UsingIpv6 = TRUE;\r
567 }\r
fa848a40 568\r
d933e70a 569 //\r
fa848a40 570 // Initialize HTTP boot.\r
d933e70a 571 //\r
fa848a40 572 Status = HttpBootStart (Private, UsingIpv6, FilePath);\r
587d204c
FS
573 if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {\r
574 return Status;\r
b659408b 575 }\r
587d204c 576 \r
fa848a40
FS
577 //\r
578 // Load the boot file.\r
579 //\r
587d204c
FS
580 ImageType = ImageTypeMax;\r
581 Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);\r
582 if (EFI_ERROR (Status)) {\r
583 if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) {\r
584 Status = EFI_WARN_FILE_SYSTEM;\r
585 } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
586 HttpBootStop (Private);\r
587 }\r
588 return Status;\r
d933e70a
JW
589 }\r
590\r
587d204c
FS
591 //\r
592 // Register the RAM Disk to the system if needed.\r
593 //\r
594 if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {\r
595 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);\r
596 if (!EFI_ERROR (Status)) {\r
597 Status = EFI_WARN_FILE_SYSTEM;\r
b659408b 598 }\r
d933e70a 599 }\r
287f05cd
FS
600\r
601 //\r
602 // Stop the HTTP Boot service after the boot image is downloaded.\r
603 //\r
604 HttpBootStop (Private);\r
d933e70a
JW
605 return Status;\r
606}\r
607\r
608///\r
609/// Load File Protocol instance\r
610///\r
611GLOBAL_REMOVE_IF_UNREFERENCED \r
612EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {\r
613 HttpBootDxeLoadFile\r
614};\r
95b5c32f
FS
615\r
616/**\r
617 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a\r
618 packet.\r
619\r
620 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.\r
621 Parameters DataType and Received specify the type of event and the format of the buffer pointed\r
622 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not\r
623 execute for more than 5 ms.\r
624 The returned status code determines the behavior of the HTTP Boot driver.\r
625\r
626 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.\r
627 @param[in] DataType The event that occurs in the current state.\r
628 @param[in] Received TRUE if the callback is being invoked due to a receive event.\r
629 FALSE if the callback is being invoked due to a transmit event.\r
630 @param[in] DataLength The length in bytes of the buffer pointed to by Data.\r
631 @param[in] Data A pointer to the buffer of data, the data type is specified by\r
632 DataType.\r
633 \r
634 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.\r
635 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.\r
636**/\r
637EFI_STATUS\r
3858c58a 638EFIAPI\r
95b5c32f
FS
639HttpBootCallback (\r
640 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,\r
641 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,\r
642 IN BOOLEAN Received,\r
643 IN UINT32 DataLength,\r
644 IN VOID *Data OPTIONAL\r
645 )\r
646{\r
647 EFI_HTTP_MESSAGE *HttpMessage;\r
648 EFI_HTTP_HEADER *HttpHeader;\r
649 HTTP_BOOT_PRIVATE_DATA *Private;\r
650 UINT32 Percentage;\r
651\r
652 Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This);\r
653\r
654 switch (DataType) {\r
655 case HttpBootDhcp4:\r
656 case HttpBootDhcp6:\r
657 Print (L".");\r
658 break;\r
659\r
660 case HttpBootHttpRequest:\r
661 if (Data != NULL) {\r
662 HttpMessage = (EFI_HTTP_MESSAGE *) Data;\r
663 if (HttpMessage->Data.Request->Method == HttpMethodGet &&\r
664 HttpMessage->Data.Request->Url != NULL) {\r
665 Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url);\r
666 }\r
667 }\r
668 break;\r
669\r
670 case HttpBootHttpResponse:\r
671 if (Data != NULL) {\r
672 HttpMessage = (EFI_HTTP_MESSAGE *) Data;\r
bb4831c0
FS
673 \r
674 if (HttpMessage->Data.Response != NULL) {\r
675 if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {\r
676 //\r
677 // Server indicates the resource has been redirected to a different URL\r
678 // according to the section 6.4 of RFC7231 and the RFC 7538.\r
679 // Display the redirect information on the screen.\r
680 //\r
681 HttpHeader = HttpFindHeader (\r
682 HttpMessage->HeaderCount,\r
683 HttpMessage->Headers,\r
684 HTTP_HEADER_LOCATION\r
685 );\r
686 if (HttpHeader != NULL) {\r
687 Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue);\r
688 }\r
689 }\r
690 }\r
691 \r
95b5c32f
FS
692 HttpHeader = HttpFindHeader (\r
693 HttpMessage->HeaderCount,\r
694 HttpMessage->Headers,\r
695 HTTP_HEADER_CONTENT_LENGTH\r
696 );\r
697 if (HttpHeader != NULL) {\r
698 Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);\r
699 Private->ReceivedSize = 0;\r
700 Private->Percentage = 0;\r
701 }\r
702 }\r
703 break;\r
704\r
705 case HttpBootHttpEntityBody:\r
706 if (DataLength != 0) {\r
707 if (Private->FileSize != 0) {\r
708 //\r
709 // We already know the file size, print in percentage format.\r
710 //\r
711 if (Private->ReceivedSize == 0) {\r
bb4831c0 712 Print (L" File Size: %lu Bytes\n", Private->FileSize);\r
95b5c32f
FS
713 }\r
714 Private->ReceivedSize += DataLength;\r
715 Percentage = (UINT32) DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);\r
716 if (Private->Percentage != Percentage) {\r
717 Private->Percentage = Percentage;\r
718 Print (L"\r Downloading...%d%%", Percentage);\r
719 }\r
720 } else {\r
721 //\r
722 // In some case we couldn't get the file size from the HTTP header, so we\r
723 // just print the downloaded file size.\r
724 //\r
725 Private->ReceivedSize += DataLength;\r
726 Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);\r
727 }\r
728 }\r
729 break;\r
730\r
731 default:\r
732 break;\r
733 };\r
734\r
735 return EFI_SUCCESS;\r
736}\r
737\r
738///\r
739/// HTTP Boot Callback Protocol instance\r
740///\r
741GLOBAL_REMOVE_IF_UNREFERENCED \r
742EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {\r
743 HttpBootCallback\r
744};\r