blob: 56faf286efac08f3f18f46782f42d39dd1a5384a [file] [log] [blame]
Aaron Durbin295d58b2015-12-15 13:33:51 -06001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2015 Google Inc.
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 <console/console.h>
17#include <commonlib/cbfs.h>
18#include <commonlib/endian.h>
19#include <commonlib/helpers.h>
20#include <string.h>
21
22#if !defined(ERROR)
23#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
24#endif
25#if !defined(LOG)
26#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
27#endif
28#if defined(IS_ENABLED)
29
30#if IS_ENABLED(CONFIG_DEBUG_CBFS)
31#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
32#else
33#define DEBUG(x...)
34#endif
35
36#elif !defined(DEBUG)
37#define DEBUG(x...)
38#endif
39
40static size_t cbfs_next_offset(const struct region_device *cbfs,
41 const struct cbfsf *f)
42{
43 size_t offset;
44
45 if (f == NULL)
46 return 0;
47
48 /* The region_device objects store absolute offets over the whole
49 * region. Therefore a relative offset needs to be calculated. */
50 offset = rdev_relative_offset(cbfs, &f->data);
51 offset += region_device_sz(&f->data);
52
53 return ALIGN_UP(offset, CBFS_ALIGNMENT);
54}
55
56static int cbfs_end(const struct region_device *cbfs, size_t offset)
57{
58 if (offset >= region_device_sz(cbfs))
59 return 1;
60
61 return 0;
62}
63
64int cbfs_for_each_file(const struct region_device *cbfs,
65 const struct cbfsf *prev, struct cbfsf *fh)
66{
67 size_t offset;
68
69 offset = cbfs_next_offset(cbfs, prev);
70
71 /* Try to scan the entire cbfs region looking for file name. */
72 while (1) {
73 struct cbfs_file file;
74 const size_t fsz = sizeof(file);
75
76 DEBUG("Checking offset %zx\n", offset);
77
78 /* End of region. */
79 if (cbfs_end(cbfs, offset))
80 return 1;
81
82 /* Can't read file. Nothing else to do but bail out. */
83 if (rdev_readat(cbfs, &file, offset, fsz) != fsz)
84 break;
85
86 if (memcmp(file.magic, CBFS_FILE_MAGIC, sizeof(file.magic))) {
87 offset++;
88 offset = ALIGN_UP(offset, CBFS_ALIGNMENT);
89 continue;
90 }
91
92 file.len = read_be32(&file.len);
93 file.offset = read_be32(&file.offset);
94
95 DEBUG("File @ offset %zx size %x\n", offset, file.len);
96
97 /* Keep track of both the metadata and the data for the file. */
98 if (rdev_chain(&fh->metadata, cbfs, offset, file.offset))
99 break;
100
101 if (rdev_chain(&fh->data, cbfs, offset + file.offset, file.len))
102 break;
103
104 /* Success. */
105 return 0;
106 }
107
108 return -1;
109}
110
Aaron Durbincbb6c752015-12-15 15:57:11 -0600111static int cbfsf_file_type(struct cbfsf *fh, uint32_t *ftype)
112{
113 const size_t sz = sizeof(*ftype);
114
115 if (rdev_readat(&fh->metadata, ftype,
116 offsetof(struct cbfs_file, type), sz) != sz)
117 return -1;
118
119 *ftype = read_be32(ftype);
120
121 return 0;
122}
123
Aaron Durbin295d58b2015-12-15 13:33:51 -0600124int cbfs_locate(struct cbfsf *fh, const struct region_device *cbfs,
125 const char *name, uint32_t *type)
126{
127 struct cbfsf *prev;
128
129 LOG("Locating '%s'\n", name);
130
131 prev = NULL;
132
133 while (1) {
134 int ret;
135 char *fname;
136 int name_match;
137 const size_t fsz = sizeof(struct cbfs_file);
138
139 ret = cbfs_for_each_file(cbfs, prev, fh);
140 prev = fh;
141
142 /* Either failed to read or hit the end of the region. */
143 if (ret < 0 || ret > 0)
144 break;
145
146 fname = rdev_mmap(&fh->metadata, fsz,
147 region_device_sz(&fh->metadata) - fsz);
148
149 if (fname == NULL)
150 break;
151
152 name_match = !strcmp(fname, name);
153 rdev_munmap(&fh->metadata, fname);
154
155 if (!name_match) {
156 DEBUG(" Unmatched '%s' at %zx\n", fname,
157 rdev_relative_offset(cbfs, &fh->metadata));
158 continue;
159 }
160
161 if (type != NULL) {
162 uint32_t ftype;
163
Aaron Durbincbb6c752015-12-15 15:57:11 -0600164 if (cbfsf_file_type(fh, &ftype))
Aaron Durbin295d58b2015-12-15 13:33:51 -0600165 break;
166
Aaron Durbin295d58b2015-12-15 13:33:51 -0600167 if (*type != ftype) {
168 DEBUG(" Unmatched type %x at %zx\n", ftype,
169 rdev_relative_offset(cbfs,
170 &fh->metadata));
171 continue;
172 }
173 }
174
175 LOG("Found @ offset %zx size %zx\n",
176 rdev_relative_offset(cbfs, &fh->metadata),
177 region_device_sz(&fh->data));
178
179 /* Success. */
180 return 0;
181 }
182
183 LOG("'%s' not found.\n", name);
184 return -1;
185}
Aaron Durbincbb6c752015-12-15 15:57:11 -0600186
187static int cbfs_extend_hash_buffer(struct vb2_digest_context *ctx,
188 void *buf, size_t sz)
189{
190 return vb2_digest_extend(ctx, buf, sz);
191}
192
193static int cbfs_extend_hash(struct vb2_digest_context *ctx,
194 const struct region_device *rdev)
195{
196 uint8_t buffer[1024];
197 size_t sz_left;
198 size_t offset;
199
200 sz_left = region_device_sz(rdev);
201 offset = 0;
202
203 while (sz_left) {
204 int rv;
205 size_t block_sz = MIN(sz_left, sizeof(buffer));
206
207 if (rdev_readat(rdev, buffer, offset, block_sz) != block_sz)
208 return VB2_ERROR_UNKNOWN;
209
210 rv = cbfs_extend_hash_buffer(ctx, buffer, block_sz);
211
212 if (rv)
213 return rv;
214
215 sz_left -= block_sz;
216 offset += block_sz;
217 }
218
219 return VB2_SUCCESS;
220}
221
222/* Include offsets of child regions within the parent into the hash. */
223static int cbfs_extend_hash_with_offset(struct vb2_digest_context *ctx,
224 const struct region_device *p,
225 const struct region_device *c)
226{
227 int32_t soffset;
228 int rv;
229
230 soffset = rdev_relative_offset(p, c);
231
232 if (soffset < 0)
233 return VB2_ERROR_UNKNOWN;
234
235 /* All offsets in big endian format. */
236 write_be32(&soffset, soffset);
237
238 rv = cbfs_extend_hash_buffer(ctx, &soffset, sizeof(soffset));
239
240 if (rv)
241 return rv;
242
243 return cbfs_extend_hash(ctx, c);
244}
245
246/* Hash in the potential CBFS header sitting at the beginning of the CBFS
247 * region as well as relative offset at the end. */
248static int cbfs_extend_hash_master_header(struct vb2_digest_context *ctx,
249 const struct region_device *cbfs)
250{
251 struct region_device rdev;
252 int rv;
253
254 if (rdev_chain(&rdev, cbfs, 0, sizeof(struct cbfs_header)))
255 return VB2_ERROR_UNKNOWN;
256
257 rv = cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
258
259 if (rv)
260 return rv;
261
262 /* Include potential relative offset at end of region. */
263 if (rdev_chain(&rdev, cbfs, region_device_sz(cbfs) - sizeof(int32_t),
264 sizeof(int32_t)))
265 return VB2_ERROR_UNKNOWN;
266
267 return cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
268}
269
270int cbfs_vb2_hash_contents(const struct region_device *cbfs,
271 enum vb2_hash_algorithm hash_alg, void *digest,
272 size_t digest_sz)
273{
274 struct vb2_digest_context ctx;
275 int rv;
276 struct cbfsf f;
277 struct cbfsf *prev;
278 struct cbfsf *fh;
279
280 rv = vb2_digest_init(&ctx, hash_alg);
281
282 if (rv)
283 return rv;
284
285 rv = cbfs_extend_hash_master_header(&ctx, cbfs);
286 if (rv)
287 return rv;
288
289 prev = NULL;
290 fh = &f;
291
292 while (1) {
293 uint32_t ftype;
294
295 rv = cbfs_for_each_file(cbfs, prev, fh);
296 prev = fh;
297
298 if (rv < 0)
299 return VB2_ERROR_UNKNOWN;
300
301 /* End of CBFS. */
302 if (rv > 0)
303 break;
304
305 rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->metadata);
306
307 if (rv)
308 return rv;
309
310 /* Include data contents in hash if file is non-empty. */
311 if (cbfsf_file_type(fh, &ftype))
312 return VB2_ERROR_UNKNOWN;
313
314 if (ftype == CBFS_TYPE_DELETED || ftype == CBFS_TYPE_DELETED2)
315 continue;
316
317 rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->data);
318
319 if (rv)
320 return rv;
321 }
322
323 return vb2_digest_finalize(&ctx, digest, digest_sz);
324}