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
':') {
58 if (Uri
[Index
] >= L
'A' && Uri
[Index
] <= L
'Z') {
59 Uri
[Index
] -= (CHAR16
)(L
'A' - L
'a');
64 // Only accept empty URI, or http and https URI.
66 if ((StrLen (Uri
) != 0) && (StrnCmp (Uri
, L
"http://", 7) != 0) && (StrnCmp (Uri
, L
"https://", 8) != 0)) {
67 return EFI_INVALID_PARAMETER
;
71 // Create a new device path by appending the IP node and URI node to
72 // the driver's parent device path
75 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
77 Status
= EFI_OUT_OF_RESOURCES
;
80 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
81 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
82 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
84 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
86 Status
= EFI_OUT_OF_RESOURCES
;
89 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
90 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
91 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
93 TmpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
95 if (TmpDevicePath
== NULL
) {
96 return EFI_OUT_OF_RESOURCES
;
99 // Update the URI node with the input boot file URI.
101 UnicodeStrToAsciiStrS (Uri
, AsciiUri
, sizeof (AsciiUri
));
102 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (AsciiUri
);
103 Node
= AllocatePool (Length
);
105 Status
= EFI_OUT_OF_RESOURCES
;
106 FreePool (TmpDevicePath
);
109 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
110 Node
->DevPath
.SubType
= MSG_URI_DP
;
111 SetDevicePathNodeLength (Node
, Length
);
112 CopyMem ((UINT8
*) Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
), AsciiUri
, AsciiStrSize (AsciiUri
));
113 NewDevicePath
= AppendDevicePathNode (TmpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
115 FreePool (TmpDevicePath
);
116 if (NewDevicePath
== NULL
) {
117 Status
= EFI_OUT_OF_RESOURCES
;
122 // Add a new load option.
124 Status
= EfiBootManagerInitializeLoadOption (
126 LoadOptionNumberUnassigned
,
134 if (EFI_ERROR (Status
)) {
138 Status
= EfiBootManagerAddLoadOptionVariable (&NewOption
, (UINTN
) -1);
139 EfiBootManagerFreeLoadOption (&NewOption
);
143 if (NewDevicePath
!= NULL
) {
144 FreePool (NewDevicePath
);
152 This function allows the caller to request the current
153 configuration for one or more named elements. The resulting
154 string is in <ConfigAltResp> format. Also, any and all alternative
155 configuration strings shall be appended to the end of the
156 current configuration string. If they are, they must appear
157 after the current configuration. They must contain the same
158 routing (GUID, NAME, PATH) as the current configuration string.
159 They must have an additional description indicating the type of
160 alternative configuration the string represents,
161 "ALTCFG=<StringToken>". That <StringToken> (when
162 converted from Hex UNICODE to binary) is a reference to a
163 string in the associated string pack.
165 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
167 @param[in] Request A null-terminated Unicode string in
168 <ConfigRequest> format. Note that this
169 includes the routing information as well as
170 the configurable name / value pairs. It is
171 invalid for this string to be in
172 <MultiConfigRequest> format.
174 @param[out] Progress On return, points to a character in the
175 Request string. Points to the string's null
176 terminator if request was successful. Points
177 to the most recent "&" before the first
178 failing name / value pair (or the beginning
179 of the string if the failure is in the first
180 name / value pair) if the request was not successful.
182 @param[out] Results A null-terminated Unicode string in
183 <ConfigAltResp> format which has all values
184 filled in for the names in the Request string.
185 String to be allocated by the called function.
187 @retval EFI_SUCCESS The Results string is filled with the
188 values corresponding to all requested
191 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
192 parts of the results that must be
193 stored awaiting possible future
196 @retval EFI_INVALID_PARAMETER For example, passing in a NULL
197 for the Request parameter
198 would result in this type of
199 error. In this case, the
200 Progress parameter would be
203 @retval EFI_NOT_FOUND Routing data doesn't match any
204 known driver. Progress set to the
205 first character in the routing header.
206 Note: There is no requirement that the
207 driver validate the routing data. It
208 must skip the <ConfigHdr> in order to
211 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
212 to most recent "&" before the
213 error or the beginning of the
216 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
217 to the & before the name in
223 HttpBootFormExtractConfig (
224 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
225 IN CONST EFI_STRING Request
,
226 OUT EFI_STRING
*Progress
,
227 OUT EFI_STRING
*Results
232 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
233 EFI_STRING ConfigRequestHdr
;
234 EFI_STRING ConfigRequest
;
235 BOOLEAN AllocatedRequest
;
238 if (Progress
== NULL
|| Results
== NULL
) {
239 return EFI_INVALID_PARAMETER
;
243 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
244 return EFI_NOT_FOUND
;
247 ConfigRequestHdr
= NULL
;
248 ConfigRequest
= NULL
;
249 AllocatedRequest
= FALSE
;
252 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
254 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
256 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
257 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
258 StrCpyS (CallbackInfo
->HttpBootNvData
.Description
, DESCRIPTION_STR_MAX_SIZE
/ sizeof (CHAR16
), HTTP_BOOT_DEFAULT_DESCRIPTION_STR
);
260 ConfigRequest
= Request
;
261 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
263 // Request has no request element, construct full request string.
264 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
265 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
267 ConfigRequestHdr
= HiiConstructConfigHdr (&gHttpBootConfigGuid
, mHttpBootConfigStorageName
, CallbackInfo
->ChildHandle
);
268 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
269 ConfigRequest
= AllocateZeroPool (Size
);
270 if (ConfigRequest
== NULL
) {
271 return EFI_OUT_OF_RESOURCES
;
273 AllocatedRequest
= TRUE
;
274 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
275 FreePool (ConfigRequestHdr
);
278 Status
= gHiiConfigRouting
->BlockToConfig (
281 (UINT8
*) &CallbackInfo
->HttpBootNvData
,
288 // Free the allocated config request string.
290 if (AllocatedRequest
) {
291 FreePool (ConfigRequest
);
292 ConfigRequest
= NULL
;
295 // Set Progress string to the original request string.
297 if (Request
== NULL
) {
299 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
300 *Progress
= Request
+ StrLen (Request
);
308 This function applies changes in a driver's configuration.
309 Input is a Configuration, which has the routing data for this
310 driver followed by name / value configuration pairs. The driver
311 must apply those pairs to its configurable storage. If the
312 driver's configuration is stored in a linear block of data
313 and the driver's name / value pairs are in <BlockConfig>
314 format, it may use the ConfigToBlock helper function (above) to
317 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
319 @param[in] Configuration A null-terminated Unicode string in
320 <ConfigString> format.
322 @param[out] Progress A pointer to a string filled in with the
323 offset of the most recent '&' before the
324 first failing name / value pair (or the
325 beginning of the string if the failure
326 is in the first name / value pair) or
327 the terminating NULL if all was
330 @retval EFI_SUCCESS The results have been distributed or are
331 awaiting distribution.
333 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
334 parts of the results that must be
335 stored awaiting possible future
338 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
339 Results parameter would result
340 in this type of error.
342 @retval EFI_NOT_FOUND Target for the specified routing data
348 HttpBootFormRouteConfig (
349 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
350 IN CONST EFI_STRING Configuration
,
351 OUT EFI_STRING
*Progress
356 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
357 HTTP_BOOT_PRIVATE_DATA
*Private
;
359 if (Progress
== NULL
) {
360 return EFI_INVALID_PARAMETER
;
362 *Progress
= Configuration
;
364 if (Configuration
== NULL
) {
365 return EFI_INVALID_PARAMETER
;
369 // Check routing data in <ConfigHdr>.
370 // Note: there is no name for Name/Value storage, only GUID will be checked
372 if (!HiiIsConfigHdrMatch (Configuration
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
373 return EFI_NOT_FOUND
;
376 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
377 Private
= HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_INFO (CallbackInfo
);
379 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
380 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
382 Status
= gHiiConfigRouting
->ConfigToBlock (
385 (UINT8
*) &CallbackInfo
->HttpBootNvData
,
389 if (EFI_ERROR (Status
)) {
394 // Create a new boot option according to the configuration data.
396 HttpBootAddBootOption (
398 (CallbackInfo
->HttpBootNvData
.IpVersion
== HTTP_BOOT_IP_VERSION_6
) ? TRUE
: FALSE
,
399 CallbackInfo
->HttpBootNvData
.Description
,
400 CallbackInfo
->HttpBootNvData
.Uri
408 This function is called to provide results data to the driver.
409 This data consists of a unique key that is used to identify
410 which data is either being passed back or being asked for.
412 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
413 @param[in] Action Specifies the type of action taken by the browser.
414 @param[in] QuestionId A unique value which is sent to the original
415 exporting driver so that it can identify the type
416 of data to expect. The format of the data tends to
417 vary based on the opcode that generated the callback.
418 @param[in] Type The type of value for the question.
419 @param[in, out] Value A pointer to the data being sent to the original
421 @param[out] ActionRequest On return, points to the action requested by the
424 @retval EFI_SUCCESS The callback successfully handled the action.
425 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
426 variable and its data.
427 @retval EFI_DEVICE_ERROR The variable could not be saved.
428 @retval EFI_UNSUPPORTED The specified Action is not supported by the
433 HttpBootFormCallback (
434 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
435 IN EFI_BROWSER_ACTION Action
,
436 IN EFI_QUESTION_ID QuestionId
,
438 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
439 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
446 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
452 Status
= EFI_SUCCESS
;
454 if (This
== NULL
|| Value
== NULL
) {
455 return EFI_INVALID_PARAMETER
;
458 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
460 if (Action
!= EFI_BROWSER_ACTION_CHANGING
) {
461 return EFI_UNSUPPORTED
;
464 switch (QuestionId
) {
465 case KEY_INITIATOR_URI
:
467 // Get user input URI string
469 Uri
= HiiGetString (CallbackInfo
->RegisteredHandle
, Value
->string
, NULL
);
471 return EFI_INVALID_PARAMETER
;
475 // The URI should be either an empty string (for corporate environment) ,or http(s) for home environment.
476 // Pop up a message box for the unsupported URI.
478 if (StrLen (Uri
) != 0) {
479 UriLen
= StrLen (Uri
) + 1;
480 AsciiUri
= AllocateZeroPool (UriLen
);
481 if (AsciiUri
== NULL
) {
483 return EFI_OUT_OF_RESOURCES
;
486 UnicodeStrToAsciiStrS (Uri
, AsciiUri
, UriLen
);
488 Status
= HttpBootCheckUriScheme (AsciiUri
);
490 if (Status
== EFI_INVALID_PARAMETER
) {
492 DEBUG ((EFI_D_ERROR
, "HttpBootFormCallback: %r.\n", Status
));
495 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
497 L
"ERROR: Unsupported URI!",
498 L
"Only supports HTTP and HTTPS",
501 } else if (Status
== EFI_ACCESS_DENIED
) {
503 DEBUG ((EFI_D_ERROR
, "HttpBootFormCallback: %r.\n", Status
));
506 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
508 L
"ERROR: Unsupported URI!",
519 if (AsciiUri
!= NULL
) {
533 Initialize the configuration form.
535 @param[in] Private Pointer to the driver private data.
537 @retval EFI_SUCCESS The configuration form is initialized.
538 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
542 HttpBootConfigFormInit (
543 IN HTTP_BOOT_PRIVATE_DATA
*Private
547 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
548 VENDOR_DEVICE_PATH VendorDeviceNode
;
550 CHAR16
*OldMenuString
;
551 CHAR16 MenuString
[128];
553 CallbackInfo
= &Private
->CallbackInfo
;
555 if (CallbackInfo
->Initilized
) {
559 CallbackInfo
->Signature
= HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE
;
562 // Construct device path node for EFI HII Config Access protocol,
563 // which consists of controller physical device path and one hardware
566 ZeroMem (&VendorDeviceNode
, sizeof (VENDOR_DEVICE_PATH
));
567 VendorDeviceNode
.Header
.Type
= HARDWARE_DEVICE_PATH
;
568 VendorDeviceNode
.Header
.SubType
= HW_VENDOR_DP
;
569 CopyGuid (&VendorDeviceNode
.Guid
, &gEfiCallerIdGuid
);
570 SetDevicePathNodeLength (&VendorDeviceNode
.Header
, sizeof (VENDOR_DEVICE_PATH
));
571 CallbackInfo
->HiiVendorDevicePath
= AppendDevicePathNode (
572 Private
->ParentDevicePath
,
573 (EFI_DEVICE_PATH_PROTOCOL
*) &VendorDeviceNode
575 if (CallbackInfo
->HiiVendorDevicePath
== NULL
) {
576 Status
= EFI_OUT_OF_RESOURCES
;
580 CallbackInfo
->ConfigAccess
.ExtractConfig
= HttpBootFormExtractConfig
;
581 CallbackInfo
->ConfigAccess
.RouteConfig
= HttpBootFormRouteConfig
;
582 CallbackInfo
->ConfigAccess
.Callback
= HttpBootFormCallback
;
585 // Install Device Path Protocol and Config Access protocol to driver handle.
587 Status
= gBS
->InstallMultipleProtocolInterfaces (
588 &CallbackInfo
->ChildHandle
,
589 &gEfiDevicePathProtocolGuid
,
590 CallbackInfo
->HiiVendorDevicePath
,
591 &gEfiHiiConfigAccessProtocolGuid
,
592 &CallbackInfo
->ConfigAccess
,
595 if (EFI_ERROR (Status
)) {
600 // Publish our HII data.
602 CallbackInfo
->RegisteredHandle
= HiiAddPackages (
603 &gHttpBootConfigGuid
,
604 CallbackInfo
->ChildHandle
,
606 HttpBootConfigVfrBin
,
609 if (CallbackInfo
->RegisteredHandle
== NULL
) {
610 Status
= EFI_OUT_OF_RESOURCES
;
615 // Append MAC string in the menu help string
617 Status
= NetLibGetMacString (Private
->Controller
, NULL
, &MacString
);
618 if (!EFI_ERROR (Status
)) {
619 OldMenuString
= HiiGetString (
620 CallbackInfo
->RegisteredHandle
,
621 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
624 UnicodeSPrint (MenuString
, 128, L
"%s (MAC:%s)", OldMenuString
, MacString
);
626 CallbackInfo
->RegisteredHandle
,
627 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
632 FreePool (MacString
);
633 FreePool (OldMenuString
);
635 CallbackInfo
->Initilized
= TRUE
;
641 HttpBootConfigFormUnload (Private
);
646 Unload the configuration form, this includes: delete all the configuration
647 entries, uninstall the form callback protocol, and free the resources used.
648 The form will only be unload completely when both IP4 and IP6 stack are stopped.
650 @param[in] Private Pointer to the driver private data.
652 @retval EFI_SUCCESS The configuration form is unloaded.
653 @retval Others Failed to unload the form.
657 HttpBootConfigFormUnload (
658 IN HTTP_BOOT_PRIVATE_DATA
*Private
661 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
663 if (Private
->Ip4Nic
!= NULL
|| Private
->Ip6Nic
!= NULL
) {
665 // Only unload the configuration form when both IP4 and IP6 stack are stopped.
670 CallbackInfo
= &Private
->CallbackInfo
;
671 if (CallbackInfo
->ChildHandle
!= NULL
) {
673 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
675 gBS
->UninstallMultipleProtocolInterfaces (
676 CallbackInfo
->ChildHandle
,
677 &gEfiDevicePathProtocolGuid
,
678 CallbackInfo
->HiiVendorDevicePath
,
679 &gEfiHiiConfigAccessProtocolGuid
,
680 &CallbackInfo
->ConfigAccess
,
683 CallbackInfo
->ChildHandle
= NULL
;
686 if (CallbackInfo
->HiiVendorDevicePath
!= NULL
) {
687 FreePool (CallbackInfo
->HiiVendorDevicePath
);
688 CallbackInfo
->HiiVendorDevicePath
= NULL
;
691 if (CallbackInfo
->RegisteredHandle
!= NULL
) {
693 // Remove HII package list
695 HiiRemovePackages (CallbackInfo
->RegisteredHandle
);
696 CallbackInfo
->RegisteredHandle
= NULL
;