summaryrefslogtreecommitdiffstats
path: root/drivers/platform/x86/hp/hp-bioscfg/bioscfg.c
blob: 5798b49ddaba901db74a072b123b7cfbb1e2ef0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
// SPDX-License-Identifier: GPL-2.0
/*
 * Common methods for use with hp-bioscfg driver
 *
 *  Copyright (c) 2022 HP Development Company, L.P.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/wmi.h>
#include "bioscfg.h"
#include "../../firmware_attributes_class.h"
#include <linux/nls.h>
#include <linux/errno.h>

MODULE_AUTHOR("Jorge Lopez <jorge.lopez2@hp.com>");
MODULE_DESCRIPTION("HP BIOS Configuration Driver");
MODULE_LICENSE("GPL");

struct bioscfg_priv bioscfg_drv = {
	.mutex = __MUTEX_INITIALIZER(bioscfg_drv.mutex),
};

static struct class *fw_attr_class;

ssize_t display_name_language_code_show(struct kobject *kobj,
					struct kobj_attribute *attr,
					char *buf)
{
	return sysfs_emit(buf, "%s\n", LANG_CODE_STR);
}

struct kobj_attribute common_display_langcode =
	__ATTR_RO(display_name_language_code);

int hp_get_integer_from_buffer(u8 **buffer, u32 *buffer_size, u32 *integer)
{
	int *ptr = PTR_ALIGN((int *)*buffer, sizeof(int));

	/* Ensure there is enough space remaining to read the integer */
	if (*buffer_size < sizeof(int))
		return -EINVAL;

	*integer = *(ptr++);
	*buffer = (u8 *)ptr;
	*buffer_size -= sizeof(int);

	return 0;
}

int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_size)
{
	u16 *src = (u16 *)*buffer;
	u16 src_size;

	u16 size;
	int i;
	int conv_dst_size;

	if (*buffer_size < sizeof(u16))
		return -EINVAL;

	src_size = *(src++);
	/* size value in u16 chars */
	size = src_size / sizeof(u16);

	/* Ensure there is enough space remaining to read and convert
	 * the string
	 */
	if (*buffer_size < src_size)
		return -EINVAL;

	for (i = 0; i < size; i++)
		if (src[i] == '\\' ||
		    src[i] == '\r' ||
		    src[i] == '\n' ||
		    src[i] == '\t')
			size++;

	/*
	 * Conversion is limited to destination string max number of
	 * bytes.
	 */
	conv_dst_size = size;
	if (size > dst_size)
		conv_dst_size = dst_size - 1;

	/*
	 * convert from UTF-16 unicode to ASCII
	 */
	utf16s_to_utf8s(src, src_size, UTF16_HOST_ENDIAN, dst, conv_dst_size);
	dst[conv_dst_size] = 0;

	for (i = 0; i < conv_dst_size; i++) {
		if (*src == '\\' ||
		    *src == '\r' ||
		    *src == '\n' ||
		    *src == '\t') {
			dst[i++] = '\\';
			if (i == conv_dst_size)
				break;
		}

		if (*src == '\r')
			dst[i] = 'r';
		else if (*src == '\n')
			dst[i] = 'n';
		else if (*src == '\t')
			dst[i] = 't';
		else if (*src == '"')
			dst[i] = '\'';
		else
			dst[i] = *src;
		src++;
	}

	*buffer = (u8 *)src;
	*buffer_size -= size * sizeof(u16);

	return size;
}

int hp_get_common_data_from_buffer(u8 **buffer_ptr, u32 *buffer_size,
				   struct common_data *common_data)
{
	int ret = 0;
	int reqs;

	// PATH:
	ret = hp_get_string_from_buffer(buffer_ptr, buffer_size, common_data->path,
					sizeof(common_data->path));
	if (ret < 0)
		goto common_exit;

