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