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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Functions corresponding to methods under BIOS interface GUID
* for use with hp-bioscfg driver.
*
* Copyright (c) 2022 Hewlett-Packard Inc.
*/
#include <linux/wmi.h>
#include "bioscfg.h"
/*
* struct bios_args buffer is dynamically allocated. New WMI command types
* were introduced that exceeds 128-byte data size. Changes to handle
* the data size allocation scheme were kept in hp_wmi_perform_query function.
*/
struct bios_args {
u32 signature;
u32 command;
u32 commandtype;
u32 datasize;
u8 data[] __counted_by(datasize);
};
/**
* hp_set_attribute
*
* @a_name: The attribute name
* @a_value: The attribute value
*
* Sets an attribute to new value
*
* Returns zero on success
* -ENODEV if device is not found
* -EINVAL if the instance of 'Setup Admin' password is not found.
* -ENOMEM unable to allocate memory
*/
int hp_set_attribute(const char *a_name, const char *a_value)
{
int security_area_size;
int a_name_size, a_value_size;
u16 *buffer = NULL;
u16 *start;
int buffer_size, instance, ret;
char *auth_token_choice;
mutex_lock(&bioscfg_drv.mutex);
instance = hp_get_password_instance_for_type(SETUP_PASSWD);
if (instance < 0) {
ret = -EINVAL;
goto out_set_attribute;
}
/* Select which auth token to use; password or [auth token] */
if (bioscfg_drv.spm_data.auth_token)
auth_token_choice = bioscfg_drv.spm_data.auth_token;
else
auth_token_choice = bioscfg_drv.password_data[instance].current_password;
a_name_size = hp_calculate_string_buffer(a_name);
a_value_size = hp_calculate_string_buffer(a_value);
security_area_size = hp_calculate_security_buffer(auth_token_choice);
buffer_size = a_name_size + a_value_size + security_area_size;
buffer = kmalloc(buffer_size + 1, GFP_KERNEL);
if (!buffer) {
ret = -ENOMEM;
goto out_set_attribute;
}
/* build variables to set */
start = buffer;
start = hp_ascii_to_utf16_unicode(start, a_name);
if (!start) {
ret = -EINVAL;
goto out_set_attribute;
}
start = hp_ascii_to_utf16_unicode(start, a_value);
if (!start) {
ret = -EINVAL;
goto out_set_attribute;
}
ret = hp_populate_security_buffer(start, auth_token_choice);
if (ret < 0)
goto out_set_attribute;
ret = hp_wmi_set_bios_setting(buffer, buffer_size);
out_set_attribute:
kfree(buffer);
mutex_unlock(&bioscfg_drv.mutex);
return ret;
}
/**
* hp_wmi_perform_query
*
* @query: The commandtype (enum hp_wmi_commandtype)
* @command: The command (enum hp_wmi_command)
* @buffer: Buffer used as input and/or output
* @insize: Size of input buffer
* @outsize: Size of output buffer
*
* returns zero on success
* an HP WMI query specific error code (which is positive)
* -EINVAL if the query was not successful at all
* -EINVAL if the output buffer size exceeds buffersize
*
* Note: The buffersize must at least be the maximum of the input and output
* size. E.g. Battery info query is defined to have 1 byte input
* and 128 byte output. The caller would do:
* buffer = kzalloc(128, GFP_KERNEL);
* ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ,
* buffer, 1, 128)
*/
int hp_wmi_perform_query(int query, enum hp_wmi_command command, void *buffer,
u32 insize, u32 outsize)
{
struct acpi_buffer input, output = { ACPI_ALLOCATE_BUFFER, NULL };
struct bios_return *bios_return;
union acpi_object *obj = NULL;
struct bios_args *args = NULL;
int mid, actual_outsize, ret;
size_t bios_args_size;
mid = hp_encode_outsize_for_pvsz(outsize);
if (WARN_ON(mid < 0))
return mid;
bios_args_size = struct_size(args, data, insize);
args = kmalloc(bios_args_size, GFP_KERNEL);
if (!args)
return -ENOMEM;
input.length = bios_args_size;
input.pointer = args;
/* BIOS expects 'SECU' in hex as the signature value*/
args->signature = 0x55434553;
args->command = command;
args->commandtype = query;
args->datasize = insize;
memcpy(args->data, buffer, flex_array_size(args, data, insize));
ret = wmi_evaluate_method(HP_WMI_BIOS_GUID, 0, mid, &input, &output);
if (ret)
goto out_free;
obj = output.pointer;
if (!obj) {
ret = -EINVAL;
goto out_free;
}
if (obj->type != ACPI_TYPE_BUFFER ||
obj->buffer.length < sizeof(*bios_return)) {
pr_warn("query 0x%x returned wrong type or too small buffer\n", query);
ret = -EINVAL;
goto out_free;
}
bios_return = (struct bios_return *)obj->buffer.pointer;
ret = bios_return->return_code;
if (ret) {
if (ret != INVALID_CMD_VALUE && ret != INVALID_CMD_TYPE)
pr_warn("query 0x%x returned error 0x%x\n", query, ret);
goto out_free;
}
/* Ignore output data of zero size */
if (!outsize)
goto out_free;
actual_outsize = min_t(u32, outsize, obj->buffer.length - sizeof(*bios_return));
memcpy_and_pad(buffer, outsize, obj->buffer.pointer + sizeof(*bios_return),
actual_outsize, 0);
out_free:
ret = hp_wmi_error_and_message(ret);
kfree(obj);
kfree(args);
return ret;
}
static void *utf16_empty_string(u16 *p)
{
*p++ = 2;
*p++ = 0x00;
return p;
}
/**
* hp_ascii_to_utf16_unicode - Convert ascii string to UTF-16 unicode
*
* BIOS supports UTF-16 characters that are 2 bytes long. No variable
* multi-byte language supported.
*
* @p: Unicode buffer address
* @str: string to convert to unicode
*
* Returns a void pointer to the buffer string
*/
void *hp_ascii_to_utf16_unicode(u16 *p, const u8 *str)
{
int len = strlen(str);
int ret;
/*
* Add null character when reading an empty string
* "02 00 00 00"
*/
if (len == 0)
return utf16_empty_string(p);
/* Move pointer len * 2 number of bytes */
*p++ = len * 2;
ret = utf8s_to_utf16s(str, strlen(str), UTF16_HOST_ENDIAN, p, len);
if (ret < 0) {
dev_err(bioscfg_drv.class_dev, "UTF16 conversion failed\n");
return NULL;
}
if (ret * sizeof(u16) > U16_MAX) {
dev_err(bioscfg_drv.class_dev, "Error string too long\n");
return NULL;
}
p += len;
return p;
}
/**
* hp_wmi_set_bios_setting - Set setting's value in BIOS
*
* @input_buffer: Input buffer address
* @input_size: Input buffer size
*
* Returns: Count of unicode characters written to BIOS if successful, otherwise
* -ENOMEM unable to allocate memory
* -EINVAL buffer not allocated or too small
*/
int hp_wmi_set_bios_setting(u16 *input_buffer, u32 input_size)
{
union acpi_object *obj;
struct acpi_buffer input = {input_size, input_buffer};
struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
int ret;
ret = wmi_evaluate_method(HP_WMI_SET_BIOS_SETTING_GUID, 0, 1, &input, &output);
obj = output.pointer;
if (!obj)
return -EINVAL;
if (obj->type != ACPI_TYPE_INTEGER) {
ret = -EINVAL;
goto out_free;
}
ret = obj->integer.value;
if (ret) {
ret = hp_wmi_error_and_message(ret);
goto out_free;
}
out_free:
kfree(obj);
return ret;
}
static int hp_attr_set_interface_probe(struct wmi_device *wdev, const void *context)
{
mutex_lock(&bioscfg_drv.mutex);
mutex_unlock(&bioscfg_drv.mutex);
return 0;
}
static void hp_attr_set_interface_remove(struct wmi_device *wdev)
{
mutex_lock(&bioscfg_drv.mutex);
mutex_unlock(&bioscfg_drv.mutex);
}
static const struct wmi_device_id hp_attr_set_interface_id_table[] = {
{ .guid_string = HP_WMI_BIOS_GUID},
{ }
};
static struct wmi_driver hp_attr_set_interface_driver = {
.driver = {
.name = DRIVER_NAME,
},
.probe = hp_attr_set_interface_probe,
.remove = hp_attr_set_interface_remove,
.id_table = hp_attr_set_interface_id_table,
};
int hp_init_attr_set_interface(void)
{
return wmi_driver_register(&hp_attr_set_interface_driver);
}
void hp_exit_attr_set_interface(void)
{
wmi_driver_unregister(&hp_attr_set_interface_driver);
}
MODULE_DEVICE_TABLE(wmi, hp_attr_set_interface_id_table);
|