1 // SPDX-License-Identifier: GPL-2.0
3 * Verified Boot for Embedded (VBE) loading firmware phases
5 * Copyright 2022 Google LLC
9 #define LOG_CATEGORY LOGC_BOOT
15 #include <bootstage.h>
24 #include <dm/device-internal.h>
25 #include "vbe_simple.h"
28 * vbe_simple_read_bootflow_fw() - Create a bootflow for firmware
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
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
39 int vbe_simple_read_bootflow_fw(struct udevice *dev, struct bootflow *bflow)
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;
54 log_debug("media=%s\n", media->name);
55 ret = blk_get_from_parent(media, &blk);
57 return log_msg_ret("med", ret);
58 log_debug("blk=%s\n", blk->name);
59 desc = dev_get_uclass_plat(blk);
61 offset = priv->area_start + priv->skip_offset;
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);
68 return log_msg_ret("rd", ret);
70 ret = fdt_check_header(sbuf);
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);
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.
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,
87 ret = blk_read(blk, blknum, num_blks, buf);
89 return log_msg_ret("rd", ret);
91 /* figure out the phase to load */
92 phase = IS_ENABLED(CONFIG_VPL_BUILD) ? IH_PHASE_SPL : IH_PHASE_U_BOOT;
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.
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,
108 return log_msg_ret("ld", ret);
110 log_debug("loaded to %lx\n", load_addr);
112 /* For FIT external data, read in the external data */
113 if (load_addr + len > addr + size) {
114 ulong base, full_size;
117 /* Find the start address to load from */
118 base = ALIGN_DOWN(load_addr, desc->blksz);
121 * Get the total number of bytes to load, taking care of
124 full_size = load_addr + len - base;
127 * Get the start block number, number of blocks and the address
128 * to load to, then load the blocks
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);
137 return log_msg_ret("rd", ret);
140 /* set up the bootflow with the info we obtained */
141 bflow->name = strdup(fdt_get_name(buf, node, NULL));
143 return log_msg_ret("name", -ENOMEM);
145 bflow->buf = map_sysmem(load_addr, len);
151 static int simple_load_from_image(struct spl_image_info *spl_image,
152 struct spl_boot_device *bootdev)
154 struct udevice *meth, *bdev;
155 struct simple_priv *priv;
156 struct bootflow bflow;
157 struct vbe_handoff *handoff;
160 if (spl_phase() != PHASE_VPL && spl_phase() != PHASE_SPL)
163 ret = bloblist_ensure_size(BLOBLISTT_VBE, sizeof(struct vbe_handoff),
164 0, (void **)&handoff);
166 return log_msg_ret("ro", ret);
168 vbe_find_first_device(&meth);
170 return log_msg_ret("vd", -ENODEV);
171 log_debug("vbe dev %s\n", meth->name);
172 ret = device_probe(meth);
174 return log_msg_ret("probe", ret);
176 priv = dev_get_priv(meth);
177 log_debug("simple %s\n", priv->storage);
178 ret = bootdev_find_by_label(priv->storage, &bdev, NULL);
180 return log_msg_ret("bd", ret);
181 log_debug("bootdev %s\n", bdev->name);
183 bootflow_init(&bflow, bdev, meth);
184 ret = bootmeth_read_bootflow(meth, &bflow);
185 log_debug("\nfw ret=%d\n", ret);
187 return log_msg_ret("rd", ret);
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,
196 /* this is not used from now on, so free it */
197 bootflow_free(&bflow);
199 /* Record that VBE was used in this phase */
200 handoff->phases |= 1 << spl_phase();
204 SPL_LOAD_IMAGE_METHOD("vbe_simple", 5, BOOT_DEVICE_VBE,
205 simple_load_from_image);