1 /* SPDX-License-Identifier: GPL-2.0 2 * 3 * Definitions for kernel modules using hp_bioscfg driver 4 * 5 * Copyright (c) 2022 HP Development Company, L.P. 6 */ 7 8 #ifndef _HP_BIOSCFG_H_ 9 #define _HP_BIOSCFG_H_ 10 11 #include <linux/wmi.h> 12 #include <linux/types.h> 13 #include <linux/string.h> 14 #include <linux/device.h> 15 #include <linux/module.h> 16 #include <linux/kernel.h> 17 #include <linux/nls.h> 18 19 #define DRIVER_NAME "hp-bioscfg" 20 21 #define MAX_BUFF_SIZE 512 22 #define MAX_KEY_MOD_SIZE 256 23 #define MAX_PASSWD_SIZE 64 24 #define MAX_PREREQUISITES_SIZE 20 25 #define MAX_REQ_ELEM_SIZE 128 26 #define MAX_VALUES_SIZE 16 27 #define MAX_ENCODINGS_SIZE 16 28 #define MAX_ELEMENTS_SIZE 16 29 30 #define SPM_STR_DESC "Secure Platform Management" 31 #define SPM_STR "SPM" 32 #define SURE_START_DESC "Sure Start" 33 #define SURE_START_STR "Sure_Start" 34 #define SETUP_PASSWD "Setup Password" 35 #define POWER_ON_PASSWD "Power-On Password" 36 37 #define LANG_CODE_STR "en_US.UTF-8" 38 #define SCHEDULE_POWER_ON "Scheduled Power-On" 39 40 #define COMMA_SEP "," 41 #define SEMICOLON_SEP ";" 42 43 /* Sure Admin Functions */ 44 45 #define UTF_PREFIX "<utf-16/>" 46 #define BEAM_PREFIX "<BEAM/>" 47 48 enum mechanism_values { 49 PASSWORD = 0x00, 50 SIGNING_KEY = 0x01, 51 ENDORSEMENT_KEY = 0x02, 52 }; 53 54 #define BIOS_ADMIN "bios-admin" 55 #define POWER_ON "power-on" 56 #define BIOS_SPM "enhanced-bios-auth" 57 58 #define PASSWD_MECHANISM_TYPES "password" 59 60 #define HP_WMI_BIOS_GUID "5FB7F034-2C63-45E9-BE91-3D44E2C707E4" 61 62 #define HP_WMI_BIOS_STRING_GUID "988D08E3-68F4-4C35-AF3E-6A1B8106F83C" 63 #define HP_WMI_BIOS_INTEGER_GUID "8232DE3D-663D-4327-A8F4-E293ADB9BF05" 64 #define HP_WMI_BIOS_ENUMERATION_GUID "2D114B49-2DFB-4130-B8FE-4A3C09E75133" 65 #define HP_WMI_BIOS_ORDERED_LIST_GUID "14EA9746-CE1F-4098-A0E0-7045CB4DA745" 66 #define HP_WMI_BIOS_PASSWORD_GUID "322F2028-0F84-4901-988E-015176049E2D" 67 #define HP_WMI_SET_BIOS_SETTING_GUID "1F4C91EB-DC5C-460B-951D-C7CB9B4B8D5E" 68 69 enum hp_wmi_spm_commandtype { 70 HPWMI_SECUREPLATFORM_GET_STATE = 0x10, 71 HPWMI_SECUREPLATFORM_SET_KEK = 0x11, 72 HPWMI_SECUREPLATFORM_SET_SK = 0x12, 73 }; 74 75 enum hp_wmi_surestart_commandtype { 76 HPWMI_SURESTART_GET_LOG_COUNT = 0x01, 77 HPWMI_SURESTART_GET_LOG = 0x02, 78 }; 79 80 enum hp_wmi_command { 81 HPWMI_READ = 0x01, 82 HPWMI_WRITE = 0x02, 83 HPWMI_ODM = 0x03, 84 HPWMI_SURESTART = 0x20006, 85 HPWMI_GM = 0x20008, 86 HPWMI_SECUREPLATFORM = 0x20010, 87 }; 88 89 struct bios_return { 90 u32 sigpass; 91 u32 return_code; 92 }; 93 94 enum wmi_error_values { 95 SUCCESS = 0x00, 96 CMD_FAILED = 0x01, 97 INVALID_SIGN = 0x02, 98 INVALID_CMD_VALUE = 0x03, 99 INVALID_CMD_TYPE = 0x04, 100 INVALID_DATA_SIZE = 0x05, 101 INVALID_CMD_PARAM = 0x06, 102 ENCRYP_CMD_REQUIRED = 0x07, 103 NO_SECURE_SESSION = 0x08, 104 SECURE_SESSION_FOUND = 0x09, 105 SECURE_SESSION_FAILED = 0x0A, 106 AUTH_FAILED = 0x0B, 107 INVALID_BIOS_AUTH = 0x0E, 108 NONCE_DID_NOT_MATCH = 0x18, 109 GENERIC_ERROR = 0x1C, 110 BIOS_ADMIN_POLICY_NOT_MET = 0x28, 111 BIOS_ADMIN_NOT_SET = 0x38, 112 P21_NO_PROVISIONED = 0x1000, 113 P21_PROVISION_IN_PROGRESS = 0x1001, 114 P21_IN_USE = 0x1002, 115 HEP_NOT_ACTIVE = 0x1004, 116 HEP_ALREADY_SET = 0x1006, 117 HEP_CHECK_STATE = 0x1007, 118 }; 119 120 struct common_data { 121 u8 display_name[MAX_BUFF_SIZE]; 122 u8 path[MAX_BUFF_SIZE]; 123 u32 is_readonly; 124 u32 display_in_ui; 125 u32 requires_physical_presence; 126 u32 sequence; 127 u32 prerequisites_size; 128 u8 prerequisites[MAX_PREREQUISITES_SIZE][MAX_BUFF_SIZE]; 129 u32 security_level; 130 }; 131 132 struct string_data { 133 struct common_data common; 134 struct kobject *attr_name_kobj; 135 u8 current_value[MAX_BUFF_SIZE]; 136 u8 new_value[MAX_BUFF_SIZE]; 137 u32 min_length; 138 u32 max_length; 139 }; 140 141 struct integer_data { 142 struct common_data common; 143 struct kobject *attr_name_kobj; 144 u32 current_value; 145 u32 new_value; 146 u32 lower_bound; 147 u32 upper_bound; 148 u32 scalar_increment; 149 }; 150 151 struct enumeration_data { 152 struct common_data common; 153 struct kobject *attr_name_kobj; 154 u8 current_value[MAX_BUFF_SIZE]; 155 u8 new_value[MAX_BUFF_SIZE]; 156 u32 possible_values_size; 157 u8 possible_values[MAX_VALUES_SIZE][MAX_BUFF_SIZE]; 158 }; 159 160 struct ordered_list_data { 161 struct common_data common; 162 struct kobject *attr_name_kobj; 163 u8 current_value[MAX_BUFF_SIZE]; 164 u8 new_value[MAX_BUFF_SIZE]; 165 u32 elements_size; 166 u8 elements[MAX_ELEMENTS_SIZE][MAX_BUFF_SIZE]; 167 }; 168 169 struct password_data { 170 struct common_data common; 171 struct kobject *attr_name_kobj; 172 u8 current_password[MAX_PASSWD_SIZE]; 173 u8 new_password[MAX_PASSWD_SIZE]; 174 u32 min_password_length; 175 u32 max_password_length; 176 u32 encodings_size; 177 u8 encodings[MAX_ENCODINGS_SIZE][MAX_BUFF_SIZE]; 178 bool is_enabled; 179 180 /* 181 * 'role' identifies the type of authentication. 182 * Two known types are bios-admin and power-on. 183 * 'bios-admin' represents BIOS administrator password 184 * 'power-on' represents a password required to use the system 185 */ 186 u32 role; 187 188 /* 189 * 'mechanism' represents the means of authentication. 190 * Only supported type currently is "password" 191 */ 192 u32 mechanism; 193 }; 194 195 struct secure_platform_data { 196 struct kobject *attr_name_kobj; 197 u8 attribute_name[MAX_BUFF_SIZE]; 198 u8 *endorsement_key; 199 u8 *signing_key; 200 u8 *auth_token; 201 bool is_enabled; 202 u32 mechanism; 203 }; 204 205 struct bioscfg_priv { 206 struct kset *authentication_dir_kset; 207 struct kset *main_dir_kset; 208 struct device *class_dev; 209 struct string_data *string_data; 210 u32 string_instances_count; 211 struct integer_data *integer_data; 212 u32 integer_instances_count; 213 struct enumeration_data *enumeration_data; 214 u32 enumeration_instances_count; 215 struct ordered_list_data *ordered_list_data; 216 u32 ordered_list_instances_count; 217 struct password_data *password_data; 218 u32 password_instances_count; 219 220 struct kobject *sure_start_attr_kobj; 221 struct secure_platform_data spm_data; 222 u8 display_name_language_code[MAX_BUFF_SIZE]; 223 bool pending_reboot; 224 struct mutex mutex; 225 }; 226 227 /* global structure used by multiple WMI interfaces */ 228 extern struct bioscfg_priv bioscfg_drv; 229 230 enum hp_wmi_data_type { 231 HPWMI_STRING_TYPE, 232 HPWMI_INTEGER_TYPE, 233 HPWMI_ENUMERATION_TYPE, 234 HPWMI_ORDERED_LIST_TYPE, 235 HPWMI_PASSWORD_TYPE, 236 HPWMI_SECURE_PLATFORM_TYPE, 237 HPWMI_SURE_START_TYPE, 238 }; 239 240 enum hp_wmi_data_elements { 241 /* Common elements */ 242 NAME = 0, 243 VALUE = 1, 244 PATH = 2, 245 IS_READONLY = 3, 246 DISPLAY_IN_UI = 4, 247 REQUIRES_PHYSICAL_PRESENCE = 5, 248 SEQUENCE = 6, 249 PREREQUISITES_SIZE = 7, 250 PREREQUISITES = 8, 251 SECURITY_LEVEL = 9, 252 253 /* String elements */ 254 STR_MIN_LENGTH = 10, 255 STR_MAX_LENGTH = 11, 256 STR_ELEM_CNT = 12, 257 258 /* Integer elements */ 259 INT_LOWER_BOUND = 10, 260 INT_UPPER_BOUND = 11, 261 INT_SCALAR_INCREMENT = 12, 262 INT_ELEM_CNT = 13, 263 264 /* Enumeration elements */ 265 ENUM_CURRENT_VALUE = 10, 266 ENUM_SIZE = 11, 267 ENUM_POSSIBLE_VALUES = 12, 268 ENUM_ELEM_CNT = 13, 269 270 /* Ordered list elements */ 271 ORD_LIST_SIZE = 10, 272 ORD_LIST_ELEMENTS = 11, 273 ORD_ELEM_CNT = 12, 274 275 /* Password elements */ 276 PSWD_MIN_LENGTH = 10, 277 PSWD_MAX_LENGTH = 11, 278 PSWD_SIZE = 12, 279 PSWD_ENCODINGS = 13, 280 PSWD_IS_SET = 14, 281 PSWD_ELEM_CNT = 15, 282 283 /* Minimum elements shared by all attribute types (NAME..SECURITY_LEVEL) */ 284 COMMON_ELEM_CNT = SECURITY_LEVEL + 1, 285 }; 286 287 #define GET_INSTANCE_ID(type) \ 288 static int get_##type##_instance_id(struct kobject *kobj) \ 289 { \ 290 int i; \ 291 \ 292 for (i = 0; i < bioscfg_drv.type##_instances_count; i++) { \ 293 if (bioscfg_drv.type##_data[i].attr_name_kobj && \ 294 !strcmp(kobj->name, bioscfg_drv.type##_data[i].attr_name_kobj->name)) \ 295 return i; \ 296 } \ 297 return -EIO; \ 298 } 299 300 #define ATTRIBUTE_S_PROPERTY_SHOW(name, type) \ 301 static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, \ 302 char *buf) \ 303 { \ 304 int i = get_##type##_instance_id(kobj); \ 305 if (i >= 0) \ 306 return sysfs_emit(buf, "%s\n", bioscfg_drv.type##_data[i].name); \ 307 return -EIO; \ 308 } 309 310 #define ATTRIBUTE_N_PROPERTY_SHOW(name, type) \ 311 static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, \ 312 char *buf) \ 313 { \ 314 int i = get_##type##_instance_id(kobj); \ 315 if (i >= 0) \ 316 return sysfs_emit(buf, "%d\n", bioscfg_drv.type##_data[i].name); \ 317 return -EIO; \ 318 } 319 320 #define ATTRIBUTE_PROPERTY_STORE(curr_val, type) \ 321 static ssize_t curr_val##_store(struct kobject *kobj, \ 322 struct kobj_attribute *attr, \ 323 const char *buf, size_t count) \ 324 { \ 325 char *attr_value = NULL; \ 326 int i; \ 327 int ret = -EIO; \ 328 \ 329 attr_value = kstrdup(buf, GFP_KERNEL); \ 330 if (!attr_value) \ 331 return -ENOMEM; \ 332 \ 333 ret = hp_enforce_single_line_input(attr_value, count); \ 334 if (!ret) { \ 335 i = get_##type##_instance_id(kobj); \ 336 if (i >= 0) \ 337 ret = validate_##type##_input(i, attr_value); \ 338 } \ 339 if (!ret) \ 340 ret = hp_set_attribute(kobj->name, attr_value); \ 341 if (!ret) { \ 342 update_##type##_value(i, attr_value); \ 343 if (bioscfg_drv.type##_data[i].common.requires_physical_presence) \ 344 hp_set_reboot_and_signal_event(); \ 345 } \ 346 hp_clear_all_credentials(); \ 347 kfree(attr_value); \ 348 \ 349 return ret ? ret : count; \ 350 } 351 352 #define ATTRIBUTE_SPM_N_PROPERTY_SHOW(name, type) \ 353 static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) \ 354 { \ 355 return sysfs_emit(buf, "%d\n", bioscfg_drv.type##_data.name); \ 356 } 357 358 #define ATTRIBUTE_SPM_S_PROPERTY_SHOW(name, type) \ 359 static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) \ 360 { \ 361 return sysfs_emit(buf, "%s\n", bioscfg_drv.type##_data.name); \ 362 } 363 364 #define ATTRIBUTE_VALUES_PROPERTY_SHOW(name, type, sep) \ 365 static ssize_t name##_show(struct kobject *kobj, \ 366 struct kobj_attribute *attr, char *buf) \ 367 { \ 368 int i; \ 369 int len = 0; \ 370 int instance_id = get_##type##_instance_id(kobj); \ 371 \ 372 if (instance_id < 0) \ 373 return 0; \ 374 \ 375 for (i = 0; i < bioscfg_drv.type##_data[instance_id].name##_size; i++) { \ 376 if (i) \ 377 len += sysfs_emit_at(buf, len, "%s", sep); \ 378 \ 379 len += sysfs_emit_at(buf, len, "%s", \ 380 bioscfg_drv.type##_data[instance_id].name[i]); \ 381 } \ 382 len += sysfs_emit_at(buf, len, "\n"); \ 383 return len; \ 384 } 385 386 #define ATTRIBUTE_S_COMMON_PROPERTY_SHOW(name, type) \ 387 static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, \ 388 char *buf) \ 389 { \ 390 int i = get_##type##_instance_id(kobj); \ 391 if (i >= 0) \ 392 return sysfs_emit(buf, "%s\n", bioscfg_drv.type##_data[i].common.name); \ 393 return -EIO; \ 394 } 395 396 extern struct kobj_attribute common_display_langcode; 397 398 /* Prototypes */ 399 400 /* String attributes */ 401 int hp_populate_string_buffer_data(u8 *buffer_ptr, u32 *buffer_size, 402 int instance_id, 403 struct kobject *attr_name_kobj); 404 int hp_alloc_string_data(void); 405 void hp_exit_string_attributes(void); 406 int hp_populate_string_package_data(union acpi_object *str_obj, 407 int str_obj_count, 408 int instance_id, 409 struct kobject *attr_name_kobj); 410 411 /* Integer attributes */ 412 int hp_populate_integer_buffer_data(u8 *buffer_ptr, u32 *buffer_size, 413 int instance_id, 414 struct kobject *attr_name_kobj); 415 int hp_alloc_integer_data(void); 416 void hp_exit_integer_attributes(void); 417 int hp_populate_integer_package_data(union acpi_object *integer_obj, 418 int integer_obj_count, 419 int instance_id, 420 struct kobject *attr_name_kobj); 421 422 /* Enumeration attributes */ 423 int hp_populate_enumeration_buffer_data(u8 *buffer_ptr, u32 *buffer_size, 424 int instance_id, 425 struct kobject *attr_name_kobj); 426 int hp_alloc_enumeration_data(void); 427 void hp_exit_enumeration_attributes(void); 428 int hp_populate_enumeration_package_data(union acpi_object *enum_obj, 429 int enum_obj_count, 430 int instance_id, 431 struct kobject *attr_name_kobj); 432 433 /* Ordered list */ 434 int hp_populate_ordered_list_buffer_data(u8 *buffer_ptr, 435 u32 *buffer_size, 436 int instance_id, 437 struct kobject *attr_name_kobj); 438 int hp_alloc_ordered_list_data(void); 439 void hp_exit_ordered_list_attributes(void); 440 int hp_populate_ordered_list_package_data(union acpi_object *order_obj, 441 int order_obj_count, 442 int instance_id, 443 struct kobject *attr_name_kobj); 444 445 /* Password authentication attributes */ 446 int hp_populate_password_buffer_data(u8 *buffer_ptr, u32 *buffer_size, 447 int instance_id, 448 struct kobject *attr_name_kobj); 449 int hp_populate_password_package_data(union acpi_object *password_obj, 450 int password_obj_count, 451 int instance_id, 452 struct kobject *attr_name_kobj); 453 int hp_alloc_password_data(void); 454 int hp_get_password_instance_for_type(const char *name); 455 int hp_clear_all_credentials(void); 456 int hp_set_attribute(const char *a_name, const char *a_value); 457 458 /* SPM attributes */ 459 void hp_exit_password_attributes(void); 460 void hp_exit_secure_platform_attributes(void); 461 int hp_populate_secure_platform_data(struct kobject *attr_name_kobj); 462 int hp_populate_security_buffer(u16 *buffer, const char *authentication); 463 464 /* Bios Attributes interface */ 465 int hp_wmi_set_bios_setting(u16 *input_buffer, u32 input_size); 466 int hp_wmi_perform_query(int query, enum hp_wmi_command command, 467 void *buffer, u32 insize, u32 outsize); 468 469 /* Sure Start attributes */ 470 void hp_exit_sure_start_attributes(void); 471 int hp_populate_sure_start_data(struct kobject *attr_name_kobj); 472 473 /* Bioscfg */ 474 475 void hp_exit_attr_set_interface(void); 476 int hp_init_attr_set_interface(void); 477 size_t hp_calculate_string_buffer(const char *str); 478 size_t hp_calculate_security_buffer(const char *authentication); 479 void *hp_ascii_to_utf16_unicode(u16 *p, const u8 *str); 480 int hp_get_integer_from_buffer(u8 **buffer, u32 *buffer_size, u32 *integer); 481 int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_size); 482 int hp_convert_hexstr_to_str(const char *input, u32 input_len, char **str, int *len); 483 int hp_encode_outsize_for_pvsz(int outsize); 484 int hp_enforce_single_line_input(char *buf, size_t count); 485 void hp_set_reboot_and_signal_event(void); 486 ssize_t display_name_language_code_show(struct kobject *kobj, 487 struct kobj_attribute *attr, 488 char *buf); 489 union acpi_object *hp_get_wmiobj_pointer(int instance_id, const char *guid_string); 490 int hp_get_instance_count(const char *guid_string); 491 void hp_update_attribute_permissions(bool isreadonly, struct kobj_attribute *current_val); 492 void hp_friendly_user_name_update(char *path, const char *attr_name, 493 char *attr_display, int attr_size); 494 int hp_wmi_error_and_message(int error_code); 495 int hp_get_common_data_from_buffer(u8 **buffer_ptr, u32 *buffer_size, struct common_data *common); 496 497 #endif 498