1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for ChromiumOS
5 * Copyright 2023 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
17 #include <display_options.h>
22 #include <linux/sizes.h>
23 #include "bootmeth_cros.h"
26 * Layout of the ChromeOS kernel
28 * Partitions 2 and 4 contain kernels
33 * 0 struct vb2_keyblock
34 * m struct vb2_kernel_preamble
37 * m is keyblock->keyblock_size
38 * n is preamble->preamble_size
40 * The kernel buffer itself consists of various parts:
43 * m + n kernel image (Flat vmlinux binary or FIT)
44 * b - 8KB Command line text
45 * b - 4KB X86 setup block (struct boot_params, extends for about 16KB)
46 * b X86 bootloader (continuation of setup block)
47 * b + 16KB X86 setup block (copy, used for hold data pointed to)
49 * b is m + n + preamble->bootloader_address - preamble->body_load_address
51 * Useful metadata extends from b - 8KB through to b + 32 KB
55 PROBE_SIZE = SZ_4K, /* initial bytes read from partition */
57 X86_SETUP_OFFSET = -0x1000, /* setup offset relative to base */
58 CMDLINE_OFFSET = -0x2000, /* cmdline offset relative to base */
59 X86_KERNEL_OFFSET = 0x4000, /* kernel offset relative to base */
63 * struct cros_priv - Private data
65 * This is read from the disk and recorded for use when the full kernel must
66 * be loaded and booted
68 * @body_offset: Offset of kernel body from start of partition (in bytes)
69 * @body_size: Size of kernel body in bytes
70 * @part_start: Block offset of selected partition from the start of the disk
71 * @body_load_address: Nominal load address for kernel body
72 * @bootloader_address: Address of bootloader, after body is loaded at
74 * @bootloader_size: Size of bootloader in bytes
75 * @info_buf: Buffer containing ChromiumOS info
81 ulong body_load_address;
82 ulong bootloader_address;
83 ulong bootloader_size;
87 static int cros_check(struct udevice *dev, struct bootflow_iter *iter)
89 /* This only works on block and network devices */
90 if (bootflow_iter_check_blk(iter))
91 return log_msg_ret("blk", -ENOTSUPP);
96 static int copy_cmdline(const char *from, const char *uuid, char **bufp)
98 const int maxlen = 2048;
100 char *cmd, *to, *end;
103 /* Allow space for cmdline + UUID */
104 len = strnlen(from, sizeof(buf));
108 log_debug("uuid %d %s\n", uuid ? (int)strlen(uuid) : 0, uuid);
109 for (to = buf, end = buf + maxlen - UUID_STR_LEN - 1; *from; from++) {
112 if (from[0] == '%' && from[1] == 'U' && uuid &&
113 strlen(uuid) == UUID_STR_LEN) {
133 * scan_part() - Scan a kernel partition to see if has a ChromeOS header
135 * This reads the first PROBE_SIZE of a partition, loookng for
138 * @blk: Block device to scan
139 * @partnum: Partition number to scan
140 * @info: Please to put partition info
141 * @hdrp: Return allocated keyblock header on success
143 static int scan_part(struct udevice *blk, int partnum,
144 struct disk_partition *info, struct vb2_keyblock **hdrp)
146 struct blk_desc *desc = dev_get_uclass_plat(blk);
147 struct vb2_keyblock *hdr;
151 ret = part_get_info(desc, partnum, info);
153 return log_msg_ret("part", ret);
155 /* Make a buffer for the header information */
156 num_blks = PROBE_SIZE >> desc->log2blksz;
157 log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n",
158 blk->name, (ulong)info->start, num_blks);
159 hdr = memalign(SZ_1K, PROBE_SIZE);
161 return log_msg_ret("hdr", -ENOMEM);
162 ret = blk_read(blk, info->start, num_blks, hdr);
163 if (ret != num_blks) {
165 return log_msg_ret("inf", -EIO);
168 if (memcmp(VB2_KEYBLOCK_MAGIC, hdr->magic, VB2_KEYBLOCK_MAGIC_SIZE)) {
179 * cros_read_buf() - Read information into a buf and parse it
181 * @bflow: Bootflow to update
182 * @buf: Buffer to use
183 * @size: Size of buffer and number of bytes to read thereinto
184 * @start: Start offset to read from on disk
185 * @before_base: Number of bytes to read before the bootloader base
186 * @uuid: UUID string if supported, else NULL
187 * Return: 0 if OK, -ENOMEM if out of memory, -EIO on read failure
189 static int cros_read_buf(struct bootflow *bflow, void *buf, ulong size,
190 loff_t start, ulong before_base, const char *uuid)
192 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
193 ulong base, setup, cmdline, kern_base;
197 num_blks = size >> desc->log2blksz;
198 log_debug("Reading info to %lx, blk=%s, size=%lx, blocks=%lx\n",
199 (ulong)map_to_sysmem(buf), bflow->blk->name, size, num_blks);
200 ret = blk_read(bflow->blk, start, num_blks, buf);
202 return log_msg_ret("inf", -EIO);
203 base = map_to_sysmem(buf) + before_base;
205 setup = base + X86_SETUP_OFFSET;
206 cmdline = base + CMDLINE_OFFSET;
207 kern_base = base + X86_KERNEL_OFFSET;
208 log_debug("base %lx setup %lx cmdline %lx kern_base %lx\n", base,
209 setup, cmdline, kern_base);
214 version = zimage_get_kernel_version(map_sysmem(setup, 0),
215 map_sysmem(kern_base, 0));
216 log_debug("version %s\n", version);
218 bflow->name = strdup(version);
221 bflow->name = strdup("ChromeOS");
223 return log_msg_ret("nam", -ENOMEM);
224 bflow->os_name = strdup("ChromeOS");
226 return log_msg_ret("os", -ENOMEM);
228 ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline);
230 return log_msg_ret("cmd", ret);
231 bflow->x86_setup = map_sysmem(setup, 0);
237 * cros_read_info() - Read information and fill out the bootflow
239 * @bflow: Bootflow to update
240 * @uuid: UUID string if supported, else NULL
241 * @preamble: Kernel preamble information
242 * Return: 0 if OK, -ENOMEM if out of memory, -EIO on read failure
244 static int cros_read_info(struct bootflow *bflow, const char *uuid,
245 const struct vb2_kernel_preamble *preamble)
247 struct cros_priv *priv = bflow->bootmeth_priv;
248 struct udevice *blk = bflow->blk;
249 struct blk_desc *desc = dev_get_uclass_plat(blk);
250 ulong offset, size, before_base;
254 log_debug("Kernel preamble at %lx, version major %x, minor %x\n",
255 (ulong)map_to_sysmem(preamble),
256 preamble->header_version_major,
257 preamble->header_version_minor);
259 log_debug(" - load_address %lx, bl_addr %lx, bl_size %lx\n",
260 (ulong)preamble->body_load_address,
261 (ulong)preamble->bootloader_address,
262 (ulong)preamble->bootloader_size);
264 priv->body_size = preamble->body_signature.data_size;
265 priv->body_load_address = preamble->body_load_address;
266 priv->bootloader_address = preamble->bootloader_address;
267 priv->bootloader_size = preamble->bootloader_size;
268 log_debug("Kernel body at %lx size %lx\n", priv->body_offset,
271 /* Work out how many bytes to read before the bootloader base */
272 before_base = -CMDLINE_OFFSET;
274 /* Read the cmdline through to the end of the bootloader */
275 size = priv->bootloader_size + before_base;
276 offset = priv->body_offset +
277 (priv->bootloader_address - priv->body_load_address) +
281 return log_msg_ret("buf", -ENOMEM);
283 ret = cros_read_buf(bflow, buf, size,
284 priv->part_start + (offset >> desc->log2blksz),
287 /* Clear this since the buffer is invalid */
288 bflow->x86_setup = NULL;
290 return log_msg_ret("pro", ret);
292 priv->info_buf = buf;
297 static int cros_read_kernel(struct bootflow *bflow)
299 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
300 struct cros_priv *priv = bflow->bootmeth_priv;
306 bflow->size = priv->body_size;
308 buf = memalign(SZ_1K, priv->body_size);
310 return log_msg_ret("buf", -ENOMEM);
312 /* Check that the header is not smaller than permitted */
313 if (priv->body_offset < PROBE_SIZE)
314 return log_msg_ret("san", EFAULT);
316 /* Read kernel body */
317 num_blks = priv->body_size >> desc->log2blksz;
318 log_debug("Reading body to %lx, blk=%s, size=%lx, blocks=%lx\n",
319 (ulong)map_to_sysmem(buf), bflow->blk->name, priv->body_size,
321 ret = blk_read(bflow->blk,
322 priv->part_start + (priv->body_offset >> desc->log2blksz),
325 return log_msg_ret("inf", -EIO);
326 base = map_to_sysmem(buf) + priv->bootloader_address -
327 priv->body_load_address;
328 setup = base + X86_SETUP_OFFSET;
331 bflow->x86_setup = map_sysmem(setup, 0);
336 static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
338 const struct vb2_kernel_preamble *preamble;
339 struct disk_partition info;
340 struct vb2_keyblock *hdr;
341 const char *uuid = NULL;
342 struct cros_priv *priv;
345 log_debug("starting, part=%d\n", bflow->part);
347 /* We consider the whole disk, not any one partition */
349 return log_msg_ret("max", -ENOENT);
351 /* Check partition 2 then 4 */
353 ret = scan_part(bflow->blk, part, &info, &hdr);
356 ret = scan_part(bflow->blk, part, &info, &hdr);
358 return log_msg_ret("scan", ret);
362 priv = malloc(sizeof(struct cros_priv));
365 return log_msg_ret("buf", -ENOMEM);
367 bflow->bootmeth_priv = priv;
369 log_info("Selected partition %d, header at %lx\n", bflow->part,
370 (ulong)map_to_sysmem(hdr));
372 /* Grab a few things from the preamble */
373 preamble = (void *)hdr + hdr->keyblock_size;
374 priv->body_offset = hdr->keyblock_size + preamble->preamble_size;
375 priv->part_start = info.start;
377 /* Now read everything we can learn about kernel */
378 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
381 ret = cros_read_info(bflow, uuid, preamble);
385 return log_msg_ret("inf", ret);
386 bflow->size = priv->body_size;
387 bflow->state = BOOTFLOWST_READY;
392 static int cros_read_file(struct udevice *dev, struct bootflow *bflow,
393 const char *file_path, ulong addr, ulong *sizep)
398 #if CONFIG_IS_ENABLED(BOOSTD_FULL)
399 static int cros_read_all(struct udevice *dev, struct bootflow *bflow)
404 return log_msg_ret("ld", -EALREADY);
405 ret = cros_read_kernel(bflow);
407 return log_msg_ret("rd", ret);
411 #endif /* BOOSTD_FULL */
413 static int cros_boot(struct udevice *dev, struct bootflow *bflow)
418 ret = cros_read_kernel(bflow);
420 return log_msg_ret("rd", ret);
423 if (IS_ENABLED(CONFIG_X86)) {
424 ret = zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0,
425 map_to_sysmem(bflow->x86_setup),
428 ret = bootm_boot_start(map_to_sysmem(bflow->buf),
432 return log_msg_ret("go", ret);
435 static int cros_bootmeth_bind(struct udevice *dev)
437 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
439 plat->desc = "ChromiumOS boot";
444 static struct bootmeth_ops cros_bootmeth_ops = {
446 .read_bootflow = cros_read_bootflow,
447 .read_file = cros_read_file,
449 #if CONFIG_IS_ENABLED(BOOSTD_FULL)
450 .read_all = cros_read_all,
451 #endif /* BOOSTD_FULL */
454 static const struct udevice_id cros_bootmeth_ids[] = {
455 { .compatible = "u-boot,cros" },
459 U_BOOT_DRIVER(bootmeth_cros) = {
460 .name = "bootmeth_cros",
461 .id = UCLASS_BOOTMETH,
462 .of_match = cros_bootmeth_ids,
463 .ops = &cros_bootmeth_ops,
464 .bind = cros_bootmeth_bind,