]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootImpl.c
NetworkPkg/HttpBootDxe: Add HTTP Boot Callback protocol support.
[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
fa848a40 4Copyright (c) 2015 - 2016, 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
FS
467\r
468 if (Private->FilePathUri!= NULL) {\r
469 FreePool (Private->FilePathUri);\r
470 HttpUrlFreeParser (Private->FilePathUriParser);\r
471 Private->FilePathUri = NULL;\r
472 Private->FilePathUriParser = NULL;\r
473 }\r
d933e70a
JW
474 \r
475 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
476 Private->OfferNum = 0;\r
477 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
478 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
fa848a40
FS
479 \r
480 HttpBootFreeCacheList (Private);\r
481 \r
d933e70a
JW
482 return EFI_SUCCESS;\r
483}\r
484\r
485/**\r
486 Causes the driver to load a specified file.\r
487\r
488 @param This Protocol instance pointer.\r
489 @param FilePath The device specific path of the file to load.\r
490 @param BootPolicy If TRUE, indicates that the request originates from the\r
491 boot manager is attempting to load FilePath as a boot\r
492 selection. If FALSE, then FilePath must match as exact file\r
493 to be loaded.\r
494 @param BufferSize On input the size of Buffer in bytes. On output with a return\r
495 code of EFI_SUCCESS, the amount of data transferred to\r
496 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
497 the size of Buffer required to retrieve the requested file.\r
498 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
499 then the size of the requested file is returned in\r
500 BufferSize.\r
501\r
502 @retval EFI_SUCCESS The file was loaded.\r
503 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy\r
504 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
505 BufferSize is NULL.\r
506 @retval EFI_NO_MEDIA No medium was present to load the file.\r
507 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
508 @retval EFI_NO_RESPONSE The remote system did not respond.\r
509 @retval EFI_NOT_FOUND The file was not found.\r
510 @retval EFI_ABORTED The file load process was manually cancelled.\r
511 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
512 BufferSize has been updated with the size needed to complete\r
513 the request.\r
514\r
515**/\r
516EFI_STATUS\r
517EFIAPI\r
518HttpBootDxeLoadFile (\r
519 IN EFI_LOAD_FILE_PROTOCOL *This,\r
520 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
521 IN BOOLEAN BootPolicy,\r
522 IN OUT UINTN *BufferSize,\r
523 IN VOID *Buffer OPTIONAL\r
524 )\r
525{\r
526 HTTP_BOOT_PRIVATE_DATA *Private;\r
b659408b 527 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;\r
d933e70a 528 BOOLEAN MediaPresent;\r
b659408b 529 BOOLEAN UsingIpv6;\r
d933e70a 530 EFI_STATUS Status;\r
587d204c 531 HTTP_BOOT_IMAGE_TYPE ImageType;\r
d933e70a 532\r
fa848a40 533 if (This == NULL || BufferSize == NULL || FilePath == NULL) {\r
d933e70a
JW
534 return EFI_INVALID_PARAMETER;\r
535 }\r
536\r
537 //\r
538 // Only support BootPolicy\r
539 //\r
540 if (!BootPolicy) {\r
541 return EFI_UNSUPPORTED;\r
542 }\r
543\r
b659408b
ZL
544 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);\r
545 Private = VirtualNic->Private;\r
b659408b 546 \r
d933e70a
JW
547 //\r
548 // Check media status before HTTP boot start\r
549 //\r
550 MediaPresent = TRUE;\r
551 NetLibDetectMedia (Private->Controller, &MediaPresent);\r
552 if (!MediaPresent) {\r
553 return EFI_NO_MEDIA;\r
554 }\r
fa848a40 555 \r
b659408b
ZL
556 //\r
557 // Check whether the virtual nic is using IPv6 or not.\r
558 //\r
fa848a40 559 UsingIpv6 = FALSE;\r
b659408b
ZL
560 if (VirtualNic == Private->Ip6Nic) {\r
561 UsingIpv6 = TRUE;\r
562 }\r
fa848a40 563\r
d933e70a 564 //\r
fa848a40 565 // Initialize HTTP boot.\r
d933e70a 566 //\r
fa848a40 567 Status = HttpBootStart (Private, UsingIpv6, FilePath);\r
587d204c
FS
568 if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {\r
569 return Status;\r
b659408b 570 }\r
587d204c 571 \r
fa848a40
FS
572 //\r
573 // Load the boot file.\r
574 //\r
587d204c
FS
575 ImageType = ImageTypeMax;\r
576 Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);\r
577 if (EFI_ERROR (Status)) {\r
578 if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) {\r
579 Status = EFI_WARN_FILE_SYSTEM;\r
580 } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
581 HttpBootStop (Private);\r
582 }\r
583 return Status;\r
d933e70a
JW
584 }\r
585\r
587d204c
FS
586 //\r
587 // Register the RAM Disk to the system if needed.\r
588 //\r
589 if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {\r
590 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);\r
591 if (!EFI_ERROR (Status)) {\r
592 Status = EFI_WARN_FILE_SYSTEM;\r
b659408b 593 }\r
d933e70a 594 }\r
287f05cd
FS
595\r
596 //\r
597 // Stop the HTTP Boot service after the boot image is downloaded.\r
598 //\r
599 HttpBootStop (Private);\r
d933e70a
JW
600 return Status;\r
601}\r
602\r
603///\r
604/// Load File Protocol instance\r
605///\r
606GLOBAL_REMOVE_IF_UNREFERENCED \r
607EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {\r
608 HttpBootDxeLoadFile\r
609};\r
95b5c32f
FS
610\r
611/**\r
612 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a\r
613 packet.\r
614\r
615 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.\r
616 Parameters DataType and Received specify the type of event and the format of the buffer pointed\r
617 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not\r
618 execute for more than 5 ms.\r
619 The returned status code determines the behavior of the HTTP Boot driver.\r
620\r
621 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.\r
622 @param[in] DataType The event that occurs in the current state.\r
623 @param[in] Received TRUE if the callback is being invoked due to a receive event.\r
624 FALSE if the callback is being invoked due to a transmit event.\r
625 @param[in] DataLength The length in bytes of the buffer pointed to by Data.\r
626 @param[in] Data A pointer to the buffer of data, the data type is specified by\r
627 DataType.\r
628 \r
629 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.\r
630 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.\r
631**/\r
632EFI_STATUS\r
633HttpBootCallback (\r
634 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,\r
635 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,\r
636 IN BOOLEAN Received,\r
637 IN UINT32 DataLength,\r
638 IN VOID *Data OPTIONAL\r
639 )\r
640{\r
641 EFI_HTTP_MESSAGE *HttpMessage;\r
642 EFI_HTTP_HEADER *HttpHeader;\r
643 HTTP_BOOT_PRIVATE_DATA *Private;\r
644 UINT32 Percentage;\r
645\r
646 Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This);\r
647\r
648 switch (DataType) {\r
649 case HttpBootDhcp4:\r
650 case HttpBootDhcp6:\r
651 Print (L".");\r
652 break;\r
653\r
654 case HttpBootHttpRequest:\r
655 if (Data != NULL) {\r
656 HttpMessage = (EFI_HTTP_MESSAGE *) Data;\r
657 if (HttpMessage->Data.Request->Method == HttpMethodGet &&\r
658 HttpMessage->Data.Request->Url != NULL) {\r
659 Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url);\r
660 }\r
661 }\r
662 break;\r
663\r
664 case HttpBootHttpResponse:\r
665 if (Data != NULL) {\r
666 HttpMessage = (EFI_HTTP_MESSAGE *) Data;\r
667 HttpHeader = HttpFindHeader (\r
668 HttpMessage->HeaderCount,\r
669 HttpMessage->Headers,\r
670 HTTP_HEADER_CONTENT_LENGTH\r
671 );\r
672 if (HttpHeader != NULL) {\r
673 Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);\r
674 Private->ReceivedSize = 0;\r
675 Private->Percentage = 0;\r
676 }\r
677 }\r
678 break;\r
679\r
680 case HttpBootHttpEntityBody:\r
681 if (DataLength != 0) {\r
682 if (Private->FileSize != 0) {\r
683 //\r
684 // We already know the file size, print in percentage format.\r
685 //\r
686 if (Private->ReceivedSize == 0) {\r
687 Print (L" File Size: %lu\n", Private->FileSize);\r
688 }\r
689 Private->ReceivedSize += DataLength;\r
690 Percentage = (UINT32) DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);\r
691 if (Private->Percentage != Percentage) {\r
692 Private->Percentage = Percentage;\r
693 Print (L"\r Downloading...%d%%", Percentage);\r
694 }\r
695 } else {\r
696 //\r
697 // In some case we couldn't get the file size from the HTTP header, so we\r
698 // just print the downloaded file size.\r
699 //\r
700 Private->ReceivedSize += DataLength;\r
701 Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);\r
702 }\r
703 }\r
704 break;\r
705\r
706 default:\r
707 break;\r
708 };\r
709\r
710 return EFI_SUCCESS;\r
711}\r
712\r
713///\r
714/// HTTP Boot Callback Protocol instance\r
715///\r
716GLOBAL_REMOVE_IF_UNREFERENCED \r
717EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {\r
718 HttpBootCallback\r
719};\r