Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 3 | * Bootmethod for extlinux boot from a block device |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
Nam Cao | c39c353 | 2024-11-07 16:01:05 +0100 | [diff] [blame] | 11 | #include <asm/cache.h> |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 12 | #include <bootdev.h> |
| 13 | #include <bootflow.h> |
| 14 | #include <bootmeth.h> |
| 15 | #include <bootstd.h> |
| 16 | #include <command.h> |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 17 | #include <dm.h> |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 18 | #include <extlinux.h> |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 19 | #include <fs.h> |
| 20 | #include <malloc.h> |
| 21 | #include <mapmem.h> |
| 22 | #include <mmc.h> |
| 23 | #include <pxe_utils.h> |
| 24 | |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 25 | struct extlinux_plat { |
| 26 | bool use_fallback; |
| 27 | }; |
| 28 | |
| 29 | enum extlinux_option_type { |
| 30 | EO_FALLBACK, |
| 31 | EO_INVALID |
| 32 | }; |
| 33 | |
| 34 | struct extlinux_option { |
| 35 | char *name; |
| 36 | enum extlinux_option_type option; |
| 37 | }; |
| 38 | |
| 39 | static const struct extlinux_option options[] = { |
| 40 | {"fallback", EO_FALLBACK}, |
| 41 | {NULL, EO_INVALID} |
| 42 | }; |
| 43 | |
| 44 | static enum extlinux_option_type get_option(const char *option) |
| 45 | { |
| 46 | int i = 0; |
| 47 | |
| 48 | while (options[i].name) { |
| 49 | if (!strcmp(options[i].name, option)) |
| 50 | return options[i].option; |
| 51 | |
| 52 | i++; |
| 53 | } |
| 54 | |
| 55 | return EO_INVALID; |
| 56 | }; |
| 57 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 58 | static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize) |
Simon Glass | f6d71a8 | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 59 | { |
| 60 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
| 61 | int len; |
| 62 | |
| 63 | len = snprintf(buf, maxsize, "OK"); |
| 64 | |
| 65 | return len + 1 < maxsize ? 0 : -ENOSPC; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 71 | static int extlinux_getfile(struct pxe_context *ctx, const char *file_path, |
| 72 | char *file_addr, ulong *sizep) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 73 | { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 74 | struct extlinux_info *info = ctx->userdata; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 75 | ulong addr; |
| 76 | int ret; |
| 77 | |
| 78 | addr = simple_strtoul(file_addr, NULL, 16); |
| 79 | |
| 80 | /* Allow up to 1GB */ |
| 81 | *sizep = 1 << 30; |
| 82 | ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr, |
| 83 | sizep); |
| 84 | if (ret) |
| 85 | return log_msg_ret("read", ret); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 90 | static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 91 | { |
| 92 | int ret; |
| 93 | |
| 94 | /* This only works on block devices */ |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 95 | ret = bootflow_iter_check_blk(iter); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 96 | if (ret) |
| 97 | return log_msg_ret("blk", ret); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 102 | /** |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 103 | * extlinux_fill_info() - Decode the extlinux file to find out its info |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 104 | * |
| 105 | * @bflow: Bootflow to process |
| 106 | * @return 0 if OK, -ve on error |
| 107 | */ |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 108 | static int extlinux_fill_info(struct bootflow *bflow) |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 109 | { |
| 110 | struct membuff mb; |
| 111 | char line[200]; |
| 112 | char *data; |
| 113 | int len; |
| 114 | |
| 115 | log_debug("parsing bflow file size %x\n", bflow->size); |
| 116 | membuff_init(&mb, bflow->buf, bflow->size); |
| 117 | membuff_putraw(&mb, bflow->size, true, &data); |
Ion Agorria | 7e06c1f | 2024-01-05 09:22:10 +0200 | [diff] [blame] | 118 | while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) { |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 119 | char *tok, *p = line; |
| 120 | |
| 121 | tok = strsep(&p, " "); |
| 122 | if (p) { |
| 123 | if (!strcmp("label", tok)) { |
| 124 | bflow->os_name = strdup(p); |
| 125 | if (!bflow->os_name) |
| 126 | return log_msg_ret("os", -ENOMEM); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 134 | static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 135 | { |
| 136 | struct blk_desc *desc; |
| 137 | const char *const *prefixes; |
| 138 | struct udevice *bootstd; |
| 139 | const char *prefix; |
| 140 | loff_t size; |
| 141 | int ret, i; |
| 142 | |
| 143 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 144 | if (ret) |
| 145 | return log_msg_ret("std", ret); |
| 146 | |
| 147 | /* If a block device, we require a partition table */ |
| 148 | if (bflow->blk && !bflow->part) |
| 149 | return -ENOENT; |
| 150 | |
| 151 | prefixes = bootstd_get_prefixes(bootstd); |
| 152 | i = 0; |
| 153 | desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL; |
| 154 | do { |
| 155 | prefix = prefixes ? prefixes[i] : NULL; |
| 156 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 157 | ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 158 | } while (ret && prefixes && prefixes[++i]); |
| 159 | if (ret) |
| 160 | return log_msg_ret("try", ret); |
| 161 | size = bflow->size; |
| 162 | |
Nam Cao | c39c353 | 2024-11-07 16:01:05 +0100 | [diff] [blame] | 163 | ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 164 | if (ret) |
| 165 | return log_msg_ret("read", ret); |
| 166 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 167 | ret = extlinux_fill_info(bflow); |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 168 | if (ret) |
| 169 | return log_msg_ret("inf", ret); |
| 170 | |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 174 | static int extlinux_boot(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 175 | { |
| 176 | struct cmd_tbl cmdtp = {}; /* dummy */ |
| 177 | struct pxe_context ctx; |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 178 | struct extlinux_info info; |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 179 | struct extlinux_plat *plat; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 180 | ulong addr; |
| 181 | int ret; |
| 182 | |
| 183 | addr = map_to_sysmem(bflow->buf); |
| 184 | info.dev = dev; |
| 185 | info.bflow = bflow; |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 186 | |
| 187 | plat = dev_get_plat(dev); |
| 188 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 189 | ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true, |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 190 | bflow->fname, false, plat->use_fallback); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 191 | if (ret) |
| 192 | return log_msg_ret("ctx", -EINVAL); |
| 193 | |
| 194 | ret = pxe_process(&ctx, addr, false); |
| 195 | if (ret) |
| 196 | return log_msg_ret("bread", -EINVAL); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 201 | static int extlinux_set_property(struct udevice *dev, const char *property, const char *value) |
| 202 | { |
| 203 | struct extlinux_plat *plat; |
| 204 | static enum extlinux_option_type option; |
| 205 | |
| 206 | plat = dev_get_plat(dev); |
| 207 | |
| 208 | option = get_option(property); |
| 209 | if (option == EO_INVALID) { |
| 210 | printf("Invalid option\n"); |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | switch (option) { |
| 215 | case EO_FALLBACK: |
| 216 | if (!strcmp(value, "1")) { |
| 217 | plat->use_fallback = true; |
| 218 | } else if (!strcmp(value, "0")) { |
| 219 | plat->use_fallback = false; |
| 220 | } else { |
| 221 | printf("Unexpected value '%s'\n", value); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | break; |
| 225 | default: |
| 226 | printf("Unrecognised property '%s'\n", property); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 233 | static int extlinux_bootmeth_bind(struct udevice *dev) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 234 | { |
| 235 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 236 | |
| 237 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 238 | "Extlinux boot from a block device" : "extlinux"; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 243 | static struct bootmeth_ops extlinux_bootmeth_ops = { |
| 244 | .get_state_desc = extlinux_get_state_desc, |
| 245 | .check = extlinux_check, |
| 246 | .read_bootflow = extlinux_read_bootflow, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 247 | .read_file = bootmeth_common_read_file, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 248 | .boot = extlinux_boot, |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 249 | .set_property = extlinux_set_property, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 250 | }; |
| 251 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 252 | static const struct udevice_id extlinux_bootmeth_ids[] = { |
| 253 | { .compatible = "u-boot,extlinux" }, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 254 | { } |
| 255 | }; |
| 256 | |
Simon Glass | e5cb018 | 2024-07-17 09:30:59 +0100 | [diff] [blame] | 257 | /* Put a number before 'extlinux' to provide a default ordering */ |
Simon Glass | 2f27e47 | 2023-08-19 16:49:35 -0600 | [diff] [blame] | 258 | U_BOOT_DRIVER(bootmeth_1extlinux) = { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 259 | .name = "bootmeth_extlinux", |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 260 | .id = UCLASS_BOOTMETH, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 261 | .of_match = extlinux_bootmeth_ids, |
| 262 | .ops = &extlinux_bootmeth_ops, |
| 263 | .bind = extlinux_bootmeth_bind, |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 264 | .plat_auto = sizeof(struct extlinux_plat) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 265 | }; |