1/* SPDX-License-Identifier: GPL-2.0
2 *
3 * Definitions for kernel modules using hp_bioscfg driver
4 *
5 *  Copyright (c) 2022 HP Development Company, L.P.
6 */
7
8#ifndef _HP_BIOSCFG_H_
9#define _HP_BIOSCFG_H_
10
11#include <linux/wmi.h>
12#include <linux/types.h>
13#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/nls.h>
17
18#define DRIVER_NAME		"hp-bioscfg"
19
20#define MAX_BUFF_SIZE		512
21#define MAX_KEY_MOD_SIZE	256
22#define MAX_PASSWD_SIZE		64
23#define MAX_PREREQUISITES_SIZE	20
24#define MAX_REQ_ELEM_SIZE	128
25#define MAX_VALUES_SIZE		16
26#define MAX_ENCODINGS_SIZE	16
27#define MAX_ELEMENTS_SIZE	16
28
29#define SPM_STR_DESC		"Secure Platform Management"
30#define SPM_STR			"SPM"
31#define SURE_START_DESC		"Sure Start"
32#define SURE_START_STR		"Sure_Start"
33#define SETUP_PASSWD		"Setup Password"
34#define POWER_ON_PASSWD		"Power-On Password"
35
36#define LANG_CODE_STR		"en_US.UTF-8"
37#define SCHEDULE_POWER_ON	"Scheduled Power-On"
38
39#define COMMA_SEP		","
40#define SEMICOLON_SEP		";"
41
42/* Sure Admin Functions */
43
44#define UTF_PREFIX		"<utf-16/>"
45#define BEAM_PREFIX		"<BEAM/>"
46
47enum mechanism_values {
48	PASSWORD		= 0x00,
49	SIGNING_KEY		= 0x01,
50	ENDORSEMENT_KEY		= 0x02,
51};
52
53#define BIOS_ADMIN		"bios-admin"
54#define POWER_ON		"power-on"
55#define BIOS_SPM		"enhanced-bios-auth"
56
57#define PASSWD_MECHANISM_TYPES "password"
58
59#define HP_WMI_BIOS_GUID		"5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
60
61#define HP_WMI_BIOS_STRING_GUID		"988D08E3-68F4-4c35-AF3E-6A1B8106F83C"
62#define HP_WMI_BIOS_INTEGER_GUID	"8232DE3D-663D-4327-A8F4-E293ADB9BF05"
63#define HP_WMI_BIOS_ENUMERATION_GUID	"2D114B49-2DFB-4130-B8FE-4A3C09E75133"
64#define HP_WMI_BIOS_ORDERED_LIST_GUID	"14EA9746-CE1F-4098-A0E0-7045CB4DA745"
65#define HP_WMI_BIOS_PASSWORD_GUID	"322F2028-0F84-4901-988E-015176049E2D"
66#define HP_WMI_SET_BIOS_SETTING_GUID	"1F4C91EB-DC5C-460b-951D-C7CB9B4B8D5E"
67
68enum hp_wmi_spm_commandtype {
69	HPWMI_SECUREPLATFORM_GET_STATE  = 0x10,
70	HPWMI_SECUREPLATFORM_SET_KEK	= 0x11,
71	HPWMI_SECUREPLATFORM_SET_SK	= 0x12,
72};
73
74enum hp_wmi_surestart_commandtype {
75	HPWMI_SURESTART_GET_LOG_COUNT	= 0x01,
76	HPWMI_SURESTART_GET_LOG		= 0x02,
77};
78
79enum hp_wmi_command {
80	HPWMI_READ		= 0x01,
81	HPWMI_WRITE		= 0x02,
82	HPWMI_ODM		= 0x03,
83	HPWMI_SURESTART		= 0x20006,
84	HPWMI_GM		= 0x20008,
85	HPWMI_SECUREPLATFORM	= 0x20010,
86};
87
88struct bios_return {
89	u32 sigpass;
90	u32 return_code;
91};
92
93enum wmi_error_values {
94	SUCCESS				= 0x00,
95	CMD_FAILED			= 0x01,
96	INVALID_SIGN			= 0x02,
97	INVALID_CMD_VALUE		= 0x03,
98	INVALID_CMD_TYPE		= 0x04,
99	INVALID_DATA_SIZE		= 0x05,
100	INVALID_CMD_PARAM		= 0x06,
101	ENCRYP_CMD_REQUIRED		= 0x07,
102	NO_SECURE_SESSION		= 0x08,
103	SECURE_SESSION_FOUND		= 0x09,
104	SECURE_SESSION_FAILED		= 0x0A,
105	AUTH_FAILED			= 0x0B,
106	INVALID_BIOS_AUTH		= 0x0E,
107	NONCE_DID_NOT_MATCH		= 0x18,
108	GENERIC_ERROR			= 0x1C,
109	BIOS_ADMIN_POLICY_NOT_MET	= 0x28,
110	BIOS_ADMIN_NOT_SET		= 0x38,
111	P21_NO_PROVISIONED		= 0x1000,
112	P21_PROVISION_IN_PROGRESS	= 0x1001,
113	P21_IN_USE			= 0x1002,
114	HEP_NOT_ACTIVE			= 0x1004,
115	HEP_ALREADY_SET			= 0x1006,
116	HEP_CHECK_STATE			= 0x1007,
117};
118
119struct common_data {
120	u8 display_name[MAX_BUFF_SIZE];
121	u8 path[MAX_BUFF_SIZE];
122	u32 is_readonly;
123	u32 display_in_ui;
124	u32 requires_physical_presence;
125	u32 sequence;
126	u32 prerequisites_size;
127	u8 prerequisites[MAX_PREREQUISITES_SIZE][MAX_BUFF_SIZE];
128	u32 security_level;
129};
130
131struct string_data {
132	struct common_data common;
133	struct kobject *attr_name_kobj;
134	u8 current_value[MAX_BUFF_SIZE];
135	u8 new_value[MAX_BUFF_SIZE];
136	u32 min_length;
137	u32 max_length;
138};
139
140struct integer_data {
141	struct common_data common;
142	struct kobject *attr_name_kobj;
143	u32 current_value;
144	u32 new_value;
145	u32 lower_bound;
146	u32 upper_bound;
147	u32 scalar_increment;
148};
149
150struct enumeration_data {
151	struct common_data common;
152	struct kobject *attr_name_kobj;
153	u8 current_value[MAX_BUFF_SIZE];
154	u8 new_value[MAX_BUFF_SIZE];
155	u32 possible_values_size;
156	u8 possible_values[MAX_VALUES_SIZE][MAX_BUFF_SIZE];
157};
158
159struct ordered_list_data {
160	struct common_data common;
161	struct kobject *attr_name_kobj;
162	u8 current_value[MAX_BUFF_SIZE];
163	u8 new_value[MAX_BUFF_SIZE];
164	u32 elements_size;
165	u8 elements[MAX_ELEMENTS_SIZE][MAX_BUFF_SIZE];
166};
167
168struct password_data {
169	struct common_data common;
170	struct kobject *attr_name_kobj;
171	u8 current_password[MAX_PASSWD_SIZE];
172	u8 new_password[MAX_PASSWD_SIZE];
173	u32 min_password_length;
174	u32 max_password_length;
175	u32 encodings_size;
176	u8 encodings[MAX_ENCODINGS_SIZE][MAX_BUFF_SIZE];
177	bool is_enabled;
178
179	/*
180	 * 'role' identifies the type of authentication.
181	 * Two known types are bios-admin and power-on.
182	 * 'bios-admin' represents BIOS administrator password
183	 * 'power-on' represents a password required to use the system
184	 */
185	u32 role;
186
187	/*
188	 * 'mechanism' represents the means of authentication.
189	 * Only supported type currently is "password"
190	 */
191	u32 mechanism;
192};
193
194struct secure_platform_data {
195	struct kobject *attr_name_kobj;
196	u8 attribute_name[MAX_BUFF_SIZE];
197	u8 *endorsement_key;
198	u8 *signing_key;
199	u8 *auth_token;
200	bool is_enabled;
201	u32 mechanism;
202};
203
204struct bioscfg_priv {
205	struct kset *authentication_dir_kset;
206	struct kset *main_dir_kset;
207	struct device *class_dev;
208	struct string_data *string_data;
209	u32 string_instances_count;
210	struct integer_data *integer_data;
211	u32 integer_instances_count;
212	struct enumeration_data *enumeration_data;
213	u32 enumeration_instances_count;
214	struct ordered_list_data *ordered_list_data;
215	u32 ordered_list_instances_count;
216	struct password_data *password_data;
217	u32 password_instances_count;
218
219	struct kobject *sure_start_attr_kobj;
220	struct secure_platform_data spm_data;
221	u8 display_name_language_code[MAX_BUFF_SIZE];
222	bool pending_reboot;
223	struct mutex mutex;
224};
225
226/* global structure used by multiple WMI interfaces */
227extern struct bioscfg_priv bioscfg_drv;
228
229enum hp_wmi_data_type {
230	HPWMI_STRING_TYPE,
231	HPWMI_INTEGER_TYPE,
232	HPWMI_ENUMERATION_TYPE,
233	HPWMI_ORDERED_LIST_TYPE,
234	HPWMI_PASSWORD_TYPE,
235	HPWMI_SECURE_PLATFORM_TYPE,
236	HPWMI_SURE_START_TYPE,
237};
238
239enum hp_wmi_data_elements {
240	/* Common elements */
241	NAME = 0,
242	VALUE = 1,
243	PATH = 2,
244	IS_READONLY = 3,
245	DISPLAY_IN_UI = 4,
246	REQUIRES_PHYSICAL_PRESENCE = 5,
247	SEQUENCE = 6,
248	PREREQUISITES_SIZE = 7,
249	PREREQUISITES = 8,
250	SECURITY_LEVEL = 9,
251
252	/* String elements */
253	STR_MIN_LENGTH = 10,
254	STR_MAX_LENGTH = 11,
255	STR_ELEM_CNT = 12,
256
257	/* Integer elements */
258	INT_LOWER_BOUND = 10,
259	INT_UPPER_BOUND = 11,
260	INT_SCALAR_INCREMENT = 12,
261	INT_ELEM_CNT = 13,
262
263	/* Enumeration elements */
264	ENUM_CURRENT_VALUE = 10,
265	ENUM_SIZE = 11,
266	ENUM_POSSIBLE_VALUES = 12,
267	ENUM_ELEM_CNT = 13,
268
269	/* Ordered list elements */
270	ORD_LIST_SIZE = 10,
271	ORD_LIST_ELEMENTS = 11,
272	ORD_ELEM_CNT = 12,
273
274	/* Password elements */
275	PSWD_MIN_LENGTH = 10,
276	PSWD_MAX_LENGTH = 11,
277	PSWD_SIZE = 12,
278	PSWD_ENCODINGS = 13,
279	PSWD_IS_SET = 14,
280	PSWD_ELEM_CNT = 15,
281};
282
283#define GET_INSTANCE_ID(type)						\
284	static int get_##type##_instance_id(struct kobject *kobj)	\
285	{								\
286		int i;							\
287									\
288		for (i = 0; i <= bioscfg_drv.type##_instances_count; i++) { \
289			if (!strcmp(kobj->name, bioscfg_drv.type##_data[i].attr_name_kobj->name)) \
290				return i;				\
291		}							\
292		return -EIO;						\
293	}
294
295#define ATTRIBUTE_S_PROPERTY_SHOW(name, type)				\
296	static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, \
297				   char *buf)				\
298	{								\
299		int i = get_##type##_instance_id(kobj);			\
300		if (i >= 0)						\
301			return sysfs_emit(buf, "%s\n", bioscfg_drv.type##_data[i].name); \
302		return -EIO;						\
303	}
304
305#define ATTRIBUTE_N_PROPERTY_SHOW(name, type)				\
306	static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, \
307				   char *buf)				\
308	{								\
309		int i = get_##type##_instance_id(kobj);			\
310		if (i >= 0)						\
311			return sysfs_emit(buf, "%d\n", bioscfg_drv.type##_data[i].name); \
312		return -EIO;						\
313	}
314
315#define ATTRIBUTE_PROPERTY_STORE(curr_val, type)			\
316	static ssize_t curr_val##_store(struct kobject *kobj,		\
317					struct kobj_attribute *attr,	\
318					const char *buf, size_t count)	\
319	{								\
320		char *attr_value = NULL;				\
321		int i;							\
322		int ret = -EIO;						\
323									\
324		attr_value = kstrdup(buf, GFP_KERNEL);			\
325		if (!attr_value)					\
326			return -ENOMEM;					\
327									\
328		ret = hp_enforce_single_line_input(attr_value, count);	\
329		if (!ret) {						\
330			i = get_##type##_instance_id(kobj);		\
331			if (i >= 0)					\
332				ret = validate_##type##_input(i, attr_value); \
333		}							\
334		if (!ret)						\
335			ret = hp_set_attribute(kobj->name, attr_value);	\
336		if (!ret) {						\
337			update_##type##_value(i, attr_value);		\
338			if (bioscfg_drv.type##_data[i].common.requires_physical_presence)	\
339				hp_set_reboot_and_signal_event();	\
340		}							\
341		hp_clear_all_credentials();				\
342		kfree(attr_value);					\
343									\
344		return ret ? ret : count;				\
345	}
346
347#define ATTRIBUTE_SPM_N_PROPERTY_SHOW(name, type)			\
348	static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
349	{								\
350		return sysfs_emit(buf, "%d\n", bioscfg_drv.type##_data.name); \
351	}
352
353#define ATTRIBUTE_SPM_S_PROPERTY_SHOW(name, type)			\
354	static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) \
355	{								\
356		return sysfs_emit(buf, "%s\n", bioscfg_drv.type##_data.name); \
357	}
358
359#define ATTRIBUTE_VALUES_PROPERTY_SHOW(name, type, sep)			\
360	static ssize_t name##_show(struct kobject *kobj,		\
361				   struct kobj_attribute *attr, char *buf) \
362	{								\
363		int i;							\
364		int len = 0;						\
365		int instance_id = get_##type##_instance_id(kobj);	\
366									\
367		if (instance_id < 0)					\
368			return 0;					\
369									\
370		for (i = 0; i < bioscfg_drv.type##_data[instance_id].name##_size; i++) { \
371			if (i)						\
372				len += sysfs_emit_at(buf, len, "%s", sep); \
373									\
374			len += sysfs_emit_at(buf, len, "%s",		\
375					     bioscfg_drv.type##_data[instance_id].name[i]); \
376		}							\
377		len += sysfs_emit_at(buf, len, "\n");			\
378		return len;						\
379	}
380
381#define ATTRIBUTE_S_COMMON_PROPERTY_SHOW(name, type)		\
382	static ssize_t name##_show(struct kobject *kobj, struct kobj_attribute *attr, \
383				   char *buf)				\
384	{								\
385		int i = get_##type##_instance_id(kobj);			\
386		if (i >= 0)						\
387			return sysfs_emit(buf, "%s\n", bioscfg_drv.type##_data[i].common.name); \
388		return -EIO;						\
389	}
390
391extern struct kobj_attribute common_display_langcode;
392
393/* Prototypes */
394
395/* String attributes */
396int hp_populate_string_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
397				   int instance_id,
398				   struct kobject *attr_name_kobj);
399int hp_alloc_string_data(void);
400void hp_exit_string_attributes(void);
401int hp_populate_string_package_data(union acpi_object *str_obj,
402				    int instance_id,
403				    struct kobject *attr_name_kobj);
404
405/* Integer attributes */
406int hp_populate_integer_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
407				    int instance_id,
408				    struct kobject *attr_name_kobj);
409int hp_alloc_integer_data(void);
410void hp_exit_integer_attributes(void);
411int hp_populate_integer_package_data(union acpi_object *integer_obj,
412				     int instance_id,
413				     struct kobject *attr_name_kobj);
414
415/* Enumeration attributes */
416int hp_populate_enumeration_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
417					int instance_id,
418					struct kobject *attr_name_kobj);
419int hp_alloc_enumeration_data(void);
420void hp_exit_enumeration_attributes(void);
421int hp_populate_enumeration_package_data(union acpi_object *enum_obj,
422					 int instance_id,
423					 struct kobject *attr_name_kobj);
424
425/* Ordered list */
426int hp_populate_ordered_list_buffer_data(u8 *buffer_ptr,
427					 u32 *buffer_size,
428					 int instance_id,
429					 struct kobject *attr_name_kobj);
430int hp_alloc_ordered_list_data(void);
431void hp_exit_ordered_list_attributes(void);
432int hp_populate_ordered_list_package_data(union acpi_object *order_obj,
433					  int instance_id,
434					  struct kobject *attr_name_kobj);
435
436/* Password authentication attributes */
437int hp_populate_password_buffer_data(u8 *buffer_ptr, u32 *buffer_size,
438				     int instance_id,
439				     struct kobject *attr_name_kobj);
440int hp_populate_password_package_data(union acpi_object *password_obj,
441				      int instance_id,
442				      struct kobject *attr_name_kobj);
443int hp_alloc_password_data(void);
444int hp_get_password_instance_for_type(const char *name);
445int hp_clear_all_credentials(void);
446int hp_set_attribute(const char *a_name, const char *a_value);
447
448/* SPM attributes */
449void hp_exit_password_attributes(void);
450void hp_exit_secure_platform_attributes(void);
451int hp_populate_secure_platform_data(struct kobject *attr_name_kobj);
452int hp_populate_security_buffer(u16 *buffer, const char *authentication);
453
454/* Bios Attributes interface */
455int hp_wmi_set_bios_setting(u16 *input_buffer, u32 input_size);
456int hp_wmi_perform_query(int query, enum hp_wmi_command command,
457			 void *buffer, u32 insize, u32 outsize);
458
459/* Sure Start attributes */
460void hp_exit_sure_start_attributes(void);
461int hp_populate_sure_start_data(struct kobject *attr_name_kobj);
462
463/* Bioscfg */
464
465void hp_exit_attr_set_interface(void);
466int hp_init_attr_set_interface(void);
467size_t hp_calculate_string_buffer(const char *str);
468size_t hp_calculate_security_buffer(const char *authentication);
469void *hp_ascii_to_utf16_unicode(u16 *p, const u8 *str);
470int hp_get_integer_from_buffer(u8 **buffer, u32 *buffer_size, u32 *integer);
471int hp_get_string_from_buffer(u8 **buffer, u32 *buffer_size, char *dst, u32 dst_size);
472int hp_convert_hexstr_to_str(const char *input, u32 input_len, char **str, int *len);
473int hp_encode_outsize_for_pvsz(int outsize);
474int hp_enforce_single_line_input(char *buf, size_t count);
475void hp_set_reboot_and_signal_event(void);
476ssize_t display_name_language_code_show(struct kobject *kobj,
477					struct kobj_attribute *attr,
478					char *buf);
479union acpi_object *hp_get_wmiobj_pointer(int instance_id, const char *guid_string);
480int hp_get_instance_count(const char *guid_string);
481void hp_update_attribute_permissions(bool isreadonly, struct kobj_attribute *current_val);
482void hp_friendly_user_name_update(char *path, const char *attr_name,
483				  char *attr_display, int attr_size);
484int hp_wmi_error_and_message(int error_code);
485int hp_get_common_data_from_buffer(u8 **buffer_ptr, u32 *buffer_size, struct common_data *common);
486
487#endif
488