summaryrefslogtreecommitdiff
path: root/board/aspeed/ibex_ast2700/fmc_hdr.c
blob: 2068a906f600006e57f5cdedfda99e7f456b6922 (plain)
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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) Aspeed Technology Inc.
 */

#include <asm/arch/fmc_hdr.h>
#include <asm/io.h>
#include <asm/sections.h>
#include <errno.h>
#include <spl.h>
#include <string.h>

int fmc_hdr_get_prebuilt(uint32_t type, uint32_t *ofst, uint32_t *size)
{
	struct fmc_hdr_preamble *preamble;
	struct fmc_hdr_body *body;
	struct fmc_hdr *hdr;
	uint32_t t, s, o;
	int i;

	if (type >= PBT_NUM)
		return -EINVAL;

	if (!ofst || !size)
		return -EINVAL;

	hdr = (struct fmc_hdr *)(_start - sizeof(*hdr));
	preamble = &hdr->preamble;
	body = &hdr->body;

	if (preamble->magic != HDR_MAGIC)
		return -EIO;

	for (i = 0, o = sizeof(*hdr) + body->fmc_size; i < HDR_PB_MAX; ++i) {
		t = body->pbs[i].type;
		s = body->pbs[i].size;

		/* skip if unrecognized, yet */
		if (t >= PBT_NUM) {
			o += s;
			continue;
		}

		/* prebuilt end mark */
		if (t == 0 && s == 0)
			break;

		/* return the prebuilt info if found */
		if (t == type) {
			*ofst = o;
			*size = s;

			goto found;
		}

		/* update offset for next prebuilt */
		o += s;
	}

	return -ENODATA;

found:
	return 0;
}