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