	// IS_READONLY:
	ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
					 &common_data->is_readonly);
	if (ret < 0)
		goto common_exit;

	//DISPLAY_IN_UI:
	ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
					 &common_data->display_in_ui);
	if (ret < 0)
		goto common_exit;

	// REQUIRES_PHYSICAL_PRESENCE:
	ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
					 &common_data->requires_physical_presence);
	if (ret < 0)
		goto common_exit;

	// SEQUENCE:
	ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
					 &common_data->sequence);
	if (ret < 0)
		goto common_exit;

	// PREREQUISITES_SIZE:
	ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
					 &common_data->prerequisites_size);
	if (ret < 0)
		goto common_exit;

	if (common_data->prerequisites_size > MAX_PREREQUISITES_SIZE) {
		/* Report a message and limit prerequisite size to maximum value */
		pr_warn("Prerequisites size value exceeded the maximum number of elements supported or data may be malformed\n");
		common_data->prerequisites_size = MAX_PREREQUISITES_SIZE;
	}

	// PREREQUISITES:
	for (reqs = 0; reqs < common_data->prerequisites_size; reqs++) {
		ret = hp_get_string_from_buffer(buffer_ptr, buffer_size,
						common_data->prerequisites[reqs],
						sizeof(common_data->prerequisites[reqs]));
		if (ret < 0)
			break;
	}

	// SECURITY_LEVEL:
	ret = hp_get_integer_from_buffer(buffer_ptr, buffer_size,
					 &common_data->security_level);

common_exit:
	return ret;
}

int hp_enforce_single_line_input(char *buf, size_t count)
{
	char *p;

	p = memchr(buf, '\n', count);

	if (p == buf + count - 1)
		*p = '\0'; /* strip trailing newline */
	else if (p)
		return -EINVAL;  /* enforce single line input */

	return 0;
}

/* Set pending reboot value and generate KOBJ_NAME event */
void hp_set_reboot_and_signal_event(void)
{
	bioscfg_drv.pending_reboot = true;
	kobject_uevent(&bioscfg_drv.class_dev->kobj, KOBJ_CHANGE);
}

/**
 * hp_calculate_string_buffer() - determines size of string buffer for
 * use with BIOS communication
 *
 * @str: the string to calculate based upon
 */
size_t hp_calculate_string_buffer(const char *str)
{
	size_t length = strlen(str);

	/* BIOS expects 4 bytes when an empty string is found */
	if (length == 0)
		return 4;

	/* u16 length field + one UTF16 char for each input char */
	return sizeof(u16) + strlen(str) * sizeof(u16);
}

