1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Think LMI BIOS configuration driver
5 * Copyright(C) 2019-2021 Lenovo
7 * Original code from Thinkpad-wmi project https://github.com/iksaif/thinkpad-wmi
8 * Copyright(C) 2017 Corentin Chary <corentin.chary@gmail.com>
9 * Distributed under the GPL-2.0 license
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/acpi.h>
15 #include <linux/errno.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19 #include <linux/wmi.h>
20 #include "firmware_attributes_class.h"
21 #include "think-lmi.h"
23 static bool debug_support;
24 module_param(debug_support, bool, 0444);
25 MODULE_PARM_DESC(debug_support, "Enable debug command support");
31 * Get item name and settings for current LMI instance.
39 #define LENOVO_BIOS_SETTING_GUID "51F5230E-9677-46CD-A1CF-C0B23EE34DB7"
43 * Lenovo_SetBiosSetting
45 * Change the BIOS setting to the desired value using the Lenovo_SetBiosSetting
46 * class. To save the settings, use the Lenovo_SaveBiosSetting class.
47 * BIOS settings and values are case sensitive.
48 * After making changes to the BIOS settings, you must reboot the computer
49 * before the changes will take effect.
53 * "Item,Value,Password,Encoding,KbdLang;"
55 * "WakeOnLAN,Disable,pa55w0rd,ascii,us;"
57 #define LENOVO_SET_BIOS_SETTINGS_GUID "98479A64-33F5-4E33-A707-8E251EBBC3A1"
61 * Lenovo_SaveBiosSettings
63 * Save any pending changes in settings.
67 * "Password,Encoding,KbdLang;"
69 * "pa55w0rd,ascii,us;"
71 #define LENOVO_SAVE_BIOS_SETTINGS_GUID "6A4B54EF-A5ED-4D33-9455-B0D9B48DF4B3"
75 * Lenovo_BiosPasswordSettings
77 * Return BIOS Password settings
81 * PasswordMode, PasswordState, MinLength, MaxLength,
82 * SupportedEncoding, SupportedKeyboard
84 #define LENOVO_BIOS_PASSWORD_SETTINGS_GUID "8ADB159E-1E32-455C-BC93-308A7ED98246"
88 * Lenovo_SetBiosPassword
90 * Change a specific password.
91 * - BIOS settings cannot be changed at the same boot as power-on
92 * passwords (POP) and hard disk passwords (HDP). If you want to change
93 * BIOS settings and POP or HDP, you must reboot the system after changing
95 * - A password cannot be set using this method when one does not already
96 * exist. Passwords can only be updated or cleared.
100 * "PasswordType,CurrentPassword,NewPassword,Encoding,KbdLang;"
102 * "pop,pa55w0rd,newpa55w0rd,ascii,us;”
104 #define LENOVO_SET_BIOS_PASSWORD_GUID "2651D9FD-911C-4B69-B94E-D0DED5963BD7"
108 * Lenovo_GetBiosSelections
110 * Return a list of valid settings for a given item.
116 * "Value1,Value2,Value3,..."
119 * <- "Enabled,Disabled"
121 #define LENOVO_GET_BIOS_SELECTIONS_GUID "7364651A-132F-4FE7-ADAA-40C6C7EE2E3B"
125 * Lenovo_DebugCmdGUID
127 * Debug entry GUID method for entering debug commands to the BIOS
129 #define LENOVO_DEBUG_CMD_GUID "7FF47003-3B6C-4E5E-A227-E979824A85D1"
131 #define TLMI_POP_PWD (1 << 0)
132 #define TLMI_PAP_PWD (1 << 1)
133 #define to_tlmi_pwd_setting(kobj) container_of(kobj, struct tlmi_pwd_setting, kobj)
134 #define to_tlmi_attr_setting(kobj) container_of(kobj, struct tlmi_attr_setting, kobj)
136 static const struct tlmi_err_codes tlmi_errs[] = {
138 {"Not Supported", -EOPNOTSUPP},
139 {"Invalid Parameter", -EINVAL},
140 {"Access Denied", -EACCES},
141 {"System Busy", -EBUSY},
144 static const char * const encoding_options[] = {
145 [TLMI_ENCODING_ASCII] = "ascii",
146 [TLMI_ENCODING_SCANCODE] = "scancode",
148 static struct think_lmi tlmi_priv;
149 static struct class *fw_attr_class;
151 /* ------ Utility functions ------------*/
152 /* Convert BIOS WMI error string to suitable error code */
153 static int tlmi_errstr_to_err(const char *errstr)
157 for (i = 0; i < sizeof(tlmi_errs)/sizeof(struct tlmi_err_codes); i++) {
158 if (!strcmp(tlmi_errs[i].err_str, errstr))
159 return tlmi_errs[i].err_code;
164 /* Extract error string from WMI return buffer */
165 static int tlmi_extract_error(const struct acpi_buffer *output)
167 const union acpi_object *obj;
169 obj = output->pointer;
172 if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer)
175 return tlmi_errstr_to_err(obj->string.pointer);
178 /* Utility function to execute WMI call to BIOS */
179 static int tlmi_simple_call(const char *guid, const char *arg)
181 const struct acpi_buffer input = { strlen(arg), (char *)arg };
182 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
187 * Duplicated call required to match BIOS workaround for behavior
188 * seen when WMI accessed via scripting on other OS.
190 for (i = 0; i < 2; i++) {
191 /* (re)initialize output buffer to default state */
192 output.length = ACPI_ALLOCATE_BUFFER;
193 output.pointer = NULL;
195 status = wmi_evaluate_method(guid, 0, 0, &input, &output);
196 if (ACPI_FAILURE(status)) {
197 kfree(output.pointer);
200 err = tlmi_extract_error(&output);
201 kfree(output.pointer);
208 /* Extract output string from WMI return buffer */
209 static int tlmi_extract_output_string(const struct acpi_buffer *output,
212 const union acpi_object *obj;
215 obj = output->pointer;
218 if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer)
221 s = kstrdup(obj->string.pointer, GFP_KERNEL);
228 /* ------ Core interface functions ------------*/
230 /* Get password settings from BIOS */
231 static int tlmi_get_pwd_settings(struct tlmi_pwdcfg *pwdcfg)
233 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
234 const union acpi_object *obj;
237 if (!tlmi_priv.can_get_password_settings)
240 status = wmi_query_block(LENOVO_BIOS_PASSWORD_SETTINGS_GUID, 0,
242 if (ACPI_FAILURE(status))
245 obj = output.pointer;
248 if (obj->type != ACPI_TYPE_BUFFER || !obj->buffer.pointer) {
253 * The size of thinkpad_wmi_pcfg on ThinkStation is larger than ThinkPad.
254 * To make the driver compatible on different brands, we permit it to get
255 * the data in below case.
257 if (obj->buffer.length < sizeof(struct tlmi_pwdcfg)) {
258 pr_warn("Unknown pwdcfg buffer length %d\n", obj->buffer.length);
262 memcpy(pwdcfg, obj->buffer.pointer, sizeof(struct tlmi_pwdcfg));
267 static int tlmi_save_bios_settings(const char *password)
269 return tlmi_simple_call(LENOVO_SAVE_BIOS_SETTINGS_GUID,
273 static int tlmi_setting(int item, char **value, const char *guid_string)
275 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
279 status = wmi_query_block(guid_string, item, &output);
280 if (ACPI_FAILURE(status)) {
281 kfree(output.pointer);
285 ret = tlmi_extract_output_string(&output, value);
286 kfree(output.pointer);
290 static int tlmi_get_bios_selections(const char *item, char **value)
292 const struct acpi_buffer input = { strlen(item), (char *)item };
293 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
297 status = wmi_evaluate_method(LENOVO_GET_BIOS_SELECTIONS_GUID,
298 0, 0, &input, &output);
300 if (ACPI_FAILURE(status)) {
301 kfree(output.pointer);
305 ret = tlmi_extract_output_string(&output, value);
306 kfree(output.pointer);
310 /* ---- Authentication sysfs --------------------------------------------------------- */
311 static ssize_t is_enabled_show(struct kobject *kobj, struct kobj_attribute *attr,
314 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
316 return sysfs_emit(buf, "%d\n", setting->valid);
319 static struct kobj_attribute auth_is_pass_set = __ATTR_RO(is_enabled);
321 static ssize_t current_password_store(struct kobject *kobj,
322 struct kobj_attribute *attr,
323 const char *buf, size_t count)
325 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
329 pwdlen = strlen(buf);
330 /* pwdlen == 0 is allowed to clear the password */
331 if (pwdlen && ((pwdlen < setting->minlen) || (pwdlen > setting->maxlen)))
334 strscpy(setting->password, buf, setting->maxlen);
335 /* Strip out CR if one is present, setting password won't work if it is present */
336 p = strchrnul(setting->password, '\n');
341 static struct kobj_attribute auth_current_password = __ATTR_WO(current_password);
343 static ssize_t new_password_store(struct kobject *kobj,
344 struct kobj_attribute *attr,
345 const char *buf, size_t count)
347 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
348 char *auth_str, *new_pwd, *p;
352 if (!capable(CAP_SYS_ADMIN))
355 if (!tlmi_priv.can_set_bios_password)
358 new_pwd = kstrdup(buf, GFP_KERNEL);
362 /* Strip out CR if one is present, setting password won't work if it is present */
363 p = strchrnul(new_pwd, '\n');
366 pwdlen = strlen(new_pwd);
367 /* pwdlen == 0 is allowed to clear the password */
368 if (pwdlen && ((pwdlen < setting->minlen) || (pwdlen > setting->maxlen))) {
373 /* Format: 'PasswordType,CurrentPw,NewPw,Encoding,KbdLang;' */
374 auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s,%s,%s;",
375 setting->pwd_type, setting->password, new_pwd,
376 encoding_options[setting->encoding], setting->kbdlang);
381 ret = tlmi_simple_call(LENOVO_SET_BIOS_PASSWORD_GUID, auth_str);
388 static struct kobj_attribute auth_new_password = __ATTR_WO(new_password);
390 static ssize_t min_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
393 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
395 return sysfs_emit(buf, "%d\n", setting->minlen);
398 static struct kobj_attribute auth_min_pass_length = __ATTR_RO(min_password_length);
400 static ssize_t max_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
403 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
405 return sysfs_emit(buf, "%d\n", setting->maxlen);
407 static struct kobj_attribute auth_max_pass_length = __ATTR_RO(max_password_length);
409 static ssize_t mechanism_show(struct kobject *kobj, struct kobj_attribute *attr,
412 return sysfs_emit(buf, "password\n");
414 static struct kobj_attribute auth_mechanism = __ATTR_RO(mechanism);
416 static ssize_t encoding_show(struct kobject *kobj, struct kobj_attribute *attr,
419 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
421 return sysfs_emit(buf, "%s\n", encoding_options[setting->encoding]);
424 static ssize_t encoding_store(struct kobject *kobj,
425 struct kobj_attribute *attr,
426 const char *buf, size_t count)
428 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
431 /* Scan for a matching profile */
432 i = sysfs_match_string(encoding_options, buf);
436 setting->encoding = i;
440 static struct kobj_attribute auth_encoding = __ATTR_RW(encoding);
442 static ssize_t kbdlang_show(struct kobject *kobj, struct kobj_attribute *attr,
445 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
447 return sysfs_emit(buf, "%s\n", setting->kbdlang);
450 static ssize_t kbdlang_store(struct kobject *kobj,
451 struct kobj_attribute *attr,
452 const char *buf, size_t count)
454 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
457 /* Calculate length till '\n' or terminating 0 */
458 length = strchrnul(buf, '\n') - buf;
459 if (!length || length >= TLMI_LANG_MAXLEN)
462 memcpy(setting->kbdlang, buf, length);
463 setting->kbdlang[length] = '\0';
467 static struct kobj_attribute auth_kbdlang = __ATTR_RW(kbdlang);
469 static ssize_t role_show(struct kobject *kobj, struct kobj_attribute *attr,
472 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
474 return sysfs_emit(buf, "%s\n", setting->role);
476 static struct kobj_attribute auth_role = __ATTR_RO(role);
478 static struct attribute *auth_attrs[] = {
479 &auth_is_pass_set.attr,
480 &auth_min_pass_length.attr,
481 &auth_max_pass_length.attr,
482 &auth_current_password.attr,
483 &auth_new_password.attr,
485 &auth_mechanism.attr,
491 static const struct attribute_group auth_attr_group = {
495 /* ---- Attributes sysfs --------------------------------------------------------- */
496 static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *attr,
499 struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
501 return sysfs_emit(buf, "%s\n", setting->display_name);
504 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
506 struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
510 ret = tlmi_setting(setting->index, &item, LENOVO_BIOS_SETTING_GUID);
514 /* validate and split from `item,value` -> `value` */
515 value = strpbrk(item, ",");
516 if (!value || value == item || !strlen(value + 1))
519 ret = sysfs_emit(buf, "%s\n", value + 1);
524 static ssize_t possible_values_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
526 struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
528 if (!tlmi_priv.can_get_bios_selections)
531 return sysfs_emit(buf, "%s\n", setting->possible_values);
534 static ssize_t current_value_store(struct kobject *kobj,
535 struct kobj_attribute *attr,
536 const char *buf, size_t count)
538 struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
539 char *set_str = NULL, *new_setting = NULL;
540 char *auth_str = NULL;
544 if (!tlmi_priv.can_set_bios_settings)
547 new_setting = kstrdup(buf, GFP_KERNEL);
551 /* Strip out CR if one is present */
552 p = strchrnul(new_setting, '\n');
555 if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
556 auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
557 tlmi_priv.pwd_admin->password,
558 encoding_options[tlmi_priv.pwd_admin->encoding],
559 tlmi_priv.pwd_admin->kbdlang);
567 set_str = kasprintf(GFP_KERNEL, "%s,%s,%s", setting->display_name,
568 new_setting, auth_str);
570 set_str = kasprintf(GFP_KERNEL, "%s,%s;", setting->display_name,
577 ret = tlmi_simple_call(LENOVO_SET_BIOS_SETTINGS_GUID, set_str);
582 ret = tlmi_save_bios_settings(auth_str);
584 ret = tlmi_save_bios_settings("");
586 if (!ret && !tlmi_priv.pending_changes) {
587 tlmi_priv.pending_changes = true;
588 /* let userland know it may need to check reboot pending again */
589 kobject_uevent(&tlmi_priv.class_dev->kobj, KOBJ_CHANGE);
598 static struct kobj_attribute attr_displ_name = __ATTR_RO(display_name);
600 static struct kobj_attribute attr_possible_values = __ATTR_RO(possible_values);
602 static struct kobj_attribute attr_current_val = __ATTR_RW_MODE(current_value, 0600);
604 static struct attribute *tlmi_attrs[] = {
605 &attr_displ_name.attr,
606 &attr_current_val.attr,
607 &attr_possible_values.attr,
611 static const struct attribute_group tlmi_attr_group = {
615 static ssize_t tlmi_attr_show(struct kobject *kobj, struct attribute *attr,
618 struct kobj_attribute *kattr;
620 kattr = container_of(attr, struct kobj_attribute, attr);
622 return kattr->show(kobj, kattr, buf);
626 static ssize_t tlmi_attr_store(struct kobject *kobj, struct attribute *attr,
627 const char *buf, size_t count)
629 struct kobj_attribute *kattr;
631 kattr = container_of(attr, struct kobj_attribute, attr);
633 return kattr->store(kobj, kattr, buf, count);
637 static const struct sysfs_ops tlmi_kobj_sysfs_ops = {
638 .show = tlmi_attr_show,
639 .store = tlmi_attr_store,
642 static void tlmi_attr_setting_release(struct kobject *kobj)
644 struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
646 kfree(setting->possible_values);
650 static void tlmi_pwd_setting_release(struct kobject *kobj)
652 struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
657 static struct kobj_type tlmi_attr_setting_ktype = {
658 .release = &tlmi_attr_setting_release,
659 .sysfs_ops = &tlmi_kobj_sysfs_ops,
662 static struct kobj_type tlmi_pwd_setting_ktype = {
663 .release = &tlmi_pwd_setting_release,
664 .sysfs_ops = &tlmi_kobj_sysfs_ops,
667 static ssize_t pending_reboot_show(struct kobject *kobj, struct kobj_attribute *attr,
670 return sprintf(buf, "%d\n", tlmi_priv.pending_changes);
673 static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);
675 /* ---- Debug interface--------------------------------------------------------- */
676 static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attr,
677 const char *buf, size_t count)
679 char *set_str = NULL, *new_setting = NULL;
680 char *auth_str = NULL;
684 if (!tlmi_priv.can_debug_cmd)
687 new_setting = kstrdup(buf, GFP_KERNEL);
691 /* Strip out CR if one is present */
692 p = strchrnul(new_setting, '\n');
695 if (tlmi_priv.pwd_admin->valid && tlmi_priv.pwd_admin->password[0]) {
696 auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
697 tlmi_priv.pwd_admin->password,
698 encoding_options[tlmi_priv.pwd_admin->encoding],
699 tlmi_priv.pwd_admin->kbdlang);
707 set_str = kasprintf(GFP_KERNEL, "%s,%s", new_setting, auth_str);
709 set_str = kasprintf(GFP_KERNEL, "%s;", new_setting);
715 ret = tlmi_simple_call(LENOVO_DEBUG_CMD_GUID, set_str);
719 if (!ret && !tlmi_priv.pending_changes) {
720 tlmi_priv.pending_changes = true;
721 /* let userland know it may need to check reboot pending again */
722 kobject_uevent(&tlmi_priv.class_dev->kobj, KOBJ_CHANGE);
731 static struct kobj_attribute debug_cmd = __ATTR_WO(debug_cmd);
733 /* ---- Initialisation --------------------------------------------------------- */
734 static void tlmi_release_attr(void)
738 /* Attribute structures */
739 for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
740 if (tlmi_priv.setting[i]) {
741 sysfs_remove_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
742 kobject_put(&tlmi_priv.setting[i]->kobj);
745 sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
746 if (tlmi_priv.can_debug_cmd && debug_support)
747 sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &debug_cmd.attr);
748 kset_unregister(tlmi_priv.attribute_kset);
750 /* Authentication structures */
751 sysfs_remove_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
752 kobject_put(&tlmi_priv.pwd_admin->kobj);
753 sysfs_remove_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
754 kobject_put(&tlmi_priv.pwd_power->kobj);
755 kset_unregister(tlmi_priv.authentication_kset);
758 static int tlmi_sysfs_init(void)
762 ret = fw_attributes_class_get(&fw_attr_class);
766 tlmi_priv.class_dev = device_create(fw_attr_class, NULL, MKDEV(0, 0),
767 NULL, "%s", "thinklmi");
768 if (IS_ERR(tlmi_priv.class_dev)) {
769 ret = PTR_ERR(tlmi_priv.class_dev);
770 goto fail_class_created;
773 tlmi_priv.attribute_kset = kset_create_and_add("attributes", NULL,
774 &tlmi_priv.class_dev->kobj);
775 if (!tlmi_priv.attribute_kset) {
777 goto fail_device_created;
780 for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
781 /* Check if index is a valid setting - skip if it isn't */
782 if (!tlmi_priv.setting[i])
785 /* check for duplicate or reserved values */
786 if (kset_find_obj(tlmi_priv.attribute_kset, tlmi_priv.setting[i]->display_name) ||
787 !strcmp(tlmi_priv.setting[i]->display_name, "Reserved")) {
788 pr_debug("duplicate or reserved attribute name found - %s\n",
789 tlmi_priv.setting[i]->display_name);
790 kfree(tlmi_priv.setting[i]->possible_values);
791 kfree(tlmi_priv.setting[i]);
792 tlmi_priv.setting[i] = NULL;
796 /* Build attribute */
797 tlmi_priv.setting[i]->kobj.kset = tlmi_priv.attribute_kset;
798 ret = kobject_add(&tlmi_priv.setting[i]->kobj, NULL,
799 "%s", tlmi_priv.setting[i]->display_name);
801 goto fail_create_attr;
803 ret = sysfs_create_group(&tlmi_priv.setting[i]->kobj, &tlmi_attr_group);
805 goto fail_create_attr;
808 ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
810 goto fail_create_attr;
812 if (tlmi_priv.can_debug_cmd && debug_support) {
813 ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &debug_cmd.attr);
815 goto fail_create_attr;
817 /* Create authentication entries */
818 tlmi_priv.authentication_kset = kset_create_and_add("authentication", NULL,
819 &tlmi_priv.class_dev->kobj);
820 if (!tlmi_priv.authentication_kset) {
822 goto fail_create_attr;
824 tlmi_priv.pwd_admin->kobj.kset = tlmi_priv.authentication_kset;
825 ret = kobject_add(&tlmi_priv.pwd_admin->kobj, NULL, "%s", "Admin");
827 goto fail_create_attr;
829 ret = sysfs_create_group(&tlmi_priv.pwd_admin->kobj, &auth_attr_group);
831 goto fail_create_attr;
833 tlmi_priv.pwd_power->kobj.kset = tlmi_priv.authentication_kset;
834 ret = kobject_add(&tlmi_priv.pwd_power->kobj, NULL, "%s", "System");
836 goto fail_create_attr;
838 ret = sysfs_create_group(&tlmi_priv.pwd_power->kobj, &auth_attr_group);
840 goto fail_create_attr;
847 device_destroy(fw_attr_class, MKDEV(0, 0));
849 fw_attributes_class_put();
853 /* ---- Base Driver -------------------------------------------------------- */
854 static int tlmi_analyze(void)
856 struct tlmi_pwdcfg pwdcfg;
860 if (wmi_has_guid(LENOVO_SET_BIOS_SETTINGS_GUID) &&
861 wmi_has_guid(LENOVO_SAVE_BIOS_SETTINGS_GUID))
862 tlmi_priv.can_set_bios_settings = true;
864 if (wmi_has_guid(LENOVO_GET_BIOS_SELECTIONS_GUID))
865 tlmi_priv.can_get_bios_selections = true;
867 if (wmi_has_guid(LENOVO_SET_BIOS_PASSWORD_GUID))
868 tlmi_priv.can_set_bios_password = true;
870 if (wmi_has_guid(LENOVO_BIOS_PASSWORD_SETTINGS_GUID))
871 tlmi_priv.can_get_password_settings = true;
873 if (wmi_has_guid(LENOVO_DEBUG_CMD_GUID))
874 tlmi_priv.can_debug_cmd = true;
877 * Try to find the number of valid settings of this machine
878 * and use it to create sysfs attributes.
880 for (i = 0; i < TLMI_SETTINGS_COUNT; ++i) {
881 struct tlmi_attr_setting *setting;
885 tlmi_priv.setting[i] = NULL;
886 status = tlmi_setting(i, &item, LENOVO_BIOS_SETTING_GUID);
887 if (ACPI_FAILURE(status))
896 /* It is not allowed to have '/' for file name. Convert it into '\'. */
897 strreplace(item, '/', '\\');
899 /* Remove the value part */
900 p = strchrnul(item, ',');
903 /* Create a setting entry */
904 setting = kzalloc(sizeof(*setting), GFP_KERNEL);
908 goto fail_clear_attr;
911 strscpy(setting->display_name, item, TLMI_SETTINGS_MAXLEN);
912 /* If BIOS selections supported, load those */
913 if (tlmi_priv.can_get_bios_selections) {
914 ret = tlmi_get_bios_selections(setting->display_name,
915 &setting->possible_values);
916 if (ret || !setting->possible_values)
917 pr_info("Error retrieving possible values for %d : %s\n",
918 i, setting->display_name);
920 kobject_init(&setting->kobj, &tlmi_attr_setting_ktype);
921 tlmi_priv.setting[i] = setting;
925 /* Create password setting structure */
926 ret = tlmi_get_pwd_settings(&pwdcfg);
928 goto fail_clear_attr;
930 tlmi_priv.pwd_admin = kzalloc(sizeof(struct tlmi_pwd_setting), GFP_KERNEL);
931 if (!tlmi_priv.pwd_admin) {
933 goto fail_clear_attr;
935 strscpy(tlmi_priv.pwd_admin->kbdlang, "us", TLMI_LANG_MAXLEN);
936 tlmi_priv.pwd_admin->encoding = TLMI_ENCODING_ASCII;
937 tlmi_priv.pwd_admin->pwd_type = "pap";
938 tlmi_priv.pwd_admin->role = "bios-admin";
939 tlmi_priv.pwd_admin->minlen = pwdcfg.min_length;
940 if (WARN_ON(pwdcfg.max_length >= TLMI_PWD_BUFSIZE))
941 pwdcfg.max_length = TLMI_PWD_BUFSIZE - 1;
942 tlmi_priv.pwd_admin->maxlen = pwdcfg.max_length;
943 if (pwdcfg.password_state & TLMI_PAP_PWD)
944 tlmi_priv.pwd_admin->valid = true;
946 kobject_init(&tlmi_priv.pwd_admin->kobj, &tlmi_pwd_setting_ktype);
948 tlmi_priv.pwd_power = kzalloc(sizeof(struct tlmi_pwd_setting), GFP_KERNEL);
949 if (!tlmi_priv.pwd_power) {
951 goto fail_free_pwd_admin;
953 strscpy(tlmi_priv.pwd_power->kbdlang, "us", TLMI_LANG_MAXLEN);
954 tlmi_priv.pwd_power->encoding = TLMI_ENCODING_ASCII;
955 tlmi_priv.pwd_power->pwd_type = "pop";
956 tlmi_priv.pwd_power->role = "power-on";
957 tlmi_priv.pwd_power->minlen = pwdcfg.min_length;
958 tlmi_priv.pwd_power->maxlen = pwdcfg.max_length;
960 if (pwdcfg.password_state & TLMI_POP_PWD)
961 tlmi_priv.pwd_power->valid = true;
963 kobject_init(&tlmi_priv.pwd_power->kobj, &tlmi_pwd_setting_ktype);
968 kfree(tlmi_priv.pwd_admin);
970 for (i = 0; i < TLMI_SETTINGS_COUNT; ++i) {
971 if (tlmi_priv.setting[i]) {
972 kfree(tlmi_priv.setting[i]->possible_values);
973 kfree(tlmi_priv.setting[i]);
979 static void tlmi_remove(struct wmi_device *wdev)
982 device_destroy(fw_attr_class, MKDEV(0, 0));
983 fw_attributes_class_put();
986 static int tlmi_probe(struct wmi_device *wdev, const void *context)
990 ret = tlmi_analyze();
994 return tlmi_sysfs_init();
997 static const struct wmi_device_id tlmi_id_table[] = {
998 { .guid_string = LENOVO_BIOS_SETTING_GUID },
1001 MODULE_DEVICE_TABLE(wmi, tlmi_id_table);
1003 static struct wmi_driver tlmi_driver = {
1005 .name = "think-lmi",
1007 .id_table = tlmi_id_table,
1008 .probe = tlmi_probe,
1009 .remove = tlmi_remove,
1012 MODULE_AUTHOR("Sugumaran L <slacshiminar@lenovo.com>");
1013 MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>");
1014 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
1015 MODULE_DESCRIPTION("ThinkLMI Driver");
1016 MODULE_LICENSE("GPL");
1018 module_wmi_driver(tlmi_driver);