xref: /linux/drivers/platform/x86/hp/hp-bioscfg/string-attributes.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions corresponding to string type attributes under
4  * HP_WMI_BIOS_STRING_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 #define WMI_STRING_TYPE "HPBIOS_BIOSString"
12 
13 GET_INSTANCE_ID(string);
14 
15 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
16 {
17 	int instance_id = get_string_instance_id(kobj);
18 
19 	if (instance_id < 0)
20 		return -EIO;
21 
22 	return  sysfs_emit(buf, "%s\n",
23 			 bioscfg_drv.string_data[instance_id].current_value);
24 }
25 
26 /**
27  * validate_string_input() -
28  * Validate input of current_value against min and max lengths
29  *
30  * @instance_id: The instance on which input is validated
31  * @buf: Input value
32  */
33 static int validate_string_input(int instance_id, const char *buf)
34 {
35 	int in_len = strlen(buf);
36 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
37 
38 	/* BIOS treats it as a read only attribute */
39 	if (string_data->common.is_readonly)
40 		return -EIO;
41 
42 	if (in_len < string_data->min_length || in_len > string_data->max_length)
43 		return -ERANGE;
44 
45 	return 0;
46 }
47 
48 static void update_string_value(int instance_id, char *attr_value)
49 {
50 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
51 
52 	/* Write settings to BIOS */
53 	strscpy(string_data->current_value, attr_value);
54 }
55 
56 /*
57  * ATTRIBUTE_S_COMMON_PROPERTY_SHOW(display_name_language_code, string);
58  * static struct kobj_attribute string_display_langcode =
59  *	__ATTR_RO(display_name_language_code);
60  */
61 
62 ATTRIBUTE_S_COMMON_PROPERTY_SHOW(display_name, string);
63 static struct kobj_attribute string_display_name =
64 	__ATTR_RO(display_name);
65 
66 ATTRIBUTE_PROPERTY_STORE(current_value, string);
67 static struct kobj_attribute string_current_val =
68 	__ATTR_RW_MODE(current_value, 0644);
69 
70 ATTRIBUTE_N_PROPERTY_SHOW(min_length, string);
71 static struct kobj_attribute string_min_length =
72 	__ATTR_RO(min_length);
73 
74 ATTRIBUTE_N_PROPERTY_SHOW(max_length, string);
75 static struct kobj_attribute string_max_length =
76 	__ATTR_RO(max_length);
77 
78 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr,
79 			 char *buf)
80 {
81 	return sysfs_emit(buf, "string\n");
82 }
83 
84 static struct kobj_attribute string_type =
85 	__ATTR_RO(type);
86 
87 static struct attribute *string_attrs[] = {
88 	&common_display_langcode.attr,
89 	&string_display_name.attr,
90 	&string_current_val.attr,
91 	&string_min_length.attr,
92 	&string_max_length.attr,
93 	&string_type.attr,
94 	NULL
95 };
96 
97 static const struct attribute_group string_attr_group = {
98 	.attrs = string_attrs,
99 };
100 
101 int hp_alloc_string_data(void)
102 {
103 	bioscfg_drv.string_instances_count = hp_get_instance_count(HP_WMI_BIOS_STRING_GUID);
104 	bioscfg_drv.string_data = kcalloc(bioscfg_drv.string_instances_count,
105 					  sizeof(*bioscfg_drv.string_data), GFP_KERNEL);
106 	if (!bioscfg_drv.string_data) {
107 		bioscfg_drv.string_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_string_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 	[STR_MIN_LENGTH] = ACPI_TYPE_INTEGER,
126 	[STR_MAX_LENGTH] = ACPI_TYPE_INTEGER,
127 };
128 
129 static int hp_populate_string_elements_from_package(union acpi_object *string_obj,
130 						    int string_obj_count,
131 						    int instance_id)
132 {
133 	char *str_value = NULL;
134 	int value_len;
135 	int ret = 0;
136 	u32 int_value = 0;
137 	int elem;
138 	int reqs;
139 	int eloc;
140 	int size;
141 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
142 
143 	if (!string_obj)
144 		return -EINVAL;
145 
146 	for (elem = 1, eloc = 1; elem < string_obj_count; elem++, eloc++) {
147 		/* ONLY look at the first STRING_ELEM_CNT elements */
148 		if (eloc == STR_ELEM_CNT)
149 			goto exit_string_package;
150 
151 		switch (string_obj[elem].type) {
152 		case ACPI_TYPE_STRING:
153 			if (elem != PREREQUISITES) {
154 				ret = hp_convert_hexstr_to_str(string_obj[elem].string.pointer,
155 							       string_obj[elem].string.length,
156 							       &str_value, &value_len);
157 
158 				if (ret)
159 					continue;
160 			}
161 			break;
162 		case ACPI_TYPE_INTEGER:
163 			int_value = (u32)string_obj[elem].integer.value;
164 			break;
165 		default:
166 			pr_warn("Unsupported object type [%d]\n", string_obj[elem].type);
167 			continue;
168 		}
169 
170 		/* Check that both expected and read object type match */
171 		if (expected_string_types[eloc] != string_obj[elem].type) {
172 			pr_err("Error expected type %d for elem %d, but got type %d instead\n",
173 			       expected_string_types[eloc], elem, string_obj[elem].type);
174 			kfree(str_value);
175 			return -EIO;
176 		}
177 
178 		/* Assign appropriate element value to corresponding field*/
179 		switch (eloc) {
180 		case VALUE:
181 			strscpy(string_data->current_value, str_value);
182 			break;
183 		case PATH:
184 			strscpy(string_data->common.path, str_value);
185 			break;
186 		case IS_READONLY:
187 			string_data->common.is_readonly = int_value;
188 			break;
189 		case DISPLAY_IN_UI:
190 			string_data->common.display_in_ui = int_value;
191 			break;
192 		case REQUIRES_PHYSICAL_PRESENCE:
193 			string_data->common.requires_physical_presence = int_value;
194 			break;
195 		case SEQUENCE:
196 			string_data->common.sequence = int_value;
197 			break;
198 		case PREREQUISITES_SIZE:
199 			if (int_value > MAX_PREREQUISITES_SIZE) {
200 				pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n");
201 				int_value = MAX_PREREQUISITES_SIZE;
202 			}
203 			string_data->common.prerequisites_size = int_value;
204 
205 			/*
206 			 * This step is needed to keep the expected
207 			 * element list pointing to the right obj[elem].type
208 			 * when the size is zero. PREREQUISITES
209 			 * object is omitted by BIOS when the size is
210 			 * zero.
211 			 */
212 			if (string_data->common.prerequisites_size == 0)
213 				eloc++;
214 			break;
215 		case PREREQUISITES:
216 			size = min_t(u32, string_data->common.prerequisites_size,
217 				     MAX_PREREQUISITES_SIZE);
218 
219 			for (reqs = 0; reqs < size; reqs++) {
220 				if (elem >= string_obj_count) {
221 					pr_err("Error elem-objects package is too small\n");
222 					return -EINVAL;
223 				}
224 
225 				ret = hp_convert_hexstr_to_str(string_obj[elem + reqs].string.pointer,
226 							       string_obj[elem + reqs].string.length,
227 							       &str_value, &value_len);
228 
229 				if (ret)
230 					continue;
231 
232 				strscpy(string_data->common.prerequisites[reqs], str_value);
233 				kfree(str_value);
234 				str_value = NULL;
235 			}
236 			break;
237 
238 		case SECURITY_LEVEL:
239 			string_data->common.security_level = int_value;
240 			break;
241 		case STR_MIN_LENGTH:
242 			string_data->min_length = int_value;
243 			break;
244 		case STR_MAX_LENGTH:
245 			string_data->max_length = int_value;
246 			break;
247 		default:
248 			pr_warn("Invalid element: %d found in String attribute or data may be malformed\n", elem);
249 			break;
250 		}
251 
252 		kfree(str_value);
253 		str_value = NULL;
254 	}
255 
256 exit_string_package:
257 	kfree(str_value);
258 	return 0;
259 }
260 
261 /**
262  * hp_populate_string_package_data() -
263  * Populate all properties of an instance under string attribute
264  *
265  * @string_obj: ACPI object with string data
266  * @instance_id: The instance to enumerate
267  * @attr_name_kobj: The parent kernel object
268  */
269 int hp_populate_string_package_data(union acpi_object *string_obj,
270 				    int instance_id,
271 				    struct kobject *attr_name_kobj)
272 {
273 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
274 
275 	string_data->attr_name_kobj = attr_name_kobj;
276 
277 	hp_populate_string_elements_from_package(string_obj,
278 						 string_obj->package.count,
279 						 instance_id);
280 
281 	hp_update_attribute_permissions(string_data->common.is_readonly,
282 					&string_current_val);
283 	hp_friendly_user_name_update(string_data->common.path,
284 				     attr_name_kobj->name,
285 				     string_data->common.display_name,
286 				     sizeof(string_data->common.display_name));
287 	return sysfs_create_group(attr_name_kobj, &string_attr_group);
288 }
289 
290 static int hp_populate_string_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
291 						   int instance_id)
292 {
293 	int ret = 0;
294 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
295 
296 	/*
297 	 * Only data relevant to this driver and its functionality is
298 	 * read. BIOS defines the order in which each * element is
299 	 * read. Element 0 data is not relevant to this
300 	 * driver hence it is ignored. For clarity, all element names
301 	 * (DISPLAY_IN_UI) which defines the order in which is read
302 	 * and the name matches the variable where the data is stored.
303 	 *
304 	 * In earlier implementation, reported errors were ignored
305 	 * causing the data to remain uninitialized. It is not
306 	 * possible to determine if data read from BIOS is valid or
307 	 * not. It is for this reason functions may return a error
308 	 * without validating the data itself.
309 	 */
310 
311 	// VALUE:
312 	ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, string_data->current_value,
313 					sizeof(string_data->current_value));
314 	if (ret < 0)
315 		goto buffer_exit;
316 
317 	// COMMON:
318 	ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size, &string_data->common);
319 	if (ret < 0)
320 		goto buffer_exit;
321 
322 	// STR_MIN_LENGTH:
323 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
324 					 &string_data->min_length);
325 	if (ret < 0)
326 		goto buffer_exit;
327 
328 	// STR_MAX_LENGTH:
329 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
330 					 &string_data->max_length);
331 
332 buffer_exit:
333 
334 	return ret;
335 }
336 
337 /**
338  * hp_populate_string_buffer_data() -
339  * Populate all properties of an instance under string attribute
340  *
341  * @buffer_ptr: Buffer pointer
342  * @buffer_size: Buffer size
343  * @instance_id: The instance to enumerate
344  * @attr_name_kobj: The parent kernel object
345  */
346 int hp_populate_string_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
347 				   int instance_id,
348 				   struct kobject *attr_name_kobj)
349 {
350 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
351 	int ret = 0;
352 
353 	string_data->attr_name_kobj = attr_name_kobj;
354 
355 	ret = hp_populate_string_elements_from_buffer(buffer_ptr, buffer_size,
356 						      instance_id);
357 	if (ret < 0)
358 		return ret;
359 
360 	hp_update_attribute_permissions(string_data->common.is_readonly,
361 					&string_current_val);
362 	hp_friendly_user_name_update(string_data->common.path,
363 				     attr_name_kobj->name,
364 				     string_data->common.display_name,
365 				     sizeof(string_data->common.display_name));
366 
367 	return sysfs_create_group(attr_name_kobj, &string_attr_group);
368 }
369 
370 /**
371  * hp_exit_string_attributes() - Clear all attribute data
372  *
373  * Clears all data allocated for this group of attributes
374  */
375 void hp_exit_string_attributes(void)
376 {
377 	int instance_id;
378 
379 	for (instance_id = 0; instance_id < bioscfg_drv.string_instances_count;
380 	     instance_id++) {
381 		struct kobject *attr_name_kobj =
382 			bioscfg_drv.string_data[instance_id].attr_name_kobj;
383 
384 		if (attr_name_kobj)
385 			sysfs_remove_group(attr_name_kobj, &string_attr_group);
386 	}
387 	bioscfg_drv.string_instances_count = 0;
388 
389 	kfree(bioscfg_drv.string_data);
390 	bioscfg_drv.string_data = NULL;
391 }
392