int hp_wmi_error_and_message(int error_code)
{
	char *error_msg = NULL;
	int ret;

	switch (error_code) {
	case SUCCESS:
		error_msg = "Success";
		ret = 0;
		break;
	case CMD_FAILED:
		error_msg = "Command failed";
		ret = -EINVAL;
		break;
	case INVALID_SIGN:
		error_msg = "Invalid signature";
		ret = -EINVAL;
		break;
	case INVALID_CMD_VALUE:
		error_msg = "Invalid command value/Feature not supported";
		ret = -EOPNOTSUPP;
		break;
	case INVALID_CMD_TYPE:
		error_msg = "Invalid command type";
		ret = -EINVAL;
		break;
	case INVALID_DATA_SIZE:
		error_msg = "Invalid data size";
		ret = -EINVAL;
		break;
	case INVALID_CMD_PARAM:
		error_msg = "Invalid command parameter";
		ret = -EINVAL;
		break;
	case ENCRYP_CMD_REQUIRED:
		error_msg = "Secure/encrypted command required";
		ret = -EACCES;
		break;
	case NO_SECURE_SESSION:
		error_msg = "No secure session established";
		ret = -EACCES;
		break;
	case SECURE_SESSION_FOUND:
		error_msg = "Secure session already established";
		ret = -EACCES;
		break;
	case SECURE_SESSION_FAILED:
		error_msg = "Secure session failed";
		ret = -EIO;
		break;
	case AUTH_FAILED:
		error_msg = "Other permission/Authentication failed";
		ret = -EACCES;
		break;
	case INVALID_BIOS_AUTH:
		error_msg = "Invalid BIOS administrator password";
		ret = -EINVAL;
		break;
	case NONCE_DID_NOT_MATCH:
		error_msg = "Nonce did not match";
		ret = -EINVAL;
		break;
	case GENERIC_ERROR:
		error_msg = "Generic/Other error";
		ret = -EIO;
		break;
	case BIOS_ADMIN_POLICY_NOT_MET:
		error_msg = "BIOS Admin password does not meet password policy requirements";
		ret = -EINVAL;
		break;
	case BIOS_ADMIN_NOT_SET:
		error_msg = "BIOS Setup password is not set";
		ret = -EPERM;
		break;
	case P21_NO_PROVISIONED:
		error_msg = "P21 is not provisioned";
		ret = -EPERM;
		break;
	case P21_PROVISION_IN_PROGRESS:
		error_msg = "P21 is already provisioned or provisioning is in progress and a signing key has already been sent";
		ret = -EINPROGRESS;
		break;
	case P21_IN_USE:
		error_msg = "P21 in use (cannot deprovision)";
		ret = -EPERM;
		break;
	case HEP_NOT_ACTIVE:
		error_msg = "HEP not activated";
		ret = -EPERM;
		break;
	case HEP_ALREADY_SET:
		error_msg = "HEP Transport already set";
		ret = -EINVAL;
		break;
	case HEP_CHECK_STATE:
		error_msg = "Check the current HEP state";
		ret = -EINVAL;
		break;
	default:
		error_msg = "Generic/Other error";
		ret = -EIO;
		break;
	}

	if (error_code)
		pr_warn_ratelimited("Returned error 0x%x, \"%s\"\n", error_code, error_msg);

	return ret;
}

static ssize_t pending_reboot_show(struct kobject *kobj,
				   struct kobj_attribute *attr,
				   char *buf)
{
	return sysfs_emit(buf, "%d\n", bioscfg_drv.pending_reboot);
}

static struct kobj_attribute pending_reboot = __ATTR_RO(pending_reboot);

/*
 * create_attributes_level_sysfs_files() - Creates pending_reboot attributes
 */
static int create_attributes_level_sysfs_files(void)
{
	return  sysfs_create_file(&bioscfg_drv.main_dir_kset->kobj,
				  &pending_reboot.attr);
}

static void attr_name_release(struct kobject *kobj)
{
	kfree(kobj);
}

static const struct kobj_type attr_name_ktype = {
	.release	= attr_name_release,
	.sysfs_ops	= &kobj_sysfs_ops,
};

/**
 * hp_get_wmiobj_pointer() - Get Content of WMI block for particular instance
 *
 * @instance_id: WMI instance ID
 * @guid_string: WMI GUID (in str form)
 *
 * Fetches the content for WMI block (instance_id) under GUID (guid_string)
 * Caller must kfree the return
 */
union acpi_object *hp_get_wmiobj_pointer(int instance_id, const char *guid_string)
{
	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
	acpi_status status;

	status = wmi_query_block(guid_string, instance_id, &out);
	return ACPI_SUCCESS(status) ? (union acpi_object *)out.pointer : NULL;
}

/**
 * hp_get_instance_count() - Compute total number of instances under guid_string
 *
 * @guid_string: WMI GUID (in string form)
 */
int hp_get_instance_count(const char *guid_string)
{
	union acpi_object *wmi_obj = NULL;
	int i = 0;

	do {
		kfree(wmi_obj);
		wmi_obj = hp_get_wmiobj_pointer(i, guid_string);
		i++;
	} while (wmi_obj);

	return i - 1;
}

/**
 * hp_alloc_attributes_data() - Allocate attributes data for a particular type
 *
 * @attr_type: Attribute type to allocate
 */
static int hp_alloc_attributes_data(int attr_type)
{
	switch (attr_type) {
	case HPWMI_STRING_TYPE:
		return hp_alloc_string_data();

	case HPWMI_INTEGER_TYPE:
		return hp_alloc_integer_data();

	case HPWMI_ENUMERATION_TYPE:
		return hp_alloc_enumeration_data();

	case HPWMI_ORDERED_LIST_TYPE:
		return hp_alloc_ordered_list_data();

	case HPWMI_PASSWORD_TYPE:
		return hp_alloc_password_data();

	default:
		return 0;
	}
}

