blob: 0210a922020cf463395da7ec7c0392e3b229f2f1 [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
Patrick Rudolpheeb4e202018-04-26 10:26:13 +0200159int cbfsf_file_type(struct cbfsf *fh, uint32_t *ftype)
Aaron Durbincbb6c752015-12-15 15:57:11 -0600160{
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
Ronald G. Minnich60fd6842018-05-14 13:27:07 -0700215 if (*type != 0 && *type != ftype) {
Aaron Durbin295d58b2015-12-15 13:33:51 -0600216 DEBUG(" Unmatched type %x at %zx\n", ftype,
217 rdev_relative_offset(cbfs,
218 &fh->metadata));
219 continue;
220 }
Ronald G. Minnich60fd6842018-05-14 13:27:07 -0700221 // *type being 0 means we want to know ftype.
222 // We could just do a blind assignment but
223 // if type is pointing to read-only memory
224 // that might be bad.
225 if (*type == 0)
226 *type = ftype;
Aaron Durbin295d58b2015-12-15 13:33:51 -0600227 }
228
229 LOG("Found @ offset %zx size %zx\n",
230 rdev_relative_offset(cbfs, &fh->metadata),
231 region_device_sz(&fh->data));
232
233 /* Success. */
234 return 0;
235 }
236
237 LOG("'%s' not found.\n", name);
238 return -1;
239}
Aaron Durbincbb6c752015-12-15 15:57:11 -0600240
241static int cbfs_extend_hash_buffer(struct vb2_digest_context *ctx,
242 void *buf, size_t sz)
243{
244 return vb2_digest_extend(ctx, buf, sz);
245}
246
247static int cbfs_extend_hash(struct vb2_digest_context *ctx,
248 const struct region_device *rdev)
249{
250 uint8_t buffer[1024];
251 size_t sz_left;
252 size_t offset;
253
254 sz_left = region_device_sz(rdev);
255 offset = 0;
256
257 while (sz_left) {
258 int rv;
259 size_t block_sz = MIN(sz_left, sizeof(buffer));
260
261 if (rdev_readat(rdev, buffer, offset, block_sz) != block_sz)
262 return VB2_ERROR_UNKNOWN;
263
264 rv = cbfs_extend_hash_buffer(ctx, buffer, block_sz);
265
266 if (rv)
267 return rv;
268
269 sz_left -= block_sz;
270 offset += block_sz;
271 }
272
273 return VB2_SUCCESS;
274}
275
276/* Include offsets of child regions within the parent into the hash. */
277static int cbfs_extend_hash_with_offset(struct vb2_digest_context *ctx,
278 const struct region_device *p,
279 const struct region_device *c)
280{
281 int32_t soffset;
282 int rv;
283
284 soffset = rdev_relative_offset(p, c);
285
286 if (soffset < 0)
287 return VB2_ERROR_UNKNOWN;
288
289 /* All offsets in big endian format. */
290 write_be32(&soffset, soffset);
291
292 rv = cbfs_extend_hash_buffer(ctx, &soffset, sizeof(soffset));
293
294 if (rv)
295 return rv;
296
297 return cbfs_extend_hash(ctx, c);
298}
299
300/* Hash in the potential CBFS header sitting at the beginning of the CBFS
301 * region as well as relative offset at the end. */
302static int cbfs_extend_hash_master_header(struct vb2_digest_context *ctx,
303 const struct region_device *cbfs)
304{
305 struct region_device rdev;
306 int rv;
307
308 if (rdev_chain(&rdev, cbfs, 0, sizeof(struct cbfs_header)))
309 return VB2_ERROR_UNKNOWN;
310
311 rv = cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
312
313 if (rv)
314 return rv;
315
316 /* Include potential relative offset at end of region. */
317 if (rdev_chain(&rdev, cbfs, region_device_sz(cbfs) - sizeof(int32_t),
318 sizeof(int32_t)))
319 return VB2_ERROR_UNKNOWN;
320
321 return cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
322}
323
324int cbfs_vb2_hash_contents(const struct region_device *cbfs,
325 enum vb2_hash_algorithm hash_alg, void *digest,
326 size_t digest_sz)
327{
328 struct vb2_digest_context ctx;
329 int rv;
330 struct cbfsf f;
331 struct cbfsf *prev;
332 struct cbfsf *fh;
333
334 rv = vb2_digest_init(&ctx, hash_alg);
335
336 if (rv)
337 return rv;
338
339 rv = cbfs_extend_hash_master_header(&ctx, cbfs);
340 if (rv)
341 return rv;
342
343 prev = NULL;
344 fh = &f;
345
346 while (1) {
347 uint32_t ftype;
348
349 rv = cbfs_for_each_file(cbfs, prev, fh);
350 prev = fh;
351
352 if (rv < 0)
353 return VB2_ERROR_UNKNOWN;
354
355 /* End of CBFS. */
356 if (rv > 0)
357 break;
358
359 rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->metadata);
360
361 if (rv)
362 return rv;
363
364 /* Include data contents in hash if file is non-empty. */
365 if (cbfsf_file_type(fh, &ftype))
366 return VB2_ERROR_UNKNOWN;
367
368 if (ftype == CBFS_TYPE_DELETED || ftype == CBFS_TYPE_DELETED2)
369 continue;
370
371 rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->data);
372
373 if (rv)
374 return rv;
375 }
376
377 return vb2_digest_finalize(&ctx, digest, digest_sz);
378}