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, 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 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 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 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_err("Error expected type %d for elem %d, but got type %d instead\n", 167 expected_enum_types[eloc], elem, enum_obj[elem].type); 168 kfree(str_value); 169 return -EIO; 170 } 171 172 /* Assign appropriate element value to corresponding field */ 173 switch (eloc) { 174 case NAME: 175 case VALUE: 176 break; 177 case PATH: 178 strscpy(enum_data->common.path, str_value); 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 + reqs >= 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], str_value); 226 227 kfree(str_value); 228 str_value = NULL; 229 } 230 break; 231 232 case SECURITY_LEVEL: 233 enum_data->common.security_level = int_value; 234 break; 235 236 case ENUM_CURRENT_VALUE: 237 strscpy(enum_data->current_value, str_value); 238 break; 239 case ENUM_SIZE: 240 if (int_value > MAX_VALUES_SIZE) { 241 pr_warn("Possible number values size value exceeded the maximum number of elements supported or data may be malformed\n"); 242 int_value = MAX_VALUES_SIZE; 243 } 244 enum_data->possible_values_size = int_value; 245 246 /* 247 * This step is needed to keep the expected 248 * element list pointing to the right obj[elem].type 249 * when the size is zero. POSSIBLE_VALUES 250 * object is omitted by BIOS when the size is zero. 251 */ 252 if (int_value == 0) 253 eloc++; 254 break; 255 256 case ENUM_POSSIBLE_VALUES: 257 size = enum_data->possible_values_size; 258 259 for (pos_values = 0; pos_values < size && pos_values < MAX_VALUES_SIZE; 260 pos_values++) { 261 if (elem + pos_values >= enum_obj_count) { 262 pr_err("Error enum-objects package is too small\n"); 263 return -EINVAL; 264 } 265 266 ret = hp_convert_hexstr_to_str(enum_obj[elem + pos_values].string.pointer, 267 enum_obj[elem + pos_values].string.length, 268 &str_value, &value_len); 269 270 if (ret) 271 return -EINVAL; 272 273 /* 274 * ignore strings when possible values size 275 * is greater than MAX_VALUES_SIZE 276 */ 277 if (size < MAX_VALUES_SIZE) 278 strscpy(enum_data->possible_values[pos_values], str_value); 279 280 kfree(str_value); 281 str_value = NULL; 282 } 283 break; 284 default: 285 pr_warn("Invalid element: %d found in Enumeration attribute or data may be malformed\n", elem); 286 break; 287 } 288 289 kfree(str_value); 290 str_value = NULL; 291 } 292 293 exit_enumeration_package: 294 kfree(str_value); 295 return 0; 296 } 297 298 /** 299 * hp_populate_enumeration_package_data() - 300 * Populate all properties of an instance under enumeration attribute 301 * 302 * @enum_obj: ACPI object with enumeration data 303 * @instance_id: The instance to enumerate 304 * @attr_name_kobj: The parent kernel object 305 */ 306 int hp_populate_enumeration_package_data(union acpi_object *enum_obj, 307 int instance_id, 308 struct kobject *attr_name_kobj) 309 { 310 struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id]; 311 312 enum_data->attr_name_kobj = attr_name_kobj; 313 314 hp_populate_enumeration_elements_from_package(enum_obj, 315 enum_obj->package.count, 316 instance_id); 317 hp_update_attribute_permissions(enum_data->common.is_readonly, 318 &enumeration_current_val); 319 /* 320 * Several attributes have names such "MONDAY". Friendly 321 * user nane is generated to make the name more descriptive 322 */ 323 hp_friendly_user_name_update(enum_data->common.path, 324 attr_name_kobj->name, 325 enum_data->common.display_name, 326 sizeof(enum_data->common.display_name)); 327 return sysfs_create_group(attr_name_kobj, &enumeration_attr_group); 328 } 329 330 static int hp_populate_enumeration_elements_from_buffer(u8 *buffer_ptr, u32 *buffer_size, 331 int instance_id) 332 { 333 int values; 334 struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id]; 335 int ret = 0; 336 337 /* 338 * Only data relevant to this driver and its functionality is 339 * read. BIOS defines the order in which each * element is 340 * read. Element 0 data is not relevant to this 341 * driver hence it is ignored. For clarity, all element names 342 * (DISPLAY_IN_UI) which defines the order in which is read 343 * and the name matches the variable where the data is stored. 344 * 345 * In earlier implementation, reported errors were ignored 346 * causing the data to remain uninitialized. It is not 347 * possible to determine if data read from BIOS is valid or 348 * not. It is for this reason functions may return a error 349 * without validating the data itself. 350 */ 351 352 // VALUE: 353 ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, enum_data->current_value, 354 sizeof(enum_data->current_value)); 355 if (ret < 0) 356 goto buffer_exit; 357 358 // COMMON: 359 ret = hp_get_common_data_from_buffer(&buffer_ptr, buffer_size, &enum_data->common); 360 if (ret < 0) 361 goto buffer_exit; 362 363 // ENUM_CURRENT_VALUE: 364 ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, 365 enum_data->current_value, 366 sizeof(enum_data->current_value)); 367 if (ret < 0) 368 goto buffer_exit; 369 370 // ENUM_SIZE: 371 ret = hp_get_integer_from_buffer(&buffer_ptr, buffer_size, 372 &enum_data->possible_values_size); 373 374 if (enum_data->possible_values_size > MAX_VALUES_SIZE) { 375 /* Report a message and limit possible values size to maximum value */ 376 pr_warn("Enum Possible size value exceeded the maximum number of elements supported or data may be malformed\n"); 377 enum_data->possible_values_size = MAX_VALUES_SIZE; 378 } 379 380 // ENUM_POSSIBLE_VALUES: 381 for (values = 0; values < enum_data->possible_values_size; values++) { 382 ret = hp_get_string_from_buffer(&buffer_ptr, buffer_size, 383 enum_data->possible_values[values], 384 sizeof(enum_data->possible_values[values])); 385 if (ret < 0) 386 break; 387 } 388 389 buffer_exit: 390 return ret; 391 } 392 393 /** 394 * hp_populate_enumeration_buffer_data() - 395 * Populate all properties of an instance under enumeration attribute 396 * 397 * @buffer_ptr: Buffer pointer 398 * @buffer_size: Buffer size 399 * @instance_id: The instance to enumerate 400 * @attr_name_kobj: The parent kernel object 401 */ 402 int hp_populate_enumeration_buffer_data(u8 *buffer_ptr, u32 *buffer_size, 403 int instance_id, 404 struct kobject *attr_name_kobj) 405 { 406 struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id]; 407 int ret = 0; 408 409 enum_data->attr_name_kobj = attr_name_kobj; 410 411 /* Populate enumeration elements */ 412 ret = hp_populate_enumeration_elements_from_buffer(buffer_ptr, buffer_size, 413 instance_id); 414 if (ret < 0) 415 return ret; 416 417 hp_update_attribute_permissions(enum_data->common.is_readonly, 418 &enumeration_current_val); 419 /* 420 * Several attributes have names such "MONDAY". A Friendlier 421 * user nane is generated to make the name more descriptive 422 */ 423 hp_friendly_user_name_update(enum_data->common.path, 424 attr_name_kobj->name, 425 enum_data->common.display_name, 426 sizeof(enum_data->common.display_name)); 427 428 return sysfs_create_group(attr_name_kobj, &enumeration_attr_group); 429 } 430 431 /** 432 * hp_exit_enumeration_attributes() - Clear all attribute data 433 * 434 * Clears all data allocated for this group of attributes 435 */ 436 void hp_exit_enumeration_attributes(void) 437 { 438 int instance_id; 439 440 for (instance_id = 0; instance_id < bioscfg_drv.enumeration_instances_count; 441 instance_id++) { 442 struct enumeration_data *enum_data = &bioscfg_drv.enumeration_data[instance_id]; 443 struct kobject *attr_name_kobj = enum_data->attr_name_kobj; 444 445 if (attr_name_kobj) 446 sysfs_remove_group(attr_name_kobj, &enumeration_attr_group); 447 } 448 bioscfg_drv.enumeration_instances_count = 0; 449 450 kvfree(bioscfg_drv.enumeration_data); 451 bioscfg_drv.enumeration_data = NULL; 452 } 453