xref: /linux/drivers/platform/x86/hp/hp-bioscfg/order-list-attributes.c (revision ae53bb89b9703e2a691651368411988782e9b1a7)
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, 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 
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 
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 
102 	if (!bioscfg_drv.ordered_list_instances_count)
103 		return -EINVAL;
104 	bioscfg_drv.ordered_list_data =
105 			kvzalloc_objs(*bioscfg_drv.ordered_list_data,
106 						bioscfg_drv.ordered_list_instances_count);
107 
108 	if (!bioscfg_drv.ordered_list_data) {
109 		bioscfg_drv.ordered_list_instances_count = 0;
110 		return -ENOMEM;
111 	}
112 	return 0;
113 }
114 
115 /* Expected Values types associated with each element */
116 static const acpi_object_type expected_order_types[] = {
117 	[NAME]	= ACPI_TYPE_STRING,
118 	[VALUE] = ACPI_TYPE_STRING,
119 	[PATH] = ACPI_TYPE_STRING,
120 	[IS_READONLY] = ACPI_TYPE_INTEGER,
121 	[DISPLAY_IN_UI] = ACPI_TYPE_INTEGER,
122 	[REQUIRES_PHYSICAL_PRESENCE] = ACPI_TYPE_INTEGER,
123 	[SEQUENCE] = ACPI_TYPE_INTEGER,
124 	[PREREQUISITES_SIZE] = ACPI_TYPE_INTEGER,
125 	[PREREQUISITES] = ACPI_TYPE_STRING,
126 	[SECURITY_LEVEL] = ACPI_TYPE_INTEGER,
127 	[ORD_LIST_SIZE] = ACPI_TYPE_INTEGER,
128 	[ORD_LIST_ELEMENTS] = ACPI_TYPE_STRING,
129 };
130 
131 static int hp_populate_ordered_list_elements_from_package(union acpi_object *order_obj,
132 							  int order_obj_count,
133 							  int instance_id)
134 {
135 	char *str_value = NULL;
136 	int value_len = 0;
137 	int ret;
138 	u32 int_value = 0;
139 	int elem;
140 	int olist_elem;
141 	int eloc;
142 	char *tmpstr = NULL;
143 	char *part_tmp = NULL;
144 	int tmp_len = 0;
145 	char *part = NULL;
146 	struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
147 
148 	if (!order_obj)
149 		return -EINVAL;
150 
151 	for (elem = 1, eloc = 1; eloc < ORD_ELEM_CNT && elem < order_obj_count; elem++, eloc++) {
152 
153 		switch (order_obj[elem].type) {
154 		case ACPI_TYPE_STRING:
155 			if (elem != PREREQUISITES && elem != ORD_LIST_ELEMENTS) {
156 				ret = hp_convert_hexstr_to_str(order_obj[elem].string.pointer,
157 							       order_obj[elem].string.length,
158 							       &str_value, &value_len);
159 				if (ret)
160 					continue;
161 			}
162 			break;
163 		case ACPI_TYPE_INTEGER:
164 			int_value = (u32)order_obj[elem].integer.value;
165 			break;
166 		default:
167 			pr_warn("Unsupported object type [%d]\n", order_obj[elem].type);
168 			continue;
169 		}
170 
171 		/* Check that both expected and read object type match */
172 		if (expected_order_types[eloc] != order_obj[elem].type) {
173 			pr_err("Error expected type %d for elem %d, but got type %d instead\n",
174 			       expected_order_types[eloc], elem, order_obj[elem].type);
175 			kfree(str_value);
176 			return -EIO;
177 		}
178 
179 		/* Assign appropriate element value to corresponding field*/
180 		switch (eloc) {
181 		case VALUE:
182 			strscpy(ordered_list_data->current_value, str_value);
183 			replace_char_str(ordered_list_data->current_value, COMMA_SEP, SEMICOLON_SEP);
184 			break;
185 		case PATH:
186 		case IS_READONLY:
187 		case DISPLAY_IN_UI:
188 		case REQUIRES_PHYSICAL_PRESENCE:
189 		case SEQUENCE:
190 		case PREREQUISITES_SIZE:
191 		case PREREQUISITES:
192 		case SECURITY_LEVEL:
193 			ret = hp_get_common_data_from_package(order_obj, order_obj_count,
194 							      &elem, &eloc, int_value, &str_value,
195 							      &ordered_list_data->common);
196 			if (ret)
197 				return ret;
198 			break;
199 
200 		case ORD_LIST_SIZE:
201 			if (int_value > MAX_ELEMENTS_SIZE) {
202 				pr_warn("Order List size value exceeded the maximum number of elements supported or data may be malformed\n");
203 				int_value = MAX_ELEMENTS_SIZE;
204 			}
205 			ordered_list_data->elements_size = int_value;
206 
207 			/*
208 			 * This step is needed to keep the expected
209 			 * element list pointing to the right obj[elem].type
210 			 * when the size is zero. ORD_LIST_ELEMENTS
211 			 * object is omitted by BIOS when the size is
212 			 * zero.
213 			 */
214 			if (int_value == 0)
215 				eloc++;
216 			break;
217 		case ORD_LIST_ELEMENTS:
218 
219 			/*
220 			 * Ordered list data is stored in hex and comma separated format
221 			 * Convert the data and split it to show each element
222 			 */
223 			ret = hp_convert_hexstr_to_str(order_obj[elem].string.pointer,
224 						       order_obj[elem].string.length,
225 						       &tmpstr, &tmp_len);
226 			if (ret)
227 				goto exit_list;
228 
229 			part_tmp = tmpstr;
230 			part = strsep(&part_tmp, COMMA_SEP);
231 
232 			for (olist_elem = 0; olist_elem < MAX_ELEMENTS_SIZE && part; olist_elem++) {
233 				strscpy(ordered_list_data->elements[olist_elem], part);
234 				part = strsep(&part_tmp, COMMA_SEP);
235 			}
236 			ordered_list_data->elements_size = olist_elem;
237 
238 			kfree(str_value);
239 			str_value = NULL;
240 			break;
241 		default:
242 			pr_warn("Invalid element: %d found in Ordered_List attribute or data may be malformed\n", elem);
243 			break;
244 		}
245 		kfree(tmpstr);
246 		tmpstr = NULL;
247 		kfree(str_value);
248 		str_value = NULL;
249 	}
250 
251 exit_list:
252 	kfree(tmpstr);
253 	kfree(str_value);
254 	return 0;
255 }
256 
257 /**
258  * hp_populate_ordered_list_package_data() -
259  * Populate all properties of an instance under ordered_list attribute
260  *
261  * @order_obj: ACPI object with ordered_list data
262  * @order_obj_count: Number of elements in @order_obj
263  * @instance_id: The instance to enumerate
264  * @attr_name_kobj: The parent kernel object
265  */
266 int hp_populate_ordered_list_package_data(union acpi_object *order_obj, int order_obj_count,
267 					  int instance_id,
268 					  struct kobject *attr_name_kobj)
269 {
270 	struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
271 
272 	ordered_list_data->attr_name_kobj = attr_name_kobj;
273 
274 	hp_populate_ordered_list_elements_from_package(order_obj,
275 						       order_obj_count,
276 						       instance_id);
277 	hp_update_attribute_permissions(ordered_list_data->common.is_readonly,
278 					&ordered_list_current_val);
279 	hp_friendly_user_name_update(ordered_list_data->common.path,
280 				     attr_name_kobj->name,
281 				     ordered_list_data->common.display_name,
282 				     sizeof(ordered_list_data->common.display_name));
283 	return sysfs_create_group(attr_name_kobj, &ordered_list_attr_group);
284 }
285 
286 static int hp_populate_ordered_list_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
287 							 int instance_id)
288 {
289 	int values;
290 	struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
291 	int ret = 0;
292 
293 	/*
294 	 * Only data relevant to this driver and its functionality is
295 	 * read. BIOS defines the order in which each * element is
296 	 * read. Element 0 data is not relevant to this
297 	 * driver hence it is ignored. For clarity, all element names
298 	 * (DISPLAY_IN_UI) which defines the order in which is read
299 	 * and the name matches the variable where the data is stored.
300 	 *
301 	 * In earlier implementation, reported errors were ignored
302 	 * causing the data to remain uninitialized. It is not
303 	 * possible to determine if data read from BIOS is valid or
304 	 * not. It is for this reason functions may return a error
305 	 * without validating the data itself.
306 	 */
307 
308 	// VALUE:
309 	ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, ordered_list_data->current_value,
310 					sizeof(ordered_list_data->current_value));
311 	if (ret < 0)
312 		goto buffer_exit;
313 
314 	replace_char_str(ordered_list_data->current_value, COMMA_SEP, SEMICOLON_SEP);
315 
316 	// COMMON:
317 	ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size,
318 					     &ordered_list_data->common);
319 	if (ret < 0)
320 		goto buffer_exit;
321 
322 	// ORD_LIST_SIZE:
323 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
324 					 &ordered_list_data->elements_size);
325 
326 	if (ordered_list_data->elements_size > MAX_ELEMENTS_SIZE) {
327 		/* Report a message and limit elements size to maximum value */
328 		pr_warn("Ordered List size value exceeded the maximum number of elements supported or data may be malformed\n");
329 		ordered_list_data->elements_size = MAX_ELEMENTS_SIZE;
330 	}
331 
332 	// ORD_LIST_ELEMENTS:
333 	for (values = 0; values < ordered_list_data->elements_size; values++) {
334 		ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size,
335 						ordered_list_data->elements[values],
336 						sizeof(ordered_list_data->elements[values]));
337 		if (ret < 0)
338 			break;
339 	}
340 
341 buffer_exit:
342 	return ret;
343 }
344 
345 /**
346  * hp_populate_ordered_list_buffer_data() - Populate all properties of an
347  * instance under ordered list attribute
348  *
349  * @buffer_ptr: Buffer pointer
350  * @buffer_size: Buffer size
351  * @instance_id: The instance to enumerate
352  * @attr_name_kobj: The parent kernel object
353  */
354 int hp_populate_ordered_list_buffer_data(u8 *buffer_ptr, u32 *buffer_size, int instance_id,
355 					 struct kobject *attr_name_kobj)
356 {
357 	struct ordered_list_data *ordered_list_data = &bioscfg_drv.ordered_list_data[instance_id];
358 	int ret = 0;
359 
360 	ordered_list_data->attr_name_kobj = attr_name_kobj;
361 
362 	/* Populate ordered list elements */
363 	ret = hp_populate_ordered_list_elements_from_buffer(buffer_ptr, buffer_size,
364 							    instance_id);
365 	if (ret < 0)
366 		return ret;
367 
368 	hp_update_attribute_permissions(ordered_list_data->common.is_readonly,
369 					&ordered_list_current_val);
370 	hp_friendly_user_name_update(ordered_list_data->common.path,
371 				     attr_name_kobj->name,
372 				     ordered_list_data->common.display_name,
373 				     sizeof(ordered_list_data->common.display_name));
374 
375 	return sysfs_create_group(attr_name_kobj, &ordered_list_attr_group);
376 }
377 
378 /**
379  * hp_exit_ordered_list_attributes() - Clear all attribute data
380  *
381  * Clears all data allocated for this group of attributes
382  */
383 void hp_exit_ordered_list_attributes(void)
384 {
385 	int instance_id;
386 
387 	for (instance_id = 0; instance_id < bioscfg_drv.ordered_list_instances_count;
388 	     instance_id++) {
389 		struct kobject *attr_name_kobj =
390 			bioscfg_drv.ordered_list_data[instance_id].attr_name_kobj;
391 
392 		if (attr_name_kobj)
393 			sysfs_remove_group(attr_name_kobj,
394 					   &ordered_list_attr_group);
395 	}
396 	bioscfg_drv.ordered_list_instances_count = 0;
397 
398 	kvfree(bioscfg_drv.ordered_list_data);
399 	bioscfg_drv.ordered_list_data = NULL;
400 }
401