1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <commonlib/region.h>
5 #include <cpu/x86/smm.h>
11 * Check that the given range is legal.
14 * - not pointing into SMRAM
16 * returns 0 on success, -1 on failure
18 static int range_check(void *start
, size_t size
)
20 if (smm_points_to_smram(start
, size
))
26 static uint32_t smmstorev1_exec(uint8_t command
, void *param
)
28 uint32_t ret
= SMMSTORE_RET_FAILURE
;
31 case SMMSTORE_CMD_READ
: {
32 printk(BIOS_DEBUG
, "Reading from SMM store\n");
33 struct smmstore_params_read
*params
= param
;
35 if (range_check(params
, sizeof(*params
)) != 0)
38 if (range_check(params
->buf
, params
->bufsize
) != 0)
41 if (smmstore_read_region(params
->buf
, ¶ms
->bufsize
) == 0)
42 ret
= SMMSTORE_RET_SUCCESS
;
46 case SMMSTORE_CMD_APPEND
: {
47 printk(BIOS_DEBUG
, "Appending into SMM store\n");
48 struct smmstore_params_append
*params
= param
;
50 if (range_check(params
, sizeof(*params
)) != 0)
52 if (range_check(params
->key
, params
->keysize
) != 0)
54 if (range_check(params
->val
, params
->valsize
) != 0)
57 if (smmstore_append_data(params
->key
, params
->keysize
,
58 params
->val
, params
->valsize
) == 0)
59 ret
= SMMSTORE_RET_SUCCESS
;
63 case SMMSTORE_CMD_CLEAR
: {
64 if (smmstore_clear_region() == 0)
65 ret
= SMMSTORE_RET_SUCCESS
;
70 "Unknown SMM store v1 command: 0x%02x\n", command
);
71 ret
= SMMSTORE_RET_UNSUPPORTED
;
78 static uint32_t smmstorev2_exec(uint8_t command
, void *param
)
80 uint32_t ret
= SMMSTORE_RET_FAILURE
;
83 case SMMSTORE_CMD_INIT
: {
84 printk(BIOS_DEBUG
, "Init SMM store\n");
85 struct smmstore_params_init
*params
= param
;
87 if (range_check(params
, sizeof(*params
)) != 0)
90 void *buf
= (void *)(uintptr_t)params
->com_buffer
;
92 if (range_check(buf
, params
->com_buffer_size
) != 0)
95 if (smmstore_init(buf
, params
->com_buffer_size
) == 0)
96 ret
= SMMSTORE_RET_SUCCESS
;
99 case SMMSTORE_CMD_RAW_READ
: {
100 printk(BIOS_DEBUG
, "Raw read from SMM store, param = %p\n", param
);
101 struct smmstore_params_raw_read
*params
= param
;
103 if (range_check(params
, sizeof(*params
)) != 0)
106 if (smmstore_rawread_region(params
->block_id
, params
->bufoffset
,
107 params
->bufsize
) == 0)
108 ret
= SMMSTORE_RET_SUCCESS
;
111 case SMMSTORE_CMD_RAW_WRITE
: {
112 printk(BIOS_DEBUG
, "Raw write to SMM store, param = %p\n", param
);
113 struct smmstore_params_raw_write
*params
= param
;
115 if (range_check(params
, sizeof(*params
)) != 0)
118 if (smmstore_rawwrite_region(params
->block_id
, params
->bufoffset
,
119 params
->bufsize
) == 0)
120 ret
= SMMSTORE_RET_SUCCESS
;
123 case SMMSTORE_CMD_RAW_CLEAR
: {
124 printk(BIOS_DEBUG
, "Raw clear SMM store, param = %p\n", param
);
125 struct smmstore_params_raw_clear
*params
= param
;
127 if (range_check(params
, sizeof(*params
)) != 0)
130 if (smmstore_rawclear_region(params
->block_id
) == 0)
131 ret
= SMMSTORE_RET_SUCCESS
;
136 "Unknown SMM store v2 command: 0x%02x\n", command
);
137 ret
= SMMSTORE_RET_UNSUPPORTED
;
144 uint32_t smmstore_exec(uint8_t command
, void *param
)
146 if (command
!= SMMSTORE_CMD_CLEAR
&& !param
)
147 return SMMSTORE_RET_FAILURE
;
149 if (CONFIG(SMMSTORE_V2
))
150 return smmstorev2_exec(command
, param
);
152 return smmstorev1_exec(command
, param
);