int hp_convert_hexstr_to_str(const char *input, u32 input_len, char **str, int *len)
{
	int ret = 0;
	int new_len = 0;
	char tmp[] = "0x00";
	char *new_str = NULL;
	long  ch;
	int i;

	if (input_len <= 0 || !input || !str || !len)
		return -EINVAL;

	*len = 0;
	*str = NULL;

	new_str = kmalloc(input_len, GFP_KERNEL);
	if (!new_str)
		return -ENOMEM;

	for (i = 0; i < input_len; i += 5) {
		strncpy(tmp, input + i, strlen(tmp));
		if (kstrtol(tmp, 16, &ch) == 0) {
			// escape char
			if (ch == '\\' ||
			    ch == '\r' ||
			    ch == '\n' || ch == '\t') {
				if (ch == '\r')
					ch = 'r';
				else if (ch == '\n')
					ch = 'n';
				else if (ch == '\t')
					ch = 't';
				new_str[new_len++] = '\\';
			}
			new_str[new_len++] = ch;
			if (ch == '\0')
				break;
		}
	}

	if (new_len) {
		new_str[new_len] = '\0';
		*str = krealloc(new_str, (new_len + 1) * sizeof(char),
				GFP_KERNEL);
		if (*str)
			*len = new_len;
		else
			ret = -ENOMEM;
	} else {
		ret = -EFAULT;
	}

	if (ret)
		kfree(new_str);
	return ret;
}

/* map output size to the corresponding WMI method id */
int hp_encode_outsize_for_pvsz(int outsize)
{
	if (outsize > 4096)
		return -EINVAL;
	if (outsize > 1024)
		return 5;
	if (outsize > 128)
		return 4;
	if (outsize > 4)
		return 3;
	if (outsize > 0)
		return 2;
	return 1;
}

/*
 * Update friendly display name for several attributes associated to
 * 'Schedule Power-On'
 */
void hp_friendly_user_name_update(char *path, const char *attr_name,
				  char *attr_display, int attr_size)
{
	if (strstr(path, SCHEDULE_POWER_ON))
		snprintf(attr_display, attr_size, "%s - %s", SCHEDULE_POWER_ON, attr_name);
	else
		strscpy(attr_display, attr_name, attr_size);
}

/**
 * hp_update_attribute_permissions() - Update attributes permissions when
 * isReadOnly value is 1
 *
 * @is_readonly:  bool value to indicate if it a readonly attribute.
 * @current_val: kobj_attribute corresponding to attribute.
 *
 */
void hp_update_attribute_permissions(bool is_readonly, struct kobj_attribute *current_val)
{
	current_val->attr.mode = is_readonly ? 0444 : 0644;
}

/**
 * destroy_attribute_objs() - Free a kset of kobjects
 * @kset: The kset to destroy
 *
 * Fress kobjects created for each attribute_name under attribute type kset
 */
static void destroy_attribute_objs(struct kset *kset)
{
	struct kobject *pos, *next;

	list_for_each_entry_safe(pos, next, &kset->list, entry)
		kobject_put(pos);
}

/**
 * release_attributes_data() - Clean-up all sysfs directories and files created
 */
static void release_attributes_data(void)
{
	mutex_lock(&bioscfg_drv.mutex);

	hp_exit_string_attributes();
	hp_exit_integer_attributes();
	hp_exit_enumeration_attributes();
	hp_exit_ordered_list_attributes();
	hp_exit_password_attributes();
	hp_exit_sure_start_attributes();
	hp_exit_secure_platform_attributes();

	if (bioscfg_drv.authentication_dir_kset) {
		destroy_attribute_objs(bioscfg_drv.authentication_dir_kset);
		kset_unregister(bioscfg_drv.authentication_dir_kset);
		bioscfg_drv.authentication_dir_kset = NULL;
	}
	if (bioscfg_drv.main_dir_kset) {
		sysfs_remove_file(&bioscfg_drv.main_dir_kset->kobj, &pending_reboot.attr);
		destroy_attribute_objs(bioscfg_drv.main_dir_kset);
		kset_unregister(bioscfg_drv.main_dir_kset);
		bioscfg_drv.main_dir_kset = NULL;
	}
	mutex_unlock(&bioscfg_drv.mutex);
}

