1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Common methods for use with hp-bioscfg driver 4 * 5 * Copyright (c) 2022 HP Development Company, L.P. 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/firmware_attributes.h> 11 #include <linux/fs.h> 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/printk.h> 15 #include <linux/string_helpers.h> 16 #include <linux/wmi.h> 17 #include "bioscfg.h" 18 #include <linux/nls.h> 19 #include <linux/errno.h> 20 21 MODULE_AUTHOR("Jorge Lopez <jorge.lopez2@hp.com>"); 22 MODULE_DESCRIPTION("HP BIOS Configuration Driver"); 23 MODULE_LICENSE("GPL"); 24 25 struct bioscfg_priv bioscfg_drv = { 26 .mutex = __MUTEX_INITIALIZER(bioscfg_drv.mutex), 27 }; 28 29 ssize_t display_name_language_code_show(struct kobject *kobj, 30 struct kobj_attribute *attr, 31 char *buf) 32 { 33 return sysfs_emit(buf, "%s\n", LANG_CODE_STR); 34 } 35 36 struct kobj_attribute common_display_langcode = 37 __ATTR_RO(display_name_language_code); 38 39 int hp_get_integer_from_buffer(u8 **buffer, u32 *buffer_size, u32 *integer) 40 { 41 int *ptr = PTR_ALIGN((int *)*buffer, sizeof(int)); 42 43 /* Ensure there is enough space remaining to read the integer */ 44 if (*buffer_size < sizeof(int)) 45 return -EINVAL; 46 47 *integer = *(ptr++); 48 *buffer = (u8 *)ptr; 49 *buffer_size -= sizeof(int); 50 51 return 0; 52 } 53 54 int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_size) 55 { 56 char utf8_buf[MAX_BUFF_SIZE]; 57 u16 *src = (u16 *)*buffer; 58 int escaped_len; 59 u16 orig_size; 60 u16 src_size; 61 int utf8_len; 62 63 if (*buffer_size < sizeof(u16)) 64 return -EINVAL; 65 66 src_size = *src; 67 68 /* 69 * Ensure there is enough space remaining for the length prefix 70 * just read plus the string data it describes. 71 */ 72 if (*buffer_size < sizeof(u16) + src_size) 73 return -EINVAL; 74 75 src++; 76 /* size value in u16 chars */ 77 orig_size = src_size / sizeof(u16); 78 79 utf8_len = utf16s_to_utf8s(src, orig_size, UTF16_HOST_ENDIAN, 80 utf8_buf, sizeof(utf8_buf)); 81 82 escaped_len = string_escape_mem(utf8_buf, utf8_len, dst, dst_size - 1, 83 ESCAPE_SPACE | ESCAPE_SPECIAL, 84 "\\\r\n\t"); 85 if (escaped_len > dst_size - 1) 86 escaped_len = dst_size - 1; 87 dst[escaped_len] = '\0'; 88 strreplace(dst, '"', '\''); 89 90 *buffer += sizeof(u16) + src_size; 91 *buffer_size -= sizeof(u16) + src_size; 92 93 return escaped_len; 94 } 95 96 int hp_get_common_data_from_buffer(u8 **buffer_ptr, u32 *buffer_size, 97 struct common_data *common_data) 98 { 99 int ret = 0; 100 int reqs; 101 102 // PATH: 103 ret = hp_get_string_from_buffer(buffer_ptr, buffer_size, common_data->path, 104 sizeof(common_data->path)); 105 if (ret < 0) 106 goto common_exit; 107 108 // IS_READONLY: 109 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size, 110 &common_data->is_readonly); 111 if (ret < 0) 112 goto common_exit; 113 114 //DISPLAY_IN_UI: 115 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size, 116 &common_data->display_in_ui); 117 if (ret < 0) 118 goto common_exit; 119 120 // REQUIRES_PHYSICAL_PRESENCE: 121 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size, 122 &common_data->requires_physical_presence); 123 if (ret < 0) 124 goto common_exit; 125 126 // SEQUENCE: 127 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size, 128 &common_data->sequence); 129 if (ret < 0) 130 goto common_exit; 131 132 // PREREQUISITES_SIZE: 133 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size, 134 &common_data->prerequisites_size); 135 if (ret < 0) 136 goto common_exit; 137 138 if (common_data->prerequisites_size > MAX_PREREQUISITES_SIZE) { 139 /* Report a message and limit prerequisite size to maximum value */ 140 pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n"); 141 common_data->prerequisites_size = MAX_PREREQUISITES_SIZE; 142 } 143 144 // PREREQUISITES: 145 for (reqs = 0; reqs < common_data->prerequisites_size; reqs++) { 146 ret = hp_get_string_from_buffer(buffer_ptr, buffer_size, 147 common_data->prerequisites[reqs], 148 sizeof(common_data->prerequisites[reqs])); 149 if (ret < 0) 150 break; 151 } 152 153 // SECURITY_LEVEL: 154 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size, 155 &common_data->security_level); 156 157 common_exit: 158 return ret; 159 } 160 161 int hp_enforce_single_line_input(char *buf, size_t count) 162 { 163 char *p; 164 165 p = memchr(buf, '\n', count); 166 167 if (p == buf + count - 1) 168 *p = '\0'; /* strip trailing newline */ 169 else if (p) 170 return -EINVAL; /* enforce single line input */ 171 172 return 0; 173 } 174 175 /* Set pending reboot value and generate KOBJ_NAME event */ 176 void hp_set_reboot_and_signal_event(void) 177 { 178 bioscfg_drv.pending_reboot = true; 179 kobject_uevent(&bioscfg_drv.class_dev->kobj, KOBJ_CHANGE); 180 } 181 182 /** 183 * hp_calculate_string_buffer() - determines size of string buffer for 184 * use with BIOS communication 185 * 186 * @str: the string to calculate based upon 187 */ 188 size_t hp_calculate_string_buffer(const char *str) 189 { 190 size_t length = strlen(str); 191 192 /* BIOS expects 4 bytes when an empty string is found */ 193 if (length == 0) 194 return 4; 195 196 /* u16 length field + one UTF16 char for each input char */ 197 return sizeof(u16) + strlen(str) * sizeof(u16); 198 } 199 200 int hp_wmi_error_and_message(int error_code) 201 { 202 char *error_msg = NULL; 203 int ret; 204 205 switch (error_code) { 206 case SUCCESS: 207 error_msg = "Success"; 208 ret = 0; 209 break; 210 case CMD_FAILED: 211 error_msg = "Command failed"; 212 ret = -EINVAL; 213 break; 214 case INVALID_SIGN: 215 error_msg = "Invalid signature"; 216 ret = -EINVAL; 217 break; 218 case INVALID_CMD_VALUE: 219 error_msg = "Invalid command value/Feature not supported"; 220 ret = -EOPNOTSUPP; 221 break; 222 case INVALID_CMD_TYPE: 223 error_msg = "Invalid command type"; 224 ret = -EINVAL; 225 break; 226 case INVALID_DATA_SIZE: 227 error_msg = "Invalid data size"; 228 ret = -EINVAL; 229 break; 230 case INVALID_CMD_PARAM: 231 error_msg = "Invalid command parameter"; 232 ret = -EINVAL; 233 break; 234 case ENCRYP_CMD_REQUIRED: 235 error_msg = "Secure/encrypted command required"; 236 ret = -EACCES; 237 break; 238 case NO_SECURE_SESSION: 239 error_msg = "No secure session established"; 240 ret = -EACCES; 241 break; 242 case SECURE_SESSION_FOUND: 243 error_msg = "Secure session already established"; 244 ret = -EACCES; 245 break; 246 case SECURE_SESSION_FAILED: 247 error_msg = "Secure session failed"; 248 ret = -EIO; 249 break; 250 case AUTH_FAILED: 251 error_msg = "Other permission/Authentication failed"; 252 ret = -EACCES; 253 break; 254 case INVALID_BIOS_AUTH: 255 error_msg = "Invalid BIOS administrator password"; 256 ret = -EINVAL; 257 break; 258 case NONCE_DID_NOT_MATCH: 259 error_msg = "Nonce did not match"; 260 ret = -EINVAL; 261 break; 262 case GENERIC_ERROR: 263 error_msg = "Generic/Other error"; 264 ret = -EIO; 265 break; 266 case BIOS_ADMIN_POLICY_NOT_MET: 267 error_msg = "BIOS Admin password does not meet password policy requirements"; 268 ret = -EINVAL; 269 break; 270 case BIOS_ADMIN_NOT_SET: 271 error_msg = "BIOS Setup password is not set"; 272 ret = -EPERM; 273 break; 274 case P21_NO_PROVISIONED: 275 error_msg = "P21 is not provisioned"; 276 ret = -EPERM; 277 break; 278 case P21_PROVISION_IN_PROGRESS: 279 error_msg = "P21 is already provisioned or provisioning is in progress and a signing key has already been sent"; 280 ret = -EINPROGRESS; 281 break; 282 case P21_IN_USE: 283 error_msg = "P21 in use (cannot deprovision)"; 284 ret = -EPERM; 285 break; 286 case HEP_NOT_ACTIVE: 287 error_msg = "HEP not activated"; 288 ret = -EPERM; 289 break; 290 case HEP_ALREADY_SET: 291 error_msg = "HEP Transport already set"; 292 ret = -EINVAL; 293 break; 294 case HEP_CHECK_STATE: 295 error_msg = "Check the current HEP state"; 296 ret = -EINVAL; 297 break; 298 default: 299 error_msg = "Generic/Other error"; 300 ret = -EIO; 301 break; 302 } 303 304 if (error_code) 305 pr_warn_ratelimited("Returned error 0x%x, \"%s\"\n", error_code, error_msg); 306 307 return ret; 308 } 309 310 static ssize_t pending_reboot_show(struct kobject *kobj, 311 struct kobj_attribute *attr, 312 char *buf) 313 { 314 return sysfs_emit(buf, "%d\n", bioscfg_drv.pending_reboot); 315 } 316 317 static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot); 318 319 /* 320 * create_attributes_level_sysfs_files() - Creates pending_reboot attributes 321 */ 322 static int create_attributes_level_sysfs_files(void) 323 { 324 return sysfs_create_file(&bioscfg_drv.main_dir_kset->kobj, 325 &pending_reboot.attr); 326 } 327 328 static void attr_name_release(struct kobject *kobj) 329 { 330 kfree(kobj); 331 } 332 333 static const struct kobj_type attr_name_ktype = { 334 .release = attr_name_release, 335 .sysfs_ops = &kobj_sysfs_ops, 336 }; 337 338 /** 339 * hp_get_wmiobj_pointer() - Get Content of WMI block for particular instance 340 * 341 * @instance_id: WMI instance ID 342 * @guid_string: WMI GUID (in str form) 343 * 344 * Fetches the content for WMI block (instance_id) under GUID (guid_string) 345 * Caller must kfree the return 346 */ 347 union acpi_object *hp_get_wmiobj_pointer(int instance_id, const char *guid_string) 348 { 349 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; 350 acpi_status status; 351 352 status = wmi_query_block(guid_string, instance_id, &out); 353 return ACPI_SUCCESS(status) ? (union acpi_object *)out.pointer : NULL; 354 } 355 356 /** 357 * hp_get_instance_count() - Compute total number of instances under guid_string 358 * 359 * @guid_string: WMI GUID (in string form) 360 */ 361 int hp_get_instance_count(const char *guid_string) 362 { 363 int ret; 364 365 ret = wmi_instance_count(guid_string); 366 if (ret < 0) 367 return 0; 368 369 return ret; 370 } 371 372 /** 373 * hp_alloc_attributes_data() - Allocate attributes data for a particular type 374 * 375 * @attr_type: Attribute type to allocate 376 */ 377 static int hp_alloc_attributes_data(int attr_type) 378 { 379 switch (attr_type) { 380 case HPWMI_STRING_TYPE: 381 return hp_alloc_string_data(); 382 383 case HPWMI_INTEGER_TYPE: 384 return hp_alloc_integer_data(); 385 386 case HPWMI_ENUMERATION_TYPE: 387 return hp_alloc_enumeration_data(); 388 389 case HPWMI_ORDERED_LIST_TYPE: 390 return hp_alloc_ordered_list_data(); 391 392 case HPWMI_PASSWORD_TYPE: 393 return hp_alloc_password_data(); 394 395 default: 396 return 0; 397 } 398 } 399 400 int hp_convert_hexstr_to_str(const char *input, u32 input_len, char **str, int *len) 401 { 402 int ret = 0; 403 int new_len = 0; 404 char tmp[] = "0x00"; 405 char *new_str = NULL; 406 long ch; 407 int i; 408 409 if (input_len <= 0 || !input || !str || !len) 410 return -EINVAL; 411 412 *len = 0; 413 *str = NULL; 414 415 new_str = kzalloc(2 * DIV_ROUND_UP(input_len, 5) + 1, GFP_KERNEL); 416 if (!new_str) 417 return -ENOMEM; 418 419 for (i = 0; i < input_len; i += 5) { 420 strscpy(tmp, input + i); 421 if (kstrtol(tmp, 16, &ch) == 0) { 422 // escape char 423 if (ch == '\\' || 424 ch == '\r' || 425 ch == '\n' || ch == '\t') { 426 if (ch == '\r') 427 ch = 'r'; 428 else if (ch == '\n') 429 ch = 'n'; 430 else if (ch == '\t') 431 ch = 't'; 432 new_str[new_len++] = '\\'; 433 } 434 new_str[new_len++] = ch; 435 if (ch == '\0') 436 break; 437 } 438 } 439 440 if (new_len) { 441 new_str[new_len] = '\0'; 442 *str = krealloc(new_str, (new_len + 1) * sizeof(char), 443 GFP_KERNEL); 444 if (*str) 445 *len = new_len; 446 else 447 ret = -ENOMEM; 448 } else { 449 ret = -EFAULT; 450 } 451 452 if (ret) 453 kfree(new_str); 454 return ret; 455 } 456 457 /** 458 * hp_get_common_data_from_package() - Parse the package elements shared 459 * by every attribute type (PATH..SECURITY_LEVEL) 460 * @obj: ACPI object package for this attribute instance 461 * @obj_count: Number of elements in @obj 462 * @elem: Current position in @obj; advanced in place when PREREQUISITES 463 * consumes additional array elements 464 * @eloc: Current common-element locator; advanced in place when 465 * PREREQUISITES_SIZE is zero, since PREREQUISITES is then omitted 466 * by BIOS 467 * @int_value: Integer value already decoded by the caller for *@eloc, 468 * when applicable 469 * @str_value: String value already decoded by the caller for *@eloc, when 470 * applicable; reused and reset to NULL while walking 471 * PREREQUISITES, same as the caller does with its own copy 472 * @common: Destination common_data struct for this attribute instance 473 * 474 * Called by each type's own hp_populate_*_elements_from_package() 475 * instead of duplicating this parsing per type, since every BIOS 476 * attribute package uses the same layout for these elements (see 477 * struct common_data). 478 * 479 * Return: 0 on success, or -EINVAL if @obj is too small for the 480 * declared PREREQUISITES_SIZE. 481 */ 482 int hp_get_common_data_from_package(union acpi_object *obj, int obj_count, 483 int *elem, int *eloc, u32 int_value, 484 char **str_value, struct common_data *common) 485 { 486 int value_len; 487 int reqs; 488 int size; 489 490 switch (*eloc) { 491 case PATH: 492 strscpy(common->path, *str_value); 493 break; 494 case IS_READONLY: 495 common->is_readonly = int_value; 496 break; 497 case DISPLAY_IN_UI: 498 common->display_in_ui = int_value; 499 break; 500 case REQUIRES_PHYSICAL_PRESENCE: 501 common->requires_physical_presence = int_value; 502 break; 503 case SEQUENCE: 504 common->sequence = int_value; 505 break; 506 case PREREQUISITES_SIZE: 507 if (int_value > MAX_PREREQUISITES_SIZE) { 508 pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n"); 509 int_value = MAX_PREREQUISITES_SIZE; 510 } 511 common->prerequisites_size = int_value; 512 513 /* 514 * This step is needed to keep the expected 515 * element list pointing to the right obj[elem].type 516 * when the size is zero. PREREQUISITES 517 * object is omitted by BIOS when the size is 518 * zero. 519 */ 520 if (common->prerequisites_size == 0) 521 (*eloc)++; 522 break; 523 case PREREQUISITES: 524 size = min_t(u32, common->prerequisites_size, MAX_PREREQUISITES_SIZE); 525 526 for (reqs = 0; reqs < size; reqs++) { 527 if (*elem + reqs >= obj_count) { 528 pr_err("Error elem-objects package is too small\n"); 529 return -EINVAL; 530 } 531 532 if (hp_convert_hexstr_to_str(obj[*elem + reqs].string.pointer, 533 obj[*elem + reqs].string.length, 534 str_value, &value_len)) 535 continue; 536 537 strscpy(common->prerequisites[reqs], *str_value); 538 kfree(*str_value); 539 *str_value = NULL; 540 } 541 if (size) 542 *elem += size - 1; 543 break; 544 case SECURITY_LEVEL: 545 common->security_level = int_value; 546 break; 547 default: 548 break; 549 } 550 551 return 0; 552 } 553 554 /* map output size to the corresponding WMI method id */ 555 int hp_encode_outsize_for_pvsz(int outsize) 556 { 557 if (outsize > 4096) 558 return -EINVAL; 559 if (outsize > 1024) 560 return 5; 561 if (outsize > 128) 562 return 4; 563 if (outsize > 4) 564 return 3; 565 if (outsize > 0) 566 return 2; 567 return 1; 568 } 569 570 /* 571 * Update friendly display name for several attributes associated to 572 * 'Schedule Power-On' 573 */ 574 void hp_friendly_user_name_update(char *path, const char *attr_name, 575 char *attr_display, int attr_size) 576 { 577 if (strstr(path, SCHEDULE_POWER_ON)) 578 snprintf(attr_display, attr_size, "%s - %s", SCHEDULE_POWER_ON, attr_name); 579 else 580 strscpy(attr_display, attr_name, attr_size); 581 } 582 583 /** 584 * hp_update_attribute_permissions() - Update attributes permissions when 585 * isReadOnly value is 1 586 * 587 * @is_readonly: bool value to indicate if it a readonly attribute. 588 * @current_val: kobj_attribute corresponding to attribute. 589 * 590 */ 591 void hp_update_attribute_permissions(bool is_readonly, struct kobj_attribute *current_val) 592 { 593 current_val->attr.mode = is_readonly ? 0444 : 0644; 594 } 595 596 /** 597 * destroy_attribute_objs() - Free a kset of kobjects 598 * @kset: The kset to destroy 599 * 600 * Fress kobjects created for each attribute_name under attribute type kset 601 */ 602 static void destroy_attribute_objs(struct kset *kset) 603 { 604 struct kobject *pos, *next; 605 606 list_for_each_entry_safe(pos, next, &kset->list, entry) 607 kobject_put(pos); 608 } 609 610 /** 611 * release_attributes_data() - Clean-up all sysfs directories and files created 612 */ 613 static void release_attributes_data(void) 614 { 615 mutex_lock(&bioscfg_drv.mutex); 616 617 hp_exit_string_attributes(); 618 hp_exit_integer_attributes(); 619 hp_exit_enumeration_attributes(); 620 hp_exit_ordered_list_attributes(); 621 hp_exit_password_attributes(); 622 hp_exit_sure_start_attributes(); 623 hp_exit_secure_platform_attributes(); 624 625 if (bioscfg_drv.authentication_dir_kset) { 626 destroy_attribute_objs(bioscfg_drv.authentication_dir_kset); 627 kset_unregister(bioscfg_drv.authentication_dir_kset); 628 bioscfg_drv.authentication_dir_kset = NULL; 629 } 630 if (bioscfg_drv.main_dir_kset) { 631 sysfs_remove_file(&bioscfg_drv.main_dir_kset->kobj, &pending_reboot.attr); 632 destroy_attribute_objs(bioscfg_drv.main_dir_kset); 633 kset_unregister(bioscfg_drv.main_dir_kset); 634 bioscfg_drv.main_dir_kset = NULL; 635 } 636 mutex_unlock(&bioscfg_drv.mutex); 637 } 638 639 /** 640 * hp_add_other_attributes() - Initialize HP custom attributes not 641 * reported by BIOS and required to support Secure Platform and Sure 642 * Start. 643 * 644 * @attr_type: Custom HP attribute not reported by BIOS 645 * 646 * Initialize all 2 types of attributes: Platform and Sure Start 647 * object. Populates each attribute types respective properties 648 * under sysfs files. 649 * 650 * Returns zero(0) if successful. Otherwise, a negative value. 651 */ 652 static int hp_add_other_attributes(int attr_type) 653 { 654 struct kobject *attr_name_kobj; 655 int ret; 656 char *attr_name; 657 658 attr_name_kobj = kzalloc_obj(*attr_name_kobj); 659 if (!attr_name_kobj) 660 return -ENOMEM; 661 662 mutex_lock(&bioscfg_drv.mutex); 663 664 /* Check if attribute type is supported */ 665 switch (attr_type) { 666 case HPWMI_SECURE_PLATFORM_TYPE: 667 attr_name_kobj->kset = bioscfg_drv.authentication_dir_kset; 668 attr_name = SPM_STR; 669 break; 670 671 case HPWMI_SURE_START_TYPE: 672 attr_name_kobj->kset = bioscfg_drv.main_dir_kset; 673 attr_name = SURE_START_STR; 674 break; 675 676 default: 677 pr_err("Error: Unknown attr_type: %d\n", attr_type); 678 ret = -EINVAL; 679 kfree(attr_name_kobj); 680 goto unlock_drv_mutex; 681 } 682 683 ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype, 684 NULL, "%s", attr_name); 685 if (ret) { 686 pr_err("Error encountered [%d]\n", ret); 687 goto err_other_attr_init; 688 } 689 690 /* Populate attribute data */ 691 switch (attr_type) { 692 case HPWMI_SECURE_PLATFORM_TYPE: 693 ret = hp_populate_secure_platform_data(attr_name_kobj); 694 break; 695 696 case HPWMI_SURE_START_TYPE: 697 ret = hp_populate_sure_start_data(attr_name_kobj); 698 break; 699 700 default: 701 ret = -EINVAL; 702 } 703 704 if (ret) 705 goto err_other_attr_init; 706 707 mutex_unlock(&bioscfg_drv.mutex); 708 return 0; 709 710 err_other_attr_init: 711 kobject_put(attr_name_kobj); 712 unlock_drv_mutex: 713 mutex_unlock(&bioscfg_drv.mutex); 714 return ret; 715 } 716 717 static int hp_init_bios_package_attribute(enum hp_wmi_data_type attr_type, 718 union acpi_object *obj, 719 const char *guid, int min_elements, 720 int instance_id) 721 { 722 struct kobject *attr_name_kobj, *duplicate; 723 union acpi_object *elements; 724 struct kset *temp_kset; 725 726 char *str_value = NULL; 727 int str_len; 728 int ret = 0; 729 730 /* Take action appropriate to each ACPI TYPE */ 731 if (obj->package.count < COMMON_ELEM_CNT) { 732 pr_err("ACPI-package is missing common elements: %d < %d\n", 733 obj->package.count, COMMON_ELEM_CNT); 734 goto pack_attr_exit; 735 } 736 737 if (obj->package.count < min_elements) { 738 pr_warn("ACPI-package has fewer elements than expected: %d < %d, parsing available elements\n", 739 obj->package.count, min_elements); 740 } 741 742 elements = obj->package.elements; 743 744 /* sanity checking */ 745 if (elements[NAME].type != ACPI_TYPE_STRING) { 746 pr_debug("incorrect element type\n"); 747 goto pack_attr_exit; 748 } 749 if (strlen(elements[NAME].string.pointer) == 0) { 750 pr_debug("empty attribute found\n"); 751 goto pack_attr_exit; 752 } 753 754 if (attr_type == HPWMI_PASSWORD_TYPE) 755 temp_kset = bioscfg_drv.authentication_dir_kset; 756 else 757 temp_kset = bioscfg_drv.main_dir_kset; 758 759 /* convert attribute name to string */ 760 ret = hp_convert_hexstr_to_str(elements[NAME].string.pointer, 761 elements[NAME].string.length, 762 &str_value, &str_len); 763 764 if (ret) { 765 pr_debug("Failed to populate integer package data. Error [0%0x]\n", 766 ret); 767 kfree(str_value); 768 return ret; 769 } 770 771 if (!str_value || !str_value[0]) { 772 pr_debug("Ignoring attribute with empty name\n"); 773 goto pack_attr_exit; 774 } 775 776 /* All duplicate attributes found are ignored */ 777 duplicate = kset_find_obj(temp_kset, str_value); 778 if (duplicate) { 779 pr_debug("Duplicate attribute name found - %s\n", str_value); 780 /* kset_find_obj() returns a reference */ 781 kobject_put(duplicate); 782 goto pack_attr_exit; 783 } 784 785 /* build attribute */ 786 attr_name_kobj = kzalloc_obj(*attr_name_kobj); 787 if (!attr_name_kobj) { 788 ret = -ENOMEM; 789 goto pack_attr_exit; 790 } 791 792 attr_name_kobj->kset = temp_kset; 793 794 ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype, 795 NULL, "%s", str_value); 796 797 if (ret) { 798 kobject_put(attr_name_kobj); 799 goto pack_attr_exit; 800 } 801 802 /* enumerate all of these attributes */ 803 switch (attr_type) { 804 case HPWMI_STRING_TYPE: 805 ret = hp_populate_string_package_data(elements, 806 obj->package.count, 807 instance_id, 808 attr_name_kobj); 809 break; 810 case HPWMI_INTEGER_TYPE: 811 ret = hp_populate_integer_package_data(elements, 812 obj->package.count, 813 instance_id, 814 attr_name_kobj); 815 break; 816 case HPWMI_ENUMERATION_TYPE: 817 ret = hp_populate_enumeration_package_data(elements, 818 obj->package.count, 819 instance_id, 820 attr_name_kobj); 821 break; 822 case HPWMI_ORDERED_LIST_TYPE: 823 ret = hp_populate_ordered_list_package_data(elements, 824 obj->package.count, 825 instance_id, 826 attr_name_kobj); 827 break; 828 case HPWMI_PASSWORD_TYPE: 829 ret = hp_populate_password_package_data(elements, 830 obj->package.count, 831 instance_id, 832 attr_name_kobj); 833 break; 834 default: 835 pr_debug("Unknown attribute type found: 0x%x\n", attr_type); 836 break; 837 } 838 839 pack_attr_exit: 840 kfree(str_value); 841 return ret; 842 } 843 844 static int hp_init_bios_buffer_attribute(enum hp_wmi_data_type attr_type, 845 union acpi_object *obj, 846 const char *guid, int min_elements, 847 int instance_id) 848 { 849 struct kobject *attr_name_kobj, *duplicate; 850 struct kset *temp_kset; 851 char str[MAX_BUFF_SIZE]; 852 853 char *temp_str = NULL; 854 char *str_value = NULL; 855 u8 *buffer_ptr = NULL; 856 int buffer_size; 857 int ret = 0; 858 859 buffer_size = obj->buffer.length; 860 buffer_ptr = obj->buffer.pointer; 861 862 ret = hp_get_string_from_buffer(&buffer_ptr, 863 &buffer_size, str, MAX_BUFF_SIZE); 864 865 if (ret < 0) 866 goto buff_attr_exit; 867 868 if (strlen(str) == 0) { 869 pr_debug("Ignoring attribute with empty name\n"); 870 ret = 0; 871 goto buff_attr_exit; 872 } 873 874 if (attr_type == HPWMI_PASSWORD_TYPE || 875 attr_type == HPWMI_SECURE_PLATFORM_TYPE) 876 temp_kset = bioscfg_drv.authentication_dir_kset; 877 else 878 temp_kset = bioscfg_drv.main_dir_kset; 879 880 /* All duplicate attributes found are ignored */ 881 duplicate = kset_find_obj(temp_kset, str); 882 if (duplicate) { 883 pr_debug("Duplicate attribute name found - %s\n", str); 884 /* kset_find_obj() returns a reference */ 885 kobject_put(duplicate); 886 goto buff_attr_exit; 887 } 888 889 /* build attribute */ 890 attr_name_kobj = kzalloc_obj(*attr_name_kobj); 891 if (!attr_name_kobj) { 892 ret = -ENOMEM; 893 goto buff_attr_exit; 894 } 895 896 attr_name_kobj->kset = temp_kset; 897 898 temp_str = str; 899 if (attr_type == HPWMI_SECURE_PLATFORM_TYPE) 900 temp_str = "SPM"; 901 902 ret = kobject_init_and_add(attr_name_kobj, 903 &attr_name_ktype, NULL, "%s", temp_str); 904 if (ret) { 905 kobject_put(attr_name_kobj); 906 goto buff_attr_exit; 907 } 908 909 /* enumerate all of these attributes */ 910 switch (attr_type) { 911 case HPWMI_STRING_TYPE: 912 ret = hp_populate_string_buffer_data(buffer_ptr, 913 &buffer_size, 914 instance_id, 915 attr_name_kobj); 916 break; 917 case HPWMI_INTEGER_TYPE: 918 ret = hp_populate_integer_buffer_data(buffer_ptr, 919 &buffer_size, 920 instance_id, 921 attr_name_kobj); 922 break; 923 case HPWMI_ENUMERATION_TYPE: 924 ret = hp_populate_enumeration_buffer_data(buffer_ptr, 925 &buffer_size, 926 instance_id, 927 attr_name_kobj); 928 break; 929 case HPWMI_ORDERED_LIST_TYPE: 930 ret = hp_populate_ordered_list_buffer_data(buffer_ptr, 931 &buffer_size, 932 instance_id, 933 attr_name_kobj); 934 break; 935 case HPWMI_PASSWORD_TYPE: 936 ret = hp_populate_password_buffer_data(buffer_ptr, 937 &buffer_size, 938 instance_id, 939 attr_name_kobj); 940 break; 941 default: 942 pr_debug("Unknown attribute type found: 0x%x\n", attr_type); 943 break; 944 } 945 946 buff_attr_exit: 947 kfree(str_value); 948 return ret; 949 } 950 951 /** 952 * hp_init_bios_attributes() - Initialize all attributes for a type 953 * @attr_type: The attribute type to initialize 954 * @guid: The WMI GUID associated with this type to initialize 955 * 956 * Initialize all 5 types of attributes: enumeration, integer, 957 * string, password, ordered list object. Populates each attribute types 958 * respective properties under sysfs files 959 */ 960 static int hp_init_bios_attributes(enum hp_wmi_data_type attr_type, const char *guid) 961 { 962 union acpi_object *obj = NULL; 963 int min_elements; 964 965 /* instance_id needs to be reset for each type GUID 966 * also, instance IDs are unique within GUID but not across 967 */ 968 int instance_id = 0; 969 int cur_instance_id = instance_id; 970 int ret = 0; 971 972 ret = hp_alloc_attributes_data(attr_type); 973 if (ret) 974 return ret; 975 976 switch (attr_type) { 977 case HPWMI_STRING_TYPE: 978 min_elements = STR_ELEM_CNT; 979 break; 980 case HPWMI_INTEGER_TYPE: 981 min_elements = INT_ELEM_CNT; 982 break; 983 case HPWMI_ENUMERATION_TYPE: 984 min_elements = ENUM_ELEM_CNT; 985 break; 986 case HPWMI_ORDERED_LIST_TYPE: 987 min_elements = ORD_ELEM_CNT; 988 break; 989 case HPWMI_PASSWORD_TYPE: 990 min_elements = PSWD_ELEM_CNT; 991 break; 992 default: 993 pr_err("Error: Unknown attr_type: %d\n", attr_type); 994 return -EINVAL; 995 } 996 997 /* need to use specific instance_id and guid combination to get right data */ 998 obj = hp_get_wmiobj_pointer(instance_id, guid); 999 if (!obj) 1000 return -ENODEV; 1001 1002 mutex_lock(&bioscfg_drv.mutex); 1003 while (obj) { 1004 /* Take action appropriate to each ACPI TYPE */ 1005 if (obj->type == ACPI_TYPE_PACKAGE) { 1006 ret = hp_init_bios_package_attribute(attr_type, obj, 1007 guid, min_elements, 1008 cur_instance_id); 1009 1010 } else if (obj->type == ACPI_TYPE_BUFFER) { 1011 ret = hp_init_bios_buffer_attribute(attr_type, obj, 1012 guid, min_elements, 1013 cur_instance_id); 1014 1015 } else { 1016 pr_err("Expected ACPI-package or buffer type, got: %d\n", 1017 obj->type); 1018 ret = -EIO; 1019 goto err_attr_init; 1020 } 1021 1022 /* 1023 * Failure reported in one attribute must not 1024 * stop process of the remaining attribute values. 1025 */ 1026 if (ret >= 0) 1027 cur_instance_id++; 1028 1029 kfree(obj); 1030 instance_id++; 1031 obj = hp_get_wmiobj_pointer(instance_id, guid); 1032 } 1033 1034 err_attr_init: 1035 mutex_unlock(&bioscfg_drv.mutex); 1036 kfree(obj); 1037 return ret; 1038 } 1039 1040 static int __init hp_init(void) 1041 { 1042 int ret; 1043 int hp_bios_capable = wmi_has_guid(HP_WMI_BIOS_GUID); 1044 int set_bios_settings = wmi_has_guid(HP_WMI_SET_BIOS_SETTING_GUID); 1045 1046 if (!hp_bios_capable) { 1047 pr_err("Unable to run on non-HP system\n"); 1048 return -ENODEV; 1049 } 1050 1051 if (!set_bios_settings) { 1052 pr_err("Unable to set BIOS settings on HP systems\n"); 1053 return -ENODEV; 1054 } 1055 1056 ret = hp_init_attr_set_interface(); 1057 if (ret) 1058 return ret; 1059 1060 bioscfg_drv.class_dev = device_create(&firmware_attributes_class, NULL, MKDEV(0, 0), 1061 NULL, "%s", DRIVER_NAME); 1062 if (IS_ERR(bioscfg_drv.class_dev)) { 1063 ret = PTR_ERR(bioscfg_drv.class_dev); 1064 goto err_unregister_class; 1065 } 1066 1067 bioscfg_drv.main_dir_kset = kset_create_and_add("attributes", NULL, 1068 &bioscfg_drv.class_dev->kobj); 1069 if (!bioscfg_drv.main_dir_kset) { 1070 ret = -ENOMEM; 1071 pr_debug("Failed to create and add attributes\n"); 1072 goto err_destroy_classdev; 1073 } 1074 1075 bioscfg_drv.authentication_dir_kset = kset_create_and_add("authentication", NULL, 1076 &bioscfg_drv.class_dev->kobj); 1077 if (!bioscfg_drv.authentication_dir_kset) { 1078 ret = -ENOMEM; 1079 pr_debug("Failed to create and add authentication\n"); 1080 goto err_release_attributes_data; 1081 } 1082 1083 /* 1084 * sysfs level attributes. 1085 * - pending_reboot 1086 */ 1087 ret = create_attributes_level_sysfs_files(); 1088 if (ret) 1089 pr_debug("Failed to create sysfs level attributes\n"); 1090 1091 ret = hp_init_bios_attributes(HPWMI_STRING_TYPE, HP_WMI_BIOS_STRING_GUID); 1092 if (ret) 1093 pr_debug("Failed to populate string type attributes\n"); 1094 1095 ret = hp_init_bios_attributes(HPWMI_INTEGER_TYPE, HP_WMI_BIOS_INTEGER_GUID); 1096 if (ret) 1097 pr_debug("Failed to populate integer type attributes\n"); 1098 1099 ret = hp_init_bios_attributes(HPWMI_ENUMERATION_TYPE, HP_WMI_BIOS_ENUMERATION_GUID); 1100 if (ret) 1101 pr_debug("Failed to populate enumeration type attributes\n"); 1102 1103 ret = hp_init_bios_attributes(HPWMI_ORDERED_LIST_TYPE, HP_WMI_BIOS_ORDERED_LIST_GUID); 1104 if (ret) 1105 pr_debug("Failed to populate ordered list object type attributes\n"); 1106 1107 ret = hp_init_bios_attributes(HPWMI_PASSWORD_TYPE, HP_WMI_BIOS_PASSWORD_GUID); 1108 if (ret) 1109 pr_debug("Failed to populate password object type attributes\n"); 1110 1111 bioscfg_drv.spm_data.attr_name_kobj = NULL; 1112 ret = hp_add_other_attributes(HPWMI_SECURE_PLATFORM_TYPE); 1113 if (ret) 1114 pr_debug("Failed to populate secure platform object type attribute\n"); 1115 1116 bioscfg_drv.sure_start_attr_kobj = NULL; 1117 ret = hp_add_other_attributes(HPWMI_SURE_START_TYPE); 1118 if (ret) 1119 pr_debug("Failed to populate sure start object type attribute\n"); 1120 1121 return 0; 1122 1123 err_release_attributes_data: 1124 release_attributes_data(); 1125 1126 err_destroy_classdev: 1127 device_unregister(bioscfg_drv.class_dev); 1128 1129 err_unregister_class: 1130 hp_exit_attr_set_interface(); 1131 1132 return ret; 1133 } 1134 1135 static void __exit hp_exit(void) 1136 { 1137 release_attributes_data(); 1138 device_unregister(bioscfg_drv.class_dev); 1139 1140 hp_exit_attr_set_interface(); 1141 } 1142 1143 module_init(hp_init); 1144 module_exit(hp_exit); 1145