blob: 81a41d01cc773d3a6c59208d62dec209b2132d86 [file] [log] [blame]
Julian Hall0a86f762021-11-08 13:31:23 +00001/*
2 * Copyright (c) 2021, Arm Limited. All rights reserved.
3 *
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{
56 if (constraints->property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
57 return EFI_WRITE_PROTECTED;
58
59 if (data_size < constraints->min_size)
60 return EFI_INVALID_PARAMETER;
61
62 if (data_size > constraints->max_size)
63 return EFI_INVALID_PARAMETER;
64
65 return EFI_SUCCESS;
66}