/**
 * hp_add_other_attributes() - Initialize HP custom attributes not
 * reported by BIOS and required to support Secure Platform and Sure
 * Start.
 *
 * @attr_type: Custom HP attribute not reported by BIOS
 *
 * Initialize all 2 types of attributes: Platform and Sure Start
 * object.  Populates each attribute types respective properties
 * under sysfs files.
 *
 * Returns zero(0) if successful. Otherwise, a negative value.
 */
static int hp_add_other_attributes(int attr_type)
{
	struct kobject *attr_name_kobj;
	union acpi_object *obj = NULL;
	int ret;
	char *attr_name;

	mutex_lock(&bioscfg_drv.mutex);

	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
	if (!attr_name_kobj) {
		ret = -ENOMEM;
		goto err_other_attr_init;
	}

	/* Check if attribute type is supported */
	switch (attr_type) {
	case HPWMI_SECURE_PLATFORM_TYPE:
		attr_name_kobj->kset = bioscfg_drv.authentication_dir_kset;
		attr_name = SPM_STR;
		break;

	case HPWMI_SURE_START_TYPE:
		attr_name_kobj->kset = bioscfg_drv.main_dir_kset;
		attr_name = SURE_START_STR;
		break;

	default:
		pr_err("Error: Unknown attr_type: %d\n", attr_type);
		ret = -EINVAL;
		goto err_other_attr_init;
	}

	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
				   NULL, "%s", attr_name);
	if (ret) {
		pr_err("Error encountered [%d]\n", ret);
		kobject_put(attr_name_kobj);
		goto err_other_attr_init;
	}

	/* Populate attribute data */
	switch (attr_type) {
	case HPWMI_SECURE_PLATFORM_TYPE:
		ret = hp_populate_secure_platform_data(attr_name_kobj);
		if (ret)
			goto err_other_attr_init;
		break;

	case HPWMI_SURE_START_TYPE:
		ret = hp_populate_sure_start_data(attr_name_kobj);
		if (ret)
			goto err_other_attr_init;
		break;

	default:
		ret = -EINVAL;
		goto err_other_attr_init;
	}

	mutex_unlock(&bioscfg_drv.mutex);
	return 0;

err_other_attr_init:
	mutex_unlock(&bioscfg_drv.mutex);
	kfree(obj);
	return ret;
}

