xref: /aosp_15_r20/external/coreboot/src/ec/google/chromeec/vstore.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdint.h>
4 #include <string.h>
5 #include "ec.h"
6 #include "ec_commands.h"
7 
8 /*
9  * google_chromeec_vstore_supported - Check if vstore is supported
10  */
google_chromeec_vstore_supported(void)11 int google_chromeec_vstore_supported(void)
12 {
13 	return google_chromeec_check_feature(EC_FEATURE_VSTORE);
14 }
15 
16 /*
17  * google_chromeec_vstore_info - Query EC for vstore information
18  *
19  * Returns the number of vstore slots supported by the EC, with the
20  * mask of locked slots saved into passed parameter if it is present.
21  */
google_chromeec_vstore_info(uint32_t * locked)22 int google_chromeec_vstore_info(uint32_t *locked)
23 {
24 	struct ec_response_vstore_info info;
25 	struct chromeec_command cmd = {
26 		.cmd_code     = EC_CMD_VSTORE_INFO,
27 		.cmd_size_out = sizeof(info),
28 		.cmd_data_out = &info,
29 	};
30 
31 	if (google_chromeec_command(&cmd) != 0)
32 		return 0;
33 
34 	if (locked)
35 		*locked = info.slot_locked;
36 	return info.slot_count;
37 }
38 
39 /*
40  * google_chromeec_vstore_read - Read data from EC vstore slot
41  *
42  * @slot: vstore slot to read from
43  * @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes
44  */
google_chromeec_vstore_read(int slot,uint8_t * data)45 int google_chromeec_vstore_read(int slot, uint8_t *data)
46 {
47 	struct ec_params_vstore_read req = {
48 		.slot         = slot,
49 	};
50 	struct chromeec_command cmd = {
51 		.cmd_code     = EC_CMD_VSTORE_READ,
52 		.cmd_size_in  = sizeof(req),
53 		.cmd_data_in  = &req,
54 		.cmd_size_out = EC_VSTORE_SLOT_SIZE,
55 		.cmd_data_out = data,
56 	};
57 
58 	if (!data || req.slot >= EC_VSTORE_SLOT_MAX)
59 		return -1;
60 
61 	return google_chromeec_command(&cmd);
62 }
63 
64 /*
65  * google_chromeec_vstore_write - Save data into EC vstore slot
66  *
67  * @slot: vstore slot to write into
68  * @data: data to write
69  * @size: size of data in bytes
70  *
71  * Maximum size of data is EC_VSTORE_SLOT_SIZE.  It is the callers
72  * responsibility to check the number of implemented slots by
73  * querying the vstore info.
74  */
google_chromeec_vstore_write(int slot,uint8_t * data,size_t size)75 int google_chromeec_vstore_write(int slot, uint8_t *data, size_t size)
76 {
77 	struct ec_params_vstore_write req = {
78 		.slot         = slot,
79 	};
80 	struct chromeec_command cmd = {
81 		.cmd_code     = EC_CMD_VSTORE_WRITE,
82 		.cmd_size_in  = sizeof(req),
83 		.cmd_data_in  = &req,
84 	};
85 
86 	if (req.slot >= EC_VSTORE_SLOT_MAX || size > EC_VSTORE_SLOT_SIZE)
87 		return -1;
88 
89 	memcpy(req.data, data, size);
90 
91 	return google_chromeec_command(&cmd);
92 }
93