Simon Glass | d74e62a | 2023-07-12 09:04:45 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Bootmethod for ChromiumOS |
| 4 | * |
| 5 | * Copyright 2023 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <blk.h> |
| 13 | #include <bootdev.h> |
| 14 | #include <bootflow.h> |
| 15 | #include <bootmeth.h> |
| 16 | #include <dm.h> |
| 17 | #include <malloc.h> |
| 18 | #include <mapmem.h> |
| 19 | #include <part.h> |
| 20 | #ifdef CONFIG_X86 |
| 21 | #include <asm/zimage.h> |
| 22 | #endif |
| 23 | #include <linux/sizes.h> |
| 24 | |
| 25 | enum { |
Simon Glass | 09ebe82 | 2023-07-30 11:16:48 -0600 | [diff] [blame^] | 26 | PROBE_SIZE = SZ_4K, /* initial bytes read from partition */ |
| 27 | |
Simon Glass | d74e62a | 2023-07-12 09:04:45 -0600 | [diff] [blame] | 28 | /* Offsets in the kernel-partition header */ |
| 29 | KERN_START = 0x4f0, |
| 30 | KERN_SIZE = 0x518, |
| 31 | |
| 32 | SETUP_OFFSET = 0x1000, /* bytes before base */ |
| 33 | CMDLINE_OFFSET = 0x2000, /* bytes before base */ |
| 34 | OFFSET_BASE = 0x100000, /* assumed kernel load-address */ |
| 35 | }; |
| 36 | |
| 37 | static int cros_check(struct udevice *dev, struct bootflow_iter *iter) |
| 38 | { |
| 39 | /* This only works on block and network devices */ |
| 40 | if (bootflow_iter_check_blk(iter)) |
| 41 | return log_msg_ret("blk", -ENOTSUPP); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int copy_cmdline(const char *from, const char *uuid, char **bufp) |
| 47 | { |
| 48 | const int maxlen = 2048; |
| 49 | char buf[maxlen]; |
| 50 | char *cmd, *to, *end; |
| 51 | int len; |
| 52 | |
| 53 | /* Allow space for cmdline + UUID */ |
| 54 | len = strnlen(from, sizeof(buf)); |
| 55 | if (len >= maxlen) |
| 56 | return -E2BIG; |
| 57 | |
| 58 | log_debug("uuid %d %s\n", uuid ? (int)strlen(uuid) : 0, uuid); |
| 59 | for (to = buf, end = buf + maxlen - UUID_STR_LEN - 1; *from; from++) { |
| 60 | if (to >= end) |
| 61 | return -E2BIG; |
| 62 | if (from[0] == '%' && from[1] == 'U' && uuid && |
| 63 | strlen(uuid) == UUID_STR_LEN) { |
| 64 | strcpy(to, uuid); |
| 65 | to += UUID_STR_LEN; |
| 66 | from++; |
| 67 | } else { |
| 68 | *to++ = *from; |
| 69 | } |
| 70 | } |
| 71 | *to = '\0'; |
| 72 | len = to - buf; |
| 73 | cmd = strdup(buf); |
| 74 | if (!cmd) |
| 75 | return -ENOMEM; |
| 76 | free(*bufp); |
| 77 | *bufp = cmd; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Simon Glass | 09ebe82 | 2023-07-30 11:16:48 -0600 | [diff] [blame^] | 82 | /** |
| 83 | * scan_part() - Scan a kernel partition to see if has a ChromeOS header |
| 84 | * |
| 85 | * This reads the first PROBE_SIZE of a partition, loookng for CHROMEOS |
| 86 | * |
| 87 | * @blk: Block device to scan |
| 88 | * @partnum: Partition number to scan |
| 89 | * @info: Please to put partition info |
| 90 | * @hdrp: Return allocated keyblock header on success |
| 91 | */ |
| 92 | static int scan_part(struct udevice *blk, int partnum, |
| 93 | struct disk_partition *info, void **hdrp) |
| 94 | { |
| 95 | struct blk_desc *desc = dev_get_uclass_plat(blk); |
| 96 | struct vb2_keyblock *hdr; |
| 97 | ulong num_blks; |
| 98 | int ret; |
| 99 | |
| 100 | ret = part_get_info(desc, partnum, info); |
| 101 | if (ret) |
| 102 | return log_msg_ret("part", ret); |
| 103 | |
| 104 | /* Make a buffer for the header information */ |
| 105 | num_blks = PROBE_SIZE >> desc->log2blksz; |
| 106 | log_debug("Reading header, blk=%s, start=%lx, blocks=%lx\n", |
| 107 | blk->name, (ulong)info->start, num_blks); |
| 108 | hdr = memalign(SZ_1K, PROBE_SIZE); |
| 109 | if (!hdr) |
| 110 | return log_msg_ret("hdr", -ENOMEM); |
| 111 | ret = blk_read(blk, info->start, num_blks, hdr); |
| 112 | if (ret != num_blks) { |
| 113 | free(hdr); |
| 114 | return log_msg_ret("inf", -EIO); |
| 115 | } |
| 116 | |
| 117 | if (memcmp("CHROMEOS", hdr, 8)) { |
| 118 | free(hdr); |
| 119 | return -ENOENT; |
| 120 | } |
| 121 | |
| 122 | *hdrp = hdr; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Simon Glass | d74e62a | 2023-07-12 09:04:45 -0600 | [diff] [blame] | 127 | static int cros_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
| 128 | { |
| 129 | struct blk_desc *desc = dev_get_uclass_plat(bflow->blk); |
| 130 | ulong base, start, size, setup, cmdline, num_blks, kern_base; |
| 131 | struct disk_partition info; |
| 132 | const char *uuid = NULL; |
| 133 | void *buf, *hdr; |
| 134 | int ret; |
| 135 | |
| 136 | log_debug("starting, part=%d\n", bflow->part); |
| 137 | |
| 138 | /* We consider the whole disk, not any one partition */ |
| 139 | if (bflow->part) |
| 140 | return log_msg_ret("max", -ENOENT); |
| 141 | |
| 142 | /* Check partition 2 */ |
Simon Glass | 09ebe82 | 2023-07-30 11:16:48 -0600 | [diff] [blame^] | 143 | ret = scan_part(bflow->blk, 2, &info, &hdr); |
Simon Glass | d74e62a | 2023-07-12 09:04:45 -0600 | [diff] [blame] | 144 | if (ret) |
Simon Glass | 09ebe82 | 2023-07-30 11:16:48 -0600 | [diff] [blame^] | 145 | return log_msg_ret("scan", ret); |
| 146 | bflow->part = 2; |
Simon Glass | d74e62a | 2023-07-12 09:04:45 -0600 | [diff] [blame] | 147 | |
| 148 | log_info("Header at %lx\n", (ulong)map_to_sysmem(hdr)); |
| 149 | start = *(u32 *)(hdr + KERN_START); |
| 150 | size = ALIGN(*(u32 *)(hdr + KERN_SIZE), desc->blksz); |
| 151 | log_debug("Reading start %lx size %lx\n", start, size); |
| 152 | bflow->size = size; |
| 153 | |
| 154 | buf = memalign(SZ_1K, size); |
| 155 | if (!buf) |
| 156 | return log_msg_ret("buf", -ENOMEM); |
| 157 | num_blks = size >> desc->log2blksz; |
| 158 | log_debug("Reading data, blk=%s, start=%lx, blocks=%lx\n", |
| 159 | bflow->blk->name, (ulong)info.start, num_blks); |
| 160 | ret = blk_read(bflow->blk, (ulong)info.start + 0x80, num_blks, buf); |
| 161 | if (ret != num_blks) |
Simon Glass | 16b1b1c | 2023-07-30 11:16:47 -0600 | [diff] [blame] | 162 | return log_msg_ret("inf", -EIO); |
Simon Glass | d74e62a | 2023-07-12 09:04:45 -0600 | [diff] [blame] | 163 | base = map_to_sysmem(buf); |
| 164 | |
| 165 | setup = base + start - OFFSET_BASE - SETUP_OFFSET; |
| 166 | cmdline = base + start - OFFSET_BASE - CMDLINE_OFFSET; |
| 167 | kern_base = base + start - OFFSET_BASE + SZ_16K; |
| 168 | log_debug("base %lx setup %lx, cmdline %lx, kern_base %lx\n", base, |
| 169 | setup, cmdline, kern_base); |
| 170 | |
| 171 | #ifdef CONFIG_X86 |
| 172 | const char *version; |
| 173 | |
| 174 | version = zimage_get_kernel_version(map_sysmem(setup, 0), |
| 175 | map_sysmem(kern_base, 0)); |
| 176 | log_debug("version %s\n", version); |
| 177 | if (version) |
| 178 | bflow->name = strdup(version); |
| 179 | #endif |
| 180 | if (!bflow->name) |
| 181 | bflow->name = strdup("ChromeOS"); |
| 182 | if (!bflow->name) |
| 183 | return log_msg_ret("nam", -ENOMEM); |
| 184 | bflow->os_name = strdup("ChromeOS"); |
| 185 | if (!bflow->os_name) |
| 186 | return log_msg_ret("os", -ENOMEM); |
| 187 | |
| 188 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
| 189 | uuid = info.uuid; |
| 190 | #endif |
| 191 | ret = copy_cmdline(map_sysmem(cmdline, 0), uuid, &bflow->cmdline); |
| 192 | if (ret) |
| 193 | return log_msg_ret("cmd", ret); |
| 194 | |
| 195 | bflow->state = BOOTFLOWST_READY; |
| 196 | bflow->buf = buf; |
| 197 | bflow->x86_setup = map_sysmem(setup, 0); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int cros_read_file(struct udevice *dev, struct bootflow *bflow, |
| 203 | const char *file_path, ulong addr, ulong *sizep) |
| 204 | { |
| 205 | return -ENOSYS; |
| 206 | } |
| 207 | |
| 208 | static int cros_boot(struct udevice *dev, struct bootflow *bflow) |
| 209 | { |
| 210 | #ifdef CONFIG_X86 |
| 211 | zboot_start(map_to_sysmem(bflow->buf), bflow->size, 0, 0, |
| 212 | map_to_sysmem(bflow->x86_setup), |
| 213 | bflow->cmdline); |
| 214 | #endif |
| 215 | |
| 216 | return log_msg_ret("go", -EFAULT); |
| 217 | } |
| 218 | |
| 219 | static int cros_bootmeth_bind(struct udevice *dev) |
| 220 | { |
| 221 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 222 | |
| 223 | plat->desc = "ChromiumOS boot"; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static struct bootmeth_ops cros_bootmeth_ops = { |
| 229 | .check = cros_check, |
| 230 | .read_bootflow = cros_read_bootflow, |
| 231 | .read_file = cros_read_file, |
| 232 | .boot = cros_boot, |
| 233 | }; |
| 234 | |
| 235 | static const struct udevice_id cros_bootmeth_ids[] = { |
| 236 | { .compatible = "u-boot,cros" }, |
| 237 | { } |
| 238 | }; |
| 239 | |
| 240 | U_BOOT_DRIVER(bootmeth_cros) = { |
| 241 | .name = "bootmeth_cros", |
| 242 | .id = UCLASS_BOOTMETH, |
| 243 | .of_match = cros_bootmeth_ids, |
| 244 | .ops = &cros_bootmeth_ops, |
| 245 | .bind = cros_bootmeth_bind, |
| 246 | }; |