Files
edk2-upstream/MdeModulePkg/Include/Library/VariablePolicyHelperLib.h
Michael Kubacki 7a934d0bef MdeModulePkg: Replace include guards with #pragma once
Replace traditional `#ifndef`/`#define`/`#endif` include guards with
`#pragma` once.

`#pragma once` is a widely supported preprocessor directive that
prevents header files from being included multiple times. It is
supported by all toolchains used to build edk2: GCC, Clang/LLVM, and
MSVC.

Compared to macro-based include guards, `#pragma once`:

- Eliminates the risk of macro name collisions or copy/paste errors
  where two headers inadvertently use the same guard macro.
- Eliminate inconsistency in the way include guard macros are named
  (e.g., some files use `__FILE_H__`, others use `FILE_H_`, etc.).
- Reduces boilerplate (three lines replaced by one).
- Avoids polluting the macro namespace with guard symbols.
- Can improve build times as the preprocessor can skip re-opening the
  file entirely, rather than re-reading it to find the matching
  `#endif` ("multiple-include optimization").
  - Note that some compilers may already optimize traditional include
    guards, by recognzining the idiomatic pattern.

This change is made acknowledging that overall portability of the
code will technically be reduced, as `#pragma once` is not part of the
C/C++ standards.

However, this is considered acceptable given:

1. edk2 already defines a subset of supported compilers in
   BaseTools/Conf/tools_def.template, all of which have supported
   `#pragma once` for over two decades.
2. There have been concerns raised to the project about inconsistent
   include guard naming and potential macro collisions.

Approximate compiler support dates:

- MSVC: Supported since Visual C++ 4.2 (1996)
- GCC: Supported since 3.4 (2004)
  (http://gnu.ist.utl.pt/software/gcc/gcc-3.4/changes.html)
- Clang (LLVM based): Since initial release in 2007

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
2026-02-23 21:01:28 +00:00

159 lines
8.1 KiB
C

/** @file -- VariablePolicyHelperLib.h
This library contains helper functions for marshalling and registering
new policies with the VariablePolicy infrastructure.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#pragma once
#include <Protocol/VariablePolicy.h>
/**
This helper function will allocate and populate a new VariablePolicy
structure for a policy that does not contain any sub-structures (such as
VARIABLE_LOCK_ON_VAR_STATE_POLICY).
NOTE: Caller will need to free structure once finished.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] LockPolicyType LockPolicyType for the VariablePolicy.
@param[out] NewEntry If successful, will be set to a pointer to the allocated buffer containing the
new policy.
@retval EFI_SUCCESS Operation completed successfully and structure is populated.
@retval EFI_INVALID_PARAMETER Namespace is NULL.
@retval EFI_INVALID_PARAMETER LockPolicyType is invalid for a basic structure.
@retval EFI_BUFFER_TOO_SMALL Finished structure would not fit in UINT16 size.
@retval EFI_OUT_OF_RESOURCES Could not allocate sufficient space for structure.
**/
EFI_STATUS
EFIAPI
CreateBasicVariablePolicy (
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN UINT8 LockPolicyType,
OUT VARIABLE_POLICY_ENTRY **NewEntry
);
/**
This helper function will allocate and populate a new VariablePolicy
structure for a policy with a lock type of VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE.
NOTE: Caller will need to free structure once finished.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] VarStateNamespace Pointer to the EFI_GUID for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace.
@param[in] VarStateValue Value for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value.
@param[in] VarStateName Pointer to the CHAR16 array for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name.
@param[out] NewEntry If successful, will be set to a pointer to the allocated buffer containing the
new policy.
@retval EFI_SUCCESS Operation completed successfully and structure is populated.
@retval EFI_INVALID_PARAMETER Namespace, VarStateNamespace, VarStateName is NULL.
@retval EFI_BUFFER_TOO_SMALL Finished structure would not fit in UINT16 size.
@retval EFI_OUT_OF_RESOURCES Could not allocate sufficient space for structure.
**/
EFI_STATUS
EFIAPI
CreateVarStateVariablePolicy (
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN CONST EFI_GUID *VarStateNamespace,
IN UINT8 VarStateValue,
IN CONST CHAR16 *VarStateName,
OUT VARIABLE_POLICY_ENTRY **NewEntry
);
/**
This helper function does everything that CreateBasicVariablePolicy() does, but also
uses the passed in protocol to register the policy with the infrastructure.
Does not return a buffer, does not require the caller to free anything.
@param[in] VariablePolicy Pointer to a valid instance of the VariablePolicy protocol.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] LockPolicyType LockPolicyType for the VariablePolicy.
@retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL.
@retval EFI_STATUS Status returned by CreateBasicVariablePolicy() or RegisterVariablePolicy().
**/
EFI_STATUS
EFIAPI
RegisterBasicVariablePolicy (
IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy,
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN UINT8 LockPolicyType
);
/**
This helper function does everything that CreateBasicVariablePolicy() does, but also
uses the passed in protocol to register the policy with the infrastructure.
Does not return a buffer, does not require the caller to free anything.
@param[in] VariablePolicy Pointer to a valid instance of the VariablePolicy protocol.
@param[in] Namespace Pointer to an EFI_GUID for the target variable namespace that this policy will protect.
@param[in] Name [Optional] If provided, a pointer to the CHAR16 array for the target variable name.
Otherwise, will create a policy that targets an entire namespace.
@param[in] MinSize MinSize for the VariablePolicy.
@param[in] MaxSize MaxSize for the VariablePolicy.
@param[in] AttributesMustHave AttributesMustHave for the VariablePolicy.
@param[in] AttributesCantHave AttributesCantHave for the VariablePolicy.
@param[in] VarStateNamespace Pointer to the EFI_GUID for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace.
@param[in] VarStateName Pointer to the CHAR16 array for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name.
@param[in] VarStateValue Value for the VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value.
@retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL.
@retval EFI_STATUS Status returned by CreateBasicVariablePolicy() or RegisterVariablePolicy().
**/
EFI_STATUS
EFIAPI
RegisterVarStateVariablePolicy (
IN EDKII_VARIABLE_POLICY_PROTOCOL *VariablePolicy,
IN CONST EFI_GUID *Namespace,
IN CONST CHAR16 *Name OPTIONAL,
IN UINT32 MinSize,
IN UINT32 MaxSize,
IN UINT32 AttributesMustHave,
IN UINT32 AttributesCantHave,
IN CONST EFI_GUID *VarStateNamespace,
IN CONST CHAR16 *VarStateName,
IN UINT8 VarStateValue
);