blob: 1db8d31acf2acaa880f824d5a07e76f84a022dcb [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
Julius Werner71885a42016-08-19 15:24:54 -0700111size_t cbfs_for_each_attr(void *metadata, size_t metadata_size,
112 size_t last_offset)
113{
114 struct cbfs_file_attribute *attr;
115
116 if (!last_offset) {
117 struct cbfs_file *file = metadata;
118 size_t start_offset = read_be32(&file->attributes_offset);
119 if (start_offset <= sizeof(struct cbfs_file) ||
120 start_offset + sizeof(*attr) > metadata_size)
121 return 0;
122 return start_offset;
123 }
124
125 attr = metadata + last_offset;
126 size_t next_offset = last_offset + read_be32(&attr->len);
127
128 if (next_offset + sizeof(*attr) > metadata_size)
129 return 0;
130 return next_offset;
131}
132
133int cbfsf_decompression_info(struct cbfsf *fh, uint32_t *algo, size_t *size)
134{
135 size_t metadata_size = region_device_sz(&fh->metadata);
136 void *metadata = rdev_mmap_full(&fh->metadata);
137 size_t offs = 0;
138
139 if (!metadata)
140 return -1;
141
142 while ((offs = cbfs_for_each_attr(metadata, metadata_size, offs))) {
143 struct cbfs_file_attr_compression *attr = metadata + offs;
144 if (read_be32(&attr->tag) != CBFS_FILE_ATTR_TAG_COMPRESSION)
145 continue;
146
147 *algo = read_be32(&attr->compression);
148 *size = read_be32(&attr->decompressed_size);
149 rdev_munmap(&fh->metadata, metadata);
150 return 0;
151 }
152
153 *algo = CBFS_COMPRESS_NONE;
154 *size = region_device_sz(&fh->data);
155 rdev_munmap(&fh->metadata, metadata);
156 return 0;
157}
158
Aaron Durbincbb6c752015-12-15 15:57:11 -0600159static int cbfsf_file_type(struct cbfsf *fh, uint32_t *ftype)
160{
161 const size_t sz = sizeof(*ftype);
162
163 if (rdev_readat(&fh->metadata, ftype,
164 offsetof(struct cbfs_file, type), sz) != sz)
165 return -1;
166
167 *ftype = read_be32(ftype);
168
169 return 0;
170}
171
Aaron Durbin295d58b2015-12-15 13:33:51 -0600172int cbfs_locate(struct cbfsf *fh, const struct region_device *cbfs,
173 const char *name, uint32_t *type)
174{
175 struct cbfsf *prev;
176
177 LOG("Locating '%s'\n", name);
178
179 prev = NULL;
180
181 while (1) {
182 int ret;
183 char *fname;
184 int name_match;
185 const size_t fsz = sizeof(struct cbfs_file);
186
187 ret = cbfs_for_each_file(cbfs, prev, fh);
188 prev = fh;
189
190 /* Either failed to read or hit the end of the region. */
191 if (ret < 0 || ret > 0)
192 break;
193
194 fname = rdev_mmap(&fh->metadata, fsz,
195 region_device_sz(&fh->metadata) - fsz);
196
197 if (fname == NULL)
198 break;
199
200 name_match = !strcmp(fname, name);
201 rdev_munmap(&fh->metadata, fname);
202
203 if (!name_match) {
204 DEBUG(" Unmatched '%s' at %zx\n", fname,
205 rdev_relative_offset(cbfs, &fh->metadata));
206 continue;
207 }
208
209 if (type != NULL) {
210 uint32_t ftype;
211
Aaron Durbincbb6c752015-12-15 15:57:11 -0600212 if (cbfsf_file_type(fh, &ftype))
Aaron Durbin295d58b2015-12-15 13:33:51 -0600213 break;
214
Aaron Durbin295d58b2015-12-15 13:33:51 -0600215 if (*type != ftype) {
216 DEBUG(" Unmatched type %x at %zx\n", ftype,
217 rdev_relative_offset(cbfs,
218 &fh->metadata));
219 continue;
220 }
221 }
222
223 LOG("Found @ offset %zx size %zx\n",
224 rdev_relative_offset(cbfs, &fh->metadata),
225 region_device_sz(&fh->data));
226
227 /* Success. */
228 return 0;
229 }
230
231 LOG("'%s' not found.\n", name);
232 return -1;
233}
Aaron Durbincbb6c752015-12-15 15:57:11 -0600234
235static int cbfs_extend_hash_buffer(struct vb2_digest_context *ctx,
236 void *buf, size_t sz)
237{
238 return vb2_digest_extend(ctx, buf, sz);
239}
240
241static int cbfs_extend_hash(struct vb2_digest_context *ctx,
242 const struct region_device *rdev)
243{
244 uint8_t buffer[1024];
245 size_t sz_left;
246 size_t offset;
247
248 sz_left = region_device_sz(rdev);
249 offset = 0;
250
251 while (sz_left) {
252 int rv;
253 size_t block_sz = MIN(sz_left, sizeof(buffer));
254
255 if (rdev_readat(rdev, buffer, offset, block_sz) != block_sz)
256 return VB2_ERROR_UNKNOWN;
257
258 rv = cbfs_extend_hash_buffer(ctx, buffer, block_sz);
259
260 if (rv)
261 return rv;
262
263 sz_left -= block_sz;
264 offset += block_sz;
265 }
266
267 return VB2_SUCCESS;
268}
269
270/* Include offsets of child regions within the parent into the hash. */
271static int cbfs_extend_hash_with_offset(struct vb2_digest_context *ctx,
272 const struct region_device *p,
273 const struct region_device *c)
274{
275 int32_t soffset;
276 int rv;
277
278 soffset = rdev_relative_offset(p, c);
279
280 if (soffset < 0)
281 return VB2_ERROR_UNKNOWN;
282
283 /* All offsets in big endian format. */
284 write_be32(&soffset, soffset);
285
286 rv = cbfs_extend_hash_buffer(ctx, &soffset, sizeof(soffset));
287
288 if (rv)
289 return rv;
290
291 return cbfs_extend_hash(ctx, c);
292}
293
294/* Hash in the potential CBFS header sitting at the beginning of the CBFS
295 * region as well as relative offset at the end. */
296static int cbfs_extend_hash_master_header(struct vb2_digest_context *ctx,
297 const struct region_device *cbfs)
298{
299 struct region_device rdev;
300 int rv;
301
302 if (rdev_chain(&rdev, cbfs, 0, sizeof(struct cbfs_header)))
303 return VB2_ERROR_UNKNOWN;
304
305 rv = cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
306
307 if (rv)
308 return rv;
309
310 /* Include potential relative offset at end of region. */
311 if (rdev_chain(&rdev, cbfs, region_device_sz(cbfs) - sizeof(int32_t),
312 sizeof(int32_t)))
313 return VB2_ERROR_UNKNOWN;
314
315 return cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
316}
317
318int cbfs_vb2_hash_contents(const struct region_device *cbfs,
319 enum vb2_hash_algorithm hash_alg, void *digest,
320 size_t digest_sz)
321{
322 struct vb2_digest_context ctx;
323 int rv;
324 struct cbfsf f;
325 struct cbfsf *prev;
326 struct cbfsf *fh;
327
328 rv = vb2_digest_init(&ctx, hash_alg);
329
330 if (rv)
331 return rv;
332
333 rv = cbfs_extend_hash_master_header(&ctx, cbfs);
334 if (rv)
335 return rv;
336
337 prev = NULL;
338 fh = &f;
339
340 while (1) {
341 uint32_t ftype;
342
343 rv = cbfs_for_each_file(cbfs, prev, fh);
344 prev = fh;
345
346 if (rv < 0)
347 return VB2_ERROR_UNKNOWN;
348
349 /* End of CBFS. */
350 if (rv > 0)
351 break;
352
353 rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->metadata);
354
355 if (rv)
356 return rv;
357
358 /* Include data contents in hash if file is non-empty. */
359 if (cbfsf_file_type(fh, &ftype))
360 return VB2_ERROR_UNKNOWN;
361
362 if (ftype == CBFS_TYPE_DELETED || ftype == CBFS_TYPE_DELETED2)
363 continue;
364
365 rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->data);
366
367 if (rv)
368 return rv;
369 }
370
371 return vb2_digest_finalize(&ctx, digest, digest_sz);
372}