static int 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)
{
	struct kobject *attr_name_kobj, *duplicate;
	union acpi_object *elements;
	struct kset *temp_kset;

	char *str_value = NULL;
	int str_len;
	int ret = 0;

	/* Take action appropriate to each ACPI TYPE */
	if (obj->package.count < min_elements) {
		pr_err("ACPI-package does not have enough elements: %d < %d\n",
		       obj->package.count, min_elements);
		goto pack_attr_exit;
	}

	elements = obj->package.elements;

	/* sanity checking */
	if (elements[NAME].type != ACPI_TYPE_STRING) {
		pr_debug("incorrect element type\n");
		goto pack_attr_exit;
	}
	if (strlen(elements[NAME].string.pointer) == 0) {
		pr_debug("empty attribute found\n");
		goto pack_attr_exit;
	}

	if (attr_type == HPWMI_PASSWORD_TYPE)
		temp_kset = bioscfg_drv.authentication_dir_kset;
	else
		temp_kset = bioscfg_drv.main_dir_kset;

	/* convert attribute name to string */
	ret = hp_convert_hexstr_to_str(elements[NAME].string.pointer,
				       elements[NAME].string.length,
				       &str_value, &str_len);

	if (ret) {
		pr_debug("Failed to populate integer package data. Error [0%0x]\n",
			 ret);
		kfree(str_value);
		return ret;
	}

	/* All duplicate attributes found are ignored */
	duplicate = kset_find_obj(temp_kset, str_value);
	if (duplicate) {
		pr_debug("Duplicate attribute name found - %s\n", str_value);
		/* kset_find_obj() returns a reference */
		kobject_put(duplicate);
		goto pack_attr_exit;
	}

	/* build attribute */
	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
	if (!attr_name_kobj) {
		ret = -ENOMEM;
		goto pack_attr_exit;
	}

	attr_name_kobj->kset = temp_kset;

	ret = kobject_init_and_add(attr_name_kobj, &attr_name_ktype,
				   NULL, "%s", str_value);

	if (ret) {
		kobject_put(attr_name_kobj);
		goto pack_attr_exit;
	}

	/* enumerate all of these attributes */
	switch (attr_type) {
	case HPWMI_STRING_TYPE:
		ret = hp_populate_string_package_data(elements,
						      instance_id,
						      attr_name_kobj);
		break;
	case HPWMI_INTEGER_TYPE:
		ret = hp_populate_integer_package_data(elements,
						       instance_id,
						       attr_name_kobj);
		break;
	case HPWMI_ENUMERATION_TYPE:
		ret = hp_populate_enumeration_package_data(elements,
							   instance_id,
							   attr_name_kobj);
		break;
	case HPWMI_ORDERED_LIST_TYPE:
		ret = hp_populate_ordered_list_package_data(elements,
							    instance_id,
							    attr_name_kobj);
		break;
	case HPWMI_PASSWORD_TYPE:
		ret = hp_populate_password_package_data(elements,
							instance_id,
							attr_name_kobj);
		break;
	default:
		pr_debug("Unknown attribute type found: 0x%x\n", attr_type);
		break;
	}

pack_attr_exit:
	kfree(str_value);
	return ret;
}

static int 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)
{
	struct kobject *attr_name_kobj, *duplicate;
	struct kset *temp_kset;
	char str[MAX_BUFF_SIZE];

	char *temp_str = NULL;
	char *str_value = NULL;
	u8 *buffer_ptr = NULL;
	int buffer_size;
	int ret = 0;

	buffer_size = obj->buffer.length;
	buffer_ptr = obj->buffer.pointer;

	ret = hp_get_string_from_buffer(&buffer_ptr,
					&buffer_size, str, MAX_BUFF_SIZE);

	if (ret < 0)
		goto buff_attr_exit;

	if (attr_type == HPWMI_PASSWORD_TYPE ||
	    attr_type == HPWMI_SECURE_PLATFORM_TYPE)
		temp_kset = bioscfg_drv.authentication_dir_kset;
	else
		temp_kset = bioscfg_drv.main_dir_kset;

	/* All duplicate attributes found are ignored */
	duplicate = kset_find_obj(temp_kset, str);
	if (duplicate) {
		pr_debug("Duplicate attribute name found - %s\n", str);
		/* kset_find_obj() returns a reference */
		kobject_put(duplicate);
		goto buff_attr_exit;
	}

	/* build attribute */
	attr_name_kobj = kzalloc(sizeof(*attr_name_kobj), GFP_KERNEL);
	if (!attr_name_kobj) {
		ret = -ENOMEM;
		goto buff_attr_exit;
	}

	attr_name_kobj->kset = temp_kset;

	temp_str = str;
	if (attr_type == HPWMI_SECURE_PLATFORM_TYPE)
		temp_str = "SPM";

	ret = kobject_init_and_add(attr_name_kobj,
				   &attr_name_ktype, NULL, "%s", temp_str);
	if (ret) {
		kobject_put(attr_name_kobj);
		goto buff_attr_exit;
	}

	/* enumerate all of these attributes */
	switch (attr_type) {
	case HPWMI_STRING_TYPE:
		ret = hp_populate_string_buffer_data(buffer_ptr,
						     &buffer_size,
						     instance_id,
						     attr_name_kobj);
		break;
	case HPWMI_INTEGER_TYPE:
		ret = hp_populate_integer_buffer_data(buffer_ptr,
						      &buffer_size,
						      instance_id,
						      attr_name_kobj);
		break;
	case HPWMI_ENUMERATION_TYPE:
		ret = hp_populate_enumeration_buffer_data(buffer_ptr,
							  &buffer_size,
							  instance_id,
							  attr_name_kobj);
		break;
	case HPWMI_ORDERED_LIST_TYPE:
		ret = hp_populate_ordered_list_buffer_data(buffer_ptr,
							   &buffer_size,
							   instance_id,
							   attr_name_kobj);
		break;
	case HPWMI_PASSWORD_TYPE:
		ret = hp_populate_password_buffer_data(buffer_ptr,
						       &buffer_size,
						       instance_id,
						       attr_name_kobj);
		break;
	default:
		pr_debug("Unknown attribute type found: 0x%x\n", attr_type);
		break;
	}

buff_attr_exit:
	kfree(str_value);
	return ret;
}

