From defa33ad29f8d2897e78fdef6218b2eeea6acf6d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:47 -0600 Subject: bootstd: cros: Correct reporting of I/O errors Return -EIO when the read failed, rather than the number of blocks read. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index aa19ae097f5..6179a547f74 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -106,7 +106,7 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) return log_msg_ret("hdr", -ENOMEM); ret = blk_read(bflow->blk, info.start, num_blks, hdr); if (ret != num_blks) - return log_msg_ret("inf", ret); + return log_msg_ret("inf", -EIO); if (memcmp("CHROMEOS", hdr, 8)) return -ENOENT; @@ -125,7 +125,7 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) bflow->blk->name, (ulong)info.start, num_blks); ret = blk_read(bflow->blk, (ulong)info.start + 0x80, num_blks, buf); if (ret != num_blks) - return log_msg_ret("inf", ret); + return log_msg_ret("inf", -EIO); base = map_to_sysmem(buf); setup = base + start - OFFSET_BASE - SETUP_OFFSET; -- cgit v1.2.3 From 5a8589ebd6b20a4ba992bdf8806e574c339b12ea Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:48 -0600 Subject: bootstd: cros: Move partition reading into a function Move the code which reads a partition into its own function. Add a constant for the number of bytes to 'probe' at the start of the partition. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 66 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 16 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 6179a547f74..f6fb521f206 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -23,6 +23,8 @@ #include enum { + PROBE_SIZE = SZ_4K, /* initial bytes read from partition */ + /* Offsets in the kernel-partition header */ KERN_START = 0x4f0, KERN_SIZE = 0x518, @@ -77,6 +79,51 @@ static int copy_cmdline(const char *from, const char *uuid, char **bufp) return 0; } +/** + * scan_part() - Scan a kernel partition to see if has a ChromeOS header + * + * This reads the first PROBE_SIZE of a partition, loookng for CHROMEOS + * + * @blk: Block device to scan + * @partnum: Partition number to scan + * @info: Please to put partition info + * @hdrp: Return allocated keyblock header on success + */ +static int scan_part(struct udevice *blk, int partnum, + struct disk_partition *info, void **hdrp) +{ + struct blk_desc *desc = dev_get_uclass_plat(blk); + struct vb2_keyblock *hdr; + ulong num_blks; + int ret; + + ret = part_get_info(desc, partnum, info); + if (ret) + return log_msg_ret("part", ret); + + /* Make a buffer for the header information */ + num_blks = PROBE_SIZE >> desc->log2blksz; + log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n", + blk->name, (ulong)info->start, num_blks); + hdr = memalign(SZ_1K, PROBE_SIZE); + if (!hdr) + return log_msg_ret("hdr", -ENOMEM); + ret = blk_read(blk, info->start, num_blks, hdr); + if (ret != num_blks) { + free(hdr); + return log_msg_ret("inf", -EIO); + } + + if (memcmp("CHROMEOS", hdr, 8)) { + free(hdr); + return -ENOENT; + } + + *hdrp = hdr; + + return 0; +} + static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); @@ -93,23 +140,10 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) return log_msg_ret("max", -ENOENT); /* Check partition 2 */ - ret = part_get_info(desc, 2, &info); + ret = scan_part(bflow->blk, 2, &info, &hdr); if (ret) - return log_msg_ret("part", ret); - - /* Make a buffer for the header information */ - num_blks = SZ_4K >> desc->log2blksz; - log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n", - bflow->blk->name, (ulong)info.start, num_blks); - hdr = memalign(SZ_1K, SZ_4K); - if (!hdr) - return log_msg_ret("hdr", -ENOMEM); - ret = blk_read(bflow->blk, info.start, num_blks, hdr); - if (ret != num_blks) - return log_msg_ret("inf", -EIO); - - if (memcmp("CHROMEOS", hdr, 8)) - return -ENOENT; + return log_msg_ret("scan", ret); + bflow->part = 2; log_info("Header at %lx\n", (ulong)map_to_sysmem(hdr)); start = *(u32 *)(hdr + KERN_START); -- cgit v1.2.3 From de30aa9a2f5659ecee599d61e1940b30c76d627c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:49 -0600 Subject: bootstd: cros: Bring in some ChromiumOS structures Add a header file with structures for booting ChromiumOS, taken from the vboot tree. Using these makes it easier to understand the code. Note that the code style has not been updated for U-Boot, with use of uint64_t, __attribute__((packed)) and one comment-style nit. This should make it easier to keep the code in sync. It was taken from commit: 5b8596ce ("2sha256_arm: Fix data abort issue") Update the CHROMEOS string to use the defined values. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index f6fb521f206..385eca37381 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -21,6 +21,7 @@ #include #endif #include +#include "bootmeth_cros.h" enum { PROBE_SIZE = SZ_4K, /* initial bytes read from partition */ @@ -82,7 +83,8 @@ static int copy_cmdline(const char *from, const char *uuid, char **bufp) /** * scan_part() - Scan a kernel partition to see if has a ChromeOS header * - * This reads the first PROBE_SIZE of a partition, loookng for CHROMEOS + * This reads the first PROBE_SIZE of a partition, loookng for + * VB2_KEYBLOCK_MAGIC * * @blk: Block device to scan * @partnum: Partition number to scan @@ -114,7 +116,7 @@ static int scan_part(struct udevice *blk, int partnum, return log_msg_ret("inf", -EIO); } - if (memcmp("CHROMEOS", hdr, 8)) { + if (memcmp(VB2_KEYBLOCK_MAGIC, hdr->magic, VB2_KEYBLOCK_MAGIC_SIZE)) { free(hdr); return -ENOENT; } -- cgit v1.2.3 From 4cfe4510f1e6e0cf2e346f5bd9de5d5be346238d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:50 -0600 Subject: bootstd: cros: Support a kernel on either partition ChromiumOS allows a kernel to be on either partition 2 or 4. Add support for scanning both and using the first one we find with a suitable signature. Record the partition which is used. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 385eca37381..7b25042bfe5 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -133,7 +134,7 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) struct disk_partition info; const char *uuid = NULL; void *buf, *hdr; - int ret; + int part, ret; log_debug("starting, part=%d\n", bflow->part); @@ -141,13 +142,19 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) if (bflow->part) return log_msg_ret("max", -ENOENT); - /* Check partition 2 */ - ret = scan_part(bflow->blk, 2, &info, &hdr); - if (ret) - return log_msg_ret("scan", ret); - bflow->part = 2; + /* Check partition 2 then 4 */ + part = 2; + ret = scan_part(bflow->blk, part, &info, &hdr); + if (ret) { + part = 4; + ret = scan_part(bflow->blk, part, &info, &hdr); + if (ret) + return log_msg_ret("scan", ret); + } + bflow->part = part; - log_info("Header at %lx\n", (ulong)map_to_sysmem(hdr)); + log_info("Selected parition %d, header at %lx\n", bflow->part, + (ulong)map_to_sysmem(hdr)); start = *(u32 *)(hdr + KERN_START); size = ALIGN(*(u32 *)(hdr + KERN_SIZE), desc->blksz); log_debug("Reading start %lx size %lx\n", start, size); -- cgit v1.2.3 From 3257835e5640d92a22581d38a97b918a89cd3783 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:51 -0600 Subject: bootstd: cros: Decode some kernel preamble fields Decode the kernel start and size using the structures provided. This accesses the same data, just in a cleaner way. Add some logging for some of the fields in the kernel preamble. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 54 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 17 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 7b25042bfe5..2a745621e30 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -27,10 +27,6 @@ enum { PROBE_SIZE = SZ_4K, /* initial bytes read from partition */ - /* Offsets in the kernel-partition header */ - KERN_START = 0x4f0, - KERN_SIZE = 0x518, - SETUP_OFFSET = 0x1000, /* bytes before base */ CMDLINE_OFFSET = 0x2000, /* bytes before base */ OFFSET_BASE = 0x100000, /* assumed kernel load-address */ @@ -93,7 +89,7 @@ static int copy_cmdline(const char *from, const char *uuid, char **bufp) * @hdrp: Return allocated keyblock header on success */ static int scan_part(struct udevice *blk, int partnum, - struct disk_partition *info, void **hdrp) + struct disk_partition *info, struct vb2_keyblock **hdrp) { struct blk_desc *desc = dev_get_uclass_plat(blk); struct vb2_keyblock *hdr; @@ -130,11 +126,14 @@ static int scan_part(struct udevice *blk, int partnum, static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); - ulong base, start, size, setup, cmdline, num_blks, kern_base; + ulong base, start, setup, cmdline, num_blks, kern_base; + const struct vb2_kernel_preamble *preamble; + ulong body_offset, body_size; struct disk_partition info; const char *uuid = NULL; - void *buf, *hdr; + struct vb2_keyblock *hdr; int part, ret; + void *buf; log_debug("starting, part=%d\n", bflow->part); @@ -155,18 +154,39 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) log_info("Selected parition %d, header at %lx\n", bflow->part, (ulong)map_to_sysmem(hdr)); - start = *(u32 *)(hdr + KERN_START); - size = ALIGN(*(u32 *)(hdr + KERN_SIZE), desc->blksz); - log_debug("Reading start %lx size %lx\n", start, size); - bflow->size = size; - - buf = memalign(SZ_1K, size); + preamble = (void *)hdr + hdr->keyblock_size; + log_debug("Kernel preamble at %lx, version major %x, minor %x\n", + (ulong)map_to_sysmem(preamble), + preamble->header_version_major, + preamble->header_version_minor); + + start = (ulong)preamble->bootloader_address; + log_debug(" - load_address %lx, bl_addr %lx, bl_size %lx\n", + (ulong)preamble->body_load_address, + (ulong)preamble->bootloader_address, + (ulong)preamble->bootloader_size); + + body_offset = hdr->keyblock_size + preamble->preamble_size; + body_size = preamble->body_signature.data_size; + log_debug("Kernel body at %lx size %lx\n", body_offset, body_size); + bflow->size = body_size; + + buf = memalign(SZ_1K, body_size); if (!buf) return log_msg_ret("buf", -ENOMEM); - num_blks = size >> desc->log2blksz; - log_debug("Reading data, blk=%s, start=%lx, blocks=%lx\n", - bflow->blk->name, (ulong)info.start, num_blks); - ret = blk_read(bflow->blk, (ulong)info.start + 0x80, num_blks, buf); + + /* Check that the header is not smaller than permitted */ + if (body_offset < PROBE_SIZE) + return log_msg_ret("san", EFAULT); + + /* Read kernel body */ + num_blks = body_size >> desc->log2blksz; + log_debug("Reading body to %lx, blk=%s, size=%lx, blocks=%lx\n", + (ulong)map_to_sysmem(buf), bflow->blk->name, body_size, + num_blks); + ret = blk_read(bflow->blk, + info.start + (body_offset >> desc->log2blksz), + num_blks, buf); if (ret != num_blks) return log_msg_ret("inf", -EIO); base = map_to_sysmem(buf); -- cgit v1.2.3 From c5dca50bbb07a0040197f9a6ccf5da3493736b5d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:52 -0600 Subject: bootstd: cros: Simplify setup and cmdline expressions Create a common base from which the other parts are offset and make all of the offsets related to that. This makes the code a little easier to read. Use X86_ prefixes for the two values which are x86-specific. Drop OFFSET_BASE since it is available in a header field. Drop the unnecessary 'start' variable too. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 2a745621e30..05284713f67 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -27,9 +27,9 @@ enum { PROBE_SIZE = SZ_4K, /* initial bytes read from partition */ - SETUP_OFFSET = 0x1000, /* bytes before base */ - CMDLINE_OFFSET = 0x2000, /* bytes before base */ - OFFSET_BASE = 0x100000, /* assumed kernel load-address */ + X86_SETUP_OFFSET = -0x1000, /* setup offset relative to base */ + CMDLINE_OFFSET = -0x2000, /* cmdline offset relative to base */ + X86_KERNEL_OFFSET = 0x4000, /* kernel offset relative to base */ }; static int cros_check(struct udevice *dev, struct bootflow_iter *iter) @@ -126,7 +126,7 @@ static int scan_part(struct udevice *blk, int partnum, static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); - ulong base, start, setup, cmdline, num_blks, kern_base; + ulong base, setup, cmdline, num_blks, kern_base; const struct vb2_kernel_preamble *preamble; ulong body_offset, body_size; struct disk_partition info; @@ -160,7 +160,6 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) preamble->header_version_major, preamble->header_version_minor); - start = (ulong)preamble->bootloader_address; log_debug(" - load_address %lx, bl_addr %lx, bl_size %lx\n", (ulong)preamble->body_load_address, (ulong)preamble->bootloader_address, @@ -189,12 +188,13 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) num_blks, buf); if (ret != num_blks) return log_msg_ret("inf", -EIO); - base = map_to_sysmem(buf); + base = map_to_sysmem(buf) + preamble->bootloader_address - + preamble->body_load_address; - setup = base + start - OFFSET_BASE - SETUP_OFFSET; - cmdline = base + start - OFFSET_BASE - CMDLINE_OFFSET; - kern_base = base + start - OFFSET_BASE + SZ_16K; - log_debug("base %lx setup %lx, cmdline %lx, kern_base %lx\n", base, + setup = base + X86_SETUP_OFFSET; + cmdline = base + CMDLINE_OFFSET; + kern_base = base + X86_KERNEL_OFFSET; + log_debug("base %lx setup %lx cmdline %lx kern_base %lx\n", base, setup, cmdline, kern_base); #ifdef CONFIG_X86 -- cgit v1.2.3 From 1a0810924a29311a330d717a2813d212865a5df0 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:53 -0600 Subject: bootstd: Move common zimage functions to bootm.h We want to avoid using #ifdefs around header files and in the code. It makes sense to collect the various functions used for loading images into a single header which can be included by all architectures. The best place for this is the arch-neutral bootm.h header, so use that. Move some zimage functions into this bootm.h header. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 05284713f67..3b9e75540aa 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -12,15 +12,13 @@ #include #include #include +#include #include #include #include #include #include #include -#ifdef CONFIG_X86 -#include -#endif #include #include "bootmeth_cros.h" -- cgit v1.2.3 From b7ed5386a4502bf48e4b04def69afae39873b536 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:54 -0600 Subject: bootstd: cros: Add docs for the kernel layout Provide brief documentation about the ChromiumOS kernel layout. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 3b9e75540aa..a551d43701d 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -22,6 +22,35 @@ #include #include "bootmeth_cros.h" +/* + * Layout of the ChromeOS kernel + * + * Partitions 2 and 4 contain kernels + * + * Contents are: + * + * Offset Contents + * 0 struct vb2_keyblock + * m struct vb2_kernel_preamble + * m + n kernel buffer + * + * m is keyblock->keyblock_size + * n is preamble->preamble_size + * + * The kernel buffer itself consists of various parts: + * + * Offset Contents + * m + n kernel image (Flat vmlinux binary or FIT) + * b - 8KB Command line text + * b - 4KB X86 setup block (struct boot_params, extends for about 16KB) + * b X86 bootloader (continuation of setup block) + * b + 16KB X86 setup block (copy, used for hold data pointed to) + * + * b is m + n + preamble->bootloader_address - preamble->body_load_address + * + * Useful metadata extends from b - 8KB through to b + 32 KB + */ + enum { PROBE_SIZE = SZ_4K, /* initial bytes read from partition */ -- cgit v1.2.3 From 1d4bbdf3e493b173d5f73150cea51c1965e3e1df Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:55 -0600 Subject: bootstd: cros: Add private info for ChromiumOS Create a new private structure to hold information gleaned from the disk. This will allow separation between reading of the bootflow information and (later) reading the whole kernel. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 55 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index a551d43701d..16a42ad46ae 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -59,6 +59,29 @@ enum { X86_KERNEL_OFFSET = 0x4000, /* kernel offset relative to base */ }; +/** + * struct cros_priv - Private data + * + * This is read from the disk and recorded for use when the full kernel must + * be loaded and booted + * + * @body_offset: Offset of kernel body from start of partition (in bytes) + * @body_size: Size of kernel body in bytes + * @part_start: Block offset of selected partition from the start of the disk + * @body_load_address: Nominal load address for kernel body + * @bootloader_address: Address of bootloader, after body is loaded at + * body_load_address + * @bootloader_size: Size of bootloader in bytes + */ +struct cros_priv { + ulong body_offset; + ulong body_size; + lbaint_t part_start; + ulong body_load_address; + ulong bootloader_address; + ulong bootloader_size; +}; + static int cros_check(struct udevice *dev, struct bootflow_iter *iter) { /* This only works on block and network devices */ @@ -155,7 +178,7 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); ulong base, setup, cmdline, num_blks, kern_base; const struct vb2_kernel_preamble *preamble; - ulong body_offset, body_size; + struct cros_priv s_priv, *priv = &s_priv; struct disk_partition info; const char *uuid = NULL; struct vb2_keyblock *hdr; @@ -192,31 +215,37 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) (ulong)preamble->bootloader_address, (ulong)preamble->bootloader_size); - body_offset = hdr->keyblock_size + preamble->preamble_size; - body_size = preamble->body_signature.data_size; - log_debug("Kernel body at %lx size %lx\n", body_offset, body_size); - bflow->size = body_size; - - buf = memalign(SZ_1K, body_size); + priv->body_offset = hdr->keyblock_size + preamble->preamble_size; + priv->part_start = info.start; + priv->body_size = preamble->body_signature.data_size; + priv->body_load_address = preamble->body_load_address; + priv->bootloader_address = preamble->bootloader_address; + priv->bootloader_size = preamble->bootloader_size; + log_debug("Kernel body at %lx size %lx\n", priv->body_offset, + priv->body_size); + bflow->size = priv->body_size; + + buf = memalign(SZ_1K, priv->body_size); if (!buf) return log_msg_ret("buf", -ENOMEM); /* Check that the header is not smaller than permitted */ - if (body_offset < PROBE_SIZE) + if (priv->body_offset < PROBE_SIZE) return log_msg_ret("san", EFAULT); /* Read kernel body */ - num_blks = body_size >> desc->log2blksz; + num_blks = priv->body_size >> desc->log2blksz; log_debug("Reading body to %lx, blk=%s, size=%lx, blocks=%lx\n", - (ulong)map_to_sysmem(buf), bflow->blk->name, body_size, + (ulong)map_to_sysmem(buf), bflow->blk->name, priv->body_size, num_blks); ret = blk_read(bflow->blk, - info.start + (body_offset >> desc->log2blksz), + priv->part_start + + (priv->body_offset >> desc->log2blksz), num_blks, buf); if (ret != num_blks) return log_msg_ret("inf", -EIO); - base = map_to_sysmem(buf) + preamble->bootloader_address - - preamble->body_load_address; + base = map_to_sysmem(buf) + priv->bootloader_address - + priv->body_load_address; setup = base + X86_SETUP_OFFSET; cmdline = base + CMDLINE_OFFSET; -- cgit v1.2.3 From f861b1ee70aafff68a9a1ecbffb0bc79b71d13eb Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:57 -0600 Subject: bootstd: cros: Add a function to read info from partition The code to read the ChromiumOS information from the partition is currently all in one function. It reads the entire kernel, which is unnecessary unless it is to be booted. Create a new function which reads just the minimum required data from the disk, then obtains what it needs from there. For now this function is not used. Future work will plumb it in. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 16a42ad46ae..82cbdcf2c93 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -72,6 +72,7 @@ enum { * @bootloader_address: Address of bootloader, after body is loaded at * body_load_address * @bootloader_size: Size of bootloader in bytes + * @info_buf: Buffer containing ChromiumOS info */ struct cros_priv { ulong body_offset; @@ -80,6 +81,7 @@ struct cros_priv { ulong body_load_address; ulong bootloader_address; ulong bootloader_size; + void *info_buf; }; static int cros_check(struct udevice *dev, struct bootflow_iter *iter) @@ -173,6 +175,125 @@ static int scan_part(struct udevice *blk, int partnum, return 0; } +/** + * cros_read_buf() - Read information into a buf and parse it + * + * @bflow: Bootflow to update + * @buf: Buffer to use + * @size: Size of buffer and number of bytes to read thereinto + * @start: Start offset to read from on disk + * @before_base: Number of bytes to read before the bootloader base + * @uuid: UUID string if supported, else NULL + * Return: 0 if OK, -ENOMEM if out of memory, -EIO on read failure + */ +static int cros_read_buf(struct bootflow *bflow, void *buf, ulong size, + loff_t start, ulong before_base, const char *uuid) +{ + struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); + ulong base, setup, cmdline, kern_base; + ulong num_blks; + int ret; + + num_blks = size >> desc->log2blksz; + log_debug("Reading info to %lx, blk=%s, size=%lx, blocks=%lx\n", + (ulong)map_to_sysmem(buf), bflow->blk->name, size, num_blks); + ret = blk_read(bflow->blk, start, num_blks, buf); + if (ret != num_blks) + return log_msg_ret("inf", -EIO); + base = map_to_sysmem(buf) + before_base; + + setup = base + X86_SETUP_OFFSET; + cmdline = base + CMDLINE_OFFSET; + kern_base = base + X86_KERNEL_OFFSET; + log_debug("base %lx setup %lx cmdline %lx kern_base %lx\n", base, + setup, cmdline, kern_base); + +#ifdef CONFIG_X86 + const char *version; + + version = zimage_get_kernel_version(map_sysmem(setup, 0), + map_sysmem(kern_base, 0)); + log_debug("version %s\n", version); + if (version) + bflow->name = strdup(version); +#endif + if (!bflow->name) + bflow->name = strdup("ChromeOS"); + if (!bflow->name) + return log_msg_ret("nam", -ENOMEM); + bflow->os_name = strdup("ChromeOS"); + if (!bflow->os_name) + return log_msg_ret("os", -ENOMEM); + + ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline); + if (ret) + return log_msg_ret("cmd", ret); + bflow->x86_setup = map_sysmem(setup, 0); + + return 0; +} + +/** + * cros_read_info() - Read information and fill out the bootflow + * + * @bflow: Bootflow to update + * @uuid: UUID string if supported, else NULL + * @preamble: Kernel preamble information + * Return: 0 if OK, -ENOMEM if out of memory, -EIO on read failure + */ +static int cros_read_info(struct bootflow *bflow, const char *uuid, + const struct vb2_kernel_preamble *preamble) +{ + struct cros_priv *priv = bflow->bootmeth_priv; + struct udevice *blk = bflow->blk; + struct blk_desc *desc = dev_get_uclass_plat(blk); + ulong offset, size, before_base; + void *buf; + int ret; + + log_debug("Kernel preamble at %lx, version major %x, minor %x\n", + (ulong)map_to_sysmem(preamble), + preamble->header_version_major, + preamble->header_version_minor); + + log_debug(" - load_address %lx, bl_addr %lx, bl_size %lx\n", + (ulong)preamble->body_load_address, + (ulong)preamble->bootloader_address, + (ulong)preamble->bootloader_size); + + priv->body_size = preamble->body_signature.data_size; + priv->body_load_address = preamble->body_load_address; + priv->bootloader_address = preamble->bootloader_address; + priv->bootloader_size = preamble->bootloader_size; + log_debug("Kernel body at %lx size %lx\n", priv->body_offset, + priv->body_size); + + /* Work out how many bytes to read before the bootloader base */ + before_base = -CMDLINE_OFFSET; + + /* Read the cmdline through to the end of the bootloader */ + size = priv->bootloader_size + before_base; + offset = priv->body_offset + + (priv->bootloader_address - priv->body_load_address) + + CMDLINE_OFFSET; + buf = malloc(size); + if (!buf) + return log_msg_ret("buf", -ENOMEM); + + ret = cros_read_buf(bflow, buf, size, + priv->part_start + (offset >> desc->log2blksz), + before_base, uuid); + if (ret) { + /* Clear this since the buffer is invalid */ + bflow->x86_setup = NULL; + free(buf); + return log_msg_ret("pro", ret); + } + priv->info_buf = buf; + + return 0; +} + static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); -- cgit v1.2.3 From 074503c40a14f0e35b69bb3f06bb255b6881ca4e Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:58 -0600 Subject: bootstd: cros: Add a function to read a kernel The code to read the ChromiumOS information from the partition is currently all in one function. Create a new function which reads the kernel, assuming that the metadata has been parsed. For now this function is not used. Future work will plumb it in. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 82cbdcf2c93..8728cd18e3c 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -294,6 +294,45 @@ static int cros_read_info(struct bootflow *bflow, const char *uuid, return 0; } +static int cros_read_kernel(struct bootflow *bflow) +{ + struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); + struct cros_priv *priv = bflow->bootmeth_priv; + ulong base, setup; + ulong num_blks; + void *buf; + int ret; + + bflow->size = priv->body_size; + + buf = memalign(SZ_1K, priv->body_size); + if (!buf) + return log_msg_ret("buf", -ENOMEM); + + /* Check that the header is not smaller than permitted */ + if (priv->body_offset < PROBE_SIZE) + return log_msg_ret("san", EFAULT); + + /* Read kernel body */ + num_blks = priv->body_size >> desc->log2blksz; + log_debug("Reading body to %lx, blk=%s, size=%lx, blocks=%lx\n", + (ulong)map_to_sysmem(buf), bflow->blk->name, priv->body_size, + num_blks); + ret = blk_read(bflow->blk, + priv->part_start + (priv->body_offset >> desc->log2blksz), + num_blks, buf); + if (ret != num_blks) + return log_msg_ret("inf", -EIO); + base = map_to_sysmem(buf) + priv->bootloader_address - + priv->body_load_address; + setup = base + X86_SETUP_OFFSET; + + bflow->buf = buf; + bflow->x86_setup = map_sysmem(setup, 0); + + return 0; +} + static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); -- cgit v1.2.3 From 598dea978d5b7076fc5b6ed31bea0767925e8db0 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:16:59 -0600 Subject: bootstd: cros: Split up reading info and kernel Use the two new functions to separate reading of the ChromiumOS info from the partition from actually reading the kernel and booting it. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 96 +++++++++++++--------------------------------------- 1 file changed, 23 insertions(+), 73 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 8728cd18e3c..06709dd9171 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -335,15 +335,12 @@ static int cros_read_kernel(struct bootflow *bflow) static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) { - struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); - ulong base, setup, cmdline, num_blks, kern_base; const struct vb2_kernel_preamble *preamble; - struct cros_priv s_priv, *priv = &s_priv; struct disk_partition info; - const char *uuid = NULL; struct vb2_keyblock *hdr; + const char *uuid = NULL; + struct cros_priv *priv; int part, ret; - void *buf; log_debug("starting, part=%d\n", bflow->part); @@ -362,84 +359,32 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) } bflow->part = part; - log_info("Selected parition %d, header at %lx\n", bflow->part, - (ulong)map_to_sysmem(hdr)); - preamble = (void *)hdr + hdr->keyblock_size; - log_debug("Kernel preamble at %lx, version major %x, minor %x\n", - (ulong)map_to_sysmem(preamble), - preamble->header_version_major, - preamble->header_version_minor); + priv = malloc(sizeof(struct cros_priv)); + if (!priv) { + free(hdr); + return log_msg_ret("buf", -ENOMEM); + } + bflow->bootmeth_priv = priv; - log_debug(" - load_address %lx, bl_addr %lx, bl_size %lx\n", - (ulong)preamble->body_load_address, - (ulong)preamble->bootloader_address, - (ulong)preamble->bootloader_size); + log_info("Selected partition %d, header at %lx\n", bflow->part, + (ulong)map_to_sysmem(hdr)); + /* Grab a few things from the preamble */ + preamble = (void *)hdr + hdr->keyblock_size; priv->body_offset = hdr->keyblock_size + preamble->preamble_size; priv->part_start = info.start; - priv->body_size = preamble->body_signature.data_size; - priv->body_load_address = preamble->body_load_address; - priv->bootloader_address = preamble->bootloader_address; - priv->bootloader_size = preamble->bootloader_size; - log_debug("Kernel body at %lx size %lx\n", priv->body_offset, - priv->body_size); - bflow->size = priv->body_size; - - buf = memalign(SZ_1K, priv->body_size); - if (!buf) - return log_msg_ret("buf", -ENOMEM); - - /* Check that the header is not smaller than permitted */ - if (priv->body_offset < PROBE_SIZE) - return log_msg_ret("san", EFAULT); - - /* Read kernel body */ - num_blks = priv->body_size >> desc->log2blksz; - log_debug("Reading body to %lx, blk=%s, size=%lx, blocks=%lx\n", - (ulong)map_to_sysmem(buf), bflow->blk->name, priv->body_size, - num_blks); - ret = blk_read(bflow->blk, - priv->part_start + - (priv->body_offset >> desc->log2blksz), - num_blks, buf); - if (ret != num_blks) - return log_msg_ret("inf", -EIO); - base = map_to_sysmem(buf) + priv->bootloader_address - - priv->body_load_address; - - setup = base + X86_SETUP_OFFSET; - cmdline = base + CMDLINE_OFFSET; - kern_base = base + X86_KERNEL_OFFSET; - log_debug("base %lx setup %lx cmdline %lx kern_base %lx\n", base, - setup, cmdline, kern_base); - -#ifdef CONFIG_X86 - const char *version; - - version = zimage_get_kernel_version(map_sysmem(setup, 0), - map_sysmem(kern_base, 0)); - log_debug("version %s\n", version); - if (version) - bflow->name = strdup(version); -#endif - if (!bflow->name) - bflow->name = strdup("ChromeOS"); - if (!bflow->name) - return log_msg_ret("nam", -ENOMEM); - bflow->os_name = strdup("ChromeOS"); - if (!bflow->os_name) - return log_msg_ret("os", -ENOMEM); + /* Now read everything we can learn about kernel */ #if CONFIG_IS_ENABLED(PARTITION_UUIDS) uuid = info.uuid; #endif - ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline); + ret = cros_read_info(bflow, uuid, preamble); + preamble = NULL; + free(hdr); if (ret) - return log_msg_ret("cmd", ret); - + return log_msg_ret("inf", ret); + bflow->size = priv->body_size; bflow->state = BOOTFLOWST_READY; - bflow->buf = buf; - bflow->x86_setup = map_sysmem(setup, 0); return 0; } @@ -452,6 +397,11 @@ static int cros_read_file(struct udevice *dev, struct bootflow *bflow, static int cros_boot(struct udevice *dev, struct bootflow *bflow) { + int ret; + + ret = cros_read_kernel(bflow); + if (ret) + return log_msg_ret("rd", ret); #ifdef CONFIG_X86 zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0, map_to_sysmem(bflow->x86_setup), -- cgit v1.2.3 From c279224ea6686a992b258b01e07fcadb7f0c7ecb Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 10 Aug 2023 19:33:18 -0600 Subject: bootstd: Add a command to read all files for a bootflow Some bootflows (such as EFI and ChromiumOS) delay reading the kernel until it is needed to boot. This saves time when scanning and avoids needing to allocate memory for something that may never be used. To permit reading of these files, add a new 'bootflow read' command. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 06709dd9171..6c28feb34fe 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -395,13 +395,30 @@ static int cros_read_file(struct udevice *dev, struct bootflow *bflow, return -ENOSYS; } -static int cros_boot(struct udevice *dev, struct bootflow *bflow) +#if CONFIG_IS_ENABLED(BOOSTD_FULL) +static int cros_read_all(struct udevice *dev, struct bootflow *bflow) { int ret; + if (bflow->buf) + return log_msg_ret("ld", -EALREADY); ret = cros_read_kernel(bflow); if (ret) return log_msg_ret("rd", ret); + + return 0; +} +#endif /* BOOSTD_FULL */ + +static int cros_boot(struct udevice *dev, struct bootflow *bflow) +{ + int ret; + + if (!bflow->buf) { + ret = cros_read_kernel(bflow); + if (ret) + return log_msg_ret("rd", ret); + } #ifdef CONFIG_X86 zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0, map_to_sysmem(bflow->x86_setup), @@ -425,6 +442,9 @@ static struct bootmeth_ops cros_bootmeth_ops = { .read_bootflow = cros_read_bootflow, .read_file = cros_read_file, .boot = cros_boot, +#if CONFIG_IS_ENABLED(BOOSTD_FULL) + .read_all = cros_read_all, +#endif /* BOOSTD_FULL */ }; static const struct udevice_id cros_bootmeth_ids[] = { -- cgit v1.2.3 From daffb0be2c839f3abe431cd68c772fae0e7e49ca Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sun, 30 Jul 2023 11:17:02 -0600 Subject: bootstd: cros: Add ARM support Support booting ChromiumOS on ARM devices using FIT. Add an entry into the boot implementation which does not require a command line. This can be expanded over time as the bootm code is refactored. Signed-off-by: Simon Glass --- boot/bootmeth_cros.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 6c28feb34fe..1776fb1838c 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -419,13 +419,17 @@ static int cros_boot(struct udevice *dev, struct bootflow *bflow) if (ret) return log_msg_ret("rd", ret); } -#ifdef CONFIG_X86 - zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0, - map_to_sysmem(bflow->x86_setup), - bflow->cmdline); -#endif - return log_msg_ret("go", -EFAULT); + if (IS_ENABLED(CONFIG_X86)) { + ret = zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0, + map_to_sysmem(bflow->x86_setup), + bflow->cmdline); + } else { + ret = bootm_boot_start(map_to_sysmem(bflow->buf), + bflow->cmdline); + } + + return log_msg_ret("go", ret); } static int cros_bootmeth_bind(struct udevice *dev) -- cgit v1.2.3 From 71f634b822ae6613c43fc960d5afbe5b5d728fb4 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Thu, 24 Aug 2023 13:55:45 -0600 Subject: bootstd: cros: Allow detection of any kernel partition The existing ChromiumOS bootmeth only supports reading a single kernel partition, either 2 or 4. In fact there are normally two options available. Use the GUID to detect kernel partitions, with the BOOTMETHF_ANY_PART flag, so that bootstd does not require a valid filesystem before calling the bootmeth. Tidy up and improve the logging while we are here. Signed-off-by: Simon Glass Suggested-by: Alper Nebi Yasak [trini: Add missing select of PARTITION_TYPE_GUID] Signed-off-by: Tom Rini --- boot/bootmeth_cros.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'boot/bootmeth_cros.c') diff --git a/boot/bootmeth_cros.c b/boot/bootmeth_cros.c index 1776fb1838c..20e0b1e89c3 100644 --- a/boot/bootmeth_cros.c +++ b/boot/bootmeth_cros.c @@ -16,16 +16,19 @@ #include #include #include +#include #include #include #include #include #include "bootmeth_cros.h" +static const efi_guid_t cros_kern_type = PARTITION_CROS_KERNEL; + /* * Layout of the ChromeOS kernel * - * Partitions 2 and 4 contain kernels + * Partitions 2 and 4 contain kernels with type GUID_CROS_KERNEL * * Contents are: * @@ -145,13 +148,25 @@ static int scan_part(struct udevice *blk, int partnum, { struct blk_desc *desc = dev_get_uclass_plat(blk); struct vb2_keyblock *hdr; + struct uuid type; ulong num_blks; int ret; + if (!partnum) + return log_msg_ret("efi", -ENOENT); + ret = part_get_info(desc, partnum, info); if (ret) return log_msg_ret("part", ret); + /* Check for kernel partition type */ + log_debug("part %x: type=%s\n", partnum, info->type_guid); + if (uuid_str_to_bin(info->type_guid, (u8 *)&type, UUID_STR_FORMAT_GUID)) + return log_msg_ret("typ", -EINVAL); + + if (memcmp(&cros_kern_type, &type, sizeof(type))) + return log_msg_ret("typ", -ENOEXEC); + /* Make a buffer for the header information */ num_blks = PROBE_SIZE >> desc->log2blksz; log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n", @@ -167,6 +182,7 @@ static int scan_part(struct udevice *blk, int partnum, if (memcmp(VB2_KEYBLOCK_MAGIC, hdr->magic, VB2_KEYBLOCK_MAGIC_SIZE)) { free(hdr); + log_debug("no magic\n"); return -ENOENT; } @@ -340,24 +356,16 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) struct vb2_keyblock *hdr; const char *uuid = NULL; struct cros_priv *priv; - int part, ret; - - log_debug("starting, part=%d\n", bflow->part); + int ret; - /* We consider the whole disk, not any one partition */ - if (bflow->part) - return log_msg_ret("max", -ENOENT); + log_debug("starting, part=%x\n", bflow->part); - /* Check partition 2 then 4 */ - part = 2; - ret = scan_part(bflow->blk, part, &info, &hdr); + /* Check for kernel partitions */ + ret = scan_part(bflow->blk, bflow->part, &info, &hdr); if (ret) { - part = 4; - ret = scan_part(bflow->blk, part, &info, &hdr); - if (ret) - return log_msg_ret("scan", ret); + log_debug("- scan failed: err=%d\n", ret); + return log_msg_ret("scan", ret); } - bflow->part = part; priv = malloc(sizeof(struct cros_priv)); if (!priv) { @@ -366,8 +374,8 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) } bflow->bootmeth_priv = priv; - log_info("Selected partition %d, header at %lx\n", bflow->part, - (ulong)map_to_sysmem(hdr)); + log_debug("Selected partition %d, header at %lx\n", bflow->part, + (ulong)map_to_sysmem(hdr)); /* Grab a few things from the preamble */ preamble = (void *)hdr + hdr->keyblock_size; @@ -381,8 +389,11 @@ static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) ret = cros_read_info(bflow, uuid, preamble); preamble = NULL; free(hdr); - if (ret) + if (ret) { + free(priv->info_buf); + free(priv); return log_msg_ret("inf", ret); + } bflow->size = priv->body_size; bflow->state = BOOTFLOWST_READY; @@ -437,6 +448,7 @@ static int cros_bootmeth_bind(struct udevice *dev) struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); plat->desc = "ChromiumOS boot"; + plat->flags = BOOTMETHF_ANY_PART; return 0; } -- cgit v1.2.3