blob: 2068a906f600006e57f5cdedfda99e7f456b6922 [file] [log] [blame]
Chia-Wei Wang956b2de2024-09-10 17:39:18 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) Aspeed Technology Inc.
4 */
5
6#include <asm/arch/fmc_hdr.h>
7#include <asm/io.h>
8#include <asm/sections.h>
9#include <errno.h>
10#include <spl.h>
11#include <string.h>
12
13int fmc_hdr_get_prebuilt(uint32_t type, uint32_t *ofst, uint32_t *size)
14{
15 struct fmc_hdr_preamble *preamble;
16 struct fmc_hdr_body *body;
17 struct fmc_hdr *hdr;
18 uint32_t t, s, o;
19 int i;
20
21 if (type >= PBT_NUM)
22 return -EINVAL;
23
24 if (!ofst || !size)
25 return -EINVAL;
26
27 hdr = (struct fmc_hdr *)(_start - sizeof(*hdr));
28 preamble = &hdr->preamble;
29 body = &hdr->body;
30
31 if (preamble->magic != HDR_MAGIC)
32 return -EIO;
33
34 for (i = 0, o = sizeof(*hdr) + body->fmc_size; i < HDR_PB_MAX; ++i) {
35 t = body->pbs[i].type;
36 s = body->pbs[i].size;
37
38 /* skip if unrecognized, yet */
39 if (t >= PBT_NUM) {
40 o += s;
41 continue;
42 }
43
44 /* prebuilt end mark */
45 if (t == 0 && s == 0)
46 break;
47
48 /* return the prebuilt info if found */
49 if (t == type) {
50 *ofst = o;
51 *size = s;
52
53 goto found;
54 }
55
56 /* update offset for next prebuilt */
57 o += s;
58 }
59
60 return -ENODATA;
61
62found:
63 return 0;
64}