blob: 23d2af015ffe426cba8af3695407adcff5b7ad1c [file] [log] [blame]
Patrick Georgi9360fea2018-03-14 21:11:21 +01001/*
2 * This file is part of the coreboot project.
3 *
Patrick Georgi9360fea2018-03-14 21:11:21 +01004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <boot_device.h>
15#include <cbfs.h>
Arthur Heymans8b04dc72018-12-01 15:28:03 +010016#include <fmap.h>
Elyes HAOUAS2195f7a2019-06-21 07:20:12 +020017#include <commonlib/helpers.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010018#include <commonlib/region.h>
19#include <console/console.h>
20#include <smmstore.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +020021#include <stdlib.h>
22#include <types.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010023
24/*
25 * The region format is still not finalized, but so far it looks like this:
26 * (
27 * uint32le_t key_sz
28 * uint32le_t value_sz
29 * uint8_t key[key_sz]
30 * uint8_t value[value_sz]
31 * uint8_t active
32 * align to 4 bytes
Elyes HAOUASa342f392018-10-17 10:56:26 +020033 * )*
Patrick Georgi9360fea2018-03-14 21:11:21 +010034 * uint32le_t endmarker = 0xffffffff
35 *
36 * active needs to be set to 0x00 for the entry to be valid. This satisfies
37 * the constraint that entries are either complete or will be ignored, as long
38 * as flash is written sequentially and into a fully erased block.
39 *
40 * Future additions to the format will split the region in half with an active
41 * block marker to allow safe compaction (ie. write the new data in the unused
42 * region, mark it active after the write completed). Otherwise a well-timed
43 * crash/reboot could clear out all variables.
44 */
45
Arthur Heymansed50d852018-12-26 11:20:59 +010046static enum cb_err lookup_store_region(struct region *region)
47{
48 if (CONFIG(SMMSTORE_IN_CBFS)) {
49 struct cbfsf file;
50 if (cbfs_locate_file_in_region(&file,
51 CONFIG_SMMSTORE_REGION,
52 CONFIG_SMMSTORE_FILENAME, NULL) < 0) {
53 printk(BIOS_WARNING,
54 "smm store: Unable to find SMM store file in region '%s'\n",
55 CONFIG_SMMSTORE_REGION);
56 return CB_ERR;
57 }
58 struct region_device rdev;
59 cbfs_file_data(&rdev, &file);
60 *region = *region_device_region(&rdev);
61 } else {
62 if (fmap_locate_area(CONFIG_SMMSTORE_REGION, region)) {
63 printk(BIOS_WARNING,
64 "smm store: Unable to find SMM store FMAP region '%s'\n",
65 CONFIG_SMMSTORE_REGION);
66 return CB_ERR;
67 }
68 }
69
70 return CB_SUCCESS;
71}
72
Patrick Georgi9360fea2018-03-14 21:11:21 +010073/*
74 * Return a region device that points into the store file.
75 *
76 * It's the image builder's responsibility to make it block aligned so that
77 * erase works without destroying other data.
78 *
79 * It doesn't cache the location to cope with flash changing underneath (eg
80 * due to an update)
81 *
82 * returns 0 on success, -1 on failure
83 * outputs the valid store rdev in rstore
84 */
85static int lookup_store(struct region_device *rstore)
86{
Arthur Heymansed50d852018-12-26 11:20:59 +010087 static struct region_device read_rdev, write_rdev;
88 static struct incoherent_rdev store_irdev;
89 struct region region;
90 const struct region_device *rdev;
Patrick Georgi9360fea2018-03-14 21:11:21 +010091
Arthur Heymansed50d852018-12-26 11:20:59 +010092 if (lookup_store_region(&region) != CB_SUCCESS)
93 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +010094
Arthur Heymansed50d852018-12-26 11:20:59 +010095 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
96 return -1;
97
98 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
99 return -1;
100
101 rdev = incoherent_rdev_init(&store_irdev, &region, &read_rdev, &write_rdev);
102
103 if (rdev == NULL)
104 return -1;
105
106 return rdev_chain(rstore, rdev, 0, region_device_sz(rdev));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100107}
108
109/*
110 * Read entire store into user provided buffer
111 *
112 * returns 0 on success, -1 on failure
113 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
114 */
115int smmstore_read_region(void *buf, ssize_t *bufsize)
116{
117 struct region_device store;
118
119 if (bufsize == NULL)
120 return -1;
121
Patrick Georgi9360fea2018-03-14 21:11:21 +0100122 if (lookup_store(&store) < 0) {
123 printk(BIOS_WARNING, "reading region failed\n");
124 return -1;
125 }
126
Arthur Heymansed50d852018-12-26 11:20:59 +0100127 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100128 *bufsize = rdev_readat(&store, buf, 0, tx);
129
130 if (*bufsize < 0)
131 return -1;
132
133 return 0;
134}
135
Arthur Heymansed50d852018-12-26 11:20:59 +0100136static enum cb_err scan_end(struct region_device *store)
Patrick Georgi9360fea2018-03-14 21:11:21 +0100137{
Patrick Georgi9360fea2018-03-14 21:11:21 +0100138 /* scan for end */
139 ssize_t end = 0;
140 uint32_t k_sz, v_sz;
Arthur Heymansed50d852018-12-26 11:20:59 +0100141 const ssize_t data_sz = region_device_sz(store);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100142 while (end < data_sz) {
143 /* make odd corner cases identifiable, eg. invalid v_sz */
144 k_sz = 0;
145
Arthur Heymansed50d852018-12-26 11:20:59 +0100146 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100147 printk(BIOS_WARNING, "failed reading key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100148 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100149 }
150
151 /* found the end */
152 if (k_sz == 0xffffffff)
153 break;
154
155 /* something is fishy here:
156 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
157 * other problems are covered by the loop condition
158 */
159 if (k_sz > data_sz) {
160 printk(BIOS_WARNING, "key size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100161 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100162 }
163
Arthur Heymansed50d852018-12-26 11:20:59 +0100164 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100165 printk(BIOS_WARNING, "failed reading value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100166 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100167 }
168
169 if (v_sz > data_sz) {
170 printk(BIOS_WARNING, "value size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100171 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100172 }
173
Arthur Heymansed50d852018-12-26 11:20:59 +0100174 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100175 end = ALIGN_UP(end, sizeof(uint32_t));
176 }
177
Arthur Heymansed50d852018-12-26 11:20:59 +0100178 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100179
180 if (k_sz != 0xffffffff) {
181 printk(BIOS_WARNING,
182 "eof of data marker looks invalid: 0x%x\n", k_sz);
Arthur Heymansed50d852018-12-26 11:20:59 +0100183 return CB_ERR;
184 }
185
186 if (rdev_chain(store, store, end, data_sz - end))
187 return CB_ERR;
188
189 return CB_SUCCESS;
190
191}
192/*
193 * Append data to region
194 *
195 * Returns 0 on success, -1 on failure
196 */
197int smmstore_append_data(void *key, uint32_t key_sz, void *value,
198 uint32_t value_sz)
199{
200 struct region_device store;
201
202 if (lookup_store(&store) < 0) {
203 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100204 return -1;
205 }
206
Arthur Heymansed50d852018-12-26 11:20:59 +0100207 ssize_t offset = 0;
208 ssize_t size;
209 uint8_t nul = 0;
210 if (scan_end(&store) != CB_SUCCESS)
211 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100212
Arthur Heymansed50d852018-12-26 11:20:59 +0100213 printk(BIOS_DEBUG, "used size looks legit\n");
214
215 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100216 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100217
Arthur Heymansed50d852018-12-26 11:20:59 +0100218 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
219 + sizeof(nul);
220 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100221 printk(BIOS_WARNING, "not enough space for new data\n");
222 return -1;
223 }
224
Arthur Heymansed50d852018-12-26 11:20:59 +0100225 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
226 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100227 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100228 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100229 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100230 offset += sizeof(key_sz);
231 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
232 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100233 printk(BIOS_WARNING, "failed writing value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100234 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100235 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100236 offset += sizeof(value_sz);
237 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100238 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100239 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100240 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100241 offset += key_sz;
242 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100243 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100244 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100245 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100246 offset += value_sz;
247 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100248 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100249 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100250 }
251
252 return 0;
253}
254
255/*
256 * Clear region
257 *
258 * Returns 0 on success, -1 on failure, including partial erase
259 */
260int smmstore_clear_region(void)
261{
262 struct region_device store;
263
264 if (lookup_store(&store) < 0) {
265 printk(BIOS_WARNING, "smm store: reading region failed\n");
266 return -1;
267 }
268
269 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
270 if (res != region_device_sz(&store)) {
271 printk(BIOS_WARNING, "smm store: erasing region failed\n");
272 return -1;
273 }
274
275 return 0;
276}