blob: da9701f9eb93a00d1679f601069d0ce4c286cfca [file] [log] [blame]
Simon Glassd78aa752022-10-20 18:23:10 -06001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Verified Boot for Embedded (VBE) loading firmware phases
4 *
5 * Copyright 2022 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY LOGC_BOOT
10
Simon Glass9d0042e2022-10-20 18:23:18 -060011#include <bloblist.h>
Simon Glassd78aa752022-10-20 18:23:10 -060012#include <bootdev.h>
13#include <bootflow.h>
14#include <bootmeth.h>
15#include <bootstage.h>
16#include <dm.h>
17#include <image.h>
18#include <log.h>
19#include <mapmem.h>
20#include <memalign.h>
21#include <mmc.h>
22#include <spl.h>
23#include <vbe.h>
24#include <dm/device-internal.h>
25#include "vbe_simple.h"
26
27/**
28 * vbe_simple_read_bootflow_fw() - Create a bootflow for firmware
29 *
30 * Locates and loads the firmware image (FIT) needed for the next phase. The FIT
31 * should ideally use external data, to reduce the amount of it that needs to be
32 * read.
33 *
34 * @bdev: bootdev device containing the firmwre
35 * @meth: VBE simple bootmeth
36 * @blow: Place to put the created bootflow, on success
37 * @return 0 if OK, -ve on error
38 */
39int vbe_simple_read_bootflow_fw(struct udevice *dev, struct bootflow *bflow)
40{
41 ALLOC_CACHE_ALIGN_BUFFER(u8, sbuf, MMC_MAX_BLOCK_LEN);
42 struct udevice *media = dev_get_parent(bflow->dev);
43 struct udevice *meth = bflow->method;
44 struct simple_priv *priv = dev_get_priv(meth);
45 const char *fit_uname, *fit_uname_config;
46 struct bootm_headers images = {};
47 ulong offset, size, blknum, addr, len, load_addr, num_blks;
48 enum image_phase_t phase;
49 struct blk_desc *desc;
50 struct udevice *blk;
51 int node, ret;
52 void *buf;
53
54 log_debug("media=%s\n", media->name);
55 ret = blk_get_from_parent(media, &blk);
56 if (ret)
57 return log_msg_ret("med", ret);
58 log_debug("blk=%s\n", blk->name);
59 desc = dev_get_uclass_plat(blk);
60
61 offset = priv->area_start + priv->skip_offset;
62
63 /* read in one block to find the FIT size */
64 blknum = offset / desc->blksz;
65 log_debug("read at %lx, blknum %lx\n", offset, blknum);
66 ret = blk_read(blk, blknum, 1, sbuf);
67 if (ret < 0)
68 return log_msg_ret("rd", ret);
69
70 ret = fdt_check_header(sbuf);
71 if (ret < 0)
72 return log_msg_ret("fdt", -EINVAL);
73 size = fdt_totalsize(sbuf);
74 if (size > priv->area_size)
75 return log_msg_ret("fdt", -E2BIG);
76 log_debug("FIT size %lx\n", size);
77
78 /*
79 * Load the FIT into the SPL memory. This is typically a FIT with
80 * external data, so this is quite small, perhaps a few KB.
81 */
82 addr = CONFIG_VAL(TEXT_BASE);
83 buf = map_sysmem(addr, size);
84 num_blks = DIV_ROUND_UP(size, desc->blksz);
85 log_debug("read %lx, %lx blocks to %lx / %p\n", size, num_blks, addr,
86 buf);
87 ret = blk_read(blk, blknum, num_blks, buf);
88 if (ret < 0)
89 return log_msg_ret("rd", ret);
90
91 /* figure out the phase to load */
92 phase = IS_ENABLED(CONFIG_VPL_BUILD) ? IH_PHASE_SPL : IH_PHASE_U_BOOT;
93
94 /*
95 * Load the image from the FIT. We ignore any load-address information
96 * so in practice this simply locates the image in the external-data
97 * region and returns its address and size. Since we only loaded the FIT
98 * itself, only a part of the image will be present, at best.
99 */
100 fit_uname = NULL;
101 fit_uname_config = NULL;
102 log_debug("loading FIT\n");
103 ret = fit_image_load(&images, addr, &fit_uname, &fit_uname_config,
104 IH_ARCH_SANDBOX, image_ph(phase, IH_TYPE_FIRMWARE),
105 BOOTSTAGE_ID_FIT_SPL_START, FIT_LOAD_IGNORED,
106 &load_addr, &len);
107 if (ret < 0)
108 return log_msg_ret("ld", ret);
109 node = ret;
110 log_debug("loaded to %lx\n", load_addr);
111
112 /* For FIT external data, read in the external data */
113 if (load_addr + len > addr + size) {
114 ulong base, full_size;
115 void *base_buf;
116
117 /* Find the start address to load from */
118 base = ALIGN_DOWN(load_addr, desc->blksz);
119
120 /*
121 * Get the total number of bytes to load, taking care of
122 * block alignment
123 */
124 full_size = load_addr + len - base;
125
126 /*
127 * Get the start block number, number of blocks and the address
128 * to load to, then load the blocks
129 */
130 blknum = (offset + base - addr) / desc->blksz;
131 num_blks = DIV_ROUND_UP(full_size, desc->blksz);
132 base_buf = map_sysmem(base, full_size);
133 ret = blk_read(blk, blknum, num_blks, base_buf);
134 log_debug("read %lx %lx, %lx blocks to %lx / %p: ret=%d\n",
135 blknum, full_size, num_blks, base, base_buf, ret);
136 if (ret < 0)
137 return log_msg_ret("rd", ret);
138 }
139
140 /* set up the bootflow with the info we obtained */
141 bflow->name = strdup(fdt_get_name(buf, node, NULL));
142 if (!bflow->name)
143 return log_msg_ret("name", -ENOMEM);
144 bflow->blk = blk;
145 bflow->buf = map_sysmem(load_addr, len);
146 bflow->size = len;
147
148 return 0;
149}
150
151static int simple_load_from_image(struct spl_image_info *spl_image,
152 struct spl_boot_device *bootdev)
153{
154 struct udevice *meth, *bdev;
155 struct simple_priv *priv;
156 struct bootflow bflow;
Simon Glass9d0042e2022-10-20 18:23:18 -0600157 struct vbe_handoff *handoff;
Simon Glassd78aa752022-10-20 18:23:10 -0600158 int ret;
159
Simon Glassd4dce4a2024-09-29 19:49:36 -0600160 if (xpl_phase() != PHASE_VPL && xpl_phase() != PHASE_SPL)
Simon Glassd78aa752022-10-20 18:23:10 -0600161 return -ENOENT;
162
Simon Glass9d0042e2022-10-20 18:23:18 -0600163 ret = bloblist_ensure_size(BLOBLISTT_VBE, sizeof(struct vbe_handoff),
164 0, (void **)&handoff);
165 if (ret)
166 return log_msg_ret("ro", ret);
167
Simon Glassd78aa752022-10-20 18:23:10 -0600168 vbe_find_first_device(&meth);
169 if (!meth)
170 return log_msg_ret("vd", -ENODEV);
171 log_debug("vbe dev %s\n", meth->name);
172 ret = device_probe(meth);
173 if (ret)
174 return log_msg_ret("probe", ret);
175
176 priv = dev_get_priv(meth);
177 log_debug("simple %s\n", priv->storage);
Simon Glasse22fe922023-01-17 10:48:05 -0700178 ret = bootdev_find_by_label(priv->storage, &bdev, NULL);
Simon Glassd78aa752022-10-20 18:23:10 -0600179 if (ret)
180 return log_msg_ret("bd", ret);
181 log_debug("bootdev %s\n", bdev->name);
182
183 bootflow_init(&bflow, bdev, meth);
184 ret = bootmeth_read_bootflow(meth, &bflow);
185 log_debug("\nfw ret=%d\n", ret);
186 if (ret)
187 return log_msg_ret("rd", ret);
188
189 /* jump to the image */
190 spl_image->flags = SPL_SANDBOXF_ARG_IS_BUF;
191 spl_image->arg = bflow.buf;
192 spl_image->size = bflow.size;
193 log_debug("Image: %s at %p size %x\n", bflow.name, bflow.buf,
194 bflow.size);
195
196 /* this is not used from now on, so free it */
197 bootflow_free(&bflow);
198
Simon Glass9d0042e2022-10-20 18:23:18 -0600199 /* Record that VBE was used in this phase */
Simon Glassd4dce4a2024-09-29 19:49:36 -0600200 handoff->phases |= 1 << xpl_phase();
Simon Glass9d0042e2022-10-20 18:23:18 -0600201
Simon Glassd78aa752022-10-20 18:23:10 -0600202 return 0;
203}
204SPL_LOAD_IMAGE_METHOD("vbe_simple", 5, BOOT_DEVICE_VBE,
205 simple_load_from_image);