1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Functions corresponding to integer type attributes under 4 * BIOS Enumeration GUID for use with hp-bioscfg driver. 5 * 6 * Copyright (c) 2022 Hewlett-Packard Inc. 7 */ 8 9 #include "bioscfg.h" 10 11 GET_INSTANCE_ID(integer); 12 13 static ssize_t current_value_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 14 { 15 int instance_id = get_integer_instance_id(kobj); 16 17 if (instance_id < 0) 18 return -EIO; 19 20 return sysfs_emit(buf, "%d\n", 21 bioscfg_drv.integer_data[instance_id].current_value); 22 } 23 24 /** 25 * validate_integer_input() - 26 * Validate input of current_value against lower and upper bound 27 * 28 * @instance_id: The instance on which input is validated 29 * @buf: Input value 30 */ 31 static int validate_integer_input(int instance_id, char *buf) 32 { 33 int in_val; 34 int ret; 35 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id]; 36 37 /* BIOS treats it as a read only attribute */ 38 if (integer_data->common.is_readonly) 39 return -EIO; 40 41 ret = kstrtoint(buf, 10, &in_val); 42 if (ret < 0) 43 return ret; 44 45 if (in_val < integer_data->lower_bound || 46 in_val > integer_data->upper_bound) 47 return -ERANGE; 48 49 return 0; 50 } 51 52 static void update_integer_value(int instance_id, char *attr_value) 53 { 54 int in_val; 55 int ret; 56 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id]; 57 58 ret = kstrtoint(attr_value, 10, &in_val); 59 if (ret == 0) 60 integer_data->current_value = in_val; 61 else 62 pr_warn("Invalid integer value found: %s\n", attr_value); 63 } 64 65 ATTRIBUTE_S_COMMON_PROPERTY_SHOW(display_name, integer); 66 static struct kobj_attribute integer_display_name = 67 __ATTR_RO(display_name); 68 69 ATTRIBUTE_PROPERTY_STORE(current_value, integer); 70 static struct kobj_attribute integer_current_val = 71 __ATTR_RW_MODE(current_value, 0644); 72 73 ATTRIBUTE_N_PROPERTY_SHOW(lower_bound, integer); 74 static struct kobj_attribute integer_lower_bound = 75 __ATTR_RO(lower_bound); 76 77 ATTRIBUTE_N_PROPERTY_SHOW(upper_bound, integer); 78 static struct kobj_attribute integer_upper_bound = 79 __ATTR_RO(upper_bound); 80 81 ATTRIBUTE_N_PROPERTY_SHOW(scalar_increment, integer); 82 static struct kobj_attribute integer_scalar_increment = 83 __ATTR_RO(scalar_increment); 84 85 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, 86 char *buf) 87 { 88 return sysfs_emit(buf, "integer\n"); 89 } 90 91 static struct kobj_attribute integer_type = 92 __ATTR_RO(type); 93 94 static struct attribute *integer_attrs[] = { 95 &common_display_langcode.attr, 96 &integer_display_name.attr, 97 &integer_current_val.attr, 98 &integer_lower_bound.attr, 99 &integer_upper_bound.attr, 100 &integer_scalar_increment.attr, 101 &integer_type.attr, 102 NULL 103 }; 104 105 static const struct attribute_group integer_attr_group = { 106 .attrs = integer_attrs, 107 }; 108 109 int hp_alloc_integer_data(void) 110 { 111 bioscfg_drv.integer_instances_count = hp_get_instance_count(HP_WMI_BIOS_INTEGER_GUID); 112 bioscfg_drv.integer_data = kcalloc(bioscfg_drv.integer_instances_count, 113 sizeof(*bioscfg_drv.integer_data), GFP_KERNEL); 114 115 if (!bioscfg_drv.integer_data) { 116 bioscfg_drv.integer_instances_count = 0; 117 return -ENOMEM; 118 } 119 return 0; 120 } 121 122 /* Expected Values types associated with each element */ 123 static const acpi_object_type expected_integer_types[] = { 124 [NAME] = ACPI_TYPE_STRING, 125 [VALUE] = ACPI_TYPE_STRING, 126 [PATH] = ACPI_TYPE_STRING, 127 [IS_READONLY] = ACPI_TYPE_INTEGER, 128 [DISPLAY_IN_UI] = ACPI_TYPE_INTEGER, 129 [REQUIRES_PHYSICAL_PRESENCE] = ACPI_TYPE_INTEGER, 130 [SEQUENCE] = ACPI_TYPE_INTEGER, 131 [PREREQUISITES_SIZE] = ACPI_TYPE_INTEGER, 132 [PREREQUISITES] = ACPI_TYPE_STRING, 133 [SECURITY_LEVEL] = ACPI_TYPE_INTEGER, 134 [INT_LOWER_BOUND] = ACPI_TYPE_INTEGER, 135 [INT_UPPER_BOUND] = ACPI_TYPE_INTEGER, 136 [INT_SCALAR_INCREMENT] = ACPI_TYPE_INTEGER, 137 }; 138 139 static int hp_populate_integer_elements_from_package(union acpi_object *integer_obj, 140 int integer_obj_count, 141 int instance_id) 142 { 143 char *str_value = NULL; 144 int value_len; 145 int ret; 146 u32 int_value = 0; 147 int elem; 148 int reqs; 149 int eloc; 150 int size; 151 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id]; 152 153 if (!integer_obj) 154 return -EINVAL; 155 156 for (elem = 1, eloc = 1; elem < integer_obj_count; elem++, eloc++) { 157 /* ONLY look at the first INTEGER_ELEM_CNT elements */ 158 if (eloc == INT_ELEM_CNT) 159 goto exit_integer_package; 160 161 switch (integer_obj[elem].type) { 162 case ACPI_TYPE_STRING: 163 if (elem != PREREQUISITES) { 164 ret = hp_convert_hexstr_to_str(integer_obj[elem].string.pointer, 165 integer_obj[elem].string.length, 166 &str_value, &value_len); 167 if (ret) 168 continue; 169 } 170 break; 171 case ACPI_TYPE_INTEGER: 172 int_value = (u32)integer_obj[elem].integer.value; 173 break; 174 default: 175 pr_warn("Unsupported object type [%d]\n", integer_obj[elem].type); 176 continue; 177 } 178 /* Check that both expected and read object type match */ 179 if (expected_integer_types[eloc] != integer_obj[elem].type) { 180 pr_err("Error expected type %d for elem %d, but got type %d instead\n", 181 expected_integer_types[eloc], elem, integer_obj[elem].type); 182 kfree(str_value); 183 return -EIO; 184 } 185 /* Assign appropriate element value to corresponding field*/ 186 switch (eloc) { 187 case VALUE: 188 ret = kstrtoint(str_value, 10, &int_value); 189 if (ret) 190 continue; 191 192 integer_data->current_value = int_value; 193 break; 194 case PATH: 195 strscpy(integer_data->common.path, str_value); 196 break; 197 case IS_READONLY: 198 integer_data->common.is_readonly = int_value; 199 break; 200 case DISPLAY_IN_UI: 201 integer_data->common.display_in_ui = int_value; 202 break; 203 case REQUIRES_PHYSICAL_PRESENCE: 204 integer_data->common.requires_physical_presence = int_value; 205 break; 206 case SEQUENCE: 207 integer_data->common.sequence = int_value; 208 break; 209 case PREREQUISITES_SIZE: 210 if (int_value > MAX_PREREQUISITES_SIZE) { 211 pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n"); 212 int_value = MAX_PREREQUISITES_SIZE; 213 } 214 integer_data->common.prerequisites_size = int_value; 215 216 /* 217 * This step is needed to keep the expected 218 * element list pointing to the right obj[elem].type 219 * when the size is zero. PREREQUISITES 220 * object is omitted by BIOS when the size is 221 * zero. 222 */ 223 if (integer_data->common.prerequisites_size == 0) 224 eloc++; 225 break; 226 case PREREQUISITES: 227 size = min_t(u32, integer_data->common.prerequisites_size, MAX_PREREQUISITES_SIZE); 228 229 for (reqs = 0; reqs < size; reqs++) { 230 if (elem >= integer_obj_count) { 231 pr_err("Error elem-objects package is too small\n"); 232 return -EINVAL; 233 } 234 235 ret = hp_convert_hexstr_to_str(integer_obj[elem + reqs].string.pointer, 236 integer_obj[elem + reqs].string.length, 237 &str_value, &value_len); 238 239 if (ret) 240 continue; 241 242 strscpy(integer_data->common.prerequisites[reqs], str_value); 243 kfree(str_value); 244 str_value = NULL; 245 } 246 break; 247 248 case SECURITY_LEVEL: 249 integer_data->common.security_level = int_value; 250 break; 251 case INT_LOWER_BOUND: 252 integer_data->lower_bound = int_value; 253 break; 254 case INT_UPPER_BOUND: 255 integer_data->upper_bound = int_value; 256 break; 257 case INT_SCALAR_INCREMENT: 258 integer_data->scalar_increment = int_value; 259 break; 260 default: 261 pr_warn("Invalid element: %d found in Integer attribute or data may be malformed\n", elem); 262 break; 263 } 264 265 kfree(str_value); 266 str_value = NULL; 267 } 268 exit_integer_package: 269 kfree(str_value); 270 return 0; 271 } 272 273 /** 274 * hp_populate_integer_package_data() - 275 * Populate all properties of an instance under integer attribute 276 * 277 * @integer_obj: ACPI object with integer data 278 * @instance_id: The instance to enumerate 279 * @attr_name_kobj: The parent kernel object 280 */ 281 int hp_populate_integer_package_data(union acpi_object *integer_obj, 282 int instance_id, 283 struct kobject *attr_name_kobj) 284 { 285 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id]; 286 287 integer_data->attr_name_kobj = attr_name_kobj; 288 hp_populate_integer_elements_from_package(integer_obj, 289 integer_obj->package.count, 290 instance_id); 291 hp_update_attribute_permissions(integer_data->common.is_readonly, 292 &integer_current_val); 293 hp_friendly_user_name_update(integer_data->common.path, 294 attr_name_kobj->name, 295 integer_data->common.display_name, 296 sizeof(integer_data->common.display_name)); 297 return sysfs_create_group(attr_name_kobj, &integer_attr_group); 298 } 299 300 static int hp_populate_integer_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size, 301 int instance_id) 302 { 303 char *dst = NULL; 304 int dst_size = *buffer_size / sizeof(u16); 305 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id]; 306 int ret = 0; 307 308 dst = kcalloc(dst_size, sizeof(char), GFP_KERNEL); 309 if (!dst) 310 return -ENOMEM; 311 312 /* 313 * Only data relevant to this driver and its functionality is 314 * read. BIOS defines the order in which each * element is 315 * read. Element 0 data is not relevant to this 316 * driver hence it is ignored. For clarity, all element names 317 * (DISPLAY_IN_UI) which defines the order in which is read 318 * and the name matches the variable where the data is stored. 319 * 320 * In earlier implementation, reported errors were ignored 321 * causing the data to remain uninitialized. It is not 322 * possible to determine if data read from BIOS is valid or 323 * not. It is for this reason functions may return a error 324 * without validating the data itself. 325 */ 326 327 // VALUE: 328 integer_data->current_value = 0; 329 330 hp_get_string_from_buffer(&buffer_ptr, buffer_size, dst, dst_size); 331 ret = kstrtoint(dst, 10, &integer_data->current_value); 332 if (ret) 333 pr_warn("Unable to convert string to integer: %s\n", dst); 334 kfree(dst); 335 336 // COMMON: 337 ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size, &integer_data->common); 338 if (ret < 0) 339 goto buffer_exit; 340 341 // INT_LOWER_BOUND: 342 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size, 343 &integer_data->lower_bound); 344 if (ret < 0) 345 goto buffer_exit; 346 347 // INT_UPPER_BOUND: 348 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size, 349 &integer_data->upper_bound); 350 if (ret < 0) 351 goto buffer_exit; 352 353 // INT_SCALAR_INCREMENT: 354 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size, 355 &integer_data->scalar_increment); 356 357 buffer_exit: 358 return ret; 359 } 360 361 /** 362 * hp_populate_integer_buffer_data() - 363 * Populate all properties of an instance under integer attribute 364 * 365 * @buffer_ptr: Buffer pointer 366 * @buffer_size: Buffer size 367 * @instance_id: The instance to enumerate 368 * @attr_name_kobj: The parent kernel object 369 */ 370 int hp_populate_integer_buffer_data(u8 *buffer_ptr, u32 *buffer_size, int instance_id, 371 struct kobject *attr_name_kobj) 372 { 373 struct integer_data *integer_data = &bioscfg_drv.integer_data[instance_id]; 374 int ret = 0; 375 376 integer_data->attr_name_kobj = attr_name_kobj; 377 378 /* Populate integer elements */ 379 ret = hp_populate_integer_elements_from_buffer(buffer_ptr, buffer_size, 380 instance_id); 381 if (ret < 0) 382 return ret; 383 384 hp_update_attribute_permissions(integer_data->common.is_readonly, 385 &integer_current_val); 386 hp_friendly_user_name_update(integer_data->common.path, 387 attr_name_kobj->name, 388 integer_data->common.display_name, 389 sizeof(integer_data->common.display_name)); 390 391 return sysfs_create_group(attr_name_kobj, &integer_attr_group); 392 } 393 394 /** 395 * hp_exit_integer_attributes() - Clear all attribute data 396 * 397 * Clears all data allocated for this group of attributes 398 */ 399 void hp_exit_integer_attributes(void) 400 { 401 int instance_id; 402 403 for (instance_id = 0; instance_id < bioscfg_drv.integer_instances_count; 404 instance_id++) { 405 struct kobject *attr_name_kobj = 406 bioscfg_drv.integer_data[instance_id].attr_name_kobj; 407 408 if (attr_name_kobj) 409 sysfs_remove_group(attr_name_kobj, &integer_attr_group); 410 } 411 bioscfg_drv.integer_instances_count = 0; 412 413 kfree(bioscfg_drv.integer_data); 414 bioscfg_drv.integer_data = NULL; 415 } 416