summaryrefslogtreecommitdiffstats
path: root/drivers/platform/x86/dell/dell-wmi-sysman/biosattr-interface.c
blob: c2dd2de6bc2034226d5b62c04e47d1d74506bc9a (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Functions corresponding to SET methods under BIOS attributes interface GUID for use
 * with dell-wmi-sysman
 *
 *  Copyright (c) 2020 Dell Inc.
 */

#include <linux/wmi.h>
#include "dell-wmi-sysman.h"

#define SETDEFAULTVALUES_METHOD_ID					0x02
#define SETBIOSDEFAULTS_METHOD_ID					0x03
#define SETATTRIBUTE_METHOD_ID						0x04

static int call_biosattributes_interface(struct wmi_device *wdev, char *in_args, size_t size,
					int method_id)
{
	struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
	struct acpi_buffer input;
	union acpi_object *obj;
	acpi_status status;
	int ret = -EIO;

	input.length =  (acpi_size) size;
	input.pointer = in_args;
	status = wmidev_evaluate_method(wdev, 0, method_id, &input, &output);
	if (ACPI_FAILURE(status))
		return -EIO;
	obj = (union acpi_object *)output.pointer;
	if (obj->type == ACPI_TYPE_INTEGER)
		ret = obj->integer.value;

	if (wmi_priv.pending_changes == 0) {
		wmi_priv.pending_changes = 1;
		/* let userland know it may need to check reboot pending again */
		kobject_uevent(&wmi_priv.class_dev->kobj, KOBJ_CHANGE);
	}
	kfree(output.pointer);
	return map_wmi_error(ret);
}

/**
 * set_attribute() - Update an attribute value
 * @a_name: The attribute name
 * @a_value: The attribute value
 *
 * Sets an attribute to new value
 */
int set_attribute(const char *a_name, const char *a_value)
{
	size_t security_area_size, buffer_size;
	size_t a_name_size, a_value_size;
	char *buffer = NULL, *start;
	int ret;

	mutex_lock(&wmi_priv.mutex);
	if (!wmi_priv.bios_attr_wdev) {
		ret = -ENODEV;
		goto out;
	}

	/* build/calculate buffer */
	security_area_size = calculate_security_buffer(wmi_priv.current_admin_password);
	a_name_size = calculate_string_buffer(a_name);
	a_value_size = calculate_string_buffer(a_value);
	buffer_size = security_area_size + a_name_size + a_value_size;
	buffer = kzalloc(buffer_size, GFP_KERNEL);
	if (!buffer) {
		ret = -ENOMEM;
		goto out;
	}

	/* build security area */
	populate_security_buffer(buffer, wmi_priv.current_admin_password);

	/* build variables to set */
	start = buffer + security_area_size;
	ret = populate_string_buffer(start, a_name_size, a_name);
	if (ret < 0)
		goto out;
	start += ret;
	ret = populate_string_buffer(start, a_value_size, a_value);
	if (ret < 0)
		goto out;

	print_hex_dump_bytes("set attribute data: ", DUMP_PREFIX_NONE, buffer, buffer_size);
	ret = call_biosattributes_interface(wmi_priv.bios_attr_wdev,
					    buffer, buffer_size,
					    SETATTRIBUTE_METHOD_ID);
	if (ret == -EOPNOTSUPP)
		dev_err(&wmi_priv.bios_attr_wdev->dev, "admin password must be configured\n");
	else if (ret == -EACCES)
		dev_err(&wmi_priv.bios_attr_wdev->dev, "invalid password\n");

out:
	kfree(buffer);
	mutex_unlock(&wmi_priv.mutex);
	return ret;
}

/**
 * set_bios_defaults() - Resets BIOS defaults
 * @deftype: the type of BIOS value reset to issue.
 *
 * Resets BIOS defaults
 */
int set_bios_defaults(u8 deftype)
{
	size_t security_area_size, buffer_size;
	size_t integer_area_size = sizeof(u8);
	char *buffer = NULL;
	u8 *defaultType;
	int ret;

	mutex_lock(&wmi_priv.mutex);
	if (!wmi_priv.bios_attr_wdev) {
		ret = -ENODEV;
		goto out;
	}

	security_area_size = calculate_security_buffer(wmi_priv.current_admin_password);
	buffer_size = security_area_size + integer_area_size;
	buffer = kzalloc(buffer_size, GFP_KERNEL);
	if (!buffer) {
		ret = -ENOMEM;
		goto out;
	}

	/* build security area */
	populate_security_buffer(buffer, wmi_priv.current_admin_password);

	defaultType = buffer + security_area_size;
	*defaultType = deftype;

	ret = call_biosattributes_interface(wmi_priv.bios_attr_wdev, buffer, buffer_size,
					    SETBIOSDEFAULTS_METHOD_ID);
	if (ret)
		dev_err(&wmi_priv.bios_attr_wdev->dev, "reset BIOS defaults failed: %d\n", ret);

	kfree(buffer);
out:
	mutex_unlock(&wmi_priv.mutex);
	return ret;
}

static int bios_attr_set_interface_probe(struct wmi_device *wdev, const void *context)
{
	mutex_lock(&wmi_priv.mutex);
	wmi_priv.bios_attr_wdev = wdev;
	mutex_unlock(&wmi_priv.mutex);
	return 0;
}

static void bios_attr_set_interface_remove(struct wmi_device *wdev)
{
	mutex_lock(&wmi_priv.mutex);
	wmi_priv.bios_attr_wdev = NULL;
	mutex_unlock(&wmi_priv.mutex);
}

static const struct wmi_device_id bios_attr_set_interface_id_table[] = {
	{ .guid_string = DELL_WMI_BIOS_ATTRIBUTES_INTERFACE_GUID },
	{ },
};
static struct wmi_driver bios_attr_set_interface_driver = {
	.driver = {
		.name = DRIVER_NAME
	},
	.probe = bios_attr_set_interface_probe,
	.remove = bios_attr_set_interface_remove,
	.id_table = bios_attr_set_interface_id_table,
};

int init_bios_attr_set_interface(void)
{
	return wmi_driver_register(&bios_attr_set_interface_driver);
}

void exit_bios_attr_set_interface(void)
{
	wmi_driver_unregister(&bios_attr_set_interface_driver);
}

MODULE_DEVICE_TABLE(wmi, bios_attr_set_interface_id_table);