1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Common methods for use with hp-bioscfg driver
4 *
5 * Copyright (c) 2022 HP Development Company, L.P.
6 */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/fs.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/wmi.h>
14 #include "bioscfg.h"
15 #include "../../firmware_attributes_class.h"
16 #include <linux/nls.h>
17 #include <linux/errno.h>
18
19 MODULE_AUTHOR("Jorge Lopez <jorge.lopez2@hp.com>");
20 MODULE_DESCRIPTION("HP BIOS Configuration Driver");
21 MODULE_LICENSE("GPL");
22
23 struct bioscfg_priv bioscfg_drv = {
24 .mutex = __MUTEX_INITIALIZER(bioscfg_drv.mutex),
25 };
26
display_name_language_code_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)27 ssize_t display_name_language_code_show(struct kobject *kobj,
28 struct kobj_attribute *attr,
29 char *buf)
30 {
31 return sysfs_emit(buf, "%s\n", LANG_CODE_STR);
32 }
33
34 struct kobj_attribute common_display_langcode =
35 __ATTR_RO(display_name_language_code);
36
hp_get_integer_from_buffer(u8 ** buffer,u32 * buffer_size,u32 * integer)37 int hp_get_integer_from_buffer(u8 **buffer, u32 *buffer_size, u32 *integer)
38 {
39 int *ptr = PTR_ALIGN((int *)*buffer, sizeof(int));
40
41 /* Ensure there is enough space remaining to read the integer */
42 if (*buffer_size < sizeof(int))
43 return -EINVAL;
44
45 *integer = *(ptr++);
46 *buffer = (u8 *)ptr;
47 *buffer_size -= sizeof(int);
48
49 return 0;
50 }
51
hp_get_string_from_buffer(u8 ** buffer,u32 * buffer_size,char * dst,u32 dst_size)52 int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_size)
53 {
54 u16 *src = (u16 *)*buffer;
55 u16 src_size;
56
57 u16 size;
58 int i;
59 int conv_dst_size;
60
61 if (*buffer_size < sizeof(u16))
62 return -EINVAL;
63
64 src_size = *(src++);
65 /* size value in u16 chars */
66 size = src_size / sizeof(u16);
67
68 /* Ensure there is enough space remaining to read and convert
69 * the string
70 */
71 if (*buffer_size < src_size)
72 return -EINVAL;
73
74 for (i = 0; i < size; i++)
75 if (src[i] == '\\' ||
76 src[i] == '\r' ||
77 src[i] == '\n' ||
78 src[i] == '\t')
79 size++;
80
81 /*
82 * Conversion is limited to destination string max number of
83 * bytes.
84 */
85 conv_dst_size = size;
86 if (size > dst_size)
87 conv_dst_size = dst_size - 1;
88
89 /*
90 * convert from UTF-16 unicode to ASCII
91 */
92 utf16s_to_utf8s(src, src_size, UTF16_HOST_ENDIAN, dst, conv_dst_size);
93 dst[conv_dst_size] = 0;
94
95 for (i = 0; i < conv_dst_size; i++) {
96 if (*src == '\\' ||
97 *src == '\r' ||
98 *src == '\n' ||
99 *src == '\t') {
100 dst[i++] = '\\';
101 if (i == conv_dst_size)
102 break;
103 }
104
105 if (*src == '\r')
106 dst[i] = 'r';
107 else if (*src == '\n')
108 dst[i] = 'n';
109 else if (*src == '\t')
110 dst[i] = 't';
111 else if (*src == '"')
112 dst[i] = '\'';
113 else
114 dst[i] = *src;
115 src++;
116 }
117
118 *buffer = (u8 *)src;
119 *buffer_size -= size * sizeof(u16);
120
121 return size;
122 }
123
hp_get_common_data_from_buffer(u8 ** buffer_ptr,u32 * buffer_size,struct common_data * common_data)124 int hp_get_common_data_from_buffer(u8 **buffer_ptr, u32 *buffer_size,
125 struct common_data *common_data)
126 {
127 int ret = 0;
128 int reqs;
129
130 // PATH:
131 ret = hp_get_string_from_buffer(buffer_ptr, buffer_size, common_data->path,
132 sizeof(common_data->path));
133 if (ret < 0)
134 goto common_exit;
135
136 // IS_READONLY:
137 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
138 &common_data->is_readonly);
139 if (ret < 0)
140 goto common_exit;
141
142 //DISPLAY_IN_UI:
143 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
144 &common_data->display_in_ui);
145 if (ret < 0)
146 goto common_exit;
147
148 // REQUIRES_PHYSICAL_PRESENCE:
149 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
150 &common_data->requires_physical_presence);
151 if (ret < 0)
152 goto common_exit;
153
154 // SEQUENCE:
155 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
156 &common_data->sequence);
157 if (ret < 0)
158 goto common_exit;
159
160 // PREREQUISITES_SIZE:
161 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
162 &common_data->prerequisites_size);
163 if (ret < 0)
164 goto common_exit;
165
166 if (common_data->prerequisites_size > MAX_PREREQUISITES_SIZE) {
167 /* Report a message and limit prerequisite size to maximum value */
168 pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n");
169 common_data->prerequisites_size = MAX_PREREQUISITES_SIZE;
170 }
171
172 // PREREQUISITES:
173 for (reqs = 0; reqs < common_data->prerequisites_size; reqs++) {
174 ret = hp_get_string_from_buffer(buffer_ptr, buffer_size,
175 common_data->prerequisites[reqs],
176 sizeof(common_data->prerequisites[reqs]));
177 if (ret < 0)
178 break;
179 }
180
181 // SECURITY_LEVEL:
182 ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
183 &common_data->security_level);
184
185 common_exit:
186 return ret;
187 }
188
hp_enforce_single_line_input(char * buf,size_t count)189 int hp_enforce_single_line_input(char *buf, size_t count)
190 {
191 char *p;
192
193 p = memchr(buf, '\n', count);
194
195 if (p == buf + count - 1)
196 *p = '\0'; /* strip trailing newline */
197 else if (p)
198 return -EINVAL; /* enforce single line input */
199
200 return 0;
201 }
202
203 /* Set pending reboot value and generate KOBJ_NAME event */
hp_set_reboot_and_signal_event(void)204 void hp_set_reboot_and_signal_event(void)
205 {
206 bioscfg_drv.pending_reboot = true;
207 kobject_uevent(&bioscfg_drv.class_dev->kobj, KOBJ_CHANGE);
208 }
209
210 /**
211 * hp_calculate_string_buffer() - determines size of string buffer for
212 * use with BIOS communication
213 *
214 * @str: the string to calculate based upon
215 */
hp_calculate_string_buffer(const char * str)216 size_t hp_calculate_string_buffer(const char *str)
217 {
218 size_t length = strlen(str);
219
220 /* BIOS expects 4 bytes when an empty string is found */
221 if (length == 0)
222 return 4;
223
224 /* u16 length field + one UTF16 char for each input char */
225 return sizeof(u16) + strlen(str) * sizeof(u16);
226 }
227
hp_wmi_error_and_message(int error_code)228 int hp_wmi_error_and_message(int error_code)
229 {
230 char *error_msg = NULL;
231 int ret;
232
233 switch (error_code) {
234 case SUCCESS:
235 error_msg = "Success";
236 ret = 0;
237 break;
238 case CMD_FAILED:
239 error_msg = "Command failed";
240 ret = -EINVAL;
241 break;
242 case INVALID_SIGN:
243 error_msg = "Invalid signature";
244 ret = -EINVAL;
245 break;
246 case INVALID_CMD_VALUE:
247 error_msg = "Invalid command value/Feature not supported";
248 ret = -EOPNOTSUPP;
249 break;
250 case INVALID_CMD_TYPE:
251 error_msg = "Invalid command type";
252 ret = -EINVAL;
253 break;
254 case INVALID_DATA_SIZE:
255 error_msg = "Invalid data size";
256 ret = -EINVAL;
257 break;
258 case INVALID_CMD_PARAM:
259 error_msg = "Invalid command parameter";
260 ret = -EINVAL;
261 break;
262 case ENCRYP_CMD_REQUIRED:
263 error_msg = "Secure/encrypted command required";
264 ret = -EACCES;
265 break;
266 case NO_SECURE_SESSION:
267 error_msg = "No secure session established";
268 ret = -EACCES;
269 break;
270 case SECURE_SESSION_FOUND:
271 error_msg = "Secure session already established";
272 ret = -EACCES;
273 break;
274 case SECURE_SESSION_FAILED:
275 error_msg = "Secure session failed";
276 ret = -EIO;
277 break;
278 case AUTH_FAILED:
279 error_msg = "Other permission/Authentication failed";
280 ret = -EACCES;
281 break;
282 case INVALID_BIOS_AUTH:
283 error_msg = "Invalid BIOS administrator password";
284 ret = -EINVAL;
285 break;
286 case NONCE_DID_NOT_MATCH:
287 error_msg = "Nonce did not match";
288 ret = -EINVAL;
289 break;
290 case GENERIC_ERROR:
291 error_msg = "Generic/Other error";
292 ret = -EIO;
293 break;
294 case BIOS_ADMIN_POLICY_NOT_MET:
295 error_msg = "BIOS Admin password does not meet password policy requirements";
296 ret = -EINVAL;
297 break;
298 case BIOS_ADMIN_NOT_SET:
299 error_msg = "BIOS Setup password is not set";
300 ret = -EPERM;
301 break;
302 case P21_NO_PROVISIONED:
303 error_msg = "P21 is not provisioned";
304 ret = -EPERM;
305 break;
306 case P21_PROVISION_IN_PROGRESS:
307 error_msg = "P21 is already provisioned or provisioning is in progress and a signing key has already been sent";
308 ret = -EINPROGRESS;
309 break;
310 case P21_IN_USE:
311 error_msg = "P21 in use (cannot deprovision)";
312 ret = -EPERM;
313 break;
314 case HEP_NOT_ACTIVE:
315 error_msg = "HEP not activated";
316 ret = -EPERM;
317 break;
318 case HEP_ALREADY_SET:
319 error_msg = "HEP Transport already set";
320 ret = -EINVAL;
321 break;
322 case HEP_CHECK_STATE:
323 error_msg = "Check the current HEP state";
324 ret = -EINVAL;
325 break;
326 default:
327 error_msg = "Generic/Other error";
328 ret = -EIO;
329 break;
330 }
331
332 if (error_code)
333 pr_warn_ratelimited("Returned error 0x%x, \"%s\"\n", error_code, error_msg);
334
335 return ret;
336 }
337
pending_reboot_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)338 static ssize_t pending_reboot_show(struct kobject *kobj,
339 struct kobj_attribute *attr,
340 char *buf)
341 {
342 return sysfs_emit(buf, "%d\n", bioscfg_drv.pending_reboot);
343 }
344
345 static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);
346
347 /*
348 * create_attributes_level_sysfs_files() - Creates pending_reboot attributes
349 */
create_attributes_level_sysfs_files(void)350 static int create_attributes_level_sysfs_files(void)
351 {
352 return sysfs_create_file(&bioscfg_drv.main_dir_kset->kobj,
353 &pending_reboot.attr);
354 }
355
attr_name_release(struct kobject * kobj)356 static void attr_name_release(struct kobject *kobj)
357 {
358 kfree(kobj);
359 }
360
361 static const struct kobj_type attr_name_ktype = {
362 .release = attr_name_release,
363 .sysfs_ops = &kobj_sysfs_ops,
364 };
365
366 /**
367 * hp_get_wmiobj_pointer() - Get Content of WMI block for particular instance
368 *
369 * @instance_id: WMI instance ID
370 * @guid_string: WMI GUID (in str form)
371 *
372 * Fetches the content for WMI block (instance_id) under GUID (guid_string)
373 * Caller must kfree the return
374 */
hp_get_wmiobj_pointer(int instance_id,const char * guid_string)375 union acpi_object *hp_get_wmiobj_pointer(int instance_id, const char *guid_string)
376 {
377 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
378 acpi_status status;
379
380 status = wmi_query_block(guid_string, instance_id, &out);
381 return ACPI_SUCCESS(status) ? (union acpi_object *)out.pointer : NULL;
382 }
383
384 /**
385 * hp_get_instance_count() - Compute total number of instances under guid_string
386 *
387 * @guid_string: WMI GUID (in string form)
388 */
hp_get_instance_count(const char * guid_string)389 int hp_get_instance_count(const char *guid_string)
390 {
391 union acpi_object *wmi_obj = NULL;
392 int i = 0;
393
394 do {
395 kfree(wmi_obj);
396 wmi_obj = hp_get_wmiobj_pointer(i, guid_string);
397 i++;
398 } while (wmi_obj);
399
400 return i - 1;
401 }
402
403 /**
404 * hp_alloc_attributes_data() - Allocate attributes data for a particular type
405 *
406 * @attr_type: Attribute type to allocate
407 */
hp_alloc_attributes_data(int attr_type)408 static int hp_alloc_attributes_data(int attr_type)
409 {
410 switch (attr_type) {
411 case HPWMI_STRING_TYPE:
412 return hp_alloc_string_data();
413
414 case HPWMI_INTEGER_TYPE:
415 return hp_alloc_integer_data();
416
417 case HPWMI_ENUMERATION_TYPE:
418 return hp_alloc_enumeration_data();
419
420 case HPWMI_ORDERED_LIST_TYPE:
421 return hp_alloc_ordered_list_data();
422
423 case HPWMI_PASSWORD_TYPE:
424 return hp_alloc_password_data();
425
426 default:
427 return 0;
428 }
429 }
430
hp_convert_hexstr_to_str(const char * input,u32 input_len,char ** str,int * len)431 int hp_convert_hexstr_to_str(const char *input, u32 input_len, char **str, int *len)
432 {
433 int ret = 0;
434 int new_len = 0;
435 char tmp[] = "0x00";
436 char *new_str = NULL;
437 long ch;
438 int i;
439
440 if (input_len <= 0 || !input || !str || !len)
441 return -EINVAL;
442
443 *len = 0;
444 *str = NULL;
445
446 new_str = kmalloc(input_len, GFP_KERNEL);
447 if (!new_str)
448 return -ENOMEM;
449
450 for (i = 0; i < input_len; i += 5) {
451 strncpy(tmp, input + i, strlen(tmp));
452 if (kstrtol(tmp, 16, &ch) == 0) {
453 // escape char
454 if (ch == '\\' ||
455 ch == '\r' ||
456 ch == '\n' || ch == '\t') {
457 if (ch == '\r')
458 ch = 'r';
459 else if (ch == '\n')
460 ch = 'n';
461 else if (ch == '\t')
462 ch = 't';
463 new_str[new_len++] = '\\';
464 }
465 new_str[new_len++] = ch;
466 if (ch == '\0')
467 break;
468 }
469 }
470
471 if (new_len) {
472 new_str[new_len] = '\0';
473 *str = krealloc(new_str, (new_len + 1) * sizeof(char),
474 GFP_KERNEL);
475 if (*str)
476 *len = new_len;
477 else
478 ret = -ENOMEM;
479 } else {
480 ret = -EFAULT;
481 }
482
483 if (ret)
484 kfree(new_str);
485 return ret;
486 }
487
488 /* map output size to the corresponding WMI method id */
hp_encode_outsize_for_pvsz(int outsize)489 int hp_encode_outsize_for_pvsz(int outsize)
490 {
491 if (outsize > 4096)
492 return -EINVAL;
493 if (outsize > 1024)
494 return 5;
495 if (outsize > 128)
496 return 4;
497 if (outsize > 4)
498 return 3;
499 if (outsize > 0)
500 return 2;
501 return 1;
502 }
503
504 /*
505 * Update friendly display name for several attributes associated to
506 * 'Schedule Power-On'
507 */
hp_friendly_user_name_update(char * path,const char * attr_name,char * attr_display,int attr_size)508 void hp_friendly_user_name_update(char *path, const char *attr_name,
509 char *attr_display, int attr_size)
510 {
511 if (strstr(path, SCHEDULE_POWER_ON))
512 snprintf(attr_display, attr_size, "%s - %s", SCHEDULE_POWER_ON, attr_name);
513 else
514 strscpy(attr_display, attr_name, attr_size);
515 }
516
517 /**
518 * hp_update_attribute_permissions() - Update attributes permissions when
519 * isReadOnly value is 1
520 *
521 * @is_readonly: bool value to indicate if it a readonly attribute.
522 * @current_val: kobj_attribute corresponding to attribute.
523 *
524 */
hp_update_attribute_permissions(bool is_readonly,struct kobj_attribute * current_val)525 void hp_update_attribute_permissions(bool is_readonly, struct kobj_attribute *current_val)
526 {
527 current_val->attr.mode = is_readonly ? 0444 : 0644;
528 }
529
530 /**
531 * destroy_attribute_objs() - Free a kset of kobjects
532 * @kset: The kset to destroy
533 *
534 * Fress kobjects created for each attribute_name under attribute type kset
535 */
destroy_attribute_objs(struct kset * kset)536 static void destroy_attribute_objs(struct kset *kset)
537 {
538 struct kobject *pos, *next;
539
540 list_for_each_entry_safe(pos, next, &kset->list, entry)
541 kobject_put(pos);
542 }
543
544 /**
545 * release_attributes_data() - Clean-up all sysfs directories and files created
546 */
release_attributes_data(void)547 static void release_attributes_data(void)
548 {
549 mutex_lock(&bioscfg_drv.mutex);
550
551 hp_exit_string_attributes();
552 hp_exit_integer_attributes();
553 hp_exit_enumeration_attributes();
554 hp_exit_ordered_list_attributes();
555 hp_exit_password_attributes();
556 hp_exit_sure_start_attributes();
557 hp_exit_secure_platform_attributes();
558
559 if (bioscfg_drv.authentication_dir_kset) {
560 destroy_attribute_objs(bioscfg_drv.authentication_dir_kset);
561 kset_unregister(bioscfg_drv.authentication_dir_kset);
562 bioscfg_drv.authentication_dir_kset = NULL;
563 }
564 if (bioscfg_drv.main_dir_kset) {
565 sysfs_remove_file(&bioscfg_drv.main_dir_kset->kobj, &pending_reboot.attr);
566 destroy_attribute_objs(bioscfg_drv.main_dir_kset);
567 kset_unregister(bioscfg_drv.main_dir_kset);
568 bioscfg_drv.main_dir_kset = NULL;
569 }
570 mutex_unlock(&bioscfg_drv.mutex);
571 }
572
573 /**
574 * hp_add_other_attributes() - Initialize HP custom attributes not
575 * reported by BIOS and required to support Secure Platform and Sure
576 * Start.
577 *
578 * @attr_type: Custom HP attribute not reported by BIOS
579 *
580 * Initialize all 2 types of attributes: Platform and Sure Start
581 * object. Populates each attribute types respective properties
582 * under sysfs files.
583 *
584 * Returns zero(0) if successful. Otherwise, a negative value.
585 */
hp_add_other_attributes(int attr_type)586 static int hp_add_other_attributes(int attr_type)
587 {
588 struct kobject *attr_name_kobj;
589 union acpi_object *obj = NULL;
590 int ret;
591 char *attr_name;
592
593 attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
594 if (!attr_name_kobj)
595 return -ENOMEM;
596
597 mutex_lock(&bioscfg_drv.mutex);
598
599 /* Check if attribute type is supported */
600 switch (attr_type) {
601 case HPWMI_SECURE_PLATFORM_TYPE:
602 attr_name_kobj->kset = bioscfg_drv.authentication_dir_kset;
603 attr_name = SPM_STR;
604 break;
605
606 case HPWMI_SURE_START_TYPE:
607 attr_name_kobj->kset = bioscfg_drv.main_dir_kset;
608 attr_name = SURE_START_STR;
609 break;
610
611 default:
612 pr_err("Error: Unknown attr_type: %d\n", attr_type);
613 ret = -EINVAL;
614 kfree(attr_name_kobj);
615 goto unlock_drv_mutex;
616 }
617
618 ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
619 NULL, "%s", attr_name);
620 if (ret) {
621 pr_err("Error encountered [%d]\n", ret);
622 goto err_other_attr_init;
623 }
624
625 /* Populate attribute data */
626 switch (attr_type) {
627 case HPWMI_SECURE_PLATFORM_TYPE:
628 ret = hp_populate_secure_platform_data(attr_name_kobj);
629 break;
630
631 case HPWMI_SURE_START_TYPE:
632 ret = hp_populate_sure_start_data(attr_name_kobj);
633 break;
634
635 default:
636 ret = -EINVAL;
637 }
638
639 if (ret)
640 goto err_other_attr_init;
641
642 mutex_unlock(&bioscfg_drv.mutex);
643 return 0;
644
645 err_other_attr_init:
646 kobject_put(attr_name_kobj);
647 unlock_drv_mutex:
648 mutex_unlock(&bioscfg_drv.mutex);
649 kfree(obj);
650 return ret;
651 }
652
hp_init_bios_package_attribute(enum hp_wmi_data_type attr_type,union acpi_object * obj,const char * guid,int min_elements,int instance_id)653 static int hp_init_bios_package_attribute(enum hp_wmi_data_type attr_type,
654 union acpi_object *obj,
655 const char *guid, int min_elements,
656 int instance_id)
657 {
658 struct kobject *attr_name_kobj, *duplicate;
659 union acpi_object *elements;
660 struct kset *temp_kset;
661
662 char *str_value = NULL;
663 int str_len;
664 int ret = 0;
665
666 /* Take action appropriate to each ACPI TYPE */
667 if (obj->package.count < min_elements) {
668 pr_err("ACPI-package does not have enough elements: %d < %d\n",
669 obj->package.count, min_elements);
670 goto pack_attr_exit;
671 }
672
673 elements = obj->package.elements;
674
675 /* sanity checking */
676 if (elements[NAME].type != ACPI_TYPE_STRING) {
677 pr_debug("incorrect element type\n");
678 goto pack_attr_exit;
679 }
680 if (strlen(elements[NAME].string.pointer) == 0) {
681 pr_debug("empty attribute found\n");
682 goto pack_attr_exit;
683 }
684
685 if (attr_type == HPWMI_PASSWORD_TYPE)
686 temp_kset = bioscfg_drv.authentication_dir_kset;
687 else
688 temp_kset = bioscfg_drv.main_dir_kset;
689
690 /* convert attribute name to string */
691 ret = hp_convert_hexstr_to_str(elements[NAME].string.pointer,
692 elements[NAME].string.length,
693 &str_value, &str_len);
694
695 if (ret) {
696 pr_debug("Failed to populate integer package data. Error [0%0x]\n",
697 ret);
698 kfree(str_value);
699 return ret;
700 }
701
702 /* All duplicate attributes found are ignored */
703 duplicate = kset_find_obj(temp_kset, str_value);
704 if (duplicate) {
705 pr_debug("Duplicate attribute name found - %s\n", str_value);
706 /* kset_find_obj() returns a reference */
707 kobject_put(duplicate);
708 goto pack_attr_exit;
709 }
710
711 /* build attribute */
712 attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
713 if (!attr_name_kobj) {
714 ret = -ENOMEM;
715 goto pack_attr_exit;
716 }
717
718 attr_name_kobj->kset = temp_kset;
719
720 ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
721 NULL, "%s", str_value);
722
723 if (ret) {
724 kobject_put(attr_name_kobj);
725 goto pack_attr_exit;
726 }
727
728 /* enumerate all of these attributes */
729 switch (attr_type) {
730 case HPWMI_STRING_TYPE:
731 ret = hp_populate_string_package_data(elements,
732 instance_id,
733 attr_name_kobj);
734 break;
735 case HPWMI_INTEGER_TYPE:
736 ret = hp_populate_integer_package_data(elements,
737 instance_id,
738 attr_name_kobj);
739 break;
740 case HPWMI_ENUMERATION_TYPE:
741 ret = hp_populate_enumeration_package_data(elements,
742 instance_id,
743 attr_name_kobj);
744 break;
745 case HPWMI_ORDERED_LIST_TYPE:
746 ret = hp_populate_ordered_list_package_data(elements,
747 instance_id,
748 attr_name_kobj);
749 break;
750 case HPWMI_PASSWORD_TYPE:
751 ret = hp_populate_password_package_data(elements,
752 instance_id,
753 attr_name_kobj);
754 break;
755 default:
756 pr_debug("Unknown attribute type found: 0x%x\n", attr_type);
757 break;
758 }
759
760 pack_attr_exit:
761 kfree(str_value);
762 return ret;
763 }
764
hp_init_bios_buffer_attribute(enum hp_wmi_data_type attr_type,union acpi_object * obj,const char * guid,int min_elements,int instance_id)765 static int hp_init_bios_buffer_attribute(enum hp_wmi_data_type attr_type,
766 union acpi_object *obj,
767 const char *guid, int min_elements,
768 int instance_id)
769 {
770 struct kobject *attr_name_kobj, *duplicate;
771 struct kset *temp_kset;
772 char str[MAX_BUFF_SIZE];
773
774 char *temp_str = NULL;
775 char *str_value = NULL;
776 u8 *buffer_ptr = NULL;
777 int buffer_size;
778 int ret = 0;
779
780 buffer_size = obj->buffer.length;
781 buffer_ptr = obj->buffer.pointer;
782
783 ret = hp_get_string_from_buffer(&buffer_ptr,
784 &buffer_size, str, MAX_BUFF_SIZE);
785
786 if (ret < 0)
787 goto buff_attr_exit;
788
789 if (attr_type == HPWMI_PASSWORD_TYPE ||
790 attr_type == HPWMI_SECURE_PLATFORM_TYPE)
791 temp_kset = bioscfg_drv.authentication_dir_kset;
792 else
793 temp_kset = bioscfg_drv.main_dir_kset;
794
795 /* All duplicate attributes found are ignored */
796 duplicate = kset_find_obj(temp_kset, str);
797 if (duplicate) {
798 pr_debug("Duplicate attribute name found - %s\n", str);
799 /* kset_find_obj() returns a reference */
800 kobject_put(duplicate);
801 goto buff_attr_exit;
802 }
803
804 /* build attribute */
805 attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
806 if (!attr_name_kobj) {
807 ret = -ENOMEM;
808 goto buff_attr_exit;
809 }
810
811 attr_name_kobj->kset = temp_kset;
812
813 temp_str = str;
814 if (attr_type == HPWMI_SECURE_PLATFORM_TYPE)
815 temp_str = "SPM";
816
817 ret = kobject_init_and_add(attr_name_kobj,
818 &attr_name_ktype, NULL, "%s", temp_str);
819 if (ret) {
820 kobject_put(attr_name_kobj);
821 goto buff_attr_exit;
822 }
823
824 /* enumerate all of these attributes */
825 switch (attr_type) {
826 case HPWMI_STRING_TYPE:
827 ret = hp_populate_string_buffer_data(buffer_ptr,
828 &buffer_size,
829 instance_id,
830 attr_name_kobj);
831 break;
832 case HPWMI_INTEGER_TYPE:
833 ret = hp_populate_integer_buffer_data(buffer_ptr,
834 &buffer_size,
835 instance_id,
836 attr_name_kobj);
837 break;
838 case HPWMI_ENUMERATION_TYPE:
839 ret = hp_populate_enumeration_buffer_data(buffer_ptr,
840 &buffer_size,
841 instance_id,
842 attr_name_kobj);
843 break;
844 case HPWMI_ORDERED_LIST_TYPE:
845 ret = hp_populate_ordered_list_buffer_data(buffer_ptr,
846 &buffer_size,
847 instance_id,
848 attr_name_kobj);
849 break;
850 case HPWMI_PASSWORD_TYPE:
851 ret = hp_populate_password_buffer_data(buffer_ptr,
852 &buffer_size,
853 instance_id,
854 attr_name_kobj);
855 break;
856 default:
857 pr_debug("Unknown attribute type found: 0x%x\n", attr_type);
858 break;
859 }
860
861 buff_attr_exit:
862 kfree(str_value);
863 return ret;
864 }
865
866 /**
867 * hp_init_bios_attributes() - Initialize all attributes for a type
868 * @attr_type: The attribute type to initialize
869 * @guid: The WMI GUID associated with this type to initialize
870 *
871 * Initialize all 5 types of attributes: enumeration, integer,
872 * string, password, ordered list object. Populates each attribute types
873 * respective properties under sysfs files
874 */
hp_init_bios_attributes(enum hp_wmi_data_type attr_type,const char * guid)875 static int hp_init_bios_attributes(enum hp_wmi_data_type attr_type, const char *guid)
876 {
877 union acpi_object *obj = NULL;
878 int min_elements;
879
880 /* instance_id needs to be reset for each type GUID
881 * also, instance IDs are unique within GUID but not across
882 */
883 int instance_id = 0;
884 int cur_instance_id = instance_id;
885 int ret = 0;
886
887 ret = hp_alloc_attributes_data(attr_type);
888 if (ret)
889 return ret;
890
891 switch (attr_type) {
892 case HPWMI_STRING_TYPE:
893 min_elements = STR_ELEM_CNT;
894 break;
895 case HPWMI_INTEGER_TYPE:
896 min_elements = INT_ELEM_CNT;
897 break;
898 case HPWMI_ENUMERATION_TYPE:
899 min_elements = ENUM_ELEM_CNT;
900 break;
901 case HPWMI_ORDERED_LIST_TYPE:
902 min_elements = ORD_ELEM_CNT;
903 break;
904 case HPWMI_PASSWORD_TYPE:
905 min_elements = PSWD_ELEM_CNT;
906 break;
907 default:
908 pr_err("Error: Unknown attr_type: %d\n", attr_type);
909 return -EINVAL;
910 }
911
912 /* need to use specific instance_id and guid combination to get right data */
913 obj = hp_get_wmiobj_pointer(instance_id, guid);
914 if (!obj)
915 return -ENODEV;
916
917 mutex_lock(&bioscfg_drv.mutex);
918 while (obj) {
919 /* Take action appropriate to each ACPI TYPE */
920 if (obj->type == ACPI_TYPE_PACKAGE) {
921 ret = hp_init_bios_package_attribute(attr_type, obj,
922 guid, min_elements,
923 cur_instance_id);
924
925 } else if (obj->type == ACPI_TYPE_BUFFER) {
926 ret = hp_init_bios_buffer_attribute(attr_type, obj,
927 guid, min_elements,
928 cur_instance_id);
929
930 } else {
931 pr_err("Expected ACPI-package or buffer type, got: %d\n",
932 obj->type);
933 ret = -EIO;
934 goto err_attr_init;
935 }
936
937 /*
938 * Failure reported in one attribute must not
939 * stop process of the remaining attribute values.
940 */
941 if (ret >= 0)
942 cur_instance_id++;
943
944 kfree(obj);
945 instance_id++;
946 obj = hp_get_wmiobj_pointer(instance_id, guid);
947 }
948
949 err_attr_init:
950 mutex_unlock(&bioscfg_drv.mutex);
951 kfree(obj);
952 return ret;
953 }
954
hp_init(void)955 static int __init hp_init(void)
956 {
957 int ret;
958 int hp_bios_capable = wmi_has_guid(HP_WMI_BIOS_GUID);
959 int set_bios_settings = wmi_has_guid(HP_WMI_SET_BIOS_SETTING_GUID);
960
961 if (!hp_bios_capable) {
962 pr_err("Unable to run on non-HP system\n");
963 return -ENODEV;
964 }
965
966 if (!set_bios_settings) {
967 pr_err("Unable to set BIOS settings on HP systems\n");
968 return -ENODEV;
969 }
970
971 ret = hp_init_attr_set_interface();
972 if (ret)
973 return ret;
974
975 bioscfg_drv.class_dev = device_create(&firmware_attributes_class, NULL, MKDEV(0, 0),
976 NULL, "%s", DRIVER_NAME);
977 if (IS_ERR(bioscfg_drv.class_dev)) {
978 ret = PTR_ERR(bioscfg_drv.class_dev);
979 goto err_unregister_class;
980 }
981
982 bioscfg_drv.main_dir_kset = kset_create_and_add("attributes", NULL,
983 &bioscfg_drv.class_dev->kobj);
984 if (!bioscfg_drv.main_dir_kset) {
985 ret = -ENOMEM;
986 pr_debug("Failed to create and add attributes\n");
987 goto err_destroy_classdev;
988 }
989
990 bioscfg_drv.authentication_dir_kset = kset_create_and_add("authentication", NULL,
991 &bioscfg_drv.class_dev->kobj);
992 if (!bioscfg_drv.authentication_dir_kset) {
993 ret = -ENOMEM;
994 pr_debug("Failed to create and add authentication\n");
995 goto err_release_attributes_data;
996 }
997
998 /*
999 * sysfs level attributes.
1000 * - pending_reboot
1001 */
1002 ret = create_attributes_level_sysfs_files();
1003 if (ret)
1004 pr_debug("Failed to create sysfs level attributes\n");
1005
1006 ret = hp_init_bios_attributes(HPWMI_STRING_TYPE, HP_WMI_BIOS_STRING_GUID);
1007 if (ret)
1008 pr_debug("Failed to populate string type attributes\n");
1009
1010 ret = hp_init_bios_attributes(HPWMI_INTEGER_TYPE, HP_WMI_BIOS_INTEGER_GUID);
1011 if (ret)
1012 pr_debug("Failed to populate integer type attributes\n");
1013
1014 ret = hp_init_bios_attributes(HPWMI_ENUMERATION_TYPE, HP_WMI_BIOS_ENUMERATION_GUID);
1015 if (ret)
1016 pr_debug("Failed to populate enumeration type attributes\n");
1017
1018 ret = hp_init_bios_attributes(HPWMI_ORDERED_LIST_TYPE, HP_WMI_BIOS_ORDERED_LIST_GUID);
1019 if (ret)
1020 pr_debug("Failed to populate ordered list object type attributes\n");
1021
1022 ret = hp_init_bios_attributes(HPWMI_PASSWORD_TYPE, HP_WMI_BIOS_PASSWORD_GUID);
1023 if (ret)
1024 pr_debug("Failed to populate password object type attributes\n");
1025
1026 bioscfg_drv.spm_data.attr_name_kobj = NULL;
1027 ret = hp_add_other_attributes(HPWMI_SECURE_PLATFORM_TYPE);
1028 if (ret)
1029 pr_debug("Failed to populate secure platform object type attribute\n");
1030
1031 bioscfg_drv.sure_start_attr_kobj = NULL;
1032 ret = hp_add_other_attributes(HPWMI_SURE_START_TYPE);
1033 if (ret)
1034 pr_debug("Failed to populate sure start object type attribute\n");
1035
1036 return 0;
1037
1038 err_release_attributes_data:
1039 release_attributes_data();
1040
1041 err_destroy_classdev:
1042 device_unregister(bioscfg_drv.class_dev);
1043
1044 err_unregister_class:
1045 hp_exit_attr_set_interface();
1046
1047 return ret;
1048 }
1049
hp_exit(void)1050 static void __exit hp_exit(void)
1051 {
1052 release_attributes_data();
1053 device_unregister(bioscfg_drv.class_dev);
1054
1055 hp_exit_attr_set_interface();
1056 }
1057
1058 module_init(hp_init);
1059 module_exit(hp_exit);
1060