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, sizeof(string_data->current_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,
182 				str_value, sizeof(string_data->current_value));
183 			break;
184 		case PATH:
185 			strscpy(string_data->common.path, str_value,
186 				sizeof(string_data->common.path));
187 			break;
188 		case IS_READONLY:
189 			string_data->common.is_readonly = int_value;
190 			break;
191 		case DISPLAY_IN_UI:
192 			string_data->common.display_in_ui = int_value;
193 			break;
194 		case REQUIRES_PHYSICAL_PRESENCE:
195 			string_data->common.requires_physical_presence = int_value;
196 			break;
197 		case SEQUENCE:
198 			string_data->common.sequence = int_value;
199 			break;
200 		case PREREQUISITES_SIZE:
201 			if (int_value > MAX_PREREQUISITES_SIZE) {
202 				pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n");
203 				int_value = MAX_PREREQUISITES_SIZE;
204 			}
205 			string_data->common.prerequisites_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. PREREQUISITES
211 			 * object is omitted by BIOS when the size is
212 			 * zero.
213 			 */
214 			if (string_data->common.prerequisites_size == 0)
215 				eloc++;
216 			break;
217 		case PREREQUISITES:
218 			size = min_t(u32, string_data->common.prerequisites_size,
219 				     MAX_PREREQUISITES_SIZE);
220 
221 			for (reqs = 0; reqs < size; reqs++) {
222 				if (elem >= string_obj_count) {
223 					pr_err("Error elem-objects package is too small\n");
224 					return -EINVAL;
225 				}
226 
227 				ret = hp_convert_hexstr_to_str(string_obj[elem + reqs].string.pointer,
228 							       string_obj[elem + reqs].string.length,
229 							       &str_value, &value_len);
230 
231 				if (ret)
232 					continue;
233 
234 				strscpy(string_data->common.prerequisites[reqs],
235 					str_value,
236 					sizeof(string_data->common.prerequisites[reqs]));
237 				kfree(str_value);
238 				str_value = NULL;
239 			}
240 			break;
241 
242 		case SECURITY_LEVEL:
243 			string_data->common.security_level = int_value;
244 			break;
245 		case STR_MIN_LENGTH:
246 			string_data->min_length = int_value;
247 			break;
248 		case STR_MAX_LENGTH:
249 			string_data->max_length = int_value;
250 			break;
251 		default:
252 			pr_warn("Invalid element: %d found in String attribute or data may be malformed\n", elem);
253 			break;
254 		}
255 
256 		kfree(str_value);
257 		str_value = NULL;
258 	}
259 
260 exit_string_package:
261 	kfree(str_value);
262 	return 0;
263 }
264 
265 /**
266  * hp_populate_string_package_data() -
267  * Populate all properties of an instance under string attribute
268  *
269  * @string_obj: ACPI object with string data
270  * @instance_id: The instance to enumerate
271  * @attr_name_kobj: The parent kernel object
272  */
273 int hp_populate_string_package_data(union acpi_object *string_obj,
274 				    int instance_id,
275 				    struct kobject *attr_name_kobj)
276 {
277 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
278 
279 	string_data->attr_name_kobj = attr_name_kobj;
280 
281 	hp_populate_string_elements_from_package(string_obj,
282 						 string_obj->package.count,
283 						 instance_id);
284 
285 	hp_update_attribute_permissions(string_data->common.is_readonly,
286 					&string_current_val);
287 	hp_friendly_user_name_update(string_data->common.path,
288 				     attr_name_kobj->name,
289 				     string_data->common.display_name,
290 				     sizeof(string_data->common.display_name));
291 	return sysfs_create_group(attr_name_kobj, &string_attr_group);
292 }
293 
294 static int hp_populate_string_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size,
295 						   int instance_id)
296 {
297 	int ret = 0;
298 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
299 
300 	/*
301 	 * Only data relevant to this driver and its functionality is
302 	 * read. BIOS defines the order in which each * element is
303 	 * read. Element 0 data is not relevant to this
304 	 * driver hence it is ignored. For clarity, all element names
305 	 * (DISPLAY_IN_UI) which defines the order in which is read
306 	 * and the name matches the variable where the data is stored.
307 	 *
308 	 * In earlier implementation, reported errors were ignored
309 	 * causing the data to remain uninitialized. It is not
310 	 * possible to determine if data read from BIOS is valid or
311 	 * not. It is for this reason functions may return a error
312 	 * without validating the data itself.
313 	 */
314 
315 	// VALUE:
316 	ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, string_data->current_value,
317 					sizeof(string_data->current_value));
318 	if (ret < 0)
319 		goto buffer_exit;
320 
321 	// COMMON:
322 	ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size, &string_data->common);
323 	if (ret < 0)
324 		goto buffer_exit;
325 
326 	// STR_MIN_LENGTH:
327 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
328 					 &string_data->min_length);
329 	if (ret < 0)
330 		goto buffer_exit;
331 
332 	// STR_MAX_LENGTH:
333 	ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size,
334 					 &string_data->max_length);
335 
336 buffer_exit:
337 
338 	return ret;
339 }
340 
341 /**
342  * hp_populate_string_buffer_data() -
343  * Populate all properties of an instance under string attribute
344  *
345  * @buffer_ptr: Buffer pointer
346  * @buffer_size: Buffer size
347  * @instance_id: The instance to enumerate
348  * @attr_name_kobj: The parent kernel object
349  */
350 int hp_populate_string_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
351 				   int instance_id,
352 				   struct kobject *attr_name_kobj)
353 {
354 	struct string_data *string_data = &bioscfg_drv.string_data[instance_id];
355 	int ret = 0;
356 
357 	string_data->attr_name_kobj = attr_name_kobj;
358 
359 	ret = hp_populate_string_elements_from_buffer(buffer_ptr, buffer_size,
360 						      instance_id);
361 	if (ret < 0)
362 		return ret;
363 
364 	hp_update_attribute_permissions(string_data->common.is_readonly,
365 					&string_current_val);
366 	hp_friendly_user_name_update(string_data->common.path,
367 				     attr_name_kobj->name,
368 				     string_data->common.display_name,
369 				     sizeof(string_data->common.display_name));
370 
371 	return sysfs_create_group(attr_name_kobj, &string_attr_group);
372 }
373 
374 /**
375  * hp_exit_string_attributes() - Clear all attribute data
376  *
377  * Clears all data allocated for this group of attributes
378  */
379 void hp_exit_string_attributes(void)
380 {
381 	int instance_id;
382 
383 	for (instance_id = 0; instance_id < bioscfg_drv.string_instances_count;
384 	     instance_id++) {
385 		struct kobject *attr_name_kobj =
386 			bioscfg_drv.string_data[instance_id].attr_name_kobj;
387 
388 		if (attr_name_kobj)
389 			sysfs_remove_group(attr_name_kobj, &string_attr_group);
390 	}
391 	bioscfg_drv.string_instances_count = 0;
392 
393 	kfree(bioscfg_drv.string_data);
394 	bioscfg_drv.string_data = NULL;
395 }
396