Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * efi_selftest_variables |
| 4 | * |
| 5 | * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de> |
| 6 | * |
Heinrich Schuchardt | 702e778 | 2018-09-27 20:44:40 +0200 | [diff] [blame] | 7 | * This unit test checks the runtime services for variables: |
| 8 | * GetVariable, GetNextVariableName, SetVariable, QueryVariableInfo. |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <efi_selftest.h> |
| 12 | |
| 13 | #define EFI_ST_MAX_DATA_SIZE 16 |
Heinrich Schuchardt | c169187 | 2020-03-20 19:20:17 +0100 | [diff] [blame] | 14 | #define EFI_ST_MAX_VARNAME_SIZE 80 |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 15 | |
| 16 | static struct efi_boot_services *boottime; |
| 17 | static struct efi_runtime_services *runtime; |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 18 | static const efi_guid_t guid_vendor0 = |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 19 | EFI_GUID(0x67029eb5, 0x0af2, 0xf6b1, |
| 20 | 0xda, 0x53, 0xfc, 0xb5, 0x66, 0xdd, 0x1c, 0xe6); |
Heinrich Schuchardt | e06ae19 | 2018-12-30 20:53:51 +0100 | [diff] [blame] | 21 | static const efi_guid_t guid_vendor1 = |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 22 | EFI_GUID(0xff629290, 0x1fc1, 0xd73f, |
| 23 | 0x8f, 0xb1, 0x32, 0xf9, 0x0c, 0xa0, 0x42, 0xea); |
| 24 | |
| 25 | /* |
| 26 | * Setup unit test. |
| 27 | * |
| 28 | * @handle handle of the loaded image |
| 29 | * @systable system table |
| 30 | */ |
| 31 | static int setup(const efi_handle_t img_handle, |
| 32 | const struct efi_system_table *systable) |
| 33 | { |
| 34 | boottime = systable->boottime; |
| 35 | runtime = systable->runtime; |
| 36 | |
| 37 | return EFI_ST_SUCCESS; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Execute unit test. |
| 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,}; |
Ivan Gorinov | da168a4 | 2018-05-25 10:45:09 -0700 | [diff] [blame] | 50 | u8 data[EFI_ST_MAX_DATA_SIZE]; |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 51 | u16 varname[EFI_ST_MAX_VARNAME_SIZE]; |
| 52 | int flag; |
| 53 | efi_guid_t guid; |
| 54 | u64 max_storage, rem_storage, max_size; |
| 55 | |
| 56 | ret = runtime->query_variable_info(EFI_VARIABLE_BOOTSERVICE_ACCESS, |
| 57 | &max_storage, &rem_storage, |
| 58 | &max_size); |
| 59 | if (ret != EFI_SUCCESS) { |
| 60 | efi_st_todo("QueryVariableInfo failed\n"); |
| 61 | } else if (!max_storage || !rem_storage || !max_size) { |
| 62 | efi_st_error("QueryVariableInfo: wrong info\n"); |
| 63 | return EFI_ST_FAILURE; |
| 64 | } |
| 65 | /* Set variable 0 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 66 | ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 67 | EFI_VARIABLE_BOOTSERVICE_ACCESS, |
| 68 | 3, v + 4); |
| 69 | if (ret != EFI_SUCCESS) { |
| 70 | efi_st_error("SetVariable failed\n"); |
| 71 | return EFI_ST_FAILURE; |
| 72 | } |
Ivan Gorinov | da168a4 | 2018-05-25 10:45:09 -0700 | [diff] [blame] | 73 | data[3] = 0xff; |
| 74 | len = 3; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 75 | ret = runtime->get_variable(u"efi_st_var0", &guid_vendor0, |
Ivan Gorinov | da168a4 | 2018-05-25 10:45:09 -0700 | [diff] [blame] | 76 | &attr, &len, data); |
| 77 | if (ret != EFI_SUCCESS) { |
| 78 | efi_st_error("GetVariable failed\n"); |
| 79 | return EFI_ST_FAILURE; |
| 80 | } |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 81 | if (memcmp(data, v + 4, 3)) { |
Ivan Gorinov | da168a4 | 2018-05-25 10:45:09 -0700 | [diff] [blame] | 82 | efi_st_error("GetVariable returned wrong value\n"); |
| 83 | return EFI_ST_FAILURE; |
| 84 | } |
| 85 | if (data[3] != 0xff) { |
| 86 | efi_st_error("GetVariable wrote past the end of the buffer\n"); |
| 87 | return EFI_ST_FAILURE; |
| 88 | } |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 89 | /* Set variable 1 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 90 | ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 91 | EFI_VARIABLE_BOOTSERVICE_ACCESS, |
| 92 | 8, v); |
| 93 | if (ret != EFI_SUCCESS) { |
| 94 | efi_st_error("SetVariable failed\n"); |
| 95 | return EFI_ST_FAILURE; |
| 96 | } |
| 97 | len = EFI_ST_MAX_DATA_SIZE; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 98 | ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 99 | &attr, &len, data); |
| 100 | if (ret != EFI_SUCCESS) { |
| 101 | efi_st_error("GetVariable failed\n"); |
| 102 | return EFI_ST_FAILURE; |
| 103 | } |
| 104 | if (len != 8) { |
| 105 | efi_st_error("GetVariable returned wrong length %u\n", |
| 106 | (unsigned int)len); |
| 107 | return EFI_ST_FAILURE; |
| 108 | } |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 109 | if (memcmp(data, v, 8)) { |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 110 | efi_st_error("GetVariable returned wrong value\n"); |
| 111 | return EFI_ST_FAILURE; |
| 112 | } |
| 113 | /* Append variable 1 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 114 | ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 115 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 116 | EFI_VARIABLE_APPEND_WRITE, |
| 117 | 7, v + 8); |
| 118 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | b4248cf | 2019-09-06 15:09:53 +0900 | [diff] [blame] | 119 | efi_st_error("SetVariable(APPEND_WRITE) failed\n"); |
Heinrich Schuchardt | f69962a | 2019-09-26 21:35:20 +0200 | [diff] [blame] | 120 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 121 | } |
Heinrich Schuchardt | f69962a | 2019-09-26 21:35:20 +0200 | [diff] [blame] | 122 | len = EFI_ST_MAX_DATA_SIZE; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 123 | ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1, |
Heinrich Schuchardt | f69962a | 2019-09-26 21:35:20 +0200 | [diff] [blame] | 124 | &attr, &len, data); |
| 125 | if (ret != EFI_SUCCESS) { |
| 126 | efi_st_error("GetVariable failed\n"); |
| 127 | return EFI_ST_FAILURE; |
| 128 | } |
| 129 | if (len != 15) |
| 130 | efi_st_todo("GetVariable returned wrong length %u\n", |
| 131 | (unsigned int)len); |
| 132 | if (memcmp(data, v, len)) |
| 133 | efi_st_todo("GetVariable returned wrong value\n"); |
Masahisa Kojima | 93a8c24 | 2024-04-02 18:09:50 +0900 | [diff] [blame] | 134 | |
| 135 | /* Append variable 2, write to non-existent variable with datasize=0 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 136 | ret = runtime->set_variable(u"efi_none", &guid_vendor1, |
AKASHI Takahiro | b4248cf | 2019-09-06 15:09:53 +0900 | [diff] [blame] | 137 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 138 | EFI_VARIABLE_APPEND_WRITE, |
Masahisa Kojima | 93a8c24 | 2024-04-02 18:09:50 +0900 | [diff] [blame] | 139 | 0, v); |
| 140 | if (ret != EFI_SUCCESS) { |
| 141 | efi_st_error( |
| 142 | "SetVariable(APPEND_WRITE) with size 0 to non-existent variable returns wrong code\n"); |
| 143 | return EFI_ST_FAILURE; |
| 144 | } |
| 145 | len = EFI_ST_MAX_DATA_SIZE; |
| 146 | ret = runtime->get_variable(u"efi_none", &guid_vendor1, |
| 147 | &attr, &len, data); |
| 148 | if (ret != EFI_NOT_FOUND) { |
| 149 | efi_st_error("Variable must not be created\n"); |
| 150 | return EFI_ST_FAILURE; |
| 151 | } |
| 152 | /* Append variable 2, write to non-existent variable with valid data size*/ |
| 153 | ret = runtime->set_variable(u"efi_none", &guid_vendor1, |
| 154 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 155 | EFI_VARIABLE_APPEND_WRITE, |
AKASHI Takahiro | b4248cf | 2019-09-06 15:09:53 +0900 | [diff] [blame] | 156 | 15, v); |
Masahisa Kojima | 93a8c24 | 2024-04-02 18:09:50 +0900 | [diff] [blame] | 157 | if (ret != EFI_SUCCESS) { |
| 158 | efi_st_error("SetVariable(APPEND_WRITE) with valid size and data to non-existent variable must be succcessful\n"); |
| 159 | return EFI_ST_FAILURE; |
| 160 | } |
| 161 | len = EFI_ST_MAX_DATA_SIZE; |
| 162 | ret = runtime->get_variable(u"efi_none", &guid_vendor1, |
| 163 | &attr, &len, data); |
| 164 | if (ret != EFI_SUCCESS) { |
| 165 | efi_st_error("GetVariable failed\n"); |
| 166 | return EFI_ST_FAILURE; |
| 167 | } |
| 168 | if (len != 15) |
| 169 | efi_st_todo("GetVariable returned wrong length %u\n", |
| 170 | (unsigned int)len); |
| 171 | if (memcmp(data, v, len)) |
| 172 | efi_st_todo("GetVariable returned wrong value\n"); |
| 173 | /* Delete variable efi_none */ |
| 174 | ret = runtime->set_variable(u"efi_none", &guid_vendor1, |
| 175 | 0, 0, NULL); |
| 176 | if (ret != EFI_SUCCESS) { |
| 177 | efi_st_error("SetVariable failed\n"); |
| 178 | return EFI_ST_FAILURE; |
| 179 | } |
| 180 | len = EFI_ST_MAX_DATA_SIZE; |
| 181 | ret = runtime->get_variable(u"efi_none", &guid_vendor1, |
| 182 | &attr, &len, data); |
Heinrich Schuchardt | f69962a | 2019-09-26 21:35:20 +0200 | [diff] [blame] | 183 | if (ret != EFI_NOT_FOUND) { |
Masahisa Kojima | 93a8c24 | 2024-04-02 18:09:50 +0900 | [diff] [blame] | 184 | efi_st_error("Variable was not deleted\n"); |
Heinrich Schuchardt | f69962a | 2019-09-26 21:35:20 +0200 | [diff] [blame] | 185 | return EFI_ST_FAILURE; |
| 186 | } |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 187 | /* Enumerate variables */ |
Heinrich Schuchardt | d6bed22 | 2022-12-18 06:08:58 +0000 | [diff] [blame] | 188 | |
| 189 | ret = runtime->get_next_variable_name(NULL, u"efi_st_var1", &guid); |
| 190 | if (ret != EFI_INVALID_PARAMETER) { |
| 191 | efi_st_error("GetNextVariableName missing parameter check\n"); |
| 192 | return EFI_ST_FAILURE; |
| 193 | } |
| 194 | |
| 195 | len = 24; |
| 196 | ret = runtime->get_next_variable_name(&len, NULL, &guid); |
| 197 | if (ret != EFI_INVALID_PARAMETER) { |
| 198 | efi_st_error("GetNextVariableName missing parameter check\n"); |
| 199 | return EFI_ST_FAILURE; |
| 200 | } |
| 201 | |
| 202 | len = 24; |
| 203 | ret = runtime->get_next_variable_name(&len, u"efi_st_var1", NULL); |
| 204 | if (ret != EFI_INVALID_PARAMETER) { |
| 205 | efi_st_error("GetNextVariableName missing parameter check\n"); |
| 206 | return EFI_ST_FAILURE; |
| 207 | } |
| 208 | |
| 209 | len = 1; |
| 210 | ret = runtime->get_next_variable_name(&len, u"", &guid); |
| 211 | if (ret != EFI_INVALID_PARAMETER) { |
| 212 | efi_st_error("GetNextVariableName missing parameter check\n"); |
| 213 | return EFI_ST_FAILURE; |
| 214 | } |
| 215 | |
| 216 | len = 16; |
| 217 | ret = runtime->get_next_variable_name(&len, u"efi_st_var1", &guid); |
| 218 | if (ret != EFI_INVALID_PARAMETER) { |
| 219 | efi_st_error("GetNextVariableName missing parameter check\n"); |
| 220 | return EFI_ST_FAILURE; |
| 221 | } |
| 222 | |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 223 | boottime->set_mem(&guid, 16, 0); |
| 224 | *varname = 0; |
| 225 | flag = 0; |
| 226 | for (;;) { |
| 227 | len = EFI_ST_MAX_VARNAME_SIZE; |
| 228 | ret = runtime->get_next_variable_name(&len, varname, &guid); |
| 229 | if (ret == EFI_NOT_FOUND) |
| 230 | break; |
| 231 | if (ret != EFI_SUCCESS) { |
AKASHI Takahiro | b3d562e | 2019-01-21 12:43:14 +0100 | [diff] [blame] | 232 | efi_st_error("GetNextVariableName failed (%u)\n", |
| 233 | (unsigned int)ret); |
| 234 | return EFI_ST_FAILURE; |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 235 | } |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 236 | if (!memcmp(&guid, &guid_vendor0, sizeof(efi_guid_t)) && |
Heinrich Schuchardt | c169187 | 2020-03-20 19:20:17 +0100 | [diff] [blame] | 237 | !efi_st_strcmp_16_8(varname, "efi_st_var0")) { |
AKASHI Takahiro | b3d562e | 2019-01-21 12:43:14 +0100 | [diff] [blame] | 238 | flag |= 1; |
Heinrich Schuchardt | c169187 | 2020-03-20 19:20:17 +0100 | [diff] [blame] | 239 | if (len != 24) { |
| 240 | efi_st_error("GetNextVariableName report wrong length %u, expected 24\n", |
| 241 | (unsigned int)len); |
| 242 | return EFI_ST_FAILURE; |
| 243 | } |
| 244 | } |
Heinrich Schuchardt | 5f94666 | 2019-05-04 19:48:38 +0200 | [diff] [blame] | 245 | if (!memcmp(&guid, &guid_vendor1, sizeof(efi_guid_t)) && |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 246 | !efi_st_strcmp_16_8(varname, "efi_st_var1")) |
| 247 | flag |= 2; |
| 248 | } |
AKASHI Takahiro | b3d562e | 2019-01-21 12:43:14 +0100 | [diff] [blame] | 249 | if (flag != 3) { |
| 250 | efi_st_error( |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 251 | "GetNextVariableName did not return all variables\n"); |
AKASHI Takahiro | b3d562e | 2019-01-21 12:43:14 +0100 | [diff] [blame] | 252 | return EFI_ST_FAILURE; |
| 253 | } |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 254 | /* Delete variable 1 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 255 | ret = runtime->set_variable(u"efi_st_var1", &guid_vendor1, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 256 | 0, 0, NULL); |
| 257 | if (ret != EFI_SUCCESS) { |
| 258 | efi_st_error("SetVariable failed\n"); |
| 259 | return EFI_ST_FAILURE; |
| 260 | } |
| 261 | len = EFI_ST_MAX_DATA_SIZE; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 262 | ret = runtime->get_variable(u"efi_st_var1", &guid_vendor1, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 263 | &attr, &len, data); |
| 264 | if (ret != EFI_NOT_FOUND) { |
| 265 | efi_st_error("Variable was not deleted\n"); |
| 266 | return EFI_ST_FAILURE; |
| 267 | } |
| 268 | /* Delete variable 0 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 269 | ret = runtime->set_variable(u"efi_st_var0", &guid_vendor0, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 270 | 0, 0, NULL); |
| 271 | if (ret != EFI_SUCCESS) { |
| 272 | efi_st_error("SetVariable failed\n"); |
| 273 | return EFI_ST_FAILURE; |
| 274 | } |
| 275 | len = EFI_ST_MAX_DATA_SIZE; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 276 | ret = runtime->get_variable(u"efi_st_var0", &guid_vendor0, |
Heinrich Schuchardt | 549fbca | 2018-05-17 07:57:06 +0200 | [diff] [blame] | 277 | &attr, &len, data); |
| 278 | if (ret != EFI_NOT_FOUND) { |
| 279 | efi_st_error("Variable was not deleted\n"); |
| 280 | return EFI_ST_FAILURE; |
| 281 | } |
| 282 | |
| 283 | return EFI_ST_SUCCESS; |
| 284 | } |
| 285 | |
| 286 | EFI_UNIT_TEST(variables) = { |
| 287 | .name = "variables", |
| 288 | .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, |
| 289 | .setup = setup, |
| 290 | .execute = execute, |
| 291 | }; |