/**
 * hp_init_bios_attributes() - Initialize all attributes for a type
 * @attr_type: The attribute type to initialize
 * @guid: The WMI GUID associated with this type to initialize
 *
 * Initialize all 5 types of attributes: enumeration, integer,
 * string, password, ordered list  object.  Populates each attribute types
 * respective properties under sysfs files
 */
static int hp_init_bios_attributes(enum hp_wmi_data_type attr_type, const char *guid)
{
	union acpi_object *obj = NULL;
	int min_elements;

	/* instance_id needs to be reset for each type GUID
	 * also, instance IDs are unique within GUID but not across
	 */
	int instance_id = 0;
	int cur_instance_id = instance_id;
	int ret = 0;

	ret = hp_alloc_attributes_data(attr_type);
	if (ret)
		return ret;

	switch (attr_type) {
	case HPWMI_STRING_TYPE:
		min_elements = STR_ELEM_CNT;
		break;
	case HPWMI_INTEGER_TYPE:
		min_elements = INT_ELEM_CNT;
		break;
	case HPWMI_ENUMERATION_TYPE:
		min_elements = ENUM_ELEM_CNT;
		break;
	case HPWMI_ORDERED_LIST_TYPE:
		min_elements = ORD_ELEM_CNT;
		break;
	case HPWMI_PASSWORD_TYPE:
		min_elements = PSWD_ELEM_CNT;
		break;
	default:
		pr_err("Error: Unknown attr_type: %d\n", attr_type);
		return -EINVAL;
	}

	/* need to use specific instance_id and guid combination to get right data */
	obj = hp_get_wmiobj_pointer(instance_id, guid);
	if (!obj)
		return -ENODEV;

	mutex_lock(&bioscfg_drv.mutex);
	while (obj) {
		/* Take action appropriate to each ACPI TYPE */
		if (obj->type == ACPI_TYPE_PACKAGE) {
			ret = hp_init_bios_package_attribute(attr_type, obj,
							     guid, min_elements,
							     cur_instance_id);

		} else if (obj->type == ACPI_TYPE_BUFFER) {
			ret = hp_init_bios_buffer_attribute(attr_type, obj,
							    guid, min_elements,
							    cur_instance_id);

		} else {
			pr_err("Expected ACPI-package or buffer type, got: %d\n",
			       obj->type);
			ret = -EIO;
			goto err_attr_init;
		}

		/*
		 * Failure reported in one attribute must not
		 * stop process of the remaining attribute values.
		 */
		if (ret >= 0)
			cur_instance_id++;

		kfree(obj);
		instance_id++;
		obj = hp_get_wmiobj_pointer(instance_id, guid);
	}

err_attr_init:
	mutex_unlock(&bioscfg_drv.mutex);
	kfree(obj);
	return ret;
}

