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