Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 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; |
Heinrich Schuchardt | fd956fc | 2020-07-06 17:06:07 +0200 | [diff] [blame] | 19 | static const efi_guid_t guid_vendor0 = EFI_GLOBAL_VARIABLE_GUID; |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 20 | |
| 21 | /* |
| 22 | * Setup unit test. |
| 23 | * |
| 24 | * @handle handle of the loaded image |
| 25 | * @systable system table |
| 26 | */ |
| 27 | static int setup(const efi_handle_t img_handle, |
| 28 | const struct efi_system_table *systable) |
| 29 | { |
| 30 | boottime = systable->boottime; |
| 31 | runtime = systable->runtime; |
| 32 | |
| 33 | return EFI_ST_SUCCESS; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * execute() - execute unit test |
| 38 | * |
| 39 | * As runtime support is not implmented expect EFI_UNSUPPORTED to be returned. |
| 40 | */ |
| 41 | static int execute(void) |
| 42 | { |
| 43 | efi_status_t ret; |
| 44 | efi_uintn_t len; |
| 45 | u32 attr; |
| 46 | u8 v[16] = {0x5d, 0xd1, 0x5e, 0x51, 0x5a, 0x05, 0xc7, 0x0c, |
| 47 | 0x35, 0x4a, 0xae, 0x87, 0xa5, 0xdf, 0x0f, 0x65,}; |
| 48 | u8 data[EFI_ST_MAX_DATA_SIZE]; |
| 49 | u16 varname[EFI_ST_MAX_VARNAME_SIZE]; |
| 50 | efi_guid_t guid; |
| 51 | u64 max_storage, rem_storage, max_size; |
| 52 | |
| 53 | ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS, |
| 54 | &max_storage, &rem_storage, |
| 55 | &max_size); |
| 56 | if (ret != EFI_UNSUPPORTED) { |
| 57 | efi_st_error("QueryVariableInfo failed\n"); |
| 58 | return EFI_ST_FAILURE; |
| 59 | } |
| 60 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 61 | ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0, |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 62 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 63 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 64 | 3, v + 4); |
Ilias Apalodimas | 86ba869 | 2024-04-18 15:54:50 +0300 | [diff] [blame^] | 65 | if (IS_ENABLED(CONFIG_EFI_RT_VOLATILE_STORE)) { |
| 66 | /* At runtime only non-volatile variables may be set. */ |
| 67 | if (ret != EFI_INVALID_PARAMETER) { |
| 68 | efi_st_error("SetVariable failed\n"); |
| 69 | return EFI_ST_FAILURE; |
| 70 | } |
| 71 | } else { |
| 72 | if (ret != EFI_UNSUPPORTED) { |
| 73 | efi_st_error("SetVariable failed\n"); |
| 74 | return EFI_ST_FAILURE; |
| 75 | } |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 76 | } |
Heinrich Schuchardt | fd956fc | 2020-07-06 17:06:07 +0200 | [diff] [blame] | 77 | len = EFI_ST_MAX_DATA_SIZE; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 78 | ret = runtime->get_variable(u"PlatformLangCodes", &guid_vendor0, |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 79 | &attr, &len, data); |
Heinrich Schuchardt | fd956fc | 2020-07-06 17:06:07 +0200 | [diff] [blame] | 80 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 81 | efi_st_error("GetVariable failed\n"); |
| 82 | return EFI_ST_FAILURE; |
| 83 | } |
| 84 | memset(&guid, 0, 16); |
| 85 | *varname = 0; |
Heinrich Schuchardt | fd956fc | 2020-07-06 17:06:07 +0200 | [diff] [blame] | 86 | len = 2 * EFI_ST_MAX_VARNAME_SIZE; |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 87 | ret = runtime->get_next_variable_name(&len, varname, &guid); |
Heinrich Schuchardt | fd956fc | 2020-07-06 17:06:07 +0200 | [diff] [blame] | 88 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | c0267f9 | 2019-06-20 16:18:48 +0200 | [diff] [blame] | 89 | efi_st_error("GetNextVariableName failed\n"); |
| 90 | return EFI_ST_FAILURE; |
| 91 | } |
| 92 | |
| 93 | return EFI_ST_SUCCESS; |
| 94 | } |
| 95 | |
| 96 | EFI_UNIT_TEST(variables_run) = { |
| 97 | .name = "variables at runtime", |
| 98 | .phase = EFI_SETUP_BEFORE_BOOTTIME_EXIT, |
| 99 | .setup = setup, |
| 100 | .execute = execute, |
| 101 | }; |