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