2 Helper functions for configuring or getting the parameters relating to HTTP Boot.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "HttpBootDxe.h"
10 #include <Library/UefiBootManagerLib.h>
12 CHAR16 mHttpBootConfigStorageName
[] = L
"HTTP_BOOT_CONFIG_IFR_NVDATA";
15 Add new boot option for HTTP boot.
17 @param[in] Private Pointer to the driver private data.
18 @param[in] UsingIpv6 Set to TRUE if creating boot option for IPv6.
19 @param[in] Description The description text of the boot option.
20 @param[in] Uri The URI string of the boot file.
22 @retval EFI_SUCCESS The boot option is created successfully.
23 @retval Others Failed to create new boot option.
27 HttpBootAddBootOption (
28 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
30 IN CHAR16
*Description
,
35 EFI_DEVICE_PATH_PROTOCOL
*TmpDevicePath
;
36 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
38 CHAR8 AsciiUri
[URI_STR_MAX_SIZE
];
41 EFI_BOOT_MANAGER_LOAD_OPTION NewOption
;
47 if (StrLen (Description
) == 0) {
48 return EFI_INVALID_PARAMETER
;
52 // Convert the scheme to all lower case.
54 for (Index
= 0; Index
< StrLen (Uri
); Index
++) {
55 if (Uri
[Index
] == L
':') {
59 if ((Uri
[Index
] >= L
'A') && (Uri
[Index
] <= L
'Z')) {
60 Uri
[Index
] -= (CHAR16
)(L
'A' - L
'a');
65 // Only accept empty URI, or http and https URI.
67 if ((StrLen (Uri
) != 0) && (StrnCmp (Uri
, L
"http://", 7) != 0) && (StrnCmp (Uri
, L
"https://", 8) != 0)) {
68 return EFI_INVALID_PARAMETER
;
72 // Create a new device path by appending the IP node and URI node to
73 // the driver's parent device path
76 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
78 Status
= EFI_OUT_OF_RESOURCES
;
82 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
83 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
84 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
86 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
88 Status
= EFI_OUT_OF_RESOURCES
;
92 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
93 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
94 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
97 TmpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)Node
);
99 if (TmpDevicePath
== NULL
) {
100 return EFI_OUT_OF_RESOURCES
;
104 // Update the URI node with the input boot file URI.
106 UnicodeStrToAsciiStrS (Uri
, AsciiUri
, sizeof (AsciiUri
));
107 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (AsciiUri
);
108 Node
= AllocatePool (Length
);
110 Status
= EFI_OUT_OF_RESOURCES
;
111 FreePool (TmpDevicePath
);
115 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
116 Node
->DevPath
.SubType
= MSG_URI_DP
;
117 SetDevicePathNodeLength (Node
, Length
);
118 CopyMem ((UINT8
*)Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
), AsciiUri
, AsciiStrSize (AsciiUri
));
119 NewDevicePath
= AppendDevicePathNode (TmpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)Node
);
121 FreePool (TmpDevicePath
);
122 if (NewDevicePath
== NULL
) {
123 Status
= EFI_OUT_OF_RESOURCES
;
128 // Add a new load option.
130 Status
= EfiBootManagerInitializeLoadOption (
132 LoadOptionNumberUnassigned
,
140 if (EFI_ERROR (Status
)) {
144 Status
= EfiBootManagerAddLoadOptionVariable (&NewOption
, (UINTN
)-1);
145 EfiBootManagerFreeLoadOption (&NewOption
);
149 if (NewDevicePath
!= NULL
) {
150 FreePool (NewDevicePath
);
158 This function allows the caller to request the current
159 configuration for one or more named elements. The resulting
160 string is in <ConfigAltResp> format. Also, any and all alternative
161 configuration strings shall be appended to the end of the
162 current configuration string. If they are, they must appear
163 after the current configuration. They must contain the same
164 routing (GUID, NAME, PATH) as the current configuration string.
165 They must have an additional description indicating the type of
166 alternative configuration the string represents,
167 "ALTCFG=<StringToken>". That <StringToken> (when
168 converted from Hex UNICODE to binary) is a reference to a
169 string in the associated string pack.
171 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
173 @param[in] Request A null-terminated Unicode string in
174 <ConfigRequest> format. Note that this
175 includes the routing information as well as
176 the configurable name / value pairs. It is
177 invalid for this string to be in
178 <MultiConfigRequest> format.
180 @param[out] Progress On return, points to a character in the
181 Request string. Points to the string's null
182 terminator if request was successful. Points
183 to the most recent "&" before the first
184 failing name / value pair (or the beginning
185 of the string if the failure is in the first
186 name / value pair) if the request was not successful.
188 @param[out] Results A null-terminated Unicode string in
189 <ConfigAltResp> format which has all values
190 filled in for the names in the Request string.
191 String to be allocated by the called function.
193 @retval EFI_SUCCESS The Results string is filled with the
194 values corresponding to all requested
197 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
198 parts of the results that must be
199 stored awaiting possible future
202 @retval EFI_INVALID_PARAMETER For example, passing in a NULL
203 for the Request parameter
204 would result in this type of
205 error. In this case, the
206 Progress parameter would be
209 @retval EFI_NOT_FOUND Routing data doesn't match any
210 known driver. Progress set to the
211 first character in the routing header.
212 Note: There is no requirement that the
213 driver validate the routing data. It
214 must skip the <ConfigHdr> in order to
217 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
218 to most recent "&" before the
219 error or the beginning of the
222 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
223 to the & before the name in
229 HttpBootFormExtractConfig (
230 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
231 IN CONST EFI_STRING Request
,
232 OUT EFI_STRING
*Progress
,
233 OUT EFI_STRING
*Results
238 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
239 EFI_STRING ConfigRequestHdr
;
240 EFI_STRING ConfigRequest
;
241 BOOLEAN AllocatedRequest
;
244 if ((Progress
== NULL
) || (Results
== NULL
)) {
245 return EFI_INVALID_PARAMETER
;
249 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
250 return EFI_NOT_FOUND
;
253 ConfigRequestHdr
= NULL
;
254 ConfigRequest
= NULL
;
255 AllocatedRequest
= FALSE
;
258 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
260 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
262 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
263 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
264 StrCpyS (CallbackInfo
->HttpBootNvData
.Description
, DESCRIPTION_STR_MAX_SIZE
/ sizeof (CHAR16
), HTTP_BOOT_DEFAULT_DESCRIPTION_STR
);
266 ConfigRequest
= Request
;
267 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
269 // Request has no request element, construct full request string.
270 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
271 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
273 ConfigRequestHdr
= HiiConstructConfigHdr (&gHttpBootConfigGuid
, mHttpBootConfigStorageName
, CallbackInfo
->ChildHandle
);
274 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
275 ConfigRequest
= AllocateZeroPool (Size
);
276 if (ConfigRequest
== NULL
) {
277 return EFI_OUT_OF_RESOURCES
;
280 AllocatedRequest
= TRUE
;
281 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
282 FreePool (ConfigRequestHdr
);
285 Status
= gHiiConfigRouting
->BlockToConfig (
288 (UINT8
*)&CallbackInfo
->HttpBootNvData
,
295 // Free the allocated config request string.
297 if (AllocatedRequest
) {
298 FreePool (ConfigRequest
);
299 ConfigRequest
= NULL
;
303 // Set Progress string to the original request string.
305 if (Request
== NULL
) {
307 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
308 *Progress
= Request
+ StrLen (Request
);
316 This function applies changes in a driver's configuration.
317 Input is a Configuration, which has the routing data for this
318 driver followed by name / value configuration pairs. The driver
319 must apply those pairs to its configurable storage. If the
320 driver's configuration is stored in a linear block of data
321 and the driver's name / value pairs are in <BlockConfig>
322 format, it may use the ConfigToBlock helper function (above) to
325 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
327 @param[in] Configuration A null-terminated Unicode string in
328 <ConfigString> format.
330 @param[out] Progress A pointer to a string filled in with the
331 offset of the most recent '&' before the
332 first failing name / value pair (or the
333 beginning of the string if the failure
334 is in the first name / value pair) or
335 the terminating NULL if all was
338 @retval EFI_SUCCESS The results have been distributed or are
339 awaiting distribution.
341 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
342 parts of the results that must be
343 stored awaiting possible future
346 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
347 Results parameter would result
348 in this type of error.
350 @retval EFI_NOT_FOUND Target for the specified routing data
356 HttpBootFormRouteConfig (
357 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
358 IN CONST EFI_STRING Configuration
,
359 OUT EFI_STRING
*Progress
364 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
365 HTTP_BOOT_PRIVATE_DATA
*Private
;
367 if (Progress
== NULL
) {
368 return EFI_INVALID_PARAMETER
;
371 *Progress
= Configuration
;
373 if (Configuration
== NULL
) {
374 return EFI_INVALID_PARAMETER
;
378 // Check routing data in <ConfigHdr>.
379 // Note: there is no name for Name/Value storage, only GUID will be checked
381 if (!HiiIsConfigHdrMatch (Configuration
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
382 return EFI_NOT_FOUND
;
385 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
386 Private
= HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_INFO (CallbackInfo
);
388 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
389 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
391 Status
= gHiiConfigRouting
->ConfigToBlock (
394 (UINT8
*)&CallbackInfo
->HttpBootNvData
,
398 if (EFI_ERROR (Status
)) {
403 // Create a new boot option according to the configuration data.
405 HttpBootAddBootOption (
407 (CallbackInfo
->HttpBootNvData
.IpVersion
== HTTP_BOOT_IP_VERSION_6
) ? TRUE
: FALSE
,
408 CallbackInfo
->HttpBootNvData
.Description
,
409 CallbackInfo
->HttpBootNvData
.Uri
417 This function is called to provide results data to the driver.
418 This data consists of a unique key that is used to identify
419 which data is either being passed back or being asked for.
421 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
422 @param[in] Action Specifies the type of action taken by the browser.
423 @param[in] QuestionId A unique value which is sent to the original
424 exporting driver so that it can identify the type
425 of data to expect. The format of the data tends to
426 vary based on the opcode that generated the callback.
427 @param[in] Type The type of value for the question.
428 @param[in, out] Value A pointer to the data being sent to the original
430 @param[out] ActionRequest On return, points to the action requested by the
433 @retval EFI_SUCCESS The callback successfully handled the action.
434 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
435 variable and its data.
436 @retval EFI_DEVICE_ERROR The variable could not be saved.
437 @retval EFI_UNSUPPORTED The specified Action is not supported by the
442 HttpBootFormCallback (
443 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
444 IN EFI_BROWSER_ACTION Action
,
445 IN EFI_QUESTION_ID QuestionId
,
447 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
448 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
455 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
461 Status
= EFI_SUCCESS
;
463 if ((This
== NULL
) || (Value
== NULL
)) {
464 return EFI_INVALID_PARAMETER
;
467 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
469 if (Action
!= EFI_BROWSER_ACTION_CHANGING
) {
470 return EFI_UNSUPPORTED
;
473 switch (QuestionId
) {
474 case KEY_INITIATOR_URI
:
476 // Get user input URI string
478 Uri
= HiiGetString (CallbackInfo
->RegisteredHandle
, Value
->string
, NULL
);
480 return EFI_INVALID_PARAMETER
;
484 // The URI should be either an empty string (for corporate environment) ,or http(s) for home environment.
485 // Pop up a message box for the unsupported URI.
487 if (StrLen (Uri
) != 0) {
488 UriLen
= StrLen (Uri
) + 1;
489 AsciiUri
= AllocateZeroPool (UriLen
);
490 if (AsciiUri
== NULL
) {
492 return EFI_OUT_OF_RESOURCES
;
495 UnicodeStrToAsciiStrS (Uri
, AsciiUri
, UriLen
);
497 Status
= HttpBootCheckUriScheme (AsciiUri
);
499 if (Status
== EFI_INVALID_PARAMETER
) {
500 DEBUG ((DEBUG_ERROR
, "HttpBootFormCallback: %r.\n", Status
));
503 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
505 L
"ERROR: Unsupported URI!",
506 L
"Only supports HTTP and HTTPS",
509 } else if (Status
== EFI_ACCESS_DENIED
) {
510 DEBUG ((DEBUG_ERROR
, "HttpBootFormCallback: %r.\n", Status
));
513 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
515 L
"ERROR: Unsupported URI!",
526 if (AsciiUri
!= NULL
) {
540 Initialize the configuration form.
542 @param[in] Private Pointer to the driver private data.
544 @retval EFI_SUCCESS The configuration form is initialized.
545 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
549 HttpBootConfigFormInit (
550 IN HTTP_BOOT_PRIVATE_DATA
*Private
554 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
555 VENDOR_DEVICE_PATH VendorDeviceNode
;
557 CHAR16
*OldMenuString
;
558 CHAR16 MenuString
[128];
560 CallbackInfo
= &Private
->CallbackInfo
;
562 if (CallbackInfo
->Initialized
) {
566 CallbackInfo
->Signature
= HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE
;
569 // Construct device path node for EFI HII Config Access protocol,
570 // which consists of controller physical device path and one hardware
573 ZeroMem (&VendorDeviceNode
, sizeof (VENDOR_DEVICE_PATH
));
574 VendorDeviceNode
.Header
.Type
= HARDWARE_DEVICE_PATH
;
575 VendorDeviceNode
.Header
.SubType
= HW_VENDOR_DP
;
576 CopyGuid (&VendorDeviceNode
.Guid
, &gEfiCallerIdGuid
);
577 SetDevicePathNodeLength (&VendorDeviceNode
.Header
, sizeof (VENDOR_DEVICE_PATH
));
578 CallbackInfo
->HiiVendorDevicePath
= AppendDevicePathNode (
579 Private
->ParentDevicePath
,
580 (EFI_DEVICE_PATH_PROTOCOL
*)&VendorDeviceNode
582 if (CallbackInfo
->HiiVendorDevicePath
== NULL
) {
583 Status
= EFI_OUT_OF_RESOURCES
;
587 CallbackInfo
->ConfigAccess
.ExtractConfig
= HttpBootFormExtractConfig
;
588 CallbackInfo
->ConfigAccess
.RouteConfig
= HttpBootFormRouteConfig
;
589 CallbackInfo
->ConfigAccess
.Callback
= HttpBootFormCallback
;
592 // Install Device Path Protocol and Config Access protocol to driver handle.
594 Status
= gBS
->InstallMultipleProtocolInterfaces (
595 &CallbackInfo
->ChildHandle
,
596 &gEfiDevicePathProtocolGuid
,
597 CallbackInfo
->HiiVendorDevicePath
,
598 &gEfiHiiConfigAccessProtocolGuid
,
599 &CallbackInfo
->ConfigAccess
,
602 if (EFI_ERROR (Status
)) {
607 // Publish our HII data.
609 CallbackInfo
->RegisteredHandle
= HiiAddPackages (
610 &gHttpBootConfigGuid
,
611 CallbackInfo
->ChildHandle
,
613 HttpBootConfigVfrBin
,
616 if (CallbackInfo
->RegisteredHandle
== NULL
) {
617 Status
= EFI_OUT_OF_RESOURCES
;
622 // Append MAC string in the menu help string
624 Status
= NetLibGetMacString (Private
->Controller
, NULL
, &MacString
);
625 if (!EFI_ERROR (Status
)) {
626 OldMenuString
= HiiGetString (
627 CallbackInfo
->RegisteredHandle
,
628 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
631 UnicodeSPrint (MenuString
, 128, L
"%s (MAC:%s)", OldMenuString
, MacString
);
633 CallbackInfo
->RegisteredHandle
,
634 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
639 FreePool (MacString
);
640 FreePool (OldMenuString
);
642 CallbackInfo
->Initialized
= TRUE
;
648 HttpBootConfigFormUnload (Private
);
653 Unload the configuration form, this includes: delete all the configuration
654 entries, uninstall the form callback protocol, and free the resources used.
655 The form will only be unload completely when both IP4 and IP6 stack are stopped.
657 @param[in] Private Pointer to the driver private data.
659 @retval EFI_SUCCESS The configuration form is unloaded.
660 @retval Others Failed to unload the form.
664 HttpBootConfigFormUnload (
665 IN HTTP_BOOT_PRIVATE_DATA
*Private
668 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
670 if ((Private
->Ip4Nic
!= NULL
) || (Private
->Ip6Nic
!= NULL
)) {
672 // Only unload the configuration form when both IP4 and IP6 stack are stopped.
677 CallbackInfo
= &Private
->CallbackInfo
;
678 if (CallbackInfo
->ChildHandle
!= NULL
) {
680 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
682 gBS
->UninstallMultipleProtocolInterfaces (
683 CallbackInfo
->ChildHandle
,
684 &gEfiDevicePathProtocolGuid
,
685 CallbackInfo
->HiiVendorDevicePath
,
686 &gEfiHiiConfigAccessProtocolGuid
,
687 &CallbackInfo
->ConfigAccess
,
690 CallbackInfo
->ChildHandle
= NULL
;
693 if (CallbackInfo
->HiiVendorDevicePath
!= NULL
) {
694 FreePool (CallbackInfo
->HiiVendorDevicePath
);
695 CallbackInfo
->HiiVendorDevicePath
= NULL
;
698 if (CallbackInfo
->RegisteredHandle
!= NULL
) {
700 // Remove HII package list
702 HiiRemovePackages (CallbackInfo
->RegisteredHandle
);
703 CallbackInfo
->RegisteredHandle
= NULL
;