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++, 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 break;
236
237 case SECURITY_LEVEL:
238 ordered_list_data->common.security_level = int_value;
239 break;
240
241 case ORD_LIST_SIZE:
242 if (int_value > MAX_ELEMENTS_SIZE) {
243 pr_warn("Order List size value exceeded the maximum number of elements supported or data may be malformed\n");
244 int_value = MAX_ELEMENTS_SIZE;
245 }
246 ordered_list_data->elements_size = int_value;
247
248 /*
249 * This step is needed to keep the expected
250 * element list pointing to the right obj[elem].type
251 * when the size is zero. ORD_LIST_ELEMENTS
252 * object is omitted by BIOS when the size is
253 * zero.
254 */
255 if (int_value == 0)
256 eloc++;
257 break;
258 case ORD_LIST_ELEMENTS:
259
260 /*
261 * Ordered list data is stored in hex and comma separated format
262 * Convert the data and split it to show each element
263 */
264 ret = hp_convert_hexstr_to_str(str_value, value_len, &tmpstr, &tmp_len);
265 if (ret)
266 goto exit_list;
267
268 part_tmp = tmpstr;
269 part = strsep(&part_tmp, COMMA_SEP);
270
271 for (olist_elem = 0; olist_elem < MAX_ELEMENTS_SIZE && part; olist_elem++) {
272 strscpy(ordered_list_data->elements[olist_elem], part);
273 part = strsep(&part_tmp, COMMA_SEP);
274 }
275 ordered_list_data->elements_size = olist_elem;
276
277 kfree(str_value);
278 str_value = NULL;
279 break;
280 default:
281 pr_warn("Invalid element: %d found in Ordered_List attribute or data may be malformed\n", elem);
282 break;
283 }
284 kfree(tmpstr);
285 tmpstr = NULL;
286 kfree(str_value);
287 str_value = NULL;
288 }
289
290 exit_list:
291 kfree(tmpstr);
292 kfree(str_value);
293 return 0;
294 }
295
296 /**
297 * hp_populate_ordered_list_package_data() -
298 * Populate all properties of an instance under ordered_list attribute
299 *
300 * @order_obj: ACPI object with ordered_list data
301 * @instance_id: The instance to enumerate
302 * @attr_name_kobj: The parent kernel object
303 */
hp_populate_ordered_list_package_data(union acpi_object * order_obj,int instance_id,struct kobject * attr_name_kobj)304 int hp_populate_ordered_list_package_data(union acpi_object *order_obj, int instance_id,
305 struct kobject *attr_name_kobj)
306 {
307 struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
308
309 ordered_list_data->attr_name_kobj = attr_name_kobj;
310
311 hp_populate_ordered_list_elements_from_package(order_obj,
312 order_obj->package.count,
313 instance_id);
314 hp_update_attribute_permissions(ordered_list_data->common.is_readonly,
315 &ordered_list_current_val);
316 hp_friendly_user_name_update(ordered_list_data->common.path,
317 attr_name_kobj->name,
318 ordered_list_data->common.display_name,
319 sizeof(ordered_list_data->common.display_name));
320 return sysfs_create_group(attr_name_kobj, &ordered_list_attr_group);
321 }
322
hp_populate_ordered_list_elements_from_buffer(u8 * buffer_ptr,u32 * buffer_size,int instance_id)323 static int hp_populate_ordered_list_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
324 int instance_id)
325 {
326 int values;
327 struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
328 int ret = 0;
329
330 /*
331 * Only data relevant to this driver and its functionality is
332 * read. BIOS defines the order in which each * element is
333 * read. Element 0 data is not relevant to this
334 * driver hence it is ignored. For clarity, all element names
335 * (DISPLAY_IN_UI) which defines the order in which is read
336 * and the name matches the variable where the data is stored.
337 *
338 * In earlier implementation, reported errors were ignored
339 * causing the data to remain uninitialized. It is not
340 * possible to determine if data read from BIOS is valid or
341 * not. It is for this reason functions may return a error
342 * without validating the data itself.
343 */
344
345 // VALUE:
346 ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, ordered_list_data->current_value,
347 sizeof(ordered_list_data->current_value));
348 if (ret < 0)
349 goto buffer_exit;
350
351 replace_char_str(ordered_list_data->current_value, COMMA_SEP, SEMICOLON_SEP);
352
353 // COMMON:
354 ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size,
355 &ordered_list_data->common);
356 if (ret < 0)
357 goto buffer_exit;
358
359 // ORD_LIST_SIZE:
360 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
361 &ordered_list_data->elements_size);
362
363 if (ordered_list_data->elements_size > MAX_ELEMENTS_SIZE) {
364 /* Report a message and limit elements size to maximum value */
365 pr_warn("Ordered List size value exceeded the maximum number of elements supported or data may be malformed\n");
366 ordered_list_data->elements_size = MAX_ELEMENTS_SIZE;
367 }
368
369 // ORD_LIST_ELEMENTS:
370 for (values = 0; values < ordered_list_data->elements_size; values++) {
371 ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size,
372 ordered_list_data->elements[values],
373 sizeof(ordered_list_data->elements[values]));
374 if (ret < 0)
375 break;
376 }
377
378 buffer_exit:
379 return ret;
380 }
381
382 /**
383 * hp_populate_ordered_list_buffer_data() - Populate all properties of an
384 * instance under ordered list attribute
385 *
386 * @buffer_ptr: Buffer pointer
387 * @buffer_size: Buffer size
388 * @instance_id: The instance to enumerate
389 * @attr_name_kobj: The parent kernel object
390 */
hp_populate_ordered_list_buffer_data(u8 * buffer_ptr,u32 * buffer_size,int instance_id,struct kobject * attr_name_kobj)391 int hp_populate_ordered_list_buffer_data(u8 *buffer_ptr, u32 *buffer_size, int instance_id,
392 struct kobject *attr_name_kobj)
393 {
394 struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
395 int ret = 0;
396
397 ordered_list_data->attr_name_kobj = attr_name_kobj;
398
399 /* Populate ordered list elements */
400 ret = hp_populate_ordered_list_elements_from_buffer(buffer_ptr, buffer_size,
401 instance_id);
402 if (ret < 0)
403 return ret;
404
405 hp_update_attribute_permissions(ordered_list_data->common.is_readonly,
406 &ordered_list_current_val);
407 hp_friendly_user_name_update(ordered_list_data->common.path,
408 attr_name_kobj->name,
409 ordered_list_data->common.display_name,
410 sizeof(ordered_list_data->common.display_name));
411
412 return sysfs_create_group(attr_name_kobj, &ordered_list_attr_group);
413 }
414
415 /**
416 * hp_exit_ordered_list_attributes() - Clear all attribute data
417 *
418 * Clears all data allocated for this group of attributes
419 */
hp_exit_ordered_list_attributes(void)420 void hp_exit_ordered_list_attributes(void)
421 {
422 int instance_id;
423
424 for (instance_id = 0; instance_id < bioscfg_drv.ordered_list_instances_count;
425 instance_id++) {
426 struct kobject *attr_name_kobj =
427 bioscfg_drv.ordered_list_data[instance_id].attr_name_kobj;
428
429 if (attr_name_kobj)
430 sysfs_remove_group(attr_name_kobj,
431 &ordered_list_attr_group);
432 }
433 bioscfg_drv.ordered_list_instances_count = 0;
434
435 kfree(bioscfg_drv.ordered_list_data);
436 bioscfg_drv.ordered_list_data = NULL;
437 }
438