blob: 4463bad84aa1b575bfb9cbdd2ee068552b33766f [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>
18#include <commonlib/region.h>
19#include <console/console.h>
20#include <smmstore.h>
21#include <string.h>
22
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;
60 if (cbfs_locate_file_in_region(&file,
61 CONFIG_SMMSTORE_REGION, CONFIG_SMMSTORE_FILENAME, NULL) < 0) {
62 printk(BIOS_WARNING, "smm store: "
63 "Unable to find SMM store file in region '%s'\n",
64 CONFIG_SMMSTORE_REGION);
65 return -1;
66 }
67
68 cbfs_file_data(rstore, &file);
69
70 return 0;
71}
72
73/*
74 * Read entire store into user provided buffer
75 *
76 * returns 0 on success, -1 on failure
77 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
78 */
79int smmstore_read_region(void *buf, ssize_t *bufsize)
80{
81 struct region_device store;
82
83 if (bufsize == NULL)
84 return -1;
85
86 *bufsize = 0;
87 if (lookup_store(&store) < 0) {
88 printk(BIOS_WARNING, "reading region failed\n");
89 return -1;
90 }
91
92 ssize_t tx = min(*bufsize, region_device_sz(&store));
93 *bufsize = rdev_readat(&store, buf, 0, tx);
94
95 if (*bufsize < 0)
96 return -1;
97
98 return 0;
99}
100
101/*
102 * Append data to region
103 *
104 * Returns 0 on success, -1 on failure
105 */
106int smmstore_append_data(void *key, uint32_t key_sz,
107 void *value, uint32_t value_sz)
108{
109 struct region_device store;
110
111 if (lookup_store(&store) < 0) {
112 printk(BIOS_WARNING, "reading region failed\n");
113 return -1;
114 }
115
116 ssize_t data_sz = region_device_sz(&store);
117
118 /* scan for end */
119 ssize_t end = 0;
120 uint32_t k_sz, v_sz;
121 while (end < data_sz) {
122 /* make odd corner cases identifiable, eg. invalid v_sz */
123 k_sz = 0;
124
125 if (rdev_readat(&store, &k_sz, end, sizeof(k_sz)) < 0) {
126 printk(BIOS_WARNING, "failed reading key size\n");
127 return -1;
128 }
129
130 /* found the end */
131 if (k_sz == 0xffffffff)
132 break;
133
134 /* something is fishy here:
135 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
136 * other problems are covered by the loop condition
137 */
138 if (k_sz > data_sz) {
139 printk(BIOS_WARNING, "key size out of bounds\n");
140 return -1;
141 }
142
143 if (rdev_readat(&store, &v_sz, end + 4, sizeof(v_sz)) < 0) {
144 printk(BIOS_WARNING, "failed reading value size\n");
145 return -1;
146 }
147
148 if (v_sz > data_sz) {
149 printk(BIOS_WARNING, "value size out of bounds\n");
150 return -1;
151 }
152
153 end += 8 + k_sz + v_sz + 1;
154 end = ALIGN_UP(end, sizeof(uint32_t));
155 }
156
157 printk(BIOS_WARNING, "used smm store size might be 0x%zx bytes\n", end);
158
159 if (k_sz != 0xffffffff) {
160 printk(BIOS_WARNING,
161 "eof of data marker looks invalid: 0x%x\n", k_sz);
162 return -1;
163 }
164
165 printk(BIOS_WARNING, "used size looks legit\n");
166
167 printk(BIOS_WARNING, "open (%zx, %zx) for writing\n",
168 region_device_offset(&store), region_device_sz(&store));
169 if (boot_device_rw_subregion(&store.region, &store) < 0) {
170 printk(BIOS_WARNING, "couldn't open store for writing\n");
171 return -1;
172 }
173
174 uint32_t record_sz = 8 + key_sz + value_sz + 1;
175 if (end + record_sz >= data_sz) {
176 printk(BIOS_WARNING, "not enough space for new data\n");
177 return -1;
178 }
179
180 if (rdev_writeat(&store, &key_sz, end, 4) != 4) {
181 printk(BIOS_WARNING, "failed writing key size\n");
182 }
183 end += 4;
184 if (rdev_writeat(&store, &value_sz, end, 4) != 4) {
185 printk(BIOS_WARNING, "failed writing value size\n");
186 }
187 end += 4;
188 if (rdev_writeat(&store, key, end, key_sz) != key_sz) {
189 printk(BIOS_WARNING, "failed writing key data\n");
190 }
191 end += key_sz;
192 if (rdev_writeat(&store, value, end, value_sz) != value_sz) {
193 printk(BIOS_WARNING, "failed writing value data\n");
194 }
195 end += value_sz;
196 uint8_t nul = 0;
197 if (rdev_writeat(&store, &nul, end, 1) != 1) {
198 printk(BIOS_WARNING, "failed writing termination\n");
199 }
200
201 return 0;
202}
203
204/*
205 * Clear region
206 *
207 * Returns 0 on success, -1 on failure, including partial erase
208 */
209int smmstore_clear_region(void)
210{
211 struct region_device store;
212
213 if (lookup_store(&store) < 0) {
214 printk(BIOS_WARNING, "smm store: reading region failed\n");
215 return -1;
216 }
217
218 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
219 if (res != region_device_sz(&store)) {
220 printk(BIOS_WARNING, "smm store: erasing region failed\n");
221 return -1;
222 }
223
224 return 0;
225}