]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - NetworkPkg/HttpBootDxe/HttpBootImpl.c
ArmPkg/LinuxLoader: eliminate calls to deprecated string functions
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootImpl.c
... / ...
CommitLineData
1/** @file\r
2 The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.\r
3\r
4Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
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
18/**\r
19 Enable the use of UEFI HTTP boot function.\r
20\r
21 If the driver has already been started but not satisfy the requirement (IP stack and \r
22 specified boot file path), this function will stop the driver and start it again.\r
23\r
24 @param[in] Private The pointer to the driver's private data.\r
25 @param[in] UsingIpv6 Specifies the type of IP addresses that are to be\r
26 used during the session that is being started.\r
27 Set to TRUE for IPv6, and FALSE for IPv4.\r
28 @param[in] FilePath The device specific path of the file to load.\r
29\r
30 @retval EFI_SUCCESS HTTP boot was successfully enabled.\r
31 @retval EFI_INVALID_PARAMETER Private is NULL or FilePath is NULL.\r
32 @retval EFI_INVALID_PARAMETER The FilePath doesn't contain a valid URI device path node.\r
33 @retval EFI_ALREADY_STARTED The driver is already in started state.\r
34 @retval EFI_OUT_OF_RESOURCES There are not enough resources.\r
35 \r
36**/\r
37EFI_STATUS\r
38HttpBootStart (\r
39 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
40 IN BOOLEAN UsingIpv6,\r
41 IN EFI_DEVICE_PATH_PROTOCOL *FilePath\r
42 )\r
43{\r
44 UINTN Index;\r
45 EFI_STATUS Status;\r
46 CHAR8 *Uri;\r
47 \r
48\r
49 if (Private == NULL || FilePath == NULL) {\r
50 return EFI_INVALID_PARAMETER;\r
51 }\r
52 \r
53 //\r
54 // Check the URI in the input FilePath, in order to see whether it is\r
55 // required to boot from a new specified boot file. \r
56 //\r
57 Status = HttpBootParseFilePath (FilePath, &Uri);\r
58 if (EFI_ERROR (Status)) {\r
59 return EFI_INVALID_PARAMETER;\r
60 }\r
61 \r
62 //\r
63 // Check whether we need to stop and restart the HTTP boot driver.\r
64 //\r
65 if (Private->Started) {\r
66 //\r
67 // Restart is needed in 2 cases:\r
68 // 1. Http boot driver has already been started but not on the required IP stack.\r
69 // 2. The specified boot file URI in FilePath is different with the one we have\r
70 // recorded before.\r
71 //\r
72 if ((UsingIpv6 != Private->UsingIpv6) || \r
73 ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0))) {\r
74 //\r
75 // Restart is required, first stop then continue this start function.\r
76 //\r
77 Status = HttpBootStop (Private);\r
78 if (EFI_ERROR (Status)) {\r
79 return Status;\r
80 }\r
81 } else {\r
82 //\r
83 // Restart is not required.\r
84 //\r
85 if (Uri != NULL) {\r
86 FreePool (Uri);\r
87 }\r
88 return EFI_ALREADY_STARTED;\r
89 }\r
90 }\r
91\r
92 //\r
93 // Detect whether using ipv6 or not, and set it to the private data.\r
94 //\r
95 if (UsingIpv6 && Private->Ip6Nic != NULL) {\r
96 Private->UsingIpv6 = TRUE;\r
97 } else if (!UsingIpv6 && Private->Ip4Nic != NULL) {\r
98 Private->UsingIpv6 = FALSE;\r
99 } else {\r
100 if (Uri != NULL) {\r
101 FreePool (Uri);\r
102 }\r
103 return EFI_UNSUPPORTED;\r
104 }\r
105\r
106 //\r
107 // Record the specified URI and prepare the URI parser if needed.\r
108 //\r
109 Private->FilePathUri = Uri;\r
110 if (Private->FilePathUri != NULL) {\r
111 Status = HttpParseUrl (\r
112 Private->FilePathUri,\r
113 (UINT32) AsciiStrLen (Private->FilePathUri),\r
114 FALSE,\r
115 &Private->FilePathUriParser\r
116 );\r
117 if (EFI_ERROR (Status)) {\r
118 FreePool (Private->FilePathUri);\r
119 return Status;\r
120 }\r
121 }\r
122 \r
123 //\r
124 // Init the content of cached DHCP offer list.\r
125 //\r
126 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
127 if (!Private->UsingIpv6) {\r
128 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
129 Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_BOOT_DHCP4_PACKET_MAX_SIZE;\r
130 }\r
131 } else {\r
132 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
133 Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_BOOT_DHCP6_PACKET_MAX_SIZE;\r
134 }\r
135 }\r
136\r
137 if (Private->UsingIpv6) {\r
138 //\r
139 // Set Ip6 policy to Automatic to start the Ip6 router discovery.\r
140 //\r
141 Status = HttpBootSetIp6Policy (Private);\r
142 if (EFI_ERROR (Status)) {\r
143 return Status;\r
144 }\r
145 }\r
146 Private->Started = TRUE;\r
147\r
148 return EFI_SUCCESS;\r
149}\r
150\r
151/**\r
152 Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.\r
153\r
154 @param[in] Private The pointer to the driver's private data.\r
155\r
156 @retval EFI_SUCCESS Boot info was successfully retrieved.\r
157 @retval EFI_INVALID_PARAMETER Private is NULL.\r
158 @retval EFI_NOT_STARTED The driver is in stopped state.\r
159 @retval EFI_DEVICE_ERROR An unexpected network error occurred.\r
160 @retval Others Other errors as indicated.\r
161 \r
162**/\r
163EFI_STATUS\r
164HttpBootDhcp (\r
165 IN HTTP_BOOT_PRIVATE_DATA *Private\r
166 )\r
167{\r
168 EFI_STATUS Status;\r
169\r
170 if (Private == NULL) {\r
171 return EFI_INVALID_PARAMETER;\r
172 }\r
173 \r
174 if (!Private->Started) {\r
175 return EFI_NOT_STARTED;\r
176 }\r
177\r
178 Status = EFI_DEVICE_ERROR;\r
179\r
180 if (!Private->UsingIpv6) {\r
181 //\r
182 // Start D.O.R.A process to get a IPv4 address and other boot information.\r
183 //\r
184 Status = HttpBootDhcp4Dora (Private);\r
185 } else {\r
186 //\r
187 // Start S.A.R.R process to get a IPv6 address and other boot information.\r
188 //\r
189 Status = HttpBootDhcp6Sarr (Private);\r
190 }\r
191\r
192 return Status;\r
193}\r
194\r
195/**\r
196 Attempt to download the boot file through HTTP message exchange.\r
197\r
198 @param[in] Private The pointer to the driver's private data.\r
199 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return\r
200 code of EFI_SUCCESS, the amount of data transferred to\r
201 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
202 the size of Buffer required to retrieve the requested file.\r
203 @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,\r
204 then the size of the requested file is returned in\r
205 BufferSize.\r
206 @param[out] ImageType The image type of the downloaded file.\r
207\r
208 @retval EFI_SUCCESS Boot file was loaded successfully.\r
209 @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL.\r
210 @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL.\r
211 @retval EFI_NOT_STARTED The driver is in stopped state.\r
212 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has \r
213 been updated with the size needed to complete the request.\r
214 @retval EFI_DEVICE_ERROR An unexpected network error occurred.\r
215 @retval Others Other errors as indicated.\r
216 \r
217**/\r
218EFI_STATUS\r
219HttpBootLoadFile (\r
220 IN HTTP_BOOT_PRIVATE_DATA *Private,\r
221 IN OUT UINTN *BufferSize,\r
222 IN VOID *Buffer, OPTIONAL\r
223 OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r
224 )\r
225{\r
226 EFI_STATUS Status;\r
227\r
228 if (Private == NULL || ImageType == NULL || BufferSize == NULL ) {\r
229 return EFI_INVALID_PARAMETER;\r
230 }\r
231\r
232 if (*BufferSize != 0 && Buffer == NULL) {\r
233 return EFI_INVALID_PARAMETER;\r
234 }\r
235 \r
236 if (!Private->Started) {\r
237 return EFI_NOT_STARTED;\r
238 }\r
239\r
240 Status = EFI_DEVICE_ERROR;\r
241\r
242 if (Private->BootFileUri == NULL) {\r
243 //\r
244 // Parse the cached offer to get the boot file URL first.\r
245 //\r
246 Status = HttpBootDiscoverBootInfo (Private);\r
247 if (EFI_ERROR (Status)) {\r
248 return Status;\r
249 }\r
250 }\r
251\r
252 if (!Private->HttpCreated) {\r
253 //\r
254 // Create HTTP child.\r
255 //\r
256 Status = HttpBootCreateHttpIo (Private);\r
257 if (EFI_ERROR (Status)) {\r
258 return Status;\r
259 }\r
260 }\r
261\r
262 if (Private->BootFileSize == 0) {\r
263 //\r
264 // Discover the information about the bootfile if we haven't.\r
265 //\r
266\r
267 //\r
268 // Try to use HTTP HEAD method.\r
269 //\r
270 Status = HttpBootGetBootFile (\r
271 Private,\r
272 TRUE,\r
273 &Private->BootFileSize,\r
274 NULL,\r
275 &Private->ImageType\r
276 );\r
277 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
278 //\r
279 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.\r
280 //\r
281 ASSERT (Private->BootFileSize == 0);\r
282 Status = HttpBootGetBootFile (\r
283 Private,\r
284 FALSE,\r
285 &Private->BootFileSize,\r
286 NULL,\r
287 &Private->ImageType\r
288 );\r
289 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
290 return Status;\r
291 }\r
292 }\r
293 }\r
294\r
295 if (*BufferSize < Private->BootFileSize) {\r
296 *BufferSize = Private->BootFileSize;\r
297 *ImageType = Private->ImageType;\r
298 return EFI_BUFFER_TOO_SMALL;\r
299 }\r
300\r
301 //\r
302 // Load the boot file into Buffer\r
303 //\r
304 return HttpBootGetBootFile (\r
305 Private,\r
306 FALSE,\r
307 BufferSize,\r
308 Buffer,\r
309 ImageType\r
310 );\r
311}\r
312\r
313/**\r
314 Disable the use of UEFI HTTP boot function.\r
315\r
316 @param[in] Private The pointer to the driver's private data.\r
317\r
318 @retval EFI_SUCCESS HTTP boot was successfully disabled.\r
319 @retval EFI_NOT_STARTED The driver is already in stopped state.\r
320 @retval EFI_INVALID_PARAMETER Private is NULL.\r
321 @retval Others Unexpected error when stop the function.\r
322 \r
323**/\r
324EFI_STATUS\r
325HttpBootStop (\r
326 IN HTTP_BOOT_PRIVATE_DATA *Private\r
327 )\r
328{\r
329 UINTN Index;\r
330\r
331 if (Private == NULL) {\r
332 return EFI_INVALID_PARAMETER;\r
333 }\r
334 \r
335 if (!Private->Started) {\r
336 return EFI_NOT_STARTED;\r
337 }\r
338 \r
339 if (Private->HttpCreated) {\r
340 HttpIoDestroyIo (&Private->HttpIo);\r
341 Private->HttpCreated = FALSE;\r
342 }\r
343 \r
344 Private->Started = FALSE;\r
345 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
346 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
347 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
348 Private->Port = 0;\r
349 Private->BootFileUri = NULL;\r
350 Private->BootFileUriParser = NULL;\r
351 Private->BootFileSize = 0;\r
352 Private->SelectIndex = 0;\r
353 Private->SelectProxyType = HttpOfferTypeMax; \r
354\r
355 if (!Private->UsingIpv6) {\r
356 //\r
357 // Stop and release the DHCP4 child.\r
358 //\r
359 Private->Dhcp4->Stop (Private->Dhcp4);\r
360 Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
361\r
362 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
363 if (Private->OfferBuffer[Index].Dhcp4.UriParser) {\r
364 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);\r
365 }\r
366 }\r
367 } else {\r
368 //\r
369 // Stop and release the DHCP6 child.\r
370 //\r
371 Private->Dhcp6->Stop (Private->Dhcp6);\r
372 Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
373 \r
374 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
375 if (Private->OfferBuffer[Index].Dhcp6.UriParser) {\r
376 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);\r
377 }\r
378 }\r
379 }\r
380\r
381 if (Private->FilePathUri!= NULL) {\r
382 FreePool (Private->FilePathUri);\r
383 HttpUrlFreeParser (Private->FilePathUriParser);\r
384 Private->FilePathUri = NULL;\r
385 Private->FilePathUriParser = NULL;\r
386 }\r
387 \r
388 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
389 Private->OfferNum = 0;\r
390 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
391 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
392 \r
393 HttpBootFreeCacheList (Private);\r
394 \r
395 return EFI_SUCCESS;\r
396}\r
397\r
398/**\r
399 Causes the driver to load a specified file.\r
400\r
401 @param This Protocol instance pointer.\r
402 @param FilePath The device specific path of the file to load.\r
403 @param BootPolicy If TRUE, indicates that the request originates from the\r
404 boot manager is attempting to load FilePath as a boot\r
405 selection. If FALSE, then FilePath must match as exact file\r
406 to be loaded.\r
407 @param BufferSize On input the size of Buffer in bytes. On output with a return\r
408 code of EFI_SUCCESS, the amount of data transferred to\r
409 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
410 the size of Buffer required to retrieve the requested file.\r
411 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,\r
412 then the size of the requested file is returned in\r
413 BufferSize.\r
414\r
415 @retval EFI_SUCCESS The file was loaded.\r
416 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy\r
417 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or\r
418 BufferSize is NULL.\r
419 @retval EFI_NO_MEDIA No medium was present to load the file.\r
420 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.\r
421 @retval EFI_NO_RESPONSE The remote system did not respond.\r
422 @retval EFI_NOT_FOUND The file was not found.\r
423 @retval EFI_ABORTED The file load process was manually cancelled.\r
424 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.\r
425 BufferSize has been updated with the size needed to complete\r
426 the request.\r
427\r
428**/\r
429EFI_STATUS\r
430EFIAPI\r
431HttpBootDxeLoadFile (\r
432 IN EFI_LOAD_FILE_PROTOCOL *This,\r
433 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
434 IN BOOLEAN BootPolicy,\r
435 IN OUT UINTN *BufferSize,\r
436 IN VOID *Buffer OPTIONAL\r
437 )\r
438{\r
439 HTTP_BOOT_PRIVATE_DATA *Private;\r
440 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;\r
441 BOOLEAN MediaPresent;\r
442 BOOLEAN UsingIpv6;\r
443 EFI_STATUS Status;\r
444 HTTP_BOOT_IMAGE_TYPE ImageType;\r
445\r
446 if (This == NULL || BufferSize == NULL || FilePath == NULL) {\r
447 return EFI_INVALID_PARAMETER;\r
448 }\r
449\r
450 //\r
451 // Only support BootPolicy\r
452 //\r
453 if (!BootPolicy) {\r
454 return EFI_UNSUPPORTED;\r
455 }\r
456\r
457 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);\r
458 Private = VirtualNic->Private;\r
459 \r
460 //\r
461 // Check media status before HTTP boot start\r
462 //\r
463 MediaPresent = TRUE;\r
464 NetLibDetectMedia (Private->Controller, &MediaPresent);\r
465 if (!MediaPresent) {\r
466 return EFI_NO_MEDIA;\r
467 }\r
468 \r
469 //\r
470 // Check whether the virtual nic is using IPv6 or not.\r
471 //\r
472 UsingIpv6 = FALSE;\r
473 if (VirtualNic == Private->Ip6Nic) {\r
474 UsingIpv6 = TRUE;\r
475 }\r
476\r
477 //\r
478 // Initialize HTTP boot.\r
479 //\r
480 Status = HttpBootStart (Private, UsingIpv6, FilePath);\r
481 if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {\r
482 return Status;\r
483 }\r
484 \r
485 //\r
486 // Load the boot file.\r
487 //\r
488 ImageType = ImageTypeMax;\r
489 Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);\r
490 if (EFI_ERROR (Status)) {\r
491 if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) {\r
492 Status = EFI_WARN_FILE_SYSTEM;\r
493 } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
494 HttpBootStop (Private);\r
495 }\r
496 return Status;\r
497 }\r
498\r
499 //\r
500 // Register the RAM Disk to the system if needed.\r
501 //\r
502 if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {\r
503 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);\r
504 if (!EFI_ERROR (Status)) {\r
505 Status = EFI_WARN_FILE_SYSTEM;\r
506 }\r
507 }\r
508\r
509 //\r
510 // Stop the HTTP Boot service after the boot image is downloaded.\r
511 //\r
512 HttpBootStop (Private);\r
513 return Status;\r
514}\r
515\r
516///\r
517/// Load File Protocol instance\r
518///\r
519GLOBAL_REMOVE_IF_UNREFERENCED \r
520EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {\r
521 HttpBootDxeLoadFile\r
522};\r