1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Common methods for use with dell-wmi-sysman
4  *
5  *  Copyright (c) 2020 Dell Inc.
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include <linux/fs.h>
11 #include <linux/dmi.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/wmi.h>
15 #include "dell-wmi-sysman.h"
16 #include "../../firmware_attributes_class.h"
17 
18 #define MAX_TYPES  4
19 #include <linux/nls.h>
20 
21 struct wmi_sysman_priv wmi_priv = {
22 	.mutex = __MUTEX_INITIALIZER(wmi_priv.mutex),
23 };
24 
25 /* reset bios to defaults */
26 static const char * const reset_types[] = {"builtinsafe", "lastknowngood", "factory", "custom"};
27 static int reset_option = -1;
28 
29 
30 /**
31  * populate_string_buffer() - populates a string buffer
32  * @buffer: the start of the destination buffer
33  * @buffer_len: length of the destination buffer
34  * @str: the string to insert into buffer
35  */
populate_string_buffer(char * buffer,size_t buffer_len,const char * str)36 ssize_t populate_string_buffer(char *buffer, size_t buffer_len, const char *str)
37 {
38 	u16 *length = (u16 *)buffer;
39 	u16 *target = length + 1;
40 	int ret;
41 
42 	ret = utf8s_to_utf16s(str, strlen(str), UTF16_HOST_ENDIAN,
43 			      target, buffer_len - sizeof(u16));
44 	if (ret < 0) {
45 		dev_err(wmi_priv.class_dev, "UTF16 conversion failed\n");
46 		return ret;
47 	}
48 
49 	if ((ret * sizeof(u16)) > U16_MAX) {
50 		dev_err(wmi_priv.class_dev, "Error string too long\n");
51 		return -ERANGE;
52 	}
53 
54 	*length = ret * sizeof(u16);
55 	return sizeof(u16) + *length;
56 }
57 
58 /**
59  * calculate_string_buffer() - determines size of string buffer for use with BIOS communication
60  * @str: the string to calculate based upon
61  *
62  */
calculate_string_buffer(const char * str)63 size_t calculate_string_buffer(const char *str)
64 {
65 	/* u16 length field + one UTF16 char for each input char */
66 	return sizeof(u16) + strlen(str) * sizeof(u16);
67 }
68 
69 /**
70  * calculate_security_buffer() - determines size of security buffer for authentication scheme
71  * @authentication: the authentication content
72  *
73  * Currently only supported type is Admin password
74  */
calculate_security_buffer(char * authentication)75 size_t calculate_security_buffer(char *authentication)
76 {
77 	if (strlen(authentication) > 0) {
78 		return (sizeof(u32) * 2) + strlen(authentication) +
79 			strlen(authentication) % 2;
80 	}
81 	return sizeof(u32) * 2;
82 }
83 
84 /**
85  * populate_security_buffer() - builds a security buffer for authentication scheme
86  * @buffer: the buffer to populate
87  * @authentication: the authentication content
88  *
89  * Currently only supported type is PLAIN TEXT
90  */
populate_security_buffer(char * buffer,char * authentication)91 void populate_security_buffer(char *buffer, char *authentication)
92 {
93 	char *auth = buffer + sizeof(u32) * 2;
94 	u32 *sectype = (u32 *) buffer;
95 	u32 *seclen = sectype + 1;
96 
97 	*sectype = strlen(authentication) > 0 ? 1 : 0;
98 	*seclen = strlen(authentication);
99 
100 	/* plain text */
101 	if (strlen(authentication) > 0)
102 		memcpy(auth, authentication, *seclen);
103 }
104 
105 /**
106  * map_wmi_error() - map errors from WMI methods to kernel error codes
107  * @error_code: integer error code returned from Dell's firmware
108  */
map_wmi_error(int error_code)109 int map_wmi_error(int error_code)
110 {
111 	switch (error_code) {
112 	case 0:
113 		/* success */
114 		return 0;
115 	case 1:
116 		/* failed */
117 		return -EIO;
118 	case 2:
119 		/* invalid parameter */
120 		return -EINVAL;
121 	case 3:
122 		/* access denied */
123 		return -EACCES;
124 	case 4:
125 		/* not supported */
126 		return -EOPNOTSUPP;
127 	case 5:
128 		/* memory error */
129 		return -ENOMEM;
130 	case 6:
131 		/* protocol error */
132 		return -EPROTO;
133 	}
134 	/* unspecified error */
135 	return -EIO;
136 }
137 
138 /**
139  * reset_bios_show() - sysfs implementaton for read reset_bios
140  * @kobj: Kernel object for this attribute
141  * @attr: Kernel object attribute
142  * @buf: The buffer to display to userspace
143  */
reset_bios_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)144 static ssize_t reset_bios_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
145 {
146 	char *start = buf;
147 	int i;
148 
149 	for (i = 0; i < MAX_TYPES; i++) {
150 		if (i == reset_option)
151 			buf += sprintf(buf, "[%s] ", reset_types[i]);
152 		else
153 			buf += sprintf(buf, "%s ", reset_types[i]);
154 	}
155 	buf += sprintf(buf, "\n");
156 	return buf-start;
157 }
158 
159 /**
160  * reset_bios_store() - sysfs implementaton for write reset_bios
161  * @kobj: Kernel object for this attribute
162  * @attr: Kernel object attribute
163  * @buf: The buffer from userspace
164  * @count: the size of the buffer from userspace
165  */
reset_bios_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)166 static ssize_t reset_bios_store(struct kobject *kobj,
167 				struct kobj_attribute *attr, const char *buf, size_t count)
168 {
169 	int type = sysfs_match_string(reset_types, buf);
170 	int ret;
171 
172 	if (type < 0)
173 		return type;
174 
175 	ret = set_bios_defaults(type);
176 	pr_debug("reset all attributes request type %d: %d\n", type, ret);
177 	if (!ret) {
178 		reset_option = type;
179 		ret = count;
180 	}
181 
182 	return ret;
183 }
184 
185 /**
186  * pending_reboot_show() - sysfs implementaton for read pending_reboot
187  * @kobj: Kernel object for this attribute
188  * @attr: Kernel object attribute
189  * @buf: The buffer to display to userspace
190  *
191  * Stores default value as 0
192  * When current_value is changed this attribute is set to 1 to notify reboot may be required
193  */
pending_reboot_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)194 static ssize_t pending_reboot_show(struct kobject *kobj, struct kobj_attribute *attr,
195 				   char *buf)
196 {
197 	return sprintf(buf, "%d\n", wmi_priv.pending_changes);
198 }
199 
200 static struct kobj_attribute reset_bios = __ATTR_RW(reset_bios);
201 static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);
202 
203 
204 /**
205  * create_attributes_level_sysfs_files() - Creates reset_bios and
206  * pending_reboot attributes
207  */
create_attributes_level_sysfs_files(void)208 static int create_attributes_level_sysfs_files(void)
209 {
210 	int ret;
211 
212 	ret = sysfs_create_file(&wmi_priv.main_dir_kset->kobj, &reset_bios.attr);
213 	if (ret)
214 		return ret;
215 
216 	ret = sysfs_create_file(&wmi_priv.main_dir_kset->kobj, &pending_reboot.attr);
217 	if (ret)
218 		return ret;
219 
220 	return 0;
221 }
222 
wmi_sysman_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)223 static ssize_t wmi_sysman_attr_show(struct kobject *kobj, struct attribute *attr,
224 				    char *buf)
225 {
226 	struct kobj_attribute *kattr;
227 	ssize_t ret = -EIO;
228 
229 	kattr = container_of(attr, struct kobj_attribute, attr);
230 	if (kattr->show)
231 		ret = kattr->show(kobj, kattr, buf);
232 	return ret;
233 }
234 
wmi_sysman_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)235 static ssize_t wmi_sysman_attr_store(struct kobject *kobj, struct attribute *attr,
236 				     const char *buf, size_t count)
237 {
238 	struct kobj_attribute *kattr;
239 	ssize_t ret = -EIO;
240 
241 	kattr = container_of(attr, struct kobj_attribute, attr);
242 	if (kattr->store)
243 		ret = kattr->store(kobj, kattr, buf, count);
244 	return ret;
245 }
246 
247 static const struct sysfs_ops wmi_sysman_kobj_sysfs_ops = {
248 	.show	= wmi_sysman_attr_show,
249 	.store	= wmi_sysman_attr_store,
250 };
251 
attr_name_release(struct kobject * kobj)252 static void attr_name_release(struct kobject *kobj)
253 {
254 	kfree(kobj);
255 }
256 
257 static const struct kobj_type attr_name_ktype = {
258 	.release	= attr_name_release,
259 	.sysfs_ops	= &wmi_sysman_kobj_sysfs_ops,
260 };
261 
262 /**
263  * strlcpy_attr - Copy a length-limited, NULL-terminated string with bound checks
264  * @dest: Where to copy the string to
265  * @src: Where to copy the string from
266  */
strlcpy_attr(char * dest,char * src)267 void strlcpy_attr(char *dest, char *src)
268 {
269 	size_t len = strlen(src) + 1;
270 
271 	if (len > 1 && len <= MAX_BUFF)
272 		strscpy(dest, src, len);
273 
274 	/*len can be zero because any property not-applicable to attribute can
275 	 * be empty so check only for too long buffers and log error
276 	 */
277 	if (len > MAX_BUFF)
278 		pr_err("Source string returned from BIOS is out of bound!\n");
279 }
280 
281 /**
282  * get_wmiobj_pointer() - Get Content of WMI block for particular instance
283  * @instance_id: WMI instance ID
284  * @guid_string: WMI GUID (in str form)
285  *
286  * Fetches the content for WMI block (instance_id) under GUID (guid_string)
287  * Caller must kfree the return
288  */
get_wmiobj_pointer(int instance_id,const char * guid_string)289 union acpi_object *get_wmiobj_pointer(int instance_id, const char *guid_string)
290 {
291 	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
292 	acpi_status status;
293 
294 	status = wmi_query_block(guid_string, instance_id, &out);
295 
296 	return ACPI_SUCCESS(status) ? (union acpi_object *)out.pointer : NULL;
297 }
298 
299 /**
300  * get_instance_count() - Compute total number of instances under guid_string
301  * @guid_string: WMI GUID (in string form)
302  */
get_instance_count(const char * guid_string)303 int get_instance_count(const char *guid_string)
304 {
305 	int ret;
306 
307 	ret = wmi_instance_count(guid_string);
308 	if (ret < 0)
309 		return 0;
310 
311 	return ret;
312 }
313 
314 /**
315  * alloc_attributes_data() - Allocate attributes data for a particular type
316  * @attr_type: Attribute type to allocate
317  */
alloc_attributes_data(int attr_type)318 static int alloc_attributes_data(int attr_type)
319 {
320 	int retval = 0;
321 
322 	switch (attr_type) {
323 	case ENUM:
324 		retval = alloc_enum_data();
325 		break;
326 	case INT:
327 		retval = alloc_int_data();
328 		break;
329 	case STR:
330 		retval = alloc_str_data();
331 		break;
332 	case PO:
333 		retval = alloc_po_data();
334 		break;
335 	default:
336 		break;
337 	}
338 
339 	return retval;
340 }
341 
342 /**
343  * destroy_attribute_objs() - Free a kset of kobjects
344  * @kset: The kset to destroy
345  *
346  * Fress kobjects created for each attribute_name under attribute type kset
347  */
destroy_attribute_objs(struct kset * kset)348 static void destroy_attribute_objs(struct kset *kset)
349 {
350 	struct kobject *pos, *next;
351 
352 	list_for_each_entry_safe(pos, next, &kset->list, entry) {
353 		kobject_put(pos);
354 	}
355 }
356 
357 /**
358  * release_attributes_data() - Clean-up all sysfs directories and files created
359  */
release_attributes_data(void)360 static void release_attributes_data(void)
361 {
362 	mutex_lock(&wmi_priv.mutex);
363 	exit_enum_attributes();
364 	exit_int_attributes();
365 	exit_str_attributes();
366 	exit_po_attributes();
367 	if (wmi_priv.authentication_dir_kset) {
368 		destroy_attribute_objs(wmi_priv.authentication_dir_kset);
369 		kset_unregister(wmi_priv.authentication_dir_kset);
370 		wmi_priv.authentication_dir_kset = NULL;
371 	}
372 	if (wmi_priv.main_dir_kset) {
373 		sysfs_remove_file(&wmi_priv.main_dir_kset->kobj, &reset_bios.attr);
374 		sysfs_remove_file(&wmi_priv.main_dir_kset->kobj, &pending_reboot.attr);
375 		destroy_attribute_objs(wmi_priv.main_dir_kset);
376 		kset_unregister(wmi_priv.main_dir_kset);
377 		wmi_priv.main_dir_kset = NULL;
378 	}
379 	mutex_unlock(&wmi_priv.mutex);
380 }
381 
382 /**
383  * init_bios_attributes() - Initialize all attributes for a type
384  * @attr_type: The attribute type to initialize
385  * @guid: The WMI GUID associated with this type to initialize
386  *
387  * Initialiaze all 4 types of attributes enumeration, integer, string and password object.
388  * Populates each attrbute typ's respective properties under sysfs files
389  */
init_bios_attributes(int attr_type,const char * guid)390 static int init_bios_attributes(int attr_type, const char *guid)
391 {
392 	struct kobject *attr_name_kobj; //individual attribute names
393 	union acpi_object *obj = NULL;
394 	union acpi_object *elements;
395 	struct kobject *duplicate;
396 	struct kset *tmp_set;
397 	int min_elements;
398 
399 	/* instance_id needs to be reset for each type GUID
400 	 * also, instance IDs are unique within GUID but not across
401 	 */
402 	int instance_id = 0;
403 	int retval = 0;
404 
405 	retval = alloc_attributes_data(attr_type);
406 	if (retval)
407 		return retval;
408 
409 	switch (attr_type) {
410 	case ENUM:	min_elements = 8;	break;
411 	case INT:	min_elements = 9;	break;
412 	case STR:	min_elements = 8;	break;
413 	case PO:	min_elements = 4;	break;
414 	default:
415 		pr_err("Error: Unknown attr_type: %d\n", attr_type);
416 		return -EINVAL;
417 	}
418 
419 	/* need to use specific instance_id and guid combination to get right data */
420 	obj = get_wmiobj_pointer(instance_id, guid);
421 	if (!obj)
422 		return -ENODEV;
423 
424 	mutex_lock(&wmi_priv.mutex);
425 	while (obj) {
426 		if (obj->type != ACPI_TYPE_PACKAGE) {
427 			pr_err("Error: Expected ACPI-package type, got: %d\n", obj->type);
428 			retval = -EIO;
429 			goto err_attr_init;
430 		}
431 
432 		if (obj->package.count < min_elements) {
433 			pr_err("Error: ACPI-package does not have enough elements: %d < %d\n",
434 			       obj->package.count, min_elements);
435 			goto nextobj;
436 		}
437 
438 		elements = obj->package.elements;
439 
440 		/* sanity checking */
441 		if (elements[ATTR_NAME].type != ACPI_TYPE_STRING) {
442 			pr_debug("incorrect element type\n");
443 			goto nextobj;
444 		}
445 		if (strlen(elements[ATTR_NAME].string.pointer) == 0) {
446 			pr_debug("empty attribute found\n");
447 			goto nextobj;
448 		}
449 		if (attr_type == PO)
450 			tmp_set = wmi_priv.authentication_dir_kset;
451 		else
452 			tmp_set = wmi_priv.main_dir_kset;
453 
454 		duplicate = kset_find_obj(tmp_set, elements[ATTR_NAME].string.pointer);
455 		if (duplicate) {
456 			pr_debug("Duplicate attribute name found - %s\n",
457 				 elements[ATTR_NAME].string.pointer);
458 			kobject_put(duplicate);
459 			goto nextobj;
460 		}
461 
462 		/* build attribute */
463 		attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
464 		if (!attr_name_kobj) {
465 			retval = -ENOMEM;
466 			goto err_attr_init;
467 		}
468 
469 		attr_name_kobj->kset = tmp_set;
470 
471 		retval = kobject_init_and_add(attr_name_kobj, &attr_name_ktype, NULL, "%s",
472 						elements[ATTR_NAME].string.pointer);
473 		if (retval) {
474 			kobject_put(attr_name_kobj);
475 			goto err_attr_init;
476 		}
477 
478 		/* enumerate all of this attribute */
479 		switch (attr_type) {
480 		case ENUM:
481 			retval = populate_enum_data(elements, instance_id, attr_name_kobj,
482 					obj->package.count);
483 			break;
484 		case INT:
485 			retval = populate_int_data(elements, instance_id, attr_name_kobj);
486 			break;
487 		case STR:
488 			retval = populate_str_data(elements, instance_id, attr_name_kobj);
489 			break;
490 		case PO:
491 			retval = populate_po_data(elements, instance_id, attr_name_kobj);
492 			break;
493 		default:
494 			break;
495 		}
496 
497 		if (retval) {
498 			pr_debug("failed to populate %s\n",
499 				elements[ATTR_NAME].string.pointer);
500 			goto err_attr_init;
501 		}
502 
503 nextobj:
504 		kfree(obj);
505 		instance_id++;
506 		obj = get_wmiobj_pointer(instance_id, guid);
507 	}
508 
509 	mutex_unlock(&wmi_priv.mutex);
510 	return 0;
511 
512 err_attr_init:
513 	mutex_unlock(&wmi_priv.mutex);
514 	kfree(obj);
515 	return retval;
516 }
517 
sysman_init(void)518 static int __init sysman_init(void)
519 {
520 	int ret = 0;
521 
522 	if (!dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL) &&
523 	    !dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Alienware", NULL) &&
524 	    !dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "www.dell.com", NULL)) {
525 		pr_err("Unable to run on non-Dell system\n");
526 		return -ENODEV;
527 	}
528 
529 	ret = init_bios_attr_set_interface();
530 	if (ret)
531 		return ret;
532 
533 	ret = init_bios_attr_pass_interface();
534 	if (ret)
535 		goto err_exit_bios_attr_set_interface;
536 
537 	if (!wmi_priv.bios_attr_wdev || !wmi_priv.password_attr_wdev) {
538 		pr_debug("failed to find set or pass interface\n");
539 		ret = -ENODEV;
540 		goto err_exit_bios_attr_pass_interface;
541 	}
542 
543 	wmi_priv.class_dev = device_create(&firmware_attributes_class, NULL, MKDEV(0, 0),
544 				  NULL, "%s", DRIVER_NAME);
545 	if (IS_ERR(wmi_priv.class_dev)) {
546 		ret = PTR_ERR(wmi_priv.class_dev);
547 		goto err_exit_bios_attr_pass_interface;
548 	}
549 
550 	wmi_priv.main_dir_kset = kset_create_and_add("attributes", NULL,
551 						     &wmi_priv.class_dev->kobj);
552 	if (!wmi_priv.main_dir_kset) {
553 		ret = -ENOMEM;
554 		goto err_destroy_classdev;
555 	}
556 
557 	wmi_priv.authentication_dir_kset = kset_create_and_add("authentication", NULL,
558 								&wmi_priv.class_dev->kobj);
559 	if (!wmi_priv.authentication_dir_kset) {
560 		ret = -ENOMEM;
561 		goto err_release_attributes_data;
562 	}
563 
564 	ret = create_attributes_level_sysfs_files();
565 	if (ret) {
566 		pr_debug("could not create reset BIOS attribute\n");
567 		goto err_release_attributes_data;
568 	}
569 
570 	ret = init_bios_attributes(ENUM, DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID);
571 	if (ret) {
572 		pr_debug("failed to populate enumeration type attributes\n");
573 		goto err_release_attributes_data;
574 	}
575 
576 	ret = init_bios_attributes(INT, DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID);
577 	if (ret) {
578 		pr_debug("failed to populate integer type attributes\n");
579 		goto err_release_attributes_data;
580 	}
581 
582 	ret = init_bios_attributes(STR, DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID);
583 	if (ret) {
584 		pr_debug("failed to populate string type attributes\n");
585 		goto err_release_attributes_data;
586 	}
587 
588 	ret = init_bios_attributes(PO, DELL_WMI_BIOS_PASSOBJ_ATTRIBUTE_GUID);
589 	if (ret) {
590 		pr_debug("failed to populate pass object type attributes\n");
591 		goto err_release_attributes_data;
592 	}
593 
594 	return 0;
595 
596 err_release_attributes_data:
597 	release_attributes_data();
598 
599 err_destroy_classdev:
600 	device_destroy(&firmware_attributes_class, MKDEV(0, 0));
601 
602 err_exit_bios_attr_pass_interface:
603 	exit_bios_attr_pass_interface();
604 
605 err_exit_bios_attr_set_interface:
606 	exit_bios_attr_set_interface();
607 
608 	return ret;
609 }
610 
sysman_exit(void)611 static void __exit sysman_exit(void)
612 {
613 	release_attributes_data();
614 	device_destroy(&firmware_attributes_class, MKDEV(0, 0));
615 	exit_bios_attr_set_interface();
616 	exit_bios_attr_pass_interface();
617 }
618 
619 module_init(sysman_init);
620 module_exit(sysman_exit);
621 
622 MODULE_AUTHOR("Mario Limonciello <[email protected]>");
623 MODULE_AUTHOR("Prasanth Ksr <[email protected]>");
624 MODULE_AUTHOR("Divya Bharathi <[email protected]>");
625 MODULE_DESCRIPTION("Dell platform setting control interface");
626 MODULE_LICENSE("GPL");
627