1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for ChromiumOS
5 * Copyright 2023 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
21 #include <asm/zimage.h>
23 #include <linux/sizes.h>
24 #include "bootmeth_cros.h"
27 PROBE_SIZE = SZ_4K, /* initial bytes read from partition */
29 /* Offsets in the kernel-partition header */
33 SETUP_OFFSET = 0x1000, /* bytes before base */
34 CMDLINE_OFFSET = 0x2000, /* bytes before base */
35 OFFSET_BASE = 0x100000, /* assumed kernel load-address */
38 static int cros_check(struct udevice *dev, struct bootflow_iter *iter)
40 /* This only works on block and network devices */
41 if (bootflow_iter_check_blk(iter))
42 return log_msg_ret("blk", -ENOTSUPP);
47 static int copy_cmdline(const char *from, const char *uuid, char **bufp)
49 const int maxlen = 2048;
54 /* Allow space for cmdline + UUID */
55 len = strnlen(from, sizeof(buf));
59 log_debug("uuid %d %s\n", uuid ? (int)strlen(uuid) : 0, uuid);
60 for (to = buf, end = buf + maxlen - UUID_STR_LEN - 1; *from; from++) {
63 if (from[0] == '%' && from[1] == 'U' && uuid &&
64 strlen(uuid) == UUID_STR_LEN) {
84 * scan_part() - Scan a kernel partition to see if has a ChromeOS header
86 * This reads the first PROBE_SIZE of a partition, loookng for
89 * @blk: Block device to scan
90 * @partnum: Partition number to scan
91 * @info: Please to put partition info
92 * @hdrp: Return allocated keyblock header on success
94 static int scan_part(struct udevice *blk, int partnum,
95 struct disk_partition *info, void **hdrp)
97 struct blk_desc *desc = dev_get_uclass_plat(blk);
98 struct vb2_keyblock *hdr;
102 ret = part_get_info(desc, partnum, info);
104 return log_msg_ret("part", ret);
106 /* Make a buffer for the header information */
107 num_blks = PROBE_SIZE >> desc->log2blksz;
108 log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n",
109 blk->name, (ulong)info->start, num_blks);
110 hdr = memalign(SZ_1K, PROBE_SIZE);
112 return log_msg_ret("hdr", -ENOMEM);
113 ret = blk_read(blk, info->start, num_blks, hdr);
114 if (ret != num_blks) {
116 return log_msg_ret("inf", -EIO);
119 if (memcmp(VB2_KEYBLOCK_MAGIC, hdr->magic, VB2_KEYBLOCK_MAGIC_SIZE)) {
129 static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
131 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
132 ulong base, start, size, setup, cmdline, num_blks, kern_base;
133 struct disk_partition info;
134 const char *uuid = NULL;
138 log_debug("starting, part=%d\n", bflow->part);
140 /* We consider the whole disk, not any one partition */
142 return log_msg_ret("max", -ENOENT);
144 /* Check partition 2 */
145 ret = scan_part(bflow->blk, 2, &info, &hdr);
147 return log_msg_ret("scan", ret);
150 log_info("Header at %lx\n", (ulong)map_to_sysmem(hdr));
151 start = *(u32 *)(hdr + KERN_START);
152 size = ALIGN(*(u32 *)(hdr + KERN_SIZE), desc->blksz);
153 log_debug("Reading start %lx size %lx\n", start, size);
156 buf = memalign(SZ_1K, size);
158 return log_msg_ret("buf", -ENOMEM);
159 num_blks = size >> desc->log2blksz;
160 log_debug("Reading data, blk=%s, start=%lx, blocks=%lx\n",
161 bflow->blk->name, (ulong)info.start, num_blks);
162 ret = blk_read(bflow->blk, (ulong)info.start + 0x80, num_blks, buf);
164 return log_msg_ret("inf", -EIO);
165 base = map_to_sysmem(buf);
167 setup = base + start - OFFSET_BASE - SETUP_OFFSET;
168 cmdline = base + start - OFFSET_BASE - CMDLINE_OFFSET;
169 kern_base = base + start - OFFSET_BASE + SZ_16K;
170 log_debug("base %lx setup %lx, cmdline %lx, kern_base %lx\n", base,
171 setup, cmdline, kern_base);
176 version = zimage_get_kernel_version(map_sysmem(setup, 0),
177 map_sysmem(kern_base, 0));
178 log_debug("version %s\n", version);
180 bflow->name = strdup(version);
183 bflow->name = strdup("ChromeOS");
185 return log_msg_ret("nam", -ENOMEM);
186 bflow->os_name = strdup("ChromeOS");
188 return log_msg_ret("os", -ENOMEM);
190 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
193 ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline);
195 return log_msg_ret("cmd", ret);
197 bflow->state = BOOTFLOWST_READY;
199 bflow->x86_setup = map_sysmem(setup, 0);
204 static int cros_read_file(struct udevice *dev, struct bootflow *bflow,
205 const char *file_path, ulong addr, ulong *sizep)
210 static int cros_boot(struct udevice *dev, struct bootflow *bflow)
213 zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0,
214 map_to_sysmem(bflow->x86_setup),
218 return log_msg_ret("go", -EFAULT);
221 static int cros_bootmeth_bind(struct udevice *dev)
223 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
225 plat->desc = "ChromiumOS boot";
230 static struct bootmeth_ops cros_bootmeth_ops = {
232 .read_bootflow = cros_read_bootflow,
233 .read_file = cros_read_file,
237 static const struct udevice_id cros_bootmeth_ids[] = {
238 { .compatible = "u-boot,cros" },
242 U_BOOT_DRIVER(bootmeth_cros) = {
243 .name = "bootmeth_cros",
244 .id = UCLASS_BOOTMETH,
245 .of_match = cros_bootmeth_ids,
246 .ops = &cros_bootmeth_ops,
247 .bind = cros_bootmeth_bind,