blob: 81a6cc6625f0277b862d3890cc90f5ad8d0c1a2e [file] [log] [blame]
Julian Hall0a86f762021-11-08 13:31:23 +00001/*
Julian Hall6e02acf2022-02-22 16:25:03 +00002 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
Julian Hall0a86f762021-11-08 13:31:23 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "variable_checker.h"
9
10efi_status_t variable_checker_set_constraints(
11 struct variable_constraints *constraints,
12 bool is_update,
13 const VAR_CHECK_VARIABLE_PROPERTY *check_var_property)
14{
15 /* Sanity check input parameters */
16 if (check_var_property->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION)
17 return EFI_INVALID_PARAMETER;
18
19 if (check_var_property->MinSize > check_var_property->MaxSize)
20 return EFI_INVALID_PARAMETER;
21
22 /* Check for an attempt to undo previously set access constraints */
23 if (is_update) {
24
25 if ((constraints->property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) &&
26 !(check_var_property->Property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY))
27 return EFI_INVALID_PARAMETER;
28 }
29
30 /* New check constraints accepted */
31 constraints->revision = check_var_property->Revision;
32 constraints->attributes = check_var_property->Attributes;
33 constraints->property = check_var_property->Property;
34 constraints->min_size = check_var_property->MinSize;
35 constraints->max_size = check_var_property->MaxSize;
36
37 return EFI_SUCCESS;
38}
39
40void variable_checker_get_constraints(
41 const struct variable_constraints *constraints,
42 VAR_CHECK_VARIABLE_PROPERTY *check_var_property)
43{
44 check_var_property->Revision = constraints->revision;
45 check_var_property->Attributes = constraints->attributes;
46 check_var_property->Property = constraints->property;
47 check_var_property->MinSize = constraints->min_size;
48 check_var_property->MaxSize = constraints->max_size;
49}
50
51efi_status_t variable_checker_check_on_set(
52 const struct variable_constraints *constraints,
53 uint32_t attributes,
54 size_t data_size)
55{
Julian Hall6e02acf2022-02-22 16:25:03 +000056 (void)attributes;
57
Julian Hall0a86f762021-11-08 13:31:23 +000058 if (constraints->property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
59 return EFI_WRITE_PROTECTED;
60
61 if (data_size < constraints->min_size)
62 return EFI_INVALID_PARAMETER;
63
64 if (data_size > constraints->max_size)
65 return EFI_INVALID_PARAMETER;
66
67 return EFI_SUCCESS;
68}