2 HII Config Access protocol implementation of RamDiskDxe driver.
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 "RamDiskImpl.h"
17 CHAR16 mRamDiskStorageName
[] = L
"RAM_DISK_CONFIGURATION";
19 RAM_DISK_CONFIG_PRIVATE_DATA mRamDiskConfigPrivateDataTemplate
= {
20 RAM_DISK_CONFIG_PRIVATE_DATA_SIGNATURE
,
28 HII_VENDOR_DEVICE_PATH mRamDiskHiiVendorDevicePath
= {
34 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
38 RAM_DISK_FORM_SET_GUID
42 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
44 (UINT8
) (END_DEVICE_PATH_LENGTH
),
45 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
52 This function publish the RAM disk configuration Form.
54 @param[in, out] ConfigPrivateData
55 Points to RAM disk configuration private data.
57 @retval EFI_SUCCESS HII Form is installed successfully.
58 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
59 @retval Others Other errors as indicated.
63 InstallRamDiskConfigForm (
64 IN OUT RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivateData
68 EFI_HII_HANDLE HiiHandle
;
69 EFI_HANDLE DriverHandle
;
70 EFI_HII_CONFIG_ACCESS_PROTOCOL
*ConfigAccess
;
73 ConfigAccess
= &ConfigPrivateData
->ConfigAccess
;
74 Status
= gBS
->InstallMultipleProtocolInterfaces (
76 &gEfiDevicePathProtocolGuid
,
77 &mRamDiskHiiVendorDevicePath
,
78 &gEfiHiiConfigAccessProtocolGuid
,
82 if (EFI_ERROR (Status
)) {
86 ConfigPrivateData
->DriverHandle
= DriverHandle
;
89 // Publish the HII package list
91 HiiHandle
= HiiAddPackages (
98 if (HiiHandle
== NULL
) {
99 gBS
->UninstallMultipleProtocolInterfaces (
101 &gEfiDevicePathProtocolGuid
,
102 &mRamDiskHiiVendorDevicePath
,
103 &gEfiHiiConfigAccessProtocolGuid
,
107 return EFI_OUT_OF_RESOURCES
;
110 ConfigPrivateData
->HiiHandle
= HiiHandle
;
117 This function removes RAM disk configuration Form.
119 @param[in, out] ConfigPrivateData
120 Points to RAM disk configuration private data.
124 UninstallRamDiskConfigForm (
125 IN OUT RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivateData
129 // Uninstall HII package list
131 if (ConfigPrivateData
->HiiHandle
!= NULL
) {
132 HiiRemovePackages (ConfigPrivateData
->HiiHandle
);
133 ConfigPrivateData
->HiiHandle
= NULL
;
137 // Uninstall HII Config Access Protocol
139 if (ConfigPrivateData
->DriverHandle
!= NULL
) {
140 gBS
->UninstallMultipleProtocolInterfaces (
141 ConfigPrivateData
->DriverHandle
,
142 &gEfiDevicePathProtocolGuid
,
143 &mRamDiskHiiVendorDevicePath
,
144 &gEfiHiiConfigAccessProtocolGuid
,
145 &ConfigPrivateData
->ConfigAccess
,
148 ConfigPrivateData
->DriverHandle
= NULL
;
151 FreePool (ConfigPrivateData
);
156 Unregister all registered RAM disks.
160 UnregisterAllRamDisks (
165 LIST_ENTRY
*NextEntry
;
166 RAM_DISK_PRIVATE_DATA
*PrivateData
;
168 if (!IsListEmpty(&RegisteredRamDisks
)) {
169 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &RegisteredRamDisks
) {
170 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
172 gBS
->UninstallMultipleProtocolInterfaces (
174 &gEfiBlockIoProtocolGuid
,
175 &PrivateData
->BlockIo
,
176 &gEfiDevicePathProtocolGuid
,
177 (EFI_DEVICE_PATH_PROTOCOL
*) PrivateData
->DevicePath
,
181 RemoveEntryList (&PrivateData
->ThisInstance
);
183 if (RamDiskCreateHii
== PrivateData
->CreateMethod
) {
185 // If a RAM disk is created within HII, then the RamDiskDxe driver
186 // driver is responsible for freeing the allocated memory for the
189 FreePool ((VOID
*)(UINTN
) PrivateData
->StartingAddr
);
193 gBS
->DisconnectController (PrivateData
->Handle
, NULL
, NULL
);
195 FreePool (PrivateData
->DevicePath
);
196 FreePool (PrivateData
);
204 This function allows a caller to extract the current configuration for one
205 or more named elements from the target driver.
207 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
208 @param[in] Request A null-terminated Unicode string in
209 <ConfigRequest> format.
210 @param[out] Progress On return, points to a character in the Request
211 string. Points to the string's null terminator if
212 request was successful. Points to the most recent
213 '&' before the first failing name/value pair (or
214 the beginning of the string if the failure is in
215 the first name/value pair) if the request was not
217 @param[out] Results A null-terminated Unicode string in
218 <ConfigAltResp> format which has all values filled
219 in for the names in the Request string. String to
220 be allocated by the called function.
222 @retval EFI_SUCCESS The Results is filled with the requested
224 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
225 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
226 @retval EFI_NOT_FOUND Routing data doesn't match any storage in
232 RamDiskExtractConfig (
233 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
234 IN CONST EFI_STRING Request
,
235 OUT EFI_STRING
*Progress
,
236 OUT EFI_STRING
*Results
241 RAM_DISK_CONFIGURATION
*Configuration
;
242 EFI_STRING ConfigRequest
;
243 EFI_STRING ConfigRequestHdr
;
244 RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivate
;
246 BOOLEAN AllocatedRequest
;
248 if (Progress
== NULL
|| Results
== NULL
) {
249 return EFI_INVALID_PARAMETER
;
253 if ((Request
!= NULL
) &&
254 !HiiIsConfigHdrMatch (Request
, &gRamDiskFormSetGuid
, mRamDiskStorageName
)) {
255 return EFI_NOT_FOUND
;
258 ConfigRequestHdr
= NULL
;
259 ConfigRequest
= NULL
;
260 AllocatedRequest
= FALSE
;
264 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
266 ConfigPrivate
= RAM_DISK_CONFIG_PRIVATE_FROM_THIS (This
);
267 BufferSize
= sizeof (RAM_DISK_CONFIGURATION
) + ListEntryNum
;
268 Configuration
= AllocateZeroPool (BufferSize
);
269 if (Configuration
== NULL
) {
270 return EFI_OUT_OF_RESOURCES
;
273 ConfigRequest
= Request
;
274 if ((Request
== NULL
) || (StrStr (Request
, L
"OFFSET") == NULL
)) {
276 // Request has no request element, construct full request string.
277 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
278 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
280 ConfigRequestHdr
= HiiConstructConfigHdr (
281 &gRamDiskFormSetGuid
,
283 ConfigPrivate
->DriverHandle
285 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
286 ConfigRequest
= AllocateZeroPool (Size
);
287 ASSERT (ConfigRequest
!= NULL
);
288 AllocatedRequest
= TRUE
;
289 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
290 FreePool (ConfigRequestHdr
);
293 Status
= gHiiConfigRouting
->BlockToConfig (
296 (UINT8
*) &Configuration
,
302 // Free the allocated config request string and RAM disk configuration data.
304 if (AllocatedRequest
) {
305 FreePool (ConfigRequest
);
306 ConfigRequest
= NULL
;
308 FreePool (Configuration
);
311 // Set Progress string to the original request string.
313 if (Request
== NULL
) {
315 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
316 *Progress
= Request
+ StrLen (Request
);
324 This function processes the results of changes in configuration.
326 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
327 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
329 @param[out] Progress A pointer to a string filled in with the offset of
330 the most recent '&' before the first failing
331 name/value pair (or the beginning of the string if
332 the failure is in the first name/value pair) or
333 the terminating NULL if all was successful.
335 @retval EFI_SUCCESS The Results is processed successfully.
336 @retval EFI_INVALID_PARAMETER Configuration is NULL.
337 @retval EFI_NOT_FOUND Routing data doesn't match any storage in
344 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
345 IN CONST EFI_STRING Configuration
,
346 OUT EFI_STRING
*Progress
349 if (Configuration
== NULL
|| Progress
== NULL
) {
350 return EFI_INVALID_PARAMETER
;
353 *Progress
= Configuration
;
354 if (!HiiIsConfigHdrMatch (Configuration
, &gRamDiskFormSetGuid
, mRamDiskStorageName
)) {
355 return EFI_NOT_FOUND
;
358 *Progress
= Configuration
+ StrLen (Configuration
);
365 Allocate memory and register the RAM disk created within RamDiskDxe
368 @param[in] Size If creating raw, size of the RAM disk to create.
369 If creating from file, zero.
370 @param[in] FileHandle If creating raw, NULL. If creating from file, the
373 @retval EFI_SUCCESS RAM disk is created and registered.
374 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to match the
381 IN EFI_FILE_HANDLE FileHandle
388 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
389 RAM_DISK_PRIVATE_DATA
*PrivateData
;
390 EFI_FILE_INFO
*FileInformation
;
392 FileInformation
= NULL
;
394 if (FileHandle
!= NULL
) {
398 FileInformation
= FileInfo (FileHandle
);
399 if (NULL
== FileInformation
) {
402 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
405 L
"Not enough memory to get the file information!",
406 L
"Press ENTER to continue ...",
410 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
412 return EFI_OUT_OF_RESOURCES
;
416 // Update the size of RAM disk according to the file size.
418 Size
= FileInformation
->FileSize
;
421 StartingAddr
= (UINTN
) AllocatePool ((UINTN
) Size
);
422 if (0 == StartingAddr
) {
425 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
428 L
"Not enough memory to create the RAM disk!",
429 L
"Press ENTER to continue ...",
433 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
435 return EFI_OUT_OF_RESOURCES
;
438 if (FileHandle
!= NULL
) {
440 // Copy the file content to the RAM disk.
442 BufferSize
= (UINTN
) Size
;
446 (VOID
*)(UINTN
) StartingAddr
448 if (BufferSize
!= FileInformation
->FileSize
) {
451 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
454 L
"File content read error!",
455 L
"Press ENTER to continue ...",
459 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
461 return EFI_DEVICE_ERROR
;
466 // Register the newly created RAM disk.
468 Status
= RamDiskRegister (
471 &gEfiVirtualDiskGuid
,
475 if (EFI_ERROR (Status
)) {
478 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
481 L
"Fail to register the newly created RAM disk!",
482 L
"Press ENTER to continue ...",
486 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
492 // If RAM disk is created within HII, memory should be freed when the
493 // RAM disk is unregisterd.
495 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (RegisteredRamDisks
.BackLink
);
496 PrivateData
->CreateMethod
= RamDiskCreateHii
;
503 This function updates the registered RAM disks list on the main form.
505 @param[in, out] ConfigPrivate
506 Private data for configurating hii data for RAM
512 IN OUT RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivate
515 VOID
*StartOpCodeHandle
;
516 VOID
*EndOpCodeHandle
;
517 EFI_IFR_GUID_LABEL
*StartLabel
;
518 EFI_IFR_GUID_LABEL
*EndLabel
;
521 RAM_DISK_PRIVATE_DATA
*PrivateData
;
523 CHAR16 RamDiskStr
[128];
524 EFI_STRING_ID StringId
;
528 // Init OpCode Handle
530 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
531 ASSERT (StartOpCodeHandle
!= NULL
);
533 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
534 ASSERT (EndOpCodeHandle
!= NULL
);
537 // Create Hii Extend Label OpCode as the start opcode
539 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
543 sizeof (EFI_IFR_GUID_LABEL
)
545 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
546 StartLabel
->Number
= MAIN_LABEL_LIST_START
;
549 // Create Hii Extend Label OpCode as the end opcode
551 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
555 sizeof (EFI_IFR_GUID_LABEL
)
557 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
558 EndLabel
->Number
= MAIN_LABEL_LIST_END
;
561 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
562 EFI_LIST_FOR_EACH (Entry
, &RegisteredRamDisks
) {
563 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
569 L
" RAM Disk %d: [0x%lx, 0x%lx]\n",
571 PrivateData
->StartingAddr
,
572 PrivateData
->StartingAddr
+ PrivateData
->Size
575 StringId
= HiiSetString (ConfigPrivate
->HiiHandle
, 0, RamDiskStr
, NULL
);
576 ASSERT (StringId
!= 0);
578 HiiCreateCheckBoxOpCode (
580 (EFI_QUESTION_ID
) (MAIN_CHECKBOX_QUESTION_ID_START
+ Index
),
581 RAM_DISK_CONFIGURATION_VARSTORE_ID
,
582 (UINT16
) (RAM_DISK_LIST_VAR_OFFSET
+ Index
),
584 STRING_TOKEN (STR_RAM_DISK_LIST_HELP
),
592 gBS
->RestoreTPL (OldTpl
);
595 ConfigPrivate
->HiiHandle
,
596 &gRamDiskFormSetGuid
,
602 HiiFreeOpCodeHandle (StartOpCodeHandle
);
603 HiiFreeOpCodeHandle (EndOpCodeHandle
);
608 This function processes the results of changes in configuration.
610 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
611 @param[in] Action Specifies the type of action taken by the browser.
612 @param[in] QuestionId A unique value which is sent to the original
613 exporting driver so that it can identify the type
615 @param[in] Type The type of value for the question.
616 @param[in] Value A pointer to the data being sent to the original
618 @param[out] ActionRequest On return, points to the action requested by the
621 @retval EFI_SUCCESS The callback successfully handled the action.
622 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
623 variable and its data.
624 @retval EFI_DEVICE_ERROR The variable could not be saved.
625 @retval EFI_UNSUPPORTED The specified Action is not supported by the
632 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
633 IN EFI_BROWSER_ACTION Action
,
634 IN EFI_QUESTION_ID QuestionId
,
636 IN EFI_IFR_TYPE_VALUE
*Value
,
637 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
642 RAM_DISK_PRIVATE_DATA
*PrivateData
;
643 RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivate
;
644 RAM_DISK_CONFIGURATION
*Configuration
;
645 EFI_DEVICE_PATH_PROTOCOL
*FileDevPath
;
646 EFI_FILE_HANDLE FileHandle
;
648 LIST_ENTRY
*NextEntry
;
651 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
652 return EFI_INVALID_PARAMETER
;
655 if (Action
== EFI_BROWSER_ACTION_RETRIEVE
) {
656 Status
= EFI_UNSUPPORTED
;
657 if (QuestionId
== CREATE_RAW_SIZE_QUESTION_ID
) {
658 Value
->u64
= EFI_PAGE_SIZE
;
659 Status
= EFI_SUCCESS
;
664 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) &&
665 (Action
!= EFI_BROWSER_ACTION_CHANGING
) &&
666 (Action
!= EFI_BROWSER_ACTION_FORM_OPEN
)) {
667 return EFI_UNSUPPORTED
;
670 ConfigPrivate
= RAM_DISK_CONFIG_PRIVATE_FROM_THIS (This
);
673 // Update the RAM disk list show at the main form first.
675 if (Action
== EFI_BROWSER_ACTION_FORM_OPEN
) {
676 Status
= EFI_UNSUPPORTED
;
677 if (QuestionId
== MAIN_GOTO_FILE_EXPLORER_ID
) {
678 UpdateMainForm (ConfigPrivate
);
679 Status
= EFI_SUCCESS
;
687 Configuration
= AllocateZeroPool (sizeof (RAM_DISK_CONFIGURATION
) + ListEntryNum
);
688 if (Configuration
== NULL
) {
689 return EFI_OUT_OF_RESOURCES
;
692 Status
= EFI_SUCCESS
;
695 &gRamDiskFormSetGuid
,
697 sizeof (RAM_DISK_CONFIGURATION
) + ListEntryNum
,
698 (UINT8
*) Configuration
701 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
702 switch (QuestionId
) {
703 case MAIN_GOTO_FILE_EXPLORER_ID
:
704 Status
= ChooseFile (NULL
, NULL
, NULL
, &FileDevPath
);
705 if (EFI_ERROR (Status
)) {
709 if (FileDevPath
!= NULL
) {
713 Status
= OpenFileByDevicePath (
719 if (EFI_ERROR (Status
)) {
724 // Create from file, RAM disk size is zero. It will be updated
725 // according to the file size.
727 Status
= HiiCreateRamDisk (0, FileHandle
);
728 if (EFI_ERROR (Status
)) {
733 // Refresh the registered RAM disks list.
735 UpdateMainForm (ConfigPrivate
);
738 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
744 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
745 switch (QuestionId
) {
746 case MAIN_REMOVE_RD_QUESTION_ID
:
748 // Remove the selected RAM disks
751 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
752 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &RegisteredRamDisks
) {
753 if (Configuration
->RamDiskList
[Index
++] != 0) {
754 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
757 (EFI_DEVICE_PATH_PROTOCOL
*) PrivateData
->DevicePath
761 gBS
->RestoreTPL (OldTpl
);
763 UpdateMainForm (ConfigPrivate
);
765 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
766 ZeroMem (Configuration
->RamDiskList
, ListEntryNum
);
769 case CREATE_RAW_SUBMIT_QUESTION_ID
:
771 // Create raw, FileHandle is NULL.
773 Status
= HiiCreateRamDisk (Configuration
->Size
, NULL
);
774 if (EFI_ERROR (Status
)) {
779 // Refresh the registered RAM disks list.
781 UpdateMainForm (ConfigPrivate
);
783 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
786 case CREATE_RAW_DISCARD_QUESTION_ID
:
787 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
795 if (!EFI_ERROR (Status
)) {
797 &gRamDiskFormSetGuid
,
799 sizeof (RAM_DISK_CONFIGURATION
) + ListEntryNum
,
800 (UINT8
*) Configuration
,
804 FreePool (Configuration
);