1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/*
* This file is part of the coreboot project.
*
* Copyright 2015 Google Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <console/console.h>
#include <commonlib/cbfs.h>
#include <commonlib/endian.h>
#include <commonlib/helpers.h>
#include <string.h>
#if !defined(ERROR)
#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
#endif
#if !defined(LOG)
#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
#endif
#if defined(IS_ENABLED)
#if IS_ENABLED(CONFIG_DEBUG_CBFS)
#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
#else
#define DEBUG(x...)
#endif
#elif !defined(DEBUG)
#define DEBUG(x...)
#endif
static size_t cbfs_next_offset(const struct region_device *cbfs,
const struct cbfsf *f)
{
size_t offset;
if (f == NULL)
return 0;
/* The region_device objects store absolute offets over the whole
* region. Therefore a relative offset needs to be calculated. */
offset = rdev_relative_offset(cbfs, &f->data);
offset += region_device_sz(&f->data);
return ALIGN_UP(offset, CBFS_ALIGNMENT);
}
static int cbfs_end(const struct region_device *cbfs, size_t offset)
{
if (offset >= region_device_sz(cbfs))
return 1;
return 0;
}
int cbfs_for_each_file(const struct region_device *cbfs,
const struct cbfsf *prev, struct cbfsf *fh)
{
size_t offset;
offset = cbfs_next_offset(cbfs, prev);
/* Try to scan the entire cbfs region looking for file name. */
while (1) {
struct cbfs_file file;
const size_t fsz = sizeof(file);
DEBUG("Checking offset %zx\n", offset);
/* End of region. */
if (cbfs_end(cbfs, offset))
return 1;
/* Can't read file. Nothing else to do but bail out. */
if (rdev_readat(cbfs, &file, offset, fsz) != fsz)
break;
if (memcmp(file.magic, CBFS_FILE_MAGIC, sizeof(file.magic))) {
offset++;
offset = ALIGN_UP(offset, CBFS_ALIGNMENT);
continue;
}
file.len = read_be32(&file.len);
file.offset = read_be32(&file.offset);
DEBUG("File @ offset %zx size %x\n", offset, file.len);
/* Keep track of both the metadata and the data for the file. */
if (rdev_chain(&fh->metadata, cbfs, offset, file.offset))
break;
if (rdev_chain(&fh->data, cbfs, offset + file.offset, file.len))
break;
/* Success. */
return 0;
}
return -1;
}
size_t cbfs_for_each_attr(void *metadata, size_t metadata_size,
size_t last_offset)
{
struct cbfs_file_attribute *attr;
if (!last_offset) {
struct cbfs_file *file = metadata;
size_t start_offset = read_be32(&file->attributes_offset);
if (start_offset <= sizeof(struct cbfs_file) ||
start_offset + sizeof(*attr) > metadata_size)
return 0;
return start_offset;
}
attr = metadata + last_offset;
size_t next_offset = last_offset + read_be32(&attr->len);
if (next_offset + sizeof(*attr) > metadata_size)
return 0;
return next_offset;
}
int cbfsf_decompression_info(struct cbfsf *fh, uint32_t *algo, size_t *size)
{
size_t metadata_size = region_device_sz(&fh->metadata);
void *metadata = rdev_mmap_full(&fh->metadata);
size_t offs = 0;
if (!metadata)
return -1;
while ((offs = cbfs_for_each_attr(metadata, metadata_size, offs))) {
struct cbfs_file_attr_compression *attr = metadata + offs;
if (read_be32(&attr->tag) != CBFS_FILE_ATTR_TAG_COMPRESSION)
continue;
*algo = read_be32(&attr->compression);
*size = read_be32(&attr->decompressed_size);
rdev_munmap(&fh->metadata, metadata);
return 0;
}
*algo = CBFS_COMPRESS_NONE;
*size = region_device_sz(&fh->data);
rdev_munmap(&fh->metadata, metadata);
return 0;
}
static int cbfsf_file_type(struct cbfsf *fh, uint32_t *ftype)
{
const size_t sz = sizeof(*ftype);
if (rdev_readat(&fh->metadata, ftype,
offsetof(struct cbfs_file, type), sz) != sz)
return -1;
*ftype = read_be32(ftype);
return 0;
}
int cbfs_locate(struct cbfsf *fh, const struct region_device *cbfs,
const char *name, uint32_t *type)
{
struct cbfsf *prev;
LOG("Locating '%s'\n", name);
prev = NULL;
while (1) {
int ret;
char *fname;
int name_match;
const size_t fsz = sizeof(struct cbfs_file);
ret = cbfs_for_each_file(cbfs, prev, fh);
prev = fh;
/* Either failed to read or hit the end of the region. */
if (ret < 0 || ret > 0)
break;
fname = rdev_mmap(&fh->metadata, fsz,
region_device_sz(&fh->metadata) - fsz);
if (fname == NULL)
break;
name_match = !strcmp(fname, name);
rdev_munmap(&fh->metadata, fname);
if (!name_match) {
DEBUG(" Unmatched '%s' at %zx\n", fname,
rdev_relative_offset(cbfs, &fh->metadata));
continue;
}
if (type != NULL) {
uint32_t ftype;
if (cbfsf_file_type(fh, &ftype))
break;
if (*type != ftype) {
DEBUG(" Unmatched type %x at %zx\n", ftype,
rdev_relative_offset(cbfs,
&fh->metadata));
continue;
}
}
LOG("Found @ offset %zx size %zx\n",
rdev_relative_offset(cbfs, &fh->metadata),
region_device_sz(&fh->data));
/* Success. */
return 0;
}
LOG("'%s' not found.\n", name);
return -1;
}
static int cbfs_extend_hash_buffer(struct vb2_digest_context *ctx,
void *buf, size_t sz)
{
return vb2_digest_extend(ctx, buf, sz);
}
static int cbfs_extend_hash(struct vb2_digest_context *ctx,
const struct region_device *rdev)
{
uint8_t buffer[1024];
size_t sz_left;
size_t offset;
sz_left = region_device_sz(rdev);
offset = 0;
while (sz_left) {
int rv;
size_t block_sz = MIN(sz_left, sizeof(buffer));
if (rdev_readat(rdev, buffer, offset, block_sz) != block_sz)
return VB2_ERROR_UNKNOWN;
rv = cbfs_extend_hash_buffer(ctx, buffer, block_sz);
if (rv)
return rv;
sz_left -= block_sz;
offset += block_sz;
}
return VB2_SUCCESS;
}
/* Include offsets of child regions within the parent into the hash. */
static int cbfs_extend_hash_with_offset(struct vb2_digest_context *ctx,
const struct region_device *p,
const struct region_device *c)
{
int32_t soffset;
int rv;
soffset = rdev_relative_offset(p, c);
if (soffset < 0)
return VB2_ERROR_UNKNOWN;
/* All offsets in big endian format. */
write_be32(&soffset, soffset);
rv = cbfs_extend_hash_buffer(ctx, &soffset, sizeof(soffset));
if (rv)
return rv;
return cbfs_extend_hash(ctx, c);
}
/* Hash in the potential CBFS header sitting at the beginning of the CBFS
* region as well as relative offset at the end. */
static int cbfs_extend_hash_master_header(struct vb2_digest_context *ctx,
const struct region_device *cbfs)
{
struct region_device rdev;
int rv;
if (rdev_chain(&rdev, cbfs, 0, sizeof(struct cbfs_header)))
return VB2_ERROR_UNKNOWN;
rv = cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
if (rv)
return rv;
/* Include potential relative offset at end of region. */
if (rdev_chain(&rdev, cbfs, region_device_sz(cbfs) - sizeof(int32_t),
sizeof(int32_t)))
return VB2_ERROR_UNKNOWN;
return cbfs_extend_hash_with_offset(ctx, cbfs, &rdev);
}
int cbfs_vb2_hash_contents(const struct region_device *cbfs,
enum vb2_hash_algorithm hash_alg, void *digest,
size_t digest_sz)
{
struct vb2_digest_context ctx;
int rv;
struct cbfsf f;
struct cbfsf *prev;
struct cbfsf *fh;
rv = vb2_digest_init(&ctx, hash_alg);
if (rv)
return rv;
rv = cbfs_extend_hash_master_header(&ctx, cbfs);
if (rv)
return rv;
prev = NULL;
fh = &f;
while (1) {
uint32_t ftype;
rv = cbfs_for_each_file(cbfs, prev, fh);
prev = fh;
if (rv < 0)
return VB2_ERROR_UNKNOWN;
/* End of CBFS. */
if (rv > 0)
break;
rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->metadata);
if (rv)
return rv;
/* Include data contents in hash if file is non-empty. */
if (cbfsf_file_type(fh, &ftype))
return VB2_ERROR_UNKNOWN;
if (ftype == CBFS_TYPE_DELETED || ftype == CBFS_TYPE_DELETED2)
continue;
rv = cbfs_extend_hash_with_offset(&ctx, cbfs, &fh->data);
if (rv)
return rv;
}
return vb2_digest_finalize(&ctx, digest, digest_sz);
}
|