xref: /linux/drivers/platform/x86/hp/hp-bioscfg/enum-attributes.c (revision 5b05bb3f6c5716fab6911e12d60dd1f43ad9806a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions corresponding to enumeration type attributes under
4  * BIOS Enumeration 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(enumeration);
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_enumeration_instance_id(kobj);
16 
17 	if (instance_id < 0)
18 		return -EIO;
19 
20 	return sysfs_emit(buf, "%s\n",
21 			 bioscfg_drv.enumeration_data[instance_id].current_value);
22 }
23 
24 /**
25  * validate_enumeration_input() -
26  * Validate input of current_value against possible values
27  *
28  * @instance_id: The instance on which input is validated
29  * @buf: Input value
30  */
validate_enumeration_input(int instance_id,const char * buf)31 static int validate_enumeration_input(int instance_id, const char *buf)
32 {
33 	int i;
34 	int found = 0;
35 	struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
36 
37 	/* Is it a read only attribute */
38 	if (enum_data->common.is_readonly)
39 		return -EIO;
40 
41 	for (i = 0; i < enum_data->possible_values_size && !found; i++)
42 		if (!strcmp(enum_data->possible_values[i], buf))
43 			found = 1;
44 
45 	if (!found)
46 		return -EINVAL;
47 
48 	return 0;
49 }
50 
update_enumeration_value(int instance_id,char * attr_value)51 static void update_enumeration_value(int instance_id, char *attr_value)
52 {
53 	struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
54 
55 	strscpy(enum_data->current_value, attr_value);
56 }
57 
58 ATTRIBUTE_S_COMMON_PROPERTY_SHOW(display_name, enumeration);
59 static struct kobj_attribute enumeration_display_name =
60 		__ATTR_RO(display_name);
61 
62 ATTRIBUTE_PROPERTY_STORE(current_value, enumeration);
63 static struct kobj_attribute enumeration_current_val =
64 		__ATTR_RW(current_value);
65 
66 ATTRIBUTE_VALUES_PROPERTY_SHOW(possible_values, enumeration, SEMICOLON_SEP);
67 static struct kobj_attribute enumeration_poss_val =
68 		__ATTR_RO(possible_values);
69 
type_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)70 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
71 			 char *buf)
72 {
73 	return sysfs_emit(buf, "enumeration\n");
74 }
75 
76 static struct kobj_attribute enumeration_type =
77 		__ATTR_RO(type);
78 
79 static struct attribute *enumeration_attrs[] = {
80 	&common_display_langcode.attr,
81 	&enumeration_display_name.attr,
82 	&enumeration_current_val.attr,
83 	&enumeration_poss_val.attr,
84 	&enumeration_type.attr,
85 	NULL
86 };
87 
88 static const struct attribute_group enumeration_attr_group = {
89 	.attrs = enumeration_attrs,
90 };
91 
hp_alloc_enumeration_data(void)92 int hp_alloc_enumeration_data(void)
93 {
94 	bioscfg_drv.enumeration_instances_count =
95 		hp_get_instance_count(HP_WMI_BIOS_ENUMERATION_GUID);
96 
97 	if (!bioscfg_drv.enumeration_instances_count)
98 		return -EINVAL;
99 	bioscfg_drv.enumeration_data = kvcalloc(bioscfg_drv.enumeration_instances_count,
100 						sizeof(*bioscfg_drv.enumeration_data), GFP_KERNEL);
101 
102 	if (!bioscfg_drv.enumeration_data) {
103 		bioscfg_drv.enumeration_instances_count = 0;
104 		return -ENOMEM;
105 	}
106 	return 0;
107 }
108 
109 /* Expected Values types associated with each element */
110 static const acpi_object_type expected_enum_types[] = {
111 	[NAME] = ACPI_TYPE_STRING,
112 	[VALUE] = ACPI_TYPE_STRING,
113 	[PATH] = ACPI_TYPE_STRING,
114 	[IS_READONLY] = ACPI_TYPE_INTEGER,
115 	[DISPLAY_IN_UI] = ACPI_TYPE_INTEGER,
116 	[REQUIRES_PHYSICAL_PRESENCE] = ACPI_TYPE_INTEGER,
117 	[SEQUENCE] = ACPI_TYPE_INTEGER,
118 	[PREREQUISITES_SIZE] = ACPI_TYPE_INTEGER,
119 	[PREREQUISITES] = ACPI_TYPE_STRING,
120 	[SECURITY_LEVEL] = ACPI_TYPE_INTEGER,
121 	[ENUM_CURRENT_VALUE] = ACPI_TYPE_STRING,
122 	[ENUM_SIZE] = ACPI_TYPE_INTEGER,
123 	[ENUM_POSSIBLE_VALUES] = ACPI_TYPE_STRING,
124 };
125 
hp_populate_enumeration_elements_from_package(union acpi_object * enum_obj,int enum_obj_count,int instance_id)126 static int hp_populate_enumeration_elements_from_package(union acpi_object *enum_obj,
127 							 int enum_obj_count,
128 							 int instance_id)
129 {
130 	char *str_value = NULL;
131 	int value_len;
132 	u32 size = 0;
133 	u32 int_value = 0;
134 	int elem = 0;
135 	int reqs;
136 	int pos_values;
137 	int ret;
138 	int eloc;
139 	struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
140 
141 	for (elem = 1, eloc = 1; elem < enum_obj_count; elem++, eloc++) {
142 		/* ONLY look at the first ENUM_ELEM_CNT elements */
143 		if (eloc == ENUM_ELEM_CNT)
144 			goto exit_enumeration_package;
145 
146 		switch (enum_obj[elem].type) {
147 		case ACPI_TYPE_STRING:
148 			if (PREREQUISITES != elem && ENUM_POSSIBLE_VALUES != elem) {
149 				ret = hp_convert_hexstr_to_str(enum_obj[elem].string.pointer,
150 							       enum_obj[elem].string.length,
151 							       &str_value, &value_len);
152 				if (ret)
153 					return -EINVAL;
154 			}
155 			break;
156 		case ACPI_TYPE_INTEGER:
157 			int_value = (u32)enum_obj[elem].integer.value;
158 			break;
159 		default:
160 			pr_warn("Unsupported object type [%d]\n", enum_obj[elem].type);
161 			continue;
162 		}
163 
164 		/* Check that both expected and read object type match */
165 		if (expected_enum_types[eloc] != enum_obj[elem].type) {
166 			pr_warn("Unexpected element type at elem %d: expected %d, got %d, skipping\n",
167 				elem, expected_enum_types[eloc], enum_obj[elem].type);
168 			kfree(str_value);
169 			str_value = NULL;
170 			continue;
171 		}
172 
173 		/* Assign appropriate element value to corresponding field */
174 		switch (eloc) {
175 		case NAME:
176 		case VALUE:
177 			break;
178 		case PATH:
179 			strscpy(enum_data->common.path, str_value);
180 			break;
181 		case IS_READONLY:
182 			enum_data->common.is_readonly = int_value;
183 			break;
184 		case DISPLAY_IN_UI:
185 			enum_data->common.display_in_ui = int_value;
186 			break;
187 		case REQUIRES_PHYSICAL_PRESENCE:
188 			enum_data->common.requires_physical_presence = int_value;
189 			break;
190 		case SEQUENCE:
191 			enum_data->common.sequence = int_value;
192 			break;
193 		case PREREQUISITES_SIZE:
194 			if (int_value > MAX_PREREQUISITES_SIZE) {
195 				pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n");
196 				int_value = MAX_PREREQUISITES_SIZE;
197 			}
198 			enum_data->common.prerequisites_size = int_value;
199 
200 			/*
201 			 * This step is needed to keep the expected
202 			 * element list pointing to the right obj[elem].type
203 			 * when the size is zero. PREREQUISITES
204 			 * object is omitted by BIOS when the size is
205 			 * zero.
206 			 */
207 			if (int_value == 0)
208 				eloc++;
209 			break;
210 
211 		case PREREQUISITES:
212 			size = min_t(u32, enum_data->common.prerequisites_size, MAX_PREREQUISITES_SIZE);
213 			for (reqs = 0; reqs < size; reqs++) {
214 				if (elem + reqs >= enum_obj_count) {
215 					pr_err("Error enum-objects package is too small\n");
216 					return -EINVAL;
217 				}
218 
219 				ret = hp_convert_hexstr_to_str(enum_obj[elem + reqs].string.pointer,
220 							       enum_obj[elem + reqs].string.length,
221 							       &str_value, &value_len);
222 
223 				if (ret)
224 					return -EINVAL;
225 
226 				strscpy(enum_data->common.prerequisites[reqs], str_value);
227 
228 				kfree(str_value);
229 				str_value = NULL;
230 			}
231 			if (size)
232 				elem += size - 1;
233 			break;
234 
235 		case SECURITY_LEVEL:
236 			enum_data->common.security_level = int_value;
237 			break;
238 
239 		case ENUM_CURRENT_VALUE:
240 			strscpy(enum_data->current_value, str_value);
241 			break;
242 		case ENUM_SIZE:
243 			if (int_value > MAX_VALUES_SIZE) {
244 				pr_warn("Possible number values size value exceeded the maximum number of elements supported or data may be malformed\n");
245 				int_value = MAX_VALUES_SIZE;
246 			}
247 			enum_data->possible_values_size = int_value;
248 
249 			/*
250 			 * This step is needed to keep the expected
251 			 * element list pointing to the right obj[elem].type
252 			 * when the size is zero. POSSIBLE_VALUES
253 			 * object is omitted by BIOS when the size is zero.
254 			 */
255 			if (int_value == 0)
256 				eloc++;
257 			break;
258 
259 		case ENUM_POSSIBLE_VALUES:
260 			size = enum_data->possible_values_size;
261 
262 			for (pos_values = 0; pos_values < size && pos_values < MAX_VALUES_SIZE;
263 			     pos_values++) {
264 				if (elem + pos_values >= enum_obj_count) {
265 					pr_err("Error enum-objects package is too small\n");
266 					return -EINVAL;
267 				}
268 
269 				ret = hp_convert_hexstr_to_str(enum_obj[elem + pos_values].string.pointer,
270 							       enum_obj[elem + pos_values].string.length,
271 							       &str_value, &value_len);
272 
273 				if (ret)
274 					return -EINVAL;
275 
276 				/*
277 				 * ignore strings when possible values size
278 				 * is greater than MAX_VALUES_SIZE
279 				 */
280 				if (size < MAX_VALUES_SIZE)
281 					strscpy(enum_data->possible_values[pos_values], str_value);
282 
283 				kfree(str_value);
284 				str_value = NULL;
285 			}
286 			if (size)
287 				elem += (size < MAX_VALUES_SIZE ? size : MAX_VALUES_SIZE) - 1;
288 			break;
289 		default:
290 			pr_warn("Invalid element: %d found in Enumeration attribute or data may be malformed\n", elem);
291 			break;
292 		}
293 
294 		kfree(str_value);
295 		str_value = NULL;
296 	}
297 
298 exit_enumeration_package:
299 	kfree(str_value);
300 	return 0;
301 }
302 
303 /**
304  * hp_populate_enumeration_package_data() -
305  * Populate all properties of an instance under enumeration attribute
306  *
307  * @enum_obj: ACPI object with enumeration data
308  * @enum_obj_count: Number of elements in @enum_obj
309  * @instance_id: The instance to enumerate
310  * @attr_name_kobj: The parent kernel object
311  */
hp_populate_enumeration_package_data(union acpi_object * enum_obj,int enum_obj_count,int instance_id,struct kobject * attr_name_kobj)312 int hp_populate_enumeration_package_data(union acpi_object *enum_obj,
313 					 int enum_obj_count,
314 					 int instance_id,
315 					 struct kobject *attr_name_kobj)
316 {
317 	struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
318 
319 	enum_data->attr_name_kobj = attr_name_kobj;
320 
321 	hp_populate_enumeration_elements_from_package(enum_obj,
322 						      enum_obj_count,
323 						      instance_id);
324 	hp_update_attribute_permissions(enum_data->common.is_readonly,
325 					&enumeration_current_val);
326 	/*
327 	 * Several attributes have names such "MONDAY". Friendly
328 	 * user nane is generated to make the name more descriptive
329 	 */
330 	hp_friendly_user_name_update(enum_data->common.path,
331 				     attr_name_kobj->name,
332 				     enum_data->common.display_name,
333 				     sizeof(enum_data->common.display_name));
334 	return sysfs_create_group(attr_name_kobj, &enumeration_attr_group);
335 }
336 
hp_populate_enumeration_elements_from_buffer(u8 * buffer_ptr,u32 * buffer_size,int instance_id)337 static int hp_populate_enumeration_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
338 							int instance_id)
339 {
340 	int values;
341 	struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
342 	int ret = 0;
343 
344 	/*
345 	 * Only data relevant to this driver and its functionality is
346 	 * read. BIOS defines the order in which each * element is
347 	 * read. Element 0 data is not relevant to this
348 	 * driver hence it is ignored. For clarity, all element names
349 	 * (DISPLAY_IN_UI) which defines the order in which is read
350 	 * and the name matches the variable where the data is stored.
351 	 *
352 	 * In earlier implementation, reported errors were ignored
353 	 * causing the data to remain uninitialized. It is not
354 	 * possible to determine if data read from BIOS is valid or
355 	 * not. It is for this reason functions may return a error
356 	 * without validating the data itself.
357 	 */
358 
359 	// VALUE:
360 	ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, enum_data->current_value,
361 					sizeof(enum_data->current_value));
362 	if (ret < 0)
363 		goto buffer_exit;
364 
365 	// COMMON:
366 	ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size, &enum_data->common);
367 	if (ret < 0)
368 		goto buffer_exit;
369 
370 	// ENUM_CURRENT_VALUE:
371 	ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size,
372 					enum_data->current_value,
373 					sizeof(enum_data->current_value));
374 	if (ret < 0)
375 		goto buffer_exit;
376 
377 	// ENUM_SIZE:
378 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
379 					 &enum_data->possible_values_size);
380 
381 	if (enum_data->possible_values_size > MAX_VALUES_SIZE) {
382 		/* Report a message and limit possible values size to maximum value */
383 		pr_warn("Enum Possible size value exceeded the maximum number of elements supported or data may be malformed\n");
384 		enum_data->possible_values_size = MAX_VALUES_SIZE;
385 	}
386 
387 	// ENUM_POSSIBLE_VALUES:
388 	for (values = 0; values < enum_data->possible_values_size; values++) {
389 		ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size,
390 						enum_data->possible_values[values],
391 						sizeof(enum_data->possible_values[values]));
392 		if (ret < 0)
393 			break;
394 	}
395 
396 buffer_exit:
397 	return ret;
398 }
399 
400 /**
401  * hp_populate_enumeration_buffer_data() -
402  * Populate all properties of an instance under enumeration attribute
403  *
404  * @buffer_ptr: Buffer pointer
405  * @buffer_size: Buffer size
406  * @instance_id: The instance to enumerate
407  * @attr_name_kobj: The parent kernel object
408  */
hp_populate_enumeration_buffer_data(u8 * buffer_ptr,u32 * buffer_size,int instance_id,struct kobject * attr_name_kobj)409 int hp_populate_enumeration_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
410 					int instance_id,
411 					struct kobject *attr_name_kobj)
412 {
413 	struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
414 	int ret = 0;
415 
416 	enum_data->attr_name_kobj = attr_name_kobj;
417 
418 	/* Populate enumeration elements */
419 	ret = hp_populate_enumeration_elements_from_buffer(buffer_ptr, buffer_size,
420 							   instance_id);
421 	if (ret < 0)
422 		return ret;
423 
424 	hp_update_attribute_permissions(enum_data->common.is_readonly,
425 					&enumeration_current_val);
426 	/*
427 	 * Several attributes have names such "MONDAY". A Friendlier
428 	 * user nane is generated to make the name more descriptive
429 	 */
430 	hp_friendly_user_name_update(enum_data->common.path,
431 				     attr_name_kobj->name,
432 				     enum_data->common.display_name,
433 				     sizeof(enum_data->common.display_name));
434 
435 	return sysfs_create_group(attr_name_kobj, &enumeration_attr_group);
436 }
437 
438 /**
439  * hp_exit_enumeration_attributes() - Clear all attribute data
440  *
441  * Clears all data allocated for this group of attributes
442  */
hp_exit_enumeration_attributes(void)443 void hp_exit_enumeration_attributes(void)
444 {
445 	int instance_id;
446 
447 	for (instance_id = 0; instance_id < bioscfg_drv.enumeration_instances_count;
448 	     instance_id++) {
449 		struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id];
450 		struct kobject *attr_name_kobj = enum_data->attr_name_kobj;
451 
452 		if (attr_name_kobj)
453 			sysfs_remove_group(attr_name_kobj, &enumeration_attr_group);
454 	}
455 	bioscfg_drv.enumeration_instances_count = 0;
456 
457 	kvfree(bioscfg_drv.enumeration_data);
458 	bioscfg_drv.enumeration_data = NULL;
459 }
460