static int __init hp_init(void)
{
	int ret;
	int hp_bios_capable = wmi_has_guid(HP_WMI_BIOS_GUID);
	int set_bios_settings = wmi_has_guid(HP_WMI_SET_BIOS_SETTING_GUID);

	if (!hp_bios_capable) {
		pr_err("Unable to run on non-HP system\n");
		return -ENODEV;
	}

	if (!set_bios_settings) {
		pr_err("Unable to set BIOS settings on HP systems\n");
		return -ENODEV;
	}

	ret = hp_init_attr_set_interface();
	if (ret)
		return ret;

	ret = fw_attributes_class_get(&fw_attr_class);
	if (ret)
		goto err_unregister_class;

	bioscfg_drv.class_dev = device_create(fw_attr_class, NULL, MKDEV(0, 0),
					      NULL, "%s", DRIVER_NAME);
	if (IS_ERR(bioscfg_drv.class_dev)) {
		ret = PTR_ERR(bioscfg_drv.class_dev);
		goto err_unregister_class;
	}

	bioscfg_drv.main_dir_kset = kset_create_and_add("attributes", NULL,
							&bioscfg_drv.class_dev->kobj);
	if (!bioscfg_drv.main_dir_kset) {
		ret = -ENOMEM;
		pr_debug("Failed to create and add attributes\n");
		goto err_destroy_classdev;
	}

	bioscfg_drv.authentication_dir_kset = kset_create_and_add("authentication", NULL,
								  &bioscfg_drv.class_dev->kobj);
	if (!bioscfg_drv.authentication_dir_kset) {
		ret = -ENOMEM;
		pr_debug("Failed to create and add authentication\n");
		goto err_release_attributes_data;
	}

	/*
	 * sysfs level attributes.
	 * - pending_reboot
	 */
	ret = create_attributes_level_sysfs_files();
	if (ret)
		pr_debug("Failed to create sysfs level attributes\n");

	ret = hp_init_bios_attributes(HPWMI_STRING_TYPE, HP_WMI_BIOS_STRING_GUID);
	if (ret)
		pr_debug("Failed to populate string type attributes\n");

	ret = hp_init_bios_attributes(HPWMI_INTEGER_TYPE, HP_WMI_BIOS_INTEGER_GUID);
	if (ret)
		pr_debug("Failed to populate integer type attributes\n");

	ret = hp_init_bios_attributes(HPWMI_ENUMERATION_TYPE, HP_WMI_BIOS_ENUMERATION_GUID);
	if (ret)
		pr_debug("Failed to populate enumeration type attributes\n");

	ret = hp_init_bios_attributes(HPWMI_ORDERED_LIST_TYPE, HP_WMI_BIOS_ORDERED_LIST_GUID);
	if (ret)
		pr_debug("Failed to populate ordered list object type attributes\n");

	ret = hp_init_bios_attributes(HPWMI_PASSWORD_TYPE, HP_WMI_BIOS_PASSWORD_GUID);
	if (ret)
		pr_debug("Failed to populate password object type attributes\n");

	bioscfg_drv.spm_data.attr_name_kobj = NULL;
	ret = hp_add_other_attributes(HPWMI_SECURE_PLATFORM_TYPE);
	if (ret)
		pr_debug("Failed to populate secure platform object type attribute\n");

	bioscfg_drv.sure_start_attr_kobj = NULL;
	ret = hp_add_other_attributes(HPWMI_SURE_START_TYPE);
	if (ret)
		pr_debug("Failed to populate sure start object type attribute\n");

	return 0;

err_release_attributes_data:
	release_attributes_data();

err_destroy_classdev:
	device_destroy(fw_attr_class, MKDEV(0, 0));

err_unregister_class:
	fw_attributes_class_put();
	hp_exit_attr_set_interface();

	return ret;
}

static void __exit hp_exit(void)
{
	release_attributes_data();
	device_destroy(fw_attr_class, MKDEV(0, 0));

	fw_attributes_class_put();
	hp_exit_attr_set_interface();
}

module_init(hp_init);
module_exit(hp_exit);