2 HII Config Access protocol implementation of RamDiskDxe driver.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "RamDiskImpl.h"
18 CHAR16 mRamDiskStorageName
[] = L
"RAM_DISK_CONFIGURATION";
20 RAM_DISK_CONFIG_PRIVATE_DATA mRamDiskConfigPrivateDataTemplate
= {
21 RAM_DISK_CONFIG_PRIVATE_DATA_SIGNATURE
,
24 RAM_DISK_BOOT_SERVICE_DATA_MEMORY
33 HII_VENDOR_DEVICE_PATH mRamDiskHiiVendorDevicePath
= {
39 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
40 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
43 RAM_DISK_FORM_SET_GUID
47 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
49 (UINT8
) (END_DEVICE_PATH_LENGTH
),
50 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
57 This function publish the RAM disk configuration Form.
59 @param[in, out] ConfigPrivateData
60 Points to RAM disk configuration private data.
62 @retval EFI_SUCCESS HII Form is installed successfully.
63 @retval EFI_OUT_OF_RESOURCES Not enough resource for HII Form installation.
64 @retval Others Other errors as indicated.
68 InstallRamDiskConfigForm (
69 IN OUT RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivateData
73 EFI_HII_HANDLE HiiHandle
;
74 EFI_HANDLE DriverHandle
;
75 EFI_HII_CONFIG_ACCESS_PROTOCOL
*ConfigAccess
;
78 ConfigAccess
= &ConfigPrivateData
->ConfigAccess
;
79 Status
= gBS
->InstallMultipleProtocolInterfaces (
81 &gEfiDevicePathProtocolGuid
,
82 &mRamDiskHiiVendorDevicePath
,
83 &gEfiHiiConfigAccessProtocolGuid
,
87 if (EFI_ERROR (Status
)) {
91 ConfigPrivateData
->DriverHandle
= DriverHandle
;
94 // Publish the HII package list
96 HiiHandle
= HiiAddPackages (
103 if (HiiHandle
== NULL
) {
104 gBS
->UninstallMultipleProtocolInterfaces (
106 &gEfiDevicePathProtocolGuid
,
107 &mRamDiskHiiVendorDevicePath
,
108 &gEfiHiiConfigAccessProtocolGuid
,
112 return EFI_OUT_OF_RESOURCES
;
115 ConfigPrivateData
->HiiHandle
= HiiHandle
;
122 This function removes RAM disk configuration Form.
124 @param[in, out] ConfigPrivateData
125 Points to RAM disk configuration private data.
129 UninstallRamDiskConfigForm (
130 IN OUT RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivateData
134 // Uninstall HII package list
136 if (ConfigPrivateData
->HiiHandle
!= NULL
) {
137 HiiRemovePackages (ConfigPrivateData
->HiiHandle
);
138 ConfigPrivateData
->HiiHandle
= NULL
;
142 // Uninstall HII Config Access Protocol
144 if (ConfigPrivateData
->DriverHandle
!= NULL
) {
145 gBS
->UninstallMultipleProtocolInterfaces (
146 ConfigPrivateData
->DriverHandle
,
147 &gEfiDevicePathProtocolGuid
,
148 &mRamDiskHiiVendorDevicePath
,
149 &gEfiHiiConfigAccessProtocolGuid
,
150 &ConfigPrivateData
->ConfigAccess
,
153 ConfigPrivateData
->DriverHandle
= NULL
;
156 FreePool (ConfigPrivateData
);
161 Unregister all registered RAM disks.
165 UnregisterAllRamDisks (
170 LIST_ENTRY
*NextEntry
;
171 RAM_DISK_PRIVATE_DATA
*PrivateData
;
173 if (!IsListEmpty(&RegisteredRamDisks
)) {
174 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &RegisteredRamDisks
) {
175 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
177 gBS
->UninstallMultipleProtocolInterfaces (
179 &gEfiBlockIoProtocolGuid
,
180 &PrivateData
->BlockIo
,
181 &gEfiBlockIo2ProtocolGuid
,
182 &PrivateData
->BlockIo2
,
183 &gEfiDevicePathProtocolGuid
,
184 (EFI_DEVICE_PATH_PROTOCOL
*) PrivateData
->DevicePath
,
188 RemoveEntryList (&PrivateData
->ThisInstance
);
190 if (RamDiskCreateHii
== PrivateData
->CreateMethod
) {
192 // If a RAM disk is created within HII, then the RamDiskDxe driver
193 // driver is responsible for freeing the allocated memory for the
196 FreePool ((VOID
*)(UINTN
) PrivateData
->StartingAddr
);
199 FreePool (PrivateData
->DevicePath
);
200 FreePool (PrivateData
);
207 This function allows a caller to extract the current configuration for one
208 or more named elements from the target driver.
210 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
211 @param[in] Request A null-terminated Unicode string in
212 <ConfigRequest> format.
213 @param[out] Progress On return, points to a character in the Request
214 string. Points to the string's null terminator if
215 request was successful. Points to the most recent
216 '&' before the first failing name/value pair (or
217 the beginning of the string if the failure is in
218 the first name/value pair) if the request was not
220 @param[out] Results A null-terminated Unicode string in
221 <ConfigAltResp> format which has all values filled
222 in for the names in the Request string. String to
223 be allocated by the called function.
225 @retval EFI_SUCCESS The Results is filled with the requested
227 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
228 @retval EFI_INVALID_PARAMETER Request is illegal syntax, or unknown name.
229 @retval EFI_NOT_FOUND Routing data doesn't match any storage in
235 RamDiskExtractConfig (
236 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
237 IN CONST EFI_STRING Request
,
238 OUT EFI_STRING
*Progress
,
239 OUT EFI_STRING
*Results
242 if (Progress
== NULL
|| Results
== NULL
) {
243 return EFI_INVALID_PARAMETER
;
246 return EFI_NOT_FOUND
;
251 This function processes the results of changes in configuration.
253 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
254 @param[in] Configuration A null-terminated Unicode string in <ConfigResp>
256 @param[out] Progress A pointer to a string filled in with the offset of
257 the most recent '&' before the first failing
258 name/value pair (or the beginning of the string if
259 the failure is in the first name/value pair) or
260 the terminating NULL if all was successful.
262 @retval EFI_SUCCESS The Results is processed successfully.
263 @retval EFI_INVALID_PARAMETER Configuration is NULL.
264 @retval EFI_NOT_FOUND Routing data doesn't match any storage in
271 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
272 IN CONST EFI_STRING Configuration
,
273 OUT EFI_STRING
*Progress
276 if (Configuration
== NULL
|| Progress
== NULL
) {
277 return EFI_INVALID_PARAMETER
;
280 return EFI_NOT_FOUND
;
285 Allocate memory and register the RAM disk created within RamDiskDxe
288 @param[in] Size If creating raw, size of the RAM disk to create.
289 If creating from file, zero.
290 @param[in] FileHandle If creating raw, NULL. If creating from file, the
292 @param[in] MemoryType Type of memory to be used to create RAM Disk.
294 @retval EFI_SUCCESS RAM disk is created and registered.
295 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to match the
302 IN EFI_FILE_HANDLE FileHandle
,
308 UINT64
*StartingAddr
;
310 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
311 RAM_DISK_PRIVATE_DATA
*PrivateData
;
312 EFI_FILE_INFO
*FileInformation
;
314 FileInformation
= NULL
;
317 if (FileHandle
!= NULL
) {
321 FileInformation
= FileInfo (FileHandle
);
322 if (NULL
== FileInformation
) {
325 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
328 L
"Not enough memory to get the file information!",
329 L
"Press ENTER to continue ...",
333 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
335 return EFI_OUT_OF_RESOURCES
;
339 // Update the size of RAM disk according to the file size.
341 Size
= FileInformation
->FileSize
;
344 if (Size
> (UINTN
) -1) {
347 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
350 L
"The given RAM disk size is too large!",
351 L
"Press ENTER to continue ...",
355 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
357 return EFI_OUT_OF_RESOURCES
;
360 if (MemoryType
== RAM_DISK_BOOT_SERVICE_DATA_MEMORY
) {
361 Status
= gBS
->AllocatePool (
364 (VOID
**)&StartingAddr
366 } else if (MemoryType
== RAM_DISK_RESERVED_MEMORY
) {
367 Status
= gBS
->AllocatePool (
368 EfiReservedMemoryType
,
370 (VOID
**)&StartingAddr
373 Status
= EFI_INVALID_PARAMETER
;
376 if ((StartingAddr
== NULL
) || EFI_ERROR(Status
)) {
379 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
382 L
"Not enough memory to create the RAM disk!",
383 L
"Press ENTER to continue ...",
387 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
389 return EFI_OUT_OF_RESOURCES
;
392 if (FileHandle
!= NULL
) {
394 // Copy the file content to the RAM disk.
396 BufferSize
= (UINTN
) Size
;
400 (VOID
*)(UINTN
) StartingAddr
402 if (BufferSize
!= FileInformation
->FileSize
) {
405 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
408 L
"File content read error!",
409 L
"Press ENTER to continue ...",
413 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
415 return EFI_DEVICE_ERROR
;
420 // Register the newly created RAM disk.
422 Status
= RamDiskRegister (
423 ((UINT64
)(UINTN
) StartingAddr
),
425 &gEfiVirtualDiskGuid
,
429 if (EFI_ERROR (Status
)) {
432 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
435 L
"Fail to register the newly created RAM disk!",
436 L
"Press ENTER to continue ...",
440 } while (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
);
446 // If RAM disk is created within HII, memory should be freed when the
447 // RAM disk is unregisterd.
449 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (RegisteredRamDisks
.BackLink
);
450 PrivateData
->CreateMethod
= RamDiskCreateHii
;
457 This function updates the registered RAM disks list on the main form.
459 @param[in, out] ConfigPrivate
460 Private data for configurating hii data for RAM
466 IN OUT RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivate
469 VOID
*StartOpCodeHandle
;
470 VOID
*EndOpCodeHandle
;
471 EFI_IFR_GUID_LABEL
*StartLabel
;
472 EFI_IFR_GUID_LABEL
*EndLabel
;
475 RAM_DISK_PRIVATE_DATA
*PrivateData
;
477 CHAR16 RamDiskStr
[128];
478 EFI_STRING_ID StringId
;
481 // Init OpCode Handle
483 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
484 ASSERT (StartOpCodeHandle
!= NULL
);
486 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
487 ASSERT (EndOpCodeHandle
!= NULL
);
490 // Create Hii Extend Label OpCode as the start opcode
492 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
496 sizeof (EFI_IFR_GUID_LABEL
)
498 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
499 StartLabel
->Number
= MAIN_LABEL_LIST_START
;
502 // Create Hii Extend Label OpCode as the end opcode
504 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (
508 sizeof (EFI_IFR_GUID_LABEL
)
510 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
511 EndLabel
->Number
= MAIN_LABEL_LIST_END
;
514 EFI_LIST_FOR_EACH (Entry
, &RegisteredRamDisks
) {
515 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
516 PrivateData
->CheckBoxId
= (EFI_QUESTION_ID
)
517 (MAIN_CHECKBOX_QUESTION_ID_START
+ Index
);
519 // CheckBox is unchecked by default.
521 PrivateData
->CheckBoxChecked
= FALSE
;
527 L
" RAM Disk %d: [0x%lx, 0x%lx]\n",
529 PrivateData
->StartingAddr
,
530 PrivateData
->StartingAddr
+ PrivateData
->Size
- 1
533 StringId
= HiiSetString (ConfigPrivate
->HiiHandle
, 0, RamDiskStr
, NULL
);
534 ASSERT (StringId
!= 0);
536 HiiCreateCheckBoxOpCode (
538 PrivateData
->CheckBoxId
,
542 STRING_TOKEN (STR_RAM_DISK_LIST_HELP
),
543 EFI_IFR_FLAG_CALLBACK
,
552 ConfigPrivate
->HiiHandle
,
553 &gRamDiskFormSetGuid
,
559 HiiFreeOpCodeHandle (StartOpCodeHandle
);
560 HiiFreeOpCodeHandle (EndOpCodeHandle
);
565 This function processes the results of changes in configuration.
567 @param[in] This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
568 @param[in] Action Specifies the type of action taken by the browser.
569 @param[in] QuestionId A unique value which is sent to the original
570 exporting driver so that it can identify the type
572 @param[in] Type The type of value for the question.
573 @param[in] Value A pointer to the data being sent to the original
575 @param[out] ActionRequest On return, points to the action requested by the
578 @retval EFI_SUCCESS The callback successfully handled the action.
579 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
580 variable and its data.
581 @retval EFI_DEVICE_ERROR The variable could not be saved.
582 @retval EFI_UNSUPPORTED The specified Action is not supported by the
589 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
590 IN EFI_BROWSER_ACTION Action
,
591 IN EFI_QUESTION_ID QuestionId
,
593 IN EFI_IFR_TYPE_VALUE
*Value
,
594 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
598 RAM_DISK_PRIVATE_DATA
*PrivateData
;
599 RAM_DISK_CONFIG_PRIVATE_DATA
*ConfigPrivate
;
600 EFI_DEVICE_PATH_PROTOCOL
*FileDevPath
;
601 EFI_FILE_HANDLE FileHandle
;
603 LIST_ENTRY
*NextEntry
;
605 if ((This
== NULL
) || (Value
== NULL
) || (ActionRequest
== NULL
)) {
606 return EFI_INVALID_PARAMETER
;
609 ConfigPrivate
= RAM_DISK_CONFIG_PRIVATE_FROM_THIS (This
);
611 if (Action
== EFI_BROWSER_ACTION_RETRIEVE
) {
612 Status
= EFI_UNSUPPORTED
;
613 if (QuestionId
== CREATE_RAW_SIZE_QUESTION_ID
) {
614 Value
->u64
= EFI_PAGE_SIZE
;
615 ConfigPrivate
->ConfigStore
.Size
= EFI_PAGE_SIZE
;
616 Status
= EFI_SUCCESS
;
617 } else if (QuestionId
== CREATE_RAW_MEMORY_TYPE_QUESTION_ID
) {
618 Value
->u8
= RAM_DISK_BOOT_SERVICE_DATA_MEMORY
;
619 ConfigPrivate
->ConfigStore
.MemType
= RAM_DISK_BOOT_SERVICE_DATA_MEMORY
;
620 Status
= EFI_SUCCESS
;
625 if ((Action
!= EFI_BROWSER_ACTION_CHANGED
) &&
626 (Action
!= EFI_BROWSER_ACTION_CHANGING
) &&
627 (Action
!= EFI_BROWSER_ACTION_FORM_OPEN
)) {
628 return EFI_UNSUPPORTED
;
632 // Update the RAM disk list show at the main form first.
634 if (Action
== EFI_BROWSER_ACTION_FORM_OPEN
) {
635 Status
= EFI_UNSUPPORTED
;
636 if (QuestionId
== MAIN_GOTO_FILE_EXPLORER_ID
) {
637 UpdateMainForm (ConfigPrivate
);
638 Status
= EFI_SUCCESS
;
643 Status
= EFI_SUCCESS
;
645 if (Action
== EFI_BROWSER_ACTION_CHANGING
) {
646 switch (QuestionId
) {
647 case MAIN_GOTO_FILE_EXPLORER_ID
:
648 Status
= ChooseFile (NULL
, NULL
, NULL
, &FileDevPath
);
649 if (EFI_ERROR (Status
)) {
653 if (FileDevPath
!= NULL
) {
657 Status
= OpenFileByDevicePath (
663 if (EFI_ERROR (Status
)) {
668 // Create from file, RAM disk size is zero. It will be updated
669 // according to the file size.
671 Status
= HiiCreateRamDisk (
674 ConfigPrivate
->ConfigStore
.MemType
676 if (EFI_ERROR (Status
)) {
681 // Refresh the registered RAM disks list.
683 UpdateMainForm (ConfigPrivate
);
690 } else if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
691 switch (QuestionId
) {
692 case MAIN_REMOVE_RD_QUESTION_ID
:
694 // Remove the selected RAM disks
696 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &RegisteredRamDisks
) {
697 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
698 if (PrivateData
->CheckBoxChecked
) {
700 (EFI_DEVICE_PATH_PROTOCOL
*) PrivateData
->DevicePath
705 UpdateMainForm (ConfigPrivate
);
707 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_APPLY
;
710 case CREATE_RAW_SIZE_QUESTION_ID
:
711 ConfigPrivate
->ConfigStore
.Size
= Value
->u64
;
714 case CREATE_RAW_MEMORY_TYPE_QUESTION_ID
:
715 ConfigPrivate
->ConfigStore
.MemType
= Value
->u8
;
718 case CREATE_RAW_SUBMIT_QUESTION_ID
:
720 // Create raw, FileHandle is NULL.
722 Status
= HiiCreateRamDisk (
723 ConfigPrivate
->ConfigStore
.Size
,
725 ConfigPrivate
->ConfigStore
.MemType
727 if (EFI_ERROR (Status
)) {
732 // Refresh the registered RAM disks list.
734 UpdateMainForm (ConfigPrivate
);
736 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
739 case CREATE_RAW_DISCARD_QUESTION_ID
:
740 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_FORM_DISCARD_EXIT
;
745 // QuestionIds for checkboxes
747 if ((QuestionId
>= MAIN_CHECKBOX_QUESTION_ID_START
) &&
748 (QuestionId
< CREATE_RAW_RAM_DISK_FORM_ID
)) {
749 EFI_LIST_FOR_EACH (Entry
, &RegisteredRamDisks
) {
750 PrivateData
= RAM_DISK_PRIVATE_FROM_THIS (Entry
);
751 if (PrivateData
->CheckBoxId
== QuestionId
) {
752 PrivateData
->CheckBoxChecked
= (BOOLEAN
) (Value
->u8
!= 0);