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"
17 CHAR16 mHttpBootConfigStorageName
[] = L
"HTTP_BOOT_CONFIG_IFR_NVDATA";
20 Add new boot option for HTTP boot.
22 @param[in] Private Pointer to the driver private data.
23 @param[in] UsingIpv6 Set to TRUE if creating boot option for IPv6.
24 @param[in] Description The description text of the boot option.
25 @param[in] Uri The URI string of the boot file.
27 @retval EFI_SUCCESS The boot option is created successfully.
28 @retval Others Failed to create new boot option.
32 HttpBootAddBootOption (
33 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
35 IN CHAR16
*Description
,
40 EFI_DEVICE_PATH_PROTOCOL
*TmpDevicePath
;
41 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
43 CHAR8 AsciiUri
[URI_STR_MAX_SIZE
];
49 UINT8
*TempByteBuffer
;
65 if (StrLen (Description
) == 0) {
66 return EFI_INVALID_PARAMETER
;
70 // Convert the scheme to all lower case.
72 for (Index
= 0; Index
< StrLen (Uri
); Index
++) {
73 if (Uri
[Index
] == L
':') {
76 if (Uri
[Index
] >= L
'A' && Uri
[Index
] <= L
'Z') {
77 Uri
[Index
] -= (CHAR16
)(L
'A' - L
'a');
82 // Only accept http and https URI.
84 if ((StrnCmp (Uri
, L
"http://", 7) != 0) && (StrnCmp (Uri
, L
"https://", 7) != 0)) {
85 return EFI_INVALID_PARAMETER
;
89 // Create a new device path by appending the IP node and URI node to
90 // the driver's parent device path
93 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
95 Status
= EFI_OUT_OF_RESOURCES
;
98 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
99 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
100 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
102 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
104 Status
= EFI_OUT_OF_RESOURCES
;
107 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
108 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
109 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
111 TmpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
113 if (TmpDevicePath
== NULL
) {
114 return EFI_OUT_OF_RESOURCES
;
117 // Update the URI node with the input boot file URI.
119 UnicodeStrToAsciiStr (Uri
, AsciiUri
);
120 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (AsciiUri
);
121 Node
= AllocatePool (Length
);
123 Status
= EFI_OUT_OF_RESOURCES
;
124 FreePool (TmpDevicePath
);
127 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
128 Node
->DevPath
.SubType
= MSG_URI_DP
;
129 SetDevicePathNodeLength (Node
, Length
);
130 CopyMem ((UINT8
*) Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
), AsciiUri
, AsciiStrSize (AsciiUri
));
131 NewDevicePath
= AppendDevicePathNode (TmpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
133 FreePool (TmpDevicePath
);
134 if (NewDevicePath
== NULL
) {
135 Status
= EFI_OUT_OF_RESOURCES
;
140 // Get current "BootOrder" variable and find a free target.
143 Status
= GetVariable2 (
145 &gEfiGlobalVariableGuid
,
146 (VOID
**)&CurrentOrder
,
149 if (EFI_ERROR (Status
) && Status
!= EFI_NOT_FOUND
) {
152 OrderCount
= Length
/ sizeof (UINT16
);
154 for (TargetLocation
=0; TargetLocation
< 0xFFFF; TargetLocation
++) {
156 for (Index
= 0; Index
< OrderCount
; Index
++) {
157 if (CurrentOrder
[Index
] == TargetLocation
) {
167 if (TargetLocation
== 0xFFFF) {
168 DEBUG ((EFI_D_ERROR
, "Could not find unused target index.\n"));
169 Status
= EFI_OUT_OF_RESOURCES
;
172 DEBUG ((EFI_D_INFO
, "TargetIndex = %04x.\n", TargetLocation
));
176 // Construct and set the "Boot####" variable
178 DescSize
= StrSize(Description
);
179 FilePathSize
= GetDevicePathSize (NewDevicePath
);
180 TempByteBuffer
= AllocateZeroPool(sizeof(EFI_LOAD_OPTION
) + DescSize
+ FilePathSize
);
181 if (TempByteBuffer
== NULL
) {
182 Status
= EFI_OUT_OF_RESOURCES
;
186 TempByteStart
= TempByteBuffer
;
187 *((UINT32
*) TempByteBuffer
) = LOAD_OPTION_ACTIVE
; // Attributes
188 TempByteBuffer
+= sizeof (UINT32
);
190 *((UINT16
*) TempByteBuffer
) = (UINT16
)FilePathSize
; // FilePathListLength
191 TempByteBuffer
+= sizeof (UINT16
);
193 CopyMem (TempByteBuffer
, Description
, DescSize
);
194 TempByteBuffer
+= DescSize
;
195 CopyMem (TempByteBuffer
, NewDevicePath
, FilePathSize
);
197 UnicodeSPrint (OptionStr
, sizeof(OptionStr
), L
"%s%04x", L
"Boot", TargetLocation
);
198 Status
= gRT
->SetVariable (
200 &gEfiGlobalVariableGuid
,
201 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
202 sizeof(UINT32
) + sizeof(UINT16
) + DescSize
+ FilePathSize
,
205 if (EFI_ERROR (Status
)) {
210 // Insert into the order list and set "BootOrder" variable
212 NewOrder
= AllocateZeroPool ((OrderCount
+ 1) * sizeof (UINT16
));
213 if (NewOrder
== NULL
) {
214 Status
= EFI_OUT_OF_RESOURCES
;
217 CopyMem(NewOrder
, CurrentOrder
, OrderCount
* sizeof(UINT16
));
218 NewOrder
[OrderCount
] = (UINT16
) TargetLocation
;
219 Status
= gRT
->SetVariable (
221 &gEfiGlobalVariableGuid
,
222 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
223 ((OrderCount
+ 1) * sizeof (UINT16
)),
230 if (CurrentOrder
!= NULL
) {
231 FreePool (CurrentOrder
);
233 if (NewOrder
!= NULL
) {
236 if (TempByteStart
!= NULL
) {
237 FreePool (TempByteStart
);
239 if (NewDevicePath
!= NULL
) {
240 FreePool (NewDevicePath
);
248 This function allows the caller to request the current
249 configuration for one or more named elements. The resulting
250 string is in <ConfigAltResp> format. Also, any and all alternative
251 configuration strings shall be appended to the end of the
252 current configuration string. If they are, they must appear
253 after the current configuration. They must contain the same
254 routing (GUID, NAME, PATH) as the current configuration string.
255 They must have an additional description indicating the type of
256 alternative configuration the string represents,
257 "ALTCFG=<StringToken>". That <StringToken> (when
258 converted from Hex UNICODE to binary) is a reference to a
259 string in the associated string pack.
261 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
263 @param[in] Request A null-terminated Unicode string in
264 <ConfigRequest> format. Note that this
265 includes the routing information as well as
266 the configurable name / value pairs. It is
267 invalid for this string to be in
268 <MultiConfigRequest> format.
270 @param[out] Progress On return, points to a character in the
271 Request string. Points to the string's null
272 terminator if request was successful. Points
273 to the most recent "&" before the first
274 failing name / value pair (or the beginning
275 of the string if the failure is in the first
276 name / value pair) if the request was not successful.
278 @param[out] Results A null-terminated Unicode string in
279 <ConfigAltResp> format which has all values
280 filled in for the names in the Request string.
281 String to be allocated by the called function.
283 @retval EFI_SUCCESS The Results string is filled with the
284 values corresponding to all requested
287 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
288 parts of the results that must be
289 stored awaiting possible future
292 @retval EFI_INVALID_PARAMETER For example, passing in a NULL
293 for the Request parameter
294 would result in this type of
295 error. In this case, the
296 Progress parameter would be
299 @retval EFI_NOT_FOUND Routing data doesn't match any
300 known driver. Progress set to the
301 first character in the routing header.
302 Note: There is no requirement that the
303 driver validate the routing data. It
304 must skip the <ConfigHdr> in order to
307 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set
308 to most recent "&" before the
309 error or the beginning of the
312 @retval EFI_INVALID_PARAMETER Unknown name. Progress points
313 to the & before the name in
319 HttpBootFormExtractConfig (
320 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
321 IN CONST EFI_STRING Request
,
322 OUT EFI_STRING
*Progress
,
323 OUT EFI_STRING
*Results
328 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
329 EFI_STRING ConfigRequestHdr
;
330 EFI_STRING ConfigRequest
;
331 BOOLEAN AllocatedRequest
;
334 if (Progress
== NULL
|| Results
== NULL
) {
335 return EFI_INVALID_PARAMETER
;
339 if ((Request
!= NULL
) && !HiiIsConfigHdrMatch (Request
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
340 return EFI_NOT_FOUND
;
343 ConfigRequestHdr
= NULL
;
344 ConfigRequest
= NULL
;
345 AllocatedRequest
= FALSE
;
348 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
350 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
352 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
353 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
355 ConfigRequest
= Request
;
356 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
358 // Request has no request element, construct full request string.
359 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
360 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
362 ConfigRequestHdr
= HiiConstructConfigHdr (&gHttpBootConfigGuid
, mHttpBootConfigStorageName
, CallbackInfo
->ChildHandle
);
363 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
364 ConfigRequest
= AllocateZeroPool (Size
);
365 ASSERT (ConfigRequest
!= NULL
);
366 AllocatedRequest
= TRUE
;
367 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
368 FreePool (ConfigRequestHdr
);
371 Status
= gHiiConfigRouting
->BlockToConfig (
374 (UINT8
*) &CallbackInfo
->HttpBootNvData
,
379 ASSERT_EFI_ERROR (Status
);
382 // Free the allocated config request string.
384 if (AllocatedRequest
) {
385 FreePool (ConfigRequest
);
386 ConfigRequest
= NULL
;
389 // Set Progress string to the original request string.
391 if (Request
== NULL
) {
393 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
394 *Progress
= Request
+ StrLen (Request
);
402 This function applies changes in a driver's configuration.
403 Input is a Configuration, which has the routing data for this
404 driver followed by name / value configuration pairs. The driver
405 must apply those pairs to its configurable storage. If the
406 driver's configuration is stored in a linear block of data
407 and the driver's name / value pairs are in <BlockConfig>
408 format, it may use the ConfigToBlock helper function (above) to
411 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
413 @param[in] Configuration A null-terminated Unicode string in
414 <ConfigString> format.
416 @param[out] Progress A pointer to a string filled in with the
417 offset of the most recent '&' before the
418 first failing name / value pair (or the
419 beginning of the string if the failure
420 is in the first name / value pair) or
421 the terminating NULL if all was
424 @retval EFI_SUCCESS The results have been distributed or are
425 awaiting distribution.
427 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the
428 parts of the results that must be
429 stored awaiting possible future
432 @retval EFI_INVALID_PARAMETERS Passing in a NULL for the
433 Results parameter would result
434 in this type of error.
436 @retval EFI_NOT_FOUND Target for the specified routing data
442 HttpBootFormRouteConfig (
443 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
444 IN CONST EFI_STRING Configuration
,
445 OUT EFI_STRING
*Progress
450 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
451 HTTP_BOOT_PRIVATE_DATA
*Private
;
453 if (Progress
== NULL
) {
454 return EFI_INVALID_PARAMETER
;
456 *Progress
= Configuration
;
458 if (Configuration
== NULL
) {
459 return EFI_INVALID_PARAMETER
;
463 // Check routing data in <ConfigHdr>.
464 // Note: there is no name for Name/Value storage, only GUID will be checked
466 if (!HiiIsConfigHdrMatch (Configuration
, &gHttpBootConfigGuid
, mHttpBootConfigStorageName
)) {
467 return EFI_NOT_FOUND
;
470 CallbackInfo
= HTTP_BOOT_FORM_CALLBACK_INFO_FROM_CONFIG_ACCESS (This
);
471 Private
= HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_INFO (CallbackInfo
);
473 BufferSize
= sizeof (HTTP_BOOT_CONFIG_IFR_NVDATA
);
474 ZeroMem (&CallbackInfo
->HttpBootNvData
, BufferSize
);
476 Status
= gHiiConfigRouting
->ConfigToBlock (
479 (UINT8
*) &CallbackInfo
->HttpBootNvData
,
483 if (EFI_ERROR (Status
)) {
488 // Create a new boot option according to the configuration data.
490 Status
= HttpBootAddBootOption (
492 (CallbackInfo
->HttpBootNvData
.IpVersion
== HTTP_BOOT_IP_VERSION_6
) ? TRUE
: FALSE
,
493 CallbackInfo
->HttpBootNvData
.Description
,
494 CallbackInfo
->HttpBootNvData
.Uri
502 This function is called to provide results data to the driver.
503 This data consists of a unique key that is used to identify
504 which data is either being passed back or being asked for.
506 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
507 @param[in] Action Specifies the type of action taken by the browser.
508 @param[in] QuestionId A unique value which is sent to the original
509 exporting driver so that it can identify the type
510 of data to expect. The format of the data tends to
511 vary based on the opcode that generated the callback.
512 @param[in] Type The type of value for the question.
513 @param[in, out] Value A pointer to the data being sent to the original
515 @param[out] ActionRequest On return, points to the action requested by the
518 @retval EFI_SUCCESS The callback successfully handled the action.
519 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
520 variable and its data.
521 @retval EFI_DEVICE_ERROR The variable could not be saved.
522 @retval EFI_UNSUPPORTED The specified Action is not supported by the
527 HttpBootFormCallback (
528 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
529 IN EFI_BROWSER_ACTION Action
,
530 IN EFI_QUESTION_ID QuestionId
,
532 IN OUT EFI_IFR_TYPE_VALUE
*Value
,
533 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
536 return EFI_UNSUPPORTED
;
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
;
556 EFI_SERVICE_BINDING_PROTOCOL
*HttpSb
;
558 CHAR16
*OldMenuString
;
559 CHAR16 MenuString
[128];
561 CallbackInfo
= &Private
->CallbackInfo
;
563 if (CallbackInfo
->Initilized
) {
567 CallbackInfo
->Signature
= HTTP_BOOT_FORM_CALLBACK_INFO_SIGNATURE
;
570 // Construct device path node for EFI HII Config Access protocol,
571 // which consists of controller physical device path and one hardware
574 ZeroMem (&VendorDeviceNode
, sizeof (VENDOR_DEVICE_PATH
));
575 VendorDeviceNode
.Header
.Type
= HARDWARE_DEVICE_PATH
;
576 VendorDeviceNode
.Header
.SubType
= HW_VENDOR_DP
;
577 CopyGuid (&VendorDeviceNode
.Guid
, &gEfiCallerIdGuid
);
578 SetDevicePathNodeLength (&VendorDeviceNode
.Header
, sizeof (VENDOR_DEVICE_PATH
));
579 CallbackInfo
->HiiVendorDevicePath
= AppendDevicePathNode (
580 Private
->ParentDevicePath
,
581 (EFI_DEVICE_PATH_PROTOCOL
*) &VendorDeviceNode
583 if (CallbackInfo
->HiiVendorDevicePath
== NULL
) {
584 Status
= EFI_OUT_OF_RESOURCES
;
588 CallbackInfo
->ConfigAccess
.ExtractConfig
= HttpBootFormExtractConfig
;
589 CallbackInfo
->ConfigAccess
.RouteConfig
= HttpBootFormRouteConfig
;
590 CallbackInfo
->ConfigAccess
.Callback
= HttpBootFormCallback
;
593 // Install Device Path Protocol and Config Access protocol to driver handle.
595 Status
= gBS
->InstallMultipleProtocolInterfaces (
596 &CallbackInfo
->ChildHandle
,
597 &gEfiDevicePathProtocolGuid
,
598 CallbackInfo
->HiiVendorDevicePath
,
599 &gEfiHiiConfigAccessProtocolGuid
,
600 &CallbackInfo
->ConfigAccess
,
603 if (!EFI_ERROR (Status
)) {
605 // Open the Parent Handle for the child
607 Status
= gBS
->OpenProtocol (
609 &gEfiHttpServiceBindingProtocolGuid
,
612 CallbackInfo
->ChildHandle
,
613 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
617 if (EFI_ERROR (Status
)) {
622 // Publish our HII data.
624 CallbackInfo
->RegisteredHandle
= HiiAddPackages (
625 &gHttpBootConfigGuid
,
626 CallbackInfo
->ChildHandle
,
628 HttpBootConfigVfrBin
,
631 if (CallbackInfo
->RegisteredHandle
== NULL
) {
632 Status
= EFI_OUT_OF_RESOURCES
;
637 // Append MAC string in the menu help string
639 Status
= NetLibGetMacString (Private
->Controller
, Private
->Image
, &MacString
);
640 if (!EFI_ERROR (Status
)) {
641 OldMenuString
= HiiGetString (
642 CallbackInfo
->RegisteredHandle
,
643 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
646 UnicodeSPrint (MenuString
, 128, L
"%s (MAC:%s)", OldMenuString
, MacString
);
648 CallbackInfo
->RegisteredHandle
,
649 STRING_TOKEN (STR_HTTP_BOOT_CONFIG_FORM_HELP
),
654 FreePool (MacString
);
655 FreePool (OldMenuString
);
662 HttpBootConfigFormUnload (Private
);
667 Unload the configuration form, this includes: delete all the configuration
668 entries, uninstall the form callback protocol, and free the resources used.
670 @param[in] Private Pointer to the driver private data.
672 @retval EFI_SUCCESS The configuration form is unloaded.
673 @retval Others Failed to unload the form.
677 HttpBootConfigFormUnload (
678 IN HTTP_BOOT_PRIVATE_DATA
*Private
681 HTTP_BOOT_FORM_CALLBACK_INFO
*CallbackInfo
;
683 CallbackInfo
= &Private
->CallbackInfo
;
684 if (CallbackInfo
->ChildHandle
!= NULL
) {
686 // Close the child handle
690 &gEfiHttpServiceBindingProtocolGuid
,
692 CallbackInfo
->ChildHandle
696 // Uninstall EFI_HII_CONFIG_ACCESS_PROTOCOL
698 gBS
->UninstallMultipleProtocolInterfaces (
699 CallbackInfo
->ChildHandle
,
700 &gEfiDevicePathProtocolGuid
,
701 CallbackInfo
->HiiVendorDevicePath
,
702 &gEfiHiiConfigAccessProtocolGuid
,
703 &CallbackInfo
->ConfigAccess
,
706 CallbackInfo
->ChildHandle
= NULL
;
709 if (CallbackInfo
->HiiVendorDevicePath
!= NULL
) {
710 FreePool (CallbackInfo
->HiiVendorDevicePath
);
711 CallbackInfo
->HiiVendorDevicePath
= NULL
;
714 if (CallbackInfo
->RegisteredHandle
!= NULL
) {
716 // Remove HII package list
718 HiiRemovePackages (CallbackInfo
->RegisteredHandle
);
719 CallbackInfo
->RegisteredHandle
= NULL
;