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