xref: /linux/drivers/platform/x86/lenovo/think-lmi.c (revision 68a052239fc4b351e961f698b824f7654a346091)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Think LMI BIOS configuration driver
4  *
5  * Copyright(C) 2019-2021 Lenovo
6  *
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
10  */
11 
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <linux/acpi.h>
15 #include <linux/array_size.h>
16 #include <linux/errno.h>
17 #include <linux/fs.h>
18 #include <linux/mutex.h>
19 #include <linux/string_helpers.h>
20 #include <linux/types.h>
21 #include <linux/dmi.h>
22 #include <linux/wmi.h>
23 #include "../firmware_attributes_class.h"
24 #include "think-lmi.h"
25 
26 static bool debug_support;
27 module_param(debug_support, bool, 0444);
28 MODULE_PARM_DESC(debug_support, "Enable debug command support");
29 
30 /*
31  * Name: BiosSetting
32  * Description: Get item name and settings for current LMI instance.
33  * Type: Query
34  * Returns: "Item,Value"
35  * Example: "WakeOnLAN,Enable"
36  */
37 #define LENOVO_BIOS_SETTING_GUID "51F5230E-9677-46CD-A1CF-C0B23EE34DB7"
38 
39 /*
40  * Name: SetBiosSetting
41  * Description: Change the BIOS setting to the desired value using the SetBiosSetting
42  *  class. To save the settings, use the SaveBiosSetting class.
43  *  BIOS settings and values are case sensitive.
44  *  After making changes to the BIOS settings, you must reboot the computer
45  *  before the changes will take effect.
46  * Type: Method
47  * Arguments: "Item,Value,Password,Encoding,KbdLang;"
48  * Example: "WakeOnLAN,Disable,pa55w0rd,ascii,us;"
49  */
50 #define LENOVO_SET_BIOS_SETTINGS_GUID "98479A64-33F5-4E33-A707-8E251EBBC3A1"
51 
52 /*
53  * Name: SaveBiosSettings
54  * Description: Save any pending changes in settings.
55  * Type: Method
56  * Arguments: "Password,Encoding,KbdLang;"
57  * Example: "pa55w0rd,ascii,us;"
58  */
59 #define LENOVO_SAVE_BIOS_SETTINGS_GUID "6A4B54EF-A5ED-4D33-9455-B0D9B48DF4B3"
60 
61 /*
62  * Name: BiosPasswordSettings
63  * Description: Return BIOS Password settings
64  * Type: Query
65  * Returns: PasswordMode, PasswordState, MinLength, MaxLength,
66  *  SupportedEncoding, SupportedKeyboard
67  */
68 #define LENOVO_BIOS_PASSWORD_SETTINGS_GUID "8ADB159E-1E32-455C-BC93-308A7ED98246"
69 
70 /*
71  * Name: SetBiosPassword
72  * Description: Change a specific password.
73  *  - BIOS settings cannot be changed at the same boot as power-on
74  *    passwords (POP) and hard disk passwords (HDP). If you want to change
75  *    BIOS settings and POP or HDP, you must reboot the system after changing
76  *    one of them.
77  *  - A password cannot be set using this method when one does not already
78  *    exist. Passwords can only be updated or cleared.
79  * Type: Method
80  * Arguments: "PasswordType,CurrentPassword,NewPassword,Encoding,KbdLang;"
81  * Example: "pop,pa55w0rd,newpa55w0rd,ascii,us;”
82  */
83 #define LENOVO_SET_BIOS_PASSWORD_GUID "2651D9FD-911C-4B69-B94E-D0DED5963BD7"
84 
85 /*
86  * Name: GetBiosSelections
87  * Description: Return a list of valid settings for a given item.
88  * Type: Method
89  * Arguments: "Item"
90  * Returns: "Value1,Value2,Value3,..."
91  * Example:
92  *  -> "FlashOverLAN"
93  *  <- "Enabled,Disabled"
94  */
95 #define LENOVO_GET_BIOS_SELECTIONS_GUID	"7364651A-132F-4FE7-ADAA-40C6C7EE2E3B"
96 
97 /*
98  * Name: DebugCmd
99  * Description: Debug entry method for entering debug commands to the BIOS
100  */
101 #define LENOVO_DEBUG_CMD_GUID "7FF47003-3B6C-4E5E-A227-E979824A85D1"
102 
103 /*
104  * Name: OpcodeIF
105  * Description: Opcode interface which provides the ability to set multiple
106  *  parameters and then trigger an action with a final command.
107  *  This is particularly useful for simplifying setting passwords.
108  *  With this support comes the ability to set System, HDD and NVMe
109  *  passwords.
110  *  This is currently available on ThinkCenter and ThinkStations platforms
111  */
112 #define LENOVO_OPCODE_IF_GUID "DFDDEF2C-57D4-48ce-B196-0FB787D90836"
113 
114 /*
115  * Name: SetBiosCert
116  * Description: Install BIOS certificate.
117  * Type: Method
118  * Arguments: "Certificate,Password"
119  * You must reboot the computer before the changes will take effect.
120  */
121 #define LENOVO_SET_BIOS_CERT_GUID    "26861C9F-47E9-44C4-BD8B-DFE7FA2610FE"
122 #define LENOVO_TC_SET_BIOS_CERT_GUID "955aaf7d-8bc4-4f04-90aa-97469512f167"
123 
124 /*
125  * Name: UpdateBiosCert
126  * Description: Update BIOS certificate.
127  * Type: Method
128  * Format: "Certificate,Signature"
129  * You must reboot the computer before the changes will take effect.
130  */
131 #define LENOVO_UPDATE_BIOS_CERT_GUID "9AA3180A-9750-41F7-B9F7-D5D3B1BAC3CE"
132 #define LENOVO_TC_UPDATE_BIOS_CERT_GUID "5f5bbbb2-c72f-4fb8-8129-228eef4fdbed"
133 
134 /*
135  * Name: ClearBiosCert
136  * Description: Uninstall BIOS certificate.
137  * Type: Method
138  * Format: "Serial,Signature"
139  * You must reboot the computer before the changes will take effect.
140  */
141 #define LENOVO_CLEAR_BIOS_CERT_GUID  "B2BC39A7-78DD-4D71-B059-A510DEC44890"
142 #define LENOVO_TC_CLEAR_BIOS_CERT_GUID  "97849cb6-cb44-42d1-a750-26a596a9eec4"
143 
144 /*
145  * Name: CertToPassword
146  * Description: Switch from certificate to password authentication.
147  * Type: Method
148  * Format: "Password,Signature"
149  * You must reboot the computer before the changes will take effect.
150  */
151 #define LENOVO_CERT_TO_PASSWORD_GUID "0DE8590D-5510-4044-9621-77C227F5A70D"
152 #define LENOVO_TC_CERT_TO_PASSWORD_GUID "ef65480d-38c9-420d-b700-ab3d6c8ebaca"
153 
154 /*
155  * Name: SetBiosSettingCert
156  * Description: Set attribute using certificate authentication.
157  * Type: Method
158  * Format: "Item,Value,Signature"
159  */
160 #define LENOVO_SET_BIOS_SETTING_CERT_GUID  "34A008CC-D205-4B62-9E67-31DFA8B90003"
161 #define LENOVO_TC_SET_BIOS_SETTING_CERT_GUID  "19ecba3b-b318-4192-a89b-43d94bc60cea"
162 
163 /*
164  * Name: SaveBiosSettingCert
165  * Description: Save any pending changes in settings.
166  * Type: Method
167  * Format: "Signature"
168  */
169 #define LENOVO_SAVE_BIOS_SETTING_CERT_GUID "C050FB9D-DF5F-4606-B066-9EFC401B2551"
170 #define LENOVO_TC_SAVE_BIOS_SETTING_CERT_GUID "0afaf46f-7cca-450a-b455-a826a0bf1af5"
171 
172 /*
173  * Name: CertThumbprint
174  * Description: Display Certificate thumbprints
175  * Type: Query
176  * Returns: MD5, SHA1 & SHA256 thumbprints
177  */
178 #define LENOVO_CERT_THUMBPRINT_GUID "C59119ED-1C0D-4806-A8E9-59AA318176C4"
179 
180 #define TLMI_POP_PWD  BIT(0) /* Supervisor */
181 #define TLMI_PAP_PWD  BIT(1) /* Power-on */
182 #define TLMI_HDD_PWD  BIT(2) /* HDD/NVME */
183 #define TLMI_SMP_PWD  BIT(6) /* System Management */
184 #define TLMI_CERT_SVC BIT(7) /* Admin Certificate Based */
185 #define TLMI_CERT_SMC BIT(8) /* System Certificate Based */
186 
187 static const struct tlmi_cert_guids thinkpad_cert_guid = {
188 	.thumbprint = LENOVO_CERT_THUMBPRINT_GUID,
189 	.set_bios_setting = LENOVO_SET_BIOS_SETTING_CERT_GUID,
190 	.save_bios_setting = LENOVO_SAVE_BIOS_SETTING_CERT_GUID,
191 	.cert_to_password = LENOVO_CERT_TO_PASSWORD_GUID,
192 	.clear_bios_cert = LENOVO_CLEAR_BIOS_CERT_GUID,
193 	.update_bios_cert = LENOVO_UPDATE_BIOS_CERT_GUID,
194 	.set_bios_cert = LENOVO_SET_BIOS_CERT_GUID,
195 };
196 
197 static const struct tlmi_cert_guids thinkcenter_cert_guid = {
198 	.thumbprint = NULL,
199 	.set_bios_setting = LENOVO_TC_SET_BIOS_SETTING_CERT_GUID,
200 	.save_bios_setting = LENOVO_TC_SAVE_BIOS_SETTING_CERT_GUID,
201 	.cert_to_password = LENOVO_TC_CERT_TO_PASSWORD_GUID,
202 	.clear_bios_cert = LENOVO_TC_CLEAR_BIOS_CERT_GUID,
203 	.update_bios_cert = LENOVO_TC_UPDATE_BIOS_CERT_GUID,
204 	.set_bios_cert = LENOVO_TC_SET_BIOS_CERT_GUID,
205 };
206 
207 static const struct tlmi_err_codes tlmi_errs[] = {
208 	{"Success", 0},
209 	{"Set Certificate operation was successful.", 0},
210 	{"Not Supported", -EOPNOTSUPP},
211 	{"Invalid Parameter", -EINVAL},
212 	{"Access Denied", -EACCES},
213 	{"System Busy", -EBUSY},
214 	{"Set Certificate operation failed with status:Invalid Parameter.", -EINVAL},
215 	{"Set Certificate operation failed with status:Invalid certificate type.", -EINVAL},
216 	{"Set Certificate operation failed with status:Invalid password format.", -EINVAL},
217 	{"Set Certificate operation failed with status:Password retry count exceeded.", -EACCES},
218 	{"Set Certificate operation failed with status:Password Invalid.", -EACCES},
219 	{"Set Certificate operation failed with status:Operation aborted.", -EBUSY},
220 	{"Set Certificate operation failed with status:No free slots to write.", -ENOSPC},
221 	{"Set Certificate operation failed with status:Certificate not found.", -EEXIST},
222 	{"Set Certificate operation failed with status:Internal error.", -EFAULT},
223 	{"Set Certificate operation failed with status:Certificate too large.", -EFBIG},
224 };
225 
226 static const char * const encoding_options[] = {
227 	[TLMI_ENCODING_ASCII] = "ascii",
228 	[TLMI_ENCODING_SCANCODE] = "scancode",
229 };
230 static const char * const level_options[] = {
231 	[TLMI_LEVEL_USER] = "user",
232 	[TLMI_LEVEL_MASTER] = "master",
233 };
234 static struct think_lmi tlmi_priv;
235 static DEFINE_MUTEX(tlmi_mutex);
236 
237 static inline struct tlmi_pwd_setting *to_tlmi_pwd_setting(struct kobject *kobj)
238 {
239 	return container_of(kobj, struct tlmi_pwd_setting, kobj);
240 }
241 
242 static inline struct tlmi_attr_setting *to_tlmi_attr_setting(struct kobject *kobj)
243 {
244 	return container_of(kobj, struct tlmi_attr_setting, kobj);
245 }
246 
247 /* Convert BIOS WMI error string to suitable error code */
248 static int tlmi_errstr_to_err(const char *errstr)
249 {
250 	int i;
251 
252 	for (i = 0; i < sizeof(tlmi_errs)/sizeof(struct tlmi_err_codes); i++) {
253 		if (!strcmp(tlmi_errs[i].err_str, errstr))
254 			return tlmi_errs[i].err_code;
255 	}
256 	return -EPERM;
257 }
258 
259 /* Extract error string from WMI return buffer */
260 static int tlmi_extract_error(const struct acpi_buffer *output)
261 {
262 	const union acpi_object *obj;
263 
264 	obj = output->pointer;
265 	if (!obj)
266 		return -ENOMEM;
267 	if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer)
268 		return -EIO;
269 
270 	return tlmi_errstr_to_err(obj->string.pointer);
271 }
272 
273 /* Utility function to execute WMI call to BIOS */
274 static int tlmi_simple_call(const char *guid, const char *arg)
275 {
276 	const struct acpi_buffer input = { strlen(arg), (char *)arg };
277 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
278 	acpi_status status;
279 	int i, err;
280 
281 	/*
282 	 * Duplicated call required to match BIOS workaround for behavior
283 	 * seen when WMI accessed via scripting on other OS.
284 	 */
285 	for (i = 0; i < 2; i++) {
286 		/* (re)initialize output buffer to default state */
287 		output.length = ACPI_ALLOCATE_BUFFER;
288 		output.pointer = NULL;
289 
290 		status = wmi_evaluate_method(guid, 0, 0, &input, &output);
291 		if (ACPI_FAILURE(status)) {
292 			kfree(output.pointer);
293 			return -EIO;
294 		}
295 		err = tlmi_extract_error(&output);
296 		kfree(output.pointer);
297 		if (err)
298 			return err;
299 	}
300 	return 0;
301 }
302 
303 /* Extract output string from WMI return value */
304 static int tlmi_extract_output_string(union acpi_object *obj, char **string)
305 {
306 	char *s;
307 
308 	if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer)
309 		return -EIO;
310 
311 	s = kstrdup(obj->string.pointer, GFP_KERNEL);
312 	if (!s)
313 		return -ENOMEM;
314 	*string = s;
315 	return 0;
316 }
317 
318 /* ------ Core interface functions ------------*/
319 
320 /* Get password settings from BIOS */
321 static int tlmi_get_pwd_settings(struct tlmi_pwdcfg *pwdcfg)
322 {
323 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
324 	const union acpi_object *obj;
325 	acpi_status status;
326 	int copy_size;
327 
328 	if (!tlmi_priv.can_get_password_settings)
329 		return -EOPNOTSUPP;
330 
331 	status = wmi_query_block(LENOVO_BIOS_PASSWORD_SETTINGS_GUID, 0,
332 				 &output);
333 	if (ACPI_FAILURE(status))
334 		return -EIO;
335 
336 	obj = output.pointer;
337 	if (!obj)
338 		return -ENOMEM;
339 	if (obj->type != ACPI_TYPE_BUFFER || !obj->buffer.pointer) {
340 		kfree(obj);
341 		return -EIO;
342 	}
343 	/*
344 	 * The size of thinkpad_wmi_pcfg on ThinkStation is larger than ThinkPad.
345 	 * To make the driver compatible on different brands, we permit it to get
346 	 * the data in below case.
347 	 * Settings must have at minimum the core fields available
348 	 */
349 	if (obj->buffer.length < sizeof(struct tlmi_pwdcfg_core)) {
350 		pr_warn("Unknown pwdcfg buffer length %d\n", obj->buffer.length);
351 		kfree(obj);
352 		return -EIO;
353 	}
354 
355 	copy_size = min_t(size_t, obj->buffer.length, sizeof(struct tlmi_pwdcfg));
356 
357 	memcpy(pwdcfg, obj->buffer.pointer, copy_size);
358 	kfree(obj);
359 
360 	if (WARN_ON(pwdcfg->core.max_length >= TLMI_PWD_BUFSIZE))
361 		pwdcfg->core.max_length = TLMI_PWD_BUFSIZE - 1;
362 	return 0;
363 }
364 
365 static int tlmi_save_bios_settings(const char *password)
366 {
367 	return tlmi_simple_call(LENOVO_SAVE_BIOS_SETTINGS_GUID,
368 				password);
369 }
370 
371 static int tlmi_opcode_setting(char *setting, const char *value)
372 {
373 	char *opcode_str;
374 	int ret;
375 
376 	opcode_str = kasprintf(GFP_KERNEL, "%s:%s;", setting, value);
377 	if (!opcode_str)
378 		return -ENOMEM;
379 
380 	ret = tlmi_simple_call(LENOVO_OPCODE_IF_GUID, opcode_str);
381 	kfree(opcode_str);
382 	return ret;
383 }
384 
385 static int tlmi_setting(struct wmi_device *wdev, int item, char **value)
386 {
387 	union acpi_object *obj;
388 	int ret;
389 
390 	obj = wmidev_block_query(wdev, item);
391 	if (!obj)
392 		return -EIO;
393 
394 	ret = tlmi_extract_output_string(obj, value);
395 	kfree(obj);
396 
397 	return ret;
398 }
399 
400 static int tlmi_get_bios_selections(const char *item, char **value)
401 {
402 	const struct acpi_buffer input = { strlen(item), (char *)item };
403 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
404 	union acpi_object *obj;
405 	acpi_status status;
406 	int ret;
407 
408 	status = wmi_evaluate_method(LENOVO_GET_BIOS_SELECTIONS_GUID,
409 				     0, 0, &input, &output);
410 	if (ACPI_FAILURE(status))
411 		return -EIO;
412 
413 	obj = output.pointer;
414 	if (!obj)
415 		return -ENODATA;
416 
417 	ret = tlmi_extract_output_string(obj, value);
418 	kfree(obj);
419 
420 	return ret;
421 }
422 
423 /* ---- Authentication sysfs --------------------------------------------------------- */
424 static ssize_t is_enabled_show(struct kobject *kobj, struct kobj_attribute *attr,
425 					  char *buf)
426 {
427 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
428 
429 	return sysfs_emit(buf, "%d\n", setting->pwd_enabled || setting->cert_installed);
430 }
431 
432 static struct kobj_attribute auth_is_pass_set = __ATTR_RO(is_enabled);
433 
434 static ssize_t current_password_store(struct kobject *kobj,
435 				      struct kobj_attribute *attr,
436 				      const char *buf, size_t count)
437 {
438 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
439 	size_t pwdlen;
440 
441 	pwdlen = strlen(buf);
442 	/* pwdlen == 0 is allowed to clear the password */
443 	if (pwdlen && ((pwdlen < setting->minlen) || (pwdlen > setting->maxlen)))
444 		return -EINVAL;
445 
446 	strscpy(setting->password, buf, setting->maxlen);
447 	/* Strip out CR if one is present, setting password won't work if it is present */
448 	strreplace(setting->password, '\n', '\0');
449 	return count;
450 }
451 
452 static struct kobj_attribute auth_current_password = __ATTR_WO(current_password);
453 
454 static ssize_t new_password_store(struct kobject *kobj,
455 				  struct kobj_attribute *attr,
456 				  const char *buf, size_t count)
457 {
458 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
459 	char *auth_str, *new_pwd;
460 	size_t pwdlen;
461 	int ret;
462 
463 	if (!capable(CAP_SYS_ADMIN))
464 		return -EPERM;
465 
466 	if (!tlmi_priv.can_set_bios_password)
467 		return -EOPNOTSUPP;
468 
469 	/* Strip out CR if one is present, setting password won't work if it is present */
470 	new_pwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
471 	if (!new_pwd)
472 		return -ENOMEM;
473 
474 	/* Use lock in case multiple WMI operations needed */
475 	mutex_lock(&tlmi_mutex);
476 
477 	pwdlen = strlen(new_pwd);
478 	/* pwdlen == 0 is allowed to clear the password */
479 	if (pwdlen && ((pwdlen < setting->minlen) || (pwdlen > setting->maxlen))) {
480 		ret = -EINVAL;
481 		goto out;
482 	}
483 
484 	/* If opcode support is present use that interface */
485 	if (tlmi_priv.opcode_support) {
486 		char pwd_type[8];
487 
488 		/* Special handling required for HDD and NVMe passwords */
489 		if (setting == tlmi_priv.pwd_hdd) {
490 			if (setting->level == TLMI_LEVEL_USER)
491 				sprintf(pwd_type, "uhdp%d", setting->index);
492 			else
493 				sprintf(pwd_type, "mhdp%d", setting->index);
494 		} else if (setting == tlmi_priv.pwd_nvme) {
495 			if (setting->level == TLMI_LEVEL_USER)
496 				sprintf(pwd_type, "udrp%d", setting->index);
497 			else
498 				sprintf(pwd_type, "adrp%d", setting->index);
499 		} else {
500 			sprintf(pwd_type, "%s", setting->pwd_type);
501 		}
502 
503 		ret = tlmi_opcode_setting("WmiOpcodePasswordType", pwd_type);
504 		if (ret)
505 			goto out;
506 
507 		/*
508 		 * Note admin password is not always required if SMPControl enabled in BIOS,
509 		 * So only set if it's configured.
510 		 * Let BIOS figure it out - we'll get an error if operation is not permitted
511 		 */
512 		if (tlmi_priv.pwd_admin->pwd_enabled && strlen(tlmi_priv.pwd_admin->password)) {
513 			ret = tlmi_opcode_setting("WmiOpcodePasswordAdmin",
514 					tlmi_priv.pwd_admin->password);
515 			if (ret)
516 				goto out;
517 		}
518 		ret = tlmi_opcode_setting("WmiOpcodePasswordCurrent01", setting->password);
519 		if (ret)
520 			goto out;
521 		ret = tlmi_opcode_setting("WmiOpcodePasswordNew01", new_pwd);
522 		if (ret)
523 			goto out;
524 		ret = tlmi_simple_call(LENOVO_OPCODE_IF_GUID, "WmiOpcodePasswordSetUpdate;");
525 	} else {
526 		/* Format: 'PasswordType,CurrentPw,NewPw,Encoding,KbdLang;' */
527 		auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s,%s,%s;",
528 				setting->pwd_type, setting->password, new_pwd,
529 				encoding_options[setting->encoding], setting->kbdlang);
530 		if (!auth_str) {
531 			ret = -ENOMEM;
532 			goto out;
533 		}
534 		ret = tlmi_simple_call(LENOVO_SET_BIOS_PASSWORD_GUID, auth_str);
535 		kfree(auth_str);
536 	}
537 out:
538 	mutex_unlock(&tlmi_mutex);
539 	kfree(new_pwd);
540 	return ret ?: count;
541 }
542 
543 static struct kobj_attribute auth_new_password = __ATTR_WO(new_password);
544 
545 static ssize_t min_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
546 			 char *buf)
547 {
548 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
549 
550 	return sysfs_emit(buf, "%d\n", setting->minlen);
551 }
552 
553 static struct kobj_attribute auth_min_pass_length = __ATTR_RO(min_password_length);
554 
555 static ssize_t max_password_length_show(struct kobject *kobj, struct kobj_attribute *attr,
556 			 char *buf)
557 {
558 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
559 
560 	return sysfs_emit(buf, "%d\n", setting->maxlen);
561 }
562 static struct kobj_attribute auth_max_pass_length = __ATTR_RO(max_password_length);
563 
564 static ssize_t mechanism_show(struct kobject *kobj, struct kobj_attribute *attr,
565 			 char *buf)
566 {
567 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
568 
569 	if (setting->cert_installed)
570 		return sysfs_emit(buf, "certificate\n");
571 	return sysfs_emit(buf, "password\n");
572 }
573 static struct kobj_attribute auth_mechanism = __ATTR_RO(mechanism);
574 
575 static ssize_t encoding_show(struct kobject *kobj, struct kobj_attribute *attr,
576 			 char *buf)
577 {
578 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
579 
580 	return sysfs_emit(buf, "%s\n", encoding_options[setting->encoding]);
581 }
582 
583 static ssize_t encoding_store(struct kobject *kobj,
584 				  struct kobj_attribute *attr,
585 				  const char *buf, size_t count)
586 {
587 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
588 	int i;
589 
590 	/* Scan for a matching profile */
591 	i = sysfs_match_string(encoding_options, buf);
592 	if (i < 0)
593 		return -EINVAL;
594 
595 	setting->encoding = i;
596 	return count;
597 }
598 
599 static struct kobj_attribute auth_encoding = __ATTR_RW(encoding);
600 
601 static ssize_t kbdlang_show(struct kobject *kobj, struct kobj_attribute *attr,
602 			 char *buf)
603 {
604 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
605 
606 	return sysfs_emit(buf, "%s\n", setting->kbdlang);
607 }
608 
609 static ssize_t kbdlang_store(struct kobject *kobj,
610 				  struct kobj_attribute *attr,
611 				  const char *buf, size_t count)
612 {
613 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
614 	int length;
615 
616 	/* Calculate length till '\n' or terminating 0 */
617 	length = strchrnul(buf, '\n') - buf;
618 	if (!length || length >= TLMI_LANG_MAXLEN)
619 		return -EINVAL;
620 
621 	memcpy(setting->kbdlang, buf, length);
622 	setting->kbdlang[length] = '\0';
623 	return count;
624 }
625 
626 static struct kobj_attribute auth_kbdlang = __ATTR_RW(kbdlang);
627 
628 static ssize_t role_show(struct kobject *kobj, struct kobj_attribute *attr,
629 			 char *buf)
630 {
631 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
632 
633 	return sysfs_emit(buf, "%s\n", setting->role);
634 }
635 static struct kobj_attribute auth_role = __ATTR_RO(role);
636 
637 static ssize_t index_show(struct kobject *kobj, struct kobj_attribute *attr,
638 			 char *buf)
639 {
640 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
641 
642 	return sysfs_emit(buf, "%d\n", setting->index);
643 }
644 
645 static ssize_t index_store(struct kobject *kobj,
646 				  struct kobj_attribute *attr,
647 				  const char *buf, size_t count)
648 {
649 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
650 	int err, val;
651 
652 	err = kstrtoint(buf, 10, &val);
653 	if (err < 0)
654 		return err;
655 
656 	if (val < 0 || val > TLMI_INDEX_MAX)
657 		return -EINVAL;
658 
659 	setting->index = val;
660 	return count;
661 }
662 
663 static struct kobj_attribute auth_index = __ATTR_RW(index);
664 
665 static ssize_t level_show(struct kobject *kobj, struct kobj_attribute *attr,
666 			 char *buf)
667 {
668 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
669 
670 	return sysfs_emit(buf, "%s\n", level_options[setting->level]);
671 }
672 
673 static ssize_t level_store(struct kobject *kobj,
674 				  struct kobj_attribute *attr,
675 				  const char *buf, size_t count)
676 {
677 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
678 	int i;
679 
680 	/* Scan for a matching profile */
681 	i = sysfs_match_string(level_options, buf);
682 	if (i < 0)
683 		return -EINVAL;
684 
685 	setting->level = i;
686 	return count;
687 }
688 
689 static struct kobj_attribute auth_level = __ATTR_RW(level);
690 
691 static char *cert_command(struct tlmi_pwd_setting *setting, const char *arg1, const char *arg2)
692 {
693 	/* Prepend with SVC or SMC if multicert supported */
694 	if (tlmi_priv.pwdcfg.core.password_mode >= TLMI_PWDCFG_MODE_MULTICERT)
695 		return kasprintf(GFP_KERNEL, "%s,%s,%s",
696 				 setting == tlmi_priv.pwd_admin ? "SVC" : "SMC",
697 				 arg1, arg2);
698 	else
699 		return kasprintf(GFP_KERNEL, "%s,%s", arg1, arg2);
700 }
701 
702 static ssize_t cert_thumbprint(char *buf, const char *arg, int count)
703 {
704 	const struct acpi_buffer input = { strlen(arg), (char *)arg };
705 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
706 	const union acpi_object *obj;
707 	acpi_status status;
708 
709 	if (!tlmi_priv.cert_guid->thumbprint)
710 		return -EOPNOTSUPP;
711 
712 	status = wmi_evaluate_method(tlmi_priv.cert_guid->thumbprint, 0, 0, &input, &output);
713 	if (ACPI_FAILURE(status)) {
714 		kfree(output.pointer);
715 		return -EIO;
716 	}
717 	obj = output.pointer;
718 	if (!obj)
719 		return -ENOMEM;
720 	if (obj->type != ACPI_TYPE_STRING || !obj->string.pointer) {
721 		kfree(output.pointer);
722 		return -EIO;
723 	}
724 	count += sysfs_emit_at(buf, count, "%s : %s\n", arg, (char *)obj->string.pointer);
725 	kfree(output.pointer);
726 
727 	return count;
728 }
729 
730 static char *thumbtypes[] = {"Md5", "Sha1", "Sha256"};
731 
732 static ssize_t certificate_thumbprint_show(struct kobject *kobj, struct kobj_attribute *attr,
733 			 char *buf)
734 {
735 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
736 	unsigned int i;
737 	int count = 0;
738 	char *wmistr;
739 
740 	if (!tlmi_priv.certificate_support || !setting->cert_installed)
741 		return -EOPNOTSUPP;
742 
743 	for (i = 0; i < ARRAY_SIZE(thumbtypes); i++) {
744 		if (tlmi_priv.pwdcfg.core.password_mode >= TLMI_PWDCFG_MODE_MULTICERT) {
745 			/* Format: 'SVC | SMC, Thumbtype' */
746 			wmistr = kasprintf(GFP_KERNEL, "%s,%s",
747 					   setting == tlmi_priv.pwd_admin ? "SVC" : "SMC",
748 					   thumbtypes[i]);
749 		} else {
750 			/* Format: 'Thumbtype' */
751 			wmistr = kasprintf(GFP_KERNEL, "%s", thumbtypes[i]);
752 		}
753 		if (!wmistr)
754 			return -ENOMEM;
755 		count += cert_thumbprint(buf, wmistr, count);
756 		kfree(wmistr);
757 	}
758 
759 	return count;
760 }
761 
762 static struct kobj_attribute auth_cert_thumb = __ATTR_RO(certificate_thumbprint);
763 
764 static ssize_t cert_to_password_store(struct kobject *kobj,
765 				  struct kobj_attribute *attr,
766 				  const char *buf, size_t count)
767 {
768 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
769 	char *auth_str, *passwd;
770 	int ret;
771 
772 	if (!capable(CAP_SYS_ADMIN))
773 		return -EPERM;
774 
775 	if (!tlmi_priv.certificate_support)
776 		return -EOPNOTSUPP;
777 
778 	if (!setting->cert_installed)
779 		return -EINVAL;
780 
781 	if (!setting->signature || !setting->signature[0])
782 		return -EACCES;
783 
784 	/* Strip out CR if one is present */
785 	passwd = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
786 	if (!passwd)
787 		return -ENOMEM;
788 
789 	/* Format: 'Password,Signature' */
790 	auth_str = cert_command(setting, passwd, setting->signature);
791 	if (!auth_str) {
792 		kfree_sensitive(passwd);
793 		return -ENOMEM;
794 	}
795 	ret = tlmi_simple_call(tlmi_priv.cert_guid->cert_to_password, auth_str);
796 	kfree(auth_str);
797 	kfree_sensitive(passwd);
798 
799 	return ret ?: count;
800 }
801 
802 static struct kobj_attribute auth_cert_to_password = __ATTR_WO(cert_to_password);
803 
804 enum cert_install_mode {
805 	TLMI_CERT_INSTALL,
806 	TLMI_CERT_UPDATE,
807 };
808 
809 static ssize_t certificate_store(struct kobject *kobj,
810 				  struct kobj_attribute *attr,
811 				  const char *buf, size_t count)
812 {
813 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
814 	enum cert_install_mode install_mode = TLMI_CERT_INSTALL;
815 	char *auth_str, *new_cert;
816 	const char *serial;
817 	char *signature;
818 	const char *guid;
819 	int ret;
820 
821 	if (!capable(CAP_SYS_ADMIN))
822 		return -EPERM;
823 
824 	if (!tlmi_priv.certificate_support)
825 		return -EOPNOTSUPP;
826 
827 	/* If empty then clear installed certificate */
828 	if ((buf[0] == '\0') || (buf[0] == '\n')) { /* Clear installed certificate */
829 		/* Check that signature is set */
830 		if (!setting->signature || !setting->signature[0])
831 			return -EACCES;
832 
833 		/* Format: 'serial#, signature' */
834 		serial = dmi_get_system_info(DMI_PRODUCT_SERIAL);
835 		if (!serial)
836 			return -ENODEV;
837 		auth_str = cert_command(setting, serial, setting->signature);
838 		if (!auth_str)
839 			return -ENOMEM;
840 
841 		ret = tlmi_simple_call(tlmi_priv.cert_guid->clear_bios_cert, auth_str);
842 		kfree(auth_str);
843 
844 		return ret ?: count;
845 	}
846 
847 	/* Strip out CR if one is present */
848 	new_cert = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
849 	if (!new_cert)
850 		return -ENOMEM;
851 
852 	if (setting->cert_installed) {
853 		/* Certificate is installed so this is an update */
854 		install_mode = TLMI_CERT_UPDATE;
855 		/* If admin account enabled - need to use its signature */
856 		if (tlmi_priv.pwd_admin->pwd_enabled)
857 			signature = tlmi_priv.pwd_admin->signature;
858 		else
859 			signature = setting->signature;
860 	} else { /* Cert install */
861 		/* Check if SMC and SVC already installed */
862 		if ((setting == tlmi_priv.pwd_system) && tlmi_priv.pwd_admin->cert_installed) {
863 			/* This gets treated as a cert update */
864 			install_mode = TLMI_CERT_UPDATE;
865 			signature = tlmi_priv.pwd_admin->signature;
866 		} else { /* Regular cert install */
867 			install_mode = TLMI_CERT_INSTALL;
868 			signature = setting->signature;
869 		}
870 	}
871 
872 	if (install_mode == TLMI_CERT_UPDATE) {
873 		/* This is a certificate update */
874 		if (!signature || !signature[0]) {
875 			kfree(new_cert);
876 			return -EACCES;
877 		}
878 		guid = tlmi_priv.cert_guid->update_bios_cert;
879 		/* Format: 'Certificate,Signature' */
880 		auth_str = cert_command(setting, new_cert, signature);
881 	} else {
882 		/* This is a fresh install */
883 		/* To set admin cert, a password must be enabled */
884 		if ((setting == tlmi_priv.pwd_admin) &&
885 		    (!setting->pwd_enabled || !setting->password[0])) {
886 			kfree(new_cert);
887 			return -EACCES;
888 		}
889 		guid = tlmi_priv.cert_guid->set_bios_cert;
890 		if (tlmi_priv.thinkcenter_mode) {
891 			/* Format: 'Certificate, password, encoding, kbdlang' */
892 			auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s,%s", new_cert,
893 					     setting->password,
894 					     encoding_options[setting->encoding],
895 					     setting->kbdlang);
896 		} else {
897 			/* Format: 'Certificate, password' */
898 			auth_str = cert_command(setting, new_cert, setting->password);
899 		}
900 	}
901 	kfree(new_cert);
902 	if (!auth_str)
903 		return -ENOMEM;
904 
905 	ret = tlmi_simple_call(guid, auth_str);
906 	kfree(auth_str);
907 
908 	return ret ?: count;
909 }
910 
911 static struct kobj_attribute auth_certificate = __ATTR_WO(certificate);
912 
913 static ssize_t signature_store(struct kobject *kobj,
914 				  struct kobj_attribute *attr,
915 				  const char *buf, size_t count)
916 {
917 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
918 	char *new_signature;
919 
920 	if (!capable(CAP_SYS_ADMIN))
921 		return -EPERM;
922 
923 	if (!tlmi_priv.certificate_support)
924 		return -EOPNOTSUPP;
925 
926 	/* Strip out CR if one is present */
927 	new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
928 	if (!new_signature)
929 		return -ENOMEM;
930 
931 	/* Free any previous signature */
932 	kfree(setting->signature);
933 	setting->signature = new_signature;
934 
935 	return count;
936 }
937 
938 static struct kobj_attribute auth_signature = __ATTR_WO(signature);
939 
940 static ssize_t save_signature_store(struct kobject *kobj,
941 				  struct kobj_attribute *attr,
942 				  const char *buf, size_t count)
943 {
944 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
945 	char *new_signature;
946 
947 	if (!capable(CAP_SYS_ADMIN))
948 		return -EPERM;
949 
950 	if (!tlmi_priv.certificate_support)
951 		return -EOPNOTSUPP;
952 
953 	/* Strip out CR if one is present */
954 	new_signature = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
955 	if (!new_signature)
956 		return -ENOMEM;
957 
958 	/* Free any previous signature */
959 	kfree(setting->save_signature);
960 	setting->save_signature = new_signature;
961 
962 	return count;
963 }
964 
965 static struct kobj_attribute auth_save_signature = __ATTR_WO(save_signature);
966 
967 static umode_t auth_attr_is_visible(struct kobject *kobj,
968 					     struct attribute *attr, int n)
969 {
970 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
971 
972 	/* We only want to display level and index settings on HDD/NVMe */
973 	if (attr == &auth_index.attr || attr == &auth_level.attr) {
974 		if ((setting == tlmi_priv.pwd_hdd) || (setting == tlmi_priv.pwd_nvme))
975 			return attr->mode;
976 		return 0;
977 	}
978 
979 	/* We only display certificates, if supported */
980 	if (attr == &auth_certificate.attr ||
981 	    attr == &auth_signature.attr ||
982 	    attr == &auth_save_signature.attr ||
983 	    attr == &auth_cert_thumb.attr ||
984 	    attr == &auth_cert_to_password.attr) {
985 		if (tlmi_priv.certificate_support) {
986 			if (setting == tlmi_priv.pwd_admin)
987 				return attr->mode;
988 			if ((tlmi_priv.pwdcfg.core.password_mode >= TLMI_PWDCFG_MODE_MULTICERT) &&
989 			    (setting == tlmi_priv.pwd_system))
990 				return attr->mode;
991 		}
992 		return 0;
993 	}
994 
995 	/* Don't display un-needed settings if opcode available */
996 	if ((attr == &auth_encoding.attr || attr == &auth_kbdlang.attr) &&
997 	    tlmi_priv.opcode_support)
998 		return 0;
999 
1000 	return attr->mode;
1001 }
1002 
1003 static struct attribute *auth_attrs[] = {
1004 	&auth_is_pass_set.attr,
1005 	&auth_min_pass_length.attr,
1006 	&auth_max_pass_length.attr,
1007 	&auth_current_password.attr,
1008 	&auth_new_password.attr,
1009 	&auth_role.attr,
1010 	&auth_mechanism.attr,
1011 	&auth_encoding.attr,
1012 	&auth_kbdlang.attr,
1013 	&auth_index.attr,
1014 	&auth_level.attr,
1015 	&auth_certificate.attr,
1016 	&auth_signature.attr,
1017 	&auth_save_signature.attr,
1018 	&auth_cert_thumb.attr,
1019 	&auth_cert_to_password.attr,
1020 	NULL
1021 };
1022 
1023 static const struct attribute_group auth_attr_group = {
1024 	.is_visible = auth_attr_is_visible,
1025 	.attrs = auth_attrs,
1026 };
1027 __ATTRIBUTE_GROUPS(auth_attr);
1028 
1029 /* ---- Attributes sysfs --------------------------------------------------------- */
1030 static ssize_t display_name_show(struct kobject *kobj, struct kobj_attribute *attr,
1031 		char *buf)
1032 {
1033 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1034 
1035 	return sysfs_emit(buf, "%s\n", setting->display_name);
1036 }
1037 
1038 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1039 {
1040 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1041 	char *item, *value;
1042 	int ret;
1043 
1044 	ret = tlmi_setting(setting->wdev, setting->index, &item);
1045 	if (ret)
1046 		return ret;
1047 
1048 	/* validate and split from `item,value` -> `value` */
1049 	value = strpbrk(item, ",");
1050 	if (!value || value == item || !strlen(value + 1))
1051 		ret = -EINVAL;
1052 	else {
1053 		/* On Workstations remove the Options part after the value */
1054 		strreplace(value, ';', '\0');
1055 		ret = sysfs_emit(buf, "%s\n", value + 1);
1056 	}
1057 	kfree(item);
1058 
1059 	return ret;
1060 }
1061 
1062 static ssize_t possible_values_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
1063 {
1064 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1065 
1066 	return sysfs_emit(buf, "%s\n", setting->possible_values);
1067 }
1068 
1069 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
1070 		char *buf)
1071 {
1072 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1073 
1074 	if (setting->possible_values) {
1075 		/* Figure out what setting type is as BIOS does not return this */
1076 		if (strchr(setting->possible_values, ';'))
1077 			return sysfs_emit(buf, "enumeration\n");
1078 	}
1079 	/* Anything else is going to be a string */
1080 	return sysfs_emit(buf, "string\n");
1081 }
1082 
1083 static ssize_t current_value_store(struct kobject *kobj,
1084 		struct kobj_attribute *attr,
1085 		const char *buf, size_t count)
1086 {
1087 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1088 	char *set_str = NULL, *new_setting = NULL;
1089 	char *auth_str = NULL;
1090 	int ret;
1091 
1092 	if (!tlmi_priv.can_set_bios_settings)
1093 		return -EOPNOTSUPP;
1094 
1095 	/*
1096 	 * If we are using bulk saves a reboot should be done once save has
1097 	 * been called
1098 	 */
1099 	if (tlmi_priv.save_mode == TLMI_SAVE_BULK && tlmi_priv.reboot_required)
1100 		return -EPERM;
1101 
1102 	/* Strip out CR if one is present */
1103 	new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
1104 	if (!new_setting)
1105 		return -ENOMEM;
1106 
1107 	/* Use lock in case multiple WMI operations needed */
1108 	mutex_lock(&tlmi_mutex);
1109 
1110 	/* Check if certificate authentication is enabled and active */
1111 	if (tlmi_priv.certificate_support && tlmi_priv.pwd_admin->cert_installed) {
1112 		if (!tlmi_priv.pwd_admin->signature || !tlmi_priv.pwd_admin->save_signature) {
1113 			ret = -EINVAL;
1114 			goto out;
1115 		}
1116 		set_str = kasprintf(GFP_KERNEL, "%s,%s,%s", setting->name,
1117 				    new_setting, tlmi_priv.pwd_admin->signature);
1118 		if (!set_str) {
1119 			ret = -ENOMEM;
1120 			goto out;
1121 		}
1122 
1123 		ret = tlmi_simple_call(tlmi_priv.cert_guid->set_bios_setting, set_str);
1124 		if (ret)
1125 			goto out;
1126 		if (tlmi_priv.save_mode == TLMI_SAVE_BULK)
1127 			tlmi_priv.save_required = true;
1128 		else
1129 			ret = tlmi_simple_call(tlmi_priv.cert_guid->save_bios_setting,
1130 					       tlmi_priv.pwd_admin->save_signature);
1131 	} else if (tlmi_priv.opcode_support) {
1132 		/*
1133 		 * If opcode support is present use that interface.
1134 		 * Note - this sets the variable and then the password as separate
1135 		 * WMI calls. Function tlmi_save_bios_settings will error if the
1136 		 * password is incorrect.
1137 		 * Workstation's require the opcode to be set before changing the
1138 		 * attribute.
1139 		 */
1140 		if (tlmi_priv.pwd_admin->pwd_enabled && tlmi_priv.pwd_admin->password[0]) {
1141 			ret = tlmi_opcode_setting("WmiOpcodePasswordAdmin",
1142 						  tlmi_priv.pwd_admin->password);
1143 			if (ret)
1144 				goto out;
1145 		}
1146 
1147 		set_str = kasprintf(GFP_KERNEL, "%s,%s;", setting->name,
1148 				    new_setting);
1149 		if (!set_str) {
1150 			ret = -ENOMEM;
1151 			goto out;
1152 		}
1153 
1154 		ret = tlmi_simple_call(LENOVO_SET_BIOS_SETTINGS_GUID, set_str);
1155 		if (ret)
1156 			goto out;
1157 
1158 		if (tlmi_priv.save_mode == TLMI_SAVE_BULK)
1159 			tlmi_priv.save_required = true;
1160 		else
1161 			ret = tlmi_save_bios_settings("");
1162 	} else { /* old non-opcode based authentication method (deprecated) */
1163 		if (tlmi_priv.pwd_admin->pwd_enabled && tlmi_priv.pwd_admin->password[0]) {
1164 			auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
1165 					tlmi_priv.pwd_admin->password,
1166 					encoding_options[tlmi_priv.pwd_admin->encoding],
1167 					tlmi_priv.pwd_admin->kbdlang);
1168 			if (!auth_str) {
1169 				ret = -ENOMEM;
1170 				goto out;
1171 			}
1172 		}
1173 
1174 		if (auth_str)
1175 			set_str = kasprintf(GFP_KERNEL, "%s,%s,%s", setting->name,
1176 					    new_setting, auth_str);
1177 		else
1178 			set_str = kasprintf(GFP_KERNEL, "%s,%s;", setting->name,
1179 					    new_setting);
1180 		if (!set_str) {
1181 			ret = -ENOMEM;
1182 			goto out;
1183 		}
1184 
1185 		ret = tlmi_simple_call(LENOVO_SET_BIOS_SETTINGS_GUID, set_str);
1186 		if (ret)
1187 			goto out;
1188 
1189 		if (tlmi_priv.save_mode == TLMI_SAVE_BULK) {
1190 			tlmi_priv.save_required = true;
1191 		} else {
1192 			if (auth_str)
1193 				ret = tlmi_save_bios_settings(auth_str);
1194 			else
1195 				ret = tlmi_save_bios_settings("");
1196 		}
1197 	}
1198 	if (!ret && !tlmi_priv.pending_changes) {
1199 		tlmi_priv.pending_changes = true;
1200 		/* let userland know it may need to check reboot pending again */
1201 		kobject_uevent(&tlmi_priv.class_dev->kobj, KOBJ_CHANGE);
1202 	}
1203 out:
1204 	mutex_unlock(&tlmi_mutex);
1205 	kfree(auth_str);
1206 	kfree(set_str);
1207 	kfree(new_setting);
1208 	return ret ?: count;
1209 }
1210 
1211 static struct kobj_attribute attr_displ_name = __ATTR_RO(display_name);
1212 
1213 static struct kobj_attribute attr_possible_values = __ATTR_RO(possible_values);
1214 
1215 static struct kobj_attribute attr_current_val = __ATTR_RW_MODE(current_value, 0600);
1216 
1217 static struct kobj_attribute attr_type = __ATTR_RO(type);
1218 
1219 static umode_t attr_is_visible(struct kobject *kobj,
1220 					     struct attribute *attr, int n)
1221 {
1222 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1223 
1224 	/* We don't want to display possible_values attributes if not available */
1225 	if ((attr == &attr_possible_values.attr) && (!setting->possible_values))
1226 		return 0;
1227 
1228 	return attr->mode;
1229 }
1230 
1231 static struct attribute *tlmi_attrs[] = {
1232 	&attr_displ_name.attr,
1233 	&attr_current_val.attr,
1234 	&attr_possible_values.attr,
1235 	&attr_type.attr,
1236 	NULL
1237 };
1238 
1239 static const struct attribute_group tlmi_attr_group = {
1240 	.is_visible = attr_is_visible,
1241 	.attrs = tlmi_attrs,
1242 };
1243 __ATTRIBUTE_GROUPS(tlmi_attr);
1244 
1245 static void tlmi_attr_setting_release(struct kobject *kobj)
1246 {
1247 	struct tlmi_attr_setting *setting = to_tlmi_attr_setting(kobj);
1248 
1249 	kfree(setting->possible_values);
1250 	kfree(setting);
1251 }
1252 
1253 static void tlmi_pwd_setting_release(struct kobject *kobj)
1254 {
1255 	struct tlmi_pwd_setting *setting = to_tlmi_pwd_setting(kobj);
1256 
1257 	kfree(setting);
1258 }
1259 
1260 static const struct kobj_type tlmi_attr_setting_ktype = {
1261 	.release        = &tlmi_attr_setting_release,
1262 	.sysfs_ops	= &kobj_sysfs_ops,
1263 	.default_groups = tlmi_attr_groups,
1264 };
1265 
1266 static const struct kobj_type tlmi_pwd_setting_ktype = {
1267 	.release        = &tlmi_pwd_setting_release,
1268 	.sysfs_ops	= &kobj_sysfs_ops,
1269 	.default_groups = auth_attr_groups,
1270 };
1271 
1272 static ssize_t pending_reboot_show(struct kobject *kobj, struct kobj_attribute *attr,
1273 				   char *buf)
1274 {
1275 	return sprintf(buf, "%d\n", tlmi_priv.pending_changes);
1276 }
1277 
1278 static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);
1279 
1280 static const char * const save_mode_strings[] = {
1281 	[TLMI_SAVE_SINGLE] = "single",
1282 	[TLMI_SAVE_BULK] = "bulk",
1283 	[TLMI_SAVE_SAVE] = "save"
1284 };
1285 
1286 static ssize_t save_settings_show(struct kobject *kobj, struct kobj_attribute *attr,
1287 				  char *buf)
1288 {
1289 	/* Check that setting is valid */
1290 	if (WARN_ON(tlmi_priv.save_mode < TLMI_SAVE_SINGLE ||
1291 		    tlmi_priv.save_mode > TLMI_SAVE_BULK))
1292 		return -EIO;
1293 	return sysfs_emit(buf, "%s\n", save_mode_strings[tlmi_priv.save_mode]);
1294 }
1295 
1296 static ssize_t save_settings_store(struct kobject *kobj, struct kobj_attribute *attr,
1297 				   const char *buf, size_t count)
1298 {
1299 	char *auth_str = NULL;
1300 	int ret = 0;
1301 	int cmd;
1302 
1303 	cmd = sysfs_match_string(save_mode_strings, buf);
1304 	if (cmd < 0)
1305 		return cmd;
1306 
1307 	/* Use lock in case multiple WMI operations needed */
1308 	mutex_lock(&tlmi_mutex);
1309 
1310 	switch (cmd) {
1311 	case TLMI_SAVE_SINGLE:
1312 	case TLMI_SAVE_BULK:
1313 		tlmi_priv.save_mode = cmd;
1314 		goto out;
1315 	case TLMI_SAVE_SAVE:
1316 		/* Check if supported*/
1317 		if (!tlmi_priv.can_set_bios_settings ||
1318 		    tlmi_priv.save_mode == TLMI_SAVE_SINGLE) {
1319 			ret = -EOPNOTSUPP;
1320 			goto out;
1321 		}
1322 		/* Check there is actually something to save */
1323 		if (!tlmi_priv.save_required) {
1324 			ret = -ENOENT;
1325 			goto out;
1326 		}
1327 		/* Check if certificate authentication is enabled and active */
1328 		if (tlmi_priv.certificate_support && tlmi_priv.pwd_admin->cert_installed) {
1329 			if (!tlmi_priv.pwd_admin->signature ||
1330 			    !tlmi_priv.pwd_admin->save_signature) {
1331 				ret = -EINVAL;
1332 				goto out;
1333 			}
1334 			ret = tlmi_simple_call(tlmi_priv.cert_guid->save_bios_setting,
1335 					       tlmi_priv.pwd_admin->save_signature);
1336 			if (ret)
1337 				goto out;
1338 		} else if (tlmi_priv.opcode_support) {
1339 			if (tlmi_priv.pwd_admin->pwd_enabled && tlmi_priv.pwd_admin->password[0]) {
1340 				ret = tlmi_opcode_setting("WmiOpcodePasswordAdmin",
1341 							  tlmi_priv.pwd_admin->password);
1342 				if (ret)
1343 					goto out;
1344 			}
1345 			ret = tlmi_save_bios_settings("");
1346 		} else { /* old non-opcode based authentication method (deprecated) */
1347 			if (tlmi_priv.pwd_admin->pwd_enabled && tlmi_priv.pwd_admin->password[0]) {
1348 				auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
1349 						     tlmi_priv.pwd_admin->password,
1350 						     encoding_options[tlmi_priv.pwd_admin->encoding],
1351 						     tlmi_priv.pwd_admin->kbdlang);
1352 				if (!auth_str) {
1353 					ret = -ENOMEM;
1354 					goto out;
1355 				}
1356 			}
1357 
1358 			if (auth_str)
1359 				ret = tlmi_save_bios_settings(auth_str);
1360 			else
1361 				ret = tlmi_save_bios_settings("");
1362 		}
1363 		tlmi_priv.save_required = false;
1364 		tlmi_priv.reboot_required = true;
1365 
1366 		if (!ret && !tlmi_priv.pending_changes) {
1367 			tlmi_priv.pending_changes = true;
1368 			/* let userland know it may need to check reboot pending again */
1369 			kobject_uevent(&tlmi_priv.class_dev->kobj, KOBJ_CHANGE);
1370 		}
1371 		break;
1372 	}
1373 out:
1374 	mutex_unlock(&tlmi_mutex);
1375 	kfree(auth_str);
1376 	return ret ?: count;
1377 }
1378 
1379 static struct kobj_attribute save_settings = __ATTR_RW(save_settings);
1380 
1381 /* ---- Debug interface--------------------------------------------------------- */
1382 static ssize_t debug_cmd_store(struct kobject *kobj, struct kobj_attribute *attr,
1383 				const char *buf, size_t count)
1384 {
1385 	char *set_str = NULL, *new_setting = NULL;
1386 	char *auth_str = NULL;
1387 	int ret;
1388 
1389 	if (!tlmi_priv.can_debug_cmd)
1390 		return -EOPNOTSUPP;
1391 
1392 	/* Strip out CR if one is present */
1393 	new_setting = kstrdup_and_replace(buf, '\n', '\0', GFP_KERNEL);
1394 	if (!new_setting)
1395 		return -ENOMEM;
1396 
1397 	if (tlmi_priv.pwd_admin->pwd_enabled && tlmi_priv.pwd_admin->password[0]) {
1398 		auth_str = kasprintf(GFP_KERNEL, "%s,%s,%s;",
1399 				tlmi_priv.pwd_admin->password,
1400 				encoding_options[tlmi_priv.pwd_admin->encoding],
1401 				tlmi_priv.pwd_admin->kbdlang);
1402 		if (!auth_str) {
1403 			ret = -ENOMEM;
1404 			goto out;
1405 		}
1406 	}
1407 
1408 	if (auth_str)
1409 		set_str = kasprintf(GFP_KERNEL, "%s,%s", new_setting, auth_str);
1410 	else
1411 		set_str = kasprintf(GFP_KERNEL, "%s;", new_setting);
1412 	if (!set_str) {
1413 		ret = -ENOMEM;
1414 		goto out;
1415 	}
1416 
1417 	ret = tlmi_simple_call(LENOVO_DEBUG_CMD_GUID, set_str);
1418 	if (ret)
1419 		goto out;
1420 
1421 	if (!ret && !tlmi_priv.pending_changes) {
1422 		tlmi_priv.pending_changes = true;
1423 		/* let userland know it may need to check reboot pending again */
1424 		kobject_uevent(&tlmi_priv.class_dev->kobj, KOBJ_CHANGE);
1425 	}
1426 out:
1427 	kfree(auth_str);
1428 	kfree(set_str);
1429 	kfree(new_setting);
1430 	return ret ?: count;
1431 }
1432 
1433 static struct kobj_attribute debug_cmd = __ATTR_WO(debug_cmd);
1434 
1435 /* ---- Initialisation --------------------------------------------------------- */
1436 static void tlmi_release_attr(void)
1437 {
1438 	struct kobject *pos, *n;
1439 
1440 	/* Attribute structures */
1441 	sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
1442 	sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &save_settings.attr);
1443 
1444 	if (tlmi_priv.can_debug_cmd && debug_support)
1445 		sysfs_remove_file(&tlmi_priv.attribute_kset->kobj, &debug_cmd.attr);
1446 
1447 	list_for_each_entry_safe(pos, n, &tlmi_priv.attribute_kset->list, entry)
1448 		kobject_put(pos);
1449 
1450 	kset_unregister(tlmi_priv.attribute_kset);
1451 
1452 	/* Free up any saved signatures */
1453 	kfree(tlmi_priv.pwd_admin->signature);
1454 	kfree(tlmi_priv.pwd_admin->save_signature);
1455 
1456 	/* Authentication structures */
1457 	list_for_each_entry_safe(pos, n, &tlmi_priv.authentication_kset->list, entry)
1458 		kobject_put(pos);
1459 
1460 	kset_unregister(tlmi_priv.authentication_kset);
1461 }
1462 
1463 static int tlmi_validate_setting_name(struct kset *attribute_kset, char *name)
1464 {
1465 	struct kobject *duplicate;
1466 
1467 	if (!strcmp(name, "Reserved"))
1468 		return -EINVAL;
1469 
1470 	duplicate = kset_find_obj(attribute_kset, name);
1471 	if (duplicate) {
1472 		pr_debug("Duplicate attribute name found - %s\n", name);
1473 		/* kset_find_obj() returns a reference */
1474 		kobject_put(duplicate);
1475 		return -EBUSY;
1476 	}
1477 
1478 	return 0;
1479 }
1480 
1481 static int tlmi_sysfs_init(void)
1482 {
1483 	int i, ret;
1484 
1485 	tlmi_priv.class_dev = device_create(&firmware_attributes_class, NULL, MKDEV(0, 0),
1486 			NULL, "%s", "thinklmi");
1487 	if (IS_ERR(tlmi_priv.class_dev)) {
1488 		ret = PTR_ERR(tlmi_priv.class_dev);
1489 		goto fail_class_created;
1490 	}
1491 
1492 	tlmi_priv.attribute_kset = kset_create_and_add("attributes", NULL,
1493 			&tlmi_priv.class_dev->kobj);
1494 	if (!tlmi_priv.attribute_kset) {
1495 		ret = -ENOMEM;
1496 		goto fail_device_created;
1497 	}
1498 
1499 	tlmi_priv.authentication_kset = kset_create_and_add("authentication", NULL,
1500 							    &tlmi_priv.class_dev->kobj);
1501 	if (!tlmi_priv.authentication_kset) {
1502 		kset_unregister(tlmi_priv.attribute_kset);
1503 		ret = -ENOMEM;
1504 		goto fail_device_created;
1505 	}
1506 
1507 	for (i = 0; i < TLMI_SETTINGS_COUNT; i++) {
1508 		/* Check if index is a valid setting - skip if it isn't */
1509 		if (!tlmi_priv.setting[i])
1510 			continue;
1511 
1512 		/* check for duplicate or reserved values */
1513 		if (tlmi_validate_setting_name(tlmi_priv.attribute_kset,
1514 					       tlmi_priv.setting[i]->display_name) < 0) {
1515 			kfree(tlmi_priv.setting[i]->possible_values);
1516 			kfree(tlmi_priv.setting[i]);
1517 			tlmi_priv.setting[i] = NULL;
1518 			continue;
1519 		}
1520 
1521 		/* Build attribute */
1522 		tlmi_priv.setting[i]->kobj.kset = tlmi_priv.attribute_kset;
1523 		ret = kobject_init_and_add(&tlmi_priv.setting[i]->kobj, &tlmi_attr_setting_ktype,
1524 					   NULL, "%s", tlmi_priv.setting[i]->display_name);
1525 		if (ret)
1526 			goto fail_create_attr;
1527 	}
1528 
1529 	ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &pending_reboot.attr);
1530 	if (ret)
1531 		goto fail_create_attr;
1532 
1533 	ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &save_settings.attr);
1534 	if (ret)
1535 		goto fail_create_attr;
1536 
1537 	if (tlmi_priv.can_debug_cmd && debug_support) {
1538 		ret = sysfs_create_file(&tlmi_priv.attribute_kset->kobj, &debug_cmd.attr);
1539 		if (ret)
1540 			goto fail_create_attr;
1541 	}
1542 
1543 	/* Create authentication entries */
1544 	tlmi_priv.pwd_admin->kobj.kset = tlmi_priv.authentication_kset;
1545 	ret = kobject_init_and_add(&tlmi_priv.pwd_admin->kobj, &tlmi_pwd_setting_ktype,
1546 				   NULL, "%s", "Admin");
1547 	if (ret)
1548 		goto fail_create_attr;
1549 
1550 	tlmi_priv.pwd_power->kobj.kset = tlmi_priv.authentication_kset;
1551 	ret = kobject_init_and_add(&tlmi_priv.pwd_power->kobj, &tlmi_pwd_setting_ktype,
1552 				   NULL, "%s", "Power-on");
1553 	if (ret)
1554 		goto fail_create_attr;
1555 
1556 	if (tlmi_priv.opcode_support) {
1557 		tlmi_priv.pwd_system->kobj.kset = tlmi_priv.authentication_kset;
1558 		ret = kobject_init_and_add(&tlmi_priv.pwd_system->kobj, &tlmi_pwd_setting_ktype,
1559 					   NULL, "%s", "System");
1560 		if (ret)
1561 			goto fail_create_attr;
1562 
1563 		tlmi_priv.pwd_hdd->kobj.kset = tlmi_priv.authentication_kset;
1564 		ret = kobject_init_and_add(&tlmi_priv.pwd_hdd->kobj, &tlmi_pwd_setting_ktype,
1565 					   NULL, "%s", "HDD");
1566 		if (ret)
1567 			goto fail_create_attr;
1568 
1569 		tlmi_priv.pwd_nvme->kobj.kset = tlmi_priv.authentication_kset;
1570 		ret = kobject_init_and_add(&tlmi_priv.pwd_nvme->kobj, &tlmi_pwd_setting_ktype,
1571 					   NULL, "%s", "NVMe");
1572 		if (ret)
1573 			goto fail_create_attr;
1574 	}
1575 
1576 	return ret;
1577 
1578 fail_create_attr:
1579 	tlmi_release_attr();
1580 fail_device_created:
1581 	device_unregister(tlmi_priv.class_dev);
1582 fail_class_created:
1583 	return ret;
1584 }
1585 
1586 /* ---- Base Driver -------------------------------------------------------- */
1587 static struct tlmi_pwd_setting *tlmi_create_auth(const char *pwd_type,
1588 			    const char *pwd_role)
1589 {
1590 	struct tlmi_pwd_setting *new_pwd;
1591 
1592 	new_pwd = kzalloc(sizeof(struct tlmi_pwd_setting), GFP_KERNEL);
1593 	if (!new_pwd)
1594 		return NULL;
1595 
1596 	strscpy(new_pwd->kbdlang, "us");
1597 	new_pwd->encoding = TLMI_ENCODING_ASCII;
1598 	new_pwd->pwd_type = pwd_type;
1599 	new_pwd->role = pwd_role;
1600 	new_pwd->minlen = tlmi_priv.pwdcfg.core.min_length;
1601 	new_pwd->maxlen = tlmi_priv.pwdcfg.core.max_length;
1602 	new_pwd->index = 0;
1603 
1604 	return new_pwd;
1605 }
1606 
1607 static int tlmi_analyze(struct wmi_device *wdev)
1608 {
1609 	int i, ret;
1610 
1611 	if (wmi_has_guid(LENOVO_SET_BIOS_SETTINGS_GUID) &&
1612 	    wmi_has_guid(LENOVO_SAVE_BIOS_SETTINGS_GUID))
1613 		tlmi_priv.can_set_bios_settings = true;
1614 
1615 	if (wmi_has_guid(LENOVO_GET_BIOS_SELECTIONS_GUID))
1616 		tlmi_priv.can_get_bios_selections = true;
1617 
1618 	if (wmi_has_guid(LENOVO_SET_BIOS_PASSWORD_GUID))
1619 		tlmi_priv.can_set_bios_password = true;
1620 
1621 	if (wmi_has_guid(LENOVO_BIOS_PASSWORD_SETTINGS_GUID))
1622 		tlmi_priv.can_get_password_settings = true;
1623 
1624 	if (wmi_has_guid(LENOVO_DEBUG_CMD_GUID))
1625 		tlmi_priv.can_debug_cmd = true;
1626 
1627 	if (wmi_has_guid(LENOVO_OPCODE_IF_GUID))
1628 		tlmi_priv.opcode_support = true;
1629 
1630 	if (wmi_has_guid(LENOVO_SET_BIOS_CERT_GUID) &&
1631 		wmi_has_guid(LENOVO_SET_BIOS_SETTING_CERT_GUID) &&
1632 		wmi_has_guid(LENOVO_SAVE_BIOS_SETTING_CERT_GUID))
1633 		tlmi_priv.certificate_support = true;
1634 
1635 	/* ThinkCenter uses different GUIDs for certificate support */
1636 	if (wmi_has_guid(LENOVO_TC_SET_BIOS_CERT_GUID) &&
1637 	    wmi_has_guid(LENOVO_TC_SET_BIOS_SETTING_CERT_GUID) &&
1638 	    wmi_has_guid(LENOVO_TC_SAVE_BIOS_SETTING_CERT_GUID)) {
1639 		tlmi_priv.certificate_support = true;
1640 		tlmi_priv.thinkcenter_mode = true;
1641 		pr_info("ThinkCenter modified support being used\n");
1642 	}
1643 
1644 	/*
1645 	 * Try to find the number of valid settings of this machine
1646 	 * and use it to create sysfs attributes.
1647 	 */
1648 	for (i = 0; i < TLMI_SETTINGS_COUNT; ++i) {
1649 		struct tlmi_attr_setting *setting;
1650 		char *item = NULL;
1651 
1652 		tlmi_priv.setting[i] = NULL;
1653 		ret = tlmi_setting(wdev, i, &item);
1654 		if (ret)
1655 			break;
1656 		if (!item)
1657 			break;
1658 		if (!*item) {
1659 			kfree(item);
1660 			continue;
1661 		}
1662 
1663 		/* Remove the value part */
1664 		strreplace(item, ',', '\0');
1665 
1666 		/* Create a setting entry */
1667 		setting = kzalloc(sizeof(*setting), GFP_KERNEL);
1668 		if (!setting) {
1669 			ret = -ENOMEM;
1670 			kfree(item);
1671 			goto fail_clear_attr;
1672 		}
1673 		setting->wdev = wdev;
1674 		setting->index = i;
1675 
1676 		strscpy(setting->name, item);
1677 		/* It is not allowed to have '/' for file name. Convert it into '\'. */
1678 		strreplace(item, '/', '\\');
1679 		strscpy(setting->display_name, item);
1680 
1681 		/* If BIOS selections supported, load those */
1682 		if (tlmi_priv.can_get_bios_selections) {
1683 			ret = tlmi_get_bios_selections(setting->name,
1684 						       &setting->possible_values);
1685 			if (ret || !setting->possible_values)
1686 				pr_info("Error retrieving possible values for %d : %s\n",
1687 						i, setting->display_name);
1688 		} else {
1689 			/*
1690 			 * Older Thinkstations don't support the bios_selections API.
1691 			 * Instead they store this as a [Optional:Option1,Option2] section of the
1692 			 * name string.
1693 			 * Try and pull that out if it's available.
1694 			 */
1695 			char *optitem, *optstart, *optend;
1696 
1697 			if (!tlmi_setting(setting->wdev, setting->index, &optitem)) {
1698 				optstart = strstr(optitem, "[Optional:");
1699 				if (optstart) {
1700 					optstart += strlen("[Optional:");
1701 					optend = strstr(optstart, "]");
1702 					if (optend)
1703 						setting->possible_values =
1704 							kstrndup(optstart, optend - optstart,
1705 									GFP_KERNEL);
1706 				}
1707 				kfree(optitem);
1708 			}
1709 		}
1710 		/*
1711 		 * firmware-attributes requires that possible_values are separated by ';' but
1712 		 * Lenovo FW uses ','. Replace appropriately.
1713 		 */
1714 		if (setting->possible_values)
1715 			strreplace(setting->possible_values, ',', ';');
1716 
1717 		tlmi_priv.setting[i] = setting;
1718 		kfree(item);
1719 	}
1720 
1721 	/* Create password setting structure */
1722 	ret = tlmi_get_pwd_settings(&tlmi_priv.pwdcfg);
1723 	if (ret)
1724 		goto fail_clear_attr;
1725 
1726 	/* All failures below boil down to kmalloc failures */
1727 	ret = -ENOMEM;
1728 
1729 	tlmi_priv.pwd_admin = tlmi_create_auth("pap", "bios-admin");
1730 	if (!tlmi_priv.pwd_admin)
1731 		goto fail_clear_attr;
1732 
1733 	if (tlmi_priv.pwdcfg.core.password_state & TLMI_PAP_PWD)
1734 		tlmi_priv.pwd_admin->pwd_enabled = true;
1735 
1736 	tlmi_priv.pwd_power = tlmi_create_auth("pop", "power-on");
1737 	if (!tlmi_priv.pwd_power)
1738 		goto fail_clear_attr;
1739 
1740 	if (tlmi_priv.pwdcfg.core.password_state & TLMI_POP_PWD)
1741 		tlmi_priv.pwd_power->pwd_enabled = true;
1742 
1743 	if (tlmi_priv.opcode_support) {
1744 		tlmi_priv.pwd_system = tlmi_create_auth("smp", "system");
1745 		if (!tlmi_priv.pwd_system)
1746 			goto fail_clear_attr;
1747 
1748 		if (tlmi_priv.pwdcfg.core.password_state & TLMI_SMP_PWD)
1749 			tlmi_priv.pwd_system->pwd_enabled = true;
1750 
1751 		tlmi_priv.pwd_hdd = tlmi_create_auth("hdd", "hdd");
1752 		if (!tlmi_priv.pwd_hdd)
1753 			goto fail_clear_attr;
1754 
1755 		tlmi_priv.pwd_nvme = tlmi_create_auth("nvm", "nvme");
1756 		if (!tlmi_priv.pwd_nvme)
1757 			goto fail_clear_attr;
1758 
1759 		/* Set default hdd/nvme index to 1 as there is no device 0 */
1760 		tlmi_priv.pwd_hdd->index = 1;
1761 		tlmi_priv.pwd_nvme->index = 1;
1762 
1763 		if (tlmi_priv.pwdcfg.core.password_state & TLMI_HDD_PWD) {
1764 			/* Check if PWD is configured and set index to first drive found */
1765 			if (tlmi_priv.pwdcfg.ext.hdd_user_password ||
1766 					tlmi_priv.pwdcfg.ext.hdd_master_password) {
1767 				tlmi_priv.pwd_hdd->pwd_enabled = true;
1768 				if (tlmi_priv.pwdcfg.ext.hdd_master_password)
1769 					tlmi_priv.pwd_hdd->index =
1770 						ffs(tlmi_priv.pwdcfg.ext.hdd_master_password) - 1;
1771 				else
1772 					tlmi_priv.pwd_hdd->index =
1773 						ffs(tlmi_priv.pwdcfg.ext.hdd_user_password) - 1;
1774 			}
1775 			if (tlmi_priv.pwdcfg.ext.nvme_user_password ||
1776 					tlmi_priv.pwdcfg.ext.nvme_master_password) {
1777 				tlmi_priv.pwd_nvme->pwd_enabled = true;
1778 				if (tlmi_priv.pwdcfg.ext.nvme_master_password)
1779 					tlmi_priv.pwd_nvme->index =
1780 						ffs(tlmi_priv.pwdcfg.ext.nvme_master_password) - 1;
1781 				else
1782 					tlmi_priv.pwd_nvme->index =
1783 						ffs(tlmi_priv.pwdcfg.ext.nvme_user_password) - 1;
1784 			}
1785 		}
1786 	}
1787 
1788 	if (tlmi_priv.certificate_support) {
1789 		if (tlmi_priv.thinkcenter_mode) {
1790 			tlmi_priv.cert_guid = &thinkcenter_cert_guid;
1791 			tlmi_priv.pwd_admin->cert_installed = tlmi_priv.pwdcfg.core.password_mode;
1792 		} else {
1793 			tlmi_priv.cert_guid = &thinkpad_cert_guid;
1794 			tlmi_priv.pwd_admin->cert_installed =
1795 				tlmi_priv.pwdcfg.core.password_state & TLMI_CERT_SVC;
1796 			tlmi_priv.pwd_system->cert_installed =
1797 				tlmi_priv.pwdcfg.core.password_state & TLMI_CERT_SMC;
1798 		}
1799 	}
1800 	return 0;
1801 
1802 fail_clear_attr:
1803 	for (i = 0; i < TLMI_SETTINGS_COUNT; ++i) {
1804 		if (tlmi_priv.setting[i]) {
1805 			kfree(tlmi_priv.setting[i]->possible_values);
1806 			kfree(tlmi_priv.setting[i]);
1807 		}
1808 	}
1809 	kfree(tlmi_priv.pwd_admin);
1810 	kfree(tlmi_priv.pwd_power);
1811 	kfree(tlmi_priv.pwd_system);
1812 	kfree(tlmi_priv.pwd_hdd);
1813 	kfree(tlmi_priv.pwd_nvme);
1814 	return ret;
1815 }
1816 
1817 static void tlmi_remove(struct wmi_device *wdev)
1818 {
1819 	tlmi_release_attr();
1820 	device_unregister(tlmi_priv.class_dev);
1821 }
1822 
1823 static int tlmi_probe(struct wmi_device *wdev, const void *context)
1824 {
1825 	int ret;
1826 
1827 	ret = tlmi_analyze(wdev);
1828 	if (ret)
1829 		return ret;
1830 
1831 	return tlmi_sysfs_init();
1832 }
1833 
1834 static const struct wmi_device_id tlmi_id_table[] = {
1835 	{ .guid_string = LENOVO_BIOS_SETTING_GUID },
1836 	{ }
1837 };
1838 MODULE_DEVICE_TABLE(wmi, tlmi_id_table);
1839 
1840 static struct wmi_driver tlmi_driver = {
1841 	.driver = {
1842 		.name = "think-lmi",
1843 	},
1844 	.id_table = tlmi_id_table,
1845 	.probe = tlmi_probe,
1846 	.remove = tlmi_remove,
1847 };
1848 
1849 MODULE_AUTHOR("Sugumaran L <slacshiminar@lenovo.com>");
1850 MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>");
1851 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
1852 MODULE_DESCRIPTION("ThinkLMI Driver");
1853 MODULE_LICENSE("GPL");
1854 
1855 module_wmi_driver(tlmi_driver);
1856