mb/google/brya: Create rull variant
[coreboot2.git] / src / drivers / smmstore / smi.c
blob4f7b4bdac0a4091744c0d84180f4723a3b1efdd1
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <console/console.h>
4 #include <commonlib/region.h>
5 #include <cpu/x86/smm.h>
6 #include <smmstore.h>
7 #include <stddef.h>
8 #include <stdint.h>
11 * Check that the given range is legal.
13 * Legal means:
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))
21 return -1;
23 return 0;
26 static uint32_t smmstorev1_exec(uint8_t command, void *param)
28 uint32_t ret = SMMSTORE_RET_FAILURE;
30 switch (command) {
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)
36 break;
38 if (range_check(params->buf, params->bufsize) != 0)
39 break;
41 if (smmstore_read_region(params->buf, &params->bufsize) == 0)
42 ret = SMMSTORE_RET_SUCCESS;
43 break;
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)
51 break;
52 if (range_check(params->key, params->keysize) != 0)
53 break;
54 if (range_check(params->val, params->valsize) != 0)
55 break;
57 if (smmstore_append_data(params->key, params->keysize,
58 params->val, params->valsize) == 0)
59 ret = SMMSTORE_RET_SUCCESS;
60 break;
63 case SMMSTORE_CMD_CLEAR: {
64 if (smmstore_clear_region() == 0)
65 ret = SMMSTORE_RET_SUCCESS;
66 break;
68 default:
69 printk(BIOS_DEBUG,
70 "Unknown SMM store v1 command: 0x%02x\n", command);
71 ret = SMMSTORE_RET_UNSUPPORTED;
72 break;
75 return ret;
78 static uint32_t smmstorev2_exec(uint8_t command, void *param)
80 uint32_t ret = SMMSTORE_RET_FAILURE;
81 static bool initialized = false;
83 if (!initialized) {
84 uintptr_t base;
85 size_t size;
86 smm_get_smmstore_com_buffer(&base, &size);
88 if (smmstore_init((void *)base, size))
89 return SMMSTORE_RET_FAILURE;
90 initialized = true;
93 switch (command) {
94 case SMMSTORE_CMD_RAW_READ: {
95 printk(BIOS_DEBUG, "Raw read from SMM store, param = %p\n", param);
96 struct smmstore_params_raw_read *params = param;
98 if (range_check(params, sizeof(*params)) != 0)
99 break;
101 if (smmstore_rawread_region(params->block_id, params->bufoffset,
102 params->bufsize) == 0)
103 ret = SMMSTORE_RET_SUCCESS;
104 break;
106 case SMMSTORE_CMD_RAW_WRITE: {
107 printk(BIOS_DEBUG, "Raw write to SMM store, param = %p\n", param);
108 struct smmstore_params_raw_write *params = param;
110 if (range_check(params, sizeof(*params)) != 0)
111 break;
113 if (smmstore_rawwrite_region(params->block_id, params->bufoffset,
114 params->bufsize) == 0)
115 ret = SMMSTORE_RET_SUCCESS;
116 break;
118 case SMMSTORE_CMD_RAW_CLEAR: {
119 printk(BIOS_DEBUG, "Raw clear SMM store, param = %p\n", param);
120 struct smmstore_params_raw_clear *params = param;
122 if (range_check(params, sizeof(*params)) != 0)
123 break;
125 if (smmstore_rawclear_region(params->block_id) == 0)
126 ret = SMMSTORE_RET_SUCCESS;
127 break;
129 default:
130 printk(BIOS_DEBUG,
131 "Unknown SMM store v2 command: 0x%02x\n", command);
132 ret = SMMSTORE_RET_UNSUPPORTED;
133 break;
136 return ret;
139 uint32_t smmstore_exec(uint8_t command, void *param)
141 if (command != SMMSTORE_CMD_CLEAR && !param)
142 return SMMSTORE_RET_FAILURE;
144 if (CONFIG(SMMSTORE_V2))
145 return smmstorev2_exec(command, param);
146 else
147 return smmstorev1_exec(command, param);