1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Functions corresponding to string type attributes under BIOS String GUID for use with
4 * dell-wmi-sysman
5 *
6 * Copyright (c) 2020 Dell Inc.
7 */
8
9 #include "dell-wmi-sysman.h"
10
11 enum string_properties {MIN_LEN = 6, MAX_LEN};
12
13 get_instance_id(str);
14
current_value_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)15 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
16 {
17 int instance_id = get_str_instance_id(kobj);
18 union acpi_object *obj;
19 ssize_t ret;
20
21 if (instance_id < 0)
22 return -EIO;
23
24 /* need to use specific instance_id and guid combination to get right data */
25 obj = get_wmiobj_pointer(instance_id, DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID);
26 if (!obj)
27 return -EIO;
28 if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count < STR_MIN_ELEMENTS ||
29 obj->package.elements[CURRENT_VAL].type != ACPI_TYPE_STRING) {
30 kfree(obj);
31 return -EIO;
32 }
33 ret = snprintf(buf, PAGE_SIZE, "%s\n", obj->package.elements[CURRENT_VAL].string.pointer);
34 kfree(obj);
35 return ret;
36 }
37
38 /**
39 * validate_str_input() - Validate input of current_value against min and max lengths
40 * @instance_id: The instance on which input is validated
41 * @buf: Input value
42 */
validate_str_input(int instance_id,const char * buf)43 static int validate_str_input(int instance_id, const char *buf)
44 {
45 int in_len = strlen(buf);
46
47 if ((in_len < wmi_priv.str_data[instance_id].min_length) ||
48 (in_len > wmi_priv.str_data[instance_id].max_length))
49 return -EINVAL;
50
51 return 0;
52 }
53
54 attribute_s_property_show(display_name_language_code, str);
55 static struct kobj_attribute str_displ_langcode =
56 __ATTR_RO(display_name_language_code);
57
58 attribute_s_property_show(display_name, str);
59 static struct kobj_attribute str_displ_name =
60 __ATTR_RO(display_name);
61
62 attribute_s_property_show(default_value, str);
63 static struct kobj_attribute str_default_val =
64 __ATTR_RO(default_value);
65
66 attribute_property_store(current_value, str);
67 static struct kobj_attribute str_current_val =
68 __ATTR_RW_MODE(current_value, 0600);
69
70 attribute_s_property_show(dell_modifier, str);
71 static struct kobj_attribute str_modifier =
72 __ATTR_RO(dell_modifier);
73
74 attribute_n_property_show(min_length, str);
75 static struct kobj_attribute str_min_length =
76 __ATTR_RO(min_length);
77
78 attribute_n_property_show(max_length, str);
79 static struct kobj_attribute str_max_length =
80 __ATTR_RO(max_length);
81
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)82 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
83 char *buf)
84 {
85 return sprintf(buf, "string\n");
86 }
87 static struct kobj_attribute str_type =
88 __ATTR_RO(type);
89
90 static struct attribute *str_attrs[] = {
91 &str_displ_langcode.attr,
92 &str_displ_name.attr,
93 &str_default_val.attr,
94 &str_current_val.attr,
95 &str_modifier.attr,
96 &str_min_length.attr,
97 &str_max_length.attr,
98 &str_type.attr,
99 NULL,
100 };
101
102 static const struct attribute_group str_attr_group = {
103 .attrs = str_attrs,
104 };
105
alloc_str_data(void)106 int alloc_str_data(void)
107 {
108 int ret = 0;
109
110 wmi_priv.str_instances_count = get_instance_count(DELL_WMI_BIOS_STRING_ATTRIBUTE_GUID);
111 wmi_priv.str_data = kcalloc(wmi_priv.str_instances_count,
112 sizeof(struct str_data), GFP_KERNEL);
113 if (!wmi_priv.str_data) {
114 wmi_priv.str_instances_count = 0;
115 ret = -ENOMEM;
116 }
117 return ret;
118 }
119
120 /**
121 * populate_str_data() - Populate all properties of an instance under string attribute
122 * @str_obj: ACPI object with string data
123 * @instance_id: The instance to enumerate
124 * @attr_name_kobj: The parent kernel object
125 */
populate_str_data(union acpi_object * str_obj,int instance_id,struct kobject * attr_name_kobj)126 int populate_str_data(union acpi_object *str_obj, int instance_id, struct kobject *attr_name_kobj)
127 {
128 wmi_priv.str_data[instance_id].attr_name_kobj = attr_name_kobj;
129 if (check_property_type(str, ATTR_NAME, ACPI_TYPE_STRING))
130 return -EINVAL;
131 strlcpy_attr(wmi_priv.str_data[instance_id].attribute_name,
132 str_obj[ATTR_NAME].string.pointer);
133 if (check_property_type(str, DISPL_NAME_LANG_CODE, ACPI_TYPE_STRING))
134 return -EINVAL;
135 strlcpy_attr(wmi_priv.str_data[instance_id].display_name_language_code,
136 str_obj[DISPL_NAME_LANG_CODE].string.pointer);
137 if (check_property_type(str, DISPLAY_NAME, ACPI_TYPE_STRING))
138 return -EINVAL;
139 strlcpy_attr(wmi_priv.str_data[instance_id].display_name,
140 str_obj[DISPLAY_NAME].string.pointer);
141 if (check_property_type(str, DEFAULT_VAL, ACPI_TYPE_STRING))
142 return -EINVAL;
143 strlcpy_attr(wmi_priv.str_data[instance_id].default_value,
144 str_obj[DEFAULT_VAL].string.pointer);
145 if (check_property_type(str, MODIFIER, ACPI_TYPE_STRING))
146 return -EINVAL;
147 strlcpy_attr(wmi_priv.str_data[instance_id].dell_modifier,
148 str_obj[MODIFIER].string.pointer);
149 if (check_property_type(str, MIN_LEN, ACPI_TYPE_INTEGER))
150 return -EINVAL;
151 wmi_priv.str_data[instance_id].min_length = (uintptr_t)str_obj[MIN_LEN].string.pointer;
152 if (check_property_type(str, MAX_LEN, ACPI_TYPE_INTEGER))
153 return -EINVAL;
154 wmi_priv.str_data[instance_id].max_length = (uintptr_t) str_obj[MAX_LEN].string.pointer;
155
156 return sysfs_create_group(attr_name_kobj, &str_attr_group);
157 }
158
159 /**
160 * exit_str_attributes() - Clear all attribute data
161 *
162 * Clears all data allocated for this group of attributes
163 */
exit_str_attributes(void)164 void exit_str_attributes(void)
165 {
166 int instance_id;
167
168 for (instance_id = 0; instance_id < wmi_priv.str_instances_count; instance_id++) {
169 if (wmi_priv.str_data[instance_id].attr_name_kobj)
170 sysfs_remove_group(wmi_priv.str_data[instance_id].attr_name_kobj,
171 &str_attr_group);
172 }
173 wmi_priv.str_instances_count = 0;
174
175 kfree(wmi_priv.str_data);
176 wmi_priv.str_data = NULL;
177 }
178