Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
Patrick Rudolph | 2fdabd9 | 2020-05-06 10:57:01 +0200 | [diff] [blame] | 4 | #include <commonlib/region.h> |
Patrick Rudolph | 0f82309 | 2020-07-27 16:06:34 +0200 | [diff] [blame] | 5 | #include <cpu/x86/smm.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 6 | #include <smmstore.h> |
| 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | /* |
| 11 | * Check that the given range is legal. |
| 12 | * |
| 13 | * Legal means: |
| 14 | * - not pointing into SMRAM |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 15 | * |
| 16 | * returns 0 on success, -1 on failure |
| 17 | */ |
| 18 | static int range_check(void *start, size_t size) |
| 19 | { |
Patrick Rudolph | 2fdabd9 | 2020-05-06 10:57:01 +0200 | [diff] [blame] | 20 | if (smm_points_to_smram(start, size)) |
| 21 | return -1; |
| 22 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 23 | return 0; |
| 24 | } |
| 25 | |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 26 | static uint32_t smmstorev1_exec(uint8_t command, void *param) |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 27 | { |
| 28 | uint32_t ret = SMMSTORE_RET_FAILURE; |
| 29 | |
| 30 | switch (command) { |
| 31 | case SMMSTORE_CMD_READ: { |
| 32 | printk(BIOS_DEBUG, "Reading from SMM store\n"); |
| 33 | struct smmstore_params_read *params = param; |
| 34 | |
Patrick Rudolph | 2fdabd9 | 2020-05-06 10:57:01 +0200 | [diff] [blame] | 35 | if (range_check(params, sizeof(*params)) != 0) |
| 36 | break; |
| 37 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 38 | if (range_check(params->buf, params->bufsize) != 0) |
| 39 | break; |
| 40 | |
| 41 | if (smmstore_read_region(params->buf, ¶ms->bufsize) == 0) |
| 42 | ret = SMMSTORE_RET_SUCCESS; |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | case SMMSTORE_CMD_APPEND: { |
| 47 | printk(BIOS_DEBUG, "Appending into SMM store\n"); |
| 48 | struct smmstore_params_append *params = param; |
| 49 | |
Patrick Rudolph | 2fdabd9 | 2020-05-06 10:57:01 +0200 | [diff] [blame] | 50 | if (range_check(params, sizeof(*params)) != 0) |
| 51 | break; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 52 | if (range_check(params->key, params->keysize) != 0) |
| 53 | break; |
| 54 | if (range_check(params->val, params->valsize) != 0) |
| 55 | break; |
| 56 | |
| 57 | if (smmstore_append_data(params->key, params->keysize, |
| 58 | params->val, params->valsize) == 0) |
| 59 | ret = SMMSTORE_RET_SUCCESS; |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | case SMMSTORE_CMD_CLEAR: { |
| 64 | if (smmstore_clear_region() == 0) |
| 65 | ret = SMMSTORE_RET_SUCCESS; |
| 66 | break; |
| 67 | } |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 68 | default: |
| 69 | printk(BIOS_DEBUG, |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 70 | "Unknown SMM store v1 command: 0x%02x\n", command); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 71 | ret = SMMSTORE_RET_UNSUPPORTED; |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 77 | |
| 78 | static uint32_t smmstorev2_exec(uint8_t command, void *param) |
| 79 | { |
| 80 | uint32_t ret = SMMSTORE_RET_FAILURE; |
Arthur Heymans | d57d5e3 | 2023-12-27 20:54:19 +0100 | [diff] [blame^] | 81 | static bool initialized = false; |
| 82 | |
| 83 | if (!initialized) { |
| 84 | uintptr_t base; |
| 85 | size_t size; |
| 86 | smm_get_smmstore_com_buffer(&base, &size); |
| 87 | |
| 88 | if (smmstore_init((void *)base, size)) |
| 89 | return SMMSTORE_RET_FAILURE; |
| 90 | initialized = true; |
| 91 | } |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 92 | |
| 93 | switch (command) { |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 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; |
| 97 | |
| 98 | if (range_check(params, sizeof(*params)) != 0) |
| 99 | break; |
| 100 | |
| 101 | if (smmstore_rawread_region(params->block_id, params->bufoffset, |
| 102 | params->bufsize) == 0) |
| 103 | ret = SMMSTORE_RET_SUCCESS; |
| 104 | break; |
| 105 | } |
| 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; |
| 109 | |
| 110 | if (range_check(params, sizeof(*params)) != 0) |
| 111 | break; |
| 112 | |
| 113 | if (smmstore_rawwrite_region(params->block_id, params->bufoffset, |
| 114 | params->bufsize) == 0) |
| 115 | ret = SMMSTORE_RET_SUCCESS; |
| 116 | break; |
| 117 | } |
| 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; |
| 121 | |
| 122 | if (range_check(params, sizeof(*params)) != 0) |
| 123 | break; |
| 124 | |
| 125 | if (smmstore_rawclear_region(params->block_id) == 0) |
| 126 | ret = SMMSTORE_RET_SUCCESS; |
| 127 | break; |
| 128 | } |
| 129 | default: |
| 130 | printk(BIOS_DEBUG, |
| 131 | "Unknown SMM store v2 command: 0x%02x\n", command); |
| 132 | ret = SMMSTORE_RET_UNSUPPORTED; |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | uint32_t smmstore_exec(uint8_t command, void *param) |
| 140 | { |
Tim Crawford | e78e909 | 2021-01-19 12:29:21 -0700 | [diff] [blame] | 141 | if (command != SMMSTORE_CMD_CLEAR && !param) |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 142 | return SMMSTORE_RET_FAILURE; |
| 143 | |
| 144 | if (CONFIG(SMMSTORE_V2)) |
| 145 | return smmstorev2_exec(command, param); |
| 146 | else |
| 147 | return smmstorev1_exec(command, param); |
| 148 | } |