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