summaryrefslogtreecommitdiff
path: root/src/ec/google/chromeec/vstore.c
blob: d973ba96ca6a988606a20aecb2a1d45d7f77da7f (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
/* SPDX-License-Identifier: GPL-2.0-only */
/* This file is part of the coreboot project. */

#include <stdint.h>
#include <string.h>
#include "ec.h"
#include "ec_commands.h"

/*
 * google_chromeec_vstore_supported - Check if vstore is supported
 */
int google_chromeec_vstore_supported(void)
{
	return google_chromeec_check_feature(EC_FEATURE_VSTORE);
}

/*
 * google_chromeec_vstore_info - Query EC for vstore information
 *
 * Returns the number of vstore slots supported by the EC, with the
 * mask of locked slots saved into passed parameter if it is present.
 */
int google_chromeec_vstore_info(uint32_t *locked)
{
	struct ec_response_vstore_info info;
	struct chromeec_command cmd = {
		.cmd_code     = EC_CMD_VSTORE_INFO,
		.cmd_size_out = sizeof(info),
		.cmd_data_out = &info,
	};

	if (google_chromeec_command(&cmd) != 0)
		return 0;

	if (locked)
		*locked = info.slot_locked;
	return info.slot_count;
}

/*
 * google_chromeec_vstore_read - Read data from EC vstore slot
 *
 * @slot: vstore slot to read from
 * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
 */
int google_chromeec_vstore_read(int slot, uint8_t *data)
{
	struct ec_params_vstore_read req = {
		.slot         = slot,
	};
	struct chromeec_command cmd = {
		.cmd_code     = EC_CMD_VSTORE_READ,
		.cmd_size_in  = sizeof(req),
		.cmd_data_in  = &req,
		.cmd_size_out = EC_VSTORE_SLOT_SIZE,
		.cmd_data_out = data,
	};

	if (!data || req.slot >= EC_VSTORE_SLOT_MAX)
		return -1;

	return google_chromeec_command(&cmd);
}

/*
 * google_chromeec_vstore_write - Save data into EC vstore slot
 *
 * @slot: vstore slot to write into
 * @data: data to write
 * @size: size of data in bytes
 *
 * Maximum size of data is EC_VSTORE_SLOT_SIZE.  It is the callers
 * responsibility to check the number of implemented slots by
 * querying the vstore info.
 */
int google_chromeec_vstore_write(int slot, uint8_t *data, size_t size)
{
	struct ec_params_vstore_write req = {
		.slot         = slot,
	};
	struct chromeec_command cmd = {
		.cmd_code     = EC_CMD_VSTORE_WRITE,
		.cmd_size_in  = sizeof(req),
		.cmd_data_in  = &req,
	};

	if (req.slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE)
		return -1;

	memcpy(req.data, data, size);

	return google_chromeec_command(&cmd);
}