blob: 6f2014b155ce08325cd9ed42f4936435fe49f09e [file] [log] [blame]
Patrick Georgi9360fea2018-03-14 21:11:21 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2018 The Chromium OS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <boot_device.h>
17#include <cbfs.h>
Arthur Heymans8b04dc72018-12-01 15:28:03 +010018#include <fmap.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010019#include <commonlib/region.h>
20#include <console/console.h>
21#include <smmstore.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010022
23/*
24 * The region format is still not finalized, but so far it looks like this:
25 * (
26 * uint32le_t key_sz
27 * uint32le_t value_sz
28 * uint8_t key[key_sz]
29 * uint8_t value[value_sz]
30 * uint8_t active
31 * align to 4 bytes
Elyes HAOUASa342f392018-10-17 10:56:26 +020032 * )*
Patrick Georgi9360fea2018-03-14 21:11:21 +010033 * uint32le_t endmarker = 0xffffffff
34 *
35 * active needs to be set to 0x00 for the entry to be valid. This satisfies
36 * the constraint that entries are either complete or will be ignored, as long
37 * as flash is written sequentially and into a fully erased block.
38 *
39 * Future additions to the format will split the region in half with an active
40 * block marker to allow safe compaction (ie. write the new data in the unused
41 * region, mark it active after the write completed). Otherwise a well-timed
42 * crash/reboot could clear out all variables.
43 */
44
45/*
46 * Return a region device that points into the store file.
47 *
48 * It's the image builder's responsibility to make it block aligned so that
49 * erase works without destroying other data.
50 *
51 * It doesn't cache the location to cope with flash changing underneath (eg
52 * due to an update)
53 *
54 * returns 0 on success, -1 on failure
55 * outputs the valid store rdev in rstore
56 */
57static int lookup_store(struct region_device *rstore)
58{
59 struct cbfsf file;
Julius Wernercd49cce2019-03-05 16:53:33 -080060 if (CONFIG(SMMSTORE_IN_CBFS)) {
Arthur Heymans8b04dc72018-12-01 15:28:03 +010061 if (cbfs_locate_file_in_region(&file,
62 CONFIG_SMMSTORE_REGION,
63 CONFIG_SMMSTORE_FILENAME, NULL) < 0) {
64 printk(BIOS_WARNING, "smm store: "
65 "Unable to find SMM store file in region '%s'\n",
66 CONFIG_SMMSTORE_REGION);
67 return -1;
68 }
Patrick Georgi9360fea2018-03-14 21:11:21 +010069
Arthur Heymans8b04dc72018-12-01 15:28:03 +010070 cbfs_file_data(rstore, &file);
71 } else {
72 if (fmap_locate_area_as_rdev_rw(CONFIG_SMMSTORE_REGION, rstore)) {
73 printk(BIOS_WARNING,
74 "smm store: Unable to find SMM store FMAP region '%s'\n",
75 CONFIG_SMMSTORE_REGION);
76 return -1;
77 }
78 }
Patrick Georgi9360fea2018-03-14 21:11:21 +010079
80 return 0;
81}
82
83/*
84 * Read entire store into user provided buffer
85 *
86 * returns 0 on success, -1 on failure
87 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
88 */
89int smmstore_read_region(void *buf, ssize_t *bufsize)
90{
91 struct region_device store;
92
93 if (bufsize == NULL)
94 return -1;
95
96 *bufsize = 0;
97 if (lookup_store(&store) < 0) {
98 printk(BIOS_WARNING, "reading region failed\n");
99 return -1;
100 }
101
102 ssize_t tx = min(*bufsize, region_device_sz(&store));
103 *bufsize = rdev_readat(&store, buf, 0, tx);
104
105 if (*bufsize < 0)
106 return -1;
107
108 return 0;
109}
110
111/*
112 * Append data to region
113 *
114 * Returns 0 on success, -1 on failure
115 */
116int smmstore_append_data(void *key, uint32_t key_sz,
117 void *value, uint32_t value_sz)
118{
119 struct region_device store;
120
121 if (lookup_store(&store) < 0) {
122 printk(BIOS_WARNING, "reading region failed\n");
123 return -1;
124 }
125
126 ssize_t data_sz = region_device_sz(&store);
127
128 /* scan for end */
129 ssize_t end = 0;
130 uint32_t k_sz, v_sz;
131 while (end < data_sz) {
132 /* make odd corner cases identifiable, eg. invalid v_sz */
133 k_sz = 0;
134
135 if (rdev_readat(&store, &k_sz, end, sizeof(k_sz)) < 0) {
136 printk(BIOS_WARNING, "failed reading key size\n");
137 return -1;
138 }
139
140 /* found the end */
141 if (k_sz == 0xffffffff)
142 break;
143
144 /* something is fishy here:
145 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
146 * other problems are covered by the loop condition
147 */
148 if (k_sz > data_sz) {
149 printk(BIOS_WARNING, "key size out of bounds\n");
150 return -1;
151 }
152
153 if (rdev_readat(&store, &v_sz, end + 4, sizeof(v_sz)) < 0) {
154 printk(BIOS_WARNING, "failed reading value size\n");
155 return -1;
156 }
157
158 if (v_sz > data_sz) {
159 printk(BIOS_WARNING, "value size out of bounds\n");
160 return -1;
161 }
162
163 end += 8 + k_sz + v_sz + 1;
164 end = ALIGN_UP(end, sizeof(uint32_t));
165 }
166
167 printk(BIOS_WARNING, "used smm store size might be 0x%zx bytes\n", end);
168
169 if (k_sz != 0xffffffff) {
170 printk(BIOS_WARNING,
171 "eof of data marker looks invalid: 0x%x\n", k_sz);
172 return -1;
173 }
174
175 printk(BIOS_WARNING, "used size looks legit\n");
176
177 printk(BIOS_WARNING, "open (%zx, %zx) for writing\n",
178 region_device_offset(&store), region_device_sz(&store));
179 if (boot_device_rw_subregion(&store.region, &store) < 0) {
180 printk(BIOS_WARNING, "couldn't open store for writing\n");
181 return -1;
182 }
183
184 uint32_t record_sz = 8 + key_sz + value_sz + 1;
185 if (end + record_sz >= data_sz) {
186 printk(BIOS_WARNING, "not enough space for new data\n");
187 return -1;
188 }
189
190 if (rdev_writeat(&store, &key_sz, end, 4) != 4) {
191 printk(BIOS_WARNING, "failed writing key size\n");
192 }
193 end += 4;
194 if (rdev_writeat(&store, &value_sz, end, 4) != 4) {
195 printk(BIOS_WARNING, "failed writing value size\n");
196 }
197 end += 4;
198 if (rdev_writeat(&store, key, end, key_sz) != key_sz) {
199 printk(BIOS_WARNING, "failed writing key data\n");
200 }
201 end += key_sz;
202 if (rdev_writeat(&store, value, end, value_sz) != value_sz) {
203 printk(BIOS_WARNING, "failed writing value data\n");
204 }
205 end += value_sz;
206 uint8_t nul = 0;
207 if (rdev_writeat(&store, &nul, end, 1) != 1) {
208 printk(BIOS_WARNING, "failed writing termination\n");
209 }
210
211 return 0;
212}
213
214/*
215 * Clear region
216 *
217 * Returns 0 on success, -1 on failure, including partial erase
218 */
219int smmstore_clear_region(void)
220{
221 struct region_device store;
222
223 if (lookup_store(&store) < 0) {
224 printk(BIOS_WARNING, "smm store: reading region failed\n");
225 return -1;
226 }
227
228 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
229 if (res != region_device_sz(&store)) {
230 printk(BIOS_WARNING, "smm store: erasing region failed\n");
231 return -1;
232 }
233
234 return 0;
235}