Skip to content

Commit

Permalink
Add basic wrappers to the Policy Library (#396)
Browse files Browse the repository at this point in the history
## Description

Adds basic wrappers for the policy service PPI/Protocols to the
PolicyLib as well as a basic test to invoke them.

- [x] Impacts functionality?
- [ ] Impacts security?
- [ ] Breaking change?
- [x] Includes tests?
- [ ] Includes documentation?

## How This Was Tested

Tested using the included test on QEMU.

## Integration Instructions

VerifiedPolicy.h has been renamed to PolicyLib.h for clarify. Update any
code using VerifiedPolicy.h

(cherry picked from commit d4d7856)
  • Loading branch information
cfernald authored and kenlautner committed Dec 18, 2023
1 parent a8dee78 commit ab9d819
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/** @file
Definitions for the verified policy libraries.
Definitions for the policy libraries.
Copyright (c) Microsoft Corporation
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef _VERIFIED_POLICY_H_
#define _VERIFIED_POLICY_H_
#ifndef _POLICY_LIB_H_
#define _POLICY_LIB_H_

#define VERIFIED_POLICY_LIB_VERSION 1

Expand All @@ -24,6 +24,66 @@ typedef struct _VERIFIED_POLICY_HEADER {

typedef VERIFIED_POLICY_HEADER VERIFIED_POLICY_DESCRIPTOR;

/**
Creates or updates a policy in the policy store. Will notify any applicable
callbacks.
@param[in] PolicyGuid The uniquely identifying GUID for the policy.
@param[in] Attributes Attributes of the policy to be set.
@param[in] Policy The policy data buffer. This buffer will be
copied into the data store.
@param[in] PolicySize The size of the provided policy data.
@retval EFI_SUCCESS Policy was created or updated.
@retval EFI_ACCESS_DENIED Policy was already finalized prior to this call.
@retval EFI_OUT_OF_RESOURCES Failed to allocate space for policy structures.
**/
EFI_STATUS
EFIAPI
SetPolicy (
IN CONST EFI_GUID *PolicyGuid,
IN UINT64 Attributes,
IN VOID *Policy,
IN UINT16 PolicySize
);

/**
Retrieves the policy descriptor, buffer, and size for a given policy GUID.
@param[in] PolicyGuid The GUID of the policy being retrieved.
@param[out] Attributes The attributes of the stored policy.
@param[out] Policy The buffer where the policy data is copied.
@param[in,out] PolicySize The size of the stored policy data buffer.
On output, contains the size of the stored policy.
@retval EFI_SUCCESS The policy was retrieved.
@retval EFI_BUFFER_TOO_SMALL The provided buffer size was too small.
@retval EFI_NOT_FOUND The policy does not exist.
**/
EFI_STATUS
EFIAPI
GetPolicy (
IN CONST EFI_GUID *PolicyGuid,
OUT UINT64 *Attributes OPTIONAL,
OUT VOID *Policy,
IN OUT UINT16 *PolicySize
);

/**
Removes a policy from the policy store. The policy will be removed from the store
and freed if possible.
@param[in] PolicyGuid The GUID of the policy being retrieved.
@retval EFI_SUCCESS The policy was removed.
@retval EFI_NOT_FOUND The policy does not exist.
**/
EFI_STATUS
EFIAPI
RemovePolicy (
IN CONST EFI_GUID *PolicyGuid
);

/**
Retrieves a verified policy of the given type from the policy store.
Expand Down
2 changes: 1 addition & 1 deletion PolicyServicePkg/Library/DxePolicyLib/DxePolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <Library/BaseLib.h>

#include <Protocol/Policy.h>
#include <Library/VerifiedPolicy.h>
#include <Library/PolicyLib.h>

#include "../PolicyLibCommon.h"

Expand Down
2 changes: 1 addition & 1 deletion PolicyServicePkg/Library/MmPolicyLib/MmPolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <Library/BaseLib.h>

#include <Protocol/MmPolicy.h>
#include <Library/VerifiedPolicy.h>
#include <Library/PolicyLib.h>

#include "../PolicyLibCommon.h"

Expand Down
2 changes: 1 addition & 1 deletion PolicyServicePkg/Library/PeiPolicyLib/PeiPolicy.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <Library/PeiServicesLib.h>

#include <Ppi/Policy.h>
#include <Library/VerifiedPolicy.h>
#include <Library/PolicyLib.h>

#include "../PolicyLibCommon.h"

Expand Down
95 changes: 94 additions & 1 deletion PolicyServicePkg/Library/PolicyLibCommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,104 @@
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>

#include <Library/VerifiedPolicy.h>
#include <Library/PolicyLib.h>
#include <PolicyInterface.h>

#include "../PolicyLibCommon.h"

/**
Creates or updates a policy in the policy store. Will notify any applicable
callbacks.
@param[in] PolicyGuid The uniquely identifying GUID for the policy.
@param[in] Attributes Attributes of the policy to be set.
@param[in] Policy The policy data buffer. This buffer will be
copied into the data store.
@param[in] PolicySize The size of the provided policy data.
@retval EFI_SUCCESS Policy was created or updated.
@retval EFI_ACCESS_DENIED Policy was already finalized prior to this call.
@retval EFI_OUT_OF_RESOURCES Failed to allocate space for policy structures.
**/
EFI_STATUS
EFIAPI
SetPolicy (
IN CONST EFI_GUID *PolicyGuid,
IN UINT64 Attributes,
IN VOID *Policy,
IN UINT16 PolicySize
)
{
POLICY_INTERFACE *PolicyService;
EFI_STATUS Status;

Status = GetPolicyInterface (&PolicyService);
if (!EFI_ERROR (Status)) {
Status = PolicyService->SetPolicy (PolicyGuid, Attributes, Policy, PolicySize);
}

return Status;
}

/**
Retrieves the policy descriptor, buffer, and size for a given policy GUID.
@param[in] PolicyGuid The GUID of the policy being retrieved.
@param[out] Attributes The attributes of the stored policy.
@param[out] Policy The buffer where the policy data is copied.
@param[in,out] PolicySize The size of the stored policy data buffer.
On output, contains the size of the stored policy.
@retval EFI_SUCCESS The policy was retrieved.
@retval EFI_BUFFER_TOO_SMALL The provided buffer size was too small.
@retval EFI_NOT_FOUND The policy does not exist.
**/
EFI_STATUS
EFIAPI
GetPolicy (
IN CONST EFI_GUID *PolicyGuid,
OUT UINT64 *Attributes OPTIONAL,
OUT VOID *Policy,
IN OUT UINT16 *PolicySize
)
{
POLICY_INTERFACE *PolicyService;
EFI_STATUS Status;

Status = GetPolicyInterface (&PolicyService);
if (!EFI_ERROR (Status)) {
Status = PolicyService->GetPolicy (PolicyGuid, Attributes, Policy, PolicySize);
}

return Status;
}

/**
Removes a policy from the policy store. The policy will be removed from the store
and freed if possible.
@param[in] PolicyGuid The GUID of the policy being retrieved.
@retval EFI_SUCCESS The policy was removed.
@retval EFI_NOT_FOUND The policy does not exist.
**/
EFI_STATUS
EFIAPI
RemovePolicy (
IN CONST EFI_GUID *PolicyGuid
)
{
POLICY_INTERFACE *PolicyService;
EFI_STATUS Status;

Status = GetPolicyInterface (&PolicyService);
if (!EFI_ERROR (Status)) {
Status = PolicyService->RemovePolicy (PolicyGuid);
}

return Status;
}

/**
Retrieves a verified policy of the given type from the policy store.
Expand Down
6 changes: 3 additions & 3 deletions PolicyServicePkg/PolicyService/DxeMm/PolicyCommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ CheckPolicyExists (
**/
EFI_STATUS
EFIAPI
GetPolicy (
CommonGetPolicy (
IN CONST EFI_GUID *PolicyGuid,
OUT UINT64 *Attributes OPTIONAL,
OUT VOID *Policy,
Expand Down Expand Up @@ -201,7 +201,7 @@ IngestPoliciesFromHob (
**/
EFI_STATUS
EFIAPI
RemovePolicy (
CommonRemovePolicy (
IN CONST EFI_GUID *PolicyGuid
)
{
Expand Down Expand Up @@ -243,7 +243,7 @@ RemovePolicy (
**/
EFI_STATUS
EFIAPI
SetPolicy (
CommonSetPolicy (
IN CONST EFI_GUID *PolicyGuid,
IN UINT64 Attributes,
IN VOID *Policy,
Expand Down
6 changes: 3 additions & 3 deletions PolicyServicePkg/PolicyService/DxeMm/PolicyCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct _POLICY_ENTRY {
**/
EFI_STATUS
EFIAPI
SetPolicy (
CommonSetPolicy (
IN CONST EFI_GUID *PolicyGuid,
IN UINT64 Attributes,
IN VOID *Policy,
Expand All @@ -79,7 +79,7 @@ SetPolicy (
**/
EFI_STATUS
EFIAPI
GetPolicy (
CommonGetPolicy (
IN CONST EFI_GUID *PolicyGuid,
OUT UINT64 *Attributes OPTIONAL,
OUT VOID *Policy,
Expand All @@ -97,7 +97,7 @@ GetPolicy (
**/
EFI_STATUS
EFIAPI
RemovePolicy (
CommonRemovePolicy (
IN CONST EFI_GUID *PolicyGuid
);

Expand Down
6 changes: 3 additions & 3 deletions PolicyServicePkg/PolicyService/DxeMm/PolicyDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ STATIC EFI_LOCK mPolicyListLock = EFI_INITIALIZE_LOCK_VARIABLE (TPL_NOTIFY);
STATIC EFI_HANDLE mImageHandle = NULL;

POLICY_PROTOCOL mPolicyProtocol = {
SetPolicy,
GetPolicy,
RemovePolicy
CommonSetPolicy,
CommonGetPolicy,
CommonRemovePolicy
};

/**
Expand Down
6 changes: 3 additions & 3 deletions PolicyServicePkg/PolicyService/DxeMm/PolicyMm.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
STATIC EFI_HANDLE mProtocolHandle = NULL;

MM_POLICY_PROTOCOL mPolicyProtocol = {
SetPolicy,
GetPolicy,
RemovePolicy
CommonSetPolicy,
CommonGetPolicy,
CommonRemovePolicy
};

/**
Expand Down
2 changes: 1 addition & 1 deletion PolicyServicePkg/PolicyServicePkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Include

[LibraryClasses]
PolicyLib|Include/Library/VerifiedPolicy.h
PolicyLib|Include/Library/PolicyLib.h

[Guids]
## GUID used for storing policy data in HOB.
Expand Down
10 changes: 5 additions & 5 deletions PolicyServicePkg/Samples/PolicyDefinitions/PolicyDataStructGFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#ifndef __POLICY_DATA_STRUCT_GFX_H__
#define __POLICY_DATA_STRUCT_GFX_H__

#include <Library/VerifiedPolicy.h>
#include <Library/PolicyLib.h>

#pragma pack(1)

Expand All @@ -23,16 +23,16 @@


typedef struct {

/* Power state of GFX port 0 */
UINT16 Power_State_Port_0;

/* Power state of GFX port 1 */
UINT16 Power_State_Port_1;

/* Flag to skip this controller or not */
UINT16 Skip_Check_0;

/* Flag to skip this controller or not */
UINT16 Skip_Check_1;

Expand Down
Loading

0 comments on commit ab9d819

Please sign in to comment.