Debian Codegraph @

Documentation/drivers/smmstorev2.md

raw symbols SCIP
1 # SMM based flash storage driver Version 2
2
3 This documents the API exposed by the x86 system management based
4 storage driver.
5
6 ## SMMSTOREv2
7
8 SMMSTOREv2 is a [SMM] mediated driver to read from, write to and erase
9 a predefined region in flash. It can be enabled by setting
10 `CONFIG_SMMSTORE=y` and `CONFIG_SMMSTORE_V2=y` in menuconfig.
11
12 This can be used by the OS or the payload to implement persistent
13 storage to hold for instance configuration data, without needing to
14 implement a (platform specific) storage driver in the payload itself.
15
16 ### Storage size and alignment
17
18 SMMSTORE version 2 requires a minimum alignment of 64 KiB, which should
19 be supported by all flash chips. Not having to perform read-modify-write
20 operations is desired, as it reduces complexity and potential for bugs.
21
22 This can be used by a FTW (FaultTolerantWrite) implementation that uses
23 at least two regions in an A/B update scheme. The FTW implementation in
24 edk2 uses three different regions in the store:
25
26 - The variable store
27 - The FTW spare block
28 - The FTW working block
29
30 All regions must be block-aligned, and the FTW spare size must be larger
31 than that of the variable store. FTW working block can be much smaller.
32 With 64 KiB as block size, the minimum size of the FTW-enabled store is:
33
34 - The variable store: 1 block = 64 KiB
35 - The FTW spare block: 2 blocks = 2 * 64 KiB
36 - The FTW working block: 1 block = 64 KiB
37
38 Therefore, the minimum size for edk2 FTW is 4 blocks, or 256 KiB.
39
40 ## API
41
42 The API provides read and write access to an unformatted block storage.
43
44 ### Storage region
45
46 By default SMMSTOREv2 will operate on a separate FMAP region called
47 `SMMSTORE`. The default generated FMAP will include such a region. On
48 systems with a locked FMAP, e.g. in an existing vboot setup with a
49 locked RO region, the option exists to add a cbfsfile called `smm_store`
50 in the `RW_LEGACY` (if CHROMEOS) or in the `COREBOOT` FMAP regions. It
51 is recommended for new builds using a handcrafted FMD that intend to
52 make use of SMMSTORE to include a sufficiently large `SMMSTORE` FMAP
53 region. It is mandatory to align the `SMMSTORE` region to 64KiB for
54 compatibility with the largest flash erase operation.
55
56 When a default generated FMAP is used, the size of the FMAP region is
57 equal to `CONFIG_SMMSTORE_SIZE`. UEFI payloads expect at least 64 KiB.
58 To support a fault tolerant write mechanism, at least a multiple of
59 this size is recommended.
60
61 ### Communication buffer
62
63 To prevent malicious ring0 code to access arbitrary memory locations,
64 SMMSTOREv2 uses a communication buffer in CBMEM/HOB for all transfers.
65 This buffer has to be at least 64 KiB in size and must be installed
66 before calling any of the SMMSTORE read or write operations. Usually,
67 coreboot will install this buffer to transfer data between ring0 and
68 the [SMM] handler.
69
70 In order to get the communication buffer address, the payload or OS
71 has to read the coreboot table with tag `0x0039`, containing:
72
73 ```C
74 struct lb_smmstorev2 {
75 uint32_t tag;
76 uint32_t size;
77 uint32_t num_blocks; /* Number of writable blocks in SMM */
78 uint32_t block_size; /* Size of a block in byte. Default: 64 KiB */
79 uint32_t mmap_addr_deprecated; /* 32-bit MMIO address of the store for read only access.
80 Prefer 'mmap_addr' for new software.
81 Zero when the address won't fit into 32-bits. */
82 uint32_t com_buffer; /* Physical address of the communication buffer */
83 uint32_t com_buffer_size; /* Size of the communication buffer in bytes */
84 uint8_t apm_cmd; /* The command byte to write to the APM I/O port */
85 uint8_t unused[3]; /* Set to zero */
86 uint64_t mmap_addr; /* 64-bit MMIO address of the store for read only access.
87 Introduced after the initial implementation. Users of
88 this table must check the 'size' field to detect if its
89 written out by coreboot. */
90 };
91 ```
92
93 The absence of this coreboot table entry indicates that there's no
94 SMMSTOREv2 support.
95
96 `mmap_addr` is an optional field added after the initial implementation.
97 Users of this table must check the size field to know if it's written by coreboot.
98 In case it's not present 'mmap_addr_deprecated' is to be used as the SPI ROM MMIO
99 address and it must be below 4 GiB.
100
101 ### Blocks
102
103 The SMMSTOREv2 splits the SMMSTORE FMAP partition into smaller chunks
104 called *blocks*. Every block is at least the size of 64KiB to support
105 arbitrary NOR flash erase ops. A payload or OS must make no further
106 assumptions about the block or communication buffer size.
107
108 ### Generating the SMI
109
110 SMMSTOREv2 is called via an SMI, which is generated via a write to the
111 IO port defined in the smi_cmd entry of the FADT ACPI table. `%al`
112 contains `APM_CNT_SMMSTORE=0xed` and is written to the smi_cmd IO
113 port. `%ah` contains the SMMSTOREv2 command. `%ebx` contains the
114 parameter buffer to the SMMSTOREv2 command.
115
116 ### Return values
117
118 If a command succeeds, SMMSTOREv2 will return with
119 `SMMSTORE_RET_SUCCESS=0` in `%eax`. On failure SMMSTORE will return
120 `SMMSTORE_RET_FAILURE=1`. For unsupported SMMSTORE commands
121 `SMMSTORE_REG_UNSUPPORTED=2` is returned.
122
123 **NOTE 1**: The caller **must** check the return value and should make
124 no assumption on the returned data if `%eax` does not contain
125 `SMMSTORE_RET_SUCCESS`.
126
127 **NOTE 2**: If the SMI returns without changing `%ax`, it can be assumed
128 that the SMMSTOREv2 feature is not installed.
129
130 ### Calling arguments
131
132 SMMSTOREv2 supports 3 subcommands that are passed via `%ah`, the
133 additional calling arguments are passed via `%ebx`.
134
135 **NOTE**: The size of the struct entries are in the native word size of
136 smihandler. This means 32 bits in almost all cases.
137
138 #### - SMMSTORE_CMD_INIT_DEPRECATED = 4
139
140 Unused, returns SMMSTORE_REG_UNSUPPORTED.
141
142 #### - SMMSTORE_CMD_RAW_READ = 5
143
144 SMMSTOREv2 allows reading arbitrary data. It is up to the caller to
145 initialize the store with meaningful data before using it.
146
147 The additional parameter buffer `%ebx` contains a pointer to the
148 following struct:
149
150 ```C
151 struct smmstore_params_raw_read {
152 uint32_t bufsize;
153 uint32_t bufoffset;
154 uint32_t block_id;
155 } __packed;
156 ```
157
158 INPUT:
159 - `bufsize`: Size of data to read within the communication buffer
160 - `bufoffset`: Offset within the communication buffer
161 - `block_id`: Block to read from
162
163 #### - SMMSTORE_CMD_RAW_WRITE = 6
164
165 SMMSTOREv2 allows writing arbitrary data. It is up to the caller to
166 erase a block before writing it.
167
168 The additional parameter buffer `%ebx` contains a pointer to
169 the following struct:
170
171 ```C
172 struct smmstore_params_raw_write {
173 uint32_t bufsize;
174 uint32_t bufoffset;
175 uint32_t block_id;
176 } __packed;
177 ```
178
179 INPUT:
180 - `bufsize`: Size of data to write within the communication buffer
181 - `bufoffset`: Offset within the communication buffer
182 - `block_id`: Block to write to
183
184 #### - SMMSTORE_CMD_RAW_CLEAR = 7
185
186 SMMSTOREv2 allows clearing blocks. A cleared block will read as `0xff`.
187 By providing multiple blocks the caller can implement a fault tolerant
188 write mechanism. It is up to the caller to clear blocks before writing
189 to them.
190
191
192 ```C
193 struct smmstore_params_raw_clear {
194 uint32_t block_id;
195 } __packed;
196 ```
197
198 INPUT:
199 - `block_id`: Block to erase
200
201 #### Security
202
203 Pointers provided by the payload or OS are checked to not overlap with
204 SMM. This protects the SMM handler from being compromised.
205
206 As all information is exchanged using the communication buffer and
207 coreboot tables, there's no risk that a malicious application capable
208 of issuing SMIs could extract arbitrary data or modify the currently
209 running kernel.
210
211 ## Capsule update API
212
213 Availability of this command is tied to `CONFIG_DRIVERS_EFI_UPDATE_CAPSULES`.
214
215 To allow updating full flash content (except if locked at hardware
216 level), few new calls were added. They reuse communication buffer, SMI
217 command, return values and calling arguments of SMMSTORE commands listed
218 above, with the exception of subcommand passed via `%ah`. If the
219 subcommand is to operate on full flash size, it has the highest bit set,
220 e.g. it is `0x85` for `SMMSTORE_CMD_RAW_READ` and `0x86` for
221 `SMMSTORE_CMD_RAW_WRITE`. Every `block_id` describes block relative to
222 the beginning of a flash, maximum value depends on its size.
223
224 Attempts to write the protected memory regions can lead to undesired
225 consequences ranging from system instability to bricking and security
226 vulnerabilities. When this feature is used, care must be taken to temporarily
227 lift protections for the duration of an update when the whole flash is
228 rewritten or the update must be constrained to affect only writable portions of
229 the flash (e.g., "BIOS" region).
230
231 There is one new subcommand that must be called before any other subcommands
232 with highest bit set can be used.
233
234 ### - SMMSTORE_CMD_USE_FULL_FLASH = 0x80
235
236 This command can only be executed once and is done by the firmware.
237 Calling this function at runtime has no effect. It takes one additional
238 parameter that, contrary to other commands, isn't a pointer. Instead,
239 `%ebx` indicates requested state of full flash access. If it equals 0,
240 commands for accessing full flash are permanently disabled, otherwise
241 they are permanently enabled until the next boot.
242
243 The assumption is that if capsule updates are enabled at build time and
244 whole flash access is enabled at runtime, a UEFI payload (highly likely
245 EDK2 or its derivative) won't allow a regular OS to boot if the handler is
246 enabled without rebooting first. There could be a way of deactivating the
247 handler, but coreboot, having no way of enforcing its usage, might as well
248 permit access until a reboot and rely on the payload to do the right thing.
249
250 ## External links
251
252 * [A Tour Beyond BIOS Implementing UEFI Authenticated Variables in SMM with EDK II](https://github.com/tianocore-docs/Docs/raw/master/White_Papers/A_Tour_Beyond_BIOS_Implementing_UEFI_Authenticated_Variables_in_SMM_with_EDKII_V2.pdf)
253
254 Note that this differs significantly from coreboot's implementation.
255
256 [SMM]: ../security/smm.md