1 // SPDX-License-Identifier: GPL-2.0
3 * Common methods for use with dell-wmi-sysman
5 * Copyright (c) 2020 Dell Inc.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
18 #include <linux/nls.h>
20 static struct class firmware_attributes_class
= {
21 .name
= "firmware-attributes",
24 struct wmi_sysman_priv wmi_priv
= {
25 .mutex
= __MUTEX_INITIALIZER(wmi_priv
.mutex
),
28 /* reset bios to defaults */
29 static const char * const reset_types
[] = {"builtinsafe", "lastknowngood", "factory", "custom"};
30 static int reset_option
= -1;
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
39 ssize_t
populate_string_buffer(char *buffer
, size_t buffer_len
, const char *str
)
41 u16
*length
= (u16
*)buffer
;
42 u16
*target
= length
+ 1;
45 ret
= utf8s_to_utf16s(str
, strlen(str
), UTF16_HOST_ENDIAN
,
46 target
, buffer_len
- sizeof(u16
));
48 dev_err(wmi_priv
.class_dev
, "UTF16 conversion failed\n");
52 if ((ret
* sizeof(u16
)) > U16_MAX
) {
53 dev_err(wmi_priv
.class_dev
, "Error string too long\n");
57 *length
= ret
* sizeof(u16
);
58 return sizeof(u16
) + *length
;
62 * calculate_string_buffer() - determines size of string buffer for use with BIOS communication
63 * @str: the string to calculate based upon
66 size_t calculate_string_buffer(const char *str
)
68 /* u16 length field + one UTF16 char for each input char */
69 return sizeof(u16
) + strlen(str
) * sizeof(u16
);
73 * calculate_security_buffer() - determines size of security buffer for authentication scheme
74 * @authentication: the authentication content
76 * Currently only supported type is Admin password
78 size_t calculate_security_buffer(char *authentication
)
80 if (strlen(authentication
) > 0) {
81 return (sizeof(u32
) * 2) + strlen(authentication
) +
82 strlen(authentication
) % 2;
84 return sizeof(u32
) * 2;
88 * populate_security_buffer() - builds a security buffer for authentication scheme
89 * @buffer: the buffer to populate
90 * @authentication: the authentication content
92 * Currently only supported type is PLAIN TEXT
94 void populate_security_buffer(char *buffer
, char *authentication
)
96 char *auth
= buffer
+ sizeof(u32
) * 2;
97 u32
*sectype
= (u32
*) buffer
;
98 u32
*seclen
= sectype
+ 1;
100 *sectype
= strlen(authentication
) > 0 ? 1 : 0;
101 *seclen
= strlen(authentication
);
104 if (strlen(authentication
) > 0)
105 memcpy(auth
, authentication
, *seclen
);
109 * map_wmi_error() - map errors from WMI methods to kernel error codes
110 * @error_code: integer error code returned from Dell's firmware
112 int map_wmi_error(int error_code
)
114 switch (error_code
) {
122 /* invalid parameter */
137 /* unspecified error */
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
147 static ssize_t
reset_bios_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
152 for (i
= 0; i
< MAX_TYPES
; i
++) {
153 if (i
== reset_option
)
154 buf
+= sprintf(buf
, "[%s] ", reset_types
[i
]);
156 buf
+= sprintf(buf
, "%s ", reset_types
[i
]);
158 buf
+= sprintf(buf
, "\n");
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
169 static ssize_t
reset_bios_store(struct kobject
*kobj
,
170 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
172 int type
= sysfs_match_string(reset_types
, buf
);
178 ret
= set_bios_defaults(type
);
179 pr_debug("reset all attributes request type %d: %d\n", type
, ret
);
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
194 * Stores default value as 0
195 * When current_value is changed this attribute is set to 1 to notify reboot may be required
197 static ssize_t
pending_reboot_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
200 return sprintf(buf
, "%d\n", wmi_priv
.pending_changes
);
203 static struct kobj_attribute reset_bios
= __ATTR_RW(reset_bios
);
204 static struct kobj_attribute pending_reboot
= __ATTR_RO(pending_reboot
);
208 * create_attributes_level_sysfs_files() - Creates reset_bios and
209 * pending_reboot attributes
211 static int create_attributes_level_sysfs_files(void)
213 int ret
= sysfs_create_file(&wmi_priv
.main_dir_kset
->kobj
, &reset_bios
.attr
);
216 pr_debug("could not create reset_bios file\n");
220 ret
= sysfs_create_file(&wmi_priv
.main_dir_kset
->kobj
, &pending_reboot
.attr
);
222 pr_debug("could not create changing_pending_reboot file\n");
223 sysfs_remove_file(&wmi_priv
.main_dir_kset
->kobj
, &reset_bios
.attr
);
228 static void release_reset_bios_data(void)
230 sysfs_remove_file(&wmi_priv
.main_dir_kset
->kobj
, &reset_bios
.attr
);
231 sysfs_remove_file(&wmi_priv
.main_dir_kset
->kobj
, &pending_reboot
.attr
);
234 static ssize_t
wmi_sysman_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
237 struct kobj_attribute
*kattr
;
240 kattr
= container_of(attr
, struct kobj_attribute
, attr
);
242 ret
= kattr
->show(kobj
, kattr
, buf
);
246 static ssize_t
wmi_sysman_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
247 const char *buf
, size_t count
)
249 struct kobj_attribute
*kattr
;
252 kattr
= container_of(attr
, struct kobj_attribute
, attr
);
254 ret
= kattr
->store(kobj
, kattr
, buf
, count
);
258 static const struct sysfs_ops wmi_sysman_kobj_sysfs_ops
= {
259 .show
= wmi_sysman_attr_show
,
260 .store
= wmi_sysman_attr_store
,
263 static void attr_name_release(struct kobject
*kobj
)
268 static struct kobj_type attr_name_ktype
= {
269 .release
= attr_name_release
,
270 .sysfs_ops
= &wmi_sysman_kobj_sysfs_ops
,
274 * strlcpy_attr - Copy a length-limited, NULL-terminated string with bound checks
275 * @dest: Where to copy the string to
276 * @src: Where to copy the string from
278 void strlcpy_attr(char *dest
, char *src
)
280 size_t len
= strlen(src
) + 1;
282 if (len
> 1 && len
<= MAX_BUFF
)
283 strlcpy(dest
, src
, len
);
285 /*len can be zero because any property not-applicable to attribute can
286 * be empty so check only for too long buffers and log error
289 pr_err("Source string returned from BIOS is out of bound!\n");
293 * get_wmiobj_pointer() - Get Content of WMI block for particular instance
294 * @instance_id: WMI instance ID
295 * @guid_string: WMI GUID (in str form)
297 * Fetches the content for WMI block (instance_id) under GUID (guid_string)
298 * Caller must kfree the return
300 union acpi_object
*get_wmiobj_pointer(int instance_id
, const char *guid_string
)
302 struct acpi_buffer out
= { ACPI_ALLOCATE_BUFFER
, NULL
};
305 status
= wmi_query_block(guid_string
, instance_id
, &out
);
307 return ACPI_SUCCESS(status
) ? (union acpi_object
*)out
.pointer
: NULL
;
311 * get_instance_count() - Compute total number of instances under guid_string
312 * @guid_string: WMI GUID (in string form)
314 int get_instance_count(const char *guid_string
)
316 union acpi_object
*wmi_obj
= NULL
;
321 wmi_obj
= get_wmiobj_pointer(i
, guid_string
);
329 * alloc_attributes_data() - Allocate attributes data for a particular type
330 * @attr_type: Attribute type to allocate
332 static int alloc_attributes_data(int attr_type
)
338 retval
= alloc_enum_data();
341 retval
= alloc_int_data();
344 retval
= alloc_str_data();
347 retval
= alloc_po_data();
357 * destroy_attribute_objs() - Free a kset of kobjects
358 * @kset: The kset to destroy
360 * Fress kobjects created for each attribute_name under attribute type kset
362 static void destroy_attribute_objs(struct kset
*kset
)
364 struct kobject
*pos
, *next
;
366 list_for_each_entry_safe(pos
, next
, &kset
->list
, entry
) {
372 * release_attributes_data() - Clean-up all sysfs directories and files created
374 static void release_attributes_data(void)
376 release_reset_bios_data();
378 mutex_lock(&wmi_priv
.mutex
);
379 exit_enum_attributes();
380 exit_int_attributes();
381 exit_str_attributes();
382 exit_po_attributes();
383 if (wmi_priv
.authentication_dir_kset
) {
384 destroy_attribute_objs(wmi_priv
.authentication_dir_kset
);
385 kset_unregister(wmi_priv
.authentication_dir_kset
);
386 wmi_priv
.authentication_dir_kset
= NULL
;
388 if (wmi_priv
.main_dir_kset
) {
389 destroy_attribute_objs(wmi_priv
.main_dir_kset
);
390 kset_unregister(wmi_priv
.main_dir_kset
);
392 mutex_unlock(&wmi_priv
.mutex
);
397 * init_bios_attributes() - Initialize all attributes for a type
398 * @attr_type: The attribute type to initialize
399 * @guid: The WMI GUID associated with this type to initialize
401 * Initialiaze all 4 types of attributes enumeration, integer, string and password object.
402 * Populates each attrbute typ's respective properties under sysfs files
404 static int init_bios_attributes(int attr_type
, const char *guid
)
406 struct kobject
*attr_name_kobj
; //individual attribute names
407 union acpi_object
*obj
= NULL
;
408 union acpi_object
*elements
;
409 struct kset
*tmp_set
;
411 /* instance_id needs to be reset for each type GUID
412 * also, instance IDs are unique within GUID but not across
417 retval
= alloc_attributes_data(attr_type
);
420 /* need to use specific instance_id and guid combination to get right data */
421 obj
= get_wmiobj_pointer(instance_id
, guid
);
424 elements
= obj
->package
.elements
;
426 mutex_lock(&wmi_priv
.mutex
);
428 /* sanity checking */
429 if (strlen(elements
[ATTR_NAME
].string
.pointer
) == 0) {
430 pr_debug("empty attribute found\n");
434 tmp_set
= wmi_priv
.authentication_dir_kset
;
436 tmp_set
= wmi_priv
.main_dir_kset
;
438 if (kset_find_obj(tmp_set
, elements
[ATTR_NAME
].string
.pointer
)) {
439 pr_debug("duplicate attribute name found - %s\n",
440 elements
[ATTR_NAME
].string
.pointer
);
444 /* build attribute */
445 attr_name_kobj
= kzalloc(sizeof(*attr_name_kobj
), GFP_KERNEL
);
446 if (!attr_name_kobj
) {
451 attr_name_kobj
->kset
= tmp_set
;
453 retval
= kobject_init_and_add(attr_name_kobj
, &attr_name_ktype
, NULL
, "%s",
454 elements
[ATTR_NAME
].string
.pointer
);
456 kobject_put(attr_name_kobj
);
460 /* enumerate all of this attribute */
463 retval
= populate_enum_data(elements
, instance_id
, attr_name_kobj
);
466 retval
= populate_int_data(elements
, instance_id
, attr_name_kobj
);
469 retval
= populate_str_data(elements
, instance_id
, attr_name_kobj
);
472 retval
= populate_po_data(elements
, instance_id
, attr_name_kobj
);
479 pr_debug("failed to populate %s\n",
480 elements
[ATTR_NAME
].string
.pointer
);
487 obj
= get_wmiobj_pointer(instance_id
, guid
);
488 elements
= obj
? obj
->package
.elements
: NULL
;
491 mutex_unlock(&wmi_priv
.mutex
);
495 mutex_unlock(&wmi_priv
.mutex
);
496 release_attributes_data();
501 static int __init
sysman_init(void)
505 if (!dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, "Dell System", NULL
) &&
506 !dmi_find_device(DMI_DEV_TYPE_OEM_STRING
, "www.dell.com", NULL
)) {
507 pr_err("Unable to run on non-Dell system\n");
511 ret
= init_bios_attr_set_interface();
512 if (ret
|| !wmi_priv
.bios_attr_wdev
) {
513 pr_debug("failed to initialize set interface\n");
514 goto fail_set_interface
;
517 ret
= init_bios_attr_pass_interface();
518 if (ret
|| !wmi_priv
.password_attr_wdev
) {
519 pr_debug("failed to initialize pass interface\n");
520 goto fail_pass_interface
;
523 ret
= class_register(&firmware_attributes_class
);
527 wmi_priv
.class_dev
= device_create(&firmware_attributes_class
, NULL
, MKDEV(0, 0),
528 NULL
, "%s", DRIVER_NAME
);
529 if (IS_ERR(wmi_priv
.class_dev
)) {
530 ret
= PTR_ERR(wmi_priv
.class_dev
);
534 wmi_priv
.main_dir_kset
= kset_create_and_add("attributes", NULL
,
535 &wmi_priv
.class_dev
->kobj
);
536 if (!wmi_priv
.main_dir_kset
) {
541 wmi_priv
.authentication_dir_kset
= kset_create_and_add("authentication", NULL
,
542 &wmi_priv
.class_dev
->kobj
);
543 if (!wmi_priv
.authentication_dir_kset
) {
545 goto fail_authentication_kset
;
548 ret
= create_attributes_level_sysfs_files();
550 pr_debug("could not create reset BIOS attribute\n");
551 goto fail_reset_bios
;
554 ret
= init_bios_attributes(ENUM
, DELL_WMI_BIOS_ENUMERATION_ATTRIBUTE_GUID
);
556 pr_debug("failed to populate enumeration type attributes\n");
557 goto fail_create_group
;
560 ret
= init_bios_attributes(INT
, DELL_WMI_BIOS_INTEGER_ATTRIBUTE_GUID
);
562 pr_debug("failed to populate integer type attributes\n");
563 goto fail_create_group
;
566 ret
= init_bios_attributes(STR
, DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID
);
568 pr_debug("failed to populate string type attributes\n");
569 goto fail_create_group
;
572 ret
= init_bios_attributes(PO
, DELL_WMI_BIOS_PASSOBJ_ATTRIBUTE_GUID
);
574 pr_debug("failed to populate pass object type attributes\n");
575 goto fail_create_group
;
581 release_attributes_data();
584 if (wmi_priv
.authentication_dir_kset
) {
585 kset_unregister(wmi_priv
.authentication_dir_kset
);
586 wmi_priv
.authentication_dir_kset
= NULL
;
589 fail_authentication_kset
:
590 if (wmi_priv
.main_dir_kset
) {
591 kset_unregister(wmi_priv
.main_dir_kset
);
592 wmi_priv
.main_dir_kset
= NULL
;
596 device_destroy(&firmware_attributes_class
, MKDEV(0, 0));
599 class_unregister(&firmware_attributes_class
);
602 exit_bios_attr_pass_interface();
605 exit_bios_attr_set_interface();
611 static void __exit
sysman_exit(void)
613 release_attributes_data();
614 device_destroy(&firmware_attributes_class
, MKDEV(0, 0));
615 class_unregister(&firmware_attributes_class
);
616 exit_bios_attr_set_interface();
617 exit_bios_attr_pass_interface();
620 module_init(sysman_init
);
621 module_exit(sysman_exit
);
623 MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>");
624 MODULE_AUTHOR("Prasanth Ksr <prasanth.ksr@dell.com>");
625 MODULE_AUTHOR("Divya Bharathi <divya.bharathi@dell.com>");
626 MODULE_DESCRIPTION("Dell platform setting control interface");
627 MODULE_LICENSE("GPL");