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