1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Functions corresponding to integer type attributes under
4 * BIOS Enumeration GUID for use with hp-bioscfg driver.
5 *
6 * Copyright (c) 2022 Hewlett-Packard Inc.
7 */
8
9 #include "bioscfg.h"
10
11 GET_INSTANCE_ID(integer);
12
current_value_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)13 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
14 {
15 int instance_id = get_integer_instance_id(kobj);
16
17 if (instance_id < 0)
18 return -EIO;
19
20 return sysfs_emit(buf, "%d\n",
21 bioscfg_drv.integer_data[instance_id].current_value);
22 }
23
24 /**
25 * validate_integer_input() -
26 * Validate input of current_value against lower and upper bound
27 *
28 * @instance_id: The instance on which input is validated
29 * @buf: Input value
30 */
validate_integer_input(int instance_id,char * buf)31 static int validate_integer_input(int instance_id, char *buf)
32 {
33 int in_val;
34 int ret;
35 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id];
36
37 /* BIOS treats it as a read only attribute */
38 if (integer_data->common.is_readonly)
39 return -EIO;
40
41 ret = kstrtoint(buf, 10, &in_val);
42 if (ret < 0)
43 return ret;
44
45 if (in_val < integer_data->lower_bound ||
46 in_val > integer_data->upper_bound)
47 return -ERANGE;
48
49 return 0;
50 }
51
update_integer_value(int instance_id,char * attr_value)52 static void update_integer_value(int instance_id, char *attr_value)
53 {
54 int in_val;
55 int ret;
56 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id];
57
58 ret = kstrtoint(attr_value, 10, &in_val);
59 if (ret == 0)
60 integer_data->current_value = in_val;
61 else
62 pr_warn("Invalid integer value found: %s\n", attr_value);
63 }
64
65 ATTRIBUTE_S_COMMON_PROPERTY_SHOW(display_name, integer);
66 static struct kobj_attribute integer_display_name =
67 __ATTR_RO(display_name);
68
69 ATTRIBUTE_PROPERTY_STORE(current_value, integer);
70 static struct kobj_attribute integer_current_val =
71 __ATTR_RW_MODE(current_value, 0644);
72
73 ATTRIBUTE_N_PROPERTY_SHOW(lower_bound, integer);
74 static struct kobj_attribute integer_lower_bound =
75 __ATTR_RO(lower_bound);
76
77 ATTRIBUTE_N_PROPERTY_SHOW(upper_bound, integer);
78 static struct kobj_attribute integer_upper_bound =
79 __ATTR_RO(upper_bound);
80
81 ATTRIBUTE_N_PROPERTY_SHOW(scalar_increment, integer);
82 static struct kobj_attribute integer_scalar_increment =
83 __ATTR_RO(scalar_increment);
84
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)85 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
86 char *buf)
87 {
88 return sysfs_emit(buf, "integer\n");
89 }
90
91 static struct kobj_attribute integer_type =
92 __ATTR_RO(type);
93
94 static struct attribute *integer_attrs[] = {
95 &common_display_langcode.attr,
96 &integer_display_name.attr,
97 &integer_current_val.attr,
98 &integer_lower_bound.attr,
99 &integer_upper_bound.attr,
100 &integer_scalar_increment.attr,
101 &integer_type.attr,
102 NULL
103 };
104
105 static const struct attribute_group integer_attr_group = {
106 .attrs = integer_attrs,
107 };
108
hp_alloc_integer_data(void)109 int hp_alloc_integer_data(void)
110 {
111 bioscfg_drv.integer_instances_count = hp_get_instance_count(HP_WMI_BIOS_INTEGER_GUID);
112 bioscfg_drv.integer_data = kcalloc(bioscfg_drv.integer_instances_count,
113 sizeof(*bioscfg_drv.integer_data), GFP_KERNEL);
114
115 if (!bioscfg_drv.integer_data) {
116 bioscfg_drv.integer_instances_count = 0;
117 return -ENOMEM;
118 }
119 return 0;
120 }
121
122 /* Expected Values types associated with each element */
123 static const acpi_object_type expected_integer_types[] = {
124 [NAME] = ACPI_TYPE_STRING,
125 [VALUE] = ACPI_TYPE_STRING,
126 [PATH] = ACPI_TYPE_STRING,
127 [IS_READONLY] = ACPI_TYPE_INTEGER,
128 [DISPLAY_IN_UI] = ACPI_TYPE_INTEGER,
129 [REQUIRES_PHYSICAL_PRESENCE] = ACPI_TYPE_INTEGER,
130 [SEQUENCE] = ACPI_TYPE_INTEGER,
131 [PREREQUISITES_SIZE] = ACPI_TYPE_INTEGER,
132 [PREREQUISITES] = ACPI_TYPE_STRING,
133 [SECURITY_LEVEL] = ACPI_TYPE_INTEGER,
134 [INT_LOWER_BOUND] = ACPI_TYPE_INTEGER,
135 [INT_UPPER_BOUND] = ACPI_TYPE_INTEGER,
136 [INT_SCALAR_INCREMENT] = ACPI_TYPE_INTEGER,
137 };
138
hp_populate_integer_elements_from_package(union acpi_object * integer_obj,int integer_obj_count,int instance_id)139 static int hp_populate_integer_elements_from_package(union acpi_object *integer_obj,
140 int integer_obj_count,
141 int instance_id)
142 {
143 char *str_value = NULL;
144 int value_len;
145 int ret;
146 u32 int_value = 0;
147 int elem;
148 int reqs;
149 int eloc;
150 int size;
151 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id];
152
153 if (!integer_obj)
154 return -EINVAL;
155
156 for (elem = 1, eloc = 1; elem < integer_obj_count; elem++, eloc++) {
157 /* ONLY look at the first INTEGER_ELEM_CNT elements */
158 if (eloc == INT_ELEM_CNT)
159 goto exit_integer_package;
160
161 switch (integer_obj[elem].type) {
162 case ACPI_TYPE_STRING:
163 if (elem != PREREQUISITES) {
164 ret = hp_convert_hexstr_to_str(integer_obj[elem].string.pointer,
165 integer_obj[elem].string.length,
166 &str_value, &value_len);
167 if (ret)
168 continue;
169 }
170 break;
171 case ACPI_TYPE_INTEGER:
172 int_value = (u32)integer_obj[elem].integer.value;
173 break;
174 default:
175 pr_warn("Unsupported object type [%d]\n", integer_obj[elem].type);
176 continue;
177 }
178 /* Check that both expected and read object type match */
179 if (expected_integer_types[eloc] != integer_obj[elem].type) {
180 pr_err("Error expected type %d for elem %d, but got type %d instead\n",
181 expected_integer_types[eloc], elem, integer_obj[elem].type);
182 kfree(str_value);
183 return -EIO;
184 }
185 /* Assign appropriate element value to corresponding field*/
186 switch (eloc) {
187 case VALUE:
188 ret = kstrtoint(str_value, 10, &int_value);
189 if (ret)
190 continue;
191
192 integer_data->current_value = int_value;
193 break;
194 case PATH:
195 strscpy(integer_data->common.path, str_value,
196 sizeof(integer_data->common.path));
197 break;
198 case IS_READONLY:
199 integer_data->common.is_readonly = int_value;
200 break;
201 case DISPLAY_IN_UI:
202 integer_data->common.display_in_ui = int_value;
203 break;
204 case REQUIRES_PHYSICAL_PRESENCE:
205 integer_data->common.requires_physical_presence = int_value;
206 break;
207 case SEQUENCE:
208 integer_data->common.sequence = int_value;
209 break;
210 case PREREQUISITES_SIZE:
211 if (int_value > MAX_PREREQUISITES_SIZE) {
212 pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n");
213 int_value = MAX_PREREQUISITES_SIZE;
214 }
215 integer_data->common.prerequisites_size = int_value;
216
217 /*
218 * This step is needed to keep the expected
219 * element list pointing to the right obj[elem].type
220 * when the size is zero. PREREQUISITES
221 * object is omitted by BIOS when the size is
222 * zero.
223 */
224 if (integer_data->common.prerequisites_size == 0)
225 eloc++;
226 break;
227 case PREREQUISITES:
228 size = min_t(u32, integer_data->common.prerequisites_size, MAX_PREREQUISITES_SIZE);
229
230 for (reqs = 0; reqs < size; reqs++) {
231 if (elem >= integer_obj_count) {
232 pr_err("Error elem-objects package is too small\n");
233 return -EINVAL;
234 }
235
236 ret = hp_convert_hexstr_to_str(integer_obj[elem + reqs].string.pointer,
237 integer_obj[elem + reqs].string.length,
238 &str_value, &value_len);
239
240 if (ret)
241 continue;
242
243 strscpy(integer_data->common.prerequisites[reqs],
244 str_value,
245 sizeof(integer_data->common.prerequisites[reqs]));
246 kfree(str_value);
247 str_value = NULL;
248 }
249 break;
250
251 case SECURITY_LEVEL:
252 integer_data->common.security_level = int_value;
253 break;
254 case INT_LOWER_BOUND:
255 integer_data->lower_bound = int_value;
256 break;
257 case INT_UPPER_BOUND:
258 integer_data->upper_bound = int_value;
259 break;
260 case INT_SCALAR_INCREMENT:
261 integer_data->scalar_increment = int_value;
262 break;
263 default:
264 pr_warn("Invalid element: %d found in Integer attribute or data may be malformed\n", elem);
265 break;
266 }
267
268 kfree(str_value);
269 str_value = NULL;
270 }
271 exit_integer_package:
272 kfree(str_value);
273 return 0;
274 }
275
276 /**
277 * hp_populate_integer_package_data() -
278 * Populate all properties of an instance under integer attribute
279 *
280 * @integer_obj: ACPI object with integer data
281 * @instance_id: The instance to enumerate
282 * @attr_name_kobj: The parent kernel object
283 */
hp_populate_integer_package_data(union acpi_object * integer_obj,int instance_id,struct kobject * attr_name_kobj)284 int hp_populate_integer_package_data(union acpi_object *integer_obj,
285 int instance_id,
286 struct kobject *attr_name_kobj)
287 {
288 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id];
289
290 integer_data->attr_name_kobj = attr_name_kobj;
291 hp_populate_integer_elements_from_package(integer_obj,
292 integer_obj->package.count,
293 instance_id);
294 hp_update_attribute_permissions(integer_data->common.is_readonly,
295 &integer_current_val);
296 hp_friendly_user_name_update(integer_data->common.path,
297 attr_name_kobj->name,
298 integer_data->common.display_name,
299 sizeof(integer_data->common.display_name));
300 return sysfs_create_group(attr_name_kobj, &integer_attr_group);
301 }
302
hp_populate_integer_elements_from_buffer(u8 * buffer_ptr,u32 * buffer_size,int instance_id)303 static int hp_populate_integer_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
304 int instance_id)
305 {
306 char *dst = NULL;
307 int dst_size = *buffer_size / sizeof(u16);
308 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id];
309 int ret = 0;
310
311 dst = kcalloc(dst_size, sizeof(char), GFP_KERNEL);
312 if (!dst)
313 return -ENOMEM;
314
315 /*
316 * Only data relevant to this driver and its functionality is
317 * read. BIOS defines the order in which each * element is
318 * read. Element 0 data is not relevant to this
319 * driver hence it is ignored. For clarity, all element names
320 * (DISPLAY_IN_UI) which defines the order in which is read
321 * and the name matches the variable where the data is stored.
322 *
323 * In earlier implementation, reported errors were ignored
324 * causing the data to remain uninitialized. It is not
325 * possible to determine if data read from BIOS is valid or
326 * not. It is for this reason functions may return a error
327 * without validating the data itself.
328 */
329
330 // VALUE:
331 integer_data->current_value = 0;
332
333 hp_get_string_from_buffer(&buffer_ptr, buffer_size, dst, dst_size);
334 ret = kstrtoint(dst, 10, &integer_data->current_value);
335 if (ret)
336 pr_warn("Unable to convert string to integer: %s\n", dst);
337 kfree(dst);
338
339 // COMMON:
340 ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size, &integer_data->common);
341 if (ret < 0)
342 goto buffer_exit;
343
344 // INT_LOWER_BOUND:
345 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
346 &integer_data->lower_bound);
347 if (ret < 0)
348 goto buffer_exit;
349
350 // INT_UPPER_BOUND:
351 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
352 &integer_data->upper_bound);
353 if (ret < 0)
354 goto buffer_exit;
355
356 // INT_SCALAR_INCREMENT:
357 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
358 &integer_data->scalar_increment);
359
360 buffer_exit:
361 return ret;
362 }
363
364 /**
365 * hp_populate_integer_buffer_data() -
366 * Populate all properties of an instance under integer attribute
367 *
368 * @buffer_ptr: Buffer pointer
369 * @buffer_size: Buffer size
370 * @instance_id: The instance to enumerate
371 * @attr_name_kobj: The parent kernel object
372 */
hp_populate_integer_buffer_data(u8 * buffer_ptr,u32 * buffer_size,int instance_id,struct kobject * attr_name_kobj)373 int hp_populate_integer_buffer_data(u8 *buffer_ptr, u32 *buffer_size, int instance_id,
374 struct kobject *attr_name_kobj)
375 {
376 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id];
377 int ret = 0;
378
379 integer_data->attr_name_kobj = attr_name_kobj;
380
381 /* Populate integer elements */
382 ret = hp_populate_integer_elements_from_buffer(buffer_ptr, buffer_size,
383 instance_id);
384 if (ret < 0)
385 return ret;
386
387 hp_update_attribute_permissions(integer_data->common.is_readonly,
388 &integer_current_val);
389 hp_friendly_user_name_update(integer_data->common.path,
390 attr_name_kobj->name,
391 integer_data->common.display_name,
392 sizeof(integer_data->common.display_name));
393
394 return sysfs_create_group(attr_name_kobj, &integer_attr_group);
395 }
396
397 /**
398 * hp_exit_integer_attributes() - Clear all attribute data
399 *
400 * Clears all data allocated for this group of attributes
401 */
hp_exit_integer_attributes(void)402 void hp_exit_integer_attributes(void)
403 {
404 int instance_id;
405
406 for (instance_id = 0; instance_id < bioscfg_drv.integer_instances_count;
407 instance_id++) {
408 struct kobject *attr_name_kobj =
409 bioscfg_drv.integer_data[instance_id].attr_name_kobj;
410
411 if (attr_name_kobj)
412 sysfs_remove_group(attr_name_kobj, &integer_attr_group);
413 }
414 bioscfg_drv.integer_instances_count = 0;
415
416 kfree(bioscfg_drv.integer_data);
417 bioscfg_drv.integer_data = NULL;
418 }
419