efi_loader: validate load option
[oweals/u-boot.git] / lib / efi_selftest / efi_selftest_variables_runtime.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * efi_selftest_variables_runtime
4  *
5  * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
6  *
7  * This unit test checks the runtime services for variables after
8  * ExitBootServices():
9  * GetVariable, GetNextVariableName, SetVariable, QueryVariableInfo.
10  */
11
12 #include <efi_selftest.h>
13
14 #define EFI_ST_MAX_DATA_SIZE 16
15 #define EFI_ST_MAX_VARNAME_SIZE 40
16
17 static struct efi_boot_services *boottime;
18 static struct efi_runtime_services *runtime;
19 static const efi_guid_t guid_vendor0 =
20         EFI_GUID(0x67029eb5, 0x0af2, 0xf6b1,
21                  0xda, 0x53, 0xfc, 0xb5, 0x66, 0xdd, 0x1c, 0xe6);
22
23 /*
24  * Setup unit test.
25  *
26  * @handle      handle of the loaded image
27  * @systable    system table
28  */
29 static int setup(const efi_handle_t img_handle,
30                  const struct efi_system_table *systable)
31 {
32         boottime = systable->boottime;
33         runtime = systable->runtime;
34
35         return EFI_ST_SUCCESS;
36 }
37
38 /**
39  * execute() - execute unit test
40  *
41  * As runtime support is not implmented expect EFI_UNSUPPORTED to be returned.
42  */
43 static int execute(void)
44 {
45         efi_status_t ret;
46         efi_uintn_t len;
47         u32 attr;
48         u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c,
49                     0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,};
50         u8 data[EFI_ST_MAX_DATA_SIZE];
51         u16 varname[EFI_ST_MAX_VARNAME_SIZE];
52         efi_guid_t guid;
53         u64 max_storage, rem_storage, max_size;
54
55         ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS,
56                                            &max_storage, &rem_storage,
57                                            &max_size);
58         if (ret != EFI_UNSUPPORTED) {
59                 efi_st_error("QueryVariableInfo failed\n");
60                 return EFI_ST_FAILURE;
61         }
62
63         ret = runtime->set_variable(L"efi_st_var0", &guid_vendor0,
64                                     EFI_VARIABLE_BOOTSERVICE_ACCESS |
65                                     EFI_VARIABLE_RUNTIME_ACCESS,
66                                     3, v + 4);
67         if (ret != EFI_UNSUPPORTED) {
68                 efi_st_error("SetVariable failed\n");
69                 return EFI_ST_FAILURE;
70         }
71         len = 3;
72         ret = runtime->get_variable(L"efi_st_var0", &guid_vendor0,
73                                     &attr, &len, data);
74         if (ret != EFI_UNSUPPORTED) {
75                 efi_st_error("GetVariable failed\n");
76                 return EFI_ST_FAILURE;
77         }
78         memset(&guid, 0, 16);
79         *varname = 0;
80         ret = runtime->get_next_variable_name(&len, varname, &guid);
81         if (ret != EFI_UNSUPPORTED) {
82                 efi_st_error("GetNextVariableName failed\n");
83                 return EFI_ST_FAILURE;
84         }
85
86         return EFI_ST_SUCCESS;
87 }
88
89 EFI_UNIT_TEST(variables_run) = {
90         .name = "variables at runtime",
91         .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT,
92         .setup = setup,
93         .execute = execute,
94 };