1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 */
5
6#include <common.h>
7#include <cbfs.h>
8#include <log.h>
9#include <malloc.h>
10#include <asm/byteorder.h>
11
12/* Offset of master header from the start of a coreboot ROM */
13#define MASTER_HDR_OFFSET	0x38
14
15static const u32 good_magic = 0x4f524243;
16static const u8 good_file_magic[] = "LARCHIVE";
17
18/**
19 * struct cbfs_priv - Private data for this driver
20 *
21 * @initialised: true if this CBFS has been inited
22 * @start: Start position of CBFS in memory, typically memory-mapped SPI flash
23 * @header: Header read from the CBFS, byte-swapped so U-Boot can access it
24 * @file_cache: List of file headers read from CBFS
25 * @result: Success/error result
26 */
27struct cbfs_priv {
28	bool initialized;
29	void *start;
30	struct cbfs_header header;
31	struct cbfs_cachenode *file_cache;
32	enum cbfs_result result;
33};
34
35static struct cbfs_priv cbfs_s;
36
37const char *file_cbfs_error(void)
38{
39	switch (cbfs_s.result) {
40	case CBFS_SUCCESS:
41		return "Success";
42	case CBFS_NOT_INITIALIZED:
43		return "CBFS not initialized";
44	case CBFS_BAD_HEADER:
45		return "Bad CBFS header";
46	case CBFS_BAD_FILE:
47		return "Bad CBFS file";
48	case CBFS_FILE_NOT_FOUND:
49		return "File not found";
50	default:
51		return "Unknown";
52	}
53}
54
55enum cbfs_result cbfs_get_result(void)
56{
57	return cbfs_s.result;
58}
59
60/* Do endian conversion on the CBFS header structure. */
61static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
62{
63	dest->magic = be32_to_cpu(src->magic);
64	dest->version = be32_to_cpu(src->version);
65	dest->rom_size = be32_to_cpu(src->rom_size);
66	dest->boot_block_size = be32_to_cpu(src->boot_block_size);
67	dest->align = be32_to_cpu(src->align);
68	dest->offset = be32_to_cpu(src->offset);
69}
70
71/* Do endian conversion on a CBFS file header. */
72static void swap_file_header(struct cbfs_fileheader *dest,
73			     const struct cbfs_fileheader *src)
74{
75	memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
76	dest->len = be32_to_cpu(src->len);
77	dest->type = be32_to_cpu(src->type);
78	dest->attributes_offset = be32_to_cpu(src->attributes_offset);
79	dest->offset = be32_to_cpu(src->offset);
80}
81
82/**
83 * fill_node() - Fill a node struct with information from the CBFS
84 *
85 * @node: Node to fill
86 * @start: Pointer to the start of the CBFS file in memory
87 * @header: Pointer to the header information (in our enddianess)
88 * Return: 0 if OK, -EBADF if the header is too small
89 */
90static int fill_node(struct cbfs_cachenode *node, void *start,
91		     struct cbfs_fileheader *header)
92{
93	uint name_len;
94	uint offset;
95
96	/* Check the header is large enough */
97	if (header->offset < sizeof(struct cbfs_fileheader))
98		return -EBADF;
99
100	node->next = NULL;
101	node->type = header->type;
102	node->data = start + header->offset;
103	node->data_length = header->len;
104	name_len = header->offset - sizeof(struct cbfs_fileheader);
105	node->name = start + sizeof(struct cbfs_fileheader);
106	node->name_length = name_len;
107	node->attr_offset = header->attributes_offset;
108	node->comp_algo = CBFS_COMPRESS_NONE;
109	node->decomp_size = 0;
110
111	for (offset = node->attr_offset; offset < header->offset;) {
112		const struct cbfs_file_attribute *attr;
113		uint tag, len;
114
115		attr = start + offset;
116		tag = be32_to_cpu(attr->tag);
117		len = be32_to_cpu(attr->len);
118		if (tag == CBFS_FILE_ATTR_TAG_COMPRESSION) {
119			struct cbfs_file_attr_compression *comp;
120
121			comp = start + offset;
122			node->comp_algo = be32_to_cpu(comp->compression);
123			node->decomp_size =
124				be32_to_cpu(comp->decompressed_size);
125		}
126
127		offset += len;
128	}
129
130	return 0;
131}
132
133/*
134 * Given a starting position in memory, scan forward, bounded by a size, and
135 * find the next valid CBFS file. No memory is allocated by this function. The
136 * caller is responsible for allocating space for the new file structure.
137 *
138 * @param start		The location in memory to start from.
139 * @param size		The size of the memory region to search.
140 * @param align		The alignment boundaries to check on.
141 * @param node	A pointer to the file structure to load.
142 * @param used		A pointer to the count of of bytes scanned through,
143 *			including the file if one is found.
144 *
145 * Return: 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
146 *	is found.
147 */
148static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
149			       int align, struct cbfs_cachenode *node,
150			       int *used)
151{
152	struct cbfs_fileheader header;
153
154	*used = 0;
155
156	while (size >= align) {
157		const struct cbfs_fileheader *file_header = start;
158		int ret;
159
160		/* Check if there's a file here. */
161		if (memcmp(good_file_magic, &file_header->magic,
162			   sizeof(file_header->magic))) {
163			*used += align;
164			size -= align;
165			start += align;
166			continue;
167		}
168
169		swap_file_header(&header, file_header);
170		if (header.offset >= size)
171			return log_msg_ret("range", -E2BIG);
172		ret = fill_node(node, start, &header);
173		if (ret) {
174			priv->result = CBFS_BAD_FILE;
175			return log_msg_ret("fill", ret);
176		}
177
178		*used += ALIGN(header.len, align);
179		return 0;
180	}
181
182	return -ENOENT;
183}
184
185/* Look through a CBFS instance and copy file metadata into regular memory. */
186static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
187{
188	struct cbfs_cachenode *cache_node;
189	struct cbfs_cachenode *node;
190	struct cbfs_cachenode **cache_tail = &priv->file_cache;
191	void *start;
192
193	/* Clear out old information. */
194	cache_node = priv->file_cache;
195	while (cache_node) {
196		struct cbfs_cachenode *old_node = cache_node;
197		cache_node = cache_node->next;
198		free(old_node);
199	}
200	priv->file_cache = NULL;
201
202	start = priv->start;
203	while (size >= align) {
204		int used;
205		int ret;
206
207		node = malloc(sizeof(struct cbfs_cachenode));
208		if (!node)
209			return -ENOMEM;
210		ret = file_cbfs_next_file(priv, start, size, align, node,
211					  &used);
212
213		if (ret < 0) {
214			free(node);
215			if (ret == -ENOENT)
216				break;
217			return ret;
218		}
219		*cache_tail = node;
220		cache_tail = &node->next;
221
222		size -= used;
223		start += used;
224	}
225	priv->result = CBFS_SUCCESS;
226
227	return 0;
228}
229
230/**
231 * load_header() - Load the CBFS header
232 *
233 * Get the CBFS header out of the ROM and do endian conversion.
234 *
235 * @priv: Private data, which is inited by this function
236 * @addr: Address of CBFS header in memory-mapped SPI flash
237 * Return: 0 if OK, -ENXIO if the header is bad
238 */
239static int load_header(struct cbfs_priv *priv, ulong addr)
240{
241	struct cbfs_header *header = &priv->header;
242	struct cbfs_header *header_in_rom;
243
244	memset(priv, '\0', sizeof(*priv));
245	header_in_rom = (struct cbfs_header *)addr;
246	swap_header(header, header_in_rom);
247
248	if (header->magic != good_magic || header->offset >
249			header->rom_size - header->boot_block_size) {
250		priv->result = CBFS_BAD_HEADER;
251		return -ENXIO;
252	}
253
254	return 0;
255}
256
257/**
258 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
259 *
260 * @priv: Private data, which is inited by this function
261 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
262 * Return: 0 if OK, -ENXIO if the header is bad
263 */
264static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
265{
266	int offset = *(u32 *)(end_of_rom - 3);
267	int ret;
268
269	ret = load_header(priv, end_of_rom + offset + 1);
270	if (ret)
271		return ret;
272	priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
273
274	return 0;
275}
276
277/**
278 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
279 *
280 * @priv: Private data, which is inited by this function
281 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
282 * Return: 0 if OK, -ENXIO if the header is bad
283 */
284static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
285{
286	int ret;
287
288	ret = load_header(priv, base + MASTER_HDR_OFFSET);
289	if (ret)
290		return ret;
291	priv->start = (void *)base;
292
293	return 0;
294}
295
296static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
297{
298	int ret;
299
300	ret = file_cbfs_load_header(priv, end_of_rom);
301	if (ret)
302		return ret;
303
304	ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
305				   priv->header.align);
306	if (ret)
307		return ret;
308	priv->initialized = true;
309
310	return 0;
311}
312
313int file_cbfs_init(ulong end_of_rom)
314{
315	return cbfs_init(&cbfs_s, end_of_rom);
316}
317
318int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
319		  struct cbfs_priv **privp)
320{
321	struct cbfs_priv priv_s, *priv = &priv_s;
322	int ret;
323
324	/*
325	 * Use a local variable to start with until we know that the * CBFS is
326	 * valid. Note that size is detected from the header, if present,
327	 * meaning the parameter is ignored.
328	 */
329	ret = cbfs_load_header_ptr(priv, base);
330	if (ret) {
331		if (require_hdr || size == CBFS_SIZE_UNKNOWN)
332			return ret;
333		memset(priv, '\0', sizeof(struct cbfs_priv));
334		priv->header.rom_size = size;
335		priv->header.align = CBFS_ALIGN_SIZE;
336		priv->start = (void *)base;
337	}
338
339	ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
340				   priv->header.align);
341	if (ret)
342		return log_msg_ret("fill", ret);
343
344	priv->initialized = true;
345	priv = malloc(sizeof(priv_s));
346	if (!priv)
347		return -ENOMEM;
348	memcpy(priv, &priv_s, sizeof(priv_s));
349	*privp = priv;
350
351	return 0;
352}
353
354const struct cbfs_header *file_cbfs_get_header(void)
355{
356	struct cbfs_priv *priv = &cbfs_s;
357
358	if (priv->initialized) {
359		priv->result = CBFS_SUCCESS;
360		return &priv->header;
361	} else {
362		priv->result = CBFS_NOT_INITIALIZED;
363		return NULL;
364	}
365}
366
367const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
368{
369	return priv->file_cache;
370}
371
372void cbfs_get_next(const struct cbfs_cachenode **filep)
373{
374	if (*filep)
375		*filep = (*filep)->next;
376}
377
378const struct cbfs_cachenode *file_cbfs_get_first(void)
379{
380	struct cbfs_priv *priv = &cbfs_s;
381
382	if (!priv->initialized) {
383		priv->result = CBFS_NOT_INITIALIZED;
384		return NULL;
385	} else {
386		priv->result = CBFS_SUCCESS;
387		return priv->file_cache;
388	}
389}
390
391void file_cbfs_get_next(const struct cbfs_cachenode **file)
392{
393	struct cbfs_priv *priv = &cbfs_s;
394
395	if (!priv->initialized) {
396		priv->result = CBFS_NOT_INITIALIZED;
397		*file = NULL;
398		return;
399	}
400
401	if (*file)
402		*file = (*file)->next;
403	priv->result = CBFS_SUCCESS;
404}
405
406const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
407					    const char *name)
408{
409	struct cbfs_cachenode *cache_node = priv->file_cache;
410
411	if (!priv->initialized) {
412		priv->result = CBFS_NOT_INITIALIZED;
413		return NULL;
414	}
415
416	while (cache_node) {
417		if (!strcmp(name, cache_node->name))
418			break;
419		cache_node = cache_node->next;
420	}
421	if (!cache_node)
422		priv->result = CBFS_FILE_NOT_FOUND;
423	else
424		priv->result = CBFS_SUCCESS;
425
426	return cache_node;
427}
428
429const struct cbfs_cachenode *file_cbfs_find(const char *name)
430{
431	return cbfs_find_file(&cbfs_s, name);
432}
433
434static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
435			 struct cbfs_cachenode *node)
436{
437	int size = priv->header.rom_size;
438	int align = priv->header.align;
439
440	while (size >= align) {
441		int used;
442		int ret;
443
444		ret = file_cbfs_next_file(priv, start, size, align, node,
445					  &used);
446		if (ret == -ENOENT)
447			break;
448		else if (ret)
449			return ret;
450		if (!strcmp(name, node->name))
451			return 0;
452
453		size -= used;
454		start += used;
455	}
456	priv->result = CBFS_FILE_NOT_FOUND;
457
458	return -ENOENT;
459}
460
461int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
462			    struct cbfs_cachenode *node)
463{
464	struct cbfs_priv priv;
465	void *start;
466	int ret;
467
468	ret = file_cbfs_load_header(&priv, end_of_rom);
469	if (ret)
470		return ret;
471	start = priv.start;
472
473	return find_uncached(&priv, name, start, node);
474}
475
476int file_cbfs_find_uncached_base(ulong base, const char *name,
477				 struct cbfs_cachenode *node)
478{
479	struct cbfs_priv priv;
480	int ret;
481
482	ret = cbfs_load_header_ptr(&priv, base);
483	if (ret)
484		return ret;
485
486	return find_uncached(&priv, name, (void *)base, node);
487}
488
489const char *file_cbfs_name(const struct cbfs_cachenode *file)
490{
491	cbfs_s.result = CBFS_SUCCESS;
492
493	return file->name;
494}
495
496u32 file_cbfs_size(const struct cbfs_cachenode *file)
497{
498	cbfs_s.result = CBFS_SUCCESS;
499
500	return file->data_length;
501}
502
503u32 file_cbfs_type(const struct cbfs_cachenode *file)
504{
505	cbfs_s.result = CBFS_SUCCESS;
506
507	return file->type;
508}
509
510long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
511		    unsigned long maxsize)
512{
513	u32 size;
514
515	size = file->data_length;
516	if (maxsize && size > maxsize)
517		size = maxsize;
518
519	memcpy(buffer, file->data, size);
520	cbfs_s.result = CBFS_SUCCESS;
521
522	return size;
523}
524