]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/HttpBootDxe/HttpBootImpl.c
NetworkPkg: Update Api from NetLibDetectMedia to NetLibDetectMediaWaitTimeout.
[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
fca04738 330 AsciiPrint ("\n Error: Could not discover the boot information for DHCP server.\n");\r
95b5c32f 331 goto ON_EXIT;\r
d933e70a
JW
332 }\r
333 }\r
334\r
335 if (!Private->HttpCreated) {\r
336 //\r
337 // Create HTTP child.\r
338 //\r
339 Status = HttpBootCreateHttpIo (Private);\r
340 if (EFI_ERROR (Status)) {\r
95b5c32f 341 goto ON_EXIT;\r
d933e70a
JW
342 }\r
343 }\r
344\r
345 if (Private->BootFileSize == 0) {\r
346 //\r
347 // Discover the information about the bootfile if we haven't.\r
348 //\r
349\r
350 //\r
351 // Try to use HTTP HEAD method.\r
352 //\r
353 Status = HttpBootGetBootFile (\r
354 Private,\r
355 TRUE,\r
356 &Private->BootFileSize,\r
587d204c 357 NULL,\r
44a7d08b 358 &Private->ImageType\r
d933e70a
JW
359 );\r
360 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
361 //\r
362 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.\r
363 //\r
364 ASSERT (Private->BootFileSize == 0);\r
365 Status = HttpBootGetBootFile (\r
366 Private,\r
367 FALSE,\r
368 &Private->BootFileSize,\r
587d204c 369 NULL,\r
44a7d08b 370 &Private->ImageType\r
d933e70a
JW
371 );\r
372 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
fca04738 373 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");\r
95b5c32f 374 goto ON_EXIT;\r
d933e70a
JW
375 }\r
376 }\r
377 }\r
378\r
379 if (*BufferSize < Private->BootFileSize) {\r
380 *BufferSize = Private->BootFileSize;\r
44a7d08b 381 *ImageType = Private->ImageType;\r
95b5c32f
FS
382 Status = EFI_BUFFER_TOO_SMALL;\r
383 goto ON_EXIT;\r
d933e70a
JW
384 }\r
385\r
386 //\r
387 // Load the boot file into Buffer\r
388 //\r
95b5c32f
FS
389 Status = HttpBootGetBootFile (\r
390 Private,\r
391 FALSE,\r
392 BufferSize,\r
393 Buffer,\r
394 ImageType\r
395 );\r
396 \r
397ON_EXIT:\r
398 HttpBootUninstallCallback (Private);\r
fca04738
FS
399\r
400 if (Status == EFI_ACCESS_DENIED) {\r
401 AsciiPrint ("\n Error: Could not establish connection with HTTP server.\n");\r
402 } else if (Status == EFI_BUFFER_TOO_SMALL && Buffer != NULL) {\r
403 AsciiPrint ("\n Error: Buffer size is smaller than the requested file.\n");\r
404 } else if (Status == EFI_OUT_OF_RESOURCES) {\r
405 AsciiPrint ("\n Error: Could not allocate I/O buffers.\n");\r
406 } else if (Status == EFI_DEVICE_ERROR) {\r
407 AsciiPrint ("\n Error: Network device error.\n");\r
408 } else if (Status == EFI_TIMEOUT) {\r
409 AsciiPrint ("\n Error: Server response timeout.\n");\r
410 } else if (Status == EFI_ABORTED) {\r
411 AsciiPrint ("\n Error: Remote boot cancelled.\n");\r
412 } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
413 AsciiPrint ("\n Error: Unexpected network error.\n");\r
414 }\r
95b5c32f 415 return Status;\r
d933e70a
JW
416}\r
417\r
418/**\r
419 Disable the use of UEFI HTTP boot function.\r
420\r
421 @param[in] Private The pointer to the driver's private data.\r
422\r
423 @retval EFI_SUCCESS HTTP boot was successfully disabled.\r
424 @retval EFI_NOT_STARTED The driver is already in stopped state.\r
425 @retval EFI_INVALID_PARAMETER Private is NULL.\r
426 @retval Others Unexpected error when stop the function.\r
427 \r
428**/\r
429EFI_STATUS\r
430HttpBootStop (\r
431 IN HTTP_BOOT_PRIVATE_DATA *Private\r
432 )\r
433{\r
434 UINTN Index;\r
435\r
436 if (Private == NULL) {\r
437 return EFI_INVALID_PARAMETER;\r
438 }\r
439 \r
440 if (!Private->Started) {\r
441 return EFI_NOT_STARTED;\r
442 }\r
443 \r
444 if (Private->HttpCreated) {\r
445 HttpIoDestroyIo (&Private->HttpIo);\r
446 Private->HttpCreated = FALSE;\r
447 }\r
448 \r
449 Private->Started = FALSE;\r
450 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
451 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
452 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
453 Private->Port = 0;\r
454 Private->BootFileUri = NULL;\r
455 Private->BootFileUriParser = NULL;\r
456 Private->BootFileSize = 0;\r
457 Private->SelectIndex = 0;\r
b659408b 458 Private->SelectProxyType = HttpOfferTypeMax; \r
d933e70a
JW
459\r
460 if (!Private->UsingIpv6) {\r
461 //\r
462 // Stop and release the DHCP4 child.\r
463 //\r
464 Private->Dhcp4->Stop (Private->Dhcp4);\r
465 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
466\r
467 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
468 if (Private->OfferBuffer[Index].Dhcp4.UriParser) {\r
469 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);\r
470 }\r
471 }\r
472 } else {\r
b659408b
ZL
473 //\r
474 // Stop and release the DHCP6 child.\r
475 //\r
476 Private->Dhcp6->Stop (Private->Dhcp6);\r
477 Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
478 \r
479 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
480 if (Private->OfferBuffer[Index].Dhcp6.UriParser) {\r
481 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);\r
482 }\r
483 }\r
d933e70a 484 }\r
fa848a40 485\r
7b1dbd15
JW
486 if (Private->DnsServerIp != NULL) {\r
487 FreePool (Private->DnsServerIp);\r
488 Private->DnsServerIp = NULL;\r
489 }\r
490\r
fa848a40
FS
491 if (Private->FilePathUri!= NULL) {\r
492 FreePool (Private->FilePathUri);\r
493 HttpUrlFreeParser (Private->FilePathUriParser);\r
494 Private->FilePathUri = NULL;\r
495 Private->FilePathUriParser = NULL;\r
496 }\r
d933e70a
JW
497 \r
498 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
499 Private->OfferNum = 0;\r
500 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
501 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
fa848a40
FS
502 \r
503 HttpBootFreeCacheList (Private);\r
504 \r
d933e70a
JW
505 return EFI_SUCCESS;\r
506}\r
507\r
508/**\r
509 Causes the driver to load a specified file.\r
510\r
511 @param This Protocol instance pointer.\r
512 @param FilePath The device specific path of the file to load.\r
513 @param BootPolicy If TRUE, indicates that the request originates from the\r
514 boot manager is attempting to load FilePath as a boot\r
515 selection. If FALSE, then FilePath must match as exact file\r
516 to be loaded.\r
517 @param BufferSize On input the size of Buffer in bytes. On output with a return\r
518 code of EFI_SUCCESS, the amount of data transferred to\r
519 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
520 the size of Buffer required to retrieve the requested file.\r
521 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
522 then the size of the requested file is returned in\r
523 BufferSize.\r
524\r
525 @retval EFI_SUCCESS The file was loaded.\r
526 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy\r
527 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
528 BufferSize is NULL.\r
529 @retval EFI_NO_MEDIA No medium was present to load the file.\r
530 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
531 @retval EFI_NO_RESPONSE The remote system did not respond.\r
532 @retval EFI_NOT_FOUND The file was not found.\r
533 @retval EFI_ABORTED The file load process was manually cancelled.\r
534 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
535 BufferSize has been updated with the size needed to complete\r
536 the request.\r
537\r
538**/\r
539EFI_STATUS\r
540EFIAPI\r
541HttpBootDxeLoadFile (\r
542 IN EFI_LOAD_FILE_PROTOCOL *This,\r
543 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
544 IN BOOLEAN BootPolicy,\r
545 IN OUT UINTN *BufferSize,\r
546 IN VOID *Buffer OPTIONAL\r
547 )\r
548{\r
549 HTTP_BOOT_PRIVATE_DATA *Private;\r
b659408b 550 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;\r
152f2d5e 551 EFI_STATUS MediaStatus;\r
b659408b 552 BOOLEAN UsingIpv6;\r
d933e70a 553 EFI_STATUS Status;\r
587d204c 554 HTTP_BOOT_IMAGE_TYPE ImageType;\r
d933e70a 555\r
fa848a40 556 if (This == NULL || BufferSize == NULL || FilePath == NULL) {\r
d933e70a
JW
557 return EFI_INVALID_PARAMETER;\r
558 }\r
559\r
560 //\r
561 // Only support BootPolicy\r
562 //\r
563 if (!BootPolicy) {\r
564 return EFI_UNSUPPORTED;\r
565 }\r
566\r
b659408b
ZL
567 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);\r
568 Private = VirtualNic->Private;\r
b659408b 569 \r
d933e70a
JW
570 //\r
571 // Check media status before HTTP boot start\r
572 //\r
152f2d5e 573 MediaStatus = EFI_SUCCESS;\r
574 NetLibDetectMediaWaitTimeout (Private->Controller, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME, &MediaStatus);\r
575 if (MediaStatus != EFI_SUCCESS) {\r
fca04738 576 AsciiPrint ("\n Error: Could not detect network connection.\n");\r
d933e70a
JW
577 return EFI_NO_MEDIA;\r
578 }\r
fa848a40 579 \r
b659408b
ZL
580 //\r
581 // Check whether the virtual nic is using IPv6 or not.\r
582 //\r
fa848a40 583 UsingIpv6 = FALSE;\r
b659408b
ZL
584 if (VirtualNic == Private->Ip6Nic) {\r
585 UsingIpv6 = TRUE;\r
586 }\r
fa848a40 587\r
d933e70a 588 //\r
fa848a40 589 // Initialize HTTP boot.\r
d933e70a 590 //\r
fa848a40 591 Status = HttpBootStart (Private, UsingIpv6, FilePath);\r
587d204c
FS
592 if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {\r
593 return Status;\r
b659408b 594 }\r
587d204c 595 \r
fa848a40
FS
596 //\r
597 // Load the boot file.\r
598 //\r
587d204c
FS
599 ImageType = ImageTypeMax;\r
600 Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);\r
601 if (EFI_ERROR (Status)) {\r
602 if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) {\r
603 Status = EFI_WARN_FILE_SYSTEM;\r
604 } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
605 HttpBootStop (Private);\r
606 }\r
607 return Status;\r
d933e70a
JW
608 }\r
609\r
587d204c
FS
610 //\r
611 // Register the RAM Disk to the system if needed.\r
612 //\r
613 if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {\r
614 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);\r
615 if (!EFI_ERROR (Status)) {\r
616 Status = EFI_WARN_FILE_SYSTEM;\r
fca04738
FS
617 } else {\r
618 AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");\r
b659408b 619 }\r
d933e70a 620 }\r
287f05cd
FS
621\r
622 //\r
623 // Stop the HTTP Boot service after the boot image is downloaded.\r
624 //\r
625 HttpBootStop (Private);\r
d933e70a
JW
626 return Status;\r
627}\r
628\r
629///\r
630/// Load File Protocol instance\r
631///\r
632GLOBAL_REMOVE_IF_UNREFERENCED \r
633EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {\r
634 HttpBootDxeLoadFile\r
635};\r
95b5c32f
FS
636\r
637/**\r
638 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a\r
639 packet.\r
640\r
641 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.\r
642 Parameters DataType and Received specify the type of event and the format of the buffer pointed\r
643 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not\r
644 execute for more than 5 ms.\r
645 The returned status code determines the behavior of the HTTP Boot driver.\r
646\r
647 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.\r
648 @param[in] DataType The event that occurs in the current state.\r
649 @param[in] Received TRUE if the callback is being invoked due to a receive event.\r
650 FALSE if the callback is being invoked due to a transmit event.\r
651 @param[in] DataLength The length in bytes of the buffer pointed to by Data.\r
652 @param[in] Data A pointer to the buffer of data, the data type is specified by\r
653 DataType.\r
654 \r
655 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.\r
656 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.\r
657**/\r
658EFI_STATUS\r
3858c58a 659EFIAPI\r
95b5c32f
FS
660HttpBootCallback (\r
661 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,\r
662 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,\r
663 IN BOOLEAN Received,\r
664 IN UINT32 DataLength,\r
665 IN VOID *Data OPTIONAL\r
666 )\r
667{\r
668 EFI_HTTP_MESSAGE *HttpMessage;\r
669 EFI_HTTP_HEADER *HttpHeader;\r
670 HTTP_BOOT_PRIVATE_DATA *Private;\r
671 UINT32 Percentage;\r
672\r
673 Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This);\r
674\r
675 switch (DataType) {\r
676 case HttpBootDhcp4:\r
677 case HttpBootDhcp6:\r
678 Print (L".");\r
679 break;\r
680\r
681 case HttpBootHttpRequest:\r
682 if (Data != NULL) {\r
683 HttpMessage = (EFI_HTTP_MESSAGE *) Data;\r
684 if (HttpMessage->Data.Request->Method == HttpMethodGet &&\r
685 HttpMessage->Data.Request->Url != NULL) {\r
686 Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url);\r
687 }\r
688 }\r
689 break;\r
690\r
691 case HttpBootHttpResponse:\r
692 if (Data != NULL) {\r
693 HttpMessage = (EFI_HTTP_MESSAGE *) Data;\r
bb4831c0
FS
694 \r
695 if (HttpMessage->Data.Response != NULL) {\r
696 if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {\r
697 //\r
698 // Server indicates the resource has been redirected to a different URL\r
699 // according to the section 6.4 of RFC7231 and the RFC 7538.\r
700 // Display the redirect information on the screen.\r
701 //\r
702 HttpHeader = HttpFindHeader (\r
703 HttpMessage->HeaderCount,\r
704 HttpMessage->Headers,\r
705 HTTP_HEADER_LOCATION\r
706 );\r
707 if (HttpHeader != NULL) {\r
708 Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue);\r
709 }\r
710 }\r
711 }\r
712 \r
95b5c32f
FS
713 HttpHeader = HttpFindHeader (\r
714 HttpMessage->HeaderCount,\r
715 HttpMessage->Headers,\r
716 HTTP_HEADER_CONTENT_LENGTH\r
717 );\r
718 if (HttpHeader != NULL) {\r
719 Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);\r
720 Private->ReceivedSize = 0;\r
721 Private->Percentage = 0;\r
722 }\r
723 }\r
724 break;\r
725\r
726 case HttpBootHttpEntityBody:\r
727 if (DataLength != 0) {\r
728 if (Private->FileSize != 0) {\r
729 //\r
730 // We already know the file size, print in percentage format.\r
731 //\r
732 if (Private->ReceivedSize == 0) {\r
bb4831c0 733 Print (L" File Size: %lu Bytes\n", Private->FileSize);\r
95b5c32f
FS
734 }\r
735 Private->ReceivedSize += DataLength;\r
736 Percentage = (UINT32) DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);\r
737 if (Private->Percentage != Percentage) {\r
738 Private->Percentage = Percentage;\r
739 Print (L"\r Downloading...%d%%", Percentage);\r
740 }\r
741 } else {\r
742 //\r
743 // In some case we couldn't get the file size from the HTTP header, so we\r
744 // just print the downloaded file size.\r
745 //\r
746 Private->ReceivedSize += DataLength;\r
747 Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);\r
748 }\r
749 }\r
750 break;\r
751\r
752 default:\r
753 break;\r
754 };\r
755\r
756 return EFI_SUCCESS;\r
757}\r
758\r
759///\r
760/// HTTP Boot Callback Protocol instance\r
761///\r
762GLOBAL_REMOVE_IF_UNREFERENCED \r
763EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {\r
764 HttpBootCallback\r
765};\r