2 Helper functions for configuring or getting the parameters relating to HTTP Boot.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "HttpBootDxe.h"
16 #include <Library/UefiBootManagerLib.h>
18 CHAR16 mHttpBootConfigStorageName
[] = L
"HTTP_BOOT_CONFIG_IFR_NVDATA";
21 Add new boot option for HTTP boot.
23 @param[in] Private Pointer to the driver private data.
24 @param[in] UsingIpv6 Set to TRUE if creating boot option for IPv6.
25 @param[in] Description The description text of the boot option.
26 @param[in] Uri The URI string of the boot file.
28 @retval EFI_SUCCESS The boot option is created successfully.
29 @retval Others Failed to create new boot option.
33 HttpBootAddBootOption (
34 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
36 IN CHAR16
*Description
,
41 EFI_DEVICE_PATH_PROTOCOL
*TmpDevicePath
;
42 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
44 CHAR8 AsciiUri
[URI_STR_MAX_SIZE
];
47 EFI_BOOT_MANAGER_LOAD_OPTION NewOption
;
53 if (StrLen (Description
) == 0) {
54 return EFI_INVALID_PARAMETER
;
58 // Convert the scheme to all lower case.
60 for (Index
= 0; Index
< StrLen (Uri
); Index
++) {
61 if (Uri
[Index
] == L
':') {
64 if (Uri
[Index
] >= L
'A' && Uri
[Index
] <= L
'Z') {
65 Uri
[Index
] -= (CHAR16
)(L
'A' - L
'a');
70 // Only accept empty URI, or http and https URI.
72 if ((StrLen (Uri
) != 0) && (StrnCmp (Uri
, L
"http://", 7) != 0) && (StrnCmp (Uri
, L
"https://", 8) != 0)) {
73 return EFI_INVALID_PARAMETER
;
77 // Create a new device path by appending the IP node and URI node to
78 // the driver's parent device path
81 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
83 Status
= EFI_OUT_OF_RESOURCES
;
86 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
87 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
88 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
90 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
92 Status
= EFI_OUT_OF_RESOURCES
;
95 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
96 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
97 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
99 TmpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
101 if (TmpDevicePath
== NULL
) {
102 return EFI_OUT_OF_RESOURCES
;
105 // Update the URI node with the input boot file URI.
107 UnicodeStrToAsciiStr (Uri
, AsciiUri
);
108 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (AsciiUri
);
109 Node
= AllocatePool (Length
);
111 Status
= EFI_OUT_OF_RESOURCES
;
112 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 if (EFI_ERROR (Status
)) {
146 EfiBootManagerFreeLoadOption (&NewOption
);
151 if (NewDevicePath
!= NULL
) {
152 FreePool (NewDevicePath
);
160 This function allows the caller to request the current
161 configuration for one or more named elements. The resulting
162 string is in <ConfigAltResp> format. Also, any and all alternative
163 configuration strings shall be appended to the end of the
164 current configuration string. If they are, they must appear
165 after the current configuration. They must contain the same
166 routing (GUID, NAME, PATH) as the current configuration string.
167 They must have an additional description indicating the type of
168 alternative configuration the string represents,
169 "ALTCFG=<StringToken>". That <StringToken> (when
170 converted from Hex UNICODE to binary) is a reference to a
171 string in the associated string pack.
173 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
175 @param[in] Request A null-terminated Unicode string in
176 <ConfigRequest> format. Note that this
177 includes the routing information as well as
178 the configurable name / value pairs. It is
179 invalid for this string to be in
180 <MultiConfigRequest> format.
182 @param[out] Progress On return, points to a character in the
183 Request string. Points to the string's null
184 terminator if request was successful. Points
185 to the most recent "&" before the first
186 failing name / value pair (or the beginning
187 of the string if the failure is in the first
188 name / value pair) if the request was not successful.
190 @param[out] Results A null-terminated Unicode string in
191 <ConfigAltResp> format which has all values
192 filled in for the names in the Request string.
193 String to be allocated by the called function.
195 @retval EFI_SUCCESS The Results string is filled with the
196 values corresponding to all requested
199 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
200 parts of the results that must be
201 stored awaiting possible future
204 @retval EFI_INVALID_PARAMETER For example, passing in a NULL
205 for the Request parameter
206 would result in this type of
207 error. In this case, the
208 Progress parameter would be
211 @retval EFI_NOT_FOUND Routing data doesn't match any
212 known driver. Progress set to the
213 first character in the routing header.
214 Note: There is no requirement that the
215 driver validate the routing data. It
216 must skip the <ConfigHdr> in order to
219 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
220 to most recent "&" before the
221 error or the beginning of the
224 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
225 to the & before the name in
231 HttpBootFormExtractConfig (
232 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
233 IN CONST EFI_STRING Request
,
234 OUT EFI_STRING
*Progress
,
235 OUT EFI_STRING
*Results
240 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
241 EFI_STRING ConfigRequestHdr
;
242 EFI_STRING ConfigRequest
;
243 BOOLEAN AllocatedRequest
;
246 if (Progress
== NULL
|| Results
== NULL
) {
247 return EFI_INVALID_PARAMETER
;
251 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
252 return EFI_NOT_FOUND
;
255 ConfigRequestHdr
= NULL
;
256 ConfigRequest
= NULL
;
257 AllocatedRequest
= FALSE
;
260 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
262 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
264 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
265 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
266 StrCpyS (CallbackInfo
->HttpBootNvData
.Description
, DESCRIPTION_STR_MAX_SIZE
/ sizeof (CHAR16
), HTTP_BOOT_DEFAULT_DESCRIPTION_STR
);
268 ConfigRequest
= Request
;
269 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
271 // Request has no request element, construct full request string.
272 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
273 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
275 ConfigRequestHdr
= HiiConstructConfigHdr (&gHttpBootConfigGuid
, mHttpBootConfigStorageName
, CallbackInfo
->ChildHandle
);
276 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
277 ConfigRequest
= AllocateZeroPool (Size
);
278 ASSERT (ConfigRequest
!= NULL
);
279 AllocatedRequest
= TRUE
;
280 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
281 FreePool (ConfigRequestHdr
);
284 Status
= gHiiConfigRouting
->BlockToConfig (
287 (UINT8
*) &CallbackInfo
->HttpBootNvData
,
294 // Free the allocated config request string.
296 if (AllocatedRequest
) {
297 FreePool (ConfigRequest
);
298 ConfigRequest
= NULL
;
301 // Set Progress string to the original request string.
303 if (Request
== NULL
) {
305 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
306 *Progress
= Request
+ StrLen (Request
);
314 This function applies changes in a driver's configuration.
315 Input is a Configuration, which has the routing data for this
316 driver followed by name / value configuration pairs. The driver
317 must apply those pairs to its configurable storage. If the
318 driver's configuration is stored in a linear block of data
319 and the driver's name / value pairs are in <BlockConfig>
320 format, it may use the ConfigToBlock helper function (above) to
323 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
325 @param[in] Configuration A null-terminated Unicode string in
326 <ConfigString> format.
328 @param[out] Progress A pointer to a string filled in with the
329 offset of the most recent '&' before the
330 first failing name / value pair (or the
331 beginning of the string if the failure
332 is in the first name / value pair) or
333 the terminating NULL if all was
336 @retval EFI_SUCCESS The results have been distributed or are
337 awaiting distribution.
339 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
340 parts of the results that must be
341 stored awaiting possible future
344 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
345 Results parameter would result
346 in this type of error.
348 @retval EFI_NOT_FOUND Target for the specified routing data
354 HttpBootFormRouteConfig (
355 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
356 IN CONST EFI_STRING Configuration
,
357 OUT EFI_STRING
*Progress
362 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
363 HTTP_BOOT_PRIVATE_DATA
*Private
;
365 if (Progress
== NULL
) {
366 return EFI_INVALID_PARAMETER
;
368 *Progress
= Configuration
;
370 if (Configuration
== NULL
) {
371 return EFI_INVALID_PARAMETER
;
375 // Check routing data in <ConfigHdr>.
376 // Note: there is no name for Name/Value storage, only GUID will be checked
378 if (!HiiIsConfigHdrMatch (Configuration
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
379 return EFI_NOT_FOUND
;
382 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
383 Private
= HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_INFO (CallbackInfo
);
385 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
386 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
388 Status
= gHiiConfigRouting
->ConfigToBlock (
391 (UINT8
*) &CallbackInfo
->HttpBootNvData
,
395 if (EFI_ERROR (Status
)) {
400 // Create a new boot option according to the configuration data.
402 HttpBootAddBootOption (
404 (CallbackInfo
->HttpBootNvData
.IpVersion
== HTTP_BOOT_IP_VERSION_6
) ? TRUE
: FALSE
,
405 CallbackInfo
->HttpBootNvData
.Description
,
406 CallbackInfo
->HttpBootNvData
.Uri
414 This function is called to provide results data to the driver.
415 This data consists of a unique key that is used to identify
416 which data is either being passed back or being asked for.
418 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
419 @param[in] Action Specifies the type of action taken by the browser.
420 @param[in] QuestionId A unique value which is sent to the original
421 exporting driver so that it can identify the type
422 of data to expect. The format of the data tends to
423 vary based on the opcode that generated the callback.
424 @param[in] Type The type of value for the question.
425 @param[in, out] Value A pointer to the data being sent to the original
427 @param[out] ActionRequest On return, points to the action requested by the
430 @retval EFI_SUCCESS The callback successfully handled the action.
431 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
432 variable and its data.
433 @retval EFI_DEVICE_ERROR The variable could not be saved.
434 @retval EFI_UNSUPPORTED The specified Action is not supported by the
439 HttpBootFormCallback (
440 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
441 IN EFI_BROWSER_ACTION Action
,
442 IN EFI_QUESTION_ID QuestionId
,
444 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
445 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
451 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
453 if (This
== NULL
|| Value
== NULL
) {
454 return EFI_INVALID_PARAMETER
;
457 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
459 if (Action
!= EFI_BROWSER_ACTION_CHANGING
) {
460 return EFI_UNSUPPORTED
;
463 switch (QuestionId
) {
464 case KEY_INITIATOR_URI
:
466 // Get user input URI string
468 Uri
= HiiGetString (CallbackInfo
->RegisteredHandle
, Value
->string
, NULL
);
469 ASSERT (Uri
!= NULL
);
471 return EFI_UNSUPPORTED
;
475 // Convert the scheme to all lower case.
477 for (Index
= 0; Index
< StrLen (Uri
); Index
++) {
478 if (Uri
[Index
] == L
':') {
481 if (Uri
[Index
] >= L
'A' && Uri
[Index
] <= L
'Z') {
482 Uri
[Index
] -= (CHAR16
)(L
'A' - L
'a');
487 // Set the converted URI string back
489 HiiSetString (CallbackInfo
->RegisteredHandle
, Value
->string
, Uri
, NULL
);
492 // The URI should be either an empty string (for corporate environment) ,or http(s) for home environment.
493 // Pop up a message box for other unsupported URI.
495 if ((StrLen (Uri
) != 0) && (StrnCmp (Uri
, L
"http://", 7) != 0) && (StrnCmp (Uri
, L
"https://", 8) != 0)) {
497 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
499 L
"ERROR: Unsupported URI!",
500 L
"Only supports HTTP and HTTPS",
516 Initialize the configuration form.
518 @param[in] Private Pointer to the driver private data.
520 @retval EFI_SUCCESS The configuration form is initialized.
521 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
525 HttpBootConfigFormInit (
526 IN HTTP_BOOT_PRIVATE_DATA
*Private
530 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
531 VENDOR_DEVICE_PATH VendorDeviceNode
;
533 CHAR16
*OldMenuString
;
534 CHAR16 MenuString
[128];
536 CallbackInfo
= &Private
->CallbackInfo
;
538 if (CallbackInfo
->Initilized
) {
542 CallbackInfo
->Signature
= HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE
;
545 // Construct device path node for EFI HII Config Access protocol,
546 // which consists of controller physical device path and one hardware
549 ZeroMem (&VendorDeviceNode
, sizeof (VENDOR_DEVICE_PATH
));
550 VendorDeviceNode
.Header
.Type
= HARDWARE_DEVICE_PATH
;
551 VendorDeviceNode
.Header
.SubType
= HW_VENDOR_DP
;
552 CopyGuid (&VendorDeviceNode
.Guid
, &gEfiCallerIdGuid
);
553 SetDevicePathNodeLength (&VendorDeviceNode
.Header
, sizeof (VENDOR_DEVICE_PATH
));
554 CallbackInfo
->HiiVendorDevicePath
= AppendDevicePathNode (
555 Private
->ParentDevicePath
,
556 (EFI_DEVICE_PATH_PROTOCOL
*) &VendorDeviceNode
558 if (CallbackInfo
->HiiVendorDevicePath
== NULL
) {
559 Status
= EFI_OUT_OF_RESOURCES
;
563 CallbackInfo
->ConfigAccess
.ExtractConfig
= HttpBootFormExtractConfig
;
564 CallbackInfo
->ConfigAccess
.RouteConfig
= HttpBootFormRouteConfig
;
565 CallbackInfo
->ConfigAccess
.Callback
= HttpBootFormCallback
;
568 // Install Device Path Protocol and Config Access protocol to driver handle.
570 Status
= gBS
->InstallMultipleProtocolInterfaces (
571 &CallbackInfo
->ChildHandle
,
572 &gEfiDevicePathProtocolGuid
,
573 CallbackInfo
->HiiVendorDevicePath
,
574 &gEfiHiiConfigAccessProtocolGuid
,
575 &CallbackInfo
->ConfigAccess
,
578 if (EFI_ERROR (Status
)) {
583 // Publish our HII data.
585 CallbackInfo
->RegisteredHandle
= HiiAddPackages (
586 &gHttpBootConfigGuid
,
587 CallbackInfo
->ChildHandle
,
589 HttpBootConfigVfrBin
,
592 if (CallbackInfo
->RegisteredHandle
== NULL
) {
593 Status
= EFI_OUT_OF_RESOURCES
;
598 // Append MAC string in the menu help string
600 Status
= NetLibGetMacString (Private
->Controller
, NULL
, &MacString
);
601 if (!EFI_ERROR (Status
)) {
602 OldMenuString
= HiiGetString (
603 CallbackInfo
->RegisteredHandle
,
604 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
607 UnicodeSPrint (MenuString
, 128, L
"%s (MAC:%s)", OldMenuString
, MacString
);
609 CallbackInfo
->RegisteredHandle
,
610 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
615 FreePool (MacString
);
616 FreePool (OldMenuString
);
618 CallbackInfo
->Initilized
= TRUE
;
624 HttpBootConfigFormUnload (Private
);
629 Unload the configuration form, this includes: delete all the configuration
630 entries, uninstall the form callback protocol, and free the resources used.
631 The form will only be unload completely when both IP4 and IP6 stack are stopped.
633 @param[in] Private Pointer to the driver private data.
635 @retval EFI_SUCCESS The configuration form is unloaded.
636 @retval Others Failed to unload the form.
640 HttpBootConfigFormUnload (
641 IN HTTP_BOOT_PRIVATE_DATA
*Private
644 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
646 if (Private
->Ip4Nic
!= NULL
|| Private
->Ip6Nic
!= NULL
) {
648 // Only unload the configuration form when both IP4 and IP6 stack are stopped.
653 CallbackInfo
= &Private
->CallbackInfo
;
654 if (CallbackInfo
->ChildHandle
!= NULL
) {
656 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
658 gBS
->UninstallMultipleProtocolInterfaces (
659 CallbackInfo
->ChildHandle
,
660 &gEfiDevicePathProtocolGuid
,
661 CallbackInfo
->HiiVendorDevicePath
,
662 &gEfiHiiConfigAccessProtocolGuid
,
663 &CallbackInfo
->ConfigAccess
,
666 CallbackInfo
->ChildHandle
= NULL
;
669 if (CallbackInfo
->HiiVendorDevicePath
!= NULL
) {
670 FreePool (CallbackInfo
->HiiVendorDevicePath
);
671 CallbackInfo
->HiiVendorDevicePath
= NULL
;
674 if (CallbackInfo
->RegisteredHandle
!= NULL
) {
676 // Remove HII package list
678 HiiRemovePackages (CallbackInfo
->RegisteredHandle
);
679 CallbackInfo
->RegisteredHandle
= NULL
;