1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for ChromiumOS
5 * Copyright 2023 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
9 #define LOG_CATEGORY UCLASS_BOOTSTD
21 #include <asm/zimage.h>
23 #include <linux/sizes.h>
26 /* Offsets in the kernel-partition header */
30 SETUP_OFFSET = 0x1000, /* bytes before base */
31 CMDLINE_OFFSET = 0x2000, /* bytes before base */
32 OFFSET_BASE = 0x100000, /* assumed kernel load-address */
35 static int cros_check(struct udevice *dev, struct bootflow_iter *iter)
37 /* This only works on block and network devices */
38 if (bootflow_iter_check_blk(iter))
39 return log_msg_ret("blk", -ENOTSUPP);
44 static int copy_cmdline(const char *from, const char *uuid, char **bufp)
46 const int maxlen = 2048;
51 /* Allow space for cmdline + UUID */
52 len = strnlen(from, sizeof(buf));
56 log_debug("uuid %d %s\n", uuid ? (int)strlen(uuid) : 0, uuid);
57 for (to = buf, end = buf + maxlen - UUID_STR_LEN - 1; *from; from++) {
60 if (from[0] == '%' && from[1] == 'U' && uuid &&
61 strlen(uuid) == UUID_STR_LEN) {
80 static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
82 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
83 ulong base, start, size, setup, cmdline, num_blks, kern_base;
84 struct disk_partition info;
85 const char *uuid = NULL;
89 log_debug("starting, part=%d\n", bflow->part);
91 /* We consider the whole disk, not any one partition */
93 return log_msg_ret("max", -ENOENT);
95 /* Check partition 2 */
96 ret = part_get_info(desc, 2, &info);
98 return log_msg_ret("part", ret);
100 /* Make a buffer for the header information */
101 num_blks = SZ_4K >> desc->log2blksz;
102 log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n",
103 bflow->blk->name, (ulong)info.start, num_blks);
104 hdr = memalign(SZ_1K, SZ_4K);
106 return log_msg_ret("hdr", -ENOMEM);
107 ret = blk_read(bflow->blk, info.start, num_blks, hdr);
109 return log_msg_ret("inf", ret);
111 if (memcmp("CHROMEOS", hdr, 8))
114 log_info("Header at %lx\n", (ulong)map_to_sysmem(hdr));
115 start = *(u32 *)(hdr + KERN_START);
116 size = ALIGN(*(u32 *)(hdr + KERN_SIZE), desc->blksz);
117 log_debug("Reading start %lx size %lx\n", start, size);
120 buf = memalign(SZ_1K, size);
122 return log_msg_ret("buf", -ENOMEM);
123 num_blks = size >> desc->log2blksz;
124 log_debug("Reading data, blk=%s, start=%lx, blocks=%lx\n",
125 bflow->blk->name, (ulong)info.start, num_blks);
126 ret = blk_read(bflow->blk, (ulong)info.start + 0x80, num_blks, buf);
128 return log_msg_ret("inf", ret);
129 base = map_to_sysmem(buf);
131 setup = base + start - OFFSET_BASE - SETUP_OFFSET;
132 cmdline = base + start - OFFSET_BASE - CMDLINE_OFFSET;
133 kern_base = base + start - OFFSET_BASE + SZ_16K;
134 log_debug("base %lx setup %lx, cmdline %lx, kern_base %lx\n", base,
135 setup, cmdline, kern_base);
140 version = zimage_get_kernel_version(map_sysmem(setup, 0),
141 map_sysmem(kern_base, 0));
142 log_debug("version %s\n", version);
144 bflow->name = strdup(version);
147 bflow->name = strdup("ChromeOS");
149 return log_msg_ret("nam", -ENOMEM);
150 bflow->os_name = strdup("ChromeOS");
152 return log_msg_ret("os", -ENOMEM);
154 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
157 ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline);
159 return log_msg_ret("cmd", ret);
161 bflow->state = BOOTFLOWST_READY;
163 bflow->x86_setup = map_sysmem(setup, 0);
168 static int cros_read_file(struct udevice *dev, struct bootflow *bflow,
169 const char *file_path, ulong addr, ulong *sizep)
174 static int cros_boot(struct udevice *dev, struct bootflow *bflow)
177 zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0,
178 map_to_sysmem(bflow->x86_setup),
182 return log_msg_ret("go", -EFAULT);
185 static int cros_bootmeth_bind(struct udevice *dev)
187 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
189 plat->desc = "ChromiumOS boot";
194 static struct bootmeth_ops cros_bootmeth_ops = {
196 .read_bootflow = cros_read_bootflow,
197 .read_file = cros_read_file,
201 static const struct udevice_id cros_bootmeth_ids[] = {
202 { .compatible = "u-boot,cros" },
206 U_BOOT_DRIVER(bootmeth_cros) = {
207 .name = "bootmeth_cros",
208 .id = UCLASS_BOOTMETH,
209 .of_match = cros_bootmeth_ids,
210 .ops = &cros_bootmeth_ops,
211 .bind = cros_bootmeth_bind,