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>
23 #include <linux/sizes.h>
24 #include "bootmeth_cros.h"
26 static const efi_guid_t cros_kern_type = PARTITION_CROS_KERNEL;
29 * Layout of the ChromeOS kernel
31 * Partitions 2 and 4 contain kernels with type GUID_CROS_KERNEL
36 * 0 struct vb2_keyblock
37 * m struct vb2_kernel_preamble
40 * m is keyblock->keyblock_size
41 * n is preamble->preamble_size
43 * The kernel buffer itself consists of various parts:
46 * m + n kernel image (Flat vmlinux binary or FIT)
47 * b - 8KB Command line text
48 * b - 4KB X86 setup block (struct boot_params, extends for about 16KB)
49 * b X86 bootloader (continuation of setup block)
50 * b + 16KB X86 setup block (copy, used for hold data pointed to)
52 * b is m + n + preamble->bootloader_address - preamble->body_load_address
54 * Useful metadata extends from b - 8KB through to b + 32 KB
58 PROBE_SIZE = SZ_4K, /* initial bytes read from partition */
60 X86_SETUP_OFFSET = -0x1000, /* setup offset relative to base */
61 CMDLINE_OFFSET = -0x2000, /* cmdline offset relative to base */
62 X86_KERNEL_OFFSET = 0x4000, /* kernel offset relative to base */
66 * struct cros_priv - Private data
68 * This is read from the disk and recorded for use when the full kernel must
69 * be loaded and booted
71 * @body_offset: Offset of kernel body from start of partition (in bytes)
72 * @body_size: Size of kernel body in bytes
73 * @part_start: Block offset of selected partition from the start of the disk
74 * @body_load_address: Nominal load address for kernel body
75 * @bootloader_address: Address of bootloader, after body is loaded at
77 * @bootloader_size: Size of bootloader in bytes
78 * @info_buf: Buffer containing ChromiumOS info
84 ulong body_load_address;
85 ulong bootloader_address;
86 ulong bootloader_size;
90 static int cros_check(struct udevice *dev, struct bootflow_iter *iter)
92 /* This only works on block and network devices */
93 if (bootflow_iter_check_blk(iter))
94 return log_msg_ret("blk", -ENOTSUPP);
99 static int copy_cmdline(const char *from, const char *uuid, char **bufp)
101 const int maxlen = 2048;
103 char *cmd, *to, *end;
106 /* Allow space for cmdline + UUID */
107 len = strnlen(from, sizeof(buf));
111 log_debug("uuid %d %s\n", uuid ? (int)strlen(uuid) : 0, uuid);
112 for (to = buf, end = buf + maxlen - UUID_STR_LEN - 1; *from; from++) {
115 if (from[0] == '%' && from[1] == 'U' && uuid &&
116 strlen(uuid) == UUID_STR_LEN) {
136 * scan_part() - Scan a kernel partition to see if has a ChromeOS header
138 * This reads the first PROBE_SIZE of a partition, loookng for
141 * @blk: Block device to scan
142 * @partnum: Partition number to scan
143 * @info: Please to put partition info
144 * @hdrp: Return allocated keyblock header on success
146 static int scan_part(struct udevice *blk, int partnum,
147 struct disk_partition *info, struct vb2_keyblock **hdrp)
149 struct blk_desc *desc = dev_get_uclass_plat(blk);
150 struct vb2_keyblock *hdr;
156 return log_msg_ret("efi", -ENOENT);
158 ret = part_get_info(desc, partnum, info);
160 return log_msg_ret("part", ret);
162 /* Check for kernel partition type */
163 log_debug("part %x: type=%s\n", partnum, info->type_guid);
164 if (uuid_str_to_bin(info->type_guid, (u8 *)&type, UUID_STR_FORMAT_GUID))
165 return log_msg_ret("typ", -EINVAL);
167 if (memcmp(&cros_kern_type, &type, sizeof(type)))
168 return log_msg_ret("typ", -ENOEXEC);
170 /* Make a buffer for the header information */
171 num_blks = PROBE_SIZE >> desc->log2blksz;
172 log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n",
173 blk->name, (ulong)info->start, num_blks);
174 hdr = memalign(SZ_1K, PROBE_SIZE);
176 return log_msg_ret("hdr", -ENOMEM);
177 ret = blk_read(blk, info->start, num_blks, hdr);
178 if (ret != num_blks) {
180 return log_msg_ret("inf", -EIO);
183 if (memcmp(VB2_KEYBLOCK_MAGIC, hdr->magic, VB2_KEYBLOCK_MAGIC_SIZE)) {
185 log_debug("no magic\n");
195 * cros_read_buf() - Read information into a buf and parse it
197 * @bflow: Bootflow to update
198 * @buf: Buffer to use
199 * @size: Size of buffer and number of bytes to read thereinto
200 * @start: Start offset to read from on disk
201 * @before_base: Number of bytes to read before the bootloader base
202 * @uuid: UUID string if supported, else NULL
203 * Return: 0 if OK, -ENOMEM if out of memory, -EIO on read failure
205 static int cros_read_buf(struct bootflow *bflow, void *buf, ulong size,
206 loff_t start, ulong before_base, const char *uuid)
208 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
209 ulong base, setup, cmdline, kern_base;
213 num_blks = size >> desc->log2blksz;
214 log_debug("Reading info to %lx, blk=%s, size=%lx, blocks=%lx\n",
215 (ulong)map_to_sysmem(buf), bflow->blk->name, size, num_blks);
216 ret = blk_read(bflow->blk, start, num_blks, buf);
218 return log_msg_ret("inf", -EIO);
219 base = map_to_sysmem(buf) + before_base;
221 setup = base + X86_SETUP_OFFSET;
222 cmdline = base + CMDLINE_OFFSET;
223 kern_base = base + X86_KERNEL_OFFSET;
224 log_debug("base %lx setup %lx cmdline %lx kern_base %lx\n", base,
225 setup, cmdline, kern_base);
230 version = zimage_get_kernel_version(map_sysmem(setup, 0),
231 map_sysmem(kern_base, 0));
232 log_debug("version %s\n", version);
234 bflow->name = strdup(version);
237 bflow->name = strdup("ChromeOS");
239 return log_msg_ret("nam", -ENOMEM);
240 bflow->os_name = strdup("ChromeOS");
242 return log_msg_ret("os", -ENOMEM);
244 ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline);
246 return log_msg_ret("cmd", ret);
247 bflow->x86_setup = map_sysmem(setup, 0);
253 * cros_read_info() - Read information and fill out the bootflow
255 * @bflow: Bootflow to update
256 * @uuid: UUID string if supported, else NULL
257 * @preamble: Kernel preamble information
258 * Return: 0 if OK, -ENOMEM if out of memory, -EIO on read failure
260 static int cros_read_info(struct bootflow *bflow, const char *uuid,
261 const struct vb2_kernel_preamble *preamble)
263 struct cros_priv *priv = bflow->bootmeth_priv;
264 struct udevice *blk = bflow->blk;
265 struct blk_desc *desc = dev_get_uclass_plat(blk);
266 ulong offset, size, before_base;
270 log_debug("Kernel preamble at %lx, version major %x, minor %x\n",
271 (ulong)map_to_sysmem(preamble),
272 preamble->header_version_major,
273 preamble->header_version_minor);
275 log_debug(" - load_address %lx, bl_addr %lx, bl_size %lx\n",
276 (ulong)preamble->body_load_address,
277 (ulong)preamble->bootloader_address,
278 (ulong)preamble->bootloader_size);
280 priv->body_size = preamble->body_signature.data_size;
281 priv->body_load_address = preamble->body_load_address;
282 priv->bootloader_address = preamble->bootloader_address;
283 priv->bootloader_size = preamble->bootloader_size;
284 log_debug("Kernel body at %lx size %lx\n", priv->body_offset,
287 /* Work out how many bytes to read before the bootloader base */
288 before_base = -CMDLINE_OFFSET;
290 /* Read the cmdline through to the end of the bootloader */
291 size = priv->bootloader_size + before_base;
292 offset = priv->body_offset +
293 (priv->bootloader_address - priv->body_load_address) +
297 return log_msg_ret("buf", -ENOMEM);
299 ret = cros_read_buf(bflow, buf, size,
300 priv->part_start + (offset >> desc->log2blksz),
303 /* Clear this since the buffer is invalid */
304 bflow->x86_setup = NULL;
306 return log_msg_ret("pro", ret);
308 priv->info_buf = buf;
313 static int cros_read_kernel(struct bootflow *bflow)
315 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
316 struct cros_priv *priv = bflow->bootmeth_priv;
322 bflow->size = priv->body_size;
324 buf = memalign(SZ_1K, priv->body_size);
326 return log_msg_ret("buf", -ENOMEM);
328 /* Check that the header is not smaller than permitted */
329 if (priv->body_offset < PROBE_SIZE)
330 return log_msg_ret("san", EFAULT);
332 /* Read kernel body */
333 num_blks = priv->body_size >> desc->log2blksz;
334 log_debug("Reading body to %lx, blk=%s, size=%lx, blocks=%lx\n",
335 (ulong)map_to_sysmem(buf), bflow->blk->name, priv->body_size,
337 ret = blk_read(bflow->blk,
338 priv->part_start + (priv->body_offset >> desc->log2blksz),
341 return log_msg_ret("inf", -EIO);
342 base = map_to_sysmem(buf) + priv->bootloader_address -
343 priv->body_load_address;
344 setup = base + X86_SETUP_OFFSET;
347 bflow->x86_setup = map_sysmem(setup, 0);
352 static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow)
354 const struct vb2_kernel_preamble *preamble;
355 struct disk_partition info;
356 struct vb2_keyblock *hdr;
357 const char *uuid = NULL;
358 struct cros_priv *priv;
361 log_debug("starting, part=%x\n", bflow->part);
363 /* Check for kernel partitions */
364 ret = scan_part(bflow->blk, bflow->part, &info, &hdr);
366 log_debug("- scan failed: err=%d\n", ret);
367 return log_msg_ret("scan", ret);
370 priv = malloc(sizeof(struct cros_priv));
373 return log_msg_ret("buf", -ENOMEM);
375 bflow->bootmeth_priv = priv;
377 log_debug("Selected partition %d, header at %lx\n", bflow->part,
378 (ulong)map_to_sysmem(hdr));
380 /* Grab a few things from the preamble */
381 preamble = (void *)hdr + hdr->keyblock_size;
382 priv->body_offset = hdr->keyblock_size + preamble->preamble_size;
383 priv->part_start = info.start;
385 /* Now read everything we can learn about kernel */
386 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
389 ret = cros_read_info(bflow, uuid, preamble);
393 free(priv->info_buf);
395 return log_msg_ret("inf", ret);
397 bflow->size = priv->body_size;
398 bflow->state = BOOTFLOWST_READY;
403 static int cros_read_file(struct udevice *dev, struct bootflow *bflow,
404 const char *file_path, ulong addr, ulong *sizep)
409 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
410 static int cros_read_all(struct udevice *dev, struct bootflow *bflow)
415 return log_msg_ret("ld", -EALREADY);
416 ret = cros_read_kernel(bflow);
418 return log_msg_ret("rd", ret);
422 #endif /* BOOTSTD_FULL */
424 static int cros_boot(struct udevice *dev, struct bootflow *bflow)
429 ret = cros_read_kernel(bflow);
431 return log_msg_ret("rd", ret);
434 if (IS_ENABLED(CONFIG_X86)) {
435 ret = zboot_run(map_to_sysmem(bflow->buf), bflow->size, 0, 0,
436 map_to_sysmem(bflow->x86_setup),
439 ret = bootm_boot_start(map_to_sysmem(bflow->buf),
443 return log_msg_ret("go", ret);
446 static int cros_bootmeth_bind(struct udevice *dev)
448 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
450 plat->desc = "ChromiumOS boot";
451 plat->flags = BOOTMETHF_ANY_PART;
456 static struct bootmeth_ops cros_bootmeth_ops = {
458 .read_bootflow = cros_read_bootflow,
459 .read_file = cros_read_file,
461 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
462 .read_all = cros_read_all,
463 #endif /* BOOTSTD_FULL */
466 static const struct udevice_id cros_bootmeth_ids[] = {
467 { .compatible = "u-boot,cros" },
471 U_BOOT_DRIVER(bootmeth_cros) = {
472 .name = "bootmeth_cros",
473 .id = UCLASS_BOOTMETH,
474 .of_match = cros_bootmeth_ids,
475 .ops = &cros_bootmeth_ops,
476 .bind = cros_bootmeth_bind,