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